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>
49 #ifdef MODULE_NAME_IS_vout_directx
52 #ifdef MODULE_NAME_IS_direct3d
55 #ifdef MODULE_NAME_IS_glwin32
64 //WINSHELLAPI BOOL WINAPI SHFullScreen(HWND hwndRequester, DWORD dwState);
67 /*#if defined(UNDER_CE) && !defined(__PLUGIN__) /*FIXME*/
68 /*# define SHFS_SHOWSIPBUTTON 0x0004
69 # define SHFS_HIDESIPBUTTON 0x0008
70 # define MENU_HEIGHT 26
71 BOOL SHFullScreen(HWND hwndRequester, DWORD dwState);
75 /*****************************************************************************
77 *****************************************************************************/
78 static int DirectXCreateWindow( vout_thread_t *p_vout );
79 static void DirectXCloseWindow ( vout_thread_t *p_vout );
80 static long FAR PASCAL DirectXEventProc( HWND, UINT, WPARAM, LPARAM );
82 static int Control( vout_thread_t *p_vout, int i_query, va_list args );
84 static void DirectXPopupMenu( event_thread_t *p_event, bool b_open )
88 var_Set( p_event->p_libvlc, "intf-popupmenu", val );
91 static int DirectXConvertKey( int i_key );
93 /*****************************************************************************
94 * EventThread: Create video window & handle its messages
95 *****************************************************************************
96 * This function creates a video window and then enters an infinite loop
97 * that handles the messages sent to that window.
98 * The main goal of this thread is to isolate the Win32 PeekMessage function
99 * because this one can block for a long time.
100 *****************************************************************************/
101 void* EventThread( vlc_object_t *p_this )
103 event_thread_t *p_event = (event_thread_t *)p_this;
105 POINT old_mouse_pos = {0,0}, mouse_pos;
107 unsigned int i_width, i_height, i_x, i_y;
109 int canc = vlc_savecancel ();
112 p_event->p_vout->pf_control = Control;
114 /* Create a window for the video */
115 /* Creating a window under Windows also initializes the thread's event
117 if( DirectXCreateWindow( p_event->p_vout ) )
119 vlc_restorecancel (canc);
123 /* Signal the creation of the window */
124 vlc_thread_ready( p_event );
127 /* Set power management stuff */
128 if( (hkernel32 = GetModuleHandle( _T("KERNEL32") ) ) )
130 ULONG (WINAPI* OurSetThreadExecutionState)( ULONG );
132 OurSetThreadExecutionState = (ULONG (WINAPI*)( ULONG ))
133 GetProcAddress( hkernel32, _T("SetThreadExecutionState") );
135 if( OurSetThreadExecutionState )
136 /* Prevent monitor from powering off */
137 OurSetThreadExecutionState( ES_DISPLAY_REQUIRED | ES_CONTINUOUS );
139 msg_Dbg( p_event, "no support for SetThreadExecutionState()" );
144 /* GetMessage will sleep if there's no message in the queue */
145 while( vlc_object_alive (p_event) && GetMessage( &msg, 0, 0, 0 ) )
147 /* Check if we are asked to exit */
148 if( !vlc_object_alive (p_event) )
151 switch( msg.message )
155 vout_PlacePicture( p_event->p_vout,
156 p_event->p_vout->p_sys->i_window_width,
157 p_event->p_vout->p_sys->i_window_height,
158 &i_x, &i_y, &i_width, &i_height );
160 if( msg.hwnd == p_event->p_vout->p_sys->hvideownd )
166 if( i_width && i_height )
168 val.i_int = ( GET_X_LPARAM(msg.lParam) - i_x ) *
169 p_event->p_vout->fmt_in.i_visible_width / i_width +
170 p_event->p_vout->fmt_in.i_x_offset;
171 var_Set( p_event->p_vout, "mouse-x", val );
172 val.i_int = ( GET_Y_LPARAM(msg.lParam) - i_y ) *
173 p_event->p_vout->fmt_in.i_visible_height / i_height +
174 p_event->p_vout->fmt_in.i_y_offset;
175 var_Set( p_event->p_vout, "mouse-y", val );
178 var_Set( p_event->p_vout, "mouse-moved", val );
182 GetCursorPos( &mouse_pos );
183 if( (abs(mouse_pos.x - old_mouse_pos.x) > 2 ||
184 (abs(mouse_pos.y - old_mouse_pos.y)) > 2 ) )
186 GetCursorPos( &old_mouse_pos );
187 p_event->p_vout->p_sys->i_lastmoved = mdate();
189 if( p_event->p_vout->p_sys->b_cursor_hidden )
191 p_event->p_vout->p_sys->b_cursor_hidden = 0;
197 case WM_VLC_HIDE_MOUSE:
198 if( p_event->p_vout->p_sys->b_cursor_hidden ) break;
199 p_event->p_vout->p_sys->b_cursor_hidden = true;
200 GetCursorPos( &old_mouse_pos );
204 case WM_VLC_SHOW_MOUSE:
205 if( !p_event->p_vout->p_sys->b_cursor_hidden ) break;
206 p_event->p_vout->p_sys->b_cursor_hidden = false;
207 GetCursorPos( &old_mouse_pos );
212 var_Get( p_event->p_vout, "mouse-button-down", &val );
214 var_Set( p_event->p_vout, "mouse-button-down", val );
215 DirectXPopupMenu( p_event, false );
219 var_Get( p_event->p_vout, "mouse-button-down", &val );
221 var_Set( p_event->p_vout, "mouse-button-down", val );
224 var_Set( p_event->p_vout, "mouse-clicked", val );
227 case WM_LBUTTONDBLCLK:
228 p_event->p_vout->p_sys->i_changes |= VOUT_FULLSCREEN_CHANGE;
232 var_Get( p_event->p_vout, "mouse-button-down", &val );
234 var_Set( p_event->p_vout, "mouse-button-down", val );
235 DirectXPopupMenu( p_event, false );
239 var_Get( p_event->p_vout, "mouse-button-down", &val );
241 var_Set( p_event->p_vout, "mouse-button-down", val );
245 var_Get( p_event->p_vout, "mouse-button-down", &val );
247 var_Set( p_event->p_vout, "mouse-button-down", val );
248 DirectXPopupMenu( p_event, false );
252 var_Get( p_event->p_vout, "mouse-button-down", &val );
254 var_Set( p_event->p_vout, "mouse-button-down", val );
255 DirectXPopupMenu( p_event, true );
260 /* The key events are first processed here and not translated
261 * into WM_CHAR events because we need to know the status of the
263 val.i_int = DirectXConvertKey( msg.wParam );
266 /* This appears to be a "normal" (ascii) key */
267 val.i_int = tolower( MapVirtualKey( msg.wParam, 2 ) );
272 if( GetKeyState(VK_CONTROL) & 0x8000 )
274 val.i_int |= KEY_MODIFIER_CTRL;
276 if( GetKeyState(VK_SHIFT) & 0x8000 )
278 val.i_int |= KEY_MODIFIER_SHIFT;
280 if( GetKeyState(VK_MENU) & 0x8000 )
282 val.i_int |= KEY_MODIFIER_ALT;
285 var_Set( p_event->p_libvlc, "key-pressed", val );
290 if( GET_WHEEL_DELTA_WPARAM( msg.wParam ) > 0 )
292 val.i_int = KEY_MOUSEWHEELUP;
296 val.i_int = KEY_MOUSEWHEELDOWN;
300 if( GetKeyState(VK_CONTROL) & 0x8000 )
302 val.i_int |= KEY_MODIFIER_CTRL;
304 if( GetKeyState(VK_SHIFT) & 0x8000 )
306 val.i_int |= KEY_MODIFIER_SHIFT;
308 if( GetKeyState(VK_MENU) & 0x8000 )
310 val.i_int |= KEY_MODIFIER_ALT;
313 var_Set( p_event->p_libvlc, "key-pressed", val );
317 case WM_VLC_CHANGE_TEXT:
318 var_Get( p_event->p_vout, "video-title", &val );
319 if( !val.psz_string || !*val.psz_string ) /* Default video title */
321 free( val.psz_string );
323 #ifdef MODULE_NAME_IS_wingdi
324 val.psz_string = strdup( VOUT_TITLE " (WinGDI output)" );
326 #ifdef MODULE_NAME_IS_wingapi
327 val.psz_string = strdup( VOUT_TITLE " (WinGAPI output)" );
329 #ifdef MODULE_NAME_IS_glwin32
330 val.psz_string = strdup( VOUT_TITLE " (OpenGL output)" );
332 #ifdef MODULE_NAME_IS_direct3d
333 val.psz_string = strdup( VOUT_TITLE " (Direct3D output)" );
335 #ifdef MODULE_NAME_IS_vout_directx
336 if( p_event->p_vout->p_sys->b_using_overlay ) val.psz_string =
337 strdup( VOUT_TITLE " (hardware YUV overlay DirectX output)" );
338 else if( p_event->p_vout->p_sys->b_hw_yuv ) val.psz_string =
339 strdup( VOUT_TITLE " (hardware YUV DirectX output)" );
340 else val.psz_string =
341 strdup( VOUT_TITLE " (software RGB DirectX output)" );
347 wchar_t *psz_title = malloc( strlen(val.psz_string) * 2 + 2 );
350 mbstowcs( psz_title, val.psz_string, strlen(val.psz_string)*2);
351 psz_title[strlen(val.psz_string)] = 0;
352 free( val.psz_string ); val.psz_string = (char *)psz_title;
357 SetWindowText( p_event->p_vout->p_sys->hwnd,
358 (LPCTSTR)val.psz_string );
359 if( p_event->p_vout->p_sys->hfswnd )
360 SetWindowText( p_event->p_vout->p_sys->hfswnd,
361 (LPCTSTR)val.psz_string );
362 free( val.psz_string );
366 /* Messages we don't handle directly are dispatched to the
367 * window procedure */
368 TranslateMessage(&msg);
369 DispatchMessage(&msg);
374 } /* End Main loop */
376 /* Check for WM_QUIT if we created the window */
377 if( !p_event->p_vout->p_sys->hparent && msg.message == WM_QUIT )
379 msg_Warn( p_event, "WM_QUIT... should not happen!!" );
380 p_event->p_vout->p_sys->hwnd = NULL; /* Window already destroyed */
383 msg_Dbg( p_event, "DirectXEventThread terminating" );
385 /* clear the changes formerly signaled */
386 p_event->p_vout->p_sys->i_changes = 0;
388 DirectXCloseWindow( p_event->p_vout );
389 vlc_restorecancel (canc);
394 /* following functions are local */
396 /*****************************************************************************
397 * DirectXCreateWindow: create a window for the video.
398 *****************************************************************************
399 * Before creating a direct draw surface, we need to create a window in which
400 * the video will be displayed. This window will also allow us to capture the
402 *****************************************************************************/
403 static int DirectXCreateWindow( vout_thread_t *p_vout )
408 WNDCLASS wc; /* window class components */
409 HICON vlc_icon = NULL;
410 char vlc_path[MAX_PATH+1];
411 int i_style, i_stylex;
413 msg_Dbg( p_vout, "DirectXCreateWindow" );
415 /* Get this module's instance */
416 hInstance = GetModuleHandle(NULL);
418 /* If an external window was specified, we'll draw in it. */
419 p_vout->p_sys->hparent =
420 vout_RequestWindow( p_vout, &p_vout->p_sys->i_window_x,
421 &p_vout->p_sys->i_window_y,
422 &p_vout->p_sys->i_window_width,
423 &p_vout->p_sys->i_window_height );
425 /* We create the window ourself, there is no previous window proc. */
426 p_vout->p_sys->pf_wndproc = NULL;
428 /* Get the Icon from the main app */
431 if( GetModuleFileName( NULL, vlc_path, MAX_PATH ) )
433 vlc_icon = ExtractIcon( hInstance, vlc_path, 0 );
437 /* Fill in the window class structure */
438 wc.style = CS_OWNDC|CS_DBLCLKS; /* style: dbl click */
439 wc.lpfnWndProc = (WNDPROC)DirectXEventProc; /* event handler */
440 wc.cbClsExtra = 0; /* no extra class data */
441 wc.cbWndExtra = 0; /* no extra window data */
442 wc.hInstance = hInstance; /* instance */
443 wc.hIcon = vlc_icon; /* load the vlc big icon */
444 wc.hCursor = LoadCursor(NULL, IDC_ARROW); /* default cursor */
445 wc.hbrBackground = GetStockObject(BLACK_BRUSH); /* background color */
446 wc.lpszMenuName = NULL; /* no menu */
447 wc.lpszClassName = _T("VLC DirectX"); /* use a special class */
449 /* Register the window class */
450 if( !RegisterClass(&wc) )
454 if( vlc_icon ) DestroyIcon( vlc_icon );
456 /* Check why it failed. If it's because one already exists
457 * then fine, otherwise return with an error. */
458 if( !GetClassInfo( hInstance, _T("VLC DirectX"), &wndclass ) )
460 msg_Err( p_vout, "DirectXCreateWindow RegisterClass FAILED (err=%lu)", GetLastError() );
465 /* Register the video sub-window class */
466 wc.lpszClassName = _T("VLC DirectX video"); wc.hIcon = 0;
467 wc.hbrBackground = NULL; /* no background color */
468 if( !RegisterClass(&wc) )
472 /* Check why it failed. If it's because one already exists
473 * then fine, otherwise return with an error. */
474 if( !GetClassInfo( hInstance, _T("VLC DirectX video"), &wndclass ) )
476 msg_Err( p_vout, "DirectXCreateWindow RegisterClass FAILED (err=%lu)", GetLastError() );
481 /* When you create a window you give the dimensions you wish it to
482 * have. Unfortunatly these dimensions will include the borders and
483 * titlebar. We use the following function to find out the size of
484 * the window corresponding to the useable surface we want */
485 rect_window.top = 10;
486 rect_window.left = 10;
487 rect_window.right = rect_window.left + p_vout->p_sys->i_window_width;
488 rect_window.bottom = rect_window.top + p_vout->p_sys->i_window_height;
490 if( var_GetBool( p_vout, "video-deco" ) )
492 /* Open with window decoration */
493 AdjustWindowRect( &rect_window, WS_OVERLAPPEDWINDOW|WS_SIZEBOX, 0 );
494 i_style = WS_OVERLAPPEDWINDOW|WS_SIZEBOX|WS_VISIBLE|WS_CLIPCHILDREN;
499 /* No window decoration */
500 AdjustWindowRect( &rect_window, WS_POPUP, 0 );
501 i_style = WS_POPUP|WS_VISIBLE|WS_CLIPCHILDREN;
502 i_stylex = 0; // WS_EX_TOOLWINDOW; Is TOOLWINDOW really needed ?
503 // It messes up the fullscreen window.
506 if( p_vout->p_sys->hparent )
508 i_style = WS_VISIBLE|WS_CLIPCHILDREN|WS_CHILD;
512 p_vout->p_sys->i_window_style = i_style;
514 /* Create the window */
515 p_vout->p_sys->hwnd =
516 CreateWindowEx( WS_EX_NOPARENTNOTIFY | i_stylex,
517 _T("VLC DirectX"), /* name of window class */
518 _T(VOUT_TITLE) _T(" (DirectX Output)"), /* window title */
519 i_style, /* window style */
520 (p_vout->p_sys->i_window_x < 0) ? CW_USEDEFAULT :
521 (UINT)p_vout->p_sys->i_window_x, /* default X coordinate */
522 (p_vout->p_sys->i_window_y < 0) ? CW_USEDEFAULT :
523 (UINT)p_vout->p_sys->i_window_y, /* default Y coordinate */
524 rect_window.right - rect_window.left, /* window width */
525 rect_window.bottom - rect_window.top, /* window height */
526 p_vout->p_sys->hparent, /* parent window */
527 NULL, /* no menu in this window */
528 hInstance, /* handle of this program instance */
529 (LPVOID)p_vout ); /* send p_vout to WM_CREATE */
531 if( !p_vout->p_sys->hwnd )
533 msg_Warn( p_vout, "DirectXCreateWindow create window FAILED (err=%lu)", GetLastError() );
537 if( p_vout->p_sys->hparent )
541 /* We don't want the window owner to overwrite our client area */
542 i_style = GetWindowLong( p_vout->p_sys->hparent, GWL_STYLE );
544 if( !(i_style & WS_CLIPCHILDREN) )
545 /* Hmmm, apparently this is a blocking call... */
546 SetWindowLong( p_vout->p_sys->hparent, GWL_STYLE,
547 i_style | WS_CLIPCHILDREN );
549 /* Create our fullscreen window */
550 p_vout->p_sys->hfswnd =
551 CreateWindowEx( WS_EX_APPWINDOW, _T("VLC DirectX"),
552 _T(VOUT_TITLE) _T(" (DirectX Output)"),
553 WS_OVERLAPPEDWINDOW|WS_CLIPCHILDREN|WS_SIZEBOX,
554 CW_USEDEFAULT, CW_USEDEFAULT,
555 CW_USEDEFAULT, CW_USEDEFAULT,
556 NULL, NULL, hInstance, NULL );
559 /* Append a "Always On Top" entry in the system menu */
560 hMenu = GetSystemMenu( p_vout->p_sys->hwnd, FALSE );
561 AppendMenu( hMenu, MF_SEPARATOR, 0, _T("") );
562 AppendMenu( hMenu, MF_STRING | MF_UNCHECKED,
563 IDM_TOGGLE_ON_TOP, _T("Always on &Top") );
565 /* Create video sub-window. This sub window will always exactly match
566 * the size of the video, which allows us to use crazy overlay colorkeys
567 * without having them shown outside of the video area. */
568 p_vout->p_sys->hvideownd =
569 CreateWindow( _T("VLC DirectX video"), _T(""), /* window class */
570 WS_CHILD, /* window style, not visible initially */
572 p_vout->render.i_width, /* default width */
573 p_vout->render.i_height, /* default height */
574 p_vout->p_sys->hwnd, /* parent window */
576 (LPVOID)p_vout ); /* send p_vout to WM_CREATE */
578 if( !p_vout->p_sys->hvideownd )
579 msg_Warn( p_vout, "can't create video sub-window" );
581 msg_Dbg( p_vout, "created video sub-window" );
583 /* Now display the window */
584 ShowWindow( p_vout->p_sys->hwnd, SW_SHOW );
589 /*****************************************************************************
590 * DirectXCloseWindow: close the window created by DirectXCreateWindow
591 *****************************************************************************
592 * This function returns all resources allocated by DirectXCreateWindow.
593 *****************************************************************************/
594 static void DirectXCloseWindow( vout_thread_t *p_vout )
596 msg_Dbg( p_vout, "DirectXCloseWindow" );
598 DestroyWindow( p_vout->p_sys->hwnd );
599 if( p_vout->p_sys->hfswnd ) DestroyWindow( p_vout->p_sys->hfswnd );
601 if( p_vout->p_sys->hparent )
602 vout_ReleaseWindow( p_vout, (void *)p_vout->p_sys->hparent );
604 p_vout->p_sys->hwnd = NULL;
606 /* We don't unregister the Window Class because it could lead to race
607 * conditions and it will be done anyway by the system when the app will
611 /*****************************************************************************
612 * UpdateRects: update clipping rectangles
613 *****************************************************************************
614 * This function is called when the window position or size are changed, and
615 * its job is to update the source and destination RECTs used to display the
617 *****************************************************************************/
618 void UpdateRects( vout_thread_t *p_vout, bool b_force )
620 #define rect_src p_vout->p_sys->rect_src
621 #define rect_src_clipped p_vout->p_sys->rect_src_clipped
622 #define rect_dest p_vout->p_sys->rect_dest
623 #define rect_dest_clipped p_vout->p_sys->rect_dest_clipped
625 int i_width, i_height, i_x, i_y;
630 /* Retrieve the window size */
631 GetClientRect( p_vout->p_sys->hwnd, &rect );
633 /* Retrieve the window position */
634 point.x = point.y = 0;
635 ClientToScreen( p_vout->p_sys->hwnd, &point );
637 /* If nothing changed, we can return */
639 && p_vout->p_sys->i_window_width == rect.right
640 && p_vout->p_sys->i_window_height == rect.bottom
641 && p_vout->p_sys->i_window_x == point.x
642 && p_vout->p_sys->i_window_y == point.y )
647 /* Update the window position and size */
648 p_vout->p_sys->i_window_x = point.x;
649 p_vout->p_sys->i_window_y = point.y;
650 p_vout->p_sys->i_window_width = rect.right;
651 p_vout->p_sys->i_window_height = rect.bottom;
653 vout_PlacePicture( p_vout, rect.right, rect.bottom,
654 &i_x, &i_y, &i_width, &i_height );
656 if( p_vout->p_sys->hvideownd )
657 SetWindowPos( p_vout->p_sys->hvideownd, 0,
658 i_x, i_y, i_width, i_height,
659 SWP_NOCOPYBITS|SWP_NOZORDER|SWP_ASYNCWINDOWPOS );
661 /* Destination image position and dimensions */
662 rect_dest.left = point.x + i_x;
663 rect_dest.right = rect_dest.left + i_width;
664 rect_dest.top = point.y + i_y;
665 rect_dest.bottom = rect_dest.top + i_height;
667 #ifdef MODULE_NAME_IS_vout_directx
668 /* Apply overlay hardware constraints */
669 if( p_vout->p_sys->b_using_overlay )
671 if( p_vout->p_sys->i_align_dest_boundary )
672 rect_dest.left = ( rect_dest.left +
673 p_vout->p_sys->i_align_dest_boundary / 2 ) &
674 ~p_vout->p_sys->i_align_dest_boundary;
676 if( p_vout->p_sys->i_align_dest_size )
677 rect_dest.right = (( rect_dest.right - rect_dest.left +
678 p_vout->p_sys->i_align_dest_size / 2 ) &
679 ~p_vout->p_sys->i_align_dest_size) + rect_dest.left;
682 /* UpdateOverlay directdraw function doesn't automatically clip to the
683 * display size so we need to do it otherwise it will fail */
685 /* Clip the destination window */
686 if( !IntersectRect( &rect_dest_clipped, &rect_dest,
687 &p_vout->p_sys->rect_display ) )
689 SetRectEmpty( &rect_src_clipped );
694 msg_Dbg( p_vout, "DirectXUpdateRects image_dst_clipped coords:"
696 rect_dest_clipped.left, rect_dest_clipped.top,
697 rect_dest_clipped.right, rect_dest_clipped.bottom );
700 #else /* MODULE_NAME_IS_vout_directx */
702 /* AFAIK, there are no clipping constraints in Direct3D, OpenGL and GDI */
703 rect_dest_clipped = rect_dest;
707 /* the 2 following lines are to fix a bug when clicking on the desktop */
708 if( (rect_dest_clipped.right - rect_dest_clipped.left)==0 ||
709 (rect_dest_clipped.bottom - rect_dest_clipped.top)==0 )
711 SetRectEmpty( &rect_src_clipped );
715 /* src image dimensions */
718 rect_src.right = p_vout->render.i_width;
719 rect_src.bottom = p_vout->render.i_height;
721 /* Clip the source image */
722 rect_src_clipped.left = p_vout->fmt_out.i_x_offset +
723 (rect_dest_clipped.left - rect_dest.left) *
724 p_vout->fmt_out.i_visible_width / (rect_dest.right - rect_dest.left);
725 rect_src_clipped.right = p_vout->fmt_out.i_x_offset +
726 p_vout->fmt_out.i_visible_width -
727 (rect_dest.right - rect_dest_clipped.right) *
728 p_vout->fmt_out.i_visible_width / (rect_dest.right - rect_dest.left);
729 rect_src_clipped.top = p_vout->fmt_out.i_y_offset +
730 (rect_dest_clipped.top - rect_dest.top) *
731 p_vout->fmt_out.i_visible_height / (rect_dest.bottom - rect_dest.top);
732 rect_src_clipped.bottom = p_vout->fmt_out.i_y_offset +
733 p_vout->fmt_out.i_visible_height -
734 (rect_dest.bottom - rect_dest_clipped.bottom) *
735 p_vout->fmt_out.i_visible_height / (rect_dest.bottom - rect_dest.top);
737 #ifdef MODULE_NAME_IS_vout_directx
738 /* Apply overlay hardware constraints */
739 if( p_vout->p_sys->b_using_overlay )
741 if( p_vout->p_sys->i_align_src_boundary )
742 rect_src_clipped.left = ( rect_src_clipped.left +
743 p_vout->p_sys->i_align_src_boundary / 2 ) &
744 ~p_vout->p_sys->i_align_src_boundary;
746 if( p_vout->p_sys->i_align_src_size )
747 rect_src_clipped.right = (( rect_src_clipped.right -
748 rect_src_clipped.left +
749 p_vout->p_sys->i_align_src_size / 2 ) &
750 ~p_vout->p_sys->i_align_src_size) + rect_src_clipped.left;
755 msg_Dbg( p_vout, "DirectXUpdateRects image_src_clipped"
756 " coords: %i,%i,%i,%i",
757 rect_src_clipped.left, rect_src_clipped.top,
758 rect_src_clipped.right, rect_src_clipped.bottom );
761 #ifdef MODULE_NAME_IS_vout_directx
762 /* The destination coordinates need to be relative to the current
763 * directdraw primary surface (display) */
764 rect_dest_clipped.left -= p_vout->p_sys->rect_display.left;
765 rect_dest_clipped.right -= p_vout->p_sys->rect_display.left;
766 rect_dest_clipped.top -= p_vout->p_sys->rect_display.top;
767 rect_dest_clipped.bottom -= p_vout->p_sys->rect_display.top;
769 if( p_vout->p_sys->b_using_overlay )
770 DirectDrawUpdateOverlay( p_vout );
773 /* Signal the change in size/position */
774 p_vout->p_sys->i_changes |= DX_POSITION_CHANGE;
777 #undef rect_src_clipped
779 #undef rect_dest_clipped
782 /*****************************************************************************
783 * DirectXEventProc: This is the window event processing function.
784 *****************************************************************************
785 * On Windows, when you create a window you have to attach an event processing
786 * function to it. The aim of this function is to manage "Queued Messages" and
787 * "Nonqueued Messages".
788 * Queued Messages are those picked up and retransmitted by vout_Manage
789 * (using the GetMessage and DispatchMessage functions).
790 * Nonqueued Messages are those that Windows will send directly to this
791 * procedure (like WM_DESTROY, WM_WINDOWPOSCHANGED...)
792 *****************************************************************************/
793 static long FAR PASCAL DirectXEventProc( HWND hwnd, UINT message,
794 WPARAM wParam, LPARAM lParam )
796 vout_thread_t *p_vout;
798 if( message == WM_CREATE )
800 /* Store p_vout for future use */
801 p_vout = (vout_thread_t *)((CREATESTRUCT *)lParam)->lpCreateParams;
802 SetWindowLongPtr( hwnd, GWLP_USERDATA, (LONG_PTR)p_vout );
807 p_vout = (vout_thread_t *)GetWindowLongPtr( hwnd, GWLP_USERDATA );
810 /* Hmmm mozilla does manage somehow to save the pointer to our
811 * windowproc and still calls it after the vout has been closed. */
812 return DefWindowProc(hwnd, message, wParam, lParam);
817 /* Catch the screensaver and the monitor turn-off */
818 if( message == WM_SYSCOMMAND &&
819 ( (wParam & 0xFFF0) == SC_SCREENSAVE || (wParam & 0xFFF0) == SC_MONITORPOWER ) )
821 //if( p_vout ) msg_Dbg( p_vout, "WinProc WM_SYSCOMMAND screensaver" );
822 return 0; /* this stops them from happening */
826 if( hwnd == p_vout->p_sys->hvideownd )
830 #ifdef MODULE_NAME_IS_vout_directx
832 /* For overlay, we need to erase background */
833 return !p_vout->p_sys->b_using_overlay ?
834 1 : DefWindowProc(hwnd, message, wParam, lParam);
837 ** For overlay, DefWindowProc() will erase dirty regions
839 ** For non-overlay, vout will paint the whole window at
840 ** regular interval, therefore dirty regions can be ignored
841 ** to minimize repaint.
843 if( !p_vout->p_sys->b_using_overlay )
845 ValidateRect(hwnd, NULL);
847 // fall through to default
850 ** For OpenGL and Direct3D, vout will update the whole
851 ** window at regular interval, therefore dirty region
852 ** can be ignored to minimize repaint.
855 /* nothing to erase */
858 /* nothing to repaint */
859 ValidateRect(hwnd, NULL);
863 return DefWindowProc(hwnd, message, wParam, lParam);
870 case WM_WINDOWPOSCHANGED:
871 UpdateRects( p_vout, true );
874 /* the user wants to close the window */
877 playlist_t * p_playlist = pl_Yield( p_vout );
880 playlist_Stop( p_playlist );
881 pl_Release( p_vout );
886 /* the window has been closed so shut down everything now */
888 msg_Dbg( p_vout, "WinProc WM_DESTROY" );
889 /* just destroy the window */
890 PostQuitMessage( 0 );
896 case IDM_TOGGLE_ON_TOP: /* toggle the "on top" status */
899 msg_Dbg( p_vout, "WinProc WM_SYSCOMMAND: IDM_TOGGLE_ON_TOP");
901 /* Change the current value */
902 var_Get( p_vout, "video-on-top", &val );
903 val.b_bool = !val.b_bool;
904 var_Set( p_vout, "video-on-top", val );
913 return DefWindowProc(hwnd, message, wParam, lParam);
916 #ifdef MODULE_NAME_IS_wingapi
917 p_vout->p_sys->b_focus = false;
918 if( !p_vout->p_sys->b_parent_focus ) GXSuspend();
921 if( hwnd == p_vout->p_sys->hfswnd )
923 HWND htbar = FindWindow( _T("HHTaskbar"), NULL );
924 ShowWindow( htbar, SW_SHOW );
927 if( !p_vout->p_sys->hparent ||
928 hwnd == p_vout->p_sys->hfswnd )
930 SHFullScreen( hwnd, SHFS_SHOWSIPBUTTON );
936 #ifdef MODULE_NAME_IS_wingapi
937 p_vout->p_sys->b_focus = true;
941 if( p_vout->p_sys->hparent &&
942 hwnd != p_vout->p_sys->hfswnd && p_vout->b_fullscreen )
943 p_vout->p_sys->i_changes |= VOUT_FULLSCREEN_CHANGE;
945 if( hwnd == p_vout->p_sys->hfswnd )
947 HWND htbar = FindWindow( _T("HHTaskbar"), NULL );
948 ShowWindow( htbar, SW_HIDE );
951 if( !p_vout->p_sys->hparent ||
952 hwnd == p_vout->p_sys->hfswnd )
954 SHFullScreen( hwnd, SHFS_HIDESIPBUTTON );
960 //msg_Dbg( p_vout, "WinProc WM Default %i", message );
964 /* Let windows handle the message */
965 return DefWindowProc(hwnd, message, wParam, lParam);
973 } dxkeys_to_vlckeys[] =
975 { VK_F1, KEY_F1 }, { VK_F2, KEY_F2 }, { VK_F3, KEY_F3 }, { VK_F4, KEY_F4 },
976 { VK_F5, KEY_F5 }, { VK_F6, KEY_F6 }, { VK_F7, KEY_F7 }, { VK_F8, KEY_F8 },
977 { VK_F9, KEY_F9 }, { VK_F10, KEY_F10 }, { VK_F11, KEY_F11 },
980 { VK_RETURN, KEY_ENTER },
981 { VK_SPACE, KEY_SPACE },
982 { VK_ESCAPE, KEY_ESC },
984 { VK_LEFT, KEY_LEFT },
985 { VK_RIGHT, KEY_RIGHT },
987 { VK_DOWN, KEY_DOWN },
989 { VK_HOME, KEY_HOME },
991 { VK_PRIOR, KEY_PAGEUP },
992 { VK_NEXT, KEY_PAGEDOWN },
994 { VK_INSERT, KEY_INSERT },
995 { VK_DELETE, KEY_DELETE },
1001 { VK_BROWSER_BACK, KEY_BROWSER_BACK },
1002 { VK_BROWSER_FORWARD, KEY_BROWSER_FORWARD },
1003 { VK_BROWSER_REFRESH, KEY_BROWSER_REFRESH },
1004 { VK_BROWSER_STOP, KEY_BROWSER_STOP },
1005 { VK_BROWSER_SEARCH, KEY_BROWSER_SEARCH },
1006 { VK_BROWSER_FAVORITES, KEY_BROWSER_FAVORITES },
1007 { VK_BROWSER_HOME, KEY_BROWSER_HOME },
1008 { VK_VOLUME_MUTE, KEY_VOLUME_MUTE },
1009 { VK_VOLUME_DOWN, KEY_VOLUME_DOWN },
1010 { VK_VOLUME_UP, KEY_VOLUME_UP },
1011 { VK_MEDIA_NEXT_TRACK, KEY_MEDIA_NEXT_TRACK },
1012 { VK_MEDIA_PREV_TRACK, KEY_MEDIA_PREV_TRACK },
1013 { VK_MEDIA_STOP, KEY_MEDIA_STOP },
1014 { VK_MEDIA_PLAY_PAUSE, KEY_MEDIA_PLAY_PAUSE },
1019 static int DirectXConvertKey( int i_key )
1023 for( i = 0; dxkeys_to_vlckeys[i].i_dxkey != 0; i++ )
1025 if( dxkeys_to_vlckeys[i].i_dxkey == i_key )
1027 return dxkeys_to_vlckeys[i].i_vlckey;
1034 /*****************************************************************************
1035 * Control: control facility for the vout
1036 *****************************************************************************/
1037 static int Control( vout_thread_t *p_vout, int i_query, va_list args )
1039 unsigned int *pi_width, *pi_height;
1046 if( p_vout->p_sys->hparent )
1047 return vout_ControlWindow( p_vout,
1048 (void *)p_vout->p_sys->hparent, i_query, args );
1050 pi_width = va_arg( args, unsigned int * );
1051 pi_height = va_arg( args, unsigned int * );
1053 GetClientRect( p_vout->p_sys->hwnd, &rect_window );
1055 *pi_width = rect_window.right - rect_window.left;
1056 *pi_height = rect_window.bottom - rect_window.top;
1060 if( p_vout->p_sys->hparent )
1061 return vout_ControlWindow( p_vout,
1062 (void *)p_vout->p_sys->hparent, i_query, args );
1064 /* Update dimensions */
1065 rect_window.top = rect_window.left = 0;
1066 rect_window.right = va_arg( args, unsigned int );
1067 rect_window.bottom = va_arg( args, unsigned int );
1068 if( !rect_window.right ) rect_window.right = p_vout->i_window_width;
1069 if( !rect_window.bottom ) rect_window.bottom = p_vout->i_window_height;
1070 AdjustWindowRect( &rect_window, p_vout->p_sys->i_window_style, 0 );
1072 SetWindowPos( p_vout->p_sys->hwnd, 0, 0, 0,
1073 rect_window.right - rect_window.left,
1074 rect_window.bottom - rect_window.top, SWP_NOMOVE );
1079 ShowWindow( p_vout->p_sys->hwnd, SW_HIDE );
1081 /* Retrieve the window position */
1082 point.x = point.y = 0;
1083 ClientToScreen( p_vout->p_sys->hwnd, &point );
1087 if( i_query == VOUT_REPARENT ) d = (HWND)va_arg( args, int );
1090 vlc_mutex_lock( &p_vout->p_sys->lock );
1091 p_vout->p_sys->hparent = 0;
1092 vlc_mutex_unlock( &p_vout->p_sys->lock );
1093 SetParent( p_vout->p_sys->hwnd, 0 );
1094 p_vout->p_sys->i_window_style =
1095 WS_CLIPCHILDREN | WS_OVERLAPPEDWINDOW | WS_SIZEBOX;
1096 SetWindowLong( p_vout->p_sys->hwnd, GWL_STYLE,
1097 p_vout->p_sys->i_window_style |
1098 (i_query == VOUT_CLOSE ? 0 : WS_VISIBLE) );
1099 SetWindowLong( p_vout->p_sys->hwnd, GWL_EXSTYLE, WS_EX_APPWINDOW );
1100 SetWindowPos( p_vout->p_sys->hwnd, 0, point.x, point.y, 0, 0,
1101 SWP_NOSIZE|SWP_NOZORDER|SWP_FRAMECHANGED );
1105 vlc_mutex_lock( &p_vout->p_sys->lock );
1106 p_vout->p_sys->hparent = d;
1107 vlc_mutex_unlock( &p_vout->p_sys->lock );
1109 SetParent( p_vout->p_sys->hwnd, d );
1110 p_vout->p_sys->i_window_style = WS_CLIPCHILDREN;
1111 SetWindowLong( p_vout->p_sys->hwnd, GWL_STYLE,
1112 p_vout->p_sys->i_window_style |
1113 (i_query == VOUT_CLOSE ? 0 : WS_VISIBLE) );
1114 SetWindowLong( p_vout->p_sys->hwnd, GWL_EXSTYLE, WS_EX_APPWINDOW );
1116 /* Retrieve the parent size */
1118 GetClientRect( d, &rect );
1119 SetWindowPos( p_vout->p_sys->hwnd, d, point.x, point.y, rect.right, rect.bottom,
1123 return vout_vaControlDefault( p_vout, i_query, args );
1125 case VOUT_SET_STAY_ON_TOP:
1126 if( p_vout->p_sys->hparent && !var_GetBool( p_vout, "fullscreen" ) )
1127 return vout_ControlWindow( p_vout,
1128 (void *)p_vout->p_sys->hparent, i_query, args );
1130 p_vout->p_sys->b_on_top_change = true;
1133 #ifdef MODULE_NAME_IS_wingapi
1134 case VOUT_SET_FOCUS:
1135 b_bool = (bool) va_arg( args, int );
1136 p_vout->p_sys->b_parent_focus = b_bool;
1137 if( b_bool ) GXResume();
1138 else if( !p_vout->p_sys->b_focus ) GXSuspend();
1143 return vout_vaControlDefault( p_vout, i_query, args );
1148 /* Internal wrapper over GetWindowPlacement / SetWindowPlacement */
1149 static void SetWindowState(HWND hwnd, int nShowCmd)
1151 WINDOWPLACEMENT window_placement;
1152 window_placement.length = sizeof(WINDOWPLACEMENT);
1153 GetWindowPlacement( hwnd, &window_placement );
1154 window_placement.showCmd = nShowCmd;
1155 SetWindowPlacement( hwnd, &window_placement );
1156 SetWindowPos( hwnd, 0, 0, 0, 0, 0,
1157 SWP_NOMOVE|SWP_NOSIZE|SWP_NOZORDER|SWP_FRAMECHANGED);
1160 /* Internal wrapper to call vout_ControlWindow for hparent */
1161 static void ControlParentWindow( vout_thread_t *p_vout, int i_query, ... )
1164 va_start( args, i_query );
1165 vout_ControlWindow( p_vout,
1166 (void *)p_vout->p_sys->hparent, i_query, args );
1170 void Win32ToggleFullscreen( vout_thread_t *p_vout )
1172 HWND hwnd = (p_vout->p_sys->hparent && p_vout->p_sys->hfswnd) ?
1173 p_vout->p_sys->hfswnd : p_vout->p_sys->hwnd;
1175 p_vout->b_fullscreen = ! p_vout->b_fullscreen;
1177 if( p_vout->b_fullscreen )
1179 msg_Dbg( p_vout, "entering fullscreen mode" );
1180 /* Change window style, no borders and no title bar */
1181 int i_style = WS_CLIPCHILDREN | WS_VISIBLE;
1182 SetWindowLong( hwnd, GWL_STYLE, i_style );
1184 if( p_vout->p_sys->hparent )
1186 /* Retrieve current window position so fullscreen will happen
1187 * on the right screen */
1188 POINT point = {0,0};
1190 ClientToScreen( p_vout->p_sys->hwnd, &point );
1191 GetClientRect( p_vout->p_sys->hwnd, &rect );
1192 SetWindowPos( hwnd, 0, point.x, point.y,
1193 rect.right, rect.bottom,
1194 SWP_NOZORDER|SWP_FRAMECHANGED );
1197 /* Maximize window */
1198 SetWindowState( hwnd, SW_SHOWMAXIMIZED );
1200 if( p_vout->p_sys->hparent )
1203 GetClientRect( hwnd, &rect );
1204 SetParent( p_vout->p_sys->hwnd, hwnd );
1205 SetWindowPos( p_vout->p_sys->hwnd, 0, 0, 0,
1206 rect.right, rect.bottom,
1207 SWP_NOZORDER|SWP_FRAMECHANGED );
1209 HWND topLevelParent = GetParent( p_vout->p_sys->hparent );
1210 //HWND topLevelParent = GetAncestor( p_vout->p_sys->hparent, GA_ROOT );
1211 ShowWindow( topLevelParent, SW_HIDE );
1214 SetForegroundWindow( hwnd );
1218 msg_Dbg( p_vout, "leaving fullscreen mode" );
1219 /* Change window style, no borders and no title bar */
1220 SetWindowLong( hwnd, GWL_STYLE, p_vout->p_sys->i_window_style );
1223 SetWindowState( hwnd, SW_SHOWNORMAL );
1225 if( p_vout->p_sys->hparent )
1228 GetClientRect( p_vout->p_sys->hparent, &rect );
1229 SetParent( p_vout->p_sys->hwnd, p_vout->p_sys->hparent );
1230 SetWindowPos( p_vout->p_sys->hwnd, 0, 0, 0,
1231 rect.right, rect.bottom,
1232 SWP_NOZORDER|SWP_FRAMECHANGED );
1234 HWND topLevelParent = GetParent( p_vout->p_sys->hparent );
1235 //HWND topLevelParent = GetAncestor( p_vout->p_sys->hparent, GA_ROOT );
1236 ShowWindow( topLevelParent, SW_SHOW );
1237 SetForegroundWindow( p_vout->p_sys->hparent );
1238 ShowWindow( hwnd, SW_HIDE );
1241 /* Make sure the mouse cursor is displayed */
1242 PostMessage( p_vout->p_sys->hwnd, WM_VLC_SHOW_MOUSE, 0, 0 );
1246 /* Update the object variable and trigger callback */
1247 val.b_bool = p_vout->b_fullscreen;
1248 var_Set( p_vout, "fullscreen", val );