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
66 //WINSHELLAPI BOOL WINAPI SHFullScreen(HWND hwndRequester, DWORD dwState);
69 /*#if defined(UNDER_CE) && !defined(__PLUGIN__) --FIXME*/
70 /*# define SHFS_SHOWSIPBUTTON 0x0004
71 # define SHFS_HIDESIPBUTTON 0x0008
72 # define MENU_HEIGHT 26
73 BOOL SHFullScreen(HWND hwndRequester, DWORD dwState);
77 /*****************************************************************************
79 *****************************************************************************/
80 static int DirectXCreateWindow( vout_thread_t *p_vout );
81 static void DirectXCloseWindow ( vout_thread_t *p_vout );
82 static long FAR PASCAL DirectXEventProc( HWND, UINT, WPARAM, LPARAM );
84 static int Control( vout_thread_t *p_vout, int i_query, va_list args );
85 static int vaControlParentWindow( vout_thread_t *, int, va_list );
87 static void DirectXPopupMenu( event_thread_t *p_event, bool b_open )
91 var_Set( p_event->p_libvlc, "intf-popupmenu", val );
94 static int DirectXConvertKey( int i_key );
96 /*****************************************************************************
97 * EventThread: Create video window & handle its messages
98 *****************************************************************************
99 * This function creates a video window and then enters an infinite loop
100 * that handles the messages sent to that window.
101 * The main goal of this thread is to isolate the Win32 PeekMessage function
102 * because this one can block for a long time.
103 *****************************************************************************/
104 void* EventThread( vlc_object_t *p_this )
106 event_thread_t *p_event = (event_thread_t *)p_this;
108 POINT old_mouse_pos = {0,0}, mouse_pos;
110 unsigned int i_width, i_height, i_x, i_y;
112 int canc = vlc_savecancel ();
115 p_event->p_vout->pf_control = Control;
117 /* Create a window for the video */
118 /* Creating a window under Windows also initializes the thread's event
120 if( DirectXCreateWindow( p_event->p_vout ) )
122 vlc_restorecancel (canc);
126 /* Signal the creation of the window */
127 SetEvent( p_event->window_ready );
130 /* Set power management stuff */
131 if( (hkernel32 = GetModuleHandle( _T("KERNEL32") ) ) )
133 ULONG (WINAPI* OurSetThreadExecutionState)( ULONG );
135 OurSetThreadExecutionState = (ULONG (WINAPI*)( ULONG ))
136 GetProcAddress( hkernel32, _T("SetThreadExecutionState") );
138 if( OurSetThreadExecutionState )
139 /* Prevent monitor from powering off */
140 OurSetThreadExecutionState( ES_DISPLAY_REQUIRED | ES_CONTINUOUS );
142 msg_Dbg( p_event, "no support for SetThreadExecutionState()" );
147 /* GetMessage will sleep if there's no message in the queue */
148 while( vlc_object_alive (p_event) && GetMessage( &msg, 0, 0, 0 ) )
150 /* Check if we are asked to exit */
151 if( !vlc_object_alive (p_event) )
154 switch( msg.message )
158 vout_PlacePicture( p_event->p_vout,
159 p_event->p_vout->p_sys->i_window_width,
160 p_event->p_vout->p_sys->i_window_height,
161 &i_x, &i_y, &i_width, &i_height );
163 if( msg.hwnd == p_event->p_vout->p_sys->hvideownd )
169 if( i_width && i_height )
171 val.i_int = ( GET_X_LPARAM(msg.lParam) - i_x ) *
172 p_event->p_vout->fmt_in.i_visible_width / i_width +
173 p_event->p_vout->fmt_in.i_x_offset;
174 var_Set( p_event->p_vout, "mouse-x", val );
175 val.i_int = ( GET_Y_LPARAM(msg.lParam) - i_y ) *
176 p_event->p_vout->fmt_in.i_visible_height / i_height +
177 p_event->p_vout->fmt_in.i_y_offset;
178 var_Set( p_event->p_vout, "mouse-y", val );
180 var_SetBool( p_event->p_vout, "mouse-moved", true );
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_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 DirectXCloseWindow( p_event->p_vout );
387 vlc_restorecancel (canc);
389 /* clear the changes formerly signaled */
390 p_event->p_vout->p_sys->i_changes = EVENT_THREAD_ENDED;
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 #ifdef MODULE_NAME_IS_direct3d
420 if( !p_vout->p_sys->b_desktop )
423 vout_window_cfg_t wnd_cfg;
424 memset( &wnd_cfg, 0, sizeof(wnd_cfg) );
425 wnd_cfg.type = VOUT_WINDOW_TYPE_HWND;
426 wnd_cfg.x = p_vout->p_sys->i_window_x;
427 wnd_cfg.y = p_vout->p_sys->i_window_y;
428 wnd_cfg.width = p_vout->p_sys->i_window_width;
429 wnd_cfg.height = p_vout->p_sys->i_window_height;
431 /* If an external window was specified, we'll draw in it. */
432 p_vout->p_sys->parent_window = vout_window_New( VLC_OBJECT(p_vout), NULL, &wnd_cfg );
433 if( p_vout->p_sys->parent_window )
434 p_vout->p_sys->hparent = p_vout->p_sys->parent_window->handle.hwnd;
435 #ifdef MODULE_NAME_IS_direct3d
439 /* Find Program Manager */
440 HWND hwnd = FindWindow( _T("Progman"), NULL );
441 if( hwnd ) hwnd = FindWindowEx( hwnd, NULL, _T("SHELLDLL_DefView"), NULL );
442 if( hwnd ) hwnd = FindWindowEx( hwnd, NULL, _T("SysListView32"), NULL );
444 msg_Err( p_vout, "Couldn't find desktop icon window. Desktop mode can't be established." );
445 p_vout->p_sys->hparent = hwnd;
449 /* We create the window ourself, there is no previous window proc. */
450 p_vout->p_sys->pf_wndproc = NULL;
452 /* Get the Icon from the main app */
455 if( GetModuleFileName( NULL, vlc_path, MAX_PATH ) )
457 vlc_icon = ExtractIcon( hInstance, vlc_path, 0 );
461 /* Fill in the window class structure */
462 wc.style = CS_OWNDC|CS_DBLCLKS; /* style: dbl click */
463 wc.lpfnWndProc = (WNDPROC)DirectXEventProc; /* event handler */
464 wc.cbClsExtra = 0; /* no extra class data */
465 wc.cbWndExtra = 0; /* no extra window data */
466 wc.hInstance = hInstance; /* instance */
467 wc.hIcon = vlc_icon; /* load the vlc big icon */
468 wc.hCursor = LoadCursor(NULL, IDC_ARROW); /* default cursor */
469 wc.hbrBackground = GetStockObject(BLACK_BRUSH); /* background color */
470 wc.lpszMenuName = NULL; /* no menu */
471 wc.lpszClassName = _T("VLC DirectX"); /* use a special class */
473 /* Register the window class */
474 if( !RegisterClass(&wc) )
478 if( vlc_icon ) DestroyIcon( vlc_icon );
480 /* Check why it failed. If it's because one already exists
481 * then fine, otherwise return with an error. */
482 if( !GetClassInfo( hInstance, _T("VLC DirectX"), &wndclass ) )
484 msg_Err( p_vout, "DirectXCreateWindow RegisterClass FAILED (err=%lu)", GetLastError() );
489 /* Register the video sub-window class */
490 wc.lpszClassName = _T("VLC DirectX video"); wc.hIcon = 0;
491 wc.hbrBackground = NULL; /* no background color */
492 if( !RegisterClass(&wc) )
496 /* Check why it failed. If it's because one already exists
497 * then fine, otherwise return with an error. */
498 if( !GetClassInfo( hInstance, _T("VLC DirectX video"), &wndclass ) )
500 msg_Err( p_vout, "DirectXCreateWindow RegisterClass FAILED (err=%lu)", GetLastError() );
505 /* When you create a window you give the dimensions you wish it to
506 * have. Unfortunatly these dimensions will include the borders and
507 * titlebar. We use the following function to find out the size of
508 * the window corresponding to the useable surface we want */
509 rect_window.top = 10;
510 rect_window.left = 10;
511 rect_window.right = rect_window.left + p_vout->p_sys->i_window_width;
512 rect_window.bottom = rect_window.top + p_vout->p_sys->i_window_height;
514 if( var_GetBool( p_vout, "video-deco" ) )
516 /* Open with window decoration */
517 AdjustWindowRect( &rect_window, WS_OVERLAPPEDWINDOW|WS_SIZEBOX, 0 );
518 i_style = WS_OVERLAPPEDWINDOW|WS_SIZEBOX|WS_VISIBLE|WS_CLIPCHILDREN;
523 /* No window decoration */
524 AdjustWindowRect( &rect_window, WS_POPUP, 0 );
525 i_style = WS_POPUP|WS_VISIBLE|WS_CLIPCHILDREN;
526 i_stylex = 0; // WS_EX_TOOLWINDOW; Is TOOLWINDOW really needed ?
527 // It messes up the fullscreen window.
530 if( p_vout->p_sys->hparent )
532 i_style = WS_VISIBLE|WS_CLIPCHILDREN|WS_CHILD;
536 p_vout->p_sys->i_window_style = i_style;
538 /* Create the window */
539 p_vout->p_sys->hwnd =
540 CreateWindowEx( WS_EX_NOPARENTNOTIFY | i_stylex,
541 _T("VLC DirectX"), /* name of window class */
542 _T(VOUT_TITLE) _T(" (DirectX Output)"), /* window title */
543 i_style, /* window style */
544 (p_vout->p_sys->i_window_x < 0) ? CW_USEDEFAULT :
545 (UINT)p_vout->p_sys->i_window_x, /* default X coordinate */
546 (p_vout->p_sys->i_window_y < 0) ? CW_USEDEFAULT :
547 (UINT)p_vout->p_sys->i_window_y, /* default Y coordinate */
548 rect_window.right - rect_window.left, /* window width */
549 rect_window.bottom - rect_window.top, /* window height */
550 p_vout->p_sys->hparent, /* parent window */
551 NULL, /* no menu in this window */
552 hInstance, /* handle of this program instance */
553 (LPVOID)p_vout ); /* send p_vout to WM_CREATE */
555 if( !p_vout->p_sys->hwnd )
557 msg_Warn( p_vout, "DirectXCreateWindow create window FAILED (err=%lu)", GetLastError() );
561 if( p_vout->p_sys->hparent )
565 /* We don't want the window owner to overwrite our client area */
566 i_style = GetWindowLong( p_vout->p_sys->hparent, GWL_STYLE );
568 if( !(i_style & WS_CLIPCHILDREN) )
569 /* Hmmm, apparently this is a blocking call... */
570 SetWindowLong( p_vout->p_sys->hparent, GWL_STYLE,
571 i_style | WS_CLIPCHILDREN );
573 /* Create our fullscreen window */
574 p_vout->p_sys->hfswnd =
575 CreateWindowEx( WS_EX_APPWINDOW, _T("VLC DirectX"),
576 _T(VOUT_TITLE) _T(" (DirectX Output)"),
577 WS_OVERLAPPEDWINDOW|WS_CLIPCHILDREN|WS_SIZEBOX,
578 CW_USEDEFAULT, CW_USEDEFAULT,
579 CW_USEDEFAULT, CW_USEDEFAULT,
580 NULL, NULL, hInstance, NULL );
583 /* Append a "Always On Top" entry in the system menu */
584 hMenu = GetSystemMenu( p_vout->p_sys->hwnd, FALSE );
585 AppendMenu( hMenu, MF_SEPARATOR, 0, _T("") );
586 AppendMenu( hMenu, MF_STRING | MF_UNCHECKED,
587 IDM_TOGGLE_ON_TOP, _T("Always on &Top") );
589 /* Create video sub-window. This sub window will always exactly match
590 * the size of the video, which allows us to use crazy overlay colorkeys
591 * without having them shown outside of the video area. */
592 p_vout->p_sys->hvideownd =
593 CreateWindow( _T("VLC DirectX video"), _T(""), /* window class */
594 WS_CHILD, /* window style, not visible initially */
596 p_vout->render.i_width, /* default width */
597 p_vout->render.i_height, /* default height */
598 p_vout->p_sys->hwnd, /* parent window */
600 (LPVOID)p_vout ); /* send p_vout to WM_CREATE */
602 if( !p_vout->p_sys->hvideownd )
603 msg_Warn( p_vout, "can't create video sub-window" );
605 msg_Dbg( p_vout, "created video sub-window" );
607 /* Now display the window */
608 ShowWindow( p_vout->p_sys->hwnd, SW_SHOW );
613 /*****************************************************************************
614 * DirectXCloseWindow: close the window created by DirectXCreateWindow
615 *****************************************************************************
616 * This function returns all resources allocated by DirectXCreateWindow.
617 *****************************************************************************/
618 static void DirectXCloseWindow( vout_thread_t *p_vout )
620 msg_Dbg( p_vout, "DirectXCloseWindow" );
622 DestroyWindow( p_vout->p_sys->hwnd );
623 if( p_vout->p_sys->hfswnd ) DestroyWindow( p_vout->p_sys->hfswnd );
625 #ifdef MODULE_NAME_IS_direct3d
626 if( !p_vout->p_sys->b_desktop )
628 vout_window_Delete( p_vout->p_sys->parent_window );
629 p_vout->p_sys->hwnd = NULL;
631 /* We don't unregister the Window Class because it could lead to race
632 * conditions and it will be done anyway by the system when the app will
636 /*****************************************************************************
637 * UpdateRects: update clipping rectangles
638 *****************************************************************************
639 * This function is called when the window position or size are changed, and
640 * its job is to update the source and destination RECTs used to display the
642 *****************************************************************************/
643 void UpdateRects( vout_thread_t *p_vout, bool b_force )
645 #define rect_src p_vout->p_sys->rect_src
646 #define rect_src_clipped p_vout->p_sys->rect_src_clipped
647 #define rect_dest p_vout->p_sys->rect_dest
648 #define rect_dest_clipped p_vout->p_sys->rect_dest_clipped
650 int i_width, i_height, i_x, i_y;
655 /* Retrieve the window size */
656 GetClientRect( p_vout->p_sys->hwnd, &rect );
658 /* Retrieve the window position */
659 point.x = point.y = 0;
660 ClientToScreen( p_vout->p_sys->hwnd, &point );
662 /* If nothing changed, we can return */
664 && p_vout->p_sys->i_window_width == rect.right
665 && p_vout->p_sys->i_window_height == rect.bottom
666 && p_vout->p_sys->i_window_x == point.x
667 && p_vout->p_sys->i_window_y == point.y )
672 /* Update the window position and size */
673 p_vout->p_sys->i_window_x = point.x;
674 p_vout->p_sys->i_window_y = point.y;
675 p_vout->p_sys->i_window_width = rect.right;
676 p_vout->p_sys->i_window_height = rect.bottom;
678 vout_PlacePicture( p_vout, rect.right, rect.bottom,
679 &i_x, &i_y, &i_width, &i_height );
681 if( p_vout->p_sys->hvideownd )
682 SetWindowPos( p_vout->p_sys->hvideownd, 0,
683 i_x, i_y, i_width, i_height,
684 SWP_NOCOPYBITS|SWP_NOZORDER|SWP_ASYNCWINDOWPOS );
686 /* Destination image position and dimensions */
687 rect_dest.left = point.x + i_x;
688 rect_dest.right = rect_dest.left + i_width;
689 rect_dest.top = point.y + i_y;
690 rect_dest.bottom = rect_dest.top + i_height;
692 #ifdef MODULE_NAME_IS_directx
693 /* Apply overlay hardware constraints */
694 if( p_vout->p_sys->b_using_overlay )
696 if( p_vout->p_sys->i_align_dest_boundary )
697 rect_dest.left = ( rect_dest.left +
698 p_vout->p_sys->i_align_dest_boundary / 2 ) &
699 ~p_vout->p_sys->i_align_dest_boundary;
701 if( p_vout->p_sys->i_align_dest_size )
702 rect_dest.right = (( rect_dest.right - rect_dest.left +
703 p_vout->p_sys->i_align_dest_size / 2 ) &
704 ~p_vout->p_sys->i_align_dest_size) + rect_dest.left;
707 /* UpdateOverlay directdraw function doesn't automatically clip to the
708 * display size so we need to do it otherwise it will fail */
710 /* Clip the destination window */
711 if( !IntersectRect( &rect_dest_clipped, &rect_dest,
712 &p_vout->p_sys->rect_display ) )
714 SetRectEmpty( &rect_src_clipped );
719 msg_Dbg( p_vout, "DirectXUpdateRects image_dst_clipped coords:"
721 rect_dest_clipped.left, rect_dest_clipped.top,
722 rect_dest_clipped.right, rect_dest_clipped.bottom );
725 #else /* MODULE_NAME_IS_directx */
727 /* AFAIK, there are no clipping constraints in Direct3D, OpenGL and GDI */
728 rect_dest_clipped = rect_dest;
732 /* the 2 following lines are to fix a bug when clicking on the desktop */
733 if( (rect_dest_clipped.right - rect_dest_clipped.left)==0 ||
734 (rect_dest_clipped.bottom - rect_dest_clipped.top)==0 )
736 SetRectEmpty( &rect_src_clipped );
740 /* src image dimensions */
743 rect_src.right = p_vout->render.i_width;
744 rect_src.bottom = p_vout->render.i_height;
746 /* Clip the source image */
747 rect_src_clipped.left = p_vout->fmt_out.i_x_offset +
748 (rect_dest_clipped.left - rect_dest.left) *
749 p_vout->fmt_out.i_visible_width / (rect_dest.right - rect_dest.left);
750 rect_src_clipped.right = p_vout->fmt_out.i_x_offset +
751 p_vout->fmt_out.i_visible_width -
752 (rect_dest.right - rect_dest_clipped.right) *
753 p_vout->fmt_out.i_visible_width / (rect_dest.right - rect_dest.left);
754 rect_src_clipped.top = p_vout->fmt_out.i_y_offset +
755 (rect_dest_clipped.top - rect_dest.top) *
756 p_vout->fmt_out.i_visible_height / (rect_dest.bottom - rect_dest.top);
757 rect_src_clipped.bottom = p_vout->fmt_out.i_y_offset +
758 p_vout->fmt_out.i_visible_height -
759 (rect_dest.bottom - rect_dest_clipped.bottom) *
760 p_vout->fmt_out.i_visible_height / (rect_dest.bottom - rect_dest.top);
762 #ifdef MODULE_NAME_IS_directx
763 /* Apply overlay hardware constraints */
764 if( p_vout->p_sys->b_using_overlay )
766 if( p_vout->p_sys->i_align_src_boundary )
767 rect_src_clipped.left = ( rect_src_clipped.left +
768 p_vout->p_sys->i_align_src_boundary / 2 ) &
769 ~p_vout->p_sys->i_align_src_boundary;
771 if( p_vout->p_sys->i_align_src_size )
772 rect_src_clipped.right = (( rect_src_clipped.right -
773 rect_src_clipped.left +
774 p_vout->p_sys->i_align_src_size / 2 ) &
775 ~p_vout->p_sys->i_align_src_size) + rect_src_clipped.left;
780 msg_Dbg( p_vout, "DirectXUpdateRects image_src_clipped"
781 " coords: %i,%i,%i,%i",
782 rect_src_clipped.left, rect_src_clipped.top,
783 rect_src_clipped.right, rect_src_clipped.bottom );
786 #ifdef MODULE_NAME_IS_directx
787 /* The destination coordinates need to be relative to the current
788 * directdraw primary surface (display) */
789 rect_dest_clipped.left -= p_vout->p_sys->rect_display.left;
790 rect_dest_clipped.right -= p_vout->p_sys->rect_display.left;
791 rect_dest_clipped.top -= p_vout->p_sys->rect_display.top;
792 rect_dest_clipped.bottom -= p_vout->p_sys->rect_display.top;
794 if( p_vout->p_sys->b_using_overlay )
795 DirectDrawUpdateOverlay( p_vout );
798 /* Signal the change in size/position */
799 p_vout->p_sys->i_changes |= DX_POSITION_CHANGE;
802 #undef rect_src_clipped
804 #undef rect_dest_clipped
807 /*****************************************************************************
808 * DirectXEventProc: This is the window event processing function.
809 *****************************************************************************
810 * On Windows, when you create a window you have to attach an event processing
811 * function to it. The aim of this function is to manage "Queued Messages" and
812 * "Nonqueued Messages".
813 * Queued Messages are those picked up and retransmitted by vout_Manage
814 * (using the GetMessage and DispatchMessage functions).
815 * Nonqueued Messages are those that Windows will send directly to this
816 * procedure (like WM_DESTROY, WM_WINDOWPOSCHANGED...)
817 *****************************************************************************/
818 static long FAR PASCAL DirectXEventProc( HWND hwnd, UINT message,
819 WPARAM wParam, LPARAM lParam )
821 vout_thread_t *p_vout;
823 if( message == WM_CREATE )
825 /* Store p_vout for future use */
826 p_vout = (vout_thread_t *)((CREATESTRUCT *)lParam)->lpCreateParams;
827 SetWindowLongPtr( hwnd, GWLP_USERDATA, (LONG_PTR)p_vout );
832 p_vout = (vout_thread_t *)GetWindowLongPtr( hwnd, GWLP_USERDATA );
835 /* Hmmm mozilla does manage somehow to save the pointer to our
836 * windowproc and still calls it after the vout has been closed. */
837 return DefWindowProc(hwnd, message, wParam, lParam);
842 /* Catch the screensaver and the monitor turn-off */
843 if( message == WM_SYSCOMMAND &&
844 ( (wParam & 0xFFF0) == SC_SCREENSAVE || (wParam & 0xFFF0) == SC_MONITORPOWER ) )
846 //if( p_vout ) msg_Dbg( p_vout, "WinProc WM_SYSCOMMAND screensaver" );
847 return 0; /* this stops them from happening */
851 if( hwnd == p_vout->p_sys->hvideownd )
855 #ifdef MODULE_NAME_IS_directx
857 /* For overlay, we need to erase background */
858 return !p_vout->p_sys->b_using_overlay ?
859 1 : DefWindowProc(hwnd, message, wParam, lParam);
862 ** For overlay, DefWindowProc() will erase dirty regions
864 ** For non-overlay, vout will paint the whole window at
865 ** regular interval, therefore dirty regions can be ignored
866 ** to minimize repaint.
868 if( !p_vout->p_sys->b_using_overlay )
870 ValidateRect(hwnd, NULL);
872 // fall through to default
875 ** For OpenGL and Direct3D, vout will update the whole
876 ** window at regular interval, therefore dirty region
877 ** can be ignored to minimize repaint.
880 /* nothing to erase */
883 /* nothing to repaint */
884 ValidateRect(hwnd, NULL);
888 return DefWindowProc(hwnd, message, wParam, lParam);
895 case WM_WINDOWPOSCHANGED:
896 UpdateRects( p_vout, true );
899 /* the user wants to close the window */
902 playlist_t * p_playlist = pl_Hold( p_vout );
905 playlist_Stop( p_playlist );
906 pl_Release( p_vout );
911 /* the window has been closed so shut down everything now */
913 msg_Dbg( p_vout, "WinProc WM_DESTROY" );
914 /* just destroy the window */
915 PostQuitMessage( 0 );
921 case IDM_TOGGLE_ON_TOP: /* toggle the "on top" status */
924 msg_Dbg( p_vout, "WinProc WM_SYSCOMMAND: IDM_TOGGLE_ON_TOP");
926 /* Change the current value */
927 var_Get( p_vout, "video-on-top", &val );
928 val.b_bool = !val.b_bool;
929 var_Set( p_vout, "video-on-top", val );
938 return DefWindowProc(hwnd, message, wParam, lParam);
941 #ifdef MODULE_NAME_IS_wingapi
942 p_vout->p_sys->b_focus = false;
943 if( !p_vout->p_sys->b_parent_focus ) GXSuspend();
946 if( hwnd == p_vout->p_sys->hfswnd )
948 HWND htbar = FindWindow( _T("HHTaskbar"), NULL );
949 ShowWindow( htbar, SW_SHOW );
952 if( !p_vout->p_sys->hparent ||
953 hwnd == p_vout->p_sys->hfswnd )
955 SHFullScreen( hwnd, SHFS_SHOWSIPBUTTON );
961 #ifdef MODULE_NAME_IS_wingapi
962 p_vout->p_sys->b_focus = true;
966 if( p_vout->p_sys->hparent &&
967 hwnd != p_vout->p_sys->hfswnd && p_vout->b_fullscreen )
968 p_vout->p_sys->i_changes |= VOUT_FULLSCREEN_CHANGE;
970 if( hwnd == p_vout->p_sys->hfswnd )
972 HWND htbar = FindWindow( _T("HHTaskbar"), NULL );
973 ShowWindow( htbar, SW_HIDE );
976 if( !p_vout->p_sys->hparent ||
977 hwnd == p_vout->p_sys->hfswnd )
979 SHFullScreen( hwnd, SHFS_HIDESIPBUTTON );
985 //msg_Dbg( p_vout, "WinProc WM Default %i", message );
989 /* Let windows handle the message */
990 return DefWindowProc(hwnd, message, wParam, lParam);
998 } dxkeys_to_vlckeys[] =
1000 { VK_F1, KEY_F1 }, { VK_F2, KEY_F2 }, { VK_F3, KEY_F3 }, { VK_F4, KEY_F4 },
1001 { VK_F5, KEY_F5 }, { VK_F6, KEY_F6 }, { VK_F7, KEY_F7 }, { VK_F8, KEY_F8 },
1002 { VK_F9, KEY_F9 }, { VK_F10, KEY_F10 }, { VK_F11, KEY_F11 },
1003 { VK_F12, KEY_F12 },
1005 { VK_RETURN, KEY_ENTER },
1006 { VK_SPACE, KEY_SPACE },
1007 { VK_ESCAPE, KEY_ESC },
1009 { VK_LEFT, KEY_LEFT },
1010 { VK_RIGHT, KEY_RIGHT },
1012 { VK_DOWN, KEY_DOWN },
1014 { VK_HOME, KEY_HOME },
1015 { VK_END, KEY_END },
1016 { VK_PRIOR, KEY_PAGEUP },
1017 { VK_NEXT, KEY_PAGEDOWN },
1019 { VK_INSERT, KEY_INSERT },
1020 { VK_DELETE, KEY_DELETE },
1026 { VK_BROWSER_BACK, KEY_BROWSER_BACK },
1027 { VK_BROWSER_FORWARD, KEY_BROWSER_FORWARD },
1028 { VK_BROWSER_REFRESH, KEY_BROWSER_REFRESH },
1029 { VK_BROWSER_STOP, KEY_BROWSER_STOP },
1030 { VK_BROWSER_SEARCH, KEY_BROWSER_SEARCH },
1031 { VK_BROWSER_FAVORITES, KEY_BROWSER_FAVORITES },
1032 { VK_BROWSER_HOME, KEY_BROWSER_HOME },
1033 { VK_VOLUME_MUTE, KEY_VOLUME_MUTE },
1034 { VK_VOLUME_DOWN, KEY_VOLUME_DOWN },
1035 { VK_VOLUME_UP, KEY_VOLUME_UP },
1036 { VK_MEDIA_NEXT_TRACK, KEY_MEDIA_NEXT_TRACK },
1037 { VK_MEDIA_PREV_TRACK, KEY_MEDIA_PREV_TRACK },
1038 { VK_MEDIA_STOP, KEY_MEDIA_STOP },
1039 { VK_MEDIA_PLAY_PAUSE, KEY_MEDIA_PLAY_PAUSE },
1044 static int DirectXConvertKey( int i_key )
1048 for( i = 0; dxkeys_to_vlckeys[i].i_dxkey != 0; i++ )
1050 if( dxkeys_to_vlckeys[i].i_dxkey == i_key )
1052 return dxkeys_to_vlckeys[i].i_vlckey;
1059 /*****************************************************************************
1060 * Control: control facility for the vout
1061 *****************************************************************************/
1062 static int Control( vout_thread_t *p_vout, int i_query, va_list args )
1069 if( p_vout->p_sys->parent_window )
1070 return vaControlParentWindow( p_vout, i_query, args );
1072 /* Update dimensions */
1073 rect_window.top = rect_window.left = 0;
1074 rect_window.right = va_arg( args, unsigned int );
1075 rect_window.bottom = va_arg( args, unsigned int );
1076 if( !rect_window.right ) rect_window.right = p_vout->i_window_width;
1077 if( !rect_window.bottom ) rect_window.bottom = p_vout->i_window_height;
1078 AdjustWindowRect( &rect_window, p_vout->p_sys->i_window_style, 0 );
1080 SetWindowPos( p_vout->p_sys->hwnd, 0, 0, 0,
1081 rect_window.right - rect_window.left,
1082 rect_window.bottom - rect_window.top, SWP_NOMOVE );
1086 case VOUT_SET_STAY_ON_TOP:
1087 if( p_vout->p_sys->hparent && !var_GetBool( p_vout, "fullscreen" ) )
1088 return vaControlParentWindow( p_vout, i_query, args );
1090 p_vout->p_sys->b_on_top_change = true;
1094 return VLC_EGENERIC;
1099 /* Internal wrapper over GetWindowPlacement */
1100 static WINDOWPLACEMENT getWindowState(HWND hwnd)
1102 WINDOWPLACEMENT window_placement;
1103 window_placement.length = sizeof(WINDOWPLACEMENT);
1104 GetWindowPlacement( hwnd, &window_placement );
1105 return window_placement;
1108 /* Internal wrapper to call vout_ControlWindow for hparent */
1109 static int vaControlParentWindow( vout_thread_t *p_vout, int i_query,
1116 const unsigned i_width = va_arg(args, unsigned);
1117 const unsigned i_height = va_arg(args, unsigned);
1118 return vout_window_SetSize( p_vout->p_sys->parent_window, i_width, i_height );
1120 case VOUT_SET_STAY_ON_TOP:
1122 const bool is_on_top = va_arg(args, int);
1123 return vout_window_SetOnTop( p_vout->p_sys->parent_window, is_on_top );
1126 return VLC_EGENERIC;
1130 static int ControlParentWindow( vout_thread_t *p_vout, int i_query, ... )
1135 va_start( args, i_query );
1136 ret = vaControlParentWindow( p_vout, i_query, args );
1141 void Win32ToggleFullscreen( vout_thread_t *p_vout )
1143 HWND hwnd = (p_vout->p_sys->hparent && p_vout->p_sys->hfswnd) ?
1144 p_vout->p_sys->hfswnd : p_vout->p_sys->hwnd;
1146 /* Save the current windows placement/placement to restore
1147 when fullscreen is over */
1148 WINDOWPLACEMENT window_placement = getWindowState( hwnd );
1150 p_vout->b_fullscreen = ! p_vout->b_fullscreen;
1152 /* We want to go to Fullscreen */
1153 if( p_vout->b_fullscreen )
1155 msg_Dbg( p_vout, "entering fullscreen mode" );
1157 /* Change window style, no borders and no title bar */
1158 int i_style = WS_CLIPCHILDREN | WS_VISIBLE;
1159 SetWindowLong( hwnd, GWL_STYLE, i_style );
1161 if( p_vout->p_sys->hparent )
1164 POINT point = {0,0};
1166 ClientToScreen( p_vout->p_sys->hwnd, &point );
1167 GetClientRect( p_vout->p_sys->hwnd, &rect );
1168 SetWindowPos( hwnd, 0, point.x, point.y,
1169 rect.right, rect.bottom,
1170 SWP_NOZORDER|SWP_FRAMECHANGED );
1172 /* Retrieve current window position so fullscreen will happen
1173 *on the right screen */
1174 HMONITOR hmon = MonitorFromWindow(p_vout->p_sys->hparent,
1175 MONITOR_DEFAULTTONEAREST);
1177 if (GetMonitorInfo(hmon, &mi))
1178 SetWindowPos( hwnd, 0,
1181 mi.rcMonitor.right - mi.rcMonitor.left,
1182 mi.rcMonitor.bottom - mi.rcMonitor.top,
1183 SWP_NOZORDER|SWP_FRAMECHANGED );
1188 /* Maximize non embedded window */
1189 ShowWindow( hwnd, SW_SHOWMAXIMIZED );
1192 if( p_vout->p_sys->hparent )
1194 /* Hide the previous window */
1196 GetClientRect( hwnd, &rect );
1197 SetParent( p_vout->p_sys->hwnd, hwnd );
1198 SetWindowPos( p_vout->p_sys->hwnd, 0, 0, 0,
1199 rect.right, rect.bottom,
1200 SWP_NOZORDER|SWP_FRAMECHANGED );
1203 HWND topLevelParent = GetParent( p_vout->p_sys->hparent );
1205 HWND topLevelParent = GetAncestor( p_vout->p_sys->hparent, GA_ROOT );
1207 ShowWindow( topLevelParent, SW_HIDE );
1210 SetForegroundWindow( hwnd );
1214 msg_Dbg( p_vout, "leaving fullscreen mode" );
1215 /* Change window style, no borders and no title bar */
1216 SetWindowLong( hwnd, GWL_STYLE, p_vout->p_sys->i_window_style );
1218 if( p_vout->p_sys->hparent )
1221 GetClientRect( p_vout->p_sys->hparent, &rect );
1222 SetParent( p_vout->p_sys->hwnd, p_vout->p_sys->hparent );
1223 SetWindowPos( p_vout->p_sys->hwnd, 0, 0, 0,
1224 rect.right, rect.bottom,
1225 SWP_NOZORDER|SWP_FRAMECHANGED );
1228 HWND topLevelParent = GetParent( p_vout->p_sys->hparent );
1230 HWND topLevelParent = GetAncestor( p_vout->p_sys->hparent, GA_ROOT );
1232 ShowWindow( topLevelParent, SW_SHOW );
1233 SetForegroundWindow( p_vout->p_sys->hparent );
1234 ShowWindow( hwnd, SW_HIDE );
1238 /* return to normal window for non embedded vout */
1239 SetWindowPlacement( hwnd, &window_placement );
1240 ShowWindow( hwnd, SW_SHOWNORMAL );
1243 /* Make sure the mouse cursor is displayed */
1244 PostMessage( p_vout->p_sys->hwnd, WM_VLC_SHOW_MOUSE, 0, 0 );
1247 /* Update the object variable and trigger callback */
1248 var_SetBool( p_vout, "fullscreen", p_vout->b_fullscreen );
1252 void DisableScreensaver( vout_thread_t *p_vout )
1254 /* disable screensaver by temporarily changing system settings */
1255 p_vout->p_sys->i_spi_lowpowertimeout = 0;
1256 p_vout->p_sys->i_spi_powerofftimeout = 0;
1257 p_vout->p_sys->i_spi_screensavetimeout = 0;
1258 if( var_GetBool( p_vout, "disable-screensaver" ) )
1260 msg_Dbg(p_vout, "disabling screen saver");
1261 SystemParametersInfo(SPI_GETLOWPOWERTIMEOUT,
1262 0, &(p_vout->p_sys->i_spi_lowpowertimeout), 0);
1263 if( 0 != p_vout->p_sys->i_spi_lowpowertimeout ) {
1264 SystemParametersInfo(SPI_SETLOWPOWERTIMEOUT, 0, NULL, 0);
1266 SystemParametersInfo(SPI_GETPOWEROFFTIMEOUT, 0,
1267 &(p_vout->p_sys->i_spi_powerofftimeout), 0);
1268 if( 0 != p_vout->p_sys->i_spi_powerofftimeout ) {
1269 SystemParametersInfo(SPI_SETPOWEROFFTIMEOUT, 0, NULL, 0);
1271 SystemParametersInfo(SPI_GETSCREENSAVETIMEOUT, 0,
1272 &(p_vout->p_sys->i_spi_screensavetimeout), 0);
1273 if( 0 != p_vout->p_sys->i_spi_screensavetimeout ) {
1274 SystemParametersInfo(SPI_SETSCREENSAVETIMEOUT, 0, NULL, 0);
1279 void RestoreScreensaver( vout_thread_t *p_vout )
1281 /* restore screensaver system settings */
1282 if( 0 != p_vout->p_sys->i_spi_lowpowertimeout ) {
1283 SystemParametersInfo(SPI_SETLOWPOWERTIMEOUT,
1284 p_vout->p_sys->i_spi_lowpowertimeout, NULL, 0);
1286 if( 0 != p_vout->p_sys->i_spi_powerofftimeout ) {
1287 SystemParametersInfo(SPI_SETPOWEROFFTIMEOUT,
1288 p_vout->p_sys->i_spi_powerofftimeout, NULL, 0);
1290 if( 0 != p_vout->p_sys->i_spi_screensavetimeout ) {
1291 SystemParametersInfo(SPI_SETSCREENSAVETIMEOUT,
1292 p_vout->p_sys->i_spi_screensavetimeout, NULL, 0);
1297 int CreateEventThread( vout_thread_t *p_vout )
1299 /* Create the Vout EventThread, this thread is created by us to isolate
1300 * the Win32 PeekMessage function calls. We want to do this because
1301 * Windows can stay blocked inside this call for a long time, and when
1302 * this happens it thus blocks vlc's video_output thread.
1303 * Vout EventThread will take care of the creation of the video
1304 * window (because PeekMessage has to be called from the same thread which
1305 * created the window). */
1306 msg_Dbg( p_vout, "creating Vout EventThread" );
1307 p_vout->p_sys->p_event =
1308 vlc_object_create( p_vout, sizeof(event_thread_t) );
1309 p_vout->p_sys->p_event->p_vout = p_vout;
1310 p_vout->p_sys->p_event->window_ready = CreateEvent( NULL, TRUE, FALSE, NULL );
1311 if( vlc_thread_create( p_vout->p_sys->p_event, "Vout Events Thread",
1314 msg_Err( p_vout, "cannot create Vout EventThread" );
1315 CloseHandle( p_vout->p_sys->p_event->window_ready );
1316 vlc_object_release( p_vout->p_sys->p_event );
1317 p_vout->p_sys->p_event = NULL;
1320 WaitForSingleObject( p_vout->p_sys->p_event->window_ready, INFINITE );
1321 CloseHandle( p_vout->p_sys->p_event->window_ready );
1323 if( p_vout->p_sys->p_event->b_error )
1325 msg_Err( p_vout, "Vout EventThread failed" );
1329 vlc_object_attach( p_vout->p_sys->p_event, p_vout );
1331 msg_Dbg( p_vout, "Vout EventThread running" );
1335 void StopEventThread( vout_thread_t *p_vout )
1337 if( p_vout->b_fullscreen )
1339 msg_Dbg( p_vout, "Quitting fullscreen" );
1340 Win32ToggleFullscreen( p_vout );
1341 /* Force fullscreen in the core for the next video */
1342 var_SetBool( p_vout, "fullscreen", true );
1345 if( p_vout->p_sys->p_event )
1347 vlc_object_detach( p_vout->p_sys->p_event );
1349 /* Kill Vout EventThread */
1350 vlc_object_kill( p_vout->p_sys->p_event );
1352 /* we need to be sure Vout EventThread won't stay stuck in
1353 * GetMessage, so we send a fake message */
1354 if( p_vout->p_sys->hwnd )
1356 PostMessage( p_vout->p_sys->hwnd, WM_NULL, 0, 0);
1359 vlc_thread_join( p_vout->p_sys->p_event );
1360 vlc_object_release( p_vout->p_sys->p_event );
1363 if( !p_vout->p_sys->i_changes & SWITCHING_MODE_FLAG )
1364 vlc_mutex_destroy( &p_vout->p_sys->lock );