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