1 /*****************************************************************************
2 * events.c: Windows DirectX video output events handler
3 *****************************************************************************
4 * Copyright (C) 2001 VideoLAN
5 * $Id: events.c,v 1.21 2003/07/29 21:46:44 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 val.b_bool = VLC_TRUE; /* make compiler happy */
180 var_Set( p_playlist, "intf-popupmenu", val );
181 vlc_object_release( p_playlist );
187 /* the key events are first processed here. The next
188 * message processed by this main message loop will be the
189 * char translation of the key event */
190 msg_Dbg( p_event, "WM_KEYDOWN" );
194 if( p_event->p_vout->b_fullscreen )
196 p_event->p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
200 /* close video window */
201 PostMessage( msg.hwnd, WM_CLOSE, 0, 0 );
207 playlist_t *p_playlist =
208 vlc_object_find( p_event, VLC_OBJECT_PLAYLIST,
210 if( p_playlist != NULL )
213 var_Set( p_playlist, "intf-popupmenu", val );
214 vlc_object_release( p_playlist );
220 /* input_Seek( p_event->p_vout, -5,
221 INPUT_SEEK_SECONDS | INPUT_SEEK_CUR ); */
222 val.psz_string = "LEFT";
223 var_Set( p_event->p_vout, "key-pressed", val );
226 /* input_Seek( p_event->p_vout, 5,
227 INPUT_SEEK_SECONDS | INPUT_SEEK_CUR ); */
228 val.psz_string = "RIGHT";
229 var_Set( p_event->p_vout, "key-pressed", val );
232 /* input_Seek( p_event->p_vout, 60,
233 INPUT_SEEK_SECONDS | INPUT_SEEK_CUR ); */
234 val.psz_string = "UP";
235 var_Set( p_event->p_vout, "key-pressed", val );
238 /* input_Seek( p_event->p_vout, -60,
239 INPUT_SEEK_SECONDS | INPUT_SEEK_CUR ); */
240 val.psz_string = "DOWN";
241 var_Set( p_event->p_vout, "key-pressed", val );
244 val.psz_string = "ENTER";
245 var_Set( p_event->p_vout, "key-pressed", val );
248 input_Seek( p_event->p_vout, 0,
249 INPUT_SEEK_BYTES | INPUT_SEEK_SET );
252 input_Seek( p_event->p_vout, 0,
253 INPUT_SEEK_BYTES | INPUT_SEEK_END );
256 input_Seek( p_event->p_vout, 10,
257 INPUT_SEEK_SECONDS | INPUT_SEEK_CUR );
260 input_Seek( p_event->p_vout, -10,
261 INPUT_SEEK_SECONDS | INPUT_SEEK_CUR );
264 input_SetStatus( p_event->p_vout, INPUT_STATUS_PAUSE );
267 TranslateMessage(&msg);
275 /* exit application */
276 p_event->p_vlc->b_die = VLC_TRUE;
279 case 'f': /* switch to fullscreen */
281 p_event->p_vout->p_sys->i_changes |= VOUT_FULLSCREEN_CHANGE;
289 /* Messages we don't handle directly are dispatched to the
290 * window procedure */
291 TranslateMessage(&msg);
292 DispatchMessage(&msg);
297 } /* End Main loop */
299 if( msg.message == WM_QUIT )
301 msg_Warn( p_event, "WM_QUIT... should not happen!!" );
302 p_event->p_vout->p_sys->hwnd = NULL; /* Window already destroyed */
305 msg_Dbg( p_event, "DirectXEventThread terminating" );
307 /* clear the changes formerly signaled */
308 p_event->p_vout->p_sys->i_changes = 0;
310 DirectXCloseWindow( p_event->p_vout );
314 /* following functions are local */
316 /*****************************************************************************
317 * DirectXCreateWindow: create a window for the video.
318 *****************************************************************************
319 * Before creating a direct draw surface, we need to create a window in which
320 * the video will be displayed. This window will also allow us to capture the
322 *****************************************************************************/
323 static int DirectXCreateWindow( vout_thread_t *p_vout )
333 msg_Dbg( p_vout, "DirectXCreateWindow" );
335 /* Get this module's instance */
336 hInstance = GetModuleHandle(NULL);
338 /* Create a BRUSH that will be used by Windows to paint the window
340 * This window background is important for us as it will be used by the
341 * graphics card to display the overlay.
342 * This is why we carefully choose the color for this background, the goal
343 * being to choose a color which isn't complete black but nearly. We
344 * obviously don't want to use black as a colorkey for the overlay because
345 * black is one of the most used color and thus would give us undesirable
347 /* the first step is to find the colorkey we want to use. The difficulty
348 * comes from the potential dithering (depends on the display depth)
349 * because we need to know the real RGB value of the chosen colorkey */
351 for( colorkey = 0x0a; colorkey < 0xff /* all shades of red */; colorkey++ )
353 if( colorkey == GetNearestColor( hdc, colorkey ) )
358 msg_Dbg( p_vout, "background color: %i", colorkey );
360 /* Create the actual brush */
361 p_vout->p_sys->hbrush = CreateSolidBrush(colorkey);
362 p_vout->p_sys->i_rgb_colorkey = (int)colorkey;
364 /* Get the current size of the display and its colour depth */
365 p_vout->p_sys->rect_display.right = GetDeviceCaps( hdc, HORZRES );
366 p_vout->p_sys->rect_display.bottom = GetDeviceCaps( hdc, VERTRES );
367 p_vout->p_sys->i_display_depth = GetDeviceCaps( hdc, BITSPIXEL );
368 msg_Dbg( p_vout, "screen dimensions %ix%i colour depth %i",
369 p_vout->p_sys->rect_display.right,
370 p_vout->p_sys->rect_display.bottom,
371 p_vout->p_sys->i_display_depth );
373 ReleaseDC( NULL, hdc );
375 /* If an external window was specified, we'll draw in it. */
376 var_Get( p_vout->p_vlc, "drawable", &val );
377 p_vout->p_sys->hparent = p_vout->p_sys->hwnd =
378 val.i_int ? (void*)(ptrdiff_t) val.i_int : NULL;
380 if( p_vout->p_sys->hparent )
382 msg_Dbg( p_vout, "using external window %p\n", p_vout->p_sys->hwnd );
384 /* Set stuff in the window that we can not put directly in
385 * a class (see below). */
386 SetClassLong( p_vout->p_sys->hwnd,
387 GCL_STYLE, CS_DBLCLKS );
388 SetClassLong( p_vout->p_sys->hwnd,
389 GCL_HBRBACKGROUND, (LONG)p_vout->p_sys->hbrush );
390 SetClassLong( p_vout->p_sys->hwnd,
391 GCL_HCURSOR, (LONG)LoadCursor(NULL, IDC_ARROW) );
392 /* Store a p_vout pointer into the window local storage (for later
393 * use in DirectXEventProc). */
394 SetWindowLong( p_vout->p_sys->hwnd, GWL_USERDATA, (LONG)p_vout );
396 p_vout->p_sys->pf_wndproc =
397 (WNDPROC)SetWindowLong( p_vout->p_sys->hwnd,
398 GWL_WNDPROC, (LONG)DirectXEventProc );
400 /* Blam! Erase everything that might have been there. */
401 RedrawWindow( p_vout->p_sys->hwnd, NULL, NULL,
402 RDW_INVALIDATE | RDW_ERASE );
406 WNDCLASSEX wc; /* window class components */
407 HICON vlc_icon = NULL;
408 char vlc_path[MAX_PATH+1];
410 /* Get the Icon from the main app */
412 if( GetModuleFileName( NULL, vlc_path, MAX_PATH ) )
414 vlc_icon = ExtractIcon( hInstance, vlc_path, 0 );
417 /* Fill in the window class structure */
418 wc.cbSize = sizeof(WNDCLASSEX);
419 wc.style = CS_DBLCLKS; /* style: dbl click */
420 wc.lpfnWndProc = (WNDPROC)DirectXEventProc; /* event handler */
421 wc.cbClsExtra = 0; /* no extra class data */
422 wc.cbWndExtra = 0; /* no extra window data */
423 wc.hInstance = hInstance; /* instance */
424 wc.hIcon = vlc_icon; /* load the vlc big icon */
425 wc.hCursor = LoadCursor(NULL, IDC_ARROW); /* default cursor */
426 wc.hbrBackground = p_vout->p_sys->hbrush; /* background color */
427 wc.lpszMenuName = NULL; /* no menu */
428 wc.lpszClassName = "VLC DirectX"; /* use a special class */
429 wc.hIconSm = vlc_icon; /* load the vlc small icon */
431 /* Register the window class */
432 if( !RegisterClassEx(&wc) )
436 /* Free window background brush */
437 if( p_vout->p_sys->hbrush )
439 DeleteObject( p_vout->p_sys->hbrush );
440 p_vout->p_sys->hbrush = NULL;
445 DestroyIcon( vlc_icon );
448 /* Check why it failed. If it's because one already exists
449 * then fine, otherwise return with an error. */
450 if( !GetClassInfo( hInstance, "VLC DirectX", &wndclass ) )
452 msg_Err( p_vout, "DirectXCreateWindow RegisterClass FAILED" );
457 /* When you create a window you give the dimensions you wish it to
458 * have. Unfortunatly these dimensions will include the borders and
459 * titlebar. We use the following function to find out the size of
460 * the window corresponding to the useable surface we want */
461 rect_window.top = 10;
462 rect_window.left = 10;
463 rect_window.right = rect_window.left + p_vout->p_sys->i_window_width;
464 rect_window.bottom = rect_window.top + p_vout->p_sys->i_window_height;
465 AdjustWindowRect( &rect_window, WS_OVERLAPPEDWINDOW|WS_SIZEBOX, 0 );
467 /* Create the window */
468 p_vout->p_sys->hwnd =
469 CreateWindow( "VLC DirectX", /* name of window class */
470 VOUT_TITLE " (DirectX Output)", /* window title bar text */
471 WS_OVERLAPPEDWINDOW | WS_SIZEBOX, /* window style */
472 CW_USEDEFAULT, /* default X coordinate */
473 0, /* default Y coordinate */
474 rect_window.right - rect_window.left, /* window width */
475 rect_window.bottom - rect_window.top, /* window height */
476 NULL, /* no parent window */
477 NULL, /* no menu in this window */
478 hInstance, /* handle of this program instance */
479 (LPVOID)p_vout ); /* send p_vout to WM_CREATE */
481 if( !p_vout->p_sys->hwnd )
483 msg_Warn( p_vout, "DirectXCreateWindow create window FAILED" );
488 /* Append a "Always On Top" entry in the system menu */
489 hMenu = GetSystemMenu( p_vout->p_sys->hwnd, FALSE );
490 AppendMenu( hMenu, MF_SEPARATOR, 0, "" );
491 AppendMenu( hMenu, MF_STRING | MF_UNCHECKED,
492 IDM_TOGGLE_ON_TOP, "Always on &Top" );
494 /* Now display the window */
495 ShowWindow( p_vout->p_sys->hwnd, SW_SHOW );
500 /*****************************************************************************
501 * DirectXCloseWindow: close the window created by DirectXCreateWindow
502 *****************************************************************************
503 * This function returns all resources allocated by DirectXCreateWindow.
504 *****************************************************************************/
505 static void DirectXCloseWindow( vout_thread_t *p_vout )
507 msg_Dbg( p_vout, "DirectXCloseWindow" );
509 if( p_vout->p_sys->hwnd && !p_vout->p_sys->hparent )
511 DestroyWindow( p_vout->p_sys->hwnd );
514 p_vout->p_sys->hwnd = NULL;
516 /* We don't unregister the Window Class because it could lead to race
517 * conditions and it will be done anyway by the system when the app will
521 /*****************************************************************************
522 * DirectXUpdateRects: update clipping rectangles
523 *****************************************************************************
524 * This function is called when the window position or size are changed, and
525 * its job is to update the source and destination RECTs used to display the
527 *****************************************************************************/
528 void DirectXUpdateRects( vout_thread_t *p_vout, vlc_bool_t b_force )
530 #define rect_src p_vout->p_sys->rect_src
531 #define rect_src_clipped p_vout->p_sys->rect_src_clipped
532 #define rect_dest p_vout->p_sys->rect_dest
533 #define rect_dest_clipped p_vout->p_sys->rect_dest_clipped
535 int i_width, i_height, i_x, i_y;
540 /* Retrieve the window size */
541 GetClientRect( p_vout->p_sys->hwnd, &rect );
543 /* Retrieve the window position */
544 point.x = point.y = 0;
545 ClientToScreen( p_vout->p_sys->hwnd, &point );
547 /* If nothing changed, we can return */
549 && p_vout->p_sys->i_window_width == rect.right
550 && p_vout->p_sys->i_window_height == rect.bottom
551 && p_vout->p_sys->i_window_x == point.x
552 && p_vout->p_sys->i_window_y == point.y )
557 /* Update the window position and size */
558 p_vout->p_sys->i_window_x = point.x;
559 p_vout->p_sys->i_window_y = point.y;
560 p_vout->p_sys->i_window_width = rect.right;
561 p_vout->p_sys->i_window_height = rect.bottom;
563 vout_PlacePicture( p_vout, rect.right, rect.bottom,
564 &i_x, &i_y, &i_width, &i_height );
566 /* Destination image position and dimensions */
567 rect_dest.left = point.x + i_x;
568 rect_dest.right = rect_dest.left + i_width;
569 rect_dest.top = point.y + i_y;
570 rect_dest.bottom = rect_dest.top + i_height;
573 /* UpdateOverlay directdraw function doesn't automatically clip to the
574 * display size so we need to do it otherwise it will fail */
576 /* Clip the destination window */
577 IntersectRect( &rect_dest_clipped,
579 &p_vout->p_sys->rect_display );
582 msg_Dbg( p_vout, "DirectXUpdateRects image_dst_clipped coords:"
584 rect_dest_clipped.left, rect_dest_clipped.top,
585 rect_dest_clipped.right, rect_dest_clipped.bottom );
588 /* the 2 following lines are to fix a bug when clicking on the desktop */
589 if( (rect_dest_clipped.right - rect_dest_clipped.left)==0 ||
590 (rect_dest_clipped.bottom - rect_dest_clipped.top)==0 )
592 SetRectEmpty( &rect_src_clipped );
596 /* src image dimensions */
599 rect_src.right = p_vout->render.i_width;
600 rect_src.bottom = p_vout->render.i_height;
602 /* Clip the source image */
603 rect_src_clipped.left = (rect_dest_clipped.left - rect_dest.left) *
604 p_vout->render.i_width / (rect_dest.right - rect_dest.left);
605 rect_src_clipped.right = p_vout->render.i_width -
606 (rect_dest.right - rect_dest_clipped.right) * p_vout->render.i_width /
607 (rect_dest.right - rect_dest.left);
608 rect_src_clipped.top = (rect_dest_clipped.top - rect_dest.top) *
609 p_vout->render.i_height / (rect_dest.bottom - rect_dest.top);
610 rect_src_clipped.bottom = p_vout->render.i_height -
611 (rect_dest.bottom - rect_dest_clipped.bottom) * p_vout->render.i_height /
612 (rect_dest.bottom - rect_dest.top);
615 msg_Dbg( p_vout, "DirectXUpdateRects image_src_clipped"
616 " coords: %i,%i,%i,%i",
617 rect_src_clipped.left, rect_src_clipped.top,
618 rect_src_clipped.right, rect_src_clipped.bottom );
621 /* Signal the size change */
622 if( !p_vout->p_sys->p_event->b_die )
624 if( p_vout->p_sys->b_using_overlay )
626 DirectXUpdateOverlay( p_vout );
630 p_vout->p_sys->i_changes |= VOUT_SIZE_CHANGE;
635 #undef rect_src_clipped
637 #undef rect_dest_clipped
640 /*****************************************************************************
641 * DirectXEventProc: This is the window event processing function.
642 *****************************************************************************
643 * On Windows, when you create a window you have to attach an event processing
644 * function to it. The aim of this function is to manage "Queued Messages" and
645 * "Nonqueued Messages".
646 * Queued Messages are those picked up and retransmitted by vout_Manage
647 * (using the GetMessage and DispatchMessage functions).
648 * Nonqueued Messages are those that Windows will send directly to this
649 * procedure (like WM_DESTROY, WM_WINDOWPOSCHANGED...)
650 *****************************************************************************/
651 static long FAR PASCAL DirectXEventProc( HWND hwnd, UINT message,
652 WPARAM wParam, LPARAM lParam )
654 vout_thread_t *p_vout;
656 if( message == WM_CREATE )
658 /* Store p_vout for future use */
659 p_vout = (vout_thread_t *)((CREATESTRUCT *)lParam)->lpCreateParams;
660 SetWindowLong( hwnd, GWL_USERDATA, (LONG)p_vout );
664 p_vout = (vout_thread_t *)GetWindowLong( hwnd, GWL_USERDATA );
670 case WM_WINDOWPOSCHANGED:
671 DirectXUpdateRects( p_vout, VLC_TRUE );
674 /* the user wants to close the window */
677 playlist_t * p_playlist =
678 (playlist_t *)vlc_object_find( p_vout, VLC_OBJECT_PLAYLIST,
680 if( p_playlist == NULL )
685 playlist_Stop( p_playlist );
686 vlc_object_release( p_playlist );
690 /* the window has been closed so shut down everything now */
692 msg_Dbg( p_vout, "WinProc WM_DESTROY" );
693 /* just destroy the window */
694 PostQuitMessage( 0 );
700 case SC_SCREENSAVE: /* catch the screensaver */
701 case SC_MONITORPOWER: /* catch the monitor turn-off */
702 msg_Dbg( p_vout, "WinProc WM_SYSCOMMAND" );
703 return 0; /* this stops them from happening */
704 case IDM_TOGGLE_ON_TOP: /* toggle the "on top" status */
707 msg_Dbg( p_vout, "WinProc WM_SYSCOMMAND: IDM_TOGGLE_ON_TOP");
709 /* Get the current value... */
710 if( var_Get( p_vout, "directx-on-top", &val ) < 0 )
712 /* ...and change it */
713 val.b_bool = !val.b_bool;
714 var_Set( p_vout, "directx-on-top", val );
722 if( !p_vout->p_sys->b_using_overlay )
724 /* We want to eliminate unnecessary background redraws which create
725 * an annoying flickering */
726 int i_width, i_height, i_x, i_y;
728 GetClipBox( (HDC)wParam, &rect_temp );
730 msg_Dbg( p_vout, "WinProc WM_ERASEBKGND %i,%i,%i,%i",
731 rect_temp.left, rect_temp.top,
732 rect_temp.right, rect_temp.bottom );
734 vout_PlacePicture( p_vout, p_vout->p_sys->i_window_width,
735 p_vout->p_sys->i_window_height,
736 &i_x, &i_y, &i_width, &i_height );
737 ExcludeClipRect( (HDC)wParam, i_x, i_y,
738 i_x + i_width, i_y + i_height );
743 //msg_Dbg( p_vout, "WinProc WM Default %i", message );
747 return DefWindowProc(hwnd, message, wParam, lParam);