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 *****************************************************************************/
29 #include <errno.h> /* ENOMEM */
30 #include <ctype.h> /* tolower() */
33 # 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
62 #if defined(UNDER_CE) && !defined(__PLUGIN__) /*FIXME*/
63 # define SHFS_SHOWSIPBUTTON 0x0004
64 # define SHFS_HIDESIPBUTTON 0x0008
65 # define MENU_HEIGHT 26
66 BOOL SHFullScreen(HWND hwndRequester, DWORD dwState);
69 /*****************************************************************************
71 *****************************************************************************/
72 static int DirectXCreateWindow( vout_thread_t *p_vout );
73 static void DirectXCloseWindow ( vout_thread_t *p_vout );
74 static long FAR PASCAL DirectXEventProc( HWND, UINT, WPARAM, LPARAM );
76 static int Control( vout_thread_t *p_vout, int i_query, va_list args );
78 static void DirectXPopupMenu( event_thread_t *p_event, bool b_open )
82 var_Set( p_event->p_libvlc, "intf-popupmenu", val );
85 static int DirectXConvertKey( int i_key );
87 /*****************************************************************************
88 * EventThread: Create video window & handle its messages
89 *****************************************************************************
90 * This function creates a video window and then enters an infinite loop
91 * that handles the messages sent to that window.
92 * The main goal of this thread is to isolate the Win32 PeekMessage function
93 * because this one can block for a long time.
94 *****************************************************************************/
95 void EventThread( event_thread_t *p_event )
98 POINT old_mouse_pos = {0,0}, mouse_pos;
100 int i_width, i_height, i_x, i_y;
104 p_event->p_vout->pf_control = Control;
106 /* Create a window for the video */
107 /* Creating a window under Windows also initializes the thread's event
109 if( DirectXCreateWindow( p_event->p_vout ) )
110 p_event->b_dead = true;
112 /* Signal the creation of the window */
113 vlc_thread_ready( p_event );
116 /* Set power management stuff */
117 if( (hkernel32 = GetModuleHandle( _T("KERNEL32") ) ) )
119 ULONG (WINAPI* OurSetThreadExecutionState)( ULONG );
121 OurSetThreadExecutionState = (ULONG (WINAPI*)( ULONG ))
122 GetProcAddress( hkernel32, _T("SetThreadExecutionState") );
124 if( OurSetThreadExecutionState )
125 /* Prevent monitor from powering off */
126 OurSetThreadExecutionState( ES_DISPLAY_REQUIRED | ES_CONTINUOUS );
128 msg_Dbg( p_event, "no support for SetThreadExecutionState()" );
133 /* GetMessage will sleep if there's no message in the queue */
134 while( vlc_object_alive (p_event) && GetMessage( &msg, 0, 0, 0 ) )
136 /* Check if we are asked to exit */
137 if( !vlc_object_alive (p_event) )
140 switch( msg.message )
144 vout_PlacePicture( p_event->p_vout,
145 p_event->p_vout->p_sys->i_window_width,
146 p_event->p_vout->p_sys->i_window_height,
147 &i_x, &i_y, &i_width, &i_height );
149 if( msg.hwnd == p_event->p_vout->p_sys->hvideownd )
155 if( i_width && i_height )
157 val.i_int = ( GET_X_LPARAM(msg.lParam) - i_x ) *
158 p_event->p_vout->fmt_in.i_visible_width / i_width +
159 p_event->p_vout->fmt_in.i_x_offset;
160 var_Set( p_event->p_vout, "mouse-x", val );
161 val.i_int = ( GET_Y_LPARAM(msg.lParam) - i_y ) *
162 p_event->p_vout->fmt_in.i_visible_height / i_height +
163 p_event->p_vout->fmt_in.i_y_offset;
164 var_Set( p_event->p_vout, "mouse-y", val );
167 var_Set( p_event->p_vout, "mouse-moved", val );
171 GetCursorPos( &mouse_pos );
172 if( (abs(mouse_pos.x - old_mouse_pos.x) > 2 ||
173 (abs(mouse_pos.y - old_mouse_pos.y)) > 2 ) )
175 GetCursorPos( &old_mouse_pos );
176 p_event->p_vout->p_sys->i_lastmoved = mdate();
178 if( p_event->p_vout->p_sys->b_cursor_hidden )
180 p_event->p_vout->p_sys->b_cursor_hidden = 0;
186 case WM_VLC_HIDE_MOUSE:
187 if( p_event->p_vout->p_sys->b_cursor_hidden ) break;
188 p_event->p_vout->p_sys->b_cursor_hidden = true;
189 GetCursorPos( &old_mouse_pos );
193 case WM_VLC_SHOW_MOUSE:
194 if( !p_event->p_vout->p_sys->b_cursor_hidden ) break;
195 p_event->p_vout->p_sys->b_cursor_hidden = false;
196 GetCursorPos( &old_mouse_pos );
201 var_Get( p_event->p_vout, "mouse-button-down", &val );
203 var_Set( p_event->p_vout, "mouse-button-down", val );
204 DirectXPopupMenu( p_event, false );
208 var_Get( p_event->p_vout, "mouse-button-down", &val );
210 var_Set( p_event->p_vout, "mouse-button-down", val );
213 var_Set( p_event->p_vout, "mouse-clicked", val );
216 case WM_LBUTTONDBLCLK:
217 p_event->p_vout->p_sys->i_changes |= VOUT_FULLSCREEN_CHANGE;
221 var_Get( p_event->p_vout, "mouse-button-down", &val );
223 var_Set( p_event->p_vout, "mouse-button-down", val );
224 DirectXPopupMenu( p_event, false );
228 var_Get( p_event->p_vout, "mouse-button-down", &val );
230 var_Set( p_event->p_vout, "mouse-button-down", val );
234 var_Get( p_event->p_vout, "mouse-button-down", &val );
236 var_Set( p_event->p_vout, "mouse-button-down", val );
237 DirectXPopupMenu( p_event, false );
241 var_Get( p_event->p_vout, "mouse-button-down", &val );
243 var_Set( p_event->p_vout, "mouse-button-down", val );
244 DirectXPopupMenu( p_event, true );
249 /* The key events are first processed here and not translated
250 * into WM_CHAR events because we need to know the status of the
252 val.i_int = DirectXConvertKey( msg.wParam );
255 /* This appears to be a "normal" (ascii) key */
256 val.i_int = tolower( MapVirtualKey( msg.wParam, 2 ) );
261 if( GetKeyState(VK_CONTROL) & 0x8000 )
263 val.i_int |= KEY_MODIFIER_CTRL;
265 if( GetKeyState(VK_SHIFT) & 0x8000 )
267 val.i_int |= KEY_MODIFIER_SHIFT;
269 if( GetKeyState(VK_MENU) & 0x8000 )
271 val.i_int |= KEY_MODIFIER_ALT;
274 var_Set( p_event->p_libvlc, "key-pressed", val );
279 if( GET_WHEEL_DELTA_WPARAM( msg.wParam ) > 0 )
281 val.i_int = KEY_MOUSEWHEELUP;
285 val.i_int = KEY_MOUSEWHEELDOWN;
289 if( GetKeyState(VK_CONTROL) & 0x8000 )
291 val.i_int |= KEY_MODIFIER_CTRL;
293 if( GetKeyState(VK_SHIFT) & 0x8000 )
295 val.i_int |= KEY_MODIFIER_SHIFT;
297 if( GetKeyState(VK_MENU) & 0x8000 )
299 val.i_int |= KEY_MODIFIER_ALT;
302 var_Set( p_event->p_libvlc, "key-pressed", val );
306 case WM_VLC_CHANGE_TEXT:
307 var_Get( p_event->p_vout, "video-title", &val );
308 if( !val.psz_string || !*val.psz_string ) /* Default video title */
310 free( val.psz_string );
312 #ifdef MODULE_NAME_IS_wingdi
313 val.psz_string = strdup( VOUT_TITLE " (WinGDI output)" );
315 #ifdef MODULE_NAME_IS_wingapi
316 val.psz_string = strdup( VOUT_TITLE " (WinGAPI output)" );
318 #ifdef MODULE_NAME_IS_glwin32
319 val.psz_string = strdup( VOUT_TITLE " (OpenGL output)" );
321 #ifdef MODULE_NAME_IS_direct3d
322 val.psz_string = strdup( VOUT_TITLE " (Direct3D output)" );
324 #ifdef MODULE_NAME_IS_vout_directx
325 if( p_event->p_vout->p_sys->b_using_overlay ) val.psz_string =
326 strdup( VOUT_TITLE " (hardware YUV overlay DirectX output)" );
327 else if( p_event->p_vout->p_sys->b_hw_yuv ) val.psz_string =
328 strdup( VOUT_TITLE " (hardware YUV DirectX output)" );
329 else val.psz_string =
330 strdup( VOUT_TITLE " (software RGB DirectX output)" );
336 wchar_t *psz_title = malloc( strlen(val.psz_string) * 2 + 2 );
339 mbstowcs( psz_title, val.psz_string, strlen(val.psz_string)*2);
340 psz_title[strlen(val.psz_string)] = 0;
341 free( val.psz_string ); val.psz_string = (char *)psz_title;
346 SetWindowText( p_event->p_vout->p_sys->hwnd,
347 (LPCTSTR)val.psz_string );
348 if( p_event->p_vout->p_sys->hfswnd )
349 SetWindowText( p_event->p_vout->p_sys->hfswnd,
350 (LPCTSTR)val.psz_string );
351 free( val.psz_string );
355 /* Messages we don't handle directly are dispatched to the
356 * window procedure */
357 TranslateMessage(&msg);
358 DispatchMessage(&msg);
363 } /* End Main loop */
365 /* Check for WM_QUIT if we created the window */
366 if( !p_event->p_vout->p_sys->hparent && msg.message == WM_QUIT )
368 msg_Warn( p_event, "WM_QUIT... should not happen!!" );
369 p_event->p_vout->p_sys->hwnd = NULL; /* Window already destroyed */
372 msg_Dbg( p_event, "DirectXEventThread terminating" );
374 /* clear the changes formerly signaled */
375 p_event->p_vout->p_sys->i_changes = 0;
377 DirectXCloseWindow( p_event->p_vout );
381 /* following functions are local */
383 /*****************************************************************************
384 * DirectXCreateWindow: create a window for the video.
385 *****************************************************************************
386 * Before creating a direct draw surface, we need to create a window in which
387 * the video will be displayed. This window will also allow us to capture the
389 *****************************************************************************/
390 static int DirectXCreateWindow( vout_thread_t *p_vout )
395 WNDCLASS wc; /* window class components */
396 HICON vlc_icon = NULL;
397 char vlc_path[MAX_PATH+1];
398 int i_style, i_stylex;
400 msg_Dbg( p_vout, "DirectXCreateWindow" );
402 /* Get this module's instance */
403 hInstance = GetModuleHandle(NULL);
405 /* If an external window was specified, we'll draw in it. */
406 p_vout->p_sys->hparent =
407 vout_RequestWindow( p_vout, &p_vout->p_sys->i_window_x,
408 &p_vout->p_sys->i_window_y,
409 &p_vout->p_sys->i_window_width,
410 &p_vout->p_sys->i_window_height );
412 /* We create the window ourself, there is no previous window proc. */
413 p_vout->p_sys->pf_wndproc = NULL;
415 /* Get the Icon from the main app */
418 if( GetModuleFileName( NULL, vlc_path, MAX_PATH ) )
420 vlc_icon = ExtractIcon( hInstance, vlc_path, 0 );
424 /* Fill in the window class structure */
425 wc.style = CS_OWNDC|CS_DBLCLKS; /* style: dbl click */
426 wc.lpfnWndProc = (WNDPROC)DirectXEventProc; /* event handler */
427 wc.cbClsExtra = 0; /* no extra class data */
428 wc.cbWndExtra = 0; /* no extra window data */
429 wc.hInstance = hInstance; /* instance */
430 wc.hIcon = vlc_icon; /* load the vlc big icon */
431 wc.hCursor = LoadCursor(NULL, IDC_ARROW); /* default cursor */
432 wc.hbrBackground = GetStockObject(BLACK_BRUSH); /* background color */
433 wc.lpszMenuName = NULL; /* no menu */
434 wc.lpszClassName = _T("VLC DirectX"); /* use a special class */
436 /* Register the window class */
437 if( !RegisterClass(&wc) )
441 if( vlc_icon ) DestroyIcon( vlc_icon );
443 /* Check why it failed. If it's because one already exists
444 * then fine, otherwise return with an error. */
445 if( !GetClassInfo( hInstance, _T("VLC DirectX"), &wndclass ) )
447 msg_Err( p_vout, "DirectXCreateWindow RegisterClass FAILED (err=%lu)", GetLastError() );
452 /* Register the video sub-window class */
453 wc.lpszClassName = _T("VLC DirectX video"); wc.hIcon = 0;
454 wc.hbrBackground = NULL; /* no background color */
455 if( !RegisterClass(&wc) )
459 /* Check why it failed. If it's because one already exists
460 * then fine, otherwise return with an error. */
461 if( !GetClassInfo( hInstance, _T("VLC DirectX video"), &wndclass ) )
463 msg_Err( p_vout, "DirectXCreateWindow RegisterClass FAILED (err=%lu)", GetLastError() );
468 /* When you create a window you give the dimensions you wish it to
469 * have. Unfortunatly these dimensions will include the borders and
470 * titlebar. We use the following function to find out the size of
471 * the window corresponding to the useable surface we want */
472 rect_window.top = 10;
473 rect_window.left = 10;
474 rect_window.right = rect_window.left + p_vout->p_sys->i_window_width;
475 rect_window.bottom = rect_window.top + p_vout->p_sys->i_window_height;
477 if( var_GetBool( p_vout, "video-deco" ) )
479 /* Open with window decoration */
480 AdjustWindowRect( &rect_window, WS_OVERLAPPEDWINDOW|WS_SIZEBOX, 0 );
481 i_style = WS_OVERLAPPEDWINDOW|WS_SIZEBOX|WS_VISIBLE|WS_CLIPCHILDREN;
486 /* No window decoration */
487 AdjustWindowRect( &rect_window, WS_POPUP, 0 );
488 i_style = WS_POPUP|WS_VISIBLE|WS_CLIPCHILDREN;
489 i_stylex = 0; // WS_EX_TOOLWINDOW; Is TOOLWINDOW really needed ?
490 // It messes up the fullscreen window.
493 if( p_vout->p_sys->hparent )
495 i_style = WS_VISIBLE|WS_CLIPCHILDREN|WS_CHILD;
499 p_vout->p_sys->i_window_style = i_style;
501 /* Create the window */
502 p_vout->p_sys->hwnd =
503 CreateWindowEx( WS_EX_NOPARENTNOTIFY | i_stylex,
504 _T("VLC DirectX"), /* name of window class */
505 _T(VOUT_TITLE) _T(" (DirectX Output)"), /* window title */
506 i_style, /* window style */
507 (p_vout->p_sys->i_window_x < 0) ? CW_USEDEFAULT :
508 (UINT)p_vout->p_sys->i_window_x, /* default X coordinate */
509 (p_vout->p_sys->i_window_y < 0) ? CW_USEDEFAULT :
510 (UINT)p_vout->p_sys->i_window_y, /* default Y coordinate */
511 rect_window.right - rect_window.left, /* window width */
512 rect_window.bottom - rect_window.top, /* window height */
513 p_vout->p_sys->hparent, /* parent window */
514 NULL, /* no menu in this window */
515 hInstance, /* handle of this program instance */
516 (LPVOID)p_vout ); /* send p_vout to WM_CREATE */
518 if( !p_vout->p_sys->hwnd )
520 msg_Warn( p_vout, "DirectXCreateWindow create window FAILED (err=%lu)", GetLastError() );
524 if( p_vout->p_sys->hparent )
528 /* We don't want the window owner to overwrite our client area */
529 i_style = GetWindowLong( p_vout->p_sys->hparent, GWL_STYLE );
531 if( !(i_style & WS_CLIPCHILDREN) )
532 /* Hmmm, apparently this is a blocking call... */
533 SetWindowLong( p_vout->p_sys->hparent, GWL_STYLE,
534 i_style | WS_CLIPCHILDREN );
536 /* Create our fullscreen window */
537 p_vout->p_sys->hfswnd =
538 CreateWindowEx( WS_EX_APPWINDOW, _T("VLC DirectX"),
539 _T(VOUT_TITLE) _T(" (DirectX Output)"),
540 WS_OVERLAPPEDWINDOW|WS_CLIPCHILDREN|WS_SIZEBOX,
541 CW_USEDEFAULT, CW_USEDEFAULT,
542 CW_USEDEFAULT, CW_USEDEFAULT,
543 NULL, NULL, hInstance, NULL );
546 /* Append a "Always On Top" entry in the system menu */
547 hMenu = GetSystemMenu( p_vout->p_sys->hwnd, FALSE );
548 AppendMenu( hMenu, MF_SEPARATOR, 0, _T("") );
549 AppendMenu( hMenu, MF_STRING | MF_UNCHECKED,
550 IDM_TOGGLE_ON_TOP, _T("Always on &Top") );
552 /* Create video sub-window. This sub window will always exactly match
553 * the size of the video, which allows us to use crazy overlay colorkeys
554 * without having them shown outside of the video area. */
555 p_vout->p_sys->hvideownd =
556 CreateWindow( _T("VLC DirectX video"), _T(""), /* window class */
557 WS_CHILD, /* window style, not visible initially */
559 p_vout->render.i_width, /* default width */
560 p_vout->render.i_height, /* default height */
561 p_vout->p_sys->hwnd, /* parent window */
563 (LPVOID)p_vout ); /* send p_vout to WM_CREATE */
565 if( !p_vout->p_sys->hvideownd )
566 msg_Warn( p_vout, "can't create video sub-window" );
568 msg_Dbg( p_vout, "created video sub-window" );
570 /* Now display the window */
571 ShowWindow( p_vout->p_sys->hwnd, SW_SHOW );
576 /*****************************************************************************
577 * DirectXCloseWindow: close the window created by DirectXCreateWindow
578 *****************************************************************************
579 * This function returns all resources allocated by DirectXCreateWindow.
580 *****************************************************************************/
581 static void DirectXCloseWindow( vout_thread_t *p_vout )
583 msg_Dbg( p_vout, "DirectXCloseWindow" );
585 DestroyWindow( p_vout->p_sys->hwnd );
586 if( p_vout->p_sys->hfswnd ) DestroyWindow( p_vout->p_sys->hfswnd );
588 if( p_vout->p_sys->hparent )
589 vout_ReleaseWindow( p_vout, (void *)p_vout->p_sys->hparent );
591 p_vout->p_sys->hwnd = NULL;
593 /* We don't unregister the Window Class because it could lead to race
594 * conditions and it will be done anyway by the system when the app will
598 /*****************************************************************************
599 * UpdateRects: update clipping rectangles
600 *****************************************************************************
601 * This function is called when the window position or size are changed, and
602 * its job is to update the source and destination RECTs used to display the
604 *****************************************************************************/
605 void UpdateRects( vout_thread_t *p_vout, bool b_force )
607 #define rect_src p_vout->p_sys->rect_src
608 #define rect_src_clipped p_vout->p_sys->rect_src_clipped
609 #define rect_dest p_vout->p_sys->rect_dest
610 #define rect_dest_clipped p_vout->p_sys->rect_dest_clipped
612 int i_width, i_height, i_x, i_y;
617 /* Retrieve the window size */
618 GetClientRect( p_vout->p_sys->hwnd, &rect );
620 /* Retrieve the window position */
621 point.x = point.y = 0;
622 ClientToScreen( p_vout->p_sys->hwnd, &point );
624 /* If nothing changed, we can return */
626 && p_vout->p_sys->i_window_width == rect.right
627 && p_vout->p_sys->i_window_height == rect.bottom
628 && p_vout->p_sys->i_window_x == point.x
629 && p_vout->p_sys->i_window_y == point.y )
634 /* Update the window position and size */
635 p_vout->p_sys->i_window_x = point.x;
636 p_vout->p_sys->i_window_y = point.y;
637 p_vout->p_sys->i_window_width = rect.right;
638 p_vout->p_sys->i_window_height = rect.bottom;
640 vout_PlacePicture( p_vout, rect.right, rect.bottom,
641 &i_x, &i_y, &i_width, &i_height );
643 if( p_vout->p_sys->hvideownd )
644 SetWindowPos( p_vout->p_sys->hvideownd, 0,
645 i_x, i_y, i_width, i_height,
646 SWP_NOCOPYBITS|SWP_NOZORDER|SWP_ASYNCWINDOWPOS );
648 /* Destination image position and dimensions */
649 rect_dest.left = point.x + i_x;
650 rect_dest.right = rect_dest.left + i_width;
651 rect_dest.top = point.y + i_y;
652 rect_dest.bottom = rect_dest.top + i_height;
654 #ifdef MODULE_NAME_IS_vout_directx
655 /* Apply overlay hardware constraints */
656 if( p_vout->p_sys->b_using_overlay )
658 if( p_vout->p_sys->i_align_dest_boundary )
659 rect_dest.left = ( rect_dest.left +
660 p_vout->p_sys->i_align_dest_boundary / 2 ) &
661 ~p_vout->p_sys->i_align_dest_boundary;
663 if( p_vout->p_sys->i_align_dest_size )
664 rect_dest.right = (( rect_dest.right - rect_dest.left +
665 p_vout->p_sys->i_align_dest_size / 2 ) &
666 ~p_vout->p_sys->i_align_dest_size) + rect_dest.left;
669 /* UpdateOverlay directdraw function doesn't automatically clip to the
670 * display size so we need to do it otherwise it will fail */
672 /* Clip the destination window */
673 if( !IntersectRect( &rect_dest_clipped, &rect_dest,
674 &p_vout->p_sys->rect_display ) )
676 SetRectEmpty( &rect_src_clipped );
681 msg_Dbg( p_vout, "DirectXUpdateRects image_dst_clipped coords:"
683 rect_dest_clipped.left, rect_dest_clipped.top,
684 rect_dest_clipped.right, rect_dest_clipped.bottom );
687 #else /* MODULE_NAME_IS_vout_directx */
689 /* AFAIK, there are no clipping constraints in Direct3D, OpenGL and GDI */
690 rect_dest_clipped = rect_dest;
694 /* the 2 following lines are to fix a bug when clicking on the desktop */
695 if( (rect_dest_clipped.right - rect_dest_clipped.left)==0 ||
696 (rect_dest_clipped.bottom - rect_dest_clipped.top)==0 )
698 SetRectEmpty( &rect_src_clipped );
702 /* src image dimensions */
705 rect_src.right = p_vout->render.i_width;
706 rect_src.bottom = p_vout->render.i_height;
708 /* Clip the source image */
709 rect_src_clipped.left = p_vout->fmt_out.i_x_offset +
710 (rect_dest_clipped.left - rect_dest.left) *
711 p_vout->fmt_out.i_visible_width / (rect_dest.right - rect_dest.left);
712 rect_src_clipped.right = p_vout->fmt_out.i_x_offset +
713 p_vout->fmt_out.i_visible_width -
714 (rect_dest.right - rect_dest_clipped.right) *
715 p_vout->fmt_out.i_visible_width / (rect_dest.right - rect_dest.left);
716 rect_src_clipped.top = p_vout->fmt_out.i_y_offset +
717 (rect_dest_clipped.top - rect_dest.top) *
718 p_vout->fmt_out.i_visible_height / (rect_dest.bottom - rect_dest.top);
719 rect_src_clipped.bottom = p_vout->fmt_out.i_y_offset +
720 p_vout->fmt_out.i_visible_height -
721 (rect_dest.bottom - rect_dest_clipped.bottom) *
722 p_vout->fmt_out.i_visible_height / (rect_dest.bottom - rect_dest.top);
724 #ifdef MODULE_NAME_IS_vout_directx
725 /* Apply overlay hardware constraints */
726 if( p_vout->p_sys->b_using_overlay )
728 if( p_vout->p_sys->i_align_src_boundary )
729 rect_src_clipped.left = ( rect_src_clipped.left +
730 p_vout->p_sys->i_align_src_boundary / 2 ) &
731 ~p_vout->p_sys->i_align_src_boundary;
733 if( p_vout->p_sys->i_align_src_size )
734 rect_src_clipped.right = (( rect_src_clipped.right -
735 rect_src_clipped.left +
736 p_vout->p_sys->i_align_src_size / 2 ) &
737 ~p_vout->p_sys->i_align_src_size) + rect_src_clipped.left;
742 msg_Dbg( p_vout, "DirectXUpdateRects image_src_clipped"
743 " coords: %i,%i,%i,%i",
744 rect_src_clipped.left, rect_src_clipped.top,
745 rect_src_clipped.right, rect_src_clipped.bottom );
748 #ifdef MODULE_NAME_IS_vout_directx
749 /* The destination coordinates need to be relative to the current
750 * directdraw primary surface (display) */
751 rect_dest_clipped.left -= p_vout->p_sys->rect_display.left;
752 rect_dest_clipped.right -= p_vout->p_sys->rect_display.left;
753 rect_dest_clipped.top -= p_vout->p_sys->rect_display.top;
754 rect_dest_clipped.bottom -= p_vout->p_sys->rect_display.top;
756 if( p_vout->p_sys->b_using_overlay )
757 DirectDrawUpdateOverlay( p_vout );
760 /* Signal the change in size/position */
761 p_vout->p_sys->i_changes |= DX_POSITION_CHANGE;
764 #undef rect_src_clipped
766 #undef rect_dest_clipped
769 /*****************************************************************************
770 * DirectXEventProc: This is the window event processing function.
771 *****************************************************************************
772 * On Windows, when you create a window you have to attach an event processing
773 * function to it. The aim of this function is to manage "Queued Messages" and
774 * "Nonqueued Messages".
775 * Queued Messages are those picked up and retransmitted by vout_Manage
776 * (using the GetMessage and DispatchMessage functions).
777 * Nonqueued Messages are those that Windows will send directly to this
778 * procedure (like WM_DESTROY, WM_WINDOWPOSCHANGED...)
779 *****************************************************************************/
780 static long FAR PASCAL DirectXEventProc( HWND hwnd, UINT message,
781 WPARAM wParam, LPARAM lParam )
783 vout_thread_t *p_vout;
785 if( message == WM_CREATE )
787 /* Store p_vout for future use */
788 p_vout = (vout_thread_t *)((CREATESTRUCT *)lParam)->lpCreateParams;
789 SetWindowLongPtr( hwnd, GWLP_USERDATA, (LONG_PTR)p_vout );
794 p_vout = (vout_thread_t *)GetWindowLongPtr( hwnd, GWLP_USERDATA );
797 /* Hmmm mozilla does manage somehow to save the pointer to our
798 * windowproc and still calls it after the vout has been closed. */
799 return DefWindowProc(hwnd, message, wParam, lParam);
804 /* Catch the screensaver and the monitor turn-off */
805 if( message == WM_SYSCOMMAND &&
806 ( (wParam & 0xFFF0) == SC_SCREENSAVE || (wParam & 0xFFF0) == SC_MONITORPOWER ) )
808 //if( p_vout ) msg_Dbg( p_vout, "WinProc WM_SYSCOMMAND screensaver" );
809 return 0; /* this stops them from happening */
813 if( hwnd == p_vout->p_sys->hvideownd )
817 #ifdef MODULE_NAME_IS_vout_directx
819 /* For overlay, we need to erase background */
820 return !p_vout->p_sys->b_using_overlay ?
821 1 : DefWindowProc(hwnd, message, wParam, lParam);
824 ** For overlay, DefWindowProc() will erase dirty regions
826 ** For non-overlay, vout will paint the whole window at
827 ** regular interval, therefore dirty regions can be ignored
828 ** to minimize repaint.
830 if( !p_vout->p_sys->b_using_overlay )
832 ValidateRect(hwnd, NULL);
834 // fall through to default
837 ** For OpenGL and Direct3D, vout will update the whole
838 ** window at regular interval, therefore dirty region
839 ** can be ignored to minimize repaint.
842 /* nothing to erase */
845 /* nothing to repaint */
846 ValidateRect(hwnd, NULL);
850 return DefWindowProc(hwnd, message, wParam, lParam);
857 case WM_WINDOWPOSCHANGED:
858 UpdateRects( p_vout, true );
861 /* the user wants to close the window */
864 playlist_t * p_playlist = vlc_object_find( p_vout,
865 VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
868 playlist_Stop( p_playlist );
869 vlc_object_release( p_playlist );
874 /* the window has been closed so shut down everything now */
876 msg_Dbg( p_vout, "WinProc WM_DESTROY" );
877 /* just destroy the window */
878 PostQuitMessage( 0 );
884 case IDM_TOGGLE_ON_TOP: /* toggle the "on top" status */
887 msg_Dbg( p_vout, "WinProc WM_SYSCOMMAND: IDM_TOGGLE_ON_TOP");
889 /* Change the current value */
890 var_Get( p_vout, "video-on-top", &val );
891 val.b_bool = !val.b_bool;
892 var_Set( p_vout, "video-on-top", val );
901 return DefWindowProc(hwnd, message, wParam, lParam);
904 #ifdef MODULE_NAME_IS_wingapi
905 p_vout->p_sys->b_focus = false;
906 if( !p_vout->p_sys->b_parent_focus ) GXSuspend();
909 if( hWnd == p_vout->p_sys->hfswnd )
911 HWND htbar = FindWindow( _T("HHTaskbar"), NULL );
912 ShowWindow( htbar, SW_SHOW );
915 if( !p_vout->p_sys->hparent ||
916 hWnd == p_vout->p_sys->hfswnd )
918 SHFullScreen( hWnd, SHFS_SHOWSIPBUTTON );
924 #ifdef MODULE_NAME_IS_wingapi
925 p_vout->p_sys->b_focus = true;
929 if( p_vout->p_sys->hparent &&
930 hWnd != p_vout->p_sys->hfswnd && p_vout->b_fullscreen )
931 p_vout->p_sys->i_changes |= VOUT_FULLSCREEN_CHANGE;
933 if( hWnd == p_vout->p_sys->hfswnd )
935 HWND htbar = FindWindow( _T("HHTaskbar"), NULL );
936 ShowWindow( htbar, SW_HIDE );
939 if( !p_vout->p_sys->hparent ||
940 hWnd == p_vout->p_sys->hfswnd )
942 SHFullScreen( hWnd, SHFS_HIDESIPBUTTON );
948 //msg_Dbg( p_vout, "WinProc WM Default %i", message );
952 /* Let windows handle the message */
953 return DefWindowProc(hwnd, message, wParam, lParam);
961 } dxkeys_to_vlckeys[] =
963 { VK_F1, KEY_F1 }, { VK_F2, KEY_F2 }, { VK_F3, KEY_F3 }, { VK_F4, KEY_F4 },
964 { VK_F5, KEY_F5 }, { VK_F6, KEY_F6 }, { VK_F7, KEY_F7 }, { VK_F8, KEY_F8 },
965 { VK_F9, KEY_F9 }, { VK_F10, KEY_F10 }, { VK_F11, KEY_F11 },
968 { VK_RETURN, KEY_ENTER },
969 { VK_SPACE, KEY_SPACE },
970 { VK_ESCAPE, KEY_ESC },
972 { VK_LEFT, KEY_LEFT },
973 { VK_RIGHT, KEY_RIGHT },
975 { VK_DOWN, KEY_DOWN },
977 { VK_HOME, KEY_HOME },
979 { VK_PRIOR, KEY_PAGEUP },
980 { VK_NEXT, KEY_PAGEDOWN },
982 { VK_INSERT, KEY_INSERT },
983 { VK_DELETE, KEY_DELETE },
989 { VK_BROWSER_BACK, KEY_BROWSER_BACK },
990 { VK_BROWSER_FORWARD, KEY_BROWSER_FORWARD },
991 { VK_BROWSER_REFRESH, KEY_BROWSER_REFRESH },
992 { VK_BROWSER_STOP, KEY_BROWSER_STOP },
993 { VK_BROWSER_SEARCH, KEY_BROWSER_SEARCH },
994 { VK_BROWSER_FAVORITES, KEY_BROWSER_FAVORITES },
995 { VK_BROWSER_HOME, KEY_BROWSER_HOME },
996 { VK_VOLUME_MUTE, KEY_VOLUME_MUTE },
997 { VK_VOLUME_DOWN, KEY_VOLUME_DOWN },
998 { VK_VOLUME_UP, KEY_VOLUME_UP },
999 { VK_MEDIA_NEXT_TRACK, KEY_MEDIA_NEXT_TRACK },
1000 { VK_MEDIA_PREV_TRACK, KEY_MEDIA_PREV_TRACK },
1001 { VK_MEDIA_STOP, KEY_MEDIA_STOP },
1002 { VK_MEDIA_PLAY_PAUSE, KEY_MEDIA_PLAY_PAUSE },
1007 static int DirectXConvertKey( int i_key )
1011 for( i = 0; dxkeys_to_vlckeys[i].i_dxkey != 0; i++ )
1013 if( dxkeys_to_vlckeys[i].i_dxkey == i_key )
1015 return dxkeys_to_vlckeys[i].i_vlckey;
1022 /*****************************************************************************
1023 * Control: control facility for the vout
1024 *****************************************************************************/
1025 static int Control( vout_thread_t *p_vout, int i_query, va_list args )
1027 unsigned int *pi_width, *pi_height;
1034 if( p_vout->p_sys->hparent )
1035 return vout_ControlWindow( p_vout,
1036 (void *)p_vout->p_sys->hparent, i_query, args );
1038 pi_width = va_arg( args, unsigned int * );
1039 pi_height = va_arg( args, unsigned int * );
1041 GetClientRect( p_vout->p_sys->hwnd, &rect_window );
1043 *pi_width = rect_window.right - rect_window.left;
1044 *pi_height = rect_window.bottom - rect_window.top;
1048 if( p_vout->p_sys->hparent )
1049 return vout_ControlWindow( p_vout,
1050 (void *)p_vout->p_sys->hparent, i_query, args );
1052 /* Update dimensions */
1053 rect_window.top = rect_window.left = 0;
1054 rect_window.right = va_arg( args, unsigned int );
1055 rect_window.bottom = va_arg( args, unsigned int );
1056 if( !rect_window.right ) rect_window.right = p_vout->i_window_width;
1057 if( !rect_window.bottom ) rect_window.bottom = p_vout->i_window_height;
1058 AdjustWindowRect( &rect_window, p_vout->p_sys->i_window_style, 0 );
1060 SetWindowPos( p_vout->p_sys->hwnd, 0, 0, 0,
1061 rect_window.right - rect_window.left,
1062 rect_window.bottom - rect_window.top, SWP_NOMOVE );
1067 ShowWindow( p_vout->p_sys->hwnd, SW_HIDE );
1069 /* Retrieve the window position */
1070 point.x = point.y = 0;
1071 ClientToScreen( p_vout->p_sys->hwnd, &point );
1075 if( i_query == VOUT_REPARENT ) d = (HWND)va_arg( args, int );
1078 vlc_mutex_lock( &p_vout->p_sys->lock );
1079 p_vout->p_sys->hparent = 0;
1080 vlc_mutex_unlock( &p_vout->p_sys->lock );
1081 SetParent( p_vout->p_sys->hwnd, 0 );
1082 p_vout->p_sys->i_window_style =
1083 WS_CLIPCHILDREN | WS_OVERLAPPEDWINDOW | WS_SIZEBOX;
1084 SetWindowLong( p_vout->p_sys->hwnd, GWL_STYLE,
1085 p_vout->p_sys->i_window_style |
1086 (i_query == VOUT_CLOSE ? 0 : WS_VISIBLE) );
1087 SetWindowLong( p_vout->p_sys->hwnd, GWL_EXSTYLE, WS_EX_APPWINDOW );
1088 SetWindowPos( p_vout->p_sys->hwnd, 0, point.x, point.y, 0, 0,
1089 SWP_NOSIZE|SWP_NOZORDER|SWP_FRAMECHANGED );
1093 vlc_mutex_lock( &p_vout->p_sys->lock );
1094 p_vout->p_sys->hparent = d;
1095 vlc_mutex_unlock( &p_vout->p_sys->lock );
1097 SetParent( p_vout->p_sys->hwnd, d );
1098 p_vout->p_sys->i_window_style = WS_CLIPCHILDREN;
1099 SetWindowLong( p_vout->p_sys->hwnd, GWL_STYLE,
1100 p_vout->p_sys->i_window_style |
1101 (i_query == VOUT_CLOSE ? 0 : WS_VISIBLE) );
1102 SetWindowLong( p_vout->p_sys->hwnd, GWL_EXSTYLE, WS_EX_APPWINDOW );
1104 /* Retrieve the parent size */
1106 GetClientRect( d, &rect );
1107 SetWindowPos( p_vout->p_sys->hwnd, d, point.x, point.y, rect.right, rect.bottom,
1111 return vout_vaControlDefault( p_vout, i_query, args );
1113 case VOUT_SET_STAY_ON_TOP:
1114 if( p_vout->p_sys->hparent && !var_GetBool( p_vout, "fullscreen" ) )
1115 return vout_ControlWindow( p_vout,
1116 (void *)p_vout->p_sys->hparent, i_query, args );
1118 p_vout->p_sys->b_on_top_change = true;
1121 #ifdef MODULE_NAME_IS_wingapi
1122 case VOUT_SET_FOCUS:
1123 b_bool = (bool) va_arg( args, int );
1124 p_vout->p_sys->b_parent_focus = b_bool;
1125 if( b_bool ) GXResume();
1126 else if( !p_vout->p_sys->b_focus ) GXSuspend();
1131 return vout_vaControlDefault( p_vout, i_query, args );
1136 /* Internal wrapper over GetWindowPlacement / SetWindowPlacement */
1137 static void SetWindowState(HWND hwnd, int nShowCmd)
1139 WINDOWPLACEMENT window_placement;
1140 window_placement.length = sizeof(WINDOWPLACEMENT);
1141 GetWindowPlacement( hwnd, &window_placement );
1142 window_placement.showCmd = nShowCmd;
1143 SetWindowPlacement( hwnd, &window_placement );
1144 SetWindowPos( hwnd, 0, 0, 0, 0, 0,
1145 SWP_NOMOVE|SWP_NOSIZE|SWP_NOZORDER|SWP_FRAMECHANGED);
1148 /* Internal wrapper to call vout_ControlWindow for hparent */
1149 static void ControlParentWindow( vout_thread_t *p_vout, int i_query, ... )
1152 va_start( args, i_query );
1153 vout_ControlWindow( p_vout,
1154 (void *)p_vout->p_sys->hparent, i_query, args );
1158 void Win32ToggleFullscreen( vout_thread_t *p_vout )
1160 HWND hwnd = (p_vout->p_sys->hparent && p_vout->p_sys->hfswnd) ?
1161 p_vout->p_sys->hfswnd : p_vout->p_sys->hwnd;
1163 p_vout->b_fullscreen = ! p_vout->b_fullscreen;
1165 if( p_vout->b_fullscreen )
1167 msg_Dbg( p_vout, "entering fullscreen mode" );
1168 /* Change window style, no borders and no title bar */
1169 int i_style = WS_CLIPCHILDREN | WS_VISIBLE;
1170 SetWindowLong( hwnd, GWL_STYLE, i_style );
1172 if( p_vout->p_sys->hparent )
1174 /* Retrieve current window position so fullscreen will happen
1175 * on the right screen */
1176 POINT point = {0,0};
1178 ClientToScreen( p_vout->p_sys->hwnd, &point );
1179 GetClientRect( p_vout->p_sys->hwnd, &rect );
1180 SetWindowPos( hwnd, 0, point.x, point.y,
1181 rect.right, rect.bottom,
1182 SWP_NOZORDER|SWP_FRAMECHANGED );
1185 /* Maximize window */
1186 SetWindowState( hwnd, SW_SHOWMAXIMIZED );
1188 if( p_vout->p_sys->hparent )
1191 GetClientRect( hwnd, &rect );
1192 SetParent( p_vout->p_sys->hwnd, hwnd );
1193 SetWindowPos( p_vout->p_sys->hwnd, 0, 0, 0,
1194 rect.right, rect.bottom,
1195 SWP_NOZORDER|SWP_FRAMECHANGED );
1197 HWND topLevelParent = GetAncestor( p_vout->p_sys->hparent, GA_ROOT );
1198 ShowWindow( topLevelParent, SW_HIDE );
1201 SetForegroundWindow( hwnd );
1205 msg_Dbg( p_vout, "leaving fullscreen mode" );
1206 /* Change window style, no borders and no title bar */
1207 SetWindowLong( hwnd, GWL_STYLE, p_vout->p_sys->i_window_style );
1210 SetWindowState( hwnd, SW_SHOWNORMAL );
1212 if( p_vout->p_sys->hparent )
1215 GetClientRect( p_vout->p_sys->hparent, &rect );
1216 SetParent( p_vout->p_sys->hwnd, p_vout->p_sys->hparent );
1217 SetWindowPos( p_vout->p_sys->hwnd, 0, 0, 0,
1218 rect.right, rect.bottom,
1219 SWP_NOZORDER|SWP_FRAMECHANGED );
1221 HWND topLevelParent = GetAncestor( p_vout->p_sys->hparent, GA_ROOT );
1222 ShowWindow( topLevelParent, SW_SHOW );
1223 SetForegroundWindow( p_vout->p_sys->hparent );
1224 ShowWindow( hwnd, SW_HIDE );
1227 /* Make sure the mouse cursor is displayed */
1228 PostMessage( p_vout->p_sys->hwnd, WM_VLC_SHOW_MOUSE, 0, 0 );
1232 /* Update the object variable and trigger callback */
1233 val.b_bool = p_vout->b_fullscreen;
1234 var_Set( p_vout, "fullscreen", val );