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