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_directdraw
45 #ifdef MODULE_NAME_IS_direct3d
48 #ifdef MODULE_NAME_IS_glwin32
49 #include "../opengl.h"
51 #ifdef MODULE_NAME_IS_direct2d
57 #include <vlc_windows_interfaces.h>
59 static void CommonChangeThumbnailClip(vout_display_t *, bool show);
60 static int CommonControlSetFullscreen(vout_display_t *, bool is_fullscreen);
62 static void DisableScreensaver(vout_display_t *);
63 static void RestoreScreensaver(vout_display_t *);
66 int CommonInit(vout_display_t *vd)
68 vout_display_sys_t *sys = vd->sys;
71 sys->hvideownd = NULL;
75 SetRectEmpty(&sys->rect_display);
76 SetRectEmpty(&sys->rect_parent);
77 sys->is_first_display = true;
78 sys->is_on_top = false;
80 var_Create(vd, "video-title", VLC_VAR_STRING | VLC_VAR_DOINHERIT);
81 var_Create(vd, "video-deco", VLC_VAR_BOOL | VLC_VAR_DOINHERIT);
84 sys->event = EventThreadCreate(vd);
89 memset(&cfg, 0, sizeof(cfg));
90 #ifdef MODULE_NAME_IS_direct3d
91 cfg.use_desktop = sys->use_desktop;
93 #ifdef MODULE_NAME_IS_directdraw
94 cfg.use_overlay = sys->use_overlay;
96 cfg.win.type = VOUT_WINDOW_TYPE_HWND;
97 cfg.win.x = var_InheritInteger(vd, "video-x");
98 cfg.win.y = var_InheritInteger(vd, "video-y");
99 cfg.win.width = vd->cfg->display.width;
100 cfg.win.height = vd->cfg->display.height;
103 if (EventThreadStart(sys->event, &hwnd, &cfg))
106 sys->parent_window = hwnd.parent_window;
107 sys->hparent = hwnd.hparent;
108 sys->hwnd = hwnd.hwnd;
109 sys->hvideownd = hwnd.hvideownd;
110 sys->hfswnd = hwnd.hfswnd;
112 if (vd->cfg->is_fullscreen) {
113 if (CommonControlSetFullscreen(vd, true))
114 vout_display_SendEventFullscreen(vd, false);
117 /* Why not with glwin32 */
118 var_Create(vd, "disable-screensaver", VLC_VAR_BOOL | VLC_VAR_DOINHERIT);
119 DisableScreensaver (vd);
125 void CommonClean(vout_display_t *vd)
127 vout_display_sys_t *sys = vd->sys;
130 CommonChangeThumbnailClip(vd, false);
131 EventThreadStop(sys->event);
132 EventThreadDestroy(sys->event);
135 RestoreScreensaver(vd);
138 void CommonManage(vout_display_t *vd)
140 vout_display_sys_t *sys = vd->sys;
142 /* We used to call the Win32 PeekMessage function here to read the window
143 * messages. But since window can stay blocked into this function for a
144 * long time (for example when you move your window on the screen), I
145 * decided to isolate PeekMessage in another thread. */
147 /* If we do not control our window, we check for geometry changes
148 * ourselves because the parent might not send us its events. */
153 /* Check if the parent window has resized or moved */
154 GetClientRect(sys->hparent, &rect_parent);
155 point.x = point.y = 0;
156 ClientToScreen(sys->hparent, &point);
157 OffsetRect(&rect_parent, point.x, point.y);
159 if (!EqualRect(&rect_parent, &sys->rect_parent)) {
160 sys->rect_parent = rect_parent;
162 /* This code deals with both resize and move
164 * For most drivers(direct3d, gdi, opengl), move is never
165 * an issue. The surface automatically gets moved together
166 * with the associated window (hvideownd)
168 * For directx, it is still important to call UpdateRects
169 * on a move of the parent window, even if no resize occurred
171 SetWindowPos(sys->hwnd, 0, 0, 0,
172 rect_parent.right - rect_parent.left,
173 rect_parent.bottom - rect_parent.top,
176 UpdateRects(vd, NULL, NULL, true);
180 /* HasMoved means here resize or move */
181 if (EventThreadGetAndResetHasMoved(sys->event))
182 UpdateRects(vd, NULL, NULL, false);
186 * It ensures that the video window is shown after the first picture
189 void CommonDisplay(vout_display_t *vd)
191 vout_display_sys_t *sys = vd->sys;
193 if (!sys->is_first_display)
196 /* Video window is initially hidden, show it now since we got a
199 SetWindowPos(sys->hvideownd, 0, 0, 0, 0, 0,
206 sys->is_first_display = false;
210 * It updates a picture data/pitches.
212 int CommonUpdatePicture(picture_t *picture, picture_t **fallback,
213 uint8_t *data, unsigned pitch)
216 if (*fallback == NULL) {
217 *fallback = picture_NewFromFormat(&picture->format);
218 if (*fallback == NULL)
221 for (int n = 0; n < picture->i_planes; n++) {
222 const plane_t *src = &(*fallback)->p[n];
223 plane_t *dst = &picture->p[n];
224 dst->p_pixels = src->p_pixels;
225 dst->i_pitch = src->i_pitch;
226 dst->i_lines = src->i_lines;
230 /* fill in buffer info in first plane */
231 picture->p->p_pixels = data;
232 picture->p->i_pitch = pitch;
233 picture->p->i_lines = picture->format.i_height;
235 /* Fill chroma planes for planar YUV */
236 if (picture->format.i_chroma == VLC_CODEC_I420 ||
237 picture->format.i_chroma == VLC_CODEC_J420 ||
238 picture->format.i_chroma == VLC_CODEC_YV12) {
240 for (int n = 1; n < picture->i_planes; n++) {
241 const plane_t *o = &picture->p[n-1];
242 plane_t *p = &picture->p[n];
244 p->p_pixels = o->p_pixels + o->i_lines * o->i_pitch;
245 p->i_pitch = pitch / 2;
246 p->i_lines = picture->format.i_height / 2;
248 /* The dx/d3d buffer is always allocated as YV12 */
249 if (vlc_fourcc_AreUVPlanesSwapped(picture->format.i_chroma, VLC_CODEC_YV12)) {
250 uint8_t *p_tmp = picture->p[1].p_pixels;
251 picture->p[1].p_pixels = picture->p[2].p_pixels;
252 picture->p[2].p_pixels = p_tmp;
258 void AlignRect(RECT *r, int align_boundary, int align_size)
261 r->left = (r->left + align_boundary/2) & ~align_boundary;
263 r->right = ((r->right - r->left + align_size/2) & ~align_size) + r->left;
267 static void CommonChangeThumbnailClip(vout_display_t *vd, bool show)
269 vout_display_sys_t *sys = vd->sys;
271 /* Windows 7 taskbar thumbnail code */
272 OSVERSIONINFO winVer;
273 winVer.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
274 if (!GetVersionEx(&winVer) || winVer.dwMajorVersion <= 5)
280 if (S_OK == CoCreateInstance(&CLSID_TaskbarList,
281 NULL, CLSCTX_INPROC_SERVER,
284 ITaskbarList3 *taskbl = ptr;
285 taskbl->lpVtbl->HrInit(taskbl);
287 HWND hroot = GetAncestor(sys->hwnd,GA_ROOT);
291 GetWindowRect(sys->hvideownd, &video);
292 GetWindowRect(hroot, &parent);
293 relative.left = video.left - parent.left - 8;
294 relative.top = video.top - parent.top - 10;
296 relative.right = video.right - video.left + relative.left;
297 relative.bottom = video.bottom - video.top + relative.top - 25;
299 if (S_OK != taskbl->lpVtbl->SetThumbnailClip(taskbl, hroot,
300 show ? &relative : NULL))
301 msg_Err(vd, "SetThumbNailClip failed");
303 taskbl->lpVtbl->Release(taskbl);
308 /*****************************************************************************
309 * UpdateRects: update clipping rectangles
310 *****************************************************************************
311 * This function is called when the window position or size are changed, and
312 * its job is to update the source and destination RECTs used to display the
314 *****************************************************************************/
315 void UpdateRects(vout_display_t *vd,
316 const vout_display_cfg_t *cfg,
317 const video_format_t *source,
320 vout_display_sys_t *sys = vd->sys;
321 #define rect_src sys->rect_src
322 #define rect_src_clipped sys->rect_src_clipped
323 #define rect_dest sys->rect_dest
324 #define rect_dest_clipped sys->rect_dest_clipped
333 source = &vd->source;
335 /* Retrieve the window size */
336 GetClientRect(sys->hwnd, &rect);
338 /* Retrieve the window position */
339 point.x = point.y = 0;
340 ClientToScreen(sys->hwnd, &point);
342 /* If nothing changed, we can return */
345 EventThreadUpdateWindowPosition(sys->event, &has_moved, &is_resized,
347 rect.right, rect.bottom);
349 vout_display_SendEventDisplaySize(vd, rect.right, rect.bottom, cfg->is_fullscreen);
350 if (!is_forced && !has_moved && !is_resized )
353 /* Update the window position and size */
354 vout_display_cfg_t place_cfg = *cfg;
355 place_cfg.display.width = rect.right;
356 place_cfg.display.height = rect.bottom;
358 vout_display_place_t place;
359 vout_display_PlacePicture(&place, source, &place_cfg, false);
361 EventThreadUpdateSourceAndPlace(sys->event, source, &place);
362 #if defined(MODULE_NAME_IS_wingapi)
363 if (place.width != vd->fmt.i_width || place.height != vd->fmt.i_height)
364 vout_display_SendEventPicturesInvalid(vd);
368 SetWindowPos(sys->hvideownd, 0,
369 place.x, place.y, place.width, place.height,
370 SWP_NOCOPYBITS|SWP_NOZORDER|SWP_ASYNCWINDOWPOS);
372 /* Destination image position and dimensions */
373 #if defined(MODULE_NAME_IS_direct3d) || defined(MODULE_NAME_IS_direct2d)
375 rect_dest.right = place.width;
377 rect_dest.bottom = place.height;
379 rect_dest.left = point.x + place.x;
380 rect_dest.right = rect_dest.left + place.width;
381 rect_dest.top = point.y + place.y;
382 rect_dest.bottom = rect_dest.top + place.height;
384 #ifdef MODULE_NAME_IS_directdraw
385 /* Apply overlay hardware constraints */
386 if (sys->use_overlay)
387 AlignRect(&rect_dest, sys->i_align_dest_boundary, sys->i_align_dest_size);
392 #if defined(MODULE_NAME_IS_directdraw)
393 /* UpdateOverlay directdraw function doesn't automatically clip to the
394 * display size so we need to do it otherwise it will fail */
396 /* Clip the destination window */
397 if (!IntersectRect(&rect_dest_clipped, &rect_dest,
398 &sys->rect_display)) {
399 SetRectEmpty(&rect_src_clipped);
404 msg_Dbg(vd, "DirectXUpdateRects image_dst_clipped coords:"
406 rect_dest_clipped.left, rect_dest_clipped.top,
407 rect_dest_clipped.right, rect_dest_clipped.bottom);
412 /* AFAIK, there are no clipping constraints in Direct3D, OpenGL and GDI */
413 rect_dest_clipped = rect_dest;
417 /* the 2 following lines are to fix a bug when clicking on the desktop */
418 if ((rect_dest_clipped.right - rect_dest_clipped.left) == 0 ||
419 (rect_dest_clipped.bottom - rect_dest_clipped.top) == 0) {
420 SetRectEmpty(&rect_src_clipped);
424 /* src image dimensions */
427 rect_src.right = source->i_width;
428 rect_src.bottom = source->i_height;
430 /* Clip the source image */
431 rect_src_clipped.left = source->i_x_offset +
432 (rect_dest_clipped.left - rect_dest.left) *
433 source->i_visible_width / (rect_dest.right - rect_dest.left);
434 rect_src_clipped.right = source->i_x_offset +
435 source->i_visible_width -
436 (rect_dest.right - rect_dest_clipped.right) *
437 source->i_visible_width / (rect_dest.right - rect_dest.left);
438 rect_src_clipped.top = source->i_y_offset +
439 (rect_dest_clipped.top - rect_dest.top) *
440 source->i_visible_height / (rect_dest.bottom - rect_dest.top);
441 rect_src_clipped.bottom = source->i_y_offset +
442 source->i_visible_height -
443 (rect_dest.bottom - rect_dest_clipped.bottom) *
444 source->i_visible_height / (rect_dest.bottom - rect_dest.top);
446 #ifdef MODULE_NAME_IS_directdraw
447 /* Apply overlay hardware constraints */
448 if (sys->use_overlay)
449 AlignRect(&rect_src_clipped, sys->i_align_src_boundary, sys->i_align_src_size);
450 #elif defined(MODULE_NAME_IS_direct3d) || defined(MODULE_NAME_IS_direct2d)
451 /* Needed at least with YUV content */
452 rect_src_clipped.left &= ~1;
453 rect_src_clipped.right &= ~1;
454 rect_src_clipped.top &= ~1;
455 rect_src_clipped.bottom &= ~1;
459 msg_Dbg(vd, "DirectXUpdateRects image_src_clipped"
460 " coords: %li,%li,%li,%li",
461 rect_src_clipped.left, rect_src_clipped.top,
462 rect_src_clipped.right, rect_src_clipped.bottom);
465 #ifdef MODULE_NAME_IS_directdraw
466 /* The destination coordinates need to be relative to the current
467 * directdraw primary surface (display) */
468 rect_dest_clipped.left -= sys->rect_display.left;
469 rect_dest_clipped.right -= sys->rect_display.left;
470 rect_dest_clipped.top -= sys->rect_display.top;
471 rect_dest_clipped.bottom -= sys->rect_display.top;
474 CommonChangeThumbnailClip(vd, true);
477 /* Signal the change in size/position */
478 sys->changes |= DX_POSITION_CHANGE;
481 #undef rect_src_clipped
483 #undef rect_dest_clipped
486 static int CommonControlSetFullscreen(vout_display_t *vd, bool is_fullscreen)
488 vout_display_sys_t *sys = vd->sys;
490 #ifdef MODULE_NAME_IS_direct3d
491 if (sys->use_desktop && is_fullscreen)
496 if (sys->parent_window)
497 return vout_window_SetFullScreen(sys->parent_window, is_fullscreen);
500 HWND hwnd = sys->hparent && sys->hfswnd ? sys->hfswnd : sys->hwnd;
502 /* Save the current windows placement/placement to restore
503 when fullscreen is over */
504 WINDOWPLACEMENT window_placement;
505 window_placement.length = sizeof(WINDOWPLACEMENT);
506 GetWindowPlacement(hwnd, &window_placement);
509 msg_Dbg(vd, "entering fullscreen mode");
511 /* Change window style, no borders and no title bar */
512 SetWindowLong(hwnd, GWL_STYLE, WS_CLIPCHILDREN | WS_VISIBLE);
515 /* Retrieve current window position so fullscreen will happen
516 *on the right screen */
517 HMONITOR hmon = MonitorFromWindow(sys->hparent,
518 MONITOR_DEFAULTTONEAREST);
520 mi.cbSize = sizeof(MONITORINFO);
521 if (GetMonitorInfo(hmon, &mi))
522 SetWindowPos(hwnd, 0,
525 mi.rcMonitor.right - mi.rcMonitor.left,
526 mi.rcMonitor.bottom - mi.rcMonitor.top,
527 SWP_NOZORDER|SWP_FRAMECHANGED);
529 /* Maximize non embedded window */
530 ShowWindow(hwnd, SW_SHOWMAXIMIZED);
534 /* Hide the previous window */
536 GetClientRect(hwnd, &rect);
537 SetParent(sys->hwnd, hwnd);
538 SetWindowPos(sys->hwnd, 0, 0, 0,
539 rect.right, rect.bottom,
540 SWP_NOZORDER|SWP_FRAMECHANGED);
542 HWND topLevelParent = GetAncestor(sys->hparent, GA_ROOT);
543 ShowWindow(topLevelParent, SW_HIDE);
545 SetForegroundWindow(hwnd);
547 msg_Dbg(vd, "leaving fullscreen mode");
549 /* Change window style, no borders and no title bar */
550 SetWindowLong(hwnd, GWL_STYLE, EventThreadGetWindowStyle(sys->event));
554 GetClientRect(sys->hparent, &rect);
555 SetParent(sys->hwnd, sys->hparent);
556 SetWindowPos(sys->hwnd, 0, 0, 0,
557 rect.right, rect.bottom,
558 SWP_NOZORDER|SWP_FRAMECHANGED);
560 HWND topLevelParent = GetAncestor(sys->hparent, GA_ROOT);
561 ShowWindow(topLevelParent, SW_SHOW);
562 SetForegroundWindow(sys->hparent);
563 ShowWindow(hwnd, SW_HIDE);
565 /* return to normal window for non embedded vout */
566 SetWindowPlacement(hwnd, &window_placement);
567 ShowWindow(hwnd, SW_SHOWNORMAL);
573 int CommonControl(vout_display_t *vd, int query, va_list args)
575 vout_display_sys_t *sys = vd->sys;
578 case VOUT_DISPLAY_CHANGE_DISPLAY_SIZE: /* const vout_display_cfg_t *p_cfg, int is_forced */
579 case VOUT_DISPLAY_CHANGE_DISPLAY_FILLED: /* const vout_display_cfg_t *p_cfg */
580 case VOUT_DISPLAY_CHANGE_ZOOM: /* const vout_display_cfg_t *p_cfg */
581 case VOUT_DISPLAY_CHANGE_SOURCE_ASPECT: /* const video_format_t *p_source */
582 case VOUT_DISPLAY_CHANGE_SOURCE_CROP: { /* const video_format_t *p_source */
583 const vout_display_cfg_t *cfg;
584 const video_format_t *source;
585 bool is_forced = true;
586 if (query == VOUT_DISPLAY_CHANGE_SOURCE_CROP ||
587 query == VOUT_DISPLAY_CHANGE_SOURCE_ASPECT) {
589 source = va_arg(args, const video_format_t *);
591 cfg = va_arg(args, const vout_display_cfg_t *);
592 source = &vd->source;
593 if (query == VOUT_DISPLAY_CHANGE_DISPLAY_SIZE)
594 is_forced = va_arg(args, int);
596 if (query == VOUT_DISPLAY_CHANGE_DISPLAY_SIZE && is_forced) {
597 /* Update dimensions */
598 if (sys->parent_window) {
599 vout_window_SetSize(sys->parent_window, cfg->display.width, cfg->display.height);
603 rect_window.left = 0;
604 rect_window.right = cfg->display.width;
605 rect_window.bottom = cfg->display.height;
606 AdjustWindowRect(&rect_window, EventThreadGetWindowStyle(sys->event), 0);
608 SetWindowPos(sys->hwnd, 0, 0, 0,
609 rect_window.right - rect_window.left,
610 rect_window.bottom - rect_window.top, SWP_NOMOVE);
613 UpdateRects(vd, cfg, source, is_forced);
616 case VOUT_DISPLAY_CHANGE_WINDOW_STATE: { /* unsigned state */
617 const unsigned state = va_arg(args, unsigned);
618 const bool is_on_top = (state & VOUT_WINDOW_STATE_ABOVE) != 0;
619 #ifdef MODULE_NAME_IS_direct3d
620 if (sys->use_desktop && is_on_top)
623 if (sys->parent_window) {
624 if (vout_window_SetState(sys->parent_window, state))
627 HMENU hMenu = GetSystemMenu(sys->hwnd, FALSE);
629 if (is_on_top && !(GetWindowLong(sys->hwnd, GWL_EXSTYLE) & WS_EX_TOPMOST)) {
630 CheckMenuItem(hMenu, IDM_TOGGLE_ON_TOP, MF_BYCOMMAND | MFS_CHECKED);
631 SetWindowPos(sys->hwnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE);
632 } else if (!is_on_top && (GetWindowLong(sys->hwnd, GWL_EXSTYLE) & WS_EX_TOPMOST)) {
633 CheckMenuItem(hMenu, IDM_TOGGLE_ON_TOP, MF_BYCOMMAND | MFS_UNCHECKED);
634 SetWindowPos(sys->hwnd, HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOSIZE|SWP_NOMOVE);
637 sys->is_on_top = is_on_top;
640 case VOUT_DISPLAY_CHANGE_FULLSCREEN: { /* const vout_display_cfg_t *p_cfg */
641 const vout_display_cfg_t *cfg = va_arg(args, const vout_display_cfg_t *);
642 if (CommonControlSetFullscreen(vd, cfg->is_fullscreen))
644 UpdateRects(vd, NULL, NULL, false);
648 case VOUT_DISPLAY_HIDE_MOUSE:
649 EventThreadMouseHide(sys->event);
651 case VOUT_DISPLAY_RESET_PICTURES:
658 static void DisableScreensaver(vout_display_t *vd)
660 vout_display_sys_t *sys = vd->sys;
662 /* disable screensaver by temporarily changing system settings */
663 sys->i_spi_screensaveactive = 0;
664 if (var_GetBool(vd, "disable-screensaver")) {
665 msg_Dbg(vd, "disabling screen saver");
666 SystemParametersInfo(SPI_GETSCREENSAVEACTIVE, 0,
667 &sys->i_spi_screensaveactive, 0);
669 if (LOWORD(GetVersion()) == 0x0005) {
670 /* If this is NT 5.0 (i.e., Win2K), we need to hack around
671 * KB318781 (see http://support.microsoft.com/kb/318781) */
675 if (ERROR_SUCCESS == RegOpenKeyEx(HKEY_CURRENT_USER,
676 TEXT("Control Panel\\Desktop"),
677 0, KEY_QUERY_VALUE, &hKeyCP) &&
678 ERROR_SUCCESS != RegQueryValueEx(hKeyCP, TEXT("SCRNSAVE.EXE"),
679 NULL, NULL, NULL, NULL)) {
680 sys->i_spi_screensaveactive = FALSE;
684 if (FALSE != sys->i_spi_screensaveactive) {
685 SystemParametersInfo(SPI_SETSCREENSAVEACTIVE, 0, NULL, 0);
690 static void RestoreScreensaver(vout_display_t *vd)
692 vout_display_sys_t *sys = vd->sys;
694 /* restore screensaver system settings */
695 if (0 != sys->i_spi_screensaveactive) {
696 SystemParametersInfo(SPI_SETSCREENSAVEACTIVE,
697 sys->i_spi_screensaveactive, NULL, 0);