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