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