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