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 $
7 * Authors: Gildas Bazin <gbazin@netcourrier.com>
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.
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.
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 *****************************************************************************/
25 /*****************************************************************************
26 * Preamble: This file contains the functions related to the creation of
27 * a window and the handling of its messages (events).
28 *****************************************************************************/
31 #include <errno.h> /* ENOMEM */
32 #include <stdlib.h> /* free() */
33 #include <string.h> /* strerror() */
43 #include "video_output.h"
48 #if defined( _MSC_VER )
55 #include "interface.h"
59 #include "modules_export.h"
61 #include "vout_directx.h"
63 /*****************************************************************************
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 );
71 /*****************************************************************************
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;
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 )
92 boolean_t b_dispatch_msg = TRUE;
96 /* Create a window for the video */
97 /* Creating a window under Windows also initializes the thread's event
99 if( DirectXCreateWindow( p_vout ) )
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 );
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 );
117 while( !p_vout->b_die && !p_vout->p_sys->b_event_thread_die )
120 /* GetMessage will sleep if there's no message in the queue */
121 if( GetMessage( &msg, NULL, 0, 0 ) >= 0 )
123 switch( msg.message )
127 intf_WarnMsg( 3, "vout: vout_Manage WM_CLOSE" );
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;
137 intf_WarnMsg( 3, "vout: vout_Manage WM_MOVE" );
138 if( !p_vout->b_need_render )
140 p_vout->p_sys->i_changes |= VOUT_SIZE_CHANGE;
142 /* don't create a never ending loop */
143 b_dispatch_msg = FALSE;
147 intf_WarnMsg( 3, "vout: vout_Manage WM_APP" );
148 if( !p_vout->b_need_render )
150 p_vout->p_sys->i_changes |= VOUT_SIZE_CHANGE;
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;
160 intf_WarnMsg( 4, "vout: vout_Manage WM_PAINT" );
164 intf_WarnMsg( 4, "vout: vout_Manage WM_ERASEBKGND" );
169 intf_WarnMsg( 4, "vout: vout_Manage WM_MOUSEMOVE" );
170 if( p_vout->p_sys->b_cursor )
172 if( p_vout->p_sys->b_cursor_autohidden )
174 p_vout->p_sys->b_cursor_autohidden = 0;
175 p_vout->p_sys->i_lastmoved = mdate();
180 p_vout->p_sys->i_lastmoved = mdate();
186 intf_WarnMsg( 4, "vout: vout_Manage WM_RBUTTONUP" );
187 p_main->p_intf->b_menu_change = 1;
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" );
199 PostQuitMessage( 0 );
202 TranslateMessage(&msg);
203 b_dispatch_msg = FALSE;
207 intf_WarnMsg( 3, "vout: vout_Manage WM_CHAR" );
212 PostQuitMessage( 0 );
215 case 'f': /* switch to fullscreen */
217 p_vout->p_sys->i_changes |= VOUT_FULLSCREEN_CHANGE;
220 case 'y': /* switch to hard YUV */
222 p_vout->p_sys->i_changes |= VOUT_YUV_CHANGE;
225 case 'c': /* toggle grayscale */
227 p_vout->b_grayscale = ! p_vout->b_grayscale;
228 p_vout->p_sys->i_changes |= VOUT_GRAYSCALE_CHANGE;
231 case 'i': /* toggle info */
233 p_vout->b_info = ! p_vout->b_info;
234 p_vout->p_sys->i_changes |= VOUT_INFO_CHANGE;
237 case 's': /* toggle scaling */
239 p_vout->b_scale = ! p_vout->b_scale;
240 p_vout->p_sys->i_changes |= VOUT_SCALE_CHANGE;
243 case ' ': /* toggle interface */
244 p_vout->b_interface = ! p_vout->b_interface;
245 p_vout->p_sys->i_changes |= VOUT_INTF_CHANGE;
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;
260 if( intf_ProcessKey( p_main->p_intf,
261 (char )msg.wParam ) )
263 intf_DbgMsg( "unhandled key '%c' (%i)",
264 (char)msg.wParam, msg.wParam );
271 intf_WarnMsg( 4, "vout: vout_Manage WM Default %i",
278 /* don't create a never ending loop */
281 TranslateMessage(&msg);
282 DispatchMessage(&msg);
284 b_dispatch_msg = TRUE;
286 } /* if( GetMessage() ) */
289 intf_ErrMsg("vout error: GetMessage failed in DirectXEventThread");
290 p_vout->p_sys->b_event_thread_die = 1;
291 } /* End if( GetMessage() ) */
294 } /* End Main loop */
296 /* Destroy the window */
297 DirectXCloseWindow( p_vout );
299 /* Set thread Status */
300 p_vout->p_sys->i_event_thread_status = THREAD_OVER;
305 /* following functions are local */
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
313 *****************************************************************************/
314 static int DirectXCreateWindow( vout_thread_t *p_vout )
317 WNDCLASSEX wc; /* window class components */
322 intf_WarnMsg( 3, "vout: DirectXCreateWindow" );
324 /* get this module's instance */
325 hInstance = GetModuleHandle(NULL);
327 /* Create a BRUSH that will be used by Windows to paint the window
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
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++ )
342 if( colorkey == GetNearestColor( hdc, colorkey ) )
345 intf_WarnMsg(3,"vout: DirectXCreateWindow background color:%i", colorkey);
346 ReleaseDC( p_vout->p_sys->hwnd, hdc );
348 /* create the actual brush */
349 p_vout->p_sys->hbrush = CreateSolidBrush(colorkey);
350 p_vout->p_sys->i_colorkey = (int)colorkey;
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 */
366 /* register the window class */
367 if (!RegisterClassEx(&wc))
369 intf_WarnMsg( 3, "vout: DirectXCreateWindow register window FAILED" );
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 );
383 /* create the window */
384 p_vout->p_sys->hwnd = CreateWindow("VLC DirectX",/* name of window class */
385 "VLC DirectX", /* window title bar text */
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 */
397 if (p_vout->p_sys->hwnd == NULL) {
398 intf_WarnMsg( 3, "vout: DirectXCreateWindow create window FAILED" );
402 /* now display the window */
403 ShowWindow(p_vout->p_sys->hwnd, SW_SHOW);
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 )
417 intf_WarnMsg( 3, "vout: DirectXCloseWindow" );
418 if( p_vout->p_sys->hwnd != NULL )
420 DestroyWindow( p_vout->p_sys->hwnd);
421 p_vout->p_sys->hwnd = NULL;
424 hInstance = GetModuleHandle(NULL);
425 UnregisterClass( "VLC DirectX", /* class name */
426 hInstance ); /* handle to application instance */
428 /* free window background brush */
429 if( p_vout->p_sys->hwnd != NULL )
431 DeleteObject( p_vout->p_sys->hbrush );
432 p_vout->p_sys->hbrush = NULL;
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 )
455 intf_WarnMsg( 3, "vout: WinProc WM_APP" );
459 intf_WarnMsg( 4, "vout: WinProc WM_ACTIVED" );
463 intf_WarnMsg( 4, "vout: WinProc WM_CREATE" );
466 /* the user wants to close the window */
468 intf_WarnMsg( 4, "vout: WinProc WM_CLOSE" );
472 /* the window has been closed so shut down everything now */
474 intf_WarnMsg( 4, "vout: WinProc WM_DESTROY" );
475 PostQuitMessage( 0 );
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 */
490 intf_WarnMsg( 4, "vout: WinProc WM_MOVE" );
494 intf_WarnMsg( 4, "vout: WinProc WM_SIZE" );
498 intf_WarnMsg( 4, "vout: WinProc WM_MOVING" );
501 case WM_ENTERSIZEMOVE:
502 intf_WarnMsg( 4, "vout: WinProc WM_ENTERSIZEMOVE" );
506 intf_WarnMsg( 4, "vout: WinProc WM_SIZING" );
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);
517 case WM_WINDOWPOSCHANGING:
518 intf_WarnMsg( 3, "vout: WinProc WM_WINDOWPOSCHANGING" );
522 intf_WarnMsg( 4, "vout: WinProc WM_PAINT" );
526 intf_WarnMsg( 4, "vout: WinProc WM_ERASEBKGND" );
530 intf_WarnMsg( 4, "vout: WinProc WM Default %i", message );
535 return DefWindowProc(hwnd, message, wParam, lParam);