]> git.sesse.net Git - vlc/blob - modules/control/globalhotkeys/x11.c
Trailing ;
[vlc] / modules / control / globalhotkeys / x11.c
1 /*****************************************************************************
2  * x11.c: Global-Hotkey X11 handling for vlc
3  *****************************************************************************
4  * Copyright (C) 2009 the VideoLAN team
5  *
6  * Authors: Laurent Aimar <fenrir _AT_ videolan _DOT_ org>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
21  *****************************************************************************/
22
23 #ifdef HAVE_CONFIG_H
24 # include "config.h"
25 #endif
26 #include <X11/Xlib.h>
27 #include <X11/keysym.h>
28 #include <X11/Xutil.h>
29 #include <X11/XF86keysym.h>
30 #include <poll.h>
31
32 #include <vlc_common.h>
33 #include <vlc_plugin.h>
34 #include <vlc_interface.h>
35 #include <vlc_keys.h>
36
37 /*****************************************************************************
38  * Local prototypes
39  *****************************************************************************/
40 static int Open( vlc_object_t *p_this );
41 static void Close( vlc_object_t *p_this );
42
43 /*****************************************************************************
44  * Module descriptor
45  *****************************************************************************/
46 vlc_module_begin()
47     set_shortname( _("Global Hotkeys") )
48     set_category( CAT_INTERFACE )
49     set_subcategory( SUBCAT_INTERFACE_HOTKEYS )
50     set_description( _("Global Hotkeys interface") )
51     set_capability( "interface", 0 )
52     set_callbacks( Open, Close )
53 vlc_module_end()
54
55 typedef struct
56 {
57     KeyCode     i_x11;
58     unsigned    i_modifier;
59     int         i_action;
60 } hotkey_mapping_t;
61
62 struct intf_sys_t
63 {
64     vlc_thread_t thread;
65     Display *p_display;
66
67     int              i_map;
68     hotkey_mapping_t *p_map;
69 };
70
71 static void Mapping( intf_thread_t *p_intf );
72 static void Register( intf_thread_t *p_intf );
73 static void Unregister( intf_thread_t *p_intf );
74 static void *Thread( void *p_data );
75 static int X11ErrorHandler( Display *, XErrorEvent * );
76
77 /*****************************************************************************
78  * Open:
79  *****************************************************************************/
80 static int Open( vlc_object_t *p_this )
81 {
82     intf_thread_t *p_intf = (intf_thread_t *)p_this;
83     intf_sys_t *p_sys;
84
85     Display *p_display = XOpenDisplay( NULL );
86     if( !p_display )
87         return VLC_EGENERIC;
88     XSetErrorHandler( X11ErrorHandler );
89
90     p_intf->p_sys = p_sys = malloc( sizeof(*p_sys) );
91     if( !p_sys )
92     {
93         XCloseDisplay( p_display );
94         return VLC_ENOMEM;
95     }
96     p_sys->p_display = p_display;
97     Mapping( p_intf );
98     Register( p_intf );
99
100     if( vlc_clone( &p_sys->thread, Thread, p_intf, VLC_THREAD_PRIORITY_LOW ) )
101     {
102         Unregister( p_intf );
103         XCloseDisplay( p_display );
104         free( p_sys->p_map );
105         free( p_sys );
106         return VLC_ENOMEM;
107     }
108     return VLC_SUCCESS;
109 }
110
111 /*****************************************************************************
112  * Close:
113  *****************************************************************************/
114 static void Close( vlc_object_t *p_this )
115 {
116     intf_thread_t *p_intf = (intf_thread_t *)p_this;
117     intf_sys_t *p_sys = p_intf->p_sys;
118
119     vlc_cancel( p_sys->thread );
120     vlc_join( p_sys->thread, NULL );
121
122     Unregister( p_intf );
123     XCloseDisplay( p_sys->p_display );
124     free( p_sys->p_map );
125
126     free( p_sys );
127 }
128
129 /*****************************************************************************
130  *
131  *****************************************************************************/
132 static int X11ErrorHandler( Display *p_display, XErrorEvent *p_event )
133 {
134 #if 0
135     char psz_txt[1024];
136
137     XGetErrorText( p_display, p_event->error_code, psz_txt, sizeof(psz_txt) );
138     fprintf( stderr,
139              "[????????] globalhotkeys interface error: X11 request %u.%u failed "
140               "with error code %u:\n %s\n",
141              p_event->request_code, p_event->minor_code, p_event->error_code, psz_txt );
142 #else
143     VLC_UNUSED(p_display); VLC_UNUSED(p_event);
144 #endif
145     /* Ignore them */
146     return 0;
147 }
148 static unsigned GetModifier( Display *p_display, KeySym sym )
149 {
150     static const unsigned pi_mask[8] = {
151         ShiftMask, LockMask, ControlMask, Mod1Mask,
152         Mod2Mask, Mod3Mask, Mod4Mask, Mod5Mask
153     };
154
155     const KeyCode key = XKeysymToKeycode( p_display, sym );
156     if( key == 0 )
157         return 0;
158
159     XModifierKeymap *p_map = XGetModifierMapping( p_display );
160     if( !p_map )
161         return 0;
162
163     unsigned i_mask = 0;
164     for( int i = 0; i < 8 * p_map->max_keypermod; i++ )
165     {
166         if( p_map->modifiermap[i] == key )
167         {
168             i_mask = pi_mask[i / p_map->max_keypermod];
169             break;
170         }
171     }
172
173     XFreeModifiermap( p_map );
174     return i_mask;
175 }
176 static unsigned GetX11Modifier( Display *p_display, unsigned i_vlc )
177 {
178     unsigned i_mask = 0;
179
180     if( i_vlc & KEY_MODIFIER_ALT )
181         i_mask |= GetModifier( p_display, XK_Alt_L ) |
182                   GetModifier( p_display, XK_Alt_R );
183     if( i_vlc & KEY_MODIFIER_CTRL )
184         i_mask |= GetModifier( p_display, XK_Control_L ) |
185                   GetModifier( p_display, XK_Control_R );
186     if( i_vlc & KEY_MODIFIER_SHIFT )
187         i_mask |= GetModifier( p_display, XK_Shift_L ) |
188                   GetModifier( p_display, XK_Shift_R );
189     return i_mask;
190 }
191
192 /* FIXME this table is also used by the vout */
193 static const struct
194 {
195     KeySym   i_x11;
196     unsigned i_vlc;
197
198 } x11keys_to_vlckeys[] =
199 {
200     { XK_F1, KEY_F1 }, { XK_F2, KEY_F2 }, { XK_F3, KEY_F3 }, { XK_F4, KEY_F4 },
201     { XK_F5, KEY_F5 }, { XK_F6, KEY_F6 }, { XK_F7, KEY_F7 }, { XK_F8, KEY_F8 },
202     { XK_F9, KEY_F9 }, { XK_F10, KEY_F10 }, { XK_F11, KEY_F11 },
203     { XK_F12, KEY_F12 },
204
205     { XK_Return, KEY_ENTER },
206     { XK_KP_Enter, KEY_ENTER },
207     { XK_space, KEY_SPACE },
208     { XK_Escape, KEY_ESC },
209
210     { XK_Menu, KEY_MENU },
211     { XK_Left, KEY_LEFT },
212     { XK_Right, KEY_RIGHT },
213     { XK_Up, KEY_UP },
214     { XK_Down, KEY_DOWN },
215
216     { XK_Home, KEY_HOME },
217     { XK_End, KEY_END },
218     { XK_Page_Up, KEY_PAGEUP },
219     { XK_Page_Down, KEY_PAGEDOWN },
220
221     { XK_Insert, KEY_INSERT },
222     { XK_Delete, KEY_DELETE },
223     { XF86XK_AudioNext, KEY_MEDIA_NEXT_TRACK},
224     { XF86XK_AudioPrev, KEY_MEDIA_PREV_TRACK},
225     { XF86XK_AudioMute, KEY_VOLUME_MUTE },
226     { XF86XK_AudioLowerVolume, KEY_VOLUME_DOWN },
227     { XF86XK_AudioRaiseVolume, KEY_VOLUME_UP },
228     { XF86XK_AudioPlay, KEY_MEDIA_PLAY_PAUSE },
229     { XF86XK_AudioPause, KEY_MEDIA_PLAY_PAUSE },
230
231     { 0, 0 }
232 };
233 static KeySym GetX11Key( unsigned i_vlc )
234 {
235     for( int i = 0; x11keys_to_vlckeys[i].i_vlc != 0; i++ )
236     {
237         if( x11keys_to_vlckeys[i].i_vlc == i_vlc )
238             return x11keys_to_vlckeys[i].i_x11;
239     }
240
241     char psz_key[2];
242     psz_key[0] = i_vlc;
243     psz_key[1] = '\0';
244     return XStringToKeysym( psz_key );
245 }
246
247 static void Mapping( intf_thread_t *p_intf )
248 {
249     static const KeySym p_x11_modifier_ignored[] = {
250         0,
251         XK_Num_Lock,
252         XK_Scroll_Lock,
253         XK_Caps_Lock,
254     };
255
256     intf_sys_t *p_sys = p_intf->p_sys;
257
258     p_sys->i_map = 0;
259     p_sys->p_map = NULL;
260
261     /* Registering of Hotkeys */
262     for( struct hotkey *p_hotkey = p_intf->p_libvlc->p_hotkeys;
263             p_hotkey->psz_action != NULL;
264             p_hotkey++ )
265     {
266         char *psz_hotkey;
267         if( asprintf( &psz_hotkey, "global-%s", p_hotkey->psz_action ) < 0 )
268             break;
269
270         const int i_vlc_action = p_hotkey->i_action;
271         const int i_vlc_key = config_GetInt( p_intf, psz_hotkey );
272
273         free( psz_hotkey );
274
275         if( !i_vlc_key )
276             continue;
277
278         const KeyCode key = XKeysymToKeycode( p_sys->p_display,
279                                               GetX11Key( i_vlc_key & ~KEY_MODIFIER ) );
280         const unsigned i_modifier = GetX11Modifier( p_sys->p_display, i_vlc_key & KEY_MODIFIER );
281
282         for( int j = 0; j < sizeof(p_x11_modifier_ignored)/sizeof(*p_x11_modifier_ignored); j++ )
283         {
284             const unsigned i_ignored = GetModifier( p_sys->p_display, p_x11_modifier_ignored[j] );
285             if( j != 0 && i_ignored == 0x00)
286                 continue;
287
288             hotkey_mapping_t *p_map_old = p_sys->p_map;
289             p_sys->p_map = realloc( p_sys->p_map, sizeof(*p_sys->p_map) * (p_sys->i_map+1) );
290             if( !p_sys->p_map )
291             {
292                 p_sys->p_map = p_map_old;
293                 break;
294             }
295             hotkey_mapping_t *p_map = &p_sys->p_map[p_sys->i_map++];
296
297             p_map->i_x11 = key;
298             p_map->i_modifier = i_modifier|i_ignored;
299             p_map->i_action = i_vlc_action;
300         }
301     }
302 }
303
304 static void Register( intf_thread_t *p_intf )
305 {
306     intf_sys_t *p_sys = p_intf->p_sys;
307
308     for( int i = 0; i < p_sys->i_map; i++ )
309     {
310         hotkey_mapping_t *p_map = &p_sys->p_map[i];
311         XGrabKey( p_sys->p_display, p_map->i_x11, p_map->i_modifier,
312                   DefaultRootWindow( p_sys->p_display ), True, GrabModeAsync, GrabModeAsync );
313     }
314 }
315 static void Unregister( intf_thread_t *p_intf )
316 {
317     intf_sys_t *p_sys = p_intf->p_sys;
318
319     for( int i = 0; i < p_sys->i_map; i++ )
320     {
321         hotkey_mapping_t *p_map = &p_sys->p_map[i];
322         XUngrabKey( p_sys->p_display, p_map->i_x11, p_map->i_modifier,
323                     DefaultRootWindow( p_sys->p_display ) );
324     }
325 }
326
327 static void *Thread( void *p_data )
328 {
329     intf_thread_t *p_intf = p_data;
330     intf_sys_t *p_sys = p_intf->p_sys;
331     Display *p_display = p_sys->p_display;
332
333     int canc = vlc_savecancel();
334
335     if( !p_display )
336         return NULL;
337
338     /* */
339     XFlush( p_display );
340
341     /* */
342     int fd = ConnectionNumber( p_display );
343     for( ;; )
344     {
345         /* Wait for x11 event */
346         vlc_restorecancel( canc );
347         struct pollfd fds = { .fd = fd, .events = POLLIN, };
348         if( poll( &fds, 1, -1 ) < 0 )
349         {
350             if( errno != EINTR )
351                 break;
352             canc = vlc_savecancel();
353             continue;
354         }
355         canc = vlc_savecancel();
356
357         while( XPending( p_display ) > 0 )
358         {
359             XEvent e;
360
361             XNextEvent( p_display, &e );
362             if( e.type != KeyPress )
363                 continue;
364
365             for( int i = 0; i < p_sys->i_map; i++ )
366             {
367                 hotkey_mapping_t *p_map = &p_sys->p_map[i];
368
369                 if( p_map->i_x11 == e.xkey.keycode &&
370                     p_map->i_modifier == e.xkey.state )
371                 {
372                     var_SetInteger( p_intf->p_libvlc, "key-action", p_map->i_action );
373                     break;
374                 }
375             }
376         }
377     }
378
379     return NULL;
380 }
381