1 /*****************************************************************************
2 * x11.c: Global-Hotkey X11 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 *****************************************************************************/
27 #include <X11/keysym.h>
28 #include <X11/Xutil.h>
29 #include <X11/XF86keysym.h>
32 #include <vlc_common.h>
33 #include <vlc_plugin.h>
34 #include <vlc_interface.h>
37 /*****************************************************************************
39 *****************************************************************************/
40 static int Open( vlc_object_t *p_this );
41 static void Close( vlc_object_t *p_this );
43 /*****************************************************************************
45 *****************************************************************************/
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 )
68 hotkey_mapping_t *p_map;
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 * );
77 /*****************************************************************************
79 *****************************************************************************/
80 static int Open( vlc_object_t *p_this )
82 intf_thread_t *p_intf = (intf_thread_t *)p_this;
85 Display *p_display = XOpenDisplay( NULL );
88 XSetErrorHandler( X11ErrorHandler );
90 p_intf->p_sys = p_sys = malloc( sizeof(*p_sys) );
93 XCloseDisplay( p_display );
96 p_sys->p_display = p_display;
100 if( vlc_clone( &p_sys->thread, Thread, p_intf, VLC_THREAD_PRIORITY_LOW ) )
102 Unregister( p_intf );
103 XCloseDisplay( p_display );
104 free( p_sys->p_map );
111 /*****************************************************************************
113 *****************************************************************************/
114 static void Close( vlc_object_t *p_this )
116 intf_thread_t *p_intf = (intf_thread_t *)p_this;
117 intf_sys_t *p_sys = p_intf->p_sys;
119 vlc_cancel( p_sys->thread );
120 vlc_join( p_sys->thread, NULL );
122 Unregister( p_intf );
123 XCloseDisplay( p_sys->p_display );
124 free( p_sys->p_map );
129 /*****************************************************************************
131 *****************************************************************************/
132 static int X11ErrorHandler( Display *p_display, XErrorEvent *p_event )
137 XGetErrorText( p_display, p_event->error_code, psz_txt, sizeof(psz_txt) );
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 );
143 VLC_UNUSED(p_display); VLC_UNUSED(p_event);
148 static unsigned GetModifier( Display *p_display, KeySym sym )
150 static const unsigned pi_mask[8] = {
151 ShiftMask, LockMask, ControlMask, Mod1Mask,
152 Mod2Mask, Mod3Mask, Mod4Mask, Mod5Mask
155 const KeyCode key = XKeysymToKeycode( p_display, sym );
159 XModifierKeymap *p_map = XGetModifierMapping( p_display );
164 for( int i = 0; i < 8 * p_map->max_keypermod; i++ )
166 if( p_map->modifiermap[i] == key )
168 i_mask = pi_mask[i / p_map->max_keypermod];
173 XFreeModifiermap( p_map );
176 static unsigned GetX11Modifier( Display *p_display, unsigned i_vlc )
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 );
192 /* FIXME this table is also used by the vout */
198 } x11keys_to_vlckeys[] =
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 },
205 { XK_Return, KEY_ENTER },
206 { XK_KP_Enter, KEY_ENTER },
207 { XK_space, KEY_SPACE },
208 { XK_Escape, KEY_ESC },
210 { XK_Menu, KEY_MENU },
211 { XK_Left, KEY_LEFT },
212 { XK_Right, KEY_RIGHT },
214 { XK_Down, KEY_DOWN },
216 { XK_Home, KEY_HOME },
218 { XK_Page_Up, KEY_PAGEUP },
219 { XK_Page_Down, KEY_PAGEDOWN },
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 },
233 static KeySym GetX11Key( unsigned i_vlc )
235 for( int i = 0; x11keys_to_vlckeys[i].i_vlc != 0; i++ )
237 if( x11keys_to_vlckeys[i].i_vlc == i_vlc )
238 return x11keys_to_vlckeys[i].i_x11;
244 return XStringToKeysym( psz_key );
247 static void Mapping( intf_thread_t *p_intf )
249 static const KeySym p_x11_modifier_ignored[] = {
256 intf_sys_t *p_sys = p_intf->p_sys;
261 /* Registering of Hotkeys */
262 for( struct hotkey *p_hotkey = p_intf->p_libvlc->p_hotkeys;
263 p_hotkey->psz_action != NULL;
267 if( asprintf( &psz_hotkey, "global-%s", p_hotkey->psz_action ) < 0 )
270 const int i_vlc_action = p_hotkey->i_action;
271 const int i_vlc_key = config_GetInt( p_intf, psz_hotkey );
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 );
282 for( int j = 0; j < sizeof(p_x11_modifier_ignored)/sizeof(*p_x11_modifier_ignored); j++ )
284 const unsigned i_ignored = GetModifier( p_sys->p_display, p_x11_modifier_ignored[j] );
285 if( j != 0 && i_ignored == 0x00)
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) );
292 p_sys->p_map = p_map_old;
295 hotkey_mapping_t *p_map = &p_sys->p_map[p_sys->i_map++];
298 p_map->i_modifier = i_modifier|i_ignored;
299 p_map->i_action = i_vlc_action;
304 static void Register( intf_thread_t *p_intf )
306 intf_sys_t *p_sys = p_intf->p_sys;
308 for( int i = 0; i < p_sys->i_map; i++ )
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 );
315 static void Unregister( intf_thread_t *p_intf )
317 intf_sys_t *p_sys = p_intf->p_sys;
319 for( int i = 0; i < p_sys->i_map; i++ )
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 ) );
327 static void *Thread( void *p_data )
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;
333 int canc = vlc_savecancel();
342 int fd = ConnectionNumber( p_display );
345 /* Wait for x11 event */
346 vlc_restorecancel( canc );
347 struct pollfd fds = { .fd = fd, .events = POLLIN, };
348 if( poll( &fds, 1, -1 ) < 0 )
352 canc = vlc_savecancel();
355 canc = vlc_savecancel();
357 while( XPending( p_display ) > 0 )
361 XNextEvent( p_display, &e );
362 if( e.type != KeyPress )
365 for( int i = 0; i < p_sys->i_map; i++ )
367 hotkey_mapping_t *p_map = &p_sys->p_map[i];
369 if( p_map->i_x11 == e.xkey.keycode &&
370 p_map->i_modifier == e.xkey.state )
372 var_SetInteger( p_intf->p_libvlc, "key-action", p_map->i_action );