]> git.sesse.net Git - vlc/blob - plugins/directx/vout_events.c
* DirectX video output plugin now uses triple buffering for YUV overlay. This
[vlc] / plugins / directx / vout_events.c
1 /*****************************************************************************
2  * vout_events.c: Windows DirectX video output events handler
3  *****************************************************************************
4  * Copyright (C) 2001 VideoLAN
5  * $Id: vout_events.c,v 1.14 2002/04/23 22:07:05 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 <videolan/vlc.h>
34
35 #include "netutils.h"
36
37 #include "video.h"
38 #include "video_output.h"
39
40 #include <windows.h>
41 #include <windowsx.h>
42 #include <shellapi.h>
43
44 #include <ddraw.h>
45
46 #include "interface.h"
47
48 #include "vout_directx.h"
49
50 /*****************************************************************************
51  * Local prototypes.
52  *****************************************************************************/
53 static int  DirectXCreateWindow( vout_thread_t *p_vout );
54 static void DirectXCloseWindow ( vout_thread_t *p_vout );
55 static void DirectXUpdateRects( vout_thread_t *p_vout );
56 static long FAR PASCAL DirectXEventProc ( HWND hwnd, UINT message,
57                                           WPARAM wParam, LPARAM lParam );
58
59 /*****************************************************************************
60  * DirectXEventThread: Create video window & handle its messages
61  *****************************************************************************
62  * This function creates a video window and then enters an infinite loop
63  * that handles the messages sent to that window.
64  * The main goal of this thread is to isolate the Win32 PeekMessage function
65  * because this one can block for a long time.
66  *****************************************************************************/
67 void DirectXEventThread( vout_thread_t *p_vout )
68 {
69     MSG msg;
70     POINT old_mouse_pos;
71
72     /* Initialisation */
73
74     /* Create a window for the video */
75     /* Creating a window under Windows also initializes the thread's event
76      * message qeue */
77     vlc_mutex_lock( &p_vout->p_sys->event_thread_lock );
78     if( DirectXCreateWindow( p_vout ) )
79     {
80         intf_ErrMsg( "vout error: can't create window" );
81         p_vout->p_sys->i_event_thread_status = THREAD_FATAL;
82         p_vout->p_sys->b_event_thread_die = 1;
83     }
84     else p_vout->p_sys->i_event_thread_status = THREAD_READY;
85
86     /* signal the creation of the window */
87     vlc_cond_signal( &p_vout->p_sys->event_thread_wait );
88     vlc_mutex_unlock( &p_vout->p_sys->event_thread_lock );
89
90     /* Main loop */
91     /* GetMessage will sleep if there's no message in the queue */
92     while( !p_vout->p_sys->b_event_thread_die
93            && GetMessage( &msg, p_vout->p_sys->hwnd, 0, 0 ) )
94     {
95
96         /* Check if we are asked to exit */
97         if( p_vout->b_die || p_vout->p_sys->b_event_thread_die )
98             break;
99
100         switch( msg.message )
101         {
102
103         case WM_NCMOUSEMOVE:
104         case WM_MOUSEMOVE:
105             if( (abs(GET_X_LPARAM(msg.lParam) - old_mouse_pos.x) > 2 ||
106                 (abs(GET_Y_LPARAM(msg.lParam) - old_mouse_pos.y)) > 2 ) )
107             {
108                 GetCursorPos( &old_mouse_pos );
109                 p_vout->p_sys->i_lastmoved = mdate();
110
111                 if( p_vout->p_sys->b_cursor_hidden )
112                 {
113                     p_vout->p_sys->b_cursor_hidden = 0;
114                     ShowCursor( TRUE );
115                 }
116             }
117             break;
118
119         case WM_VLC_HIDE_MOUSE:
120             GetCursorPos( &old_mouse_pos );
121             ShowCursor( FALSE );
122             break;
123
124         case WM_RBUTTONUP:
125             p_main->p_intf->b_menu_change = 1;
126             break;
127
128         case WM_LBUTTONDOWN:
129             p_vout->p_sys->i_changes |= VOUT_FULLSCREEN_CHANGE;
130             break;
131
132         case WM_LBUTTONDBLCLK:
133             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             intf_WarnMsg( 3, "vout: vout_Manage WM_KEYDOWN" );
141             switch( msg.wParam )
142             {
143             case VK_ESCAPE:
144             case VK_F12:
145                 /* exit application */
146                 p_main->p_intf->b_die = 1;
147                 break;
148             }
149             TranslateMessage(&msg);
150             break;
151
152         case WM_CHAR:
153             switch( msg.wParam )
154             {
155             case 'q':
156             case 'Q':
157                 /* exit application */
158                 p_main->p_intf->b_die = 1;
159                 break;
160
161             case 'f':                            /* switch to fullscreen */
162             case 'F':
163                 p_vout->p_sys->i_changes |= VOUT_FULLSCREEN_CHANGE;
164                 break;
165
166             case 'c':                                /* toggle grayscale */
167             case 'C':
168                 p_vout->b_grayscale = ! p_vout->b_grayscale;
169                 p_vout->p_sys->i_changes |= VOUT_GRAYSCALE_CHANGE;
170                 break;
171
172             case 'i':                                     /* toggle info */
173             case 'I':
174                 p_vout->b_info = ! p_vout->b_info;
175                 p_vout->p_sys->i_changes |= VOUT_INFO_CHANGE;
176                 break;
177
178             case 's':                                  /* toggle scaling */
179             case 'S':
180                 p_vout->b_scale = ! p_vout->b_scale;
181                 p_vout->p_sys->i_changes |= VOUT_SCALE_CHANGE;
182                 break;
183
184             case ' ':                                /* toggle interface */
185                 p_vout->b_interface = ! p_vout->b_interface;
186                 p_vout->p_sys->i_changes |= VOUT_INTF_CHANGE;
187                 break;
188
189             case '0': network_ChannelJoin( 0 ); break;
190             case '1': network_ChannelJoin( 1 ); break;
191             case '2': network_ChannelJoin( 2 ); break;
192             case '3': network_ChannelJoin( 3 ); break;
193             case '4': network_ChannelJoin( 4 ); break;
194             case '5': network_ChannelJoin( 5 ); break;
195             case '6': network_ChannelJoin( 6 ); break;
196             case '7': network_ChannelJoin( 7 ); break;
197             case '8': network_ChannelJoin( 8 ); break;
198             case '9': network_ChannelJoin( 9 ); break;
199
200             default:
201                 break;
202             }
203
204         default:
205             /* Messages we don't handle directly are dispatched to the
206              * window procedure */
207             TranslateMessage(&msg);
208             DispatchMessage(&msg);
209             break;
210
211         } /* End Switch */
212
213     } /* End Main loop */
214
215     if( msg.message == WM_QUIT )
216     {
217         intf_WarnMsg( 3, "vout: DirectXEventThread WM_QUIT... "
218                       "shouldn't happen!!" );
219         p_vout->p_sys->hwnd = NULL; /* Window already destroyed */
220     }
221
222     intf_WarnMsg( 3, "vout: DirectXEventThread Terminating" );
223
224     /* clear the changes formerly signaled */
225     p_vout->p_sys->i_changes = 0;
226
227     DirectXCloseWindow( 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     HICON      vlc_icon = NULL;
248     char       vlc_path[_MAX_PATH+1];
249
250     intf_WarnMsg( 3, "vout: DirectXCreateWindow" );
251
252     /* get this module's instance */
253     hInstance = GetModuleHandle(NULL);
254
255     /* Create a BRUSH that will be used by Windows to paint the window
256      * background.
257      * This window background is important for us as it will be used by the
258      * graphics card to display the overlay.
259      * This is why we carefully choose the color for this background, the goal
260      * being to choose a color which isn't complete black but nearly. We
261      * obviously don't want to use black as a colorkey for the overlay because
262      * black is one of the most used color and thus would give us undesirable
263      * effects */
264     /* the first step is to find the colorkey we want to use. The difficulty
265      * comes from the potential dithering (depends on the display depth)
266      * because we need to know the real RGB value of the chosen colorkey */
267     hdc = GetDC( NULL );
268     for( colorkey = 5; colorkey < 0xFF /*all shades of red*/; colorkey++ )
269     {
270         if( colorkey == GetNearestColor( hdc, colorkey ) )
271           break;
272     }
273     intf_WarnMsg(3,"vout: DirectXCreateWindow background color:%i", colorkey);
274
275     /* create the actual brush */  
276     p_vout->p_sys->hbrush = CreateSolidBrush(colorkey);
277     p_vout->p_sys->i_rgb_colorkey = (int)colorkey;
278
279     /* Get the current size of the display and its colour depth */
280     p_vout->p_sys->rect_display.right = GetDeviceCaps( hdc, HORZRES );
281     p_vout->p_sys->rect_display.bottom = GetDeviceCaps( hdc, VERTRES );
282     p_vout->p_sys->i_display_depth = GetDeviceCaps( hdc, BITSPIXEL );
283     intf_WarnMsg( 3, "vout: Screen dimensions %ix%i colour depth %i",
284                   p_vout->p_sys->rect_display.right,
285                   p_vout->p_sys->rect_display.bottom,
286                   p_vout->p_sys->i_display_depth );
287
288     ReleaseDC( p_vout->p_sys->hwnd, hdc );
289
290     /* Get the Icon from the main app */
291     if( GetModuleFileName( NULL, vlc_path, _MAX_PATH ) )
292     {
293         vlc_icon = ExtractIcon( hInstance, vlc_path, 0 );
294     }
295     if( !vlc_icon )
296         vlc_icon = LoadIcon( NULL, IDI_APPLICATION );
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         = CopyIcon( 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       = CopyIcon( vlc_icon );            /* load the vlc icon */
312
313     /* register the window class */
314     if (!RegisterClassEx(&wc))
315     {
316         /* Check why it failed. If it's because one already exists then fine */
317         WNDCLASS wndclass;
318         if( !GetClassInfo( hInstance, "VLC DirectX", &wndclass ) )
319         {
320             intf_ErrMsg( "vout: DirectXCreateWindow RegisterClass FAILED" );
321             return (1);
322         }
323     }
324
325     /* when you create a window you give the dimensions you wish it to have.
326      * Unfortunatly these dimensions will include the borders and title bar.
327      * We use the following function to find out the size of the window
328      * corresponding to the useable surface we want */
329     rect_window.top    = 10;
330     rect_window.left   = 10;
331     rect_window.right  = rect_window.left + p_vout->p_sys->i_window_width;
332     rect_window.bottom = rect_window.top + p_vout->p_sys->i_window_height;
333     AdjustWindowRect( &rect_window, WS_OVERLAPPEDWINDOW|WS_SIZEBOX, 0 );
334
335     /* create the window */
336     p_vout->p_sys->hwnd = CreateWindow("VLC DirectX",/* name of window class */
337                     "VLC DirectX",                  /* window title bar text */
338                     WS_OVERLAPPEDWINDOW
339                     | WS_SIZEBOX,               /* window style */
340                     CW_USEDEFAULT,                   /* default X coordinate */
341                     0,                               /* default Y coordinate */
342                     rect_window.right - rect_window.left,    /* window width */
343                     rect_window.bottom - rect_window.top,   /* window height */
344                     NULL,                                /* no parent window */
345                     NULL,                          /* no menu in this window */
346                     hInstance,            /* handle of this program instance */
347                     NULL);                        /* no additional arguments */
348
349     if (p_vout->p_sys->hwnd == NULL) {
350         intf_WarnMsg( 3, "vout: DirectXCreateWindow create window FAILED" );
351         return (1);
352     }
353
354     /* store a p_vout pointer into the window local storage (for later use
355      * in DirectXEventProc).
356      * We need to use SetWindowLongPtr when it is available in mingw */
357     SetWindowLong( p_vout->p_sys->hwnd, GWL_USERDATA, (LONG)p_vout );
358
359     /* now display the window */
360     ShowWindow(p_vout->p_sys->hwnd, SW_SHOW);
361
362     return ( 0 );
363 }
364
365 /*****************************************************************************
366  * DirectXCloseWindow: close the window created by DirectXCreateWindow
367  *****************************************************************************
368  * This function returns all resources allocated by DirectXCreateWindow.
369  *****************************************************************************/
370 static void DirectXCloseWindow( vout_thread_t *p_vout )
371 {
372     intf_WarnMsg( 3, "vout: DirectXCloseWindow" );
373     if( p_vout->p_sys->hwnd != NULL )
374     {
375         DestroyWindow( p_vout->p_sys->hwnd);
376         p_vout->p_sys->hwnd = NULL;
377     }
378
379     /* We don't unregister the Window Class because it could lead to race
380      * conditions and it will be done anyway by the system when the app will
381      * exit */
382
383     /* free window background brush */
384     if( p_vout->p_sys->hbrush != NULL )
385     {
386         DeleteObject( p_vout->p_sys->hbrush );
387         p_vout->p_sys->hbrush = NULL;
388     }
389 }
390
391 /*****************************************************************************
392  * DirectXUpdateRects: 
393  *****************************************************************************
394  * This function is called when the window position and size is changed, and
395  * its job is to update the source and destination RECTs used to display the
396  * picture.
397  *****************************************************************************/
398 static void DirectXUpdateRects( vout_thread_t *p_vout )
399 {
400     int i_width, i_height, i_x, i_y;
401
402 #define rect_src p_vout->p_sys->rect_src
403 #define rect_src_clipped p_vout->p_sys->rect_src_clipped
404 #define rect_dest p_vout->p_sys->rect_dest
405 #define rect_dest_clipped p_vout->p_sys->rect_dest_clipped
406 #define rect_display p_vout->p_sys->rect_display
407
408     vout_PlacePicture( p_vout, p_vout->p_sys->i_window_width,
409                        p_vout->p_sys->i_window_height,
410                        &i_x, &i_y, &i_width, &i_height );
411
412     /* Destination image position and dimensions */
413     rect_dest.left = i_x + p_vout->p_sys->i_window_x;
414     rect_dest.top = i_y + p_vout->p_sys->i_window_y;
415     rect_dest.right = rect_dest.left + i_width;
416     rect_dest.bottom = rect_dest.top + i_height;
417
418
419     /* UpdateOverlay directdraw function doesn't automatically clip to the
420      * display size so we need to do it otherwise it will fails */
421
422     /* Clip the destination window */
423     IntersectRect( &rect_dest_clipped, &rect_dest, &rect_display );
424
425 #if 0
426     intf_WarnMsg( 3, "vout: DirectXUpdateRects image_dst_clipped coords:"
427                   " %i,%i,%i,%i",
428                   rect_dest_clipped.left, rect_dest_clipped.top,
429                   rect_dest_clipped.right, rect_dest_clipped.bottom);
430 #endif
431
432     /* the 2 following lines are to fix a bug when clicking on the desktop */
433     if( (rect_dest_clipped.right - rect_dest_clipped.left)==0 ||
434         (rect_dest_clipped.bottom - rect_dest_clipped.top)==0 )
435     {
436         SetRectEmpty( &rect_src_clipped );
437         return;
438     }
439
440     /* src image dimensions */
441     rect_src.left = 0;
442     rect_src.top = 0;
443     rect_src.right = p_vout->render.i_width;
444     rect_src.bottom = p_vout->render.i_height;
445
446     /* Clip the source image */
447     rect_src_clipped.left = (rect_dest_clipped.left - rect_dest.left) *
448       p_vout->render.i_width / (rect_dest.right - rect_dest.left);
449     rect_src_clipped.right = p_vout->render.i_width - 
450       (rect_dest.right - rect_dest_clipped.right) * p_vout->render.i_width /
451       (rect_dest.right - rect_dest.left);
452     rect_src_clipped.top = (rect_dest_clipped.top - rect_dest.top) *
453       p_vout->render.i_height / (rect_dest.bottom - rect_dest.top);
454     rect_src_clipped.bottom = p_vout->render.i_height -
455       (rect_dest.bottom - rect_dest_clipped.bottom) * p_vout->render.i_height /
456       (rect_dest.bottom - rect_dest.top);
457
458 #if 0
459     intf_WarnMsg( 3, "vout: DirectXUpdateRects image_src_clipped"
460                   " coords: %i,%i,%i,%i",
461                   rect_src_clipped.left, rect_src_clipped.top,
462                   rect_src_clipped.right, rect_src_clipped.bottom);
463 #endif
464
465 #undef rect_src
466 #undef rect_src_clipped
467 #undef rect_dest
468 #undef rect_dest_clipped
469 #undef rect_display
470 }
471
472 /*****************************************************************************
473  * DirectXEventProc: This is the window event processing function.
474  *****************************************************************************
475  * On Windows, when you create a window you have to attach an event processing
476  * function to it. The aim of this function is to manage "Queued Messages" and
477  * "Nonqueued Messages".
478  * Queued Messages are those picked up and retransmitted by vout_Manage
479  * (using the GetMessage and DispatchMessage functions).
480  * Nonqueued Messages are those that Windows will send directly to this
481  * procedure (like WM_DESTROY, WM_WINDOWPOSCHANGED...)
482  *****************************************************************************/
483 static long FAR PASCAL DirectXEventProc( HWND hwnd, UINT message,
484                                          WPARAM wParam, LPARAM lParam )
485 {
486     vout_thread_t *p_vout;
487
488     switch( message )
489     {
490
491     case WM_WINDOWPOSCHANGED:
492         {
493         RECT     rect_window;
494         POINT    point_window;
495
496         p_vout = (vout_thread_t *)GetWindowLong( hwnd, GWL_USERDATA );
497
498         /* update the window position */
499         point_window.x = 0;
500         point_window.y = 0;
501         ClientToScreen( hwnd, &point_window );
502         p_vout->p_sys->i_window_x = point_window.x;
503         p_vout->p_sys->i_window_y = point_window.y;
504
505         /* update the window size */
506         GetClientRect( hwnd, &rect_window );
507         p_vout->p_sys->i_window_width = rect_window.right;
508         p_vout->p_sys->i_window_height = rect_window.bottom;
509
510         DirectXUpdateRects( p_vout );
511         if( p_vout->p_sys->b_using_overlay &&
512             !p_vout->p_sys->b_event_thread_die )
513             DirectXUpdateOverlay( p_vout );
514
515         /* signal the size change */
516         p_vout->p_sys->i_changes |= VOUT_SIZE_CHANGE;
517
518         return 0;
519         }
520         break;
521
522     /* the user wants to close the window */
523     case WM_CLOSE:
524         intf_WarnMsg( 4, "vout: WinProc WM_CLOSE" );
525         /* exit application */
526         p_main->p_intf->b_die = 1;
527         return 0;
528         break;
529
530     /* the window has been closed so shut down everything now */
531     case WM_DESTROY:
532         intf_WarnMsg( 4, "vout: WinProc WM_DESTROY" );
533         /* just destroy the window */
534         PostQuitMessage( 0 );
535         return 0;
536         break;
537
538     case WM_SYSCOMMAND:
539         switch (wParam)
540         {
541             case SC_SCREENSAVE:                     /* catch the screensaver */
542             case SC_MONITORPOWER:              /* catch the monitor turn-off */
543             intf_WarnMsg( 3, "vout: WinProc WM_SYSCOMMAND" );
544             return 0;                      /* this stops them from happening */
545         }
546         break;
547
548     case WM_ERASEBKGND:
549         p_vout = (vout_thread_t *)GetWindowLong( hwnd, GWL_USERDATA );
550         if( !p_vout->p_sys->b_using_overlay )
551         {
552             /* We want to eliminate unnecessary background redraws which create
553              * an annoying flickering */
554             int i_width, i_height, i_x, i_y;
555             RECT rect_temp;
556             GetClipBox( (HDC)wParam, &rect_temp );
557 #if 0
558             intf_WarnMsg( 4, "vout: WinProc WM_ERASEBKGND %i,%i,%i,%i",
559                           rect_temp.left, rect_temp.top,
560                           rect_temp.right, rect_temp.bottom );
561 #endif
562             vout_PlacePicture( p_vout, p_vout->p_sys->i_window_width,
563                                p_vout->p_sys->i_window_height,
564                                &i_x, &i_y, &i_width, &i_height );
565             ExcludeClipRect( (HDC)wParam, i_x, i_y,
566                              i_x + i_width, i_y + i_height );
567         }
568         break;
569
570     default:
571         //intf_WarnMsg( 4, "vout: WinProc WM Default %i", message );
572         break;
573     }
574
575     return DefWindowProc(hwnd, message, wParam, lParam);
576 }