1 /*****************************************************************************
2 * events.c: Windows DirectX video output events handler
3 *****************************************************************************
4 * Copyright (C) 2001-2004 the VideoLAN team
7 * Authors: Gildas Bazin <gbazin@videolan.org>
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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, 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 *****************************************************************************/
33 #include <errno.h> /* ENOMEM */
34 #include <ctype.h> /* tolower() */
37 # define _WIN32_WINNT 0x0500
40 #include <vlc_common.h>
41 #include <vlc_interface.h>
42 #include <vlc_playlist.h>
44 #include <vlc_window.h>
50 #ifdef MODULE_NAME_IS_vout_directx
53 #ifdef MODULE_NAME_IS_direct3d
56 #ifdef MODULE_NAME_IS_glwin32
65 //WINSHELLAPI BOOL WINAPI SHFullScreen(HWND hwndRequester, DWORD dwState);
68 /*#if defined(UNDER_CE) && !defined(__PLUGIN__) --FIXME*/
69 /*# define SHFS_SHOWSIPBUTTON 0x0004
70 # define SHFS_HIDESIPBUTTON 0x0008
71 # define MENU_HEIGHT 26
72 BOOL SHFullScreen(HWND hwndRequester, DWORD dwState);
76 /*****************************************************************************
78 *****************************************************************************/
79 static int DirectXCreateWindow( vout_thread_t *p_vout );
80 static void DirectXCloseWindow ( vout_thread_t *p_vout );
81 static long FAR PASCAL DirectXEventProc( HWND, UINT, WPARAM, LPARAM );
83 static int Control( vout_thread_t *p_vout, int i_query, va_list args );
84 static int vaControlParentWindow( vout_thread_t *, int, va_list );
86 static void DirectXPopupMenu( event_thread_t *p_event, bool b_open )
90 var_Set( p_event->p_libvlc, "intf-popupmenu", val );
93 static int DirectXConvertKey( int i_key );
95 /*****************************************************************************
96 * EventThread: Create video window & handle its messages
97 *****************************************************************************
98 * This function creates a video window and then enters an infinite loop
99 * that handles the messages sent to that window.
100 * The main goal of this thread is to isolate the Win32 PeekMessage function
101 * because this one can block for a long time.
102 *****************************************************************************/
103 void* EventThread( vlc_object_t *p_this )
105 event_thread_t *p_event = (event_thread_t *)p_this;
107 POINT old_mouse_pos = {0,0}, mouse_pos;
109 unsigned int i_width, i_height, i_x, i_y;
111 int canc = vlc_savecancel ();
114 p_event->p_vout->pf_control = Control;
116 /* Create a window for the video */
117 /* Creating a window under Windows also initializes the thread's event
119 if( DirectXCreateWindow( p_event->p_vout ) )
121 vlc_restorecancel (canc);
125 /* Signal the creation of the window */
126 SetEvent( p_event->window_ready );
129 /* Set power management stuff */
130 if( (hkernel32 = GetModuleHandle( _T("KERNEL32") ) ) )
132 ULONG (WINAPI* OurSetThreadExecutionState)( ULONG );
134 OurSetThreadExecutionState = (ULONG (WINAPI*)( ULONG ))
135 GetProcAddress( hkernel32, _T("SetThreadExecutionState") );
137 if( OurSetThreadExecutionState )
138 /* Prevent monitor from powering off */
139 OurSetThreadExecutionState( ES_DISPLAY_REQUIRED | ES_CONTINUOUS );
141 msg_Dbg( p_event, "no support for SetThreadExecutionState()" );
146 /* GetMessage will sleep if there's no message in the queue */
147 while( vlc_object_alive (p_event) && GetMessage( &msg, 0, 0, 0 ) )
149 /* Check if we are asked to exit */
150 if( !vlc_object_alive (p_event) )
153 switch( msg.message )
157 vout_PlacePicture( p_event->p_vout,
158 p_event->p_vout->p_sys->i_window_width,
159 p_event->p_vout->p_sys->i_window_height,
160 &i_x, &i_y, &i_width, &i_height );
162 if( msg.hwnd == p_event->p_vout->p_sys->hvideownd )
168 if( i_width && i_height )
170 val.i_int = ( GET_X_LPARAM(msg.lParam) - i_x ) *
171 p_event->p_vout->fmt_in.i_visible_width / i_width +
172 p_event->p_vout->fmt_in.i_x_offset;
173 var_Set( p_event->p_vout, "mouse-x", val );
174 val.i_int = ( GET_Y_LPARAM(msg.lParam) - i_y ) *
175 p_event->p_vout->fmt_in.i_visible_height / i_height +
176 p_event->p_vout->fmt_in.i_y_offset;
177 var_Set( p_event->p_vout, "mouse-y", val );
180 var_Set( p_event->p_vout, "mouse-moved", val );
184 GetCursorPos( &mouse_pos );
185 if( (abs(mouse_pos.x - old_mouse_pos.x) > 2 ||
186 (abs(mouse_pos.y - old_mouse_pos.y)) > 2 ) )
188 GetCursorPos( &old_mouse_pos );
189 p_event->p_vout->p_sys->i_lastmoved = mdate();
191 if( p_event->p_vout->p_sys->b_cursor_hidden )
193 p_event->p_vout->p_sys->b_cursor_hidden = 0;
199 case WM_VLC_HIDE_MOUSE:
200 if( p_event->p_vout->p_sys->b_cursor_hidden ) break;
201 p_event->p_vout->p_sys->b_cursor_hidden = true;
202 GetCursorPos( &old_mouse_pos );
206 case WM_VLC_SHOW_MOUSE:
207 if( !p_event->p_vout->p_sys->b_cursor_hidden ) break;
208 p_event->p_vout->p_sys->b_cursor_hidden = false;
209 GetCursorPos( &old_mouse_pos );
214 var_Get( p_event->p_vout, "mouse-button-down", &val );
216 var_Set( p_event->p_vout, "mouse-button-down", val );
217 DirectXPopupMenu( p_event, false );
221 var_Get( p_event->p_vout, "mouse-button-down", &val );
223 var_Set( p_event->p_vout, "mouse-button-down", val );
225 var_SetBool( p_event->p_vout, "mouse-clicked", true );
228 case WM_LBUTTONDBLCLK:
229 p_event->p_vout->p_sys->i_changes |= VOUT_FULLSCREEN_CHANGE;
233 var_Get( p_event->p_vout, "mouse-button-down", &val );
235 var_Set( p_event->p_vout, "mouse-button-down", val );
236 DirectXPopupMenu( p_event, false );
240 var_Get( p_event->p_vout, "mouse-button-down", &val );
242 var_Set( p_event->p_vout, "mouse-button-down", val );
246 var_Get( p_event->p_vout, "mouse-button-down", &val );
248 var_Set( p_event->p_vout, "mouse-button-down", val );
249 DirectXPopupMenu( p_event, false );
253 var_Get( p_event->p_vout, "mouse-button-down", &val );
255 var_Set( p_event->p_vout, "mouse-button-down", val );
256 DirectXPopupMenu( p_event, true );
261 /* The key events are first processed here and not translated
262 * into WM_CHAR events because we need to know the status of the
264 val.i_int = DirectXConvertKey( msg.wParam );
267 /* This appears to be a "normal" (ascii) key */
268 val.i_int = tolower( MapVirtualKey( msg.wParam, 2 ) );
273 if( GetKeyState(VK_CONTROL) & 0x8000 )
275 val.i_int |= KEY_MODIFIER_CTRL;
277 if( GetKeyState(VK_SHIFT) & 0x8000 )
279 val.i_int |= KEY_MODIFIER_SHIFT;
281 if( GetKeyState(VK_MENU) & 0x8000 )
283 val.i_int |= KEY_MODIFIER_ALT;
286 var_Set( p_event->p_libvlc, "key-pressed", val );
291 if( GET_WHEEL_DELTA_WPARAM( msg.wParam ) > 0 )
293 val.i_int = KEY_MOUSEWHEELUP;
297 val.i_int = KEY_MOUSEWHEELDOWN;
301 if( GetKeyState(VK_CONTROL) & 0x8000 )
303 val.i_int |= KEY_MODIFIER_CTRL;
305 if( GetKeyState(VK_SHIFT) & 0x8000 )
307 val.i_int |= KEY_MODIFIER_SHIFT;
309 if( GetKeyState(VK_MENU) & 0x8000 )
311 val.i_int |= KEY_MODIFIER_ALT;
314 var_Set( p_event->p_libvlc, "key-pressed", val );
318 case WM_VLC_CHANGE_TEXT:
319 var_Get( p_event->p_vout, "video-title", &val );
320 if( !val.psz_string || !*val.psz_string ) /* Default video title */
322 free( val.psz_string );
324 #ifdef MODULE_NAME_IS_wingdi
325 val.psz_string = strdup( VOUT_TITLE " (WinGDI output)" );
327 #ifdef MODULE_NAME_IS_wingapi
328 val.psz_string = strdup( VOUT_TITLE " (WinGAPI output)" );
330 #ifdef MODULE_NAME_IS_glwin32
331 val.psz_string = strdup( VOUT_TITLE " (OpenGL output)" );
333 #ifdef MODULE_NAME_IS_direct3d
334 val.psz_string = strdup( VOUT_TITLE " (Direct3D output)" );
336 #ifdef MODULE_NAME_IS_vout_directx
337 if( p_event->p_vout->p_sys->b_using_overlay ) val.psz_string =
338 strdup( VOUT_TITLE " (hardware YUV overlay DirectX output)" );
339 else if( p_event->p_vout->p_sys->b_hw_yuv ) val.psz_string =
340 strdup( VOUT_TITLE " (hardware YUV DirectX output)" );
341 else val.psz_string =
342 strdup( VOUT_TITLE " (software RGB DirectX output)" );
348 wchar_t *psz_title = malloc( strlen(val.psz_string) * 2 + 2 );
351 mbstowcs( psz_title, val.psz_string, strlen(val.psz_string)*2);
352 psz_title[strlen(val.psz_string)] = 0;
353 free( val.psz_string ); val.psz_string = (char *)psz_title;
358 SetWindowText( p_event->p_vout->p_sys->hwnd,
359 (LPCTSTR)val.psz_string );
360 if( p_event->p_vout->p_sys->hfswnd )
361 SetWindowText( p_event->p_vout->p_sys->hfswnd,
362 (LPCTSTR)val.psz_string );
363 free( val.psz_string );
367 /* Messages we don't handle directly are dispatched to the
368 * window procedure */
369 TranslateMessage(&msg);
370 DispatchMessage(&msg);
375 } /* End Main loop */
377 /* Check for WM_QUIT if we created the window */
378 if( !p_event->p_vout->p_sys->hparent && msg.message == WM_QUIT )
380 msg_Warn( p_event, "WM_QUIT... should not happen!!" );
381 p_event->p_vout->p_sys->hwnd = NULL; /* Window already destroyed */
384 msg_Dbg( p_event, "DirectXEventThread terminating" );
386 /* clear the changes formerly signaled */
387 p_event->p_vout->p_sys->i_changes = 0;
389 DirectXCloseWindow( p_event->p_vout );
390 vlc_restorecancel (canc);
395 /* following functions are local */
397 /*****************************************************************************
398 * DirectXCreateWindow: create a window for the video.
399 *****************************************************************************
400 * Before creating a direct draw surface, we need to create a window in which
401 * the video will be displayed. This window will also allow us to capture the
403 *****************************************************************************/
404 static int DirectXCreateWindow( vout_thread_t *p_vout )
409 WNDCLASS wc; /* window class components */
410 HICON vlc_icon = NULL;
411 char vlc_path[MAX_PATH+1];
412 int i_style, i_stylex;
414 msg_Dbg( p_vout, "DirectXCreateWindow" );
416 /* Get this module's instance */
417 hInstance = GetModuleHandle(NULL);
419 /* If an external window was specified, we'll draw in it. */
420 p_vout->p_sys->parent_window =
421 vout_RequestHWND( p_vout, &p_vout->p_sys->i_window_x,
422 &p_vout->p_sys->i_window_y,
423 &p_vout->p_sys->i_window_width,
424 &p_vout->p_sys->i_window_height );
425 if( p_vout->p_sys->parent_window )
426 p_vout->p_sys->hparent = p_vout->p_sys->parent_window->handle.hwnd;
428 /* We create the window ourself, there is no previous window proc. */
429 p_vout->p_sys->pf_wndproc = NULL;
431 /* Get the Icon from the main app */
434 if( GetModuleFileName( NULL, vlc_path, MAX_PATH ) )
436 vlc_icon = ExtractIcon( hInstance, vlc_path, 0 );
440 /* Fill in the window class structure */
441 wc.style = CS_OWNDC|CS_DBLCLKS; /* style: dbl click */
442 wc.lpfnWndProc = (WNDPROC)DirectXEventProc; /* event handler */
443 wc.cbClsExtra = 0; /* no extra class data */
444 wc.cbWndExtra = 0; /* no extra window data */
445 wc.hInstance = hInstance; /* instance */
446 wc.hIcon = vlc_icon; /* load the vlc big icon */
447 wc.hCursor = LoadCursor(NULL, IDC_ARROW); /* default cursor */
448 wc.hbrBackground = GetStockObject(BLACK_BRUSH); /* background color */
449 wc.lpszMenuName = NULL; /* no menu */
450 wc.lpszClassName = _T("VLC DirectX"); /* use a special class */
452 /* Register the window class */
453 if( !RegisterClass(&wc) )
457 if( vlc_icon ) DestroyIcon( vlc_icon );
459 /* Check why it failed. If it's because one already exists
460 * then fine, otherwise return with an error. */
461 if( !GetClassInfo( hInstance, _T("VLC DirectX"), &wndclass ) )
463 msg_Err( p_vout, "DirectXCreateWindow RegisterClass FAILED (err=%lu)", GetLastError() );
468 /* Register the video sub-window class */
469 wc.lpszClassName = _T("VLC DirectX video"); wc.hIcon = 0;
470 wc.hbrBackground = NULL; /* no background color */
471 if( !RegisterClass(&wc) )
475 /* Check why it failed. If it's because one already exists
476 * then fine, otherwise return with an error. */
477 if( !GetClassInfo( hInstance, _T("VLC DirectX video"), &wndclass ) )
479 msg_Err( p_vout, "DirectXCreateWindow RegisterClass FAILED (err=%lu)", GetLastError() );
484 /* When you create a window you give the dimensions you wish it to
485 * have. Unfortunatly these dimensions will include the borders and
486 * titlebar. We use the following function to find out the size of
487 * the window corresponding to the useable surface we want */
488 rect_window.top = 10;
489 rect_window.left = 10;
490 rect_window.right = rect_window.left + p_vout->p_sys->i_window_width;
491 rect_window.bottom = rect_window.top + p_vout->p_sys->i_window_height;
493 if( var_GetBool( p_vout, "video-deco" ) )
495 /* Open with window decoration */
496 AdjustWindowRect( &rect_window, WS_OVERLAPPEDWINDOW|WS_SIZEBOX, 0 );
497 i_style = WS_OVERLAPPEDWINDOW|WS_SIZEBOX|WS_VISIBLE|WS_CLIPCHILDREN;
502 /* No window decoration */
503 AdjustWindowRect( &rect_window, WS_POPUP, 0 );
504 i_style = WS_POPUP|WS_VISIBLE|WS_CLIPCHILDREN;
505 i_stylex = 0; // WS_EX_TOOLWINDOW; Is TOOLWINDOW really needed ?
506 // It messes up the fullscreen window.
509 if( p_vout->p_sys->hparent )
511 i_style = WS_VISIBLE|WS_CLIPCHILDREN|WS_CHILD;
515 p_vout->p_sys->i_window_style = i_style;
517 /* Create the window */
518 p_vout->p_sys->hwnd =
519 CreateWindowEx( WS_EX_NOPARENTNOTIFY | i_stylex,
520 _T("VLC DirectX"), /* name of window class */
521 _T(VOUT_TITLE) _T(" (DirectX Output)"), /* window title */
522 i_style, /* window style */
523 (p_vout->p_sys->i_window_x < 0) ? CW_USEDEFAULT :
524 (UINT)p_vout->p_sys->i_window_x, /* default X coordinate */
525 (p_vout->p_sys->i_window_y < 0) ? CW_USEDEFAULT :
526 (UINT)p_vout->p_sys->i_window_y, /* default Y coordinate */
527 rect_window.right - rect_window.left, /* window width */
528 rect_window.bottom - rect_window.top, /* window height */
529 p_vout->p_sys->hparent, /* parent window */
530 NULL, /* no menu in this window */
531 hInstance, /* handle of this program instance */
532 (LPVOID)p_vout ); /* send p_vout to WM_CREATE */
534 if( !p_vout->p_sys->hwnd )
536 msg_Warn( p_vout, "DirectXCreateWindow create window FAILED (err=%lu)", GetLastError() );
540 if( p_vout->p_sys->hparent )
544 /* We don't want the window owner to overwrite our client area */
545 i_style = GetWindowLong( p_vout->p_sys->hparent, GWL_STYLE );
547 if( !(i_style & WS_CLIPCHILDREN) )
548 /* Hmmm, apparently this is a blocking call... */
549 SetWindowLong( p_vout->p_sys->hparent, GWL_STYLE,
550 i_style | WS_CLIPCHILDREN );
552 /* Create our fullscreen window */
553 p_vout->p_sys->hfswnd =
554 CreateWindowEx( WS_EX_APPWINDOW, _T("VLC DirectX"),
555 _T(VOUT_TITLE) _T(" (DirectX Output)"),
556 WS_OVERLAPPEDWINDOW|WS_CLIPCHILDREN|WS_SIZEBOX,
557 CW_USEDEFAULT, CW_USEDEFAULT,
558 CW_USEDEFAULT, CW_USEDEFAULT,
559 NULL, NULL, hInstance, NULL );
562 /* Append a "Always On Top" entry in the system menu */
563 hMenu = GetSystemMenu( p_vout->p_sys->hwnd, FALSE );
564 AppendMenu( hMenu, MF_SEPARATOR, 0, _T("") );
565 AppendMenu( hMenu, MF_STRING | MF_UNCHECKED,
566 IDM_TOGGLE_ON_TOP, _T("Always on &Top") );
568 /* Create video sub-window. This sub window will always exactly match
569 * the size of the video, which allows us to use crazy overlay colorkeys
570 * without having them shown outside of the video area. */
571 p_vout->p_sys->hvideownd =
572 CreateWindow( _T("VLC DirectX video"), _T(""), /* window class */
573 WS_CHILD, /* window style, not visible initially */
575 p_vout->render.i_width, /* default width */
576 p_vout->render.i_height, /* default height */
577 p_vout->p_sys->hwnd, /* parent window */
579 (LPVOID)p_vout ); /* send p_vout to WM_CREATE */
581 if( !p_vout->p_sys->hvideownd )
582 msg_Warn( p_vout, "can't create video sub-window" );
584 msg_Dbg( p_vout, "created video sub-window" );
586 /* Now display the window */
587 ShowWindow( p_vout->p_sys->hwnd, SW_SHOW );
592 /*****************************************************************************
593 * DirectXCloseWindow: close the window created by DirectXCreateWindow
594 *****************************************************************************
595 * This function returns all resources allocated by DirectXCreateWindow.
596 *****************************************************************************/
597 static void DirectXCloseWindow( vout_thread_t *p_vout )
599 msg_Dbg( p_vout, "DirectXCloseWindow" );
601 DestroyWindow( p_vout->p_sys->hwnd );
602 if( p_vout->p_sys->hfswnd ) DestroyWindow( p_vout->p_sys->hfswnd );
604 vout_ReleaseWindow( p_vout->p_sys->parent_window );
605 p_vout->p_sys->hwnd = NULL;
607 /* We don't unregister the Window Class because it could lead to race
608 * conditions and it will be done anyway by the system when the app will
612 /*****************************************************************************
613 * UpdateRects: update clipping rectangles
614 *****************************************************************************
615 * This function is called when the window position or size are changed, and
616 * its job is to update the source and destination RECTs used to display the
618 *****************************************************************************/
619 void UpdateRects( vout_thread_t *p_vout, bool b_force )
621 #define rect_src p_vout->p_sys->rect_src
622 #define rect_src_clipped p_vout->p_sys->rect_src_clipped
623 #define rect_dest p_vout->p_sys->rect_dest
624 #define rect_dest_clipped p_vout->p_sys->rect_dest_clipped
626 int i_width, i_height, i_x, i_y;
631 /* Retrieve the window size */
632 GetClientRect( p_vout->p_sys->hwnd, &rect );
634 /* Retrieve the window position */
635 point.x = point.y = 0;
636 ClientToScreen( p_vout->p_sys->hwnd, &point );
638 /* If nothing changed, we can return */
640 && p_vout->p_sys->i_window_width == rect.right
641 && p_vout->p_sys->i_window_height == rect.bottom
642 && p_vout->p_sys->i_window_x == point.x
643 && p_vout->p_sys->i_window_y == point.y )
648 /* Update the window position and size */
649 p_vout->p_sys->i_window_x = point.x;
650 p_vout->p_sys->i_window_y = point.y;
651 p_vout->p_sys->i_window_width = rect.right;
652 p_vout->p_sys->i_window_height = rect.bottom;
654 vout_PlacePicture( p_vout, rect.right, rect.bottom,
655 &i_x, &i_y, &i_width, &i_height );
657 if( p_vout->p_sys->hvideownd )
658 SetWindowPos( p_vout->p_sys->hvideownd, 0,
659 i_x, i_y, i_width, i_height,
660 SWP_NOCOPYBITS|SWP_NOZORDER|SWP_ASYNCWINDOWPOS );
662 /* Destination image position and dimensions */
663 rect_dest.left = point.x + i_x;
664 rect_dest.right = rect_dest.left + i_width;
665 rect_dest.top = point.y + i_y;
666 rect_dest.bottom = rect_dest.top + i_height;
668 #ifdef MODULE_NAME_IS_vout_directx
669 /* Apply overlay hardware constraints */
670 if( p_vout->p_sys->b_using_overlay )
672 if( p_vout->p_sys->i_align_dest_boundary )
673 rect_dest.left = ( rect_dest.left +
674 p_vout->p_sys->i_align_dest_boundary / 2 ) &
675 ~p_vout->p_sys->i_align_dest_boundary;
677 if( p_vout->p_sys->i_align_dest_size )
678 rect_dest.right = (( rect_dest.right - rect_dest.left +
679 p_vout->p_sys->i_align_dest_size / 2 ) &
680 ~p_vout->p_sys->i_align_dest_size) + rect_dest.left;
683 /* UpdateOverlay directdraw function doesn't automatically clip to the
684 * display size so we need to do it otherwise it will fail */
686 /* Clip the destination window */
687 if( !IntersectRect( &rect_dest_clipped, &rect_dest,
688 &p_vout->p_sys->rect_display ) )
690 SetRectEmpty( &rect_src_clipped );
695 msg_Dbg( p_vout, "DirectXUpdateRects image_dst_clipped coords:"
697 rect_dest_clipped.left, rect_dest_clipped.top,
698 rect_dest_clipped.right, rect_dest_clipped.bottom );
701 #else /* MODULE_NAME_IS_vout_directx */
703 /* AFAIK, there are no clipping constraints in Direct3D, OpenGL and GDI */
704 rect_dest_clipped = rect_dest;
708 /* the 2 following lines are to fix a bug when clicking on the desktop */
709 if( (rect_dest_clipped.right - rect_dest_clipped.left)==0 ||
710 (rect_dest_clipped.bottom - rect_dest_clipped.top)==0 )
712 SetRectEmpty( &rect_src_clipped );
716 /* src image dimensions */
719 rect_src.right = p_vout->render.i_width;
720 rect_src.bottom = p_vout->render.i_height;
722 /* Clip the source image */
723 rect_src_clipped.left = p_vout->fmt_out.i_x_offset +
724 (rect_dest_clipped.left - rect_dest.left) *
725 p_vout->fmt_out.i_visible_width / (rect_dest.right - rect_dest.left);
726 rect_src_clipped.right = p_vout->fmt_out.i_x_offset +
727 p_vout->fmt_out.i_visible_width -
728 (rect_dest.right - rect_dest_clipped.right) *
729 p_vout->fmt_out.i_visible_width / (rect_dest.right - rect_dest.left);
730 rect_src_clipped.top = p_vout->fmt_out.i_y_offset +
731 (rect_dest_clipped.top - rect_dest.top) *
732 p_vout->fmt_out.i_visible_height / (rect_dest.bottom - rect_dest.top);
733 rect_src_clipped.bottom = p_vout->fmt_out.i_y_offset +
734 p_vout->fmt_out.i_visible_height -
735 (rect_dest.bottom - rect_dest_clipped.bottom) *
736 p_vout->fmt_out.i_visible_height / (rect_dest.bottom - rect_dest.top);
738 #ifdef MODULE_NAME_IS_vout_directx
739 /* Apply overlay hardware constraints */
740 if( p_vout->p_sys->b_using_overlay )
742 if( p_vout->p_sys->i_align_src_boundary )
743 rect_src_clipped.left = ( rect_src_clipped.left +
744 p_vout->p_sys->i_align_src_boundary / 2 ) &
745 ~p_vout->p_sys->i_align_src_boundary;
747 if( p_vout->p_sys->i_align_src_size )
748 rect_src_clipped.right = (( rect_src_clipped.right -
749 rect_src_clipped.left +
750 p_vout->p_sys->i_align_src_size / 2 ) &
751 ~p_vout->p_sys->i_align_src_size) + rect_src_clipped.left;
756 msg_Dbg( p_vout, "DirectXUpdateRects image_src_clipped"
757 " coords: %i,%i,%i,%i",
758 rect_src_clipped.left, rect_src_clipped.top,
759 rect_src_clipped.right, rect_src_clipped.bottom );
762 #ifdef MODULE_NAME_IS_vout_directx
763 /* The destination coordinates need to be relative to the current
764 * directdraw primary surface (display) */
765 rect_dest_clipped.left -= p_vout->p_sys->rect_display.left;
766 rect_dest_clipped.right -= p_vout->p_sys->rect_display.left;
767 rect_dest_clipped.top -= p_vout->p_sys->rect_display.top;
768 rect_dest_clipped.bottom -= p_vout->p_sys->rect_display.top;
770 if( p_vout->p_sys->b_using_overlay )
771 DirectDrawUpdateOverlay( p_vout );
774 /* Signal the change in size/position */
775 p_vout->p_sys->i_changes |= DX_POSITION_CHANGE;
778 #undef rect_src_clipped
780 #undef rect_dest_clipped
783 /*****************************************************************************
784 * DirectXEventProc: This is the window event processing function.
785 *****************************************************************************
786 * On Windows, when you create a window you have to attach an event processing
787 * function to it. The aim of this function is to manage "Queued Messages" and
788 * "Nonqueued Messages".
789 * Queued Messages are those picked up and retransmitted by vout_Manage
790 * (using the GetMessage and DispatchMessage functions).
791 * Nonqueued Messages are those that Windows will send directly to this
792 * procedure (like WM_DESTROY, WM_WINDOWPOSCHANGED...)
793 *****************************************************************************/
794 static long FAR PASCAL DirectXEventProc( HWND hwnd, UINT message,
795 WPARAM wParam, LPARAM lParam )
797 vout_thread_t *p_vout;
799 if( message == WM_CREATE )
801 /* Store p_vout for future use */
802 p_vout = (vout_thread_t *)((CREATESTRUCT *)lParam)->lpCreateParams;
803 SetWindowLongPtr( hwnd, GWLP_USERDATA, (LONG_PTR)p_vout );
808 p_vout = (vout_thread_t *)GetWindowLongPtr( hwnd, GWLP_USERDATA );
811 /* Hmmm mozilla does manage somehow to save the pointer to our
812 * windowproc and still calls it after the vout has been closed. */
813 return DefWindowProc(hwnd, message, wParam, lParam);
818 /* Catch the screensaver and the monitor turn-off */
819 if( message == WM_SYSCOMMAND &&
820 ( (wParam & 0xFFF0) == SC_SCREENSAVE || (wParam & 0xFFF0) == SC_MONITORPOWER ) )
822 //if( p_vout ) msg_Dbg( p_vout, "WinProc WM_SYSCOMMAND screensaver" );
823 return 0; /* this stops them from happening */
827 if( hwnd == p_vout->p_sys->hvideownd )
831 #ifdef MODULE_NAME_IS_vout_directx
833 /* For overlay, we need to erase background */
834 return !p_vout->p_sys->b_using_overlay ?
835 1 : DefWindowProc(hwnd, message, wParam, lParam);
838 ** For overlay, DefWindowProc() will erase dirty regions
840 ** For non-overlay, vout will paint the whole window at
841 ** regular interval, therefore dirty regions can be ignored
842 ** to minimize repaint.
844 if( !p_vout->p_sys->b_using_overlay )
846 ValidateRect(hwnd, NULL);
848 // fall through to default
851 ** For OpenGL and Direct3D, vout will update the whole
852 ** window at regular interval, therefore dirty region
853 ** can be ignored to minimize repaint.
856 /* nothing to erase */
859 /* nothing to repaint */
860 ValidateRect(hwnd, NULL);
864 return DefWindowProc(hwnd, message, wParam, lParam);
871 case WM_WINDOWPOSCHANGED:
872 UpdateRects( p_vout, true );
875 /* the user wants to close the window */
878 playlist_t * p_playlist = pl_Hold( p_vout );
881 playlist_Stop( p_playlist );
882 pl_Release( p_vout );
887 /* the window has been closed so shut down everything now */
889 msg_Dbg( p_vout, "WinProc WM_DESTROY" );
890 /* just destroy the window */
891 PostQuitMessage( 0 );
897 case IDM_TOGGLE_ON_TOP: /* toggle the "on top" status */
900 msg_Dbg( p_vout, "WinProc WM_SYSCOMMAND: IDM_TOGGLE_ON_TOP");
902 /* Change the current value */
903 var_Get( p_vout, "video-on-top", &val );
904 val.b_bool = !val.b_bool;
905 var_Set( p_vout, "video-on-top", val );
914 return DefWindowProc(hwnd, message, wParam, lParam);
917 #ifdef MODULE_NAME_IS_wingapi
918 p_vout->p_sys->b_focus = false;
919 if( !p_vout->p_sys->b_parent_focus ) GXSuspend();
922 if( hwnd == p_vout->p_sys->hfswnd )
924 HWND htbar = FindWindow( _T("HHTaskbar"), NULL );
925 ShowWindow( htbar, SW_SHOW );
928 if( !p_vout->p_sys->hparent ||
929 hwnd == p_vout->p_sys->hfswnd )
931 SHFullScreen( hwnd, SHFS_SHOWSIPBUTTON );
937 #ifdef MODULE_NAME_IS_wingapi
938 p_vout->p_sys->b_focus = true;
942 if( p_vout->p_sys->hparent &&
943 hwnd != p_vout->p_sys->hfswnd && p_vout->b_fullscreen )
944 p_vout->p_sys->i_changes |= VOUT_FULLSCREEN_CHANGE;
946 if( hwnd == p_vout->p_sys->hfswnd )
948 HWND htbar = FindWindow( _T("HHTaskbar"), NULL );
949 ShowWindow( htbar, SW_HIDE );
952 if( !p_vout->p_sys->hparent ||
953 hwnd == p_vout->p_sys->hfswnd )
955 SHFullScreen( hwnd, SHFS_HIDESIPBUTTON );
961 //msg_Dbg( p_vout, "WinProc WM Default %i", message );
965 /* Let windows handle the message */
966 return DefWindowProc(hwnd, message, wParam, lParam);
974 } dxkeys_to_vlckeys[] =
976 { VK_F1, KEY_F1 }, { VK_F2, KEY_F2 }, { VK_F3, KEY_F3 }, { VK_F4, KEY_F4 },
977 { VK_F5, KEY_F5 }, { VK_F6, KEY_F6 }, { VK_F7, KEY_F7 }, { VK_F8, KEY_F8 },
978 { VK_F9, KEY_F9 }, { VK_F10, KEY_F10 }, { VK_F11, KEY_F11 },
981 { VK_RETURN, KEY_ENTER },
982 { VK_SPACE, KEY_SPACE },
983 { VK_ESCAPE, KEY_ESC },
985 { VK_LEFT, KEY_LEFT },
986 { VK_RIGHT, KEY_RIGHT },
988 { VK_DOWN, KEY_DOWN },
990 { VK_HOME, KEY_HOME },
992 { VK_PRIOR, KEY_PAGEUP },
993 { VK_NEXT, KEY_PAGEDOWN },
995 { VK_INSERT, KEY_INSERT },
996 { VK_DELETE, KEY_DELETE },
1002 { VK_BROWSER_BACK, KEY_BROWSER_BACK },
1003 { VK_BROWSER_FORWARD, KEY_BROWSER_FORWARD },
1004 { VK_BROWSER_REFRESH, KEY_BROWSER_REFRESH },
1005 { VK_BROWSER_STOP, KEY_BROWSER_STOP },
1006 { VK_BROWSER_SEARCH, KEY_BROWSER_SEARCH },
1007 { VK_BROWSER_FAVORITES, KEY_BROWSER_FAVORITES },
1008 { VK_BROWSER_HOME, KEY_BROWSER_HOME },
1009 { VK_VOLUME_MUTE, KEY_VOLUME_MUTE },
1010 { VK_VOLUME_DOWN, KEY_VOLUME_DOWN },
1011 { VK_VOLUME_UP, KEY_VOLUME_UP },
1012 { VK_MEDIA_NEXT_TRACK, KEY_MEDIA_NEXT_TRACK },
1013 { VK_MEDIA_PREV_TRACK, KEY_MEDIA_PREV_TRACK },
1014 { VK_MEDIA_STOP, KEY_MEDIA_STOP },
1015 { VK_MEDIA_PLAY_PAUSE, KEY_MEDIA_PLAY_PAUSE },
1020 static int DirectXConvertKey( int i_key )
1024 for( i = 0; dxkeys_to_vlckeys[i].i_dxkey != 0; i++ )
1026 if( dxkeys_to_vlckeys[i].i_dxkey == i_key )
1028 return dxkeys_to_vlckeys[i].i_vlckey;
1035 /*****************************************************************************
1036 * Control: control facility for the vout
1037 *****************************************************************************/
1038 static int Control( vout_thread_t *p_vout, int i_query, va_list args )
1040 unsigned int *pi_width, *pi_height;
1048 if( p_vout->p_sys->parent_window )
1049 return vaControlParentWindow( p_vout, i_query, args );
1051 /* Update dimensions */
1052 rect_window.top = rect_window.left = 0;
1053 rect_window.right = va_arg( args, unsigned int );
1054 rect_window.bottom = va_arg( args, unsigned int );
1055 if( !rect_window.right ) rect_window.right = p_vout->i_window_width;
1056 if( !rect_window.bottom ) rect_window.bottom = p_vout->i_window_height;
1057 AdjustWindowRect( &rect_window, p_vout->p_sys->i_window_style, 0 );
1059 SetWindowPos( p_vout->p_sys->hwnd, 0, 0, 0,
1060 rect_window.right - rect_window.left,
1061 rect_window.bottom - rect_window.top, SWP_NOMOVE );
1066 /* Retrieve the window position */
1067 point.x = point.y = 0;
1068 ClientToScreen( p_vout->p_sys->hwnd, &point );
1072 if( i_query == VOUT_REPARENT ) d = (HWND)va_arg( args, int );
1075 vlc_mutex_lock( &p_vout->p_sys->lock );
1076 p_vout->p_sys->hparent = 0;
1077 vlc_mutex_unlock( &p_vout->p_sys->lock );
1078 SetParent( p_vout->p_sys->hwnd, 0 );
1079 p_vout->p_sys->i_window_style =
1080 WS_CLIPCHILDREN | WS_OVERLAPPEDWINDOW | WS_SIZEBOX;
1081 SetWindowLong( p_vout->p_sys->hwnd, GWL_STYLE,
1082 p_vout->p_sys->i_window_style | WS_VISIBLE);
1083 SetWindowLong( p_vout->p_sys->hwnd, GWL_EXSTYLE, WS_EX_APPWINDOW );
1084 SetWindowPos( p_vout->p_sys->hwnd, 0, point.x, point.y, 0, 0,
1085 SWP_NOSIZE|SWP_NOZORDER|SWP_FRAMECHANGED );
1089 vlc_mutex_lock( &p_vout->p_sys->lock );
1090 p_vout->p_sys->hparent = d;
1091 vlc_mutex_unlock( &p_vout->p_sys->lock );
1093 SetParent( p_vout->p_sys->hwnd, d );
1094 p_vout->p_sys->i_window_style = WS_CLIPCHILDREN;
1095 SetWindowLong( p_vout->p_sys->hwnd, GWL_STYLE,
1096 p_vout->p_sys->i_window_style | WS_VISIBLE);
1097 SetWindowLong( p_vout->p_sys->hwnd, GWL_EXSTYLE, WS_EX_APPWINDOW );
1099 /* Retrieve the parent size */
1101 GetClientRect( d, &rect );
1102 SetWindowPos( p_vout->p_sys->hwnd, d, point.x, point.y, rect.right, rect.bottom,
1106 vout_ReleaseWindow( p_vout->p_sys->parent_window );
1109 case VOUT_SET_STAY_ON_TOP:
1110 if( p_vout->p_sys->hparent && !var_GetBool( p_vout, "fullscreen" ) )
1111 return vaControlParentWindow( p_vout, i_query, args );
1113 p_vout->p_sys->b_on_top_change = true;
1117 return VLC_EGENERIC;
1122 /* Internal wrapper over GetWindowPlacement */
1123 static WINDOWPLACEMENT getWindowState(HWND hwnd)
1125 WINDOWPLACEMENT window_placement;
1126 window_placement.length = sizeof(WINDOWPLACEMENT);
1127 GetWindowPlacement( hwnd, &window_placement );
1128 return window_placement;
1131 /* Internal wrapper to call vout_ControlWindow for hparent */
1132 static int vaControlParentWindow( vout_thread_t *p_vout, int i_query,
1135 return vout_ControlWindow( p_vout->p_sys->parent_window, i_query, args );
1138 static int ControlParentWindow( vout_thread_t *p_vout, int i_query, ... )
1143 va_start( args, i_query );
1144 ret = vaControlParentWindow( p_vout, i_query, args );
1149 void Win32ToggleFullscreen( vout_thread_t *p_vout )
1151 HWND hwnd = (p_vout->p_sys->hparent && p_vout->p_sys->hfswnd) ?
1152 p_vout->p_sys->hfswnd : p_vout->p_sys->hwnd;
1154 /* Save the current windows placement/placement to restore
1155 when fullscreen is over */
1156 WINDOWPLACEMENT window_placement = getWindowState( hwnd );
1158 p_vout->b_fullscreen = ! p_vout->b_fullscreen;
1160 /* We want to go to Fullscreen */
1161 if( p_vout->b_fullscreen )
1163 msg_Dbg( p_vout, "entering fullscreen mode" );
1165 /* Change window style, no borders and no title bar */
1166 int i_style = WS_CLIPCHILDREN | WS_VISIBLE;
1167 SetWindowLong( hwnd, GWL_STYLE, i_style );
1169 if( p_vout->p_sys->hparent )
1172 POINT point = {0,0};
1174 ClientToScreen( p_vout->p_sys->hwnd, &point );
1175 GetClientRect( p_vout->p_sys->hwnd, &rect );
1176 SetWindowPos( hwnd, 0, point.x, point.y,
1177 rect.right, rect.bottom,
1178 SWP_NOZORDER|SWP_FRAMECHANGED );
1180 /* Retrieve current window position so fullscreen will happen
1181 *on the right screen */
1182 HMONITOR hmon = MonitorFromWindow(p_vout->p_sys->hparent,
1183 MONITOR_DEFAULTTONEAREST);
1184 MONITORINFO mi = {sizeof(mi)};
1185 if (GetMonitorInfo(hmon, &mi))
1186 SetWindowPos( hwnd, 0,
1189 mi.rcMonitor.right - mi.rcMonitor.left,
1190 mi.rcMonitor.bottom - mi.rcMonitor.top,
1191 SWP_NOZORDER|SWP_FRAMECHANGED );
1196 /* Maximize non embedded window */
1197 ShowWindow( hwnd, SW_SHOWMAXIMIZED );
1200 if( p_vout->p_sys->hparent )
1202 /* Hide the previous window */
1204 GetClientRect( hwnd, &rect );
1205 SetParent( p_vout->p_sys->hwnd, hwnd );
1206 SetWindowPos( p_vout->p_sys->hwnd, 0, 0, 0,
1207 rect.right, rect.bottom,
1208 SWP_NOZORDER|SWP_FRAMECHANGED );
1211 HWND topLevelParent = GetParent( p_vout->p_sys->hparent );
1213 HWND topLevelParent = GetAncestor( p_vout->p_sys->hparent, GA_ROOT );
1215 ShowWindow( topLevelParent, SW_HIDE );
1218 SetForegroundWindow( hwnd );
1222 msg_Dbg( p_vout, "leaving fullscreen mode" );
1223 /* Change window style, no borders and no title bar */
1224 SetWindowLong( hwnd, GWL_STYLE, p_vout->p_sys->i_window_style );
1226 if( p_vout->p_sys->hparent )
1229 GetClientRect( p_vout->p_sys->hparent, &rect );
1230 SetParent( p_vout->p_sys->hwnd, p_vout->p_sys->hparent );
1231 SetWindowPos( p_vout->p_sys->hwnd, 0, 0, 0,
1232 rect.right, rect.bottom,
1233 SWP_NOZORDER|SWP_FRAMECHANGED );
1236 HWND topLevelParent = GetParent( p_vout->p_sys->hparent );
1238 HWND topLevelParent = GetAncestor( p_vout->p_sys->hparent, GA_ROOT );
1240 ShowWindow( topLevelParent, SW_SHOW );
1241 SetForegroundWindow( p_vout->p_sys->hparent );
1242 ShowWindow( hwnd, SW_HIDE );
1246 /* return to normal window for non embedded vout */
1247 SetWindowPlacement( hwnd, &window_placement );
1248 ShowWindow( hwnd, SW_SHOWNORMAL );
1251 /* Make sure the mouse cursor is displayed */
1252 PostMessage( p_vout->p_sys->hwnd, WM_VLC_SHOW_MOUSE, 0, 0 );
1256 /* Update the object variable and trigger callback */
1257 val.b_bool = p_vout->b_fullscreen;
1258 var_Set( p_vout, "fullscreen", val );