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