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