1 /*****************************************************************************
2 * xcb.c: Global-Hotkey X11 using xcb handling for vlc
3 *****************************************************************************
4 * Copyright (C) 2009 the VideoLAN team
6 * Authors: Laurent Aimar <fenrir _AT_ videolan _DOT_ org>
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.
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.
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 *****************************************************************************/
26 #define VLC_MODULE_LICENSE VLC_LICENSE_GPL_2_PLUS
27 #include <vlc_common.h>
28 #include <vlc_plugin.h>
29 #include <vlc_interface.h>
34 #include <xcb/xcb_keysyms.h>
35 #include <X11/keysym.h>
36 #include <X11/XF86keysym.h>
40 /*****************************************************************************
42 *****************************************************************************/
43 static int Open( vlc_object_t *p_this );
44 static void Close( vlc_object_t *p_this );
46 /*****************************************************************************
48 *****************************************************************************/
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 add_shortcut( "globalhotkeys" )
61 xcb_keycode_t *p_keys;
70 xcb_connection_t *p_connection;
72 xcb_key_symbols_t *p_symbols;
75 hotkey_mapping_t *p_map;
78 static bool Mapping( intf_thread_t *p_intf );
79 static void Register( intf_thread_t *p_intf );
80 static void *Thread( void *p_data );
82 /*****************************************************************************
84 *****************************************************************************/
85 static int Open( vlc_object_t *p_this )
87 intf_thread_t *p_intf = (intf_thread_t *)p_this;
89 int ret = VLC_EGENERIC;
91 p_intf->p_sys = p_sys = calloc( 1, sizeof(*p_sys) );
96 p_sys->p_connection = xcb_connect( NULL, &i_screen_default );
98 if( xcb_connection_has_error( p_sys->p_connection ) )
101 /* Get the root windows of the default screen */
102 const xcb_setup_t* xcbsetup = xcb_get_setup( p_sys->p_connection );
105 xcb_screen_iterator_t iter = xcb_setup_roots_iterator( xcbsetup );
106 for( int i = 0; i < i_screen_default; i++ )
110 xcb_screen_next( &iter );
114 p_sys->root = iter.data->root;
117 p_sys->p_symbols = xcb_key_symbols_alloc( p_sys->p_connection ); // FIXME
118 if( !p_sys->p_symbols )
121 if( !Mapping( p_intf ) )
124 p_intf->p_sys = NULL; /* for Close() */
129 if( vlc_clone( &p_sys->thread, Thread, p_intf, VLC_THREAD_PRIORITY_LOW ) )
133 free( p_sys->p_map->p_keys );
134 free( p_sys->p_map );
141 if( p_sys->p_symbols )
142 xcb_key_symbols_free( p_sys->p_symbols );
143 xcb_disconnect( p_sys->p_connection );
148 /*****************************************************************************
150 *****************************************************************************/
151 static void Close( vlc_object_t *p_this )
153 intf_thread_t *p_intf = (intf_thread_t *)p_this;
154 intf_sys_t *p_sys = p_intf->p_sys;
157 return; /* if we were running disabled */
159 vlc_cancel( p_sys->thread );
160 vlc_join( p_sys->thread, NULL );
164 free( p_sys->p_map->p_keys );
165 free( p_sys->p_map );
167 xcb_key_symbols_free( p_sys->p_symbols );
168 xcb_disconnect( p_sys->p_connection );
172 /*****************************************************************************
174 *****************************************************************************/
175 static unsigned GetModifier( xcb_connection_t *p_connection, xcb_key_symbols_t *p_symbols, xcb_keysym_t sym )
177 static const unsigned pi_mask[8] = {
178 XCB_MOD_MASK_SHIFT, XCB_MOD_MASK_LOCK, XCB_MOD_MASK_CONTROL,
179 XCB_MOD_MASK_1, XCB_MOD_MASK_2, XCB_MOD_MASK_3,
180 XCB_MOD_MASK_4, XCB_MOD_MASK_5
184 return 0; /* no modifier */
186 xcb_get_modifier_mapping_cookie_t r =
187 xcb_get_modifier_mapping( p_connection );
188 xcb_get_modifier_mapping_reply_t *p_map =
189 xcb_get_modifier_mapping_reply( p_connection, r, NULL );
193 xcb_keycode_t *p_keys = xcb_key_symbols_get_keycode( p_symbols, sym );
198 bool no_modifier = true;
199 while( p_keys[i] != XCB_NO_SYMBOL )
212 xcb_keycode_t *p_keycode = xcb_get_modifier_mapping_keycodes( p_map );
216 for( int i = 0; i < 8; i++ )
217 for( int j = 0; j < p_map->keycodes_per_modifier; j++ )
218 for( int k = 0; p_keys[k] != XCB_NO_SYMBOL; k++ )
219 if( p_keycode[i*p_map->keycodes_per_modifier + j] == p_keys[k])
228 free( p_map ); // FIXME to check
233 static unsigned GetX11Modifier( xcb_connection_t *p_connection,
234 xcb_key_symbols_t *p_symbols, unsigned i_vlc )
238 if( i_vlc & KEY_MODIFIER_ALT )
239 i_mask |= GetModifier( p_connection, p_symbols, XK_Alt_L ) |
240 GetModifier( p_connection, p_symbols, XK_Alt_R );
241 if( i_vlc & KEY_MODIFIER_SHIFT )
242 i_mask |= GetModifier( p_connection, p_symbols, XK_Shift_L ) |
243 GetModifier( p_connection, p_symbols, XK_Shift_R );
244 if( i_vlc & KEY_MODIFIER_CTRL )
245 i_mask |= GetModifier( p_connection, p_symbols, XK_Control_L ) |
246 GetModifier( p_connection, p_symbols, XK_Control_R );
247 if( i_vlc & KEY_MODIFIER_META )
248 i_mask |= GetModifier( p_connection, p_symbols, XK_Meta_L ) |
249 GetModifier( p_connection, p_symbols, XK_Meta_R ) |
250 GetModifier( p_connection, p_symbols, XK_Super_L ) |
251 GetModifier( p_connection, p_symbols, XK_Super_R );
255 /* FIXME this table is also used by the vout */
261 } x11keys_to_vlckeys[] =
263 #include "../../video_output/xcb/xcb_keysym.h"
266 static xcb_keysym_t GetX11Key( unsigned i_vlc )
268 /* X11 and VLC use ASCII for printable ASCII characters */
269 if( i_vlc >= 32 && i_vlc <= 127 )
272 for( int i = 0; x11keys_to_vlckeys[i].i_vlc != 0; i++ )
274 if( x11keys_to_vlckeys[i].i_vlc == i_vlc )
275 return x11keys_to_vlckeys[i].i_x11;
278 return XK_VoidSymbol;
281 static bool Mapping( intf_thread_t *p_intf )
283 static const xcb_keysym_t p_x11_modifier_ignored[] = {
290 intf_sys_t *p_sys = p_intf->p_sys;
296 /* Registering of Hotkeys */
297 for( const struct hotkey *p_hotkey = p_intf->p_libvlc->p_hotkeys;
298 p_hotkey->psz_action != NULL;
301 char varname[12 + strlen( p_hotkey->psz_action )];
302 sprintf( varname, "global-key-%s", p_hotkey->psz_action );
304 char *key = var_InheritString( p_intf, varname );
308 uint_fast32_t i_vlc_key = vlc_str2keycode( key );
310 if( i_vlc_key == KEY_UNSET )
313 xcb_keycode_t *p_keys = xcb_key_symbols_get_keycode(
314 p_sys->p_symbols, GetX11Key( i_vlc_key & ~KEY_MODIFIER ) );
318 const unsigned i_modifier = GetX11Modifier( p_sys->p_connection,
319 p_sys->p_symbols, i_vlc_key & KEY_MODIFIER );
321 const size_t max = sizeof(p_x11_modifier_ignored) /
322 sizeof(*p_x11_modifier_ignored);
323 for( unsigned int i = 0; i < max; i++ )
325 const unsigned i_ignored = GetModifier( p_sys->p_connection,
326 p_sys->p_symbols, p_x11_modifier_ignored[i] );
327 if( i != 0 && i_ignored == 0)
330 hotkey_mapping_t *p_map_old = p_sys->p_map;
331 p_sys->p_map = realloc( p_sys->p_map,
332 sizeof(*p_sys->p_map) * (p_sys->i_map+1) );
335 p_sys->p_map = p_map_old;
338 hotkey_mapping_t *p_map = &p_sys->p_map[p_sys->i_map++];
340 p_map->p_keys = p_keys;
341 p_map->i_modifier = i_modifier|i_ignored;
342 p_map->i_vlc = i_vlc_key;
349 static void Register( intf_thread_t *p_intf )
351 intf_sys_t *p_sys = p_intf->p_sys;
353 for( int i = 0; i < p_sys->i_map; i++ )
355 const hotkey_mapping_t *p_map = &p_sys->p_map[i];
356 for( int j = 0; p_map->p_keys[j] != XCB_NO_SYMBOL; j++ )
358 xcb_grab_key( p_sys->p_connection, true, p_sys->root,
359 p_map->i_modifier, p_map->p_keys[j],
360 XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC );
365 static void *Thread( void *p_data )
367 intf_thread_t *p_intf = p_data;
368 intf_sys_t *p_sys = p_intf->p_sys;
369 xcb_connection_t *p_connection = p_sys->p_connection;
371 int canc = vlc_savecancel();
374 xcb_flush( p_connection );
377 int fd = xcb_get_file_descriptor( p_connection );
380 /* Wait for x11 event */
381 vlc_restorecancel( canc );
382 struct pollfd fds = { .fd = fd, .events = POLLIN, };
383 if( poll( &fds, 1, -1 ) < 0 )
387 canc = vlc_savecancel();
390 canc = vlc_savecancel();
392 xcb_generic_event_t *p_event;
393 while( ( p_event = xcb_poll_for_event( p_connection ) ) )
395 if( ( p_event->response_type & 0x7f ) != XCB_KEY_PRESS )
401 xcb_key_press_event_t *e = (xcb_key_press_event_t *)p_event;
403 for( int i = 0; i < p_sys->i_map; i++ )
405 hotkey_mapping_t *p_map = &p_sys->p_map[i];
407 for( int j = 0; p_map->p_keys[j] != XCB_NO_SYMBOL; j++ )
408 if( p_map->p_keys[j] == e->detail &&
409 p_map->i_modifier == e->state )
411 var_SetInteger( p_intf->p_libvlc, "global-key-pressed",