]> git.sesse.net Git - vlc/blob - modules/video_output/msw/common.c
Win32: use visible dimensions for all vouts
[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  *
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.
13  *
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.
18  *
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  *****************************************************************************/
23
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  *****************************************************************************/
29
30 #ifdef HAVE_CONFIG_H
31 # include "config.h"
32 #endif
33
34 #include <vlc_common.h>
35 #include <vlc_vout_display.h>
36
37 #include <windows.h>
38 #include <assert.h>
39
40 #include "common.h"
41
42 #include <vlc_windows_interfaces.h>
43
44 static void CommonChangeThumbnailClip(vout_display_t *, bool show);
45 static int  CommonControlSetFullscreen(vout_display_t *, bool is_fullscreen);
46
47 static void DisableScreensaver(vout_display_t *);
48 static void RestoreScreensaver(vout_display_t *);
49
50 /* */
51 int CommonInit(vout_display_t *vd)
52 {
53     vout_display_sys_t *sys = vd->sys;
54
55     sys->hwnd      = NULL;
56     sys->hvideownd = NULL;
57     sys->hparent   = NULL;
58     sys->hfswnd    = NULL;
59     sys->changes   = 0;
60     SetRectEmpty(&sys->rect_display);
61     SetRectEmpty(&sys->rect_parent);
62     sys->is_first_display = true;
63     sys->is_on_top = false;
64
65     var_Create(vd, "video-deco", VLC_VAR_BOOL | VLC_VAR_DOINHERIT);
66     var_Create(vd, "disable-screensaver", VLC_VAR_BOOL | VLC_VAR_DOINHERIT);
67
68     /* */
69     sys->event = EventThreadCreate(vd);
70     if (!sys->event)
71         return VLC_EGENERIC;
72
73     event_cfg_t cfg;
74     memset(&cfg, 0, sizeof(cfg));
75 #ifdef MODULE_NAME_IS_direct3d
76     cfg.use_desktop = sys->use_desktop;
77 #endif
78 #ifdef MODULE_NAME_IS_directdraw
79     cfg.use_overlay = sys->use_overlay;
80 #endif
81     cfg.win.type   = VOUT_WINDOW_TYPE_HWND;
82     cfg.win.x      = var_InheritInteger(vd, "video-x");
83     cfg.win.y      = var_InheritInteger(vd, "video-y");
84     cfg.win.width  = vd->cfg->display.width;
85     cfg.win.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 void CommonManage(vout_display_t *vd)
122 {
123     vout_display_sys_t *sys = vd->sys;
124
125     /* We used to call the Win32 PeekMessage function here to read the window
126      * messages. But since window can stay blocked into this function for a
127      * long time (for example when you move your window on the screen), I
128      * decided to isolate PeekMessage in another thread. */
129
130     /* If we do not control our window, we check for geometry changes
131      * ourselves because the parent might not send us its events. */
132     if (sys->hparent) {
133         RECT rect_parent;
134         POINT point;
135
136         /* Check if the parent window has resized or moved */
137         GetClientRect(sys->hparent, &rect_parent);
138         point.x = point.y = 0;
139         ClientToScreen(sys->hparent, &point);
140         OffsetRect(&rect_parent, point.x, point.y);
141
142         if (!EqualRect(&rect_parent, &sys->rect_parent)) {
143             sys->rect_parent = rect_parent;
144
145             /* This code deals with both resize and move
146              *
147              * For most drivers(direct3d, gdi, opengl), move is never
148              * an issue. The surface automatically gets moved together
149              * with the associated window (hvideownd)
150              *
151              * For directx, it is still important to call UpdateRects
152              * on a move of the parent window, even if no resize occurred
153              */
154             SetWindowPos(sys->hwnd, 0, 0, 0,
155                          rect_parent.right - rect_parent.left,
156                          rect_parent.bottom - rect_parent.top,
157                          SWP_NOZORDER);
158
159             UpdateRects(vd, NULL, NULL, true);
160         }
161     }
162
163     /* HasMoved means here resize or move */
164     if (EventThreadGetAndResetHasMoved(sys->event))
165         UpdateRects(vd, NULL, NULL, false);
166 }
167
168 /**
169  * It ensures that the video window is shown after the first picture
170  * is displayed.
171  */
172 void CommonDisplay(vout_display_t *vd)
173 {
174     vout_display_sys_t *sys = vd->sys;
175
176     if (!sys->is_first_display)
177         return;
178
179     /* Video window is initially hidden, show it now since we got a
180      * picture to show.
181      */
182     SetWindowPos(sys->hvideownd, 0, 0, 0, 0, 0,
183                  SWP_ASYNCWINDOWPOS|
184                  SWP_FRAMECHANGED|
185                  SWP_SHOWWINDOW|
186                  SWP_NOMOVE|
187                  SWP_NOSIZE|
188                  SWP_NOZORDER);
189     sys->is_first_display = false;
190 }
191
192 /**
193  * It updates a picture data/pitches.
194  */
195 int CommonUpdatePicture(picture_t *picture, picture_t **fallback,
196                         uint8_t *data, unsigned pitch)
197 {
198     if (fallback) {
199         if (*fallback == NULL) {
200             *fallback = picture_NewFromFormat(&picture->format);
201             if (*fallback == NULL)
202                 return VLC_EGENERIC;
203         }
204         for (int n = 0; n < picture->i_planes; n++) {
205             const plane_t *src = &(*fallback)->p[n];
206             plane_t       *dst = &picture->p[n];
207             dst->p_pixels = src->p_pixels;
208             dst->i_pitch  = src->i_pitch;
209             dst->i_lines  = src->i_lines;
210         }
211         return VLC_SUCCESS;
212     }
213     /* fill in buffer info in first plane */
214     picture->p->p_pixels = data;
215     picture->p->i_pitch  = pitch;
216     picture->p->i_lines  = picture->format.i_visible_height;
217
218     /*  Fill chroma planes for planar YUV */
219     if (picture->format.i_chroma == VLC_CODEC_I420 ||
220         picture->format.i_chroma == VLC_CODEC_J420 ||
221         picture->format.i_chroma == VLC_CODEC_YV12) {
222
223         for (int n = 1; n < picture->i_planes; n++) {
224             const plane_t *o = &picture->p[n-1];
225             plane_t *p = &picture->p[n];
226
227             p->p_pixels = o->p_pixels + o->i_lines * o->i_pitch;
228             p->i_pitch  = pitch / 2;
229             p->i_lines  = picture->format.i_visible_height / 2;
230         }
231         /* The dx/d3d buffer is always allocated as YV12 */
232         if (vlc_fourcc_AreUVPlanesSwapped(picture->format.i_chroma, VLC_CODEC_YV12)) {
233             uint8_t *p_tmp = picture->p[1].p_pixels;
234             picture->p[1].p_pixels = picture->p[2].p_pixels;
235             picture->p[2].p_pixels = p_tmp;
236         }
237     }
238     return VLC_SUCCESS;
239 }
240
241 void AlignRect(RECT *r, int align_boundary, int align_size)
242 {
243     if (align_boundary)
244         r->left = (r->left + align_boundary/2) & ~align_boundary;
245     if (align_size)
246         r->right = ((r->right - r->left + align_size/2) & ~align_size) + r->left;
247 }
248
249 /* */
250 static void CommonChangeThumbnailClip(vout_display_t *vd, bool show)
251 {
252     vout_display_sys_t *sys = vd->sys;
253
254     /* Windows 7 taskbar thumbnail code */
255     OSVERSIONINFO winVer;
256     winVer.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
257     if (!GetVersionEx(&winVer) || winVer.dwMajorVersion <= 5)
258         return;
259
260     CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
261
262     void *ptr;
263     if (S_OK == CoCreateInstance(&CLSID_TaskbarList,
264                                  NULL, CLSCTX_INPROC_SERVER,
265                                  &IID_ITaskbarList3,
266                                  &ptr)) {
267         ITaskbarList3 *taskbl = ptr;
268         taskbl->lpVtbl->HrInit(taskbl);
269
270         HWND hroot = GetAncestor(sys->hwnd,GA_ROOT);
271         RECT relative;
272         if (show) {
273             RECT video, parent;
274             GetWindowRect(sys->hvideownd, &video);
275             GetWindowRect(hroot, &parent);
276             relative.left   = video.left   - parent.left - 8;
277             relative.top    = video.top    - parent.top - 10;
278
279             relative.right  = video.right  - video.left + relative.left;
280             relative.bottom = video.bottom - video.top  + relative.top - 25;
281         }
282         if (S_OK != taskbl->lpVtbl->SetThumbnailClip(taskbl, hroot,
283                                                  show ? &relative : NULL))
284             msg_Err(vd, "SetThumbNailClip failed");
285
286         taskbl->lpVtbl->Release(taskbl);
287     }
288     CoUninitialize();
289 }
290
291 /*****************************************************************************
292  * UpdateRects: update clipping rectangles
293  *****************************************************************************
294  * This function is called when the window position or size are changed, and
295  * its job is to update the source and destination RECTs used to display the
296  * picture.
297  *****************************************************************************/
298 void UpdateRects(vout_display_t *vd,
299                   const vout_display_cfg_t *cfg,
300                   const video_format_t *source,
301                   bool is_forced)
302 {
303     vout_display_sys_t *sys = vd->sys;
304 #define rect_src sys->rect_src
305 #define rect_src_clipped sys->rect_src_clipped
306 #define rect_dest sys->rect_dest
307 #define rect_dest_clipped sys->rect_dest_clipped
308
309     RECT  rect;
310     POINT point;
311
312     /* */
313     if (!cfg)
314         cfg = vd->cfg;
315     if (!source)
316         source = &vd->source;
317
318     /* Retrieve the window size */
319     GetClientRect(sys->hwnd, &rect);
320
321     /* Retrieve the window position */
322     point.x = point.y = 0;
323     ClientToScreen(sys->hwnd, &point);
324
325     /* If nothing changed, we can return */
326     bool has_moved;
327     bool is_resized;
328     EventThreadUpdateWindowPosition(sys->event, &has_moved, &is_resized,
329                                     point.x, point.y,
330                                     rect.right, rect.bottom);
331     if (is_resized)
332         vout_display_SendEventDisplaySize(vd, rect.right, rect.bottom, cfg->is_fullscreen);
333     if (!is_forced && !has_moved && !is_resized )
334         return;
335
336     /* Update the window position and size */
337     vout_display_cfg_t place_cfg = *cfg;
338     place_cfg.display.width  = rect.right;
339     place_cfg.display.height = rect.bottom;
340
341     vout_display_place_t place;
342     vout_display_PlacePicture(&place, source, &place_cfg, false);
343
344     EventThreadUpdateSourceAndPlace(sys->event, source, &place);
345
346     if (sys->hvideownd)
347         SetWindowPos(sys->hvideownd, 0,
348                      place.x, place.y, place.width, place.height,
349                      SWP_NOCOPYBITS|SWP_NOZORDER|SWP_ASYNCWINDOWPOS);
350
351     /* Destination image position and dimensions */
352 #if defined(MODULE_NAME_IS_direct3d) || defined(MODULE_NAME_IS_direct2d)
353     rect_dest.left   = 0;
354     rect_dest.right  = place.width;
355     rect_dest.top    = 0;
356     rect_dest.bottom = place.height;
357 #else
358     rect_dest.left = point.x + place.x;
359     rect_dest.right = rect_dest.left + place.width;
360     rect_dest.top = point.y + place.y;
361     rect_dest.bottom = rect_dest.top + place.height;
362
363 #ifdef MODULE_NAME_IS_directdraw
364     /* Apply overlay hardware constraints */
365     if (sys->use_overlay)
366         AlignRect(&rect_dest, sys->i_align_dest_boundary, sys->i_align_dest_size);
367 #endif
368
369 #endif
370
371 #if defined(MODULE_NAME_IS_directdraw)
372     /* UpdateOverlay directdraw function doesn't automatically clip to the
373      * display size so we need to do it otherwise it will fail */
374
375     /* Clip the destination window */
376     if (!IntersectRect(&rect_dest_clipped, &rect_dest,
377                        &sys->rect_display)) {
378         SetRectEmpty(&rect_src_clipped);
379         goto exit;
380     }
381
382 #ifndef NDEBUG
383     msg_Dbg(vd, "DirectXUpdateRects image_dst_clipped coords:"
384                 " %li,%li,%li,%li",
385                 rect_dest_clipped.left, rect_dest_clipped.top,
386                 rect_dest_clipped.right, rect_dest_clipped.bottom);
387 #endif
388
389 #else
390
391     /* AFAIK, there are no clipping constraints in Direct3D, OpenGL and GDI */
392     rect_dest_clipped = rect_dest;
393
394 #endif
395
396     /* the 2 following lines are to fix a bug when clicking on the desktop */
397     if ((rect_dest_clipped.right - rect_dest_clipped.left) == 0 ||
398         (rect_dest_clipped.bottom - rect_dest_clipped.top) == 0) {
399         SetRectEmpty(&rect_src_clipped);
400         goto exit;
401     }
402
403     /* src image dimensions */
404     rect_src.left   = 0;
405     rect_src.top    = 0;
406     rect_src.right  = source->i_visible_width;
407     rect_src.bottom = source->i_visible_height;
408
409     /* Clip the source image */
410     rect_src_clipped.left = source->i_x_offset +
411       (rect_dest_clipped.left - rect_dest.left) *
412       source->i_visible_width / (rect_dest.right - rect_dest.left);
413     rect_src_clipped.right = source->i_x_offset +
414       source->i_visible_width -
415       (rect_dest.right - rect_dest_clipped.right) *
416       source->i_visible_width / (rect_dest.right - rect_dest.left);
417     rect_src_clipped.top = source->i_y_offset +
418       (rect_dest_clipped.top - rect_dest.top) *
419       source->i_visible_height / (rect_dest.bottom - rect_dest.top);
420     rect_src_clipped.bottom = source->i_y_offset +
421       source->i_visible_height -
422       (rect_dest.bottom - rect_dest_clipped.bottom) *
423       source->i_visible_height / (rect_dest.bottom - rect_dest.top);
424
425 #ifdef MODULE_NAME_IS_directdraw
426     /* Apply overlay hardware constraints */
427     if (sys->use_overlay)
428         AlignRect(&rect_src_clipped, sys->i_align_src_boundary, sys->i_align_src_size);
429 #elif defined(MODULE_NAME_IS_direct3d) || defined(MODULE_NAME_IS_direct2d)
430     /* Needed at least with YUV content */
431     rect_src_clipped.left &= ~1;
432     rect_src_clipped.right &= ~1;
433     rect_src_clipped.top &= ~1;
434     rect_src_clipped.bottom &= ~1;
435 #endif
436
437 #ifndef NDEBUG
438     msg_Dbg(vd, "DirectXUpdateRects image_src_clipped"
439                 " coords: %li,%li,%li,%li",
440                 rect_src_clipped.left, rect_src_clipped.top,
441                 rect_src_clipped.right, rect_src_clipped.bottom);
442 #endif
443
444 #ifdef MODULE_NAME_IS_directdraw
445     /* The destination coordinates need to be relative to the current
446      * directdraw primary surface (display) */
447     rect_dest_clipped.left -= sys->rect_display.left;
448     rect_dest_clipped.right -= sys->rect_display.left;
449     rect_dest_clipped.top -= sys->rect_display.top;
450     rect_dest_clipped.bottom -= sys->rect_display.top;
451 #endif
452
453     CommonChangeThumbnailClip(vd, true);
454
455 exit:
456     /* Signal the change in size/position */
457     sys->changes |= DX_POSITION_CHANGE;
458
459 #undef rect_src
460 #undef rect_src_clipped
461 #undef rect_dest
462 #undef rect_dest_clipped
463 }
464
465 static int CommonControlSetFullscreen(vout_display_t *vd, bool is_fullscreen)
466 {
467     vout_display_sys_t *sys = vd->sys;
468
469 #ifdef MODULE_NAME_IS_direct3d
470     if (sys->use_desktop && is_fullscreen)
471         return VLC_EGENERIC;
472 #endif
473
474     /* */
475     if (sys->parent_window)
476         return vout_window_SetFullScreen(sys->parent_window, is_fullscreen);
477
478     /* */
479     HWND hwnd = sys->hparent && sys->hfswnd ? sys->hfswnd : sys->hwnd;
480
481     /* Save the current windows placement/placement to restore
482        when fullscreen is over */
483     WINDOWPLACEMENT window_placement;
484     window_placement.length = sizeof(WINDOWPLACEMENT);
485     GetWindowPlacement(hwnd, &window_placement);
486
487     if (is_fullscreen) {
488         msg_Dbg(vd, "entering fullscreen mode");
489
490         /* Change window style, no borders and no title bar */
491         SetWindowLong(hwnd, GWL_STYLE, WS_CLIPCHILDREN | WS_VISIBLE);
492
493         if (sys->hparent) {
494             /* Retrieve current window position so fullscreen will happen
495             *on the right screen */
496             HMONITOR hmon = MonitorFromWindow(sys->hparent,
497                                               MONITOR_DEFAULTTONEAREST);
498             MONITORINFO mi;
499             mi.cbSize = sizeof(MONITORINFO);
500             if (GetMonitorInfo(hmon, &mi))
501                 SetWindowPos(hwnd, 0,
502                              mi.rcMonitor.left,
503                              mi.rcMonitor.top,
504                              mi.rcMonitor.right - mi.rcMonitor.left,
505                              mi.rcMonitor.bottom - mi.rcMonitor.top,
506                              SWP_NOZORDER|SWP_FRAMECHANGED);
507         } else {
508             /* Maximize non embedded window */
509             ShowWindow(hwnd, SW_SHOWMAXIMIZED);
510         }
511
512         if (sys->hparent) {
513             /* Hide the previous window */
514             RECT rect;
515             GetClientRect(hwnd, &rect);
516             SetParent(sys->hwnd, hwnd);
517             SetWindowPos(sys->hwnd, 0, 0, 0,
518                          rect.right, rect.bottom,
519                          SWP_NOZORDER|SWP_FRAMECHANGED);
520
521             HWND topLevelParent = GetAncestor(sys->hparent, GA_ROOT);
522             ShowWindow(topLevelParent, SW_HIDE);
523         }
524         SetForegroundWindow(hwnd);
525     } else {
526         msg_Dbg(vd, "leaving fullscreen mode");
527
528         /* Change window style, no borders and no title bar */
529         SetWindowLong(hwnd, GWL_STYLE, EventThreadGetWindowStyle(sys->event));
530
531         if (sys->hparent) {
532             RECT rect;
533             GetClientRect(sys->hparent, &rect);
534             SetParent(sys->hwnd, sys->hparent);
535             SetWindowPos(sys->hwnd, 0, 0, 0,
536                          rect.right, rect.bottom,
537                          SWP_NOZORDER|SWP_FRAMECHANGED);
538
539             HWND topLevelParent = GetAncestor(sys->hparent, GA_ROOT);
540             ShowWindow(topLevelParent, SW_SHOW);
541             SetForegroundWindow(sys->hparent);
542             ShowWindow(hwnd, SW_HIDE);
543         } else {
544             /* return to normal window for non embedded vout */
545             SetWindowPlacement(hwnd, &window_placement);
546             ShowWindow(hwnd, SW_SHOWNORMAL);
547         }
548     }
549     return VLC_SUCCESS;
550 }
551
552 int CommonControl(vout_display_t *vd, int query, va_list args)
553 {
554     vout_display_sys_t *sys = vd->sys;
555
556     switch (query) {
557     case VOUT_DISPLAY_CHANGE_DISPLAY_SIZE:   /* const vout_display_cfg_t *p_cfg, int is_forced */
558     case VOUT_DISPLAY_CHANGE_DISPLAY_FILLED: /* const vout_display_cfg_t *p_cfg */
559     case VOUT_DISPLAY_CHANGE_ZOOM:           /* const vout_display_cfg_t *p_cfg */
560     case VOUT_DISPLAY_CHANGE_SOURCE_ASPECT:  /* const video_format_t *p_source */
561     case VOUT_DISPLAY_CHANGE_SOURCE_CROP: {  /* const video_format_t *p_source */
562         const vout_display_cfg_t *cfg;
563         const video_format_t *source;
564         bool  is_forced = true;
565         if (query == VOUT_DISPLAY_CHANGE_SOURCE_CROP ||
566             query == VOUT_DISPLAY_CHANGE_SOURCE_ASPECT) {
567             cfg    = vd->cfg;
568             source = va_arg(args, const video_format_t *);
569         } else {
570             cfg    = va_arg(args, const vout_display_cfg_t *);
571             source = &vd->source;
572             if (query == VOUT_DISPLAY_CHANGE_DISPLAY_SIZE)
573                 is_forced = va_arg(args, int);
574         }
575         if (query == VOUT_DISPLAY_CHANGE_DISPLAY_SIZE && is_forced) {
576             /* Update dimensions */
577             if (sys->parent_window) {
578                 vout_window_SetSize(sys->parent_window, cfg->display.width, cfg->display.height);
579             } else {
580                 RECT rect_window;
581                 rect_window.top    = 0;
582                 rect_window.left   = 0;
583                 rect_window.right  = cfg->display.width;
584                 rect_window.bottom = cfg->display.height;
585                 AdjustWindowRect(&rect_window, EventThreadGetWindowStyle(sys->event), 0);
586
587                 SetWindowPos(sys->hwnd, 0, 0, 0,
588                              rect_window.right - rect_window.left,
589                              rect_window.bottom - rect_window.top, SWP_NOMOVE);
590             }
591             return VLC_EGENERIC;
592         }
593         UpdateRects(vd, cfg, source, is_forced);
594         return VLC_SUCCESS;
595     }
596     case VOUT_DISPLAY_CHANGE_WINDOW_STATE: {       /* unsigned state */
597         const unsigned state = va_arg(args, unsigned);
598         const bool is_on_top = (state & VOUT_WINDOW_STATE_ABOVE) != 0;
599 #ifdef MODULE_NAME_IS_direct3d
600         if (sys->use_desktop && is_on_top)
601             return VLC_EGENERIC;
602 #endif
603         if (sys->parent_window) {
604             if (vout_window_SetState(sys->parent_window, state))
605                 return VLC_EGENERIC;
606         } else {
607             HMENU hMenu = GetSystemMenu(sys->hwnd, FALSE);
608
609             if (is_on_top && !(GetWindowLong(sys->hwnd, GWL_EXSTYLE) & WS_EX_TOPMOST)) {
610                 CheckMenuItem(hMenu, IDM_TOGGLE_ON_TOP, MF_BYCOMMAND | MFS_CHECKED);
611                 SetWindowPos(sys->hwnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE);
612             } else if (!is_on_top && (GetWindowLong(sys->hwnd, GWL_EXSTYLE) & WS_EX_TOPMOST)) {
613                 CheckMenuItem(hMenu, IDM_TOGGLE_ON_TOP, MF_BYCOMMAND | MFS_UNCHECKED);
614                 SetWindowPos(sys->hwnd, HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOSIZE|SWP_NOMOVE);
615             }
616         }
617         sys->is_on_top = is_on_top;
618         return VLC_SUCCESS;
619     }
620     case VOUT_DISPLAY_CHANGE_FULLSCREEN: {   /* const vout_display_cfg_t *p_cfg */
621         const vout_display_cfg_t *cfg = va_arg(args, const vout_display_cfg_t *);
622         if (CommonControlSetFullscreen(vd, cfg->is_fullscreen))
623             return VLC_EGENERIC;
624         UpdateRects(vd, NULL, NULL, false);
625         return VLC_SUCCESS;
626     }
627
628     case VOUT_DISPLAY_HIDE_MOUSE:
629         EventThreadMouseHide(sys->event);
630         return VLC_SUCCESS;
631     case VOUT_DISPLAY_RESET_PICTURES:
632         assert(0);
633     default:
634         return VLC_EGENERIC;
635     }
636 }
637
638 static void DisableScreensaver(vout_display_t *vd)
639 {
640     vout_display_sys_t *sys = vd->sys;
641
642     /* disable screensaver by temporarily changing system settings */
643     sys->i_spi_screensaveactive = 0;
644     if (var_GetBool(vd, "disable-screensaver")) {
645         msg_Dbg(vd, "disabling screen saver");
646         SystemParametersInfo(SPI_GETSCREENSAVEACTIVE, 0,
647                              &sys->i_spi_screensaveactive, 0);
648
649         if (LOWORD(GetVersion()) == 0x0005) {
650             /* If this is NT 5.0 (i.e., Win2K), we need to hack around
651              * KB318781 (see http://support.microsoft.com/kb/318781) */
652
653             HKEY hKeyCP = NULL;
654
655             if (ERROR_SUCCESS == RegOpenKeyEx(HKEY_CURRENT_USER,
656                                               TEXT("Control Panel\\Desktop"),
657                                               0, KEY_QUERY_VALUE, &hKeyCP) &&
658                 ERROR_SUCCESS != RegQueryValueEx(hKeyCP, TEXT("SCRNSAVE.EXE"),
659                                                  NULL, NULL, NULL, NULL)) {
660                 sys->i_spi_screensaveactive = FALSE;
661             }
662         }
663
664         if (FALSE != sys->i_spi_screensaveactive) {
665             SystemParametersInfo(SPI_SETSCREENSAVEACTIVE, 0, NULL, 0);
666         }
667     }
668 }
669
670 static void RestoreScreensaver(vout_display_t *vd)
671 {
672     vout_display_sys_t *sys = vd->sys;
673
674     /* restore screensaver system settings */
675     if (0 != sys->i_spi_screensaveactive) {
676         SystemParametersInfo(SPI_SETSCREENSAVEACTIVE,
677                              sys->i_spi_screensaveactive, NULL, 0);
678     }
679 }