]> git.sesse.net Git - vlc/blob - modules/video_output/directx/events.c
* modules/video_output/directx/events.c: fixed mouse coordinates for dvd navigation...
[vlc] / modules / video_output / directx / events.c
1 /*****************************************************************************
2  * events.c: Windows DirectX video output events handler
3  *****************************************************************************
4  * Copyright (C) 2001 VideoLAN
5  * $Id: events.c,v 1.35 2003/12/23 15:27:50 gbazin Exp $
6  *
7  * Authors: Gildas Bazin <gbazin@netcourrier.com>
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 void DirectXPopupMenu( event_thread_t *p_event, vlc_bool_t b_open )
57 {
58     playlist_t *p_playlist =
59         vlc_object_find( p_event, VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
60     if( p_playlist != NULL )
61     {
62         vlc_value_t val;
63         val.b_bool = b_open;
64         var_Set( p_playlist, "intf-popupmenu", val );
65         vlc_object_release( p_playlist );
66     }
67 }
68
69 static int DirectXConvertKey( int i_key );
70
71 /*****************************************************************************
72  * DirectXEventThread: Create video window & handle its messages
73  *****************************************************************************
74  * This function creates a video window and then enters an infinite loop
75  * that handles the messages sent to that window.
76  * The main goal of this thread is to isolate the Win32 PeekMessage function
77  * because this one can block for a long time.
78  *****************************************************************************/
79 void DirectXEventThread( event_thread_t *p_event )
80 {
81     MSG msg;
82     POINT old_mouse_pos = {0,0};
83     vlc_value_t val;
84     int i_width, i_height, i_x, i_y;
85
86     /* Initialisation */
87
88     /* Create a window for the video */
89     /* Creating a window under Windows also initializes the thread's event
90      * message queue */
91     if( DirectXCreateWindow( p_event->p_vout ) )
92     {
93         msg_Err( p_event, "out of memory" );
94         p_event->b_dead = VLC_TRUE;
95     }
96
97     /* Signal the creation of the window */
98     vlc_thread_ready( p_event );
99
100     /* Main loop */
101     /* GetMessage will sleep if there's no message in the queue */
102     while( !p_event->b_die && ( p_event->p_vout->p_sys->hparent ||
103            GetMessage( &msg, p_event->p_vout->p_sys->hwnd, 0, 0 ) ) )
104     {
105         /* Check if we are asked to exit */
106         if( p_event->b_die )
107             break;
108
109         if( p_event->p_vout->p_sys->hparent )
110         {
111             /* Parent window was created in another thread so we can't
112              * access the window messages. */
113             msleep( INTF_IDLE_SLEEP );
114             continue;
115         }
116
117         switch( msg.message )
118         {
119
120         case WM_MOUSEMOVE:
121             vout_PlacePicture( p_event->p_vout,
122                                p_event->p_vout->p_sys->i_window_width,
123                                p_event->p_vout->p_sys->i_window_height,
124                                &i_x, &i_y, &i_width, &i_height );
125
126             if( msg.hwnd != p_event->p_vout->p_sys->hwnd )
127             {
128                 /* Child window */
129                 i_x = i_y = 0;
130             }
131
132             val.i_int = ( GET_X_LPARAM(msg.lParam) - i_x )
133                          * p_event->p_vout->render.i_width / i_width;
134             var_Set( p_event->p_vout, "mouse-x", val );
135             val.i_int = ( GET_Y_LPARAM(msg.lParam) - i_y )
136                          * p_event->p_vout->render.i_height / i_height;
137             var_Set( p_event->p_vout, "mouse-y", val );
138
139             val.b_bool = VLC_TRUE;
140             var_Set( p_event->p_vout, "mouse-moved", val );
141
142         case WM_NCMOUSEMOVE:
143             if( (abs(GET_X_LPARAM(msg.lParam) - old_mouse_pos.x) > 2 ||
144                 (abs(GET_Y_LPARAM(msg.lParam) - old_mouse_pos.y)) > 2 ) )
145             {
146                 GetCursorPos( &old_mouse_pos );
147                 p_event->p_vout->p_sys->i_lastmoved = mdate();
148
149                 if( p_event->p_vout->p_sys->b_cursor_hidden )
150                 {
151                     p_event->p_vout->p_sys->b_cursor_hidden = 0;
152                     ShowCursor( TRUE );
153                 }
154             }
155             break;
156
157         case WM_VLC_HIDE_MOUSE:
158             GetCursorPos( &old_mouse_pos );
159             ShowCursor( FALSE );
160             break;
161
162         case WM_LBUTTONDOWN:
163             var_Get( p_event->p_vout, "mouse-button-down", &val );
164             val.i_int |= 1;
165             var_Set( p_event->p_vout, "mouse-button-down", val );
166             DirectXPopupMenu( p_event, VLC_FALSE );
167             break;
168
169         case WM_LBUTTONUP:
170             var_Get( p_event->p_vout, "mouse-button-down", &val );
171             val.i_int &= ~1;
172             var_Set( p_event->p_vout, "mouse-button-down", val );
173
174             val.b_bool = VLC_TRUE;
175             var_Set( p_event->p_vout, "mouse-clicked", val );
176             break;
177
178         case WM_LBUTTONDBLCLK:
179             p_event->p_vout->p_sys->i_changes |= VOUT_FULLSCREEN_CHANGE;
180             break;
181
182         case WM_MBUTTONDOWN:
183             var_Get( p_event->p_vout, "mouse-button-down", &val );
184             val.i_int |= 2;
185             var_Set( p_event->p_vout, "mouse-button-down", val );
186             DirectXPopupMenu( p_event, VLC_FALSE );
187             break;
188
189         case WM_MBUTTONUP:
190             var_Get( p_event->p_vout, "mouse-button-down", &val );
191             val.i_int &= ~2;
192             var_Set( p_event->p_vout, "mouse-button-down", val );
193             break;
194
195         case WM_RBUTTONDOWN:
196             var_Get( p_event->p_vout, "mouse-button-down", &val );
197             val.i_int |= 4;
198             var_Set( p_event->p_vout, "mouse-button-down", val );
199             DirectXPopupMenu( p_event, VLC_FALSE );
200             break;
201
202         case WM_RBUTTONUP:
203             var_Get( p_event->p_vout, "mouse-button-down", &val );
204             val.i_int &= ~4;
205             var_Set( p_event->p_vout, "mouse-button-down", val );
206             DirectXPopupMenu( p_event, VLC_TRUE );
207             break;
208
209         case WM_KEYDOWN:
210         case WM_SYSKEYDOWN:
211             /* The key events are first processed here and not translated
212              * into WM_CHAR events because we need to know the status of the
213              * modifier keys. */
214             val.i_int = DirectXConvertKey( msg.wParam );
215             if( !val.i_int )
216             {
217                 /* This appears to be a "normal" (ascii) key */
218                 val.i_int = tolower( MapVirtualKey( msg.wParam, 2 ) );
219             }
220
221             if( val.i_int )
222             {
223                 if( GetKeyState(VK_CONTROL) & 0x8000 )
224                 {
225                     val.i_int |= KEY_MODIFIER_CTRL;
226                 }
227                 if( GetKeyState(VK_SHIFT) & 0x8000 )
228                 {
229                     val.i_int |= KEY_MODIFIER_SHIFT;
230                 }
231                 if( GetKeyState(VK_MENU) & 0x8000 )
232                 {
233                     val.i_int |= KEY_MODIFIER_ALT;
234                 }
235
236                 var_Set( p_event->p_vlc, "key-pressed", val );
237             }
238             break;
239
240         case WM_VLC_CHANGE_TEXT:
241             if( p_event->p_vout->p_sys->b_using_overlay )
242                 SetWindowText( p_event->p_vout->p_sys->hwnd,
243                     VOUT_TITLE " (hardware YUV overlay DirectX output)" );
244             else if( p_event->p_vout->p_sys->b_hw_yuv )
245                 SetWindowText( p_event->p_vout->p_sys->hwnd,
246                     VOUT_TITLE " (hardware YUV DirectX output)" );
247             else SetWindowText( p_event->p_vout->p_sys->hwnd,
248                     VOUT_TITLE " (software RGB DirectX output)" );
249             break;
250
251         default:
252             /* Messages we don't handle directly are dispatched to the
253              * window procedure */
254             TranslateMessage(&msg);
255             DispatchMessage(&msg);
256             break;
257
258         } /* End Switch */
259
260     } /* End Main loop */
261
262     if( msg.message == WM_QUIT )
263     {
264         msg_Warn( p_event, "WM_QUIT... should not happen!!" );
265         p_event->p_vout->p_sys->hwnd = NULL; /* Window already destroyed */
266     }
267
268     msg_Dbg( p_event, "DirectXEventThread terminating" );
269
270     /* clear the changes formerly signaled */
271     p_event->p_vout->p_sys->i_changes = 0;
272
273     DirectXCloseWindow( p_event->p_vout );
274 }
275
276
277 /* following functions are local */
278
279 /*****************************************************************************
280  * DirectXCreateWindow: create a window for the video.
281  *****************************************************************************
282  * Before creating a direct draw surface, we need to create a window in which
283  * the video will be displayed. This window will also allow us to capture the
284  * events.
285  *****************************************************************************/
286 static int DirectXCreateWindow( vout_thread_t *p_vout )
287 {
288     HINSTANCE  hInstance;
289     HMENU      hMenu;
290     RECT       rect_window;
291
292     vlc_value_t val;
293
294     msg_Dbg( p_vout, "DirectXCreateWindow" );
295
296     /* Get this module's instance */
297     hInstance = GetModuleHandle(NULL);
298
299     /* If an external window was specified, we'll draw in it. */
300     var_Get( p_vout->p_vlc, "drawable", &val );
301     p_vout->p_sys->hparent = p_vout->p_sys->hwnd =
302              val.i_int ?  (void*)(ptrdiff_t) val.i_int : NULL;
303
304     if( p_vout->p_sys->hparent )
305     {
306         msg_Dbg( p_vout, "using external window %p\n", p_vout->p_sys->hwnd );
307
308         /* Set stuff in the window that we can not put directly in
309          * a class (see below). */
310         SetClassLong( p_vout->p_sys->hwnd,
311                       GCL_STYLE, CS_DBLCLKS );
312         SetClassLong( p_vout->p_sys->hwnd,
313                       GCL_HBRBACKGROUND, (LONG)GetStockObject(BLACK_BRUSH) );
314         SetClassLong( p_vout->p_sys->hwnd,
315                       GCL_HCURSOR, (LONG)LoadCursor(NULL, IDC_ARROW) );
316         /* Store a p_vout pointer into the window local storage (for later
317          * use in DirectXEventProc). */
318         SetWindowLong( p_vout->p_sys->hwnd, GWL_USERDATA, (LONG)p_vout );
319
320         p_vout->p_sys->pf_wndproc =
321                (WNDPROC)SetWindowLong( p_vout->p_sys->hwnd,
322                                        GWL_WNDPROC, (LONG)DirectXEventProc );
323
324         /* Blam! Erase everything that might have been there. */
325         InvalidateRect( p_vout->p_sys->hwnd, NULL, TRUE );
326     }
327     else
328     {
329         WNDCLASSEX wc;                            /* window class components */
330         HICON      vlc_icon = NULL;
331         char       vlc_path[MAX_PATH+1];
332
333         /* Get the Icon from the main app */
334         vlc_icon = NULL;
335         if( GetModuleFileName( NULL, vlc_path, MAX_PATH ) )
336         {
337             vlc_icon = ExtractIcon( hInstance, vlc_path, 0 );
338         }
339
340         /* Fill in the window class structure */
341         wc.cbSize        = sizeof(WNDCLASSEX);
342         wc.style         = CS_DBLCLKS;                   /* style: dbl click */
343         wc.lpfnWndProc   = (WNDPROC)DirectXEventProc;       /* event handler */
344         wc.cbClsExtra    = 0;                         /* no extra class data */
345         wc.cbWndExtra    = 0;                        /* no extra window data */
346         wc.hInstance     = hInstance;                            /* instance */
347         wc.hIcon         = vlc_icon;                /* load the vlc big icon */
348         wc.hCursor       = LoadCursor(NULL, IDC_ARROW);    /* default cursor */
349         wc.hbrBackground = GetStockObject(BLACK_BRUSH);  /* background color */
350         wc.lpszMenuName  = NULL;                                  /* no menu */
351         wc.lpszClassName = "VLC DirectX";             /* use a special class */
352         wc.hIconSm       = vlc_icon;              /* load the vlc small icon */
353
354         /* Register the window class */
355         if( !RegisterClassEx(&wc) )
356         {
357             WNDCLASS wndclass;
358
359             if( vlc_icon )
360             {
361                 DestroyIcon( vlc_icon );
362             }
363
364             /* Check why it failed. If it's because one already exists
365              * then fine, otherwise return with an error. */
366             if( !GetClassInfo( hInstance, "VLC DirectX", &wndclass ) )
367             {
368                 msg_Err( p_vout, "DirectXCreateWindow RegisterClass FAILED" );
369                 return VLC_EGENERIC;
370             }
371         }
372
373         /* When you create a window you give the dimensions you wish it to
374          * have. Unfortunatly these dimensions will include the borders and
375          * titlebar. We use the following function to find out the size of
376          * the window corresponding to the useable surface we want */
377         rect_window.top    = 10;
378         rect_window.left   = 10;
379         rect_window.right  = rect_window.left + p_vout->p_sys->i_window_width;
380         rect_window.bottom = rect_window.top + p_vout->p_sys->i_window_height;
381         AdjustWindowRect( &rect_window, WS_OVERLAPPEDWINDOW|WS_SIZEBOX, 0 );
382
383         /* Create the window */
384         p_vout->p_sys->hwnd =
385             CreateWindow( "VLC DirectX",             /* name of window class */
386                     VOUT_TITLE " (DirectX Output)", /* window title bar text */
387                     WS_OVERLAPPEDWINDOW | WS_SIZEBOX | WS_VISIBLE |
388                     WS_CLIPCHILDREN,                         /* window style */
389                     CW_USEDEFAULT,                   /* default X coordinate */
390                     0,                               /* default Y coordinate */
391                     rect_window.right - rect_window.left,    /* window width */
392                     rect_window.bottom - rect_window.top,   /* window height */
393                     NULL,                                /* no parent window */
394                     NULL,                          /* no menu in this window */
395                     hInstance,            /* handle of this program instance */
396                     (LPVOID)p_vout );            /* send p_vout to WM_CREATE */
397
398         if( !p_vout->p_sys->hwnd )
399         {
400             msg_Warn( p_vout, "DirectXCreateWindow create window FAILED" );
401             return VLC_EGENERIC;
402         }
403     }
404
405     /* Now display the window */
406     ShowWindow( p_vout->p_sys->hwnd, SW_SHOW );
407
408     /* Create video sub-window. This sub window will always exactly match
409      * the size of the video, which allows us to use crazy overlay colorkeys
410      * without having them shown outside of the video area. */
411     SendMessage( p_vout->p_sys->hwnd, WM_VLC_CREATE_VIDEO_WIN, 0, 0 );
412
413     /* Append a "Always On Top" entry in the system menu */
414     hMenu = GetSystemMenu( p_vout->p_sys->hwnd, FALSE );
415     AppendMenu( hMenu, MF_SEPARATOR, 0, "" );
416     AppendMenu( hMenu, MF_STRING | MF_UNCHECKED,
417                        IDM_TOGGLE_ON_TOP, "Always on &Top" );
418
419     return VLC_SUCCESS;
420 }
421
422 /*****************************************************************************
423  * DirectXCloseWindow: close the window created by DirectXCreateWindow
424  *****************************************************************************
425  * This function returns all resources allocated by DirectXCreateWindow.
426  *****************************************************************************/
427 static void DirectXCloseWindow( vout_thread_t *p_vout )
428 {
429     msg_Dbg( p_vout, "DirectXCloseWindow" );
430
431     if( p_vout->p_sys->hwnd && !p_vout->p_sys->hparent )
432     {
433         DestroyWindow( p_vout->p_sys->hwnd );
434     }
435     else if( p_vout->p_sys->hparent )
436     {
437         /* Get rid of the video sub-window */
438         PostMessage( p_vout->p_sys->hvideownd, WM_VLC_DESTROY_VIDEO_WIN, 0, 0);
439
440         /* We don't want our windowproc to be called anymore */
441         SetWindowLong( p_vout->p_sys->hwnd,
442                        GWL_WNDPROC, (LONG)p_vout->p_sys->pf_wndproc );
443         SetWindowLong( p_vout->p_sys->hwnd, GWL_USERDATA, (LONG)NULL );
444
445         /* Blam! Erase everything that might have been there. */
446         InvalidateRect( p_vout->p_sys->hwnd, NULL, TRUE );
447     }
448
449     p_vout->p_sys->hwnd = NULL;
450
451     /* We don't unregister the Window Class because it could lead to race
452      * conditions and it will be done anyway by the system when the app will
453      * exit */
454 }
455
456 /*****************************************************************************
457  * DirectXUpdateRects: update clipping rectangles
458  *****************************************************************************
459  * This function is called when the window position or size are changed, and
460  * its job is to update the source and destination RECTs used to display the
461  * picture.
462  *****************************************************************************/
463 void DirectXUpdateRects( vout_thread_t *p_vout, vlc_bool_t b_force )
464 {
465 #define rect_src p_vout->p_sys->rect_src
466 #define rect_src_clipped p_vout->p_sys->rect_src_clipped
467 #define rect_dest p_vout->p_sys->rect_dest
468 #define rect_dest_clipped p_vout->p_sys->rect_dest_clipped
469
470     int i_width, i_height, i_x, i_y;
471
472     RECT  rect;
473     POINT point;
474
475     /* Retrieve the window size */
476     GetClientRect( p_vout->p_sys->hwnd, &rect );
477
478     /* Retrieve the window position */
479     point.x = point.y = 0;
480     ClientToScreen( p_vout->p_sys->hwnd, &point );
481
482     /* If nothing changed, we can return */
483     if( !b_force
484          && p_vout->p_sys->i_window_width == rect.right
485          && p_vout->p_sys->i_window_height == rect.bottom
486          && p_vout->p_sys->i_window_x == point.x
487          && p_vout->p_sys->i_window_y == point.y )
488     {
489         return;
490     }
491
492     /* Update the window position and size */
493     p_vout->p_sys->i_window_x = point.x;
494     p_vout->p_sys->i_window_y = point.y;
495     p_vout->p_sys->i_window_width = rect.right;
496     p_vout->p_sys->i_window_height = rect.bottom;
497
498     vout_PlacePicture( p_vout, rect.right, rect.bottom,
499                        &i_x, &i_y, &i_width, &i_height );
500
501     SetWindowPos( p_vout->p_sys->hvideownd, HWND_TOP,
502                   i_x, i_y, i_width, i_height, 0 );
503
504     /* Destination image position and dimensions */
505     rect_dest.left = point.x + i_x;
506     rect_dest.right = rect_dest.left + i_width;
507     rect_dest.top = point.y + i_y;
508     rect_dest.bottom = rect_dest.top + i_height;
509
510     /* UpdateOverlay directdraw function doesn't automatically clip to the
511      * display size so we need to do it otherwise it will fail */
512
513     /* Clip the destination window */
514     if( !IntersectRect( &rect_dest_clipped, &rect_dest,
515                         &p_vout->p_sys->rect_display ) )
516     {
517         SetRectEmpty( &rect_src_clipped );
518         return;
519     }
520
521 #if 0
522     msg_Dbg( p_vout, "DirectXUpdateRects image_dst_clipped coords:"
523                      " %i,%i,%i,%i",
524                      rect_dest_clipped.left, rect_dest_clipped.top,
525                      rect_dest_clipped.right, rect_dest_clipped.bottom );
526 #endif
527
528     /* the 2 following lines are to fix a bug when clicking on the desktop */
529     if( (rect_dest_clipped.right - rect_dest_clipped.left)==0 ||
530         (rect_dest_clipped.bottom - rect_dest_clipped.top)==0 )
531     {
532         SetRectEmpty( &rect_src_clipped );
533         return;
534     }
535
536     /* src image dimensions */
537     rect_src.left = 0;
538     rect_src.top = 0;
539     rect_src.right = p_vout->render.i_width;
540     rect_src.bottom = p_vout->render.i_height;
541
542     /* Clip the source image */
543     rect_src_clipped.left = (rect_dest_clipped.left - rect_dest.left) *
544       p_vout->render.i_width / (rect_dest.right - rect_dest.left);
545     rect_src_clipped.right = p_vout->render.i_width -
546       (rect_dest.right - rect_dest_clipped.right) * p_vout->render.i_width /
547       (rect_dest.right - rect_dest.left);
548     rect_src_clipped.top = (rect_dest_clipped.top - rect_dest.top) *
549       p_vout->render.i_height / (rect_dest.bottom - rect_dest.top);
550     rect_src_clipped.bottom = p_vout->render.i_height -
551       (rect_dest.bottom - rect_dest_clipped.bottom) * p_vout->render.i_height /
552       (rect_dest.bottom - rect_dest.top);
553
554     /* The destination coordinates need to be relative to the current
555      * directdraw primary surface (display) */
556     rect_dest_clipped.left -= p_vout->p_sys->rect_display.left;
557     rect_dest_clipped.right -= p_vout->p_sys->rect_display.left;
558     rect_dest_clipped.top -= p_vout->p_sys->rect_display.top;
559     rect_dest_clipped.bottom -= p_vout->p_sys->rect_display.top;
560
561 #if 0
562     msg_Dbg( p_vout, "DirectXUpdateRects image_src_clipped"
563                      " coords: %i,%i,%i,%i",
564                      rect_src_clipped.left, rect_src_clipped.top,
565                      rect_src_clipped.right, rect_src_clipped.bottom );
566 #endif
567
568     /* Signal the change in size/position */
569     p_vout->p_sys->i_changes |= DX_POSITION_CHANGE;
570
571 #undef rect_src
572 #undef rect_src_clipped
573 #undef rect_dest
574 #undef rect_dest_clipped
575 }
576
577 /*****************************************************************************
578  * DirectXEventProc: This is the window event processing function.
579  *****************************************************************************
580  * On Windows, when you create a window you have to attach an event processing
581  * function to it. The aim of this function is to manage "Queued Messages" and
582  * "Nonqueued Messages".
583  * Queued Messages are those picked up and retransmitted by vout_Manage
584  * (using the GetMessage and DispatchMessage functions).
585  * Nonqueued Messages are those that Windows will send directly to this
586  * procedure (like WM_DESTROY, WM_WINDOWPOSCHANGED...)
587  *****************************************************************************/
588 static long FAR PASCAL DirectXEventProc( HWND hwnd, UINT message,
589                                          WPARAM wParam, LPARAM lParam )
590 {
591     vout_thread_t *p_vout;
592
593     if( message == WM_CREATE )
594     {
595         /* Store p_vout for future use */
596         p_vout = (vout_thread_t *)((CREATESTRUCT *)lParam)->lpCreateParams;
597         SetWindowLong( hwnd, GWL_USERDATA, (LONG)p_vout );
598     }
599     else
600     {
601         p_vout = (vout_thread_t *)GetWindowLong( hwnd, GWL_USERDATA );
602     }
603
604     if( !p_vout )
605     {
606         /* Hmmm mozilla does manage somehow to save the pointer to our
607          * windowproc and still calls it after the vout has been closed. */
608         return DefWindowProc(hwnd, message, wParam, lParam);
609     }
610
611     switch( message )
612     {
613
614     case WM_WINDOWPOSCHANGED:
615         DirectXUpdateRects( p_vout, VLC_TRUE );
616         return 0;
617
618     /* the user wants to close the window */
619     case WM_CLOSE:
620     {
621         playlist_t * p_playlist =
622             (playlist_t *)vlc_object_find( p_vout, VLC_OBJECT_PLAYLIST,
623                                            FIND_ANYWHERE );
624         if( p_playlist == NULL )
625         {
626             return 0;
627         }
628
629         playlist_Stop( p_playlist );
630         vlc_object_release( p_playlist );
631         return 0;
632     }
633
634     /* the window has been closed so shut down everything now */
635     case WM_DESTROY:
636         msg_Dbg( p_vout, "WinProc WM_DESTROY" );
637         /* just destroy the window */
638         PostQuitMessage( 0 );
639         return 0;
640
641     case WM_SYSCOMMAND:
642         switch (wParam)
643         {
644             case SC_SCREENSAVE:                     /* catch the screensaver */
645             case SC_MONITORPOWER:              /* catch the monitor turn-off */
646                 msg_Dbg( p_vout, "WinProc WM_SYSCOMMAND" );
647                 return 0;                  /* this stops them from happening */
648             case IDM_TOGGLE_ON_TOP:            /* toggle the "on top" status */
649             {
650                 vlc_value_t val;
651                 msg_Dbg( p_vout, "WinProc WM_SYSCOMMAND: IDM_TOGGLE_ON_TOP");
652
653                 /* Get the current value... */
654                 if( var_Get( p_vout, "video-on-top", &val ) < 0 )
655                     return 0;
656                 /* ...and change it */
657                 val.b_bool = !val.b_bool;
658                 var_Set( p_vout, "video-on-top", val );
659                 return 0;
660                 break;
661             }
662         }
663         break;
664
665     case WM_VLC_CREATE_VIDEO_WIN:
666         /* Create video sub-window */
667         p_vout->p_sys->hvideownd =
668             CreateWindow( "STATIC", "",   /* window class and title bar text */
669                     WS_CHILD | WS_VISIBLE,                   /* window style */
670                     CW_USEDEFAULT, CW_USEDEFAULT,     /* default coordinates */
671                     CW_USEDEFAULT, CW_USEDEFAULT,
672                     hwnd,                                   /* parent window */
673                     NULL, GetModuleHandle(NULL), NULL );
674
675         if( !p_vout->p_sys->hvideownd )
676         {
677             msg_Warn( p_vout, "Can't create video sub-window" );
678         }
679         else
680         {
681             msg_Warn( p_vout, "Created video sub-window" );
682             SetWindowLong( p_vout->p_sys->hvideownd,
683                            GWL_WNDPROC, (LONG)DirectXVideoEventProc );
684         }
685         break;
686
687     default:
688         //msg_Dbg( p_vout, "WinProc WM Default %i", message );
689         break;
690     }
691
692     return DefWindowProc(hwnd, message, wParam, lParam);
693 }
694 static long FAR PASCAL DirectXVideoEventProc( HWND hwnd, UINT message,
695                                               WPARAM wParam, LPARAM lParam )
696 {
697     switch( message )
698     {
699     case WM_VLC_DESTROY_VIDEO_WIN:
700         /* Destroy video sub-window */
701         DestroyWindow( hwnd );
702         break;
703     }
704
705     return DefWindowProc(hwnd, message, wParam, lParam);
706 }
707
708 static struct
709 {
710     int i_dxkey;
711     int i_vlckey;
712
713 } dxkeys_to_vlckeys[] =
714 {
715     { VK_F1, KEY_F1 }, { VK_F2, KEY_F2 }, { VK_F3, KEY_F3 }, { VK_F4, KEY_F4 },
716     { VK_F5, KEY_F5 }, { VK_F6, KEY_F6 }, { VK_F7, KEY_F7 }, { VK_F8, KEY_F8 },
717     { VK_F9, KEY_F9 }, { VK_F10, KEY_F10 }, { VK_F11, KEY_F11 },
718     { VK_F12, KEY_F12 },
719
720     { VK_RETURN, KEY_ENTER },
721     { VK_SPACE, KEY_SPACE },
722     { VK_ESCAPE, KEY_ESC },
723
724     { VK_LEFT, KEY_LEFT },
725     { VK_RIGHT, KEY_RIGHT },
726     { VK_UP, KEY_UP },
727     { VK_DOWN, KEY_DOWN },
728
729     { VK_HOME, KEY_HOME },
730     { VK_END, KEY_END },
731     { VK_PRIOR, KEY_PAGEUP },
732     { VK_NEXT, KEY_PAGEDOWN },
733
734     { VK_CONTROL, 0 },
735     { VK_SHIFT, 0 },
736     { VK_MENU, 0 },
737
738     { 0, 0 }
739 };
740
741 static int DirectXConvertKey( int i_key )
742 {
743     int i;
744
745     for( i = 0; dxkeys_to_vlckeys[i].i_dxkey != 0; i++ )
746     {
747         if( dxkeys_to_vlckeys[i].i_dxkey == i_key )
748         {
749             return dxkeys_to_vlckeys[i].i_vlckey;
750         }
751     }
752
753     return 0;
754 }