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 $
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 *****************************************************************************/
29 #include <errno.h> /* ENOMEM */
30 #include <stdlib.h> /* free() */
31 #include <string.h> /* strerror() */
33 #include <videolan/vlc.h>
38 #include "video_output.h"
46 #include "interface.h"
48 #include "vout_directx.h"
50 /*****************************************************************************
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 );
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 )
73 /* Create a window for the video */
74 /* Creating a window under Windows also initializes the thread's event
76 if( DirectXCreateWindow( p_vout ) )
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 );
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 );
94 /* GetMessage will sleep if there's no message in the queue */
95 while( GetMessage( &msg, p_vout->p_sys->hwnd, 0, 0 ) )
98 /* Check if we are asked to exit */
99 if( p_vout->b_die || p_vout->p_sys->b_event_thread_die )
102 switch( msg.message )
106 if( p_vout->p_sys->b_cursor )
108 if( p_vout->p_sys->b_cursor_autohidden )
110 p_vout->p_sys->b_cursor_autohidden = 0;
111 p_vout->p_sys->i_lastmoved = mdate();
116 p_vout->p_sys->i_lastmoved = mdate();
119 DispatchMessage(&msg);
123 intf_WarnMsg( 4, "vout: vout_Manage WM_RBUTTONUP" );
124 p_main->p_intf->b_menu_change = 1;
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" );
136 /* exit application */
137 p_main->p_intf->b_die = 1;
140 TranslateMessage(&msg);
144 intf_WarnMsg( 3, "vout: vout_Manage WM_CHAR" );
149 /* exit application */
150 p_main->p_intf->b_die = 1;
153 case 'f': /* switch to fullscreen */
155 p_vout->p_sys->i_changes |= VOUT_FULLSCREEN_CHANGE;
158 case 'c': /* toggle grayscale */
160 p_vout->b_grayscale = ! p_vout->b_grayscale;
161 p_vout->p_sys->i_changes |= VOUT_GRAYSCALE_CHANGE;
164 case 'i': /* toggle info */
166 p_vout->b_info = ! p_vout->b_info;
167 p_vout->p_sys->i_changes |= VOUT_INFO_CHANGE;
170 case 's': /* toggle scaling */
172 p_vout->b_scale = ! p_vout->b_scale;
173 p_vout->p_sys->i_changes |= VOUT_SCALE_CHANGE;
176 case ' ': /* toggle interface */
177 p_vout->b_interface = ! p_vout->b_interface;
178 p_vout->p_sys->i_changes |= VOUT_INTF_CHANGE;
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;
197 /* Messages we don't handle directly are dispatched to the
198 * window procedure */
200 intf_WarnMsg( 5, "vout: vout_Manage unhandled message",
203 TranslateMessage(&msg);
204 DispatchMessage(&msg);
209 } /* End Main loop */
211 if( msg.message == WM_QUIT )
213 intf_WarnMsg( 3, "vout: DirectXEventThread WM_QUIT... "
214 "shouldn't happen!!" );
215 p_vout->p_sys->hwnd = NULL; /* Window already destroyed */
218 intf_WarnMsg( 3, "vout: DirectXEventThread Terminating" );
220 /* clear the changes formerly signaled */
221 p_vout->p_sys->i_changes = 0;
223 DirectXCloseWindow( p_vout );
227 /* following functions are local */
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
235 *****************************************************************************/
236 static int DirectXCreateWindow( vout_thread_t *p_vout )
239 WNDCLASSEX wc; /* window class components */
243 HICON vlc_icon = NULL;
244 char vlc_path[_MAX_PATH+1];
246 intf_WarnMsg( 3, "vout: DirectXCreateWindow" );
248 /* get this module's instance */
249 hInstance = GetModuleHandle(NULL);
251 /* Create a BRUSH that will be used by Windows to paint the window
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
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 */
264 for( colorkey = 5; colorkey < 0xFF /*all shades of red*/; colorkey++ )
266 if( colorkey == GetNearestColor( hdc, colorkey ) )
269 intf_WarnMsg(3,"vout: DirectXCreateWindow background color:%i", colorkey);
271 /* create the actual brush */
272 p_vout->p_sys->hbrush = CreateSolidBrush(colorkey);
273 p_vout->p_sys->i_rgb_colorkey = (int)colorkey;
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 );
284 ReleaseDC( p_vout->p_sys->hwnd, hdc );
286 /* Get the Icon from the main app */
287 if( GetModuleFileName( NULL, vlc_path, _MAX_PATH ) )
289 vlc_icon = ExtractIcon( hInstance, vlc_path, 0 );
292 vlc_icon = LoadIcon( NULL, IDI_APPLICATION );
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 */
309 /* register the window class */
310 if (!RegisterClassEx(&wc))
312 /* Check why it failed. If it's because one already exists then fine */
314 if( !GetClassInfo( hInstance, "VLC DirectX", &wndclass ) )
316 intf_ErrMsg( "vout: DirectXCreateWindow RegisterClass FAILED" );
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 );
331 /* create the window */
332 p_vout->p_sys->hwnd = CreateWindow("VLC DirectX",/* name of window class */
333 "VLC DirectX", /* window title bar text */
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 */
345 if (p_vout->p_sys->hwnd == NULL) {
346 intf_WarnMsg( 3, "vout: DirectXCreateWindow create window FAILED" );
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 );
355 /* now display the window */
356 ShowWindow(p_vout->p_sys->hwnd, SW_SHOW);
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 )
368 intf_WarnMsg( 3, "vout: DirectXCloseWindow" );
369 if( p_vout->p_sys->hwnd != NULL )
371 DestroyWindow( p_vout->p_sys->hwnd);
372 p_vout->p_sys->hwnd = NULL;
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
379 /* free window background brush */
380 if( p_vout->p_sys->hbrush != NULL )
382 DeleteObject( p_vout->p_sys->hbrush );
383 p_vout->p_sys->hbrush = NULL;
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
393 *****************************************************************************/
394 static void DirectXUpdateRects( vout_thread_t *p_vout )
396 int i_width, i_height, i_x, i_y;
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
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 );
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;
415 /* UpdateOverlay directdraw function doesn't automatically clip to the
416 * display size so we need to do it otherwise it will fails */
418 /* Clip the destination window */
419 IntersectRect( &rect_dest_clipped, &rect_dest, &rect_display );
421 intf_WarnMsg( 3, "vout: DirectXUpdateRects image_dst_clipped coords:"
423 rect_dest_clipped.left, rect_dest_clipped.top,
424 rect_dest_clipped.right, rect_dest_clipped.bottom);
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 )
430 SetRectEmpty( &rect_src_clipped );
434 /* src image dimensions */
437 rect_src.right = p_vout->render.i_width;
438 rect_src.bottom = p_vout->render.i_height;
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);
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);
458 #undef rect_src_clipped
460 #undef rect_dest_clipped
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 )
478 vout_thread_t *p_vout;
483 case WM_WINDOWPOSCHANGED:
488 p_vout = (vout_thread_t *)GetWindowLong( hwnd, GWL_USERDATA );
490 /* update the window position */
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;
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 );
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 );
511 /* signal the size change */
512 p_vout->p_sys->i_changes |= VOUT_SIZE_CHANGE;
519 intf_WarnMsg( 4, "vout: WinProc WM_ACTIVE" );
523 intf_WarnMsg( 4, "vout: WinProc WM_CREATE" );
526 /* the user wants to close the window */
528 intf_WarnMsg( 4, "vout: WinProc WM_CLOSE" );
529 /* exit application */
530 p_main->p_intf->b_die = 1;
534 /* the window has been closed so shut down everything now */
536 intf_WarnMsg( 4, "vout: WinProc WM_DESTROY" );
537 /* just destroy the window */
538 PostQuitMessage( 0 );
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 */
554 intf_WarnMsg( 4, "vout: WinProc WM_PAINT" );
559 p_vout = (vout_thread_t *)GetWindowLong( hwnd, GWL_USERDATA );
560 if( !p_vout->p_sys->b_using_overlay )
562 /* We want to eliminate unnecessary background redraws which create
563 * an annoying flickering */
564 int i_width, i_height, i_x, i_y;
566 GetClipBox( (HDC)wParam, &rect_temp );
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 );
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 );
582 intf_WarnMsg( 4, "vout: WinProc WM Default %i", message );
587 return DefWindowProc(hwnd, message, wParam, lParam);