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 p_sys->b_cursor_hidden = 0;
90 p_sys->i_lastmoved = mdate();
91 p_sys->i_mouse_hide_timeout =
92 var_GetInteger(p_vout, "mouse-hide-timeout") * 1000;
94 var_Create( p_vout, "video-title", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
96 /* Set main window's size */
97 p_sys->i_window_width = p_vout->i_window_width;
98 p_sys->i_window_height = p_vout->i_window_height;
100 p_sys->p_event = EventThreadCreate( p_vout );
101 if( !p_sys->p_event )
103 if( EventThreadStart( p_sys->p_event ) )
106 /* Variable to indicate if the window should be on top of others */
107 /* Trigger a callback right now */
108 var_TriggerCallback( p_vout, "video-on-top" );
110 /* Why not with glwin32 */
111 #if !defined(UNDER_CE) && !defined(MODULE_NAME_IS_glwin32)
112 var_Create( p_vout, "disable-screensaver", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
113 DisableScreensaver ( p_vout );
120 void CommonClean( vout_thread_t *p_vout )
122 vout_sys_t *p_sys = p_vout->p_sys;
124 ExitFullscreen( p_vout );
127 EventThreadStop( p_sys->p_event );
128 EventThreadDestroy( p_sys->p_event );
131 vlc_mutex_destroy( &p_sys->lock );
133 #if !defined(UNDER_CE) && !defined(MODULE_NAME_IS_glwin32)
134 RestoreScreensaver( p_vout );
138 /*****************************************************************************
139 * UpdateRects: update clipping rectangles
140 *****************************************************************************
141 * This function is called when the window position or size are changed, and
142 * its job is to update the source and destination RECTs used to display the
144 *****************************************************************************/
145 void UpdateRects( vout_thread_t *p_vout, bool b_force )
147 #define rect_src p_vout->p_sys->rect_src
148 #define rect_src_clipped p_vout->p_sys->rect_src_clipped
149 #define rect_dest p_vout->p_sys->rect_dest
150 #define rect_dest_clipped p_vout->p_sys->rect_dest_clipped
152 unsigned int i_width, i_height, i_x, i_y;
157 /* Retrieve the window size */
158 GetClientRect( p_vout->p_sys->hwnd, &rect );
160 /* Retrieve the window position */
161 point.x = point.y = 0;
162 ClientToScreen( p_vout->p_sys->hwnd, &point );
164 /* If nothing changed, we can return */
166 && p_vout->p_sys->i_window_width == rect.right
167 && p_vout->p_sys->i_window_height == rect.bottom
168 && p_vout->p_sys->i_window_x == point.x
169 && p_vout->p_sys->i_window_y == point.y )
174 /* Update the window position and size */
175 p_vout->p_sys->i_window_x = point.x;
176 p_vout->p_sys->i_window_y = point.y;
177 p_vout->p_sys->i_window_width = rect.right;
178 p_vout->p_sys->i_window_height = rect.bottom;
180 vout_PlacePicture( p_vout, rect.right, rect.bottom,
181 &i_x, &i_y, &i_width, &i_height );
183 if( p_vout->p_sys->hvideownd )
184 SetWindowPos( p_vout->p_sys->hvideownd, 0,
185 i_x, i_y, i_width, i_height,
186 SWP_NOCOPYBITS|SWP_NOZORDER|SWP_ASYNCWINDOWPOS );
188 /* Destination image position and dimensions */
189 rect_dest.left = point.x + i_x;
190 rect_dest.right = rect_dest.left + i_width;
191 rect_dest.top = point.y + i_y;
192 rect_dest.bottom = rect_dest.top + i_height;
194 #ifdef MODULE_NAME_IS_directx
195 /* Apply overlay hardware constraints */
196 if( p_vout->p_sys->b_using_overlay )
198 if( p_vout->p_sys->i_align_dest_boundary )
199 rect_dest.left = ( rect_dest.left +
200 p_vout->p_sys->i_align_dest_boundary / 2 ) &
201 ~p_vout->p_sys->i_align_dest_boundary;
203 if( p_vout->p_sys->i_align_dest_size )
204 rect_dest.right = (( rect_dest.right - rect_dest.left +
205 p_vout->p_sys->i_align_dest_size / 2 ) &
206 ~p_vout->p_sys->i_align_dest_size) + rect_dest.left;
209 /* UpdateOverlay directdraw function doesn't automatically clip to the
210 * display size so we need to do it otherwise it will fail */
212 /* Clip the destination window */
213 if( !IntersectRect( &rect_dest_clipped, &rect_dest,
214 &p_vout->p_sys->rect_display ) )
216 SetRectEmpty( &rect_src_clipped );
221 msg_Dbg( p_vout, "DirectXUpdateRects image_dst_clipped coords:"
223 rect_dest_clipped.left, rect_dest_clipped.top,
224 rect_dest_clipped.right, rect_dest_clipped.bottom );
227 #else /* MODULE_NAME_IS_directx */
229 /* AFAIK, there are no clipping constraints in Direct3D, OpenGL and GDI */
230 rect_dest_clipped = rect_dest;
234 /* the 2 following lines are to fix a bug when clicking on the desktop */
235 if( (rect_dest_clipped.right - rect_dest_clipped.left)==0 ||
236 (rect_dest_clipped.bottom - rect_dest_clipped.top)==0 )
238 SetRectEmpty( &rect_src_clipped );
242 /* src image dimensions */
245 rect_src.right = p_vout->render.i_width;
246 rect_src.bottom = p_vout->render.i_height;
248 /* Clip the source image */
249 rect_src_clipped.left = p_vout->fmt_out.i_x_offset +
250 (rect_dest_clipped.left - rect_dest.left) *
251 p_vout->fmt_out.i_visible_width / (rect_dest.right - rect_dest.left);
252 rect_src_clipped.right = p_vout->fmt_out.i_x_offset +
253 p_vout->fmt_out.i_visible_width -
254 (rect_dest.right - rect_dest_clipped.right) *
255 p_vout->fmt_out.i_visible_width / (rect_dest.right - rect_dest.left);
256 rect_src_clipped.top = p_vout->fmt_out.i_y_offset +
257 (rect_dest_clipped.top - rect_dest.top) *
258 p_vout->fmt_out.i_visible_height / (rect_dest.bottom - rect_dest.top);
259 rect_src_clipped.bottom = p_vout->fmt_out.i_y_offset +
260 p_vout->fmt_out.i_visible_height -
261 (rect_dest.bottom - rect_dest_clipped.bottom) *
262 p_vout->fmt_out.i_visible_height / (rect_dest.bottom - rect_dest.top);
264 #ifdef MODULE_NAME_IS_directx
265 /* Apply overlay hardware constraints */
266 if( p_vout->p_sys->b_using_overlay )
268 if( p_vout->p_sys->i_align_src_boundary )
269 rect_src_clipped.left = ( rect_src_clipped.left +
270 p_vout->p_sys->i_align_src_boundary / 2 ) &
271 ~p_vout->p_sys->i_align_src_boundary;
273 if( p_vout->p_sys->i_align_src_size )
274 rect_src_clipped.right = (( rect_src_clipped.right -
275 rect_src_clipped.left +
276 p_vout->p_sys->i_align_src_size / 2 ) &
277 ~p_vout->p_sys->i_align_src_size) + rect_src_clipped.left;
282 msg_Dbg( p_vout, "DirectXUpdateRects image_src_clipped"
283 " coords: %li,%li,%li,%li",
284 rect_src_clipped.left, rect_src_clipped.top,
285 rect_src_clipped.right, rect_src_clipped.bottom );
288 #ifdef MODULE_NAME_IS_directx
289 /* The destination coordinates need to be relative to the current
290 * directdraw primary surface (display) */
291 rect_dest_clipped.left -= p_vout->p_sys->rect_display.left;
292 rect_dest_clipped.right -= p_vout->p_sys->rect_display.left;
293 rect_dest_clipped.top -= p_vout->p_sys->rect_display.top;
294 rect_dest_clipped.bottom -= p_vout->p_sys->rect_display.top;
296 if( p_vout->p_sys->b_using_overlay )
297 DirectDrawUpdateOverlay( p_vout );
301 /* Windows 7 taskbar thumbnail code */
302 LPTASKBARLIST3 p_taskbl;
303 OSVERSIONINFO winVer;
304 winVer.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
305 if( GetVersionEx(&winVer) && winVer.dwMajorVersion > 5 )
309 if( S_OK == CoCreateInstance( &clsid_ITaskbarList,
310 NULL, CLSCTX_INPROC_SERVER,
314 RECT rect_video, rect_parent, rect_relative;
315 HWND hroot = GetAncestor(p_vout->p_sys->hwnd,GA_ROOT);
317 p_taskbl->vt->HrInit(p_taskbl);
318 GetWindowRect(p_vout->p_sys->hvideownd, &rect_video);
319 GetWindowRect(hroot, &rect_parent);
320 rect_relative.left = rect_video.left - rect_parent.left - 8;
321 rect_relative.right = rect_video.right - rect_video.left + rect_relative.left;
322 rect_relative.top = rect_video.top - rect_parent.top - 10;
323 rect_relative.bottom = rect_video.bottom - rect_video.top + rect_relative.top - 25;
325 if (S_OK != p_taskbl->vt->SetThumbnailClip(p_taskbl, hroot, &rect_relative))
326 msg_Err( p_vout, "SetThumbNailClip failed");
328 p_taskbl->vt->Release(p_taskbl);
333 /* Signal the change in size/position */
334 p_vout->p_sys->i_changes |= DX_POSITION_CHANGE;
337 #undef rect_src_clipped
339 #undef rect_dest_clipped
342 /*****************************************************************************
343 * Control: control facility for the vout
344 *****************************************************************************/
345 int Control( vout_thread_t *p_vout, int i_query, va_list args )
352 if( p_vout->p_sys->parent_window )
353 return vaControlParentWindow( p_vout, i_query, args );
355 /* Update dimensions */
356 rect_window.top = rect_window.left = 0;
357 rect_window.right = va_arg( args, unsigned int );
358 rect_window.bottom = va_arg( args, unsigned int );
359 if( !rect_window.right ) rect_window.right = p_vout->i_window_width;
360 if( !rect_window.bottom ) rect_window.bottom = p_vout->i_window_height;
361 AdjustWindowRect( &rect_window, p_vout->p_sys->i_window_style, 0 );
363 SetWindowPos( p_vout->p_sys->hwnd, 0, 0, 0,
364 rect_window.right - rect_window.left,
365 rect_window.bottom - rect_window.top, SWP_NOMOVE );
369 case VOUT_SET_STAY_ON_TOP:
370 if( p_vout->p_sys->hparent && !var_GetBool( p_vout, "fullscreen" ) )
371 return vaControlParentWindow( p_vout, i_query, args );
373 p_vout->p_sys->b_on_top_change = true;
382 /* Internal wrapper over GetWindowPlacement */
383 static WINDOWPLACEMENT getWindowState(HWND hwnd)
385 WINDOWPLACEMENT window_placement;
386 window_placement.length = sizeof(WINDOWPLACEMENT);
387 GetWindowPlacement( hwnd, &window_placement );
388 return window_placement;
391 /* Internal wrapper to call vout_ControlWindow for hparent */
392 static int vaControlParentWindow( vout_thread_t *p_vout, int i_query,
399 const unsigned i_width = va_arg(args, unsigned);
400 const unsigned i_height = va_arg(args, unsigned);
401 return vout_window_SetSize( p_vout->p_sys->parent_window, i_width, i_height );
403 case VOUT_SET_STAY_ON_TOP:
405 const bool is_on_top = va_arg(args, int);
406 return vout_window_SetOnTop( p_vout->p_sys->parent_window, is_on_top );
414 static int ControlParentWindow( vout_thread_t *p_vout, int i_query, ... )
419 va_start( args, i_query );
420 ret = vaControlParentWindow( p_vout, i_query, args );
426 void ExitFullscreen( vout_thread_t *p_vout )
428 if( p_vout->b_fullscreen )
430 msg_Dbg( p_vout, "Quitting fullscreen" );
431 Win32ToggleFullscreen( p_vout );
432 /* Force fullscreen in the core for the next video */
433 var_SetBool( p_vout, "fullscreen", true );
437 void Win32ToggleFullscreen( vout_thread_t *p_vout )
439 HWND hwnd = (p_vout->p_sys->hparent && p_vout->p_sys->hfswnd) ?
440 p_vout->p_sys->hfswnd : p_vout->p_sys->hwnd;
442 /* Save the current windows placement/placement to restore
443 when fullscreen is over */
444 WINDOWPLACEMENT window_placement = getWindowState( hwnd );
446 p_vout->b_fullscreen = ! p_vout->b_fullscreen;
448 /* We want to go to Fullscreen */
449 if( p_vout->b_fullscreen )
451 msg_Dbg( p_vout, "entering fullscreen mode" );
453 /* Change window style, no borders and no title bar */
454 int i_style = WS_CLIPCHILDREN | WS_VISIBLE;
455 SetWindowLong( hwnd, GWL_STYLE, i_style );
457 if( p_vout->p_sys->hparent )
462 ClientToScreen( p_vout->p_sys->hwnd, &point );
463 GetClientRect( p_vout->p_sys->hwnd, &rect );
464 SetWindowPos( hwnd, 0, point.x, point.y,
465 rect.right, rect.bottom,
466 SWP_NOZORDER|SWP_FRAMECHANGED );
468 /* Retrieve current window position so fullscreen will happen
469 *on the right screen */
470 HMONITOR hmon = MonitorFromWindow(p_vout->p_sys->hparent,
471 MONITOR_DEFAULTTONEAREST);
473 if (GetMonitorInfo(hmon, &mi))
474 SetWindowPos( hwnd, 0,
477 mi.rcMonitor.right - mi.rcMonitor.left,
478 mi.rcMonitor.bottom - mi.rcMonitor.top,
479 SWP_NOZORDER|SWP_FRAMECHANGED );
484 /* Maximize non embedded window */
485 ShowWindow( hwnd, SW_SHOWMAXIMIZED );
488 if( p_vout->p_sys->hparent )
490 /* Hide the previous window */
492 GetClientRect( hwnd, &rect );
493 SetParent( p_vout->p_sys->hwnd, hwnd );
494 SetWindowPos( p_vout->p_sys->hwnd, 0, 0, 0,
495 rect.right, rect.bottom,
496 SWP_NOZORDER|SWP_FRAMECHANGED );
499 HWND topLevelParent = GetParent( p_vout->p_sys->hparent );
501 HWND topLevelParent = GetAncestor( p_vout->p_sys->hparent, GA_ROOT );
503 ShowWindow( topLevelParent, SW_HIDE );
506 SetForegroundWindow( hwnd );
510 msg_Dbg( p_vout, "leaving fullscreen mode" );
511 /* Change window style, no borders and no title bar */
512 SetWindowLong( hwnd, GWL_STYLE, p_vout->p_sys->i_window_style );
514 if( p_vout->p_sys->hparent )
517 GetClientRect( p_vout->p_sys->hparent, &rect );
518 SetParent( p_vout->p_sys->hwnd, p_vout->p_sys->hparent );
519 SetWindowPos( p_vout->p_sys->hwnd, 0, 0, 0,
520 rect.right, rect.bottom,
521 SWP_NOZORDER|SWP_FRAMECHANGED );
524 HWND topLevelParent = GetParent( p_vout->p_sys->hparent );
526 HWND topLevelParent = GetAncestor( p_vout->p_sys->hparent, GA_ROOT );
528 ShowWindow( topLevelParent, SW_SHOW );
529 SetForegroundWindow( p_vout->p_sys->hparent );
530 ShowWindow( hwnd, SW_HIDE );
534 /* return to normal window for non embedded vout */
535 SetWindowPlacement( hwnd, &window_placement );
536 ShowWindow( hwnd, SW_SHOWNORMAL );
539 /* Make sure the mouse cursor is displayed */
540 PostMessage( p_vout->p_sys->hwnd, WM_VLC_SHOW_MOUSE, 0, 0 );
543 /* Update the object variable and trigger callback */
544 var_SetBool( p_vout, "fullscreen", p_vout->b_fullscreen );
548 void DisableScreensaver( vout_thread_t *p_vout )
550 /* disable screensaver by temporarily changing system settings */
551 p_vout->p_sys->i_spi_lowpowertimeout = 0;
552 p_vout->p_sys->i_spi_powerofftimeout = 0;
553 p_vout->p_sys->i_spi_screensavetimeout = 0;
554 if( var_GetBool( p_vout, "disable-screensaver" ) )
556 msg_Dbg(p_vout, "disabling screen saver");
557 SystemParametersInfo(SPI_GETLOWPOWERTIMEOUT,
558 0, &(p_vout->p_sys->i_spi_lowpowertimeout), 0);
559 if( 0 != p_vout->p_sys->i_spi_lowpowertimeout ) {
560 SystemParametersInfo(SPI_SETLOWPOWERTIMEOUT, 0, NULL, 0);
562 SystemParametersInfo(SPI_GETPOWEROFFTIMEOUT, 0,
563 &(p_vout->p_sys->i_spi_powerofftimeout), 0);
564 if( 0 != p_vout->p_sys->i_spi_powerofftimeout ) {
565 SystemParametersInfo(SPI_SETPOWEROFFTIMEOUT, 0, NULL, 0);
567 SystemParametersInfo(SPI_GETSCREENSAVETIMEOUT, 0,
568 &(p_vout->p_sys->i_spi_screensavetimeout), 0);
569 if( 0 != p_vout->p_sys->i_spi_screensavetimeout ) {
570 SystemParametersInfo(SPI_SETSCREENSAVETIMEOUT, 0, NULL, 0);
575 void RestoreScreensaver( vout_thread_t *p_vout )
577 /* restore screensaver system settings */
578 if( 0 != p_vout->p_sys->i_spi_lowpowertimeout ) {
579 SystemParametersInfo(SPI_SETLOWPOWERTIMEOUT,
580 p_vout->p_sys->i_spi_lowpowertimeout, NULL, 0);
582 if( 0 != p_vout->p_sys->i_spi_powerofftimeout ) {
583 SystemParametersInfo(SPI_SETPOWEROFFTIMEOUT,
584 p_vout->p_sys->i_spi_powerofftimeout, NULL, 0);
586 if( 0 != p_vout->p_sys->i_spi_screensavetimeout ) {
587 SystemParametersInfo(SPI_SETSCREENSAVETIMEOUT,
588 p_vout->p_sys->i_spi_screensavetimeout, NULL, 0);