]> git.sesse.net Git - vlc/blob - modules/video_output/msw/events.c
642ae914ae1cf32f7b699c35786ea1b9948d5bee
[vlc] / modules / video_output / msw / events.c
1 /*****************************************************************************
2  * events.c: Windows DirectX video output events handler
3  *****************************************************************************
4  * Copyright (C) 2001-2009 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Gildas Bazin <gbazin@videolan.org>
8  *
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.
13  *
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.
18  *
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  *****************************************************************************/
23
24
25 /*****************************************************************************
26  * Preamble: This file contains the functions related to the creation of
27  *             a window and the handling of its messages (events).
28  *****************************************************************************/
29 #ifdef HAVE_CONFIG_H
30 # include "config.h"
31 #endif
32
33 #include <errno.h>                                                 /* ENOMEM */
34 #include <ctype.h>                                              /* tolower() */
35
36 #ifndef _WIN32_WINNT
37 #   define _WIN32_WINNT 0x0500
38 #endif
39
40 #include <vlc_common.h>
41 #include <vlc_interface.h>
42 #include <vlc_playlist.h>
43 #include <vlc_vout.h>
44 #include <vlc_vout_window.h>
45
46 #include <windows.h>
47 #include <tchar.h>
48 #include <windowsx.h>
49 #include <shellapi.h>
50
51 #ifdef MODULE_NAME_IS_directx
52 #include <ddraw.h>
53 #endif
54 #ifdef MODULE_NAME_IS_direct3d
55 #include <d3d9.h>
56 #endif
57 #ifdef MODULE_NAME_IS_glwin32
58 #include <GL/gl.h>
59 #endif
60
61 #include <vlc_keys.h>
62 #include "vout.h"
63
64 #ifdef UNDER_CE
65 #include <aygshell.h>
66     //WINSHELLAPI BOOL WINAPI SHFullScreen(HWND hwndRequester, DWORD dwState);
67 #endif
68
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);
74 #endif*/
75
76
77 /*****************************************************************************
78  * Local prototypes.
79  *****************************************************************************/
80 static int  DirectXCreateWindow( vout_thread_t *p_vout );
81 static void DirectXCloseWindow ( vout_thread_t *p_vout );
82 static long FAR PASCAL DirectXEventProc( HWND, UINT, WPARAM, LPARAM );
83
84 static void DirectXPopupMenu( event_thread_t *p_event, bool b_open )
85 {
86     vlc_value_t val;
87     val.b_bool = b_open;
88     var_Set( p_event->p_vout->p_libvlc, "intf-popupmenu", val );
89 }
90
91 static int DirectXConvertKey( int i_key );
92
93 /*****************************************************************************
94  * EventThread: Create video window & handle its messages
95  *****************************************************************************
96  * This function creates a video window and then enters an infinite loop
97  * that handles the messages sent to that window.
98  * The main goal of this thread is to isolate the Win32 PeekMessage function
99  * because this one can block for a long time.
100  *****************************************************************************/
101 static void *EventThread( void *p_this )
102 {
103     event_thread_t *p_event = (event_thread_t *)p_this;
104     vout_thread_t *p_vout = p_event->p_vout;
105     MSG msg;
106     POINT old_mouse_pos = {0,0}, mouse_pos;
107     vlc_value_t val;
108     unsigned int i_width, i_height, i_x, i_y;
109     HMODULE hkernel32;
110     int canc = vlc_savecancel ();
111
112     vlc_mutex_lock( &p_event->lock );
113     /* Create a window for the video */
114     /* Creating a window under Windows also initializes the thread's event
115      * message queue */
116     if( DirectXCreateWindow( p_event->p_vout ) )
117         p_event->b_error = true;
118
119     p_event->b_ready = true;
120     vlc_cond_signal( &p_event->wait );
121
122     const bool b_error = p_event->b_error;
123     vlc_mutex_unlock( &p_event->lock );
124
125     if( b_error )
126     {
127         vlc_restorecancel( canc );
128         return NULL;
129     }
130
131 #ifndef UNDER_CE
132     /* Set power management stuff */
133     if( (hkernel32 = GetModuleHandle( _T("KERNEL32") ) ) )
134     {
135         ULONG (WINAPI* OurSetThreadExecutionState)( ULONG );
136
137         OurSetThreadExecutionState = (ULONG (WINAPI*)( ULONG ))
138             GetProcAddress( hkernel32, _T("SetThreadExecutionState") );
139
140         if( OurSetThreadExecutionState )
141             /* Prevent monitor from powering off */
142             OurSetThreadExecutionState( ES_DISPLAY_REQUIRED | ES_CONTINUOUS );
143         else
144             msg_Dbg( p_vout, "no support for SetThreadExecutionState()" );
145     }
146 #endif
147
148     /* Main loop */
149     /* GetMessage will sleep if there's no message in the queue */
150     for( ;; )
151     {
152         if( !GetMessage( &msg, 0, 0, 0 ) )
153         {
154             vlc_mutex_lock( &p_event->lock );
155             p_event->b_done = true;
156             vlc_mutex_unlock( &p_event->lock );
157             break;
158         }
159
160         /* Check if we are asked to exit */
161         vlc_mutex_lock( &p_event->lock );
162         const bool b_done = p_event->b_done;
163         vlc_mutex_unlock( &p_event->lock );
164         if( b_done )
165             break;
166
167         /* */
168         switch( msg.message )
169         {
170
171         case WM_MOUSEMOVE:
172             vout_PlacePicture( p_event->p_vout,
173                                p_event->p_vout->p_sys->i_window_width,
174                                p_event->p_vout->p_sys->i_window_height,
175                                &i_x, &i_y, &i_width, &i_height );
176
177             if( msg.hwnd == p_event->p_vout->p_sys->hvideownd )
178             {
179                 /* Child window */
180                 i_x = i_y = 0;
181             }
182
183             if( i_width && i_height )
184             {
185                 val.i_int = ( GET_X_LPARAM(msg.lParam) - i_x ) *
186                     p_event->p_vout->fmt_in.i_visible_width / i_width +
187                     p_event->p_vout->fmt_in.i_x_offset;
188                 var_Set( p_event->p_vout, "mouse-x", val );
189                 val.i_int = ( GET_Y_LPARAM(msg.lParam) - i_y ) *
190                     p_event->p_vout->fmt_in.i_visible_height / i_height +
191                     p_event->p_vout->fmt_in.i_y_offset;
192                 var_Set( p_event->p_vout, "mouse-y", val );
193
194                 var_SetBool( p_event->p_vout, "mouse-moved", true );
195             }
196
197         case WM_NCMOUSEMOVE:
198             GetCursorPos( &mouse_pos );
199             if( (abs(mouse_pos.x - old_mouse_pos.x) > 2 ||
200                 (abs(mouse_pos.y - old_mouse_pos.y)) > 2 ) )
201             {
202                 GetCursorPos( &old_mouse_pos );
203                 p_event->i_lastmoved = mdate();
204
205                 if( p_event->b_cursor_hidden )
206                 {
207                     p_event->b_cursor_hidden = 0;
208                     ShowCursor( TRUE );
209                 }
210             }
211             break;
212
213         case WM_VLC_HIDE_MOUSE:
214             if( p_event->b_cursor_hidden ) break;
215             p_event->b_cursor_hidden = true;
216             GetCursorPos( &old_mouse_pos );
217             ShowCursor( FALSE );
218             break;
219
220         case WM_VLC_SHOW_MOUSE:
221             if( !p_event->b_cursor_hidden ) break;
222             p_event->b_cursor_hidden = false;
223             GetCursorPos( &old_mouse_pos );
224             ShowCursor( TRUE );
225             break;
226
227         case WM_LBUTTONDOWN:
228             var_Get( p_event->p_vout, "mouse-button-down", &val );
229             val.i_int |= 1;
230             var_Set( p_event->p_vout, "mouse-button-down", val );
231             DirectXPopupMenu( p_event, false );
232             break;
233
234         case WM_LBUTTONUP:
235             var_Get( p_event->p_vout, "mouse-button-down", &val );
236             val.i_int &= ~1;
237             var_Set( p_event->p_vout, "mouse-button-down", val );
238
239             var_SetBool( p_event->p_vout, "mouse-clicked", true );
240             break;
241
242         case WM_LBUTTONDBLCLK:
243             p_event->p_vout->p_sys->i_changes |= VOUT_FULLSCREEN_CHANGE;
244             break;
245
246         case WM_MBUTTONDOWN:
247             var_Get( p_event->p_vout, "mouse-button-down", &val );
248             val.i_int |= 2;
249             var_Set( p_event->p_vout, "mouse-button-down", val );
250             DirectXPopupMenu( p_event, false );
251             break;
252
253         case WM_MBUTTONUP:
254             var_Get( p_event->p_vout, "mouse-button-down", &val );
255             val.i_int &= ~2;
256             var_Set( p_event->p_vout, "mouse-button-down", val );
257             break;
258
259         case WM_RBUTTONDOWN:
260             var_Get( p_event->p_vout, "mouse-button-down", &val );
261             val.i_int |= 4;
262             var_Set( p_event->p_vout, "mouse-button-down", val );
263             DirectXPopupMenu( p_event, false );
264             break;
265
266         case WM_RBUTTONUP:
267             var_Get( p_event->p_vout, "mouse-button-down", &val );
268             val.i_int &= ~4;
269             var_Set( p_event->p_vout, "mouse-button-down", val );
270             DirectXPopupMenu( p_event, true );
271             break;
272
273         case WM_KEYDOWN:
274         case WM_SYSKEYDOWN:
275             /* The key events are first processed here and not translated
276              * into WM_CHAR events because we need to know the status of the
277              * modifier keys. */
278             val.i_int = DirectXConvertKey( msg.wParam );
279             if( !val.i_int )
280             {
281                 /* This appears to be a "normal" (ascii) key */
282                 val.i_int = tolower( MapVirtualKey( msg.wParam, 2 ) );
283             }
284
285             if( val.i_int )
286             {
287                 if( GetKeyState(VK_CONTROL) & 0x8000 )
288                 {
289                     val.i_int |= KEY_MODIFIER_CTRL;
290                 }
291                 if( GetKeyState(VK_SHIFT) & 0x8000 )
292                 {
293                     val.i_int |= KEY_MODIFIER_SHIFT;
294                 }
295                 if( GetKeyState(VK_MENU) & 0x8000 )
296                 {
297                     val.i_int |= KEY_MODIFIER_ALT;
298                 }
299
300                 var_Set( p_vout->p_libvlc, "key-pressed", val );
301             }
302             break;
303
304         case WM_MOUSEWHEEL:
305             if( GET_WHEEL_DELTA_WPARAM( msg.wParam ) > 0 )
306             {
307                 val.i_int = KEY_MOUSEWHEELUP;
308             }
309             else
310             {
311                 val.i_int = KEY_MOUSEWHEELDOWN;
312             }
313             if( val.i_int )
314             {
315                 if( GetKeyState(VK_CONTROL) & 0x8000 )
316                 {
317                     val.i_int |= KEY_MODIFIER_CTRL;
318                 }
319                 if( GetKeyState(VK_SHIFT) & 0x8000 )
320                 {
321                     val.i_int |= KEY_MODIFIER_SHIFT;
322                 }
323                 if( GetKeyState(VK_MENU) & 0x8000 )
324                 {
325                     val.i_int |= KEY_MODIFIER_ALT;
326                 }
327
328                 var_Set( p_vout->p_libvlc, "key-pressed", val );
329             }
330             break;
331
332         case WM_VLC_CHANGE_TEXT:
333             var_Get( p_vout, "video-title", &val );
334             if( !val.psz_string || !*val.psz_string ) /* Default video title */
335             {
336                 free( val.psz_string );
337
338 #ifdef MODULE_NAME_IS_wingdi
339                 val.psz_string = strdup( VOUT_TITLE " (WinGDI output)" );
340 #endif
341 #ifdef MODULE_NAME_IS_wingapi
342                 val.psz_string = strdup( VOUT_TITLE " (WinGAPI output)" );
343 #endif
344 #ifdef MODULE_NAME_IS_glwin32
345                 val.psz_string = strdup( VOUT_TITLE " (OpenGL output)" );
346 #endif
347 #ifdef MODULE_NAME_IS_direct3d
348                 val.psz_string = strdup( VOUT_TITLE " (Direct3D output)" );
349 #endif
350 #ifdef MODULE_NAME_IS_directx
351                 if( p_event->p_vout->p_sys->b_using_overlay ) val.psz_string =
352                     strdup( VOUT_TITLE " (hardware YUV overlay DirectX output)" );
353                 else if( p_event->p_vout->p_sys->b_hw_yuv ) val.psz_string =
354                     strdup( VOUT_TITLE " (hardware YUV DirectX output)" );
355                 else val.psz_string =
356                     strdup( VOUT_TITLE " (software RGB DirectX output)" );
357 #endif
358             }
359
360             {
361                 wchar_t *psz_title = malloc( strlen(val.psz_string) * 2 + 2 );
362                 if( psz_title )
363                 {
364                     mbstowcs( psz_title, val.psz_string, strlen(val.psz_string)*2);
365                     psz_title[strlen(val.psz_string)] = 0;
366                     free( val.psz_string ); val.psz_string = (char *)psz_title;
367                 }
368             }
369
370             SetWindowText( p_event->p_vout->p_sys->hwnd,
371                            (LPCTSTR)val.psz_string );
372             if( p_event->p_vout->p_sys->hfswnd )
373                 SetWindowText( p_event->p_vout->p_sys->hfswnd,
374                                (LPCTSTR)val.psz_string );
375             free( val.psz_string );
376             break;
377
378         default:
379             /* Messages we don't handle directly are dispatched to the
380              * window procedure */
381             TranslateMessage(&msg);
382             DispatchMessage(&msg);
383             break;
384
385         } /* End Switch */
386
387     } /* End Main loop */
388
389     /* Check for WM_QUIT if we created the window */
390     if( !p_event->p_vout->p_sys->hparent && msg.message == WM_QUIT )
391     {
392         msg_Warn( p_vout, "WM_QUIT... should not happen!!" );
393         p_event->p_vout->p_sys->hwnd = NULL; /* Window already destroyed */
394     }
395
396     msg_Dbg( p_vout, "DirectXEventThread terminating" );
397
398     DirectXCloseWindow( p_event->p_vout );
399     vlc_restorecancel(canc);
400     return NULL;
401 }
402
403
404 /* following functions are local */
405
406 /*****************************************************************************
407  * DirectXCreateWindow: create a window for the video.
408  *****************************************************************************
409  * Before creating a direct draw surface, we need to create a window in which
410  * the video will be displayed. This window will also allow us to capture the
411  * events.
412  *****************************************************************************/
413 static int DirectXCreateWindow( vout_thread_t *p_vout )
414 {
415     HINSTANCE  hInstance;
416     HMENU      hMenu;
417     RECT       rect_window;
418     WNDCLASS   wc;                            /* window class components */
419     HICON      vlc_icon = NULL;
420     char       vlc_path[MAX_PATH+1];
421     int        i_style, i_stylex;
422
423     msg_Dbg( p_vout, "DirectXCreateWindow" );
424
425     /* Get this module's instance */
426     hInstance = GetModuleHandle(NULL);
427
428     #ifdef MODULE_NAME_IS_direct3d
429     if( !p_vout->p_sys->b_desktop )
430     {
431     #endif
432         vout_window_cfg_t wnd_cfg;
433         memset( &wnd_cfg, 0, sizeof(wnd_cfg) );
434         wnd_cfg.type   = VOUT_WINDOW_TYPE_HWND;
435         wnd_cfg.x      = p_vout->p_sys->i_window_x;
436         wnd_cfg.y      = p_vout->p_sys->i_window_y;
437         wnd_cfg.width  = p_vout->p_sys->i_window_width;
438         wnd_cfg.height = p_vout->p_sys->i_window_height;
439
440         /* If an external window was specified, we'll draw in it. */
441         p_vout->p_sys->parent_window = vout_window_New( VLC_OBJECT(p_vout), NULL, &wnd_cfg );
442         if( p_vout->p_sys->parent_window )
443             p_vout->p_sys->hparent = p_vout->p_sys->parent_window->handle.hwnd;
444     #ifdef MODULE_NAME_IS_direct3d
445     }
446     else
447     {
448         /* Find Program Manager */
449         HWND hwnd = FindWindow( _T("Progman"), NULL );
450         if( hwnd ) hwnd = FindWindowEx( hwnd, NULL, _T("SHELLDLL_DefView"), NULL );
451         if( hwnd ) hwnd = FindWindowEx( hwnd, NULL, _T("SysListView32"), NULL );
452         if( !hwnd )
453             msg_Err( p_vout, "Couldn't find desktop icon window. Desktop mode can't be established." );
454         p_vout->p_sys->hparent = hwnd;
455     }
456     #endif
457
458     /* We create the window ourself, there is no previous window proc. */
459     p_vout->p_sys->pf_wndproc = NULL;
460
461     /* Get the Icon from the main app */
462     vlc_icon = NULL;
463 #ifndef UNDER_CE
464     if( GetModuleFileName( NULL, vlc_path, MAX_PATH ) )
465     {
466         vlc_icon = ExtractIcon( hInstance, vlc_path, 0 );
467     }
468 #endif
469
470     /* Fill in the window class structure */
471     wc.style         = CS_OWNDC|CS_DBLCLKS;          /* style: dbl click */
472     wc.lpfnWndProc   = (WNDPROC)DirectXEventProc;       /* event handler */
473     wc.cbClsExtra    = 0;                         /* no extra class data */
474     wc.cbWndExtra    = 0;                        /* no extra window data */
475     wc.hInstance     = hInstance;                            /* instance */
476     wc.hIcon         = vlc_icon;                /* load the vlc big icon */
477     wc.hCursor       = LoadCursor(NULL, IDC_ARROW);    /* default cursor */
478     wc.hbrBackground = GetStockObject(BLACK_BRUSH);  /* background color */
479     wc.lpszMenuName  = NULL;                                  /* no menu */
480     wc.lpszClassName = _T("VLC DirectX");         /* use a special class */
481
482     /* Register the window class */
483     if( !RegisterClass(&wc) )
484     {
485         WNDCLASS wndclass;
486
487         if( vlc_icon ) DestroyIcon( vlc_icon );
488
489         /* Check why it failed. If it's because one already exists
490          * then fine, otherwise return with an error. */
491         if( !GetClassInfo( hInstance, _T("VLC DirectX"), &wndclass ) )
492         {
493             msg_Err( p_vout, "DirectXCreateWindow RegisterClass FAILED (err=%lu)", GetLastError() );
494             return VLC_EGENERIC;
495         }
496     }
497
498     /* Register the video sub-window class */
499     wc.lpszClassName = _T("VLC DirectX video"); wc.hIcon = 0;
500     wc.hbrBackground = NULL; /* no background color */
501     if( !RegisterClass(&wc) )
502     {
503         WNDCLASS wndclass;
504
505         /* Check why it failed. If it's because one already exists
506          * then fine, otherwise return with an error. */
507         if( !GetClassInfo( hInstance, _T("VLC DirectX video"), &wndclass ) )
508         {
509             msg_Err( p_vout, "DirectXCreateWindow RegisterClass FAILED (err=%lu)", GetLastError() );
510             return VLC_EGENERIC;
511         }
512     }
513
514     /* When you create a window you give the dimensions you wish it to
515      * have. Unfortunatly these dimensions will include the borders and
516      * titlebar. We use the following function to find out the size of
517      * the window corresponding to the useable surface we want */
518     rect_window.top    = 10;
519     rect_window.left   = 10;
520     rect_window.right  = rect_window.left + p_vout->p_sys->i_window_width;
521     rect_window.bottom = rect_window.top + p_vout->p_sys->i_window_height;
522
523     if( var_GetBool( p_vout, "video-deco" ) )
524     {
525         /* Open with window decoration */
526         AdjustWindowRect( &rect_window, WS_OVERLAPPEDWINDOW|WS_SIZEBOX, 0 );
527         i_style = WS_OVERLAPPEDWINDOW|WS_SIZEBOX|WS_VISIBLE|WS_CLIPCHILDREN;
528         i_stylex = 0;
529     }
530     else
531     {
532         /* No window decoration */
533         AdjustWindowRect( &rect_window, WS_POPUP, 0 );
534         i_style = WS_POPUP|WS_VISIBLE|WS_CLIPCHILDREN;
535         i_stylex = 0; // WS_EX_TOOLWINDOW; Is TOOLWINDOW really needed ?
536                       // It messes up the fullscreen window.
537     }
538
539     if( p_vout->p_sys->hparent )
540     {
541         i_style = WS_VISIBLE|WS_CLIPCHILDREN|WS_CHILD;
542         i_stylex = 0;
543     }
544
545     p_vout->p_sys->i_window_style = i_style;
546
547     /* Create the window */
548     p_vout->p_sys->hwnd =
549         CreateWindowEx( WS_EX_NOPARENTNOTIFY | i_stylex,
550                     _T("VLC DirectX"),               /* name of window class */
551                     _T(VOUT_TITLE) _T(" (DirectX Output)"),  /* window title */
552                     i_style,                                 /* window style */
553                     (p_vout->p_sys->i_window_x < 0) ? CW_USEDEFAULT :
554                         (UINT)p_vout->p_sys->i_window_x,   /* default X coordinate */
555                     (p_vout->p_sys->i_window_y < 0) ? CW_USEDEFAULT :
556                         (UINT)p_vout->p_sys->i_window_y,   /* default Y coordinate */
557                     rect_window.right - rect_window.left,    /* window width */
558                     rect_window.bottom - rect_window.top,   /* window height */
559                     p_vout->p_sys->hparent,                 /* parent window */
560                     NULL,                          /* no menu in this window */
561                     hInstance,            /* handle of this program instance */
562                     (LPVOID)p_vout );            /* send p_vout to WM_CREATE */
563
564     if( !p_vout->p_sys->hwnd )
565     {
566         msg_Warn( p_vout, "DirectXCreateWindow create window FAILED (err=%lu)", GetLastError() );
567         return VLC_EGENERIC;
568     }
569
570     if( p_vout->p_sys->hparent )
571     {
572         LONG i_style;
573
574         /* We don't want the window owner to overwrite our client area */
575         i_style = GetWindowLong( p_vout->p_sys->hparent, GWL_STYLE );
576
577         if( !(i_style & WS_CLIPCHILDREN) )
578             /* Hmmm, apparently this is a blocking call... */
579             SetWindowLong( p_vout->p_sys->hparent, GWL_STYLE,
580                            i_style | WS_CLIPCHILDREN );
581
582         /* Create our fullscreen window */
583         p_vout->p_sys->hfswnd =
584             CreateWindowEx( WS_EX_APPWINDOW, _T("VLC DirectX"),
585                             _T(VOUT_TITLE) _T(" (DirectX Output)"),
586                             WS_OVERLAPPEDWINDOW|WS_CLIPCHILDREN|WS_SIZEBOX,
587                             CW_USEDEFAULT, CW_USEDEFAULT,
588                             CW_USEDEFAULT, CW_USEDEFAULT,
589                             NULL, NULL, hInstance, NULL );
590     }
591
592     /* Append a "Always On Top" entry in the system menu */
593     hMenu = GetSystemMenu( p_vout->p_sys->hwnd, FALSE );
594     AppendMenu( hMenu, MF_SEPARATOR, 0, _T("") );
595     AppendMenu( hMenu, MF_STRING | MF_UNCHECKED,
596                        IDM_TOGGLE_ON_TOP, _T("Always on &Top") );
597
598     /* Create video sub-window. This sub window will always exactly match
599      * the size of the video, which allows us to use crazy overlay colorkeys
600      * without having them shown outside of the video area. */
601     p_vout->p_sys->hvideownd =
602     CreateWindow( _T("VLC DirectX video"), _T(""),   /* window class */
603         WS_CHILD,                   /* window style, not visible initially */
604         0, 0,
605         p_vout->render.i_width,         /* default width */
606         p_vout->render.i_height,        /* default height */
607         p_vout->p_sys->hwnd,                    /* parent window */
608         NULL, hInstance,
609         (LPVOID)p_vout );            /* send p_vout to WM_CREATE */
610
611     if( !p_vout->p_sys->hvideownd )
612         msg_Warn( p_vout, "can't create video sub-window" );
613     else
614         msg_Dbg( p_vout, "created video sub-window" );
615
616     /* Now display the window */
617     ShowWindow( p_vout->p_sys->hwnd, SW_SHOW );
618
619     return VLC_SUCCESS;
620 }
621
622 /*****************************************************************************
623  * DirectXCloseWindow: close the window created by DirectXCreateWindow
624  *****************************************************************************
625  * This function returns all resources allocated by DirectXCreateWindow.
626  *****************************************************************************/
627 static void DirectXCloseWindow( vout_thread_t *p_vout )
628 {
629     msg_Dbg( p_vout, "DirectXCloseWindow" );
630
631     DestroyWindow( p_vout->p_sys->hwnd );
632     if( p_vout->p_sys->hfswnd ) DestroyWindow( p_vout->p_sys->hfswnd );
633
634     #ifdef MODULE_NAME_IS_direct3d
635     if( !p_vout->p_sys->b_desktop )
636     #endif
637         vout_window_Delete( p_vout->p_sys->parent_window );
638     p_vout->p_sys->hwnd = NULL;
639
640     /* We don't unregister the Window Class because it could lead to race
641      * conditions and it will be done anyway by the system when the app will
642      * exit */
643 }
644
645 /*****************************************************************************
646  * DirectXEventProc: This is the window event processing function.
647  *****************************************************************************
648  * On Windows, when you create a window you have to attach an event processing
649  * function to it. The aim of this function is to manage "Queued Messages" and
650  * "Nonqueued Messages".
651  * Queued Messages are those picked up and retransmitted by vout_Manage
652  * (using the GetMessage and DispatchMessage functions).
653  * Nonqueued Messages are those that Windows will send directly to this
654  * procedure (like WM_DESTROY, WM_WINDOWPOSCHANGED...)
655  *****************************************************************************/
656 static long FAR PASCAL DirectXEventProc( HWND hwnd, UINT message,
657                                          WPARAM wParam, LPARAM lParam )
658 {
659     vout_thread_t *p_vout;
660
661     if( message == WM_CREATE )
662     {
663         /* Store p_vout for future use */
664         p_vout = (vout_thread_t *)((CREATESTRUCT *)lParam)->lpCreateParams;
665         SetWindowLongPtr( hwnd, GWLP_USERDATA, (LONG_PTR)p_vout );
666         return TRUE;
667     }
668     else
669     {
670         LONG_PTR p_user_data = GetWindowLongPtr( hwnd, GWLP_USERDATA );
671         p_vout = (vout_thread_t *)p_user_data;
672         if( !p_vout )
673         {
674             /* Hmmm mozilla does manage somehow to save the pointer to our
675              * windowproc and still calls it after the vout has been closed. */
676             return DefWindowProc(hwnd, message, wParam, lParam);
677         }
678     }
679
680 #ifndef UNDER_CE
681     /* Catch the screensaver and the monitor turn-off */
682     if( message == WM_SYSCOMMAND &&
683         ( (wParam & 0xFFF0) == SC_SCREENSAVE || (wParam & 0xFFF0) == SC_MONITORPOWER ) )
684     {
685         //if( p_vout ) msg_Dbg( p_vout, "WinProc WM_SYSCOMMAND screensaver" );
686         return 0; /* this stops them from happening */
687     }
688 #endif
689
690     if( hwnd == p_vout->p_sys->hvideownd )
691     {
692         switch( message )
693         {
694 #ifdef MODULE_NAME_IS_directx
695         case WM_ERASEBKGND:
696         /* For overlay, we need to erase background */
697             return !p_vout->p_sys->b_using_overlay ?
698                 1 : DefWindowProc(hwnd, message, wParam, lParam);
699         case WM_PAINT:
700         /*
701         ** For overlay, DefWindowProc() will erase dirty regions
702         ** with colorkey.
703         ** For non-overlay, vout will paint the whole window at
704         ** regular interval, therefore dirty regions can be ignored
705         ** to minimize repaint.
706         */
707             if( !p_vout->p_sys->b_using_overlay )
708             {
709                 ValidateRect(hwnd, NULL);
710             }
711             // fall through to default
712 #else
713         /*
714         ** For OpenGL and Direct3D, vout will update the whole
715         ** window at regular interval, therefore dirty region
716         ** can be ignored to minimize repaint.
717         */
718         case WM_ERASEBKGND:
719             /* nothing to erase */
720             return 1;
721         case WM_PAINT:
722             /* nothing to repaint */
723             ValidateRect(hwnd, NULL);
724             // fall through
725 #endif
726         default:
727             return DefWindowProc(hwnd, message, wParam, lParam);
728         }
729     }
730
731     switch( message )
732     {
733
734     case WM_WINDOWPOSCHANGED:
735         UpdateRects( p_vout, true );
736         return 0;
737
738     /* the user wants to close the window */
739     case WM_CLOSE:
740     {
741         playlist_t * p_playlist = pl_Hold( p_vout );
742         if( p_playlist )
743         {
744             playlist_Stop( p_playlist );
745             pl_Release( p_vout );
746         }
747         return 0;
748     }
749
750     /* the window has been closed so shut down everything now */
751     case WM_DESTROY:
752         msg_Dbg( p_vout, "WinProc WM_DESTROY" );
753         /* just destroy the window */
754         PostQuitMessage( 0 );
755         return 0;
756
757     case WM_SYSCOMMAND:
758         switch (wParam)
759         {
760             case IDM_TOGGLE_ON_TOP:            /* toggle the "on top" status */
761             {
762                 vlc_value_t val;
763                 msg_Dbg( p_vout, "WinProc WM_SYSCOMMAND: IDM_TOGGLE_ON_TOP");
764
765                 /* Change the current value */
766                 var_Get( p_vout, "video-on-top", &val );
767                 val.b_bool = !val.b_bool;
768                 var_Set( p_vout, "video-on-top", val );
769                 return 0;
770             }
771         }
772         break;
773
774     case WM_PAINT:
775     case WM_NCPAINT:
776     case WM_ERASEBKGND:
777         return DefWindowProc(hwnd, message, wParam, lParam);
778
779     case WM_KILLFOCUS:
780 #ifdef MODULE_NAME_IS_wingapi
781         p_vout->p_sys->b_focus = false;
782         if( !p_vout->p_sys->b_parent_focus ) GXSuspend();
783 #endif
784 #ifdef UNDER_CE
785         if( hwnd == p_vout->p_sys->hfswnd )
786         {
787             HWND htbar = FindWindow( _T("HHTaskbar"), NULL );
788             ShowWindow( htbar, SW_SHOW );
789         }
790
791         if( !p_vout->p_sys->hparent ||
792             hwnd == p_vout->p_sys->hfswnd )
793         {
794             SHFullScreen( hwnd, SHFS_SHOWSIPBUTTON );
795         }
796 #endif
797         return 0;
798
799     case WM_SETFOCUS:
800 #ifdef MODULE_NAME_IS_wingapi
801         p_vout->p_sys->b_focus = true;
802         GXResume();
803 #endif
804 #ifdef UNDER_CE
805         if( p_vout->p_sys->hparent &&
806             hwnd != p_vout->p_sys->hfswnd && p_vout->b_fullscreen )
807             p_vout->p_sys->i_changes |= VOUT_FULLSCREEN_CHANGE;
808
809         if( hwnd == p_vout->p_sys->hfswnd )
810         {
811             HWND htbar = FindWindow( _T("HHTaskbar"), NULL );
812             ShowWindow( htbar, SW_HIDE );
813         }
814
815         if( !p_vout->p_sys->hparent ||
816             hwnd == p_vout->p_sys->hfswnd )
817         {
818             SHFullScreen( hwnd, SHFS_HIDESIPBUTTON );
819         }
820 #endif
821         return 0;
822
823     default:
824         //msg_Dbg( p_vout, "WinProc WM Default %i", message );
825         break;
826     }
827
828     /* Let windows handle the message */
829     return DefWindowProc(hwnd, message, wParam, lParam);
830 }
831
832 static struct
833 {
834     int i_dxkey;
835     int i_vlckey;
836
837 } dxkeys_to_vlckeys[] =
838 {
839     { VK_F1, KEY_F1 }, { VK_F2, KEY_F2 }, { VK_F3, KEY_F3 }, { VK_F4, KEY_F4 },
840     { VK_F5, KEY_F5 }, { VK_F6, KEY_F6 }, { VK_F7, KEY_F7 }, { VK_F8, KEY_F8 },
841     { VK_F9, KEY_F9 }, { VK_F10, KEY_F10 }, { VK_F11, KEY_F11 },
842     { VK_F12, KEY_F12 },
843
844     { VK_RETURN, KEY_ENTER },
845     { VK_SPACE, KEY_SPACE },
846     { VK_ESCAPE, KEY_ESC },
847
848     { VK_LEFT, KEY_LEFT },
849     { VK_RIGHT, KEY_RIGHT },
850     { VK_UP, KEY_UP },
851     { VK_DOWN, KEY_DOWN },
852
853     { VK_HOME, KEY_HOME },
854     { VK_END, KEY_END },
855     { VK_PRIOR, KEY_PAGEUP },
856     { VK_NEXT, KEY_PAGEDOWN },
857
858     { VK_INSERT, KEY_INSERT },
859     { VK_DELETE, KEY_DELETE },
860
861     { VK_CONTROL, 0 },
862     { VK_SHIFT, 0 },
863     { VK_MENU, 0 },
864
865     { VK_BROWSER_BACK, KEY_BROWSER_BACK },
866     { VK_BROWSER_FORWARD, KEY_BROWSER_FORWARD },
867     { VK_BROWSER_REFRESH, KEY_BROWSER_REFRESH },
868     { VK_BROWSER_STOP, KEY_BROWSER_STOP },
869     { VK_BROWSER_SEARCH, KEY_BROWSER_SEARCH },
870     { VK_BROWSER_FAVORITES, KEY_BROWSER_FAVORITES },
871     { VK_BROWSER_HOME, KEY_BROWSER_HOME },
872     { VK_VOLUME_MUTE, KEY_VOLUME_MUTE },
873     { VK_VOLUME_DOWN, KEY_VOLUME_DOWN },
874     { VK_VOLUME_UP, KEY_VOLUME_UP },
875     { VK_MEDIA_NEXT_TRACK, KEY_MEDIA_NEXT_TRACK },
876     { VK_MEDIA_PREV_TRACK, KEY_MEDIA_PREV_TRACK },
877     { VK_MEDIA_STOP, KEY_MEDIA_STOP },
878     { VK_MEDIA_PLAY_PAUSE, KEY_MEDIA_PLAY_PAUSE },
879
880     { 0, 0 }
881 };
882
883 static int DirectXConvertKey( int i_key )
884 {
885     int i;
886
887     for( i = 0; dxkeys_to_vlckeys[i].i_dxkey != 0; i++ )
888     {
889         if( dxkeys_to_vlckeys[i].i_dxkey == i_key )
890         {
891             return dxkeys_to_vlckeys[i].i_vlckey;
892         }
893     }
894
895     return 0;
896 }
897
898 void EventThreadMouseAutoHide( event_thread_t *p_event )
899 {
900     vout_thread_t *p_vout = p_event->p_vout;
901
902     if( p_vout->b_fullscreen &&
903         !p_event->b_cursor_hidden &&
904         (mdate() - p_event->i_lastmoved) > p_event->i_mouse_hide_timeout )
905     {
906         /* Hide the cursor only if it is inside our window */
907         POINT point;
908         GetCursorPos( &point );
909
910         HWND hwnd = WindowFromPoint(point);
911         if( hwnd == p_vout->p_sys->hwnd || hwnd == p_vout->p_sys->hvideownd )
912         {
913             PostMessage( p_vout->p_sys->hwnd, WM_VLC_HIDE_MOUSE, 0, 0 );
914         }
915         else
916         {
917             p_event->i_lastmoved = mdate();
918         }
919     }
920 }
921
922 event_thread_t *EventThreadCreate( vout_thread_t *p_vout )
923 {
924      /* Create the Vout EventThread, this thread is created by us to isolate
925      * the Win32 PeekMessage function calls. We want to do this because
926      * Windows can stay blocked inside this call for a long time, and when
927      * this happens it thus blocks vlc's video_output thread.
928      * Vout EventThread will take care of the creation of the video
929      * window (because PeekMessage has to be called from the same thread which
930      * created the window). */
931     msg_Dbg( p_vout, "creating Vout EventThread" );
932     event_thread_t *p_event = malloc( sizeof(*p_event) );
933     if( !p_event )
934         return NULL;
935
936     p_event->p_vout = p_vout;
937     vlc_mutex_init( &p_event->lock );
938     vlc_cond_init( &p_event->wait );
939
940     p_event->b_cursor_hidden      = false;
941     p_event->i_lastmoved          = mdate();
942     p_event->i_mouse_hide_timeout =
943         var_GetInteger(p_vout, "mouse-hide-timeout") * 1000;
944    
945     return p_event;
946 }
947
948 void EventThreadDestroy( event_thread_t *p_event )
949 {
950     vlc_cond_destroy( &p_event->wait );
951     vlc_mutex_destroy( &p_event->lock );
952     free( p_event );
953 }
954
955 int EventThreadStart( event_thread_t *p_event )
956 {
957     p_event->b_ready = false;
958     p_event->b_done  = false;
959     p_event->b_error = false;
960
961     if( vlc_clone( &p_event->thread, EventThread, p_event,
962                    VLC_THREAD_PRIORITY_LOW ) )
963     {
964         msg_Err( p_event->p_vout, "cannot create Vout EventThread" );
965         return VLC_EGENERIC;
966     }
967
968     vlc_mutex_lock( &p_event->lock );
969     while( !p_event->b_ready )
970         vlc_cond_wait( &p_event->wait, &p_event->lock );
971     const bool b_error = p_event->b_error;
972     vlc_mutex_unlock( &p_event->lock );
973
974     if( b_error )
975     {
976         vlc_join( p_event->thread, NULL );
977         p_event->b_ready = false;
978         return VLC_EGENERIC;
979     }
980     msg_Dbg( p_event->p_vout, "Vout EventThread running" );
981     return VLC_SUCCESS;
982 }
983
984 void EventThreadStop( event_thread_t *p_event )
985 {
986     if( !p_event->b_ready )
987         return;
988
989     vlc_mutex_lock( &p_event->lock );
990     p_event->b_done = true;
991     vlc_mutex_unlock( &p_event->lock );
992
993     /* we need to be sure Vout EventThread won't stay stuck in
994      * GetMessage, so we send a fake message */
995     if( p_event->p_vout->p_sys->hwnd )
996         PostMessage( p_event->p_vout->p_sys->hwnd, WM_NULL, 0, 0);
997
998     vlc_join( p_event->thread, NULL );
999     p_event->b_ready = false;
1000
1001     /* clear the changes formerly signaled */
1002     p_event->p_vout->p_sys->i_changes = 0;
1003 }
1004