]> git.sesse.net Git - vlc/blob - plugins/directx/vout_events.c
b711141eeeb69ebf21f0741b91c61641d6796141
[vlc] / plugins / directx / vout_events.c
1 /*****************************************************************************
2  * vout_events.c: Windows DirectX video output events handler
3  *****************************************************************************
4  * Copyright (C) 1998, 1999, 2000 VideoLAN
5  * $Id: vout_events.c,v 1.2 2001/07/12 23:06:54 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 "defs.h"
30
31 #include <errno.h>                                                 /* ENOMEM */
32 #include <stdlib.h>                                                /* free() */
33 #include <string.h>                                            /* strerror() */
34
35 #include "config.h"
36 #include "common.h"
37 #include "threads.h"
38 #include "mtime.h"
39 #include "tests.h"
40 #include "netutils.h"
41
42 #include "video.h"
43 #include "video_output.h"
44
45 #include <windows.h>
46 #include <windowsx.h>
47 #include <directx.h>
48
49 #include "intf_msg.h"
50 #include "interface.h"
51 #include "main.h"
52
53 #include "modules.h"
54 #include "modules_export.h"
55
56 #include "vout_directx.h"
57
58 /*****************************************************************************
59  * Local prototypes.
60  *****************************************************************************/
61 static int  DirectXCreateWindow( vout_thread_t *p_vout );
62 static void DirectXCloseWindow ( vout_thread_t *p_vout );
63 static long FAR PASCAL DirectXEventProc ( HWND hwnd, UINT message,
64                                           WPARAM wParam, LPARAM lParam );
65
66 /*****************************************************************************
67  * Global variables.
68  * I really hate them, but here I don't have any choice. And anyway, this
69  * shouldn't really affect reentrancy.
70  * This variable is used to know if we have to update the overlay position
71  * and size. This is to fix the bug we've got when the Windows option, to show
72  * the content of a window when you drag it, is enabled.
73  *****************************************************************************/
74 int b_directx_update_overlay = 0;
75
76 /*****************************************************************************
77  * DirectXEventThread: Create video window & handle its messages
78  *****************************************************************************
79  * This function creates a video window and then enters an infinite loop
80  * that handles the messages sent to that window.
81  * The main goal of this thread is to isolate the Win32 PeekMessage function
82  * because this one can block for a long time.
83  *****************************************************************************/
84 void DirectXEventThread( vout_thread_t *p_vout )
85 {
86     MSG             msg;
87     boolean_t       b_dispatch_msg = TRUE;
88
89     /* Initialisation */
90
91     /* Create a window for the video */
92     /* Creating a window under Windows also initializes the thread's event
93      * message qeue */
94     if( DirectXCreateWindow( p_vout ) )
95     {
96         intf_ErrMsg( "vout error: can't create window" );
97         p_vout->p_sys->i_event_thread_status = THREAD_FATAL;
98         /* signal the creation of the window */
99         vlc_mutex_lock( &p_vout->p_sys->event_thread_lock );
100         vlc_cond_signal( &p_vout->p_sys->event_thread_wait );
101         vlc_mutex_unlock( &p_vout->p_sys->event_thread_lock );
102         return;
103     }
104
105     /* signal the creation of the window */
106     p_vout->p_sys->i_event_thread_status = THREAD_READY;
107     vlc_mutex_lock( &p_vout->p_sys->event_thread_lock );
108     vlc_cond_signal( &p_vout->p_sys->event_thread_wait );
109     vlc_mutex_unlock( &p_vout->p_sys->event_thread_lock );
110
111     /* Main loop */
112     while( !p_vout->b_die && !p_vout->p_sys->b_event_thread_die )
113     {
114
115         /* GetMessage will sleep if there's no message in the queue */
116         if( GetMessage( &msg, NULL, 0, 0 ) >= 0 )
117         {
118             switch( msg.message )
119             {
120                 
121             case WM_CLOSE:
122                 intf_WarnMsg( 3, "vout: vout_Manage WM_CLOSE" );
123                 break;
124                 
125             case WM_QUIT:
126                 intf_WarnMsg( 3, "vout: vout_Manage WM_QUIT" );
127                 p_vout->p_sys->b_event_thread_die = 1;
128                 p_main->p_intf->b_die = 1;
129                 break;
130                 
131             case WM_MOVE:
132                 intf_WarnMsg( 3, "vout: vout_Manage WM_MOVE" );
133                 if( !p_vout->b_need_render )
134                 {
135                     p_vout->p_sys->i_changes |= VOUT_SIZE_CHANGE;
136                 }
137                 /* don't create a never ending loop */
138                 b_dispatch_msg = FALSE;
139                 break;
140           
141             case WM_APP:
142                 intf_WarnMsg( 3, "vout: vout_Manage WM_APP" );
143                 if( !p_vout->b_need_render )
144                 {
145                     p_vout->p_sys->i_changes |= VOUT_SIZE_CHANGE;
146                 }
147                 /* size change has been handled (to fix a bug)*/
148                 b_directx_update_overlay = 0;
149                 /* don't create a never ending loop */
150                 b_dispatch_msg = FALSE;
151                 break;
152               
153 #if 0
154             case WM_PAINT:
155                 intf_WarnMsg( 4, "vout: vout_Manage WM_PAINT" );
156                 break;
157               
158             case WM_ERASEBKGND:
159                 intf_WarnMsg( 4, "vout: vout_Manage WM_ERASEBKGND" );
160                 break;
161 #endif
162               
163             case WM_MOUSEMOVE:
164                 intf_WarnMsg( 4, "vout: vout_Manage WM_MOUSEMOVE" );
165                 if( p_vout->p_sys->b_cursor )
166                 {
167                     if( p_vout->p_sys->b_cursor_autohidden )
168                     {
169                         p_vout->p_sys->b_cursor_autohidden = 0;
170                         p_vout->p_sys->i_lastmoved = mdate();
171                         ShowCursor( TRUE );
172                     }
173                     else
174                     {
175                         p_vout->p_sys->i_lastmoved = mdate();
176                     }
177                 }               
178                 break;
179                 
180             case WM_RBUTTONUP:
181                 intf_WarnMsg( 4, "vout: vout_Manage WM_RBUTTONUP" );
182                 /* FIXME: need locking ! */
183                 p_main->p_intf->b_menu_change = 1;
184                 break;
185
186             case WM_KEYDOWN:
187                 /* the key events are first processed here. The next
188                  * message processed by this main message loop will be the
189                  * char translation of the key event */
190                 intf_WarnMsg( 3, "vout: vout_Manage WM_KEYDOWN" );
191                 switch( msg.wParam )
192                 {
193                 case VK_ESCAPE:
194                 case VK_F12:
195                     p_main->p_intf->b_die = 1;
196                     break;
197                 }
198                 TranslateMessage(&msg);
199                 b_dispatch_msg = FALSE;
200                 break;
201               
202             case WM_CHAR:
203                 intf_WarnMsg( 3, "vout: vout_Manage WM_CHAR" );
204                 switch( msg.wParam )
205                 {
206                 case 'q':
207                 case 'Q':
208                     p_main->p_intf->b_die = 1;
209                     break;
210                   
211                 case 'f':                            /* switch to fullscreen */
212                 case 'F':
213                     p_vout->p_sys->i_changes |= VOUT_FULLSCREEN_CHANGE;
214                     break;
215                   
216                 case 'y':                              /* switch to hard YUV */
217                 case 'Y':
218                     p_vout->p_sys->i_changes |= VOUT_YUV_CHANGE;
219                     break;
220                   
221                 case 'c':                                /* toggle grayscale */
222                 case 'C':
223                     p_vout->b_grayscale = ! p_vout->b_grayscale;
224                     p_vout->p_sys->i_changes |= VOUT_GRAYSCALE_CHANGE;
225                     break;
226                   
227                 case 'i':                                     /* toggle info */
228                 case 'I':
229                     p_vout->b_info = ! p_vout->b_info;
230                     p_vout->p_sys->i_changes |= VOUT_INFO_CHANGE;
231                     break;
232                   
233                 case 's':                                  /* toggle scaling */
234                 case 'S':
235                     p_vout->b_scale = ! p_vout->b_scale;
236                     p_vout->p_sys->i_changes |= VOUT_SCALE_CHANGE;
237                     break;
238                   
239                 case ' ':                                /* toggle interface */
240                     p_vout->b_interface = ! p_vout->b_interface;
241                     p_vout->p_sys->i_changes |= VOUT_INTF_CHANGE;
242                     break;
243                   
244                 case '0': network_ChannelJoin( 0 ); break;
245                 case '1': network_ChannelJoin( 1 ); break;
246                 case '2': network_ChannelJoin( 2 ); break;
247                 case '3': network_ChannelJoin( 3 ); break;
248                 case '4': network_ChannelJoin( 4 ); break;
249                 case '5': network_ChannelJoin( 5 ); break;
250                 case '6': network_ChannelJoin( 6 ); break;
251                 case '7': network_ChannelJoin( 7 ); break;
252                 case '8': network_ChannelJoin( 8 ); break;
253                 case '9': network_ChannelJoin( 9 ); break;
254                   
255                 default:
256                     if( intf_ProcessKey( p_main->p_intf,
257                                          (char )msg.wParam ) )
258                     {
259                         intf_DbgMsg( "unhandled key '%c' (%i)",
260                                      (char)msg.wParam, msg.wParam );
261                     }
262                     break;
263                 }
264               
265 #if 0          
266             default:
267                 intf_WarnMsg( 4, "vout: vout_Manage WM Default %i",
268                               msg.message );
269                 break;
270 #endif
271
272             } /* End Switch */
273
274             /* don't create a never ending loop */
275             if( b_dispatch_msg )
276             {
277                 TranslateMessage(&msg);
278                 DispatchMessage(&msg);
279             }
280             b_dispatch_msg = TRUE;
281
282         } /* if( GetMessage() ) */
283         else
284         {
285             intf_ErrMsg("vout error: GetMessage failed in DirectXEventThread");
286             p_vout->p_sys->b_event_thread_die = 1;
287         } /* End if( GetMessage() ) */
288
289
290     } /* End Main loop */
291
292     /* Destroy the window */
293     DirectXCloseWindow( p_vout );
294
295     /* Set thread Status */
296     p_vout->p_sys->i_event_thread_status = THREAD_OVER;
297
298     return;
299 }
300
301
302 /* following functions are local */
303
304 /*****************************************************************************
305  * DirectXCreateWindow: create a window for the video.
306  *****************************************************************************
307  * Before creating a direct draw surface, we need to create a window in which
308  * the video will be displayed. This window will also allow us to capture the
309  * events.
310  *****************************************************************************/
311 static int DirectXCreateWindow( vout_thread_t *p_vout )
312 {
313     HINSTANCE  hInstance;
314     WNDCLASSEX wc;                                /* window class components */
315     RECT       rect_window;
316     COLORREF   colorkey; 
317     HDC        hdc;
318
319     intf_WarnMsg( 3, "vout: DirectXCreateWindow" );
320
321     /* get this module's instance */
322     hInstance = GetModuleHandle(NULL);
323
324     /* Create a BRUSH that will be used by Windows to paint the window
325      * background.
326      * This window background is important for us as it will be used by the
327      * graphics card to display the overlay.
328      * This is why we carefully choose the color for this background, the goal
329      * being to choose a color which isn't complete black but nearly. We
330      * obviously don't want to use black as a colorkey for the overlay because
331      * black is one of the most used color and thus would give us undesirable
332      * effects */
333     /* the first step is to find the colorkey we want to use. The difficulty
334      * comes from the potential dithering (depends on the display depth)
335      * because we need to know the real RGB value of the chosen colorkey */
336     hdc = GetDC( GetDesktopWindow() );
337     for( colorkey = 5; colorkey < 0xFF /*all shades of red*/; colorkey++ )
338     {
339         if( colorkey == GetNearestColor( hdc, colorkey ) )
340           break;
341     }
342     intf_WarnMsg(3,"vout: DirectXCreateWindow background color:%i", colorkey);
343     ReleaseDC( p_vout->p_sys->hwnd, hdc );
344
345     /* create the actual brush */  
346     p_vout->p_sys->hbrush = CreateSolidBrush(colorkey);
347     p_vout->p_sys->i_colorkey = (int)colorkey;
348
349     /* fill in the window class structure */
350     wc.cbSize        = sizeof(WNDCLASSEX);
351     wc.style         = 0;                               /* no special styles */
352     wc.lpfnWndProc   = (WNDPROC)DirectXEventProc;           /* event handler */
353     wc.cbClsExtra    = 0;                             /* no extra class data */
354     wc.cbWndExtra    = 0;                            /* no extra window data */
355     wc.hInstance     = hInstance;                                /* instance */
356     wc.hIcon         = LoadIcon(NULL, IDI_APPLICATION); /* load the vlc icon */
357     wc.hCursor       = LoadCursor(NULL, IDC_ARROW); /* load a default cursor */
358     wc.hbrBackground = p_vout->p_sys->hbrush;            /* background color */
359     wc.lpszMenuName  = NULL;                                      /* no menu */
360     wc.lpszClassName = "VLC DirectX";                 /* use a special class */
361     wc.hIconSm       = LoadIcon(NULL, IDI_APPLICATION); /* load the vlc icon */
362
363     /* register the window class */
364     if (!RegisterClassEx(&wc))
365     {
366         intf_WarnMsg( 3, "vout: DirectXCreateWindow register window FAILED" );
367         return (1);
368     }
369
370     /* when you create a window you give the dimensions you wish it to have.
371      * Unfortunatly these dimensions will include the borders and title bar.
372      * We use the following function to find out the size of the window
373      * corresponding to the useable surface we want */
374     rect_window.top    = 10;
375     rect_window.left   = 10;
376     rect_window.right  = rect_window.left + p_vout->p_sys->i_window_width;
377     rect_window.bottom = rect_window.top + p_vout->p_sys->i_window_height;
378     AdjustWindowRect( &rect_window, WS_OVERLAPPEDWINDOW|WS_SIZEBOX, 0 );
379
380     /* create the window */
381     p_vout->p_sys->hwnd = CreateWindow("VLC DirectX",/* name of window class */
382                     "VLC DirectX",                  /* window title bar text */
383                     WS_OVERLAPPEDWINDOW
384                     | WS_SIZEBOX | WS_VISIBLE,               /* window style */
385                     10,                              /* default X coordinate */
386                     10,                              /* default Y coordinate */
387                     rect_window.right - rect_window.left,    /* window width */
388                     rect_window.bottom - rect_window.top,   /* window height */
389                     NULL,                                /* no parent window */
390                     NULL,                          /* no menu in this window */
391                     hInstance,            /* handle of this program instance */
392                     NULL);                        /* no additional arguments */
393
394     if (p_vout->p_sys->hwnd == NULL) {
395         intf_WarnMsg( 3, "vout: DirectXCreateWindow create window FAILED" );
396         return (1);
397     }
398
399     /* now display the window */
400     ShowWindow(p_vout->p_sys->hwnd, SW_SHOW);
401
402     return ( 0 );
403 }
404
405 /*****************************************************************************
406  * DirectXCloseWindow: close the window created by DirectXCreateWindow
407  *****************************************************************************
408  * This function returns all resources allocated by DirectXCreateWindow.
409  *****************************************************************************/
410 static void DirectXCloseWindow( vout_thread_t *p_vout )
411 {
412     HINSTANCE hInstance;
413
414     intf_WarnMsg( 3, "vout: DirectXCloseWindow" );
415     if( p_vout->p_sys->hwnd != NULL )
416     {
417         DestroyWindow( p_vout->p_sys->hwnd);
418         p_vout->p_sys->hwnd = NULL;
419     }
420
421     hInstance = GetModuleHandle(NULL);
422     UnregisterClass( "VLC DirectX",                            /* class name */
423                      hInstance );          /* handle to application instance */
424
425     /* free window background brush */
426     if( p_vout->p_sys->hwnd != NULL )
427     {
428         DeleteObject( p_vout->p_sys->hbrush );
429         p_vout->p_sys->hbrush = NULL;
430     }
431 }
432
433 /*****************************************************************************
434  * DirectXEventProc: This is the window event processing function.
435  *****************************************************************************
436  * On Windows, when you create a window you have to attach an event processing
437  * function to it. The aim of this function is to manage "Queued Messages" and
438  * "Nonqueued Messages".
439  * Queued Messages are those picked up and retransmitted by vout_Manage
440  * (using the GetMessage function).
441  * Nonqueued Messages are those that Windows will send directly to this
442  * function (like WM_DESTROY, WM_WINDOWPOSCHANGED...)
443  *****************************************************************************/
444 static long FAR PASCAL DirectXEventProc( HWND hwnd, UINT message,
445                                          WPARAM wParam, LPARAM lParam )
446 {
447     switch( message )
448     {
449
450 #if 0
451     case WM_APP:
452         intf_WarnMsg( 3, "vout: WinProc WM_APP" );
453         break;
454
455     case WM_ACTIVATE:
456         intf_WarnMsg( 4, "vout: WinProc WM_ACTIVED" );
457         break;
458
459     case WM_CREATE:
460         intf_WarnMsg( 4, "vout: WinProc WM_CREATE" );
461         break;
462
463     /* the user wants to close the window */
464     case WM_CLOSE:
465         intf_WarnMsg( 4, "vout: WinProc WM_CLOSE" );
466         break;
467 #endif
468
469     /* the window has been closed so shut down everything now */
470     case WM_DESTROY:
471         intf_WarnMsg( 4, "vout: WinProc WM_DESTROY" );
472         PostQuitMessage( 0 );
473         break;
474
475     case WM_SYSCOMMAND:
476         switch (wParam)
477         {
478             case SC_SCREENSAVE:                     /* catch the screensaver */
479             case SC_MONITORPOWER:              /* catch the monitor turn-off */
480             intf_WarnMsg( 3, "vout: WinProc WM_SYSCOMMAND" );
481             return 0;                      /* this stops them from happening */
482         }
483         break;
484
485 #if 0
486     case WM_MOVE:
487         intf_WarnMsg( 4, "vout: WinProc WM_MOVE" );
488         break;
489
490     case WM_SIZE:
491         intf_WarnMsg( 4, "vout: WinProc WM_SIZE" );
492         break;
493
494     case WM_MOVING:
495         intf_WarnMsg( 4, "vout: WinProc WM_MOVING" );
496         break;
497
498     case WM_ENTERSIZEMOVE:
499         intf_WarnMsg( 4, "vout: WinProc WM_ENTERSIZEMOVE" );
500         break;
501
502     case WM_SIZING:
503         intf_WarnMsg( 4, "vout: WinProc WM_SIZING" );
504         break;
505 #endif
506
507     case WM_WINDOWPOSCHANGED:
508         intf_WarnMsg( 3, "vout: WinProc WM_WINDOWPOSCHANGED" );
509         b_directx_update_overlay = 1;
510         PostMessage( hwnd, WM_APP, 0, 0);
511         break;
512
513 #if 0
514     case WM_WINDOWPOSCHANGING:
515         intf_WarnMsg( 3, "vout: WinProc WM_WINDOWPOSCHANGING" );
516         break;
517
518     case WM_PAINT:
519         intf_WarnMsg( 4, "vout: WinProc WM_PAINT" );
520         break;
521
522     case WM_ERASEBKGND:
523         intf_WarnMsg( 4, "vout: WinProc WM_ERASEBKGND" );
524         break;
525
526     default:
527         intf_WarnMsg( 4, "vout: WinProc WM Default %i", message );
528         break;
529 #endif
530     }
531
532     return DefWindowProc(hwnd, message, wParam, lParam);
533 }