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