]> git.sesse.net Git - vlc/blob - modules/gui/skins2/x11/x11_loop.cpp
f9cd7779d62c89a3227bdc2527943ed9eaab9656
[vlc] / modules / gui / skins2 / x11 / x11_loop.cpp
1 /*****************************************************************************
2  * x11_loop.cpp
3  *****************************************************************************
4  * Copyright (C) 2003 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Cyril Deguet     <asmax@via.ecp.fr>
8  *          Olivier Teulière <ipkiss@via.ecp.fr>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24
25 #ifdef X11_SKINS
26
27 #include <X11/keysym.h>
28 #include "x11_loop.hpp"
29 #include "x11_display.hpp"
30 #include "x11_dragdrop.hpp"
31 #include "x11_factory.hpp"
32 #include "x11_timer.hpp"
33 #include "../src/generic_window.hpp"
34 #include "../src/theme.hpp"
35 #include "../src/window_manager.hpp"
36 #include "../events/evt_focus.hpp"
37 #include "../events/evt_key.hpp"
38 #include "../events/evt_mouse.hpp"
39 #include "../events/evt_motion.hpp"
40 #include "../events/evt_leave.hpp"
41 #include "../events/evt_refresh.hpp"
42 #include "../events/evt_scroll.hpp"
43 #include "../commands/async_queue.hpp"
44 #include "../utils/var_bool.hpp"
45 #include "vlc_keys.h"
46
47
48 // Maximum interval between clicks for a double-click (in microsec)
49 int X11Loop::m_dblClickDelay = 400000;
50
51
52 X11Loop::X11Loop( intf_thread_t *pIntf, X11Display &rDisplay ):
53     OSLoop( pIntf ), m_rDisplay( rDisplay ), m_exit( false ),
54     m_lastClickTime( 0 ), m_lastClickPosX( 0 ), m_lastClickPosY( 0 )
55 {
56     // Initialize the key map
57     keysymToVlcKey[XK_F1] = KEY_F1;
58     keysymToVlcKey[XK_F2] = KEY_F2;
59     keysymToVlcKey[XK_F3] = KEY_F3;
60     keysymToVlcKey[XK_F4] = KEY_F4;
61     keysymToVlcKey[XK_F5] = KEY_F5;
62     keysymToVlcKey[XK_F6] = KEY_F6;
63     keysymToVlcKey[XK_F7] = KEY_F7;
64     keysymToVlcKey[XK_F8] = KEY_F8;
65     keysymToVlcKey[XK_F9] = KEY_F9;
66     keysymToVlcKey[XK_F10] = KEY_F10;
67     keysymToVlcKey[XK_F11] = KEY_F11;
68     keysymToVlcKey[XK_F12] = KEY_F12;
69     keysymToVlcKey[XK_Return] = KEY_ENTER;
70     keysymToVlcKey[XK_space] = KEY_SPACE;
71     keysymToVlcKey[XK_Escape] = KEY_ESC;
72     keysymToVlcKey[XK_Left] = KEY_LEFT;
73     keysymToVlcKey[XK_Right] = KEY_RIGHT;
74     keysymToVlcKey[XK_Up] = KEY_UP;
75     keysymToVlcKey[XK_Down] = KEY_DOWN;
76     keysymToVlcKey[XK_Home] = KEY_HOME;
77     keysymToVlcKey[XK_End] = KEY_END;
78     keysymToVlcKey[XK_Prior] = KEY_PAGEUP;
79     keysymToVlcKey[XK_Next] = KEY_PAGEDOWN;
80     keysymToVlcKey[XK_Delete] = KEY_DELETE;
81     keysymToVlcKey[XK_Insert] = KEY_INSERT;
82 }
83
84
85 X11Loop::~X11Loop()
86 {
87 }
88
89
90 OSLoop *X11Loop::instance( intf_thread_t *pIntf, X11Display &rDisplay )
91 {
92     if( pIntf->p_sys->p_osLoop == NULL )
93     {
94         OSLoop *pOsLoop = new X11Loop( pIntf, rDisplay );
95         pIntf->p_sys->p_osLoop = pOsLoop;
96     }
97     return pIntf->p_sys->p_osLoop;
98 }
99
100
101 void X11Loop::destroy( intf_thread_t *pIntf )
102 {
103     if( pIntf->p_sys->p_osLoop )
104     {
105         delete pIntf->p_sys->p_osLoop;
106         pIntf->p_sys->p_osLoop = NULL;
107     }
108 }
109
110
111 void X11Loop::run()
112 {
113     OSFactory *pOsFactory = OSFactory::instance( getIntf() );
114     X11TimerLoop *pTimerLoop = ((X11Factory*)pOsFactory)->getTimerLoop();
115
116     // Main event loop
117     while( ! m_exit )
118     {
119         int nPending;
120
121         // Number of pending events in the queue
122         nPending = XPending( XDISPLAY );
123
124         while( ! m_exit && nPending > 0 )
125         {
126             // Handle the next X11 event
127             handleX11Event();
128
129             // Number of pending events in the queue
130             nPending = XPending( XDISPLAY );
131         }
132
133         // Wait for the next timer and execute it
134         // The sleep is interrupted if an X11 event is received
135         if( !m_exit )
136         {
137             pTimerLoop->waitNextTimer();
138         }
139     }
140 }
141
142
143 void X11Loop::exit()
144 {
145     m_exit = true;
146 }
147
148
149 void X11Loop::handleX11Event()
150 {
151     XEvent event;
152     OSFactory *pOsFactory = OSFactory::instance( getIntf() );
153
154     // Look for the next event in the queue
155     XNextEvent( XDISPLAY, &event );
156
157     if( event.xany.window == m_rDisplay.getMainWindow() )
158     {
159         if( event.type == MapNotify )
160         {
161             // When the "parent" window is mapped, show all the visible
162             // windows, as it is not automatic, unfortunately
163             Theme *pTheme = getIntf()->p_sys->p_theme;
164             if( pTheme )
165             {
166                 pTheme->getWindowManager().synchVisibility();
167             }
168         }
169         return;
170     }
171
172     // Find the window to which the event is sent
173     GenericWindow *pWin =
174         ((X11Factory*)pOsFactory)->m_windowMap[event.xany.window];
175
176     if( !pWin )
177     {
178         return;
179     }
180
181     // Send the right event object to the window
182     switch( event.type )
183     {
184         case Expose:
185         {
186             EvtRefresh evt( getIntf(), event.xexpose.x,
187                             event.xexpose.y, event.xexpose.width,
188                             event.xexpose.height );
189             pWin->processEvent( evt );
190             break;
191         }
192         case FocusIn:
193         {
194             EvtFocus evt( getIntf(), true );
195             pWin->processEvent( evt );
196             break;
197         }
198         case FocusOut:
199         {
200             EvtFocus evt( getIntf(), false );
201             pWin->processEvent( evt );
202             break;
203         }
204
205         case MotionNotify:
206         {
207             // Don't trust the position in the event, it is
208             // out of date. Get the actual current position instead
209             int x, y;
210             pOsFactory->getMousePos( x, y );
211             EvtMotion evt( getIntf(), x, y );
212             pWin->processEvent( evt );
213             break;
214         }
215         case LeaveNotify:
216         {
217             EvtLeave evt( getIntf() );
218             pWin->processEvent( evt );
219             break;
220         }
221         case ButtonPress:
222         case ButtonRelease:
223         {
224             EvtMouse::ActionType_t action = EvtMouse::kDown;
225             switch( event.type )
226             {
227                 case ButtonPress:
228                     action = EvtMouse::kDown;
229                     break;
230                 case ButtonRelease:
231                     action = EvtMouse::kUp;
232                     break;
233             }
234
235             // Get the modifiers
236             int mod = EvtInput::kModNone;
237             if( event.xbutton.state & Mod1Mask )
238             {
239                 mod |= EvtInput::kModAlt;
240             }
241             if( event.xbutton.state & ControlMask )
242             {
243                 mod |= EvtInput::kModCtrl;
244             }
245             if( event.xbutton.state & ShiftMask )
246             {
247                 mod |= EvtInput::kModShift;
248             }
249
250             // Check for double clicks
251             if( event.type == ButtonPress &&
252                 event.xbutton.button == 1 )
253             {
254                 mtime_t time = mdate();
255                 int x, y;
256                 pOsFactory->getMousePos( x, y );
257                 if( time - m_lastClickTime < m_dblClickDelay &&
258                     x == m_lastClickPosX && y == m_lastClickPosY )
259                 {
260                     m_lastClickTime = 0;
261                     action = EvtMouse::kDblClick;
262                 }
263                 else
264                 {
265                     m_lastClickTime = time;
266                     m_lastClickPosX = x;
267                     m_lastClickPosY = y;
268                 }
269             }
270
271             switch( event.xbutton.button )
272             {
273                 case 1:
274                 {
275                     EvtMouse evt( getIntf(), event.xbutton.x,
276                                   event.xbutton.y, EvtMouse::kLeft,
277                                   action, mod );
278                     pWin->processEvent( evt );
279                     break;
280                 }
281                 case 2:
282                 {
283                     EvtMouse evt( getIntf(), event.xbutton.x,
284                                   event.xbutton.y, EvtMouse::kMiddle,
285                                   action, mod );
286                     pWin->processEvent( evt );
287                     break;
288                 }
289                 case 3:
290                 {
291                     EvtMouse evt( getIntf(), event.xbutton.x,
292                                   event.xbutton.y, EvtMouse::kRight,
293                                   action, mod );
294                     pWin->processEvent( evt );
295                     break;
296                 }
297                 case 4:
298                 {
299                     // Scroll up
300                     EvtScroll evt( getIntf(), event.xbutton.x,
301                                    event.xbutton.y, EvtScroll::kUp,
302                                    mod );
303                     pWin->processEvent( evt );
304                     break;
305                 }
306                 case 5:
307                 {
308                     // Scroll down
309                     EvtScroll evt( getIntf(), event.xbutton.x,
310                                    event.xbutton.y, EvtScroll::kDown,
311                                    mod );
312                     pWin->processEvent( evt );
313                     break;
314                 }
315             }
316             break;
317         }
318         case KeyPress:
319         case KeyRelease:
320         {
321             EvtKey::ActionType_t action = EvtKey::kDown;
322             int mod = EvtInput::kModNone;
323             // Get the modifiers
324             if( event.xkey.state & Mod1Mask )
325             {
326                 mod |= EvtInput::kModAlt;
327             }
328             if( event.xkey.state & ControlMask )
329             {
330                 mod |= EvtInput::kModCtrl;
331             }
332             if( event.xkey.state & ShiftMask )
333             {
334                 mod |= EvtInput::kModShift;
335             }
336
337             // Take the first keysym = lower case character
338             KeySym keysym = XLookupKeysym( &event.xkey, 0 );
339
340             // Get VLC key code from the keysym
341             int key = keysymToVlcKey[keysym];
342             if( !key )
343             {
344                 // Normal key
345                 key = keysym;
346             }
347
348             switch( event.type )
349             {
350                 case KeyPress:
351                     action = EvtKey::kDown;
352                     break;
353                 case KeyRelease:
354                     action = EvtKey::kUp;
355                     break;
356             }
357             EvtKey evt( getIntf(), key, action, mod );
358             pWin->processEvent( evt );
359             break;
360         }
361
362         case ClientMessage:
363         {
364             // Get the message type
365             string type = XGetAtomName( XDISPLAY, event.xclient.message_type );
366
367             // Find the DnD object for this window
368             X11DragDrop *pDnd =
369                 ((X11Factory*)pOsFactory)->m_dndMap[event.xany.window];
370             if( !pDnd )
371             {
372                 msg_Err( getIntf(), "no associated D&D object" );
373                 return;
374             }
375
376             if( type == "XdndEnter" )
377             {
378                 pDnd->dndEnter( event.xclient.data.l );
379             }
380             else if( type == "XdndPosition" )
381             {
382                 pDnd->dndPosition( event.xclient.data.l );
383             }
384             else if( type == "XdndLeave" )
385             {
386                 pDnd->dndLeave( event.xclient.data.l );
387             }
388             else if( type == "XdndDrop" )
389             {
390                 pDnd->dndDrop( event.xclient.data.l );
391             }
392             break;
393         }
394     }
395 }
396
397 #endif