1 /*****************************************************************************
2 * common.c: Windows video output common code
3 *****************************************************************************
4 * Copyright (C) 2001-2009 VLC authors and VideoLAN
7 * Authors: Gildas Bazin <gbazin@videolan.org>
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU Lesser General Public License as published by
11 * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public License
20 * along with this program; if not, write to the Free Software Foundation,
21 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 /*****************************************************************************
25 * Preamble: This file contains the functions related to the init of the vout
26 * structure, the common display code, the screensaver, but not the
27 * events and the Window Creation (events.c)
28 *****************************************************************************/
34 #include <vlc_common.h>
35 #include <vlc_vout_display.h>
42 #include <vlc_windows_interfaces.h>
44 static void CommonChangeThumbnailClip(vout_display_t *, bool show);
45 static int CommonControlSetFullscreen(vout_display_t *, bool is_fullscreen);
47 static void DisableScreensaver(vout_display_t *);
48 static void RestoreScreensaver(vout_display_t *);
51 int CommonInit(vout_display_t *vd)
53 vout_display_sys_t *sys = vd->sys;
56 sys->hvideownd = NULL;
60 SetRectEmpty(&sys->rect_display);
61 SetRectEmpty(&sys->rect_parent);
62 sys->is_first_display = true;
63 sys->is_on_top = false;
65 var_Create(vd, "video-deco", VLC_VAR_BOOL | VLC_VAR_DOINHERIT);
66 var_Create(vd, "disable-screensaver", VLC_VAR_BOOL | VLC_VAR_DOINHERIT);
69 sys->event = EventThreadCreate(vd);
74 memset(&cfg, 0, sizeof(cfg));
75 #ifdef MODULE_NAME_IS_direct3d
76 cfg.use_desktop = sys->use_desktop;
78 #ifdef MODULE_NAME_IS_directdraw
79 cfg.use_overlay = sys->use_overlay;
81 cfg.win.type = VOUT_WINDOW_TYPE_HWND;
82 cfg.win.x = var_InheritInteger(vd, "video-x");
83 cfg.win.y = var_InheritInteger(vd, "video-y");
84 cfg.win.width = vd->cfg->display.width;
85 cfg.win.height = vd->cfg->display.height;
88 if (EventThreadStart(sys->event, &hwnd, &cfg))
91 sys->parent_window = hwnd.parent_window;
92 sys->hparent = hwnd.hparent;
93 sys->hwnd = hwnd.hwnd;
94 sys->hvideownd = hwnd.hvideownd;
95 sys->hfswnd = hwnd.hfswnd;
97 if (vd->cfg->is_fullscreen) {
98 if (CommonControlSetFullscreen(vd, true))
99 vout_display_SendEventFullscreen(vd, false);
102 DisableScreensaver (vd);
108 void CommonClean(vout_display_t *vd)
110 vout_display_sys_t *sys = vd->sys;
113 CommonChangeThumbnailClip(vd, false);
114 EventThreadStop(sys->event);
115 EventThreadDestroy(sys->event);
118 RestoreScreensaver(vd);
121 void CommonManage(vout_display_t *vd)
123 vout_display_sys_t *sys = vd->sys;
125 /* We used to call the Win32 PeekMessage function here to read the window
126 * messages. But since window can stay blocked into this function for a
127 * long time (for example when you move your window on the screen), I
128 * decided to isolate PeekMessage in another thread. */
130 /* If we do not control our window, we check for geometry changes
131 * ourselves because the parent might not send us its events. */
136 /* Check if the parent window has resized or moved */
137 GetClientRect(sys->hparent, &rect_parent);
138 point.x = point.y = 0;
139 ClientToScreen(sys->hparent, &point);
140 OffsetRect(&rect_parent, point.x, point.y);
142 if (!EqualRect(&rect_parent, &sys->rect_parent)) {
143 sys->rect_parent = rect_parent;
145 /* This code deals with both resize and move
147 * For most drivers(direct3d, gdi, opengl), move is never
148 * an issue. The surface automatically gets moved together
149 * with the associated window (hvideownd)
151 * For directx, it is still important to call UpdateRects
152 * on a move of the parent window, even if no resize occurred
154 SetWindowPos(sys->hwnd, 0, 0, 0,
155 rect_parent.right - rect_parent.left,
156 rect_parent.bottom - rect_parent.top,
159 UpdateRects(vd, NULL, NULL, true);
163 /* HasMoved means here resize or move */
164 if (EventThreadGetAndResetHasMoved(sys->event))
165 UpdateRects(vd, NULL, NULL, false);
169 * It ensures that the video window is shown after the first picture
172 void CommonDisplay(vout_display_t *vd)
174 vout_display_sys_t *sys = vd->sys;
176 if (!sys->is_first_display)
179 /* Video window is initially hidden, show it now since we got a
182 SetWindowPos(sys->hvideownd, 0, 0, 0, 0, 0,
189 sys->is_first_display = false;
193 * It updates a picture data/pitches.
195 int CommonUpdatePicture(picture_t *picture, picture_t **fallback,
196 uint8_t *data, unsigned pitch)
199 if (*fallback == NULL) {
200 *fallback = picture_NewFromFormat(&picture->format);
201 if (*fallback == NULL)
204 for (int n = 0; n < picture->i_planes; n++) {
205 const plane_t *src = &(*fallback)->p[n];
206 plane_t *dst = &picture->p[n];
207 dst->p_pixels = src->p_pixels;
208 dst->i_pitch = src->i_pitch;
209 dst->i_lines = src->i_lines;
213 /* fill in buffer info in first plane */
214 picture->p->p_pixels = data;
215 picture->p->i_pitch = pitch;
216 picture->p->i_lines = picture->format.i_visible_height;
218 /* Fill chroma planes for planar YUV */
219 if (picture->format.i_chroma == VLC_CODEC_I420 ||
220 picture->format.i_chroma == VLC_CODEC_J420 ||
221 picture->format.i_chroma == VLC_CODEC_YV12) {
223 for (int n = 1; n < picture->i_planes; n++) {
224 const plane_t *o = &picture->p[n-1];
225 plane_t *p = &picture->p[n];
227 p->p_pixels = o->p_pixels + o->i_lines * o->i_pitch;
228 p->i_pitch = pitch / 2;
229 p->i_lines = picture->format.i_visible_height / 2;
231 /* The dx/d3d buffer is always allocated as YV12 */
232 if (vlc_fourcc_AreUVPlanesSwapped(picture->format.i_chroma, VLC_CODEC_YV12)) {
233 uint8_t *p_tmp = picture->p[1].p_pixels;
234 picture->p[1].p_pixels = picture->p[2].p_pixels;
235 picture->p[2].p_pixels = p_tmp;
241 void AlignRect(RECT *r, int align_boundary, int align_size)
244 r->left = (r->left + align_boundary/2) & ~align_boundary;
246 r->right = ((r->right - r->left + align_size/2) & ~align_size) + r->left;
250 static void CommonChangeThumbnailClip(vout_display_t *vd, bool show)
252 vout_display_sys_t *sys = vd->sys;
254 /* Windows 7 taskbar thumbnail code */
255 OSVERSIONINFO winVer;
256 winVer.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
257 if (!GetVersionEx(&winVer) || winVer.dwMajorVersion <= 5)
260 CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
263 if (S_OK == CoCreateInstance(&CLSID_TaskbarList,
264 NULL, CLSCTX_INPROC_SERVER,
267 ITaskbarList3 *taskbl = ptr;
268 taskbl->lpVtbl->HrInit(taskbl);
270 HWND hroot = GetAncestor(sys->hwnd,GA_ROOT);
274 GetWindowRect(sys->hvideownd, &video);
275 GetWindowRect(hroot, &parent);
276 relative.left = video.left - parent.left - 8;
277 relative.top = video.top - parent.top - 10;
279 relative.right = video.right - video.left + relative.left;
280 relative.bottom = video.bottom - video.top + relative.top - 25;
282 if (S_OK != taskbl->lpVtbl->SetThumbnailClip(taskbl, hroot,
283 show ? &relative : NULL))
284 msg_Err(vd, "SetThumbNailClip failed");
286 taskbl->lpVtbl->Release(taskbl);
291 /*****************************************************************************
292 * UpdateRects: update clipping rectangles
293 *****************************************************************************
294 * This function is called when the window position or size are changed, and
295 * its job is to update the source and destination RECTs used to display the
297 *****************************************************************************/
298 void UpdateRects(vout_display_t *vd,
299 const vout_display_cfg_t *cfg,
300 const video_format_t *source,
303 vout_display_sys_t *sys = vd->sys;
304 #define rect_src sys->rect_src
305 #define rect_src_clipped sys->rect_src_clipped
306 #define rect_dest sys->rect_dest
307 #define rect_dest_clipped sys->rect_dest_clipped
316 source = &vd->source;
318 /* Retrieve the window size */
319 GetClientRect(sys->hwnd, &rect);
321 /* Retrieve the window position */
322 point.x = point.y = 0;
323 ClientToScreen(sys->hwnd, &point);
325 /* If nothing changed, we can return */
328 EventThreadUpdateWindowPosition(sys->event, &has_moved, &is_resized,
330 rect.right, rect.bottom);
332 vout_display_SendEventDisplaySize(vd, rect.right, rect.bottom, cfg->is_fullscreen);
333 if (!is_forced && !has_moved && !is_resized )
336 /* Update the window position and size */
337 vout_display_cfg_t place_cfg = *cfg;
338 place_cfg.display.width = rect.right;
339 place_cfg.display.height = rect.bottom;
341 vout_display_place_t place;
342 vout_display_PlacePicture(&place, source, &place_cfg, false);
344 EventThreadUpdateSourceAndPlace(sys->event, source, &place);
347 SetWindowPos(sys->hvideownd, 0,
348 place.x, place.y, place.width, place.height,
349 SWP_NOCOPYBITS|SWP_NOZORDER|SWP_ASYNCWINDOWPOS);
351 /* Destination image position and dimensions */
352 #if defined(MODULE_NAME_IS_direct3d) || defined(MODULE_NAME_IS_direct2d)
354 rect_dest.right = place.width;
356 rect_dest.bottom = place.height;
358 rect_dest.left = point.x + place.x;
359 rect_dest.right = rect_dest.left + place.width;
360 rect_dest.top = point.y + place.y;
361 rect_dest.bottom = rect_dest.top + place.height;
363 #ifdef MODULE_NAME_IS_directdraw
364 /* Apply overlay hardware constraints */
365 if (sys->use_overlay)
366 AlignRect(&rect_dest, sys->i_align_dest_boundary, sys->i_align_dest_size);
371 #if defined(MODULE_NAME_IS_directdraw)
372 /* UpdateOverlay directdraw function doesn't automatically clip to the
373 * display size so we need to do it otherwise it will fail */
375 /* Clip the destination window */
376 if (!IntersectRect(&rect_dest_clipped, &rect_dest,
377 &sys->rect_display)) {
378 SetRectEmpty(&rect_src_clipped);
383 msg_Dbg(vd, "DirectXUpdateRects image_dst_clipped coords:"
385 rect_dest_clipped.left, rect_dest_clipped.top,
386 rect_dest_clipped.right, rect_dest_clipped.bottom);
391 /* AFAIK, there are no clipping constraints in Direct3D, OpenGL and GDI */
392 rect_dest_clipped = rect_dest;
396 /* the 2 following lines are to fix a bug when clicking on the desktop */
397 if ((rect_dest_clipped.right - rect_dest_clipped.left) == 0 ||
398 (rect_dest_clipped.bottom - rect_dest_clipped.top) == 0) {
399 SetRectEmpty(&rect_src_clipped);
403 /* src image dimensions */
406 rect_src.right = source->i_visible_width;
407 rect_src.bottom = source->i_visible_height;
409 /* Clip the source image */
410 rect_src_clipped.left = source->i_x_offset +
411 (rect_dest_clipped.left - rect_dest.left) *
412 source->i_visible_width / (rect_dest.right - rect_dest.left);
413 rect_src_clipped.right = source->i_x_offset +
414 source->i_visible_width -
415 (rect_dest.right - rect_dest_clipped.right) *
416 source->i_visible_width / (rect_dest.right - rect_dest.left);
417 rect_src_clipped.top = source->i_y_offset +
418 (rect_dest_clipped.top - rect_dest.top) *
419 source->i_visible_height / (rect_dest.bottom - rect_dest.top);
420 rect_src_clipped.bottom = source->i_y_offset +
421 source->i_visible_height -
422 (rect_dest.bottom - rect_dest_clipped.bottom) *
423 source->i_visible_height / (rect_dest.bottom - rect_dest.top);
425 #ifdef MODULE_NAME_IS_directdraw
426 /* Apply overlay hardware constraints */
427 if (sys->use_overlay)
428 AlignRect(&rect_src_clipped, sys->i_align_src_boundary, sys->i_align_src_size);
429 #elif defined(MODULE_NAME_IS_direct3d) || defined(MODULE_NAME_IS_direct2d)
430 /* Needed at least with YUV content */
431 rect_src_clipped.left &= ~1;
432 rect_src_clipped.right &= ~1;
433 rect_src_clipped.top &= ~1;
434 rect_src_clipped.bottom &= ~1;
438 msg_Dbg(vd, "DirectXUpdateRects image_src_clipped"
439 " coords: %li,%li,%li,%li",
440 rect_src_clipped.left, rect_src_clipped.top,
441 rect_src_clipped.right, rect_src_clipped.bottom);
444 #ifdef MODULE_NAME_IS_directdraw
445 /* The destination coordinates need to be relative to the current
446 * directdraw primary surface (display) */
447 rect_dest_clipped.left -= sys->rect_display.left;
448 rect_dest_clipped.right -= sys->rect_display.left;
449 rect_dest_clipped.top -= sys->rect_display.top;
450 rect_dest_clipped.bottom -= sys->rect_display.top;
453 CommonChangeThumbnailClip(vd, true);
456 /* Signal the change in size/position */
457 sys->changes |= DX_POSITION_CHANGE;
460 #undef rect_src_clipped
462 #undef rect_dest_clipped
465 static int CommonControlSetFullscreen(vout_display_t *vd, bool is_fullscreen)
467 vout_display_sys_t *sys = vd->sys;
469 #ifdef MODULE_NAME_IS_direct3d
470 if (sys->use_desktop && is_fullscreen)
475 if (sys->parent_window)
476 return vout_window_SetFullScreen(sys->parent_window, is_fullscreen);
479 HWND hwnd = sys->hparent && sys->hfswnd ? sys->hfswnd : sys->hwnd;
481 /* Save the current windows placement/placement to restore
482 when fullscreen is over */
483 WINDOWPLACEMENT window_placement;
484 window_placement.length = sizeof(WINDOWPLACEMENT);
485 GetWindowPlacement(hwnd, &window_placement);
488 msg_Dbg(vd, "entering fullscreen mode");
490 /* Change window style, no borders and no title bar */
491 SetWindowLong(hwnd, GWL_STYLE, WS_CLIPCHILDREN | WS_VISIBLE);
494 /* Retrieve current window position so fullscreen will happen
495 *on the right screen */
496 HMONITOR hmon = MonitorFromWindow(sys->hparent,
497 MONITOR_DEFAULTTONEAREST);
499 mi.cbSize = sizeof(MONITORINFO);
500 if (GetMonitorInfo(hmon, &mi))
501 SetWindowPos(hwnd, 0,
504 mi.rcMonitor.right - mi.rcMonitor.left,
505 mi.rcMonitor.bottom - mi.rcMonitor.top,
506 SWP_NOZORDER|SWP_FRAMECHANGED);
508 /* Maximize non embedded window */
509 ShowWindow(hwnd, SW_SHOWMAXIMIZED);
513 /* Hide the previous window */
515 GetClientRect(hwnd, &rect);
516 SetParent(sys->hwnd, hwnd);
517 SetWindowPos(sys->hwnd, 0, 0, 0,
518 rect.right, rect.bottom,
519 SWP_NOZORDER|SWP_FRAMECHANGED);
521 HWND topLevelParent = GetAncestor(sys->hparent, GA_ROOT);
522 ShowWindow(topLevelParent, SW_HIDE);
524 SetForegroundWindow(hwnd);
526 msg_Dbg(vd, "leaving fullscreen mode");
528 /* Change window style, no borders and no title bar */
529 SetWindowLong(hwnd, GWL_STYLE, EventThreadGetWindowStyle(sys->event));
533 GetClientRect(sys->hparent, &rect);
534 SetParent(sys->hwnd, sys->hparent);
535 SetWindowPos(sys->hwnd, 0, 0, 0,
536 rect.right, rect.bottom,
537 SWP_NOZORDER|SWP_FRAMECHANGED);
539 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);
593 UpdateRects(vd, cfg, source, is_forced);
596 case VOUT_DISPLAY_CHANGE_WINDOW_STATE: { /* unsigned state */
597 const unsigned state = va_arg(args, unsigned);
598 const bool is_on_top = (state & VOUT_WINDOW_STATE_ABOVE) != 0;
599 #ifdef MODULE_NAME_IS_direct3d
600 if (sys->use_desktop && is_on_top)
603 if (sys->parent_window) {
604 if (vout_window_SetState(sys->parent_window, state))
607 HMENU hMenu = GetSystemMenu(sys->hwnd, FALSE);
609 if (is_on_top && !(GetWindowLong(sys->hwnd, GWL_EXSTYLE) & WS_EX_TOPMOST)) {
610 CheckMenuItem(hMenu, IDM_TOGGLE_ON_TOP, MF_BYCOMMAND | MFS_CHECKED);
611 SetWindowPos(sys->hwnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE);
612 } else if (!is_on_top && (GetWindowLong(sys->hwnd, GWL_EXSTYLE) & WS_EX_TOPMOST)) {
613 CheckMenuItem(hMenu, IDM_TOGGLE_ON_TOP, MF_BYCOMMAND | MFS_UNCHECKED);
614 SetWindowPos(sys->hwnd, HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOSIZE|SWP_NOMOVE);
617 sys->is_on_top = is_on_top;
620 case VOUT_DISPLAY_CHANGE_FULLSCREEN: { /* const vout_display_cfg_t *p_cfg */
621 const vout_display_cfg_t *cfg = va_arg(args, const vout_display_cfg_t *);
622 if (CommonControlSetFullscreen(vd, cfg->is_fullscreen))
624 UpdateRects(vd, NULL, NULL, false);
628 case VOUT_DISPLAY_HIDE_MOUSE:
629 EventThreadMouseHide(sys->event);
631 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_screensaveactive = 0;
644 if (var_GetBool(vd, "disable-screensaver")) {
645 msg_Dbg(vd, "disabling screen saver");
646 SystemParametersInfo(SPI_GETSCREENSAVEACTIVE, 0,
647 &sys->i_spi_screensaveactive, 0);
649 if (LOWORD(GetVersion()) == 0x0005) {
650 /* If this is NT 5.0 (i.e., Win2K), we need to hack around
651 * KB318781 (see http://support.microsoft.com/kb/318781) */
655 if (ERROR_SUCCESS == RegOpenKeyEx(HKEY_CURRENT_USER,
656 TEXT("Control Panel\\Desktop"),
657 0, KEY_QUERY_VALUE, &hKeyCP) &&
658 ERROR_SUCCESS != RegQueryValueEx(hKeyCP, TEXT("SCRNSAVE.EXE"),
659 NULL, NULL, NULL, NULL)) {
660 sys->i_spi_screensaveactive = FALSE;
664 if (FALSE != sys->i_spi_screensaveactive) {
665 SystemParametersInfo(SPI_SETSCREENSAVEACTIVE, 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_screensaveactive) {
676 SystemParametersInfo(SPI_SETSCREENSAVEACTIVE,
677 sys->i_spi_screensaveactive, NULL, 0);