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