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