]> git.sesse.net Git - vlc/blob - modules/video_output/directx/events.c
6462ceda552396ddbfc04ee7c584f9545954b43f
[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.2 2002/10/01 20:43:35 ipkiss 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/vout.h>
36
37 #include "netutils.h"
38
39 #include <windows.h>
40 #include <windowsx.h>
41 #include <shellapi.h>
42
43 #include <ddraw.h>
44
45 #include "vout.h"
46
47 /*****************************************************************************
48  * Local prototypes.
49  *****************************************************************************/
50 static int  DirectXCreateWindow( vout_thread_t *p_vout );
51 static void DirectXCloseWindow ( vout_thread_t *p_vout );
52 static void DirectXUpdateRects( 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
69     /* Initialisation */
70
71     /* Create a window for the video */
72     /* Creating a window under Windows also initializes the thread's event
73      * message qeue */
74     if( DirectXCreateWindow( p_event->p_vout ) )
75     {
76         msg_Err( p_event, "out of memory" );
77         p_event->b_dead = 1;
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             if( (abs(GET_X_LPARAM(msg.lParam) - old_mouse_pos.x) > 2 ||
98                 (abs(GET_Y_LPARAM(msg.lParam) - old_mouse_pos.y)) > 2 ) )
99             {
100                 GetCursorPos( &old_mouse_pos );
101                 p_event->p_vout->p_sys->i_lastmoved = mdate();
102
103                 if( p_event->p_vout->p_sys->b_cursor_hidden )
104                 {
105                     p_event->p_vout->p_sys->b_cursor_hidden = 0;
106                     ShowCursor( TRUE );
107                 }
108             }
109             break;
110
111         case WM_VLC_HIDE_MOUSE:
112             GetCursorPos( &old_mouse_pos );
113             ShowCursor( FALSE );
114             break;
115
116         case WM_RBUTTONUP:
117             {
118                 intf_thread_t *p_intf;
119                 p_intf = vlc_object_find( p_event, VLC_OBJECT_INTF,
120                                                    FIND_ANYWHERE );
121                 if( p_intf )
122                 {
123                     p_intf->b_menu_change = 1;
124                     vlc_object_release( p_intf );
125                 }
126             }
127             break;
128
129         case WM_LBUTTONDOWN:
130             break;
131
132         case WM_LBUTTONDBLCLK:
133             p_event->p_vout->p_sys->i_changes |= VOUT_FULLSCREEN_CHANGE;
134             break;
135
136         case WM_KEYDOWN:
137             /* the key events are first processed here. The next
138              * message processed by this main message loop will be the
139              * char translation of the key event */
140             msg_Dbg( p_event, "WM_KEYDOWN" );
141             switch( msg.wParam )
142             {
143             case VK_ESCAPE:
144                 /* exit application */
145                 p_event->p_vlc->b_die = 1;
146                 break;
147
148             case VK_F1: network_ChannelJoin( p_event, 1 ); break;
149             case VK_F2: network_ChannelJoin( p_event, 2 ); break;
150             case VK_F3: network_ChannelJoin( p_event, 3 ); break;
151             case VK_F4: network_ChannelJoin( p_event, 4 ); break;
152             case VK_F5: network_ChannelJoin( p_event, 5 ); break;
153             case VK_F6: network_ChannelJoin( p_event, 6 ); break;
154             case VK_F7: network_ChannelJoin( p_event, 7 ); break;
155             case VK_F8: network_ChannelJoin( p_event, 8 ); break;
156             case VK_F9: network_ChannelJoin( p_event, 9 ); break;
157             case VK_F10: network_ChannelJoin( p_event, 10 ); break;
158             case VK_F11: network_ChannelJoin( p_event, 11 ); break;
159             case VK_F12: network_ChannelJoin( p_event, 12 ); break;
160             }
161             TranslateMessage(&msg);
162             break;
163
164         case WM_CHAR:
165             switch( msg.wParam )
166             {
167             case 'q':
168             case 'Q':
169                 /* exit application */
170                 p_event->p_vlc->b_die = 1;
171                 break;
172
173             case 'f':                            /* switch to fullscreen */
174             case 'F':
175                 p_event->p_vout->p_sys->i_changes |= VOUT_FULLSCREEN_CHANGE;
176                 break;
177
178             case 'c':                                /* toggle grayscale */
179             case 'C':
180                 p_event->p_vout->b_grayscale = ! p_event->p_vout->b_grayscale;
181                 p_event->p_vout->p_sys->i_changes |= VOUT_GRAYSCALE_CHANGE;
182                 break;
183
184             case 'i':                                     /* toggle info */
185             case 'I':
186                 p_event->p_vout->b_info = ! p_event->p_vout->b_info;
187                 p_event->p_vout->p_sys->i_changes |= VOUT_INFO_CHANGE;
188                 break;
189
190             case 's':                                  /* toggle scaling */
191             case 'S':
192                 p_event->p_vout->b_scale = ! p_event->p_vout->b_scale;
193                 p_event->p_vout->p_sys->i_changes |= VOUT_SCALE_CHANGE;
194                 break;
195
196             case ' ':                                /* toggle interface */
197                 p_event->p_vout->b_interface = ! p_event->p_vout->b_interface;
198                 p_event->p_vout->p_sys->i_changes |= VOUT_INTF_CHANGE;
199                 break;
200
201             default:
202                 break;
203             }
204
205         default:
206             /* Messages we don't handle directly are dispatched to the
207              * window procedure */
208             TranslateMessage(&msg);
209             DispatchMessage(&msg);
210             break;
211
212         } /* End Switch */
213
214     } /* End Main loop */
215
216     if( msg.message == WM_QUIT )
217     {
218         msg_Warn( p_event, "WM_QUIT... should not happen!!" );
219         p_event->p_vout->p_sys->hwnd = NULL; /* Window already destroyed */
220     }
221
222     msg_Dbg( p_event, "DirectXEventThread Terminating" );
223
224     /* clear the changes formerly signaled */
225     p_event->p_vout->p_sys->i_changes = 0;
226
227     DirectXCloseWindow( p_event->p_vout );
228 }
229
230
231 /* following functions are local */
232
233 /*****************************************************************************
234  * DirectXCreateWindow: create a window for the video.
235  *****************************************************************************
236  * Before creating a direct draw surface, we need to create a window in which
237  * the video will be displayed. This window will also allow us to capture the
238  * events.
239  *****************************************************************************/
240 static int DirectXCreateWindow( vout_thread_t *p_vout )
241 {
242     HINSTANCE  hInstance;
243     WNDCLASSEX wc;                                /* window class components */
244     RECT       rect_window;
245     COLORREF   colorkey; 
246     HDC        hdc;
247     HMENU      hMenu;
248     HICON      vlc_icon = NULL;
249     char       vlc_path[_MAX_PATH+1];
250
251     msg_Dbg( p_vout, "DirectXCreateWindow" );
252
253     /* get this module's instance */
254     hInstance = GetModuleHandle(NULL);
255
256     /* Create a BRUSH that will be used by Windows to paint the window
257      * background.
258      * This window background is important for us as it will be used by the
259      * graphics card to display the overlay.
260      * This is why we carefully choose the color for this background, the goal
261      * being to choose a color which isn't complete black but nearly. We
262      * obviously don't want to use black as a colorkey for the overlay because
263      * black is one of the most used color and thus would give us undesirable
264      * effects */
265     /* the first step is to find the colorkey we want to use. The difficulty
266      * comes from the potential dithering (depends on the display depth)
267      * because we need to know the real RGB value of the chosen colorkey */
268     hdc = GetDC( NULL );
269     for( colorkey = 5; colorkey < 0xFF /*all shades of red*/; colorkey++ )
270     {
271         if( colorkey == GetNearestColor( hdc, colorkey ) )
272           break;
273     }
274     msg_Dbg( p_vout, "background color: %i", colorkey );
275
276     /* create the actual brush */  
277     p_vout->p_sys->hbrush = CreateSolidBrush(colorkey);
278     p_vout->p_sys->i_rgb_colorkey = (int)colorkey;
279
280     /* Get the current size of the display and its colour depth */
281     p_vout->p_sys->rect_display.right = GetDeviceCaps( hdc, HORZRES );
282     p_vout->p_sys->rect_display.bottom = GetDeviceCaps( hdc, VERTRES );
283     p_vout->p_sys->i_display_depth = GetDeviceCaps( hdc, BITSPIXEL );
284     msg_Dbg( p_vout, "screen dimensions %ix%i colour depth %i",
285                       p_vout->p_sys->rect_display.right,
286                       p_vout->p_sys->rect_display.bottom,
287                       p_vout->p_sys->i_display_depth );
288
289     ReleaseDC( NULL, hdc );
290
291     /* Get the Icon from the main app */
292     vlc_icon = NULL;
293     if( GetModuleFileName( NULL, vlc_path, _MAX_PATH ) )
294     {
295         vlc_icon = ExtractIcon( hInstance, vlc_path, 0 );
296     }
297
298
299     /* fill in the window class structure */
300     wc.cbSize        = sizeof(WNDCLASSEX);
301     wc.style         = CS_DBLCLKS;                       /* style: dbl click */
302     wc.lpfnWndProc   = (WNDPROC)DirectXEventProc;           /* event handler */
303     wc.cbClsExtra    = 0;                             /* no extra class data */
304     wc.cbWndExtra    = 0;                            /* no extra window data */
305     wc.hInstance     = hInstance;                                /* instance */
306     wc.hIcon         = vlc_icon;                        /* load the vlc icon */
307     wc.hCursor       = LoadCursor(NULL, IDC_ARROW); /* load a default cursor */
308     wc.hbrBackground = p_vout->p_sys->hbrush;            /* background color */
309     wc.lpszMenuName  = NULL;                                      /* no menu */
310     wc.lpszClassName = "VLC DirectX";                 /* use a special class */
311     wc.hIconSm       = vlc_icon;                        /* load the vlc icon */
312
313     /* register the window class */
314     if (!RegisterClassEx(&wc))
315     {
316         WNDCLASS wndclass;
317
318         /* free window background brush */
319         if( p_vout->p_sys->hbrush )
320         {
321             DeleteObject( p_vout->p_sys->hbrush );
322             p_vout->p_sys->hbrush = NULL;
323         }
324
325         if( vlc_icon )
326             DestroyIcon( vlc_icon );
327
328         /* Check why it failed. If it's because one already exists then fine */
329         if( !GetClassInfo( hInstance, "VLC DirectX", &wndclass ) )
330         {
331             msg_Err( p_vout, "DirectXCreateWindow RegisterClass FAILED" );
332             return (1);
333         }
334     }
335
336     /* when you create a window you give the dimensions you wish it to have.
337      * Unfortunatly these dimensions will include the borders and title bar.
338      * We use the following function to find out the size of the window
339      * corresponding to the useable surface we want */
340     rect_window.top    = 10;
341     rect_window.left   = 10;
342     rect_window.right  = rect_window.left + p_vout->p_sys->i_window_width;
343     rect_window.bottom = rect_window.top + p_vout->p_sys->i_window_height;
344     AdjustWindowRect( &rect_window, WS_OVERLAPPEDWINDOW|WS_SIZEBOX, 0 );
345
346     /* create the window */
347     p_vout->p_sys->hwnd = CreateWindow("VLC DirectX",/* name of window class */
348                     VOUT_TITLE " (DirectX Output)", /* window title bar text */
349                     WS_OVERLAPPEDWINDOW
350                     | WS_SIZEBOX,               /* window style */
351                     CW_USEDEFAULT,                   /* default X coordinate */
352                     0,                               /* default Y coordinate */
353                     rect_window.right - rect_window.left,    /* window width */
354                     rect_window.bottom - rect_window.top,   /* window height */
355                     NULL,                                /* no parent window */
356                     NULL,                          /* no menu in this window */
357                     hInstance,            /* handle of this program instance */
358                     NULL);                        /* no additional arguments */
359
360     if (p_vout->p_sys->hwnd == NULL) {
361         msg_Warn( p_vout, "DirectXCreateWindow create window FAILED" );
362         return (1);
363     }
364
365     /* store a p_vout pointer into the window local storage (for later use
366      * in DirectXEventProc).
367      * We need to use SetWindowLongPtr when it is available in mingw */
368     SetWindowLong( p_vout->p_sys->hwnd, GWL_USERDATA, (LONG)p_vout );
369
370     /* append a "Always On Top" entry in the system menu */
371     hMenu = GetSystemMenu( p_vout->p_sys->hwnd, FALSE );
372     AppendMenu( hMenu, MF_SEPARATOR, 0, "" );
373     AppendMenu( hMenu, MF_STRING | MF_UNCHECKED, IDM_TOGGLE_ON_TOP, "Always on &Top");
374
375     /* now display the window */
376     ShowWindow(p_vout->p_sys->hwnd, SW_SHOW);
377
378     return ( 0 );
379 }
380
381 /*****************************************************************************
382  * DirectXCloseWindow: close the window created by DirectXCreateWindow
383  *****************************************************************************
384  * This function returns all resources allocated by DirectXCreateWindow.
385  *****************************************************************************/
386 static void DirectXCloseWindow( vout_thread_t *p_vout )
387 {
388     msg_Dbg( p_vout, "DirectXCloseWindow" );
389
390     if( p_vout->p_sys->hwnd != NULL )
391     {
392         DestroyWindow( p_vout->p_sys->hwnd );
393         p_vout->p_sys->hwnd = NULL;
394     }
395
396     /* We don't unregister the Window Class because it could lead to race
397      * conditions and it will be done anyway by the system when the app will
398      * exit */
399 }
400
401 /*****************************************************************************
402  * DirectXUpdateRects: 
403  *****************************************************************************
404  * This function is called when the window position and size is changed, and
405  * its job is to update the source and destination RECTs used to display the
406  * picture.
407  *****************************************************************************/
408 static void DirectXUpdateRects( vout_thread_t *p_vout )
409 {
410     int i_width, i_height, i_x, i_y;
411
412 #define rect_src p_vout->p_sys->rect_src
413 #define rect_src_clipped p_vout->p_sys->rect_src_clipped
414 #define rect_dest p_vout->p_sys->rect_dest
415 #define rect_dest_clipped p_vout->p_sys->rect_dest_clipped
416 #define rect_display p_vout->p_sys->rect_display
417
418     vout_PlacePicture( p_vout, p_vout->p_sys->i_window_width,
419                        p_vout->p_sys->i_window_height,
420                        &i_x, &i_y, &i_width, &i_height );
421
422     /* Destination image position and dimensions */
423     rect_dest.left = i_x + p_vout->p_sys->i_window_x;
424     rect_dest.top = i_y + p_vout->p_sys->i_window_y;
425     rect_dest.right = rect_dest.left + i_width;
426     rect_dest.bottom = rect_dest.top + i_height;
427
428
429     /* UpdateOverlay directdraw function doesn't automatically clip to the
430      * display size so we need to do it otherwise it will fails */
431
432     /* Clip the destination window */
433     IntersectRect( &rect_dest_clipped, &rect_dest, &rect_display );
434
435 #if 0
436     msg_Dbg( p_vout, "DirectXUpdateRects image_dst_clipped coords:"
437                      " %i,%i,%i,%i",
438                      rect_dest_clipped.left, rect_dest_clipped.top,
439                      rect_dest_clipped.right, rect_dest_clipped.bottom );
440 #endif
441
442     /* the 2 following lines are to fix a bug when clicking on the desktop */
443     if( (rect_dest_clipped.right - rect_dest_clipped.left)==0 ||
444         (rect_dest_clipped.bottom - rect_dest_clipped.top)==0 )
445     {
446         SetRectEmpty( &rect_src_clipped );
447         return;
448     }
449
450     /* src image dimensions */
451     rect_src.left = 0;
452     rect_src.top = 0;
453     rect_src.right = p_vout->render.i_width;
454     rect_src.bottom = p_vout->render.i_height;
455
456     /* Clip the source image */
457     rect_src_clipped.left = (rect_dest_clipped.left - rect_dest.left) *
458       p_vout->render.i_width / (rect_dest.right - rect_dest.left);
459     rect_src_clipped.right = p_vout->render.i_width - 
460       (rect_dest.right - rect_dest_clipped.right) * p_vout->render.i_width /
461       (rect_dest.right - rect_dest.left);
462     rect_src_clipped.top = (rect_dest_clipped.top - rect_dest.top) *
463       p_vout->render.i_height / (rect_dest.bottom - rect_dest.top);
464     rect_src_clipped.bottom = p_vout->render.i_height -
465       (rect_dest.bottom - rect_dest_clipped.bottom) * p_vout->render.i_height /
466       (rect_dest.bottom - rect_dest.top);
467
468 #if 0
469     msg_Dbg( p_vout, "DirectXUpdateRects image_src_clipped"
470                      " coords: %i,%i,%i,%i",
471                      rect_src_clipped.left, rect_src_clipped.top,
472                      rect_src_clipped.right, rect_src_clipped.bottom );
473 #endif
474
475 #undef rect_src
476 #undef rect_src_clipped
477 #undef rect_dest
478 #undef rect_dest_clipped
479 #undef rect_display
480 }
481
482 /*****************************************************************************
483  * DirectXEventProc: This is the window event processing function.
484  *****************************************************************************
485  * On Windows, when you create a window you have to attach an event processing
486  * function to it. The aim of this function is to manage "Queued Messages" and
487  * "Nonqueued Messages".
488  * Queued Messages are those picked up and retransmitted by vout_Manage
489  * (using the GetMessage and DispatchMessage functions).
490  * Nonqueued Messages are those that Windows will send directly to this
491  * procedure (like WM_DESTROY, WM_WINDOWPOSCHANGED...)
492  *****************************************************************************/
493 static long FAR PASCAL DirectXEventProc( HWND hwnd, UINT message,
494                                          WPARAM wParam, LPARAM lParam )
495 {
496     vout_thread_t *p_vout =
497             (vout_thread_t *)GetWindowLong( hwnd, GWL_USERDATA );
498
499     switch( message )
500     {
501
502     case WM_WINDOWPOSCHANGED:
503         {
504         RECT     rect_window;
505         POINT    point_window;
506
507         /* update the window position */
508         point_window.x = 0;
509         point_window.y = 0;
510         ClientToScreen( hwnd, &point_window );
511         p_vout->p_sys->i_window_x = point_window.x;
512         p_vout->p_sys->i_window_y = point_window.y;
513
514         /* update the window size */
515         GetClientRect( hwnd, &rect_window );
516         p_vout->p_sys->i_window_width = rect_window.right;
517         p_vout->p_sys->i_window_height = rect_window.bottom;
518
519         DirectXUpdateRects( p_vout );
520         if( p_vout->p_sys->b_using_overlay &&
521             !p_vout->p_sys->p_event->b_die )
522             DirectXUpdateOverlay( p_vout );
523
524         /* signal the size change */
525         if( !p_vout->p_sys->b_using_overlay &&
526             !p_vout->p_sys->p_event->b_die )
527             p_vout->p_sys->i_changes |= VOUT_SIZE_CHANGE;
528
529         return 0;
530         }
531         break;
532
533     /* the user wants to close the window */
534     case WM_CLOSE:
535         msg_Dbg( p_vout, "WinProc WM_CLOSE" );
536         /* exit application */
537         p_vout->p_vlc->b_die = 1;
538         return 0;
539         break;
540
541     /* the window has been closed so shut down everything now */
542     case WM_DESTROY:
543         msg_Dbg( p_vout, "WinProc WM_DESTROY" );
544         /* just destroy the window */
545         PostQuitMessage( 0 );
546         return 0;
547         break;
548
549     case WM_SYSCOMMAND:
550         switch (wParam)
551         {
552             case SC_SCREENSAVE:                     /* catch the screensaver */
553             case SC_MONITORPOWER:              /* catch the monitor turn-off */
554                 msg_Dbg( p_vout, "WinProc WM_SYSCOMMAND" );
555                 return 0;                  /* this stops them from happening */
556             case IDM_TOGGLE_ON_TOP:            /* toggle the "on top" status */
557             {
558                 HMENU hMenu = GetSystemMenu( hwnd , FALSE );
559
560                 msg_Dbg( p_vout, "WinProc WM_SYSCOMMAND: IDM_TOGGLE_ON_TOP");
561
562                 // Check if the window is already on top
563                 if( GetWindowLong( hwnd, GWL_EXSTYLE ) & WS_EX_TOPMOST )
564                 {
565                     CheckMenuItem( hMenu, IDM_TOGGLE_ON_TOP,
566                                    MF_BYCOMMAND | MFS_UNCHECKED );
567                     SetWindowPos( hwnd, HWND_NOTOPMOST,
568                                   0, 0, 0, 0,
569                                   SWP_NOSIZE | SWP_NOMOVE );
570                 }
571                 else
572                 {
573                     CheckMenuItem( hMenu, IDM_TOGGLE_ON_TOP,
574                                    MF_BYCOMMAND | MFS_CHECKED );
575                     SetWindowPos( hwnd, HWND_TOPMOST,
576                                   0, 0, 0, 0,
577                                   SWP_NOSIZE | SWP_NOMOVE );
578                 }
579                 return 0;
580                 break;
581             }
582         }
583         break;
584
585     case WM_ERASEBKGND:
586         if( !p_vout->p_sys->b_using_overlay )
587         {
588             /* We want to eliminate unnecessary background redraws which create
589              * an annoying flickering */
590             int i_width, i_height, i_x, i_y;
591             RECT rect_temp;
592             GetClipBox( (HDC)wParam, &rect_temp );
593 #if 0
594             msg_Dbg( p_vout, "WinProc WM_ERASEBKGND %i,%i,%i,%i",
595                           rect_temp.left, rect_temp.top,
596                           rect_temp.right, rect_temp.bottom );
597 #endif
598             vout_PlacePicture( p_vout, p_vout->p_sys->i_window_width,
599                                p_vout->p_sys->i_window_height,
600                                &i_x, &i_y, &i_width, &i_height );
601             ExcludeClipRect( (HDC)wParam, i_x, i_y,
602                              i_x + i_width, i_y + i_height );
603         }
604         break;
605
606     default:
607         //msg_Dbg( p_vout, "WinProc WM Default %i", message );
608         break;
609     }
610
611     return DefWindowProc(hwnd, message, wParam, lParam);
612 }