1 /*****************************************************************************
2 * events.c: Windows DirectX video output events handler
3 *****************************************************************************
4 * Copyright (C) 2001-2009 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_vout_window.h>
51 #ifdef MODULE_NAME_IS_directx
54 #ifdef MODULE_NAME_IS_direct3d
57 #ifdef MODULE_NAME_IS_glwin32
65 #include <vlc_windows_interfaces.h>
70 //WINSHELLAPI BOOL WINAPI SHFullScreen(HWND hwndRequester, DWORD dwState);
73 /*#if defined(UNDER_CE) && !defined(__PLUGIN__) --FIXME*/
74 /*# define SHFS_SHOWSIPBUTTON 0x0004
75 # define SHFS_HIDESIPBUTTON 0x0008
76 # define MENU_HEIGHT 26
77 BOOL SHFullScreen(HWND hwndRequester, DWORD dwState);
81 /*****************************************************************************
83 *****************************************************************************/
84 static int DirectXCreateWindow( vout_thread_t *p_vout );
85 static void DirectXCloseWindow ( vout_thread_t *p_vout );
86 static long FAR PASCAL DirectXEventProc( HWND, UINT, WPARAM, LPARAM );
88 static int Control( vout_thread_t *p_vout, int i_query, va_list args );
89 static int vaControlParentWindow( vout_thread_t *, int, va_list );
91 static void DirectXPopupMenu( event_thread_t *p_event, bool b_open )
95 var_Set( p_event->p_libvlc, "intf-popupmenu", val );
98 static int DirectXConvertKey( int i_key );
100 /*****************************************************************************
101 * EventThread: Create video window & handle its messages
102 *****************************************************************************
103 * This function creates a video window and then enters an infinite loop
104 * that handles the messages sent to that window.
105 * The main goal of this thread is to isolate the Win32 PeekMessage function
106 * because this one can block for a long time.
107 *****************************************************************************/
108 void* EventThread( vlc_object_t *p_this )
110 event_thread_t *p_event = (event_thread_t *)p_this;
112 POINT old_mouse_pos = {0,0}, mouse_pos;
114 unsigned int i_width, i_height, i_x, i_y;
116 int canc = vlc_savecancel ();
119 p_event->p_vout->pf_control = Control;
121 /* Create a window for the video */
122 /* Creating a window under Windows also initializes the thread's event
124 if( DirectXCreateWindow( p_event->p_vout ) )
126 vlc_restorecancel (canc);
130 /* Signal the creation of the window */
131 SetEvent( p_event->window_ready );
134 /* Set power management stuff */
135 if( (hkernel32 = GetModuleHandle( _T("KERNEL32") ) ) )
137 ULONG (WINAPI* OurSetThreadExecutionState)( ULONG );
139 OurSetThreadExecutionState = (ULONG (WINAPI*)( ULONG ))
140 GetProcAddress( hkernel32, _T("SetThreadExecutionState") );
142 if( OurSetThreadExecutionState )
143 /* Prevent monitor from powering off */
144 OurSetThreadExecutionState( ES_DISPLAY_REQUIRED | ES_CONTINUOUS );
146 msg_Dbg( p_event, "no support for SetThreadExecutionState()" );
151 /* GetMessage will sleep if there's no message in the queue */
152 while( vlc_object_alive (p_event) && GetMessage( &msg, 0, 0, 0 ) )
154 /* Check if we are asked to exit */
155 if( !vlc_object_alive (p_event) )
158 switch( msg.message )
162 vout_PlacePicture( p_event->p_vout,
163 p_event->p_vout->p_sys->i_window_width,
164 p_event->p_vout->p_sys->i_window_height,
165 &i_x, &i_y, &i_width, &i_height );
167 if( msg.hwnd == p_event->p_vout->p_sys->hvideownd )
173 if( i_width && i_height )
175 val.i_int = ( GET_X_LPARAM(msg.lParam) - i_x ) *
176 p_event->p_vout->fmt_in.i_visible_width / i_width +
177 p_event->p_vout->fmt_in.i_x_offset;
178 var_Set( p_event->p_vout, "mouse-x", val );
179 val.i_int = ( GET_Y_LPARAM(msg.lParam) - i_y ) *
180 p_event->p_vout->fmt_in.i_visible_height / i_height +
181 p_event->p_vout->fmt_in.i_y_offset;
182 var_Set( p_event->p_vout, "mouse-y", val );
184 var_SetBool( p_event->p_vout, "mouse-moved", true );
188 GetCursorPos( &mouse_pos );
189 if( (abs(mouse_pos.x - old_mouse_pos.x) > 2 ||
190 (abs(mouse_pos.y - old_mouse_pos.y)) > 2 ) )
192 GetCursorPos( &old_mouse_pos );
193 p_event->p_vout->p_sys->i_lastmoved = mdate();
195 if( p_event->p_vout->p_sys->b_cursor_hidden )
197 p_event->p_vout->p_sys->b_cursor_hidden = 0;
203 case WM_VLC_HIDE_MOUSE:
204 if( p_event->p_vout->p_sys->b_cursor_hidden ) break;
205 p_event->p_vout->p_sys->b_cursor_hidden = true;
206 GetCursorPos( &old_mouse_pos );
210 case WM_VLC_SHOW_MOUSE:
211 if( !p_event->p_vout->p_sys->b_cursor_hidden ) break;
212 p_event->p_vout->p_sys->b_cursor_hidden = false;
213 GetCursorPos( &old_mouse_pos );
218 var_Get( p_event->p_vout, "mouse-button-down", &val );
220 var_Set( p_event->p_vout, "mouse-button-down", val );
221 DirectXPopupMenu( p_event, false );
225 var_Get( p_event->p_vout, "mouse-button-down", &val );
227 var_Set( p_event->p_vout, "mouse-button-down", val );
229 var_SetBool( p_event->p_vout, "mouse-clicked", true );
232 case WM_LBUTTONDBLCLK:
233 p_event->p_vout->p_sys->i_changes |= VOUT_FULLSCREEN_CHANGE;
237 var_Get( p_event->p_vout, "mouse-button-down", &val );
239 var_Set( p_event->p_vout, "mouse-button-down", val );
240 DirectXPopupMenu( p_event, false );
244 var_Get( p_event->p_vout, "mouse-button-down", &val );
246 var_Set( p_event->p_vout, "mouse-button-down", val );
250 var_Get( p_event->p_vout, "mouse-button-down", &val );
252 var_Set( p_event->p_vout, "mouse-button-down", val );
253 DirectXPopupMenu( p_event, false );
257 var_Get( p_event->p_vout, "mouse-button-down", &val );
259 var_Set( p_event->p_vout, "mouse-button-down", val );
260 DirectXPopupMenu( p_event, true );
265 /* The key events are first processed here and not translated
266 * into WM_CHAR events because we need to know the status of the
268 val.i_int = DirectXConvertKey( msg.wParam );
271 /* This appears to be a "normal" (ascii) key */
272 val.i_int = tolower( MapVirtualKey( msg.wParam, 2 ) );
277 if( GetKeyState(VK_CONTROL) & 0x8000 )
279 val.i_int |= KEY_MODIFIER_CTRL;
281 if( GetKeyState(VK_SHIFT) & 0x8000 )
283 val.i_int |= KEY_MODIFIER_SHIFT;
285 if( GetKeyState(VK_MENU) & 0x8000 )
287 val.i_int |= KEY_MODIFIER_ALT;
290 var_Set( p_event->p_libvlc, "key-pressed", val );
295 if( GET_WHEEL_DELTA_WPARAM( msg.wParam ) > 0 )
297 val.i_int = KEY_MOUSEWHEELUP;
301 val.i_int = KEY_MOUSEWHEELDOWN;
305 if( GetKeyState(VK_CONTROL) & 0x8000 )
307 val.i_int |= KEY_MODIFIER_CTRL;
309 if( GetKeyState(VK_SHIFT) & 0x8000 )
311 val.i_int |= KEY_MODIFIER_SHIFT;
313 if( GetKeyState(VK_MENU) & 0x8000 )
315 val.i_int |= KEY_MODIFIER_ALT;
318 var_Set( p_event->p_libvlc, "key-pressed", val );
322 case WM_VLC_CHANGE_TEXT:
323 var_Get( p_event->p_vout, "video-title", &val );
324 if( !val.psz_string || !*val.psz_string ) /* Default video title */
326 free( val.psz_string );
328 #ifdef MODULE_NAME_IS_wingdi
329 val.psz_string = strdup( VOUT_TITLE " (WinGDI output)" );
331 #ifdef MODULE_NAME_IS_wingapi
332 val.psz_string = strdup( VOUT_TITLE " (WinGAPI output)" );
334 #ifdef MODULE_NAME_IS_glwin32
335 val.psz_string = strdup( VOUT_TITLE " (OpenGL output)" );
337 #ifdef MODULE_NAME_IS_direct3d
338 val.psz_string = strdup( VOUT_TITLE " (Direct3D output)" );
340 #ifdef MODULE_NAME_IS_directx
341 if( p_event->p_vout->p_sys->b_using_overlay ) val.psz_string =
342 strdup( VOUT_TITLE " (hardware YUV overlay DirectX output)" );
343 else if( p_event->p_vout->p_sys->b_hw_yuv ) val.psz_string =
344 strdup( VOUT_TITLE " (hardware YUV DirectX output)" );
345 else val.psz_string =
346 strdup( VOUT_TITLE " (software RGB DirectX output)" );
351 wchar_t *psz_title = malloc( strlen(val.psz_string) * 2 + 2 );
354 mbstowcs( psz_title, val.psz_string, strlen(val.psz_string)*2);
355 psz_title[strlen(val.psz_string)] = 0;
356 free( val.psz_string ); val.psz_string = (char *)psz_title;
360 SetWindowText( p_event->p_vout->p_sys->hwnd,
361 (LPCTSTR)val.psz_string );
362 if( p_event->p_vout->p_sys->hfswnd )
363 SetWindowText( p_event->p_vout->p_sys->hfswnd,
364 (LPCTSTR)val.psz_string );
365 free( val.psz_string );
369 /* Messages we don't handle directly are dispatched to the
370 * window procedure */
371 TranslateMessage(&msg);
372 DispatchMessage(&msg);
377 } /* End Main loop */
379 /* Check for WM_QUIT if we created the window */
380 if( !p_event->p_vout->p_sys->hparent && msg.message == WM_QUIT )
382 msg_Warn( p_event, "WM_QUIT... should not happen!!" );
383 p_event->p_vout->p_sys->hwnd = NULL; /* Window already destroyed */
386 msg_Dbg( p_event, "DirectXEventThread terminating" );
388 DirectXCloseWindow( p_event->p_vout );
389 vlc_restorecancel (canc);
391 /* clear the changes formerly signaled */
392 p_event->p_vout->p_sys->i_changes = EVENT_THREAD_ENDED;
397 /* following functions are local */
399 /*****************************************************************************
400 * DirectXCreateWindow: create a window for the video.
401 *****************************************************************************
402 * Before creating a direct draw surface, we need to create a window in which
403 * the video will be displayed. This window will also allow us to capture the
405 *****************************************************************************/
406 static int DirectXCreateWindow( vout_thread_t *p_vout )
411 WNDCLASS wc; /* window class components */
412 HICON vlc_icon = NULL;
413 char vlc_path[MAX_PATH+1];
414 int i_style, i_stylex;
416 msg_Dbg( p_vout, "DirectXCreateWindow" );
418 /* Get this module's instance */
419 hInstance = GetModuleHandle(NULL);
421 #ifdef MODULE_NAME_IS_direct3d
422 if( !p_vout->p_sys->b_desktop )
425 vout_window_cfg_t wnd_cfg;
426 memset( &wnd_cfg, 0, sizeof(wnd_cfg) );
427 wnd_cfg.type = VOUT_WINDOW_TYPE_HWND;
428 wnd_cfg.x = p_vout->p_sys->i_window_x;
429 wnd_cfg.y = p_vout->p_sys->i_window_y;
430 wnd_cfg.width = p_vout->p_sys->i_window_width;
431 wnd_cfg.height = p_vout->p_sys->i_window_height;
433 /* If an external window was specified, we'll draw in it. */
434 p_vout->p_sys->parent_window = vout_window_New( VLC_OBJECT(p_vout), NULL, &wnd_cfg );
435 if( p_vout->p_sys->parent_window )
436 p_vout->p_sys->hparent = p_vout->p_sys->parent_window->handle.hwnd;
437 #ifdef MODULE_NAME_IS_direct3d
441 /* Find Program Manager */
442 HWND hwnd = FindWindow( _T("Progman"), NULL );
443 if( hwnd ) hwnd = FindWindowEx( hwnd, NULL, _T("SHELLDLL_DefView"), NULL );
444 if( hwnd ) hwnd = FindWindowEx( hwnd, NULL, _T("SysListView32"), NULL );
446 msg_Err( p_vout, "Couldn't find desktop icon window. Desktop mode can't be established." );
447 p_vout->p_sys->hparent = hwnd;
451 /* We create the window ourself, there is no previous window proc. */
452 p_vout->p_sys->pf_wndproc = NULL;
454 /* Get the Icon from the main app */
457 if( GetModuleFileName( NULL, vlc_path, MAX_PATH ) )
459 vlc_icon = ExtractIcon( hInstance, vlc_path, 0 );
463 /* Fill in the window class structure */
464 wc.style = CS_OWNDC|CS_DBLCLKS; /* style: dbl click */
465 wc.lpfnWndProc = (WNDPROC)DirectXEventProc; /* event handler */
466 wc.cbClsExtra = 0; /* no extra class data */
467 wc.cbWndExtra = 0; /* no extra window data */
468 wc.hInstance = hInstance; /* instance */
469 wc.hIcon = vlc_icon; /* load the vlc big icon */
470 wc.hCursor = LoadCursor(NULL, IDC_ARROW); /* default cursor */
471 wc.hbrBackground = GetStockObject(BLACK_BRUSH); /* background color */
472 wc.lpszMenuName = NULL; /* no menu */
473 wc.lpszClassName = _T("VLC DirectX"); /* use a special class */
475 /* Register the window class */
476 if( !RegisterClass(&wc) )
480 if( vlc_icon ) DestroyIcon( vlc_icon );
482 /* Check why it failed. If it's because one already exists
483 * then fine, otherwise return with an error. */
484 if( !GetClassInfo( hInstance, _T("VLC DirectX"), &wndclass ) )
486 msg_Err( p_vout, "DirectXCreateWindow RegisterClass FAILED (err=%lu)", GetLastError() );
491 /* Register the video sub-window class */
492 wc.lpszClassName = _T("VLC DirectX video"); wc.hIcon = 0;
493 wc.hbrBackground = NULL; /* no background color */
494 if( !RegisterClass(&wc) )
498 /* Check why it failed. If it's because one already exists
499 * then fine, otherwise return with an error. */
500 if( !GetClassInfo( hInstance, _T("VLC DirectX video"), &wndclass ) )
502 msg_Err( p_vout, "DirectXCreateWindow RegisterClass FAILED (err=%lu)", GetLastError() );
507 /* When you create a window you give the dimensions you wish it to
508 * have. Unfortunatly these dimensions will include the borders and
509 * titlebar. We use the following function to find out the size of
510 * the window corresponding to the useable surface we want */
511 rect_window.top = 10;
512 rect_window.left = 10;
513 rect_window.right = rect_window.left + p_vout->p_sys->i_window_width;
514 rect_window.bottom = rect_window.top + p_vout->p_sys->i_window_height;
516 if( var_GetBool( p_vout, "video-deco" ) )
518 /* Open with window decoration */
519 AdjustWindowRect( &rect_window, WS_OVERLAPPEDWINDOW|WS_SIZEBOX, 0 );
520 i_style = WS_OVERLAPPEDWINDOW|WS_SIZEBOX|WS_VISIBLE|WS_CLIPCHILDREN;
525 /* No window decoration */
526 AdjustWindowRect( &rect_window, WS_POPUP, 0 );
527 i_style = WS_POPUP|WS_VISIBLE|WS_CLIPCHILDREN;
528 i_stylex = 0; // WS_EX_TOOLWINDOW; Is TOOLWINDOW really needed ?
529 // It messes up the fullscreen window.
532 if( p_vout->p_sys->hparent )
534 i_style = WS_VISIBLE|WS_CLIPCHILDREN|WS_CHILD;
538 p_vout->p_sys->i_window_style = i_style;
540 /* Create the window */
541 p_vout->p_sys->hwnd =
542 CreateWindowEx( WS_EX_NOPARENTNOTIFY | i_stylex,
543 _T("VLC DirectX"), /* name of window class */
544 _T(VOUT_TITLE) _T(" (DirectX Output)"), /* window title */
545 i_style, /* window style */
546 (p_vout->p_sys->i_window_x < 0) ? CW_USEDEFAULT :
547 (UINT)p_vout->p_sys->i_window_x, /* default X coordinate */
548 (p_vout->p_sys->i_window_y < 0) ? CW_USEDEFAULT :
549 (UINT)p_vout->p_sys->i_window_y, /* default Y coordinate */
550 rect_window.right - rect_window.left, /* window width */
551 rect_window.bottom - rect_window.top, /* window height */
552 p_vout->p_sys->hparent, /* parent window */
553 NULL, /* no menu in this window */
554 hInstance, /* handle of this program instance */
555 (LPVOID)p_vout ); /* send p_vout to WM_CREATE */
557 if( !p_vout->p_sys->hwnd )
559 msg_Warn( p_vout, "DirectXCreateWindow create window FAILED (err=%lu)", GetLastError() );
563 if( p_vout->p_sys->hparent )
567 /* We don't want the window owner to overwrite our client area */
568 i_style = GetWindowLong( p_vout->p_sys->hparent, GWL_STYLE );
570 if( !(i_style & WS_CLIPCHILDREN) )
571 /* Hmmm, apparently this is a blocking call... */
572 SetWindowLong( p_vout->p_sys->hparent, GWL_STYLE,
573 i_style | WS_CLIPCHILDREN );
575 /* Create our fullscreen window */
576 p_vout->p_sys->hfswnd =
577 CreateWindowEx( WS_EX_APPWINDOW, _T("VLC DirectX"),
578 _T(VOUT_TITLE) _T(" (DirectX Output)"),
579 WS_OVERLAPPEDWINDOW|WS_CLIPCHILDREN|WS_SIZEBOX,
580 CW_USEDEFAULT, CW_USEDEFAULT,
581 CW_USEDEFAULT, CW_USEDEFAULT,
582 NULL, NULL, hInstance, NULL );
585 /* Append a "Always On Top" entry in the system menu */
586 hMenu = GetSystemMenu( p_vout->p_sys->hwnd, FALSE );
587 AppendMenu( hMenu, MF_SEPARATOR, 0, _T("") );
588 AppendMenu( hMenu, MF_STRING | MF_UNCHECKED,
589 IDM_TOGGLE_ON_TOP, _T("Always on &Top") );
591 /* Create video sub-window. This sub window will always exactly match
592 * the size of the video, which allows us to use crazy overlay colorkeys
593 * without having them shown outside of the video area. */
594 p_vout->p_sys->hvideownd =
595 CreateWindow( _T("VLC DirectX video"), _T(""), /* window class */
596 WS_CHILD, /* window style, not visible initially */
598 p_vout->render.i_width, /* default width */
599 p_vout->render.i_height, /* default height */
600 p_vout->p_sys->hwnd, /* parent window */
602 (LPVOID)p_vout ); /* send p_vout to WM_CREATE */
604 if( !p_vout->p_sys->hvideownd )
605 msg_Warn( p_vout, "can't create video sub-window" );
607 msg_Dbg( p_vout, "created video sub-window" );
609 /* Now display the window */
610 ShowWindow( p_vout->p_sys->hwnd, SW_SHOW );
615 /*****************************************************************************
616 * DirectXCloseWindow: close the window created by DirectXCreateWindow
617 *****************************************************************************
618 * This function returns all resources allocated by DirectXCreateWindow.
619 *****************************************************************************/
620 static void DirectXCloseWindow( vout_thread_t *p_vout )
622 msg_Dbg( p_vout, "DirectXCloseWindow" );
624 DestroyWindow( p_vout->p_sys->hwnd );
625 if( p_vout->p_sys->hfswnd ) DestroyWindow( p_vout->p_sys->hfswnd );
627 #ifdef MODULE_NAME_IS_direct3d
628 if( !p_vout->p_sys->b_desktop )
630 vout_window_Delete( p_vout->p_sys->parent_window );
631 p_vout->p_sys->hwnd = NULL;
633 /* We don't unregister the Window Class because it could lead to race
634 * conditions and it will be done anyway by the system when the app will
638 /*****************************************************************************
639 * UpdateRects: update clipping rectangles
640 *****************************************************************************
641 * This function is called when the window position or size are changed, and
642 * its job is to update the source and destination RECTs used to display the
644 *****************************************************************************/
645 void UpdateRects( vout_thread_t *p_vout, bool b_force )
647 #define rect_src p_vout->p_sys->rect_src
648 #define rect_src_clipped p_vout->p_sys->rect_src_clipped
649 #define rect_dest p_vout->p_sys->rect_dest
650 #define rect_dest_clipped p_vout->p_sys->rect_dest_clipped
652 unsigned int i_width, i_height, i_x, i_y;
657 /* Retrieve the window size */
658 GetClientRect( p_vout->p_sys->hwnd, &rect );
660 /* Retrieve the window position */
661 point.x = point.y = 0;
662 ClientToScreen( p_vout->p_sys->hwnd, &point );
664 /* If nothing changed, we can return */
666 && p_vout->p_sys->i_window_width == rect.right
667 && p_vout->p_sys->i_window_height == rect.bottom
668 && p_vout->p_sys->i_window_x == point.x
669 && p_vout->p_sys->i_window_y == point.y )
674 /* Update the window position and size */
675 p_vout->p_sys->i_window_x = point.x;
676 p_vout->p_sys->i_window_y = point.y;
677 p_vout->p_sys->i_window_width = rect.right;
678 p_vout->p_sys->i_window_height = rect.bottom;
680 vout_PlacePicture( p_vout, rect.right, rect.bottom,
681 &i_x, &i_y, &i_width, &i_height );
683 if( p_vout->p_sys->hvideownd )
684 SetWindowPos( p_vout->p_sys->hvideownd, 0,
685 i_x, i_y, i_width, i_height,
686 SWP_NOCOPYBITS|SWP_NOZORDER|SWP_ASYNCWINDOWPOS );
688 /* Destination image position and dimensions */
689 rect_dest.left = point.x + i_x;
690 rect_dest.right = rect_dest.left + i_width;
691 rect_dest.top = point.y + i_y;
692 rect_dest.bottom = rect_dest.top + i_height;
694 #ifdef MODULE_NAME_IS_directx
695 /* Apply overlay hardware constraints */
696 if( p_vout->p_sys->b_using_overlay )
698 if( p_vout->p_sys->i_align_dest_boundary )
699 rect_dest.left = ( rect_dest.left +
700 p_vout->p_sys->i_align_dest_boundary / 2 ) &
701 ~p_vout->p_sys->i_align_dest_boundary;
703 if( p_vout->p_sys->i_align_dest_size )
704 rect_dest.right = (( rect_dest.right - rect_dest.left +
705 p_vout->p_sys->i_align_dest_size / 2 ) &
706 ~p_vout->p_sys->i_align_dest_size) + rect_dest.left;
709 /* UpdateOverlay directdraw function doesn't automatically clip to the
710 * display size so we need to do it otherwise it will fail */
712 /* Clip the destination window */
713 if( !IntersectRect( &rect_dest_clipped, &rect_dest,
714 &p_vout->p_sys->rect_display ) )
716 SetRectEmpty( &rect_src_clipped );
721 msg_Dbg( p_vout, "DirectXUpdateRects image_dst_clipped coords:"
723 rect_dest_clipped.left, rect_dest_clipped.top,
724 rect_dest_clipped.right, rect_dest_clipped.bottom );
727 #else /* MODULE_NAME_IS_directx */
729 /* AFAIK, there are no clipping constraints in Direct3D, OpenGL and GDI */
730 rect_dest_clipped = rect_dest;
734 /* the 2 following lines are to fix a bug when clicking on the desktop */
735 if( (rect_dest_clipped.right - rect_dest_clipped.left)==0 ||
736 (rect_dest_clipped.bottom - rect_dest_clipped.top)==0 )
738 SetRectEmpty( &rect_src_clipped );
742 /* src image dimensions */
745 rect_src.right = p_vout->render.i_width;
746 rect_src.bottom = p_vout->render.i_height;
748 /* Clip the source image */
749 rect_src_clipped.left = p_vout->fmt_out.i_x_offset +
750 (rect_dest_clipped.left - rect_dest.left) *
751 p_vout->fmt_out.i_visible_width / (rect_dest.right - rect_dest.left);
752 rect_src_clipped.right = p_vout->fmt_out.i_x_offset +
753 p_vout->fmt_out.i_visible_width -
754 (rect_dest.right - rect_dest_clipped.right) *
755 p_vout->fmt_out.i_visible_width / (rect_dest.right - rect_dest.left);
756 rect_src_clipped.top = p_vout->fmt_out.i_y_offset +
757 (rect_dest_clipped.top - rect_dest.top) *
758 p_vout->fmt_out.i_visible_height / (rect_dest.bottom - rect_dest.top);
759 rect_src_clipped.bottom = p_vout->fmt_out.i_y_offset +
760 p_vout->fmt_out.i_visible_height -
761 (rect_dest.bottom - rect_dest_clipped.bottom) *
762 p_vout->fmt_out.i_visible_height / (rect_dest.bottom - rect_dest.top);
764 #ifdef MODULE_NAME_IS_directx
765 /* Apply overlay hardware constraints */
766 if( p_vout->p_sys->b_using_overlay )
768 if( p_vout->p_sys->i_align_src_boundary )
769 rect_src_clipped.left = ( rect_src_clipped.left +
770 p_vout->p_sys->i_align_src_boundary / 2 ) &
771 ~p_vout->p_sys->i_align_src_boundary;
773 if( p_vout->p_sys->i_align_src_size )
774 rect_src_clipped.right = (( rect_src_clipped.right -
775 rect_src_clipped.left +
776 p_vout->p_sys->i_align_src_size / 2 ) &
777 ~p_vout->p_sys->i_align_src_size) + rect_src_clipped.left;
782 msg_Dbg( p_vout, "DirectXUpdateRects image_src_clipped"
783 " coords: %li,%li,%li,%li",
784 rect_src_clipped.left, rect_src_clipped.top,
785 rect_src_clipped.right, rect_src_clipped.bottom );
788 #ifdef MODULE_NAME_IS_directx
789 /* The destination coordinates need to be relative to the current
790 * directdraw primary surface (display) */
791 rect_dest_clipped.left -= p_vout->p_sys->rect_display.left;
792 rect_dest_clipped.right -= p_vout->p_sys->rect_display.left;
793 rect_dest_clipped.top -= p_vout->p_sys->rect_display.top;
794 rect_dest_clipped.bottom -= p_vout->p_sys->rect_display.top;
796 if( p_vout->p_sys->b_using_overlay )
797 DirectDrawUpdateOverlay( p_vout );
801 /* Windows 7 taskbar thumbnail code */
802 LPTASKBARLIST3 p_taskbl;
803 OSVERSIONINFO winVer;
804 winVer.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
805 if( GetVersionEx(&winVer) && winVer.dwMajorVersion > 5 )
809 if( S_OK == CoCreateInstance( &clsid_ITaskbarList,
810 NULL, CLSCTX_INPROC_SERVER,
814 RECT rect_video, rect_parent, rect_relative;
815 HWND hroot = GetAncestor(p_vout->p_sys->hwnd,GA_ROOT);
817 p_taskbl->vt->HrInit(p_taskbl);
818 GetWindowRect(p_vout->p_sys->hvideownd, &rect_video);
819 GetWindowRect(hroot, &rect_parent);
820 rect_relative.left = rect_video.left - rect_parent.left - 8;
821 rect_relative.right = rect_video.right - rect_video.left + rect_relative.left;
822 rect_relative.top = rect_video.top - rect_parent.top - 10;
823 rect_relative.bottom = rect_video.bottom - rect_video.top + rect_relative.top - 25;
825 if (S_OK != p_taskbl->vt->SetThumbnailClip(p_taskbl, hroot, &rect_relative))
826 msg_Err( p_vout, "SetThumbNailClip failed");
828 p_taskbl->vt->Release(p_taskbl);
833 /* Signal the change in size/position */
834 p_vout->p_sys->i_changes |= DX_POSITION_CHANGE;
837 #undef rect_src_clipped
839 #undef rect_dest_clipped
842 /*****************************************************************************
843 * DirectXEventProc: This is the window event processing function.
844 *****************************************************************************
845 * On Windows, when you create a window you have to attach an event processing
846 * function to it. The aim of this function is to manage "Queued Messages" and
847 * "Nonqueued Messages".
848 * Queued Messages are those picked up and retransmitted by vout_Manage
849 * (using the GetMessage and DispatchMessage functions).
850 * Nonqueued Messages are those that Windows will send directly to this
851 * procedure (like WM_DESTROY, WM_WINDOWPOSCHANGED...)
852 *****************************************************************************/
853 static long FAR PASCAL DirectXEventProc( HWND hwnd, UINT message,
854 WPARAM wParam, LPARAM lParam )
856 vout_thread_t *p_vout;
858 if( message == WM_CREATE )
860 /* Store p_vout for future use */
861 p_vout = (vout_thread_t *)((CREATESTRUCT *)lParam)->lpCreateParams;
862 SetWindowLongPtr( hwnd, GWLP_USERDATA, (LONG_PTR)p_vout );
867 LONG_PTR p_user_data = GetWindowLongPtr( hwnd, GWLP_USERDATA );
868 p_vout = (vout_thread_t *)p_user_data;
871 /* Hmmm mozilla does manage somehow to save the pointer to our
872 * windowproc and still calls it after the vout has been closed. */
873 return DefWindowProc(hwnd, message, wParam, lParam);
878 /* Catch the screensaver and the monitor turn-off */
879 if( message == WM_SYSCOMMAND &&
880 ( (wParam & 0xFFF0) == SC_SCREENSAVE || (wParam & 0xFFF0) == SC_MONITORPOWER ) )
882 //if( p_vout ) msg_Dbg( p_vout, "WinProc WM_SYSCOMMAND screensaver" );
883 return 0; /* this stops them from happening */
887 if( hwnd == p_vout->p_sys->hvideownd )
891 #ifdef MODULE_NAME_IS_directx
893 /* For overlay, we need to erase background */
894 return !p_vout->p_sys->b_using_overlay ?
895 1 : DefWindowProc(hwnd, message, wParam, lParam);
898 ** For overlay, DefWindowProc() will erase dirty regions
900 ** For non-overlay, vout will paint the whole window at
901 ** regular interval, therefore dirty regions can be ignored
902 ** to minimize repaint.
904 if( !p_vout->p_sys->b_using_overlay )
906 ValidateRect(hwnd, NULL);
908 // fall through to default
911 ** For OpenGL and Direct3D, vout will update the whole
912 ** window at regular interval, therefore dirty region
913 ** can be ignored to minimize repaint.
916 /* nothing to erase */
919 /* nothing to repaint */
920 ValidateRect(hwnd, NULL);
924 return DefWindowProc(hwnd, message, wParam, lParam);
931 case WM_WINDOWPOSCHANGED:
932 UpdateRects( p_vout, true );
935 /* the user wants to close the window */
938 playlist_t * p_playlist = pl_Hold( p_vout );
941 playlist_Stop( p_playlist );
942 pl_Release( p_vout );
947 /* the window has been closed so shut down everything now */
949 msg_Dbg( p_vout, "WinProc WM_DESTROY" );
950 /* just destroy the window */
951 PostQuitMessage( 0 );
957 case IDM_TOGGLE_ON_TOP: /* toggle the "on top" status */
960 msg_Dbg( p_vout, "WinProc WM_SYSCOMMAND: IDM_TOGGLE_ON_TOP");
962 /* Change the current value */
963 var_Get( p_vout, "video-on-top", &val );
964 val.b_bool = !val.b_bool;
965 var_Set( p_vout, "video-on-top", val );
974 return DefWindowProc(hwnd, message, wParam, lParam);
977 #ifdef MODULE_NAME_IS_wingapi
978 p_vout->p_sys->b_focus = false;
979 if( !p_vout->p_sys->b_parent_focus ) GXSuspend();
982 if( hwnd == p_vout->p_sys->hfswnd )
984 HWND htbar = FindWindow( _T("HHTaskbar"), NULL );
985 ShowWindow( htbar, SW_SHOW );
988 if( !p_vout->p_sys->hparent ||
989 hwnd == p_vout->p_sys->hfswnd )
991 SHFullScreen( hwnd, SHFS_SHOWSIPBUTTON );
997 #ifdef MODULE_NAME_IS_wingapi
998 p_vout->p_sys->b_focus = true;
1002 if( p_vout->p_sys->hparent &&
1003 hwnd != p_vout->p_sys->hfswnd && p_vout->b_fullscreen )
1004 p_vout->p_sys->i_changes |= VOUT_FULLSCREEN_CHANGE;
1006 if( hwnd == p_vout->p_sys->hfswnd )
1008 HWND htbar = FindWindow( _T("HHTaskbar"), NULL );
1009 ShowWindow( htbar, SW_HIDE );
1012 if( !p_vout->p_sys->hparent ||
1013 hwnd == p_vout->p_sys->hfswnd )
1015 SHFullScreen( hwnd, SHFS_HIDESIPBUTTON );
1021 //msg_Dbg( p_vout, "WinProc WM Default %i", message );
1025 /* Let windows handle the message */
1026 return DefWindowProc(hwnd, message, wParam, lParam);
1034 } dxkeys_to_vlckeys[] =
1036 { VK_F1, KEY_F1 }, { VK_F2, KEY_F2 }, { VK_F3, KEY_F3 }, { VK_F4, KEY_F4 },
1037 { VK_F5, KEY_F5 }, { VK_F6, KEY_F6 }, { VK_F7, KEY_F7 }, { VK_F8, KEY_F8 },
1038 { VK_F9, KEY_F9 }, { VK_F10, KEY_F10 }, { VK_F11, KEY_F11 },
1039 { VK_F12, KEY_F12 },
1041 { VK_RETURN, KEY_ENTER },
1042 { VK_SPACE, KEY_SPACE },
1043 { VK_ESCAPE, KEY_ESC },
1045 { VK_LEFT, KEY_LEFT },
1046 { VK_RIGHT, KEY_RIGHT },
1048 { VK_DOWN, KEY_DOWN },
1050 { VK_HOME, KEY_HOME },
1051 { VK_END, KEY_END },
1052 { VK_PRIOR, KEY_PAGEUP },
1053 { VK_NEXT, KEY_PAGEDOWN },
1055 { VK_INSERT, KEY_INSERT },
1056 { VK_DELETE, KEY_DELETE },
1062 { VK_BROWSER_BACK, KEY_BROWSER_BACK },
1063 { VK_BROWSER_FORWARD, KEY_BROWSER_FORWARD },
1064 { VK_BROWSER_REFRESH, KEY_BROWSER_REFRESH },
1065 { VK_BROWSER_STOP, KEY_BROWSER_STOP },
1066 { VK_BROWSER_SEARCH, KEY_BROWSER_SEARCH },
1067 { VK_BROWSER_FAVORITES, KEY_BROWSER_FAVORITES },
1068 { VK_BROWSER_HOME, KEY_BROWSER_HOME },
1069 { VK_VOLUME_MUTE, KEY_VOLUME_MUTE },
1070 { VK_VOLUME_DOWN, KEY_VOLUME_DOWN },
1071 { VK_VOLUME_UP, KEY_VOLUME_UP },
1072 { VK_MEDIA_NEXT_TRACK, KEY_MEDIA_NEXT_TRACK },
1073 { VK_MEDIA_PREV_TRACK, KEY_MEDIA_PREV_TRACK },
1074 { VK_MEDIA_STOP, KEY_MEDIA_STOP },
1075 { VK_MEDIA_PLAY_PAUSE, KEY_MEDIA_PLAY_PAUSE },
1080 static int DirectXConvertKey( int i_key )
1084 for( i = 0; dxkeys_to_vlckeys[i].i_dxkey != 0; i++ )
1086 if( dxkeys_to_vlckeys[i].i_dxkey == i_key )
1088 return dxkeys_to_vlckeys[i].i_vlckey;
1095 /*****************************************************************************
1096 * Control: control facility for the vout
1097 *****************************************************************************/
1098 static int Control( vout_thread_t *p_vout, int i_query, va_list args )
1105 if( p_vout->p_sys->parent_window )
1106 return vaControlParentWindow( p_vout, i_query, args );
1108 /* Update dimensions */
1109 rect_window.top = rect_window.left = 0;
1110 rect_window.right = va_arg( args, unsigned int );
1111 rect_window.bottom = va_arg( args, unsigned int );
1112 if( !rect_window.right ) rect_window.right = p_vout->i_window_width;
1113 if( !rect_window.bottom ) rect_window.bottom = p_vout->i_window_height;
1114 AdjustWindowRect( &rect_window, p_vout->p_sys->i_window_style, 0 );
1116 SetWindowPos( p_vout->p_sys->hwnd, 0, 0, 0,
1117 rect_window.right - rect_window.left,
1118 rect_window.bottom - rect_window.top, SWP_NOMOVE );
1122 case VOUT_SET_STAY_ON_TOP:
1123 if( p_vout->p_sys->hparent && !var_GetBool( p_vout, "fullscreen" ) )
1124 return vaControlParentWindow( p_vout, i_query, args );
1126 p_vout->p_sys->b_on_top_change = true;
1130 return VLC_EGENERIC;
1135 /* Internal wrapper over GetWindowPlacement */
1136 static WINDOWPLACEMENT getWindowState(HWND hwnd)
1138 WINDOWPLACEMENT window_placement;
1139 window_placement.length = sizeof(WINDOWPLACEMENT);
1140 GetWindowPlacement( hwnd, &window_placement );
1141 return window_placement;
1144 /* Internal wrapper to call vout_ControlWindow for hparent */
1145 static int vaControlParentWindow( vout_thread_t *p_vout, int i_query,
1152 const unsigned i_width = va_arg(args, unsigned);
1153 const unsigned i_height = va_arg(args, unsigned);
1154 return vout_window_SetSize( p_vout->p_sys->parent_window, i_width, i_height );
1156 case VOUT_SET_STAY_ON_TOP:
1158 const bool is_on_top = va_arg(args, int);
1159 return vout_window_SetOnTop( p_vout->p_sys->parent_window, is_on_top );
1162 return VLC_EGENERIC;
1167 static int ControlParentWindow( vout_thread_t *p_vout, int i_query, ... )
1172 va_start( args, i_query );
1173 ret = vaControlParentWindow( p_vout, i_query, args );
1179 void Win32ToggleFullscreen( vout_thread_t *p_vout )
1181 HWND hwnd = (p_vout->p_sys->hparent && p_vout->p_sys->hfswnd) ?
1182 p_vout->p_sys->hfswnd : p_vout->p_sys->hwnd;
1184 /* Save the current windows placement/placement to restore
1185 when fullscreen is over */
1186 WINDOWPLACEMENT window_placement = getWindowState( hwnd );
1188 p_vout->b_fullscreen = ! p_vout->b_fullscreen;
1190 /* We want to go to Fullscreen */
1191 if( p_vout->b_fullscreen )
1193 msg_Dbg( p_vout, "entering fullscreen mode" );
1195 /* Change window style, no borders and no title bar */
1196 int i_style = WS_CLIPCHILDREN | WS_VISIBLE;
1197 SetWindowLong( hwnd, GWL_STYLE, i_style );
1199 if( p_vout->p_sys->hparent )
1202 POINT point = {0,0};
1204 ClientToScreen( p_vout->p_sys->hwnd, &point );
1205 GetClientRect( p_vout->p_sys->hwnd, &rect );
1206 SetWindowPos( hwnd, 0, point.x, point.y,
1207 rect.right, rect.bottom,
1208 SWP_NOZORDER|SWP_FRAMECHANGED );
1210 /* Retrieve current window position so fullscreen will happen
1211 *on the right screen */
1212 HMONITOR hmon = MonitorFromWindow(p_vout->p_sys->hparent,
1213 MONITOR_DEFAULTTONEAREST);
1215 if (GetMonitorInfo(hmon, &mi))
1216 SetWindowPos( hwnd, 0,
1219 mi.rcMonitor.right - mi.rcMonitor.left,
1220 mi.rcMonitor.bottom - mi.rcMonitor.top,
1221 SWP_NOZORDER|SWP_FRAMECHANGED );
1226 /* Maximize non embedded window */
1227 ShowWindow( hwnd, SW_SHOWMAXIMIZED );
1230 if( p_vout->p_sys->hparent )
1232 /* Hide the previous window */
1234 GetClientRect( hwnd, &rect );
1235 SetParent( p_vout->p_sys->hwnd, hwnd );
1236 SetWindowPos( p_vout->p_sys->hwnd, 0, 0, 0,
1237 rect.right, rect.bottom,
1238 SWP_NOZORDER|SWP_FRAMECHANGED );
1241 HWND topLevelParent = GetParent( p_vout->p_sys->hparent );
1243 HWND topLevelParent = GetAncestor( p_vout->p_sys->hparent, GA_ROOT );
1245 ShowWindow( topLevelParent, SW_HIDE );
1248 SetForegroundWindow( hwnd );
1252 msg_Dbg( p_vout, "leaving fullscreen mode" );
1253 /* Change window style, no borders and no title bar */
1254 SetWindowLong( hwnd, GWL_STYLE, p_vout->p_sys->i_window_style );
1256 if( p_vout->p_sys->hparent )
1259 GetClientRect( p_vout->p_sys->hparent, &rect );
1260 SetParent( p_vout->p_sys->hwnd, p_vout->p_sys->hparent );
1261 SetWindowPos( p_vout->p_sys->hwnd, 0, 0, 0,
1262 rect.right, rect.bottom,
1263 SWP_NOZORDER|SWP_FRAMECHANGED );
1266 HWND topLevelParent = GetParent( p_vout->p_sys->hparent );
1268 HWND topLevelParent = GetAncestor( p_vout->p_sys->hparent, GA_ROOT );
1270 ShowWindow( topLevelParent, SW_SHOW );
1271 SetForegroundWindow( p_vout->p_sys->hparent );
1272 ShowWindow( hwnd, SW_HIDE );
1276 /* return to normal window for non embedded vout */
1277 SetWindowPlacement( hwnd, &window_placement );
1278 ShowWindow( hwnd, SW_SHOWNORMAL );
1281 /* Make sure the mouse cursor is displayed */
1282 PostMessage( p_vout->p_sys->hwnd, WM_VLC_SHOW_MOUSE, 0, 0 );
1285 /* Update the object variable and trigger callback */
1286 var_SetBool( p_vout, "fullscreen", p_vout->b_fullscreen );
1290 void DisableScreensaver( vout_thread_t *p_vout )
1292 /* disable screensaver by temporarily changing system settings */
1293 p_vout->p_sys->i_spi_lowpowertimeout = 0;
1294 p_vout->p_sys->i_spi_powerofftimeout = 0;
1295 p_vout->p_sys->i_spi_screensavetimeout = 0;
1296 if( var_GetBool( p_vout, "disable-screensaver" ) )
1298 msg_Dbg(p_vout, "disabling screen saver");
1299 SystemParametersInfo(SPI_GETLOWPOWERTIMEOUT,
1300 0, &(p_vout->p_sys->i_spi_lowpowertimeout), 0);
1301 if( 0 != p_vout->p_sys->i_spi_lowpowertimeout ) {
1302 SystemParametersInfo(SPI_SETLOWPOWERTIMEOUT, 0, NULL, 0);
1304 SystemParametersInfo(SPI_GETPOWEROFFTIMEOUT, 0,
1305 &(p_vout->p_sys->i_spi_powerofftimeout), 0);
1306 if( 0 != p_vout->p_sys->i_spi_powerofftimeout ) {
1307 SystemParametersInfo(SPI_SETPOWEROFFTIMEOUT, 0, NULL, 0);
1309 SystemParametersInfo(SPI_GETSCREENSAVETIMEOUT, 0,
1310 &(p_vout->p_sys->i_spi_screensavetimeout), 0);
1311 if( 0 != p_vout->p_sys->i_spi_screensavetimeout ) {
1312 SystemParametersInfo(SPI_SETSCREENSAVETIMEOUT, 0, NULL, 0);
1317 void RestoreScreensaver( vout_thread_t *p_vout )
1319 /* restore screensaver system settings */
1320 if( 0 != p_vout->p_sys->i_spi_lowpowertimeout ) {
1321 SystemParametersInfo(SPI_SETLOWPOWERTIMEOUT,
1322 p_vout->p_sys->i_spi_lowpowertimeout, NULL, 0);
1324 if( 0 != p_vout->p_sys->i_spi_powerofftimeout ) {
1325 SystemParametersInfo(SPI_SETPOWEROFFTIMEOUT,
1326 p_vout->p_sys->i_spi_powerofftimeout, NULL, 0);
1328 if( 0 != p_vout->p_sys->i_spi_screensavetimeout ) {
1329 SystemParametersInfo(SPI_SETSCREENSAVETIMEOUT,
1330 p_vout->p_sys->i_spi_screensavetimeout, NULL, 0);
1335 int CreateEventThread( vout_thread_t *p_vout )
1337 if( !( p_vout->p_sys->i_changes & SWITCHING_MODE_FLAG ) )
1338 vlc_mutex_init( &p_vout->p_sys->lock );
1340 /* Create the Vout EventThread, this thread is created by us to isolate
1341 * the Win32 PeekMessage function calls. We want to do this because
1342 * Windows can stay blocked inside this call for a long time, and when
1343 * this happens it thus blocks vlc's video_output thread.
1344 * Vout EventThread will take care of the creation of the video
1345 * window (because PeekMessage has to be called from the same thread which
1346 * created the window). */
1347 msg_Dbg( p_vout, "creating Vout EventThread" );
1348 event_thread_t *p_event = p_vout->p_sys->p_event =
1349 vlc_object_create( p_vout, sizeof(event_thread_t) );
1350 p_event->p_vout = p_vout;
1351 p_event->window_ready = CreateEvent( NULL, TRUE, FALSE, NULL );
1352 if( vlc_thread_create( p_event, "Vout Events Thread",
1355 msg_Err( p_vout, "cannot create Vout EventThread" );
1356 CloseHandle( p_event->window_ready );
1357 vlc_object_release( p_event );
1361 WaitForSingleObject( p_event->window_ready, INFINITE );
1362 CloseHandle( p_event->window_ready );
1364 if( p_event->b_error )
1366 msg_Err( p_vout, "Vout EventThread failed" );
1370 vlc_object_attach( p_event, p_vout );
1372 msg_Dbg( p_vout, "Vout EventThread running" );
1376 void StopEventThread( vout_thread_t *p_vout )
1378 if( p_vout->b_fullscreen )
1380 msg_Dbg( p_vout, "Quitting fullscreen" );
1381 Win32ToggleFullscreen( p_vout );
1382 /* Force fullscreen in the core for the next video */
1383 var_SetBool( p_vout, "fullscreen", true );
1386 if( p_vout->p_sys->p_event )
1388 event_thread_t *p_event = p_vout->p_sys->p_event;
1389 vlc_object_detach( p_event );
1391 /* Kill Vout EventThread */
1392 vlc_object_kill( p_event );
1394 /* we need to be sure Vout EventThread won't stay stuck in
1395 * GetMessage, so we send a fake message */
1396 if( p_vout->p_sys->hwnd )
1398 PostMessage( p_vout->p_sys->hwnd, WM_NULL, 0, 0);
1401 vlc_thread_join( p_event );
1402 vlc_object_release( p_event );
1405 if( !( p_vout->p_sys->i_changes & SWITCHING_MODE_FLAG ) )
1406 vlc_mutex_destroy( &p_vout->p_sys->lock );