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