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