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