Modified the way title is updated (msw).
[vlc.git] / 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         {
334             vlc_mutex_lock( &p_event->lock );
335             wchar_t *pwz_title = NULL;
336             if( p_event->psz_title )
337             {
338                 const size_t i_length = strlen(p_event->psz_title);
339                 pwz_title = malloc( 2 * (i_length + 1) );
340                 if( pwz_title )
341                 {
342                     mbstowcs( pwz_title, p_event->psz_title, 2 * i_length );
343                     pwz_title[i_length] = 0;
344                 }
345             }
346             vlc_mutex_unlock( &p_event->lock );
347
348             if( pwz_title )
349             {
350                 SetWindowText( p_vout->p_sys->hwnd, (LPCTSTR)pwz_title );
351                 if( p_vout->p_sys->hfswnd )
352                     SetWindowText( p_vout->p_sys->hfswnd, (LPCTSTR)pwz_title );
353                 free( pwz_title );
354             }
355             break;
356         }
357
358         default:
359             /* Messages we don't handle directly are dispatched to the
360              * window procedure */
361             TranslateMessage(&msg);
362             DispatchMessage(&msg);
363             break;
364
365         } /* End Switch */
366
367     } /* End Main loop */
368
369     /* Check for WM_QUIT if we created the window */
370     if( !p_event->p_vout->p_sys->hparent && msg.message == WM_QUIT )
371     {
372         msg_Warn( p_vout, "WM_QUIT... should not happen!!" );
373         p_event->p_vout->p_sys->hwnd = NULL; /* Window already destroyed */
374     }
375
376     msg_Dbg( p_vout, "DirectXEventThread terminating" );
377
378     DirectXCloseWindow( p_event->p_vout );
379     vlc_restorecancel(canc);
380     return NULL;
381 }
382
383
384 /* following functions are local */
385
386 /*****************************************************************************
387  * DirectXCreateWindow: create a window for the video.
388  *****************************************************************************
389  * Before creating a direct draw surface, we need to create a window in which
390  * the video will be displayed. This window will also allow us to capture the
391  * events.
392  *****************************************************************************/
393 static int DirectXCreateWindow( vout_thread_t *p_vout )
394 {
395     HINSTANCE  hInstance;
396     HMENU      hMenu;
397     RECT       rect_window;
398     WNDCLASS   wc;                            /* window class components */
399     HICON      vlc_icon = NULL;
400     char       vlc_path[MAX_PATH+1];
401     int        i_style, i_stylex;
402
403     msg_Dbg( p_vout, "DirectXCreateWindow" );
404
405     /* Get this module's instance */
406     hInstance = GetModuleHandle(NULL);
407
408     #ifdef MODULE_NAME_IS_direct3d
409     if( !p_vout->p_sys->b_desktop )
410     {
411     #endif
412         vout_window_cfg_t wnd_cfg;
413         memset( &wnd_cfg, 0, sizeof(wnd_cfg) );
414         wnd_cfg.type   = VOUT_WINDOW_TYPE_HWND;
415         wnd_cfg.x      = p_vout->p_sys->i_window_x;
416         wnd_cfg.y      = p_vout->p_sys->i_window_y;
417         wnd_cfg.width  = p_vout->p_sys->i_window_width;
418         wnd_cfg.height = p_vout->p_sys->i_window_height;
419
420         /* If an external window was specified, we'll draw in it. */
421         p_vout->p_sys->parent_window = vout_window_New( VLC_OBJECT(p_vout), NULL, &wnd_cfg );
422         if( p_vout->p_sys->parent_window )
423             p_vout->p_sys->hparent = p_vout->p_sys->parent_window->handle.hwnd;
424     #ifdef MODULE_NAME_IS_direct3d
425     }
426     else
427     {
428         /* Find Program Manager */
429         HWND hwnd = FindWindow( _T("Progman"), NULL );
430         if( hwnd ) hwnd = FindWindowEx( hwnd, NULL, _T("SHELLDLL_DefView"), NULL );
431         if( hwnd ) hwnd = FindWindowEx( hwnd, NULL, _T("SysListView32"), NULL );
432         if( !hwnd )
433             msg_Err( p_vout, "Couldn't find desktop icon window. Desktop mode can't be established." );
434         p_vout->p_sys->hparent = hwnd;
435     }
436     #endif
437
438     /* Get the Icon from the main app */
439     vlc_icon = NULL;
440 #ifndef UNDER_CE
441     if( GetModuleFileName( NULL, vlc_path, MAX_PATH ) )
442     {
443         vlc_icon = ExtractIcon( hInstance, vlc_path, 0 );
444     }
445 #endif
446
447     /* Fill in the window class structure */
448     wc.style         = CS_OWNDC|CS_DBLCLKS;          /* style: dbl click */
449     wc.lpfnWndProc   = (WNDPROC)DirectXEventProc;       /* event handler */
450     wc.cbClsExtra    = 0;                         /* no extra class data */
451     wc.cbWndExtra    = 0;                        /* no extra window data */
452     wc.hInstance     = hInstance;                            /* instance */
453     wc.hIcon         = vlc_icon;                /* load the vlc big icon */
454     wc.hCursor       = LoadCursor(NULL, IDC_ARROW);    /* default cursor */
455     wc.hbrBackground = GetStockObject(BLACK_BRUSH);  /* background color */
456     wc.lpszMenuName  = NULL;                                  /* no menu */
457     wc.lpszClassName = _T("VLC DirectX");         /* use a special class */
458
459     /* Register the window class */
460     if( !RegisterClass(&wc) )
461     {
462         WNDCLASS wndclass;
463
464         if( vlc_icon ) DestroyIcon( vlc_icon );
465
466         /* Check why it failed. If it's because one already exists
467          * then fine, otherwise return with an error. */
468         if( !GetClassInfo( hInstance, _T("VLC DirectX"), &wndclass ) )
469         {
470             msg_Err( p_vout, "DirectXCreateWindow RegisterClass FAILED (err=%lu)", GetLastError() );
471             return VLC_EGENERIC;
472         }
473     }
474
475     /* Register the video sub-window class */
476     wc.lpszClassName = _T("VLC DirectX video"); wc.hIcon = 0;
477     wc.hbrBackground = NULL; /* no background color */
478     if( !RegisterClass(&wc) )
479     {
480         WNDCLASS wndclass;
481
482         /* Check why it failed. If it's because one already exists
483          * then fine, otherwise return with an error. */
484         if( !GetClassInfo( hInstance, _T("VLC DirectX video"), &wndclass ) )
485         {
486             msg_Err( p_vout, "DirectXCreateWindow RegisterClass FAILED (err=%lu)", GetLastError() );
487             return VLC_EGENERIC;
488         }
489     }
490
491     /* When you create a window you give the dimensions you wish it to
492      * have. Unfortunatly these dimensions will include the borders and
493      * titlebar. We use the following function to find out the size of
494      * the window corresponding to the useable surface we want */
495     rect_window.top    = 10;
496     rect_window.left   = 10;
497     rect_window.right  = rect_window.left + p_vout->p_sys->i_window_width;
498     rect_window.bottom = rect_window.top + p_vout->p_sys->i_window_height;
499
500     if( var_GetBool( p_vout, "video-deco" ) )
501     {
502         /* Open with window decoration */
503         AdjustWindowRect( &rect_window, WS_OVERLAPPEDWINDOW|WS_SIZEBOX, 0 );
504         i_style = WS_OVERLAPPEDWINDOW|WS_SIZEBOX|WS_VISIBLE|WS_CLIPCHILDREN;
505         i_stylex = 0;
506     }
507     else
508     {
509         /* No window decoration */
510         AdjustWindowRect( &rect_window, WS_POPUP, 0 );
511         i_style = WS_POPUP|WS_VISIBLE|WS_CLIPCHILDREN;
512         i_stylex = 0; // WS_EX_TOOLWINDOW; Is TOOLWINDOW really needed ?
513                       // It messes up the fullscreen window.
514     }
515
516     if( p_vout->p_sys->hparent )
517     {
518         i_style = WS_VISIBLE|WS_CLIPCHILDREN|WS_CHILD;
519         i_stylex = 0;
520     }
521
522     p_vout->p_sys->i_window_style = i_style;
523
524     /* Create the window */
525     p_vout->p_sys->hwnd =
526         CreateWindowEx( WS_EX_NOPARENTNOTIFY | i_stylex,
527                     _T("VLC DirectX"),               /* name of window class */
528                     _T(VOUT_TITLE) _T(" (DirectX Output)"),  /* window title */
529                     i_style,                                 /* window style */
530                     (p_vout->p_sys->i_window_x < 0) ? CW_USEDEFAULT :
531                         (UINT)p_vout->p_sys->i_window_x,   /* default X coordinate */
532                     (p_vout->p_sys->i_window_y < 0) ? CW_USEDEFAULT :
533                         (UINT)p_vout->p_sys->i_window_y,   /* default Y coordinate */
534                     rect_window.right - rect_window.left,    /* window width */
535                     rect_window.bottom - rect_window.top,   /* window height */
536                     p_vout->p_sys->hparent,                 /* parent window */
537                     NULL,                          /* no menu in this window */
538                     hInstance,            /* handle of this program instance */
539                     (LPVOID)p_vout );            /* send p_vout to WM_CREATE */
540
541     if( !p_vout->p_sys->hwnd )
542     {
543         msg_Warn( p_vout, "DirectXCreateWindow create window FAILED (err=%lu)", GetLastError() );
544         return VLC_EGENERIC;
545     }
546
547     if( p_vout->p_sys->hparent )
548     {
549         LONG i_style;
550
551         /* We don't want the window owner to overwrite our client area */
552         i_style = GetWindowLong( p_vout->p_sys->hparent, GWL_STYLE );
553
554         if( !(i_style & WS_CLIPCHILDREN) )
555             /* Hmmm, apparently this is a blocking call... */
556             SetWindowLong( p_vout->p_sys->hparent, GWL_STYLE,
557                            i_style | WS_CLIPCHILDREN );
558
559         /* Create our fullscreen window */
560         p_vout->p_sys->hfswnd =
561             CreateWindowEx( WS_EX_APPWINDOW, _T("VLC DirectX"),
562                             _T(VOUT_TITLE) _T(" (DirectX Output)"),
563                             WS_OVERLAPPEDWINDOW|WS_CLIPCHILDREN|WS_SIZEBOX,
564                             CW_USEDEFAULT, CW_USEDEFAULT,
565                             CW_USEDEFAULT, CW_USEDEFAULT,
566                             NULL, NULL, hInstance, NULL );
567     }
568
569     /* Append a "Always On Top" entry in the system menu */
570     hMenu = GetSystemMenu( p_vout->p_sys->hwnd, FALSE );
571     AppendMenu( hMenu, MF_SEPARATOR, 0, _T("") );
572     AppendMenu( hMenu, MF_STRING | MF_UNCHECKED,
573                        IDM_TOGGLE_ON_TOP, _T("Always on &Top") );
574
575     /* Create video sub-window. This sub window will always exactly match
576      * the size of the video, which allows us to use crazy overlay colorkeys
577      * without having them shown outside of the video area. */
578     p_vout->p_sys->hvideownd =
579     CreateWindow( _T("VLC DirectX video"), _T(""),   /* window class */
580         WS_CHILD,                   /* window style, not visible initially */
581         0, 0,
582         p_vout->render.i_width,         /* default width */
583         p_vout->render.i_height,        /* default height */
584         p_vout->p_sys->hwnd,                    /* parent window */
585         NULL, hInstance,
586         (LPVOID)p_vout );            /* send p_vout to WM_CREATE */
587
588     if( !p_vout->p_sys->hvideownd )
589         msg_Warn( p_vout, "can't create video sub-window" );
590     else
591         msg_Dbg( p_vout, "created video sub-window" );
592
593     /* Now display the window */
594     ShowWindow( p_vout->p_sys->hwnd, SW_SHOW );
595
596     return VLC_SUCCESS;
597 }
598
599 /*****************************************************************************
600  * DirectXCloseWindow: close the window created by DirectXCreateWindow
601  *****************************************************************************
602  * This function returns all resources allocated by DirectXCreateWindow.
603  *****************************************************************************/
604 static void DirectXCloseWindow( vout_thread_t *p_vout )
605 {
606     msg_Dbg( p_vout, "DirectXCloseWindow" );
607
608     DestroyWindow( p_vout->p_sys->hwnd );
609     if( p_vout->p_sys->hfswnd ) DestroyWindow( p_vout->p_sys->hfswnd );
610
611     #ifdef MODULE_NAME_IS_direct3d
612     if( !p_vout->p_sys->b_desktop )
613     #endif
614         vout_window_Delete( p_vout->p_sys->parent_window );
615     p_vout->p_sys->hwnd = NULL;
616
617     /* We don't unregister the Window Class because it could lead to race
618      * conditions and it will be done anyway by the system when the app will
619      * exit */
620 }
621
622 /*****************************************************************************
623  * DirectXEventProc: This is the window event processing function.
624  *****************************************************************************
625  * On Windows, when you create a window you have to attach an event processing
626  * function to it. The aim of this function is to manage "Queued Messages" and
627  * "Nonqueued Messages".
628  * Queued Messages are those picked up and retransmitted by vout_Manage
629  * (using the GetMessage and DispatchMessage functions).
630  * Nonqueued Messages are those that Windows will send directly to this
631  * procedure (like WM_DESTROY, WM_WINDOWPOSCHANGED...)
632  *****************************************************************************/
633 static long FAR PASCAL DirectXEventProc( HWND hwnd, UINT message,
634                                          WPARAM wParam, LPARAM lParam )
635 {
636     vout_thread_t *p_vout;
637
638     if( message == WM_CREATE )
639     {
640         /* Store p_vout for future use */
641         p_vout = (vout_thread_t *)((CREATESTRUCT *)lParam)->lpCreateParams;
642         SetWindowLongPtr( hwnd, GWLP_USERDATA, (LONG_PTR)p_vout );
643         return TRUE;
644     }
645     else
646     {
647         LONG_PTR p_user_data = GetWindowLongPtr( hwnd, GWLP_USERDATA );
648         p_vout = (vout_thread_t *)p_user_data;
649         if( !p_vout )
650         {
651             /* Hmmm mozilla does manage somehow to save the pointer to our
652              * windowproc and still calls it after the vout has been closed. */
653             return DefWindowProc(hwnd, message, wParam, lParam);
654         }
655     }
656
657 #ifndef UNDER_CE
658     /* Catch the screensaver and the monitor turn-off */
659     if( message == WM_SYSCOMMAND &&
660         ( (wParam & 0xFFF0) == SC_SCREENSAVE || (wParam & 0xFFF0) == SC_MONITORPOWER ) )
661     {
662         //if( p_vout ) msg_Dbg( p_vout, "WinProc WM_SYSCOMMAND screensaver" );
663         return 0; /* this stops them from happening */
664     }
665 #endif
666
667     if( hwnd == p_vout->p_sys->hvideownd )
668     {
669         switch( message )
670         {
671 #ifdef MODULE_NAME_IS_directx
672         case WM_ERASEBKGND:
673         /* For overlay, we need to erase background */
674             return !p_vout->p_sys->b_using_overlay ?
675                 1 : DefWindowProc(hwnd, message, wParam, lParam);
676         case WM_PAINT:
677         /*
678         ** For overlay, DefWindowProc() will erase dirty regions
679         ** with colorkey.
680         ** For non-overlay, vout will paint the whole window at
681         ** regular interval, therefore dirty regions can be ignored
682         ** to minimize repaint.
683         */
684             if( !p_vout->p_sys->b_using_overlay )
685             {
686                 ValidateRect(hwnd, NULL);
687             }
688             // fall through to default
689 #else
690         /*
691         ** For OpenGL and Direct3D, vout will update the whole
692         ** window at regular interval, therefore dirty region
693         ** can be ignored to minimize repaint.
694         */
695         case WM_ERASEBKGND:
696             /* nothing to erase */
697             return 1;
698         case WM_PAINT:
699             /* nothing to repaint */
700             ValidateRect(hwnd, NULL);
701             // fall through
702 #endif
703         default:
704             return DefWindowProc(hwnd, message, wParam, lParam);
705         }
706     }
707
708     switch( message )
709     {
710
711     case WM_WINDOWPOSCHANGED:
712         UpdateRects( p_vout, true );
713         return 0;
714
715     /* the user wants to close the window */
716     case WM_CLOSE:
717     {
718         playlist_t * p_playlist = pl_Hold( p_vout );
719         if( p_playlist )
720         {
721             playlist_Stop( p_playlist );
722             pl_Release( p_vout );
723         }
724         return 0;
725     }
726
727     /* the window has been closed so shut down everything now */
728     case WM_DESTROY:
729         msg_Dbg( p_vout, "WinProc WM_DESTROY" );
730         /* just destroy the window */
731         PostQuitMessage( 0 );
732         return 0;
733
734     case WM_SYSCOMMAND:
735         switch (wParam)
736         {
737             case IDM_TOGGLE_ON_TOP:            /* toggle the "on top" status */
738             {
739                 vlc_value_t val;
740                 msg_Dbg( p_vout, "WinProc WM_SYSCOMMAND: IDM_TOGGLE_ON_TOP");
741
742                 /* Change the current value */
743                 var_Get( p_vout, "video-on-top", &val );
744                 val.b_bool = !val.b_bool;
745                 var_Set( p_vout, "video-on-top", val );
746                 return 0;
747             }
748         }
749         break;
750
751     case WM_PAINT:
752     case WM_NCPAINT:
753     case WM_ERASEBKGND:
754         return DefWindowProc(hwnd, message, wParam, lParam);
755
756     case WM_KILLFOCUS:
757 #ifdef MODULE_NAME_IS_wingapi
758         GXSuspend();
759 #endif
760 #ifdef UNDER_CE
761         if( hwnd == p_vout->p_sys->hfswnd )
762         {
763             HWND htbar = FindWindow( _T("HHTaskbar"), NULL );
764             ShowWindow( htbar, SW_SHOW );
765         }
766
767         if( !p_vout->p_sys->hparent ||
768             hwnd == p_vout->p_sys->hfswnd )
769         {
770             SHFullScreen( hwnd, SHFS_SHOWSIPBUTTON );
771         }
772 #endif
773         return 0;
774
775     case WM_SETFOCUS:
776 #ifdef MODULE_NAME_IS_wingapi
777         GXResume();
778 #endif
779 #ifdef UNDER_CE
780         if( p_vout->p_sys->hparent &&
781             hwnd != p_vout->p_sys->hfswnd && p_vout->b_fullscreen )
782             p_vout->p_sys->i_changes |= VOUT_FULLSCREEN_CHANGE;
783
784         if( hwnd == p_vout->p_sys->hfswnd )
785         {
786             HWND htbar = FindWindow( _T("HHTaskbar"), NULL );
787             ShowWindow( htbar, SW_HIDE );
788         }
789
790         if( !p_vout->p_sys->hparent ||
791             hwnd == p_vout->p_sys->hfswnd )
792         {
793             SHFullScreen( hwnd, SHFS_HIDESIPBUTTON );
794         }
795 #endif
796         return 0;
797
798     default:
799         //msg_Dbg( p_vout, "WinProc WM Default %i", message );
800         break;
801     }
802
803     /* Let windows handle the message */
804     return DefWindowProc(hwnd, message, wParam, lParam);
805 }
806
807 static struct
808 {
809     int i_dxkey;
810     int i_vlckey;
811
812 } dxkeys_to_vlckeys[] =
813 {
814     { VK_F1, KEY_F1 }, { VK_F2, KEY_F2 }, { VK_F3, KEY_F3 }, { VK_F4, KEY_F4 },
815     { VK_F5, KEY_F5 }, { VK_F6, KEY_F6 }, { VK_F7, KEY_F7 }, { VK_F8, KEY_F8 },
816     { VK_F9, KEY_F9 }, { VK_F10, KEY_F10 }, { VK_F11, KEY_F11 },
817     { VK_F12, KEY_F12 },
818
819     { VK_RETURN, KEY_ENTER },
820     { VK_SPACE, KEY_SPACE },
821     { VK_ESCAPE, KEY_ESC },
822
823     { VK_LEFT, KEY_LEFT },
824     { VK_RIGHT, KEY_RIGHT },
825     { VK_UP, KEY_UP },
826     { VK_DOWN, KEY_DOWN },
827
828     { VK_HOME, KEY_HOME },
829     { VK_END, KEY_END },
830     { VK_PRIOR, KEY_PAGEUP },
831     { VK_NEXT, KEY_PAGEDOWN },
832
833     { VK_INSERT, KEY_INSERT },
834     { VK_DELETE, KEY_DELETE },
835
836     { VK_CONTROL, 0 },
837     { VK_SHIFT, 0 },
838     { VK_MENU, 0 },
839
840     { VK_BROWSER_BACK, KEY_BROWSER_BACK },
841     { VK_BROWSER_FORWARD, KEY_BROWSER_FORWARD },
842     { VK_BROWSER_REFRESH, KEY_BROWSER_REFRESH },
843     { VK_BROWSER_STOP, KEY_BROWSER_STOP },
844     { VK_BROWSER_SEARCH, KEY_BROWSER_SEARCH },
845     { VK_BROWSER_FAVORITES, KEY_BROWSER_FAVORITES },
846     { VK_BROWSER_HOME, KEY_BROWSER_HOME },
847     { VK_VOLUME_MUTE, KEY_VOLUME_MUTE },
848     { VK_VOLUME_DOWN, KEY_VOLUME_DOWN },
849     { VK_VOLUME_UP, KEY_VOLUME_UP },
850     { VK_MEDIA_NEXT_TRACK, KEY_MEDIA_NEXT_TRACK },
851     { VK_MEDIA_PREV_TRACK, KEY_MEDIA_PREV_TRACK },
852     { VK_MEDIA_STOP, KEY_MEDIA_STOP },
853     { VK_MEDIA_PLAY_PAUSE, KEY_MEDIA_PLAY_PAUSE },
854
855     { 0, 0 }
856 };
857
858 static int DirectXConvertKey( int i_key )
859 {
860     int i;
861
862     for( i = 0; dxkeys_to_vlckeys[i].i_dxkey != 0; i++ )
863     {
864         if( dxkeys_to_vlckeys[i].i_dxkey == i_key )
865         {
866             return dxkeys_to_vlckeys[i].i_vlckey;
867         }
868     }
869
870     return 0;
871 }
872
873 void EventThreadMouseAutoHide( event_thread_t *p_event )
874 {
875     vout_thread_t *p_vout = p_event->p_vout;
876
877     if( p_vout->b_fullscreen &&
878         !p_event->b_cursor_hidden &&
879         (mdate() - p_event->i_lastmoved) > p_event->i_mouse_hide_timeout )
880     {
881         /* Hide the cursor only if it is inside our window */
882         POINT point;
883         GetCursorPos( &point );
884
885         HWND hwnd = WindowFromPoint(point);
886         if( hwnd == p_vout->p_sys->hwnd || hwnd == p_vout->p_sys->hvideownd )
887         {
888             PostMessage( p_vout->p_sys->hwnd, WM_VLC_HIDE_MOUSE, 0, 0 );
889         }
890         else
891         {
892             p_event->i_lastmoved = mdate();
893         }
894     }
895 }
896 void EventThreadUpdateTitle( event_thread_t *p_event, const char *psz_fallback )
897 {
898     char *psz_title = var_GetNonEmptyString( p_event->p_vout, "video-title" );
899     if( !psz_title )
900         psz_title = strdup( psz_fallback );
901     if( !psz_title )
902         return;
903
904     vlc_mutex_lock( &p_event->lock );
905     free( p_event->psz_title );
906     p_event->psz_title = psz_title;
907     vlc_mutex_unlock( &p_event->lock );
908
909     PostMessage( p_event->p_vout->p_sys->hwnd, WM_VLC_CHANGE_TEXT, 0, 0 );
910 }
911
912 event_thread_t *EventThreadCreate( vout_thread_t *p_vout )
913 {
914      /* Create the Vout EventThread, this thread is created by us to isolate
915      * the Win32 PeekMessage function calls. We want to do this because
916      * Windows can stay blocked inside this call for a long time, and when
917      * this happens it thus blocks vlc's video_output thread.
918      * Vout EventThread will take care of the creation of the video
919      * window (because PeekMessage has to be called from the same thread which
920      * created the window). */
921     msg_Dbg( p_vout, "creating Vout EventThread" );
922     event_thread_t *p_event = malloc( sizeof(*p_event) );
923     if( !p_event )
924         return NULL;
925
926     p_event->p_vout = p_vout;
927     vlc_mutex_init( &p_event->lock );
928     vlc_cond_init( &p_event->wait );
929
930     p_event->b_cursor_hidden      = false;
931     p_event->i_lastmoved          = mdate();
932     p_event->i_mouse_hide_timeout =
933         var_GetInteger(p_vout, "mouse-hide-timeout") * 1000;
934     p_event->psz_title = NULL;
935    
936     return p_event;
937 }
938
939 void EventThreadDestroy( event_thread_t *p_event )
940 {
941     free( p_event->psz_title );
942     vlc_cond_destroy( &p_event->wait );
943     vlc_mutex_destroy( &p_event->lock );
944     free( p_event );
945 }
946
947 int EventThreadStart( event_thread_t *p_event )
948 {
949     p_event->b_ready = false;
950     p_event->b_done  = false;
951     p_event->b_error = false;
952
953     if( vlc_clone( &p_event->thread, EventThread, p_event,
954                    VLC_THREAD_PRIORITY_LOW ) )
955     {
956         msg_Err( p_event->p_vout, "cannot create Vout EventThread" );
957         return VLC_EGENERIC;
958     }
959
960     vlc_mutex_lock( &p_event->lock );
961     while( !p_event->b_ready )
962         vlc_cond_wait( &p_event->wait, &p_event->lock );
963     const bool b_error = p_event->b_error;
964     vlc_mutex_unlock( &p_event->lock );
965
966     if( b_error )
967     {
968         vlc_join( p_event->thread, NULL );
969         p_event->b_ready = false;
970         return VLC_EGENERIC;
971     }
972     msg_Dbg( p_event->p_vout, "Vout EventThread running" );
973     return VLC_SUCCESS;
974 }
975
976 void EventThreadStop( event_thread_t *p_event )
977 {
978     if( !p_event->b_ready )
979         return;
980
981     vlc_mutex_lock( &p_event->lock );
982     p_event->b_done = true;
983     vlc_mutex_unlock( &p_event->lock );
984
985     /* we need to be sure Vout EventThread won't stay stuck in
986      * GetMessage, so we send a fake message */
987     if( p_event->p_vout->p_sys->hwnd )
988         PostMessage( p_event->p_vout->p_sys->hwnd, WM_NULL, 0, 0);
989
990     vlc_join( p_event->thread, NULL );
991     p_event->b_ready = false;
992
993     /* clear the changes formerly signaled */
994     p_event->p_vout->p_sys->i_changes = 0;
995 }
996