1 /*****************************************************************************
2 * events.c: Windows DirectX video output events handler
3 *****************************************************************************
4 * Copyright (C) 2001 VideoLAN
5 * $Id: events.c,v 1.17 2003/05/26 19:26:53 gbazin 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() */
35 #include <vlc/input.h>
46 /*****************************************************************************
48 *****************************************************************************/
49 static int DirectXCreateWindow( vout_thread_t *p_vout );
50 static void DirectXCloseWindow ( vout_thread_t *p_vout );
51 static long FAR PASCAL DirectXEventProc ( HWND hwnd, UINT message,
52 WPARAM wParam, LPARAM lParam );
54 /*****************************************************************************
55 * DirectXEventThread: Create video window & handle its messages
56 *****************************************************************************
57 * This function creates a video window and then enters an infinite loop
58 * that handles the messages sent to that window.
59 * The main goal of this thread is to isolate the Win32 PeekMessage function
60 * because this one can block for a long time.
61 *****************************************************************************/
62 void DirectXEventThread( event_thread_t *p_event )
65 POINT old_mouse_pos = {0,0};
67 int i_width, i_height, i_x, i_y;
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" );
77 p_event->b_dead = VLC_TRUE;
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 vout_PlacePicture( p_event->p_vout,
98 p_event->p_vout->p_sys->i_window_width,
99 p_event->p_vout->p_sys->i_window_height,
100 &i_x, &i_y, &i_width, &i_height );
102 val.i_int = ( GET_X_LPARAM(msg.lParam) - i_x )
103 * p_event->p_vout->render.i_width / i_width;
104 var_Set( p_event->p_vout, "mouse-x", val );
105 val.i_int = ( GET_Y_LPARAM(msg.lParam) - i_y )
106 * p_event->p_vout->render.i_height / i_height;
107 var_Set( p_event->p_vout, "mouse-y", val );
109 val.b_bool = VLC_TRUE;
110 var_Set( p_event->p_vout, "mouse-moved", val );
112 if( (abs(GET_X_LPARAM(msg.lParam) - old_mouse_pos.x) > 2 ||
113 (abs(GET_Y_LPARAM(msg.lParam) - old_mouse_pos.y)) > 2 ) )
115 GetCursorPos( &old_mouse_pos );
116 p_event->p_vout->p_sys->i_lastmoved = mdate();
118 if( p_event->p_vout->p_sys->b_cursor_hidden )
120 p_event->p_vout->p_sys->b_cursor_hidden = 0;
126 case WM_VLC_HIDE_MOUSE:
127 GetCursorPos( &old_mouse_pos );
132 var_Get( p_event->p_vout, "mouse-button-down", &val );
134 var_Set( p_event->p_vout, "mouse-button-down", val );
138 var_Get( p_event->p_vout, "mouse-button-down", &val );
140 var_Set( p_event->p_vout, "mouse-button-down", val );
142 val.b_bool = VLC_TRUE;
143 var_Set( p_event->p_vout, "mouse-clicked", val );
146 case WM_LBUTTONDBLCLK:
147 p_event->p_vout->p_sys->i_changes |= VOUT_FULLSCREEN_CHANGE;
151 var_Get( p_event->p_vout, "mouse-button-down", &val );
153 var_Set( p_event->p_vout, "mouse-button-down", val );
157 var_Get( p_event->p_vout, "mouse-button-down", &val );
159 var_Set( p_event->p_vout, "mouse-button-down", val );
163 var_Get( p_event->p_vout, "mouse-button-down", &val );
165 var_Set( p_event->p_vout, "mouse-button-down", val );
169 var_Get( p_event->p_vout, "mouse-button-down", &val );
171 var_Set( p_event->p_vout, "mouse-button-down", val );
173 playlist_t *p_playlist =
174 vlc_object_find( p_event, VLC_OBJECT_PLAYLIST,
176 if( p_playlist != NULL )
179 var_Set( p_playlist, "intf-popupmenu", val );
180 vlc_object_release( p_playlist );
186 /* the key events are first processed here. The next
187 * message processed by this main message loop will be the
188 * char translation of the key event */
189 msg_Dbg( p_event, "WM_KEYDOWN" );
193 if( p_event->p_vout->b_fullscreen )
195 p_event->p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
199 /* exit application */
200 p_event->p_vout->p_vlc->b_die = VLC_TRUE;
206 playlist_t *p_playlist =
207 vlc_object_find( p_event, VLC_OBJECT_PLAYLIST,
209 if( p_playlist != NULL )
212 var_Set( p_playlist, "intf-popupmenu", val );
213 vlc_object_release( p_playlist );
219 input_Seek( p_event->p_vout, -5,
220 INPUT_SEEK_SECONDS | INPUT_SEEK_CUR );
223 input_Seek( p_event->p_vout, 5,
224 INPUT_SEEK_SECONDS | INPUT_SEEK_CUR );
227 input_Seek( p_event->p_vout, 60,
228 INPUT_SEEK_SECONDS | INPUT_SEEK_CUR );
231 input_Seek( p_event->p_vout, -60,
232 INPUT_SEEK_SECONDS | INPUT_SEEK_CUR );
235 input_Seek( p_event->p_vout, 0,
236 INPUT_SEEK_BYTES | INPUT_SEEK_SET );
239 input_Seek( p_event->p_vout, 0,
240 INPUT_SEEK_BYTES | INPUT_SEEK_END );
243 input_Seek( p_event->p_vout, 900,
244 INPUT_SEEK_SECONDS | INPUT_SEEK_CUR );
247 input_Seek( p_event->p_vout, -900,
248 INPUT_SEEK_SECONDS | INPUT_SEEK_CUR );
251 input_SetStatus( p_event->p_vout, INPUT_STATUS_PAUSE );
254 TranslateMessage(&msg);
262 /* exit application */
263 p_event->p_vlc->b_die = VLC_TRUE;
266 case 'f': /* switch to fullscreen */
268 p_event->p_vout->p_sys->i_changes |= VOUT_FULLSCREEN_CHANGE;
276 /* Messages we don't handle directly are dispatched to the
277 * window procedure */
278 TranslateMessage(&msg);
279 DispatchMessage(&msg);
284 } /* End Main loop */
286 if( msg.message == WM_QUIT )
288 msg_Warn( p_event, "WM_QUIT... should not happen!!" );
289 p_event->p_vout->p_sys->hwnd = NULL; /* Window already destroyed */
292 msg_Dbg( p_event, "DirectXEventThread terminating" );
294 /* clear the changes formerly signaled */
295 p_event->p_vout->p_sys->i_changes = 0;
297 DirectXCloseWindow( p_event->p_vout );
301 /* following functions are local */
303 /*****************************************************************************
304 * DirectXCreateWindow: create a window for the video.
305 *****************************************************************************
306 * Before creating a direct draw surface, we need to create a window in which
307 * the video will be displayed. This window will also allow us to capture the
309 *****************************************************************************/
310 static int DirectXCreateWindow( vout_thread_t *p_vout )
320 msg_Dbg( p_vout, "DirectXCreateWindow" );
322 /* Get this module's instance */
323 hInstance = GetModuleHandle(NULL);
325 /* Create a BRUSH that will be used by Windows to paint the window
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
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 */
338 for( colorkey = 0x0a; colorkey < 0xff /* all shades of red */; colorkey++ )
340 if( colorkey == GetNearestColor( hdc, colorkey ) )
345 msg_Dbg( p_vout, "background color: %i", colorkey );
347 /* Create the actual brush */
348 p_vout->p_sys->hbrush = CreateSolidBrush(colorkey);
349 p_vout->p_sys->i_rgb_colorkey = (int)colorkey;
351 /* Get the current size of the display and its colour depth */
352 p_vout->p_sys->rect_display.right = GetDeviceCaps( hdc, HORZRES );
353 p_vout->p_sys->rect_display.bottom = GetDeviceCaps( hdc, VERTRES );
354 p_vout->p_sys->i_display_depth = GetDeviceCaps( hdc, BITSPIXEL );
355 msg_Dbg( p_vout, "screen dimensions %ix%i colour depth %i",
356 p_vout->p_sys->rect_display.right,
357 p_vout->p_sys->rect_display.bottom,
358 p_vout->p_sys->i_display_depth );
360 ReleaseDC( NULL, hdc );
362 /* If an external window was specified, we'll draw in it. */
363 var_Get( p_vout->p_vlc, "drawable", &val );
364 p_vout->p_sys->hparent = p_vout->p_sys->hwnd =
365 val.i_int ? (void*)(ptrdiff_t) val.i_int : NULL;
367 if( p_vout->p_sys->hparent )
369 msg_Dbg( p_vout, "using external window %p\n", p_vout->p_sys->hwnd );
371 /* Set stuff in the window that we can not put directly in
372 * a class (see below). */
373 SetClassLong( p_vout->p_sys->hwnd,
374 GCL_STYLE, CS_DBLCLKS );
375 SetClassLong( p_vout->p_sys->hwnd,
376 GCL_HBRBACKGROUND, (LONG)p_vout->p_sys->hbrush );
377 SetClassLong( p_vout->p_sys->hwnd,
378 GCL_HCURSOR, (LONG)LoadCursor(NULL, IDC_ARROW) );
379 /* Store a p_vout pointer into the window local storage (for later
380 * use in DirectXEventProc). */
381 SetWindowLong( p_vout->p_sys->hwnd, GWL_USERDATA, (LONG)p_vout );
383 p_vout->p_sys->pf_wndproc =
384 (WNDPROC)SetWindowLong( p_vout->p_sys->hwnd,
385 GWL_WNDPROC, (LONG)DirectXEventProc );
387 /* Blam! Erase everything that might have been there. */
388 RedrawWindow( p_vout->p_sys->hwnd, NULL, NULL,
389 RDW_INVALIDATE | RDW_ERASE );
393 WNDCLASSEX wc; /* window class components */
394 HICON vlc_icon = NULL;
395 char vlc_path[MAX_PATH+1];
397 /* Get the Icon from the main app */
399 if( GetModuleFileName( NULL, vlc_path, MAX_PATH ) )
401 vlc_icon = ExtractIcon( hInstance, vlc_path, 0 );
404 /* Fill in the window class structure */
405 wc.cbSize = sizeof(WNDCLASSEX);
406 wc.style = CS_DBLCLKS; /* style: dbl click */
407 wc.lpfnWndProc = (WNDPROC)DirectXEventProc; /* event handler */
408 wc.cbClsExtra = 0; /* no extra class data */
409 wc.cbWndExtra = 0; /* no extra window data */
410 wc.hInstance = hInstance; /* instance */
411 wc.hIcon = vlc_icon; /* load the vlc big icon */
412 wc.hCursor = LoadCursor(NULL, IDC_ARROW); /* default cursor */
413 wc.hbrBackground = p_vout->p_sys->hbrush; /* background color */
414 wc.lpszMenuName = NULL; /* no menu */
415 wc.lpszClassName = "VLC DirectX"; /* use a special class */
416 wc.hIconSm = vlc_icon; /* load the vlc small icon */
418 /* Register the window class */
419 if( !RegisterClassEx(&wc) )
423 /* Free window background brush */
424 if( p_vout->p_sys->hbrush )
426 DeleteObject( p_vout->p_sys->hbrush );
427 p_vout->p_sys->hbrush = NULL;
432 DestroyIcon( vlc_icon );
435 /* Check why it failed. If it's because one already exists
436 * then fine, otherwise return with an error. */
437 if( !GetClassInfo( hInstance, "VLC DirectX", &wndclass ) )
439 msg_Err( p_vout, "DirectXCreateWindow RegisterClass FAILED" );
444 /* When you create a window you give the dimensions you wish it to
445 * have. Unfortunatly these dimensions will include the borders and
446 * titlebar. We use the following function to find out the size of
447 * the window corresponding to the useable surface we want */
448 rect_window.top = 10;
449 rect_window.left = 10;
450 rect_window.right = rect_window.left + p_vout->p_sys->i_window_width;
451 rect_window.bottom = rect_window.top + p_vout->p_sys->i_window_height;
452 AdjustWindowRect( &rect_window, WS_OVERLAPPEDWINDOW|WS_SIZEBOX, 0 );
454 /* Create the window */
455 p_vout->p_sys->hwnd =
456 CreateWindow( "VLC DirectX", /* name of window class */
457 VOUT_TITLE " (DirectX Output)", /* window title bar text */
458 WS_OVERLAPPEDWINDOW | WS_SIZEBOX, /* window style */
459 CW_USEDEFAULT, /* default X coordinate */
460 0, /* default Y coordinate */
461 rect_window.right - rect_window.left, /* window width */
462 rect_window.bottom - rect_window.top, /* window height */
463 NULL, /* no parent window */
464 NULL, /* no menu in this window */
465 hInstance, /* handle of this program instance */
466 (LPVOID)p_vout ); /* send p_vout to WM_CREATE */
468 if( !p_vout->p_sys->hwnd )
470 msg_Warn( p_vout, "DirectXCreateWindow create window FAILED" );
475 /* Append a "Always On Top" entry in the system menu */
476 hMenu = GetSystemMenu( p_vout->p_sys->hwnd, FALSE );
477 AppendMenu( hMenu, MF_SEPARATOR, 0, "" );
478 AppendMenu( hMenu, MF_STRING | MF_UNCHECKED,
479 IDM_TOGGLE_ON_TOP, "Always on &Top" );
481 /* Now display the window */
482 ShowWindow( p_vout->p_sys->hwnd, SW_SHOW );
487 /*****************************************************************************
488 * DirectXCloseWindow: close the window created by DirectXCreateWindow
489 *****************************************************************************
490 * This function returns all resources allocated by DirectXCreateWindow.
491 *****************************************************************************/
492 static void DirectXCloseWindow( vout_thread_t *p_vout )
494 msg_Dbg( p_vout, "DirectXCloseWindow" );
496 if( p_vout->p_sys->hwnd && !p_vout->p_sys->hparent )
498 DestroyWindow( p_vout->p_sys->hwnd );
501 p_vout->p_sys->hwnd = NULL;
503 /* We don't unregister the Window Class because it could lead to race
504 * conditions and it will be done anyway by the system when the app will
508 /*****************************************************************************
509 * DirectXUpdateRects: update clipping rectangles
510 *****************************************************************************
511 * This function is called when the window position or size are changed, and
512 * its job is to update the source and destination RECTs used to display the
514 *****************************************************************************/
515 void DirectXUpdateRects( vout_thread_t *p_vout, vlc_bool_t b_force )
517 #define rect_src p_vout->p_sys->rect_src
518 #define rect_src_clipped p_vout->p_sys->rect_src_clipped
519 #define rect_dest p_vout->p_sys->rect_dest
520 #define rect_dest_clipped p_vout->p_sys->rect_dest_clipped
522 int i_width, i_height, i_x, i_y;
527 /* Retrieve the window size */
528 GetClientRect( p_vout->p_sys->hwnd, &rect );
530 /* Retrieve the window position */
531 point.x = point.y = 0;
532 ClientToScreen( p_vout->p_sys->hwnd, &point );
534 /* If nothing changed, we can return */
536 && p_vout->p_sys->i_window_width == rect.right
537 && p_vout->p_sys->i_window_height == rect.bottom
538 && p_vout->p_sys->i_window_x == point.x
539 && p_vout->p_sys->i_window_y == point.y )
544 /* Update the window position and size */
545 p_vout->p_sys->i_window_x = point.x;
546 p_vout->p_sys->i_window_y = point.y;
547 p_vout->p_sys->i_window_width = rect.right;
548 p_vout->p_sys->i_window_height = rect.bottom;
550 vout_PlacePicture( p_vout, rect.right, rect.bottom,
551 &i_x, &i_y, &i_width, &i_height );
553 /* Destination image position and dimensions */
554 rect_dest.left = point.x + i_x;
555 rect_dest.right = rect_dest.left + i_width;
556 rect_dest.top = point.y + i_y;
557 rect_dest.bottom = rect_dest.top + i_height;
560 /* UpdateOverlay directdraw function doesn't automatically clip to the
561 * display size so we need to do it otherwise it will fail */
563 /* Clip the destination window */
564 IntersectRect( &rect_dest_clipped,
566 &p_vout->p_sys->rect_display );
569 msg_Dbg( p_vout, "DirectXUpdateRects image_dst_clipped coords:"
571 rect_dest_clipped.left, rect_dest_clipped.top,
572 rect_dest_clipped.right, rect_dest_clipped.bottom );
575 /* the 2 following lines are to fix a bug when clicking on the desktop */
576 if( (rect_dest_clipped.right - rect_dest_clipped.left)==0 ||
577 (rect_dest_clipped.bottom - rect_dest_clipped.top)==0 )
579 SetRectEmpty( &rect_src_clipped );
583 /* src image dimensions */
586 rect_src.right = p_vout->render.i_width;
587 rect_src.bottom = p_vout->render.i_height;
589 /* Clip the source image */
590 rect_src_clipped.left = (rect_dest_clipped.left - rect_dest.left) *
591 p_vout->render.i_width / (rect_dest.right - rect_dest.left);
592 rect_src_clipped.right = p_vout->render.i_width -
593 (rect_dest.right - rect_dest_clipped.right) * p_vout->render.i_width /
594 (rect_dest.right - rect_dest.left);
595 rect_src_clipped.top = (rect_dest_clipped.top - rect_dest.top) *
596 p_vout->render.i_height / (rect_dest.bottom - rect_dest.top);
597 rect_src_clipped.bottom = p_vout->render.i_height -
598 (rect_dest.bottom - rect_dest_clipped.bottom) * p_vout->render.i_height /
599 (rect_dest.bottom - rect_dest.top);
602 msg_Dbg( p_vout, "DirectXUpdateRects image_src_clipped"
603 " coords: %i,%i,%i,%i",
604 rect_src_clipped.left, rect_src_clipped.top,
605 rect_src_clipped.right, rect_src_clipped.bottom );
608 /* Signal the size change */
609 if( !p_vout->p_sys->p_event->b_die )
611 if( p_vout->p_sys->b_using_overlay )
613 DirectXUpdateOverlay( p_vout );
617 p_vout->p_sys->i_changes |= VOUT_SIZE_CHANGE;
622 #undef rect_src_clipped
624 #undef rect_dest_clipped
627 /*****************************************************************************
628 * DirectXEventProc: This is the window event processing function.
629 *****************************************************************************
630 * On Windows, when you create a window you have to attach an event processing
631 * function to it. The aim of this function is to manage "Queued Messages" and
632 * "Nonqueued Messages".
633 * Queued Messages are those picked up and retransmitted by vout_Manage
634 * (using the GetMessage and DispatchMessage functions).
635 * Nonqueued Messages are those that Windows will send directly to this
636 * procedure (like WM_DESTROY, WM_WINDOWPOSCHANGED...)
637 *****************************************************************************/
638 static long FAR PASCAL DirectXEventProc( HWND hwnd, UINT message,
639 WPARAM wParam, LPARAM lParam )
641 vout_thread_t *p_vout;
643 if( message == WM_CREATE )
645 /* Store p_vout for future use */
646 p_vout = (vout_thread_t *)((CREATESTRUCT *)lParam)->lpCreateParams;
647 SetWindowLong( hwnd, GWL_USERDATA, (LONG)p_vout );
651 p_vout = (vout_thread_t *)GetWindowLong( hwnd, GWL_USERDATA );
657 case WM_WINDOWPOSCHANGED:
658 DirectXUpdateRects( p_vout, VLC_TRUE );
661 /* the user wants to close the window */
663 msg_Dbg( p_vout, "WinProc WM_CLOSE" );
664 /* exit application */
665 p_vout->p_vlc->b_die = VLC_TRUE;
668 /* the window has been closed so shut down everything now */
670 msg_Dbg( p_vout, "WinProc WM_DESTROY" );
671 /* just destroy the window */
672 PostQuitMessage( 0 );
678 case SC_SCREENSAVE: /* catch the screensaver */
679 case SC_MONITORPOWER: /* catch the monitor turn-off */
680 msg_Dbg( p_vout, "WinProc WM_SYSCOMMAND" );
681 return 0; /* this stops them from happening */
682 case IDM_TOGGLE_ON_TOP: /* toggle the "on top" status */
685 msg_Dbg( p_vout, "WinProc WM_SYSCOMMAND: IDM_TOGGLE_ON_TOP");
687 /* Get the current value... */
688 if( var_Get( p_vout, "directx-on-top", &val ) < 0 )
690 /* ...and change it */
691 val.b_bool = !val.b_bool;
692 var_Set( p_vout, "directx-on-top", val );
700 if( !p_vout->p_sys->b_using_overlay )
702 /* We want to eliminate unnecessary background redraws which create
703 * an annoying flickering */
704 int i_width, i_height, i_x, i_y;
706 GetClipBox( (HDC)wParam, &rect_temp );
708 msg_Dbg( p_vout, "WinProc WM_ERASEBKGND %i,%i,%i,%i",
709 rect_temp.left, rect_temp.top,
710 rect_temp.right, rect_temp.bottom );
712 vout_PlacePicture( p_vout, p_vout->p_sys->i_window_width,
713 p_vout->p_sys->i_window_height,
714 &i_x, &i_y, &i_width, &i_height );
715 ExcludeClipRect( (HDC)wParam, i_x, i_y,
716 i_x + i_width, i_y + i_height );
721 //msg_Dbg( p_vout, "WinProc WM Default %i", message );
725 return DefWindowProc(hwnd, message, wParam, lParam);