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 <stdlib.h> /* free() */
31 #include <ctype.h> /* tolower() */
32 #include <string.h> /* strerror() */
35 # define _WIN32_WINNT 0x0500
39 #include <vlc_interface.h>
40 #include <vlc_playlist.h>
47 #ifdef MODULE_NAME_IS_vout_directx
50 #ifdef MODULE_NAME_IS_direct3d
53 #ifdef MODULE_NAME_IS_glwin32
60 /*****************************************************************************
62 *****************************************************************************/
63 static int DirectXCreateWindow( vout_thread_t *p_vout );
64 static void DirectXCloseWindow ( vout_thread_t *p_vout );
65 static long FAR PASCAL DirectXEventProc( HWND, UINT, WPARAM, LPARAM );
67 static int Control( vout_thread_t *p_vout, int i_query, va_list args );
69 static void DirectXPopupMenu( event_thread_t *p_event, vlc_bool_t b_open )
71 playlist_t *p_playlist =
72 vlc_object_find( p_event, VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
73 if( p_playlist != NULL )
77 var_Set( p_playlist, "intf-popupmenu", val );
78 vlc_object_release( p_playlist );
82 static int DirectXConvertKey( int i_key );
84 /*****************************************************************************
85 * DirectXEventThread: Create video window & handle its messages
86 *****************************************************************************
87 * This function creates a video window and then enters an infinite loop
88 * that handles the messages sent to that window.
89 * The main goal of this thread is to isolate the Win32 PeekMessage function
90 * because this one can block for a long time.
91 *****************************************************************************/
92 void E_(DirectXEventThread)( event_thread_t *p_event )
95 POINT old_mouse_pos = {0,0}, mouse_pos;
97 int i_width, i_height, i_x, i_y;
101 p_event->p_vout->pf_control = Control;
103 /* Create a window for the video */
104 /* Creating a window under Windows also initializes the thread's event
106 if( DirectXCreateWindow( p_event->p_vout ) )
108 msg_Err( p_event, "out of memory" );
109 p_event->b_dead = VLC_TRUE;
112 /* Signal the creation of the window */
113 vlc_thread_ready( p_event );
115 /* Set power management stuff */
116 if( (hkernel32 = GetModuleHandle( _T("KERNEL32") ) ) )
118 ULONG (WINAPI* OurSetThreadExecutionState)( ULONG );
120 OurSetThreadExecutionState = (ULONG (WINAPI*)( ULONG ))
121 GetProcAddress( hkernel32, _T("SetThreadExecutionState") );
123 if( OurSetThreadExecutionState )
124 /* Prevent monitor from powering off */
125 OurSetThreadExecutionState( ES_DISPLAY_REQUIRED | ES_CONTINUOUS );
127 msg_Dbg( p_event, "no support for SetThreadExecutionState()" );
131 /* GetMessage will sleep if there's no message in the queue */
132 while( !p_event->b_die && GetMessage( &msg, 0, 0, 0 ) )
134 /* Check if we are asked to exit */
138 switch( msg.message )
142 vout_PlacePicture( p_event->p_vout,
143 p_event->p_vout->p_sys->i_window_width,
144 p_event->p_vout->p_sys->i_window_height,
145 &i_x, &i_y, &i_width, &i_height );
147 if( msg.hwnd == p_event->p_vout->p_sys->hvideownd )
153 if( i_width && i_height )
155 val.i_int = ( GET_X_LPARAM(msg.lParam) - i_x ) *
156 p_event->p_vout->fmt_in.i_visible_width / i_width +
157 p_event->p_vout->fmt_in.i_x_offset;
158 var_Set( p_event->p_vout, "mouse-x", val );
159 val.i_int = ( GET_Y_LPARAM(msg.lParam) - i_y ) *
160 p_event->p_vout->fmt_in.i_visible_height / i_height +
161 p_event->p_vout->fmt_in.i_y_offset;
162 var_Set( p_event->p_vout, "mouse-y", val );
164 val.b_bool = VLC_TRUE;
165 var_Set( p_event->p_vout, "mouse-moved", val );
169 GetCursorPos( &mouse_pos );
170 if( (abs(mouse_pos.x - old_mouse_pos.x) > 2 ||
171 (abs(mouse_pos.y - old_mouse_pos.y)) > 2 ) )
173 GetCursorPos( &old_mouse_pos );
174 p_event->p_vout->p_sys->i_lastmoved = mdate();
176 if( p_event->p_vout->p_sys->b_cursor_hidden )
178 p_event->p_vout->p_sys->b_cursor_hidden = 0;
184 case WM_VLC_HIDE_MOUSE:
185 if( p_event->p_vout->p_sys->b_cursor_hidden ) break;
186 p_event->p_vout->p_sys->b_cursor_hidden = VLC_TRUE;
187 GetCursorPos( &old_mouse_pos );
191 case WM_VLC_SHOW_MOUSE:
192 if( !p_event->p_vout->p_sys->b_cursor_hidden ) break;
193 p_event->p_vout->p_sys->b_cursor_hidden = VLC_FALSE;
194 GetCursorPos( &old_mouse_pos );
199 var_Get( p_event->p_vout, "mouse-button-down", &val );
201 var_Set( p_event->p_vout, "mouse-button-down", val );
202 DirectXPopupMenu( p_event, VLC_FALSE );
206 var_Get( p_event->p_vout, "mouse-button-down", &val );
208 var_Set( p_event->p_vout, "mouse-button-down", val );
210 val.b_bool = VLC_TRUE;
211 var_Set( p_event->p_vout, "mouse-clicked", val );
214 case WM_LBUTTONDBLCLK:
215 p_event->p_vout->p_sys->i_changes |= VOUT_FULLSCREEN_CHANGE;
219 var_Get( p_event->p_vout, "mouse-button-down", &val );
221 var_Set( p_event->p_vout, "mouse-button-down", val );
222 DirectXPopupMenu( p_event, VLC_FALSE );
226 var_Get( p_event->p_vout, "mouse-button-down", &val );
228 var_Set( p_event->p_vout, "mouse-button-down", val );
232 var_Get( p_event->p_vout, "mouse-button-down", &val );
234 var_Set( p_event->p_vout, "mouse-button-down", val );
235 DirectXPopupMenu( p_event, VLC_FALSE );
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_TRUE );
247 /* The key events are first processed here and not translated
248 * into WM_CHAR events because we need to know the status of the
250 val.i_int = DirectXConvertKey( msg.wParam );
253 /* This appears to be a "normal" (ascii) key */
254 val.i_int = tolower( MapVirtualKey( msg.wParam, 2 ) );
259 if( GetKeyState(VK_CONTROL) & 0x8000 )
261 val.i_int |= KEY_MODIFIER_CTRL;
263 if( GetKeyState(VK_SHIFT) & 0x8000 )
265 val.i_int |= KEY_MODIFIER_SHIFT;
267 if( GetKeyState(VK_MENU) & 0x8000 )
269 val.i_int |= KEY_MODIFIER_ALT;
272 var_Set( p_event->p_libvlc, "key-pressed", val );
277 if( GET_WHEEL_DELTA_WPARAM( msg.wParam ) > 0 )
279 val.i_int = KEY_MOUSEWHEELUP;
283 val.i_int = KEY_MOUSEWHEELDOWN;
287 if( GetKeyState(VK_CONTROL) & 0x8000 )
289 val.i_int |= KEY_MODIFIER_CTRL;
291 if( GetKeyState(VK_SHIFT) & 0x8000 )
293 val.i_int |= KEY_MODIFIER_SHIFT;
295 if( GetKeyState(VK_MENU) & 0x8000 )
297 val.i_int |= KEY_MODIFIER_ALT;
300 var_Set( p_event->p_libvlc, "key-pressed", val );
304 case WM_VLC_CHANGE_TEXT:
305 var_Get( p_event->p_vout, "video-title", &val );
306 if( !val.psz_string || !*val.psz_string ) /* Default video title */
308 if( val.psz_string ) free( val.psz_string );
310 #ifdef MODULE_NAME_IS_glwin32
311 val.psz_string = strdup( VOUT_TITLE " (OpenGL output)" );
313 #ifdef MODULE_NAME_IS_direct3d
314 val.psz_string = strdup( VOUT_TITLE " (Direct3D output)" );
316 #ifdef MODULE_NAME_IS_vout_directx
317 if( p_event->p_vout->p_sys->b_using_overlay ) val.psz_string =
318 strdup( VOUT_TITLE " (hardware YUV overlay DirectX output)" );
319 else if( p_event->p_vout->p_sys->b_hw_yuv ) val.psz_string =
320 strdup( VOUT_TITLE " (hardware YUV DirectX output)" );
321 else val.psz_string =
322 strdup( VOUT_TITLE " (software RGB DirectX output)" );
328 wchar_t *psz_title = malloc( strlen(val.psz_string) * 2 + 2 );
329 mbstowcs( psz_title, val.psz_string, strlen(val.psz_string)*2);
330 psz_title[strlen(val.psz_string)] = 0;
331 free( val.psz_string ); val.psz_string = (char *)psz_title;
335 SetWindowText( p_event->p_vout->p_sys->hwnd,
336 (LPCTSTR)val.psz_string );
337 if( p_event->p_vout->p_sys->hfswnd )
338 SetWindowText( p_event->p_vout->p_sys->hfswnd,
339 (LPCTSTR)val.psz_string );
340 free( val.psz_string );
344 /* Messages we don't handle directly are dispatched to the
345 * window procedure */
346 TranslateMessage(&msg);
347 DispatchMessage(&msg);
352 } /* End Main loop */
354 /* Check for WM_QUIT if we created the window */
355 if( !p_event->p_vout->p_sys->hparent && msg.message == WM_QUIT )
357 msg_Warn( p_event, "WM_QUIT... should not happen!!" );
358 p_event->p_vout->p_sys->hwnd = NULL; /* Window already destroyed */
361 msg_Dbg( p_event, "DirectXEventThread terminating" );
363 /* clear the changes formerly signaled */
364 p_event->p_vout->p_sys->i_changes = 0;
366 DirectXCloseWindow( p_event->p_vout );
370 /* following functions are local */
372 /*****************************************************************************
373 * DirectXCreateWindow: create a window for the video.
374 *****************************************************************************
375 * Before creating a direct draw surface, we need to create a window in which
376 * the video will be displayed. This window will also allow us to capture the
378 *****************************************************************************/
379 static int DirectXCreateWindow( vout_thread_t *p_vout )
384 WNDCLASS wc; /* window class components */
385 HICON vlc_icon = NULL;
386 char vlc_path[MAX_PATH+1];
387 int i_style, i_stylex;
389 msg_Dbg( p_vout, "DirectXCreateWindow" );
391 /* Get this module's instance */
392 hInstance = GetModuleHandle(NULL);
394 /* If an external window was specified, we'll draw in it. */
395 p_vout->p_sys->hparent =
396 vout_RequestWindow( p_vout, &p_vout->p_sys->i_window_x,
397 &p_vout->p_sys->i_window_y,
398 &p_vout->p_sys->i_window_width,
399 &p_vout->p_sys->i_window_height );
401 /* We create the window ourself, there is no previous window proc. */
402 p_vout->p_sys->pf_wndproc = NULL;
404 /* Get the Icon from the main app */
407 if( GetModuleFileName( NULL, vlc_path, MAX_PATH ) )
409 vlc_icon = ExtractIcon( hInstance, vlc_path, 0 );
413 /* Fill in the window class structure */
414 wc.style = CS_OWNDC|CS_DBLCLKS; /* style: dbl click */
415 wc.lpfnWndProc = (WNDPROC)DirectXEventProc; /* event handler */
416 wc.cbClsExtra = 0; /* no extra class data */
417 wc.cbWndExtra = 0; /* no extra window data */
418 wc.hInstance = hInstance; /* instance */
419 wc.hIcon = vlc_icon; /* load the vlc big icon */
420 wc.hCursor = LoadCursor(NULL, IDC_ARROW); /* default cursor */
421 wc.hbrBackground = GetStockObject(BLACK_BRUSH); /* background color */
422 wc.lpszMenuName = NULL; /* no menu */
423 wc.lpszClassName = _T("VLC DirectX"); /* use a special class */
425 /* Register the window class */
426 if( !RegisterClass(&wc) )
430 if( vlc_icon ) DestroyIcon( vlc_icon );
432 /* Check why it failed. If it's because one already exists
433 * then fine, otherwise return with an error. */
434 if( !GetClassInfo( hInstance, _T("VLC DirectX"), &wndclass ) )
436 msg_Err( p_vout, "DirectXCreateWindow RegisterClass FAILED (err=%lu)", GetLastError() );
441 /* Register the video sub-window class */
442 wc.lpszClassName = _T("VLC DirectX video"); wc.hIcon = 0;
443 wc.hbrBackground = NULL; /* no background color */
444 if( !RegisterClass(&wc) )
448 /* Check why it failed. If it's because one already exists
449 * then fine, otherwise return with an error. */
450 if( !GetClassInfo( hInstance, _T("VLC DirectX video"), &wndclass ) )
452 msg_Err( p_vout, "DirectXCreateWindow RegisterClass FAILED (err=%lu)", GetLastError() );
457 /* When you create a window you give the dimensions you wish it to
458 * have. Unfortunatly these dimensions will include the borders and
459 * titlebar. We use the following function to find out the size of
460 * the window corresponding to the useable surface we want */
461 rect_window.top = 10;
462 rect_window.left = 10;
463 rect_window.right = rect_window.left + p_vout->p_sys->i_window_width;
464 rect_window.bottom = rect_window.top + p_vout->p_sys->i_window_height;
466 if( var_GetBool( p_vout, "video-deco" ) )
468 /* Open with window decoration */
469 AdjustWindowRect( &rect_window, WS_OVERLAPPEDWINDOW|WS_SIZEBOX, 0 );
470 i_style = WS_OVERLAPPEDWINDOW|WS_SIZEBOX|WS_VISIBLE|WS_CLIPCHILDREN;
475 /* No window decoration */
476 AdjustWindowRect( &rect_window, WS_POPUP, 0 );
477 i_style = WS_POPUP|WS_VISIBLE|WS_CLIPCHILDREN;
478 i_stylex = 0; // WS_EX_TOOLWINDOW; Is TOOLWINDOW really needed ?
479 // It messes up the fullscreen window.
482 if( p_vout->p_sys->hparent )
484 i_style = WS_VISIBLE|WS_CLIPCHILDREN|WS_CHILD;
488 p_vout->p_sys->i_window_style = i_style;
490 /* Create the window */
491 p_vout->p_sys->hwnd =
492 CreateWindowEx( WS_EX_NOPARENTNOTIFY | i_stylex,
493 _T("VLC DirectX"), /* name of window class */
494 _T(VOUT_TITLE) _T(" (DirectX Output)"), /* window title */
495 i_style, /* window style */
496 (p_vout->p_sys->i_window_x < 0) ? CW_USEDEFAULT :
497 (UINT)p_vout->p_sys->i_window_x, /* default X coordinate */
498 (p_vout->p_sys->i_window_y < 0) ? CW_USEDEFAULT :
499 (UINT)p_vout->p_sys->i_window_y, /* default Y coordinate */
500 rect_window.right - rect_window.left, /* window width */
501 rect_window.bottom - rect_window.top, /* window height */
502 p_vout->p_sys->hparent, /* parent window */
503 NULL, /* no menu in this window */
504 hInstance, /* handle of this program instance */
505 (LPVOID)p_vout ); /* send p_vout to WM_CREATE */
507 if( !p_vout->p_sys->hwnd )
509 msg_Warn( p_vout, "DirectXCreateWindow create window FAILED (err=%lu)", GetLastError() );
513 if( p_vout->p_sys->hparent )
517 /* We don't want the window owner to overwrite our client area */
518 i_style = GetWindowLong( p_vout->p_sys->hparent, GWL_STYLE );
520 if( !(i_style & WS_CLIPCHILDREN) )
521 /* Hmmm, apparently this is a blocking call... */
522 SetWindowLong( p_vout->p_sys->hparent, GWL_STYLE,
523 i_style | WS_CLIPCHILDREN );
525 /* Create our fullscreen window */
526 p_vout->p_sys->hfswnd =
527 CreateWindowEx( WS_EX_APPWINDOW, _T("VLC DirectX"),
528 _T(VOUT_TITLE) _T(" (DirectX Output)"),
529 WS_OVERLAPPEDWINDOW|WS_CLIPCHILDREN|WS_SIZEBOX,
530 CW_USEDEFAULT, CW_USEDEFAULT,
531 CW_USEDEFAULT, CW_USEDEFAULT,
532 NULL, NULL, hInstance, NULL );
535 /* Append a "Always On Top" entry in the system menu */
536 hMenu = GetSystemMenu( p_vout->p_sys->hwnd, FALSE );
537 AppendMenu( hMenu, MF_SEPARATOR, 0, _T("") );
538 AppendMenu( hMenu, MF_STRING | MF_UNCHECKED,
539 IDM_TOGGLE_ON_TOP, _T("Always on &Top") );
541 /* Create video sub-window. This sub window will always exactly match
542 * the size of the video, which allows us to use crazy overlay colorkeys
543 * without having them shown outside of the video area. */
544 p_vout->p_sys->hvideownd =
545 CreateWindow( _T("VLC DirectX video"), _T(""), /* window class */
546 WS_CHILD, /* window style, not visible initially */
548 p_vout->render.i_width, /* default width */
549 p_vout->render.i_height, /* default height */
550 p_vout->p_sys->hwnd, /* parent window */
552 (LPVOID)p_vout ); /* send p_vout to WM_CREATE */
554 if( !p_vout->p_sys->hvideownd )
555 msg_Warn( p_vout, "can't create video sub-window" );
557 msg_Dbg( p_vout, "created video sub-window" );
559 /* Now display the window */
560 ShowWindow( p_vout->p_sys->hwnd, SW_SHOW );
565 /*****************************************************************************
566 * DirectXCloseWindow: close the window created by DirectXCreateWindow
567 *****************************************************************************
568 * This function returns all resources allocated by DirectXCreateWindow.
569 *****************************************************************************/
570 static void DirectXCloseWindow( vout_thread_t *p_vout )
572 msg_Dbg( p_vout, "DirectXCloseWindow" );
574 DestroyWindow( p_vout->p_sys->hwnd );
575 if( p_vout->p_sys->hfswnd ) DestroyWindow( p_vout->p_sys->hfswnd );
577 if( p_vout->p_sys->hparent )
578 vout_ReleaseWindow( p_vout, (void *)p_vout->p_sys->hparent );
580 p_vout->p_sys->hwnd = NULL;
582 /* We don't unregister the Window Class because it could lead to race
583 * conditions and it will be done anyway by the system when the app will
587 /*****************************************************************************
588 * DirectXUpdateRects: update clipping rectangles
589 *****************************************************************************
590 * This function is called when the window position or size are changed, and
591 * its job is to update the source and destination RECTs used to display the
593 *****************************************************************************/
594 void E_(DirectXUpdateRects)( vout_thread_t *p_vout, vlc_bool_t b_force )
596 #define rect_src p_vout->p_sys->rect_src
597 #define rect_src_clipped p_vout->p_sys->rect_src_clipped
598 #define rect_dest p_vout->p_sys->rect_dest
599 #define rect_dest_clipped p_vout->p_sys->rect_dest_clipped
601 int i_width, i_height, i_x, i_y;
606 /* Retrieve the window size */
607 GetClientRect( p_vout->p_sys->hwnd, &rect );
609 /* Retrieve the window position */
610 point.x = point.y = 0;
611 ClientToScreen( p_vout->p_sys->hwnd, &point );
613 /* If nothing changed, we can return */
615 && p_vout->p_sys->i_window_width == rect.right
616 && p_vout->p_sys->i_window_height == rect.bottom
617 && p_vout->p_sys->i_window_x == point.x
618 && p_vout->p_sys->i_window_y == point.y )
623 /* Update the window position and size */
624 p_vout->p_sys->i_window_x = point.x;
625 p_vout->p_sys->i_window_y = point.y;
626 p_vout->p_sys->i_window_width = rect.right;
627 p_vout->p_sys->i_window_height = rect.bottom;
629 vout_PlacePicture( p_vout, rect.right, rect.bottom,
630 &i_x, &i_y, &i_width, &i_height );
632 if( p_vout->p_sys->hvideownd )
633 SetWindowPos( p_vout->p_sys->hvideownd, 0,
634 i_x, i_y, i_width, i_height,
635 SWP_NOCOPYBITS|SWP_NOZORDER|SWP_ASYNCWINDOWPOS );
637 /* Destination image position and dimensions */
638 rect_dest.left = point.x + i_x;
639 rect_dest.right = rect_dest.left + i_width;
640 rect_dest.top = point.y + i_y;
641 rect_dest.bottom = rect_dest.top + i_height;
643 #ifdef MODULE_NAME_IS_vout_directx
644 /* Apply overlay hardware constraints */
645 if( p_vout->p_sys->b_using_overlay )
647 if( p_vout->p_sys->i_align_dest_boundary )
648 rect_dest.left = ( rect_dest.left +
649 p_vout->p_sys->i_align_dest_boundary / 2 ) &
650 ~p_vout->p_sys->i_align_dest_boundary;
652 if( p_vout->p_sys->i_align_dest_size )
653 rect_dest.right = (( rect_dest.right - rect_dest.left +
654 p_vout->p_sys->i_align_dest_size / 2 ) &
655 ~p_vout->p_sys->i_align_dest_size) + rect_dest.left;
658 /* UpdateOverlay directdraw function doesn't automatically clip to the
659 * display size so we need to do it otherwise it will fail */
661 /* Clip the destination window */
662 if( !IntersectRect( &rect_dest_clipped, &rect_dest,
663 &p_vout->p_sys->rect_display ) )
665 SetRectEmpty( &rect_src_clipped );
670 msg_Dbg( p_vout, "DirectXUpdateRects image_dst_clipped coords:"
672 rect_dest_clipped.left, rect_dest_clipped.top,
673 rect_dest_clipped.right, rect_dest_clipped.bottom );
676 /* the 2 following lines are to fix a bug when clicking on the desktop */
677 if( (rect_dest_clipped.right - rect_dest_clipped.left)==0 ||
678 (rect_dest_clipped.bottom - rect_dest_clipped.top)==0 )
680 SetRectEmpty( &rect_src_clipped );
683 #else /* MODULE_NAME_IS_vout_directx */
685 /* AFAIK, there are no clipping constraints in Direct3D or OpenGL */
686 rect_dest_clipped = rect_dest;
690 /* src image dimensions */
693 rect_src.right = p_vout->render.i_width;
694 rect_src.bottom = p_vout->render.i_height;
696 /* Clip the source image */
697 rect_src_clipped.left = p_vout->fmt_out.i_x_offset +
698 (rect_dest_clipped.left - rect_dest.left) *
699 p_vout->fmt_out.i_visible_width / (rect_dest.right - rect_dest.left);
700 rect_src_clipped.right = p_vout->fmt_out.i_x_offset +
701 p_vout->fmt_out.i_visible_width -
702 (rect_dest.right - rect_dest_clipped.right) *
703 p_vout->fmt_out.i_visible_width / (rect_dest.right - rect_dest.left);
704 rect_src_clipped.top = p_vout->fmt_out.i_y_offset +
705 (rect_dest_clipped.top - rect_dest.top) *
706 p_vout->fmt_out.i_visible_height / (rect_dest.bottom - rect_dest.top);
707 rect_src_clipped.bottom = p_vout->fmt_out.i_y_offset +
708 p_vout->fmt_out.i_visible_height -
709 (rect_dest.bottom - rect_dest_clipped.bottom) *
710 p_vout->fmt_out.i_visible_height / (rect_dest.bottom - rect_dest.top);
712 #ifdef MODULE_NAME_IS_vout_directx
713 /* Apply overlay hardware constraints */
714 if( p_vout->p_sys->b_using_overlay )
716 if( p_vout->p_sys->i_align_src_boundary )
717 rect_src_clipped.left = ( rect_src_clipped.left +
718 p_vout->p_sys->i_align_src_boundary / 2 ) &
719 ~p_vout->p_sys->i_align_src_boundary;
721 if( p_vout->p_sys->i_align_src_size )
722 rect_src_clipped.right = (( rect_src_clipped.right -
723 rect_src_clipped.left +
724 p_vout->p_sys->i_align_src_size / 2 ) &
725 ~p_vout->p_sys->i_align_src_size) + rect_src_clipped.left;
730 msg_Dbg( p_vout, "DirectXUpdateRects image_src_clipped"
731 " coords: %i,%i,%i,%i",
732 rect_src_clipped.left, rect_src_clipped.top,
733 rect_src_clipped.right, rect_src_clipped.bottom );
736 #ifdef MODULE_NAME_IS_vout_directx
737 /* The destination coordinates need to be relative to the current
738 * directdraw primary surface (display) */
739 rect_dest_clipped.left -= p_vout->p_sys->rect_display.left;
740 rect_dest_clipped.right -= p_vout->p_sys->rect_display.left;
741 rect_dest_clipped.top -= p_vout->p_sys->rect_display.top;
742 rect_dest_clipped.bottom -= p_vout->p_sys->rect_display.top;
744 if( p_vout->p_sys->b_using_overlay )
745 E_(DirectXUpdateOverlay)( p_vout );
748 /* Signal the change in size/position */
749 p_vout->p_sys->i_changes |= DX_POSITION_CHANGE;
752 #undef rect_src_clipped
754 #undef rect_dest_clipped
757 /*****************************************************************************
758 * DirectXEventProc: This is the window event processing function.
759 *****************************************************************************
760 * On Windows, when you create a window you have to attach an event processing
761 * function to it. The aim of this function is to manage "Queued Messages" and
762 * "Nonqueued Messages".
763 * Queued Messages are those picked up and retransmitted by vout_Manage
764 * (using the GetMessage and DispatchMessage functions).
765 * Nonqueued Messages are those that Windows will send directly to this
766 * procedure (like WM_DESTROY, WM_WINDOWPOSCHANGED...)
767 *****************************************************************************/
768 static long FAR PASCAL DirectXEventProc( HWND hwnd, UINT message,
769 WPARAM wParam, LPARAM lParam )
771 vout_thread_t *p_vout;
773 if( message == WM_CREATE )
775 /* Store p_vout for future use */
776 p_vout = (vout_thread_t *)((CREATESTRUCT *)lParam)->lpCreateParams;
777 SetWindowLongPtr( hwnd, GWLP_USERDATA, (LONG_PTR)p_vout );
782 p_vout = (vout_thread_t *)GetWindowLongPtr( hwnd, GWLP_USERDATA );
785 /* Hmmm mozilla does manage somehow to save the pointer to our
786 * windowproc and still calls it after the vout has been closed. */
787 return DefWindowProc(hwnd, message, wParam, lParam);
791 /* Catch the screensaver and the monitor turn-off */
792 if( message == WM_SYSCOMMAND &&
793 ( (wParam & 0xFFF0) == SC_SCREENSAVE || (wParam & 0xFFF0) == SC_MONITORPOWER ) )
795 //if( p_vout ) msg_Dbg( p_vout, "WinProc WM_SYSCOMMAND screensaver" );
796 return 0; /* this stops them from happening */
799 if( hwnd == p_vout->p_sys->hvideownd )
803 #ifdef MODULE_NAME_IS_vout_directx
805 /* For overlay, we need to erase background */
806 return !p_vout->p_sys->b_using_overlay ?
807 1 : DefWindowProc(hwnd, message, wParam, lParam);
810 ** For overlay, DefWindowProc() will erase dirty regions
812 ** For non-overlay, vout will paint the whole window at
813 ** regular interval, therefore dirty regions can be ignored
814 ** to minimize repaint.
816 if( !p_vout->p_sys->b_using_overlay )
818 ValidateRect(hwnd, NULL);
820 // fall through to default
823 ** For OpenGL and Direct3D, vout will update the whole
824 ** window at regular interval, therefore dirty region
825 ** can be ignored to minimize repaint.
828 /* nothing to erase */
831 /* nothing to repaint */
832 ValidateRect(hwnd, NULL);
836 return DefWindowProc(hwnd, message, wParam, lParam);
843 case WM_WINDOWPOSCHANGED:
844 E_(DirectXUpdateRects)( p_vout, VLC_TRUE );
847 /* the user wants to close the window */
850 playlist_t * p_playlist =
851 (playlist_t *)vlc_object_find( p_vout, VLC_OBJECT_PLAYLIST,
853 if( p_playlist == NULL )
858 playlist_Stop( p_playlist );
859 vlc_object_release( p_playlist );
863 /* the window has been closed so shut down everything now */
865 msg_Dbg( p_vout, "WinProc WM_DESTROY" );
866 /* just destroy the window */
867 PostQuitMessage( 0 );
873 case IDM_TOGGLE_ON_TOP: /* toggle the "on top" status */
876 msg_Dbg( p_vout, "WinProc WM_SYSCOMMAND: IDM_TOGGLE_ON_TOP");
878 /* Change the current value */
879 var_Get( p_vout, "video-on-top", &val );
880 val.b_bool = !val.b_bool;
881 var_Set( p_vout, "video-on-top", val );
890 return DefWindowProc(hwnd, message, wParam, lParam);
893 //msg_Dbg( p_vout, "WinProc WM Default %i", message );
897 /* Let windows handle the message */
898 return DefWindowProc(hwnd, message, wParam, lParam);
906 } dxkeys_to_vlckeys[] =
908 { VK_F1, KEY_F1 }, { VK_F2, KEY_F2 }, { VK_F3, KEY_F3 }, { VK_F4, KEY_F4 },
909 { VK_F5, KEY_F5 }, { VK_F6, KEY_F6 }, { VK_F7, KEY_F7 }, { VK_F8, KEY_F8 },
910 { VK_F9, KEY_F9 }, { VK_F10, KEY_F10 }, { VK_F11, KEY_F11 },
913 { VK_RETURN, KEY_ENTER },
914 { VK_SPACE, KEY_SPACE },
915 { VK_ESCAPE, KEY_ESC },
917 { VK_LEFT, KEY_LEFT },
918 { VK_RIGHT, KEY_RIGHT },
920 { VK_DOWN, KEY_DOWN },
922 { VK_HOME, KEY_HOME },
924 { VK_PRIOR, KEY_PAGEUP },
925 { VK_NEXT, KEY_PAGEDOWN },
927 { VK_INSERT, KEY_INSERT },
928 { VK_DELETE, KEY_DELETE },
934 { VK_BROWSER_BACK, KEY_BROWSER_BACK },
935 { VK_BROWSER_FORWARD, KEY_BROWSER_FORWARD },
936 { VK_BROWSER_REFRESH, KEY_BROWSER_REFRESH },
937 { VK_BROWSER_STOP, KEY_BROWSER_STOP },
938 { VK_BROWSER_SEARCH, KEY_BROWSER_SEARCH },
939 { VK_BROWSER_FAVORITES, KEY_BROWSER_FAVORITES },
940 { VK_BROWSER_HOME, KEY_BROWSER_HOME },
941 { VK_VOLUME_MUTE, KEY_VOLUME_MUTE },
942 { VK_VOLUME_DOWN, KEY_VOLUME_DOWN },
943 { VK_VOLUME_UP, KEY_VOLUME_UP },
944 { VK_MEDIA_NEXT_TRACK, KEY_MEDIA_NEXT_TRACK },
945 { VK_MEDIA_PREV_TRACK, KEY_MEDIA_PREV_TRACK },
946 { VK_MEDIA_STOP, KEY_MEDIA_STOP },
947 { VK_MEDIA_PLAY_PAUSE, KEY_MEDIA_PLAY_PAUSE },
952 static int DirectXConvertKey( int i_key )
956 for( i = 0; dxkeys_to_vlckeys[i].i_dxkey != 0; i++ )
958 if( dxkeys_to_vlckeys[i].i_dxkey == i_key )
960 return dxkeys_to_vlckeys[i].i_vlckey;
967 /*****************************************************************************
968 * Control: control facility for the vout
969 *****************************************************************************/
970 static int Control( vout_thread_t *p_vout, int i_query, va_list args )
972 unsigned int *pi_width, *pi_height;
979 if( p_vout->p_sys->hparent )
980 return vout_ControlWindow( p_vout,
981 (void *)p_vout->p_sys->hparent, i_query, args );
983 pi_width = va_arg( args, unsigned int * );
984 pi_height = va_arg( args, unsigned int * );
986 GetClientRect( p_vout->p_sys->hwnd, &rect_window );
988 *pi_width = rect_window.right - rect_window.left;
989 *pi_height = rect_window.bottom - rect_window.top;
993 if( p_vout->p_sys->hparent )
994 return vout_ControlWindow( p_vout,
995 (void *)p_vout->p_sys->hparent, i_query, args );
997 /* Update dimensions */
998 rect_window.top = rect_window.left = 0;
999 rect_window.right = va_arg( args, unsigned int );
1000 rect_window.bottom = va_arg( args, unsigned int );
1001 if( !rect_window.right ) rect_window.right = p_vout->i_window_width;
1002 if( !rect_window.bottom ) rect_window.bottom = p_vout->i_window_height;
1003 AdjustWindowRect( &rect_window, p_vout->p_sys->i_window_style, 0 );
1005 SetWindowPos( p_vout->p_sys->hwnd, 0, 0, 0,
1006 rect_window.right - rect_window.left,
1007 rect_window.bottom - rect_window.top, SWP_NOMOVE );
1012 ShowWindow( p_vout->p_sys->hwnd, SW_HIDE );
1014 /* Retrieve the window position */
1015 point.x = point.y = 0;
1016 ClientToScreen( p_vout->p_sys->hwnd, &point );
1020 if( i_query == VOUT_REPARENT ) d = (HWND)va_arg( args, int );
1023 vlc_mutex_lock( &p_vout->p_sys->lock );
1024 p_vout->p_sys->hparent = 0;
1025 vlc_mutex_unlock( &p_vout->p_sys->lock );
1026 SetParent( p_vout->p_sys->hwnd, 0 );
1027 p_vout->p_sys->i_window_style =
1028 WS_CLIPCHILDREN | WS_OVERLAPPEDWINDOW | WS_SIZEBOX;
1029 SetWindowLong( p_vout->p_sys->hwnd, GWL_STYLE,
1030 p_vout->p_sys->i_window_style |
1031 (i_query == VOUT_CLOSE ? 0 : WS_VISIBLE) );
1032 SetWindowLong( p_vout->p_sys->hwnd, GWL_EXSTYLE, WS_EX_APPWINDOW );
1033 SetWindowPos( p_vout->p_sys->hwnd, 0, point.x, point.y, 0, 0,
1034 SWP_NOSIZE|SWP_NOZORDER|SWP_FRAMECHANGED );
1038 vlc_mutex_lock( &p_vout->p_sys->lock );
1039 p_vout->p_sys->hparent = d;
1040 vlc_mutex_unlock( &p_vout->p_sys->lock );
1042 SetParent( p_vout->p_sys->hwnd, d );
1043 p_vout->p_sys->i_window_style = WS_CLIPCHILDREN;
1044 SetWindowLong( p_vout->p_sys->hwnd, GWL_STYLE,
1045 p_vout->p_sys->i_window_style |
1046 (i_query == VOUT_CLOSE ? 0 : WS_VISIBLE) );
1047 SetWindowLong( p_vout->p_sys->hwnd, GWL_EXSTYLE, WS_EX_APPWINDOW );
1049 /* Retrieve the parent size */
1051 GetClientRect( d, &rect );
1052 SetWindowPos( p_vout->p_sys->hwnd, d, point.x, point.y, rect.right, rect.bottom,
1056 return vout_vaControlDefault( p_vout, i_query, args );
1058 case VOUT_SET_STAY_ON_TOP:
1059 if( p_vout->p_sys->hparent && !var_GetBool( p_vout, "fullscreen" ) )
1060 return vout_ControlWindow( p_vout,
1061 (void *)p_vout->p_sys->hparent, i_query, args );
1063 p_vout->p_sys->b_on_top_change = VLC_TRUE;
1067 return vout_vaControlDefault( p_vout, i_query, args );
1072 /* Internal wrapper over GetWindowPlacement / SetWindowPlacement */
1073 static void SetWindowState(HWND hwnd, int nShowCmd)
1075 WINDOWPLACEMENT window_placement;
1076 window_placement.length = sizeof(WINDOWPLACEMENT);
1077 GetWindowPlacement( hwnd, &window_placement );
1078 window_placement.showCmd = nShowCmd;
1079 SetWindowPlacement( hwnd, &window_placement );
1080 SetWindowPos( hwnd, 0, 0, 0, 0, 0,
1081 SWP_NOMOVE|SWP_NOSIZE|SWP_NOZORDER|SWP_FRAMECHANGED);
1084 /* Internal wrapper to call vout_ControlWindow for hparent */
1085 static void ControlParentWindow( vout_thread_t *p_vout, int i_query, ... )
1088 va_start( args, i_query );
1089 vout_ControlWindow( p_vout,
1090 (void *)p_vout->p_sys->hparent, i_query, args );
1094 void Win32ToggleFullscreen( vout_thread_t *p_vout )
1096 HWND hwnd = (p_vout->p_sys->hparent && p_vout->p_sys->hfswnd) ?
1097 p_vout->p_sys->hfswnd : p_vout->p_sys->hwnd;
1099 p_vout->b_fullscreen = ! p_vout->b_fullscreen;
1101 if( p_vout->b_fullscreen )
1103 msg_Dbg( p_vout, "entering fullscreen mode" );
1104 /* Change window style, no borders and no title bar */
1105 int i_style = WS_CLIPCHILDREN | WS_VISIBLE;
1106 SetWindowLong( hwnd, GWL_STYLE, i_style );
1108 if( p_vout->p_sys->hparent )
1110 /* Retrieve current window position so fullscreen will happen
1111 * on the right screen */
1112 POINT point = {0,0};
1114 ClientToScreen( p_vout->p_sys->hwnd, &point );
1115 GetClientRect( p_vout->p_sys->hwnd, &rect );
1116 SetWindowPos( hwnd, 0, point.x, point.y,
1117 rect.right, rect.bottom,
1118 SWP_NOZORDER|SWP_FRAMECHANGED );
1121 /* Maximize window */
1122 SetWindowState( hwnd, SW_SHOWMAXIMIZED );
1124 if( p_vout->p_sys->hparent )
1127 GetClientRect( hwnd, &rect );
1128 SetParent( p_vout->p_sys->hwnd, hwnd );
1129 SetWindowPos( p_vout->p_sys->hwnd, 0, 0, 0,
1130 rect.right, rect.bottom,
1131 SWP_NOZORDER|SWP_FRAMECHANGED );
1133 HWND topLevelParent = GetAncestor( p_vout->p_sys->hparent, GA_ROOT );
1134 ShowWindow( topLevelParent, SW_HIDE );
1137 SetForegroundWindow( hwnd );
1141 msg_Dbg( p_vout, "leaving fullscreen mode" );
1142 /* Change window style, no borders and no title bar */
1143 SetWindowLong( hwnd, GWL_STYLE, p_vout->p_sys->i_window_style );
1146 SetWindowState( hwnd, SW_SHOWNORMAL );
1148 if( p_vout->p_sys->hparent )
1151 GetClientRect( p_vout->p_sys->hparent, &rect );
1152 SetParent( p_vout->p_sys->hwnd, p_vout->p_sys->hparent );
1153 SetWindowPos( p_vout->p_sys->hwnd, 0, 0, 0,
1154 rect.right, rect.bottom,
1155 SWP_NOZORDER|SWP_FRAMECHANGED );
1157 HWND topLevelParent = GetAncestor( p_vout->p_sys->hparent, GA_ROOT );
1158 ShowWindow( topLevelParent, SW_SHOW );
1159 SetForegroundWindow( p_vout->p_sys->hparent );
1160 ShowWindow( hwnd, SW_HIDE );
1162 /* Update "video-on-top" status for main interface window, it
1163 needs to be updated as we were hiding VOUT_SET_STAY_ON_TOP
1164 queries from it while we were in fullscreen mode */
1165 int b_ontop = var_GetBool( p_vout, "video-on-top" );
1166 ControlParentWindow( p_vout, VOUT_SET_STAY_ON_TOP, b_ontop );
1169 /* Make sure the mouse cursor is displayed */
1170 PostMessage( p_vout->p_sys->hwnd, WM_VLC_SHOW_MOUSE, 0, 0 );
1175 /* Update the object variable and trigger callback */
1176 val.b_bool = p_vout->b_fullscreen;
1177 var_Set( p_vout, "fullscreen", val );