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