1 /*****************************************************************************
2 * events.c: Windows DirectX video output events handler
3 *****************************************************************************
4 * Copyright (C) 2001-2009 the VideoLAN team
7 * Authors: Gildas Bazin <gbazin@videolan.org>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
25 /*****************************************************************************
26 * Preamble: This file contains the functions related to the creation of
27 * a window and the handling of its messages (events).
28 *****************************************************************************/
33 #include <vlc_common.h>
34 #include <vlc_vout_display.h>
35 #include <vlc_vout_window.h>
43 #ifdef MODULE_NAME_IS_directx
46 #ifdef MODULE_NAME_IS_direct3d
49 #ifdef MODULE_NAME_IS_glwin32
50 #include "../opengl.h"
58 //WINSHELLAPI BOOL WINAPI SHFullScreen(HWND hwndRequester, DWORD dwState);
60 UINT GetMenuState(HMENU hMenu, UINT id, UINT flags)
63 memset(&info, 0, sizeof(info));
64 info.cbSize = sizeof(info);
65 info.fMask = MIIM_STATE;
66 if (!GetMenuItemInfo(hMenu, id, (flags & MF_BYPOSITION) != 0, &info))
68 /* XXX Submenu handling is missing... */
73 /*#if defined(UNDER_CE) && !defined(__PLUGIN__) --FIXME*/
74 /*# define SHFS_SHOWSIPBUTTON 0x0004
75 # define SHFS_HIDESIPBUTTON 0x0008
76 # define MENU_HEIGHT 26
77 BOOL SHFullScreen(HWND hwndRequester, DWORD dwState);
80 /*****************************************************************************
82 *****************************************************************************/
83 #define WM_VLC_HIDE_MOUSE (WM_APP + 0)
84 #define WM_VLC_SHOW_MOUSE (WM_APP + 1)
85 #define WM_VLC_CHANGE_TEXT (WM_APP + 2)
104 volatile bool b_cursor_hidden;
105 volatile mtime_t i_lastmoved;
106 mtime_t i_mouse_hide_timeout;
112 vout_window_cfg_t wnd_cfg;
115 vout_window_t *parent_window;
120 video_format_t source;
121 vout_display_place_t place;
126 static int DirectXCreateWindow( event_thread_t * );
127 static void DirectXCloseWindow ( event_thread_t * );
128 static long FAR PASCAL DirectXEventProc( HWND, UINT, WPARAM, LPARAM );
130 static int DirectXConvertKey( int i_key );
132 static inline bool isMouseEvent( WPARAM type )
134 return type >= WM_MOUSEFIRST &&
135 type <= WM_MOUSELAST;
138 static inline bool isKeyEvent( WPARAM type )
140 return type >= WM_KEYFIRST &&
144 /*****************************************************************************
145 * EventThread: Create video window & handle its messages
146 *****************************************************************************
147 * This function creates a video window and then enters an infinite loop
148 * that handles the messages sent to that window.
149 * The main goal of this thread is to isolate the Win32 PeekMessage function
150 * because this one can block for a long time.
151 *****************************************************************************/
152 static void *EventThread( void *p_this )
154 event_thread_t *p_event = (event_thread_t *)p_this;
155 vout_display_t *vd = p_event->vd;
157 POINT old_mouse_pos = {0,0}, mouse_pos;
159 int canc = vlc_savecancel ();
161 bool b_mouse_support = var_InheritBool( p_event->vd, "mouse-events" );
162 bool b_key_support = var_InheritBool( p_event->vd, "keyboard-events" );
164 vlc_mutex_lock( &p_event->lock );
165 /* Create a window for the video */
166 /* Creating a window under Windows also initializes the thread's event
168 if( DirectXCreateWindow( p_event ) )
169 p_event->b_error = true;
171 p_event->b_ready = true;
172 vlc_cond_signal( &p_event->wait );
174 const bool b_error = p_event->b_error;
175 vlc_mutex_unlock( &p_event->lock );
179 vlc_restorecancel( canc );
184 /* Set power management stuff */
185 if( (hkernel32 = GetModuleHandle( _T("KERNEL32") ) ) )
187 ULONG (WINAPI* OurSetThreadExecutionState)( ULONG );
189 OurSetThreadExecutionState = (ULONG (WINAPI*)( ULONG ))
190 GetProcAddress( hkernel32, _T("SetThreadExecutionState") );
192 if( OurSetThreadExecutionState )
193 /* Prevent monitor from powering off */
194 OurSetThreadExecutionState( ES_DISPLAY_REQUIRED | ES_CONTINUOUS );
196 msg_Dbg( vd, "no support for SetThreadExecutionState()" );
201 /* GetMessage will sleep if there's no message in the queue */
204 vout_display_place_t place;
205 video_format_t source;
207 if( !GetMessage( &msg, 0, 0, 0 ) )
209 vlc_mutex_lock( &p_event->lock );
210 p_event->b_done = true;
211 vlc_mutex_unlock( &p_event->lock );
215 /* Check if we are asked to exit */
216 vlc_mutex_lock( &p_event->lock );
217 const bool b_done = p_event->b_done;
218 vlc_mutex_unlock( &p_event->lock );
222 if( !b_mouse_support && isMouseEvent( msg.message ) )
225 if( !b_key_support && isKeyEvent( msg.message ) )
229 switch( msg.message )
232 vlc_mutex_lock( &p_event->lock );
233 place = p_event->place;
234 source = p_event->source;
235 vlc_mutex_unlock( &p_event->lock );
237 if( place.width > 0 && place.height > 0 )
239 if( msg.hwnd == p_event->hvideownd )
245 const int x = source.i_x_offset +
246 (int64_t)(GET_X_LPARAM(msg.lParam) - place.x) * source.i_width / place.width;
247 const int y = source.i_y_offset +
248 (int64_t)(GET_Y_LPARAM(msg.lParam) - place.y) * source.i_height / place.height;
249 vout_display_SendEventMouseMoved(vd, x, y);
253 GetCursorPos( &mouse_pos );
254 /* FIXME, why this >2 limits ? */
255 if( (abs(mouse_pos.x - old_mouse_pos.x) > 2 ||
256 (abs(mouse_pos.y - old_mouse_pos.y)) > 2 ) )
258 GetCursorPos( &old_mouse_pos );
259 p_event->i_lastmoved = mdate();
261 if( p_event->b_cursor_hidden )
263 p_event->b_cursor_hidden = false;
269 case WM_VLC_HIDE_MOUSE:
270 if( p_event->b_cursor_hidden )
272 p_event->b_cursor_hidden = true;
273 GetCursorPos( &old_mouse_pos );
277 case WM_VLC_SHOW_MOUSE:
278 if( !p_event->b_cursor_hidden )
280 p_event->b_cursor_hidden = false;
281 GetCursorPos( &old_mouse_pos );
286 vout_display_SendEventMousePressed(vd, MOUSE_BUTTON_LEFT);
289 vout_display_SendEventMouseReleased(vd, MOUSE_BUTTON_LEFT);
291 case WM_LBUTTONDBLCLK:
292 vout_display_SendEventMouseDoubleClick(vd);
296 vout_display_SendEventMousePressed(vd, MOUSE_BUTTON_CENTER);
299 vout_display_SendEventMouseReleased(vd, MOUSE_BUTTON_CENTER);
303 vout_display_SendEventMousePressed(vd, MOUSE_BUTTON_RIGHT);
306 vout_display_SendEventMouseReleased(vd, MOUSE_BUTTON_RIGHT);
312 /* The key events are first processed here and not translated
313 * into WM_CHAR events because we need to know the status of the
315 int i_key = DirectXConvertKey( msg.wParam );
318 /* This appears to be a "normal" (ascii) key */
319 i_key = tolower( MapVirtualKey( msg.wParam, 2 ) );
324 if( GetKeyState(VK_CONTROL) & 0x8000 )
326 i_key |= KEY_MODIFIER_CTRL;
328 if( GetKeyState(VK_SHIFT) & 0x8000 )
330 i_key |= KEY_MODIFIER_SHIFT;
332 if( GetKeyState(VK_MENU) & 0x8000 )
334 i_key |= KEY_MODIFIER_ALT;
337 vout_display_SendEventKey(vd, i_key);
345 if( GET_WHEEL_DELTA_WPARAM( msg.wParam ) > 0 )
347 i_key = KEY_MOUSEWHEELUP;
351 i_key = KEY_MOUSEWHEELDOWN;
355 if( GetKeyState(VK_CONTROL) & 0x8000 )
357 i_key |= KEY_MODIFIER_CTRL;
359 if( GetKeyState(VK_SHIFT) & 0x8000 )
361 i_key |= KEY_MODIFIER_SHIFT;
363 if( GetKeyState(VK_MENU) & 0x8000 )
365 i_key |= KEY_MODIFIER_ALT;
367 vout_display_SendEventKey(vd, i_key);
372 case WM_VLC_CHANGE_TEXT:
374 vlc_mutex_lock( &p_event->lock );
375 wchar_t *pwz_title = NULL;
376 if( p_event->psz_title )
378 const size_t i_length = strlen(p_event->psz_title);
379 pwz_title = malloc( 2 * (i_length + 1) );
382 mbstowcs( pwz_title, p_event->psz_title, 2 * i_length );
383 pwz_title[i_length] = 0;
386 vlc_mutex_unlock( &p_event->lock );
390 SetWindowTextW( p_event->hwnd, (LPCTSTR)pwz_title );
391 if( p_event->hfswnd )
392 SetWindowTextW( p_event->hfswnd, (LPCTSTR)pwz_title );
399 /* Messages we don't handle directly are dispatched to the
400 * window procedure */
401 TranslateMessage(&msg);
402 DispatchMessage(&msg);
407 } /* End Main loop */
409 /* Check for WM_QUIT if we created the window */
410 if( !p_event->hparent && msg.message == WM_QUIT )
412 msg_Warn( vd, "WM_QUIT... should not happen!!" );
413 p_event->hwnd = NULL; /* Window already destroyed */
416 msg_Dbg( vd, "DirectXEventThread terminating" );
418 DirectXCloseWindow( p_event );
419 vlc_restorecancel(canc);
424 /* following functions are local */
426 /*****************************************************************************
427 * DirectXCreateWindow: create a window for the video.
428 *****************************************************************************
429 * Before creating a direct draw surface, we need to create a window in which
430 * the video will be displayed. This window will also allow us to capture the
432 *****************************************************************************/
433 static int DirectXCreateWindow( event_thread_t *p_event )
435 vout_display_t *vd = p_event->vd;
439 WNDCLASS wc; /* window class components */
441 char vlc_path[MAX_PATH+1];
442 int i_style, i_stylex;
444 msg_Dbg( vd, "DirectXCreateWindow" );
446 /* Get this module's instance */
447 hInstance = GetModuleHandle(NULL);
449 #ifdef MODULE_NAME_IS_direct3d
450 if( !p_event->use_desktop )
453 /* If an external window was specified, we'll draw in it. */
454 p_event->parent_window = vout_display_NewWindow(vd, &p_event->wnd_cfg );
455 if( p_event->parent_window )
456 p_event->hparent = p_event->parent_window->handle.hwnd;
458 p_event->hparent = NULL;
459 #ifdef MODULE_NAME_IS_direct3d
463 /* Find Program Manager */
464 HWND hwnd = FindWindow( _T("Progman"), NULL );
465 if( hwnd ) hwnd = FindWindowEx( hwnd, NULL, _T("SHELLDLL_DefView"), NULL );
466 if( hwnd ) hwnd = FindWindowEx( hwnd, NULL, _T("SysListView32"), NULL );
468 msg_Err( vd, "Couldn't find desktop icon window. Desktop mode can't be established." );
469 p_event->parent_window = NULL;
470 p_event->hparent = hwnd;
474 /* Get the Icon from the main app */
477 if( GetModuleFileName( NULL, vlc_path, MAX_PATH ) )
479 vlc_icon = ExtractIcon( hInstance, vlc_path, 0 );
483 /* Fill in the window class structure */
484 wc.style = CS_OWNDC|CS_DBLCLKS; /* style: dbl click */
485 wc.lpfnWndProc = (WNDPROC)DirectXEventProc; /* event handler */
486 wc.cbClsExtra = 0; /* no extra class data */
487 wc.cbWndExtra = 0; /* no extra window data */
488 wc.hInstance = hInstance; /* instance */
489 wc.hIcon = vlc_icon; /* load the vlc big icon */
490 wc.hCursor = LoadCursor(NULL, IDC_ARROW); /* default cursor */
491 wc.hbrBackground = GetStockObject(BLACK_BRUSH); /* background color */
492 wc.lpszMenuName = NULL; /* no menu */
493 wc.lpszClassName = _T("VLC DirectX"); /* use a special class */
495 /* Register the window class */
496 if( !RegisterClass(&wc) )
500 if( vlc_icon ) DestroyIcon( vlc_icon );
502 /* Check why it failed. If it's because one already exists
503 * then fine, otherwise return with an error. */
504 if( !GetClassInfo( hInstance, _T("VLC DirectX"), &wndclass ) )
506 msg_Err( vd, "DirectXCreateWindow RegisterClass FAILED (err=%lu)", GetLastError() );
511 /* Register the video sub-window class */
512 wc.lpszClassName = _T("VLC DirectX video"); wc.hIcon = 0;
513 wc.hbrBackground = NULL; /* no background color */
514 if( !RegisterClass(&wc) )
518 /* Check why it failed. If it's because one already exists
519 * then fine, otherwise return with an error. */
520 if( !GetClassInfo( hInstance, _T("VLC DirectX video"), &wndclass ) )
522 msg_Err( vd, "DirectXCreateWindow RegisterClass FAILED (err=%lu)", GetLastError() );
527 /* When you create a window you give the dimensions you wish it to
528 * have. Unfortunatly these dimensions will include the borders and
529 * titlebar. We use the following function to find out the size of
530 * the window corresponding to the useable surface we want */
531 rect_window.left = 10;
532 rect_window.top = 10;
533 rect_window.right = rect_window.left + p_event->wnd_cfg.width;
534 rect_window.bottom = rect_window.top + p_event->wnd_cfg.height;
536 if( var_GetBool( vd, "video-deco" ) )
538 /* Open with window decoration */
539 AdjustWindowRect( &rect_window, WS_OVERLAPPEDWINDOW|WS_SIZEBOX, 0 );
540 i_style = WS_OVERLAPPEDWINDOW|WS_SIZEBOX|WS_VISIBLE|WS_CLIPCHILDREN;
545 /* No window decoration */
546 AdjustWindowRect( &rect_window, WS_POPUP, 0 );
547 i_style = WS_POPUP|WS_VISIBLE|WS_CLIPCHILDREN;
548 i_stylex = 0; // WS_EX_TOOLWINDOW; Is TOOLWINDOW really needed ?
549 // It messes up the fullscreen window.
552 if( p_event->hparent )
554 i_style = WS_VISIBLE|WS_CLIPCHILDREN|WS_CHILD;
558 p_event->i_window_style = i_style;
560 /* Create the window */
562 CreateWindowEx( WS_EX_NOPARENTNOTIFY | i_stylex,
563 _T("VLC DirectX"), /* name of window class */
564 _T(VOUT_TITLE) _T(" (DirectX Output)"), /* window title */
565 i_style, /* window style */
566 (!p_event->wnd_cfg.x) ? CW_USEDEFAULT :
567 (UINT)p_event->wnd_cfg.x, /* default X coordinate */
568 (!p_event->wnd_cfg.y) ? CW_USEDEFAULT :
569 (UINT)p_event->wnd_cfg.y, /* default Y coordinate */
570 rect_window.right - rect_window.left, /* window width */
571 rect_window.bottom - rect_window.top, /* window height */
572 p_event->hparent, /* parent window */
573 NULL, /* no menu in this window */
574 hInstance, /* handle of this program instance */
575 (LPVOID)p_event ); /* send vd to WM_CREATE */
579 msg_Warn( vd, "DirectXCreateWindow create window FAILED (err=%lu)", GetLastError() );
583 if( p_event->hparent )
587 /* We don't want the window owner to overwrite our client area */
588 i_style = GetWindowLong( p_event->hparent, GWL_STYLE );
590 if( !(i_style & WS_CLIPCHILDREN) )
591 /* Hmmm, apparently this is a blocking call... */
592 SetWindowLong( p_event->hparent, GWL_STYLE,
593 i_style | WS_CLIPCHILDREN );
595 /* Create our fullscreen window */
597 CreateWindowEx( WS_EX_APPWINDOW, _T("VLC DirectX"),
598 _T(VOUT_TITLE) _T(" (DirectX Output)"),
599 WS_OVERLAPPEDWINDOW|WS_CLIPCHILDREN|WS_SIZEBOX,
600 CW_USEDEFAULT, CW_USEDEFAULT,
601 CW_USEDEFAULT, CW_USEDEFAULT,
602 NULL, NULL, hInstance, NULL );
606 p_event->hfswnd = NULL;
609 /* Append a "Always On Top" entry in the system menu */
610 hMenu = GetSystemMenu( p_event->hwnd, FALSE );
611 AppendMenu( hMenu, MF_SEPARATOR, 0, _T("") );
612 AppendMenu( hMenu, MF_STRING | MF_UNCHECKED,
613 IDM_TOGGLE_ON_TOP, _T("Always on &Top") );
615 /* Create video sub-window. This sub window will always exactly match
616 * the size of the video, which allows us to use crazy overlay colorkeys
617 * without having them shown outside of the video area. */
618 /* FIXME vd->source.i_width/i_height seems wrong */
620 CreateWindow( _T("VLC DirectX video"), _T(""), /* window class */
621 WS_CHILD, /* window style, not visible initially */
623 vd->source.i_width, /* default width */
624 vd->source.i_height, /* default height */
625 p_event->hwnd, /* parent window */
627 (LPVOID)p_event ); /* send vd to WM_CREATE */
629 if( !p_event->hvideownd )
630 msg_Warn( vd, "can't create video sub-window" );
632 msg_Dbg( vd, "created video sub-window" );
634 /* Now display the window */
635 ShowWindow( p_event->hwnd, SW_SHOW );
640 /*****************************************************************************
641 * DirectXCloseWindow: close the window created by DirectXCreateWindow
642 *****************************************************************************
643 * This function returns all resources allocated by DirectXCreateWindow.
644 *****************************************************************************/
645 static void DirectXCloseWindow( event_thread_t *p_event )
647 vout_display_t *vd = p_event->vd;
648 msg_Dbg( vd, "DirectXCloseWindow" );
650 DestroyWindow( p_event->hwnd );
651 if( p_event->hfswnd )
652 DestroyWindow( p_event->hfswnd );
654 #ifdef MODULE_NAME_IS_direct3d
655 if( !p_event->use_desktop )
657 vout_display_DeleteWindow( vd, p_event->parent_window );
658 p_event->hwnd = NULL;
660 /* We don't unregister the Window Class because it could lead to race
661 * conditions and it will be done anyway by the system when the app will
665 /*****************************************************************************
666 * DirectXEventProc: This is the window event processing function.
667 *****************************************************************************
668 * On Windows, when you create a window you have to attach an event processing
669 * function to it. The aim of this function is to manage "Queued Messages" and
670 * "Nonqueued Messages".
671 * Queued Messages are those picked up and retransmitted by vout_Manage
672 * (using the GetMessage and DispatchMessage functions).
673 * Nonqueued Messages are those that Windows will send directly to this
674 * procedure (like WM_DESTROY, WM_WINDOWPOSCHANGED...)
675 *****************************************************************************/
676 static long FAR PASCAL DirectXEventProc( HWND hwnd, UINT message,
677 WPARAM wParam, LPARAM lParam )
679 event_thread_t *p_event;
681 if( message == WM_CREATE )
683 /* Store vd for future use */
684 p_event = (event_thread_t *)((CREATESTRUCT *)lParam)->lpCreateParams;
685 SetWindowLongPtr( hwnd, GWLP_USERDATA, (LONG_PTR)p_event );
690 LONG_PTR p_user_data = GetWindowLongPtr( hwnd, GWLP_USERDATA );
691 p_event = (event_thread_t *)p_user_data;
694 /* Hmmm mozilla does manage somehow to save the pointer to our
695 * windowproc and still calls it after the vout has been closed. */
696 return DefWindowProc(hwnd, message, wParam, lParam);
699 vout_display_t *vd = p_event->vd;
702 /* Catch the screensaver and the monitor turn-off */
703 if( message == WM_SYSCOMMAND &&
704 ( (wParam & 0xFFF0) == SC_SCREENSAVE || (wParam & 0xFFF0) == SC_MONITORPOWER ) )
706 //if( vd ) msg_Dbg( vd, "WinProc WM_SYSCOMMAND screensaver" );
707 return 0; /* this stops them from happening */
711 if( hwnd == p_event->hvideownd )
713 #ifdef MODULE_NAME_IS_directx
714 vlc_mutex_lock( &p_event->lock );
715 const bool use_overlay = p_event->use_overlay;
716 vlc_mutex_unlock( &p_event->lock );
721 #ifdef MODULE_NAME_IS_directx
723 /* For overlay, we need to erase background */
724 return !use_overlay ? 1 : DefWindowProc(hwnd, message, wParam, lParam);
727 ** For overlay, DefWindowProc() will erase dirty regions
729 ** For non-overlay, vout will paint the whole window at
730 ** regular interval, therefore dirty regions can be ignored
731 ** to minimize repaint.
735 ValidateRect(hwnd, NULL);
737 // fall through to default
740 ** For OpenGL and Direct3D, vout will update the whole
741 ** window at regular interval, therefore dirty region
742 ** can be ignored to minimize repaint.
745 /* nothing to erase */
748 /* nothing to repaint */
749 ValidateRect(hwnd, NULL);
753 return DefWindowProc(hwnd, message, wParam, lParam);
760 case WM_WINDOWPOSCHANGED:
761 vlc_mutex_lock( &p_event->lock );
762 p_event->has_moved = true;
763 vlc_mutex_unlock( &p_event->lock );
766 /* the user wants to close the window */
768 vout_display_SendEventClose(vd);
771 /* the window has been closed so shut down everything now */
773 msg_Dbg( vd, "WinProc WM_DESTROY" );
774 /* just destroy the window */
775 PostQuitMessage( 0 );
781 case IDM_TOGGLE_ON_TOP: /* toggle the "on top" status */
783 msg_Dbg(vd, "WinProc WM_SYSCOMMAND: IDM_TOGGLE_ON_TOP");
784 HMENU hMenu = GetSystemMenu(vd->sys->hwnd, FALSE);
785 vout_display_SendWindowState(vd, (GetMenuState(hMenu, IDM_TOGGLE_ON_TOP, MF_BYCOMMAND) & MF_CHECKED) ?
786 VOUT_WINDOW_STATE_NORMAL : VOUT_WINDOW_STATE_ABOVE);
797 return DefWindowProc(hwnd, message, wParam, lParam);
800 #ifdef MODULE_NAME_IS_wingapi
804 if( hwnd == p_event->hfswnd )
806 HWND htbar = FindWindow( _T("HHTaskbar"), NULL );
807 ShowWindow( htbar, SW_SHOW );
810 if( !p_event->hparent ||
811 hwnd == p_event->hfswnd )
813 SHFullScreen( hwnd, SHFS_SHOWSIPBUTTON );
819 #ifdef MODULE_NAME_IS_wingapi
823 /* FIXME vd->cfg is not lock[ed/able] */
824 #warning "FIXME: race condition"
825 if( p_event->hparent &&
826 hwnd != p_event->hfswnd && vd->cfg->is_fullscreen )
827 vout_display_SendEventFullscreen(vd, false);
829 if( hwnd == p_event->hfswnd )
831 HWND htbar = FindWindow( _T("HHTaskbar"), NULL );
832 ShowWindow( htbar, SW_HIDE );
835 if( !p_event->hparent ||
836 hwnd == p_event->hfswnd )
838 SHFullScreen( hwnd, SHFS_HIDESIPBUTTON );
844 //msg_Dbg( vd, "WinProc WM Default %i", message );
848 /* Let windows handle the message */
849 return DefWindowProc(hwnd, message, wParam, lParam);
857 } dxkeys_to_vlckeys[] =
859 { VK_F1, KEY_F1 }, { VK_F2, KEY_F2 }, { VK_F3, KEY_F3 }, { VK_F4, KEY_F4 },
860 { VK_F5, KEY_F5 }, { VK_F6, KEY_F6 }, { VK_F7, KEY_F7 }, { VK_F8, KEY_F8 },
861 { VK_F9, KEY_F9 }, { VK_F10, KEY_F10 }, { VK_F11, KEY_F11 },
864 { VK_RETURN, KEY_ENTER },
866 { VK_ESCAPE, KEY_ESC },
868 { VK_LEFT, KEY_LEFT },
869 { VK_RIGHT, KEY_RIGHT },
871 { VK_DOWN, KEY_DOWN },
873 { VK_HOME, KEY_HOME },
875 { VK_PRIOR, KEY_PAGEUP },
876 { VK_NEXT, KEY_PAGEDOWN },
878 { VK_INSERT, KEY_INSERT },
879 { VK_DELETE, KEY_DELETE },
885 { VK_BROWSER_BACK, KEY_BROWSER_BACK },
886 { VK_BROWSER_FORWARD, KEY_BROWSER_FORWARD },
887 { VK_BROWSER_REFRESH, KEY_BROWSER_REFRESH },
888 { VK_BROWSER_STOP, KEY_BROWSER_STOP },
889 { VK_BROWSER_SEARCH, KEY_BROWSER_SEARCH },
890 { VK_BROWSER_FAVORITES, KEY_BROWSER_FAVORITES },
891 { VK_BROWSER_HOME, KEY_BROWSER_HOME },
892 { VK_VOLUME_MUTE, KEY_VOLUME_MUTE },
893 { VK_VOLUME_DOWN, KEY_VOLUME_DOWN },
894 { VK_VOLUME_UP, KEY_VOLUME_UP },
895 { VK_MEDIA_NEXT_TRACK, KEY_MEDIA_NEXT_TRACK },
896 { VK_MEDIA_PREV_TRACK, KEY_MEDIA_PREV_TRACK },
897 { VK_MEDIA_STOP, KEY_MEDIA_STOP },
898 { VK_MEDIA_PLAY_PAUSE, KEY_MEDIA_PLAY_PAUSE },
903 static int DirectXConvertKey( int i_key )
907 for( i = 0; dxkeys_to_vlckeys[i].i_dxkey != 0; i++ )
909 if( dxkeys_to_vlckeys[i].i_dxkey == i_key )
911 return dxkeys_to_vlckeys[i].i_vlckey;
918 void EventThreadMouseAutoHide( event_thread_t *p_event )
920 if (!p_event->b_cursor_hidden &&
921 (mdate() - p_event->i_lastmoved) > p_event->i_mouse_hide_timeout )
923 /* Hide the cursor only if it is inside our window */
925 GetCursorPos( &point );
927 HWND hwnd = WindowFromPoint(point);
928 if( hwnd == p_event->hwnd || hwnd == p_event->hvideownd )
930 PostMessage( p_event->hwnd, WM_VLC_HIDE_MOUSE, 0, 0 );
934 p_event->i_lastmoved = mdate();
938 void EventThreadMouseShow( event_thread_t *p_event )
940 PostMessage( p_event->hwnd, WM_VLC_SHOW_MOUSE, 0, 0 );
942 void EventThreadUpdateTitle( event_thread_t *p_event, const char *psz_fallback )
944 char *psz_title = var_GetNonEmptyString( p_event->vd, "video-title" );
946 psz_title = strdup( psz_fallback );
950 vlc_mutex_lock( &p_event->lock );
951 free( p_event->psz_title );
952 p_event->psz_title = psz_title;
953 vlc_mutex_unlock( &p_event->lock );
955 PostMessage( p_event->hwnd, WM_VLC_CHANGE_TEXT, 0, 0 );
957 int EventThreadGetWindowStyle( event_thread_t *p_event )
959 /* No need to lock, it is serialized by EventThreadStart */
960 return p_event->i_window_style;
963 void EventThreadUpdateWindowPosition( event_thread_t *p_event,
964 bool *pb_moved, bool *pb_resized,
965 int x, int y, int w, int h )
967 vlc_mutex_lock( &p_event->lock );
968 *pb_moved = x != p_event->wnd_cfg.x ||
969 y != p_event->wnd_cfg.y;
970 *pb_resized = w != p_event->wnd_cfg.width ||
971 h != p_event->wnd_cfg.height;
973 p_event->wnd_cfg.x = x;
974 p_event->wnd_cfg.y = y;
975 p_event->wnd_cfg.width = w;
976 p_event->wnd_cfg.height = h;
977 vlc_mutex_unlock( &p_event->lock );
980 void EventThreadUpdateSourceAndPlace( event_thread_t *p_event,
981 const video_format_t *p_source,
982 const vout_display_place_t *p_place )
984 vlc_mutex_lock( &p_event->lock );
985 p_event->source = *p_source;
986 p_event->place = *p_place;
987 vlc_mutex_unlock( &p_event->lock );
990 void EventThreadUseOverlay( event_thread_t *p_event, bool b_used )
992 vlc_mutex_lock( &p_event->lock );
993 p_event->use_overlay = b_used;
994 vlc_mutex_unlock( &p_event->lock );
996 bool EventThreadGetAndResetHasMoved( event_thread_t *p_event )
998 vlc_mutex_lock( &p_event->lock );
999 const bool has_moved = p_event->has_moved;
1000 p_event->has_moved = false;
1001 vlc_mutex_unlock( &p_event->lock );
1006 event_thread_t *EventThreadCreate( vout_display_t *vd)
1008 /* Create the Vout EventThread, this thread is created by us to isolate
1009 * the Win32 PeekMessage function calls. We want to do this because
1010 * Windows can stay blocked inside this call for a long time, and when
1011 * this happens it thus blocks vlc's video_output thread.
1012 * Vout EventThread will take care of the creation of the video
1013 * window (because PeekMessage has to be called from the same thread which
1014 * created the window). */
1015 msg_Dbg( vd, "creating Vout EventThread" );
1016 event_thread_t *p_event = malloc( sizeof(*p_event) );
1021 vlc_mutex_init( &p_event->lock );
1022 vlc_cond_init( &p_event->wait );
1024 p_event->b_cursor_hidden = false;
1025 p_event->i_lastmoved = mdate();
1026 p_event->i_mouse_hide_timeout =
1027 var_GetInteger(vd, "mouse-hide-timeout") * 1000;
1028 p_event->psz_title = NULL;
1029 p_event->source = vd->source;
1030 vout_display_PlacePicture(&p_event->place, &vd->source, vd->cfg, false);
1035 void EventThreadDestroy( event_thread_t *p_event )
1037 free( p_event->psz_title );
1038 vlc_cond_destroy( &p_event->wait );
1039 vlc_mutex_destroy( &p_event->lock );
1043 int EventThreadStart( event_thread_t *p_event, event_hwnd_t *p_hwnd, const event_cfg_t *p_cfg )
1045 p_event->use_desktop = p_cfg->use_desktop;
1046 p_event->use_overlay = p_cfg->use_overlay;
1047 p_event->wnd_cfg = p_cfg->win;
1049 p_event->has_moved = false;
1051 p_event->b_ready = false;
1052 p_event->b_done = false;
1053 p_event->b_error = false;
1055 if( vlc_clone( &p_event->thread, EventThread, p_event,
1056 VLC_THREAD_PRIORITY_LOW ) )
1058 msg_Err( p_event->vd, "cannot create Vout EventThread" );
1059 return VLC_EGENERIC;
1062 vlc_mutex_lock( &p_event->lock );
1063 while( !p_event->b_ready )
1064 vlc_cond_wait( &p_event->wait, &p_event->lock );
1065 const bool b_error = p_event->b_error;
1066 vlc_mutex_unlock( &p_event->lock );
1070 vlc_join( p_event->thread, NULL );
1071 p_event->b_ready = false;
1072 return VLC_EGENERIC;
1074 msg_Dbg( p_event->vd, "Vout EventThread running" );
1077 p_hwnd->parent_window = p_event->parent_window;
1078 p_hwnd->hparent = p_event->hparent;
1079 p_hwnd->hwnd = p_event->hwnd;
1080 p_hwnd->hvideownd = p_event->hvideownd;
1081 p_hwnd->hfswnd = p_event->hfswnd;
1085 void EventThreadStop( event_thread_t *p_event )
1087 if( !p_event->b_ready )
1090 vlc_mutex_lock( &p_event->lock );
1091 p_event->b_done = true;
1092 vlc_mutex_unlock( &p_event->lock );
1094 /* we need to be sure Vout EventThread won't stay stuck in
1095 * GetMessage, so we send a fake message */
1097 PostMessage( p_event->hwnd, WM_NULL, 0, 0);
1099 vlc_join( p_event->thread, NULL );
1100 p_event->b_ready = false;