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