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