]> git.sesse.net Git - vlc/blob - modules/control/globalhotkeys/xcb.c
fix a segfault when launching a video inside a tty.
[vlc] / modules / control / globalhotkeys / xcb.c
1 /*****************************************************************************
2  * xcb.c: Global-Hotkey X11 using xcb 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 <vlc_common.h>
27 #include <vlc_plugin.h>
28 #include <vlc_interface.h>
29 #include <vlc_keys.h>
30 #include <ctype.h>
31
32 #include <xcb/xcb.h>
33 #include <xcb/xcb_keysyms.h>
34 #include <X11/keysym.h>
35 #include <X11/XF86keysym.h>
36
37 #include <poll.h>
38
39 /*****************************************************************************
40  * Local prototypes
41  *****************************************************************************/
42 static int Open( vlc_object_t *p_this );
43 static void Close( vlc_object_t *p_this );
44
45 /*****************************************************************************
46  * Module descriptor
47  *****************************************************************************/
48 vlc_module_begin()
49     set_shortname( N_("Global Hotkeys") )
50     set_category( CAT_INTERFACE )
51     set_subcategory( SUBCAT_INTERFACE_HOTKEYS )
52     set_description( N_("Global Hotkeys interface") )
53     set_capability( "interface", 0 )
54     set_callbacks( Open, Close )
55 vlc_module_end()
56
57 typedef struct
58 {
59     xcb_keycode_t i_x11;
60     unsigned      i_modifier;
61     int           i_action;
62 } hotkey_mapping_t;
63
64 struct intf_sys_t
65 {
66     vlc_thread_t thread;
67
68     xcb_connection_t  *p_connection;
69     xcb_window_t      root;
70     xcb_key_symbols_t *p_symbols;
71
72     int              i_map;
73     hotkey_mapping_t *p_map;
74 };
75
76 static void Mapping( intf_thread_t *p_intf );
77 static void Register( intf_thread_t *p_intf );
78 static void Unregister( intf_thread_t *p_intf );
79 static void *Thread( void *p_data );
80
81 /*****************************************************************************
82  * Open:
83  *****************************************************************************/
84 static int Open( vlc_object_t *p_this )
85 {
86     intf_thread_t *p_intf = (intf_thread_t *)p_this;
87     intf_sys_t *p_sys;
88
89     p_intf->p_sys = p_sys = calloc( 1, sizeof(*p_sys) );
90     if( !p_sys )
91         return VLC_ENOMEM;
92
93     char *psz_display = var_CreateGetNonEmptyString( p_intf, "x11-display" );
94
95     int i_screen_default;
96     p_sys->p_connection = xcb_connect( psz_display, &i_screen_default );
97     free( psz_display );
98
99     if( !p_sys->p_connection )
100         goto error;
101
102     /* Get the root windows of the default screen */
103     memset( &p_sys->root, 0, sizeof( p_sys->root ) );
104
105     const xcb_setup_t* xcbsetup = xcb_get_setup( p_sys->p_connection );
106     if( !xcbsetup )
107         goto error;
108     xcb_screen_iterator_t iter = xcb_setup_roots_iterator( xcbsetup );
109     for( int i = 0; i < i_screen_default; i++ )
110     {
111         if( !iter.rem )
112             break;
113         xcb_screen_next( &iter );
114     }
115     if( !iter.rem )
116         goto error;
117     p_sys->root = iter.data->root;
118
119     /* */
120     p_sys->p_symbols = xcb_key_symbols_alloc( p_sys->p_connection ); // FIXME
121     if( !p_sys->p_symbols )
122         goto error;
123
124     Mapping( p_intf );
125     Register( p_intf );
126
127     if( vlc_clone( &p_sys->thread, Thread, p_intf, VLC_THREAD_PRIORITY_LOW ) )
128     {
129         Unregister( p_intf );
130         free( p_sys->p_map );
131         goto error;
132     }
133     return VLC_SUCCESS;
134
135 error:
136     if( p_sys->p_symbols )
137         xcb_key_symbols_free( p_sys->p_symbols );
138     if( p_sys->p_connection )
139         xcb_disconnect( p_sys->p_connection );
140     free( p_sys );
141     return VLC_EGENERIC;
142 }
143
144 /*****************************************************************************
145  * Close:
146  *****************************************************************************/
147 static void Close( vlc_object_t *p_this )
148 {
149     intf_thread_t *p_intf = (intf_thread_t *)p_this;
150     intf_sys_t *p_sys = p_intf->p_sys;
151
152     vlc_cancel( p_sys->thread );
153     vlc_join( p_sys->thread, NULL );
154
155     Unregister( p_intf );
156     free( p_sys->p_map );
157
158     xcb_key_symbols_free( p_sys->p_symbols );
159     xcb_disconnect( p_sys->p_connection );
160     free( p_sys );
161 }
162
163 /*****************************************************************************
164  *
165  *****************************************************************************/
166 static unsigned GetModifier( xcb_connection_t *p_connection, xcb_key_symbols_t *p_symbols, xcb_keysym_t sym )
167 {
168     static const unsigned pi_mask[8] = {
169         XCB_MOD_MASK_SHIFT, XCB_MOD_MASK_LOCK, XCB_MOD_MASK_CONTROL, XCB_MOD_MASK_1,
170         XCB_MOD_MASK_2, XCB_MOD_MASK_3, XCB_MOD_MASK_4, XCB_MOD_MASK_5
171     };
172
173     const xcb_keycode_t key = xcb_key_symbols_get_keycode( p_symbols, sym );
174     if( key == 0 )
175         return 0;
176
177     xcb_get_modifier_mapping_cookie_t r = xcb_get_modifier_mapping( p_connection );
178     xcb_get_modifier_mapping_reply_t *p_map = xcb_get_modifier_mapping_reply( p_connection, r, NULL );
179     if( !p_map )
180         return 0;
181
182     xcb_keycode_t *p_keycode = xcb_get_modifier_mapping_keycodes( p_map );
183     if( !p_keycode )
184         return 0;
185
186     unsigned i_mask = 0;
187     for( int i = 0; i < 8; i++ )
188     {
189         for( int j = 0; j < p_map->keycodes_per_modifier; j++ )
190         {
191             if( p_keycode[i * p_map->keycodes_per_modifier + j] == key )
192                 i_mask = pi_mask[i];
193         }
194     }
195
196     free( p_map ); // FIXME to check
197     return i_mask;
198 }
199 static unsigned GetX11Modifier( xcb_connection_t *p_connection, xcb_key_symbols_t *p_symbols, unsigned i_vlc )
200 {
201     unsigned i_mask = 0;
202
203     if( i_vlc & KEY_MODIFIER_ALT )
204         i_mask |= GetModifier( p_connection, p_symbols, XK_Alt_L ) |
205                   GetModifier( p_connection, p_symbols, XK_Alt_R );
206     if( i_vlc & KEY_MODIFIER_CTRL )
207         i_mask |= GetModifier( p_connection, p_symbols, XK_Control_L ) |
208                   GetModifier( p_connection, p_symbols, XK_Control_R );
209     if( i_vlc & KEY_MODIFIER_SHIFT )
210         i_mask |= GetModifier( p_connection, p_symbols, XK_Shift_L ) |
211                   GetModifier( p_connection, p_symbols, XK_Shift_R );
212     return i_mask;
213 }
214
215 /* FIXME this table is also used by the vout */
216 static const struct
217 {
218     xcb_keysym_t i_x11;
219     unsigned     i_vlc;
220
221 } x11keys_to_vlckeys[] =
222 {
223     { XK_F1, KEY_F1 }, { XK_F2, KEY_F2 }, { XK_F3, KEY_F3 }, { XK_F4, KEY_F4 },
224     { XK_F5, KEY_F5 }, { XK_F6, KEY_F6 }, { XK_F7, KEY_F7 }, { XK_F8, KEY_F8 },
225     { XK_F9, KEY_F9 }, { XK_F10, KEY_F10 }, { XK_F11, KEY_F11 },
226     { XK_F12, KEY_F12 },
227
228     { XK_Return, KEY_ENTER },
229     { XK_KP_Enter, KEY_ENTER },
230     { XK_space, KEY_SPACE },
231     { XK_Escape, KEY_ESC },
232
233     { XK_Menu, KEY_MENU },
234     { XK_Left, KEY_LEFT },
235     { XK_Right, KEY_RIGHT },
236     { XK_Up, KEY_UP },
237     { XK_Down, KEY_DOWN },
238
239     { XK_Home, KEY_HOME },
240     { XK_End, KEY_END },
241     { XK_Page_Up, KEY_PAGEUP },
242     { XK_Page_Down, KEY_PAGEDOWN },
243
244     { XK_Insert, KEY_INSERT },
245     { XK_Delete, KEY_DELETE },
246     { XF86XK_AudioNext, KEY_MEDIA_NEXT_TRACK},
247     { XF86XK_AudioPrev, KEY_MEDIA_PREV_TRACK},
248     { XF86XK_AudioMute, KEY_VOLUME_MUTE },
249     { XF86XK_AudioLowerVolume, KEY_VOLUME_DOWN },
250     { XF86XK_AudioRaiseVolume, KEY_VOLUME_UP },
251     { XF86XK_AudioPlay, KEY_MEDIA_PLAY_PAUSE },
252     { XF86XK_AudioPause, KEY_MEDIA_PLAY_PAUSE },
253
254     { 0, 0 }
255 };
256 static xcb_keysym_t GetX11Key( unsigned i_vlc )
257 {
258     for( int i = 0; x11keys_to_vlckeys[i].i_vlc != 0; i++ )
259     {
260         if( x11keys_to_vlckeys[i].i_vlc == i_vlc )
261             return x11keys_to_vlckeys[i].i_x11;
262     }
263
264     /* Copied from xcb, it seems that xcb use ascii code for ascii characters */
265     if( isascii( i_vlc ) )
266         return i_vlc;
267
268     return XK_VoidSymbol;
269 }
270
271 static void Mapping( intf_thread_t *p_intf )
272 {
273     static const xcb_keysym_t p_x11_modifier_ignored[] = {
274         0,
275         XK_Num_Lock,
276         XK_Scroll_Lock,
277         XK_Caps_Lock,
278     };
279
280     intf_sys_t *p_sys = p_intf->p_sys;
281
282     p_sys->i_map = 0;
283     p_sys->p_map = NULL;
284
285     /* Registering of Hotkeys */
286     for( struct hotkey *p_hotkey = p_intf->p_libvlc->p_hotkeys;
287             p_hotkey->psz_action != NULL;
288             p_hotkey++ )
289     {
290         char *psz_hotkey;
291         if( asprintf( &psz_hotkey, "global-%s", p_hotkey->psz_action ) < 0 )
292             break;
293
294         const int i_vlc_action = p_hotkey->i_action;
295         const int i_vlc_key = config_GetInt( p_intf, psz_hotkey );
296
297         free( psz_hotkey );
298
299         if( !i_vlc_key )
300             continue;
301
302         const xcb_keycode_t key = xcb_key_symbols_get_keycode( p_sys->p_symbols, GetX11Key( i_vlc_key & ~KEY_MODIFIER ) );
303         const unsigned i_modifier = GetX11Modifier( p_sys->p_connection, p_sys->p_symbols, i_vlc_key & KEY_MODIFIER );
304
305         for( int j = 0; j < sizeof(p_x11_modifier_ignored)/sizeof(*p_x11_modifier_ignored); j++ )
306         {
307             const unsigned i_ignored = GetModifier( p_sys->p_connection, p_sys->p_symbols, p_x11_modifier_ignored[j] );
308             if( j != 0 && i_ignored == 0x00)
309                 continue;
310
311             hotkey_mapping_t *p_map_old = p_sys->p_map;
312             p_sys->p_map = realloc( p_sys->p_map, sizeof(*p_sys->p_map) * (p_sys->i_map+1) );
313             if( !p_sys->p_map )
314             {
315                 p_sys->p_map = p_map_old;
316                 break;
317             }
318             hotkey_mapping_t *p_map = &p_sys->p_map[p_sys->i_map++];
319
320             p_map->i_x11 = key;
321             p_map->i_modifier = i_modifier|i_ignored;
322             p_map->i_action = i_vlc_action;
323         }
324     }
325 }
326
327 static void Register( intf_thread_t *p_intf )
328 {
329     intf_sys_t *p_sys = p_intf->p_sys;
330
331     for( int i = 0; i < p_sys->i_map; i++ )
332     {
333         const hotkey_mapping_t *p_map = &p_sys->p_map[i];
334         xcb_grab_key( p_sys->p_connection, true, p_sys->root,
335                       p_map->i_modifier, p_map->i_x11,
336                       XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC );
337     }
338 }
339 static void Unregister( intf_thread_t *p_intf )
340 {
341     intf_sys_t *p_sys = p_intf->p_sys;
342
343     for( int i = 0; i < p_sys->i_map; i++ )
344     {
345         const hotkey_mapping_t *p_map = &p_sys->p_map[i];
346         xcb_ungrab_key( p_sys->p_connection, p_map->i_x11, p_sys->root, p_map->i_modifier );
347     }
348 }
349
350 static void *Thread( void *p_data )
351 {
352     intf_thread_t *p_intf = p_data;
353     intf_sys_t *p_sys = p_intf->p_sys;
354     xcb_connection_t *p_connection = p_sys->p_connection;
355
356     int canc = vlc_savecancel();
357
358     /* */
359     xcb_flush( p_connection );
360
361     /* */
362     int fd = xcb_get_file_descriptor( p_connection );
363     for( ;; )
364     {
365         /* Wait for x11 event */
366         vlc_restorecancel( canc );
367         struct pollfd fds = { .fd = fd, .events = POLLIN, };
368         if( poll( &fds, 1, -1 ) < 0 )
369         {
370             if( errno != EINTR )
371                 break;
372             canc = vlc_savecancel();
373             continue;
374         }
375         canc = vlc_savecancel();
376
377         xcb_generic_event_t *p_event;
378         while( ( p_event = xcb_poll_for_event( p_connection ) ) )
379         {
380             if( ( p_event->response_type & 0x7f ) != XCB_KEY_PRESS )
381             {
382                 free( p_event );
383                 continue;
384             }
385
386             xcb_key_press_event_t *e = (xcb_key_press_event_t *)p_event;
387
388             for( int i = 0; i < p_sys->i_map; i++ )
389             {
390                 hotkey_mapping_t *p_map = &p_sys->p_map[i];
391
392                 if( p_map->i_x11 == e->detail &&
393                     p_map->i_modifier == e->state )
394                 {
395                     var_SetInteger( p_intf->p_libvlc, "key-action", p_map->i_action );
396                     break;
397                 }
398             }
399             free( p_event );
400         }
401     }
402
403     return NULL;
404 }
405