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.x = var_InheritInteger(vd, "video-x");
82 cfg.y = var_InheritInteger(vd, "video-y");
83 cfg.width = vd->cfg->display.width;
84 cfg.height = vd->cfg->display.height;
87 if (EventThreadStart(sys->event, &hwnd, &cfg))
90 sys->parent_window = hwnd.parent_window;
91 sys->hparent = hwnd.hparent;
92 sys->hwnd = hwnd.hwnd;
93 sys->hvideownd = hwnd.hvideownd;
94 sys->hfswnd = hwnd.hfswnd;
96 if (vd->cfg->is_fullscreen) {
97 if (CommonControlSetFullscreen(vd, true))
98 vout_display_SendEventFullscreen(vd, false);
101 DisableScreensaver (vd);
107 void CommonClean(vout_display_t *vd)
109 vout_display_sys_t *sys = vd->sys;
112 CommonChangeThumbnailClip(vd, false);
113 EventThreadStop(sys->event);
114 EventThreadDestroy(sys->event);
117 RestoreScreensaver(vd);
120 void CommonManage(vout_display_t *vd)
122 vout_display_sys_t *sys = vd->sys;
124 /* We used to call the Win32 PeekMessage function here to read the window
125 * messages. But since window can stay blocked into this function for a
126 * long time (for example when you move your window on the screen), I
127 * decided to isolate PeekMessage in another thread. */
129 /* If we do not control our window, we check for geometry changes
130 * ourselves because the parent might not send us its events. */
135 /* Check if the parent window has resized or moved */
136 GetClientRect(sys->hparent, &rect_parent);
137 point.x = point.y = 0;
138 ClientToScreen(sys->hparent, &point);
139 OffsetRect(&rect_parent, point.x, point.y);
141 if (!EqualRect(&rect_parent, &sys->rect_parent)) {
142 sys->rect_parent = rect_parent;
144 /* This code deals with both resize and move
146 * For most drivers(direct3d, gdi, opengl), move is never
147 * an issue. The surface automatically gets moved together
148 * with the associated window (hvideownd)
150 * For directx, it is still important to call UpdateRects
151 * on a move of the parent window, even if no resize occurred
153 SetWindowPos(sys->hwnd, 0, 0, 0,
154 rect_parent.right - rect_parent.left,
155 rect_parent.bottom - rect_parent.top,
158 UpdateRects(vd, NULL, NULL, true);
162 /* HasMoved means here resize or move */
163 if (EventThreadGetAndResetHasMoved(sys->event))
164 UpdateRects(vd, NULL, NULL, false);
168 * It ensures that the video window is shown after the first picture
171 void CommonDisplay(vout_display_t *vd)
173 vout_display_sys_t *sys = vd->sys;
175 if (!sys->is_first_display)
178 /* Video window is initially hidden, show it now since we got a
181 SetWindowPos(sys->hvideownd, 0, 0, 0, 0, 0,
188 sys->is_first_display = false;
192 * It updates a picture data/pitches.
194 int CommonUpdatePicture(picture_t *picture, picture_t **fallback,
195 uint8_t *data, unsigned pitch)
198 if (*fallback == NULL) {
199 *fallback = picture_NewFromFormat(&picture->format);
200 if (*fallback == NULL)
203 for (int n = 0; n < picture->i_planes; n++) {
204 const plane_t *src = &(*fallback)->p[n];
205 plane_t *dst = &picture->p[n];
206 dst->p_pixels = src->p_pixels;
207 dst->i_pitch = src->i_pitch;
208 dst->i_lines = src->i_lines;
212 /* fill in buffer info in first plane */
213 picture->p->p_pixels = data;
214 picture->p->i_pitch = pitch;
215 picture->p->i_lines = picture->format.i_visible_height;
217 /* Fill chroma planes for planar YUV */
218 if (picture->format.i_chroma == VLC_CODEC_I420 ||
219 picture->format.i_chroma == VLC_CODEC_J420 ||
220 picture->format.i_chroma == VLC_CODEC_YV12) {
222 for (int n = 1; n < picture->i_planes; n++) {
223 const plane_t *o = &picture->p[n-1];
224 plane_t *p = &picture->p[n];
226 p->p_pixels = o->p_pixels + o->i_lines * o->i_pitch;
227 p->i_pitch = pitch / 2;
228 p->i_lines = picture->format.i_visible_height / 2;
230 /* The dx/d3d buffer is always allocated as YV12 */
231 if (vlc_fourcc_AreUVPlanesSwapped(picture->format.i_chroma, VLC_CODEC_YV12)) {
232 uint8_t *p_tmp = picture->p[1].p_pixels;
233 picture->p[1].p_pixels = picture->p[2].p_pixels;
234 picture->p[2].p_pixels = p_tmp;
240 void AlignRect(RECT *r, int align_boundary, int align_size)
243 r->left = (r->left + align_boundary/2) & ~align_boundary;
245 r->right = ((r->right - r->left + align_size/2) & ~align_size) + r->left;
249 static void CommonChangeThumbnailClip(vout_display_t *vd, bool show)
251 vout_display_sys_t *sys = vd->sys;
253 /* Windows 7 taskbar thumbnail code */
254 OSVERSIONINFO winVer;
255 winVer.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
256 if (!GetVersionEx(&winVer) || winVer.dwMajorVersion <= 5)
259 CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
262 if (S_OK == CoCreateInstance(&CLSID_TaskbarList,
263 NULL, CLSCTX_INPROC_SERVER,
266 ITaskbarList3 *taskbl = ptr;
267 taskbl->lpVtbl->HrInit(taskbl);
269 HWND hroot = GetAncestor(sys->hwnd,GA_ROOT);
273 GetWindowRect(sys->hvideownd, &video);
274 GetWindowRect(hroot, &parent);
275 relative.left = video.left - parent.left - 8;
276 relative.top = video.top - parent.top - 10;
278 relative.right = video.right - video.left + relative.left;
279 relative.bottom = video.bottom - video.top + relative.top - 25;
281 if (S_OK != taskbl->lpVtbl->SetThumbnailClip(taskbl, hroot,
282 show ? &relative : NULL))
283 msg_Err(vd, "SetThumbNailClip failed");
285 taskbl->lpVtbl->Release(taskbl);
290 /*****************************************************************************
291 * UpdateRects: update clipping rectangles
292 *****************************************************************************
293 * This function is called when the window position or size are changed, and
294 * its job is to update the source and destination RECTs used to display the
296 *****************************************************************************/
297 void UpdateRects(vout_display_t *vd,
298 const vout_display_cfg_t *cfg,
299 const video_format_t *source,
302 vout_display_sys_t *sys = vd->sys;
303 #define rect_src sys->rect_src
304 #define rect_src_clipped sys->rect_src_clipped
305 #define rect_dest sys->rect_dest
306 #define rect_dest_clipped sys->rect_dest_clipped
315 source = &vd->source;
317 /* Retrieve the window size */
318 GetClientRect(sys->hwnd, &rect);
320 /* Retrieve the window position */
321 point.x = point.y = 0;
322 ClientToScreen(sys->hwnd, &point);
324 /* If nothing changed, we can return */
327 EventThreadUpdateWindowPosition(sys->event, &has_moved, &is_resized,
329 rect.right, rect.bottom);
331 vout_display_SendEventDisplaySize(vd, rect.right, rect.bottom, cfg->is_fullscreen);
332 if (!is_forced && !has_moved && !is_resized )
335 /* Update the window position and size */
336 vout_display_cfg_t place_cfg = *cfg;
337 place_cfg.display.width = rect.right;
338 place_cfg.display.height = rect.bottom;
340 vout_display_place_t place;
341 vout_display_PlacePicture(&place, source, &place_cfg, false);
343 EventThreadUpdateSourceAndPlace(sys->event, source, &place);
346 SetWindowPos(sys->hvideownd, 0,
347 place.x, place.y, place.width, place.height,
348 SWP_NOCOPYBITS|SWP_NOZORDER|SWP_ASYNCWINDOWPOS);
350 /* Destination image position and dimensions */
351 #if defined(MODULE_NAME_IS_direct3d) || defined(MODULE_NAME_IS_direct2d)
353 rect_dest.right = place.width;
355 rect_dest.bottom = place.height;
357 rect_dest.left = point.x + place.x;
358 rect_dest.right = rect_dest.left + place.width;
359 rect_dest.top = point.y + place.y;
360 rect_dest.bottom = rect_dest.top + place.height;
362 #ifdef MODULE_NAME_IS_directdraw
363 /* Apply overlay hardware constraints */
364 if (sys->use_overlay)
365 AlignRect(&rect_dest, sys->i_align_dest_boundary, sys->i_align_dest_size);
370 #if defined(MODULE_NAME_IS_directdraw)
371 /* UpdateOverlay directdraw function doesn't automatically clip to the
372 * display size so we need to do it otherwise it will fail */
374 /* Clip the destination window */
375 if (!IntersectRect(&rect_dest_clipped, &rect_dest,
376 &sys->rect_display)) {
377 SetRectEmpty(&rect_src_clipped);
382 msg_Dbg(vd, "DirectXUpdateRects image_dst_clipped coords:"
384 rect_dest_clipped.left, rect_dest_clipped.top,
385 rect_dest_clipped.right, rect_dest_clipped.bottom);
390 /* AFAIK, there are no clipping constraints in Direct3D, OpenGL and GDI */
391 rect_dest_clipped = rect_dest;
395 /* the 2 following lines are to fix a bug when clicking on the desktop */
396 if ((rect_dest_clipped.right - rect_dest_clipped.left) == 0 ||
397 (rect_dest_clipped.bottom - rect_dest_clipped.top) == 0) {
398 SetRectEmpty(&rect_src_clipped);
402 /* src image dimensions */
405 rect_src.right = vd->fmt.i_visible_width;
406 rect_src.bottom = vd->fmt.i_visible_height;
408 /* Clip the source image */
409 rect_src_clipped.left = source->i_x_offset +
410 (rect_dest_clipped.left - rect_dest.left) *
411 source->i_visible_width / (rect_dest.right - rect_dest.left);
412 rect_src_clipped.right = source->i_x_offset +
413 source->i_visible_width -
414 (rect_dest.right - rect_dest_clipped.right) *
415 source->i_visible_width / (rect_dest.right - rect_dest.left);
416 rect_src_clipped.top = source->i_y_offset +
417 (rect_dest_clipped.top - rect_dest.top) *
418 source->i_visible_height / (rect_dest.bottom - rect_dest.top);
419 rect_src_clipped.bottom = source->i_y_offset +
420 source->i_visible_height -
421 (rect_dest.bottom - rect_dest_clipped.bottom) *
422 source->i_visible_height / (rect_dest.bottom - rect_dest.top);
424 #ifdef MODULE_NAME_IS_directdraw
425 /* Apply overlay hardware constraints */
426 if (sys->use_overlay)
427 AlignRect(&rect_src_clipped, sys->i_align_src_boundary, sys->i_align_src_size);
428 #elif defined(MODULE_NAME_IS_direct3d) || defined(MODULE_NAME_IS_direct2d)
429 /* Needed at least with YUV content */
430 rect_src_clipped.left &= ~1;
431 rect_src_clipped.right &= ~1;
432 rect_src_clipped.top &= ~1;
433 rect_src_clipped.bottom &= ~1;
437 msg_Dbg(vd, "DirectXUpdateRects image_src_clipped"
438 " coords: %li,%li,%li,%li",
439 rect_src_clipped.left, rect_src_clipped.top,
440 rect_src_clipped.right, rect_src_clipped.bottom);
443 #ifdef MODULE_NAME_IS_directdraw
444 /* The destination coordinates need to be relative to the current
445 * directdraw primary surface (display) */
446 rect_dest_clipped.left -= sys->rect_display.left;
447 rect_dest_clipped.right -= sys->rect_display.left;
448 rect_dest_clipped.top -= sys->rect_display.top;
449 rect_dest_clipped.bottom -= sys->rect_display.top;
452 CommonChangeThumbnailClip(vd, true);
455 /* Signal the change in size/position */
456 sys->changes |= DX_POSITION_CHANGE;
459 #undef rect_src_clipped
461 #undef rect_dest_clipped
464 static int CommonControlSetFullscreen(vout_display_t *vd, bool is_fullscreen)
466 vout_display_sys_t *sys = vd->sys;
468 #ifdef MODULE_NAME_IS_direct3d
469 if (sys->use_desktop && is_fullscreen)
474 if (sys->parent_window)
475 return vout_window_SetFullScreen(sys->parent_window, is_fullscreen);
478 HWND hwnd = sys->hparent && sys->hfswnd ? sys->hfswnd : sys->hwnd;
480 /* Save the current windows placement/placement to restore
481 when fullscreen is over */
482 WINDOWPLACEMENT window_placement;
483 window_placement.length = sizeof(WINDOWPLACEMENT);
484 GetWindowPlacement(hwnd, &window_placement);
487 msg_Dbg(vd, "entering fullscreen mode");
489 /* Change window style, no borders and no title bar */
490 SetWindowLong(hwnd, GWL_STYLE, WS_CLIPCHILDREN | WS_VISIBLE);
493 /* Retrieve current window position so fullscreen will happen
494 *on the right screen */
495 HMONITOR hmon = MonitorFromWindow(sys->hparent,
496 MONITOR_DEFAULTTONEAREST);
498 mi.cbSize = sizeof(MONITORINFO);
499 if (GetMonitorInfo(hmon, &mi))
500 SetWindowPos(hwnd, 0,
503 mi.rcMonitor.right - mi.rcMonitor.left,
504 mi.rcMonitor.bottom - mi.rcMonitor.top,
505 SWP_NOZORDER|SWP_FRAMECHANGED);
507 /* Maximize non embedded window */
508 ShowWindow(hwnd, SW_SHOWMAXIMIZED);
512 /* Hide the previous window */
514 GetClientRect(hwnd, &rect);
515 SetParent(sys->hwnd, hwnd);
516 SetWindowPos(sys->hwnd, 0, 0, 0,
517 rect.right, rect.bottom,
518 SWP_NOZORDER|SWP_FRAMECHANGED);
520 HWND topLevelParent = GetAncestor(sys->hparent, GA_ROOT);
521 ShowWindow(topLevelParent, SW_HIDE);
523 SetForegroundWindow(hwnd);
525 msg_Dbg(vd, "leaving fullscreen mode");
527 /* Change window style, no borders and no title bar */
528 SetWindowLong(hwnd, GWL_STYLE, EventThreadGetWindowStyle(sys->event));
532 GetClientRect(sys->hparent, &rect);
533 SetParent(sys->hwnd, sys->hparent);
534 SetWindowPos(sys->hwnd, 0, 0, 0,
535 rect.right, rect.bottom,
536 SWP_NOZORDER|SWP_FRAMECHANGED);
538 HWND topLevelParent = GetAncestor(sys->hparent, GA_ROOT);
539 ShowWindow(topLevelParent, SW_SHOW);
540 SetForegroundWindow(sys->hparent);
541 ShowWindow(hwnd, SW_HIDE);
543 /* return to normal window for non embedded vout */
544 SetWindowPlacement(hwnd, &window_placement);
545 ShowWindow(hwnd, SW_SHOWNORMAL);
551 int CommonControl(vout_display_t *vd, int query, va_list args)
553 vout_display_sys_t *sys = vd->sys;
556 case VOUT_DISPLAY_CHANGE_DISPLAY_SIZE: /* const vout_display_cfg_t *p_cfg, int is_forced */
557 case VOUT_DISPLAY_CHANGE_DISPLAY_FILLED: /* const vout_display_cfg_t *p_cfg */
558 case VOUT_DISPLAY_CHANGE_ZOOM: /* const vout_display_cfg_t *p_cfg */
559 case VOUT_DISPLAY_CHANGE_SOURCE_ASPECT: /* const video_format_t *p_source */
560 case VOUT_DISPLAY_CHANGE_SOURCE_CROP: { /* const video_format_t *p_source */
561 const vout_display_cfg_t *cfg;
562 const video_format_t *source;
563 bool is_forced = true;
564 if (query == VOUT_DISPLAY_CHANGE_SOURCE_CROP ||
565 query == VOUT_DISPLAY_CHANGE_SOURCE_ASPECT) {
567 source = va_arg(args, const video_format_t *);
569 cfg = va_arg(args, const vout_display_cfg_t *);
570 source = &vd->source;
571 if (query == VOUT_DISPLAY_CHANGE_DISPLAY_SIZE)
572 is_forced = va_arg(args, int);
574 if (query == VOUT_DISPLAY_CHANGE_DISPLAY_SIZE && is_forced) {
575 /* Update dimensions */
576 if (sys->parent_window) {
577 vout_window_SetSize(sys->parent_window, cfg->display.width, cfg->display.height);
581 rect_window.left = 0;
582 rect_window.right = cfg->display.width;
583 rect_window.bottom = cfg->display.height;
584 AdjustWindowRect(&rect_window, EventThreadGetWindowStyle(sys->event), 0);
586 SetWindowPos(sys->hwnd, 0, 0, 0,
587 rect_window.right - rect_window.left,
588 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:
637 static void DisableScreensaver(vout_display_t *vd)
639 vout_display_sys_t *sys = vd->sys;
641 /* disable screensaver by temporarily changing system settings */
642 sys->i_spi_screensaveactive = 0;
643 if (var_GetBool(vd, "disable-screensaver")) {
644 msg_Dbg(vd, "disabling screen saver");
645 SystemParametersInfo(SPI_GETSCREENSAVEACTIVE, 0,
646 &sys->i_spi_screensaveactive, 0);
648 if (LOWORD(GetVersion()) == 0x0005) {
649 /* If this is NT 5.0 (i.e., Win2K), we need to hack around
650 * KB318781 (see http://support.microsoft.com/kb/318781) */
654 if (ERROR_SUCCESS == RegOpenKeyEx(HKEY_CURRENT_USER,
655 TEXT("Control Panel\\Desktop"),
656 0, KEY_QUERY_VALUE, &hKeyCP) &&
657 ERROR_SUCCESS != RegQueryValueEx(hKeyCP, TEXT("SCRNSAVE.EXE"),
658 NULL, NULL, NULL, NULL)) {
659 sys->i_spi_screensaveactive = FALSE;
663 if (FALSE != sys->i_spi_screensaveactive) {
664 SystemParametersInfo(SPI_SETSCREENSAVEACTIVE, 0, NULL, 0);
669 static void RestoreScreensaver(vout_display_t *vd)
671 vout_display_sys_t *sys = vd->sys;
673 /* restore screensaver system settings */
674 if (0 != sys->i_spi_screensaveactive) {
675 SystemParametersInfo(SPI_SETSCREENSAVEACTIVE,
676 sys->i_spi_screensaveactive, NULL, 0);