1 /*****************************************************************************
2 * vout_events.c: Windows DirectX video output events handler
3 *****************************************************************************
4 * Copyright (C) 2001 VideoLAN
5 * $Id: vout_events.c,v 1.21 2002/06/01 18:04:48 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() */
45 #include "vout_directx.h"
47 /*****************************************************************************
49 *****************************************************************************/
50 static int DirectXCreateWindow( vout_thread_t *p_vout );
51 static void DirectXCloseWindow ( vout_thread_t *p_vout );
52 static void DirectXUpdateRects( vout_thread_t *p_vout );
53 static long FAR PASCAL DirectXEventProc ( HWND hwnd, UINT message,
54 WPARAM wParam, LPARAM lParam );
56 /*****************************************************************************
57 * DirectXEventThread: Create video window & handle its messages
58 *****************************************************************************
59 * This function creates a video window and then enters an infinite loop
60 * that handles the messages sent to that window.
61 * The main goal of this thread is to isolate the Win32 PeekMessage function
62 * because this one can block for a long time.
63 *****************************************************************************/
64 void DirectXEventThread( event_thread_t *p_event )
71 /* Create a window for the video */
72 /* Creating a window under Windows also initializes the thread's event
74 if( DirectXCreateWindow( p_event->p_vout ) )
76 msg_Err( p_event, "out of memory" );
80 /* signal the creation of the window */
81 vlc_thread_ready( p_event );
84 /* GetMessage will sleep if there's no message in the queue */
85 while( !p_event->b_die
86 && GetMessage( &msg, p_event->p_vout->p_sys->hwnd, 0, 0 ) )
88 /* Check if we are asked to exit */
97 if( (abs(GET_X_LPARAM(msg.lParam) - old_mouse_pos.x) > 2 ||
98 (abs(GET_Y_LPARAM(msg.lParam) - old_mouse_pos.y)) > 2 ) )
100 GetCursorPos( &old_mouse_pos );
101 p_event->p_vout->p_sys->i_lastmoved = mdate();
103 if( p_event->p_vout->p_sys->b_cursor_hidden )
105 p_event->p_vout->p_sys->b_cursor_hidden = 0;
111 case WM_VLC_HIDE_MOUSE:
112 GetCursorPos( &old_mouse_pos );
118 intf_thread_t *p_intf;
119 p_intf = vlc_object_find( p_event->p_vlc, VLC_OBJECT_INTF,
123 p_intf->b_menu_change = 1;
124 vlc_object_release( p_intf );
132 case WM_LBUTTONDBLCLK:
133 p_event->p_vout->p_sys->i_changes |= VOUT_FULLSCREEN_CHANGE;
137 /* the key events are first processed here. The next
138 * message processed by this main message loop will be the
139 * char translation of the key event */
140 msg_Dbg( p_event, "WM_KEYDOWN" );
145 /* exit application */
146 p_event->p_vlc->b_die = 1;
149 TranslateMessage(&msg);
157 /* exit application */
158 p_event->p_vlc->b_die = 1;
161 case 'f': /* switch to fullscreen */
163 p_event->p_vout->p_sys->i_changes |= VOUT_FULLSCREEN_CHANGE;
166 case 'c': /* toggle grayscale */
168 p_event->p_vout->b_grayscale = ! p_event->p_vout->b_grayscale;
169 p_event->p_vout->p_sys->i_changes |= VOUT_GRAYSCALE_CHANGE;
172 case 'i': /* toggle info */
174 p_event->p_vout->b_info = ! p_event->p_vout->b_info;
175 p_event->p_vout->p_sys->i_changes |= VOUT_INFO_CHANGE;
178 case 's': /* toggle scaling */
180 p_event->p_vout->b_scale = ! p_event->p_vout->b_scale;
181 p_event->p_vout->p_sys->i_changes |= VOUT_SCALE_CHANGE;
184 case ' ': /* toggle interface */
185 p_event->p_vout->b_interface = ! p_event->p_vout->b_interface;
186 p_event->p_vout->p_sys->i_changes |= VOUT_INTF_CHANGE;
189 case '0': network_ChannelJoin( p_event, 0 ); break;
190 case '1': network_ChannelJoin( p_event, 1 ); break;
191 case '2': network_ChannelJoin( p_event, 2 ); break;
192 case '3': network_ChannelJoin( p_event, 3 ); break;
193 case '4': network_ChannelJoin( p_event, 4 ); break;
194 case '5': network_ChannelJoin( p_event, 5 ); break;
195 case '6': network_ChannelJoin( p_event, 6 ); break;
196 case '7': network_ChannelJoin( p_event, 7 ); break;
197 case '8': network_ChannelJoin( p_event, 8 ); break;
198 case '9': network_ChannelJoin( p_event, 9 ); break;
205 /* Messages we don't handle directly are dispatched to the
206 * window procedure */
207 TranslateMessage(&msg);
208 DispatchMessage(&msg);
213 } /* End Main loop */
215 if( msg.message == WM_QUIT )
217 msg_Warn( p_event, "WM_QUIT... should not happen!!" );
218 p_event->p_vout->p_sys->hwnd = NULL; /* Window already destroyed */
221 msg_Dbg( p_event, "DirectXEventThread Terminating" );
223 /* clear the changes formerly signaled */
224 p_event->p_vout->p_sys->i_changes = 0;
226 DirectXCloseWindow( p_event->p_vout );
230 /* following functions are local */
232 /*****************************************************************************
233 * DirectXCreateWindow: create a window for the video.
234 *****************************************************************************
235 * Before creating a direct draw surface, we need to create a window in which
236 * the video will be displayed. This window will also allow us to capture the
238 *****************************************************************************/
239 static int DirectXCreateWindow( vout_thread_t *p_vout )
242 WNDCLASSEX wc; /* window class components */
246 HICON vlc_icon = NULL;
247 char vlc_path[_MAX_PATH+1];
249 msg_Dbg( p_vout, "DirectXCreateWindow" );
251 /* get this module's instance */
252 hInstance = GetModuleHandle(NULL);
254 /* Create a BRUSH that will be used by Windows to paint the window
256 * This window background is important for us as it will be used by the
257 * graphics card to display the overlay.
258 * This is why we carefully choose the color for this background, the goal
259 * being to choose a color which isn't complete black but nearly. We
260 * obviously don't want to use black as a colorkey for the overlay because
261 * black is one of the most used color and thus would give us undesirable
263 /* the first step is to find the colorkey we want to use. The difficulty
264 * comes from the potential dithering (depends on the display depth)
265 * because we need to know the real RGB value of the chosen colorkey */
267 for( colorkey = 5; colorkey < 0xFF /*all shades of red*/; colorkey++ )
269 if( colorkey == GetNearestColor( hdc, colorkey ) )
272 msg_Dbg( p_vout, "background color: %i", colorkey );
274 /* create the actual brush */
275 p_vout->p_sys->hbrush = CreateSolidBrush(colorkey);
276 p_vout->p_sys->i_rgb_colorkey = (int)colorkey;
278 /* Get the current size of the display and its colour depth */
279 p_vout->p_sys->rect_display.right = GetDeviceCaps( hdc, HORZRES );
280 p_vout->p_sys->rect_display.bottom = GetDeviceCaps( hdc, VERTRES );
281 p_vout->p_sys->i_display_depth = GetDeviceCaps( hdc, BITSPIXEL );
282 msg_Dbg( p_vout, "screen dimensions %ix%i colour depth %i",
283 p_vout->p_sys->rect_display.right,
284 p_vout->p_sys->rect_display.bottom,
285 p_vout->p_sys->i_display_depth );
287 ReleaseDC( p_vout->p_sys->hwnd, hdc );
289 /* Get the Icon from the main app */
291 if( GetModuleFileName( NULL, vlc_path, _MAX_PATH ) )
293 vlc_icon = ExtractIcon( hInstance, vlc_path, 0 );
297 /* fill in the window class structure */
298 wc.cbSize = sizeof(WNDCLASSEX);
299 wc.style = CS_DBLCLKS; /* style: dbl click */
300 wc.lpfnWndProc = (WNDPROC)DirectXEventProc; /* event handler */
301 wc.cbClsExtra = 0; /* no extra class data */
302 wc.cbWndExtra = 0; /* no extra window data */
303 wc.hInstance = hInstance; /* instance */
304 wc.hIcon = vlc_icon; /* load the vlc icon */
305 wc.hCursor = LoadCursor(NULL, IDC_ARROW); /* load a default cursor */
306 wc.hbrBackground = p_vout->p_sys->hbrush; /* background color */
307 wc.lpszMenuName = NULL; /* no menu */
308 wc.lpszClassName = "VLC DirectX"; /* use a special class */
309 wc.hIconSm = vlc_icon; /* load the vlc icon */
311 /* register the window class */
312 if (!RegisterClassEx(&wc))
316 /* free window background brush */
317 if( p_vout->p_sys->hbrush )
319 DeleteObject( p_vout->p_sys->hbrush );
320 p_vout->p_sys->hbrush = NULL;
324 DestroyIcon( vlc_icon );
326 /* Check why it failed. If it's because one already exists then fine */
327 if( !GetClassInfo( hInstance, "VLC DirectX", &wndclass ) )
329 msg_Err( p_vout, "DirectXCreateWindow RegisterClass FAILED" );
334 /* when you create a window you give the dimensions you wish it to have.
335 * Unfortunatly these dimensions will include the borders and title bar.
336 * We use the following function to find out the size of the window
337 * corresponding to the useable surface we want */
338 rect_window.top = 10;
339 rect_window.left = 10;
340 rect_window.right = rect_window.left + p_vout->p_sys->i_window_width;
341 rect_window.bottom = rect_window.top + p_vout->p_sys->i_window_height;
342 AdjustWindowRect( &rect_window, WS_OVERLAPPEDWINDOW|WS_SIZEBOX, 0 );
344 /* create the window */
345 p_vout->p_sys->hwnd = CreateWindow("VLC DirectX",/* name of window class */
346 "VLC DirectX", /* window title bar text */
348 | WS_SIZEBOX, /* window style */
349 CW_USEDEFAULT, /* default X coordinate */
350 0, /* default Y coordinate */
351 rect_window.right - rect_window.left, /* window width */
352 rect_window.bottom - rect_window.top, /* window height */
353 NULL, /* no parent window */
354 NULL, /* no menu in this window */
355 hInstance, /* handle of this program instance */
356 NULL); /* no additional arguments */
358 if (p_vout->p_sys->hwnd == NULL) {
359 msg_Warn( p_vout, "DirectXCreateWindow create window FAILED" );
363 /* store a p_vout pointer into the window local storage (for later use
364 * in DirectXEventProc).
365 * We need to use SetWindowLongPtr when it is available in mingw */
366 SetWindowLong( p_vout->p_sys->hwnd, GWL_USERDATA, (LONG)p_vout );
368 /* now display the window */
369 ShowWindow(p_vout->p_sys->hwnd, SW_SHOW);
374 /*****************************************************************************
375 * DirectXCloseWindow: close the window created by DirectXCreateWindow
376 *****************************************************************************
377 * This function returns all resources allocated by DirectXCreateWindow.
378 *****************************************************************************/
379 static void DirectXCloseWindow( vout_thread_t *p_vout )
381 msg_Dbg( p_vout, "DirectXCloseWindow" );
383 if( p_vout->p_sys->hwnd != NULL )
385 DestroyWindow( p_vout->p_sys->hwnd );
386 p_vout->p_sys->hwnd = NULL;
389 /* We don't unregister the Window Class because it could lead to race
390 * conditions and it will be done anyway by the system when the app will
394 /*****************************************************************************
395 * DirectXUpdateRects:
396 *****************************************************************************
397 * This function is called when the window position and size is changed, and
398 * its job is to update the source and destination RECTs used to display the
400 *****************************************************************************/
401 static void DirectXUpdateRects( vout_thread_t *p_vout )
403 int i_width, i_height, i_x, i_y;
405 #define rect_src p_vout->p_sys->rect_src
406 #define rect_src_clipped p_vout->p_sys->rect_src_clipped
407 #define rect_dest p_vout->p_sys->rect_dest
408 #define rect_dest_clipped p_vout->p_sys->rect_dest_clipped
409 #define rect_display p_vout->p_sys->rect_display
411 vout_PlacePicture( p_vout, p_vout->p_sys->i_window_width,
412 p_vout->p_sys->i_window_height,
413 &i_x, &i_y, &i_width, &i_height );
415 /* Destination image position and dimensions */
416 rect_dest.left = i_x + p_vout->p_sys->i_window_x;
417 rect_dest.top = i_y + p_vout->p_sys->i_window_y;
418 rect_dest.right = rect_dest.left + i_width;
419 rect_dest.bottom = rect_dest.top + i_height;
422 /* UpdateOverlay directdraw function doesn't automatically clip to the
423 * display size so we need to do it otherwise it will fails */
425 /* Clip the destination window */
426 IntersectRect( &rect_dest_clipped, &rect_dest, &rect_display );
429 msg_Dbg( p_vout, "DirectXUpdateRects image_dst_clipped coords:"
431 rect_dest_clipped.left, rect_dest_clipped.top,
432 rect_dest_clipped.right, rect_dest_clipped.bottom );
435 /* the 2 following lines are to fix a bug when clicking on the desktop */
436 if( (rect_dest_clipped.right - rect_dest_clipped.left)==0 ||
437 (rect_dest_clipped.bottom - rect_dest_clipped.top)==0 )
439 SetRectEmpty( &rect_src_clipped );
443 /* src image dimensions */
446 rect_src.right = p_vout->render.i_width;
447 rect_src.bottom = p_vout->render.i_height;
449 /* Clip the source image */
450 rect_src_clipped.left = (rect_dest_clipped.left - rect_dest.left) *
451 p_vout->render.i_width / (rect_dest.right - rect_dest.left);
452 rect_src_clipped.right = p_vout->render.i_width -
453 (rect_dest.right - rect_dest_clipped.right) * p_vout->render.i_width /
454 (rect_dest.right - rect_dest.left);
455 rect_src_clipped.top = (rect_dest_clipped.top - rect_dest.top) *
456 p_vout->render.i_height / (rect_dest.bottom - rect_dest.top);
457 rect_src_clipped.bottom = p_vout->render.i_height -
458 (rect_dest.bottom - rect_dest_clipped.bottom) * p_vout->render.i_height /
459 (rect_dest.bottom - rect_dest.top);
462 msg_Dbg( p_vout, "DirectXUpdateRects image_src_clipped"
463 " coords: %i,%i,%i,%i",
464 rect_src_clipped.left, rect_src_clipped.top,
465 rect_src_clipped.right, rect_src_clipped.bottom );
469 #undef rect_src_clipped
471 #undef rect_dest_clipped
475 /*****************************************************************************
476 * DirectXEventProc: This is the window event processing function.
477 *****************************************************************************
478 * On Windows, when you create a window you have to attach an event processing
479 * function to it. The aim of this function is to manage "Queued Messages" and
480 * "Nonqueued Messages".
481 * Queued Messages are those picked up and retransmitted by vout_Manage
482 * (using the GetMessage and DispatchMessage functions).
483 * Nonqueued Messages are those that Windows will send directly to this
484 * procedure (like WM_DESTROY, WM_WINDOWPOSCHANGED...)
485 *****************************************************************************/
486 static long FAR PASCAL DirectXEventProc( HWND hwnd, UINT message,
487 WPARAM wParam, LPARAM lParam )
489 vout_thread_t *p_vout =
490 (vout_thread_t *)GetWindowLong( hwnd, GWL_USERDATA );
495 case WM_WINDOWPOSCHANGED:
500 /* update the window position */
503 ClientToScreen( hwnd, &point_window );
504 p_vout->p_sys->i_window_x = point_window.x;
505 p_vout->p_sys->i_window_y = point_window.y;
507 /* update the window size */
508 GetClientRect( hwnd, &rect_window );
509 p_vout->p_sys->i_window_width = rect_window.right;
510 p_vout->p_sys->i_window_height = rect_window.bottom;
512 DirectXUpdateRects( p_vout );
513 if( p_vout->p_sys->b_using_overlay &&
514 !p_vout->p_sys->b_event_thread_die )
515 DirectXUpdateOverlay( p_vout );
517 /* signal the size change */
518 if( !p_vout->p_sys->b_using_overlay &&
519 !p_vout->p_sys->b_event_thread_die )
520 p_vout->p_sys->i_changes |= VOUT_SIZE_CHANGE;
526 /* the user wants to close the window */
528 msg_Dbg( p_vout, "WinProc WM_CLOSE" );
529 /* exit application */
530 p_vout->p_vlc->b_die = 1;
534 /* the window has been closed so shut down everything now */
536 msg_Dbg( p_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 msg_Dbg( p_vout, "WinProc WM_SYSCOMMAND" );
548 return 0; /* this stops them from happening */
553 if( !p_vout->p_sys->b_using_overlay )
555 /* We want to eliminate unnecessary background redraws which create
556 * an annoying flickering */
557 int i_width, i_height, i_x, i_y;
559 GetClipBox( (HDC)wParam, &rect_temp );
561 msg_Dbg( p_vout, "WinProc WM_ERASEBKGND %i,%i,%i,%i",
562 rect_temp.left, rect_temp.top,
563 rect_temp.right, rect_temp.bottom );
565 vout_PlacePicture( p_vout, p_vout->p_sys->i_window_width,
566 p_vout->p_sys->i_window_height,
567 &i_x, &i_y, &i_width, &i_height );
568 ExcludeClipRect( (HDC)wParam, i_x, i_y,
569 i_x + i_width, i_y + i_height );
574 //msg_Dbg( p_vout, "WinProc WM Default %i", message );
578 return DefWindowProc(hwnd, message, wParam, lParam);