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