3 * @brief X C Bindings VLC keyboard event handling
5 /*****************************************************************************
6 * Copyright © 2009 Rémi Denis-Courmont
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2.0
11 * of the License, or (at your option) any later version.
13 * This library 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 Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 ****************************************************************************/
33 #include <xcb/xcb_keysyms.h>
34 #include <X11/keysym.h>
35 #include <X11/XF86keysym.h>
37 #include <vlc_common.h>
45 xcb_key_symbols_t *syms;
49 * Create an X11 key event handler for a VLC window.
50 * The caller shall arrange receiving applicable X11 events, and pass them to
51 * ProcessKeyEvent() later.
53 * @param obj VLC object owning an X window
54 * @param conn XCB connection to the X server (to fetch key mappings)
55 * @return NULL on error, or a key handling context.
57 key_handler_t *CreateKeyHandler (vlc_object_t *obj, xcb_connection_t *conn)
59 key_handler_t *ctx = malloc (sizeof (*ctx));
64 ctx->syms = xcb_key_symbols_alloc (conn);
68 void DestroyKeyHandler (key_handler_t *ctx)
70 xcb_key_symbols_free (ctx->syms);
74 static int keysymcmp (const void *pa, const void *pb)
76 int a = *(const xcb_keysym_t *)pa;
77 int b = *(const xcb_keysym_t *)pb;
82 static int ConvertKeySym (xcb_keysym_t sym)
89 /* This list MUST be in XK_* incremental order (see keysymdef.h),
90 * so that binary search works.
91 * Multiple X keys can match the same VLC key.
92 * X key symbols must be in the first column of the struct. */
93 { XK_BackSpace, KEY_BACKSPACE, },
95 { XK_Return, KEY_ENTER, },
96 { XK_Escape, KEY_ESC, },
97 { XK_Home, KEY_HOME, },
98 { XK_Left, KEY_LEFT, },
100 { XK_Right, KEY_RIGHT, },
101 { XK_Down, KEY_DOWN, },
102 { XK_Page_Up, KEY_PAGEUP, },
103 { XK_Page_Down, KEY_PAGEDOWN, },
104 { XK_End, KEY_END, },
105 { XK_Begin, KEY_HOME, },
106 { XK_Insert, KEY_INSERT, },
107 { XK_Menu, KEY_MENU },
108 { XK_KP_Space, ' ', },
109 { XK_KP_Tab, KEY_TAB, },
110 { XK_KP_Enter, KEY_ENTER, },
111 { XK_KP_F1, KEY_F1, },
112 { XK_KP_F2, KEY_F2, },
113 { XK_KP_F3, KEY_F3, },
114 { XK_KP_F4, KEY_F4, },
115 { XK_KP_Home, KEY_HOME, },
116 { XK_KP_Left, KEY_LEFT, },
117 { XK_KP_Up, KEY_UP, },
118 { XK_KP_Right, KEY_RIGHT, },
119 { XK_KP_Down, KEY_DOWN, },
120 { XK_KP_Page_Up, KEY_PAGEUP, },
121 { XK_KP_Page_Down, KEY_PAGEDOWN, },
122 { XK_KP_End, KEY_END, },
123 { XK_KP_Begin, KEY_HOME, },
124 { XK_KP_Insert, KEY_INSERT, },
125 { XK_KP_Delete, KEY_DELETE, },
135 { XK_F10, KEY_F10, },
136 { XK_F11, KEY_F11, },
137 { XK_F12, KEY_F12, },
138 { XK_Delete, KEY_DELETE, },
140 /* XFree86 extensions */
141 { XF86XK_AudioLowerVolume, KEY_VOLUME_DOWN, },
142 { XF86XK_AudioMute, KEY_VOLUME_MUTE, },
143 { XF86XK_AudioRaiseVolume, KEY_VOLUME_UP, },
144 { XF86XK_AudioPlay, KEY_MEDIA_PLAY_PAUSE, },
145 { XF86XK_AudioStop, KEY_MEDIA_STOP, },
146 { XF86XK_AudioPrev, KEY_MEDIA_PREV_TRACK, },
147 { XF86XK_AudioNext, KEY_MEDIA_NEXT_TRACK, },
148 { XF86XK_HomePage, KEY_BROWSER_HOME, },
149 { XF86XK_Search, KEY_BROWSER_SEARCH, },
150 { XF86XK_Back, KEY_BROWSER_BACK, },
151 { XF86XK_Forward, KEY_BROWSER_FORWARD, },
152 { XF86XK_Stop, KEY_BROWSER_STOP, },
153 { XF86XK_Refresh, KEY_BROWSER_REFRESH, },
154 { XF86XK_Favorites, KEY_BROWSER_FAVORITES, },
155 { XF86XK_AudioPause, KEY_MEDIA_PLAY_PAUSE, },
156 { XF86XK_Reload, KEY_BROWSER_REFRESH, },
159 /* X11 and VLC both use the ASCII code for printable ASCII characters. */
164 res = bsearch (&sym, tab, sizeof (tab) / sizeof (tab[0]), sizeof (tab[0]),
174 * Process an X11 event, convert into VLC hotkey event if applicable.
176 * @param ctx key handler created with CreateKeyHandler()
177 * @param ev XCB event to process
178 * @return 0 if the event was handled and free()'d, non-zero otherwise
180 int ProcessKeyEvent (key_handler_t *ctx, xcb_generic_event_t *ev)
184 switch (ev->response_type & 0x7f)
188 xcb_key_press_event_t *e = (xcb_key_press_event_t *)ev;
189 xcb_keysym_t sym = xcb_key_press_lookup_keysym (ctx->syms, e, 0);
190 int vk = ConvertKeySym (sym);
194 if (e->state & XCB_MOD_MASK_SHIFT)
195 vk |= KEY_MODIFIER_SHIFT;
196 if (e->state & XCB_MOD_MASK_CONTROL)
197 vk |= KEY_MODIFIER_CTRL;
198 if (e->state & XCB_MOD_MASK_1)
199 vk |= KEY_MODIFIER_ALT;
200 if (e->state & XCB_MOD_MASK_4)
201 vk |= KEY_MODIFIER_COMMAND;
202 var_SetInteger (ctx->obj->p_libvlc, "key-pressed", vk);
206 case XCB_KEY_RELEASE:
209 case XCB_MAPPING_NOTIFY:
211 xcb_mapping_notify_event_t *e = (xcb_mapping_notify_event_t *)ev;
212 msg_Dbg (ctx->obj, "refreshing keyboard mapping");
213 xcb_refresh_keyboard_mapping (ctx->syms, e);