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