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 *****************************************************************************/
34 #include <vlc_common.h>
35 #include <vlc_vout_display.h>
36 #include <vlc_vout_window.h>
42 #ifdef MODULE_NAME_IS_directx
45 #ifdef MODULE_NAME_IS_direct3d
48 #ifdef MODULE_NAME_IS_glwin32
49 #include "../opengl.h"
55 #include <vlc_windows_interfaces.h>
60 //WINSHELLAPI BOOL WINAPI SHFullScreen(HWND hwndRequester, DWORD dwState);
63 static void CommonChangeThumbnailClip(vout_display_t *, bool show);
64 static int CommonControlSetFullscreen(vout_display_t *, bool is_fullscreen);
66 static void DisableScreensaver(vout_display_t *);
67 static void RestoreScreensaver(vout_display_t *);
70 int CommonInit(vout_display_t *vd)
72 vout_display_sys_t *sys = vd->sys;
75 sys->hvideownd = NULL;
79 SetRectEmpty(&sys->rect_display);
80 SetRectEmpty(&sys->rect_parent);
81 sys->is_first_display = true;
82 sys->is_on_top = false;
84 var_Create(vd, "video-title", VLC_VAR_STRING | VLC_VAR_DOINHERIT);
85 var_Create(vd, "video-deco", VLC_VAR_BOOL | VLC_VAR_DOINHERIT);
88 sys->event = EventThreadCreate(vd);
93 memset(&cfg, 0, sizeof(cfg));
94 #ifdef MODULE_NAME_IS_direct3d
95 cfg.use_desktop = sys->use_desktop;
97 #ifdef MODULE_NAME_IS_directx
98 cfg.use_overlay = sys->use_overlay;
100 cfg.win.type = VOUT_WINDOW_TYPE_HWND;
101 cfg.win.x = var_InheritInteger(vd, "video-x");
102 cfg.win.y = var_InheritInteger(vd, "video-y");
103 cfg.win.width = vd->cfg->display.width;
104 cfg.win.height = vd->cfg->display.height;
107 if (EventThreadStart(sys->event, &hwnd, &cfg))
110 sys->parent_window = hwnd.parent_window;
111 sys->hparent = hwnd.hparent;
112 sys->hwnd = hwnd.hwnd;
113 sys->hvideownd = hwnd.hvideownd;
114 sys->hfswnd = hwnd.hfswnd;
116 if (vd->cfg->is_fullscreen) {
117 if (CommonControlSetFullscreen(vd, true))
118 vout_display_SendEventFullscreen(vd, false);
121 /* Why not with glwin32 */
122 #if !defined(UNDER_CE) && !defined(MODULE_NAME_IS_glwin32)
123 var_Create(vd, "disable-screensaver", VLC_VAR_BOOL | VLC_VAR_DOINHERIT);
124 DisableScreensaver (vd);
131 void CommonClean(vout_display_t *vd)
133 vout_display_sys_t *sys = vd->sys;
136 CommonChangeThumbnailClip(vd, false);
137 EventThreadStop(sys->event);
138 EventThreadDestroy(sys->event);
141 #if !defined(UNDER_CE) && !defined(MODULE_NAME_IS_glwin32)
142 RestoreScreensaver(vd);
146 void CommonManage(vout_display_t *vd)
148 vout_display_sys_t *sys = vd->sys;
150 /* We used to call the Win32 PeekMessage function here to read the window
151 * messages. But since window can stay blocked into this function for a
152 * long time (for example when you move your window on the screen), I
153 * decided to isolate PeekMessage in another thread. */
155 /* If we do not control our window, we check for geometry changes
156 * ourselves because the parent might not send us its events. */
161 /* Check if the parent window has resized or moved */
162 GetClientRect(sys->hparent, &rect_parent);
163 point.x = point.y = 0;
164 ClientToScreen(sys->hparent, &point);
165 OffsetRect(&rect_parent, point.x, point.y);
167 if (!EqualRect(&rect_parent, &sys->rect_parent)) {
168 sys->rect_parent = rect_parent;
170 /* This code deals with both resize and move
172 * For most drivers(direct3d, gdi, opengl), move is never
173 * an issue. The surface automatically gets moved together
174 * with the associated window (hvideownd)
176 * For directx, it is still important to call UpdateRects
177 * on a move of the parent window, even if no resize occured
179 SetWindowPos(sys->hwnd, 0, 0, 0,
180 rect_parent.right - rect_parent.left,
181 rect_parent.bottom - rect_parent.top,
184 UpdateRects(vd, NULL, NULL, true);
188 /* HasMoved means here resize or move */
189 if (EventThreadGetAndResetHasMoved(sys->event))
190 UpdateRects(vd, NULL, NULL, false);
194 * It ensures that the video window is shown after the first picture
197 void CommonDisplay(vout_display_t *vd)
199 vout_display_sys_t *sys = vd->sys;
201 if (!sys->is_first_display)
204 /* Video window is initially hidden, show it now since we got a
207 SetWindowPos(sys->hvideownd, 0, 0, 0, 0, 0,
214 sys->is_first_display = false;
217 void AlignRect(RECT *r, int align_boundary, int align_size)
220 r->left = (r->left + align_boundary/2) & ~align_boundary;
222 r->right = ((r->right - r->left + align_size/2) & ~align_size) + r->left;
226 static void CommonChangeThumbnailClip(vout_display_t *vd, bool show)
229 vout_display_sys_t *sys = vd->sys;
231 /* Windows 7 taskbar thumbnail code */
232 OSVERSIONINFO winVer;
233 winVer.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
234 if (!GetVersionEx(&winVer) || winVer.dwMajorVersion <= 5)
239 LPTASKBARLIST3 taskbl;
240 if (S_OK == CoCreateInstance(&clsid_ITaskbarList,
241 NULL, CLSCTX_INPROC_SERVER,
244 taskbl->vt->HrInit(taskbl);
246 HWND hroot = GetAncestor(sys->hwnd,GA_ROOT);
250 GetWindowRect(sys->hvideownd, &video);
251 GetWindowRect(hroot, &parent);
252 relative.left = video.left - parent.left - 8;
253 relative.top = video.top - parent.top - 10;
255 relative.right = video.right - video.left + relative.left;
256 relative.bottom = video.bottom - video.top + relative.top - 25;
258 if (S_OK != taskbl->vt->SetThumbnailClip(taskbl, hroot,
259 show ? &relative : NULL))
260 msg_Err(vd, "SetThumbNailClip failed");
262 taskbl->vt->Release(taskbl);
268 /*****************************************************************************
269 * UpdateRects: update clipping rectangles
270 *****************************************************************************
271 * This function is called when the window position or size are changed, and
272 * its job is to update the source and destination RECTs used to display the
274 *****************************************************************************/
275 void UpdateRects(vout_display_t *vd,
276 const vout_display_cfg_t *cfg,
277 const video_format_t *source,
280 vout_display_sys_t *sys = vd->sys;
281 #define rect_src sys->rect_src
282 #define rect_src_clipped sys->rect_src_clipped
283 #define rect_dest sys->rect_dest
284 #define rect_dest_clipped sys->rect_dest_clipped
293 source = &vd->source;
295 /* Retrieve the window size */
296 GetClientRect(sys->hwnd, &rect);
298 /* Retrieve the window position */
299 point.x = point.y = 0;
300 ClientToScreen(sys->hwnd, &point);
302 /* If nothing changed, we can return */
305 EventThreadUpdateWindowPosition(sys->event, &has_moved, &is_resized,
307 rect.right, rect.bottom);
309 vout_display_SendEventDisplaySize(vd, rect.right, rect.bottom, cfg->is_fullscreen);
310 if (!is_forced && !has_moved && !is_resized )
313 /* Update the window position and size */
314 vout_display_cfg_t place_cfg = *cfg;
315 place_cfg.display.width = rect.right;
316 place_cfg.display.height = rect.bottom;
318 vout_display_place_t place;
319 vout_display_PlacePicture(&place, source, &place_cfg, false);
321 EventThreadUpdateSourceAndPlace(sys->event, source, &place);
322 #if defined(MODULE_NAME_IS_wingapi)
323 if (place.width != vd->fmt.i_width || place.height != vd->fmt.i_height)
324 vout_display_SendEventPicturesInvalid(vd);
328 SetWindowPos(sys->hvideownd, 0,
329 place.x, place.y, place.width, place.height,
330 SWP_NOCOPYBITS|SWP_NOZORDER|SWP_ASYNCWINDOWPOS);
332 /* Destination image position and dimensions */
333 #if defined(MODULE_NAME_IS_direct3d)
335 rect_dest.right = place.width;
337 rect_dest.bottom = place.height;
339 rect_dest.left = point.x + place.x;
340 rect_dest.right = rect_dest.left + place.width;
341 rect_dest.top = point.y + place.y;
342 rect_dest.bottom = rect_dest.top + place.height;
344 #ifdef MODULE_NAME_IS_directx
345 /* Apply overlay hardware constraints */
346 if (sys->use_overlay)
347 AlignRect(&rect_dest, sys->i_align_dest_boundary, sys->i_align_dest_size);
352 #if defined(MODULE_NAME_IS_directx) || defined(MODULE_NAME_IS_direct3d)
353 /* UpdateOverlay directdraw function doesn't automatically clip to the
354 * display size so we need to do it otherwise it will fail
355 * It is also needed for d3d to avoid exceding our surface size */
357 /* Clip the destination window */
358 if (!IntersectRect(&rect_dest_clipped, &rect_dest,
359 &sys->rect_display)) {
360 SetRectEmpty(&rect_src_clipped);
365 msg_Dbg(vd, "DirectXUpdateRects image_dst_clipped coords:"
367 rect_dest_clipped.left, rect_dest_clipped.top,
368 rect_dest_clipped.right, rect_dest_clipped.bottom);
373 /* AFAIK, there are no clipping constraints in Direct3D, OpenGL and GDI */
374 rect_dest_clipped = rect_dest;
378 /* the 2 following lines are to fix a bug when clicking on the desktop */
379 if ((rect_dest_clipped.right - rect_dest_clipped.left) == 0 ||
380 (rect_dest_clipped.bottom - rect_dest_clipped.top) == 0) {
381 SetRectEmpty(&rect_src_clipped);
385 /* src image dimensions */
388 rect_src.right = source->i_width;
389 rect_src.bottom = source->i_height;
391 /* Clip the source image */
392 rect_src_clipped.left = source->i_x_offset +
393 (rect_dest_clipped.left - rect_dest.left) *
394 source->i_visible_width / (rect_dest.right - rect_dest.left);
395 rect_src_clipped.right = source->i_x_offset +
396 source->i_visible_width -
397 (rect_dest.right - rect_dest_clipped.right) *
398 source->i_visible_width / (rect_dest.right - rect_dest.left);
399 rect_src_clipped.top = source->i_y_offset +
400 (rect_dest_clipped.top - rect_dest.top) *
401 source->i_visible_height / (rect_dest.bottom - rect_dest.top);
402 rect_src_clipped.bottom = source->i_y_offset +
403 source->i_visible_height -
404 (rect_dest.bottom - rect_dest_clipped.bottom) *
405 source->i_visible_height / (rect_dest.bottom - rect_dest.top);
407 #ifdef MODULE_NAME_IS_directx
408 /* Apply overlay hardware constraints */
409 if (sys->use_overlay)
410 AlignRect(&rect_src_clipped, sys->i_align_src_boundary, sys->i_align_src_size);
411 #elif defined(MODULE_NAME_IS_direct3d)
412 /* Needed at least with YUV content */
413 rect_src_clipped.left &= ~1;
414 rect_src_clipped.right &= ~1;
415 rect_src_clipped.top &= ~1;
416 rect_src_clipped.bottom &= ~1;
420 msg_Dbg(vd, "DirectXUpdateRects image_src_clipped"
421 " coords: %li,%li,%li,%li",
422 rect_src_clipped.left, rect_src_clipped.top,
423 rect_src_clipped.right, rect_src_clipped.bottom);
426 #ifdef MODULE_NAME_IS_directx
427 /* The destination coordinates need to be relative to the current
428 * directdraw primary surface (display) */
429 rect_dest_clipped.left -= sys->rect_display.left;
430 rect_dest_clipped.right -= sys->rect_display.left;
431 rect_dest_clipped.top -= sys->rect_display.top;
432 rect_dest_clipped.bottom -= sys->rect_display.top;
435 CommonChangeThumbnailClip(vd, true);
438 /* Signal the change in size/position */
439 sys->changes |= DX_POSITION_CHANGE;
442 #undef rect_src_clipped
444 #undef rect_dest_clipped
447 static int CommonControlSetFullscreen(vout_display_t *vd, bool is_fullscreen)
449 vout_display_sys_t *sys = vd->sys;
451 #ifdef MODULE_NAME_IS_direct3d
452 if (sys->use_desktop && is_fullscreen)
457 if (sys->parent_window)
458 return vout_window_SetFullScreen(sys->parent_window, is_fullscreen);
461 HWND hwnd = sys->hparent && sys->hfswnd ? sys->hfswnd : sys->hwnd;
463 /* Save the current windows placement/placement to restore
464 when fullscreen is over */
465 WINDOWPLACEMENT window_placement;
466 window_placement.length = sizeof(WINDOWPLACEMENT);
467 GetWindowPlacement(hwnd, &window_placement);
470 msg_Dbg(vd, "entering fullscreen mode");
472 /* Change window style, no borders and no title bar */
473 SetWindowLong(hwnd, GWL_STYLE, WS_CLIPCHILDREN | WS_VISIBLE);
479 ClientToScreen(sys->hwnd, &point);
480 GetClientRect(sys->hwnd, &rect);
481 SetWindowPos(hwnd, 0, point.x, point.y,
482 rect.right, rect.bottom,
483 SWP_NOZORDER|SWP_FRAMECHANGED);
485 /* Retrieve current window position so fullscreen will happen
486 *on the right screen */
487 HMONITOR hmon = MonitorFromWindow(sys->hparent,
488 MONITOR_DEFAULTTONEAREST);
490 mi.cbSize = sizeof(MONITORINFO);
491 if (GetMonitorInfo(hmon, &mi))
492 SetWindowPos(hwnd, 0,
495 mi.rcMonitor.right - mi.rcMonitor.left,
496 mi.rcMonitor.bottom - mi.rcMonitor.top,
497 SWP_NOZORDER|SWP_FRAMECHANGED);
500 /* Maximize non embedded window */
501 ShowWindow(hwnd, SW_SHOWMAXIMIZED);
505 /* Hide the previous window */
507 GetClientRect(hwnd, &rect);
508 SetParent(sys->hwnd, hwnd);
509 SetWindowPos(sys->hwnd, 0, 0, 0,
510 rect.right, rect.bottom,
511 SWP_NOZORDER|SWP_FRAMECHANGED);
514 HWND topLevelParent = GetParent(sys->hparent);
516 HWND topLevelParent = GetAncestor(sys->hparent, GA_ROOT);
518 ShowWindow(topLevelParent, SW_HIDE);
520 SetForegroundWindow(hwnd);
522 msg_Dbg(vd, "leaving fullscreen mode");
524 /* Change window style, no borders and no title bar */
525 SetWindowLong(hwnd, GWL_STYLE, EventThreadGetWindowStyle(sys->event));
529 GetClientRect(sys->hparent, &rect);
530 SetParent(sys->hwnd, sys->hparent);
531 SetWindowPos(sys->hwnd, 0, 0, 0,
532 rect.right, rect.bottom,
533 SWP_NOZORDER|SWP_FRAMECHANGED);
536 HWND topLevelParent = GetParent(sys->hparent);
538 HWND topLevelParent = GetAncestor(sys->hparent, GA_ROOT);
540 ShowWindow(topLevelParent, SW_SHOW);
541 SetForegroundWindow(sys->hparent);
542 ShowWindow(hwnd, SW_HIDE);
544 /* return to normal window for non embedded vout */
545 SetWindowPlacement(hwnd, &window_placement);
546 ShowWindow(hwnd, SW_SHOWNORMAL);
552 int CommonControl(vout_display_t *vd, int query, va_list args)
554 vout_display_sys_t *sys = vd->sys;
557 case VOUT_DISPLAY_CHANGE_DISPLAY_SIZE: /* const vout_display_cfg_t *p_cfg, int is_forced */
558 case VOUT_DISPLAY_CHANGE_DISPLAY_FILLED: /* const vout_display_cfg_t *p_cfg */
559 case VOUT_DISPLAY_CHANGE_ZOOM: /* const vout_display_cfg_t *p_cfg */
560 case VOUT_DISPLAY_CHANGE_SOURCE_ASPECT: /* const video_format_t *p_source */
561 case VOUT_DISPLAY_CHANGE_SOURCE_CROP: { /* const video_format_t *p_source */
562 const vout_display_cfg_t *cfg;
563 const video_format_t *source;
564 bool is_forced = true;
565 if (query == VOUT_DISPLAY_CHANGE_SOURCE_CROP ||
566 query == VOUT_DISPLAY_CHANGE_SOURCE_ASPECT) {
568 source = va_arg(args, const video_format_t *);
570 cfg = va_arg(args, const vout_display_cfg_t *);
571 source = &vd->source;
572 if (query == VOUT_DISPLAY_CHANGE_DISPLAY_SIZE)
573 is_forced = va_arg(args, int);
575 if (query == VOUT_DISPLAY_CHANGE_DISPLAY_SIZE && is_forced) {
576 /* Update dimensions */
577 if (sys->parent_window) {
578 vout_window_SetSize(sys->parent_window, cfg->display.width, cfg->display.height);
582 rect_window.left = 0;
583 rect_window.right = cfg->display.width;
584 rect_window.bottom = cfg->display.height;
585 AdjustWindowRect(&rect_window, EventThreadGetWindowStyle(sys->event), 0);
587 SetWindowPos(sys->hwnd, 0, 0, 0,
588 rect_window.right - rect_window.left,
589 rect_window.bottom - rect_window.top, SWP_NOMOVE);
592 UpdateRects(vd, cfg, source, is_forced);
595 case VOUT_DISPLAY_CHANGE_WINDOW_STATE: { /* unsigned state */
596 const unsigned state = va_arg(args, unsigned);
597 const bool is_on_top = (state & VOUT_WINDOW_STATE_ABOVE) != 0;
598 #ifdef MODULE_NAME_IS_direct3d
599 if (sys->use_desktop && is_on_top)
602 if (sys->parent_window) {
603 if (vout_window_SetState(sys->parent_window, state))
606 HMENU hMenu = GetSystemMenu(sys->hwnd, FALSE);
608 if (is_on_top && !(GetWindowLong(sys->hwnd, GWL_EXSTYLE) & WS_EX_TOPMOST)) {
609 CheckMenuItem(hMenu, IDM_TOGGLE_ON_TOP, MF_BYCOMMAND | MFS_CHECKED);
610 SetWindowPos(sys->hwnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE);
611 } else if (!is_on_top && (GetWindowLong(sys->hwnd, GWL_EXSTYLE) & WS_EX_TOPMOST)) {
612 CheckMenuItem(hMenu, IDM_TOGGLE_ON_TOP, MF_BYCOMMAND | MFS_UNCHECKED);
613 SetWindowPos(sys->hwnd, HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOSIZE|SWP_NOMOVE);
616 sys->is_on_top = is_on_top;
619 case VOUT_DISPLAY_CHANGE_FULLSCREEN: { /* const vout_display_cfg_t *p_cfg */
620 const vout_display_cfg_t *cfg = va_arg(args, const vout_display_cfg_t *);
621 if (CommonControlSetFullscreen(vd, cfg->is_fullscreen))
623 UpdateRects(vd, NULL, NULL, false);
627 case VOUT_DISPLAY_HIDE_MOUSE:
628 EventThreadMouseHide(sys->event);
630 case VOUT_DISPLAY_RESET_PICTURES:
638 static void DisableScreensaver(vout_display_t *vd)
640 vout_display_sys_t *sys = vd->sys;
642 /* disable screensaver by temporarily changing system settings */
643 sys->i_spi_lowpowertimeout = 0;
644 sys->i_spi_powerofftimeout = 0;
645 sys->i_spi_screensavetimeout = 0;
646 if (var_GetBool(vd, "disable-screensaver")) {
647 msg_Dbg(vd, "disabling screen saver");
648 SystemParametersInfo(SPI_GETLOWPOWERTIMEOUT, 0,
649 &sys->i_spi_lowpowertimeout, 0);
650 if (0 != sys->i_spi_lowpowertimeout) {
651 SystemParametersInfo(SPI_SETLOWPOWERTIMEOUT, 0, NULL, 0);
653 SystemParametersInfo(SPI_GETPOWEROFFTIMEOUT, 0,
654 &sys->i_spi_powerofftimeout, 0);
655 if (0 != sys->i_spi_powerofftimeout) {
656 SystemParametersInfo(SPI_SETPOWEROFFTIMEOUT, 0, NULL, 0);
658 SystemParametersInfo(SPI_GETSCREENSAVETIMEOUT, 0,
659 &sys->i_spi_screensavetimeout, 0);
660 if (0 != sys->i_spi_screensavetimeout) {
661 SystemParametersInfo(SPI_SETSCREENSAVETIMEOUT, 0, NULL, 0);
666 static void RestoreScreensaver(vout_display_t *vd)
668 vout_display_sys_t *sys = vd->sys;
670 /* restore screensaver system settings */
671 if (0 != sys->i_spi_lowpowertimeout) {
672 SystemParametersInfo(SPI_SETLOWPOWERTIMEOUT,
673 sys->i_spi_lowpowertimeout, NULL, 0);
675 if (0 != sys->i_spi_powerofftimeout) {
676 SystemParametersInfo(SPI_SETPOWEROFFTIMEOUT,
677 sys->i_spi_powerofftimeout, NULL, 0);
679 if (0 != sys->i_spi_screensavetimeout) {
680 SystemParametersInfo(SPI_SETSCREENSAVETIMEOUT,
681 sys->i_spi_screensavetimeout, NULL, 0);