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