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