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;
218 * It updates a picture data/pitches.
220 int CommonUpdatePicture(picture_t *picture, picture_t **fallback,
221 uint8_t *data, unsigned pitch)
224 if (*fallback == NULL) {
225 *fallback = picture_NewFromFormat(&picture->format);
226 if (*fallback == NULL)
229 for (int n = 0; n < picture->i_planes; n++) {
230 const plane_t *src = &(*fallback)->p[n];
231 plane_t *dst = &picture->p[n];
232 dst->p_pixels = src->p_pixels;
233 dst->i_pitch = src->i_pitch;
234 dst->i_lines = src->i_lines;
238 /* fill in buffer info in first plane */
239 picture->p->p_pixels = data;
240 picture->p->i_pitch = pitch;
241 picture->p->i_lines = picture->format.i_height;
243 /* Fill chroma planes for planar YUV */
244 if (picture->format.i_chroma == VLC_CODEC_I420 ||
245 picture->format.i_chroma == VLC_CODEC_J420 ||
246 picture->format.i_chroma == VLC_CODEC_YV12) {
248 for (int n = 1; n < picture->i_planes; n++) {
249 const plane_t *o = &picture->p[n-1];
250 plane_t *p = &picture->p[n];
252 p->p_pixels = o->p_pixels + o->i_lines * o->i_pitch;
253 p->i_pitch = pitch / 2;
254 p->i_lines = picture->format.i_height / 2;
256 /* The dx/d3d buffer is always allocated as YV12 */
257 if (vlc_fourcc_AreUVPlanesSwapped(picture->format.i_chroma, VLC_CODEC_YV12)) {
258 uint8_t *p_tmp = picture->p[1].p_pixels;
259 picture->p[1].p_pixels = picture->p[2].p_pixels;
260 picture->p[2].p_pixels = p_tmp;
266 void AlignRect(RECT *r, int align_boundary, int align_size)
269 r->left = (r->left + align_boundary/2) & ~align_boundary;
271 r->right = ((r->right - r->left + align_size/2) & ~align_size) + r->left;
275 static void CommonChangeThumbnailClip(vout_display_t *vd, bool show)
278 vout_display_sys_t *sys = vd->sys;
280 /* Windows 7 taskbar thumbnail code */
281 OSVERSIONINFO winVer;
282 winVer.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
283 if (!GetVersionEx(&winVer) || winVer.dwMajorVersion <= 5)
288 LPTASKBARLIST3 taskbl;
289 if (S_OK == CoCreateInstance(&clsid_ITaskbarList,
290 NULL, CLSCTX_INPROC_SERVER,
293 taskbl->vt->HrInit(taskbl);
295 HWND hroot = GetAncestor(sys->hwnd,GA_ROOT);
299 GetWindowRect(sys->hvideownd, &video);
300 GetWindowRect(hroot, &parent);
301 relative.left = video.left - parent.left - 8;
302 relative.top = video.top - parent.top - 10;
304 relative.right = video.right - video.left + relative.left;
305 relative.bottom = video.bottom - video.top + relative.top - 25;
307 if (S_OK != taskbl->vt->SetThumbnailClip(taskbl, hroot,
308 show ? &relative : NULL))
309 msg_Err(vd, "SetThumbNailClip failed");
311 taskbl->vt->Release(taskbl);
317 /*****************************************************************************
318 * UpdateRects: update clipping rectangles
319 *****************************************************************************
320 * This function is called when the window position or size are changed, and
321 * its job is to update the source and destination RECTs used to display the
323 *****************************************************************************/
324 void UpdateRects(vout_display_t *vd,
325 const vout_display_cfg_t *cfg,
326 const video_format_t *source,
329 vout_display_sys_t *sys = vd->sys;
330 #define rect_src sys->rect_src
331 #define rect_src_clipped sys->rect_src_clipped
332 #define rect_dest sys->rect_dest
333 #define rect_dest_clipped sys->rect_dest_clipped
342 source = &vd->source;
344 /* Retrieve the window size */
345 GetClientRect(sys->hwnd, &rect);
347 /* Retrieve the window position */
348 point.x = point.y = 0;
349 ClientToScreen(sys->hwnd, &point);
351 /* If nothing changed, we can return */
354 EventThreadUpdateWindowPosition(sys->event, &has_moved, &is_resized,
356 rect.right, rect.bottom);
358 vout_display_SendEventDisplaySize(vd, rect.right, rect.bottom, cfg->is_fullscreen);
359 if (!is_forced && !has_moved && !is_resized )
362 /* Update the window position and size */
363 vout_display_cfg_t place_cfg = *cfg;
364 place_cfg.display.width = rect.right;
365 place_cfg.display.height = rect.bottom;
367 vout_display_place_t place;
368 vout_display_PlacePicture(&place, source, &place_cfg, false);
370 EventThreadUpdateSourceAndPlace(sys->event, source, &place);
371 #if defined(MODULE_NAME_IS_wingapi)
372 if (place.width != vd->fmt.i_width || place.height != vd->fmt.i_height)
373 vout_display_SendEventPicturesInvalid(vd);
377 SetWindowPos(sys->hvideownd, 0,
378 place.x, place.y, place.width, place.height,
379 SWP_NOCOPYBITS|SWP_NOZORDER|SWP_ASYNCWINDOWPOS);
381 /* Destination image position and dimensions */
382 #if defined(MODULE_NAME_IS_direct3d)
384 rect_dest.right = place.width;
386 rect_dest.bottom = place.height;
388 rect_dest.left = point.x + place.x;
389 rect_dest.right = rect_dest.left + place.width;
390 rect_dest.top = point.y + place.y;
391 rect_dest.bottom = rect_dest.top + place.height;
393 #ifdef MODULE_NAME_IS_directx
394 /* Apply overlay hardware constraints */
395 if (sys->use_overlay)
396 AlignRect(&rect_dest, sys->i_align_dest_boundary, sys->i_align_dest_size);
401 #if defined(MODULE_NAME_IS_directx) || defined(MODULE_NAME_IS_direct3d)
402 /* UpdateOverlay directdraw function doesn't automatically clip to the
403 * display size so we need to do it otherwise it will fail
404 * It is also needed for d3d to avoid exceding our surface size */
406 /* Clip the destination window */
407 if (!IntersectRect(&rect_dest_clipped, &rect_dest,
408 &sys->rect_display)) {
409 SetRectEmpty(&rect_src_clipped);
414 msg_Dbg(vd, "DirectXUpdateRects image_dst_clipped coords:"
416 rect_dest_clipped.left, rect_dest_clipped.top,
417 rect_dest_clipped.right, rect_dest_clipped.bottom);
422 /* AFAIK, there are no clipping constraints in Direct3D, OpenGL and GDI */
423 rect_dest_clipped = rect_dest;
427 /* the 2 following lines are to fix a bug when clicking on the desktop */
428 if ((rect_dest_clipped.right - rect_dest_clipped.left) == 0 ||
429 (rect_dest_clipped.bottom - rect_dest_clipped.top) == 0) {
430 SetRectEmpty(&rect_src_clipped);
434 /* src image dimensions */
437 rect_src.right = source->i_width;
438 rect_src.bottom = source->i_height;
440 /* Clip the source image */
441 rect_src_clipped.left = source->i_x_offset +
442 (rect_dest_clipped.left - rect_dest.left) *
443 source->i_visible_width / (rect_dest.right - rect_dest.left);
444 rect_src_clipped.right = source->i_x_offset +
445 source->i_visible_width -
446 (rect_dest.right - rect_dest_clipped.right) *
447 source->i_visible_width / (rect_dest.right - rect_dest.left);
448 rect_src_clipped.top = source->i_y_offset +
449 (rect_dest_clipped.top - rect_dest.top) *
450 source->i_visible_height / (rect_dest.bottom - rect_dest.top);
451 rect_src_clipped.bottom = source->i_y_offset +
452 source->i_visible_height -
453 (rect_dest.bottom - rect_dest_clipped.bottom) *
454 source->i_visible_height / (rect_dest.bottom - rect_dest.top);
456 #ifdef MODULE_NAME_IS_directx
457 /* Apply overlay hardware constraints */
458 if (sys->use_overlay)
459 AlignRect(&rect_src_clipped, sys->i_align_src_boundary, sys->i_align_src_size);
460 #elif defined(MODULE_NAME_IS_direct3d)
461 /* Needed at least with YUV content */
462 rect_src_clipped.left &= ~1;
463 rect_src_clipped.right &= ~1;
464 rect_src_clipped.top &= ~1;
465 rect_src_clipped.bottom &= ~1;
469 msg_Dbg(vd, "DirectXUpdateRects image_src_clipped"
470 " coords: %li,%li,%li,%li",
471 rect_src_clipped.left, rect_src_clipped.top,
472 rect_src_clipped.right, rect_src_clipped.bottom);
475 #ifdef MODULE_NAME_IS_directx
476 /* The destination coordinates need to be relative to the current
477 * directdraw primary surface (display) */
478 rect_dest_clipped.left -= sys->rect_display.left;
479 rect_dest_clipped.right -= sys->rect_display.left;
480 rect_dest_clipped.top -= sys->rect_display.top;
481 rect_dest_clipped.bottom -= sys->rect_display.top;
484 CommonChangeThumbnailClip(vd, true);
487 /* Signal the change in size/position */
488 sys->changes |= DX_POSITION_CHANGE;
491 #undef rect_src_clipped
493 #undef rect_dest_clipped
496 static int CommonControlSetFullscreen(vout_display_t *vd, bool is_fullscreen)
498 vout_display_sys_t *sys = vd->sys;
500 #ifdef MODULE_NAME_IS_direct3d
501 if (sys->use_desktop && is_fullscreen)
506 if (sys->parent_window)
507 return vout_window_SetFullScreen(sys->parent_window, is_fullscreen);
510 HWND hwnd = sys->hparent && sys->hfswnd ? sys->hfswnd : sys->hwnd;
512 /* Save the current windows placement/placement to restore
513 when fullscreen is over */
514 WINDOWPLACEMENT window_placement;
515 window_placement.length = sizeof(WINDOWPLACEMENT);
516 GetWindowPlacement(hwnd, &window_placement);
519 msg_Dbg(vd, "entering fullscreen mode");
521 /* Change window style, no borders and no title bar */
522 SetWindowLong(hwnd, GWL_STYLE, WS_CLIPCHILDREN | WS_VISIBLE);
528 ClientToScreen(sys->hwnd, &point);
529 GetClientRect(sys->hwnd, &rect);
530 SetWindowPos(hwnd, 0, point.x, point.y,
531 rect.right, rect.bottom,
532 SWP_NOZORDER|SWP_FRAMECHANGED);
534 /* Retrieve current window position so fullscreen will happen
535 *on the right screen */
536 HMONITOR hmon = MonitorFromWindow(sys->hparent,
537 MONITOR_DEFAULTTONEAREST);
539 mi.cbSize = sizeof(MONITORINFO);
540 if (GetMonitorInfo(hmon, &mi))
541 SetWindowPos(hwnd, 0,
544 mi.rcMonitor.right - mi.rcMonitor.left,
545 mi.rcMonitor.bottom - mi.rcMonitor.top,
546 SWP_NOZORDER|SWP_FRAMECHANGED);
549 /* Maximize non embedded window */
550 ShowWindow(hwnd, SW_SHOWMAXIMIZED);
554 /* Hide the previous window */
556 GetClientRect(hwnd, &rect);
557 SetParent(sys->hwnd, hwnd);
558 SetWindowPos(sys->hwnd, 0, 0, 0,
559 rect.right, rect.bottom,
560 SWP_NOZORDER|SWP_FRAMECHANGED);
563 HWND topLevelParent = GetParent(sys->hparent);
565 HWND topLevelParent = GetAncestor(sys->hparent, GA_ROOT);
567 ShowWindow(topLevelParent, SW_HIDE);
569 SetForegroundWindow(hwnd);
571 msg_Dbg(vd, "leaving fullscreen mode");
573 /* Change window style, no borders and no title bar */
574 SetWindowLong(hwnd, GWL_STYLE, EventThreadGetWindowStyle(sys->event));
578 GetClientRect(sys->hparent, &rect);
579 SetParent(sys->hwnd, sys->hparent);
580 SetWindowPos(sys->hwnd, 0, 0, 0,
581 rect.right, rect.bottom,
582 SWP_NOZORDER|SWP_FRAMECHANGED);
585 HWND topLevelParent = GetParent(sys->hparent);
587 HWND topLevelParent = GetAncestor(sys->hparent, GA_ROOT);
589 ShowWindow(topLevelParent, SW_SHOW);
590 SetForegroundWindow(sys->hparent);
591 ShowWindow(hwnd, SW_HIDE);
593 /* return to normal window for non embedded vout */
594 SetWindowPlacement(hwnd, &window_placement);
595 ShowWindow(hwnd, SW_SHOWNORMAL);
601 int CommonControl(vout_display_t *vd, int query, va_list args)
603 vout_display_sys_t *sys = vd->sys;
606 case VOUT_DISPLAY_CHANGE_DISPLAY_SIZE: /* const vout_display_cfg_t *p_cfg, int is_forced */
607 case VOUT_DISPLAY_CHANGE_DISPLAY_FILLED: /* const vout_display_cfg_t *p_cfg */
608 case VOUT_DISPLAY_CHANGE_ZOOM: /* const vout_display_cfg_t *p_cfg */
609 case VOUT_DISPLAY_CHANGE_SOURCE_ASPECT: /* const video_format_t *p_source */
610 case VOUT_DISPLAY_CHANGE_SOURCE_CROP: { /* const video_format_t *p_source */
611 const vout_display_cfg_t *cfg;
612 const video_format_t *source;
613 bool is_forced = true;
614 if (query == VOUT_DISPLAY_CHANGE_SOURCE_CROP ||
615 query == VOUT_DISPLAY_CHANGE_SOURCE_ASPECT) {
617 source = va_arg(args, const video_format_t *);
619 cfg = va_arg(args, const vout_display_cfg_t *);
620 source = &vd->source;
621 if (query == VOUT_DISPLAY_CHANGE_DISPLAY_SIZE)
622 is_forced = va_arg(args, int);
624 if (query == VOUT_DISPLAY_CHANGE_DISPLAY_SIZE && is_forced) {
625 /* Update dimensions */
626 if (sys->parent_window) {
627 vout_window_SetSize(sys->parent_window, cfg->display.width, cfg->display.height);
631 rect_window.left = 0;
632 rect_window.right = cfg->display.width;
633 rect_window.bottom = cfg->display.height;
634 AdjustWindowRect(&rect_window, EventThreadGetWindowStyle(sys->event), 0);
636 SetWindowPos(sys->hwnd, 0, 0, 0,
637 rect_window.right - rect_window.left,
638 rect_window.bottom - rect_window.top, SWP_NOMOVE);
641 UpdateRects(vd, cfg, source, is_forced);
644 case VOUT_DISPLAY_CHANGE_WINDOW_STATE: { /* unsigned state */
645 const unsigned state = va_arg(args, unsigned);
646 const bool is_on_top = (state & VOUT_WINDOW_STATE_ABOVE) != 0;
647 #ifdef MODULE_NAME_IS_direct3d
648 if (sys->use_desktop && is_on_top)
651 if (sys->parent_window) {
652 if (vout_window_SetState(sys->parent_window, state))
655 HMENU hMenu = GetSystemMenu(sys->hwnd, FALSE);
657 if (is_on_top && !(GetWindowLong(sys->hwnd, GWL_EXSTYLE) & WS_EX_TOPMOST)) {
658 CheckMenuItem(hMenu, IDM_TOGGLE_ON_TOP, MF_BYCOMMAND | MFS_CHECKED);
659 SetWindowPos(sys->hwnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE);
660 } else if (!is_on_top && (GetWindowLong(sys->hwnd, GWL_EXSTYLE) & WS_EX_TOPMOST)) {
661 CheckMenuItem(hMenu, IDM_TOGGLE_ON_TOP, MF_BYCOMMAND | MFS_UNCHECKED);
662 SetWindowPos(sys->hwnd, HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOSIZE|SWP_NOMOVE);
665 sys->is_on_top = is_on_top;
668 case VOUT_DISPLAY_CHANGE_FULLSCREEN: { /* const vout_display_cfg_t *p_cfg */
669 const vout_display_cfg_t *cfg = va_arg(args, const vout_display_cfg_t *);
670 if (CommonControlSetFullscreen(vd, cfg->is_fullscreen))
672 UpdateRects(vd, NULL, NULL, false);
676 case VOUT_DISPLAY_HIDE_MOUSE:
677 EventThreadMouseHide(sys->event);
679 case VOUT_DISPLAY_RESET_PICTURES:
687 static void DisableScreensaver(vout_display_t *vd)
689 vout_display_sys_t *sys = vd->sys;
691 /* disable screensaver by temporarily changing system settings */
692 sys->i_spi_lowpowertimeout = 0;
693 sys->i_spi_powerofftimeout = 0;
694 sys->i_spi_screensavetimeout = 0;
695 if (var_GetBool(vd, "disable-screensaver")) {
696 msg_Dbg(vd, "disabling screen saver");
697 SystemParametersInfo(SPI_GETLOWPOWERTIMEOUT, 0,
698 &sys->i_spi_lowpowertimeout, 0);
699 if (0 != sys->i_spi_lowpowertimeout) {
700 SystemParametersInfo(SPI_SETLOWPOWERTIMEOUT, 0, NULL, 0);
702 SystemParametersInfo(SPI_GETPOWEROFFTIMEOUT, 0,
703 &sys->i_spi_powerofftimeout, 0);
704 if (0 != sys->i_spi_powerofftimeout) {
705 SystemParametersInfo(SPI_SETPOWEROFFTIMEOUT, 0, NULL, 0);
707 SystemParametersInfo(SPI_GETSCREENSAVETIMEOUT, 0,
708 &sys->i_spi_screensavetimeout, 0);
709 if (0 != sys->i_spi_screensavetimeout) {
710 SystemParametersInfo(SPI_SETSCREENSAVETIMEOUT, 0, NULL, 0);
715 static void RestoreScreensaver(vout_display_t *vd)
717 vout_display_sys_t *sys = vd->sys;
719 /* restore screensaver system settings */
720 if (0 != sys->i_spi_lowpowertimeout) {
721 SystemParametersInfo(SPI_SETLOWPOWERTIMEOUT,
722 sys->i_spi_lowpowertimeout, NULL, 0);
724 if (0 != sys->i_spi_powerofftimeout) {
725 SystemParametersInfo(SPI_SETPOWEROFFTIMEOUT,
726 sys->i_spi_powerofftimeout, NULL, 0);
728 if (0 != sys->i_spi_screensavetimeout) {
729 SystemParametersInfo(SPI_SETSCREENSAVETIMEOUT,
730 sys->i_spi_screensavetimeout, NULL, 0);