]> git.sesse.net Git - vlc/blob - modules/video_output/msw/wingdi.c
Fix file mode
[vlc] / modules / video_output / msw / wingdi.c
1 /*****************************************************************************
2  * wingdi.c : Win32 / WinCE GDI video output plugin for vlc
3  *****************************************************************************
4  * Copyright (C) 2002 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Gildas Bazin <gbazin@videolan.org>
8  *          Samuel Hocevar <sam@zoy.org>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28
29 #ifdef HAVE_CONFIG_H
30 # include "config.h"
31 #endif
32
33 #include <vlc_common.h>
34 #include <vlc_plugin.h>
35 #include <vlc_interface.h>
36 #include <vlc_playlist.h>
37 #include <vlc_vout.h>
38
39 #include <commctrl.h>
40
41 #include "vout.h"
42
43 #ifdef MODULE_NAME_IS_wingapi
44     typedef struct GXDisplayProperties {
45         DWORD cxWidth;
46         DWORD cyHeight;
47         long cbxPitch;
48         long cbyPitch;
49         long cBPP;
50         DWORD ffFormat;
51     } GXDisplayProperties;
52
53     typedef struct GXScreenRect {
54         DWORD dwTop;
55         DWORD dwLeft;
56         DWORD dwWidth;
57         DWORD dwHeight;
58     } GXScreenRect;
59
60 #   define GX_FULLSCREEN    0x01
61 #   define GX_NORMALKEYS    0x02
62 #   define GX_LANDSCAPEKEYS 0x03
63
64 #   ifndef kfLandscape
65 #       define kfLandscape      0x8
66 #       define kfPalette        0x10
67 #       define kfDirect         0x20
68 #       define kfDirect555      0x40
69 #       define kfDirect565      0x80
70 #       define kfDirect888      0x100
71 #       define kfDirect444      0x200
72 #       define kfDirectInverted 0x400
73 #   endif
74 #endif /* MODULE_NAME_IS_wingapi */
75
76 #define MAX_DIRECTBUFFERS 10
77
78 #ifdef UNDER_CE
79 #ifndef WS_OVERLAPPEDWINDOW
80 #   define WS_OVERLAPPEDWINDOW 0xcf0000
81 #endif
82 #ifndef WS_EX_NOPARENTNOTIFY
83 #   define WS_EX_NOPARENTNOTIFY 4
84 #endif
85 #ifndef WS_EX_APPWINDOW
86 #define WS_EX_APPWINDOW 0x40000
87 #endif
88 #define SetWindowLongPtr SetWindowLong
89 #define GetWindowLongPtr GetWindowLong
90 #define GWLP_USERDATA GWL_USERDATA
91 #define AdjustWindowRect(a,b,c)
92 #endif //UNDER_CE
93
94 #ifndef WS_NONAVDONEBUTTON
95 #define WS_NONAVDONEBUTTON 0
96 #endif
97 /*****************************************************************************
98  * Local prototypes
99  *****************************************************************************/
100 static int  OpenVideo  ( vlc_object_t * );
101 static void CloseVideo ( vlc_object_t * );
102
103 static int  Init      ( vout_thread_t * );
104 static void End       ( vout_thread_t * );
105 static int  Manage    ( vout_thread_t * );
106 static void Render    ( vout_thread_t *, picture_t * );
107 #ifdef MODULE_NAME_IS_wingapi
108 static void FirstDisplayGAPI( vout_thread_t *, picture_t * );
109 static void DisplayGAPI( vout_thread_t *, picture_t * );
110 static int GAPILockSurface( vout_thread_t *, picture_t * );
111 static int GAPIUnlockSurface( vout_thread_t *, picture_t * );
112 #else
113 static void FirstDisplayGDI( vout_thread_t *, picture_t * );
114 static void DisplayGDI( vout_thread_t *, picture_t * );
115 #endif
116 static void SetPalette( vout_thread_t *, uint16_t *, uint16_t *, uint16_t * );
117
118 static void InitBuffers        ( vout_thread_t * );
119
120 #ifdef MODULE_NAME_IS_wingapi
121 #   define GXOpenDisplay p_vout->p_sys->GXOpenDisplay
122 #   define GXCloseDisplay p_vout->p_sys->GXCloseDisplay
123 #   define GXBeginDraw p_vout->p_sys->GXBeginDraw
124 #   define GXEndDraw p_vout->p_sys->GXEndDraw
125 #   define GXGetDisplayProperties p_vout->p_sys->GXGetDisplayProperties
126 #   define GXSuspend p_vout->p_sys->GXSuspend
127 #   define GXResume p_vout->p_sys->GXResume
128 #endif
129
130 #define DX_POSITION_CHANGE 0x1000
131
132 /*****************************************************************************
133  * Module descriptor
134  *****************************************************************************/
135 vlc_module_begin();
136     set_category( CAT_VIDEO );
137     set_subcategory( SUBCAT_VIDEO_VOUT );
138 #ifdef MODULE_NAME_IS_wingapi
139     set_shortname( "Windows GAPI" );
140     set_description( N_("Windows GAPI video output") );
141     set_capability( "video output", 20 );
142 #else
143     set_shortname( "Windows GDI" );
144     set_description( N_("Windows GDI video output") );
145     set_capability( "video output", 10 );
146 #endif
147     set_callbacks( OpenVideo, CloseVideo );
148 vlc_module_end();
149
150 /*****************************************************************************
151  * OpenVideo: activate GDI video thread output method
152  *****************************************************************************/
153 static int OpenVideo ( vlc_object_t *p_this )
154 {
155     vout_thread_t * p_vout = (vout_thread_t *)p_this;
156     vlc_value_t val;
157
158     p_vout->p_sys = (vout_sys_t *)malloc( sizeof(vout_sys_t) );
159     if( !p_vout->p_sys ) return VLC_ENOMEM;
160     memset( p_vout->p_sys, 0, sizeof( vout_sys_t ) );
161
162 #ifdef MODULE_NAME_IS_wingapi
163     /* Load GAPI */
164     p_vout->p_sys->gapi_dll = LoadLibrary( _T("GX.DLL") );
165     if( p_vout->p_sys->gapi_dll == NULL )
166     {
167         msg_Warn( p_vout, "failed loading gx.dll" );
168         free( p_vout->p_sys );
169         return VLC_EGENERIC;
170     }
171
172     GXOpenDisplay = (void *)GetProcAddress( p_vout->p_sys->gapi_dll,
173         _T("?GXOpenDisplay@@YAHPAUHWND__@@K@Z") );
174     GXCloseDisplay = (void *)GetProcAddress( p_vout->p_sys->gapi_dll,
175         _T("?GXCloseDisplay@@YAHXZ") );
176     GXBeginDraw = (void *)GetProcAddress( p_vout->p_sys->gapi_dll,
177         _T("?GXBeginDraw@@YAPAXXZ") );
178     GXEndDraw = (void *)GetProcAddress( p_vout->p_sys->gapi_dll,
179         _T("?GXEndDraw@@YAHXZ") );
180     GXGetDisplayProperties = (void *)GetProcAddress( p_vout->p_sys->gapi_dll,
181         _T("?GXGetDisplayProperties@@YA?AUGXDisplayProperties@@XZ") );
182     GXSuspend = (void *)GetProcAddress( p_vout->p_sys->gapi_dll,
183         _T("?GXSuspend@@YAHXZ") );
184     GXResume = GetProcAddress( p_vout->p_sys->gapi_dll,
185         _T("?GXResume@@YAHXZ") );
186
187     if( !GXOpenDisplay || !GXCloseDisplay || !GXBeginDraw || !GXEndDraw ||
188         !GXGetDisplayProperties || !GXSuspend || !GXResume )
189     {
190         msg_Err( p_vout, "failed GetProcAddress on gapi.dll" );
191         free( p_vout->p_sys );
192         return VLC_EGENERIC;
193     }
194
195     msg_Dbg( p_vout, "GAPI DLL loaded" );
196
197     p_vout->p_sys->render_width = p_vout->render.i_width;
198     p_vout->p_sys->render_height = p_vout->render.i_height;
199 #endif
200
201     p_vout->p_sys->p_event = (vlc_object_t *)
202         vlc_object_create( p_vout, sizeof( vlc_object_t ) );
203     if( !p_vout->p_sys->p_event )
204     {
205         free( p_vout->p_sys );
206         return VLC_ENOMEM;
207     }
208
209     p_vout->pf_init = Init;
210     p_vout->pf_end = End;
211     p_vout->pf_manage = Manage;
212     p_vout->pf_render = Render;
213 #ifdef MODULE_NAME_IS_wingapi
214     p_vout->pf_display = FirstDisplayGAPI;
215
216     p_vout->p_sys->b_focus = 0;
217     p_vout->p_sys->b_parent_focus = 0;
218
219 #else
220     p_vout->pf_display = FirstDisplayGDI;
221 #endif
222
223     p_vout->p_sys->hwnd = p_vout->p_sys->hvideownd = NULL;
224     p_vout->p_sys->hparent = p_vout->p_sys->hfswnd = NULL;
225     p_vout->p_sys->i_changes = 0;
226     vlc_mutex_init( &p_vout->p_sys->lock );
227     SetRectEmpty( &p_vout->p_sys->rect_display );
228     SetRectEmpty( &p_vout->p_sys->rect_parent );
229
230     var_Create( p_vout, "video-title", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
231     var_Create( p_vout, "disable-screensaver", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
232
233     p_vout->p_sys->b_cursor_hidden = 0;
234     p_vout->p_sys->i_lastmoved = mdate();
235     p_vout->p_sys->i_mouse_hide_timeout =
236         var_GetInteger(p_vout, "mouse-hide-timeout") * 1000;
237
238     /* Set main window's size */
239     p_vout->p_sys->i_window_width = p_vout->i_window_width;
240     p_vout->p_sys->i_window_height = p_vout->i_window_height;
241
242     /* Create the EventThread, this thread is created by us to isolate
243      * the Win32 PeekMessage function calls. We want to do this because
244      * Windows can stay blocked inside this call for a long time, and when
245      * this happens it thus blocks vlc's video_output thread.
246      * Vout EventThread will take care of the creation of the video
247      * window (because PeekMessage has to be called from the same thread which
248      * created the window). */
249     msg_Dbg( p_vout, "creating Vout EventThread" );
250     p_vout->p_sys->p_event =
251         vlc_object_create( p_vout, sizeof(event_thread_t) );
252     p_vout->p_sys->p_event->p_vout = p_vout;
253     if( vlc_thread_create( p_vout->p_sys->p_event, "VLC Vout Events Thread",
254                            EventThread, 0, 1 ) )
255     {
256         msg_Err( p_vout, "cannot create Vout EventThread" );
257         vlc_object_release( p_vout->p_sys->p_event );
258         p_vout->p_sys->p_event = NULL;
259         goto error;
260     }
261
262     if( p_vout->p_sys->p_event->b_error )
263     {
264         msg_Err( p_vout, "Vout EventThread failed" );
265         goto error;
266     }
267
268     vlc_object_attach( p_vout->p_sys->p_event, p_vout );
269
270     msg_Dbg( p_vout, "Vout EventThread running" );
271
272 #ifndef UNDER_CE
273     /* Variable to indicate if the window should be on top of others */
274     /* Trigger a callback right now */
275     var_Get( p_vout, "video-on-top", &val );
276     var_Set( p_vout, "video-on-top", val );
277
278     /* disable screensaver by temporarily changing system settings */
279     p_vout->p_sys->i_spi_lowpowertimeout = 0;
280     p_vout->p_sys->i_spi_powerofftimeout = 0;
281     p_vout->p_sys->i_spi_screensavetimeout = 0;
282     var_Get( p_vout, "disable-screensaver", &val);
283     if( val.b_bool ) {
284         msg_Dbg(p_vout, "disabling screen saver");
285         SystemParametersInfo(SPI_GETLOWPOWERTIMEOUT,
286             0, &(p_vout->p_sys->i_spi_lowpowertimeout), 0);
287         if( 0 != p_vout->p_sys->i_spi_lowpowertimeout ) {
288             SystemParametersInfo(SPI_SETLOWPOWERTIMEOUT, 0, NULL, 0);
289         }
290         SystemParametersInfo(SPI_GETPOWEROFFTIMEOUT, 0,
291             &(p_vout->p_sys->i_spi_powerofftimeout), 0);
292         if( 0 != p_vout->p_sys->i_spi_powerofftimeout ) {
293             SystemParametersInfo(SPI_SETPOWEROFFTIMEOUT, 0, NULL, 0);
294         }
295         SystemParametersInfo(SPI_GETSCREENSAVETIMEOUT, 0,
296             &(p_vout->p_sys->i_spi_screensavetimeout), 0);
297         if( 0 != p_vout->p_sys->i_spi_screensavetimeout ) {
298             SystemParametersInfo(SPI_SETSCREENSAVETIMEOUT, 0, NULL, 0);
299         }
300     }
301 #endif
302     return VLC_SUCCESS;
303
304 error:
305     CloseVideo( VLC_OBJECT(p_vout) );
306     return VLC_EGENERIC;
307 }
308
309 /*****************************************************************************
310  * CloseVideo: deactivate the GDI video output
311  *****************************************************************************/
312 static void CloseVideo ( vlc_object_t *p_this )
313 {
314     vout_thread_t * p_vout = (vout_thread_t *)p_this;
315
316     if( p_vout->p_sys->p_event )
317     {
318         vlc_object_detach( p_vout->p_sys->p_event );
319
320         /* Kill Vout EventThread */
321         vlc_object_kill( p_vout->p_sys->p_event );
322
323         /* we need to be sure Vout EventThread won't stay stuck in
324          * GetMessage, so we send a fake message */
325         if( p_vout->p_sys->hwnd )
326         {
327             PostMessage( p_vout->p_sys->hwnd, WM_NULL, 0, 0);
328         }
329
330         vlc_thread_join( p_vout->p_sys->p_event );
331         vlc_object_release( p_vout->p_sys->p_event );
332     }
333     vlc_mutex_destroy( &p_vout->p_sys->lock );
334
335 #ifndef UNDER_CE
336     /* restore screensaver system settings */
337     if( 0 != p_vout->p_sys->i_spi_lowpowertimeout ) {
338         SystemParametersInfo(SPI_SETLOWPOWERTIMEOUT,
339             p_vout->p_sys->i_spi_lowpowertimeout, NULL, 0);
340     }
341     if( 0 != p_vout->p_sys->i_spi_powerofftimeout ) {
342         SystemParametersInfo(SPI_SETPOWEROFFTIMEOUT,
343             p_vout->p_sys->i_spi_powerofftimeout, NULL, 0);
344     }
345     if( 0 != p_vout->p_sys->i_spi_screensavetimeout ) {
346         SystemParametersInfo(SPI_SETSCREENSAVETIMEOUT,
347             p_vout->p_sys->i_spi_screensavetimeout, NULL, 0);
348     }
349 #endif
350
351 #ifdef MODULE_NAME_IS_wingapi
352     FreeLibrary( p_vout->p_sys->gapi_dll );
353 #endif
354
355     if( p_vout->p_sys )
356     {
357         free( p_vout->p_sys );
358         p_vout->p_sys = NULL;
359     }
360 }
361
362 /*****************************************************************************
363  * Init: initialize video thread output method
364  *****************************************************************************/
365 static int Init( vout_thread_t *p_vout )
366 {
367     picture_t *p_pic;
368
369     /* Initialize offscreen buffer */
370     InitBuffers( p_vout );
371
372     p_vout->p_sys->rect_display.left = 0;
373     p_vout->p_sys->rect_display.top = 0;
374     p_vout->p_sys->rect_display.right  = GetSystemMetrics(SM_CXSCREEN);
375     p_vout->p_sys->rect_display.bottom = GetSystemMetrics(SM_CYSCREEN);
376
377     I_OUTPUTPICTURES = 0;
378
379     /* Initialize the output structure */
380     switch( p_vout->p_sys->i_depth )
381     {
382     case 8:
383         p_vout->output.i_chroma = VLC_FOURCC('R','G','B','2');
384         p_vout->output.pf_setpalette = SetPalette;
385         break;
386     case 15:
387         p_vout->output.i_chroma = VLC_FOURCC('R','V','1','5');
388         p_vout->output.i_rmask  = 0x7c00;
389         p_vout->output.i_gmask  = 0x03e0;
390         p_vout->output.i_bmask  = 0x001f;
391         break;
392     case 16:
393         p_vout->output.i_chroma = VLC_FOURCC('R','V','1','6');
394         p_vout->output.i_rmask  = 0xf800;
395         p_vout->output.i_gmask  = 0x07e0;
396         p_vout->output.i_bmask  = 0x001f;
397         break;
398     case 24:
399         p_vout->output.i_chroma = VLC_FOURCC('R','V','2','4');
400         p_vout->output.i_rmask  = 0x00ff0000;
401         p_vout->output.i_gmask  = 0x0000ff00;
402         p_vout->output.i_bmask  = 0x000000ff;
403         break;
404     case 32:
405         p_vout->output.i_chroma = VLC_FOURCC('R','V','3','2');
406         p_vout->output.i_rmask  = 0x00ff0000;
407         p_vout->output.i_gmask  = 0x0000ff00;
408         p_vout->output.i_bmask  = 0x000000ff;
409         break;
410     default:
411         msg_Err( p_vout, "screen depth %i not supported",
412                  p_vout->p_sys->i_depth );
413         return VLC_EGENERIC;
414         break;
415     }
416
417     p_pic = &p_vout->p_picture[0];
418
419 #ifdef MODULE_NAME_IS_wingapi
420     p_vout->output.i_width  = 0;
421     p_vout->output.i_height = 0;
422     p_pic->pf_lock  = GAPILockSurface;
423     p_pic->pf_unlock = GAPIUnlockSurface;
424     Manage( p_vout );
425     GAPILockSurface( p_vout, p_pic );
426     p_vout->i_changes = 0;
427     p_vout->output.i_width  = p_vout->p_sys->render_width;
428     p_vout->output.i_height = p_vout->p_sys->render_height;
429
430 #else
431     p_vout->output.i_width  = p_vout->render.i_width;
432     p_vout->output.i_height = p_vout->render.i_height;
433
434     p_vout->fmt_out = p_vout->fmt_in;
435     p_vout->fmt_out.i_chroma = p_vout->output.i_chroma;
436 #endif
437
438     p_vout->output.i_aspect = p_vout->render.i_aspect;
439
440     p_pic->p->p_pixels = p_vout->p_sys->p_pic_buffer;
441     p_pic->p->i_lines = p_vout->output.i_height;
442     p_pic->p->i_visible_lines = p_vout->output.i_height;
443     p_pic->p->i_pitch = p_vout->p_sys->i_pic_pitch;
444     p_pic->p->i_pixel_pitch = p_vout->p_sys->i_pic_pixel_pitch;
445     p_pic->p->i_visible_pitch = p_vout->output.i_width *
446         p_pic->p->i_pixel_pitch;
447     p_pic->i_planes = 1;
448     p_pic->i_status = DESTROYED_PICTURE;
449     p_pic->i_type   = DIRECT_PICTURE;
450
451     PP_OUTPUTPICTURE[ I_OUTPUTPICTURES++ ] = p_pic;
452
453     /* Change the window title bar text */
454     PostMessage( p_vout->p_sys->hwnd, WM_VLC_CHANGE_TEXT, 0, 0 );
455     UpdateRects( p_vout, true );
456
457     return VLC_SUCCESS;
458 }
459
460 /*****************************************************************************
461  * End: terminate video thread output method
462  *****************************************************************************/
463 static void End( vout_thread_t *p_vout )
464 {
465 #ifdef MODULE_NAME_IS_wingapi
466     GXCloseDisplay();
467 #else
468     DeleteDC( p_vout->p_sys->off_dc );
469     DeleteObject( p_vout->p_sys->off_bitmap );
470 #endif
471 }
472
473 /*****************************************************************************
474  * Manage: handle events
475  *****************************************************************************
476  * This function should be called regularly by video output thread. It manages
477  * console events. It returns a non null value on error.
478  *****************************************************************************/
479 static int Manage( vout_thread_t *p_vout )
480 {
481     /* If we do not control our window, we check for geometry changes
482      * ourselves because the parent might not send us its events. */
483     vlc_mutex_lock( &p_vout->p_sys->lock );
484     if( p_vout->p_sys->hparent && !p_vout->b_fullscreen )
485     {
486         RECT rect_parent;
487         POINT point;
488
489         vlc_mutex_unlock( &p_vout->p_sys->lock );
490
491         GetClientRect( p_vout->p_sys->hparent, &rect_parent );
492         point.x = point.y = 0;
493         ClientToScreen( p_vout->p_sys->hparent, &point );
494         OffsetRect( &rect_parent, point.x, point.y );
495
496         if( !EqualRect( &rect_parent, &p_vout->p_sys->rect_parent ) )
497         {
498             int i_x, i_y, i_width, i_height;
499             p_vout->p_sys->rect_parent = rect_parent;
500
501             /* This one is to force the update even if only
502              * the position has changed */
503             SetWindowPos( p_vout->p_sys->hwnd, 0, 1, 1,
504                           rect_parent.right - rect_parent.left,
505                           rect_parent.bottom - rect_parent.top, 0 );
506
507             SetWindowPos( p_vout->p_sys->hwnd, 0, 0, 0,
508                           rect_parent.right - rect_parent.left,
509                           rect_parent.bottom - rect_parent.top, 0 );
510
511             vout_PlacePicture( p_vout, rect_parent.right - rect_parent.left,
512                                rect_parent.bottom - rect_parent.top,
513                                &i_x, &i_y, &i_width, &i_height );
514
515             SetWindowPos( p_vout->p_sys->hvideownd, HWND_TOP,
516                           i_x, i_y, i_width, i_height, 0 );
517         }
518     }
519     else
520     {
521         vlc_mutex_unlock( &p_vout->p_sys->lock );
522     }
523
524     /* Check for cropping / aspect changes */
525     if( p_vout->i_changes & VOUT_CROP_CHANGE ||
526         p_vout->i_changes & VOUT_ASPECT_CHANGE )
527     {
528         p_vout->i_changes &= ~VOUT_CROP_CHANGE;
529         p_vout->i_changes &= ~VOUT_ASPECT_CHANGE;
530
531         p_vout->fmt_out.i_x_offset = p_vout->fmt_in.i_x_offset;
532         p_vout->fmt_out.i_y_offset = p_vout->fmt_in.i_y_offset;
533         p_vout->fmt_out.i_visible_width = p_vout->fmt_in.i_visible_width;
534         p_vout->fmt_out.i_visible_height = p_vout->fmt_in.i_visible_height;
535         p_vout->fmt_out.i_aspect = p_vout->fmt_in.i_aspect;
536         p_vout->fmt_out.i_sar_num = p_vout->fmt_in.i_sar_num;
537         p_vout->fmt_out.i_sar_den = p_vout->fmt_in.i_sar_den;
538         p_vout->output.i_aspect = p_vout->fmt_in.i_aspect;
539         UpdateRects( p_vout, true );
540     }
541
542     /*
543      * Position Change
544      */
545     if( p_vout->p_sys->i_changes & DX_POSITION_CHANGE )
546     {
547         p_vout->p_sys->i_changes &= ~DX_POSITION_CHANGE;
548     }
549
550     /* We used to call the Win32 PeekMessage function here to read the window
551      * messages. But since window can stay blocked into this function for a
552      * long time (for example when you move your window on the screen), I
553      * decided to isolate PeekMessage in another thread. */
554
555     /*
556      * Fullscreen change
557      */
558     if( p_vout->i_changes & VOUT_FULLSCREEN_CHANGE
559         || p_vout->p_sys->i_changes & VOUT_FULLSCREEN_CHANGE )
560     {
561         Win32ToggleFullscreen( p_vout );
562
563         p_vout->i_changes &= ~VOUT_FULLSCREEN_CHANGE;
564         p_vout->p_sys->i_changes &= ~VOUT_FULLSCREEN_CHANGE;
565     }
566
567     /*
568      * Pointer change
569      */
570     if( p_vout->b_fullscreen && !p_vout->p_sys->b_cursor_hidden &&
571         (mdate() - p_vout->p_sys->i_lastmoved) >
572             p_vout->p_sys->i_mouse_hide_timeout )
573     {
574         POINT point;
575         HWND hwnd;
576
577         /* Hide the cursor only if it is inside our window */
578         GetCursorPos( &point );
579         hwnd = WindowFromPoint(point);
580         if( hwnd == p_vout->p_sys->hwnd || hwnd == p_vout->p_sys->hvideownd )
581         {
582             PostMessage( p_vout->p_sys->hwnd, WM_VLC_HIDE_MOUSE, 0, 0 );
583         }
584         else
585         {
586             p_vout->p_sys->i_lastmoved = mdate();
587         }
588     }
589
590     /*
591      * "Always on top" status change
592      */
593     if( p_vout->p_sys->b_on_top_change )
594     {
595         vlc_value_t val;
596         HMENU hMenu = GetSystemMenu( p_vout->p_sys->hwnd, FALSE );
597
598         var_Get( p_vout, "video-on-top", &val );
599
600         /* Set the window on top if necessary */
601         if( val.b_bool && !( GetWindowLong( p_vout->p_sys->hwnd, GWL_EXSTYLE )
602                            & WS_EX_TOPMOST ) )
603         {
604             CheckMenuItem( hMenu, IDM_TOGGLE_ON_TOP,
605                            MF_BYCOMMAND | MFS_CHECKED );
606             SetWindowPos( p_vout->p_sys->hwnd, HWND_TOPMOST, 0, 0, 0, 0,
607                           SWP_NOSIZE | SWP_NOMOVE );
608         }
609         else
610         /* The window shouldn't be on top */
611         if( !val.b_bool && ( GetWindowLong( p_vout->p_sys->hwnd, GWL_EXSTYLE )
612                            & WS_EX_TOPMOST ) )
613         {
614             CheckMenuItem( hMenu, IDM_TOGGLE_ON_TOP,
615                            MF_BYCOMMAND | MFS_UNCHECKED );
616             SetWindowPos( p_vout->p_sys->hwnd, HWND_NOTOPMOST, 0, 0, 0, 0,
617                           SWP_NOSIZE | SWP_NOMOVE );
618         }
619
620         p_vout->p_sys->b_on_top_change = false;
621     }
622
623     /* Check if the event thread is still running */
624     if( !vlc_object_alive (p_vout->p_sys->p_event) )
625     {
626         return VLC_EGENERIC; /* exit */
627     }
628
629     return VLC_SUCCESS;
630 }
631
632 /*****************************************************************************
633  * Render: render previously calculated output
634  *****************************************************************************/
635 static void Render( vout_thread_t *p_vout, picture_t *p_pic )
636 {
637     /* No need to do anything, the fake direct buffers stay as they are */
638 }
639
640 /*****************************************************************************
641  * Display: displays previously rendered output
642  *****************************************************************************/
643 #define rect_src p_vout->p_sys->rect_src
644 #define rect_src_clipped p_vout->p_sys->rect_src_clipped
645 #define rect_dest p_vout->p_sys->rect_dest
646 #define rect_dest_clipped p_vout->p_sys->rect_dest_clipped
647
648 #ifndef MODULE_NAME_IS_wingapi
649 static void DisplayGDI( vout_thread_t *p_vout, picture_t *p_pic )
650 {
651     vout_sys_t *p_sys = p_vout->p_sys;
652     RECT rect_dst = rect_dest_clipped;
653     HDC hdc = GetDC( p_sys->hvideownd );
654
655     OffsetRect( &rect_dst, -rect_dest.left, -rect_dest.top );
656     SelectObject( p_sys->off_dc, p_sys->off_bitmap );
657
658     if( rect_dest_clipped.right - rect_dest_clipped.left !=
659         rect_src_clipped.right - rect_src_clipped.left ||
660         rect_dest_clipped.bottom - rect_dest_clipped.top !=
661         rect_src_clipped.bottom - rect_src_clipped.top )
662     {
663         StretchBlt( hdc, rect_dst.left, rect_dst.top,
664                     rect_dst.right, rect_dst.bottom,
665                     p_sys->off_dc, rect_src_clipped.left, rect_src_clipped.top,
666                     rect_src_clipped.right, rect_src_clipped.bottom, SRCCOPY );
667     }
668     else
669     {
670         BitBlt( hdc, rect_dst.left, rect_dst.top,
671                 rect_dst.right, rect_dst.bottom,
672                 p_sys->off_dc, rect_src_clipped.left,
673                 rect_src_clipped.top, SRCCOPY );
674     }
675
676     ReleaseDC( p_sys->hvideownd, hdc );
677 }
678
679 static void FirstDisplayGDI( vout_thread_t *p_vout, picture_t *p_pic )
680 {
681     /*
682     ** Video window is initially hidden, show it now since we got a
683     ** picture to show.
684     */
685     SetWindowPos( p_vout->p_sys->hvideownd, 0, 0, 0, 0, 0,
686         SWP_ASYNCWINDOWPOS|
687         SWP_FRAMECHANGED|
688         SWP_SHOWWINDOW|
689         SWP_NOMOVE|
690         SWP_NOSIZE|
691         SWP_NOZORDER );
692
693     /* get initial picture presented */
694     DisplayGDI(p_vout, p_pic);
695
696     /* use and restores proper display function for further pictures */
697     p_vout->pf_display = DisplayGDI;
698 }
699
700 #else
701
702 static int GAPILockSurface( vout_thread_t *p_vout, picture_t *p_pic )
703 {
704     vout_sys_t *p_sys = p_vout->p_sys;
705     int i_x, i_y, i_width, i_height;
706     RECT video_rect;
707     POINT point;
708
709     GetClientRect( p_sys->hwnd, &video_rect);
710     vout_PlacePicture( p_vout, video_rect.right - video_rect.left,
711                        video_rect.bottom - video_rect.top,
712                        &i_x, &i_y, &i_width, &i_height );
713     point.x = point.y = 0;
714     ClientToScreen( p_sys->hwnd, &point );
715     i_x += point.x + video_rect.left;
716     i_y += point.y + video_rect.top;
717
718     if( i_width != p_vout->output.i_width ||
719         i_height != p_vout->output.i_height )
720     {
721         GXDisplayProperties gxdisplayprop = GXGetDisplayProperties();
722
723         p_sys->render_width = i_width;
724         p_sys->render_height = i_height;
725         p_vout->i_changes |= VOUT_SIZE_CHANGE;
726
727         msg_Dbg( p_vout, "vout size change (%ix%i -> %ix%i)",
728                  i_width, i_height, p_vout->output.i_width,
729                  p_vout->output.i_height );
730
731         p_vout->p_sys->i_pic_pixel_pitch = gxdisplayprop.cbxPitch;
732         p_vout->p_sys->i_pic_pitch = gxdisplayprop.cbyPitch;
733         return VLC_EGENERIC;
734     }
735     else
736     {
737         GXDisplayProperties gxdisplayprop;
738         RECT display_rect, dest_rect;
739         uint8_t *p_dest, *p_src = p_pic->p->p_pixels;
740
741         video_rect.left = i_x; video_rect.top = i_y;
742         video_rect.right = i_x + i_width;
743         video_rect.bottom = i_y + i_height;
744
745         gxdisplayprop = GXGetDisplayProperties();
746         display_rect.left = 0; display_rect.top = 0;
747         display_rect.right = gxdisplayprop.cxWidth;
748         display_rect.bottom = gxdisplayprop.cyHeight;
749
750         if( !IntersectRect( &dest_rect, &video_rect, &display_rect ) )
751         {
752             return VLC_EGENERIC;
753         }
754
755 #if 0
756         msg_Err( p_vout, "video (%d,%d,%d,%d) display (%d,%d,%d,%d) "
757                  "dest (%d,%d,%d,%d)",
758                  video_rect.left, video_rect.right,
759                  video_rect.top, video_rect.bottom,
760                  display_rect.left, display_rect.right,
761                  display_rect.top, display_rect.bottom,
762                  dest_rect.left, dest_rect.right,
763                  dest_rect.top, dest_rect.bottom );
764 #endif
765
766         if( !(p_dest = GXBeginDraw()) )
767         {
768 #if 0
769             msg_Err( p_vout, "GXBeginDraw error %d ", GetLastError() );
770 #endif
771             return VLC_EGENERIC;
772         }
773
774         p_src += (dest_rect.left - video_rect.left) * gxdisplayprop.cbxPitch +
775             (dest_rect.top - video_rect.top) * p_pic->p->i_pitch;
776         p_dest += dest_rect.left * gxdisplayprop.cbxPitch +
777             dest_rect.top * gxdisplayprop.cbyPitch;
778         i_width = dest_rect.right - dest_rect.left;
779         i_height = dest_rect.bottom - dest_rect.top;
780
781         p_pic->p->p_pixels = p_dest;
782     }
783
784     return VLC_SUCCESS;
785 }
786
787 static int GAPIUnlockSurface( vout_thread_t *p_vout, picture_t *p_pic )
788 {
789     GXEndDraw();
790     return VLC_SUCCESS;
791 }
792
793 static void DisplayGAPI( vout_thread_t *p_vout, picture_t *p_pic )
794 {
795 }
796
797 static void FirstDisplayGAPI( vout_thread_t *p_vout, picture_t *p_pic )
798 {
799     /* get initial picture presented through D3D */
800     DisplayGAPI(p_vout, p_pic);
801
802     /*
803     ** Video window is initially hidden, show it now since we got a
804     ** picture to show.
805     */
806     SetWindowPos( p_vout->p_sys->hvideownd, 0, 0, 0, 0, 0,
807         SWP_ASYNCWINDOWPOS|
808         SWP_FRAMECHANGED|
809         SWP_SHOWWINDOW|
810         SWP_NOMOVE|
811         SWP_NOSIZE|
812         SWP_NOZORDER );
813
814     /* use and restores proper display function for further pictures */
815     p_vout->pf_display = DisplayGAPI;
816 }
817
818 #endif
819
820 #undef rect_src
821 #undef rect_src_clipped
822 #undef rect_dest
823 #undef rect_dest_clipped
824 /*****************************************************************************
825  * SetPalette: sets an 8 bpp palette
826  *****************************************************************************/
827 static void SetPalette( vout_thread_t *p_vout,
828                         uint16_t *red, uint16_t *green, uint16_t *blue )
829 {
830     msg_Err( p_vout, "FIXME: SetPalette unimplemented" );
831 }
832
833 /*****************************************************************************
834  * InitBuffers: initialize an offscreen bitmap for direct buffer operations.
835  *****************************************************************************/
836 static void InitBuffers( vout_thread_t *p_vout )
837 {
838     BITMAPINFOHEADER *p_header = &p_vout->p_sys->bitmapinfo.bmiHeader;
839     BITMAPINFO *p_info = &p_vout->p_sys->bitmapinfo;
840     HDC window_dc = GetDC( p_vout->p_sys->hvideownd );
841
842     /* Get screen properties */
843 #ifdef MODULE_NAME_IS_wingapi
844     GXDisplayProperties gx_displayprop = GXGetDisplayProperties();
845     p_vout->p_sys->i_depth = gx_displayprop.cBPP;
846 #else
847     p_vout->p_sys->i_depth = GetDeviceCaps( window_dc, PLANES ) *
848         GetDeviceCaps( window_dc, BITSPIXEL );
849 #endif
850     msg_Dbg( p_vout, "GDI depth is %i", p_vout->p_sys->i_depth );
851
852 #ifdef MODULE_NAME_IS_wingapi
853     GXOpenDisplay( p_vout->p_sys->hvideownd, GX_FULLSCREEN );
854
855 #else
856
857     /* Initialize offscreen bitmap */
858     memset( p_info, 0, sizeof( BITMAPINFO ) + 3 * sizeof( RGBQUAD ) );
859
860     p_header->biSize = sizeof( BITMAPINFOHEADER );
861     p_header->biSizeImage = 0;
862     p_header->biPlanes = 1;
863     switch( p_vout->p_sys->i_depth )
864     {
865     case 8:
866         p_header->biBitCount = 8;
867         p_header->biCompression = BI_RGB;
868         /* FIXME: we need a palette here */
869         break;
870     case 15:
871         p_header->biBitCount = 15;
872         p_header->biCompression = BI_BITFIELDS;//BI_RGB;
873         ((DWORD*)p_info->bmiColors)[0] = 0x00007c00;
874         ((DWORD*)p_info->bmiColors)[1] = 0x000003e0;
875         ((DWORD*)p_info->bmiColors)[2] = 0x0000001f;
876         break;
877     case 16:
878         p_header->biBitCount = 16;
879         p_header->biCompression = BI_BITFIELDS;//BI_RGB;
880         ((DWORD*)p_info->bmiColors)[0] = 0x0000f800;
881         ((DWORD*)p_info->bmiColors)[1] = 0x000007e0;
882         ((DWORD*)p_info->bmiColors)[2] = 0x0000001f;
883         break;
884     case 24:
885         p_header->biBitCount = 24;
886         p_header->biCompression = BI_RGB;
887         ((DWORD*)p_info->bmiColors)[0] = 0x00ff0000;
888         ((DWORD*)p_info->bmiColors)[1] = 0x0000ff00;
889         ((DWORD*)p_info->bmiColors)[2] = 0x000000ff;
890         break;
891     case 32:
892         p_header->biBitCount = 32;
893         p_header->biCompression = BI_RGB;
894         ((DWORD*)p_info->bmiColors)[0] = 0x00ff0000;
895         ((DWORD*)p_info->bmiColors)[1] = 0x0000ff00;
896         ((DWORD*)p_info->bmiColors)[2] = 0x000000ff;
897         break;
898     default:
899         msg_Err( p_vout, "screen depth %i not supported",
900                  p_vout->p_sys->i_depth );
901         return;
902         break;
903     }
904     p_header->biWidth = p_vout->render.i_width;
905     p_header->biHeight = -p_vout->render.i_height;
906     p_header->biClrImportant = 0;
907     p_header->biClrUsed = 0;
908     p_header->biXPelsPerMeter = 0;
909     p_header->biYPelsPerMeter = 0;
910
911     p_vout->p_sys->i_pic_pixel_pitch = p_header->biBitCount / 8;
912     p_vout->p_sys->i_pic_pitch = p_header->biBitCount * p_header->biWidth / 8;
913
914     p_vout->p_sys->off_bitmap =
915         CreateDIBSection( window_dc, (BITMAPINFO *)p_header, DIB_RGB_COLORS,
916                           (void**)&p_vout->p_sys->p_pic_buffer, NULL, 0 );
917
918     p_vout->p_sys->off_dc = CreateCompatibleDC( window_dc );
919
920     SelectObject( p_vout->p_sys->off_dc, p_vout->p_sys->off_bitmap );
921     ReleaseDC( p_vout->p_sys->hvideownd, window_dc );
922 #endif
923 }
924