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