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