]> git.sesse.net Git - vlc/blob - modules/video_output/directx/events.c
56570f67936485b5396534c828b3478851e79a40
[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.25 2003/10/28 17:02:14 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                 if( val.i_int == config_GetInt( p_event, "fullscreen-key" ) )
224                 {
225                     p_event->p_vout->p_sys->i_changes |=
226                         VOUT_FULLSCREEN_CHANGE;
227                 }
228                 else
229                 {
230                     var_Set( p_event->p_vlc, "key-pressed", val );
231                 }
232             }
233             break;
234
235         default:
236             /* Messages we don't handle directly are dispatched to the
237              * window procedure */
238             TranslateMessage(&msg);
239             DispatchMessage(&msg);
240             break;
241
242         } /* End Switch */
243
244     } /* End Main loop */
245
246     if( msg.message == WM_QUIT )
247     {
248         msg_Warn( p_event, "WM_QUIT... should not happen!!" );
249         p_event->p_vout->p_sys->hwnd = NULL; /* Window already destroyed */
250     }
251
252     msg_Dbg( p_event, "DirectXEventThread terminating" );
253
254     /* clear the changes formerly signaled */
255     p_event->p_vout->p_sys->i_changes = 0;
256
257     DirectXCloseWindow( p_event->p_vout );
258 }
259
260
261 /* following functions are local */
262
263 /*****************************************************************************
264  * DirectXCreateWindow: create a window for the video.
265  *****************************************************************************
266  * Before creating a direct draw surface, we need to create a window in which
267  * the video will be displayed. This window will also allow us to capture the
268  * events.
269  *****************************************************************************/
270 static int DirectXCreateWindow( vout_thread_t *p_vout )
271 {
272     HINSTANCE  hInstance;
273     COLORREF   colorkey;
274     HDC        hdc;
275     HMENU      hMenu;
276     RECT       rect_window;
277
278     vlc_value_t val;
279
280     msg_Dbg( p_vout, "DirectXCreateWindow" );
281
282     /* Get this module's instance */
283     hInstance = GetModuleHandle(NULL);
284
285     /* Create a BRUSH that will be used by Windows to paint the window
286      * background.
287      * This window background is important for us as it will be used by the
288      * graphics card to display the overlay.
289      * This is why we carefully choose the color for this background, the goal
290      * being to choose a color which isn't complete black but nearly. We
291      * obviously don't want to use black as a colorkey for the overlay because
292      * black is one of the most used color and thus would give us undesirable
293      * effects */
294     /* the first step is to find the colorkey we want to use. The difficulty
295      * comes from the potential dithering (depends on the display depth)
296      * because we need to know the real RGB value of the chosen colorkey */
297     hdc = GetDC( NULL );
298     for( colorkey = 0x0a; colorkey < 0xff /* all shades of red */; colorkey++ )
299     {
300         if( colorkey == GetNearestColor( hdc, colorkey ) )
301         {
302             break;
303         }
304     }
305     msg_Dbg( p_vout, "background color: %i", colorkey );
306
307     /* Create the actual brush */
308     p_vout->p_sys->hbrush = CreateSolidBrush(colorkey);
309     p_vout->p_sys->i_rgb_colorkey = (int)colorkey;
310
311     /* Get the current size of the display and its colour depth */
312     p_vout->p_sys->rect_display.right = GetDeviceCaps( hdc, HORZRES );
313     p_vout->p_sys->rect_display.bottom = GetDeviceCaps( hdc, VERTRES );
314     p_vout->p_sys->i_display_depth = GetDeviceCaps( hdc, BITSPIXEL );
315     msg_Dbg( p_vout, "screen dimensions %ix%i colour depth %i",
316                       p_vout->p_sys->rect_display.right,
317                       p_vout->p_sys->rect_display.bottom,
318                       p_vout->p_sys->i_display_depth );
319
320     ReleaseDC( NULL, hdc );
321
322     /* If an external window was specified, we'll draw in it. */
323     var_Get( p_vout->p_vlc, "drawable", &val );
324     p_vout->p_sys->hparent = p_vout->p_sys->hwnd =
325              val.i_int ?  (void*)(ptrdiff_t) val.i_int : NULL;
326
327     if( p_vout->p_sys->hparent )
328     {
329         msg_Dbg( p_vout, "using external window %p\n", p_vout->p_sys->hwnd );
330
331         /* Set stuff in the window that we can not put directly in
332          * a class (see below). */
333         SetClassLong( p_vout->p_sys->hwnd,
334                       GCL_STYLE, CS_DBLCLKS );
335         SetClassLong( p_vout->p_sys->hwnd,
336                       GCL_HBRBACKGROUND, (LONG)p_vout->p_sys->hbrush );
337         SetClassLong( p_vout->p_sys->hwnd,
338                       GCL_HCURSOR, (LONG)LoadCursor(NULL, IDC_ARROW) );
339         /* Store a p_vout pointer into the window local storage (for later
340          * use in DirectXEventProc). */
341         SetWindowLong( p_vout->p_sys->hwnd, GWL_USERDATA, (LONG)p_vout );
342
343         p_vout->p_sys->pf_wndproc =
344                (WNDPROC)SetWindowLong( p_vout->p_sys->hwnd,
345                                        GWL_WNDPROC, (LONG)DirectXEventProc );
346
347         /* Blam! Erase everything that might have been there. */
348         RedrawWindow( p_vout->p_sys->hwnd, NULL, NULL,
349                       RDW_INVALIDATE | RDW_ERASE );
350     }
351     else
352     {
353         WNDCLASSEX wc;                            /* window class components */
354         HICON      vlc_icon = NULL;
355         char       vlc_path[MAX_PATH+1];
356
357         /* Get the Icon from the main app */
358         vlc_icon = NULL;
359         if( GetModuleFileName( NULL, vlc_path, MAX_PATH ) )
360         {
361             vlc_icon = ExtractIcon( hInstance, vlc_path, 0 );
362         }
363
364         /* Fill in the window class structure */
365         wc.cbSize        = sizeof(WNDCLASSEX);
366         wc.style         = CS_DBLCLKS;                   /* style: dbl click */
367         wc.lpfnWndProc   = (WNDPROC)DirectXEventProc;       /* event handler */
368         wc.cbClsExtra    = 0;                         /* no extra class data */
369         wc.cbWndExtra    = 0;                        /* no extra window data */
370         wc.hInstance     = hInstance;                            /* instance */
371         wc.hIcon         = vlc_icon;                /* load the vlc big icon */
372         wc.hCursor       = LoadCursor(NULL, IDC_ARROW);    /* default cursor */
373         wc.hbrBackground = p_vout->p_sys->hbrush;        /* background color */
374         wc.lpszMenuName  = NULL;                                  /* no menu */
375         wc.lpszClassName = "VLC DirectX";             /* use a special class */
376         wc.hIconSm       = vlc_icon;              /* load the vlc small icon */
377
378         /* Register the window class */
379         if( !RegisterClassEx(&wc) )
380         {
381             WNDCLASS wndclass;
382
383             /* Free window background brush */
384             if( p_vout->p_sys->hbrush )
385             {
386                 DeleteObject( p_vout->p_sys->hbrush );
387                 p_vout->p_sys->hbrush = NULL;
388             }
389
390             if( vlc_icon )
391             {
392                 DestroyIcon( vlc_icon );
393             }
394
395             /* Check why it failed. If it's because one already exists
396              * then fine, otherwise return with an error. */
397             if( !GetClassInfo( hInstance, "VLC DirectX", &wndclass ) )
398             {
399                 msg_Err( p_vout, "DirectXCreateWindow RegisterClass FAILED" );
400                 return VLC_EGENERIC;
401             }
402         }
403
404         /* When you create a window you give the dimensions you wish it to
405          * have. Unfortunatly these dimensions will include the borders and
406          * titlebar. We use the following function to find out the size of
407          * the window corresponding to the useable surface we want */
408         rect_window.top    = 10;
409         rect_window.left   = 10;
410         rect_window.right  = rect_window.left + p_vout->p_sys->i_window_width;
411         rect_window.bottom = rect_window.top + p_vout->p_sys->i_window_height;
412         AdjustWindowRect( &rect_window, WS_OVERLAPPEDWINDOW|WS_SIZEBOX, 0 );
413
414         /* Create the window */
415         p_vout->p_sys->hwnd =
416             CreateWindow( "VLC DirectX",             /* name of window class */
417                     VOUT_TITLE " (DirectX Output)", /* window title bar text */
418                     WS_OVERLAPPEDWINDOW | WS_SIZEBOX,        /* window style */
419                     CW_USEDEFAULT,                   /* default X coordinate */
420                     0,                               /* default Y coordinate */
421                     rect_window.right - rect_window.left,    /* window width */
422                     rect_window.bottom - rect_window.top,   /* window height */
423                     NULL,                                /* no parent window */
424                     NULL,                          /* no menu in this window */
425                     hInstance,            /* handle of this program instance */
426                     (LPVOID)p_vout );            /* send p_vout to WM_CREATE */
427
428         if( !p_vout->p_sys->hwnd )
429         {
430             msg_Warn( p_vout, "DirectXCreateWindow create window FAILED" );
431             return VLC_EGENERIC;
432         }
433     }
434
435     /* Append a "Always On Top" entry in the system menu */
436     hMenu = GetSystemMenu( p_vout->p_sys->hwnd, FALSE );
437     AppendMenu( hMenu, MF_SEPARATOR, 0, "" );
438     AppendMenu( hMenu, MF_STRING | MF_UNCHECKED,
439                        IDM_TOGGLE_ON_TOP, "Always on &Top" );
440
441     /* Now display the window */
442     ShowWindow( p_vout->p_sys->hwnd, SW_SHOW );
443
444     return VLC_SUCCESS;
445 }
446
447 /*****************************************************************************
448  * DirectXCloseWindow: close the window created by DirectXCreateWindow
449  *****************************************************************************
450  * This function returns all resources allocated by DirectXCreateWindow.
451  *****************************************************************************/
452 static void DirectXCloseWindow( vout_thread_t *p_vout )
453 {
454     msg_Dbg( p_vout, "DirectXCloseWindow" );
455
456     if( p_vout->p_sys->hwnd && !p_vout->p_sys->hparent )
457     {
458         DestroyWindow( p_vout->p_sys->hwnd );
459     }
460     else if( p_vout->p_sys->hparent )
461     {
462         /* We don't want our windowproc to be called anymore */
463         SetWindowLong( p_vout->p_sys->hwnd,
464                        GWL_WNDPROC, (LONG)p_vout->p_sys->pf_wndproc );
465
466         /* Blam! Erase everything that might have been there. */
467         InvalidateRect( p_vout->p_sys->hwnd, NULL, TRUE );
468     }
469
470     p_vout->p_sys->hwnd = NULL;
471
472     /* We don't unregister the Window Class because it could lead to race
473      * conditions and it will be done anyway by the system when the app will
474      * exit */
475 }
476
477 /*****************************************************************************
478  * DirectXUpdateRects: update clipping rectangles
479  *****************************************************************************
480  * This function is called when the window position or size are changed, and
481  * its job is to update the source and destination RECTs used to display the
482  * picture.
483  *****************************************************************************/
484 void DirectXUpdateRects( vout_thread_t *p_vout, vlc_bool_t b_force )
485 {
486 #define rect_src p_vout->p_sys->rect_src
487 #define rect_src_clipped p_vout->p_sys->rect_src_clipped
488 #define rect_dest p_vout->p_sys->rect_dest
489 #define rect_dest_clipped p_vout->p_sys->rect_dest_clipped
490
491     int i_width, i_height, i_x, i_y;
492
493     RECT  rect;
494     POINT point;
495
496     /* Retrieve the window size */
497     GetClientRect( p_vout->p_sys->hwnd, &rect );
498
499     /* Retrieve the window position */
500     point.x = point.y = 0;
501     ClientToScreen( p_vout->p_sys->hwnd, &point );
502
503     /* If nothing changed, we can return */
504     if( !b_force
505          && p_vout->p_sys->i_window_width == rect.right
506          && p_vout->p_sys->i_window_height == rect.bottom
507          && p_vout->p_sys->i_window_x == point.x
508          && p_vout->p_sys->i_window_y == point.y )
509     {
510         return;
511     }
512
513     /* Update the window position and size */
514     p_vout->p_sys->i_window_x = point.x;
515     p_vout->p_sys->i_window_y = point.y;
516     p_vout->p_sys->i_window_width = rect.right;
517     p_vout->p_sys->i_window_height = rect.bottom;
518
519     vout_PlacePicture( p_vout, rect.right, rect.bottom,
520                        &i_x, &i_y, &i_width, &i_height );
521
522     /* Destination image position and dimensions */
523     rect_dest.left = point.x + i_x;
524     rect_dest.right = rect_dest.left + i_width;
525     rect_dest.top = point.y + i_y;
526     rect_dest.bottom = rect_dest.top + i_height;
527
528
529     /* UpdateOverlay directdraw function doesn't automatically clip to the
530      * display size so we need to do it otherwise it will fail */
531
532     /* Clip the destination window */
533     IntersectRect( &rect_dest_clipped,
534                    &rect_dest,
535                    &p_vout->p_sys->rect_display );
536
537 #if 0
538     msg_Dbg( p_vout, "DirectXUpdateRects image_dst_clipped coords:"
539                      " %i,%i,%i,%i",
540                      rect_dest_clipped.left, rect_dest_clipped.top,
541                      rect_dest_clipped.right, rect_dest_clipped.bottom );
542 #endif
543
544     /* the 2 following lines are to fix a bug when clicking on the desktop */
545     if( (rect_dest_clipped.right - rect_dest_clipped.left)==0 ||
546         (rect_dest_clipped.bottom - rect_dest_clipped.top)==0 )
547     {
548         SetRectEmpty( &rect_src_clipped );
549         return;
550     }
551
552     /* src image dimensions */
553     rect_src.left = 0;
554     rect_src.top = 0;
555     rect_src.right = p_vout->render.i_width;
556     rect_src.bottom = p_vout->render.i_height;
557
558     /* Clip the source image */
559     rect_src_clipped.left = (rect_dest_clipped.left - rect_dest.left) *
560       p_vout->render.i_width / (rect_dest.right - rect_dest.left);
561     rect_src_clipped.right = p_vout->render.i_width -
562       (rect_dest.right - rect_dest_clipped.right) * p_vout->render.i_width /
563       (rect_dest.right - rect_dest.left);
564     rect_src_clipped.top = (rect_dest_clipped.top - rect_dest.top) *
565       p_vout->render.i_height / (rect_dest.bottom - rect_dest.top);
566     rect_src_clipped.bottom = p_vout->render.i_height -
567       (rect_dest.bottom - rect_dest_clipped.bottom) * p_vout->render.i_height /
568       (rect_dest.bottom - rect_dest.top);
569
570 #if 0
571     msg_Dbg( p_vout, "DirectXUpdateRects image_src_clipped"
572                      " coords: %i,%i,%i,%i",
573                      rect_src_clipped.left, rect_src_clipped.top,
574                      rect_src_clipped.right, rect_src_clipped.bottom );
575 #endif
576
577     /* Signal the size change */
578     if( !p_vout->p_sys->p_event->b_die )
579     {
580         if( p_vout->p_sys->b_using_overlay )
581         {
582             DirectXUpdateOverlay( p_vout );
583         }
584         else
585         {
586             p_vout->p_sys->i_changes |= VOUT_SIZE_CHANGE;
587         }
588     }
589
590 #undef rect_src
591 #undef rect_src_clipped
592 #undef rect_dest
593 #undef rect_dest_clipped
594 }
595
596 /*****************************************************************************
597  * DirectXEventProc: This is the window event processing function.
598  *****************************************************************************
599  * On Windows, when you create a window you have to attach an event processing
600  * function to it. The aim of this function is to manage "Queued Messages" and
601  * "Nonqueued Messages".
602  * Queued Messages are those picked up and retransmitted by vout_Manage
603  * (using the GetMessage and DispatchMessage functions).
604  * Nonqueued Messages are those that Windows will send directly to this
605  * procedure (like WM_DESTROY, WM_WINDOWPOSCHANGED...)
606  *****************************************************************************/
607 static long FAR PASCAL DirectXEventProc( HWND hwnd, UINT message,
608                                          WPARAM wParam, LPARAM lParam )
609 {
610     vout_thread_t *p_vout;
611
612     if( message == WM_CREATE )
613     {
614         /* Store p_vout for future use */
615         p_vout = (vout_thread_t *)((CREATESTRUCT *)lParam)->lpCreateParams;
616         SetWindowLong( hwnd, GWL_USERDATA, (LONG)p_vout );
617     }
618     else
619     {
620         p_vout = (vout_thread_t *)GetWindowLong( hwnd, GWL_USERDATA );
621     }
622
623     switch( message )
624     {
625
626     case WM_WINDOWPOSCHANGED:
627         DirectXUpdateRects( p_vout, VLC_TRUE );
628         return 0;
629
630     /* the user wants to close the window */
631     case WM_CLOSE:
632     {
633         playlist_t * p_playlist =
634             (playlist_t *)vlc_object_find( p_vout, VLC_OBJECT_PLAYLIST,
635                                            FIND_ANYWHERE );
636         if( p_playlist == NULL )
637         {
638             return 0;
639         }
640
641         playlist_Stop( p_playlist );
642         vlc_object_release( p_playlist );
643         return 0;
644     }
645
646     /* the window has been closed so shut down everything now */
647     case WM_DESTROY:
648         msg_Dbg( p_vout, "WinProc WM_DESTROY" );
649         /* just destroy the window */
650         PostQuitMessage( 0 );
651         return 0;
652
653     case WM_SYSCOMMAND:
654         switch (wParam)
655         {
656             case SC_SCREENSAVE:                     /* catch the screensaver */
657             case SC_MONITORPOWER:              /* catch the monitor turn-off */
658                 msg_Dbg( p_vout, "WinProc WM_SYSCOMMAND" );
659                 return 0;                  /* this stops them from happening */
660             case IDM_TOGGLE_ON_TOP:            /* toggle the "on top" status */
661             {
662                 vlc_value_t val;
663                 msg_Dbg( p_vout, "WinProc WM_SYSCOMMAND: IDM_TOGGLE_ON_TOP");
664
665                 /* Get the current value... */
666                 if( var_Get( p_vout, "directx-on-top", &val ) < 0 )
667                     return 0;
668                 /* ...and change it */
669                 val.b_bool = !val.b_bool;
670                 var_Set( p_vout, "directx-on-top", val );
671                 return 0;
672                 break;
673             }
674         }
675         break;
676
677     case WM_ERASEBKGND:
678         if( !p_vout->p_sys->b_using_overlay )
679         {
680             /* We want to eliminate unnecessary background redraws which create
681              * an annoying flickering */
682             int i_width, i_height, i_x, i_y;
683             RECT rect_temp;
684             GetClipBox( (HDC)wParam, &rect_temp );
685 #if 0
686             msg_Dbg( p_vout, "WinProc WM_ERASEBKGND %i,%i,%i,%i",
687                           rect_temp.left, rect_temp.top,
688                           rect_temp.right, rect_temp.bottom );
689 #endif
690             vout_PlacePicture( p_vout, p_vout->p_sys->i_window_width,
691                                p_vout->p_sys->i_window_height,
692                                &i_x, &i_y, &i_width, &i_height );
693             ExcludeClipRect( (HDC)wParam, i_x, i_y,
694                              i_x + i_width, i_y + i_height );
695         }
696         break;
697
698     default:
699         //msg_Dbg( p_vout, "WinProc WM Default %i", message );
700         break;
701     }
702
703     return DefWindowProc(hwnd, message, wParam, lParam);
704 }
705
706 static struct
707 {
708     int i_dxkey;
709     int i_vlckey;
710
711 } dxkeys_to_vlckeys[] =
712 {
713     { VK_F1, KEY_F1 }, { VK_F2, KEY_F2 }, { VK_F3, KEY_F3 }, { VK_F4, KEY_F4 },
714     { VK_F5, KEY_F5 }, { VK_F6, KEY_F6 }, { VK_F7, KEY_F7 }, { VK_F8, KEY_F8 },
715     { VK_F9, KEY_F9 }, { VK_F10, KEY_F10 }, { VK_F11, KEY_F11 },
716     { VK_F12, KEY_F12 },
717
718     { VK_RETURN, KEY_ENTER },
719     { VK_SPACE, KEY_SPACE },
720     { VK_ESCAPE, KEY_ESC },
721
722     { VK_MENU, KEY_MENU },
723     { VK_LEFT, KEY_LEFT },
724     { VK_RIGHT, KEY_RIGHT },
725     { VK_UP, KEY_UP },
726     { VK_DOWN, KEY_DOWN },
727
728     { VK_HOME, KEY_HOME },
729     { VK_END, KEY_END },
730     { VK_PRIOR, KEY_PAGEUP },
731     { VK_NEXT, KEY_PAGEDOWN },
732     { 0, 0 }
733 };
734
735 static int DirectXConvertKey( int i_key )
736 {
737     int i;
738
739     for( i = 0; dxkeys_to_vlckeys[i].i_dxkey != 0; i++ )
740     {
741         if( dxkeys_to_vlckeys[i].i_dxkey == i_key )
742         {
743             return dxkeys_to_vlckeys[i].i_vlckey;
744         }
745     }
746
747     return 0;
748 }