]> git.sesse.net Git - vlc/blob - modules/video_output/msw/common_vo.c
msw : support for vout_window_SetFullscreen
[vlc] / modules / video_output / msw / common_vo.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
33 #include <vlc_common.h>
34 #include <vlc_vout.h>
35 #include <vlc_vout_window.h>
36
37 #include <windows.h>
38 #include <windowsx.h>
39 #include <shellapi.h>
40
41 #ifdef MODULE_NAME_IS_directx
42 #include <ddraw.h>
43 #endif
44 #ifdef MODULE_NAME_IS_direct3d
45 #include <d3d9.h>
46 #endif
47 #ifdef MODULE_NAME_IS_glwin32
48 #include <GL/gl.h>
49 #endif
50
51 #include "vout.h"
52
53 #ifndef UNDER_CE
54 #include <vlc_windows_interfaces.h>
55 #endif
56
57 #ifdef UNDER_CE
58 #include <aygshell.h>
59     //WINSHELLAPI BOOL WINAPI SHFullScreen(HWND hwndRequester, DWORD dwState);
60 #endif
61
62 static int vaControlParentWindow( vout_thread_t *, int, va_list );
63
64 /* */
65 int CommonInit( vout_thread_t *p_vout )
66 {
67     vout_sys_t *p_sys = p_vout->p_sys;
68
69     p_sys->hwnd      = NULL;
70     p_sys->hvideownd = NULL;
71     p_sys->hparent   = NULL;
72     p_sys->hfswnd    = NULL;
73     p_sys->i_changes = 0;
74     SetRectEmpty( &p_sys->rect_display );
75     SetRectEmpty( &p_sys->rect_parent );
76
77     var_Create( p_vout, "video-title", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
78
79     /* Set main window's size */
80     vout_window_cfg_t wnd_cfg;
81
82     memset( &wnd_cfg, 0, sizeof(wnd_cfg) );
83     wnd_cfg.type   = VOUT_WINDOW_TYPE_HWND;
84     wnd_cfg.x      = 0;
85     wnd_cfg.y      = 0;
86     wnd_cfg.width  = p_vout->i_window_width;
87     wnd_cfg.height = p_vout->i_window_height;
88
89     p_sys->p_event = EventThreadCreate( p_vout, &wnd_cfg );
90     if( !p_sys->p_event )
91         return VLC_EGENERIC;
92
93     event_cfg_t cfg;
94     memset(&cfg, 0, sizeof(cfg));
95 #ifdef MODULE_NAME_IS_direct3d
96     cfg.use_desktop = p_vout->p_sys->b_desktop;
97 #endif
98 #ifdef MODULE_NAME_IS_directx
99     cfg.use_overlay = p_vout->p_sys->b_using_overlay;
100 #endif
101     event_hwnd_t hwnd;
102     if( EventThreadStart( p_sys->p_event, &hwnd, &cfg ) )
103         return VLC_EGENERIC;
104
105     p_sys->parent_window = hwnd.parent_window;
106     p_sys->hparent       = hwnd.hparent;
107     p_sys->hwnd          = hwnd.hwnd;
108     p_sys->hvideownd     = hwnd.hvideownd;
109     p_sys->hfswnd        = hwnd.hfswnd;
110
111     /* Variable to indicate if the window should be on top of others */
112     /* Trigger a callback right now */
113     var_TriggerCallback( p_vout, "video-on-top" );
114
115     /* Why not with glwin32 */
116 #if !defined(UNDER_CE) && !defined(MODULE_NAME_IS_glwin32)
117     var_Create( p_vout, "disable-screensaver", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
118     DisableScreensaver ( p_vout );
119 #endif
120
121     return VLC_SUCCESS;
122 }
123
124 /* */
125 void CommonClean( vout_thread_t *p_vout )
126 {
127     vout_sys_t *p_sys = p_vout->p_sys;
128
129     ExitFullscreen( p_vout );
130     if( p_sys->p_event )
131     {
132         EventThreadStop( p_sys->p_event );
133         EventThreadDestroy( p_sys->p_event );
134     }
135
136 #if !defined(UNDER_CE) && !defined(MODULE_NAME_IS_glwin32)
137     RestoreScreensaver( p_vout );
138 #endif
139 }
140
141 void CommonManage( vout_thread_t *p_vout )
142 {
143     /* If we do not control our window, we check for geometry changes
144      * ourselves because the parent might not send us its events. */
145     if( p_vout->p_sys->hparent )
146     {
147         RECT rect_parent;
148         POINT point;
149
150         GetClientRect( p_vout->p_sys->hparent, &rect_parent );
151         point.x = point.y = 0;
152         ClientToScreen( p_vout->p_sys->hparent, &point );
153         OffsetRect( &rect_parent, point.x, point.y );
154
155         if( !EqualRect( &rect_parent, &p_vout->p_sys->rect_parent ) )
156         {
157             p_vout->p_sys->rect_parent = rect_parent;
158
159             /* FIXME I find such #ifdef quite weirds. Are they really needed ? */
160
161 #if defined(MODULE_NAME_IS_direct3d)
162             SetWindowPos( p_vout->p_sys->hwnd, 0, 0, 0,
163                           rect_parent.right - rect_parent.left,
164                           rect_parent.bottom - rect_parent.top,
165                           SWP_NOZORDER );
166             UpdateRects( p_vout, true );
167 #else
168             /* This one is to force the update even if only
169              * the position has changed */
170             SetWindowPos( p_vout->p_sys->hwnd, 0, 1, 1,
171                           rect_parent.right - rect_parent.left,
172                           rect_parent.bottom - rect_parent.top, 0 );
173
174             SetWindowPos( p_vout->p_sys->hwnd, 0, 0, 0,
175                           rect_parent.right - rect_parent.left,
176                           rect_parent.bottom - rect_parent.top, 0 );
177
178 #if defined(MODULE_NAME_IS_wingdi) || defined(MODULE_NAME_IS_wingapi)
179             unsigned int i_x, i_y, i_width, i_height;
180             vout_PlacePicture( p_vout, rect_parent.right - rect_parent.left,
181                                rect_parent.bottom - rect_parent.top,
182                                &i_x, &i_y, &i_width, &i_height );
183
184             SetWindowPos( p_vout->p_sys->hvideownd, HWND_TOP,
185                           i_x, i_y, i_width, i_height, 0 );
186 #endif
187 #endif
188         }
189     }
190
191     /* */
192     p_vout->p_sys->i_changes |= EventThreadRetreiveChanges( p_vout->p_sys->p_event );
193
194     /* autoscale toggle */
195     if( p_vout->i_changes & VOUT_SCALE_CHANGE )
196     {
197         p_vout->i_changes &= ~VOUT_SCALE_CHANGE;
198
199         p_vout->b_autoscale = var_GetBool( p_vout, "autoscale" );
200         p_vout->i_zoom = (int) ZOOM_FP_FACTOR;
201
202         UpdateRects( p_vout, true );
203     }
204
205     /* scaling factor */
206     if( p_vout->i_changes & VOUT_ZOOM_CHANGE )
207     {
208         p_vout->i_changes &= ~VOUT_ZOOM_CHANGE;
209
210         p_vout->b_autoscale = false;
211         p_vout->i_zoom =
212             (int)( ZOOM_FP_FACTOR * var_GetFloat( p_vout, "scale" ) );
213         UpdateRects( p_vout, true );
214     }
215
216     /* Check for cropping / aspect changes */
217     if( p_vout->i_changes & VOUT_CROP_CHANGE ||
218         p_vout->i_changes & VOUT_ASPECT_CHANGE )
219     {
220         p_vout->i_changes &= ~VOUT_CROP_CHANGE;
221         p_vout->i_changes &= ~VOUT_ASPECT_CHANGE;
222
223         p_vout->fmt_out.i_x_offset = p_vout->fmt_in.i_x_offset;
224         p_vout->fmt_out.i_y_offset = p_vout->fmt_in.i_y_offset;
225         p_vout->fmt_out.i_visible_width = p_vout->fmt_in.i_visible_width;
226         p_vout->fmt_out.i_visible_height = p_vout->fmt_in.i_visible_height;
227         p_vout->fmt_out.i_sar_num = p_vout->fmt_in.i_sar_num;
228         p_vout->fmt_out.i_sar_den = p_vout->fmt_in.i_sar_den;
229         p_vout->output.i_aspect = (int64_t)VOUT_ASPECT_FACTOR *
230                                    p_vout->fmt_in.i_sar_num * p_vout->fmt_in.i_width /
231                                    (p_vout->fmt_in.i_sar_den * p_vout->fmt_in.i_height);
232         UpdateRects( p_vout, true );
233     }
234
235     /* We used to call the Win32 PeekMessage function here to read the window
236      * messages. But since window can stay blocked into this function for a
237      * long time (for example when you move your window on the screen), I
238      * decided to isolate PeekMessage in another thread. */
239
240     /*
241      * Fullscreen change
242      */
243     if( p_vout->i_changes & VOUT_FULLSCREEN_CHANGE
244         || p_vout->p_sys->i_changes & VOUT_FULLSCREEN_CHANGE )
245     {
246         Win32ToggleFullscreen( p_vout );
247
248         p_vout->i_changes &= ~VOUT_FULLSCREEN_CHANGE;
249         p_vout->p_sys->i_changes &= ~VOUT_FULLSCREEN_CHANGE;
250     }
251
252     /*
253      * Pointer change
254      */
255     EventThreadMouseAutoHide( p_vout->p_sys->p_event );
256
257     /*
258      * "Always on top" status change
259      */
260     if( p_vout->p_sys->b_on_top_change )
261     {
262         HMENU hMenu = GetSystemMenu( p_vout->p_sys->hwnd, FALSE );
263         bool b = var_GetBool( p_vout, "video-on-top" );
264
265         /* Set the window on top if necessary */
266         if( b && !( GetWindowLong( p_vout->p_sys->hwnd, GWL_EXSTYLE )
267                            & WS_EX_TOPMOST ) )
268         {
269             CheckMenuItem( hMenu, IDM_TOGGLE_ON_TOP,
270                            MF_BYCOMMAND | MFS_CHECKED );
271             SetWindowPos( p_vout->p_sys->hwnd, HWND_TOPMOST, 0, 0, 0, 0,
272                           SWP_NOSIZE | SWP_NOMOVE );
273         }
274         else
275         /* The window shouldn't be on top */
276         if( !b && ( GetWindowLong( p_vout->p_sys->hwnd, GWL_EXSTYLE )
277                            & WS_EX_TOPMOST ) )
278         {
279             CheckMenuItem( hMenu, IDM_TOGGLE_ON_TOP,
280                            MF_BYCOMMAND | MFS_UNCHECKED );
281             SetWindowPos( p_vout->p_sys->hwnd, HWND_NOTOPMOST, 0, 0, 0, 0,
282                           SWP_NOSIZE | SWP_NOMOVE );
283         }
284
285         p_vout->p_sys->b_on_top_change = false;
286     }
287 }
288
289 /*****************************************************************************
290  * UpdateRects: update clipping rectangles
291  *****************************************************************************
292  * This function is called when the window position or size are changed, and
293  * its job is to update the source and destination RECTs used to display the
294  * picture.
295  *****************************************************************************/
296 void UpdateRects( vout_thread_t *p_vout, bool b_force )
297 {
298 #define rect_src p_vout->p_sys->rect_src
299 #define rect_src_clipped p_vout->p_sys->rect_src_clipped
300 #define rect_dest p_vout->p_sys->rect_dest
301 #define rect_dest_clipped p_vout->p_sys->rect_dest_clipped
302
303     unsigned int i_width, i_height, i_x, i_y;
304
305     RECT  rect;
306     POINT point;
307
308     /* Retrieve the window size */
309     GetClientRect( p_vout->p_sys->hwnd, &rect );
310
311     /* Retrieve the window position */
312     point.x = point.y = 0;
313     ClientToScreen( p_vout->p_sys->hwnd, &point );
314
315     /* If nothing changed, we can return */
316     bool b_changed;
317     EventThreadUpdateWindowPosition( p_vout->p_sys->p_event, &b_changed,
318                                      point.x, point.y,
319                                      rect.right, rect.bottom );
320     if( !b_force && !b_changed )
321         return;
322
323     /* Update the window position and size */
324     vout_PlacePicture( p_vout, rect.right, rect.bottom,
325                        &i_x, &i_y, &i_width, &i_height );
326
327     if( p_vout->p_sys->hvideownd )
328         SetWindowPos( p_vout->p_sys->hvideownd, 0,
329                       i_x, i_y, i_width, i_height,
330                       SWP_NOCOPYBITS|SWP_NOZORDER|SWP_ASYNCWINDOWPOS );
331
332     /* Destination image position and dimensions */
333 #if defined(MODULE_NAME_IS_direct3d)
334     rect_dest.left   = 0;
335     rect_dest.right  = i_width;
336     rect_dest.top    = 0;
337     rect_dest.bottom = i_height;
338 #else
339     rect_dest.left = point.x + i_x;
340     rect_dest.right = rect_dest.left + i_width;
341     rect_dest.top = point.y + i_y;
342     rect_dest.bottom = rect_dest.top + i_height;
343
344 #ifdef MODULE_NAME_IS_directx
345     /* Apply overlay hardware constraints */
346     if( p_vout->p_sys->b_using_overlay )
347     {
348         if( p_vout->p_sys->i_align_dest_boundary )
349             rect_dest.left = ( rect_dest.left +
350                 p_vout->p_sys->i_align_dest_boundary / 2 ) &
351                 ~p_vout->p_sys->i_align_dest_boundary;
352
353         if( p_vout->p_sys->i_align_dest_size )
354             rect_dest.right = (( rect_dest.right - rect_dest.left +
355                 p_vout->p_sys->i_align_dest_size / 2 ) &
356                 ~p_vout->p_sys->i_align_dest_size) + rect_dest.left;
357     }
358 #endif
359
360 #endif
361
362 #if defined(MODULE_NAME_IS_directx) || defined(MODULE_NAME_IS_direct3d)
363     /* UpdateOverlay directdraw function doesn't automatically clip to the
364      * display size so we need to do it otherwise it will fail
365      * It is also needed for d3d to avoid exceding our surface size */
366
367     /* Clip the destination window */
368     if( !IntersectRect( &rect_dest_clipped, &rect_dest,
369                         &p_vout->p_sys->rect_display ) )
370     {
371         SetRectEmpty( &rect_src_clipped );
372         return;
373     }
374
375 #ifndef NDEBUG
376     msg_Dbg( p_vout, "DirectXUpdateRects image_dst_clipped coords:"
377                      " %li,%li,%li,%li",
378                      rect_dest_clipped.left, rect_dest_clipped.top,
379                      rect_dest_clipped.right, rect_dest_clipped.bottom );
380 #endif
381
382 #else
383
384     /* AFAIK, there are no clipping constraints in Direct3D, OpenGL and GDI */
385     rect_dest_clipped = rect_dest;
386
387 #endif
388
389     /* the 2 following lines are to fix a bug when clicking on the desktop */
390     if( (rect_dest_clipped.right - rect_dest_clipped.left)==0 ||
391         (rect_dest_clipped.bottom - rect_dest_clipped.top)==0 )
392     {
393         SetRectEmpty( &rect_src_clipped );
394         return;
395     }
396
397     /* src image dimensions */
398     rect_src.left = 0;
399     rect_src.top = 0;
400     rect_src.right = p_vout->render.i_width;
401     rect_src.bottom = p_vout->render.i_height;
402
403     /* Clip the source image */
404     rect_src_clipped.left = p_vout->fmt_out.i_x_offset +
405       (rect_dest_clipped.left - rect_dest.left) *
406       p_vout->fmt_out.i_visible_width / (rect_dest.right - rect_dest.left);
407     rect_src_clipped.right = p_vout->fmt_out.i_x_offset +
408       p_vout->fmt_out.i_visible_width -
409       (rect_dest.right - rect_dest_clipped.right) *
410       p_vout->fmt_out.i_visible_width / (rect_dest.right - rect_dest.left);
411     rect_src_clipped.top = p_vout->fmt_out.i_y_offset +
412       (rect_dest_clipped.top - rect_dest.top) *
413       p_vout->fmt_out.i_visible_height / (rect_dest.bottom - rect_dest.top);
414     rect_src_clipped.bottom = p_vout->fmt_out.i_y_offset +
415       p_vout->fmt_out.i_visible_height -
416       (rect_dest.bottom - rect_dest_clipped.bottom) *
417       p_vout->fmt_out.i_visible_height / (rect_dest.bottom - rect_dest.top);
418
419 #ifdef MODULE_NAME_IS_directx
420     /* Apply overlay hardware constraints */
421     if( p_vout->p_sys->b_using_overlay )
422     {
423         if( p_vout->p_sys->i_align_src_boundary )
424             rect_src_clipped.left = ( rect_src_clipped.left +
425                 p_vout->p_sys->i_align_src_boundary / 2 ) &
426                 ~p_vout->p_sys->i_align_src_boundary;
427
428         if( p_vout->p_sys->i_align_src_size )
429             rect_src_clipped.right = (( rect_src_clipped.right -
430                 rect_src_clipped.left +
431                 p_vout->p_sys->i_align_src_size / 2 ) &
432                 ~p_vout->p_sys->i_align_src_size) + rect_src_clipped.left;
433     }
434 #elif defined(MODULE_NAME_IS_direct3d)
435     /* Needed at least with YUV content */
436     rect_src_clipped.left &= ~1;
437     rect_src_clipped.right &= ~1;
438     rect_src_clipped.top &= ~1;
439     rect_src_clipped.bottom &= ~1;
440 #endif
441
442 #ifndef NDEBUG
443     msg_Dbg( p_vout, "DirectXUpdateRects image_src_clipped"
444                      " coords: %li,%li,%li,%li",
445                      rect_src_clipped.left, rect_src_clipped.top,
446                      rect_src_clipped.right, rect_src_clipped.bottom );
447 #endif
448
449 #ifdef MODULE_NAME_IS_directx
450     /* The destination coordinates need to be relative to the current
451      * directdraw primary surface (display) */
452     rect_dest_clipped.left -= p_vout->p_sys->rect_display.left;
453     rect_dest_clipped.right -= p_vout->p_sys->rect_display.left;
454     rect_dest_clipped.top -= p_vout->p_sys->rect_display.top;
455     rect_dest_clipped.bottom -= p_vout->p_sys->rect_display.top;
456
457     if( p_vout->p_sys->b_using_overlay )
458         DirectDrawUpdateOverlay( p_vout );
459 #endif
460
461 #ifndef UNDER_CE
462     /* Windows 7 taskbar thumbnail code */
463     LPTASKBARLIST3 p_taskbl;
464     OSVERSIONINFO winVer;
465     winVer.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
466     if( GetVersionEx(&winVer) && winVer.dwMajorVersion > 5 )
467     {
468         CoInitialize( 0 );
469
470         if( S_OK == CoCreateInstance( &clsid_ITaskbarList,
471                     NULL, CLSCTX_INPROC_SERVER,
472                     &IID_ITaskbarList3,
473                     &p_taskbl) )
474         {
475             RECT rect_video, rect_parent, rect_relative;
476             HWND hroot = GetAncestor(p_vout->p_sys->hwnd,GA_ROOT);
477
478             p_taskbl->vt->HrInit(p_taskbl);
479             GetWindowRect(p_vout->p_sys->hvideownd, &rect_video);
480             GetWindowRect(hroot, &rect_parent);
481             rect_relative.left = rect_video.left - rect_parent.left - 8;
482             rect_relative.right = rect_video.right - rect_video.left + rect_relative.left;
483             rect_relative.top = rect_video.top - rect_parent.top - 10;
484             rect_relative.bottom = rect_video.bottom - rect_video.top + rect_relative.top - 25;
485
486             if (S_OK != p_taskbl->vt->SetThumbnailClip(p_taskbl, hroot, &rect_relative))
487                 msg_Err( p_vout, "SetThumbNailClip failed");
488
489             p_taskbl->vt->Release(p_taskbl);
490         }
491         CoUninitialize();
492     }
493 #endif
494     /* Signal the change in size/position */
495     p_vout->p_sys->i_changes |= DX_POSITION_CHANGE;
496
497 #undef rect_src
498 #undef rect_src_clipped
499 #undef rect_dest
500 #undef rect_dest_clipped
501 }
502
503 /*****************************************************************************
504  * Control: control facility for the vout
505  *****************************************************************************/
506 int Control( vout_thread_t *p_vout, int i_query, va_list args )
507 {
508     RECT rect_window;
509
510     switch( i_query )
511     {
512     case VOUT_SET_SIZE:
513         if( p_vout->p_sys->parent_window )
514             return vaControlParentWindow( p_vout, i_query, args );
515
516         /* Update dimensions */
517         rect_window.top = rect_window.left = 0;
518         rect_window.right  = va_arg( args, unsigned int );
519         rect_window.bottom = va_arg( args, unsigned int );
520         if( !rect_window.right ) rect_window.right = p_vout->i_window_width;
521         if( !rect_window.bottom ) rect_window.bottom = p_vout->i_window_height;
522         AdjustWindowRect( &rect_window, EventThreadGetWindowStyle( p_vout->p_sys->p_event ), 0 );
523
524         SetWindowPos( p_vout->p_sys->hwnd, 0, 0, 0,
525                       rect_window.right - rect_window.left,
526                       rect_window.bottom - rect_window.top, SWP_NOMOVE );
527
528         return VLC_SUCCESS;
529
530     case VOUT_SET_STAY_ON_TOP:
531         if( p_vout->p_sys->hparent && !var_GetBool( p_vout, "fullscreen" ) )
532             return vaControlParentWindow( p_vout, i_query, args );
533
534         p_vout->p_sys->b_on_top_change = true;
535         return VLC_SUCCESS;
536
537     default:
538         return VLC_EGENERIC;
539     }
540 }
541
542
543 /* Internal wrapper over GetWindowPlacement */
544 static WINDOWPLACEMENT getWindowState(HWND hwnd)
545 {
546     WINDOWPLACEMENT window_placement;
547     window_placement.length = sizeof(WINDOWPLACEMENT);
548     GetWindowPlacement( hwnd, &window_placement );
549     return window_placement;
550 }
551
552 /* Internal wrapper to call vout_ControlWindow for hparent */
553 static int vaControlParentWindow( vout_thread_t *p_vout, int i_query,
554                                    va_list args )
555 {
556     switch( i_query )
557     {
558     case VOUT_SET_SIZE:
559     {
560         const unsigned i_width  = va_arg(args, unsigned);
561         const unsigned i_height = va_arg(args, unsigned);
562         return vout_window_SetSize( p_vout->p_sys->parent_window, i_width, i_height );
563     }
564     case VOUT_SET_STAY_ON_TOP:
565     {
566         const bool is_on_top = va_arg(args, int);
567         return vout_window_SetState( p_vout->p_sys->parent_window, is_on_top );
568     }
569     default:
570         return VLC_EGENERIC;
571     }
572 }
573
574 #if 0
575 static int ControlParentWindow( vout_thread_t *p_vout, int i_query, ... )
576 {
577     va_list args;
578     int ret;
579
580     va_start( args, i_query );
581     ret = vaControlParentWindow( p_vout, i_query, args );
582     va_end( args );
583     return ret;
584 }
585 #endif
586
587 void ExitFullscreen( vout_thread_t *p_vout )
588 {
589     if( p_vout->b_fullscreen )
590     {
591         msg_Dbg( p_vout, "Quitting fullscreen" );
592         Win32ToggleFullscreen( p_vout );
593         /* Force fullscreen in the core for the next video */
594         var_SetBool( p_vout, "fullscreen", true );
595     }
596 }
597
598 void Win32ToggleFullscreen( vout_thread_t *p_vout )
599 {
600     HWND hwnd = (p_vout->p_sys->hparent && p_vout->p_sys->hfswnd) ?
601         p_vout->p_sys->hfswnd : p_vout->p_sys->hwnd;
602
603     /* Save the current windows placement/placement to restore
604        when fullscreen is over */
605     WINDOWPLACEMENT window_placement = getWindowState( hwnd );
606
607     p_vout->b_fullscreen = ! p_vout->b_fullscreen;
608
609     if( p_vout->p_sys->parent_window )
610     {
611         vout_window_SetFullScreen( p_vout->p_sys->parent_window,
612                                    p_vout->b_fullscreen );
613         return;
614     }
615
616     /* We want to go to Fullscreen */
617     if( p_vout->b_fullscreen )
618     {
619         msg_Dbg( p_vout, "entering fullscreen mode" );
620
621         /* Change window style, no borders and no title bar */
622         int i_style = WS_CLIPCHILDREN | WS_VISIBLE;
623         SetWindowLong( hwnd, GWL_STYLE, i_style );
624
625         if( p_vout->p_sys->hparent )
626         {
627 #ifdef UNDER_CE
628             POINT point = {0,0};
629             RECT rect;
630             ClientToScreen( p_vout->p_sys->hwnd, &point );
631             GetClientRect( p_vout->p_sys->hwnd, &rect );
632             SetWindowPos( hwnd, 0, point.x, point.y,
633                           rect.right, rect.bottom,
634                           SWP_NOZORDER|SWP_FRAMECHANGED );
635 #else
636             /* Retrieve current window position so fullscreen will happen
637             *on the right screen */
638             HMONITOR hmon = MonitorFromWindow(p_vout->p_sys->hparent,
639                                             MONITOR_DEFAULTTONEAREST);
640             MONITORINFO mi;
641             mi.cbSize = sizeof(MONITORINFO);
642             if (GetMonitorInfo(hmon, &mi))
643             SetWindowPos( hwnd, 0,
644                             mi.rcMonitor.left,
645                             mi.rcMonitor.top,
646                             mi.rcMonitor.right - mi.rcMonitor.left,
647                             mi.rcMonitor.bottom - mi.rcMonitor.top,
648                             SWP_NOZORDER|SWP_FRAMECHANGED );
649 #endif
650         }
651         else
652         {
653             /* Maximize non embedded window */
654             ShowWindow( hwnd, SW_SHOWMAXIMIZED );
655         }
656
657         if( p_vout->p_sys->hparent )
658         {
659             /* Hide the previous window */
660             RECT rect;
661             GetClientRect( hwnd, &rect );
662             SetParent( p_vout->p_sys->hwnd, hwnd );
663             SetWindowPos( p_vout->p_sys->hwnd, 0, 0, 0,
664                           rect.right, rect.bottom,
665                           SWP_NOZORDER|SWP_FRAMECHANGED );
666
667 #ifdef UNDER_CE
668             HWND topLevelParent = GetParent( p_vout->p_sys->hparent );
669 #else
670             HWND topLevelParent = GetAncestor( p_vout->p_sys->hparent, GA_ROOT );
671 #endif
672             ShowWindow( topLevelParent, SW_HIDE );
673         }
674
675         SetForegroundWindow( hwnd );
676     }
677     else
678     {
679         msg_Dbg( p_vout, "leaving fullscreen mode" );
680         /* Change window style, no borders and no title bar */
681         SetWindowLong( hwnd, GWL_STYLE, EventThreadGetWindowStyle( p_vout->p_sys->p_event ) );
682
683         if( p_vout->p_sys->hparent )
684         {
685             RECT rect;
686             GetClientRect( p_vout->p_sys->hparent, &rect );
687             SetParent( p_vout->p_sys->hwnd, p_vout->p_sys->hparent );
688             SetWindowPos( p_vout->p_sys->hwnd, 0, 0, 0,
689                           rect.right, rect.bottom,
690                           SWP_NOZORDER|SWP_FRAMECHANGED );
691
692 #ifdef UNDER_CE
693             HWND topLevelParent = GetParent( p_vout->p_sys->hparent );
694 #else
695             HWND topLevelParent = GetAncestor( p_vout->p_sys->hparent, GA_ROOT );
696 #endif
697             ShowWindow( topLevelParent, SW_SHOW );
698             SetForegroundWindow( p_vout->p_sys->hparent );
699             ShowWindow( hwnd, SW_HIDE );
700         }
701         else
702         {
703             /* return to normal window for non embedded vout */
704             SetWindowPlacement( hwnd, &window_placement );
705             ShowWindow( hwnd, SW_SHOWNORMAL );
706         }
707
708         /* Make sure the mouse cursor is displayed */
709         EventThreadMouseShow( p_vout->p_sys->p_event );
710     }
711
712     /* Update the object variable and trigger callback */
713     var_SetBool( p_vout, "fullscreen", p_vout->b_fullscreen );
714 }
715
716 #ifndef UNDER_CE
717 void DisableScreensaver( vout_thread_t *p_vout )
718 {
719     /* disable screensaver by temporarily changing system settings */
720     p_vout->p_sys->i_spi_lowpowertimeout = 0;
721     p_vout->p_sys->i_spi_powerofftimeout = 0;
722     p_vout->p_sys->i_spi_screensavetimeout = 0;
723     if( var_GetBool( p_vout, "disable-screensaver" ) )
724     {
725         msg_Dbg(p_vout, "disabling screen saver");
726         SystemParametersInfo(SPI_GETLOWPOWERTIMEOUT,
727             0, &(p_vout->p_sys->i_spi_lowpowertimeout), 0);
728         if( 0 != p_vout->p_sys->i_spi_lowpowertimeout ) {
729             SystemParametersInfo(SPI_SETLOWPOWERTIMEOUT, 0, NULL, 0);
730         }
731         SystemParametersInfo(SPI_GETPOWEROFFTIMEOUT, 0,
732             &(p_vout->p_sys->i_spi_powerofftimeout), 0);
733         if( 0 != p_vout->p_sys->i_spi_powerofftimeout ) {
734             SystemParametersInfo(SPI_SETPOWEROFFTIMEOUT, 0, NULL, 0);
735         }
736         SystemParametersInfo(SPI_GETSCREENSAVETIMEOUT, 0,
737             &(p_vout->p_sys->i_spi_screensavetimeout), 0);
738         if( 0 != p_vout->p_sys->i_spi_screensavetimeout ) {
739             SystemParametersInfo(SPI_SETSCREENSAVETIMEOUT, 0, NULL, 0);
740         }
741     }
742 }
743
744 void RestoreScreensaver( vout_thread_t *p_vout )
745 {
746     /* restore screensaver system settings */
747     if( 0 != p_vout->p_sys->i_spi_lowpowertimeout ) {
748         SystemParametersInfo(SPI_SETLOWPOWERTIMEOUT,
749             p_vout->p_sys->i_spi_lowpowertimeout, NULL, 0);
750     }
751     if( 0 != p_vout->p_sys->i_spi_powerofftimeout ) {
752         SystemParametersInfo(SPI_SETPOWEROFFTIMEOUT,
753             p_vout->p_sys->i_spi_powerofftimeout, NULL, 0);
754     }
755     if( 0 != p_vout->p_sys->i_spi_screensavetimeout ) {
756         SystemParametersInfo(SPI_SETSCREENSAVETIMEOUT,
757             p_vout->p_sys->i_spi_screensavetimeout, NULL, 0);
758     }
759 }
760 #endif
761