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 );
75 /*****************************************************************************
76 * UpdateRects: update clipping rectangles
77 *****************************************************************************
78 * This function is called when the window position or size are changed, and
79 * its job is to update the source and destination RECTs used to display the
81 *****************************************************************************/
82 void UpdateRects( vout_thread_t *p_vout, bool b_force )
84 #define rect_src p_vout->p_sys->rect_src
85 #define rect_src_clipped p_vout->p_sys->rect_src_clipped
86 #define rect_dest p_vout->p_sys->rect_dest
87 #define rect_dest_clipped p_vout->p_sys->rect_dest_clipped
89 unsigned int i_width, i_height, i_x, i_y;
94 /* Retrieve the window size */
95 GetClientRect( p_vout->p_sys->hwnd, &rect );
97 /* Retrieve the window position */
98 point.x = point.y = 0;
99 ClientToScreen( p_vout->p_sys->hwnd, &point );
101 /* If nothing changed, we can return */
103 && p_vout->p_sys->i_window_width == rect.right
104 && p_vout->p_sys->i_window_height == rect.bottom
105 && p_vout->p_sys->i_window_x == point.x
106 && p_vout->p_sys->i_window_y == point.y )
111 /* Update the window position and size */
112 p_vout->p_sys->i_window_x = point.x;
113 p_vout->p_sys->i_window_y = point.y;
114 p_vout->p_sys->i_window_width = rect.right;
115 p_vout->p_sys->i_window_height = rect.bottom;
117 vout_PlacePicture( p_vout, rect.right, rect.bottom,
118 &i_x, &i_y, &i_width, &i_height );
120 if( p_vout->p_sys->hvideownd )
121 SetWindowPos( p_vout->p_sys->hvideownd, 0,
122 i_x, i_y, i_width, i_height,
123 SWP_NOCOPYBITS|SWP_NOZORDER|SWP_ASYNCWINDOWPOS );
125 /* Destination image position and dimensions */
126 rect_dest.left = point.x + i_x;
127 rect_dest.right = rect_dest.left + i_width;
128 rect_dest.top = point.y + i_y;
129 rect_dest.bottom = rect_dest.top + i_height;
131 #ifdef MODULE_NAME_IS_directx
132 /* Apply overlay hardware constraints */
133 if( p_vout->p_sys->b_using_overlay )
135 if( p_vout->p_sys->i_align_dest_boundary )
136 rect_dest.left = ( rect_dest.left +
137 p_vout->p_sys->i_align_dest_boundary / 2 ) &
138 ~p_vout->p_sys->i_align_dest_boundary;
140 if( p_vout->p_sys->i_align_dest_size )
141 rect_dest.right = (( rect_dest.right - rect_dest.left +
142 p_vout->p_sys->i_align_dest_size / 2 ) &
143 ~p_vout->p_sys->i_align_dest_size) + rect_dest.left;
146 /* UpdateOverlay directdraw function doesn't automatically clip to the
147 * display size so we need to do it otherwise it will fail */
149 /* Clip the destination window */
150 if( !IntersectRect( &rect_dest_clipped, &rect_dest,
151 &p_vout->p_sys->rect_display ) )
153 SetRectEmpty( &rect_src_clipped );
158 msg_Dbg( p_vout, "DirectXUpdateRects image_dst_clipped coords:"
160 rect_dest_clipped.left, rect_dest_clipped.top,
161 rect_dest_clipped.right, rect_dest_clipped.bottom );
164 #else /* MODULE_NAME_IS_directx */
166 /* AFAIK, there are no clipping constraints in Direct3D, OpenGL and GDI */
167 rect_dest_clipped = rect_dest;
171 /* the 2 following lines are to fix a bug when clicking on the desktop */
172 if( (rect_dest_clipped.right - rect_dest_clipped.left)==0 ||
173 (rect_dest_clipped.bottom - rect_dest_clipped.top)==0 )
175 SetRectEmpty( &rect_src_clipped );
179 /* src image dimensions */
182 rect_src.right = p_vout->render.i_width;
183 rect_src.bottom = p_vout->render.i_height;
185 /* Clip the source image */
186 rect_src_clipped.left = p_vout->fmt_out.i_x_offset +
187 (rect_dest_clipped.left - rect_dest.left) *
188 p_vout->fmt_out.i_visible_width / (rect_dest.right - rect_dest.left);
189 rect_src_clipped.right = p_vout->fmt_out.i_x_offset +
190 p_vout->fmt_out.i_visible_width -
191 (rect_dest.right - rect_dest_clipped.right) *
192 p_vout->fmt_out.i_visible_width / (rect_dest.right - rect_dest.left);
193 rect_src_clipped.top = p_vout->fmt_out.i_y_offset +
194 (rect_dest_clipped.top - rect_dest.top) *
195 p_vout->fmt_out.i_visible_height / (rect_dest.bottom - rect_dest.top);
196 rect_src_clipped.bottom = p_vout->fmt_out.i_y_offset +
197 p_vout->fmt_out.i_visible_height -
198 (rect_dest.bottom - rect_dest_clipped.bottom) *
199 p_vout->fmt_out.i_visible_height / (rect_dest.bottom - rect_dest.top);
201 #ifdef MODULE_NAME_IS_directx
202 /* Apply overlay hardware constraints */
203 if( p_vout->p_sys->b_using_overlay )
205 if( p_vout->p_sys->i_align_src_boundary )
206 rect_src_clipped.left = ( rect_src_clipped.left +
207 p_vout->p_sys->i_align_src_boundary / 2 ) &
208 ~p_vout->p_sys->i_align_src_boundary;
210 if( p_vout->p_sys->i_align_src_size )
211 rect_src_clipped.right = (( rect_src_clipped.right -
212 rect_src_clipped.left +
213 p_vout->p_sys->i_align_src_size / 2 ) &
214 ~p_vout->p_sys->i_align_src_size) + rect_src_clipped.left;
219 msg_Dbg( p_vout, "DirectXUpdateRects image_src_clipped"
220 " coords: %li,%li,%li,%li",
221 rect_src_clipped.left, rect_src_clipped.top,
222 rect_src_clipped.right, rect_src_clipped.bottom );
225 #ifdef MODULE_NAME_IS_directx
226 /* The destination coordinates need to be relative to the current
227 * directdraw primary surface (display) */
228 rect_dest_clipped.left -= p_vout->p_sys->rect_display.left;
229 rect_dest_clipped.right -= p_vout->p_sys->rect_display.left;
230 rect_dest_clipped.top -= p_vout->p_sys->rect_display.top;
231 rect_dest_clipped.bottom -= p_vout->p_sys->rect_display.top;
233 if( p_vout->p_sys->b_using_overlay )
234 DirectDrawUpdateOverlay( p_vout );
238 /* Windows 7 taskbar thumbnail code */
239 LPTASKBARLIST3 p_taskbl;
240 OSVERSIONINFO winVer;
241 winVer.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
242 if( GetVersionEx(&winVer) && winVer.dwMajorVersion > 5 )
246 if( S_OK == CoCreateInstance( &clsid_ITaskbarList,
247 NULL, CLSCTX_INPROC_SERVER,
251 RECT rect_video, rect_parent, rect_relative;
252 HWND hroot = GetAncestor(p_vout->p_sys->hwnd,GA_ROOT);
254 p_taskbl->vt->HrInit(p_taskbl);
255 GetWindowRect(p_vout->p_sys->hvideownd, &rect_video);
256 GetWindowRect(hroot, &rect_parent);
257 rect_relative.left = rect_video.left - rect_parent.left - 8;
258 rect_relative.right = rect_video.right - rect_video.left + rect_relative.left;
259 rect_relative.top = rect_video.top - rect_parent.top - 10;
260 rect_relative.bottom = rect_video.bottom - rect_video.top + rect_relative.top - 25;
262 if (S_OK != p_taskbl->vt->SetThumbnailClip(p_taskbl, hroot, &rect_relative))
263 msg_Err( p_vout, "SetThumbNailClip failed");
265 p_taskbl->vt->Release(p_taskbl);
270 /* Signal the change in size/position */
271 p_vout->p_sys->i_changes |= DX_POSITION_CHANGE;
274 #undef rect_src_clipped
276 #undef rect_dest_clipped
279 /*****************************************************************************
280 * Control: control facility for the vout
281 *****************************************************************************/
282 int Control( vout_thread_t *p_vout, int i_query, va_list args )
289 if( p_vout->p_sys->parent_window )
290 return vaControlParentWindow( p_vout, i_query, args );
292 /* Update dimensions */
293 rect_window.top = rect_window.left = 0;
294 rect_window.right = va_arg( args, unsigned int );
295 rect_window.bottom = va_arg( args, unsigned int );
296 if( !rect_window.right ) rect_window.right = p_vout->i_window_width;
297 if( !rect_window.bottom ) rect_window.bottom = p_vout->i_window_height;
298 AdjustWindowRect( &rect_window, p_vout->p_sys->i_window_style, 0 );
300 SetWindowPos( p_vout->p_sys->hwnd, 0, 0, 0,
301 rect_window.right - rect_window.left,
302 rect_window.bottom - rect_window.top, SWP_NOMOVE );
306 case VOUT_SET_STAY_ON_TOP:
307 if( p_vout->p_sys->hparent && !var_GetBool( p_vout, "fullscreen" ) )
308 return vaControlParentWindow( p_vout, i_query, args );
310 p_vout->p_sys->b_on_top_change = true;
319 /* Internal wrapper over GetWindowPlacement */
320 static WINDOWPLACEMENT getWindowState(HWND hwnd)
322 WINDOWPLACEMENT window_placement;
323 window_placement.length = sizeof(WINDOWPLACEMENT);
324 GetWindowPlacement( hwnd, &window_placement );
325 return window_placement;
328 /* Internal wrapper to call vout_ControlWindow for hparent */
329 static int vaControlParentWindow( vout_thread_t *p_vout, int i_query,
336 const unsigned i_width = va_arg(args, unsigned);
337 const unsigned i_height = va_arg(args, unsigned);
338 return vout_window_SetSize( p_vout->p_sys->parent_window, i_width, i_height );
340 case VOUT_SET_STAY_ON_TOP:
342 const bool is_on_top = va_arg(args, int);
343 return vout_window_SetOnTop( p_vout->p_sys->parent_window, is_on_top );
351 static int ControlParentWindow( vout_thread_t *p_vout, int i_query, ... )
356 va_start( args, i_query );
357 ret = vaControlParentWindow( p_vout, i_query, args );
363 void Win32ToggleFullscreen( vout_thread_t *p_vout )
365 HWND hwnd = (p_vout->p_sys->hparent && p_vout->p_sys->hfswnd) ?
366 p_vout->p_sys->hfswnd : p_vout->p_sys->hwnd;
368 /* Save the current windows placement/placement to restore
369 when fullscreen is over */
370 WINDOWPLACEMENT window_placement = getWindowState( hwnd );
372 p_vout->b_fullscreen = ! p_vout->b_fullscreen;
374 /* We want to go to Fullscreen */
375 if( p_vout->b_fullscreen )
377 msg_Dbg( p_vout, "entering fullscreen mode" );
379 /* Change window style, no borders and no title bar */
380 int i_style = WS_CLIPCHILDREN | WS_VISIBLE;
381 SetWindowLong( hwnd, GWL_STYLE, i_style );
383 if( p_vout->p_sys->hparent )
388 ClientToScreen( p_vout->p_sys->hwnd, &point );
389 GetClientRect( p_vout->p_sys->hwnd, &rect );
390 SetWindowPos( hwnd, 0, point.x, point.y,
391 rect.right, rect.bottom,
392 SWP_NOZORDER|SWP_FRAMECHANGED );
394 /* Retrieve current window position so fullscreen will happen
395 *on the right screen */
396 HMONITOR hmon = MonitorFromWindow(p_vout->p_sys->hparent,
397 MONITOR_DEFAULTTONEAREST);
399 if (GetMonitorInfo(hmon, &mi))
400 SetWindowPos( hwnd, 0,
403 mi.rcMonitor.right - mi.rcMonitor.left,
404 mi.rcMonitor.bottom - mi.rcMonitor.top,
405 SWP_NOZORDER|SWP_FRAMECHANGED );
410 /* Maximize non embedded window */
411 ShowWindow( hwnd, SW_SHOWMAXIMIZED );
414 if( p_vout->p_sys->hparent )
416 /* Hide the previous window */
418 GetClientRect( hwnd, &rect );
419 SetParent( p_vout->p_sys->hwnd, hwnd );
420 SetWindowPos( p_vout->p_sys->hwnd, 0, 0, 0,
421 rect.right, rect.bottom,
422 SWP_NOZORDER|SWP_FRAMECHANGED );
425 HWND topLevelParent = GetParent( p_vout->p_sys->hparent );
427 HWND topLevelParent = GetAncestor( p_vout->p_sys->hparent, GA_ROOT );
429 ShowWindow( topLevelParent, SW_HIDE );
432 SetForegroundWindow( hwnd );
436 msg_Dbg( p_vout, "leaving fullscreen mode" );
437 /* Change window style, no borders and no title bar */
438 SetWindowLong( hwnd, GWL_STYLE, p_vout->p_sys->i_window_style );
440 if( p_vout->p_sys->hparent )
443 GetClientRect( p_vout->p_sys->hparent, &rect );
444 SetParent( p_vout->p_sys->hwnd, p_vout->p_sys->hparent );
445 SetWindowPos( p_vout->p_sys->hwnd, 0, 0, 0,
446 rect.right, rect.bottom,
447 SWP_NOZORDER|SWP_FRAMECHANGED );
450 HWND topLevelParent = GetParent( p_vout->p_sys->hparent );
452 HWND topLevelParent = GetAncestor( p_vout->p_sys->hparent, GA_ROOT );
454 ShowWindow( topLevelParent, SW_SHOW );
455 SetForegroundWindow( p_vout->p_sys->hparent );
456 ShowWindow( hwnd, SW_HIDE );
460 /* return to normal window for non embedded vout */
461 SetWindowPlacement( hwnd, &window_placement );
462 ShowWindow( hwnd, SW_SHOWNORMAL );
465 /* Make sure the mouse cursor is displayed */
466 PostMessage( p_vout->p_sys->hwnd, WM_VLC_SHOW_MOUSE, 0, 0 );
469 /* Update the object variable and trigger callback */
470 var_SetBool( p_vout, "fullscreen", p_vout->b_fullscreen );
474 void DisableScreensaver( vout_thread_t *p_vout )
476 /* disable screensaver by temporarily changing system settings */
477 p_vout->p_sys->i_spi_lowpowertimeout = 0;
478 p_vout->p_sys->i_spi_powerofftimeout = 0;
479 p_vout->p_sys->i_spi_screensavetimeout = 0;
480 if( var_GetBool( p_vout, "disable-screensaver" ) )
482 msg_Dbg(p_vout, "disabling screen saver");
483 SystemParametersInfo(SPI_GETLOWPOWERTIMEOUT,
484 0, &(p_vout->p_sys->i_spi_lowpowertimeout), 0);
485 if( 0 != p_vout->p_sys->i_spi_lowpowertimeout ) {
486 SystemParametersInfo(SPI_SETLOWPOWERTIMEOUT, 0, NULL, 0);
488 SystemParametersInfo(SPI_GETPOWEROFFTIMEOUT, 0,
489 &(p_vout->p_sys->i_spi_powerofftimeout), 0);
490 if( 0 != p_vout->p_sys->i_spi_powerofftimeout ) {
491 SystemParametersInfo(SPI_SETPOWEROFFTIMEOUT, 0, NULL, 0);
493 SystemParametersInfo(SPI_GETSCREENSAVETIMEOUT, 0,
494 &(p_vout->p_sys->i_spi_screensavetimeout), 0);
495 if( 0 != p_vout->p_sys->i_spi_screensavetimeout ) {
496 SystemParametersInfo(SPI_SETSCREENSAVETIMEOUT, 0, NULL, 0);
501 void RestoreScreensaver( vout_thread_t *p_vout )
503 /* restore screensaver system settings */
504 if( 0 != p_vout->p_sys->i_spi_lowpowertimeout ) {
505 SystemParametersInfo(SPI_SETLOWPOWERTIMEOUT,
506 p_vout->p_sys->i_spi_lowpowertimeout, NULL, 0);
508 if( 0 != p_vout->p_sys->i_spi_powerofftimeout ) {
509 SystemParametersInfo(SPI_SETPOWEROFFTIMEOUT,
510 p_vout->p_sys->i_spi_powerofftimeout, NULL, 0);
512 if( 0 != p_vout->p_sys->i_spi_screensavetimeout ) {
513 SystemParametersInfo(SPI_SETSCREENSAVETIMEOUT,
514 p_vout->p_sys->i_spi_screensavetimeout, NULL, 0);