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);
87 /* FIXME remove mouse hide from msw */
88 var_Create(vd, "mouse-hide-timeout", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
91 sys->event = EventThreadCreate(vd);
96 memset(&cfg, 0, sizeof(cfg));
97 #ifdef MODULE_NAME_IS_direct3d
98 cfg.use_desktop = sys->use_desktop;
100 #ifdef MODULE_NAME_IS_directx
101 cfg.use_overlay = sys->use_overlay;
103 cfg.win.type = VOUT_WINDOW_TYPE_HWND;
104 cfg.win.x = var_InheritInteger(vd, "video-x");
105 cfg.win.y = var_InheritInteger(vd, "video-y");
106 cfg.win.width = vd->cfg->display.width;
107 cfg.win.height = vd->cfg->display.height;
110 if (EventThreadStart(sys->event, &hwnd, &cfg))
113 sys->parent_window = hwnd.parent_window;
114 sys->hparent = hwnd.hparent;
115 sys->hwnd = hwnd.hwnd;
116 sys->hvideownd = hwnd.hvideownd;
117 sys->hfswnd = hwnd.hfswnd;
119 if (vd->cfg->is_fullscreen) {
120 if (CommonControlSetFullscreen(vd, true))
121 vout_display_SendEventFullscreen(vd, false);
124 /* Why not with glwin32 */
125 #if !defined(UNDER_CE) && !defined(MODULE_NAME_IS_glwin32)
126 var_Create(vd, "disable-screensaver", VLC_VAR_BOOL | VLC_VAR_DOINHERIT);
127 DisableScreensaver (vd);
134 void CommonClean(vout_display_t *vd)
136 vout_display_sys_t *sys = vd->sys;
139 CommonChangeThumbnailClip(vd, false);
140 EventThreadStop(sys->event);
141 EventThreadDestroy(sys->event);
144 #if !defined(UNDER_CE) && !defined(MODULE_NAME_IS_glwin32)
145 RestoreScreensaver(vd);
149 void CommonManage(vout_display_t *vd)
151 vout_display_sys_t *sys = vd->sys;
153 /* We used to call the Win32 PeekMessage function here to read the window
154 * messages. But since window can stay blocked into this function for a
155 * long time (for example when you move your window on the screen), I
156 * decided to isolate PeekMessage in another thread. */
158 /* If we do not control our window, we check for geometry changes
159 * ourselves because the parent might not send us its events. */
164 /* Check if the parent window has resized or moved */
165 GetClientRect(sys->hparent, &rect_parent);
166 point.x = point.y = 0;
167 ClientToScreen(sys->hparent, &point);
168 OffsetRect(&rect_parent, point.x, point.y);
170 if (!EqualRect(&rect_parent, &sys->rect_parent)) {
171 sys->rect_parent = rect_parent;
173 /* This code deals with both resize and move
175 * For most drivers(direct3d, gdi, opengl), move is never
176 * an issue. The surface automatically gets moved together
177 * with the associated window (hvideownd)
179 * For directx, it is still important to call UpdateRects
180 * on a move of the parent window, even if no resize occured
182 SetWindowPos(sys->hwnd, 0, 0, 0,
183 rect_parent.right - rect_parent.left,
184 rect_parent.bottom - rect_parent.top,
187 UpdateRects(vd, NULL, NULL, true);
191 /* HasMoved means here resize or move */
192 if (EventThreadGetAndResetHasMoved(sys->event))
193 UpdateRects(vd, NULL, NULL, false);
196 EventThreadMouseAutoHide(sys->event);
200 * It ensures that the video window is shown after the first picture
203 void CommonDisplay(vout_display_t *vd)
205 vout_display_sys_t *sys = vd->sys;
207 if (!sys->is_first_display)
210 /* Video window is initially hidden, show it now since we got a
213 SetWindowPos(sys->hvideownd, 0, 0, 0, 0, 0,
220 sys->is_first_display = false;
223 void AlignRect(RECT *r, int align_boundary, int align_size)
226 r->left = (r->left + align_boundary/2) & ~align_boundary;
228 r->right = ((r->right - r->left + align_size/2) & ~align_size) + r->left;
232 static void CommonChangeThumbnailClip(vout_display_t *vd, bool show)
235 vout_display_sys_t *sys = vd->sys;
237 /* Windows 7 taskbar thumbnail code */
238 OSVERSIONINFO winVer;
239 winVer.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
240 if (!GetVersionEx(&winVer) || winVer.dwMajorVersion <= 5)
245 LPTASKBARLIST3 taskbl;
246 if (S_OK == CoCreateInstance(&clsid_ITaskbarList,
247 NULL, CLSCTX_INPROC_SERVER,
250 taskbl->vt->HrInit(taskbl);
252 HWND hroot = GetAncestor(sys->hwnd,GA_ROOT);
256 GetWindowRect(sys->hvideownd, &video);
257 GetWindowRect(hroot, &parent);
258 relative.left = video.left - parent.left - 8;
259 relative.top = video.top - parent.top - 10;
261 relative.right = video.right - video.left + relative.left;
262 relative.bottom = video.bottom - video.top + relative.top - 25;
264 if (S_OK != taskbl->vt->SetThumbnailClip(taskbl, hroot,
265 show ? &relative : NULL))
266 msg_Err(vd, "SetThumbNailClip failed");
268 taskbl->vt->Release(taskbl);
274 /*****************************************************************************
275 * UpdateRects: update clipping rectangles
276 *****************************************************************************
277 * This function is called when the window position or size are changed, and
278 * its job is to update the source and destination RECTs used to display the
280 *****************************************************************************/
281 void UpdateRects(vout_display_t *vd,
282 const vout_display_cfg_t *cfg,
283 const video_format_t *source,
286 vout_display_sys_t *sys = vd->sys;
287 #define rect_src sys->rect_src
288 #define rect_src_clipped sys->rect_src_clipped
289 #define rect_dest sys->rect_dest
290 #define rect_dest_clipped sys->rect_dest_clipped
299 source = &vd->source;
301 /* Retrieve the window size */
302 GetClientRect(sys->hwnd, &rect);
304 /* Retrieve the window position */
305 point.x = point.y = 0;
306 ClientToScreen(sys->hwnd, &point);
308 /* If nothing changed, we can return */
311 EventThreadUpdateWindowPosition(sys->event, &has_moved, &is_resized,
313 rect.right, rect.bottom);
315 vout_display_SendEventDisplaySize(vd, rect.right, rect.bottom, cfg->is_fullscreen);
316 if (!is_forced && !has_moved && !is_resized )
319 /* Update the window position and size */
320 vout_display_cfg_t place_cfg = *cfg;
321 place_cfg.display.width = rect.right;
322 place_cfg.display.height = rect.bottom;
324 vout_display_place_t place;
325 vout_display_PlacePicture(&place, source, &place_cfg, false);
327 EventThreadUpdateSourceAndPlace(sys->event, source, &place);
328 #if defined(MODULE_NAME_IS_wingapi)
329 if (place.width != vd->fmt.i_width || place.height != vd->fmt.i_height)
330 vout_display_SendEventPicturesInvalid(vd);
334 SetWindowPos(sys->hvideownd, 0,
335 place.x, place.y, place.width, place.height,
336 SWP_NOCOPYBITS|SWP_NOZORDER|SWP_ASYNCWINDOWPOS);
338 /* Destination image position and dimensions */
339 #if defined(MODULE_NAME_IS_direct3d)
341 rect_dest.right = place.width;
343 rect_dest.bottom = place.height;
345 rect_dest.left = point.x + place.x;
346 rect_dest.right = rect_dest.left + place.width;
347 rect_dest.top = point.y + place.y;
348 rect_dest.bottom = rect_dest.top + place.height;
350 #ifdef MODULE_NAME_IS_directx
351 /* Apply overlay hardware constraints */
352 if (sys->use_overlay)
353 AlignRect(&rect_dest, sys->i_align_dest_boundary, sys->i_align_dest_size);
358 #if defined(MODULE_NAME_IS_directx) || defined(MODULE_NAME_IS_direct3d)
359 /* UpdateOverlay directdraw function doesn't automatically clip to the
360 * display size so we need to do it otherwise it will fail
361 * It is also needed for d3d to avoid exceding our surface size */
363 /* Clip the destination window */
364 if (!IntersectRect(&rect_dest_clipped, &rect_dest,
365 &sys->rect_display)) {
366 SetRectEmpty(&rect_src_clipped);
371 msg_Dbg(vd, "DirectXUpdateRects image_dst_clipped coords:"
373 rect_dest_clipped.left, rect_dest_clipped.top,
374 rect_dest_clipped.right, rect_dest_clipped.bottom);
379 /* AFAIK, there are no clipping constraints in Direct3D, OpenGL and GDI */
380 rect_dest_clipped = rect_dest;
384 /* the 2 following lines are to fix a bug when clicking on the desktop */
385 if ((rect_dest_clipped.right - rect_dest_clipped.left) == 0 ||
386 (rect_dest_clipped.bottom - rect_dest_clipped.top) == 0) {
387 SetRectEmpty(&rect_src_clipped);
391 /* src image dimensions */
394 rect_src.right = source->i_width;
395 rect_src.bottom = source->i_height;
397 /* Clip the source image */
398 rect_src_clipped.left = source->i_x_offset +
399 (rect_dest_clipped.left - rect_dest.left) *
400 source->i_visible_width / (rect_dest.right - rect_dest.left);
401 rect_src_clipped.right = source->i_x_offset +
402 source->i_visible_width -
403 (rect_dest.right - rect_dest_clipped.right) *
404 source->i_visible_width / (rect_dest.right - rect_dest.left);
405 rect_src_clipped.top = source->i_y_offset +
406 (rect_dest_clipped.top - rect_dest.top) *
407 source->i_visible_height / (rect_dest.bottom - rect_dest.top);
408 rect_src_clipped.bottom = source->i_y_offset +
409 source->i_visible_height -
410 (rect_dest.bottom - rect_dest_clipped.bottom) *
411 source->i_visible_height / (rect_dest.bottom - rect_dest.top);
413 #ifdef MODULE_NAME_IS_directx
414 /* Apply overlay hardware constraints */
415 if (sys->use_overlay)
416 AlignRect(&rect_src_clipped, sys->i_align_src_boundary, sys->i_align_src_size);
417 #elif defined(MODULE_NAME_IS_direct3d)
418 /* Needed at least with YUV content */
419 rect_src_clipped.left &= ~1;
420 rect_src_clipped.right &= ~1;
421 rect_src_clipped.top &= ~1;
422 rect_src_clipped.bottom &= ~1;
426 msg_Dbg(vd, "DirectXUpdateRects image_src_clipped"
427 " coords: %li,%li,%li,%li",
428 rect_src_clipped.left, rect_src_clipped.top,
429 rect_src_clipped.right, rect_src_clipped.bottom);
432 #ifdef MODULE_NAME_IS_directx
433 /* The destination coordinates need to be relative to the current
434 * directdraw primary surface (display) */
435 rect_dest_clipped.left -= sys->rect_display.left;
436 rect_dest_clipped.right -= sys->rect_display.left;
437 rect_dest_clipped.top -= sys->rect_display.top;
438 rect_dest_clipped.bottom -= sys->rect_display.top;
441 CommonChangeThumbnailClip(vd, true);
444 /* Signal the change in size/position */
445 sys->changes |= DX_POSITION_CHANGE;
448 #undef rect_src_clipped
450 #undef rect_dest_clipped
453 static int CommonControlSetFullscreen(vout_display_t *vd, bool is_fullscreen)
455 vout_display_sys_t *sys = vd->sys;
457 #ifdef MODULE_NAME_IS_direct3d
458 if (sys->use_desktop && is_fullscreen)
463 if (sys->parent_window)
464 return vout_window_SetFullScreen(sys->parent_window, is_fullscreen);
467 HWND hwnd = sys->hparent && sys->hfswnd ? sys->hfswnd : sys->hwnd;
469 /* Save the current windows placement/placement to restore
470 when fullscreen is over */
471 WINDOWPLACEMENT window_placement;
472 window_placement.length = sizeof(WINDOWPLACEMENT);
473 GetWindowPlacement(hwnd, &window_placement);
476 msg_Dbg(vd, "entering fullscreen mode");
478 /* Change window style, no borders and no title bar */
479 SetWindowLong(hwnd, GWL_STYLE, WS_CLIPCHILDREN | WS_VISIBLE);
485 ClientToScreen(sys->hwnd, &point);
486 GetClientRect(sys->hwnd, &rect);
487 SetWindowPos(hwnd, 0, point.x, point.y,
488 rect.right, rect.bottom,
489 SWP_NOZORDER|SWP_FRAMECHANGED);
491 /* Retrieve current window position so fullscreen will happen
492 *on the right screen */
493 HMONITOR hmon = MonitorFromWindow(sys->hparent,
494 MONITOR_DEFAULTTONEAREST);
496 mi.cbSize = sizeof(MONITORINFO);
497 if (GetMonitorInfo(hmon, &mi))
498 SetWindowPos(hwnd, 0,
501 mi.rcMonitor.right - mi.rcMonitor.left,
502 mi.rcMonitor.bottom - mi.rcMonitor.top,
503 SWP_NOZORDER|SWP_FRAMECHANGED);
506 /* Maximize non embedded window */
507 ShowWindow(hwnd, SW_SHOWMAXIMIZED);
511 /* Hide the previous window */
513 GetClientRect(hwnd, &rect);
514 SetParent(sys->hwnd, hwnd);
515 SetWindowPos(sys->hwnd, 0, 0, 0,
516 rect.right, rect.bottom,
517 SWP_NOZORDER|SWP_FRAMECHANGED);
520 HWND topLevelParent = GetParent(sys->hparent);
522 HWND topLevelParent = GetAncestor(sys->hparent, GA_ROOT);
524 ShowWindow(topLevelParent, SW_HIDE);
526 SetForegroundWindow(hwnd);
528 msg_Dbg(vd, "leaving fullscreen mode");
530 /* Change window style, no borders and no title bar */
531 SetWindowLong(hwnd, GWL_STYLE, EventThreadGetWindowStyle(sys->event));
535 GetClientRect(sys->hparent, &rect);
536 SetParent(sys->hwnd, sys->hparent);
537 SetWindowPos(sys->hwnd, 0, 0, 0,
538 rect.right, rect.bottom,
539 SWP_NOZORDER|SWP_FRAMECHANGED);
542 HWND topLevelParent = GetParent(sys->hparent);
544 HWND topLevelParent = GetAncestor(sys->hparent, GA_ROOT);
546 ShowWindow(topLevelParent, SW_SHOW);
547 SetForegroundWindow(sys->hparent);
548 ShowWindow(hwnd, SW_HIDE);
550 /* return to normal window for non embedded vout */
551 SetWindowPlacement(hwnd, &window_placement);
552 ShowWindow(hwnd, SW_SHOWNORMAL);
555 /* Make sure the mouse cursor is displayed */
556 EventThreadMouseShow(sys->event);
561 int CommonControl(vout_display_t *vd, int query, va_list args)
563 vout_display_sys_t *sys = vd->sys;
566 case VOUT_DISPLAY_CHANGE_DISPLAY_SIZE: /* const vout_display_cfg_t *p_cfg, int is_forced */
567 case VOUT_DISPLAY_CHANGE_DISPLAY_FILLED: /* const vout_display_cfg_t *p_cfg */
568 case VOUT_DISPLAY_CHANGE_ZOOM: /* const vout_display_cfg_t *p_cfg */
569 case VOUT_DISPLAY_CHANGE_SOURCE_ASPECT: /* const video_format_t *p_source */
570 case VOUT_DISPLAY_CHANGE_SOURCE_CROP: { /* const video_format_t *p_source */
571 const vout_display_cfg_t *cfg;
572 const video_format_t *source;
573 bool is_forced = true;
574 if (query == VOUT_DISPLAY_CHANGE_SOURCE_CROP ||
575 query == VOUT_DISPLAY_CHANGE_SOURCE_ASPECT) {
577 source = va_arg(args, const video_format_t *);
579 cfg = va_arg(args, const vout_display_cfg_t *);
580 source = &vd->source;
581 if (query == VOUT_DISPLAY_CHANGE_DISPLAY_SIZE)
582 is_forced = va_arg(args, int);
584 if (query == VOUT_DISPLAY_CHANGE_DISPLAY_SIZE && is_forced) {
585 /* Update dimensions */
586 if (sys->parent_window) {
587 vout_window_SetSize(sys->parent_window, cfg->display.width, cfg->display.height);
591 rect_window.left = 0;
592 rect_window.right = cfg->display.width;
593 rect_window.bottom = cfg->display.height;
594 AdjustWindowRect(&rect_window, EventThreadGetWindowStyle(sys->event), 0);
596 SetWindowPos(sys->hwnd, 0, 0, 0,
597 rect_window.right - rect_window.left,
598 rect_window.bottom - rect_window.top, SWP_NOMOVE);
601 UpdateRects(vd, cfg, source, is_forced);
604 case VOUT_DISPLAY_CHANGE_WINDOW_STATE: { /* unsigned state */
605 const unsigned state = va_arg(args, unsigned);
606 const bool is_on_top = (state & VOUT_WINDOW_STATE_ABOVE) != 0;
607 #ifdef MODULE_NAME_IS_direct3d
608 if (sys->use_desktop && is_on_top)
611 if (sys->parent_window) {
612 if (vout_window_SetState(sys->parent_window, state))
615 HMENU hMenu = GetSystemMenu(sys->hwnd, FALSE);
617 if (is_on_top && !(GetWindowLong(sys->hwnd, GWL_EXSTYLE) & WS_EX_TOPMOST)) {
618 CheckMenuItem(hMenu, IDM_TOGGLE_ON_TOP, MF_BYCOMMAND | MFS_CHECKED);
619 SetWindowPos(sys->hwnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE);
620 } else if (!is_on_top && (GetWindowLong(sys->hwnd, GWL_EXSTYLE) & WS_EX_TOPMOST)) {
621 CheckMenuItem(hMenu, IDM_TOGGLE_ON_TOP, MF_BYCOMMAND | MFS_UNCHECKED);
622 SetWindowPos(sys->hwnd, HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOSIZE|SWP_NOMOVE);
625 sys->is_on_top = is_on_top;
628 case VOUT_DISPLAY_CHANGE_FULLSCREEN: { /* const vout_display_cfg_t *p_cfg */
629 const vout_display_cfg_t *cfg = va_arg(args, const vout_display_cfg_t *);
630 return CommonControlSetFullscreen(vd, cfg->is_fullscreen);
633 case VOUT_DISPLAY_RESET_PICTURES:
634 case VOUT_DISPLAY_HIDE_MOUSE:
642 static void DisableScreensaver(vout_display_t *vd)
644 vout_display_sys_t *sys = vd->sys;
646 /* disable screensaver by temporarily changing system settings */
647 sys->i_spi_lowpowertimeout = 0;
648 sys->i_spi_powerofftimeout = 0;
649 sys->i_spi_screensavetimeout = 0;
650 if (var_GetBool(vd, "disable-screensaver")) {
651 msg_Dbg(vd, "disabling screen saver");
652 SystemParametersInfo(SPI_GETLOWPOWERTIMEOUT, 0,
653 &sys->i_spi_lowpowertimeout, 0);
654 if (0 != sys->i_spi_lowpowertimeout) {
655 SystemParametersInfo(SPI_SETLOWPOWERTIMEOUT, 0, NULL, 0);
657 SystemParametersInfo(SPI_GETPOWEROFFTIMEOUT, 0,
658 &sys->i_spi_powerofftimeout, 0);
659 if (0 != sys->i_spi_powerofftimeout) {
660 SystemParametersInfo(SPI_SETPOWEROFFTIMEOUT, 0, NULL, 0);
662 SystemParametersInfo(SPI_GETSCREENSAVETIMEOUT, 0,
663 &sys->i_spi_screensavetimeout, 0);
664 if (0 != sys->i_spi_screensavetimeout) {
665 SystemParametersInfo(SPI_SETSCREENSAVETIMEOUT, 0, NULL, 0);
670 static void RestoreScreensaver(vout_display_t *vd)
672 vout_display_sys_t *sys = vd->sys;
674 /* restore screensaver system settings */
675 if (0 != sys->i_spi_lowpowertimeout) {
676 SystemParametersInfo(SPI_SETLOWPOWERTIMEOUT,
677 sys->i_spi_lowpowertimeout, NULL, 0);
679 if (0 != sys->i_spi_powerofftimeout) {
680 SystemParametersInfo(SPI_SETPOWEROFFTIMEOUT,
681 sys->i_spi_powerofftimeout, NULL, 0);
683 if (0 != sys->i_spi_screensavetimeout) {
684 SystemParametersInfo(SPI_SETSCREENSAVETIMEOUT,
685 sys->i_spi_screensavetimeout, NULL, 0);