]> git.sesse.net Git - vlc/blob - modules/video_output/msw/common.c
Used a CPU memory fallback when Direct3DLockSurface/DirectXLock fails.
[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 int CommonUpdatePicture(picture_t *picture, picture_t **fallback,
221                         uint8_t *data, unsigned pitch)
222 {
223     if (fallback) {
224         if (*fallback == NULL) {
225             *fallback = picture_NewFromFormat(&picture->format);
226             if (*fallback == NULL)
227                 return VLC_EGENERIC;
228         }
229         for (int n = 0; n < picture->i_planes; n++) {
230             const plane_t *src = &(*fallback)->p[n];
231             plane_t       *dst = &picture->p[n];
232             dst->p_pixels = src->p_pixels;
233             dst->i_pitch  = src->i_pitch;
234             dst->i_lines  = src->i_lines;
235         }
236         return VLC_SUCCESS;
237     }
238     /* fill in buffer info in first plane */
239     picture->p->p_pixels = data;
240     picture->p->i_pitch  = pitch;
241     picture->p->i_lines  = picture->format.i_height;
242
243     /*  Fill chroma planes for planar YUV */
244     if (picture->format.i_chroma == VLC_CODEC_I420 ||
245         picture->format.i_chroma == VLC_CODEC_J420 ||
246         picture->format.i_chroma == VLC_CODEC_YV12) {
247
248         for (int n = 1; n < picture->i_planes; n++) {
249             const plane_t *o = &picture->p[n-1];
250             plane_t *p = &picture->p[n];
251
252             p->p_pixels = o->p_pixels + o->i_lines * o->i_pitch;
253             p->i_pitch  = pitch / 2;
254             p->i_lines  = picture->format.i_height / 2;
255         }
256         /* The dx/d3d buffer is always allocated as YV12 */
257         if (vlc_fourcc_AreUVPlanesSwapped(picture->format.i_chroma, VLC_CODEC_YV12)) {
258             uint8_t *p_tmp = picture->p[1].p_pixels;
259             picture->p[1].p_pixels = picture->p[2].p_pixels;
260             picture->p[2].p_pixels = p_tmp;
261         }
262     }
263     return VLC_SUCCESS;
264 }
265
266 void AlignRect(RECT *r, int align_boundary, int align_size)
267 {
268     if (align_boundary)
269         r->left = (r->left + align_boundary/2) & ~align_boundary;
270     if (align_size)
271         r->right = ((r->right - r->left + align_size/2) & ~align_size) + r->left;
272 }
273
274 /* */
275 static void CommonChangeThumbnailClip(vout_display_t *vd, bool show)
276 {
277 #ifndef UNDER_CE
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     CoInitialize(0);
287
288     LPTASKBARLIST3 taskbl;
289     if (S_OK == CoCreateInstance(&clsid_ITaskbarList,
290                                  NULL, CLSCTX_INPROC_SERVER,
291                                  &IID_ITaskbarList3,
292                                  &taskbl)) {
293         taskbl->vt->HrInit(taskbl);
294
295         HWND hroot = GetAncestor(sys->hwnd,GA_ROOT);
296         RECT relative;
297         if (show) {
298             RECT video, parent;
299             GetWindowRect(sys->hvideownd, &video);
300             GetWindowRect(hroot, &parent);
301             relative.left   = video.left   - parent.left - 8;
302             relative.top    = video.top    - parent.top - 10;
303
304             relative.right  = video.right  - video.left + relative.left;
305             relative.bottom = video.bottom - video.top  + relative.top - 25;
306         }
307         if (S_OK != taskbl->vt->SetThumbnailClip(taskbl, hroot,
308                                                  show ? &relative : NULL))
309             msg_Err(vd, "SetThumbNailClip failed");
310
311         taskbl->vt->Release(taskbl);
312     }
313     CoUninitialize();
314 #endif
315 }
316
317 /*****************************************************************************
318  * UpdateRects: update clipping rectangles
319  *****************************************************************************
320  * This function is called when the window position or size are changed, and
321  * its job is to update the source and destination RECTs used to display the
322  * picture.
323  *****************************************************************************/
324 void UpdateRects(vout_display_t *vd,
325                   const vout_display_cfg_t *cfg,
326                   const video_format_t *source,
327                   bool is_forced)
328 {
329     vout_display_sys_t *sys = vd->sys;
330 #define rect_src sys->rect_src
331 #define rect_src_clipped sys->rect_src_clipped
332 #define rect_dest sys->rect_dest
333 #define rect_dest_clipped sys->rect_dest_clipped
334
335     RECT  rect;
336     POINT point;
337
338     /* */
339     if (!cfg)
340         cfg = vd->cfg;
341     if (!source)
342         source = &vd->source;
343
344     /* Retrieve the window size */
345     GetClientRect(sys->hwnd, &rect);
346
347     /* Retrieve the window position */
348     point.x = point.y = 0;
349     ClientToScreen(sys->hwnd, &point);
350
351     /* If nothing changed, we can return */
352     bool has_moved;
353     bool is_resized;
354     EventThreadUpdateWindowPosition(sys->event, &has_moved, &is_resized,
355                                     point.x, point.y,
356                                     rect.right, rect.bottom);
357     if (is_resized)
358         vout_display_SendEventDisplaySize(vd, rect.right, rect.bottom, cfg->is_fullscreen);
359     if (!is_forced && !has_moved && !is_resized )
360         return;
361
362     /* Update the window position and size */
363     vout_display_cfg_t place_cfg = *cfg;
364     place_cfg.display.width  = rect.right;
365     place_cfg.display.height = rect.bottom;
366
367     vout_display_place_t place;
368     vout_display_PlacePicture(&place, source, &place_cfg, false);
369
370     EventThreadUpdateSourceAndPlace(sys->event, source, &place);
371 #if defined(MODULE_NAME_IS_wingapi)
372     if (place.width != vd->fmt.i_width || place.height != vd->fmt.i_height)
373         vout_display_SendEventPicturesInvalid(vd);
374 #endif
375
376     if (sys->hvideownd)
377         SetWindowPos(sys->hvideownd, 0,
378                      place.x, place.y, place.width, place.height,
379                      SWP_NOCOPYBITS|SWP_NOZORDER|SWP_ASYNCWINDOWPOS);
380
381     /* Destination image position and dimensions */
382 #if defined(MODULE_NAME_IS_direct3d)
383     rect_dest.left   = 0;
384     rect_dest.right  = place.width;
385     rect_dest.top    = 0;
386     rect_dest.bottom = place.height;
387 #else
388     rect_dest.left = point.x + place.x;
389     rect_dest.right = rect_dest.left + place.width;
390     rect_dest.top = point.y + place.y;
391     rect_dest.bottom = rect_dest.top + place.height;
392
393 #ifdef MODULE_NAME_IS_directx
394     /* Apply overlay hardware constraints */
395     if (sys->use_overlay)
396         AlignRect(&rect_dest, sys->i_align_dest_boundary, sys->i_align_dest_size);
397 #endif
398
399 #endif
400
401 #if defined(MODULE_NAME_IS_directx) || defined(MODULE_NAME_IS_direct3d)
402     /* UpdateOverlay directdraw function doesn't automatically clip to the
403      * display size so we need to do it otherwise it will fail
404      * It is also needed for d3d to avoid exceding our surface size */
405
406     /* Clip the destination window */
407     if (!IntersectRect(&rect_dest_clipped, &rect_dest,
408                        &sys->rect_display)) {
409         SetRectEmpty(&rect_src_clipped);
410         goto exit;
411     }
412
413 #ifndef NDEBUG
414     msg_Dbg(vd, "DirectXUpdateRects image_dst_clipped coords:"
415                 " %li,%li,%li,%li",
416                 rect_dest_clipped.left, rect_dest_clipped.top,
417                 rect_dest_clipped.right, rect_dest_clipped.bottom);
418 #endif
419
420 #else
421
422     /* AFAIK, there are no clipping constraints in Direct3D, OpenGL and GDI */
423     rect_dest_clipped = rect_dest;
424
425 #endif
426
427     /* the 2 following lines are to fix a bug when clicking on the desktop */
428     if ((rect_dest_clipped.right - rect_dest_clipped.left) == 0 ||
429         (rect_dest_clipped.bottom - rect_dest_clipped.top) == 0) {
430         SetRectEmpty(&rect_src_clipped);
431         goto exit;
432     }
433
434     /* src image dimensions */
435     rect_src.left   = 0;
436     rect_src.top    = 0;
437     rect_src.right  = source->i_width;
438     rect_src.bottom = source->i_height;
439
440     /* Clip the source image */
441     rect_src_clipped.left = source->i_x_offset +
442       (rect_dest_clipped.left - rect_dest.left) *
443       source->i_visible_width / (rect_dest.right - rect_dest.left);
444     rect_src_clipped.right = source->i_x_offset +
445       source->i_visible_width -
446       (rect_dest.right - rect_dest_clipped.right) *
447       source->i_visible_width / (rect_dest.right - rect_dest.left);
448     rect_src_clipped.top = source->i_y_offset +
449       (rect_dest_clipped.top - rect_dest.top) *
450       source->i_visible_height / (rect_dest.bottom - rect_dest.top);
451     rect_src_clipped.bottom = source->i_y_offset +
452       source->i_visible_height -
453       (rect_dest.bottom - rect_dest_clipped.bottom) *
454       source->i_visible_height / (rect_dest.bottom - rect_dest.top);
455
456 #ifdef MODULE_NAME_IS_directx
457     /* Apply overlay hardware constraints */
458     if (sys->use_overlay)
459         AlignRect(&rect_src_clipped, sys->i_align_src_boundary, sys->i_align_src_size);
460 #elif defined(MODULE_NAME_IS_direct3d)
461     /* Needed at least with YUV content */
462     rect_src_clipped.left &= ~1;
463     rect_src_clipped.right &= ~1;
464     rect_src_clipped.top &= ~1;
465     rect_src_clipped.bottom &= ~1;
466 #endif
467
468 #ifndef NDEBUG
469     msg_Dbg(vd, "DirectXUpdateRects image_src_clipped"
470                 " coords: %li,%li,%li,%li",
471                 rect_src_clipped.left, rect_src_clipped.top,
472                 rect_src_clipped.right, rect_src_clipped.bottom);
473 #endif
474
475 #ifdef MODULE_NAME_IS_directx
476     /* The destination coordinates need to be relative to the current
477      * directdraw primary surface (display) */
478     rect_dest_clipped.left -= sys->rect_display.left;
479     rect_dest_clipped.right -= sys->rect_display.left;
480     rect_dest_clipped.top -= sys->rect_display.top;
481     rect_dest_clipped.bottom -= sys->rect_display.top;
482 #endif
483
484     CommonChangeThumbnailClip(vd, true);
485
486 exit:
487     /* Signal the change in size/position */
488     sys->changes |= DX_POSITION_CHANGE;
489
490 #undef rect_src
491 #undef rect_src_clipped
492 #undef rect_dest
493 #undef rect_dest_clipped
494 }
495
496 static int CommonControlSetFullscreen(vout_display_t *vd, bool is_fullscreen)
497 {
498     vout_display_sys_t *sys = vd->sys;
499
500 #ifdef MODULE_NAME_IS_direct3d
501     if (sys->use_desktop && is_fullscreen)
502         return VLC_EGENERIC;
503 #endif
504
505     /* */
506     if (sys->parent_window)
507         return vout_window_SetFullScreen(sys->parent_window, is_fullscreen);
508
509     /* */
510     HWND hwnd = sys->hparent && sys->hfswnd ? sys->hfswnd : sys->hwnd;
511
512     /* Save the current windows placement/placement to restore
513        when fullscreen is over */
514     WINDOWPLACEMENT window_placement;
515     window_placement.length = sizeof(WINDOWPLACEMENT);
516     GetWindowPlacement(hwnd, &window_placement);
517
518     if (is_fullscreen) {
519         msg_Dbg(vd, "entering fullscreen mode");
520
521         /* Change window style, no borders and no title bar */
522         SetWindowLong(hwnd, GWL_STYLE, WS_CLIPCHILDREN | WS_VISIBLE);
523
524         if (sys->hparent) {
525 #ifdef UNDER_CE
526             POINT point = {0,0};
527             RECT rect;
528             ClientToScreen(sys->hwnd, &point);
529             GetClientRect(sys->hwnd, &rect);
530             SetWindowPos(hwnd, 0, point.x, point.y,
531                          rect.right, rect.bottom,
532                          SWP_NOZORDER|SWP_FRAMECHANGED);
533 #else
534             /* Retrieve current window position so fullscreen will happen
535             *on the right screen */
536             HMONITOR hmon = MonitorFromWindow(sys->hparent,
537                                               MONITOR_DEFAULTTONEAREST);
538             MONITORINFO mi;
539             mi.cbSize = sizeof(MONITORINFO);
540             if (GetMonitorInfo(hmon, &mi))
541                 SetWindowPos(hwnd, 0,
542                              mi.rcMonitor.left,
543                              mi.rcMonitor.top,
544                              mi.rcMonitor.right - mi.rcMonitor.left,
545                              mi.rcMonitor.bottom - mi.rcMonitor.top,
546                              SWP_NOZORDER|SWP_FRAMECHANGED);
547 #endif
548         } else {
549             /* Maximize non embedded window */
550             ShowWindow(hwnd, SW_SHOWMAXIMIZED);
551         }
552
553         if (sys->hparent) {
554             /* Hide the previous window */
555             RECT rect;
556             GetClientRect(hwnd, &rect);
557             SetParent(sys->hwnd, hwnd);
558             SetWindowPos(sys->hwnd, 0, 0, 0,
559                          rect.right, rect.bottom,
560                          SWP_NOZORDER|SWP_FRAMECHANGED);
561
562 #ifdef UNDER_CE
563             HWND topLevelParent = GetParent(sys->hparent);
564 #else
565             HWND topLevelParent = GetAncestor(sys->hparent, GA_ROOT);
566 #endif
567             ShowWindow(topLevelParent, SW_HIDE);
568         }
569         SetForegroundWindow(hwnd);
570     } else {
571         msg_Dbg(vd, "leaving fullscreen mode");
572
573         /* Change window style, no borders and no title bar */
574         SetWindowLong(hwnd, GWL_STYLE, EventThreadGetWindowStyle(sys->event));
575
576         if (sys->hparent) {
577             RECT rect;
578             GetClientRect(sys->hparent, &rect);
579             SetParent(sys->hwnd, sys->hparent);
580             SetWindowPos(sys->hwnd, 0, 0, 0,
581                          rect.right, rect.bottom,
582                          SWP_NOZORDER|SWP_FRAMECHANGED);
583
584 #ifdef UNDER_CE
585             HWND topLevelParent = GetParent(sys->hparent);
586 #else
587             HWND topLevelParent = GetAncestor(sys->hparent, GA_ROOT);
588 #endif
589             ShowWindow(topLevelParent, SW_SHOW);
590             SetForegroundWindow(sys->hparent);
591             ShowWindow(hwnd, SW_HIDE);
592         } else {
593             /* return to normal window for non embedded vout */
594             SetWindowPlacement(hwnd, &window_placement);
595             ShowWindow(hwnd, SW_SHOWNORMAL);
596         }
597     }
598     return VLC_SUCCESS;
599 }
600
601 int CommonControl(vout_display_t *vd, int query, va_list args)
602 {
603     vout_display_sys_t *sys = vd->sys;
604
605     switch (query) {
606     case VOUT_DISPLAY_CHANGE_DISPLAY_SIZE:   /* const vout_display_cfg_t *p_cfg, int is_forced */
607     case VOUT_DISPLAY_CHANGE_DISPLAY_FILLED: /* const vout_display_cfg_t *p_cfg */
608     case VOUT_DISPLAY_CHANGE_ZOOM:           /* const vout_display_cfg_t *p_cfg */
609     case VOUT_DISPLAY_CHANGE_SOURCE_ASPECT:  /* const video_format_t *p_source */
610     case VOUT_DISPLAY_CHANGE_SOURCE_CROP: {  /* const video_format_t *p_source */
611         const vout_display_cfg_t *cfg;
612         const video_format_t *source;
613         bool  is_forced = true;
614         if (query == VOUT_DISPLAY_CHANGE_SOURCE_CROP ||
615             query == VOUT_DISPLAY_CHANGE_SOURCE_ASPECT) {
616             cfg    = vd->cfg;
617             source = va_arg(args, const video_format_t *);
618         } else {
619             cfg    = va_arg(args, const vout_display_cfg_t *);
620             source = &vd->source;
621             if (query == VOUT_DISPLAY_CHANGE_DISPLAY_SIZE)
622                 is_forced = va_arg(args, int);
623         }
624         if (query == VOUT_DISPLAY_CHANGE_DISPLAY_SIZE && is_forced) {
625             /* Update dimensions */
626             if (sys->parent_window) {
627                 vout_window_SetSize(sys->parent_window, cfg->display.width, cfg->display.height);
628             } else {
629                 RECT rect_window;
630                 rect_window.top    = 0;
631                 rect_window.left   = 0;
632                 rect_window.right  = cfg->display.width;
633                 rect_window.bottom = cfg->display.height;
634                 AdjustWindowRect(&rect_window, EventThreadGetWindowStyle(sys->event), 0);
635
636                 SetWindowPos(sys->hwnd, 0, 0, 0,
637                              rect_window.right - rect_window.left,
638                              rect_window.bottom - rect_window.top, SWP_NOMOVE);
639             }
640         }
641         UpdateRects(vd, cfg, source, is_forced);
642         return VLC_SUCCESS;
643     }
644     case VOUT_DISPLAY_CHANGE_WINDOW_STATE: {       /* unsigned state */
645         const unsigned state = va_arg(args, unsigned);
646         const bool is_on_top = (state & VOUT_WINDOW_STATE_ABOVE) != 0;
647 #ifdef MODULE_NAME_IS_direct3d
648         if (sys->use_desktop && is_on_top)
649             return VLC_EGENERIC;
650 #endif
651         if (sys->parent_window) {
652             if (vout_window_SetState(sys->parent_window, state))
653                 return VLC_EGENERIC;
654         } else {
655             HMENU hMenu = GetSystemMenu(sys->hwnd, FALSE);
656
657             if (is_on_top && !(GetWindowLong(sys->hwnd, GWL_EXSTYLE) & WS_EX_TOPMOST)) {
658                 CheckMenuItem(hMenu, IDM_TOGGLE_ON_TOP, MF_BYCOMMAND | MFS_CHECKED);
659                 SetWindowPos(sys->hwnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE);
660             } else if (!is_on_top && (GetWindowLong(sys->hwnd, GWL_EXSTYLE) & WS_EX_TOPMOST)) {
661                 CheckMenuItem(hMenu, IDM_TOGGLE_ON_TOP, MF_BYCOMMAND | MFS_UNCHECKED);
662                 SetWindowPos(sys->hwnd, HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOSIZE|SWP_NOMOVE);
663             }
664         }
665         sys->is_on_top = is_on_top;
666         return VLC_SUCCESS;
667     }
668     case VOUT_DISPLAY_CHANGE_FULLSCREEN: {   /* const vout_display_cfg_t *p_cfg */
669         const vout_display_cfg_t *cfg = va_arg(args, const vout_display_cfg_t *);
670         if (CommonControlSetFullscreen(vd, cfg->is_fullscreen))
671             return VLC_EGENERIC;
672         UpdateRects(vd, NULL, NULL, false);
673         return VLC_SUCCESS;
674     }
675
676     case VOUT_DISPLAY_HIDE_MOUSE:
677         EventThreadMouseHide(sys->event);
678         return VLC_SUCCESS;
679     case VOUT_DISPLAY_RESET_PICTURES:
680         assert(0);
681     default:
682         return VLC_EGENERIC;
683     }
684 }
685
686 #ifndef UNDER_CE
687 static void DisableScreensaver(vout_display_t *vd)
688 {
689     vout_display_sys_t *sys = vd->sys;
690
691     /* disable screensaver by temporarily changing system settings */
692     sys->i_spi_lowpowertimeout = 0;
693     sys->i_spi_powerofftimeout = 0;
694     sys->i_spi_screensavetimeout = 0;
695     if (var_GetBool(vd, "disable-screensaver")) {
696         msg_Dbg(vd, "disabling screen saver");
697         SystemParametersInfo(SPI_GETLOWPOWERTIMEOUT, 0,
698                              &sys->i_spi_lowpowertimeout, 0);
699         if (0 != sys->i_spi_lowpowertimeout) {
700             SystemParametersInfo(SPI_SETLOWPOWERTIMEOUT, 0, NULL, 0);
701         }
702         SystemParametersInfo(SPI_GETPOWEROFFTIMEOUT, 0,
703                              &sys->i_spi_powerofftimeout, 0);
704         if (0 != sys->i_spi_powerofftimeout) {
705             SystemParametersInfo(SPI_SETPOWEROFFTIMEOUT, 0, NULL, 0);
706         }
707         SystemParametersInfo(SPI_GETSCREENSAVETIMEOUT, 0,
708                              &sys->i_spi_screensavetimeout, 0);
709         if (0 != sys->i_spi_screensavetimeout) {
710             SystemParametersInfo(SPI_SETSCREENSAVETIMEOUT, 0, NULL, 0);
711         }
712     }
713 }
714
715 static void RestoreScreensaver(vout_display_t *vd)
716 {
717     vout_display_sys_t *sys = vd->sys;
718
719     /* restore screensaver system settings */
720     if (0 != sys->i_spi_lowpowertimeout) {
721         SystemParametersInfo(SPI_SETLOWPOWERTIMEOUT,
722                              sys->i_spi_lowpowertimeout, NULL, 0);
723     }
724     if (0 != sys->i_spi_powerofftimeout) {
725         SystemParametersInfo(SPI_SETPOWEROFFTIMEOUT,
726                              sys->i_spi_powerofftimeout, NULL, 0);
727     }
728     if (0 != sys->i_spi_screensavetimeout) {
729         SystemParametersInfo(SPI_SETSCREENSAVETIMEOUT,
730                              sys->i_spi_screensavetimeout, NULL, 0);
731     }
732 }
733 #endif
734