]> git.sesse.net Git - vlc/blob - modules/video_output/msw/common.c
Factorized Direct3DLockSurface/DirectXLock.
[vlc] / modules / video_output / msw / common.c
1 /*****************************************************************************
2  * common.c:
3  *****************************************************************************
4  * Copyright (C) 2001-2009 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Gildas Bazin <gbazin@videolan.org>
8  *
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.
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 General Public License for more details.
18  *
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  *****************************************************************************/
23
24
25 /*****************************************************************************
26  * Preamble: This file contains the functions related to the creation of
27  *             a window and the handling of its messages (events).
28  *****************************************************************************/
29 #ifdef HAVE_CONFIG_H
30 # include "config.h"
31 #endif
32 #include <assert.h>
33
34 #include <vlc_common.h>
35 #include <vlc_vout_display.h>
36 #include <vlc_vout_window.h>
37
38 #include <windows.h>
39 #include <windowsx.h>
40 #include <shellapi.h>
41
42 #ifdef MODULE_NAME_IS_directx
43 #include <ddraw.h>
44 #endif
45 #ifdef MODULE_NAME_IS_direct3d
46 #include <d3d9.h>
47 #endif
48 #ifdef MODULE_NAME_IS_glwin32
49 #include "../opengl.h"
50 #endif
51
52 #include "common.h"
53
54 #ifndef UNDER_CE
55 #include <vlc_windows_interfaces.h>
56 #endif
57
58 #ifdef UNDER_CE
59 #include <aygshell.h>
60     //WINSHELLAPI BOOL WINAPI SHFullScreen(HWND hwndRequester, DWORD dwState);
61 #endif
62
63 static void CommonChangeThumbnailClip(vout_display_t *, bool show);
64 static int CommonControlSetFullscreen(vout_display_t *, bool is_fullscreen);
65
66 static void DisableScreensaver(vout_display_t *);
67 static void RestoreScreensaver(vout_display_t *);
68
69 /* */
70 int CommonInit(vout_display_t *vd)
71 {
72     vout_display_sys_t *sys = vd->sys;
73
74     sys->hwnd      = NULL;
75     sys->hvideownd = NULL;
76     sys->hparent   = NULL;
77     sys->hfswnd    = NULL;
78     sys->changes   = 0;
79     SetRectEmpty(&sys->rect_display);
80     SetRectEmpty(&sys->rect_parent);
81     sys->is_first_display = true;
82     sys->is_on_top = false;
83
84     var_Create(vd, "video-title", VLC_VAR_STRING | VLC_VAR_DOINHERIT);
85     var_Create(vd, "video-deco", VLC_VAR_BOOL | VLC_VAR_DOINHERIT);
86
87     /* */
88     sys->event = EventThreadCreate(vd);
89     if (!sys->event)
90         return VLC_EGENERIC;
91
92     event_cfg_t cfg;
93     memset(&cfg, 0, sizeof(cfg));
94 #ifdef MODULE_NAME_IS_direct3d
95     cfg.use_desktop = sys->use_desktop;
96 #endif
97 #ifdef MODULE_NAME_IS_directx
98     cfg.use_overlay = sys->use_overlay;
99 #endif
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;
105
106     event_hwnd_t hwnd;
107     if (EventThreadStart(sys->event, &hwnd, &cfg))
108         return VLC_EGENERIC;
109
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;
115
116     if (vd->cfg->is_fullscreen) {
117         if (CommonControlSetFullscreen(vd, true))
118             vout_display_SendEventFullscreen(vd, false);
119     }
120
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);
125 #endif
126
127     return VLC_SUCCESS;
128 }
129
130 /* */
131 void CommonClean(vout_display_t *vd)
132 {
133     vout_display_sys_t *sys = vd->sys;
134
135     if (sys->event) {
136         CommonChangeThumbnailClip(vd, false);
137         EventThreadStop(sys->event);
138         EventThreadDestroy(sys->event);
139     }
140
141 #if !defined(UNDER_CE) && !defined(MODULE_NAME_IS_glwin32)
142     RestoreScreensaver(vd);
143 #endif
144 }
145
146 void CommonManage(vout_display_t *vd)
147 {
148     vout_display_sys_t *sys = vd->sys;
149
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. */
154
155     /* If we do not control our window, we check for geometry changes
156      * ourselves because the parent might not send us its events. */
157     if (sys->hparent) {
158         RECT rect_parent;
159         POINT point;
160
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);
166
167         if (!EqualRect(&rect_parent, &sys->rect_parent)) {
168             sys->rect_parent = rect_parent;
169
170             /* This code deals with both resize and move
171              *
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)
175              *
176              * For directx, it is still important to call UpdateRects
177              * on a move of the parent window, even if no resize occured
178              */
179             SetWindowPos(sys->hwnd, 0, 0, 0,
180                          rect_parent.right - rect_parent.left,
181                          rect_parent.bottom - rect_parent.top,
182                          SWP_NOZORDER);
183
184             UpdateRects(vd, NULL, NULL, true);
185         }
186     }
187
188     /* HasMoved means here resize or move */
189     if (EventThreadGetAndResetHasMoved(sys->event))
190         UpdateRects(vd, NULL, NULL, false);
191 }
192
193 /**
194  * It ensures that the video window is shown after the first picture
195  * is displayed.
196  */
197 void CommonDisplay(vout_display_t *vd)
198 {
199     vout_display_sys_t *sys = vd->sys;
200
201     if (!sys->is_first_display)
202         return;
203
204     /* Video window is initially hidden, show it now since we got a
205      * picture to show.
206      */
207     SetWindowPos(sys->hvideownd, 0, 0, 0, 0, 0,
208                  SWP_ASYNCWINDOWPOS|
209                  SWP_FRAMECHANGED|
210                  SWP_SHOWWINDOW|
211                  SWP_NOMOVE|
212                  SWP_NOSIZE|
213                  SWP_NOZORDER);
214     sys->is_first_display = false;
215 }
216
217 /**
218  * It updates a picture data/pitches.
219  */
220 void CommonUpdatePicture(picture_t *picture,
221                          uint8_t *data, unsigned pitch)
222 {
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;
227
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) {
232
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];
236
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;
240         }
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;
246         }
247     }
248 }
249
250 void AlignRect(RECT *r, int align_boundary, int align_size)
251 {
252     if (align_boundary)
253         r->left = (r->left + align_boundary/2) & ~align_boundary;
254     if (align_size)
255         r->right = ((r->right - r->left + align_size/2) & ~align_size) + r->left;
256 }
257
258 /* */
259 static void CommonChangeThumbnailClip(vout_display_t *vd, bool show)
260 {
261 #ifndef UNDER_CE
262     vout_display_sys_t *sys = vd->sys;
263
264     /* Windows 7 taskbar thumbnail code */
265     OSVERSIONINFO winVer;
266     winVer.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
267     if (!GetVersionEx(&winVer) || winVer.dwMajorVersion <= 5)
268         return;
269
270     CoInitialize(0);
271
272     LPTASKBARLIST3 taskbl;
273     if (S_OK == CoCreateInstance(&clsid_ITaskbarList,
274                                  NULL, CLSCTX_INPROC_SERVER,
275                                  &IID_ITaskbarList3,
276                                  &taskbl)) {
277         taskbl->vt->HrInit(taskbl);
278
279         HWND hroot = GetAncestor(sys->hwnd,GA_ROOT);
280         RECT relative;
281         if (show) {
282             RECT video, parent;
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;
287
288             relative.right  = video.right  - video.left + relative.left;
289             relative.bottom = video.bottom - video.top  + relative.top - 25;
290         }
291         if (S_OK != taskbl->vt->SetThumbnailClip(taskbl, hroot,
292                                                  show ? &relative : NULL))
293             msg_Err(vd, "SetThumbNailClip failed");
294
295         taskbl->vt->Release(taskbl);
296     }
297     CoUninitialize();
298 #endif
299 }
300
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
306  * picture.
307  *****************************************************************************/
308 void UpdateRects(vout_display_t *vd,
309                   const vout_display_cfg_t *cfg,
310                   const video_format_t *source,
311                   bool is_forced)
312 {
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
318
319     RECT  rect;
320     POINT point;
321
322     /* */
323     if (!cfg)
324         cfg = vd->cfg;
325     if (!source)
326         source = &vd->source;
327
328     /* Retrieve the window size */
329     GetClientRect(sys->hwnd, &rect);
330
331     /* Retrieve the window position */
332     point.x = point.y = 0;
333     ClientToScreen(sys->hwnd, &point);
334
335     /* If nothing changed, we can return */
336     bool has_moved;
337     bool is_resized;
338     EventThreadUpdateWindowPosition(sys->event, &has_moved, &is_resized,
339                                     point.x, point.y,
340                                     rect.right, rect.bottom);
341     if (is_resized)
342         vout_display_SendEventDisplaySize(vd, rect.right, rect.bottom, cfg->is_fullscreen);
343     if (!is_forced && !has_moved && !is_resized )
344         return;
345
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;
350
351     vout_display_place_t place;
352     vout_display_PlacePicture(&place, source, &place_cfg, false);
353
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);
358 #endif
359
360     if (sys->hvideownd)
361         SetWindowPos(sys->hvideownd, 0,
362                      place.x, place.y, place.width, place.height,
363                      SWP_NOCOPYBITS|SWP_NOZORDER|SWP_ASYNCWINDOWPOS);
364
365     /* Destination image position and dimensions */
366 #if defined(MODULE_NAME_IS_direct3d)
367     rect_dest.left   = 0;
368     rect_dest.right  = place.width;
369     rect_dest.top    = 0;
370     rect_dest.bottom = place.height;
371 #else
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;
376
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);
381 #endif
382
383 #endif
384
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 */
389
390     /* Clip the destination window */
391     if (!IntersectRect(&rect_dest_clipped, &rect_dest,
392                        &sys->rect_display)) {
393         SetRectEmpty(&rect_src_clipped);
394         goto exit;
395     }
396
397 #ifndef NDEBUG
398     msg_Dbg(vd, "DirectXUpdateRects image_dst_clipped coords:"
399                 " %li,%li,%li,%li",
400                 rect_dest_clipped.left, rect_dest_clipped.top,
401                 rect_dest_clipped.right, rect_dest_clipped.bottom);
402 #endif
403
404 #else
405
406     /* AFAIK, there are no clipping constraints in Direct3D, OpenGL and GDI */
407     rect_dest_clipped = rect_dest;
408
409 #endif
410
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);
415         goto exit;
416     }
417
418     /* src image dimensions */
419     rect_src.left   = 0;
420     rect_src.top    = 0;
421     rect_src.right  = source->i_width;
422     rect_src.bottom = source->i_height;
423
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);
439
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;
450 #endif
451
452 #ifndef NDEBUG
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);
457 #endif
458
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;
466 #endif
467
468     CommonChangeThumbnailClip(vd, true);
469
470 exit:
471     /* Signal the change in size/position */
472     sys->changes |= DX_POSITION_CHANGE;
473
474 #undef rect_src
475 #undef rect_src_clipped
476 #undef rect_dest
477 #undef rect_dest_clipped
478 }
479
480 static int CommonControlSetFullscreen(vout_display_t *vd, bool is_fullscreen)
481 {
482     vout_display_sys_t *sys = vd->sys;
483
484 #ifdef MODULE_NAME_IS_direct3d
485     if (sys->use_desktop && is_fullscreen)
486         return VLC_EGENERIC;
487 #endif
488
489     /* */
490     if (sys->parent_window)
491         return vout_window_SetFullScreen(sys->parent_window, is_fullscreen);
492
493     /* */
494     HWND hwnd = sys->hparent && sys->hfswnd ? sys->hfswnd : sys->hwnd;
495
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);
501
502     if (is_fullscreen) {
503         msg_Dbg(vd, "entering fullscreen mode");
504
505         /* Change window style, no borders and no title bar */
506         SetWindowLong(hwnd, GWL_STYLE, WS_CLIPCHILDREN | WS_VISIBLE);
507
508         if (sys->hparent) {
509 #ifdef UNDER_CE
510             POINT point = {0,0};
511             RECT rect;
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);
517 #else
518             /* Retrieve current window position so fullscreen will happen
519             *on the right screen */
520             HMONITOR hmon = MonitorFromWindow(sys->hparent,
521                                               MONITOR_DEFAULTTONEAREST);
522             MONITORINFO mi;
523             mi.cbSize = sizeof(MONITORINFO);
524             if (GetMonitorInfo(hmon, &mi))
525                 SetWindowPos(hwnd, 0,
526                              mi.rcMonitor.left,
527                              mi.rcMonitor.top,
528                              mi.rcMonitor.right - mi.rcMonitor.left,
529                              mi.rcMonitor.bottom - mi.rcMonitor.top,
530                              SWP_NOZORDER|SWP_FRAMECHANGED);
531 #endif
532         } else {
533             /* Maximize non embedded window */
534             ShowWindow(hwnd, SW_SHOWMAXIMIZED);
535         }
536
537         if (sys->hparent) {
538             /* Hide the previous window */
539             RECT rect;
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);
545
546 #ifdef UNDER_CE
547             HWND topLevelParent = GetParent(sys->hparent);
548 #else
549             HWND topLevelParent = GetAncestor(sys->hparent, GA_ROOT);
550 #endif
551             ShowWindow(topLevelParent, SW_HIDE);
552         }
553         SetForegroundWindow(hwnd);
554     } else {
555         msg_Dbg(vd, "leaving fullscreen mode");
556
557         /* Change window style, no borders and no title bar */
558         SetWindowLong(hwnd, GWL_STYLE, EventThreadGetWindowStyle(sys->event));
559
560         if (sys->hparent) {
561             RECT rect;
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);
567
568 #ifdef UNDER_CE
569             HWND topLevelParent = GetParent(sys->hparent);
570 #else
571             HWND topLevelParent = GetAncestor(sys->hparent, GA_ROOT);
572 #endif
573             ShowWindow(topLevelParent, SW_SHOW);
574             SetForegroundWindow(sys->hparent);
575             ShowWindow(hwnd, SW_HIDE);
576         } else {
577             /* return to normal window for non embedded vout */
578             SetWindowPlacement(hwnd, &window_placement);
579             ShowWindow(hwnd, SW_SHOWNORMAL);
580         }
581     }
582     return VLC_SUCCESS;
583 }
584
585 int CommonControl(vout_display_t *vd, int query, va_list args)
586 {
587     vout_display_sys_t *sys = vd->sys;
588
589     switch (query) {
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) {
600             cfg    = vd->cfg;
601             source = va_arg(args, const video_format_t *);
602         } else {
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);
607         }
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);
612             } else {
613                 RECT rect_window;
614                 rect_window.top    = 0;
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);
619
620                 SetWindowPos(sys->hwnd, 0, 0, 0,
621                              rect_window.right - rect_window.left,
622                              rect_window.bottom - rect_window.top, SWP_NOMOVE);
623             }
624         }
625         UpdateRects(vd, cfg, source, is_forced);
626         return VLC_SUCCESS;
627     }
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)
633             return VLC_EGENERIC;
634 #endif
635         if (sys->parent_window) {
636             if (vout_window_SetState(sys->parent_window, state))
637                 return VLC_EGENERIC;
638         } else {
639             HMENU hMenu = GetSystemMenu(sys->hwnd, FALSE);
640
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);
647             }
648         }
649         sys->is_on_top = is_on_top;
650         return VLC_SUCCESS;
651     }
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))
655             return VLC_EGENERIC;
656         UpdateRects(vd, NULL, NULL, false);
657         return VLC_SUCCESS;
658     }
659
660     case VOUT_DISPLAY_HIDE_MOUSE:
661         EventThreadMouseHide(sys->event);
662         return VLC_SUCCESS;
663     case VOUT_DISPLAY_RESET_PICTURES:
664         assert(0);
665     default:
666         return VLC_EGENERIC;
667     }
668 }
669
670 #ifndef UNDER_CE
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_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);
685         }
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);
690         }
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);
695         }
696     }
697 }
698
699 static void RestoreScreensaver(vout_display_t *vd)
700 {
701     vout_display_sys_t *sys = vd->sys;
702
703     /* restore screensaver system settings */
704     if (0 != sys->i_spi_lowpowertimeout) {
705         SystemParametersInfo(SPI_SETLOWPOWERTIMEOUT,
706                              sys->i_spi_lowpowertimeout, NULL, 0);
707     }
708     if (0 != sys->i_spi_powerofftimeout) {
709         SystemParametersInfo(SPI_SETPOWEROFFTIMEOUT,
710                              sys->i_spi_powerofftimeout, NULL, 0);
711     }
712     if (0 != sys->i_spi_screensavetimeout) {
713         SystemParametersInfo(SPI_SETSCREENSAVETIMEOUT,
714                              sys->i_spi_screensavetimeout, NULL, 0);
715     }
716 }
717 #endif
718