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 int CommonControlSetFullscreen(vout_display_t *, bool is_fullscreen);
65 static void DisableScreensaver(vout_display_t *);
66 static void RestoreScreensaver(vout_display_t *);
69 int CommonInit(vout_display_t *vd)
71 vout_display_sys_t *sys = vd->sys;
74 sys->hvideownd = NULL;
78 SetRectEmpty(&sys->rect_display);
79 SetRectEmpty(&sys->rect_parent);
80 sys->is_first_display = true;
81 sys->is_on_top = false;
83 var_Create(vd, "video-title", VLC_VAR_STRING | VLC_VAR_DOINHERIT);
84 var_Create(vd, "video-deco", VLC_VAR_BOOL | VLC_VAR_DOINHERIT);
86 /* FIXME remove mouse hide from msw */
87 var_Create(vd, "mouse-hide-timeout", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
90 sys->event = EventThreadCreate(vd);
95 memset(&cfg, 0, sizeof(cfg));
96 #ifdef MODULE_NAME_IS_direct3d
97 cfg.use_desktop = sys->use_desktop;
99 #ifdef MODULE_NAME_IS_directx
100 cfg.use_overlay = sys->use_overlay;
102 cfg.win.type = VOUT_WINDOW_TYPE_HWND;
105 cfg.win.width = vd->cfg->display.width;
106 cfg.win.height = vd->cfg->display.height;
109 if (EventThreadStart(sys->event, &hwnd, &cfg))
112 sys->parent_window = hwnd.parent_window;
113 sys->hparent = hwnd.hparent;
114 sys->hwnd = hwnd.hwnd;
115 sys->hvideownd = hwnd.hvideownd;
116 sys->hfswnd = hwnd.hfswnd;
118 if (vd->cfg->is_fullscreen) {
119 if (CommonControlSetFullscreen(vd, true))
120 vout_display_SendEventFullscreen(vd, false);
123 /* Why not with glwin32 */
124 #if !defined(UNDER_CE) && !defined(MODULE_NAME_IS_glwin32)
125 var_Create(vd, "disable-screensaver", VLC_VAR_BOOL | VLC_VAR_DOINHERIT);
126 DisableScreensaver (vd);
133 void CommonClean(vout_display_t *vd)
135 vout_display_sys_t *sys = vd->sys;
138 EventThreadStop(sys->event);
139 EventThreadDestroy(sys->event);
142 #if !defined(UNDER_CE) && !defined(MODULE_NAME_IS_glwin32)
143 RestoreScreensaver(vd);
147 void CommonManage(vout_display_t *vd)
149 vout_display_sys_t *sys = vd->sys;
151 /* We used to call the Win32 PeekMessage function here to read the window
152 * messages. But since window can stay blocked into this function for a
153 * long time (for example when you move your window on the screen), I
154 * decided to isolate PeekMessage in another thread. */
156 /* If we do not control our window, we check for geometry changes
157 * ourselves because the parent might not send us its events. */
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 /* FIXME I find such #ifdef quite weirds. Are they really needed ? */
172 #if defined(MODULE_NAME_IS_direct3d) || defined(MODULE_NAME_IS_wingdi) || defined(MODULE_NAME_IS_wingapi)
173 SetWindowPos(sys->hwnd, 0, 0, 0,
174 rect_parent.right - rect_parent.left,
175 rect_parent.bottom - rect_parent.top,
177 UpdateRects(vd, NULL, NULL, true);
179 /* This one is to force the update even if only
180 * the position has changed */
181 SetWindowPos(sys->hwnd, 0, 1, 1,
182 rect_parent.right - rect_parent.left,
183 rect_parent.bottom - rect_parent.top, 0);
185 SetWindowPos(sys->hwnd, 0, 0, 0,
186 rect_parent.right - rect_parent.left,
187 rect_parent.bottom - rect_parent.top, 0);
194 if (EventThreadGetAndResetHasMoved(sys->event))
195 UpdateRects(vd, NULL, NULL, false);
198 EventThreadMouseAutoHide(sys->event);
202 * It ensures that the video window is shown after the first picture
205 void CommonDisplay(vout_display_t *vd)
207 vout_display_sys_t *sys = vd->sys;
209 if (!sys->is_first_display)
212 /* Video window is initially hidden, show it now since we got a
215 SetWindowPos(sys->hvideownd, 0, 0, 0, 0, 0,
222 sys->is_first_display = false;
225 void AlignRect(RECT *r, int align_boundary, int align_size)
228 r->left = (r->left + align_boundary/2) & ~align_boundary;
230 r->right = ((r->right - r->left + align_size/2) & ~align_size) + r->left;
233 /*****************************************************************************
234 * UpdateRects: update clipping rectangles
235 *****************************************************************************
236 * This function is called when the window position or size are changed, and
237 * its job is to update the source and destination RECTs used to display the
239 *****************************************************************************/
240 void UpdateRects(vout_display_t *vd,
241 const vout_display_cfg_t *cfg,
242 const video_format_t *source,
245 vout_display_sys_t *sys = vd->sys;
246 #define rect_src sys->rect_src
247 #define rect_src_clipped sys->rect_src_clipped
248 #define rect_dest sys->rect_dest
249 #define rect_dest_clipped sys->rect_dest_clipped
258 source = &vd->source;
260 /* Retrieve the window size */
261 GetClientRect(sys->hwnd, &rect);
263 /* Retrieve the window position */
264 point.x = point.y = 0;
265 ClientToScreen(sys->hwnd, &point);
267 /* If nothing changed, we can return */
270 EventThreadUpdateWindowPosition(sys->event, &has_moved, &is_resized,
272 rect.right, rect.bottom);
274 vout_display_SendEventDisplaySize(vd, rect.right, rect.bottom, cfg->is_fullscreen);
275 if (!is_forced && !has_moved && !is_resized )
278 /* Update the window position and size */
279 vout_display_cfg_t place_cfg = *cfg;
280 place_cfg.display.width = rect.right;
281 place_cfg.display.height = rect.bottom;
283 vout_display_place_t place;
284 vout_display_PlacePicture(&place, source, &place_cfg, true);
286 EventThreadUpdateSourceAndPlace(sys->event, source, &place);
287 #if defined(MODULE_NAME_IS_wingapi)
288 if (place.width != vd->fmt.i_width || place.height != vd->fmt.i_height)
289 vout_display_SendEventPicturesInvalid(vd);
293 SetWindowPos(sys->hvideownd, 0,
294 place.x, place.y, place.width, place.height,
295 SWP_NOCOPYBITS|SWP_NOZORDER|SWP_ASYNCWINDOWPOS);
297 /* Destination image position and dimensions */
298 #if defined(MODULE_NAME_IS_direct3d)
300 rect_dest.right = place.width;
302 rect_dest.bottom = place.height;
304 rect_dest.left = point.x + place.x;
305 rect_dest.right = rect_dest.left + place.width;
306 rect_dest.top = point.y + place.y;
307 rect_dest.bottom = rect_dest.top + place.height;
309 #ifdef MODULE_NAME_IS_directx
310 /* Apply overlay hardware constraints */
311 if (sys->use_overlay)
312 AlignRect(&rect_dest, sys->i_align_dest_boundary, sys->i_align_dest_size);
317 #if defined(MODULE_NAME_IS_directx) || defined(MODULE_NAME_IS_direct3d)
318 /* UpdateOverlay directdraw function doesn't automatically clip to the
319 * display size so we need to do it otherwise it will fail
320 * It is also needed for d3d to avoid exceding our surface size */
322 /* Clip the destination window */
323 if (!IntersectRect(&rect_dest_clipped, &rect_dest,
324 &sys->rect_display)) {
325 SetRectEmpty(&rect_src_clipped);
330 msg_Dbg(vd, "DirectXUpdateRects image_dst_clipped coords:"
332 rect_dest_clipped.left, rect_dest_clipped.top,
333 rect_dest_clipped.right, rect_dest_clipped.bottom);
338 /* AFAIK, there are no clipping constraints in Direct3D, OpenGL and GDI */
339 rect_dest_clipped = rect_dest;
343 /* the 2 following lines are to fix a bug when clicking on the desktop */
344 if ((rect_dest_clipped.right - rect_dest_clipped.left) == 0 ||
345 (rect_dest_clipped.bottom - rect_dest_clipped.top) == 0) {
346 SetRectEmpty(&rect_src_clipped);
350 /* src image dimensions */
353 rect_src.right = source->i_width;
354 rect_src.bottom = source->i_height;
356 /* Clip the source image */
357 rect_src_clipped.left = source->i_x_offset +
358 (rect_dest_clipped.left - rect_dest.left) *
359 source->i_visible_width / (rect_dest.right - rect_dest.left);
360 rect_src_clipped.right = source->i_x_offset +
361 source->i_visible_width -
362 (rect_dest.right - rect_dest_clipped.right) *
363 source->i_visible_width / (rect_dest.right - rect_dest.left);
364 rect_src_clipped.top = source->i_y_offset +
365 (rect_dest_clipped.top - rect_dest.top) *
366 source->i_visible_height / (rect_dest.bottom - rect_dest.top);
367 rect_src_clipped.bottom = source->i_y_offset +
368 source->i_visible_height -
369 (rect_dest.bottom - rect_dest_clipped.bottom) *
370 source->i_visible_height / (rect_dest.bottom - rect_dest.top);
372 #ifdef MODULE_NAME_IS_directx
373 /* Apply overlay hardware constraints */
374 if (sys->use_overlay)
375 AlignRect(&rect_src_clipped, sys->i_align_src_boundary, sys->i_align_src_size);
376 #elif defined(MODULE_NAME_IS_direct3d)
377 /* Needed at least with YUV content */
378 rect_src_clipped.left &= ~1;
379 rect_src_clipped.right &= ~1;
380 rect_src_clipped.top &= ~1;
381 rect_src_clipped.bottom &= ~1;
385 msg_Dbg(vd, "DirectXUpdateRects image_src_clipped"
386 " coords: %li,%li,%li,%li",
387 rect_src_clipped.left, rect_src_clipped.top,
388 rect_src_clipped.right, rect_src_clipped.bottom);
391 #ifdef MODULE_NAME_IS_directx
392 /* The destination coordinates need to be relative to the current
393 * directdraw primary surface (display) */
394 rect_dest_clipped.left -= sys->rect_display.left;
395 rect_dest_clipped.right -= sys->rect_display.left;
396 rect_dest_clipped.top -= sys->rect_display.top;
397 rect_dest_clipped.bottom -= sys->rect_display.top;
401 /* Windows 7 taskbar thumbnail code */
402 LPTASKBARLIST3 taskbl;
403 OSVERSIONINFO winVer;
404 winVer.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
405 if (GetVersionEx(&winVer) && winVer.dwMajorVersion > 5) {
408 if (S_OK == CoCreateInstance(&clsid_ITaskbarList,
409 NULL, CLSCTX_INPROC_SERVER,
412 RECT rect_video, rect_parent, rect_relative;
413 HWND hroot = GetAncestor(sys->hwnd,GA_ROOT);
415 taskbl->vt->HrInit(taskbl);
416 GetWindowRect(sys->hvideownd, &rect_video);
417 GetWindowRect(hroot, &rect_parent);
418 rect_relative.left = rect_video.left - rect_parent.left - 8;
419 rect_relative.right = rect_video.right - rect_video.left + rect_relative.left;
420 rect_relative.top = rect_video.top - rect_parent.top - 10;
421 rect_relative.bottom = rect_video.bottom - rect_video.top + rect_relative.top - 25;
423 if (S_OK != taskbl->vt->SetThumbnailClip(taskbl, hroot, &rect_relative))
424 msg_Err(vd, "SetThumbNailClip failed");
426 taskbl->vt->Release(taskbl);
431 /* Signal the change in size/position */
432 sys->changes |= DX_POSITION_CHANGE;
435 #undef rect_src_clipped
437 #undef rect_dest_clipped
440 static int CommonControlSetFullscreen(vout_display_t *vd, bool is_fullscreen)
442 vout_display_sys_t *sys = vd->sys;
444 #ifdef MODULE_NAME_IS_direct3d
445 if (sys->use_desktop && is_fullscreen)
450 if (sys->parent_window)
451 return vout_window_SetFullScreen(sys->parent_window, is_fullscreen);
454 HWND hwnd = sys->hparent && sys->hfswnd ? sys->hfswnd : sys->hwnd;
456 /* Save the current windows placement/placement to restore
457 when fullscreen is over */
458 WINDOWPLACEMENT window_placement;
459 window_placement.length = sizeof(WINDOWPLACEMENT);
460 GetWindowPlacement(hwnd, &window_placement);
463 msg_Dbg(vd, "entering fullscreen mode");
465 /* Change window style, no borders and no title bar */
466 SetWindowLong(hwnd, GWL_STYLE, WS_CLIPCHILDREN | WS_VISIBLE);
472 ClientToScreen(sys->hwnd, &point);
473 GetClientRect(sys->hwnd, &rect);
474 SetWindowPos(hwnd, 0, point.x, point.y,
475 rect.right, rect.bottom,
476 SWP_NOZORDER|SWP_FRAMECHANGED);
478 /* Retrieve current window position so fullscreen will happen
479 *on the right screen */
480 HMONITOR hmon = MonitorFromWindow(sys->hparent,
481 MONITOR_DEFAULTTONEAREST);
483 mi.cbSize = sizeof(MONITORINFO);
484 if (GetMonitorInfo(hmon, &mi))
485 SetWindowPos(hwnd, 0,
488 mi.rcMonitor.right - mi.rcMonitor.left,
489 mi.rcMonitor.bottom - mi.rcMonitor.top,
490 SWP_NOZORDER|SWP_FRAMECHANGED);
493 /* Maximize non embedded window */
494 ShowWindow(hwnd, SW_SHOWMAXIMIZED);
498 /* Hide the previous window */
500 GetClientRect(hwnd, &rect);
501 SetParent(sys->hwnd, hwnd);
502 SetWindowPos(sys->hwnd, 0, 0, 0,
503 rect.right, rect.bottom,
504 SWP_NOZORDER|SWP_FRAMECHANGED);
507 HWND topLevelParent = GetParent(sys->hparent);
509 HWND topLevelParent = GetAncestor(sys->hparent, GA_ROOT);
511 ShowWindow(topLevelParent, SW_HIDE);
513 SetForegroundWindow(hwnd);
515 msg_Dbg(vd, "leaving fullscreen mode");
517 /* Change window style, no borders and no title bar */
518 SetWindowLong(hwnd, GWL_STYLE, EventThreadGetWindowStyle(sys->event));
522 GetClientRect(sys->hparent, &rect);
523 SetParent(sys->hwnd, sys->hparent);
524 SetWindowPos(sys->hwnd, 0, 0, 0,
525 rect.right, rect.bottom,
526 SWP_NOZORDER|SWP_FRAMECHANGED);
529 HWND topLevelParent = GetParent(sys->hparent);
531 HWND topLevelParent = GetAncestor(sys->hparent, GA_ROOT);
533 ShowWindow(topLevelParent, SW_SHOW);
534 SetForegroundWindow(sys->hparent);
535 ShowWindow(hwnd, SW_HIDE);
537 /* return to normal window for non embedded vout */
538 SetWindowPlacement(hwnd, &window_placement);
539 ShowWindow(hwnd, SW_SHOWNORMAL);
542 /* Make sure the mouse cursor is displayed */
543 EventThreadMouseShow(sys->event);
548 int CommonControl(vout_display_t *vd, int query, va_list args)
550 vout_display_sys_t *sys = vd->sys;
553 case VOUT_DISPLAY_CHANGE_DISPLAY_SIZE: /* const vout_display_cfg_t *p_cfg, int is_forced */
554 case VOUT_DISPLAY_CHANGE_DISPLAY_FILLED: /* const vout_display_cfg_t *p_cfg */
555 case VOUT_DISPLAY_CHANGE_ZOOM: /* const vout_display_cfg_t *p_cfg */
556 case VOUT_DISPLAY_CHANGE_SOURCE_ASPECT: /* const video_format_t *p_source */
557 case VOUT_DISPLAY_CHANGE_SOURCE_CROP: { /* const video_format_t *p_source */
558 const vout_display_cfg_t *cfg;
559 const video_format_t *source;
560 bool is_forced = true;
561 if (query == VOUT_DISPLAY_CHANGE_SOURCE_CROP ||
562 query == VOUT_DISPLAY_CHANGE_SOURCE_ASPECT) {
564 source = va_arg(args, const video_format_t *);
566 cfg = va_arg(args, const vout_display_cfg_t *);
567 source = &vd->source;
568 if (query == VOUT_DISPLAY_CHANGE_DISPLAY_SIZE)
569 is_forced = va_arg(args, int);
571 if (query == VOUT_DISPLAY_CHANGE_DISPLAY_SIZE && is_forced) {
572 /* Update dimensions */
573 if (sys->parent_window) {
574 vout_window_SetSize(sys->parent_window, cfg->display.width, cfg->display.height);
578 rect_window.left = 0;
579 rect_window.right = cfg->display.width;
580 rect_window.bottom = cfg->display.height;
581 AdjustWindowRect(&rect_window, EventThreadGetWindowStyle(sys->event), 0);
583 SetWindowPos(sys->hwnd, 0, 0, 0,
584 rect_window.right - rect_window.left,
585 rect_window.bottom - rect_window.top, SWP_NOMOVE);
588 UpdateRects(vd, cfg, source, is_forced);
591 case VOUT_DISPLAY_CHANGE_WINDOW_STATE: { /* unsigned state */
592 const unsigned state = va_arg(args, unsigned);
593 const bool is_on_top = (state & VOUT_WINDOW_STATE_ABOVE) != 0;
594 #ifdef MODULE_NAME_IS_direct3d
595 if (sys->use_desktop && is_on_top)
598 if (sys->parent_window) {
599 if (vout_window_SetState(sys->parent_window, state))
602 HMENU hMenu = GetSystemMenu(sys->hwnd, FALSE);
604 if (is_on_top && !(GetWindowLong(sys->hwnd, GWL_EXSTYLE) & WS_EX_TOPMOST)) {
605 CheckMenuItem(hMenu, IDM_TOGGLE_ON_TOP, MF_BYCOMMAND | MFS_CHECKED);
606 SetWindowPos(sys->hwnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE);
607 } else if (!is_on_top && (GetWindowLong(sys->hwnd, GWL_EXSTYLE) & WS_EX_TOPMOST)) {
608 CheckMenuItem(hMenu, IDM_TOGGLE_ON_TOP, MF_BYCOMMAND | MFS_UNCHECKED);
609 SetWindowPos(sys->hwnd, HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOSIZE|SWP_NOMOVE);
612 sys->is_on_top = is_on_top;
615 case VOUT_DISPLAY_CHANGE_FULLSCREEN: { /* const vout_display_cfg_t *p_cfg */
616 const vout_display_cfg_t *cfg = va_arg(args, const vout_display_cfg_t *);
617 return CommonControlSetFullscreen(vd, cfg->is_fullscreen);
620 case VOUT_DISPLAY_RESET_PICTURES:
621 case VOUT_DISPLAY_HIDE_MOUSE:
629 static void DisableScreensaver(vout_display_t *vd)
631 vout_display_sys_t *sys = vd->sys;
633 /* disable screensaver by temporarily changing system settings */
634 sys->i_spi_lowpowertimeout = 0;
635 sys->i_spi_powerofftimeout = 0;
636 sys->i_spi_screensavetimeout = 0;
637 if (var_GetBool(vd, "disable-screensaver")) {
638 msg_Dbg(vd, "disabling screen saver");
639 SystemParametersInfo(SPI_GETLOWPOWERTIMEOUT, 0,
640 &sys->i_spi_lowpowertimeout, 0);
641 if (0 != sys->i_spi_lowpowertimeout) {
642 SystemParametersInfo(SPI_SETLOWPOWERTIMEOUT, 0, NULL, 0);
644 SystemParametersInfo(SPI_GETPOWEROFFTIMEOUT, 0,
645 &sys->i_spi_powerofftimeout, 0);
646 if (0 != sys->i_spi_powerofftimeout) {
647 SystemParametersInfo(SPI_SETPOWEROFFTIMEOUT, 0, NULL, 0);
649 SystemParametersInfo(SPI_GETSCREENSAVETIMEOUT, 0,
650 &sys->i_spi_screensavetimeout, 0);
651 if (0 != sys->i_spi_screensavetimeout) {
652 SystemParametersInfo(SPI_SETSCREENSAVETIMEOUT, 0, NULL, 0);
657 static void RestoreScreensaver(vout_display_t *vd)
659 vout_display_sys_t *sys = vd->sys;
661 /* restore screensaver system settings */
662 if (0 != sys->i_spi_lowpowertimeout) {
663 SystemParametersInfo(SPI_SETLOWPOWERTIMEOUT,
664 sys->i_spi_lowpowertimeout, NULL, 0);
666 if (0 != sys->i_spi_powerofftimeout) {
667 SystemParametersInfo(SPI_SETPOWEROFFTIMEOUT,
668 sys->i_spi_powerofftimeout, NULL, 0);
670 if (0 != sys->i_spi_screensavetimeout) {
671 SystemParametersInfo(SPI_SETSCREENSAVETIMEOUT,
672 sys->i_spi_screensavetimeout, NULL, 0);