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