1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 2001-2009 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>
44 #include <vlc_vout_window.h>
51 #ifdef MODULE_NAME_IS_directx
54 #ifdef MODULE_NAME_IS_direct3d
57 #ifdef MODULE_NAME_IS_glwin32
65 #include <vlc_windows_interfaces.h>
70 //WINSHELLAPI BOOL WINAPI SHFullScreen(HWND hwndRequester, DWORD dwState);
73 static int vaControlParentWindow( vout_thread_t *, int, va_list );
76 int CommonInit( vout_thread_t *p_vout )
78 vout_sys_t *p_sys = p_vout->p_sys;
81 p_sys->hvideownd = NULL;
82 p_sys->hparent = NULL;
85 SetRectEmpty( &p_sys->rect_display );
86 SetRectEmpty( &p_sys->rect_parent );
87 vlc_mutex_init( &p_sys->lock );
89 var_Create( p_vout, "video-title", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
91 /* Set main window's size */
92 p_sys->i_window_width = p_vout->i_window_width;
93 p_sys->i_window_height = p_vout->i_window_height;
95 p_sys->p_event = EventThreadCreate( p_vout );
98 if( EventThreadStart( p_sys->p_event ) )
101 /* Variable to indicate if the window should be on top of others */
102 /* Trigger a callback right now */
103 var_TriggerCallback( p_vout, "video-on-top" );
105 /* Why not with glwin32 */
106 #if !defined(UNDER_CE) && !defined(MODULE_NAME_IS_glwin32)
107 var_Create( p_vout, "disable-screensaver", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
108 DisableScreensaver ( p_vout );
115 void CommonClean( vout_thread_t *p_vout )
117 vout_sys_t *p_sys = p_vout->p_sys;
119 ExitFullscreen( p_vout );
122 EventThreadStop( p_sys->p_event );
123 EventThreadDestroy( p_sys->p_event );
126 vlc_mutex_destroy( &p_sys->lock );
128 #if !defined(UNDER_CE) && !defined(MODULE_NAME_IS_glwin32)
129 RestoreScreensaver( p_vout );
133 /*****************************************************************************
134 * UpdateRects: update clipping rectangles
135 *****************************************************************************
136 * This function is called when the window position or size are changed, and
137 * its job is to update the source and destination RECTs used to display the
139 *****************************************************************************/
140 void UpdateRects( vout_thread_t *p_vout, bool b_force )
142 #define rect_src p_vout->p_sys->rect_src
143 #define rect_src_clipped p_vout->p_sys->rect_src_clipped
144 #define rect_dest p_vout->p_sys->rect_dest
145 #define rect_dest_clipped p_vout->p_sys->rect_dest_clipped
147 unsigned int i_width, i_height, i_x, i_y;
152 /* Retrieve the window size */
153 GetClientRect( p_vout->p_sys->hwnd, &rect );
155 /* Retrieve the window position */
156 point.x = point.y = 0;
157 ClientToScreen( p_vout->p_sys->hwnd, &point );
159 /* If nothing changed, we can return */
161 && p_vout->p_sys->i_window_width == rect.right
162 && p_vout->p_sys->i_window_height == rect.bottom
163 && p_vout->p_sys->i_window_x == point.x
164 && p_vout->p_sys->i_window_y == point.y )
169 /* Update the window position and size */
170 p_vout->p_sys->i_window_x = point.x;
171 p_vout->p_sys->i_window_y = point.y;
172 p_vout->p_sys->i_window_width = rect.right;
173 p_vout->p_sys->i_window_height = rect.bottom;
175 vout_PlacePicture( p_vout, rect.right, rect.bottom,
176 &i_x, &i_y, &i_width, &i_height );
178 if( p_vout->p_sys->hvideownd )
179 SetWindowPos( p_vout->p_sys->hvideownd, 0,
180 i_x, i_y, i_width, i_height,
181 SWP_NOCOPYBITS|SWP_NOZORDER|SWP_ASYNCWINDOWPOS );
183 /* Destination image position and dimensions */
184 rect_dest.left = point.x + i_x;
185 rect_dest.right = rect_dest.left + i_width;
186 rect_dest.top = point.y + i_y;
187 rect_dest.bottom = rect_dest.top + i_height;
189 #ifdef MODULE_NAME_IS_directx
190 /* Apply overlay hardware constraints */
191 if( p_vout->p_sys->b_using_overlay )
193 if( p_vout->p_sys->i_align_dest_boundary )
194 rect_dest.left = ( rect_dest.left +
195 p_vout->p_sys->i_align_dest_boundary / 2 ) &
196 ~p_vout->p_sys->i_align_dest_boundary;
198 if( p_vout->p_sys->i_align_dest_size )
199 rect_dest.right = (( rect_dest.right - rect_dest.left +
200 p_vout->p_sys->i_align_dest_size / 2 ) &
201 ~p_vout->p_sys->i_align_dest_size) + rect_dest.left;
204 /* UpdateOverlay directdraw function doesn't automatically clip to the
205 * display size so we need to do it otherwise it will fail */
207 /* Clip the destination window */
208 if( !IntersectRect( &rect_dest_clipped, &rect_dest,
209 &p_vout->p_sys->rect_display ) )
211 SetRectEmpty( &rect_src_clipped );
216 msg_Dbg( p_vout, "DirectXUpdateRects image_dst_clipped coords:"
218 rect_dest_clipped.left, rect_dest_clipped.top,
219 rect_dest_clipped.right, rect_dest_clipped.bottom );
222 #else /* MODULE_NAME_IS_directx */
224 /* AFAIK, there are no clipping constraints in Direct3D, OpenGL and GDI */
225 rect_dest_clipped = rect_dest;
229 /* the 2 following lines are to fix a bug when clicking on the desktop */
230 if( (rect_dest_clipped.right - rect_dest_clipped.left)==0 ||
231 (rect_dest_clipped.bottom - rect_dest_clipped.top)==0 )
233 SetRectEmpty( &rect_src_clipped );
237 /* src image dimensions */
240 rect_src.right = p_vout->render.i_width;
241 rect_src.bottom = p_vout->render.i_height;
243 /* Clip the source image */
244 rect_src_clipped.left = p_vout->fmt_out.i_x_offset +
245 (rect_dest_clipped.left - rect_dest.left) *
246 p_vout->fmt_out.i_visible_width / (rect_dest.right - rect_dest.left);
247 rect_src_clipped.right = p_vout->fmt_out.i_x_offset +
248 p_vout->fmt_out.i_visible_width -
249 (rect_dest.right - rect_dest_clipped.right) *
250 p_vout->fmt_out.i_visible_width / (rect_dest.right - rect_dest.left);
251 rect_src_clipped.top = p_vout->fmt_out.i_y_offset +
252 (rect_dest_clipped.top - rect_dest.top) *
253 p_vout->fmt_out.i_visible_height / (rect_dest.bottom - rect_dest.top);
254 rect_src_clipped.bottom = p_vout->fmt_out.i_y_offset +
255 p_vout->fmt_out.i_visible_height -
256 (rect_dest.bottom - rect_dest_clipped.bottom) *
257 p_vout->fmt_out.i_visible_height / (rect_dest.bottom - rect_dest.top);
259 #ifdef MODULE_NAME_IS_directx
260 /* Apply overlay hardware constraints */
261 if( p_vout->p_sys->b_using_overlay )
263 if( p_vout->p_sys->i_align_src_boundary )
264 rect_src_clipped.left = ( rect_src_clipped.left +
265 p_vout->p_sys->i_align_src_boundary / 2 ) &
266 ~p_vout->p_sys->i_align_src_boundary;
268 if( p_vout->p_sys->i_align_src_size )
269 rect_src_clipped.right = (( rect_src_clipped.right -
270 rect_src_clipped.left +
271 p_vout->p_sys->i_align_src_size / 2 ) &
272 ~p_vout->p_sys->i_align_src_size) + rect_src_clipped.left;
277 msg_Dbg( p_vout, "DirectXUpdateRects image_src_clipped"
278 " coords: %li,%li,%li,%li",
279 rect_src_clipped.left, rect_src_clipped.top,
280 rect_src_clipped.right, rect_src_clipped.bottom );
283 #ifdef MODULE_NAME_IS_directx
284 /* The destination coordinates need to be relative to the current
285 * directdraw primary surface (display) */
286 rect_dest_clipped.left -= p_vout->p_sys->rect_display.left;
287 rect_dest_clipped.right -= p_vout->p_sys->rect_display.left;
288 rect_dest_clipped.top -= p_vout->p_sys->rect_display.top;
289 rect_dest_clipped.bottom -= p_vout->p_sys->rect_display.top;
291 if( p_vout->p_sys->b_using_overlay )
292 DirectDrawUpdateOverlay( p_vout );
296 /* Windows 7 taskbar thumbnail code */
297 LPTASKBARLIST3 p_taskbl;
298 OSVERSIONINFO winVer;
299 winVer.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
300 if( GetVersionEx(&winVer) && winVer.dwMajorVersion > 5 )
304 if( S_OK == CoCreateInstance( &clsid_ITaskbarList,
305 NULL, CLSCTX_INPROC_SERVER,
309 RECT rect_video, rect_parent, rect_relative;
310 HWND hroot = GetAncestor(p_vout->p_sys->hwnd,GA_ROOT);
312 p_taskbl->vt->HrInit(p_taskbl);
313 GetWindowRect(p_vout->p_sys->hvideownd, &rect_video);
314 GetWindowRect(hroot, &rect_parent);
315 rect_relative.left = rect_video.left - rect_parent.left - 8;
316 rect_relative.right = rect_video.right - rect_video.left + rect_relative.left;
317 rect_relative.top = rect_video.top - rect_parent.top - 10;
318 rect_relative.bottom = rect_video.bottom - rect_video.top + rect_relative.top - 25;
320 if (S_OK != p_taskbl->vt->SetThumbnailClip(p_taskbl, hroot, &rect_relative))
321 msg_Err( p_vout, "SetThumbNailClip failed");
323 p_taskbl->vt->Release(p_taskbl);
328 /* Signal the change in size/position */
329 p_vout->p_sys->i_changes |= DX_POSITION_CHANGE;
332 #undef rect_src_clipped
334 #undef rect_dest_clipped
337 /*****************************************************************************
338 * Control: control facility for the vout
339 *****************************************************************************/
340 int Control( vout_thread_t *p_vout, int i_query, va_list args )
347 if( p_vout->p_sys->parent_window )
348 return vaControlParentWindow( p_vout, i_query, args );
350 /* Update dimensions */
351 rect_window.top = rect_window.left = 0;
352 rect_window.right = va_arg( args, unsigned int );
353 rect_window.bottom = va_arg( args, unsigned int );
354 if( !rect_window.right ) rect_window.right = p_vout->i_window_width;
355 if( !rect_window.bottom ) rect_window.bottom = p_vout->i_window_height;
356 AdjustWindowRect( &rect_window, p_vout->p_sys->i_window_style, 0 );
358 SetWindowPos( p_vout->p_sys->hwnd, 0, 0, 0,
359 rect_window.right - rect_window.left,
360 rect_window.bottom - rect_window.top, SWP_NOMOVE );
364 case VOUT_SET_STAY_ON_TOP:
365 if( p_vout->p_sys->hparent && !var_GetBool( p_vout, "fullscreen" ) )
366 return vaControlParentWindow( p_vout, i_query, args );
368 p_vout->p_sys->b_on_top_change = true;
377 /* Internal wrapper over GetWindowPlacement */
378 static WINDOWPLACEMENT getWindowState(HWND hwnd)
380 WINDOWPLACEMENT window_placement;
381 window_placement.length = sizeof(WINDOWPLACEMENT);
382 GetWindowPlacement( hwnd, &window_placement );
383 return window_placement;
386 /* Internal wrapper to call vout_ControlWindow for hparent */
387 static int vaControlParentWindow( vout_thread_t *p_vout, int i_query,
394 const unsigned i_width = va_arg(args, unsigned);
395 const unsigned i_height = va_arg(args, unsigned);
396 return vout_window_SetSize( p_vout->p_sys->parent_window, i_width, i_height );
398 case VOUT_SET_STAY_ON_TOP:
400 const bool is_on_top = va_arg(args, int);
401 return vout_window_SetOnTop( p_vout->p_sys->parent_window, is_on_top );
409 static int ControlParentWindow( vout_thread_t *p_vout, int i_query, ... )
414 va_start( args, i_query );
415 ret = vaControlParentWindow( p_vout, i_query, args );
421 void ExitFullscreen( vout_thread_t *p_vout )
423 if( p_vout->b_fullscreen )
425 msg_Dbg( p_vout, "Quitting fullscreen" );
426 Win32ToggleFullscreen( p_vout );
427 /* Force fullscreen in the core for the next video */
428 var_SetBool( p_vout, "fullscreen", true );
432 void Win32ToggleFullscreen( vout_thread_t *p_vout )
434 HWND hwnd = (p_vout->p_sys->hparent && p_vout->p_sys->hfswnd) ?
435 p_vout->p_sys->hfswnd : p_vout->p_sys->hwnd;
437 /* Save the current windows placement/placement to restore
438 when fullscreen is over */
439 WINDOWPLACEMENT window_placement = getWindowState( hwnd );
441 p_vout->b_fullscreen = ! p_vout->b_fullscreen;
443 /* We want to go to Fullscreen */
444 if( p_vout->b_fullscreen )
446 msg_Dbg( p_vout, "entering fullscreen mode" );
448 /* Change window style, no borders and no title bar */
449 int i_style = WS_CLIPCHILDREN | WS_VISIBLE;
450 SetWindowLong( hwnd, GWL_STYLE, i_style );
452 if( p_vout->p_sys->hparent )
457 ClientToScreen( p_vout->p_sys->hwnd, &point );
458 GetClientRect( p_vout->p_sys->hwnd, &rect );
459 SetWindowPos( hwnd, 0, point.x, point.y,
460 rect.right, rect.bottom,
461 SWP_NOZORDER|SWP_FRAMECHANGED );
463 /* Retrieve current window position so fullscreen will happen
464 *on the right screen */
465 HMONITOR hmon = MonitorFromWindow(p_vout->p_sys->hparent,
466 MONITOR_DEFAULTTONEAREST);
468 if (GetMonitorInfo(hmon, &mi))
469 SetWindowPos( hwnd, 0,
472 mi.rcMonitor.right - mi.rcMonitor.left,
473 mi.rcMonitor.bottom - mi.rcMonitor.top,
474 SWP_NOZORDER|SWP_FRAMECHANGED );
479 /* Maximize non embedded window */
480 ShowWindow( hwnd, SW_SHOWMAXIMIZED );
483 if( p_vout->p_sys->hparent )
485 /* Hide the previous window */
487 GetClientRect( hwnd, &rect );
488 SetParent( p_vout->p_sys->hwnd, hwnd );
489 SetWindowPos( p_vout->p_sys->hwnd, 0, 0, 0,
490 rect.right, rect.bottom,
491 SWP_NOZORDER|SWP_FRAMECHANGED );
494 HWND topLevelParent = GetParent( p_vout->p_sys->hparent );
496 HWND topLevelParent = GetAncestor( p_vout->p_sys->hparent, GA_ROOT );
498 ShowWindow( topLevelParent, SW_HIDE );
501 SetForegroundWindow( hwnd );
505 msg_Dbg( p_vout, "leaving fullscreen mode" );
506 /* Change window style, no borders and no title bar */
507 SetWindowLong( hwnd, GWL_STYLE, p_vout->p_sys->i_window_style );
509 if( p_vout->p_sys->hparent )
512 GetClientRect( p_vout->p_sys->hparent, &rect );
513 SetParent( p_vout->p_sys->hwnd, p_vout->p_sys->hparent );
514 SetWindowPos( p_vout->p_sys->hwnd, 0, 0, 0,
515 rect.right, rect.bottom,
516 SWP_NOZORDER|SWP_FRAMECHANGED );
519 HWND topLevelParent = GetParent( p_vout->p_sys->hparent );
521 HWND topLevelParent = GetAncestor( p_vout->p_sys->hparent, GA_ROOT );
523 ShowWindow( topLevelParent, SW_SHOW );
524 SetForegroundWindow( p_vout->p_sys->hparent );
525 ShowWindow( hwnd, SW_HIDE );
529 /* return to normal window for non embedded vout */
530 SetWindowPlacement( hwnd, &window_placement );
531 ShowWindow( hwnd, SW_SHOWNORMAL );
534 /* Make sure the mouse cursor is displayed */
535 PostMessage( p_vout->p_sys->hwnd, WM_VLC_SHOW_MOUSE, 0, 0 );
538 /* Update the object variable and trigger callback */
539 var_SetBool( p_vout, "fullscreen", p_vout->b_fullscreen );
543 void DisableScreensaver( vout_thread_t *p_vout )
545 /* disable screensaver by temporarily changing system settings */
546 p_vout->p_sys->i_spi_lowpowertimeout = 0;
547 p_vout->p_sys->i_spi_powerofftimeout = 0;
548 p_vout->p_sys->i_spi_screensavetimeout = 0;
549 if( var_GetBool( p_vout, "disable-screensaver" ) )
551 msg_Dbg(p_vout, "disabling screen saver");
552 SystemParametersInfo(SPI_GETLOWPOWERTIMEOUT,
553 0, &(p_vout->p_sys->i_spi_lowpowertimeout), 0);
554 if( 0 != p_vout->p_sys->i_spi_lowpowertimeout ) {
555 SystemParametersInfo(SPI_SETLOWPOWERTIMEOUT, 0, NULL, 0);
557 SystemParametersInfo(SPI_GETPOWEROFFTIMEOUT, 0,
558 &(p_vout->p_sys->i_spi_powerofftimeout), 0);
559 if( 0 != p_vout->p_sys->i_spi_powerofftimeout ) {
560 SystemParametersInfo(SPI_SETPOWEROFFTIMEOUT, 0, NULL, 0);
562 SystemParametersInfo(SPI_GETSCREENSAVETIMEOUT, 0,
563 &(p_vout->p_sys->i_spi_screensavetimeout), 0);
564 if( 0 != p_vout->p_sys->i_spi_screensavetimeout ) {
565 SystemParametersInfo(SPI_SETSCREENSAVETIMEOUT, 0, NULL, 0);
570 void RestoreScreensaver( vout_thread_t *p_vout )
572 /* restore screensaver system settings */
573 if( 0 != p_vout->p_sys->i_spi_lowpowertimeout ) {
574 SystemParametersInfo(SPI_SETLOWPOWERTIMEOUT,
575 p_vout->p_sys->i_spi_lowpowertimeout, NULL, 0);
577 if( 0 != p_vout->p_sys->i_spi_powerofftimeout ) {
578 SystemParametersInfo(SPI_SETPOWEROFFTIMEOUT,
579 p_vout->p_sys->i_spi_powerofftimeout, NULL, 0);
581 if( 0 != p_vout->p_sys->i_spi_screensavetimeout ) {
582 SystemParametersInfo(SPI_SETSCREENSAVETIMEOUT,
583 p_vout->p_sys->i_spi_screensavetimeout, NULL, 0);