1 /*****************************************************************************
2 * win32.c: Global-Hotkey WIN32 handling for vlc
3 *****************************************************************************
4 * Copyright (C) 2008-2009 the VideoLAN team
6 * Authors: Domani Hannes <ssbssa at yahoo dot de>
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 *****************************************************************************/
29 #include <vlc_common.h>
30 #include <vlc_plugin.h>
31 #include <vlc_interface.h>
34 /*****************************************************************************
36 *****************************************************************************/
37 static int Open( vlc_object_t *p_this );
38 static void Close( vlc_object_t *p_this );
39 static void *Thread( void *p_data );
40 LRESULT CALLBACK WMHOTKEYPROC( HWND, UINT, WPARAM, LPARAM );
42 /*****************************************************************************
44 *****************************************************************************/
46 set_shortname( N_("Global Hotkeys") )
47 set_category( CAT_INTERFACE )
48 set_subcategory( SUBCAT_INTERFACE_HOTKEYS )
49 set_description( N_("Global Hotkeys interface") )
50 set_capability( "interface", 0 )
51 set_callbacks( Open, Close )
62 /*****************************************************************************
63 * Open: initialize interface
64 *****************************************************************************/
65 static int Open( vlc_object_t *p_this )
67 intf_thread_t *p_intf = (intf_thread_t *)p_this;
68 intf_sys_t *p_sys = malloc( sizeof (intf_sys_t) );
73 p_intf->p_sys = p_sys;
74 p_sys->hotkeyWindow = NULL;
75 vlc_mutex_init( &p_sys->lock );
76 vlc_cond_init( &p_sys->wait );
78 if( vlc_clone( &p_sys->thread, Thread, p_intf, VLC_THREAD_PRIORITY_LOW ) )
80 vlc_mutex_destroy( &p_sys->lock );
81 vlc_cond_destroy( &p_sys->wait );
88 vlc_mutex_lock( &p_sys->lock );
89 while( p_sys->hotkeyWindow == NULL )
90 vlc_cond_wait( &p_sys->wait, &p_sys->lock );
91 if( p_sys->hotkeyWindow == INVALID_HANDLE_VALUE )
93 vlc_mutex_unlock( &p_sys->lock );
94 vlc_join( p_sys->thread, NULL );
95 vlc_mutex_destroy( &p_sys->lock );
96 vlc_cond_destroy( &p_sys->wait );
102 vlc_mutex_unlock( &p_sys->lock );
107 /*****************************************************************************
108 * Close: destroy interface
109 *****************************************************************************/
110 static void Close( vlc_object_t *p_this )
112 intf_thread_t *p_intf = (intf_thread_t *)p_this;
113 intf_sys_t *p_sys = p_intf->p_sys;
115 /* stop hotkey window */
116 vlc_mutex_lock( &p_sys->lock );
117 if( p_sys->hotkeyWindow != NULL )
118 PostMessage( p_sys->hotkeyWindow, WM_CLOSE, 0, 0 );
119 vlc_mutex_unlock( &p_sys->lock );
121 vlc_join( p_sys->thread, NULL );
122 vlc_mutex_destroy( &p_sys->lock );
123 vlc_cond_destroy( &p_sys->wait );
127 /*****************************************************************************
129 *****************************************************************************/
130 static void *Thread( void *p_data )
134 intf_thread_t *p_intf = p_data;
135 intf_sys_t *p_sys = p_intf->p_sys;
137 /* Window which receives Hotkeys */
138 vlc_mutex_lock( &p_sys->lock );
139 p_sys->hotkeyWindow =
140 (void*)CreateWindow( _T("STATIC"), /* name of window class */
141 _T("VLC ghk ") _T(VERSION), /* window title bar text */
142 0, /* window style */
143 0, /* default X coordinate */
144 0, /* default Y coordinate */
145 0, /* window width */
146 0, /* window height */
147 NULL, /* no parent window */
148 NULL, /* no menu in this window */
149 GetModuleHandle(NULL), /* handle of this program instance */
150 NULL ); /* sent to WM_CREATE */
152 if( p_sys->hotkeyWindow == NULL )
154 p_sys->hotkeyWindow = INVALID_HANDLE_VALUE;
155 vlc_cond_signal( &p_sys->wait );
156 vlc_mutex_unlock( &p_sys->lock );
159 vlc_cond_signal( &p_sys->wait );
160 vlc_mutex_unlock( &p_sys->lock );
162 SetWindowLongPtr( p_sys->hotkeyWindow, GWLP_WNDPROC,
163 (LONG_PTR)WMHOTKEYPROC );
164 SetWindowLongPtr( p_sys->hotkeyWindow, GWLP_USERDATA,
167 /* Registering of Hotkeys */
168 for( const struct hotkey *p_hotkey = p_intf->p_libvlc->p_hotkeys;
169 p_hotkey->psz_action != NULL;
172 char varname[12 + strlen( p_hotkey->psz_action )];
173 sprintf( varname, "global-key-%s", p_hotkey->psz_action );
175 char *key = var_InheritString( p_intf, varname );
179 UINT i_key = vlc_str2keycode( key );
181 if( i_key == KEY_UNSET )
185 if( i_key & KEY_MODIFIER_SHIFT ) i_keyMod |= MOD_SHIFT;
186 if( i_key & KEY_MODIFIER_ALT ) i_keyMod |= MOD_ALT;
187 if( i_key & KEY_MODIFIER_CTRL ) i_keyMod |= MOD_CONTROL;
189 #define HANDLE( key ) case KEY_##key: i_vk = VK_##key; break
190 #define HANDLE2( key, key2 ) case KEY_##key: i_vk = VK_##key2; break
192 #define KEY_SPACE ' '
194 #ifndef VK_VOLUME_DOWN
195 #define VK_VOLUME_DOWN 0xAE
196 #define VK_VOLUME_UP 0xAF
199 #ifndef VK_MEDIA_NEXT_TRACK
200 #define VK_MEDIA_NEXT_TRACK 0xB0
201 #define VK_MEDIA_PREV_TRACK 0xB1
202 #define VK_MEDIA_STOP 0xB2
203 #define VK_MEDIA_PLAY_PAUSE 0xB3
207 #define VK_PAGEUP 0x21
208 #define VK_PAGEDOWN 0x22
212 switch( i_key & ~KEY_MODIFIER )
219 HANDLE2( ESC, ESCAPE );
220 HANDLE2( ENTER, RETURN );
239 HANDLE( VOLUME_DOWN );
241 HANDLE( MEDIA_PLAY_PAUSE );
242 HANDLE( MEDIA_STOP );
243 HANDLE( MEDIA_PREV_TRACK );
244 HANDLE( MEDIA_NEXT_TRACK );
247 i_vk = toupper( (uint8_t)(i_key & ~KEY_MODIFIER) );
250 if( !i_vk ) continue;
255 ATOM atom = GlobalAddAtomA( p_hotkey->psz_action );
256 if( !atom ) continue;
258 if( !RegisterHotKey( p_sys->hotkeyWindow, atom, i_keyMod, i_vk ) )
259 GlobalDeleteAtom( atom );
262 /* Main message loop */
263 while( GetMessage( &message, NULL, 0, 0 ) )
264 DispatchMessage( &message );
266 /* Unregistering of Hotkeys */
267 for( const struct hotkey *p_hotkey = p_intf->p_libvlc->p_hotkeys;
268 p_hotkey->psz_action != NULL;
271 ATOM atom = GlobalFindAtomA( p_hotkey->psz_action );
272 if( !atom ) continue;
274 if( UnregisterHotKey( p_sys->hotkeyWindow, atom ) )
275 GlobalDeleteAtom( atom );
279 vlc_mutex_lock( &p_sys->lock );
280 DestroyWindow( p_sys->hotkeyWindow );
281 p_sys->hotkeyWindow = NULL;
282 vlc_mutex_unlock( &p_sys->lock );
287 /*****************************************************************************
288 * WMHOTKEYPROC: event callback
289 *****************************************************************************/
290 LRESULT CALLBACK WMHOTKEYPROC( HWND hwnd, UINT uMsg, WPARAM wParam,
297 char psz_atomName[44];
299 LONG_PTR ret = GetWindowLongPtr( hwnd, GWLP_USERDATA );
300 intf_thread_t *p_intf = (intf_thread_t*)ret;
301 strcpy( psz_atomName, "key-" );
303 if( !GlobalGetAtomNameA(
304 wParam, psz_atomName + 4,
305 sizeof( psz_atomName ) - 4 ) )
308 /* search for key associated with VLC */
309 vlc_action_t action = vlc_GetActionId( psz_atomName );
310 if( action != ACTIONID_NONE )
312 var_SetInteger( p_intf->p_libvlc,
313 "key-action", action );
320 PostQuitMessage( 0 );
324 return DefWindowProc( hwnd, uMsg, wParam, lParam );