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
37 #include <vlc_interface.h>
38 #include <vlc_playlist.h>
45 #ifdef MODULE_NAME_IS_vout_directx
48 #ifdef MODULE_NAME_IS_direct3d
51 #ifdef MODULE_NAME_IS_glwin32
58 #if defined(UNDER_CE) && !defined(__PLUGIN__) /*FIXME*/
59 # define SHFS_SHOWSIPBUTTON 0x0004
60 # define SHFS_HIDESIPBUTTON 0x0008
61 # define MENU_HEIGHT 26
62 BOOL SHFullScreen(HWND hwndRequester, DWORD dwState);
65 /*****************************************************************************
67 *****************************************************************************/
68 static int DirectXCreateWindow( vout_thread_t *p_vout );
69 static void DirectXCloseWindow ( vout_thread_t *p_vout );
70 static long FAR PASCAL DirectXEventProc( HWND, UINT, WPARAM, LPARAM );
72 static int Control( vout_thread_t *p_vout, int i_query, va_list args );
74 static void DirectXPopupMenu( event_thread_t *p_event, vlc_bool_t b_open )
76 playlist_t *p_playlist =
77 vlc_object_find( p_event, VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
78 if( p_playlist != NULL )
82 var_Set( p_playlist, "intf-popupmenu", val );
83 vlc_object_release( p_playlist );
87 static int DirectXConvertKey( int i_key );
89 /*****************************************************************************
90 * EventThread: Create video window & handle its messages
91 *****************************************************************************
92 * This function creates a video window and then enters an infinite loop
93 * that handles the messages sent to that window.
94 * The main goal of this thread is to isolate the Win32 PeekMessage function
95 * because this one can block for a long time.
96 *****************************************************************************/
97 void E_(EventThread)( event_thread_t *p_event )
100 POINT old_mouse_pos = {0,0}, mouse_pos;
102 int i_width, i_height, i_x, i_y;
106 p_event->p_vout->pf_control = Control;
108 /* Create a window for the video */
109 /* Creating a window under Windows also initializes the thread's event
111 if( DirectXCreateWindow( p_event->p_vout ) )
113 msg_Err( p_event, "out of memory" );
114 p_event->b_dead = VLC_TRUE;
117 /* Signal the creation of the window */
118 vlc_thread_ready( p_event );
121 /* Set power management stuff */
122 if( (hkernel32 = GetModuleHandle( _T("KERNEL32") ) ) )
124 ULONG (WINAPI* OurSetThreadExecutionState)( ULONG );
126 OurSetThreadExecutionState = (ULONG (WINAPI*)( ULONG ))
127 GetProcAddress( hkernel32, _T("SetThreadExecutionState") );
129 if( OurSetThreadExecutionState )
130 /* Prevent monitor from powering off */
131 OurSetThreadExecutionState( ES_DISPLAY_REQUIRED | ES_CONTINUOUS );
133 msg_Dbg( p_event, "no support for SetThreadExecutionState()" );
138 /* GetMessage will sleep if there's no message in the queue */
139 while( !p_event->b_die && GetMessage( &msg, 0, 0, 0 ) )
141 /* Check if we are asked to exit */
145 switch( msg.message )
149 vout_PlacePicture( p_event->p_vout,
150 p_event->p_vout->p_sys->i_window_width,
151 p_event->p_vout->p_sys->i_window_height,
152 &i_x, &i_y, &i_width, &i_height );
154 if( msg.hwnd == p_event->p_vout->p_sys->hvideownd )
160 if( i_width && i_height )
162 val.i_int = ( GET_X_LPARAM(msg.lParam) - i_x ) *
163 p_event->p_vout->fmt_in.i_visible_width / i_width +
164 p_event->p_vout->fmt_in.i_x_offset;
165 var_Set( p_event->p_vout, "mouse-x", val );
166 val.i_int = ( GET_Y_LPARAM(msg.lParam) - i_y ) *
167 p_event->p_vout->fmt_in.i_visible_height / i_height +
168 p_event->p_vout->fmt_in.i_y_offset;
169 var_Set( p_event->p_vout, "mouse-y", val );
171 val.b_bool = VLC_TRUE;
172 var_Set( p_event->p_vout, "mouse-moved", val );
176 GetCursorPos( &mouse_pos );
177 if( (abs(mouse_pos.x - old_mouse_pos.x) > 2 ||
178 (abs(mouse_pos.y - old_mouse_pos.y)) > 2 ) )
180 GetCursorPos( &old_mouse_pos );
181 p_event->p_vout->p_sys->i_lastmoved = mdate();
183 if( p_event->p_vout->p_sys->b_cursor_hidden )
185 p_event->p_vout->p_sys->b_cursor_hidden = 0;
191 case WM_VLC_HIDE_MOUSE:
192 if( p_event->p_vout->p_sys->b_cursor_hidden ) break;
193 p_event->p_vout->p_sys->b_cursor_hidden = VLC_TRUE;
194 GetCursorPos( &old_mouse_pos );
198 case WM_VLC_SHOW_MOUSE:
199 if( !p_event->p_vout->p_sys->b_cursor_hidden ) break;
200 p_event->p_vout->p_sys->b_cursor_hidden = VLC_FALSE;
201 GetCursorPos( &old_mouse_pos );
206 var_Get( p_event->p_vout, "mouse-button-down", &val );
208 var_Set( p_event->p_vout, "mouse-button-down", val );
209 DirectXPopupMenu( p_event, VLC_FALSE );
213 var_Get( p_event->p_vout, "mouse-button-down", &val );
215 var_Set( p_event->p_vout, "mouse-button-down", val );
217 val.b_bool = VLC_TRUE;
218 var_Set( p_event->p_vout, "mouse-clicked", val );
221 case WM_LBUTTONDBLCLK:
222 p_event->p_vout->p_sys->i_changes |= VOUT_FULLSCREEN_CHANGE;
226 var_Get( p_event->p_vout, "mouse-button-down", &val );
228 var_Set( p_event->p_vout, "mouse-button-down", val );
229 DirectXPopupMenu( p_event, VLC_FALSE );
233 var_Get( p_event->p_vout, "mouse-button-down", &val );
235 var_Set( p_event->p_vout, "mouse-button-down", val );
239 var_Get( p_event->p_vout, "mouse-button-down", &val );
241 var_Set( p_event->p_vout, "mouse-button-down", val );
242 DirectXPopupMenu( p_event, VLC_FALSE );
246 var_Get( p_event->p_vout, "mouse-button-down", &val );
248 var_Set( p_event->p_vout, "mouse-button-down", val );
249 DirectXPopupMenu( p_event, VLC_TRUE );
254 /* The key events are first processed here and not translated
255 * into WM_CHAR events because we need to know the status of the
257 val.i_int = DirectXConvertKey( msg.wParam );
260 /* This appears to be a "normal" (ascii) key */
261 val.i_int = tolower( MapVirtualKey( msg.wParam, 2 ) );
266 if( GetKeyState(VK_CONTROL) & 0x8000 )
268 val.i_int |= KEY_MODIFIER_CTRL;
270 if( GetKeyState(VK_SHIFT) & 0x8000 )
272 val.i_int |= KEY_MODIFIER_SHIFT;
274 if( GetKeyState(VK_MENU) & 0x8000 )
276 val.i_int |= KEY_MODIFIER_ALT;
279 var_Set( p_event->p_libvlc, "key-pressed", val );
284 if( GET_WHEEL_DELTA_WPARAM( msg.wParam ) > 0 )
286 val.i_int = KEY_MOUSEWHEELUP;
290 val.i_int = KEY_MOUSEWHEELDOWN;
294 if( GetKeyState(VK_CONTROL) & 0x8000 )
296 val.i_int |= KEY_MODIFIER_CTRL;
298 if( GetKeyState(VK_SHIFT) & 0x8000 )
300 val.i_int |= KEY_MODIFIER_SHIFT;
302 if( GetKeyState(VK_MENU) & 0x8000 )
304 val.i_int |= KEY_MODIFIER_ALT;
307 var_Set( p_event->p_libvlc, "key-pressed", val );
311 case WM_VLC_CHANGE_TEXT:
312 var_Get( p_event->p_vout, "video-title", &val );
313 if( !val.psz_string || !*val.psz_string ) /* Default video title */
315 if( val.psz_string ) free( val.psz_string );
317 #ifdef MODULE_NAME_IS_wingdi
318 val.psz_string = strdup( VOUT_TITLE " (WinGDI output)" );
320 #ifdef MODULE_NAME_IS_wingapi
321 val.psz_string = strdup( VOUT_TITLE " (WinGAPI output)" );
323 #ifdef MODULE_NAME_IS_glwin32
324 val.psz_string = strdup( VOUT_TITLE " (OpenGL output)" );
326 #ifdef MODULE_NAME_IS_direct3d
327 val.psz_string = strdup( VOUT_TITLE " (Direct3D output)" );
329 #ifdef MODULE_NAME_IS_vout_directx
330 if( p_event->p_vout->p_sys->b_using_overlay ) val.psz_string =
331 strdup( VOUT_TITLE " (hardware YUV overlay DirectX output)" );
332 else if( p_event->p_vout->p_sys->b_hw_yuv ) val.psz_string =
333 strdup( VOUT_TITLE " (hardware YUV DirectX output)" );
334 else val.psz_string =
335 strdup( VOUT_TITLE " (software RGB DirectX output)" );
341 wchar_t *psz_title = malloc( strlen(val.psz_string) * 2 + 2 );
342 mbstowcs( psz_title, val.psz_string, strlen(val.psz_string)*2);
343 psz_title[strlen(val.psz_string)] = 0;
344 free( val.psz_string ); val.psz_string = (char *)psz_title;
348 SetWindowText( p_event->p_vout->p_sys->hwnd,
349 (LPCTSTR)val.psz_string );
350 if( p_event->p_vout->p_sys->hfswnd )
351 SetWindowText( p_event->p_vout->p_sys->hfswnd,
352 (LPCTSTR)val.psz_string );
353 free( val.psz_string );
357 /* Messages we don't handle directly are dispatched to the
358 * window procedure */
359 TranslateMessage(&msg);
360 DispatchMessage(&msg);
365 } /* End Main loop */
367 /* Check for WM_QUIT if we created the window */
368 if( !p_event->p_vout->p_sys->hparent && msg.message == WM_QUIT )
370 msg_Warn( p_event, "WM_QUIT... should not happen!!" );
371 p_event->p_vout->p_sys->hwnd = NULL; /* Window already destroyed */
374 msg_Dbg( p_event, "DirectXEventThread terminating" );
376 /* clear the changes formerly signaled */
377 p_event->p_vout->p_sys->i_changes = 0;
379 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 E_(UpdateRects)( vout_thread_t *p_vout, vlc_bool_t 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 E_(UpdateRects)( p_vout, VLC_TRUE );
863 /* the user wants to close the window */
866 playlist_t * p_playlist =
867 (playlist_t *)vlc_object_find( p_vout, VLC_OBJECT_PLAYLIST,
869 if( p_playlist == NULL )
874 playlist_Stop( p_playlist );
875 vlc_object_release( p_playlist );
879 /* the window has been closed so shut down everything now */
881 msg_Dbg( p_vout, "WinProc WM_DESTROY" );
882 /* just destroy the window */
883 PostQuitMessage( 0 );
889 case IDM_TOGGLE_ON_TOP: /* toggle the "on top" status */
892 msg_Dbg( p_vout, "WinProc WM_SYSCOMMAND: IDM_TOGGLE_ON_TOP");
894 /* Change the current value */
895 var_Get( p_vout, "video-on-top", &val );
896 val.b_bool = !val.b_bool;
897 var_Set( p_vout, "video-on-top", val );
906 return DefWindowProc(hwnd, message, wParam, lParam);
909 #ifdef MODULE_NAME_IS_wingapi
910 p_vout->p_sys->b_focus = VLC_FALSE;
911 if( !p_vout->p_sys->b_parent_focus ) GXSuspend();
914 if( hWnd == p_vout->p_sys->hfswnd )
916 HWND htbar = FindWindow( _T("HHTaskbar"), NULL );
917 ShowWindow( htbar, SW_SHOW );
920 if( !p_vout->p_sys->hparent ||
921 hWnd == p_vout->p_sys->hfswnd )
923 SHFullScreen( hWnd, SHFS_SHOWSIPBUTTON );
929 #ifdef MODULE_NAME_IS_wingapi
930 p_vout->p_sys->b_focus = VLC_TRUE;
934 if( p_vout->p_sys->hparent &&
935 hWnd != p_vout->p_sys->hfswnd && p_vout->b_fullscreen )
936 p_vout->p_sys->i_changes |= VOUT_FULLSCREEN_CHANGE;
938 if( hWnd == p_vout->p_sys->hfswnd )
940 HWND htbar = FindWindow( _T("HHTaskbar"), NULL );
941 ShowWindow( htbar, SW_HIDE );
944 if( !p_vout->p_sys->hparent ||
945 hWnd == p_vout->p_sys->hfswnd )
947 SHFullScreen( hWnd, SHFS_HIDESIPBUTTON );
953 //msg_Dbg( p_vout, "WinProc WM Default %i", message );
957 /* Let windows handle the message */
958 return DefWindowProc(hwnd, message, wParam, lParam);
966 } dxkeys_to_vlckeys[] =
968 { VK_F1, KEY_F1 }, { VK_F2, KEY_F2 }, { VK_F3, KEY_F3 }, { VK_F4, KEY_F4 },
969 { VK_F5, KEY_F5 }, { VK_F6, KEY_F6 }, { VK_F7, KEY_F7 }, { VK_F8, KEY_F8 },
970 { VK_F9, KEY_F9 }, { VK_F10, KEY_F10 }, { VK_F11, KEY_F11 },
973 { VK_RETURN, KEY_ENTER },
974 { VK_SPACE, KEY_SPACE },
975 { VK_ESCAPE, KEY_ESC },
977 { VK_LEFT, KEY_LEFT },
978 { VK_RIGHT, KEY_RIGHT },
980 { VK_DOWN, KEY_DOWN },
982 { VK_HOME, KEY_HOME },
984 { VK_PRIOR, KEY_PAGEUP },
985 { VK_NEXT, KEY_PAGEDOWN },
987 { VK_INSERT, KEY_INSERT },
988 { VK_DELETE, KEY_DELETE },
994 { VK_BROWSER_BACK, KEY_BROWSER_BACK },
995 { VK_BROWSER_FORWARD, KEY_BROWSER_FORWARD },
996 { VK_BROWSER_REFRESH, KEY_BROWSER_REFRESH },
997 { VK_BROWSER_STOP, KEY_BROWSER_STOP },
998 { VK_BROWSER_SEARCH, KEY_BROWSER_SEARCH },
999 { VK_BROWSER_FAVORITES, KEY_BROWSER_FAVORITES },
1000 { VK_BROWSER_HOME, KEY_BROWSER_HOME },
1001 { VK_VOLUME_MUTE, KEY_VOLUME_MUTE },
1002 { VK_VOLUME_DOWN, KEY_VOLUME_DOWN },
1003 { VK_VOLUME_UP, KEY_VOLUME_UP },
1004 { VK_MEDIA_NEXT_TRACK, KEY_MEDIA_NEXT_TRACK },
1005 { VK_MEDIA_PREV_TRACK, KEY_MEDIA_PREV_TRACK },
1006 { VK_MEDIA_STOP, KEY_MEDIA_STOP },
1007 { VK_MEDIA_PLAY_PAUSE, KEY_MEDIA_PLAY_PAUSE },
1012 static int DirectXConvertKey( int i_key )
1016 for( i = 0; dxkeys_to_vlckeys[i].i_dxkey != 0; i++ )
1018 if( dxkeys_to_vlckeys[i].i_dxkey == i_key )
1020 return dxkeys_to_vlckeys[i].i_vlckey;
1027 /*****************************************************************************
1028 * Control: control facility for the vout
1029 *****************************************************************************/
1030 static int Control( vout_thread_t *p_vout, int i_query, va_list args )
1032 unsigned int *pi_width, *pi_height;
1039 if( p_vout->p_sys->hparent )
1040 return vout_ControlWindow( p_vout,
1041 (void *)p_vout->p_sys->hparent, i_query, args );
1043 pi_width = va_arg( args, unsigned int * );
1044 pi_height = va_arg( args, unsigned int * );
1046 GetClientRect( p_vout->p_sys->hwnd, &rect_window );
1048 *pi_width = rect_window.right - rect_window.left;
1049 *pi_height = rect_window.bottom - rect_window.top;
1053 if( p_vout->p_sys->hparent )
1054 return vout_ControlWindow( p_vout,
1055 (void *)p_vout->p_sys->hparent, i_query, args );
1057 /* Update dimensions */
1058 rect_window.top = rect_window.left = 0;
1059 rect_window.right = va_arg( args, unsigned int );
1060 rect_window.bottom = va_arg( args, unsigned int );
1061 if( !rect_window.right ) rect_window.right = p_vout->i_window_width;
1062 if( !rect_window.bottom ) rect_window.bottom = p_vout->i_window_height;
1063 AdjustWindowRect( &rect_window, p_vout->p_sys->i_window_style, 0 );
1065 SetWindowPos( p_vout->p_sys->hwnd, 0, 0, 0,
1066 rect_window.right - rect_window.left,
1067 rect_window.bottom - rect_window.top, SWP_NOMOVE );
1072 ShowWindow( p_vout->p_sys->hwnd, SW_HIDE );
1074 /* Retrieve the window position */
1075 point.x = point.y = 0;
1076 ClientToScreen( p_vout->p_sys->hwnd, &point );
1080 if( i_query == VOUT_REPARENT ) d = (HWND)va_arg( args, int );
1083 vlc_mutex_lock( &p_vout->p_sys->lock );
1084 p_vout->p_sys->hparent = 0;
1085 vlc_mutex_unlock( &p_vout->p_sys->lock );
1086 SetParent( p_vout->p_sys->hwnd, 0 );
1087 p_vout->p_sys->i_window_style =
1088 WS_CLIPCHILDREN | WS_OVERLAPPEDWINDOW | WS_SIZEBOX;
1089 SetWindowLong( p_vout->p_sys->hwnd, GWL_STYLE,
1090 p_vout->p_sys->i_window_style |
1091 (i_query == VOUT_CLOSE ? 0 : WS_VISIBLE) );
1092 SetWindowLong( p_vout->p_sys->hwnd, GWL_EXSTYLE, WS_EX_APPWINDOW );
1093 SetWindowPos( p_vout->p_sys->hwnd, 0, point.x, point.y, 0, 0,
1094 SWP_NOSIZE|SWP_NOZORDER|SWP_FRAMECHANGED );
1098 vlc_mutex_lock( &p_vout->p_sys->lock );
1099 p_vout->p_sys->hparent = d;
1100 vlc_mutex_unlock( &p_vout->p_sys->lock );
1102 SetParent( p_vout->p_sys->hwnd, d );
1103 p_vout->p_sys->i_window_style = WS_CLIPCHILDREN;
1104 SetWindowLong( p_vout->p_sys->hwnd, GWL_STYLE,
1105 p_vout->p_sys->i_window_style |
1106 (i_query == VOUT_CLOSE ? 0 : WS_VISIBLE) );
1107 SetWindowLong( p_vout->p_sys->hwnd, GWL_EXSTYLE, WS_EX_APPWINDOW );
1109 /* Retrieve the parent size */
1111 GetClientRect( d, &rect );
1112 SetWindowPos( p_vout->p_sys->hwnd, d, point.x, point.y, rect.right, rect.bottom,
1116 return vout_vaControlDefault( p_vout, i_query, args );
1118 case VOUT_SET_STAY_ON_TOP:
1119 if( p_vout->p_sys->hparent && !var_GetBool( p_vout, "fullscreen" ) )
1120 return vout_ControlWindow( p_vout,
1121 (void *)p_vout->p_sys->hparent, i_query, args );
1123 p_vout->p_sys->b_on_top_change = VLC_TRUE;
1126 #ifdef MODULE_NAME_IS_wingapi
1127 case VOUT_SET_FOCUS:
1128 b_bool = va_arg( args, vlc_bool_t );
1129 p_vout->p_sys->b_parent_focus = b_bool;
1130 if( b_bool ) GXResume();
1131 else if( !p_vout->p_sys->b_focus ) GXSuspend();
1136 return vout_vaControlDefault( p_vout, i_query, args );
1141 /* Internal wrapper over GetWindowPlacement / SetWindowPlacement */
1142 static void SetWindowState(HWND hwnd, int nShowCmd)
1144 WINDOWPLACEMENT window_placement;
1145 window_placement.length = sizeof(WINDOWPLACEMENT);
1146 GetWindowPlacement( hwnd, &window_placement );
1147 window_placement.showCmd = nShowCmd;
1148 SetWindowPlacement( hwnd, &window_placement );
1149 SetWindowPos( hwnd, 0, 0, 0, 0, 0,
1150 SWP_NOMOVE|SWP_NOSIZE|SWP_NOZORDER|SWP_FRAMECHANGED);
1153 /* Internal wrapper to call vout_ControlWindow for hparent */
1154 static void ControlParentWindow( vout_thread_t *p_vout, int i_query, ... )
1157 va_start( args, i_query );
1158 vout_ControlWindow( p_vout,
1159 (void *)p_vout->p_sys->hparent, i_query, args );
1163 void Win32ToggleFullscreen( vout_thread_t *p_vout )
1165 HWND hwnd = (p_vout->p_sys->hparent && p_vout->p_sys->hfswnd) ?
1166 p_vout->p_sys->hfswnd : p_vout->p_sys->hwnd;
1168 p_vout->b_fullscreen = ! p_vout->b_fullscreen;
1170 if( p_vout->b_fullscreen )
1172 msg_Dbg( p_vout, "entering fullscreen mode" );
1173 /* Change window style, no borders and no title bar */
1174 int i_style = WS_CLIPCHILDREN | WS_VISIBLE;
1175 SetWindowLong( hwnd, GWL_STYLE, i_style );
1177 if( p_vout->p_sys->hparent )
1179 /* Retrieve current window position so fullscreen will happen
1180 * on the right screen */
1181 POINT point = {0,0};
1183 ClientToScreen( p_vout->p_sys->hwnd, &point );
1184 GetClientRect( p_vout->p_sys->hwnd, &rect );
1185 SetWindowPos( hwnd, 0, point.x, point.y,
1186 rect.right, rect.bottom,
1187 SWP_NOZORDER|SWP_FRAMECHANGED );
1190 /* Maximize window */
1191 SetWindowState( hwnd, SW_SHOWMAXIMIZED );
1193 if( p_vout->p_sys->hparent )
1196 GetClientRect( hwnd, &rect );
1197 SetParent( p_vout->p_sys->hwnd, hwnd );
1198 SetWindowPos( p_vout->p_sys->hwnd, 0, 0, 0,
1199 rect.right, rect.bottom,
1200 SWP_NOZORDER|SWP_FRAMECHANGED );
1202 HWND topLevelParent = GetAncestor( p_vout->p_sys->hparent, GA_ROOT );
1203 ShowWindow( topLevelParent, SW_HIDE );
1206 SetForegroundWindow( hwnd );
1210 msg_Dbg( p_vout, "leaving fullscreen mode" );
1211 /* Change window style, no borders and no title bar */
1212 SetWindowLong( hwnd, GWL_STYLE, p_vout->p_sys->i_window_style );
1215 SetWindowState( hwnd, SW_SHOWNORMAL );
1217 if( p_vout->p_sys->hparent )
1220 GetClientRect( p_vout->p_sys->hparent, &rect );
1221 SetParent( p_vout->p_sys->hwnd, p_vout->p_sys->hparent );
1222 SetWindowPos( p_vout->p_sys->hwnd, 0, 0, 0,
1223 rect.right, rect.bottom,
1224 SWP_NOZORDER|SWP_FRAMECHANGED );
1226 HWND topLevelParent = GetAncestor( p_vout->p_sys->hparent, GA_ROOT );
1227 ShowWindow( topLevelParent, SW_SHOW );
1228 SetForegroundWindow( p_vout->p_sys->hparent );
1229 ShowWindow( hwnd, SW_HIDE );
1231 /* Update "video-on-top" status for main interface window, it
1232 needs to be updated as we were hiding VOUT_SET_STAY_ON_TOP
1233 queries from it while we were in fullscreen mode */
1234 int b_ontop = var_GetBool( p_vout, "video-on-top" );
1235 ControlParentWindow( p_vout, VOUT_SET_STAY_ON_TOP, b_ontop );
1238 /* Make sure the mouse cursor is displayed */
1239 PostMessage( p_vout->p_sys->hwnd, WM_VLC_SHOW_MOUSE, 0, 0 );
1244 /* Update the object variable and trigger callback */
1245 val.b_bool = p_vout->b_fullscreen;
1246 var_Set( p_vout, "fullscreen", val );