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
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( vlc_object_t *p_this )
97 event_thread_t *p_event = (event_thread_t *)p_this;
99 POINT old_mouse_pos = {0,0}, mouse_pos;
101 unsigned int i_width, i_height, i_x, i_y;
105 p_event->p_vout->pf_control = Control;
107 /* Create a window for the video */
108 /* Creating a window under Windows also initializes the thread's event
110 if( DirectXCreateWindow( p_event->p_vout ) )
111 p_event->b_dead = true;
113 /* Signal the creation of the window */
114 vlc_thread_ready( p_event );
117 /* Set power management stuff */
118 if( (hkernel32 = GetModuleHandle( _T("KERNEL32") ) ) )
120 ULONG (WINAPI* OurSetThreadExecutionState)( ULONG );
122 OurSetThreadExecutionState = (ULONG (WINAPI*)( ULONG ))
123 GetProcAddress( hkernel32, _T("SetThreadExecutionState") );
125 if( OurSetThreadExecutionState )
126 /* Prevent monitor from powering off */
127 OurSetThreadExecutionState( ES_DISPLAY_REQUIRED | ES_CONTINUOUS );
129 msg_Dbg( p_event, "no support for SetThreadExecutionState()" );
134 /* GetMessage will sleep if there's no message in the queue */
135 while( vlc_object_alive (p_event) && GetMessage( &msg, 0, 0, 0 ) )
137 /* Check if we are asked to exit */
138 if( !vlc_object_alive (p_event) )
141 switch( msg.message )
145 vout_PlacePicture( p_event->p_vout,
146 p_event->p_vout->p_sys->i_window_width,
147 p_event->p_vout->p_sys->i_window_height,
148 &i_x, &i_y, &i_width, &i_height );
150 if( msg.hwnd == p_event->p_vout->p_sys->hvideownd )
156 if( i_width && i_height )
158 val.i_int = ( GET_X_LPARAM(msg.lParam) - i_x ) *
159 p_event->p_vout->fmt_in.i_visible_width / i_width +
160 p_event->p_vout->fmt_in.i_x_offset;
161 var_Set( p_event->p_vout, "mouse-x", val );
162 val.i_int = ( GET_Y_LPARAM(msg.lParam) - i_y ) *
163 p_event->p_vout->fmt_in.i_visible_height / i_height +
164 p_event->p_vout->fmt_in.i_y_offset;
165 var_Set( p_event->p_vout, "mouse-y", val );
168 var_Set( p_event->p_vout, "mouse-moved", val );
172 GetCursorPos( &mouse_pos );
173 if( (abs(mouse_pos.x - old_mouse_pos.x) > 2 ||
174 (abs(mouse_pos.y - old_mouse_pos.y)) > 2 ) )
176 GetCursorPos( &old_mouse_pos );
177 p_event->p_vout->p_sys->i_lastmoved = mdate();
179 if( p_event->p_vout->p_sys->b_cursor_hidden )
181 p_event->p_vout->p_sys->b_cursor_hidden = 0;
187 case WM_VLC_HIDE_MOUSE:
188 if( p_event->p_vout->p_sys->b_cursor_hidden ) break;
189 p_event->p_vout->p_sys->b_cursor_hidden = true;
190 GetCursorPos( &old_mouse_pos );
194 case WM_VLC_SHOW_MOUSE:
195 if( !p_event->p_vout->p_sys->b_cursor_hidden ) break;
196 p_event->p_vout->p_sys->b_cursor_hidden = false;
197 GetCursorPos( &old_mouse_pos );
202 var_Get( p_event->p_vout, "mouse-button-down", &val );
204 var_Set( p_event->p_vout, "mouse-button-down", val );
205 DirectXPopupMenu( p_event, false );
209 var_Get( p_event->p_vout, "mouse-button-down", &val );
211 var_Set( p_event->p_vout, "mouse-button-down", val );
214 var_Set( p_event->p_vout, "mouse-clicked", val );
217 case WM_LBUTTONDBLCLK:
218 p_event->p_vout->p_sys->i_changes |= VOUT_FULLSCREEN_CHANGE;
222 var_Get( p_event->p_vout, "mouse-button-down", &val );
224 var_Set( p_event->p_vout, "mouse-button-down", val );
225 DirectXPopupMenu( p_event, false );
229 var_Get( p_event->p_vout, "mouse-button-down", &val );
231 var_Set( p_event->p_vout, "mouse-button-down", val );
235 var_Get( p_event->p_vout, "mouse-button-down", &val );
237 var_Set( p_event->p_vout, "mouse-button-down", val );
238 DirectXPopupMenu( p_event, false );
242 var_Get( p_event->p_vout, "mouse-button-down", &val );
244 var_Set( p_event->p_vout, "mouse-button-down", val );
245 DirectXPopupMenu( p_event, true );
250 /* The key events are first processed here and not translated
251 * into WM_CHAR events because we need to know the status of the
253 val.i_int = DirectXConvertKey( msg.wParam );
256 /* This appears to be a "normal" (ascii) key */
257 val.i_int = tolower( MapVirtualKey( msg.wParam, 2 ) );
262 if( GetKeyState(VK_CONTROL) & 0x8000 )
264 val.i_int |= KEY_MODIFIER_CTRL;
266 if( GetKeyState(VK_SHIFT) & 0x8000 )
268 val.i_int |= KEY_MODIFIER_SHIFT;
270 if( GetKeyState(VK_MENU) & 0x8000 )
272 val.i_int |= KEY_MODIFIER_ALT;
275 var_Set( p_event->p_libvlc, "key-pressed", val );
280 if( GET_WHEEL_DELTA_WPARAM( msg.wParam ) > 0 )
282 val.i_int = KEY_MOUSEWHEELUP;
286 val.i_int = KEY_MOUSEWHEELDOWN;
290 if( GetKeyState(VK_CONTROL) & 0x8000 )
292 val.i_int |= KEY_MODIFIER_CTRL;
294 if( GetKeyState(VK_SHIFT) & 0x8000 )
296 val.i_int |= KEY_MODIFIER_SHIFT;
298 if( GetKeyState(VK_MENU) & 0x8000 )
300 val.i_int |= KEY_MODIFIER_ALT;
303 var_Set( p_event->p_libvlc, "key-pressed", val );
307 case WM_VLC_CHANGE_TEXT:
308 var_Get( p_event->p_vout, "video-title", &val );
309 if( !val.psz_string || !*val.psz_string ) /* Default video title */
311 free( val.psz_string );
313 #ifdef MODULE_NAME_IS_wingdi
314 val.psz_string = strdup( VOUT_TITLE " (WinGDI output)" );
316 #ifdef MODULE_NAME_IS_wingapi
317 val.psz_string = strdup( VOUT_TITLE " (WinGAPI output)" );
319 #ifdef MODULE_NAME_IS_glwin32
320 val.psz_string = strdup( VOUT_TITLE " (OpenGL output)" );
322 #ifdef MODULE_NAME_IS_direct3d
323 val.psz_string = strdup( VOUT_TITLE " (Direct3D output)" );
325 #ifdef MODULE_NAME_IS_vout_directx
326 if( p_event->p_vout->p_sys->b_using_overlay ) val.psz_string =
327 strdup( VOUT_TITLE " (hardware YUV overlay DirectX output)" );
328 else if( p_event->p_vout->p_sys->b_hw_yuv ) val.psz_string =
329 strdup( VOUT_TITLE " (hardware YUV DirectX output)" );
330 else val.psz_string =
331 strdup( VOUT_TITLE " (software RGB DirectX output)" );
337 wchar_t *psz_title = malloc( strlen(val.psz_string) * 2 + 2 );
340 mbstowcs( psz_title, val.psz_string, strlen(val.psz_string)*2);
341 psz_title[strlen(val.psz_string)] = 0;
342 free( val.psz_string ); val.psz_string = (char *)psz_title;
347 SetWindowText( p_event->p_vout->p_sys->hwnd,
348 (LPCTSTR)val.psz_string );
349 if( p_event->p_vout->p_sys->hfswnd )
350 SetWindowText( p_event->p_vout->p_sys->hfswnd,
351 (LPCTSTR)val.psz_string );
352 free( val.psz_string );
356 /* Messages we don't handle directly are dispatched to the
357 * window procedure */
358 TranslateMessage(&msg);
359 DispatchMessage(&msg);
364 } /* End Main loop */
366 /* Check for WM_QUIT if we created the window */
367 if( !p_event->p_vout->p_sys->hparent && msg.message == WM_QUIT )
369 msg_Warn( p_event, "WM_QUIT... should not happen!!" );
370 p_event->p_vout->p_sys->hwnd = NULL; /* Window already destroyed */
373 msg_Dbg( p_event, "DirectXEventThread terminating" );
375 /* clear the changes formerly signaled */
376 p_event->p_vout->p_sys->i_changes = 0;
378 DirectXCloseWindow( p_event->p_vout );
383 /* following functions are local */
385 /*****************************************************************************
386 * DirectXCreateWindow: create a window for the video.
387 *****************************************************************************
388 * Before creating a direct draw surface, we need to create a window in which
389 * the video will be displayed. This window will also allow us to capture the
391 *****************************************************************************/
392 static int DirectXCreateWindow( vout_thread_t *p_vout )
397 WNDCLASS wc; /* window class components */
398 HICON vlc_icon = NULL;
399 char vlc_path[MAX_PATH+1];
400 int i_style, i_stylex;
402 msg_Dbg( p_vout, "DirectXCreateWindow" );
404 /* Get this module's instance */
405 hInstance = GetModuleHandle(NULL);
407 /* If an external window was specified, we'll draw in it. */
408 p_vout->p_sys->hparent =
409 vout_RequestWindow( p_vout, &p_vout->p_sys->i_window_x,
410 &p_vout->p_sys->i_window_y,
411 &p_vout->p_sys->i_window_width,
412 &p_vout->p_sys->i_window_height );
414 /* We create the window ourself, there is no previous window proc. */
415 p_vout->p_sys->pf_wndproc = NULL;
417 /* Get the Icon from the main app */
420 if( GetModuleFileName( NULL, vlc_path, MAX_PATH ) )
422 vlc_icon = ExtractIcon( hInstance, vlc_path, 0 );
426 /* Fill in the window class structure */
427 wc.style = CS_OWNDC|CS_DBLCLKS; /* style: dbl click */
428 wc.lpfnWndProc = (WNDPROC)DirectXEventProc; /* event handler */
429 wc.cbClsExtra = 0; /* no extra class data */
430 wc.cbWndExtra = 0; /* no extra window data */
431 wc.hInstance = hInstance; /* instance */
432 wc.hIcon = vlc_icon; /* load the vlc big icon */
433 wc.hCursor = LoadCursor(NULL, IDC_ARROW); /* default cursor */
434 wc.hbrBackground = GetStockObject(BLACK_BRUSH); /* background color */
435 wc.lpszMenuName = NULL; /* no menu */
436 wc.lpszClassName = _T("VLC DirectX"); /* use a special class */
438 /* Register the window class */
439 if( !RegisterClass(&wc) )
443 if( vlc_icon ) DestroyIcon( vlc_icon );
445 /* Check why it failed. If it's because one already exists
446 * then fine, otherwise return with an error. */
447 if( !GetClassInfo( hInstance, _T("VLC DirectX"), &wndclass ) )
449 msg_Err( p_vout, "DirectXCreateWindow RegisterClass FAILED (err=%lu)", GetLastError() );
454 /* Register the video sub-window class */
455 wc.lpszClassName = _T("VLC DirectX video"); wc.hIcon = 0;
456 wc.hbrBackground = NULL; /* no background color */
457 if( !RegisterClass(&wc) )
461 /* Check why it failed. If it's because one already exists
462 * then fine, otherwise return with an error. */
463 if( !GetClassInfo( hInstance, _T("VLC DirectX video"), &wndclass ) )
465 msg_Err( p_vout, "DirectXCreateWindow RegisterClass FAILED (err=%lu)", GetLastError() );
470 /* When you create a window you give the dimensions you wish it to
471 * have. Unfortunatly these dimensions will include the borders and
472 * titlebar. We use the following function to find out the size of
473 * the window corresponding to the useable surface we want */
474 rect_window.top = 10;
475 rect_window.left = 10;
476 rect_window.right = rect_window.left + p_vout->p_sys->i_window_width;
477 rect_window.bottom = rect_window.top + p_vout->p_sys->i_window_height;
479 if( var_GetBool( p_vout, "video-deco" ) )
481 /* Open with window decoration */
482 AdjustWindowRect( &rect_window, WS_OVERLAPPEDWINDOW|WS_SIZEBOX, 0 );
483 i_style = WS_OVERLAPPEDWINDOW|WS_SIZEBOX|WS_VISIBLE|WS_CLIPCHILDREN;
488 /* No window decoration */
489 AdjustWindowRect( &rect_window, WS_POPUP, 0 );
490 i_style = WS_POPUP|WS_VISIBLE|WS_CLIPCHILDREN;
491 i_stylex = 0; // WS_EX_TOOLWINDOW; Is TOOLWINDOW really needed ?
492 // It messes up the fullscreen window.
495 if( p_vout->p_sys->hparent )
497 i_style = WS_VISIBLE|WS_CLIPCHILDREN|WS_CHILD;
501 p_vout->p_sys->i_window_style = i_style;
503 /* Create the window */
504 p_vout->p_sys->hwnd =
505 CreateWindowEx( WS_EX_NOPARENTNOTIFY | i_stylex,
506 _T("VLC DirectX"), /* name of window class */
507 _T(VOUT_TITLE) _T(" (DirectX Output)"), /* window title */
508 i_style, /* window style */
509 (p_vout->p_sys->i_window_x < 0) ? CW_USEDEFAULT :
510 (UINT)p_vout->p_sys->i_window_x, /* default X coordinate */
511 (p_vout->p_sys->i_window_y < 0) ? CW_USEDEFAULT :
512 (UINT)p_vout->p_sys->i_window_y, /* default Y coordinate */
513 rect_window.right - rect_window.left, /* window width */
514 rect_window.bottom - rect_window.top, /* window height */
515 p_vout->p_sys->hparent, /* parent window */
516 NULL, /* no menu in this window */
517 hInstance, /* handle of this program instance */
518 (LPVOID)p_vout ); /* send p_vout to WM_CREATE */
520 if( !p_vout->p_sys->hwnd )
522 msg_Warn( p_vout, "DirectXCreateWindow create window FAILED (err=%lu)", GetLastError() );
526 if( p_vout->p_sys->hparent )
530 /* We don't want the window owner to overwrite our client area */
531 i_style = GetWindowLong( p_vout->p_sys->hparent, GWL_STYLE );
533 if( !(i_style & WS_CLIPCHILDREN) )
534 /* Hmmm, apparently this is a blocking call... */
535 SetWindowLong( p_vout->p_sys->hparent, GWL_STYLE,
536 i_style | WS_CLIPCHILDREN );
538 /* Create our fullscreen window */
539 p_vout->p_sys->hfswnd =
540 CreateWindowEx( WS_EX_APPWINDOW, _T("VLC DirectX"),
541 _T(VOUT_TITLE) _T(" (DirectX Output)"),
542 WS_OVERLAPPEDWINDOW|WS_CLIPCHILDREN|WS_SIZEBOX,
543 CW_USEDEFAULT, CW_USEDEFAULT,
544 CW_USEDEFAULT, CW_USEDEFAULT,
545 NULL, NULL, hInstance, NULL );
548 /* Append a "Always On Top" entry in the system menu */
549 hMenu = GetSystemMenu( p_vout->p_sys->hwnd, FALSE );
550 AppendMenu( hMenu, MF_SEPARATOR, 0, _T("") );
551 AppendMenu( hMenu, MF_STRING | MF_UNCHECKED,
552 IDM_TOGGLE_ON_TOP, _T("Always on &Top") );
554 /* Create video sub-window. This sub window will always exactly match
555 * the size of the video, which allows us to use crazy overlay colorkeys
556 * without having them shown outside of the video area. */
557 p_vout->p_sys->hvideownd =
558 CreateWindow( _T("VLC DirectX video"), _T(""), /* window class */
559 WS_CHILD, /* window style, not visible initially */
561 p_vout->render.i_width, /* default width */
562 p_vout->render.i_height, /* default height */
563 p_vout->p_sys->hwnd, /* parent window */
565 (LPVOID)p_vout ); /* send p_vout to WM_CREATE */
567 if( !p_vout->p_sys->hvideownd )
568 msg_Warn( p_vout, "can't create video sub-window" );
570 msg_Dbg( p_vout, "created video sub-window" );
572 /* Now display the window */
573 ShowWindow( p_vout->p_sys->hwnd, SW_SHOW );
578 /*****************************************************************************
579 * DirectXCloseWindow: close the window created by DirectXCreateWindow
580 *****************************************************************************
581 * This function returns all resources allocated by DirectXCreateWindow.
582 *****************************************************************************/
583 static void DirectXCloseWindow( vout_thread_t *p_vout )
585 msg_Dbg( p_vout, "DirectXCloseWindow" );
587 DestroyWindow( p_vout->p_sys->hwnd );
588 if( p_vout->p_sys->hfswnd ) DestroyWindow( p_vout->p_sys->hfswnd );
590 if( p_vout->p_sys->hparent )
591 vout_ReleaseWindow( p_vout, (void *)p_vout->p_sys->hparent );
593 p_vout->p_sys->hwnd = NULL;
595 /* We don't unregister the Window Class because it could lead to race
596 * conditions and it will be done anyway by the system when the app will
600 /*****************************************************************************
601 * UpdateRects: update clipping rectangles
602 *****************************************************************************
603 * This function is called when the window position or size are changed, and
604 * its job is to update the source and destination RECTs used to display the
606 *****************************************************************************/
607 void UpdateRects( vout_thread_t *p_vout, bool b_force )
609 #define rect_src p_vout->p_sys->rect_src
610 #define rect_src_clipped p_vout->p_sys->rect_src_clipped
611 #define rect_dest p_vout->p_sys->rect_dest
612 #define rect_dest_clipped p_vout->p_sys->rect_dest_clipped
614 int i_width, i_height, i_x, i_y;
619 /* Retrieve the window size */
620 GetClientRect( p_vout->p_sys->hwnd, &rect );
622 /* Retrieve the window position */
623 point.x = point.y = 0;
624 ClientToScreen( p_vout->p_sys->hwnd, &point );
626 /* If nothing changed, we can return */
628 && p_vout->p_sys->i_window_width == rect.right
629 && p_vout->p_sys->i_window_height == rect.bottom
630 && p_vout->p_sys->i_window_x == point.x
631 && p_vout->p_sys->i_window_y == point.y )
636 /* Update the window position and size */
637 p_vout->p_sys->i_window_x = point.x;
638 p_vout->p_sys->i_window_y = point.y;
639 p_vout->p_sys->i_window_width = rect.right;
640 p_vout->p_sys->i_window_height = rect.bottom;
642 vout_PlacePicture( p_vout, rect.right, rect.bottom,
643 &i_x, &i_y, &i_width, &i_height );
645 if( p_vout->p_sys->hvideownd )
646 SetWindowPos( p_vout->p_sys->hvideownd, 0,
647 i_x, i_y, i_width, i_height,
648 SWP_NOCOPYBITS|SWP_NOZORDER|SWP_ASYNCWINDOWPOS );
650 /* Destination image position and dimensions */
651 rect_dest.left = point.x + i_x;
652 rect_dest.right = rect_dest.left + i_width;
653 rect_dest.top = point.y + i_y;
654 rect_dest.bottom = rect_dest.top + i_height;
656 #ifdef MODULE_NAME_IS_vout_directx
657 /* Apply overlay hardware constraints */
658 if( p_vout->p_sys->b_using_overlay )
660 if( p_vout->p_sys->i_align_dest_boundary )
661 rect_dest.left = ( rect_dest.left +
662 p_vout->p_sys->i_align_dest_boundary / 2 ) &
663 ~p_vout->p_sys->i_align_dest_boundary;
665 if( p_vout->p_sys->i_align_dest_size )
666 rect_dest.right = (( rect_dest.right - rect_dest.left +
667 p_vout->p_sys->i_align_dest_size / 2 ) &
668 ~p_vout->p_sys->i_align_dest_size) + rect_dest.left;
671 /* UpdateOverlay directdraw function doesn't automatically clip to the
672 * display size so we need to do it otherwise it will fail */
674 /* Clip the destination window */
675 if( !IntersectRect( &rect_dest_clipped, &rect_dest,
676 &p_vout->p_sys->rect_display ) )
678 SetRectEmpty( &rect_src_clipped );
683 msg_Dbg( p_vout, "DirectXUpdateRects image_dst_clipped coords:"
685 rect_dest_clipped.left, rect_dest_clipped.top,
686 rect_dest_clipped.right, rect_dest_clipped.bottom );
689 #else /* MODULE_NAME_IS_vout_directx */
691 /* AFAIK, there are no clipping constraints in Direct3D, OpenGL and GDI */
692 rect_dest_clipped = rect_dest;
696 /* the 2 following lines are to fix a bug when clicking on the desktop */
697 if( (rect_dest_clipped.right - rect_dest_clipped.left)==0 ||
698 (rect_dest_clipped.bottom - rect_dest_clipped.top)==0 )
700 SetRectEmpty( &rect_src_clipped );
704 /* src image dimensions */
707 rect_src.right = p_vout->render.i_width;
708 rect_src.bottom = p_vout->render.i_height;
710 /* Clip the source image */
711 rect_src_clipped.left = p_vout->fmt_out.i_x_offset +
712 (rect_dest_clipped.left - rect_dest.left) *
713 p_vout->fmt_out.i_visible_width / (rect_dest.right - rect_dest.left);
714 rect_src_clipped.right = p_vout->fmt_out.i_x_offset +
715 p_vout->fmt_out.i_visible_width -
716 (rect_dest.right - rect_dest_clipped.right) *
717 p_vout->fmt_out.i_visible_width / (rect_dest.right - rect_dest.left);
718 rect_src_clipped.top = p_vout->fmt_out.i_y_offset +
719 (rect_dest_clipped.top - rect_dest.top) *
720 p_vout->fmt_out.i_visible_height / (rect_dest.bottom - rect_dest.top);
721 rect_src_clipped.bottom = p_vout->fmt_out.i_y_offset +
722 p_vout->fmt_out.i_visible_height -
723 (rect_dest.bottom - rect_dest_clipped.bottom) *
724 p_vout->fmt_out.i_visible_height / (rect_dest.bottom - rect_dest.top);
726 #ifdef MODULE_NAME_IS_vout_directx
727 /* Apply overlay hardware constraints */
728 if( p_vout->p_sys->b_using_overlay )
730 if( p_vout->p_sys->i_align_src_boundary )
731 rect_src_clipped.left = ( rect_src_clipped.left +
732 p_vout->p_sys->i_align_src_boundary / 2 ) &
733 ~p_vout->p_sys->i_align_src_boundary;
735 if( p_vout->p_sys->i_align_src_size )
736 rect_src_clipped.right = (( rect_src_clipped.right -
737 rect_src_clipped.left +
738 p_vout->p_sys->i_align_src_size / 2 ) &
739 ~p_vout->p_sys->i_align_src_size) + rect_src_clipped.left;
744 msg_Dbg( p_vout, "DirectXUpdateRects image_src_clipped"
745 " coords: %i,%i,%i,%i",
746 rect_src_clipped.left, rect_src_clipped.top,
747 rect_src_clipped.right, rect_src_clipped.bottom );
750 #ifdef MODULE_NAME_IS_vout_directx
751 /* The destination coordinates need to be relative to the current
752 * directdraw primary surface (display) */
753 rect_dest_clipped.left -= p_vout->p_sys->rect_display.left;
754 rect_dest_clipped.right -= p_vout->p_sys->rect_display.left;
755 rect_dest_clipped.top -= p_vout->p_sys->rect_display.top;
756 rect_dest_clipped.bottom -= p_vout->p_sys->rect_display.top;
758 if( p_vout->p_sys->b_using_overlay )
759 DirectDrawUpdateOverlay( p_vout );
762 /* Signal the change in size/position */
763 p_vout->p_sys->i_changes |= DX_POSITION_CHANGE;
766 #undef rect_src_clipped
768 #undef rect_dest_clipped
771 /*****************************************************************************
772 * DirectXEventProc: This is the window event processing function.
773 *****************************************************************************
774 * On Windows, when you create a window you have to attach an event processing
775 * function to it. The aim of this function is to manage "Queued Messages" and
776 * "Nonqueued Messages".
777 * Queued Messages are those picked up and retransmitted by vout_Manage
778 * (using the GetMessage and DispatchMessage functions).
779 * Nonqueued Messages are those that Windows will send directly to this
780 * procedure (like WM_DESTROY, WM_WINDOWPOSCHANGED...)
781 *****************************************************************************/
782 static long FAR PASCAL DirectXEventProc( HWND hwnd, UINT message,
783 WPARAM wParam, LPARAM lParam )
785 vout_thread_t *p_vout;
787 if( message == WM_CREATE )
789 /* Store p_vout for future use */
790 p_vout = (vout_thread_t *)((CREATESTRUCT *)lParam)->lpCreateParams;
791 SetWindowLongPtr( hwnd, GWLP_USERDATA, (LONG_PTR)p_vout );
796 p_vout = (vout_thread_t *)GetWindowLongPtr( hwnd, GWLP_USERDATA );
799 /* Hmmm mozilla does manage somehow to save the pointer to our
800 * windowproc and still calls it after the vout has been closed. */
801 return DefWindowProc(hwnd, message, wParam, lParam);
806 /* Catch the screensaver and the monitor turn-off */
807 if( message == WM_SYSCOMMAND &&
808 ( (wParam & 0xFFF0) == SC_SCREENSAVE || (wParam & 0xFFF0) == SC_MONITORPOWER ) )
810 //if( p_vout ) msg_Dbg( p_vout, "WinProc WM_SYSCOMMAND screensaver" );
811 return 0; /* this stops them from happening */
815 if( hwnd == p_vout->p_sys->hvideownd )
819 #ifdef MODULE_NAME_IS_vout_directx
821 /* For overlay, we need to erase background */
822 return !p_vout->p_sys->b_using_overlay ?
823 1 : DefWindowProc(hwnd, message, wParam, lParam);
826 ** For overlay, DefWindowProc() will erase dirty regions
828 ** For non-overlay, vout will paint the whole window at
829 ** regular interval, therefore dirty regions can be ignored
830 ** to minimize repaint.
832 if( !p_vout->p_sys->b_using_overlay )
834 ValidateRect(hwnd, NULL);
836 // fall through to default
839 ** For OpenGL and Direct3D, vout will update the whole
840 ** window at regular interval, therefore dirty region
841 ** can be ignored to minimize repaint.
844 /* nothing to erase */
847 /* nothing to repaint */
848 ValidateRect(hwnd, NULL);
852 return DefWindowProc(hwnd, message, wParam, lParam);
859 case WM_WINDOWPOSCHANGED:
860 UpdateRects( p_vout, true );
863 /* the user wants to close the window */
866 playlist_t * p_playlist = pl_Yield( p_vout );
869 playlist_Stop( p_playlist );
870 pl_Release( p_vout );
875 /* the window has been closed so shut down everything now */
877 msg_Dbg( p_vout, "WinProc WM_DESTROY" );
878 /* just destroy the window */
879 PostQuitMessage( 0 );
885 case IDM_TOGGLE_ON_TOP: /* toggle the "on top" status */
888 msg_Dbg( p_vout, "WinProc WM_SYSCOMMAND: IDM_TOGGLE_ON_TOP");
890 /* Change the current value */
891 var_Get( p_vout, "video-on-top", &val );
892 val.b_bool = !val.b_bool;
893 var_Set( p_vout, "video-on-top", val );
902 return DefWindowProc(hwnd, message, wParam, lParam);
905 #ifdef MODULE_NAME_IS_wingapi
906 p_vout->p_sys->b_focus = false;
907 if( !p_vout->p_sys->b_parent_focus ) GXSuspend();
910 if( hWnd == p_vout->p_sys->hfswnd )
912 HWND htbar = FindWindow( _T("HHTaskbar"), NULL );
913 ShowWindow( htbar, SW_SHOW );
916 if( !p_vout->p_sys->hparent ||
917 hWnd == p_vout->p_sys->hfswnd )
919 SHFullScreen( hWnd, SHFS_SHOWSIPBUTTON );
925 #ifdef MODULE_NAME_IS_wingapi
926 p_vout->p_sys->b_focus = true;
930 if( p_vout->p_sys->hparent &&
931 hWnd != p_vout->p_sys->hfswnd && p_vout->b_fullscreen )
932 p_vout->p_sys->i_changes |= VOUT_FULLSCREEN_CHANGE;
934 if( hWnd == p_vout->p_sys->hfswnd )
936 HWND htbar = FindWindow( _T("HHTaskbar"), NULL );
937 ShowWindow( htbar, SW_HIDE );
940 if( !p_vout->p_sys->hparent ||
941 hWnd == p_vout->p_sys->hfswnd )
943 SHFullScreen( hWnd, SHFS_HIDESIPBUTTON );
949 //msg_Dbg( p_vout, "WinProc WM Default %i", message );
953 /* Let windows handle the message */
954 return DefWindowProc(hwnd, message, wParam, lParam);
962 } dxkeys_to_vlckeys[] =
964 { VK_F1, KEY_F1 }, { VK_F2, KEY_F2 }, { VK_F3, KEY_F3 }, { VK_F4, KEY_F4 },
965 { VK_F5, KEY_F5 }, { VK_F6, KEY_F6 }, { VK_F7, KEY_F7 }, { VK_F8, KEY_F8 },
966 { VK_F9, KEY_F9 }, { VK_F10, KEY_F10 }, { VK_F11, KEY_F11 },
969 { VK_RETURN, KEY_ENTER },
970 { VK_SPACE, KEY_SPACE },
971 { VK_ESCAPE, KEY_ESC },
973 { VK_LEFT, KEY_LEFT },
974 { VK_RIGHT, KEY_RIGHT },
976 { VK_DOWN, KEY_DOWN },
978 { VK_HOME, KEY_HOME },
980 { VK_PRIOR, KEY_PAGEUP },
981 { VK_NEXT, KEY_PAGEDOWN },
983 { VK_INSERT, KEY_INSERT },
984 { VK_DELETE, KEY_DELETE },
990 { VK_BROWSER_BACK, KEY_BROWSER_BACK },
991 { VK_BROWSER_FORWARD, KEY_BROWSER_FORWARD },
992 { VK_BROWSER_REFRESH, KEY_BROWSER_REFRESH },
993 { VK_BROWSER_STOP, KEY_BROWSER_STOP },
994 { VK_BROWSER_SEARCH, KEY_BROWSER_SEARCH },
995 { VK_BROWSER_FAVORITES, KEY_BROWSER_FAVORITES },
996 { VK_BROWSER_HOME, KEY_BROWSER_HOME },
997 { VK_VOLUME_MUTE, KEY_VOLUME_MUTE },
998 { VK_VOLUME_DOWN, KEY_VOLUME_DOWN },
999 { VK_VOLUME_UP, KEY_VOLUME_UP },
1000 { VK_MEDIA_NEXT_TRACK, KEY_MEDIA_NEXT_TRACK },
1001 { VK_MEDIA_PREV_TRACK, KEY_MEDIA_PREV_TRACK },
1002 { VK_MEDIA_STOP, KEY_MEDIA_STOP },
1003 { VK_MEDIA_PLAY_PAUSE, KEY_MEDIA_PLAY_PAUSE },
1008 static int DirectXConvertKey( int i_key )
1012 for( i = 0; dxkeys_to_vlckeys[i].i_dxkey != 0; i++ )
1014 if( dxkeys_to_vlckeys[i].i_dxkey == i_key )
1016 return dxkeys_to_vlckeys[i].i_vlckey;
1023 /*****************************************************************************
1024 * Control: control facility for the vout
1025 *****************************************************************************/
1026 static int Control( vout_thread_t *p_vout, int i_query, va_list args )
1028 unsigned int *pi_width, *pi_height;
1035 if( p_vout->p_sys->hparent )
1036 return vout_ControlWindow( p_vout,
1037 (void *)p_vout->p_sys->hparent, i_query, args );
1039 pi_width = va_arg( args, unsigned int * );
1040 pi_height = va_arg( args, unsigned int * );
1042 GetClientRect( p_vout->p_sys->hwnd, &rect_window );
1044 *pi_width = rect_window.right - rect_window.left;
1045 *pi_height = rect_window.bottom - rect_window.top;
1049 if( p_vout->p_sys->hparent )
1050 return vout_ControlWindow( p_vout,
1051 (void *)p_vout->p_sys->hparent, i_query, args );
1053 /* Update dimensions */
1054 rect_window.top = rect_window.left = 0;
1055 rect_window.right = va_arg( args, unsigned int );
1056 rect_window.bottom = va_arg( args, unsigned int );
1057 if( !rect_window.right ) rect_window.right = p_vout->i_window_width;
1058 if( !rect_window.bottom ) rect_window.bottom = p_vout->i_window_height;
1059 AdjustWindowRect( &rect_window, p_vout->p_sys->i_window_style, 0 );
1061 SetWindowPos( p_vout->p_sys->hwnd, 0, 0, 0,
1062 rect_window.right - rect_window.left,
1063 rect_window.bottom - rect_window.top, SWP_NOMOVE );
1068 ShowWindow( p_vout->p_sys->hwnd, SW_HIDE );
1070 /* Retrieve the window position */
1071 point.x = point.y = 0;
1072 ClientToScreen( p_vout->p_sys->hwnd, &point );
1076 if( i_query == VOUT_REPARENT ) d = (HWND)va_arg( args, int );
1079 vlc_mutex_lock( &p_vout->p_sys->lock );
1080 p_vout->p_sys->hparent = 0;
1081 vlc_mutex_unlock( &p_vout->p_sys->lock );
1082 SetParent( p_vout->p_sys->hwnd, 0 );
1083 p_vout->p_sys->i_window_style =
1084 WS_CLIPCHILDREN | WS_OVERLAPPEDWINDOW | WS_SIZEBOX;
1085 SetWindowLong( p_vout->p_sys->hwnd, GWL_STYLE,
1086 p_vout->p_sys->i_window_style |
1087 (i_query == VOUT_CLOSE ? 0 : WS_VISIBLE) );
1088 SetWindowLong( p_vout->p_sys->hwnd, GWL_EXSTYLE, WS_EX_APPWINDOW );
1089 SetWindowPos( p_vout->p_sys->hwnd, 0, point.x, point.y, 0, 0,
1090 SWP_NOSIZE|SWP_NOZORDER|SWP_FRAMECHANGED );
1094 vlc_mutex_lock( &p_vout->p_sys->lock );
1095 p_vout->p_sys->hparent = d;
1096 vlc_mutex_unlock( &p_vout->p_sys->lock );
1098 SetParent( p_vout->p_sys->hwnd, d );
1099 p_vout->p_sys->i_window_style = WS_CLIPCHILDREN;
1100 SetWindowLong( p_vout->p_sys->hwnd, GWL_STYLE,
1101 p_vout->p_sys->i_window_style |
1102 (i_query == VOUT_CLOSE ? 0 : WS_VISIBLE) );
1103 SetWindowLong( p_vout->p_sys->hwnd, GWL_EXSTYLE, WS_EX_APPWINDOW );
1105 /* Retrieve the parent size */
1107 GetClientRect( d, &rect );
1108 SetWindowPos( p_vout->p_sys->hwnd, d, point.x, point.y, rect.right, rect.bottom,
1112 return vout_vaControlDefault( p_vout, i_query, args );
1114 case VOUT_SET_STAY_ON_TOP:
1115 if( p_vout->p_sys->hparent && !var_GetBool( p_vout, "fullscreen" ) )
1116 return vout_ControlWindow( p_vout,
1117 (void *)p_vout->p_sys->hparent, i_query, args );
1119 p_vout->p_sys->b_on_top_change = true;
1122 #ifdef MODULE_NAME_IS_wingapi
1123 case VOUT_SET_FOCUS:
1124 b_bool = (bool) va_arg( args, int );
1125 p_vout->p_sys->b_parent_focus = b_bool;
1126 if( b_bool ) GXResume();
1127 else if( !p_vout->p_sys->b_focus ) GXSuspend();
1132 return vout_vaControlDefault( p_vout, i_query, args );
1137 /* Internal wrapper over GetWindowPlacement / SetWindowPlacement */
1138 static void SetWindowState(HWND hwnd, int nShowCmd)
1140 WINDOWPLACEMENT window_placement;
1141 window_placement.length = sizeof(WINDOWPLACEMENT);
1142 GetWindowPlacement( hwnd, &window_placement );
1143 window_placement.showCmd = nShowCmd;
1144 SetWindowPlacement( hwnd, &window_placement );
1145 SetWindowPos( hwnd, 0, 0, 0, 0, 0,
1146 SWP_NOMOVE|SWP_NOSIZE|SWP_NOZORDER|SWP_FRAMECHANGED);
1149 /* Internal wrapper to call vout_ControlWindow for hparent */
1150 static void ControlParentWindow( vout_thread_t *p_vout, int i_query, ... )
1153 va_start( args, i_query );
1154 vout_ControlWindow( p_vout,
1155 (void *)p_vout->p_sys->hparent, i_query, args );
1159 void Win32ToggleFullscreen( vout_thread_t *p_vout )
1161 HWND hwnd = (p_vout->p_sys->hparent && p_vout->p_sys->hfswnd) ?
1162 p_vout->p_sys->hfswnd : p_vout->p_sys->hwnd;
1164 p_vout->b_fullscreen = ! p_vout->b_fullscreen;
1166 if( p_vout->b_fullscreen )
1168 msg_Dbg( p_vout, "entering fullscreen mode" );
1169 /* Change window style, no borders and no title bar */
1170 int i_style = WS_CLIPCHILDREN | WS_VISIBLE;
1171 SetWindowLong( hwnd, GWL_STYLE, i_style );
1173 if( p_vout->p_sys->hparent )
1175 /* Retrieve current window position so fullscreen will happen
1176 * on the right screen */
1177 POINT point = {0,0};
1179 ClientToScreen( p_vout->p_sys->hwnd, &point );
1180 GetClientRect( p_vout->p_sys->hwnd, &rect );
1181 SetWindowPos( hwnd, 0, point.x, point.y,
1182 rect.right, rect.bottom,
1183 SWP_NOZORDER|SWP_FRAMECHANGED );
1186 /* Maximize window */
1187 SetWindowState( hwnd, SW_SHOWMAXIMIZED );
1189 if( p_vout->p_sys->hparent )
1192 GetClientRect( hwnd, &rect );
1193 SetParent( p_vout->p_sys->hwnd, hwnd );
1194 SetWindowPos( p_vout->p_sys->hwnd, 0, 0, 0,
1195 rect.right, rect.bottom,
1196 SWP_NOZORDER|SWP_FRAMECHANGED );
1198 HWND topLevelParent = GetAncestor( p_vout->p_sys->hparent, GA_ROOT );
1199 ShowWindow( topLevelParent, SW_HIDE );
1202 SetForegroundWindow( hwnd );
1206 msg_Dbg( p_vout, "leaving fullscreen mode" );
1207 /* Change window style, no borders and no title bar */
1208 SetWindowLong( hwnd, GWL_STYLE, p_vout->p_sys->i_window_style );
1211 SetWindowState( hwnd, SW_SHOWNORMAL );
1213 if( p_vout->p_sys->hparent )
1216 GetClientRect( p_vout->p_sys->hparent, &rect );
1217 SetParent( p_vout->p_sys->hwnd, p_vout->p_sys->hparent );
1218 SetWindowPos( p_vout->p_sys->hwnd, 0, 0, 0,
1219 rect.right, rect.bottom,
1220 SWP_NOZORDER|SWP_FRAMECHANGED );
1222 HWND topLevelParent = GetAncestor( p_vout->p_sys->hparent, GA_ROOT );
1223 ShowWindow( topLevelParent, SW_SHOW );
1224 SetForegroundWindow( p_vout->p_sys->hparent );
1225 ShowWindow( hwnd, SW_HIDE );
1228 /* Make sure the mouse cursor is displayed */
1229 PostMessage( p_vout->p_sys->hwnd, WM_VLC_SHOW_MOUSE, 0, 0 );
1233 /* Update the object variable and trigger callback */
1234 val.b_bool = p_vout->b_fullscreen;
1235 var_Set( p_vout, "fullscreen", val );