]> git.sesse.net Git - vlc/blob - plugins/directx/vout_events.c
* updated INSTALL.win32
[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.18 2002/05/18 22:41:43 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             break;
130
131         case WM_LBUTTONDBLCLK:
132             p_vout->p_sys->i_changes |= VOUT_FULLSCREEN_CHANGE;
133             break;
134
135         case WM_KEYDOWN:
136             /* the key events are first processed here. The next
137              * message processed by this main message loop will be the
138              * char translation of the key event */
139             intf_WarnMsg( 3, "vout: vout_Manage WM_KEYDOWN" );
140             switch( msg.wParam )
141             {
142             case VK_ESCAPE:
143             case VK_F12:
144                 /* exit application */
145                 p_main->p_intf->b_die = 1;
146                 break;
147             }
148             TranslateMessage(&msg);
149             break;
150
151         case WM_CHAR:
152             switch( msg.wParam )
153             {
154             case 'q':
155             case 'Q':
156                 /* exit application */
157                 p_main->p_intf->b_die = 1;
158                 break;
159
160             case 'f':                            /* switch to fullscreen */
161             case 'F':
162                 p_vout->p_sys->i_changes |= VOUT_FULLSCREEN_CHANGE;
163                 break;
164
165             case 'c':                                /* toggle grayscale */
166             case 'C':
167                 p_vout->b_grayscale = ! p_vout->b_grayscale;
168                 p_vout->p_sys->i_changes |= VOUT_GRAYSCALE_CHANGE;
169                 break;
170
171             case 'i':                                     /* toggle info */
172             case 'I':
173                 p_vout->b_info = ! p_vout->b_info;
174                 p_vout->p_sys->i_changes |= VOUT_INFO_CHANGE;
175                 break;
176
177             case 's':                                  /* toggle scaling */
178             case 'S':
179                 p_vout->b_scale = ! p_vout->b_scale;
180                 p_vout->p_sys->i_changes |= VOUT_SCALE_CHANGE;
181                 break;
182
183             case ' ':                                /* toggle interface */
184                 p_vout->b_interface = ! p_vout->b_interface;
185                 p_vout->p_sys->i_changes |= VOUT_INTF_CHANGE;
186                 break;
187
188             case '0': network_ChannelJoin( 0 ); break;
189             case '1': network_ChannelJoin( 1 ); break;
190             case '2': network_ChannelJoin( 2 ); break;
191             case '3': network_ChannelJoin( 3 ); break;
192             case '4': network_ChannelJoin( 4 ); break;
193             case '5': network_ChannelJoin( 5 ); break;
194             case '6': network_ChannelJoin( 6 ); break;
195             case '7': network_ChannelJoin( 7 ); break;
196             case '8': network_ChannelJoin( 8 ); break;
197             case '9': network_ChannelJoin( 9 ); break;
198
199             default:
200                 break;
201             }
202
203         default:
204             /* Messages we don't handle directly are dispatched to the
205              * window procedure */
206             TranslateMessage(&msg);
207             DispatchMessage(&msg);
208             break;
209
210         } /* End Switch */
211
212     } /* End Main loop */
213
214     if( msg.message == WM_QUIT )
215     {
216         intf_WarnMsg( 3, "vout: DirectXEventThread WM_QUIT... "
217                       "shouldn't happen!!" );
218         p_vout->p_sys->hwnd = NULL; /* Window already destroyed */
219     }
220
221     intf_WarnMsg( 3, "vout: DirectXEventThread Terminating" );
222
223     /* clear the changes formerly signaled */
224     p_vout->p_sys->i_changes = 0;
225
226     DirectXCloseWindow( p_vout );
227 }
228
229
230 /* following functions are local */
231
232 /*****************************************************************************
233  * DirectXCreateWindow: create a window for the video.
234  *****************************************************************************
235  * Before creating a direct draw surface, we need to create a window in which
236  * the video will be displayed. This window will also allow us to capture the
237  * events.
238  *****************************************************************************/
239 static int DirectXCreateWindow( vout_thread_t *p_vout )
240 {
241     HINSTANCE  hInstance;
242     WNDCLASSEX wc;                                /* window class components */
243     RECT       rect_window;
244     COLORREF   colorkey; 
245     HDC        hdc;
246     HICON      vlc_icon = NULL;
247     char       vlc_path[_MAX_PATH+1];
248
249     intf_WarnMsg( 3, "vout: DirectXCreateWindow" );
250
251     /* get this module's instance */
252     hInstance = GetModuleHandle(NULL);
253
254     /* Create a BRUSH that will be used by Windows to paint the window
255      * background.
256      * This window background is important for us as it will be used by the
257      * graphics card to display the overlay.
258      * This is why we carefully choose the color for this background, the goal
259      * being to choose a color which isn't complete black but nearly. We
260      * obviously don't want to use black as a colorkey for the overlay because
261      * black is one of the most used color and thus would give us undesirable
262      * effects */
263     /* the first step is to find the colorkey we want to use. The difficulty
264      * comes from the potential dithering (depends on the display depth)
265      * because we need to know the real RGB value of the chosen colorkey */
266     hdc = GetDC( NULL );
267     for( colorkey = 5; colorkey < 0xFF /*all shades of red*/; colorkey++ )
268     {
269         if( colorkey == GetNearestColor( hdc, colorkey ) )
270           break;
271     }
272     intf_WarnMsg(3,"vout: DirectXCreateWindow background color:%i", colorkey);
273
274     /* create the actual brush */  
275     p_vout->p_sys->hbrush = CreateSolidBrush(colorkey);
276     p_vout->p_sys->i_rgb_colorkey = (int)colorkey;
277
278     /* Get the current size of the display and its colour depth */
279     p_vout->p_sys->rect_display.right = GetDeviceCaps( hdc, HORZRES );
280     p_vout->p_sys->rect_display.bottom = GetDeviceCaps( hdc, VERTRES );
281     p_vout->p_sys->i_display_depth = GetDeviceCaps( hdc, BITSPIXEL );
282     intf_WarnMsg( 3, "vout: Screen dimensions %ix%i colour depth %i",
283                   p_vout->p_sys->rect_display.right,
284                   p_vout->p_sys->rect_display.bottom,
285                   p_vout->p_sys->i_display_depth );
286
287     ReleaseDC( p_vout->p_sys->hwnd, hdc );
288
289     /* Get the Icon from the main app */
290     vlc_icon = NULL;
291     if( GetModuleFileName( NULL, vlc_path, _MAX_PATH ) )
292     {
293         vlc_icon = ExtractIcon( hInstance, vlc_path, 0 );
294     }
295
296
297     /* fill in the window class structure */
298     wc.cbSize        = sizeof(WNDCLASSEX);
299     wc.style         = CS_DBLCLKS;                       /* style: dbl click */
300     wc.lpfnWndProc   = (WNDPROC)DirectXEventProc;           /* event handler */
301     wc.cbClsExtra    = 0;                             /* no extra class data */
302     wc.cbWndExtra    = 0;                            /* no extra window data */
303     wc.hInstance     = hInstance;                                /* instance */
304     wc.hIcon         = vlc_icon;                        /* load the vlc icon */
305     wc.hCursor       = LoadCursor(NULL, IDC_ARROW); /* load a default cursor */
306     wc.hbrBackground = p_vout->p_sys->hbrush;            /* background color */
307     wc.lpszMenuName  = NULL;                                      /* no menu */
308     wc.lpszClassName = "VLC DirectX";                 /* use a special class */
309     wc.hIconSm       = vlc_icon;                        /* load the vlc icon */
310
311     /* register the window class */
312     if (!RegisterClassEx(&wc))
313     {
314         WNDCLASS wndclass;
315
316         /* free window background brush */
317         if( p_vout->p_sys->hbrush )
318         {
319             DeleteObject( p_vout->p_sys->hbrush );
320             p_vout->p_sys->hbrush = NULL;
321         }
322
323         if( vlc_icon )
324             DestroyIcon( vlc_icon );
325
326         /* Check why it failed. If it's because one already exists then fine */
327         if( !GetClassInfo( hInstance, "VLC DirectX", &wndclass ) )
328         {
329             intf_ErrMsg( "vout: DirectXCreateWindow RegisterClass FAILED" );
330             return (1);
331         }
332     }
333
334     /* when you create a window you give the dimensions you wish it to have.
335      * Unfortunatly these dimensions will include the borders and title bar.
336      * We use the following function to find out the size of the window
337      * corresponding to the useable surface we want */
338     rect_window.top    = 10;
339     rect_window.left   = 10;
340     rect_window.right  = rect_window.left + p_vout->p_sys->i_window_width;
341     rect_window.bottom = rect_window.top + p_vout->p_sys->i_window_height;
342     AdjustWindowRect( &rect_window, WS_OVERLAPPEDWINDOW|WS_SIZEBOX, 0 );
343
344     /* create the window */
345     p_vout->p_sys->hwnd = CreateWindow("VLC DirectX",/* name of window class */
346                     "VLC DirectX",                  /* window title bar text */
347                     WS_OVERLAPPEDWINDOW
348                     | WS_SIZEBOX,               /* window style */
349                     CW_USEDEFAULT,                   /* default X coordinate */
350                     0,                               /* default Y coordinate */
351                     rect_window.right - rect_window.left,    /* window width */
352                     rect_window.bottom - rect_window.top,   /* window height */
353                     NULL,                                /* no parent window */
354                     NULL,                          /* no menu in this window */
355                     hInstance,            /* handle of this program instance */
356                     NULL);                        /* no additional arguments */
357
358     if (p_vout->p_sys->hwnd == NULL) {
359         intf_WarnMsg( 3, "vout: DirectXCreateWindow create window FAILED" );
360         return (1);
361     }
362
363     /* store a p_vout pointer into the window local storage (for later use
364      * in DirectXEventProc).
365      * We need to use SetWindowLongPtr when it is available in mingw */
366     SetWindowLong( p_vout->p_sys->hwnd, GWL_USERDATA, (LONG)p_vout );
367
368     /* now display the window */
369     ShowWindow(p_vout->p_sys->hwnd, SW_SHOW);
370
371     return ( 0 );
372 }
373
374 /*****************************************************************************
375  * DirectXCloseWindow: close the window created by DirectXCreateWindow
376  *****************************************************************************
377  * This function returns all resources allocated by DirectXCreateWindow.
378  *****************************************************************************/
379 static void DirectXCloseWindow( vout_thread_t *p_vout )
380 {
381     intf_WarnMsg( 3, "vout: DirectXCloseWindow" );
382
383     vlc_mutex_lock( &p_vout->p_sys->event_thread_lock );
384
385     if( p_vout->p_sys->hwnd != NULL )
386     {
387         DestroyWindow( p_vout->p_sys->hwnd );
388         p_vout->p_sys->hwnd = NULL;
389     }
390
391     p_vout->p_sys->i_event_thread_status = THREAD_OVER;
392
393     vlc_mutex_unlock( &p_vout->p_sys->event_thread_lock );
394
395     /* We don't unregister the Window Class because it could lead to race
396      * conditions and it will be done anyway by the system when the app will
397      * exit */
398 }
399
400 /*****************************************************************************
401  * DirectXUpdateRects: 
402  *****************************************************************************
403  * This function is called when the window position and size is changed, and
404  * its job is to update the source and destination RECTs used to display the
405  * picture.
406  *****************************************************************************/
407 static void DirectXUpdateRects( vout_thread_t *p_vout )
408 {
409     int i_width, i_height, i_x, i_y;
410
411 #define rect_src p_vout->p_sys->rect_src
412 #define rect_src_clipped p_vout->p_sys->rect_src_clipped
413 #define rect_dest p_vout->p_sys->rect_dest
414 #define rect_dest_clipped p_vout->p_sys->rect_dest_clipped
415 #define rect_display p_vout->p_sys->rect_display
416
417     vout_PlacePicture( p_vout, p_vout->p_sys->i_window_width,
418                        p_vout->p_sys->i_window_height,
419                        &i_x, &i_y, &i_width, &i_height );
420
421     /* Destination image position and dimensions */
422     rect_dest.left = i_x + p_vout->p_sys->i_window_x;
423     rect_dest.top = i_y + p_vout->p_sys->i_window_y;
424     rect_dest.right = rect_dest.left + i_width;
425     rect_dest.bottom = rect_dest.top + i_height;
426
427
428     /* UpdateOverlay directdraw function doesn't automatically clip to the
429      * display size so we need to do it otherwise it will fails */
430
431     /* Clip the destination window */
432     IntersectRect( &rect_dest_clipped, &rect_dest, &rect_display );
433
434 #if 0
435     intf_WarnMsg( 3, "vout: DirectXUpdateRects image_dst_clipped coords:"
436                   " %i,%i,%i,%i",
437                   rect_dest_clipped.left, rect_dest_clipped.top,
438                   rect_dest_clipped.right, rect_dest_clipped.bottom);
439 #endif
440
441     /* the 2 following lines are to fix a bug when clicking on the desktop */
442     if( (rect_dest_clipped.right - rect_dest_clipped.left)==0 ||
443         (rect_dest_clipped.bottom - rect_dest_clipped.top)==0 )
444     {
445         SetRectEmpty( &rect_src_clipped );
446         return;
447     }
448
449     /* src image dimensions */
450     rect_src.left = 0;
451     rect_src.top = 0;
452     rect_src.right = p_vout->render.i_width;
453     rect_src.bottom = p_vout->render.i_height;
454
455     /* Clip the source image */
456     rect_src_clipped.left = (rect_dest_clipped.left - rect_dest.left) *
457       p_vout->render.i_width / (rect_dest.right - rect_dest.left);
458     rect_src_clipped.right = p_vout->render.i_width - 
459       (rect_dest.right - rect_dest_clipped.right) * p_vout->render.i_width /
460       (rect_dest.right - rect_dest.left);
461     rect_src_clipped.top = (rect_dest_clipped.top - rect_dest.top) *
462       p_vout->render.i_height / (rect_dest.bottom - rect_dest.top);
463     rect_src_clipped.bottom = p_vout->render.i_height -
464       (rect_dest.bottom - rect_dest_clipped.bottom) * p_vout->render.i_height /
465       (rect_dest.bottom - rect_dest.top);
466
467 #if 0
468     intf_WarnMsg( 3, "vout: DirectXUpdateRects image_src_clipped"
469                   " coords: %i,%i,%i,%i",
470                   rect_src_clipped.left, rect_src_clipped.top,
471                   rect_src_clipped.right, rect_src_clipped.bottom);
472 #endif
473
474 #undef rect_src
475 #undef rect_src_clipped
476 #undef rect_dest
477 #undef rect_dest_clipped
478 #undef rect_display
479 }
480
481 /*****************************************************************************
482  * DirectXEventProc: This is the window event processing function.
483  *****************************************************************************
484  * On Windows, when you create a window you have to attach an event processing
485  * function to it. The aim of this function is to manage "Queued Messages" and
486  * "Nonqueued Messages".
487  * Queued Messages are those picked up and retransmitted by vout_Manage
488  * (using the GetMessage and DispatchMessage functions).
489  * Nonqueued Messages are those that Windows will send directly to this
490  * procedure (like WM_DESTROY, WM_WINDOWPOSCHANGED...)
491  *****************************************************************************/
492 static long FAR PASCAL DirectXEventProc( HWND hwnd, UINT message,
493                                          WPARAM wParam, LPARAM lParam )
494 {
495     vout_thread_t *p_vout;
496
497     switch( message )
498     {
499
500     case WM_WINDOWPOSCHANGED:
501         {
502         RECT     rect_window;
503         POINT    point_window;
504
505         p_vout = (vout_thread_t *)GetWindowLong( hwnd, GWL_USERDATA );
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->b_event_thread_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->b_event_thread_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         intf_WarnMsg( 4, "vout: WinProc WM_CLOSE" );
536         /* exit application */
537         p_main->p_intf->b_die = 1;
538         return 0;
539         break;
540
541     case WM_SYSCOMMAND:
542         switch (wParam)
543         {
544             case SC_SCREENSAVE:                     /* catch the screensaver */
545             case SC_MONITORPOWER:              /* catch the monitor turn-off */
546             intf_WarnMsg( 3, "vout: WinProc WM_SYSCOMMAND" );
547             return 0;                      /* this stops them from happening */
548         }
549         break;
550
551     case WM_ERASEBKGND:
552         p_vout = (vout_thread_t *)GetWindowLong( hwnd, GWL_USERDATA );
553         if( !p_vout->p_sys->b_using_overlay )
554         {
555             /* We want to eliminate unnecessary background redraws which create
556              * an annoying flickering */
557             int i_width, i_height, i_x, i_y;
558             RECT rect_temp;
559             GetClipBox( (HDC)wParam, &rect_temp );
560 #if 0
561             intf_WarnMsg( 4, "vout: WinProc WM_ERASEBKGND %i,%i,%i,%i",
562                           rect_temp.left, rect_temp.top,
563                           rect_temp.right, rect_temp.bottom );
564 #endif
565             vout_PlacePicture( p_vout, p_vout->p_sys->i_window_width,
566                                p_vout->p_sys->i_window_height,
567                                &i_x, &i_y, &i_width, &i_height );
568             ExcludeClipRect( (HDC)wParam, i_x, i_y,
569                              i_x + i_width, i_y + i_height );
570         }
571         break;
572
573     default:
574         //intf_WarnMsg( 4, "vout: WinProc WM Default %i", message );
575         break;
576     }
577
578     return DefWindowProc(hwnd, message, wParam, lParam);
579 }