]> git.sesse.net Git - vlc/blob - modules/video_output/directx/events.c
* modules/video_output/directx/events.c: compilation fix.
[vlc] / modules / video_output / directx / events.c
1 /*****************************************************************************
2  * events.c: Windows DirectX video output events handler
3  *****************************************************************************
4  * Copyright (C) 2001-2004 VideoLAN
5  * $Id$
6  *
7  * Authors: Gildas Bazin <gbazin@videolan.org>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
22  *****************************************************************************/
23
24
25 /*****************************************************************************
26  * Preamble: This file contains the functions related to the creation of
27  *             a window and the handling of its messages (events).
28  *****************************************************************************/
29 #include <errno.h>                                                 /* ENOMEM */
30 #include <stdlib.h>                                                /* free() */
31 #include <ctype.h>                                              /* tolower() */
32 #include <string.h>                                            /* strerror() */
33
34 #include <vlc/vlc.h>
35 #include <vlc/intf.h>
36 #include <vlc/input.h>
37 #include <vlc/vout.h>
38
39 #include <windows.h>
40 #include <windowsx.h>
41 #include <shellapi.h>
42
43 #include <ddraw.h>
44
45 #include "vlc_keys.h"
46 #include "vout.h"
47
48 /*****************************************************************************
49  * Local prototypes.
50  *****************************************************************************/
51 static int  DirectXCreateWindow( vout_thread_t *p_vout );
52 static void DirectXCloseWindow ( vout_thread_t *p_vout );
53 static long FAR PASCAL DirectXEventProc( HWND, UINT, WPARAM, LPARAM );
54 static long FAR PASCAL DirectXVideoEventProc( HWND, UINT, WPARAM, LPARAM );
55
56 static int Control( vout_thread_t *p_vout, int i_query, va_list args );
57
58 static void DirectXPopupMenu( event_thread_t *p_event, vlc_bool_t b_open )
59 {
60     playlist_t *p_playlist =
61         vlc_object_find( p_event, VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
62     if( p_playlist != NULL )
63     {
64         vlc_value_t val;
65         val.b_bool = b_open;
66         var_Set( p_playlist, "intf-popupmenu", val );
67         vlc_object_release( p_playlist );
68     }
69 }
70
71 static int DirectXConvertKey( int i_key );
72
73 /*****************************************************************************
74  * DirectXEventThread: Create video window & handle its messages
75  *****************************************************************************
76  * This function creates a video window and then enters an infinite loop
77  * that handles the messages sent to that window.
78  * The main goal of this thread is to isolate the Win32 PeekMessage function
79  * because this one can block for a long time.
80  *****************************************************************************/
81 void DirectXEventThread( event_thread_t *p_event )
82 {
83     MSG msg;
84     POINT old_mouse_pos = {0,0};
85     vlc_value_t val;
86     int i_width, i_height, i_x, i_y;
87
88     /* Initialisation */
89     p_event->p_vout->pf_control = Control;
90
91     /* Create a window for the video */
92     /* Creating a window under Windows also initializes the thread's event
93      * message queue */
94     if( DirectXCreateWindow( p_event->p_vout ) )
95     {
96         msg_Err( p_event, "out of memory" );
97         p_event->b_dead = VLC_TRUE;
98     }
99
100     /* Signal the creation of the window */
101     vlc_thread_ready( p_event );
102
103     /* Main loop */
104     /* GetMessage will sleep if there's no message in the queue */
105     while( !p_event->b_die && ( p_event->p_vout->p_sys->hparent ||
106            GetMessage( &msg, p_event->p_vout->p_sys->hwnd, 0, 0 ) ) )
107     {
108         /* Check if we are asked to exit */
109         if( p_event->b_die )
110             break;
111
112         if( p_event->p_vout->p_sys->hparent )
113         {
114             /* Parent window was created in another thread so we can't
115              * access the window messages. */
116             msleep( INTF_IDLE_SLEEP );
117             continue;
118         }
119
120         switch( msg.message )
121         {
122
123         case WM_MOUSEMOVE:
124             vout_PlacePicture( p_event->p_vout,
125                                p_event->p_vout->p_sys->i_window_width,
126                                p_event->p_vout->p_sys->i_window_height,
127                                &i_x, &i_y, &i_width, &i_height );
128
129             if( msg.hwnd != p_event->p_vout->p_sys->hwnd )
130             {
131                 /* Child window */
132                 i_x = i_y = 0;
133             }
134
135             if( i_width && i_height )
136             {
137                 val.i_int = ( GET_X_LPARAM(msg.lParam) - i_x )
138                              * p_event->p_vout->render.i_width / i_width;
139                 var_Set( p_event->p_vout, "mouse-x", val );
140                 val.i_int = ( GET_Y_LPARAM(msg.lParam) - i_y )
141                              * p_event->p_vout->render.i_height / i_height;
142                 var_Set( p_event->p_vout, "mouse-y", val );
143
144                 val.b_bool = VLC_TRUE;
145                 var_Set( p_event->p_vout, "mouse-moved", val );
146             }
147
148         case WM_NCMOUSEMOVE:
149             if( (abs(GET_X_LPARAM(msg.lParam) - old_mouse_pos.x) > 2 ||
150                 (abs(GET_Y_LPARAM(msg.lParam) - old_mouse_pos.y)) > 2 ) )
151             {
152                 GetCursorPos( &old_mouse_pos );
153                 p_event->p_vout->p_sys->i_lastmoved = mdate();
154
155                 if( p_event->p_vout->p_sys->b_cursor_hidden )
156                 {
157                     p_event->p_vout->p_sys->b_cursor_hidden = 0;
158                     ShowCursor( TRUE );
159                 }
160             }
161             break;
162
163         case WM_VLC_HIDE_MOUSE:
164             GetCursorPos( &old_mouse_pos );
165             ShowCursor( FALSE );
166             break;
167
168         case WM_LBUTTONDOWN:
169             var_Get( p_event->p_vout, "mouse-button-down", &val );
170             val.i_int |= 1;
171             var_Set( p_event->p_vout, "mouse-button-down", val );
172             DirectXPopupMenu( p_event, VLC_FALSE );
173             break;
174
175         case WM_LBUTTONUP:
176             var_Get( p_event->p_vout, "mouse-button-down", &val );
177             val.i_int &= ~1;
178             var_Set( p_event->p_vout, "mouse-button-down", val );
179
180             val.b_bool = VLC_TRUE;
181             var_Set( p_event->p_vout, "mouse-clicked", val );
182             break;
183
184         case WM_LBUTTONDBLCLK:
185             p_event->p_vout->p_sys->i_changes |= VOUT_FULLSCREEN_CHANGE;
186             break;
187
188         case WM_MBUTTONDOWN:
189             var_Get( p_event->p_vout, "mouse-button-down", &val );
190             val.i_int |= 2;
191             var_Set( p_event->p_vout, "mouse-button-down", val );
192             DirectXPopupMenu( p_event, VLC_FALSE );
193             break;
194
195         case WM_MBUTTONUP:
196             var_Get( p_event->p_vout, "mouse-button-down", &val );
197             val.i_int &= ~2;
198             var_Set( p_event->p_vout, "mouse-button-down", val );
199             break;
200
201         case WM_RBUTTONDOWN:
202             var_Get( p_event->p_vout, "mouse-button-down", &val );
203             val.i_int |= 4;
204             var_Set( p_event->p_vout, "mouse-button-down", val );
205             DirectXPopupMenu( p_event, VLC_FALSE );
206             break;
207
208         case WM_RBUTTONUP:
209             var_Get( p_event->p_vout, "mouse-button-down", &val );
210             val.i_int &= ~4;
211             var_Set( p_event->p_vout, "mouse-button-down", val );
212             DirectXPopupMenu( p_event, VLC_TRUE );
213             break;
214
215         case WM_KEYDOWN:
216         case WM_SYSKEYDOWN:
217             /* The key events are first processed here and not translated
218              * into WM_CHAR events because we need to know the status of the
219              * modifier keys. */
220             val.i_int = DirectXConvertKey( msg.wParam );
221             if( !val.i_int )
222             {
223                 /* This appears to be a "normal" (ascii) key */
224                 val.i_int = tolower( MapVirtualKey( msg.wParam, 2 ) );
225             }
226
227             if( val.i_int )
228             {
229                 if( GetKeyState(VK_CONTROL) & 0x8000 )
230                 {
231                     val.i_int |= KEY_MODIFIER_CTRL;
232                 }
233                 if( GetKeyState(VK_SHIFT) & 0x8000 )
234                 {
235                     val.i_int |= KEY_MODIFIER_SHIFT;
236                 }
237                 if( GetKeyState(VK_MENU) & 0x8000 )
238                 {
239                     val.i_int |= KEY_MODIFIER_ALT;
240                 }
241
242                 var_Set( p_event->p_vlc, "key-pressed", val );
243             }
244             break;
245
246         case WM_MOUSEWHEEL:
247             if( GET_WHEEL_DELTA_WPARAM( msg.wParam ) > 0 )
248             {
249                 val.i_int = KEY_MOUSEWHEELUP;
250             }
251             else
252             {
253                 val.i_int = KEY_MOUSEWHEELDOWN;
254             }
255             if( val.i_int )
256             {
257                 if( GetKeyState(VK_CONTROL) & 0x8000 )
258                 {
259                     val.i_int |= KEY_MODIFIER_CTRL;
260                 }
261                 if( GetKeyState(VK_SHIFT) & 0x8000 )
262                 {
263                     val.i_int |= KEY_MODIFIER_SHIFT;
264                 }
265                 if( GetKeyState(VK_MENU) & 0x8000 )
266                 {
267                     val.i_int |= KEY_MODIFIER_ALT;
268                 }
269
270                 var_Set( p_event->p_vlc, "key-pressed", val );
271             }
272             break;
273
274         case WM_VLC_CHANGE_TEXT:
275             var_Get( p_event->p_vout, "video-title", &val );
276
277             if( !val.psz_string || !*val.psz_string ) /* Default video title */
278             {
279                 if( p_event->p_vout->p_sys->b_using_overlay )
280                     SetWindowText( p_event->p_vout->p_sys->hwnd,
281                         VOUT_TITLE " (hardware YUV overlay DirectX output)" );
282                 else if( p_event->p_vout->p_sys->b_hw_yuv )
283                     SetWindowText( p_event->p_vout->p_sys->hwnd,
284                         VOUT_TITLE " (hardware YUV DirectX output)" );
285                 else SetWindowText( p_event->p_vout->p_sys->hwnd,
286                         VOUT_TITLE " (software RGB DirectX output)" );
287             }
288             else
289             {
290                 SetWindowText( p_event->p_vout->p_sys->hwnd, val.psz_string );
291             }
292             break;
293
294         default:
295             /* Messages we don't handle directly are dispatched to the
296              * window procedure */
297             TranslateMessage(&msg);
298             DispatchMessage(&msg);
299             break;
300
301         } /* End Switch */
302
303     } /* End Main loop */
304
305     /* Check for WM_QUIT if we created the window */
306     if( !p_event->p_vout->p_sys->hparent && msg.message == WM_QUIT )
307     {
308         msg_Warn( p_event, "WM_QUIT... should not happen!!" );
309         p_event->p_vout->p_sys->hwnd = NULL; /* Window already destroyed */
310     }
311
312     msg_Dbg( p_event, "DirectXEventThread terminating" );
313
314     /* clear the changes formerly signaled */
315     p_event->p_vout->p_sys->i_changes = 0;
316
317     DirectXCloseWindow( p_event->p_vout );
318 }
319
320
321 /* following functions are local */
322
323 /*****************************************************************************
324  * DirectXCreateWindow: create a window for the video.
325  *****************************************************************************
326  * Before creating a direct draw surface, we need to create a window in which
327  * the video will be displayed. This window will also allow us to capture the
328  * events.
329  *****************************************************************************/
330 static int DirectXCreateWindow( vout_thread_t *p_vout )
331 {
332     HINSTANCE  hInstance;
333     HMENU      hMenu;
334     RECT       rect_window;
335
336     msg_Dbg( p_vout, "DirectXCreateWindow" );
337
338     /* Get this module's instance */
339     hInstance = GetModuleHandle(NULL);
340
341     /* If an external window was specified, we'll draw in it. */
342     p_vout->p_sys->hparent = p_vout->p_sys->hwnd =
343         vout_RequestWindow( p_vout, &p_vout->p_sys->i_window_x,
344                             &p_vout->p_sys->i_window_y,
345                             &p_vout->p_sys->i_window_width,
346                             &p_vout->p_sys->i_window_height );
347
348     if( p_vout->p_sys->hparent )
349     {
350         msg_Dbg( p_vout, "using external window %p\n", p_vout->p_sys->hwnd );
351
352         /* Set stuff in the window that we can not put directly in
353          * a class (see below). */
354         SetClassLong( p_vout->p_sys->hwnd,
355                       GCL_STYLE, CS_DBLCLKS );
356         SetClassLong( p_vout->p_sys->hwnd,
357                       GCL_HBRBACKGROUND, (LONG)GetStockObject(BLACK_BRUSH) );
358         SetClassLong( p_vout->p_sys->hwnd,
359                       GCL_HCURSOR, (LONG)LoadCursor(NULL, IDC_ARROW) );
360         /* Store a p_vout pointer into the window local storage (for later
361          * use in DirectXEventProc). */
362         SetWindowLongPtr( p_vout->p_sys->hwnd, GWLP_USERDATA, (LONG_PTR)p_vout );
363
364         p_vout->p_sys->pf_wndproc =
365             (WNDPROC)SetWindowLong( p_vout->p_sys->hwnd, GWLP_WNDPROC,
366                                     (LONG_PTR)DirectXEventProc );
367
368         /* Blam! Erase everything that might have been there. */
369         InvalidateRect( p_vout->p_sys->hwnd, NULL, TRUE );
370     }
371     else
372     {
373         WNDCLASSEX wc;                            /* window class components */
374         HICON      vlc_icon = NULL;
375         char       vlc_path[MAX_PATH+1];
376
377         /* We create the window ourself, there is no previous window proc. */
378         p_vout->p_sys->pf_wndproc = NULL;
379
380         /* Get the Icon from the main app */
381         vlc_icon = NULL;
382         if( GetModuleFileName( NULL, vlc_path, MAX_PATH ) )
383         {
384             vlc_icon = ExtractIcon( hInstance, vlc_path, 0 );
385         }
386
387         /* Fill in the window class structure */
388         wc.cbSize        = sizeof(WNDCLASSEX);
389         wc.style         = CS_DBLCLKS;                   /* style: dbl click */
390         wc.lpfnWndProc   = (WNDPROC)DirectXEventProc;       /* event handler */
391         wc.cbClsExtra    = 0;                         /* no extra class data */
392         wc.cbWndExtra    = 0;                        /* no extra window data */
393         wc.hInstance     = hInstance;                            /* instance */
394         wc.hIcon         = vlc_icon;                /* load the vlc big icon */
395         wc.hCursor       = LoadCursor(NULL, IDC_ARROW);    /* default cursor */
396         wc.hbrBackground = GetStockObject(BLACK_BRUSH);  /* background color */
397         wc.lpszMenuName  = NULL;                                  /* no menu */
398         wc.lpszClassName = "VLC DirectX";             /* use a special class */
399         wc.hIconSm       = vlc_icon;              /* load the vlc small icon */
400
401         /* Register the window class */
402         if( !RegisterClassEx(&wc) )
403         {
404             WNDCLASS wndclass;
405
406             if( vlc_icon )
407             {
408                 DestroyIcon( vlc_icon );
409             }
410
411             /* Check why it failed. If it's because one already exists
412              * then fine, otherwise return with an error. */
413             if( !GetClassInfo( hInstance, "VLC DirectX", &wndclass ) )
414             {
415                 msg_Err( p_vout, "DirectXCreateWindow RegisterClass FAILED" );
416                 return VLC_EGENERIC;
417             }
418         }
419
420         /* When you create a window you give the dimensions you wish it to
421          * have. Unfortunatly these dimensions will include the borders and
422          * titlebar. We use the following function to find out the size of
423          * the window corresponding to the useable surface we want */
424         rect_window.top    = 10;
425         rect_window.left   = 10;
426         rect_window.right  = rect_window.left + p_vout->p_sys->i_window_width;
427         rect_window.bottom = rect_window.top + p_vout->p_sys->i_window_height;
428         AdjustWindowRect( &rect_window, WS_OVERLAPPEDWINDOW|WS_SIZEBOX, 0 );
429
430         /* Create the window */
431         p_vout->p_sys->hwnd =
432             CreateWindow( "VLC DirectX",             /* name of window class */
433                     VOUT_TITLE " (DirectX Output)", /* window title bar text */
434                     WS_OVERLAPPEDWINDOW | WS_SIZEBOX | WS_VISIBLE |
435                     WS_CLIPCHILDREN,                         /* window style */
436                     (p_vout->p_sys->i_window_x < 0) ? CW_USEDEFAULT :
437                         p_vout->p_sys->i_window_x,   /* default X coordinate */
438                     (p_vout->p_sys->i_window_y < 0) ? CW_USEDEFAULT :
439                         p_vout->p_sys->i_window_y,   /* default Y coordinate */
440                     rect_window.right - rect_window.left,    /* window width */
441                     rect_window.bottom - rect_window.top,   /* window height */
442                     NULL,                                /* no parent window */
443                     NULL,                          /* no menu in this window */
444                     hInstance,            /* handle of this program instance */
445                     (LPVOID)p_vout );            /* send p_vout to WM_CREATE */
446
447         if( !p_vout->p_sys->hwnd )
448         {
449             msg_Warn( p_vout, "DirectXCreateWindow create window FAILED" );
450             return VLC_EGENERIC;
451         }
452     }
453
454     /* Now display the window */
455     ShowWindow( p_vout->p_sys->hwnd, SW_SHOW );
456
457     /* Create video sub-window. This sub window will always exactly match
458      * the size of the video, which allows us to use crazy overlay colorkeys
459      * without having them shown outside of the video area. */
460     SendMessage( p_vout->p_sys->hwnd, WM_VLC_CREATE_VIDEO_WIN, 0, 0 );
461
462     /* Append a "Always On Top" entry in the system menu */
463     hMenu = GetSystemMenu( p_vout->p_sys->hwnd, FALSE );
464     AppendMenu( hMenu, MF_SEPARATOR, 0, "" );
465     AppendMenu( hMenu, MF_STRING | MF_UNCHECKED,
466                        IDM_TOGGLE_ON_TOP, "Always on &Top" );
467
468     return VLC_SUCCESS;
469 }
470
471 /*****************************************************************************
472  * DirectXCloseWindow: close the window created by DirectXCreateWindow
473  *****************************************************************************
474  * This function returns all resources allocated by DirectXCreateWindow.
475  *****************************************************************************/
476 static void DirectXCloseWindow( vout_thread_t *p_vout )
477 {
478     msg_Dbg( p_vout, "DirectXCloseWindow" );
479
480     if( p_vout->p_sys->hwnd && !p_vout->p_sys->hparent )
481     {
482         DestroyWindow( p_vout->p_sys->hwnd );
483     }
484     else if( p_vout->p_sys->hparent )
485     {
486         /* Get rid of the video sub-window */
487         PostMessage( p_vout->p_sys->hvideownd, WM_VLC_DESTROY_VIDEO_WIN, 0, 0);
488
489         /* We don't want our windowproc to be called anymore */
490         SetWindowLongPtr( p_vout->p_sys->hwnd,
491                           GWLP_WNDPROC, (LONG_PTR)p_vout->p_sys->pf_wndproc );
492         SetWindowLongPtr( p_vout->p_sys->hwnd, GWLP_USERDATA, 0 );
493
494         /* Blam! Erase everything that might have been there. */
495         InvalidateRect( p_vout->p_sys->hwnd, NULL, TRUE );
496
497         vout_ReleaseWindow( p_vout, (void *)p_vout->p_sys->hparent );
498     }
499
500     p_vout->p_sys->hwnd = NULL;
501
502     /* We don't unregister the Window Class because it could lead to race
503      * conditions and it will be done anyway by the system when the app will
504      * exit */
505 }
506
507 /*****************************************************************************
508  * DirectXUpdateRects: update clipping rectangles
509  *****************************************************************************
510  * This function is called when the window position or size are changed, and
511  * its job is to update the source and destination RECTs used to display the
512  * picture.
513  *****************************************************************************/
514 void DirectXUpdateRects( vout_thread_t *p_vout, vlc_bool_t b_force )
515 {
516 #define rect_src p_vout->p_sys->rect_src
517 #define rect_src_clipped p_vout->p_sys->rect_src_clipped
518 #define rect_dest p_vout->p_sys->rect_dest
519 #define rect_dest_clipped p_vout->p_sys->rect_dest_clipped
520
521     int i_width, i_height, i_x, i_y;
522
523     RECT  rect;
524     POINT point;
525
526     /* Retrieve the window size */
527     GetClientRect( p_vout->p_sys->hwnd, &rect );
528
529     /* Retrieve the window position */
530     point.x = point.y = 0;
531     ClientToScreen( p_vout->p_sys->hwnd, &point );
532
533     /* If nothing changed, we can return */
534     if( !b_force
535          && p_vout->p_sys->i_window_width == rect.right
536          && p_vout->p_sys->i_window_height == rect.bottom
537          && p_vout->p_sys->i_window_x == point.x
538          && p_vout->p_sys->i_window_y == point.y )
539     {
540         return;
541     }
542
543     /* Update the window position and size */
544     p_vout->p_sys->i_window_x = point.x;
545     p_vout->p_sys->i_window_y = point.y;
546     p_vout->p_sys->i_window_width = rect.right;
547     p_vout->p_sys->i_window_height = rect.bottom;
548
549     vout_PlacePicture( p_vout, rect.right, rect.bottom,
550                        &i_x, &i_y, &i_width, &i_height );
551
552     if( p_vout->p_sys->hvideownd )
553         SetWindowPos( p_vout->p_sys->hvideownd, HWND_TOP,
554                       i_x, i_y, i_width, i_height, 0 );
555
556     /* Destination image position and dimensions */
557     rect_dest.left = point.x + i_x;
558     rect_dest.right = rect_dest.left + i_width;
559     rect_dest.top = point.y + i_y;
560     rect_dest.bottom = rect_dest.top + i_height;
561
562     /* Apply overlay hardware constraints */
563     if( p_vout->p_sys->b_using_overlay )
564     {
565         if( p_vout->p_sys->i_align_dest_boundary )
566             rect_dest.left = ( rect_dest.left +
567                 p_vout->p_sys->i_align_dest_boundary / 2 ) & 
568                 ~p_vout->p_sys->i_align_dest_boundary;
569
570         if( p_vout->p_sys->i_align_dest_size )
571             rect_dest.right = (( rect_dest.right - rect_dest.left +
572                 p_vout->p_sys->i_align_dest_size / 2 ) & 
573                 ~p_vout->p_sys->i_align_dest_size) + rect_dest.left;
574     }
575
576     /* UpdateOverlay directdraw function doesn't automatically clip to the
577      * display size so we need to do it otherwise it will fail */
578
579     /* Clip the destination window */
580     if( !IntersectRect( &rect_dest_clipped, &rect_dest,
581                         &p_vout->p_sys->rect_display ) )
582     {
583         SetRectEmpty( &rect_src_clipped );
584         return;
585     }
586
587 #if 0
588     msg_Dbg( p_vout, "DirectXUpdateRects image_dst_clipped coords:"
589                      " %i,%i,%i,%i",
590                      rect_dest_clipped.left, rect_dest_clipped.top,
591                      rect_dest_clipped.right, rect_dest_clipped.bottom );
592 #endif
593
594     /* the 2 following lines are to fix a bug when clicking on the desktop */
595     if( (rect_dest_clipped.right - rect_dest_clipped.left)==0 ||
596         (rect_dest_clipped.bottom - rect_dest_clipped.top)==0 )
597     {
598         SetRectEmpty( &rect_src_clipped );
599         return;
600     }
601
602     /* src image dimensions */
603     rect_src.left = 0;
604     rect_src.top = 0;
605     rect_src.right = p_vout->render.i_width;
606     rect_src.bottom = p_vout->render.i_height;
607
608     /* Clip the source image */
609     rect_src_clipped.left = (rect_dest_clipped.left - rect_dest.left) *
610       p_vout->render.i_width / (rect_dest.right - rect_dest.left);
611     rect_src_clipped.right = p_vout->render.i_width -
612       (rect_dest.right - rect_dest_clipped.right) * p_vout->render.i_width /
613       (rect_dest.right - rect_dest.left);
614     rect_src_clipped.top = (rect_dest_clipped.top - rect_dest.top) *
615       p_vout->render.i_height / (rect_dest.bottom - rect_dest.top);
616     rect_src_clipped.bottom = p_vout->render.i_height -
617       (rect_dest.bottom - rect_dest_clipped.bottom) * p_vout->render.i_height /
618       (rect_dest.bottom - rect_dest.top);
619
620     /* Apply overlay hardware constraints */
621     if( p_vout->p_sys->b_using_overlay )
622     {
623         if( p_vout->p_sys->i_align_src_boundary )
624             rect_src_clipped.left = ( rect_src_clipped.left +
625                 p_vout->p_sys->i_align_src_boundary / 2 ) & 
626                 ~p_vout->p_sys->i_align_src_boundary;
627
628         if( p_vout->p_sys->i_align_src_size )
629             rect_src_clipped.right = (( rect_src_clipped.right -
630                 rect_src_clipped.left +
631                 p_vout->p_sys->i_align_src_size / 2 ) & 
632                 ~p_vout->p_sys->i_align_src_size) + rect_src_clipped.left;
633     }
634
635 #if 0
636     msg_Dbg( p_vout, "DirectXUpdateRects image_src_clipped"
637                      " coords: %i,%i,%i,%i",
638                      rect_src_clipped.left, rect_src_clipped.top,
639                      rect_src_clipped.right, rect_src_clipped.bottom );
640 #endif
641
642     /* The destination coordinates need to be relative to the current
643      * directdraw primary surface (display) */
644     rect_dest_clipped.left -= p_vout->p_sys->rect_display.left;
645     rect_dest_clipped.right -= p_vout->p_sys->rect_display.left;
646     rect_dest_clipped.top -= p_vout->p_sys->rect_display.top;
647     rect_dest_clipped.bottom -= p_vout->p_sys->rect_display.top;
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     if( p_vout->p_sys->pf_wndproc )
787     {
788         LRESULT i_ret;
789
790         /* Hmmm mozilla does manage somehow to save the pointer to our
791          * windowproc and will call us again whereby creating an
792          * infinite loop.
793          * We can detect this by resetting GWL_USERDATA before calling
794          * the parent's windowproc. */
795         SetWindowLongPtr( p_vout->p_sys->hwnd, GWLP_USERDATA, 0 );
796
797         /* Call next window proc in chain */
798         i_ret = CallWindowProc( p_vout->p_sys->pf_wndproc, hwnd, message,
799                                 wParam, lParam );
800
801         SetWindowLongPtr( p_vout->p_sys->hwnd, GWLP_USERDATA,
802                           (LONG_PTR)p_vout );
803         return i_ret;
804     }
805     else
806     {
807         /* Let windows handle the message */
808         return DefWindowProc(hwnd, message, wParam, lParam);
809     }
810
811 }
812
813 static long FAR PASCAL DirectXVideoEventProc( HWND hwnd, UINT message,
814                                               WPARAM wParam, LPARAM lParam )
815 {
816     WNDPROC pf_parentwndproc;
817     POINT pt;
818
819     switch( message )
820     {
821     case WM_VLC_DESTROY_VIDEO_WIN:
822         /* Destroy video sub-window */
823         DestroyWindow( hwnd );
824         break;
825     case WM_MOUSEMOVE:
826     case WM_LBUTTONDOWN:
827     case WM_LBUTTONUP:
828     case WM_LBUTTONDBLCLK:
829     case WM_MBUTTONDOWN:
830     case WM_MBUTTONUP:
831     case WM_RBUTTONDOWN:
832     case WM_RBUTTONUP:
833         /* Translate mouse cursor position to parent window coordinates. */
834         pt.x = LOWORD( lParam );
835         pt.y = HIWORD( lParam );
836         MapWindowPoints( hwnd, GetParent( hwnd ), &pt, 1 );
837         lParam = MAKELPARAM( pt.x, pt.y );
838         /* Fall through. */
839     case WM_KEYDOWN:
840     case WM_KEYUP:
841     case WM_SYSKEYDOWN:
842     case WM_SYSKEYUP:
843         /* Foward these to the original window proc of the parent so the
844          * creator of the window gets a chance to process them. If we created
845          * the parent window ourself DirectXEventThread will process these
846          * and they will never make it here.
847          * Note that we fake the hwnd to be our parent in order to prevent
848          * confusion in the creator's window proc. */
849         pf_parentwndproc = (WNDPROC)GetWindowLongPtr( hwnd, GWLP_USERDATA );
850
851         if( pf_parentwndproc )
852         {
853             LRESULT i_ret;
854             LONG_PTR p_backup;
855
856             pf_parentwndproc = (WNDPROC)GetWindowLongPtr( hwnd, GWLP_USERDATA);
857
858             p_backup = SetWindowLongPtr( GetParent( hwnd ), GWLP_USERDATA, 0 );
859             i_ret = CallWindowProc( pf_parentwndproc, GetParent( hwnd ),
860                                     message, wParam, lParam );
861             SetWindowLongPtr( GetParent( hwnd ), GWLP_USERDATA, p_backup );
862
863             return i_ret;
864         }
865         break;
866     }
867
868     return DefWindowProc(hwnd, message, wParam, lParam);
869 }
870
871 static struct
872 {
873     int i_dxkey;
874     int i_vlckey;
875
876 } dxkeys_to_vlckeys[] =
877 {
878     { VK_F1, KEY_F1 }, { VK_F2, KEY_F2 }, { VK_F3, KEY_F3 }, { VK_F4, KEY_F4 },
879     { VK_F5, KEY_F5 }, { VK_F6, KEY_F6 }, { VK_F7, KEY_F7 }, { VK_F8, KEY_F8 },
880     { VK_F9, KEY_F9 }, { VK_F10, KEY_F10 }, { VK_F11, KEY_F11 },
881     { VK_F12, KEY_F12 },
882
883     { VK_RETURN, KEY_ENTER },
884     { VK_SPACE, KEY_SPACE },
885     { VK_ESCAPE, KEY_ESC },
886
887     { VK_LEFT, KEY_LEFT },
888     { VK_RIGHT, KEY_RIGHT },
889     { VK_UP, KEY_UP },
890     { VK_DOWN, KEY_DOWN },
891
892     { VK_HOME, KEY_HOME },
893     { VK_END, KEY_END },
894     { VK_PRIOR, KEY_PAGEUP },
895     { VK_NEXT, KEY_PAGEDOWN },
896
897     { VK_CONTROL, 0 },
898     { VK_SHIFT, 0 },
899     { VK_MENU, 0 },
900
901     { 0, 0 }
902 };
903
904 static int DirectXConvertKey( int i_key )
905 {
906     int i;
907
908     for( i = 0; dxkeys_to_vlckeys[i].i_dxkey != 0; i++ )
909     {
910         if( dxkeys_to_vlckeys[i].i_dxkey == i_key )
911         {
912             return dxkeys_to_vlckeys[i].i_vlckey;
913         }
914     }
915
916     return 0;
917 }
918
919 /*****************************************************************************
920  * Control: control facility for the vout
921  *****************************************************************************/
922 static int Control( vout_thread_t *p_vout, int i_query, va_list args )
923 {
924     double f_arg;
925     RECT rect_window;
926
927     switch( i_query )
928     {
929     case VOUT_SET_ZOOM:
930         if( p_vout->p_sys->hparent )
931             return vout_ControlWindow( p_vout,
932                     (void *)p_vout->p_sys->hparent, i_query, args );
933
934         f_arg = va_arg( args, double );
935
936         /* Update dimensions */
937         rect_window.top = rect_window.left = 0;
938         rect_window.right  = p_vout->i_window_width * f_arg;
939         rect_window.bottom = p_vout->i_window_height * f_arg;
940         AdjustWindowRect( &rect_window, WS_OVERLAPPEDWINDOW|WS_SIZEBOX, 0 );
941
942         SetWindowPos( p_vout->p_sys->hwnd, 0, 0, 0,
943                       rect_window.right - rect_window.left,
944                       rect_window.bottom - rect_window.top, SWP_NOMOVE );
945
946         return VLC_SUCCESS;
947
948     case VOUT_CLOSE:
949         return VLC_SUCCESS;
950
951     default:
952         msg_Dbg( p_vout, "control query not supported" );
953         return VLC_EGENERIC;
954     }
955 }