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 <errno.h> /* ENOMEM */
34 #include <ctype.h> /* tolower() */
37 # define _WIN32_WINNT 0x0500
40 #include <vlc_common.h>
41 #include <vlc_interface.h>
42 #include <vlc_playlist.h>
44 #include <vlc_vout_window.h>
51 #ifdef MODULE_NAME_IS_directx
54 #ifdef MODULE_NAME_IS_direct3d
57 #ifdef MODULE_NAME_IS_glwin32
66 //WINSHELLAPI BOOL WINAPI SHFullScreen(HWND hwndRequester, DWORD dwState);
69 /*#if defined(UNDER_CE) && !defined(__PLUGIN__) --FIXME*/
70 /*# define SHFS_SHOWSIPBUTTON 0x0004
71 # define SHFS_HIDESIPBUTTON 0x0008
72 # define MENU_HEIGHT 26
73 BOOL SHFullScreen(HWND hwndRequester, DWORD dwState);
76 /*****************************************************************************
78 *****************************************************************************/
79 #define WM_VLC_HIDE_MOUSE (WM_APP + 0)
80 #define WM_VLC_SHOW_MOUSE (WM_APP + 1)
81 #define WM_VLC_CHANGE_TEXT (WM_APP + 2)
85 vout_thread_t *p_vout;
100 volatile bool b_cursor_hidden;
101 volatile mtime_t i_lastmoved;
102 mtime_t i_mouse_hide_timeout;
108 vout_window_cfg_t wnd_cfg;
114 vout_window_t *parent_window;
121 static int DirectXCreateWindow( event_thread_t * );
122 static void DirectXCloseWindow ( event_thread_t * );
123 static long FAR PASCAL DirectXEventProc( HWND, UINT, WPARAM, LPARAM );
125 static void DirectXPopupMenu( event_thread_t *p_event, bool b_open )
127 var_SetBool( p_event->p_vout->p_libvlc, "intf-popupmenu", b_open );
130 static int DirectXConvertKey( int i_key );
132 /*****************************************************************************
133 * EventThread: Create video window & handle its messages
134 *****************************************************************************
135 * This function creates a video window and then enters an infinite loop
136 * that handles the messages sent to that window.
137 * The main goal of this thread is to isolate the Win32 PeekMessage function
138 * because this one can block for a long time.
139 *****************************************************************************/
140 static void *EventThread( void *p_this )
142 event_thread_t *p_event = (event_thread_t *)p_this;
143 vout_thread_t *p_vout = p_event->p_vout;
145 POINT old_mouse_pos = {0,0}, mouse_pos;
147 unsigned int i_width, i_height, i_x, i_y;
149 int canc = vlc_savecancel ();
151 vlc_mutex_lock( &p_event->lock );
152 /* Create a window for the video */
153 /* Creating a window under Windows also initializes the thread's event
155 if( DirectXCreateWindow( p_event ) )
156 p_event->b_error = true;
158 p_event->b_ready = true;
159 vlc_cond_signal( &p_event->wait );
161 const bool b_error = p_event->b_error;
162 vlc_mutex_unlock( &p_event->lock );
166 vlc_restorecancel( canc );
171 /* Set power management stuff */
172 if( (hkernel32 = GetModuleHandle( _T("KERNEL32") ) ) )
174 ULONG (WINAPI* OurSetThreadExecutionState)( ULONG );
176 OurSetThreadExecutionState = (ULONG (WINAPI*)( ULONG ))
177 GetProcAddress( hkernel32, _T("SetThreadExecutionState") );
179 if( OurSetThreadExecutionState )
180 /* Prevent monitor from powering off */
181 OurSetThreadExecutionState( ES_DISPLAY_REQUIRED | ES_CONTINUOUS );
183 msg_Dbg( p_vout, "no support for SetThreadExecutionState()" );
188 /* GetMessage will sleep if there's no message in the queue */
191 if( !GetMessage( &msg, 0, 0, 0 ) )
193 vlc_mutex_lock( &p_event->lock );
194 p_event->b_done = true;
195 vlc_mutex_unlock( &p_event->lock );
199 /* Check if we are asked to exit */
200 vlc_mutex_lock( &p_event->lock );
201 const bool b_done = p_event->b_done;
202 vlc_mutex_unlock( &p_event->lock );
207 switch( msg.message )
211 vlc_mutex_lock( &p_event->lock );
212 vout_PlacePicture( p_event->p_vout,
213 p_event->wnd_cfg.width,
214 p_event->wnd_cfg.height,
215 &i_x, &i_y, &i_width, &i_height );
216 vlc_mutex_unlock( &p_event->lock );
218 if( msg.hwnd == p_event->hvideownd )
224 if( i_width && i_height )
226 val.i_int = ( GET_X_LPARAM(msg.lParam) - i_x ) *
227 p_event->p_vout->fmt_in.i_visible_width / i_width +
228 p_event->p_vout->fmt_in.i_x_offset;
229 var_Set( p_event->p_vout, "mouse-x", val );
230 val.i_int = ( GET_Y_LPARAM(msg.lParam) - i_y ) *
231 p_event->p_vout->fmt_in.i_visible_height / i_height +
232 p_event->p_vout->fmt_in.i_y_offset;
233 var_Set( p_event->p_vout, "mouse-y", val );
235 var_SetBool( p_event->p_vout, "mouse-moved", true );
239 GetCursorPos( &mouse_pos );
240 if( (abs(mouse_pos.x - old_mouse_pos.x) > 2 ||
241 (abs(mouse_pos.y - old_mouse_pos.y)) > 2 ) )
243 GetCursorPos( &old_mouse_pos );
244 p_event->i_lastmoved = mdate();
246 if( p_event->b_cursor_hidden )
248 p_event->b_cursor_hidden = 0;
254 case WM_VLC_HIDE_MOUSE:
255 if( p_event->b_cursor_hidden ) break;
256 p_event->b_cursor_hidden = true;
257 GetCursorPos( &old_mouse_pos );
261 case WM_VLC_SHOW_MOUSE:
262 if( !p_event->b_cursor_hidden ) break;
263 p_event->b_cursor_hidden = false;
264 GetCursorPos( &old_mouse_pos );
269 var_Get( p_event->p_vout, "mouse-button-down", &val );
271 var_Set( p_event->p_vout, "mouse-button-down", val );
272 DirectXPopupMenu( p_event, false );
276 var_Get( p_event->p_vout, "mouse-button-down", &val );
278 var_Set( p_event->p_vout, "mouse-button-down", val );
280 var_SetBool( p_event->p_vout, "mouse-clicked", true );
283 case WM_LBUTTONDBLCLK:
284 vlc_mutex_lock( &p_event->lock );
285 p_event->i_changes |= VOUT_FULLSCREEN_CHANGE;
286 vlc_mutex_unlock( &p_event->lock );
290 var_Get( p_event->p_vout, "mouse-button-down", &val );
292 var_Set( p_event->p_vout, "mouse-button-down", val );
293 DirectXPopupMenu( p_event, false );
297 var_Get( p_event->p_vout, "mouse-button-down", &val );
299 var_Set( p_event->p_vout, "mouse-button-down", val );
303 var_Get( p_event->p_vout, "mouse-button-down", &val );
305 var_Set( p_event->p_vout, "mouse-button-down", val );
306 DirectXPopupMenu( p_event, false );
310 var_Get( p_event->p_vout, "mouse-button-down", &val );
312 var_Set( p_event->p_vout, "mouse-button-down", val );
313 DirectXPopupMenu( p_event, true );
318 /* The key events are first processed here and not translated
319 * into WM_CHAR events because we need to know the status of the
321 val.i_int = DirectXConvertKey( msg.wParam );
324 /* This appears to be a "normal" (ascii) key */
325 val.i_int = tolower( MapVirtualKey( msg.wParam, 2 ) );
330 if( GetKeyState(VK_CONTROL) & 0x8000 )
332 val.i_int |= KEY_MODIFIER_CTRL;
334 if( GetKeyState(VK_SHIFT) & 0x8000 )
336 val.i_int |= KEY_MODIFIER_SHIFT;
338 if( GetKeyState(VK_MENU) & 0x8000 )
340 val.i_int |= KEY_MODIFIER_ALT;
343 var_Set( p_vout->p_libvlc, "key-pressed", val );
348 if( GET_WHEEL_DELTA_WPARAM( msg.wParam ) > 0 )
350 val.i_int = KEY_MOUSEWHEELUP;
354 val.i_int = KEY_MOUSEWHEELDOWN;
358 if( GetKeyState(VK_CONTROL) & 0x8000 )
360 val.i_int |= KEY_MODIFIER_CTRL;
362 if( GetKeyState(VK_SHIFT) & 0x8000 )
364 val.i_int |= KEY_MODIFIER_SHIFT;
366 if( GetKeyState(VK_MENU) & 0x8000 )
368 val.i_int |= KEY_MODIFIER_ALT;
371 var_Set( p_vout->p_libvlc, "key-pressed", val );
375 case WM_VLC_CHANGE_TEXT:
377 vlc_mutex_lock( &p_event->lock );
378 wchar_t *pwz_title = NULL;
379 if( p_event->psz_title )
381 const size_t i_length = strlen(p_event->psz_title);
382 pwz_title = malloc( 2 * (i_length + 1) );
385 mbstowcs( pwz_title, p_event->psz_title, 2 * i_length );
386 pwz_title[i_length] = 0;
389 vlc_mutex_unlock( &p_event->lock );
393 SetWindowText( p_event->hwnd, (LPCTSTR)pwz_title );
394 if( p_event->hfswnd )
395 SetWindowText( p_event->hfswnd, (LPCTSTR)pwz_title );
402 /* Messages we don't handle directly are dispatched to the
403 * window procedure */
404 TranslateMessage(&msg);
405 DispatchMessage(&msg);
410 } /* End Main loop */
412 /* Check for WM_QUIT if we created the window */
413 if( !p_event->hparent && msg.message == WM_QUIT )
415 msg_Warn( p_vout, "WM_QUIT... should not happen!!" );
416 p_event->hwnd = NULL; /* Window already destroyed */
419 msg_Dbg( p_vout, "DirectXEventThread terminating" );
421 DirectXCloseWindow( p_event );
422 vlc_restorecancel(canc);
427 /* following functions are local */
429 /*****************************************************************************
430 * DirectXCreateWindow: create a window for the video.
431 *****************************************************************************
432 * Before creating a direct draw surface, we need to create a window in which
433 * the video will be displayed. This window will also allow us to capture the
435 *****************************************************************************/
436 static int DirectXCreateWindow( event_thread_t *p_event )
438 vout_thread_t *p_vout = p_event->p_vout;
442 WNDCLASS wc; /* window class components */
444 char vlc_path[MAX_PATH+1];
445 int i_style, i_stylex;
447 msg_Dbg( p_vout, "DirectXCreateWindow" );
449 /* Get this module's instance */
450 hInstance = GetModuleHandle(NULL);
452 #ifdef MODULE_NAME_IS_direct3d
453 if( !p_event->use_desktop )
456 /* If an external window was specified, we'll draw in it. */
457 p_event->parent_window = vout_window_New( VLC_OBJECT(p_vout), NULL, &p_event->wnd_cfg );
458 if( p_event->parent_window )
459 p_event->hparent = p_event->parent_window->handle.hwnd;
461 p_event->hparent = NULL;
462 #ifdef MODULE_NAME_IS_direct3d
466 /* Find Program Manager */
467 HWND hwnd = FindWindow( _T("Progman"), NULL );
468 if( hwnd ) hwnd = FindWindowEx( hwnd, NULL, _T("SHELLDLL_DefView"), NULL );
469 if( hwnd ) hwnd = FindWindowEx( hwnd, NULL, _T("SysListView32"), NULL );
471 msg_Err( p_vout, "Couldn't find desktop icon window. Desktop mode can't be established." );
472 p_event->hparent = hwnd;
476 /* Get the Icon from the main app */
479 if( GetModuleFileName( NULL, vlc_path, MAX_PATH ) )
481 vlc_icon = ExtractIcon( hInstance, vlc_path, 0 );
485 /* Fill in the window class structure */
486 wc.style = CS_OWNDC|CS_DBLCLKS; /* style: dbl click */
487 wc.lpfnWndProc = (WNDPROC)DirectXEventProc; /* event handler */
488 wc.cbClsExtra = 0; /* no extra class data */
489 wc.cbWndExtra = 0; /* no extra window data */
490 wc.hInstance = hInstance; /* instance */
491 wc.hIcon = vlc_icon; /* load the vlc big icon */
492 wc.hCursor = LoadCursor(NULL, IDC_ARROW); /* default cursor */
493 wc.hbrBackground = GetStockObject(BLACK_BRUSH); /* background color */
494 wc.lpszMenuName = NULL; /* no menu */
495 wc.lpszClassName = _T("VLC DirectX"); /* use a special class */
497 /* Register the window class */
498 if( !RegisterClass(&wc) )
502 if( vlc_icon ) DestroyIcon( vlc_icon );
504 /* Check why it failed. If it's because one already exists
505 * then fine, otherwise return with an error. */
506 if( !GetClassInfo( hInstance, _T("VLC DirectX"), &wndclass ) )
508 msg_Err( p_vout, "DirectXCreateWindow RegisterClass FAILED (err=%lu)", GetLastError() );
513 /* Register the video sub-window class */
514 wc.lpszClassName = _T("VLC DirectX video"); wc.hIcon = 0;
515 wc.hbrBackground = NULL; /* no background color */
516 if( !RegisterClass(&wc) )
520 /* Check why it failed. If it's because one already exists
521 * then fine, otherwise return with an error. */
522 if( !GetClassInfo( hInstance, _T("VLC DirectX video"), &wndclass ) )
524 msg_Err( p_vout, "DirectXCreateWindow RegisterClass FAILED (err=%lu)", GetLastError() );
529 /* When you create a window you give the dimensions you wish it to
530 * have. Unfortunatly these dimensions will include the borders and
531 * titlebar. We use the following function to find out the size of
532 * the window corresponding to the useable surface we want */
533 rect_window.top = 10;
534 rect_window.left = 10;
535 rect_window.right = rect_window.left + p_event->wnd_cfg.width;
536 rect_window.bottom = rect_window.top + p_event->wnd_cfg.height;
538 if( var_GetBool( p_vout, "video-deco" ) )
540 /* Open with window decoration */
541 AdjustWindowRect( &rect_window, WS_OVERLAPPEDWINDOW|WS_SIZEBOX, 0 );
542 i_style = WS_OVERLAPPEDWINDOW|WS_SIZEBOX|WS_VISIBLE|WS_CLIPCHILDREN;
547 /* No window decoration */
548 AdjustWindowRect( &rect_window, WS_POPUP, 0 );
549 i_style = WS_POPUP|WS_VISIBLE|WS_CLIPCHILDREN;
550 i_stylex = 0; // WS_EX_TOOLWINDOW; Is TOOLWINDOW really needed ?
551 // It messes up the fullscreen window.
554 if( p_event->hparent )
556 i_style = WS_VISIBLE|WS_CLIPCHILDREN|WS_CHILD;
560 p_event->i_window_style = i_style;
562 /* Create the window */
564 CreateWindowEx( WS_EX_NOPARENTNOTIFY | i_stylex,
565 _T("VLC DirectX"), /* name of window class */
566 _T(VOUT_TITLE) _T(" (DirectX Output)"), /* window title */
567 i_style, /* window style */
568 (p_event->wnd_cfg.x < 0) ? CW_USEDEFAULT :
569 (UINT)p_event->wnd_cfg.x, /* default X coordinate */
570 (p_event->wnd_cfg.y < 0) ? CW_USEDEFAULT :
571 (UINT)p_event->wnd_cfg.y, /* default Y coordinate */
572 rect_window.right - rect_window.left, /* window width */
573 rect_window.bottom - rect_window.top, /* window height */
574 p_event->hparent, /* parent window */
575 NULL, /* no menu in this window */
576 hInstance, /* handle of this program instance */
577 (LPVOID)p_event ); /* send p_vout to WM_CREATE */
581 msg_Warn( p_vout, "DirectXCreateWindow create window FAILED (err=%lu)", GetLastError() );
585 if( p_event->hparent )
589 /* We don't want the window owner to overwrite our client area */
590 i_style = GetWindowLong( p_event->hparent, GWL_STYLE );
592 if( !(i_style & WS_CLIPCHILDREN) )
593 /* Hmmm, apparently this is a blocking call... */
594 SetWindowLong( p_event->hparent, GWL_STYLE,
595 i_style | WS_CLIPCHILDREN );
597 /* Create our fullscreen window */
599 CreateWindowEx( WS_EX_APPWINDOW, _T("VLC DirectX"),
600 _T(VOUT_TITLE) _T(" (DirectX Output)"),
601 WS_OVERLAPPEDWINDOW|WS_CLIPCHILDREN|WS_SIZEBOX,
602 CW_USEDEFAULT, CW_USEDEFAULT,
603 CW_USEDEFAULT, CW_USEDEFAULT,
604 NULL, NULL, hInstance, NULL );
608 p_event->hfswnd = NULL;
611 /* Append a "Always On Top" entry in the system menu */
612 hMenu = GetSystemMenu( p_event->hwnd, FALSE );
613 AppendMenu( hMenu, MF_SEPARATOR, 0, _T("") );
614 AppendMenu( hMenu, MF_STRING | MF_UNCHECKED,
615 IDM_TOGGLE_ON_TOP, _T("Always on &Top") );
617 /* Create video sub-window. This sub window will always exactly match
618 * the size of the video, which allows us to use crazy overlay colorkeys
619 * without having them shown outside of the video area. */
621 CreateWindow( _T("VLC DirectX video"), _T(""), /* window class */
622 WS_CHILD, /* window style, not visible initially */
624 p_vout->render.i_width, /* default width */
625 p_vout->render.i_height, /* default height */
626 p_event->hwnd, /* parent window */
628 (LPVOID)p_event ); /* send p_vout to WM_CREATE */
630 if( !p_event->hvideownd )
631 msg_Warn( p_vout, "can't create video sub-window" );
633 msg_Dbg( p_vout, "created video sub-window" );
635 /* Now display the window */
636 ShowWindow( p_event->hwnd, SW_SHOW );
641 /*****************************************************************************
642 * DirectXCloseWindow: close the window created by DirectXCreateWindow
643 *****************************************************************************
644 * This function returns all resources allocated by DirectXCreateWindow.
645 *****************************************************************************/
646 static void DirectXCloseWindow( event_thread_t *p_event )
648 vout_thread_t *p_vout = p_event->p_vout;
649 msg_Dbg( p_vout, "DirectXCloseWindow" );
651 DestroyWindow( p_event->hwnd );
652 if( p_event->hfswnd ) DestroyWindow( p_event->hfswnd );
654 #ifdef MODULE_NAME_IS_direct3d
655 if( !p_event->use_desktop )
657 vout_window_Delete( 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 p_vout 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_thread_t *p_vout = p_event->p_vout;
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( p_vout ) msg_Dbg( p_vout, "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 UpdateRects( p_vout, true );
764 /* the user wants to close the window */
767 playlist_t * p_playlist = pl_Hold( p_vout );
770 playlist_Stop( p_playlist );
771 pl_Release( p_vout );
776 /* the window has been closed so shut down everything now */
778 msg_Dbg( p_vout, "WinProc WM_DESTROY" );
779 /* just destroy the window */
780 PostQuitMessage( 0 );
786 case IDM_TOGGLE_ON_TOP: /* toggle the "on top" status */
788 msg_Dbg( p_vout, "WinProc WM_SYSCOMMAND: IDM_TOGGLE_ON_TOP");
790 /* Change the current value */
791 var_ToggleBool( p_vout, "video-on-top" );
800 return DefWindowProc(hwnd, message, wParam, lParam);
803 #ifdef MODULE_NAME_IS_wingapi
807 if( hwnd == p_event->hfswnd )
809 HWND htbar = FindWindow( _T("HHTaskbar"), NULL );
810 ShowWindow( htbar, SW_SHOW );
813 if( !p_event->hparent ||
814 hwnd == p_event->hfswnd )
816 SHFullScreen( hwnd, SHFS_SHOWSIPBUTTON );
822 #ifdef MODULE_NAME_IS_wingapi
826 if( p_event->hparent &&
827 hwnd != p_event->hfswnd && p_vout->b_fullscreen )
829 vlc_mutex_lock( &p_event->lock );
830 p_event->i_changes |= VOUT_FULLSCREEN_CHANGE;
831 vlc_mutex_unlock( &p_event->lock );
834 if( hwnd == p_event->hfswnd )
836 HWND htbar = FindWindow( _T("HHTaskbar"), NULL );
837 ShowWindow( htbar, SW_HIDE );
840 if( !p_event->hparent ||
841 hwnd == p_event->hfswnd )
843 SHFullScreen( hwnd, SHFS_HIDESIPBUTTON );
849 //msg_Dbg( p_vout, "WinProc WM Default %i", message );
853 /* Let windows handle the message */
854 return DefWindowProc(hwnd, message, wParam, lParam);
862 } dxkeys_to_vlckeys[] =
864 { VK_F1, KEY_F1 }, { VK_F2, KEY_F2 }, { VK_F3, KEY_F3 }, { VK_F4, KEY_F4 },
865 { VK_F5, KEY_F5 }, { VK_F6, KEY_F6 }, { VK_F7, KEY_F7 }, { VK_F8, KEY_F8 },
866 { VK_F9, KEY_F9 }, { VK_F10, KEY_F10 }, { VK_F11, KEY_F11 },
869 { VK_RETURN, KEY_ENTER },
871 { VK_ESCAPE, KEY_ESC },
873 { VK_LEFT, KEY_LEFT },
874 { VK_RIGHT, KEY_RIGHT },
876 { VK_DOWN, KEY_DOWN },
878 { VK_HOME, KEY_HOME },
880 { VK_PRIOR, KEY_PAGEUP },
881 { VK_NEXT, KEY_PAGEDOWN },
883 { VK_INSERT, KEY_INSERT },
884 { VK_DELETE, KEY_DELETE },
890 { VK_BROWSER_BACK, KEY_BROWSER_BACK },
891 { VK_BROWSER_FORWARD, KEY_BROWSER_FORWARD },
892 { VK_BROWSER_REFRESH, KEY_BROWSER_REFRESH },
893 { VK_BROWSER_STOP, KEY_BROWSER_STOP },
894 { VK_BROWSER_SEARCH, KEY_BROWSER_SEARCH },
895 { VK_BROWSER_FAVORITES, KEY_BROWSER_FAVORITES },
896 { VK_BROWSER_HOME, KEY_BROWSER_HOME },
897 { VK_VOLUME_MUTE, KEY_VOLUME_MUTE },
898 { VK_VOLUME_DOWN, KEY_VOLUME_DOWN },
899 { VK_VOLUME_UP, KEY_VOLUME_UP },
900 { VK_MEDIA_NEXT_TRACK, KEY_MEDIA_NEXT_TRACK },
901 { VK_MEDIA_PREV_TRACK, KEY_MEDIA_PREV_TRACK },
902 { VK_MEDIA_STOP, KEY_MEDIA_STOP },
903 { VK_MEDIA_PLAY_PAUSE, KEY_MEDIA_PLAY_PAUSE },
908 static int DirectXConvertKey( int i_key )
912 for( i = 0; dxkeys_to_vlckeys[i].i_dxkey != 0; i++ )
914 if( dxkeys_to_vlckeys[i].i_dxkey == i_key )
916 return dxkeys_to_vlckeys[i].i_vlckey;
923 void EventThreadMouseAutoHide( event_thread_t *p_event )
925 vout_thread_t *p_vout = p_event->p_vout;
927 if( p_vout->b_fullscreen &&
928 !p_event->b_cursor_hidden &&
929 (mdate() - p_event->i_lastmoved) > p_event->i_mouse_hide_timeout )
931 /* Hide the cursor only if it is inside our window */
933 GetCursorPos( &point );
935 HWND hwnd = WindowFromPoint(point);
936 if( hwnd == p_event->hwnd || hwnd == p_event->hvideownd )
938 PostMessage( p_event->hwnd, WM_VLC_HIDE_MOUSE, 0, 0 );
942 p_event->i_lastmoved = mdate();
946 void EventThreadMouseShow( event_thread_t *p_event )
948 PostMessage( p_event->hwnd, WM_VLC_SHOW_MOUSE, 0, 0 );
950 void EventThreadUpdateTitle( event_thread_t *p_event, const char *psz_fallback )
952 char *psz_title = var_GetNonEmptyString( p_event->p_vout, "video-title" );
954 psz_title = strdup( psz_fallback );
958 vlc_mutex_lock( &p_event->lock );
959 free( p_event->psz_title );
960 p_event->psz_title = psz_title;
961 vlc_mutex_unlock( &p_event->lock );
963 PostMessage( p_event->hwnd, WM_VLC_CHANGE_TEXT, 0, 0 );
965 unsigned EventThreadRetreiveChanges( event_thread_t *p_event )
967 vlc_mutex_lock( &p_event->lock );
968 unsigned i_changes = p_event->i_changes;
969 p_event->i_changes = 0;
970 vlc_mutex_unlock( &p_event->lock );
974 int EventThreadGetWindowStyle( event_thread_t *p_event )
976 /* No need to lock, it is serialized by EventThreadStart */
977 return p_event->i_window_style;
980 void EventThreadUpdateWindowPosition( event_thread_t *p_event, bool *pb_changed,
981 int x, int y, int w, int h )
983 vlc_mutex_lock( &p_event->lock );
984 *pb_changed = x != p_event->wnd_cfg.x ||
985 y != p_event->wnd_cfg.y ||
986 w != p_event->wnd_cfg.width ||
987 h != p_event->wnd_cfg.height;
989 p_event->wnd_cfg.x = x;
990 p_event->wnd_cfg.y = y;
991 p_event->wnd_cfg.width = w;
992 p_event->wnd_cfg.height = h;
993 vlc_mutex_unlock( &p_event->lock );
996 void EventThreadUseOverlay( event_thread_t *p_event, bool b_used )
998 vlc_mutex_lock( &p_event->lock );
999 p_event->use_overlay = b_used;
1000 vlc_mutex_unlock( &p_event->lock );
1003 event_thread_t *EventThreadCreate( vout_thread_t *p_vout, const vout_window_cfg_t *p_wnd_cfg )
1005 /* Create the Vout EventThread, this thread is created by us to isolate
1006 * the Win32 PeekMessage function calls. We want to do this because
1007 * Windows can stay blocked inside this call for a long time, and when
1008 * this happens it thus blocks vlc's video_output thread.
1009 * Vout EventThread will take care of the creation of the video
1010 * window (because PeekMessage has to be called from the same thread which
1011 * created the window). */
1012 msg_Dbg( p_vout, "creating Vout EventThread" );
1013 event_thread_t *p_event = malloc( sizeof(*p_event) );
1017 p_event->p_vout = p_vout;
1018 vlc_mutex_init( &p_event->lock );
1019 vlc_cond_init( &p_event->wait );
1021 p_event->b_cursor_hidden = false;
1022 p_event->i_lastmoved = mdate();
1023 p_event->i_mouse_hide_timeout =
1024 var_GetInteger(p_vout, "mouse-hide-timeout") * 1000;
1025 p_event->psz_title = NULL;
1026 p_event->wnd_cfg = *p_wnd_cfg;
1031 void EventThreadDestroy( event_thread_t *p_event )
1033 free( p_event->psz_title );
1034 vlc_cond_destroy( &p_event->wait );
1035 vlc_mutex_destroy( &p_event->lock );
1039 int EventThreadStart( event_thread_t *p_event, event_hwnd_t *p_hwnd, const event_cfg_t *p_cfg )
1041 p_event->use_desktop = p_cfg->use_desktop;
1042 p_event->use_overlay = p_cfg->use_overlay;
1044 p_event->i_changes = 0;
1046 p_event->b_ready = false;
1047 p_event->b_done = false;
1048 p_event->b_error = false;
1050 if( vlc_clone( &p_event->thread, EventThread, p_event,
1051 VLC_THREAD_PRIORITY_LOW ) )
1053 msg_Err( p_event->p_vout, "cannot create Vout EventThread" );
1054 return VLC_EGENERIC;
1057 vlc_mutex_lock( &p_event->lock );
1058 while( !p_event->b_ready )
1059 vlc_cond_wait( &p_event->wait, &p_event->lock );
1060 const bool b_error = p_event->b_error;
1061 vlc_mutex_unlock( &p_event->lock );
1065 vlc_join( p_event->thread, NULL );
1066 p_event->b_ready = false;
1067 return VLC_EGENERIC;
1069 msg_Dbg( p_event->p_vout, "Vout EventThread running" );
1071 if( !p_event->use_desktop )
1072 p_hwnd->parent_window = p_event->parent_window;
1074 p_hwnd->parent_window = NULL;
1075 p_hwnd->hparent = p_event->hparent;
1076 p_hwnd->hwnd = p_event->hwnd;
1077 p_hwnd->hvideownd = p_event->hvideownd;
1078 p_hwnd->hfswnd = p_event->hfswnd;
1082 void EventThreadStop( event_thread_t *p_event )
1084 if( !p_event->b_ready )
1087 vlc_mutex_lock( &p_event->lock );
1088 p_event->b_done = true;
1089 vlc_mutex_unlock( &p_event->lock );
1091 /* we need to be sure Vout EventThread won't stay stuck in
1092 * GetMessage, so we send a fake message */
1094 PostMessage( p_event->hwnd, WM_NULL, 0, 0);
1096 vlc_join( p_event->thread, NULL );
1097 p_event->b_ready = false;