]> git.sesse.net Git - vlc/blob - modules/video_output/msw/common.c
Centralized common init/clean code (msw).
[vlc] / modules / video_output / msw / common.c
1 /*****************************************************************************
2  * common.c:
3  *****************************************************************************
4  * Copyright (C) 2001-2009 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Gildas Bazin <gbazin@videolan.org>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24
25 /*****************************************************************************
26  * Preamble: This file contains the functions related to the creation of
27  *             a window and the handling of its messages (events).
28  *****************************************************************************/
29 #ifdef HAVE_CONFIG_H
30 # include "config.h"
31 #endif
32
33 #include <errno.h>                                                 /* ENOMEM */
34 #include <ctype.h>                                              /* tolower() */
35
36 #ifndef _WIN32_WINNT
37 #   define _WIN32_WINNT 0x0500
38 #endif
39
40 #include <vlc_common.h>
41 #include <vlc_interface.h>
42 #include <vlc_playlist.h>
43 #include <vlc_vout.h>
44 #include <vlc_vout_window.h>
45
46 #include <windows.h>
47 #include <tchar.h>
48 #include <windowsx.h>
49 #include <shellapi.h>
50
51 #ifdef MODULE_NAME_IS_directx
52 #include <ddraw.h>
53 #endif
54 #ifdef MODULE_NAME_IS_direct3d
55 #include <d3d9.h>
56 #endif
57 #ifdef MODULE_NAME_IS_glwin32
58 #include <GL/gl.h>
59 #endif
60
61 #include <vlc_keys.h>
62 #include "vout.h"
63
64 #ifndef UNDER_CE
65 #include <vlc_windows_interfaces.h>
66 #endif
67
68 #ifdef UNDER_CE
69 #include <aygshell.h>
70     //WINSHELLAPI BOOL WINAPI SHFullScreen(HWND hwndRequester, DWORD dwState);
71 #endif
72
73 static int vaControlParentWindow( vout_thread_t *, int, va_list );
74
75 /* */
76 int CommonInit( vout_thread_t *p_vout )
77 {
78     vout_sys_t *p_sys = p_vout->p_sys;
79
80     p_sys->hwnd      = NULL;
81     p_sys->hvideownd = NULL;
82     p_sys->hparent   = NULL;
83     p_sys->hfswnd    = NULL;
84     p_sys->i_changes = 0;
85     SetRectEmpty( &p_sys->rect_display );
86     SetRectEmpty( &p_sys->rect_parent );
87
88     p_sys->b_cursor_hidden = 0;
89     p_sys->i_lastmoved = mdate();
90     p_sys->i_mouse_hide_timeout =
91         var_GetInteger(p_vout, "mouse-hide-timeout") * 1000;
92
93     var_Create( p_vout, "video-title", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
94
95     /* Set main window's size */
96     p_sys->i_window_width  = p_vout->i_window_width;
97     p_sys->i_window_height = p_vout->i_window_height;
98
99     if( !CreateEventThread( p_vout ) )
100         return VLC_EGENERIC;
101
102     /* Variable to indicate if the window should be on top of others */
103     /* Trigger a callback right now */
104     var_TriggerCallback( p_vout, "video-on-top" );
105
106     /* Why not with glwin32 */
107 #if !defined(UNDER_CE) && !defined(MODULE_NAME_IS_glwin32)
108     var_Create( p_vout, "disable-screensaver", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
109     DisableScreensaver ( p_vout );
110 #endif
111
112     return VLC_SUCCESS;
113 }
114
115 /* */
116 void CommonClean( vout_thread_t *p_vout )
117 {
118     StopEventThread( p_vout );
119
120 #if !defined(UNDER_CE) && !defined(MODULE_NAME_IS_glwin32)
121     RestoreScreensaver( p_vout );
122 #endif
123 }
124
125 /*****************************************************************************
126  * UpdateRects: update clipping rectangles
127  *****************************************************************************
128  * This function is called when the window position or size are changed, and
129  * its job is to update the source and destination RECTs used to display the
130  * picture.
131  *****************************************************************************/
132 void UpdateRects( vout_thread_t *p_vout, bool b_force )
133 {
134 #define rect_src p_vout->p_sys->rect_src
135 #define rect_src_clipped p_vout->p_sys->rect_src_clipped
136 #define rect_dest p_vout->p_sys->rect_dest
137 #define rect_dest_clipped p_vout->p_sys->rect_dest_clipped
138
139     unsigned int i_width, i_height, i_x, i_y;
140
141     RECT  rect;
142     POINT point;
143
144     /* Retrieve the window size */
145     GetClientRect( p_vout->p_sys->hwnd, &rect );
146
147     /* Retrieve the window position */
148     point.x = point.y = 0;
149     ClientToScreen( p_vout->p_sys->hwnd, &point );
150
151     /* If nothing changed, we can return */
152     if( !b_force
153          && p_vout->p_sys->i_window_width == rect.right
154          && p_vout->p_sys->i_window_height == rect.bottom
155          && p_vout->p_sys->i_window_x == point.x
156          && p_vout->p_sys->i_window_y == point.y )
157     {
158         return;
159     }
160
161     /* Update the window position and size */
162     p_vout->p_sys->i_window_x = point.x;
163     p_vout->p_sys->i_window_y = point.y;
164     p_vout->p_sys->i_window_width = rect.right;
165     p_vout->p_sys->i_window_height = rect.bottom;
166
167     vout_PlacePicture( p_vout, rect.right, rect.bottom,
168                        &i_x, &i_y, &i_width, &i_height );
169
170     if( p_vout->p_sys->hvideownd )
171         SetWindowPos( p_vout->p_sys->hvideownd, 0,
172                       i_x, i_y, i_width, i_height,
173                       SWP_NOCOPYBITS|SWP_NOZORDER|SWP_ASYNCWINDOWPOS );
174
175     /* Destination image position and dimensions */
176     rect_dest.left = point.x + i_x;
177     rect_dest.right = rect_dest.left + i_width;
178     rect_dest.top = point.y + i_y;
179     rect_dest.bottom = rect_dest.top + i_height;
180
181 #ifdef MODULE_NAME_IS_directx
182     /* Apply overlay hardware constraints */
183     if( p_vout->p_sys->b_using_overlay )
184     {
185         if( p_vout->p_sys->i_align_dest_boundary )
186             rect_dest.left = ( rect_dest.left +
187                 p_vout->p_sys->i_align_dest_boundary / 2 ) &
188                 ~p_vout->p_sys->i_align_dest_boundary;
189
190         if( p_vout->p_sys->i_align_dest_size )
191             rect_dest.right = (( rect_dest.right - rect_dest.left +
192                 p_vout->p_sys->i_align_dest_size / 2 ) &
193                 ~p_vout->p_sys->i_align_dest_size) + rect_dest.left;
194     }
195
196     /* UpdateOverlay directdraw function doesn't automatically clip to the
197      * display size so we need to do it otherwise it will fail */
198
199     /* Clip the destination window */
200     if( !IntersectRect( &rect_dest_clipped, &rect_dest,
201                         &p_vout->p_sys->rect_display ) )
202     {
203         SetRectEmpty( &rect_src_clipped );
204         return;
205     }
206
207 #ifndef NDEBUG
208     msg_Dbg( p_vout, "DirectXUpdateRects image_dst_clipped coords:"
209                      " %li,%li,%li,%li",
210                      rect_dest_clipped.left, rect_dest_clipped.top,
211                      rect_dest_clipped.right, rect_dest_clipped.bottom );
212 #endif
213
214 #else /* MODULE_NAME_IS_directx */
215
216     /* AFAIK, there are no clipping constraints in Direct3D, OpenGL and GDI */
217     rect_dest_clipped = rect_dest;
218
219 #endif
220
221     /* the 2 following lines are to fix a bug when clicking on the desktop */
222     if( (rect_dest_clipped.right - rect_dest_clipped.left)==0 ||
223         (rect_dest_clipped.bottom - rect_dest_clipped.top)==0 )
224     {
225         SetRectEmpty( &rect_src_clipped );
226         return;
227     }
228
229     /* src image dimensions */
230     rect_src.left = 0;
231     rect_src.top = 0;
232     rect_src.right = p_vout->render.i_width;
233     rect_src.bottom = p_vout->render.i_height;
234
235     /* Clip the source image */
236     rect_src_clipped.left = p_vout->fmt_out.i_x_offset +
237       (rect_dest_clipped.left - rect_dest.left) *
238       p_vout->fmt_out.i_visible_width / (rect_dest.right - rect_dest.left);
239     rect_src_clipped.right = p_vout->fmt_out.i_x_offset +
240       p_vout->fmt_out.i_visible_width -
241       (rect_dest.right - rect_dest_clipped.right) *
242       p_vout->fmt_out.i_visible_width / (rect_dest.right - rect_dest.left);
243     rect_src_clipped.top = p_vout->fmt_out.i_y_offset +
244       (rect_dest_clipped.top - rect_dest.top) *
245       p_vout->fmt_out.i_visible_height / (rect_dest.bottom - rect_dest.top);
246     rect_src_clipped.bottom = p_vout->fmt_out.i_y_offset +
247       p_vout->fmt_out.i_visible_height -
248       (rect_dest.bottom - rect_dest_clipped.bottom) *
249       p_vout->fmt_out.i_visible_height / (rect_dest.bottom - rect_dest.top);
250
251 #ifdef MODULE_NAME_IS_directx
252     /* Apply overlay hardware constraints */
253     if( p_vout->p_sys->b_using_overlay )
254     {
255         if( p_vout->p_sys->i_align_src_boundary )
256             rect_src_clipped.left = ( rect_src_clipped.left +
257                 p_vout->p_sys->i_align_src_boundary / 2 ) &
258                 ~p_vout->p_sys->i_align_src_boundary;
259
260         if( p_vout->p_sys->i_align_src_size )
261             rect_src_clipped.right = (( rect_src_clipped.right -
262                 rect_src_clipped.left +
263                 p_vout->p_sys->i_align_src_size / 2 ) &
264                 ~p_vout->p_sys->i_align_src_size) + rect_src_clipped.left;
265     }
266 #endif
267
268 #ifndef NDEBUG
269     msg_Dbg( p_vout, "DirectXUpdateRects image_src_clipped"
270                      " coords: %li,%li,%li,%li",
271                      rect_src_clipped.left, rect_src_clipped.top,
272                      rect_src_clipped.right, rect_src_clipped.bottom );
273 #endif
274
275 #ifdef MODULE_NAME_IS_directx
276     /* The destination coordinates need to be relative to the current
277      * directdraw primary surface (display) */
278     rect_dest_clipped.left -= p_vout->p_sys->rect_display.left;
279     rect_dest_clipped.right -= p_vout->p_sys->rect_display.left;
280     rect_dest_clipped.top -= p_vout->p_sys->rect_display.top;
281     rect_dest_clipped.bottom -= p_vout->p_sys->rect_display.top;
282
283     if( p_vout->p_sys->b_using_overlay )
284         DirectDrawUpdateOverlay( p_vout );
285 #endif
286
287 #ifndef UNDER_CE
288     /* Windows 7 taskbar thumbnail code */
289     LPTASKBARLIST3 p_taskbl;
290     OSVERSIONINFO winVer;
291     winVer.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
292     if( GetVersionEx(&winVer) && winVer.dwMajorVersion > 5 )
293     {
294         CoInitialize( 0 );
295
296         if( S_OK == CoCreateInstance( &clsid_ITaskbarList,
297                     NULL, CLSCTX_INPROC_SERVER,
298                     &IID_ITaskbarList3,
299                     &p_taskbl) )
300         {
301             RECT rect_video, rect_parent, rect_relative;
302             HWND hroot = GetAncestor(p_vout->p_sys->hwnd,GA_ROOT);
303
304             p_taskbl->vt->HrInit(p_taskbl);
305             GetWindowRect(p_vout->p_sys->hvideownd, &rect_video);
306             GetWindowRect(hroot, &rect_parent);
307             rect_relative.left = rect_video.left - rect_parent.left - 8;
308             rect_relative.right = rect_video.right - rect_video.left + rect_relative.left;
309             rect_relative.top = rect_video.top - rect_parent.top - 10;
310             rect_relative.bottom = rect_video.bottom - rect_video.top + rect_relative.top - 25;
311
312             if (S_OK != p_taskbl->vt->SetThumbnailClip(p_taskbl, hroot, &rect_relative))
313                 msg_Err( p_vout, "SetThumbNailClip failed");
314
315             p_taskbl->vt->Release(p_taskbl);
316         }
317         CoUninitialize();
318     }
319 #endif
320     /* Signal the change in size/position */
321     p_vout->p_sys->i_changes |= DX_POSITION_CHANGE;
322
323 #undef rect_src
324 #undef rect_src_clipped
325 #undef rect_dest
326 #undef rect_dest_clipped
327 }
328
329 /*****************************************************************************
330  * Control: control facility for the vout
331  *****************************************************************************/
332 int Control( vout_thread_t *p_vout, int i_query, va_list args )
333 {
334     RECT rect_window;
335
336     switch( i_query )
337     {
338     case VOUT_SET_SIZE:
339         if( p_vout->p_sys->parent_window )
340             return vaControlParentWindow( p_vout, i_query, args );
341
342         /* Update dimensions */
343         rect_window.top = rect_window.left = 0;
344         rect_window.right  = va_arg( args, unsigned int );
345         rect_window.bottom = va_arg( args, unsigned int );
346         if( !rect_window.right ) rect_window.right = p_vout->i_window_width;
347         if( !rect_window.bottom ) rect_window.bottom = p_vout->i_window_height;
348         AdjustWindowRect( &rect_window, p_vout->p_sys->i_window_style, 0 );
349
350         SetWindowPos( p_vout->p_sys->hwnd, 0, 0, 0,
351                       rect_window.right - rect_window.left,
352                       rect_window.bottom - rect_window.top, SWP_NOMOVE );
353
354         return VLC_SUCCESS;
355
356     case VOUT_SET_STAY_ON_TOP:
357         if( p_vout->p_sys->hparent && !var_GetBool( p_vout, "fullscreen" ) )
358             return vaControlParentWindow( p_vout, i_query, args );
359
360         p_vout->p_sys->b_on_top_change = true;
361         return VLC_SUCCESS;
362
363     default:
364         return VLC_EGENERIC;
365     }
366 }
367
368
369 /* Internal wrapper over GetWindowPlacement */
370 static WINDOWPLACEMENT getWindowState(HWND hwnd)
371 {
372     WINDOWPLACEMENT window_placement;
373     window_placement.length = sizeof(WINDOWPLACEMENT);
374     GetWindowPlacement( hwnd, &window_placement );
375     return window_placement;
376 }
377
378 /* Internal wrapper to call vout_ControlWindow for hparent */
379 static int vaControlParentWindow( vout_thread_t *p_vout, int i_query,
380                                    va_list args )
381 {
382     switch( i_query )
383     {
384     case VOUT_SET_SIZE:
385     {
386         const unsigned i_width  = va_arg(args, unsigned);
387         const unsigned i_height = va_arg(args, unsigned);
388         return vout_window_SetSize( p_vout->p_sys->parent_window, i_width, i_height );
389     }
390     case VOUT_SET_STAY_ON_TOP:
391     {
392         const bool is_on_top = va_arg(args, int);
393         return vout_window_SetOnTop( p_vout->p_sys->parent_window, is_on_top );
394     }
395     default:
396         return VLC_EGENERIC;
397     }
398 }
399
400 #if 0
401 static int ControlParentWindow( vout_thread_t *p_vout, int i_query, ... )
402 {
403     va_list args;
404     int ret;
405
406     va_start( args, i_query );
407     ret = vaControlParentWindow( p_vout, i_query, args );
408     va_end( args );
409     return ret;
410 }
411 #endif
412
413 void Win32ToggleFullscreen( vout_thread_t *p_vout )
414 {
415     HWND hwnd = (p_vout->p_sys->hparent && p_vout->p_sys->hfswnd) ?
416         p_vout->p_sys->hfswnd : p_vout->p_sys->hwnd;
417
418     /* Save the current windows placement/placement to restore
419        when fullscreen is over */
420     WINDOWPLACEMENT window_placement = getWindowState( hwnd );
421
422     p_vout->b_fullscreen = ! p_vout->b_fullscreen;
423
424     /* We want to go to Fullscreen */
425     if( p_vout->b_fullscreen )
426     {
427         msg_Dbg( p_vout, "entering fullscreen mode" );
428
429         /* Change window style, no borders and no title bar */
430         int i_style = WS_CLIPCHILDREN | WS_VISIBLE;
431         SetWindowLong( hwnd, GWL_STYLE, i_style );
432
433         if( p_vout->p_sys->hparent )
434         {
435 #ifdef UNDER_CE
436             POINT point = {0,0};
437             RECT rect;
438             ClientToScreen( p_vout->p_sys->hwnd, &point );
439             GetClientRect( p_vout->p_sys->hwnd, &rect );
440             SetWindowPos( hwnd, 0, point.x, point.y,
441                           rect.right, rect.bottom,
442                           SWP_NOZORDER|SWP_FRAMECHANGED );
443 #else
444             /* Retrieve current window position so fullscreen will happen
445             *on the right screen */
446             HMONITOR hmon = MonitorFromWindow(p_vout->p_sys->hparent,
447                                             MONITOR_DEFAULTTONEAREST);
448             MONITORINFO mi;
449             if (GetMonitorInfo(hmon, &mi))
450             SetWindowPos( hwnd, 0,
451                             mi.rcMonitor.left,
452                             mi.rcMonitor.top,
453                             mi.rcMonitor.right - mi.rcMonitor.left,
454                             mi.rcMonitor.bottom - mi.rcMonitor.top,
455                             SWP_NOZORDER|SWP_FRAMECHANGED );
456 #endif
457         }
458         else
459         {
460             /* Maximize non embedded window */
461             ShowWindow( hwnd, SW_SHOWMAXIMIZED );
462         }
463
464         if( p_vout->p_sys->hparent )
465         {
466             /* Hide the previous window */
467             RECT rect;
468             GetClientRect( hwnd, &rect );
469             SetParent( p_vout->p_sys->hwnd, hwnd );
470             SetWindowPos( p_vout->p_sys->hwnd, 0, 0, 0,
471                           rect.right, rect.bottom,
472                           SWP_NOZORDER|SWP_FRAMECHANGED );
473
474 #ifdef UNDER_CE
475             HWND topLevelParent = GetParent( p_vout->p_sys->hparent );
476 #else
477             HWND topLevelParent = GetAncestor( p_vout->p_sys->hparent, GA_ROOT );
478 #endif
479             ShowWindow( topLevelParent, SW_HIDE );
480         }
481
482         SetForegroundWindow( hwnd );
483     }
484     else
485     {
486         msg_Dbg( p_vout, "leaving fullscreen mode" );
487         /* Change window style, no borders and no title bar */
488         SetWindowLong( hwnd, GWL_STYLE, p_vout->p_sys->i_window_style );
489
490         if( p_vout->p_sys->hparent )
491         {
492             RECT rect;
493             GetClientRect( p_vout->p_sys->hparent, &rect );
494             SetParent( p_vout->p_sys->hwnd, p_vout->p_sys->hparent );
495             SetWindowPos( p_vout->p_sys->hwnd, 0, 0, 0,
496                           rect.right, rect.bottom,
497                           SWP_NOZORDER|SWP_FRAMECHANGED );
498
499 #ifdef UNDER_CE
500             HWND topLevelParent = GetParent( p_vout->p_sys->hparent );
501 #else
502             HWND topLevelParent = GetAncestor( p_vout->p_sys->hparent, GA_ROOT );
503 #endif
504             ShowWindow( topLevelParent, SW_SHOW );
505             SetForegroundWindow( p_vout->p_sys->hparent );
506             ShowWindow( hwnd, SW_HIDE );
507         }
508         else
509         {
510             /* return to normal window for non embedded vout */
511             SetWindowPlacement( hwnd, &window_placement );
512             ShowWindow( hwnd, SW_SHOWNORMAL );
513         }
514
515         /* Make sure the mouse cursor is displayed */
516         PostMessage( p_vout->p_sys->hwnd, WM_VLC_SHOW_MOUSE, 0, 0 );
517     }
518
519     /* Update the object variable and trigger callback */
520     var_SetBool( p_vout, "fullscreen", p_vout->b_fullscreen );
521 }
522
523 #ifndef UNDER_CE
524 void DisableScreensaver( vout_thread_t *p_vout )
525 {
526     /* disable screensaver by temporarily changing system settings */
527     p_vout->p_sys->i_spi_lowpowertimeout = 0;
528     p_vout->p_sys->i_spi_powerofftimeout = 0;
529     p_vout->p_sys->i_spi_screensavetimeout = 0;
530     if( var_GetBool( p_vout, "disable-screensaver" ) )
531     {
532         msg_Dbg(p_vout, "disabling screen saver");
533         SystemParametersInfo(SPI_GETLOWPOWERTIMEOUT,
534             0, &(p_vout->p_sys->i_spi_lowpowertimeout), 0);
535         if( 0 != p_vout->p_sys->i_spi_lowpowertimeout ) {
536             SystemParametersInfo(SPI_SETLOWPOWERTIMEOUT, 0, NULL, 0);
537         }
538         SystemParametersInfo(SPI_GETPOWEROFFTIMEOUT, 0,
539             &(p_vout->p_sys->i_spi_powerofftimeout), 0);
540         if( 0 != p_vout->p_sys->i_spi_powerofftimeout ) {
541             SystemParametersInfo(SPI_SETPOWEROFFTIMEOUT, 0, NULL, 0);
542         }
543         SystemParametersInfo(SPI_GETSCREENSAVETIMEOUT, 0,
544             &(p_vout->p_sys->i_spi_screensavetimeout), 0);
545         if( 0 != p_vout->p_sys->i_spi_screensavetimeout ) {
546             SystemParametersInfo(SPI_SETSCREENSAVETIMEOUT, 0, NULL, 0);
547         }
548     }
549 }
550
551 void RestoreScreensaver( vout_thread_t *p_vout )
552 {
553     /* restore screensaver system settings */
554     if( 0 != p_vout->p_sys->i_spi_lowpowertimeout ) {
555         SystemParametersInfo(SPI_SETLOWPOWERTIMEOUT,
556             p_vout->p_sys->i_spi_lowpowertimeout, NULL, 0);
557     }
558     if( 0 != p_vout->p_sys->i_spi_powerofftimeout ) {
559         SystemParametersInfo(SPI_SETPOWEROFFTIMEOUT,
560             p_vout->p_sys->i_spi_powerofftimeout, NULL, 0);
561     }
562     if( 0 != p_vout->p_sys->i_spi_screensavetimeout ) {
563         SystemParametersInfo(SPI_SETSCREENSAVETIMEOUT,
564             p_vout->p_sys->i_spi_screensavetimeout, NULL, 0);
565     }
566 }
567 #endif
568