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