]> git.sesse.net Git - vlc/blob - modules/video_output/msw/common.c
Split out non events code into common.c
[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  * UpdateRects: update clipping rectangles
77  *****************************************************************************
78  * This function is called when the window position or size are changed, and
79  * its job is to update the source and destination RECTs used to display the
80  * picture.
81  *****************************************************************************/
82 void UpdateRects( vout_thread_t *p_vout, bool b_force )
83 {
84 #define rect_src p_vout->p_sys->rect_src
85 #define rect_src_clipped p_vout->p_sys->rect_src_clipped
86 #define rect_dest p_vout->p_sys->rect_dest
87 #define rect_dest_clipped p_vout->p_sys->rect_dest_clipped
88
89     unsigned int i_width, i_height, i_x, i_y;
90
91     RECT  rect;
92     POINT point;
93
94     /* Retrieve the window size */
95     GetClientRect( p_vout->p_sys->hwnd, &rect );
96
97     /* Retrieve the window position */
98     point.x = point.y = 0;
99     ClientToScreen( p_vout->p_sys->hwnd, &point );
100
101     /* If nothing changed, we can return */
102     if( !b_force
103          && p_vout->p_sys->i_window_width == rect.right
104          && p_vout->p_sys->i_window_height == rect.bottom
105          && p_vout->p_sys->i_window_x == point.x
106          && p_vout->p_sys->i_window_y == point.y )
107     {
108         return;
109     }
110
111     /* Update the window position and size */
112     p_vout->p_sys->i_window_x = point.x;
113     p_vout->p_sys->i_window_y = point.y;
114     p_vout->p_sys->i_window_width = rect.right;
115     p_vout->p_sys->i_window_height = rect.bottom;
116
117     vout_PlacePicture( p_vout, rect.right, rect.bottom,
118                        &i_x, &i_y, &i_width, &i_height );
119
120     if( p_vout->p_sys->hvideownd )
121         SetWindowPos( p_vout->p_sys->hvideownd, 0,
122                       i_x, i_y, i_width, i_height,
123                       SWP_NOCOPYBITS|SWP_NOZORDER|SWP_ASYNCWINDOWPOS );
124
125     /* Destination image position and dimensions */
126     rect_dest.left = point.x + i_x;
127     rect_dest.right = rect_dest.left + i_width;
128     rect_dest.top = point.y + i_y;
129     rect_dest.bottom = rect_dest.top + i_height;
130
131 #ifdef MODULE_NAME_IS_directx
132     /* Apply overlay hardware constraints */
133     if( p_vout->p_sys->b_using_overlay )
134     {
135         if( p_vout->p_sys->i_align_dest_boundary )
136             rect_dest.left = ( rect_dest.left +
137                 p_vout->p_sys->i_align_dest_boundary / 2 ) &
138                 ~p_vout->p_sys->i_align_dest_boundary;
139
140         if( p_vout->p_sys->i_align_dest_size )
141             rect_dest.right = (( rect_dest.right - rect_dest.left +
142                 p_vout->p_sys->i_align_dest_size / 2 ) &
143                 ~p_vout->p_sys->i_align_dest_size) + rect_dest.left;
144     }
145
146     /* UpdateOverlay directdraw function doesn't automatically clip to the
147      * display size so we need to do it otherwise it will fail */
148
149     /* Clip the destination window */
150     if( !IntersectRect( &rect_dest_clipped, &rect_dest,
151                         &p_vout->p_sys->rect_display ) )
152     {
153         SetRectEmpty( &rect_src_clipped );
154         return;
155     }
156
157 #ifndef NDEBUG
158     msg_Dbg( p_vout, "DirectXUpdateRects image_dst_clipped coords:"
159                      " %li,%li,%li,%li",
160                      rect_dest_clipped.left, rect_dest_clipped.top,
161                      rect_dest_clipped.right, rect_dest_clipped.bottom );
162 #endif
163
164 #else /* MODULE_NAME_IS_directx */
165
166     /* AFAIK, there are no clipping constraints in Direct3D, OpenGL and GDI */
167     rect_dest_clipped = rect_dest;
168
169 #endif
170
171     /* the 2 following lines are to fix a bug when clicking on the desktop */
172     if( (rect_dest_clipped.right - rect_dest_clipped.left)==0 ||
173         (rect_dest_clipped.bottom - rect_dest_clipped.top)==0 )
174     {
175         SetRectEmpty( &rect_src_clipped );
176         return;
177     }
178
179     /* src image dimensions */
180     rect_src.left = 0;
181     rect_src.top = 0;
182     rect_src.right = p_vout->render.i_width;
183     rect_src.bottom = p_vout->render.i_height;
184
185     /* Clip the source image */
186     rect_src_clipped.left = p_vout->fmt_out.i_x_offset +
187       (rect_dest_clipped.left - rect_dest.left) *
188       p_vout->fmt_out.i_visible_width / (rect_dest.right - rect_dest.left);
189     rect_src_clipped.right = p_vout->fmt_out.i_x_offset +
190       p_vout->fmt_out.i_visible_width -
191       (rect_dest.right - rect_dest_clipped.right) *
192       p_vout->fmt_out.i_visible_width / (rect_dest.right - rect_dest.left);
193     rect_src_clipped.top = p_vout->fmt_out.i_y_offset +
194       (rect_dest_clipped.top - rect_dest.top) *
195       p_vout->fmt_out.i_visible_height / (rect_dest.bottom - rect_dest.top);
196     rect_src_clipped.bottom = p_vout->fmt_out.i_y_offset +
197       p_vout->fmt_out.i_visible_height -
198       (rect_dest.bottom - rect_dest_clipped.bottom) *
199       p_vout->fmt_out.i_visible_height / (rect_dest.bottom - rect_dest.top);
200
201 #ifdef MODULE_NAME_IS_directx
202     /* Apply overlay hardware constraints */
203     if( p_vout->p_sys->b_using_overlay )
204     {
205         if( p_vout->p_sys->i_align_src_boundary )
206             rect_src_clipped.left = ( rect_src_clipped.left +
207                 p_vout->p_sys->i_align_src_boundary / 2 ) &
208                 ~p_vout->p_sys->i_align_src_boundary;
209
210         if( p_vout->p_sys->i_align_src_size )
211             rect_src_clipped.right = (( rect_src_clipped.right -
212                 rect_src_clipped.left +
213                 p_vout->p_sys->i_align_src_size / 2 ) &
214                 ~p_vout->p_sys->i_align_src_size) + rect_src_clipped.left;
215     }
216 #endif
217
218 #ifndef NDEBUG
219     msg_Dbg( p_vout, "DirectXUpdateRects image_src_clipped"
220                      " coords: %li,%li,%li,%li",
221                      rect_src_clipped.left, rect_src_clipped.top,
222                      rect_src_clipped.right, rect_src_clipped.bottom );
223 #endif
224
225 #ifdef MODULE_NAME_IS_directx
226     /* The destination coordinates need to be relative to the current
227      * directdraw primary surface (display) */
228     rect_dest_clipped.left -= p_vout->p_sys->rect_display.left;
229     rect_dest_clipped.right -= p_vout->p_sys->rect_display.left;
230     rect_dest_clipped.top -= p_vout->p_sys->rect_display.top;
231     rect_dest_clipped.bottom -= p_vout->p_sys->rect_display.top;
232
233     if( p_vout->p_sys->b_using_overlay )
234         DirectDrawUpdateOverlay( p_vout );
235 #endif
236
237 #ifndef UNDER_CE
238     /* Windows 7 taskbar thumbnail code */
239     LPTASKBARLIST3 p_taskbl;
240     OSVERSIONINFO winVer;
241     winVer.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
242     if( GetVersionEx(&winVer) && winVer.dwMajorVersion > 5 )
243     {
244         CoInitialize( 0 );
245
246         if( S_OK == CoCreateInstance( &clsid_ITaskbarList,
247                     NULL, CLSCTX_INPROC_SERVER,
248                     &IID_ITaskbarList3,
249                     &p_taskbl) )
250         {
251             RECT rect_video, rect_parent, rect_relative;
252             HWND hroot = GetAncestor(p_vout->p_sys->hwnd,GA_ROOT);
253
254             p_taskbl->vt->HrInit(p_taskbl);
255             GetWindowRect(p_vout->p_sys->hvideownd, &rect_video);
256             GetWindowRect(hroot, &rect_parent);
257             rect_relative.left = rect_video.left - rect_parent.left - 8;
258             rect_relative.right = rect_video.right - rect_video.left + rect_relative.left;
259             rect_relative.top = rect_video.top - rect_parent.top - 10;
260             rect_relative.bottom = rect_video.bottom - rect_video.top + rect_relative.top - 25;
261
262             if (S_OK != p_taskbl->vt->SetThumbnailClip(p_taskbl, hroot, &rect_relative))
263                 msg_Err( p_vout, "SetThumbNailClip failed");
264
265             p_taskbl->vt->Release(p_taskbl);
266         }
267         CoUninitialize();
268     }
269 #endif
270     /* Signal the change in size/position */
271     p_vout->p_sys->i_changes |= DX_POSITION_CHANGE;
272
273 #undef rect_src
274 #undef rect_src_clipped
275 #undef rect_dest
276 #undef rect_dest_clipped
277 }
278
279 /*****************************************************************************
280  * Control: control facility for the vout
281  *****************************************************************************/
282 int Control( vout_thread_t *p_vout, int i_query, va_list args )
283 {
284     RECT rect_window;
285
286     switch( i_query )
287     {
288     case VOUT_SET_SIZE:
289         if( p_vout->p_sys->parent_window )
290             return vaControlParentWindow( p_vout, i_query, args );
291
292         /* Update dimensions */
293         rect_window.top = rect_window.left = 0;
294         rect_window.right  = va_arg( args, unsigned int );
295         rect_window.bottom = va_arg( args, unsigned int );
296         if( !rect_window.right ) rect_window.right = p_vout->i_window_width;
297         if( !rect_window.bottom ) rect_window.bottom = p_vout->i_window_height;
298         AdjustWindowRect( &rect_window, p_vout->p_sys->i_window_style, 0 );
299
300         SetWindowPos( p_vout->p_sys->hwnd, 0, 0, 0,
301                       rect_window.right - rect_window.left,
302                       rect_window.bottom - rect_window.top, SWP_NOMOVE );
303
304         return VLC_SUCCESS;
305
306     case VOUT_SET_STAY_ON_TOP:
307         if( p_vout->p_sys->hparent && !var_GetBool( p_vout, "fullscreen" ) )
308             return vaControlParentWindow( p_vout, i_query, args );
309
310         p_vout->p_sys->b_on_top_change = true;
311         return VLC_SUCCESS;
312
313     default:
314         return VLC_EGENERIC;
315     }
316 }
317
318
319 /* Internal wrapper over GetWindowPlacement */
320 static WINDOWPLACEMENT getWindowState(HWND hwnd)
321 {
322     WINDOWPLACEMENT window_placement;
323     window_placement.length = sizeof(WINDOWPLACEMENT);
324     GetWindowPlacement( hwnd, &window_placement );
325     return window_placement;
326 }
327
328 /* Internal wrapper to call vout_ControlWindow for hparent */
329 static int vaControlParentWindow( vout_thread_t *p_vout, int i_query,
330                                    va_list args )
331 {
332     switch( i_query )
333     {
334     case VOUT_SET_SIZE:
335     {
336         const unsigned i_width  = va_arg(args, unsigned);
337         const unsigned i_height = va_arg(args, unsigned);
338         return vout_window_SetSize( p_vout->p_sys->parent_window, i_width, i_height );
339     }
340     case VOUT_SET_STAY_ON_TOP:
341     {
342         const bool is_on_top = va_arg(args, int);
343         return vout_window_SetOnTop( p_vout->p_sys->parent_window, is_on_top );
344     }
345     default:
346         return VLC_EGENERIC;
347     }
348 }
349
350 #if 0
351 static int ControlParentWindow( vout_thread_t *p_vout, int i_query, ... )
352 {
353     va_list args;
354     int ret;
355
356     va_start( args, i_query );
357     ret = vaControlParentWindow( p_vout, i_query, args );
358     va_end( args );
359     return ret;
360 }
361 #endif
362
363 void Win32ToggleFullscreen( vout_thread_t *p_vout )
364 {
365     HWND hwnd = (p_vout->p_sys->hparent && p_vout->p_sys->hfswnd) ?
366         p_vout->p_sys->hfswnd : p_vout->p_sys->hwnd;
367
368     /* Save the current windows placement/placement to restore
369        when fullscreen is over */
370     WINDOWPLACEMENT window_placement = getWindowState( hwnd );
371
372     p_vout->b_fullscreen = ! p_vout->b_fullscreen;
373
374     /* We want to go to Fullscreen */
375     if( p_vout->b_fullscreen )
376     {
377         msg_Dbg( p_vout, "entering fullscreen mode" );
378
379         /* Change window style, no borders and no title bar */
380         int i_style = WS_CLIPCHILDREN | WS_VISIBLE;
381         SetWindowLong( hwnd, GWL_STYLE, i_style );
382
383         if( p_vout->p_sys->hparent )
384         {
385 #ifdef UNDER_CE
386             POINT point = {0,0};
387             RECT rect;
388             ClientToScreen( p_vout->p_sys->hwnd, &point );
389             GetClientRect( p_vout->p_sys->hwnd, &rect );
390             SetWindowPos( hwnd, 0, point.x, point.y,
391                           rect.right, rect.bottom,
392                           SWP_NOZORDER|SWP_FRAMECHANGED );
393 #else
394             /* Retrieve current window position so fullscreen will happen
395             *on the right screen */
396             HMONITOR hmon = MonitorFromWindow(p_vout->p_sys->hparent,
397                                             MONITOR_DEFAULTTONEAREST);
398             MONITORINFO mi;
399             if (GetMonitorInfo(hmon, &mi))
400             SetWindowPos( hwnd, 0,
401                             mi.rcMonitor.left,
402                             mi.rcMonitor.top,
403                             mi.rcMonitor.right - mi.rcMonitor.left,
404                             mi.rcMonitor.bottom - mi.rcMonitor.top,
405                             SWP_NOZORDER|SWP_FRAMECHANGED );
406 #endif
407         }
408         else
409         {
410             /* Maximize non embedded window */
411             ShowWindow( hwnd, SW_SHOWMAXIMIZED );
412         }
413
414         if( p_vout->p_sys->hparent )
415         {
416             /* Hide the previous window */
417             RECT rect;
418             GetClientRect( hwnd, &rect );
419             SetParent( p_vout->p_sys->hwnd, hwnd );
420             SetWindowPos( p_vout->p_sys->hwnd, 0, 0, 0,
421                           rect.right, rect.bottom,
422                           SWP_NOZORDER|SWP_FRAMECHANGED );
423
424 #ifdef UNDER_CE
425             HWND topLevelParent = GetParent( p_vout->p_sys->hparent );
426 #else
427             HWND topLevelParent = GetAncestor( p_vout->p_sys->hparent, GA_ROOT );
428 #endif
429             ShowWindow( topLevelParent, SW_HIDE );
430         }
431
432         SetForegroundWindow( hwnd );
433     }
434     else
435     {
436         msg_Dbg( p_vout, "leaving fullscreen mode" );
437         /* Change window style, no borders and no title bar */
438         SetWindowLong( hwnd, GWL_STYLE, p_vout->p_sys->i_window_style );
439
440         if( p_vout->p_sys->hparent )
441         {
442             RECT rect;
443             GetClientRect( p_vout->p_sys->hparent, &rect );
444             SetParent( p_vout->p_sys->hwnd, p_vout->p_sys->hparent );
445             SetWindowPos( p_vout->p_sys->hwnd, 0, 0, 0,
446                           rect.right, rect.bottom,
447                           SWP_NOZORDER|SWP_FRAMECHANGED );
448
449 #ifdef UNDER_CE
450             HWND topLevelParent = GetParent( p_vout->p_sys->hparent );
451 #else
452             HWND topLevelParent = GetAncestor( p_vout->p_sys->hparent, GA_ROOT );
453 #endif
454             ShowWindow( topLevelParent, SW_SHOW );
455             SetForegroundWindow( p_vout->p_sys->hparent );
456             ShowWindow( hwnd, SW_HIDE );
457         }
458         else
459         {
460             /* return to normal window for non embedded vout */
461             SetWindowPlacement( hwnd, &window_placement );
462             ShowWindow( hwnd, SW_SHOWNORMAL );
463         }
464
465         /* Make sure the mouse cursor is displayed */
466         PostMessage( p_vout->p_sys->hwnd, WM_VLC_SHOW_MOUSE, 0, 0 );
467     }
468
469     /* Update the object variable and trigger callback */
470     var_SetBool( p_vout, "fullscreen", p_vout->b_fullscreen );
471 }
472
473 #ifndef UNDER_CE
474 void DisableScreensaver( vout_thread_t *p_vout )
475 {
476     /* disable screensaver by temporarily changing system settings */
477     p_vout->p_sys->i_spi_lowpowertimeout = 0;
478     p_vout->p_sys->i_spi_powerofftimeout = 0;
479     p_vout->p_sys->i_spi_screensavetimeout = 0;
480     if( var_GetBool( p_vout, "disable-screensaver" ) )
481     {
482         msg_Dbg(p_vout, "disabling screen saver");
483         SystemParametersInfo(SPI_GETLOWPOWERTIMEOUT,
484             0, &(p_vout->p_sys->i_spi_lowpowertimeout), 0);
485         if( 0 != p_vout->p_sys->i_spi_lowpowertimeout ) {
486             SystemParametersInfo(SPI_SETLOWPOWERTIMEOUT, 0, NULL, 0);
487         }
488         SystemParametersInfo(SPI_GETPOWEROFFTIMEOUT, 0,
489             &(p_vout->p_sys->i_spi_powerofftimeout), 0);
490         if( 0 != p_vout->p_sys->i_spi_powerofftimeout ) {
491             SystemParametersInfo(SPI_SETPOWEROFFTIMEOUT, 0, NULL, 0);
492         }
493         SystemParametersInfo(SPI_GETSCREENSAVETIMEOUT, 0,
494             &(p_vout->p_sys->i_spi_screensavetimeout), 0);
495         if( 0 != p_vout->p_sys->i_spi_screensavetimeout ) {
496             SystemParametersInfo(SPI_SETSCREENSAVETIMEOUT, 0, NULL, 0);
497         }
498     }
499 }
500
501 void RestoreScreensaver( vout_thread_t *p_vout )
502 {
503     /* restore screensaver system settings */
504     if( 0 != p_vout->p_sys->i_spi_lowpowertimeout ) {
505         SystemParametersInfo(SPI_SETLOWPOWERTIMEOUT,
506             p_vout->p_sys->i_spi_lowpowertimeout, NULL, 0);
507     }
508     if( 0 != p_vout->p_sys->i_spi_powerofftimeout ) {
509         SystemParametersInfo(SPI_SETPOWEROFFTIMEOUT,
510             p_vout->p_sys->i_spi_powerofftimeout, NULL, 0);
511     }
512     if( 0 != p_vout->p_sys->i_spi_screensavetimeout ) {
513         SystemParametersInfo(SPI_SETSCREENSAVETIMEOUT,
514             p_vout->p_sys->i_spi_screensavetimeout, NULL, 0);
515     }
516 }
517 #endif
518