equalizer.c: compile fix for old compilers (thx sam)
[vlc.git] / 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
459         if( !(i_style & WS_CLIPCHILDREN) )
460             /* Hmmm, apparently this is a blocking call... */
461             SetWindowLong( p_vout->p_sys->hparent, GWL_STYLE,
462                            i_style | WS_CLIPCHILDREN );
463
464     }
465
466     /* Now display the window */
467     ShowWindow( p_vout->p_sys->hwnd, SW_SHOW );
468
469     /* Create video sub-window. This sub window will always exactly match
470      * the size of the video, which allows us to use crazy overlay colorkeys
471      * without having them shown outside of the video area. */
472     SendMessage( p_vout->p_sys->hwnd, WM_VLC_CREATE_VIDEO_WIN, 0, 0 );
473
474     /* Append a "Always On Top" entry in the system menu */
475     hMenu = GetSystemMenu( p_vout->p_sys->hwnd, FALSE );
476     AppendMenu( hMenu, MF_SEPARATOR, 0, "" );
477     AppendMenu( hMenu, MF_STRING | MF_UNCHECKED,
478                        IDM_TOGGLE_ON_TOP, "Always on &Top" );
479
480     return VLC_SUCCESS;
481 }
482
483 /*****************************************************************************
484  * DirectXCloseWindow: close the window created by DirectXCreateWindow
485  *****************************************************************************
486  * This function returns all resources allocated by DirectXCreateWindow.
487  *****************************************************************************/
488 static void DirectXCloseWindow( vout_thread_t *p_vout )
489 {
490     msg_Dbg( p_vout, "DirectXCloseWindow" );
491
492     DestroyWindow( p_vout->p_sys->hwnd );
493
494     if( p_vout->p_sys->hparent )
495         vout_ReleaseWindow( p_vout, (void *)p_vout->p_sys->hparent );
496
497     p_vout->p_sys->hwnd = NULL;
498
499     /* We don't unregister the Window Class because it could lead to race
500      * conditions and it will be done anyway by the system when the app will
501      * exit */
502 }
503
504 /*****************************************************************************
505  * DirectXUpdateRects: update clipping rectangles
506  *****************************************************************************
507  * This function is called when the window position or size are changed, and
508  * its job is to update the source and destination RECTs used to display the
509  * picture.
510  *****************************************************************************/
511 void DirectXUpdateRects( vout_thread_t *p_vout, vlc_bool_t b_force )
512 {
513 #define rect_src p_vout->p_sys->rect_src
514 #define rect_src_clipped p_vout->p_sys->rect_src_clipped
515 #define rect_dest p_vout->p_sys->rect_dest
516 #define rect_dest_clipped p_vout->p_sys->rect_dest_clipped
517
518     int i_width, i_height, i_x, i_y;
519
520     RECT  rect;
521     POINT point;
522
523     /* Retrieve the window size */
524     GetClientRect( p_vout->p_sys->hwnd, &rect );
525
526     /* Retrieve the window position */
527     point.x = point.y = 0;
528     ClientToScreen( p_vout->p_sys->hwnd, &point );
529
530     /* If nothing changed, we can return */
531     if( !b_force
532          && p_vout->p_sys->i_window_width == rect.right
533          && p_vout->p_sys->i_window_height == rect.bottom
534          && p_vout->p_sys->i_window_x == point.x
535          && p_vout->p_sys->i_window_y == point.y )
536     {
537         return;
538     }
539
540     /* Update the window position and size */
541     p_vout->p_sys->i_window_x = point.x;
542     p_vout->p_sys->i_window_y = point.y;
543     p_vout->p_sys->i_window_width = rect.right;
544     p_vout->p_sys->i_window_height = rect.bottom;
545
546     vout_PlacePicture( p_vout, rect.right, rect.bottom,
547                        &i_x, &i_y, &i_width, &i_height );
548
549     if( p_vout->p_sys->hvideownd )
550         SetWindowPos( p_vout->p_sys->hvideownd, HWND_TOP,
551                       i_x, i_y, i_width, i_height, 0 );
552
553     /* Destination image position and dimensions */
554     rect_dest.left = point.x + i_x;
555     rect_dest.right = rect_dest.left + i_width;
556     rect_dest.top = point.y + i_y;
557     rect_dest.bottom = rect_dest.top + i_height;
558
559     /* Apply overlay hardware constraints */
560     if( p_vout->p_sys->b_using_overlay )
561     {
562         if( p_vout->p_sys->i_align_dest_boundary )
563             rect_dest.left = ( rect_dest.left +
564                 p_vout->p_sys->i_align_dest_boundary / 2 ) & 
565                 ~p_vout->p_sys->i_align_dest_boundary;
566
567         if( p_vout->p_sys->i_align_dest_size )
568             rect_dest.right = (( rect_dest.right - rect_dest.left +
569                 p_vout->p_sys->i_align_dest_size / 2 ) & 
570                 ~p_vout->p_sys->i_align_dest_size) + rect_dest.left;
571     }
572
573     /* UpdateOverlay directdraw function doesn't automatically clip to the
574      * display size so we need to do it otherwise it will fail */
575
576     /* Clip the destination window */
577     if( !IntersectRect( &rect_dest_clipped, &rect_dest,
578                         &p_vout->p_sys->rect_display ) )
579     {
580         SetRectEmpty( &rect_src_clipped );
581         return;
582     }
583
584 #if 0
585     msg_Dbg( p_vout, "DirectXUpdateRects image_dst_clipped coords:"
586                      " %i,%i,%i,%i",
587                      rect_dest_clipped.left, rect_dest_clipped.top,
588                      rect_dest_clipped.right, rect_dest_clipped.bottom );
589 #endif
590
591     /* the 2 following lines are to fix a bug when clicking on the desktop */
592     if( (rect_dest_clipped.right - rect_dest_clipped.left)==0 ||
593         (rect_dest_clipped.bottom - rect_dest_clipped.top)==0 )
594     {
595         SetRectEmpty( &rect_src_clipped );
596         return;
597     }
598
599     /* src image dimensions */
600     rect_src.left = 0;
601     rect_src.top = 0;
602     rect_src.right = p_vout->render.i_width;
603     rect_src.bottom = p_vout->render.i_height;
604
605     /* Clip the source image */
606     rect_src_clipped.left = (rect_dest_clipped.left - rect_dest.left) *
607       p_vout->render.i_width / (rect_dest.right - rect_dest.left);
608     rect_src_clipped.right = p_vout->render.i_width -
609       (rect_dest.right - rect_dest_clipped.right) * p_vout->render.i_width /
610       (rect_dest.right - rect_dest.left);
611     rect_src_clipped.top = (rect_dest_clipped.top - rect_dest.top) *
612       p_vout->render.i_height / (rect_dest.bottom - rect_dest.top);
613     rect_src_clipped.bottom = p_vout->render.i_height -
614       (rect_dest.bottom - rect_dest_clipped.bottom) * p_vout->render.i_height /
615       (rect_dest.bottom - rect_dest.top);
616
617     /* Apply overlay hardware constraints */
618     if( p_vout->p_sys->b_using_overlay )
619     {
620         if( p_vout->p_sys->i_align_src_boundary )
621             rect_src_clipped.left = ( rect_src_clipped.left +
622                 p_vout->p_sys->i_align_src_boundary / 2 ) & 
623                 ~p_vout->p_sys->i_align_src_boundary;
624
625         if( p_vout->p_sys->i_align_src_size )
626             rect_src_clipped.right = (( rect_src_clipped.right -
627                 rect_src_clipped.left +
628                 p_vout->p_sys->i_align_src_size / 2 ) & 
629                 ~p_vout->p_sys->i_align_src_size) + rect_src_clipped.left;
630     }
631
632 #if 0
633     msg_Dbg( p_vout, "DirectXUpdateRects image_src_clipped"
634                      " coords: %i,%i,%i,%i",
635                      rect_src_clipped.left, rect_src_clipped.top,
636                      rect_src_clipped.right, rect_src_clipped.bottom );
637 #endif
638
639     /* The destination coordinates need to be relative to the current
640      * directdraw primary surface (display) */
641     rect_dest_clipped.left -= p_vout->p_sys->rect_display.left;
642     rect_dest_clipped.right -= p_vout->p_sys->rect_display.left;
643     rect_dest_clipped.top -= p_vout->p_sys->rect_display.top;
644     rect_dest_clipped.bottom -= p_vout->p_sys->rect_display.top;
645
646     if( p_vout->p_sys->b_using_overlay )
647         DirectXUpdateOverlay( p_vout );
648
649     /* Signal the change in size/position */
650     p_vout->p_sys->i_changes |= DX_POSITION_CHANGE;
651
652 #undef rect_src
653 #undef rect_src_clipped
654 #undef rect_dest
655 #undef rect_dest_clipped
656 }
657
658 /*****************************************************************************
659  * DirectXEventProc: This is the window event processing function.
660  *****************************************************************************
661  * On Windows, when you create a window you have to attach an event processing
662  * function to it. The aim of this function is to manage "Queued Messages" and
663  * "Nonqueued Messages".
664  * Queued Messages are those picked up and retransmitted by vout_Manage
665  * (using the GetMessage and DispatchMessage functions).
666  * Nonqueued Messages are those that Windows will send directly to this
667  * procedure (like WM_DESTROY, WM_WINDOWPOSCHANGED...)
668  *****************************************************************************/
669 static long FAR PASCAL DirectXEventProc( HWND hwnd, UINT message,
670                                          WPARAM wParam, LPARAM lParam )
671 {
672     vout_thread_t *p_vout;
673
674     if( message == WM_CREATE )
675     {
676         /* Store p_vout for future use */
677         p_vout = (vout_thread_t *)((CREATESTRUCT *)lParam)->lpCreateParams;
678         SetWindowLongPtr( hwnd, GWLP_USERDATA, (LONG_PTR)p_vout );
679     }
680     else
681     {
682         p_vout = (vout_thread_t *)GetWindowLongPtr( hwnd, GWLP_USERDATA );
683     }
684
685     if( !p_vout )
686     {
687         /* Hmmm mozilla does manage somehow to save the pointer to our
688          * windowproc and still calls it after the vout has been closed. */
689         return DefWindowProc(hwnd, message, wParam, lParam);
690     }
691
692     switch( message )
693     {
694
695     case WM_WINDOWPOSCHANGED:
696         DirectXUpdateRects( p_vout, VLC_TRUE );
697         return 0;
698
699     /* the user wants to close the window */
700     case WM_CLOSE:
701     {
702         playlist_t * p_playlist =
703             (playlist_t *)vlc_object_find( p_vout, VLC_OBJECT_PLAYLIST,
704                                            FIND_ANYWHERE );
705         if( p_playlist == NULL )
706         {
707             return 0;
708         }
709
710         playlist_Stop( p_playlist );
711         vlc_object_release( p_playlist );
712         return 0;
713     }
714
715     /* the window has been closed so shut down everything now */
716     case WM_DESTROY:
717         msg_Dbg( p_vout, "WinProc WM_DESTROY" );
718         /* just destroy the window */
719         PostQuitMessage( 0 );
720         return 0;
721
722     case WM_SYSCOMMAND:
723         switch (wParam)
724         {
725             case SC_SCREENSAVE:                     /* catch the screensaver */
726             case SC_MONITORPOWER:              /* catch the monitor turn-off */
727                 msg_Dbg( p_vout, "WinProc WM_SYSCOMMAND" );
728                 return 0;                  /* this stops them from happening */
729             case IDM_TOGGLE_ON_TOP:            /* toggle the "on top" status */
730             {
731                 vlc_value_t val;
732                 msg_Dbg( p_vout, "WinProc WM_SYSCOMMAND: IDM_TOGGLE_ON_TOP");
733
734                 /* Get the current value... */
735                 if( var_Get( p_vout, "video-on-top", &val ) < 0 )
736                     return 0;
737                 /* ...and change it */
738                 val.b_bool = !val.b_bool;
739                 var_Set( p_vout, "video-on-top", val );
740                 return 0;
741                 break;
742             }
743         }
744         break;
745
746     case WM_VLC_CREATE_VIDEO_WIN:
747         /* Create video sub-window */
748         p_vout->p_sys->hvideownd =
749             CreateWindow( "STATIC", "",   /* window class and title bar text */
750                     WS_CHILD | WS_VISIBLE,                   /* window style */
751                     CW_USEDEFAULT, CW_USEDEFAULT,     /* default coordinates */
752                     CW_USEDEFAULT, CW_USEDEFAULT,
753                     hwnd,                                   /* parent window */
754                     NULL, GetModuleHandle(NULL), NULL );
755
756         if( !p_vout->p_sys->hvideownd )
757         {
758             msg_Warn( p_vout, "Can't create video sub-window" );
759         }
760         else
761         {
762             msg_Dbg( p_vout, "Created video sub-window" );
763             SetWindowLongPtr( p_vout->p_sys->hvideownd,
764                               GWLP_WNDPROC, (LONG_PTR)DirectXVideoEventProc );
765             /* Store the previous window proc of _this_ window with the video
766              * window so we can use it in DirectXVideoEventProc to pass
767              * messages to the creator of _this_ window */
768             SetWindowLongPtr( p_vout->p_sys->hvideownd, GWLP_USERDATA,
769                               (LONG_PTR)p_vout->p_sys->pf_wndproc );
770         }
771         break;
772
773     case WM_PAINT:
774     case WM_NCPAINT:
775     case WM_ERASEBKGND:
776         /* We do not want to relay these messages to the parent window
777          * because we rely on the background color for the overlay. */
778         return DefWindowProc(hwnd, message, wParam, lParam);
779         break;
780
781     default:
782         //msg_Dbg( p_vout, "WinProc WM Default %i", message );
783         break;
784     }
785
786     /* Let windows handle the message */
787     return DefWindowProc(hwnd, message, wParam, lParam);
788 }
789
790 static long FAR PASCAL DirectXVideoEventProc( HWND hwnd, UINT message,
791                                               WPARAM wParam, LPARAM lParam )
792 {
793     WNDPROC pf_parentwndproc;
794     POINT pt;
795
796     switch( message )
797     {
798     case WM_MOUSEMOVE:
799     case WM_LBUTTONDOWN:
800     case WM_LBUTTONUP:
801     case WM_LBUTTONDBLCLK:
802     case WM_MBUTTONDOWN:
803     case WM_MBUTTONUP:
804     case WM_RBUTTONDOWN:
805     case WM_RBUTTONUP:
806         /* Translate mouse cursor position to parent window coordinates. */
807         pt.x = LOWORD( lParam );
808         pt.y = HIWORD( lParam );
809         MapWindowPoints( hwnd, GetParent( hwnd ), &pt, 1 );
810         lParam = MAKELPARAM( pt.x, pt.y );
811         /* Fall through. */
812     case WM_KEYDOWN:
813     case WM_KEYUP:
814     case WM_SYSKEYDOWN:
815     case WM_SYSKEYUP:
816         /* Foward these to the original window proc of the parent so the
817          * creator of the window gets a chance to process them. If we created
818          * the parent window ourself DirectXEventThread will process these
819          * and they will never make it here.
820          * Note that we fake the hwnd to be our parent in order to prevent
821          * confusion in the creator's window proc. */
822         pf_parentwndproc = (WNDPROC)GetWindowLongPtr( hwnd, GWLP_USERDATA );
823
824         if( pf_parentwndproc )
825         {
826             LRESULT i_ret;
827             LONG_PTR p_backup;
828
829             pf_parentwndproc = (WNDPROC)GetWindowLongPtr( hwnd, GWLP_USERDATA);
830
831             p_backup = SetWindowLongPtr( GetParent( hwnd ), GWLP_USERDATA, 0 );
832             i_ret = CallWindowProc( pf_parentwndproc, GetParent( hwnd ),
833                                     message, wParam, lParam );
834             SetWindowLongPtr( GetParent( hwnd ), GWLP_USERDATA, p_backup );
835
836             return i_ret;
837         }
838         break;
839     }
840
841     return DefWindowProc(hwnd, message, wParam, lParam);
842 }
843
844 static struct
845 {
846     int i_dxkey;
847     int i_vlckey;
848
849 } dxkeys_to_vlckeys[] =
850 {
851     { VK_F1, KEY_F1 }, { VK_F2, KEY_F2 }, { VK_F3, KEY_F3 }, { VK_F4, KEY_F4 },
852     { VK_F5, KEY_F5 }, { VK_F6, KEY_F6 }, { VK_F7, KEY_F7 }, { VK_F8, KEY_F8 },
853     { VK_F9, KEY_F9 }, { VK_F10, KEY_F10 }, { VK_F11, KEY_F11 },
854     { VK_F12, KEY_F12 },
855
856     { VK_RETURN, KEY_ENTER },
857     { VK_SPACE, KEY_SPACE },
858     { VK_ESCAPE, KEY_ESC },
859
860     { VK_LEFT, KEY_LEFT },
861     { VK_RIGHT, KEY_RIGHT },
862     { VK_UP, KEY_UP },
863     { VK_DOWN, KEY_DOWN },
864
865     { VK_HOME, KEY_HOME },
866     { VK_END, KEY_END },
867     { VK_PRIOR, KEY_PAGEUP },
868     { VK_NEXT, KEY_PAGEDOWN },
869
870     { VK_CONTROL, 0 },
871     { VK_SHIFT, 0 },
872     { VK_MENU, 0 },
873
874     { 0, 0 }
875 };
876
877 static int DirectXConvertKey( int i_key )
878 {
879     int i;
880
881     for( i = 0; dxkeys_to_vlckeys[i].i_dxkey != 0; i++ )
882     {
883         if( dxkeys_to_vlckeys[i].i_dxkey == i_key )
884         {
885             return dxkeys_to_vlckeys[i].i_vlckey;
886         }
887     }
888
889     return 0;
890 }
891
892 /*****************************************************************************
893  * Control: control facility for the vout
894  *****************************************************************************/
895 static int Control( vout_thread_t *p_vout, int i_query, va_list args )
896 {
897     double f_arg;
898     RECT rect_window;
899     POINT point;
900
901     switch( i_query )
902     {
903     case VOUT_SET_ZOOM:
904         if( p_vout->p_sys->hparent )
905             return vout_ControlWindow( p_vout,
906                     (void *)p_vout->p_sys->hparent, i_query, args );
907
908         f_arg = va_arg( args, double );
909
910         /* Update dimensions */
911         rect_window.top = rect_window.left = 0;
912         rect_window.right  = p_vout->i_window_width * f_arg;
913         rect_window.bottom = p_vout->i_window_height * f_arg;
914         AdjustWindowRect( &rect_window, WS_OVERLAPPEDWINDOW|WS_SIZEBOX, 0 );
915
916         SetWindowPos( p_vout->p_sys->hwnd, 0, 0, 0,
917                       rect_window.right - rect_window.left,
918                       rect_window.bottom - rect_window.top, SWP_NOMOVE );
919
920         return VLC_SUCCESS;
921
922     case VOUT_CLOSE:
923         ShowWindow( p_vout->p_sys->hwnd, SW_HIDE );
924     case VOUT_REPARENT:
925         /* Change window style, borders and title bar */
926         vlc_mutex_lock( &p_vout->p_sys->lock );
927         p_vout->p_sys->hparent = 0;
928         vlc_mutex_unlock( &p_vout->p_sys->lock );
929
930         /* Retrieve the window position */
931         point.x = point.y = 0;
932         ClientToScreen( p_vout->p_sys->hwnd, &point );
933
934         SetParent( p_vout->p_sys->hwnd, GetDesktopWindow() );
935         SetWindowLong( p_vout->p_sys->hwnd, GWL_STYLE,
936                        WS_CLIPCHILDREN | WS_OVERLAPPEDWINDOW |
937                        WS_SIZEBOX | (i_query == VOUT_CLOSE ? 0 : WS_VISIBLE) );
938         SetWindowPos( p_vout->p_sys->hwnd, 0, point.x, point.y, 0, 0,
939                       SWP_NOSIZE|SWP_NOZORDER|SWP_FRAMECHANGED );
940
941         return vout_vaControlDefault( p_vout, i_query, args );
942
943     case VOUT_SET_STAY_ON_TOP:
944         if( p_vout->p_sys->hparent )
945             return vout_ControlWindow( p_vout,
946                     (void *)p_vout->p_sys->hparent, i_query, args );
947
948         p_vout->p_sys->b_on_top_change = VLC_TRUE;
949         return VLC_SUCCESS;
950
951     default:
952         return vout_vaControlDefault( p_vout, i_query, args );
953     }
954 }