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