]> git.sesse.net Git - vlc/blob - modules/gui/skins2/x11/x11_loop.cpp
KEY_SPACE = 32, simplify several outputs and interfaces
[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_Escape] = KEY_ESC;
71     keysymToVlcKey[XK_Left] = KEY_LEFT;
72     keysymToVlcKey[XK_Right] = KEY_RIGHT;
73     keysymToVlcKey[XK_Up] = KEY_UP;
74     keysymToVlcKey[XK_Down] = KEY_DOWN;
75     keysymToVlcKey[XK_Home] = KEY_HOME;
76     keysymToVlcKey[XK_End] = KEY_END;
77     keysymToVlcKey[XK_Prior] = KEY_PAGEUP;
78     keysymToVlcKey[XK_Next] = KEY_PAGEDOWN;
79     keysymToVlcKey[XK_Delete] = KEY_DELETE;
80     keysymToVlcKey[XK_Insert] = KEY_INSERT;
81 }
82
83
84 X11Loop::~X11Loop()
85 {
86 }
87
88
89 OSLoop *X11Loop::instance( intf_thread_t *pIntf, X11Display &rDisplay )
90 {
91     if( pIntf->p_sys->p_osLoop == NULL )
92     {
93         OSLoop *pOsLoop = new X11Loop( pIntf, rDisplay );
94         pIntf->p_sys->p_osLoop = pOsLoop;
95     }
96     return pIntf->p_sys->p_osLoop;
97 }
98
99
100 void X11Loop::destroy( intf_thread_t *pIntf )
101 {
102     delete pIntf->p_sys->p_osLoop;
103     pIntf->p_sys->p_osLoop = NULL;
104 }
105
106
107 void X11Loop::run()
108 {
109     OSFactory *pOsFactory = OSFactory::instance( getIntf() );
110     X11TimerLoop *pTimerLoop = ((X11Factory*)pOsFactory)->getTimerLoop();
111
112     // Main event loop
113     while( ! m_exit )
114     {
115         int nPending;
116
117         // Number of pending events in the queue
118         nPending = XPending( XDISPLAY );
119
120         while( ! m_exit && nPending > 0 )
121         {
122             // Handle the next X11 event
123             handleX11Event();
124
125             // Number of pending events in the queue
126             nPending = XPending( XDISPLAY );
127         }
128
129         // Wait for the next timer and execute it
130         // The sleep is interrupted if an X11 event is received
131         if( !m_exit )
132         {
133             pTimerLoop->waitNextTimer();
134         }
135     }
136 }
137
138
139 void X11Loop::exit()
140 {
141     m_exit = true;
142 }
143
144
145 void X11Loop::handleX11Event()
146 {
147     XEvent event;
148     OSFactory *pOsFactory = OSFactory::instance( getIntf() );
149
150     // Look for the next event in the queue
151     XNextEvent( XDISPLAY, &event );
152
153     if( event.xany.window == m_rDisplay.getMainWindow() )
154     {
155         if( event.type == MapNotify )
156         {
157             // When the "parent" window is mapped, show all the visible
158             // windows, as it is not automatic, unfortunately
159             Theme *pTheme = getIntf()->p_sys->p_theme;
160             if( pTheme )
161             {
162                 // Commented out as it really doesn't seem useful
163                 // but rather brings visible problems
164                 // pTheme->getWindowManager().synchVisibility();
165             }
166         }
167         return;
168     }
169
170     // Find the window to which the event is sent
171     GenericWindow *pWin =
172         ((X11Factory*)pOsFactory)->m_windowMap[event.xany.window];
173
174     if( !pWin )
175     {
176         return;
177     }
178
179     // Send the right event object to the window
180     switch( event.type )
181     {
182         case Expose:
183         {
184             EvtRefresh evt( getIntf(), event.xexpose.x,
185                             event.xexpose.y, event.xexpose.width,
186                             event.xexpose.height );
187             pWin->processEvent( evt );
188             break;
189         }
190         case FocusIn:
191         {
192             EvtFocus evt( getIntf(), true );
193             pWin->processEvent( evt );
194             break;
195         }
196         case FocusOut:
197         {
198             EvtFocus evt( getIntf(), false );
199             pWin->processEvent( evt );
200             break;
201         }
202
203         case MotionNotify:
204         {
205             // Don't trust the position in the event, it is
206             // out of date. Get the actual current position instead
207             int x, y;
208             pOsFactory->getMousePos( x, y );
209             EvtMotion evt( getIntf(), x, y );
210             pWin->processEvent( evt );
211             break;
212         }
213         case LeaveNotify:
214         {
215             EvtLeave evt( getIntf() );
216             pWin->processEvent( evt );
217             break;
218         }
219         case ButtonPress:
220         case ButtonRelease:
221         {
222             EvtMouse::ActionType_t action = EvtMouse::kDown;
223             switch( event.type )
224             {
225                 case ButtonPress:
226                     action = EvtMouse::kDown;
227                     break;
228                 case ButtonRelease:
229                     action = EvtMouse::kUp;
230                     break;
231             }
232
233             // Get the modifiers
234             int mod = EvtInput::kModNone;
235             if( event.xbutton.state & Mod1Mask )
236             {
237                 mod |= EvtInput::kModAlt;
238             }
239             if( event.xbutton.state & ControlMask )
240             {
241                 mod |= EvtInput::kModCtrl;
242             }
243             if( event.xbutton.state & ShiftMask )
244             {
245                 mod |= EvtInput::kModShift;
246             }
247
248             // Check for double clicks
249             if( event.type == ButtonPress &&
250                 event.xbutton.button == 1 )
251             {
252                 mtime_t time = mdate();
253                 int x, y;
254                 pOsFactory->getMousePos( x, y );
255                 if( time - m_lastClickTime < m_dblClickDelay &&
256                     x == m_lastClickPosX && y == m_lastClickPosY )
257                 {
258                     m_lastClickTime = 0;
259                     action = EvtMouse::kDblClick;
260                 }
261                 else
262                 {
263                     m_lastClickTime = time;
264                     m_lastClickPosX = x;
265                     m_lastClickPosY = y;
266                 }
267             }
268
269             switch( event.xbutton.button )
270             {
271                 case 1:
272                 {
273                     EvtMouse evt( getIntf(), event.xbutton.x,
274                                   event.xbutton.y, EvtMouse::kLeft,
275                                   action, mod );
276                     pWin->processEvent( evt );
277                     break;
278                 }
279                 case 2:
280                 {
281                     EvtMouse evt( getIntf(), event.xbutton.x,
282                                   event.xbutton.y, EvtMouse::kMiddle,
283                                   action, mod );
284                     pWin->processEvent( evt );
285                     break;
286                 }
287                 case 3:
288                 {
289                     EvtMouse evt( getIntf(), event.xbutton.x,
290                                   event.xbutton.y, EvtMouse::kRight,
291                                   action, mod );
292                     pWin->processEvent( evt );
293                     break;
294                 }
295                 case 4:
296                 {
297                     // Scroll up
298                     EvtScroll evt( getIntf(), event.xbutton.x,
299                                    event.xbutton.y, EvtScroll::kUp,
300                                    mod );
301                     pWin->processEvent( evt );
302                     break;
303                 }
304                 case 5:
305                 {
306                     // Scroll down
307                     EvtScroll evt( getIntf(), event.xbutton.x,
308                                    event.xbutton.y, EvtScroll::kDown,
309                                    mod );
310                     pWin->processEvent( evt );
311                     break;
312                 }
313             }
314             break;
315         }
316         case KeyPress:
317         case KeyRelease:
318         {
319             EvtKey::ActionType_t action = EvtKey::kDown;
320             int mod = EvtInput::kModNone;
321             // Get the modifiers
322             if( event.xkey.state & Mod1Mask )
323             {
324                 mod |= EvtInput::kModAlt;
325             }
326             if( event.xkey.state & ControlMask )
327             {
328                 mod |= EvtInput::kModCtrl;
329             }
330             if( event.xkey.state & ShiftMask )
331             {
332                 mod |= EvtInput::kModShift;
333             }
334
335             // Take the first keysym = lower case character
336             KeySym keysym = XLookupKeysym( &event.xkey, 0 );
337
338             // Get VLC key code from the keysym
339             int key = keysymToVlcKey[keysym];
340             if( !key )
341             {
342                 // Normal key
343                 key = keysym;
344             }
345
346             switch( event.type )
347             {
348                 case KeyPress:
349                     action = EvtKey::kDown;
350                     break;
351                 case KeyRelease:
352                     action = EvtKey::kUp;
353                     break;
354             }
355             EvtKey evt( getIntf(), key, action, mod );
356             pWin->processEvent( evt );
357             break;
358         }
359
360         case ClientMessage:
361         {
362             // Get the message type
363             string type = XGetAtomName( XDISPLAY, event.xclient.message_type );
364
365             // Find the DnD object for this window
366             X11DragDrop *pDnd =
367                 ((X11Factory*)pOsFactory)->m_dndMap[event.xany.window];
368             if( !pDnd )
369             {
370                 msg_Err( getIntf(), "no associated D&D object" );
371                 return;
372             }
373
374             if( type == "XdndEnter" )
375             {
376                 pDnd->dndEnter( event.xclient.data.l );
377             }
378             else if( type == "XdndPosition" )
379             {
380                 pDnd->dndPosition( event.xclient.data.l );
381             }
382             else if( type == "XdndLeave" )
383             {
384                 pDnd->dndLeave( event.xclient.data.l );
385             }
386             else if( type == "XdndDrop" )
387             {
388                 pDnd->dndDrop( event.xclient.data.l );
389             }
390             break;
391         }
392     }
393 }
394
395 #endif