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>
8 * Martell Malone <martellmalone@gmail.com>
10 * This program is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU Lesser General Public License as published by
12 * the Free Software Foundation; either version 2.1 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public License
21 * along with this program; if not, write to the Free Software Foundation,
22 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
25 /*****************************************************************************
26 * Preamble: This file contains the functions related to the init of the vout
27 * structure, the common display code, the screensaver, but not the
28 * events and the Window Creation (events.c)
29 *****************************************************************************/
35 #include <vlc_common.h>
36 #include <vlc_vout_display.h>
43 #include <vlc_windows_interfaces.h>
45 static void CommonChangeThumbnailClip(vout_display_t *, bool show);
46 static int CommonControlSetFullscreen(vout_display_t *, bool is_fullscreen);
48 static void DisableScreensaver(vout_display_t *);
49 static void RestoreScreensaver(vout_display_t *);
52 int CommonInit(vout_display_t *vd)
54 vout_display_sys_t *sys = vd->sys;
57 sys->hvideownd = NULL;
61 SetRectEmpty(&sys->rect_display);
62 SetRectEmpty(&sys->rect_parent);
63 sys->is_first_display = true;
64 sys->is_on_top = false;
66 var_Create(vd, "video-deco", VLC_VAR_BOOL | VLC_VAR_DOINHERIT);
67 var_Create(vd, "disable-screensaver", VLC_VAR_BOOL | VLC_VAR_DOINHERIT);
70 sys->event = EventThreadCreate(vd);
75 memset(&cfg, 0, sizeof(cfg));
76 #ifdef MODULE_NAME_IS_direct3d9
77 cfg.use_desktop = sys->use_desktop;
79 #ifdef MODULE_NAME_IS_directdraw
80 cfg.use_overlay = sys->use_overlay;
82 cfg.x = var_InheritInteger(vd, "video-x");
83 cfg.y = var_InheritInteger(vd, "video-y");
84 cfg.width = vd->cfg->display.width;
85 cfg.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);
122 picture_pool_t *CommonPool(vout_display_t *vd, unsigned count)
125 return vd->sys->pool;
128 void CommonManage(vout_display_t *vd)
130 vout_display_sys_t *sys = vd->sys;
132 /* We used to call the Win32 PeekMessage function here to read the window
133 * messages. But since window can stay blocked into this function for a
134 * long time (for example when you move your window on the screen), I
135 * decided to isolate PeekMessage in another thread. */
137 /* If we do not control our window, we check for geometry changes
138 * ourselves because the parent might not send us its events. */
143 /* Check if the parent window has resized or moved */
144 GetClientRect(sys->hparent, &rect_parent);
145 point.x = point.y = 0;
146 ClientToScreen(sys->hparent, &point);
147 OffsetRect(&rect_parent, point.x, point.y);
149 if (!EqualRect(&rect_parent, &sys->rect_parent)) {
150 sys->rect_parent = rect_parent;
152 /* This code deals with both resize and move
154 * For most drivers(direct3d9, gdi, opengl), move is never
155 * an issue. The surface automatically gets moved together
156 * with the associated window (hvideownd)
158 * For directdraw, it is still important to call UpdateRects
159 * on a move of the parent window, even if no resize occurred
161 SetWindowPos(sys->hwnd, 0, 0, 0,
162 rect_parent.right - rect_parent.left,
163 rect_parent.bottom - rect_parent.top,
166 UpdateRects(vd, NULL, NULL, true);
170 /* HasMoved means here resize or move */
171 if (EventThreadGetAndResetHasMoved(sys->event))
172 UpdateRects(vd, NULL, NULL, false);
176 * It ensures that the video window is shown after the first picture
179 void CommonDisplay(vout_display_t *vd)
181 vout_display_sys_t *sys = vd->sys;
183 if (!sys->is_first_display)
186 /* Video window is initially hidden, show it now since we got a
189 SetWindowPos(sys->hvideownd, 0, 0, 0, 0, 0,
196 sys->is_first_display = false;
200 * It updates a picture data/pitches.
202 int CommonUpdatePicture(picture_t *picture, picture_t **fallback,
203 uint8_t *data, unsigned pitch)
206 if (*fallback == NULL) {
207 *fallback = picture_NewFromFormat(&picture->format);
208 if (*fallback == NULL)
211 for (int n = 0; n < picture->i_planes; n++) {
212 const plane_t *src = &(*fallback)->p[n];
213 plane_t *dst = &picture->p[n];
214 dst->p_pixels = src->p_pixels;
215 dst->i_pitch = src->i_pitch;
216 dst->i_lines = src->i_lines;
220 /* fill in buffer info in first plane */
221 picture->p->p_pixels = data;
222 picture->p->i_pitch = pitch;
223 picture->p->i_lines = picture->format.i_visible_height;
225 /* Fill chroma planes for biplanar YUV */
226 if (picture->format.i_chroma == VLC_CODEC_NV12 ||
227 picture->format.i_chroma == VLC_CODEC_NV21) {
229 for (int n = 1; n < picture->i_planes; n++) {
230 const plane_t *o = &picture->p[n-1];
231 plane_t *p = &picture->p[n];
233 p->p_pixels = o->p_pixels + o->i_lines * o->i_pitch;
235 p->i_lines = picture->format.i_visible_height;
237 /* The dx/d3d buffer is always allocated as NV12 */
238 if (vlc_fourcc_AreUVPlanesSwapped(picture->format.i_chroma, VLC_CODEC_NV12)) {
239 /* TODO : Swap NV21 UV planes to match NV12 */
244 /* Fill chroma planes for planar YUV */
245 if (picture->format.i_chroma == VLC_CODEC_I420 ||
246 picture->format.i_chroma == VLC_CODEC_J420 ||
247 picture->format.i_chroma == VLC_CODEC_YV12) {
249 for (int n = 1; n < picture->i_planes; n++) {
250 const plane_t *o = &picture->p[n-1];
251 plane_t *p = &picture->p[n];
253 p->p_pixels = o->p_pixels + o->i_lines * o->i_pitch;
254 p->i_pitch = pitch / 2;
255 p->i_lines = picture->format.i_visible_height / 2;
257 /* The dx/d3d buffer is always allocated as YV12 */
258 if (vlc_fourcc_AreUVPlanesSwapped(picture->format.i_chroma, VLC_CODEC_YV12)) {
259 uint8_t *p_tmp = picture->p[1].p_pixels;
260 picture->p[1].p_pixels = picture->p[2].p_pixels;
261 picture->p[2].p_pixels = p_tmp;
267 void AlignRect(RECT *r, int align_boundary, int align_size)
270 r->left = (r->left + align_boundary/2) & ~align_boundary;
272 r->right = ((r->right - r->left + align_size/2) & ~align_size) + r->left;
276 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)
286 if( FAILED(CoInitializeEx(NULL, COINIT_APARTMENTTHREADED)) )
287 vlc_assert_unreachable();
290 if (S_OK == CoCreateInstance(&CLSID_TaskbarList,
291 NULL, CLSCTX_INPROC_SERVER,
294 ITaskbarList3 *taskbl = ptr;
295 taskbl->lpVtbl->HrInit(taskbl);
297 HWND hroot = GetAncestor(sys->hwnd,GA_ROOT);
301 GetWindowRect(sys->hvideownd, &video);
302 GetWindowRect(hroot, &parent);
303 relative.left = video.left - parent.left - 8;
304 relative.top = video.top - parent.top - 10;
306 relative.right = video.right - video.left + relative.left;
307 relative.bottom = video.bottom - video.top + relative.top - 25;
309 if (S_OK != taskbl->lpVtbl->SetThumbnailClip(taskbl, hroot,
310 show ? &relative : NULL))
311 msg_Err(vd, "SetThumbNailClip failed");
313 taskbl->lpVtbl->Release(taskbl);
318 /*****************************************************************************
319 * UpdateRects: update clipping rectangles
320 *****************************************************************************
321 * This function is called when the window position or size are changed, and
322 * its job is to update the source and destination RECTs used to display the
324 *****************************************************************************/
325 void UpdateRects(vout_display_t *vd,
326 const vout_display_cfg_t *cfg,
327 const video_format_t *source,
330 vout_display_sys_t *sys = vd->sys;
331 #define rect_src sys->rect_src
332 #define rect_src_clipped sys->rect_src_clipped
333 #define rect_dest sys->rect_dest
334 #define rect_dest_clipped sys->rect_dest_clipped
343 source = &vd->source;
345 /* Retrieve the window size */
346 GetClientRect(sys->hwnd, &rect);
348 /* Retrieve the window position */
349 point.x = point.y = 0;
350 ClientToScreen(sys->hwnd, &point);
352 /* If nothing changed, we can return */
355 EventThreadUpdateWindowPosition(sys->event, &has_moved, &is_resized,
357 rect.right, rect.bottom);
359 vout_display_SendEventDisplaySize(vd, rect.right, rect.bottom);
360 if (!is_forced && !has_moved && !is_resized)
363 /* Update the window position and size */
364 vout_display_cfg_t place_cfg = *cfg;
365 place_cfg.display.width = rect.right;
366 place_cfg.display.height = rect.bottom;
368 vout_display_place_t place;
369 vout_display_PlacePicture(&place, source, &place_cfg, false);
371 EventThreadUpdateSourceAndPlace(sys->event, source, &place);
374 SetWindowPos(sys->hvideownd, 0,
375 place.x, place.y, place.width, place.height,
376 SWP_NOCOPYBITS|SWP_NOZORDER|SWP_ASYNCWINDOWPOS);
378 /* Destination image position and dimensions */
379 #if defined(MODULE_NAME_IS_direct3d9) || defined(MODULE_NAME_IS_direct3d11) || defined(MODULE_NAME_IS_direct2d)
381 rect_dest.right = place.width;
383 rect_dest.bottom = place.height;
385 rect_dest.left = point.x + place.x;
386 rect_dest.right = rect_dest.left + place.width;
387 rect_dest.top = point.y + place.y;
388 rect_dest.bottom = rect_dest.top + place.height;
390 #ifdef MODULE_NAME_IS_directdraw
391 /* Apply overlay hardware constraints */
392 if (sys->use_overlay)
393 AlignRect(&rect_dest, sys->i_align_dest_boundary, sys->i_align_dest_size);
398 #if defined(MODULE_NAME_IS_directdraw)
399 /* UpdateOverlay directdraw function doesn't automatically clip to the
400 * display size so we need to do it otherwise it will fail */
402 /* Clip the destination window */
403 if (!IntersectRect(&rect_dest_clipped, &rect_dest,
404 &sys->rect_display)) {
405 SetRectEmpty(&rect_src_clipped);
410 msg_Dbg(vd, "DirectXUpdateRects image_dst_clipped coords:"
412 rect_dest_clipped.left, rect_dest_clipped.top,
413 rect_dest_clipped.right, rect_dest_clipped.bottom);
418 /* AFAIK, there are no clipping constraints in Direct3D, OpenGL and GDI */
419 rect_dest_clipped = rect_dest;
423 /* the 2 following lines are to fix a bug when clicking on the desktop */
424 if ((rect_dest_clipped.right - rect_dest_clipped.left) == 0 ||
425 (rect_dest_clipped.bottom - rect_dest_clipped.top) == 0) {
426 SetRectEmpty(&rect_src_clipped);
430 /* src image dimensions */
433 rect_src.right = vd->fmt.i_visible_width;
434 rect_src.bottom = vd->fmt.i_visible_height;
436 /* Clip the source image */
437 rect_src_clipped.left = source->i_x_offset +
438 (rect_dest_clipped.left - rect_dest.left) *
439 source->i_visible_width / (rect_dest.right - rect_dest.left);
440 rect_src_clipped.right = source->i_x_offset +
441 source->i_visible_width -
442 (rect_dest.right - rect_dest_clipped.right) *
443 source->i_visible_width / (rect_dest.right - rect_dest.left);
444 rect_src_clipped.top = source->i_y_offset +
445 (rect_dest_clipped.top - rect_dest.top) *
446 source->i_visible_height / (rect_dest.bottom - rect_dest.top);
447 rect_src_clipped.bottom = source->i_y_offset +
448 source->i_visible_height -
449 (rect_dest.bottom - rect_dest_clipped.bottom) *
450 source->i_visible_height / (rect_dest.bottom - rect_dest.top);
452 #ifdef MODULE_NAME_IS_directdraw
453 /* Apply overlay hardware constraints */
454 if (sys->use_overlay)
455 AlignRect(&rect_src_clipped, sys->i_align_src_boundary, sys->i_align_src_size);
456 #elif defined(MODULE_NAME_IS_direct3d9) || defined(MODULE_NAME_IS_direct3d11) || defined(MODULE_NAME_IS_direct2d)
457 /* Needed at least with YUV content */
458 rect_src_clipped.left &= ~1;
459 rect_src_clipped.right &= ~1;
460 rect_src_clipped.top &= ~1;
461 rect_src_clipped.bottom &= ~1;
465 msg_Dbg(vd, "DirectXUpdateRects souce"
466 " offset: %i,%i visible: %ix%i",
467 source->i_x_offset, source->i_y_offset,
468 source->i_visible_width, source->i_visible_height);
469 msg_Dbg(vd, "DirectXUpdateRects image_src"
470 " coords: %li,%li,%li,%li",
471 rect_src.left, rect_src.top,
472 rect_src.right, rect_src.bottom);
473 msg_Dbg(vd, "DirectXUpdateRects image_src_clipped"
474 " coords: %li,%li,%li,%li",
475 rect_src_clipped.left, rect_src_clipped.top,
476 rect_src_clipped.right, rect_src_clipped.bottom);
477 msg_Dbg(vd, "DirectXUpdateRects image_dst"
478 " coords: %li,%li,%li,%li",
479 rect_dest.left, rect_dest.top,
480 rect_dest.right, rect_dest.bottom);
481 msg_Dbg(vd, "DirectXUpdateRects image_dst_clipped"
482 " coords: %li,%li,%li,%li",
483 rect_dest_clipped.left, rect_dest_clipped.top,
484 rect_dest_clipped.right, rect_dest_clipped.bottom);
487 #ifdef MODULE_NAME_IS_directdraw
488 /* The destination coordinates need to be relative to the current
489 * directdraw primary surface (display) */
490 rect_dest_clipped.left -= sys->rect_display.left;
491 rect_dest_clipped.right -= sys->rect_display.left;
492 rect_dest_clipped.top -= sys->rect_display.top;
493 rect_dest_clipped.bottom -= sys->rect_display.top;
496 CommonChangeThumbnailClip(vd, true);
499 /* Signal the change in size/position */
500 sys->changes |= DX_POSITION_CHANGE;
503 #undef rect_src_clipped
505 #undef rect_dest_clipped
508 static int CommonControlSetFullscreen(vout_display_t *vd, bool is_fullscreen)
510 vout_display_sys_t *sys = vd->sys;
512 #ifdef MODULE_NAME_IS_direct3d9
513 if (sys->use_desktop && is_fullscreen)
518 if (sys->parent_window)
522 HWND hwnd = sys->hparent && sys->hfswnd ? sys->hfswnd : sys->hwnd;
524 /* Save the current windows placement/placement to restore
525 when fullscreen is over */
526 WINDOWPLACEMENT window_placement;
527 window_placement.length = sizeof(WINDOWPLACEMENT);
528 GetWindowPlacement(hwnd, &window_placement);
531 msg_Dbg(vd, "entering fullscreen mode");
533 /* Change window style, no borders and no title bar */
534 SetWindowLong(hwnd, GWL_STYLE, WS_CLIPCHILDREN | WS_VISIBLE);
537 /* Retrieve current window position so fullscreen will happen
538 *on the right screen */
539 HMONITOR hmon = MonitorFromWindow(sys->hparent,
540 MONITOR_DEFAULTTONEAREST);
542 mi.cbSize = sizeof(MONITORINFO);
543 if (GetMonitorInfo(hmon, &mi))
544 SetWindowPos(hwnd, 0,
547 mi.rcMonitor.right - mi.rcMonitor.left,
548 mi.rcMonitor.bottom - mi.rcMonitor.top,
549 SWP_NOZORDER|SWP_FRAMECHANGED);
551 /* Maximize non embedded window */
552 ShowWindow(hwnd, SW_SHOWMAXIMIZED);
556 /* Hide the previous window */
558 GetClientRect(hwnd, &rect);
559 SetParent(sys->hwnd, hwnd);
560 SetWindowPos(sys->hwnd, 0, 0, 0,
561 rect.right, rect.bottom,
562 SWP_NOZORDER|SWP_FRAMECHANGED);
564 HWND topLevelParent = GetAncestor(sys->hparent, GA_ROOT);
565 ShowWindow(topLevelParent, SW_HIDE);
567 SetForegroundWindow(hwnd);
569 msg_Dbg(vd, "leaving fullscreen mode");
571 /* Change window style, no borders and no title bar */
572 SetWindowLong(hwnd, GWL_STYLE, EventThreadGetWindowStyle(sys->event));
576 GetClientRect(sys->hparent, &rect);
577 SetParent(sys->hwnd, sys->hparent);
578 SetWindowPos(sys->hwnd, 0, 0, 0,
579 rect.right, rect.bottom,
580 SWP_NOZORDER|SWP_FRAMECHANGED);
582 HWND topLevelParent = GetAncestor(sys->hparent, GA_ROOT);
583 ShowWindow(topLevelParent, SW_SHOW);
584 SetForegroundWindow(sys->hparent);
585 ShowWindow(hwnd, SW_HIDE);
587 /* return to normal window for non embedded vout */
588 SetWindowPlacement(hwnd, &window_placement);
589 ShowWindow(hwnd, SW_SHOWNORMAL);
595 int CommonControl(vout_display_t *vd, int query, va_list args)
597 vout_display_sys_t *sys = vd->sys;
600 case VOUT_DISPLAY_CHANGE_DISPLAY_SIZE: /* const vout_display_cfg_t *p_cfg */
601 { /* Update dimensions */
602 const vout_display_cfg_t *cfg = va_arg(args, const vout_display_cfg_t *);
606 .right = cfg->display.width,
607 .bottom = cfg->display.height,
610 AdjustWindowRect(&rect_window, EventThreadGetWindowStyle(sys->event), 0);
611 SetWindowPos(sys->hwnd, 0, 0, 0,
612 rect_window.right - rect_window.left,
613 rect_window.bottom - rect_window.top, SWP_NOMOVE);
614 UpdateRects(vd, cfg, &vd->source, false);
617 case VOUT_DISPLAY_CHANGE_DISPLAY_FILLED: /* const vout_display_cfg_t *p_cfg */
618 case VOUT_DISPLAY_CHANGE_ZOOM: /* const vout_display_cfg_t *p_cfg */
619 case VOUT_DISPLAY_CHANGE_SOURCE_ASPECT: /* const video_format_t *p_source */
620 case VOUT_DISPLAY_CHANGE_SOURCE_CROP: { /* const video_format_t *p_source */
621 const vout_display_cfg_t *cfg;
623 if (query == VOUT_DISPLAY_CHANGE_SOURCE_CROP ||
624 query == VOUT_DISPLAY_CHANGE_SOURCE_ASPECT) {
625 const video_format_t *source = va_arg(args, const video_format_t *);
627 UpdateRects(vd, cfg, source, true);
629 cfg = va_arg(args, const vout_display_cfg_t *);
630 UpdateRects(vd, cfg, NULL, false);
634 case VOUT_DISPLAY_CHANGE_WINDOW_STATE: { /* unsigned state */
635 const unsigned state = va_arg(args, unsigned);
636 const bool is_on_top = (state & VOUT_WINDOW_STATE_ABOVE) != 0;
637 #ifdef MODULE_NAME_IS_direct3d9
638 if (sys->use_desktop && is_on_top)
641 HMENU hMenu = GetSystemMenu(sys->hwnd, FALSE);
643 if (is_on_top && !(GetWindowLong(sys->hwnd, GWL_EXSTYLE) & WS_EX_TOPMOST)) {
644 CheckMenuItem(hMenu, IDM_TOGGLE_ON_TOP, MF_BYCOMMAND | MFS_CHECKED);
645 SetWindowPos(sys->hwnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE);
646 } else if (!is_on_top && (GetWindowLong(sys->hwnd, GWL_EXSTYLE) & WS_EX_TOPMOST)) {
647 CheckMenuItem(hMenu, IDM_TOGGLE_ON_TOP, MF_BYCOMMAND | MFS_UNCHECKED);
648 SetWindowPos(sys->hwnd, HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOSIZE|SWP_NOMOVE);
650 sys->is_on_top = is_on_top;
653 case VOUT_DISPLAY_CHANGE_FULLSCREEN: {
654 bool fs = va_arg(args, int);
655 if (CommonControlSetFullscreen(vd, fs))
657 UpdateRects(vd, NULL, NULL, false);
661 case VOUT_DISPLAY_HIDE_MOUSE:
662 EventThreadMouseHide(sys->event);
664 case VOUT_DISPLAY_RESET_PICTURES:
665 vlc_assert_unreachable();
671 static void DisableScreensaver(vout_display_t *vd)
673 vout_display_sys_t *sys = vd->sys;
675 /* disable screensaver by temporarily changing system settings */
676 sys->i_spi_screensaveactive = 0;
677 if (var_GetBool(vd, "disable-screensaver")) {
678 msg_Dbg(vd, "disabling screen saver");
679 SystemParametersInfo(SPI_GETSCREENSAVEACTIVE, 0,
680 &sys->i_spi_screensaveactive, 0);
682 if (LOWORD(GetVersion()) == 0x0005) {
683 /* If this is NT 5.0 (i.e., Win2K), we need to hack around
684 * KB318781 (see http://support.microsoft.com/kb/318781) */
688 if (ERROR_SUCCESS == RegOpenKeyEx(HKEY_CURRENT_USER,
689 TEXT("Control Panel\\Desktop"),
690 0, KEY_QUERY_VALUE, &hKeyCP) &&
691 ERROR_SUCCESS != RegQueryValueEx(hKeyCP, TEXT("SCRNSAVE.EXE"),
692 NULL, NULL, NULL, NULL)) {
693 sys->i_spi_screensaveactive = FALSE;
697 if (FALSE != sys->i_spi_screensaveactive) {
698 SystemParametersInfo(SPI_SETSCREENSAVEACTIVE, 0, NULL, 0);
703 static void RestoreScreensaver(vout_display_t *vd)
705 vout_display_sys_t *sys = vd->sys;
707 /* restore screensaver system settings */
708 if (0 != sys->i_spi_screensaveactive) {
709 SystemParametersInfo(SPI_SETSCREENSAVEACTIVE,
710 sys->i_spi_screensaveactive, NULL, 0);