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