]> git.sesse.net Git - vlc/blob - modules/video_output/msw/common.c
DirectX/GDI: use COINIT_MULTITHREADED instead of COINIT_APARTMENTTHREADED
[vlc] / modules / video_output / msw / common.c
1 /*****************************************************************************
2  * common.c: Windows video output common code
3  *****************************************************************************
4  * Copyright (C) 2001-2009 VLC authors and VideoLAN
5  * $Id$
6  *
7  * Authors: Gildas Bazin <gbazin@videolan.org>
8  *          Martell Malone <martellmalone@gmail.com>
9  *
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.
14  *
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.
19  *
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  *****************************************************************************/
24
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  *****************************************************************************/
30
31 #ifdef HAVE_CONFIG_H
32 # include "config.h"
33 #endif
34
35 #include <vlc_common.h>
36 #include <vlc_vout_display.h>
37
38 #include <windows.h>
39 #include <assert.h>
40
41 #include "common.h"
42
43 #include <vlc_windows_interfaces.h>
44
45 static void CommonChangeThumbnailClip(vout_display_t *, bool show);
46 static int  CommonControlSetFullscreen(vout_display_t *, bool is_fullscreen);
47
48 static void DisableScreensaver(vout_display_t *);
49 static void RestoreScreensaver(vout_display_t *);
50
51 /* */
52 int CommonInit(vout_display_t *vd)
53 {
54     vout_display_sys_t *sys = vd->sys;
55
56     sys->hwnd      = NULL;
57     sys->hvideownd = NULL;
58     sys->hparent   = NULL;
59     sys->hfswnd    = NULL;
60     sys->changes   = 0;
61     SetRectEmpty(&sys->rect_display);
62     SetRectEmpty(&sys->rect_parent);
63     sys->is_first_display = true;
64     sys->is_on_top = false;
65
66     var_Create(vd, "video-deco", VLC_VAR_BOOL | VLC_VAR_DOINHERIT);
67     var_Create(vd, "disable-screensaver", VLC_VAR_BOOL | VLC_VAR_DOINHERIT);
68
69     /* */
70     sys->event = EventThreadCreate(vd);
71     if (!sys->event)
72         return VLC_EGENERIC;
73
74     event_cfg_t cfg;
75     memset(&cfg, 0, sizeof(cfg));
76 #ifdef MODULE_NAME_IS_direct3d9
77     cfg.use_desktop = sys->use_desktop;
78 #endif
79 #ifdef MODULE_NAME_IS_directdraw
80     cfg.use_overlay = sys->use_overlay;
81 #endif
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;
86
87     event_hwnd_t hwnd;
88     if (EventThreadStart(sys->event, &hwnd, &cfg))
89         return VLC_EGENERIC;
90
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;
96
97     if (vd->cfg->is_fullscreen) {
98         if (CommonControlSetFullscreen(vd, true))
99             vout_display_SendEventFullscreen(vd, false);
100     }
101
102     DisableScreensaver (vd);
103
104     return VLC_SUCCESS;
105 }
106
107 /* */
108 void CommonClean(vout_display_t *vd)
109 {
110     vout_display_sys_t *sys = vd->sys;
111
112     if (sys->event) {
113         CommonChangeThumbnailClip(vd, false);
114         EventThreadStop(sys->event);
115         EventThreadDestroy(sys->event);
116     }
117
118     RestoreScreensaver(vd);
119 }
120
121 /* */
122 picture_pool_t *CommonPool(vout_display_t *vd, unsigned count)
123 {
124     VLC_UNUSED(count);
125     return vd->sys->pool;
126 }
127
128 void CommonManage(vout_display_t *vd)
129 {
130     vout_display_sys_t *sys = vd->sys;
131
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. */
136
137     /* If we do not control our window, we check for geometry changes
138      * ourselves because the parent might not send us its events. */
139     if (sys->hparent) {
140         RECT rect_parent;
141         POINT point;
142
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);
148
149         if (!EqualRect(&rect_parent, &sys->rect_parent)) {
150             sys->rect_parent = rect_parent;
151
152             /* This code deals with both resize and move
153              *
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)
157              *
158              * For directdraw, it is still important to call UpdateRects
159              * on a move of the parent window, even if no resize occurred
160              */
161             SetWindowPos(sys->hwnd, 0, 0, 0,
162                          rect_parent.right - rect_parent.left,
163                          rect_parent.bottom - rect_parent.top,
164                          SWP_NOZORDER);
165
166             UpdateRects(vd, NULL, NULL, true);
167         }
168     }
169
170     /* HasMoved means here resize or move */
171     if (EventThreadGetAndResetHasMoved(sys->event))
172         UpdateRects(vd, NULL, NULL, false);
173 }
174
175 /**
176  * It ensures that the video window is shown after the first picture
177  * is displayed.
178  */
179 void CommonDisplay(vout_display_t *vd)
180 {
181     vout_display_sys_t *sys = vd->sys;
182
183     if (!sys->is_first_display)
184         return;
185
186     /* Video window is initially hidden, show it now since we got a
187      * picture to show.
188      */
189     SetWindowPos(sys->hvideownd, 0, 0, 0, 0, 0,
190                  SWP_ASYNCWINDOWPOS|
191                  SWP_FRAMECHANGED|
192                  SWP_SHOWWINDOW|
193                  SWP_NOMOVE|
194                  SWP_NOSIZE|
195                  SWP_NOZORDER);
196     sys->is_first_display = false;
197 }
198
199 /**
200  * It updates a picture data/pitches.
201  */
202 int CommonUpdatePicture(picture_t *picture, picture_t **fallback,
203                         uint8_t *data, unsigned pitch)
204 {
205     if (fallback) {
206         if (*fallback == NULL) {
207             *fallback = picture_NewFromFormat(&picture->format);
208             if (*fallback == NULL)
209                 return VLC_EGENERIC;
210         }
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;
217         }
218         return VLC_SUCCESS;
219     }
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;
224
225     /*  Fill chroma planes for biplanar YUV */
226     if (picture->format.i_chroma == VLC_CODEC_NV12 ||
227         picture->format.i_chroma == VLC_CODEC_NV21) {
228
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];
232
233             p->p_pixels = o->p_pixels + o->i_lines * o->i_pitch;
234             p->i_pitch  = pitch;
235             p->i_lines  = picture->format.i_visible_height;
236         }
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 */
240             return VLC_EGENERIC;
241         }
242     }
243
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) {
248
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];
252
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;
256         }
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;
262         }
263     }
264     return VLC_SUCCESS;
265 }
266
267 void AlignRect(RECT *r, int align_boundary, int align_size)
268 {
269     if (align_boundary)
270         r->left = (r->left + align_boundary/2) & ~align_boundary;
271     if (align_size)
272         r->right = ((r->right - r->left + align_size/2) & ~align_size) + r->left;
273 }
274
275 /* */
276 static void CommonChangeThumbnailClip(vout_display_t *vd, bool show)
277 {
278     vout_display_sys_t *sys = vd->sys;
279
280     /* Windows 7 taskbar thumbnail code */
281     OSVERSIONINFO winVer;
282     winVer.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
283     if (!GetVersionEx(&winVer) || winVer.dwMajorVersion <= 5)
284         return;
285
286     if( FAILED(CoInitializeEx(NULL, COINIT_MULTITHREADED)) )
287         vlc_assert_unreachable();
288
289     void *ptr;
290     if (S_OK == CoCreateInstance(&CLSID_TaskbarList,
291                                  NULL, CLSCTX_INPROC_SERVER,
292                                  &IID_ITaskbarList3,
293                                  &ptr)) {
294         ITaskbarList3 *taskbl = ptr;
295         taskbl->lpVtbl->HrInit(taskbl);
296
297         HWND hroot = GetAncestor(sys->hwnd,GA_ROOT);
298         RECT relative;
299         if (show) {
300             RECT video, parent;
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;
305
306             relative.right  = video.right  - video.left + relative.left;
307             relative.bottom = video.bottom - video.top  + relative.top - 25;
308         }
309         if (S_OK != taskbl->lpVtbl->SetThumbnailClip(taskbl, hroot,
310                                                  show ? &relative : NULL))
311             msg_Err(vd, "SetThumbNailClip failed");
312
313         taskbl->lpVtbl->Release(taskbl);
314     }
315     CoUninitialize();
316 }
317
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
323  * picture.
324  *****************************************************************************/
325 void UpdateRects(vout_display_t *vd,
326                   const vout_display_cfg_t *cfg,
327                   const video_format_t *source,
328                   bool is_forced)
329 {
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
335
336     RECT  rect;
337     POINT point;
338
339     /* */
340     if (!cfg)
341         cfg = vd->cfg;
342     if (!source)
343         source = &vd->source;
344
345     /* Retrieve the window size */
346     GetClientRect(sys->hwnd, &rect);
347
348     /* Retrieve the window position */
349     point.x = point.y = 0;
350     ClientToScreen(sys->hwnd, &point);
351
352     /* If nothing changed, we can return */
353     bool has_moved;
354     bool is_resized;
355     EventThreadUpdateWindowPosition(sys->event, &has_moved, &is_resized,
356                                     point.x, point.y,
357                                     rect.right, rect.bottom);
358     if (is_resized)
359         vout_display_SendEventDisplaySize(vd, rect.right, rect.bottom);
360     if (!is_forced && !has_moved && !is_resized)
361         return;
362
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;
367
368     vout_display_place_t place;
369     vout_display_PlacePicture(&place, source, &place_cfg, false);
370
371     EventThreadUpdateSourceAndPlace(sys->event, source, &place);
372
373     if (sys->hvideownd)
374         SetWindowPos(sys->hvideownd, 0,
375                      place.x, place.y, place.width, place.height,
376                      SWP_NOCOPYBITS|SWP_NOZORDER|SWP_ASYNCWINDOWPOS);
377
378     /* Destination image position and dimensions */
379 #if defined(MODULE_NAME_IS_direct3d9) || defined(MODULE_NAME_IS_direct3d11) || defined(MODULE_NAME_IS_direct2d)
380     rect_dest.left   = 0;
381     rect_dest.right  = place.width;
382     rect_dest.top    = 0;
383     rect_dest.bottom = place.height;
384 #else
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;
389
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);
394 #endif
395
396 #endif
397
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 */
401
402     /* Clip the destination window */
403     if (!IntersectRect(&rect_dest_clipped, &rect_dest,
404                        &sys->rect_display)) {
405         SetRectEmpty(&rect_src_clipped);
406         goto exit;
407     }
408
409 #ifndef NDEBUG
410     msg_Dbg(vd, "DirectXUpdateRects image_dst_clipped coords:"
411                 " %li,%li,%li,%li",
412                 rect_dest_clipped.left, rect_dest_clipped.top,
413                 rect_dest_clipped.right, rect_dest_clipped.bottom);
414 #endif
415
416 #else
417
418     /* AFAIK, there are no clipping constraints in Direct3D, OpenGL and GDI */
419     rect_dest_clipped = rect_dest;
420
421 #endif
422
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);
427         goto exit;
428     }
429
430     /* src image dimensions */
431     rect_src.left   = 0;
432     rect_src.top    = 0;
433     rect_src.right  = vd->fmt.i_visible_width;
434     rect_src.bottom = vd->fmt.i_visible_height;
435
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);
451
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;
462 #endif
463
464 #ifndef NDEBUG
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);
485 #endif
486
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;
494 #endif
495
496     CommonChangeThumbnailClip(vd, true);
497
498 exit:
499     /* Signal the change in size/position */
500     sys->changes |= DX_POSITION_CHANGE;
501
502 #undef rect_src
503 #undef rect_src_clipped
504 #undef rect_dest
505 #undef rect_dest_clipped
506 }
507
508 static int CommonControlSetFullscreen(vout_display_t *vd, bool is_fullscreen)
509 {
510     vout_display_sys_t *sys = vd->sys;
511
512 #ifdef MODULE_NAME_IS_direct3d9
513     if (sys->use_desktop && is_fullscreen)
514         return VLC_EGENERIC;
515 #endif
516
517     /* */
518     if (sys->parent_window)
519         return VLC_EGENERIC;
520
521     /* */
522     HWND hwnd = sys->hparent && sys->hfswnd ? sys->hfswnd : sys->hwnd;
523
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);
529
530     if (is_fullscreen) {
531         msg_Dbg(vd, "entering fullscreen mode");
532
533         /* Change window style, no borders and no title bar */
534         SetWindowLong(hwnd, GWL_STYLE, WS_CLIPCHILDREN | WS_VISIBLE);
535
536         if (sys->hparent) {
537             /* Retrieve current window position so fullscreen will happen
538             *on the right screen */
539             HMONITOR hmon = MonitorFromWindow(sys->hparent,
540                                               MONITOR_DEFAULTTONEAREST);
541             MONITORINFO mi;
542             mi.cbSize = sizeof(MONITORINFO);
543             if (GetMonitorInfo(hmon, &mi))
544                 SetWindowPos(hwnd, 0,
545                              mi.rcMonitor.left,
546                              mi.rcMonitor.top,
547                              mi.rcMonitor.right - mi.rcMonitor.left,
548                              mi.rcMonitor.bottom - mi.rcMonitor.top,
549                              SWP_NOZORDER|SWP_FRAMECHANGED);
550         } else {
551             /* Maximize non embedded window */
552             ShowWindow(hwnd, SW_SHOWMAXIMIZED);
553         }
554
555         if (sys->hparent) {
556             /* Hide the previous window */
557             RECT rect;
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);
563
564             HWND topLevelParent = GetAncestor(sys->hparent, GA_ROOT);
565             ShowWindow(topLevelParent, SW_HIDE);
566         }
567         SetForegroundWindow(hwnd);
568     } else {
569         msg_Dbg(vd, "leaving fullscreen mode");
570
571         /* Change window style, no borders and no title bar */
572         SetWindowLong(hwnd, GWL_STYLE, EventThreadGetWindowStyle(sys->event));
573
574         if (sys->hparent) {
575             RECT rect;
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);
581
582             HWND topLevelParent = GetAncestor(sys->hparent, GA_ROOT);
583             ShowWindow(topLevelParent, SW_SHOW);
584             SetForegroundWindow(sys->hparent);
585             ShowWindow(hwnd, SW_HIDE);
586         } else {
587             /* return to normal window for non embedded vout */
588             SetWindowPlacement(hwnd, &window_placement);
589             ShowWindow(hwnd, SW_SHOWNORMAL);
590         }
591     }
592     return VLC_SUCCESS;
593 }
594
595 int CommonControl(vout_display_t *vd, int query, va_list args)
596 {
597     vout_display_sys_t *sys = vd->sys;
598
599     switch (query) {
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 *);
603         RECT rect_window = {
604             .top    = 0,
605             .left   = 0,
606             .right  = cfg->display.width,
607             .bottom = cfg->display.height,
608         };
609
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);
615         return VLC_SUCCESS;
616     }
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;
622
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 *);
626             cfg    = vd->cfg;
627             UpdateRects(vd, cfg, source, true);
628         } else {
629             cfg    = va_arg(args, const vout_display_cfg_t *);
630             UpdateRects(vd, cfg, NULL, false);
631         }
632         return VLC_SUCCESS;
633     }
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)
639             return VLC_EGENERIC;
640 #endif
641         HMENU hMenu = GetSystemMenu(sys->hwnd, FALSE);
642
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);
649         }
650         sys->is_on_top = is_on_top;
651         return VLC_SUCCESS;
652     }
653     case VOUT_DISPLAY_CHANGE_FULLSCREEN: {
654         bool fs = va_arg(args, int);
655         if (CommonControlSetFullscreen(vd, fs))
656             return VLC_EGENERIC;
657         UpdateRects(vd, NULL, NULL, false);
658         return VLC_SUCCESS;
659     }
660
661     case VOUT_DISPLAY_HIDE_MOUSE:
662         EventThreadMouseHide(sys->event);
663         return VLC_SUCCESS;
664     case VOUT_DISPLAY_RESET_PICTURES:
665         vlc_assert_unreachable();
666     default:
667         return VLC_EGENERIC;
668     }
669 }
670
671 static void DisableScreensaver(vout_display_t *vd)
672 {
673     vout_display_sys_t *sys = vd->sys;
674
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);
681
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) */
685
686             HKEY hKeyCP = NULL;
687
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;
694             }
695         }
696
697         if (FALSE != sys->i_spi_screensaveactive) {
698             SystemParametersInfo(SPI_SETSCREENSAVEACTIVE, 0, NULL, 0);
699         }
700     }
701 }
702
703 static void RestoreScreensaver(vout_display_t *vd)
704 {
705     vout_display_sys_t *sys = vd->sys;
706
707     /* restore screensaver system settings */
708     if (0 != sys->i_spi_screensaveactive) {
709         SystemParametersInfo(SPI_SETSCREENSAVEACTIVE,
710                              sys->i_spi_screensaveactive, NULL, 0);
711     }
712 }