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 void CommonUpdatePicture(picture_t *picture,
221 uint8_t *data, unsigned pitch)
223 /* fill in buffer info in first plane */
224 picture->p->p_pixels = data;
225 picture->p->i_pitch = pitch;
226 picture->p->i_lines = picture->format.i_height;
228 /* Fill chroma planes for planar YUV */
229 if (picture->format.i_chroma == VLC_CODEC_I420 ||
230 picture->format.i_chroma == VLC_CODEC_J420 ||
231 picture->format.i_chroma == VLC_CODEC_YV12) {
233 for (int n = 1; n < picture->i_planes; n++) {
234 const plane_t *o = &picture->p[n-1];
235 plane_t *p = &picture->p[n];
237 p->p_pixels = o->p_pixels + o->i_lines * o->i_pitch;
238 p->i_pitch = pitch / 2;
239 p->i_lines = picture->format.i_height / 2;
241 /* The dx/d3d buffer is always allocated as YV12 */
242 if (vlc_fourcc_AreUVPlanesSwapped(picture->format.i_chroma, VLC_CODEC_YV12)) {
243 uint8_t *p_tmp = picture->p[1].p_pixels;
244 picture->p[1].p_pixels = picture->p[2].p_pixels;
245 picture->p[2].p_pixels = p_tmp;
250 void AlignRect(RECT *r, int align_boundary, int align_size)
253 r->left = (r->left + align_boundary/2) & ~align_boundary;
255 r->right = ((r->right - r->left + align_size/2) & ~align_size) + r->left;
259 static void CommonChangeThumbnailClip(vout_display_t *vd, bool show)
262 vout_display_sys_t *sys = vd->sys;
264 /* Windows 7 taskbar thumbnail code */
265 OSVERSIONINFO winVer;
266 winVer.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
267 if (!GetVersionEx(&winVer) || winVer.dwMajorVersion <= 5)
272 LPTASKBARLIST3 taskbl;
273 if (S_OK == CoCreateInstance(&clsid_ITaskbarList,
274 NULL, CLSCTX_INPROC_SERVER,
277 taskbl->vt->HrInit(taskbl);
279 HWND hroot = GetAncestor(sys->hwnd,GA_ROOT);
283 GetWindowRect(sys->hvideownd, &video);
284 GetWindowRect(hroot, &parent);
285 relative.left = video.left - parent.left - 8;
286 relative.top = video.top - parent.top - 10;
288 relative.right = video.right - video.left + relative.left;
289 relative.bottom = video.bottom - video.top + relative.top - 25;
291 if (S_OK != taskbl->vt->SetThumbnailClip(taskbl, hroot,
292 show ? &relative : NULL))
293 msg_Err(vd, "SetThumbNailClip failed");
295 taskbl->vt->Release(taskbl);
301 /*****************************************************************************
302 * UpdateRects: update clipping rectangles
303 *****************************************************************************
304 * This function is called when the window position or size are changed, and
305 * its job is to update the source and destination RECTs used to display the
307 *****************************************************************************/
308 void UpdateRects(vout_display_t *vd,
309 const vout_display_cfg_t *cfg,
310 const video_format_t *source,
313 vout_display_sys_t *sys = vd->sys;
314 #define rect_src sys->rect_src
315 #define rect_src_clipped sys->rect_src_clipped
316 #define rect_dest sys->rect_dest
317 #define rect_dest_clipped sys->rect_dest_clipped
326 source = &vd->source;
328 /* Retrieve the window size */
329 GetClientRect(sys->hwnd, &rect);
331 /* Retrieve the window position */
332 point.x = point.y = 0;
333 ClientToScreen(sys->hwnd, &point);
335 /* If nothing changed, we can return */
338 EventThreadUpdateWindowPosition(sys->event, &has_moved, &is_resized,
340 rect.right, rect.bottom);
342 vout_display_SendEventDisplaySize(vd, rect.right, rect.bottom, cfg->is_fullscreen);
343 if (!is_forced && !has_moved && !is_resized )
346 /* Update the window position and size */
347 vout_display_cfg_t place_cfg = *cfg;
348 place_cfg.display.width = rect.right;
349 place_cfg.display.height = rect.bottom;
351 vout_display_place_t place;
352 vout_display_PlacePicture(&place, source, &place_cfg, false);
354 EventThreadUpdateSourceAndPlace(sys->event, source, &place);
355 #if defined(MODULE_NAME_IS_wingapi)
356 if (place.width != vd->fmt.i_width || place.height != vd->fmt.i_height)
357 vout_display_SendEventPicturesInvalid(vd);
361 SetWindowPos(sys->hvideownd, 0,
362 place.x, place.y, place.width, place.height,
363 SWP_NOCOPYBITS|SWP_NOZORDER|SWP_ASYNCWINDOWPOS);
365 /* Destination image position and dimensions */
366 #if defined(MODULE_NAME_IS_direct3d)
368 rect_dest.right = place.width;
370 rect_dest.bottom = place.height;
372 rect_dest.left = point.x + place.x;
373 rect_dest.right = rect_dest.left + place.width;
374 rect_dest.top = point.y + place.y;
375 rect_dest.bottom = rect_dest.top + place.height;
377 #ifdef MODULE_NAME_IS_directx
378 /* Apply overlay hardware constraints */
379 if (sys->use_overlay)
380 AlignRect(&rect_dest, sys->i_align_dest_boundary, sys->i_align_dest_size);
385 #if defined(MODULE_NAME_IS_directx) || defined(MODULE_NAME_IS_direct3d)
386 /* UpdateOverlay directdraw function doesn't automatically clip to the
387 * display size so we need to do it otherwise it will fail
388 * It is also needed for d3d to avoid exceding our surface size */
390 /* Clip the destination window */
391 if (!IntersectRect(&rect_dest_clipped, &rect_dest,
392 &sys->rect_display)) {
393 SetRectEmpty(&rect_src_clipped);
398 msg_Dbg(vd, "DirectXUpdateRects image_dst_clipped coords:"
400 rect_dest_clipped.left, rect_dest_clipped.top,
401 rect_dest_clipped.right, rect_dest_clipped.bottom);
406 /* AFAIK, there are no clipping constraints in Direct3D, OpenGL and GDI */
407 rect_dest_clipped = rect_dest;
411 /* the 2 following lines are to fix a bug when clicking on the desktop */
412 if ((rect_dest_clipped.right - rect_dest_clipped.left) == 0 ||
413 (rect_dest_clipped.bottom - rect_dest_clipped.top) == 0) {
414 SetRectEmpty(&rect_src_clipped);
418 /* src image dimensions */
421 rect_src.right = source->i_width;
422 rect_src.bottom = source->i_height;
424 /* Clip the source image */
425 rect_src_clipped.left = source->i_x_offset +
426 (rect_dest_clipped.left - rect_dest.left) *
427 source->i_visible_width / (rect_dest.right - rect_dest.left);
428 rect_src_clipped.right = source->i_x_offset +
429 source->i_visible_width -
430 (rect_dest.right - rect_dest_clipped.right) *
431 source->i_visible_width / (rect_dest.right - rect_dest.left);
432 rect_src_clipped.top = source->i_y_offset +
433 (rect_dest_clipped.top - rect_dest.top) *
434 source->i_visible_height / (rect_dest.bottom - rect_dest.top);
435 rect_src_clipped.bottom = source->i_y_offset +
436 source->i_visible_height -
437 (rect_dest.bottom - rect_dest_clipped.bottom) *
438 source->i_visible_height / (rect_dest.bottom - rect_dest.top);
440 #ifdef MODULE_NAME_IS_directx
441 /* Apply overlay hardware constraints */
442 if (sys->use_overlay)
443 AlignRect(&rect_src_clipped, sys->i_align_src_boundary, sys->i_align_src_size);
444 #elif defined(MODULE_NAME_IS_direct3d)
445 /* Needed at least with YUV content */
446 rect_src_clipped.left &= ~1;
447 rect_src_clipped.right &= ~1;
448 rect_src_clipped.top &= ~1;
449 rect_src_clipped.bottom &= ~1;
453 msg_Dbg(vd, "DirectXUpdateRects image_src_clipped"
454 " coords: %li,%li,%li,%li",
455 rect_src_clipped.left, rect_src_clipped.top,
456 rect_src_clipped.right, rect_src_clipped.bottom);
459 #ifdef MODULE_NAME_IS_directx
460 /* The destination coordinates need to be relative to the current
461 * directdraw primary surface (display) */
462 rect_dest_clipped.left -= sys->rect_display.left;
463 rect_dest_clipped.right -= sys->rect_display.left;
464 rect_dest_clipped.top -= sys->rect_display.top;
465 rect_dest_clipped.bottom -= sys->rect_display.top;
468 CommonChangeThumbnailClip(vd, true);
471 /* Signal the change in size/position */
472 sys->changes |= DX_POSITION_CHANGE;
475 #undef rect_src_clipped
477 #undef rect_dest_clipped
480 static int CommonControlSetFullscreen(vout_display_t *vd, bool is_fullscreen)
482 vout_display_sys_t *sys = vd->sys;
484 #ifdef MODULE_NAME_IS_direct3d
485 if (sys->use_desktop && is_fullscreen)
490 if (sys->parent_window)
491 return vout_window_SetFullScreen(sys->parent_window, is_fullscreen);
494 HWND hwnd = sys->hparent && sys->hfswnd ? sys->hfswnd : sys->hwnd;
496 /* Save the current windows placement/placement to restore
497 when fullscreen is over */
498 WINDOWPLACEMENT window_placement;
499 window_placement.length = sizeof(WINDOWPLACEMENT);
500 GetWindowPlacement(hwnd, &window_placement);
503 msg_Dbg(vd, "entering fullscreen mode");
505 /* Change window style, no borders and no title bar */
506 SetWindowLong(hwnd, GWL_STYLE, WS_CLIPCHILDREN | WS_VISIBLE);
512 ClientToScreen(sys->hwnd, &point);
513 GetClientRect(sys->hwnd, &rect);
514 SetWindowPos(hwnd, 0, point.x, point.y,
515 rect.right, rect.bottom,
516 SWP_NOZORDER|SWP_FRAMECHANGED);
518 /* Retrieve current window position so fullscreen will happen
519 *on the right screen */
520 HMONITOR hmon = MonitorFromWindow(sys->hparent,
521 MONITOR_DEFAULTTONEAREST);
523 mi.cbSize = sizeof(MONITORINFO);
524 if (GetMonitorInfo(hmon, &mi))
525 SetWindowPos(hwnd, 0,
528 mi.rcMonitor.right - mi.rcMonitor.left,
529 mi.rcMonitor.bottom - mi.rcMonitor.top,
530 SWP_NOZORDER|SWP_FRAMECHANGED);
533 /* Maximize non embedded window */
534 ShowWindow(hwnd, SW_SHOWMAXIMIZED);
538 /* Hide the previous window */
540 GetClientRect(hwnd, &rect);
541 SetParent(sys->hwnd, hwnd);
542 SetWindowPos(sys->hwnd, 0, 0, 0,
543 rect.right, rect.bottom,
544 SWP_NOZORDER|SWP_FRAMECHANGED);
547 HWND topLevelParent = GetParent(sys->hparent);
549 HWND topLevelParent = GetAncestor(sys->hparent, GA_ROOT);
551 ShowWindow(topLevelParent, SW_HIDE);
553 SetForegroundWindow(hwnd);
555 msg_Dbg(vd, "leaving fullscreen mode");
557 /* Change window style, no borders and no title bar */
558 SetWindowLong(hwnd, GWL_STYLE, EventThreadGetWindowStyle(sys->event));
562 GetClientRect(sys->hparent, &rect);
563 SetParent(sys->hwnd, sys->hparent);
564 SetWindowPos(sys->hwnd, 0, 0, 0,
565 rect.right, rect.bottom,
566 SWP_NOZORDER|SWP_FRAMECHANGED);
569 HWND topLevelParent = GetParent(sys->hparent);
571 HWND topLevelParent = GetAncestor(sys->hparent, GA_ROOT);
573 ShowWindow(topLevelParent, SW_SHOW);
574 SetForegroundWindow(sys->hparent);
575 ShowWindow(hwnd, SW_HIDE);
577 /* return to normal window for non embedded vout */
578 SetWindowPlacement(hwnd, &window_placement);
579 ShowWindow(hwnd, SW_SHOWNORMAL);
585 int CommonControl(vout_display_t *vd, int query, va_list args)
587 vout_display_sys_t *sys = vd->sys;
590 case VOUT_DISPLAY_CHANGE_DISPLAY_SIZE: /* const vout_display_cfg_t *p_cfg, int is_forced */
591 case VOUT_DISPLAY_CHANGE_DISPLAY_FILLED: /* const vout_display_cfg_t *p_cfg */
592 case VOUT_DISPLAY_CHANGE_ZOOM: /* const vout_display_cfg_t *p_cfg */
593 case VOUT_DISPLAY_CHANGE_SOURCE_ASPECT: /* const video_format_t *p_source */
594 case VOUT_DISPLAY_CHANGE_SOURCE_CROP: { /* const video_format_t *p_source */
595 const vout_display_cfg_t *cfg;
596 const video_format_t *source;
597 bool is_forced = true;
598 if (query == VOUT_DISPLAY_CHANGE_SOURCE_CROP ||
599 query == VOUT_DISPLAY_CHANGE_SOURCE_ASPECT) {
601 source = va_arg(args, const video_format_t *);
603 cfg = va_arg(args, const vout_display_cfg_t *);
604 source = &vd->source;
605 if (query == VOUT_DISPLAY_CHANGE_DISPLAY_SIZE)
606 is_forced = va_arg(args, int);
608 if (query == VOUT_DISPLAY_CHANGE_DISPLAY_SIZE && is_forced) {
609 /* Update dimensions */
610 if (sys->parent_window) {
611 vout_window_SetSize(sys->parent_window, cfg->display.width, cfg->display.height);
615 rect_window.left = 0;
616 rect_window.right = cfg->display.width;
617 rect_window.bottom = cfg->display.height;
618 AdjustWindowRect(&rect_window, EventThreadGetWindowStyle(sys->event), 0);
620 SetWindowPos(sys->hwnd, 0, 0, 0,
621 rect_window.right - rect_window.left,
622 rect_window.bottom - rect_window.top, SWP_NOMOVE);
625 UpdateRects(vd, cfg, source, is_forced);
628 case VOUT_DISPLAY_CHANGE_WINDOW_STATE: { /* unsigned state */
629 const unsigned state = va_arg(args, unsigned);
630 const bool is_on_top = (state & VOUT_WINDOW_STATE_ABOVE) != 0;
631 #ifdef MODULE_NAME_IS_direct3d
632 if (sys->use_desktop && is_on_top)
635 if (sys->parent_window) {
636 if (vout_window_SetState(sys->parent_window, state))
639 HMENU hMenu = GetSystemMenu(sys->hwnd, FALSE);
641 if (is_on_top && !(GetWindowLong(sys->hwnd, GWL_EXSTYLE) & WS_EX_TOPMOST)) {
642 CheckMenuItem(hMenu, IDM_TOGGLE_ON_TOP, MF_BYCOMMAND | MFS_CHECKED);
643 SetWindowPos(sys->hwnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE);
644 } else if (!is_on_top && (GetWindowLong(sys->hwnd, GWL_EXSTYLE) & WS_EX_TOPMOST)) {
645 CheckMenuItem(hMenu, IDM_TOGGLE_ON_TOP, MF_BYCOMMAND | MFS_UNCHECKED);
646 SetWindowPos(sys->hwnd, HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOSIZE|SWP_NOMOVE);
649 sys->is_on_top = is_on_top;
652 case VOUT_DISPLAY_CHANGE_FULLSCREEN: { /* const vout_display_cfg_t *p_cfg */
653 const vout_display_cfg_t *cfg = va_arg(args, const vout_display_cfg_t *);
654 if (CommonControlSetFullscreen(vd, cfg->is_fullscreen))
656 UpdateRects(vd, NULL, NULL, false);
660 case VOUT_DISPLAY_HIDE_MOUSE:
661 EventThreadMouseHide(sys->event);
663 case VOUT_DISPLAY_RESET_PICTURES:
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_lowpowertimeout = 0;
677 sys->i_spi_powerofftimeout = 0;
678 sys->i_spi_screensavetimeout = 0;
679 if (var_GetBool(vd, "disable-screensaver")) {
680 msg_Dbg(vd, "disabling screen saver");
681 SystemParametersInfo(SPI_GETLOWPOWERTIMEOUT, 0,
682 &sys->i_spi_lowpowertimeout, 0);
683 if (0 != sys->i_spi_lowpowertimeout) {
684 SystemParametersInfo(SPI_SETLOWPOWERTIMEOUT, 0, NULL, 0);
686 SystemParametersInfo(SPI_GETPOWEROFFTIMEOUT, 0,
687 &sys->i_spi_powerofftimeout, 0);
688 if (0 != sys->i_spi_powerofftimeout) {
689 SystemParametersInfo(SPI_SETPOWEROFFTIMEOUT, 0, NULL, 0);
691 SystemParametersInfo(SPI_GETSCREENSAVETIMEOUT, 0,
692 &sys->i_spi_screensavetimeout, 0);
693 if (0 != sys->i_spi_screensavetimeout) {
694 SystemParametersInfo(SPI_SETSCREENSAVETIMEOUT, 0, NULL, 0);
699 static void RestoreScreensaver(vout_display_t *vd)
701 vout_display_sys_t *sys = vd->sys;
703 /* restore screensaver system settings */
704 if (0 != sys->i_spi_lowpowertimeout) {
705 SystemParametersInfo(SPI_SETLOWPOWERTIMEOUT,
706 sys->i_spi_lowpowertimeout, NULL, 0);
708 if (0 != sys->i_spi_powerofftimeout) {
709 SystemParametersInfo(SPI_SETPOWEROFFTIMEOUT,
710 sys->i_spi_powerofftimeout, NULL, 0);
712 if (0 != sys->i_spi_screensavetimeout) {
713 SystemParametersInfo(SPI_SETSCREENSAVETIMEOUT,
714 sys->i_spi_screensavetimeout, NULL, 0);