]> git.sesse.net Git - vlc/blob - modules/video_output/omapfb.c
OMAP framebuffer: let vout window handle fullscreen mode
[vlc] / modules / video_output / omapfb.c
1 /*****************************************************************************
2 * omapfb.c : omap framebuffer plugin for vlc
3 *****************************************************************************
4 * Copyright (C) 2008-2009 the VideoLAN team
5 * $Id$
6 *
7 * Authors: Antoine Lejeune <phytos @ videolan.org>
8 *          Based on fb.c and work of Siarhei Siamashka on mplayer for Maemo
9 *          Needs a recent omapfb.h to compile
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
24 *****************************************************************************/
25
26 #ifdef HAVE_CONFIG_H
27 # include "config.h"
28 #endif
29
30 #include <errno.h>                                                 /* ENOMEM */
31 #include <fcntl.h>                                                 /* open() */
32 #include <unistd.h>                                               /* close() */
33
34 #include <sys/ioctl.h>
35 #include <sys/mman.h>                                              /* mmap() */
36
37 #include <linux/fb.h>
38 #include <asm/arch-omap/omapfb.h>
39
40 /* Embedded window handling */
41 #include <X11/Xlib.h>
42 #include <X11/Xutil.h>
43 #include <X11/keysym.h>
44
45 #ifdef HAVE_OSSO
46 #include <libosso.h>
47 #endif
48
49 #include <vlc_common.h>
50 #include <vlc_plugin.h>
51 #include <vlc_keys.h>
52 #include <vlc_vout.h>
53 #include <vlc_vout_window.h>
54 #include <vlc_playlist.h>
55
56 /*****************************************************************************
57 * Local prototypes
58 *****************************************************************************/
59 static int  Create           ( vlc_object_t * );
60 static void Destroy          ( vlc_object_t * );
61
62 static int  Init             ( vout_thread_t * );
63 static void End              ( vout_thread_t * );
64 static int  Manage           ( vout_thread_t * );
65 static void DisplayVideo     ( vout_thread_t *, picture_t * );
66 static int  Control          ( vout_thread_t *, int, va_list );
67
68 static void FreePicture      ( vout_thread_t *, picture_t * );
69
70 static int  OpenDisplay      ( vout_thread_t * );
71 static void CloseDisplay     ( vout_thread_t * );
72 static void UpdateScreen     ( vout_thread_t *,
73                                int, int, int, int, int, int, int );
74
75 static int  InitWindow       ( vout_thread_t * );
76 static void CreateWindow     ( vout_sys_t * );
77 static void ToggleFullScreen ( vout_thread_t * );
78
79 #ifdef HAVE_OSSO
80 static const int i_backlight_on_interval = 300;
81 #endif
82
83 /*****************************************************************************
84  * Module descriptor
85  *****************************************************************************/
86 #define FB_DEV_VAR "omapfbdev"
87
88 #define DEVICE_TEXT N_("OMAP Framebuffer device")
89 #define DEVICE_LONGTEXT N_( \
90     "OMAP Framebuffer device to use for rendering (usually /dev/fb0).")
91
92 #define CHROMA_TEXT N_("Chroma used.")
93 #define CHROMA_LONGTEXT N_( \
94     "Force use of a specific chroma for output. Default is Y420 (specific to N770/N8xx hardware)." )
95
96 #define OVERLAY_TEXT N_("Embed the overlay")
97 #define OVERLAY_LONGTEXT N_( \
98     "Embed the framebuffer overlay into a X11 window" )
99
100 vlc_module_begin();
101     set_shortname( "OMAP framebuffer" );
102     set_category( CAT_VIDEO );
103     set_subcategory( SUBCAT_VIDEO_VOUT );
104     add_file( FB_DEV_VAR, "/dev/fb0", NULL, DEVICE_TEXT, DEVICE_LONGTEXT,
105               false )
106     add_string( "omap-chroma", NULL, NULL, CHROMA_TEXT, CHROMA_LONGTEXT,
107                 true )
108     add_bool( "omap-embedded", true, NULL, OVERLAY_TEXT, OVERLAY_LONGTEXT,
109                  true )
110     set_description( N_("OMAP framebuffer video output") );
111     set_capability( "video output", 200 );
112     set_callbacks( Create, Destroy );
113 vlc_module_end();
114
115 /*****************************************************************************
116  * omap_window_t: simple structure with the geometry of a window
117  *****************************************************************************/
118 struct omap_window_t
119 {
120     uint32_t i_x;
121     uint32_t i_y;
122     uint32_t i_width;
123     uint32_t i_height;
124 };
125
126 /*****************************************************************************
127 * vout_sys_t: video output omap framebuffer method descriptor
128  *****************************************************************************
129  * This structure is part of the video output thread descriptor.
130  * It describes the FB specific properties of an output thread.
131  *****************************************************************************/
132 struct vout_sys_t
133 {
134     /* Framebuffer information */
135     int                         i_fd;                       /* device handle */
136     struct fb_var_screeninfo    fb_vinfo;        /* current mode information */
137     struct fb_fix_screeninfo    fb_finfo;
138     struct omapfb_caps          caps;
139     bool                        b_tearsync;
140
141     /* Window information */
142     struct omap_window_t output_window;    /* Size of the real output window */
143     struct omap_window_t main_window;  /* Size of the area we got to display */
144     struct omap_window_t embedded_window;     /* Size of the embedded window */
145
146     /* Video information */
147     uint32_t             i_video_width;                       /* video width */
148     uint32_t             i_video_height;                     /* video height */
149     vlc_fourcc_t         i_chroma;
150     int                  i_color_format;                   /* OMAPFB_COLOR_* */
151     bool                 b_video_enabled;        /* Video must be displayed? */
152     picture_t           *p_output_picture;
153
154     /* Video memory */
155     uint8_t    *p_video;                                      /* base adress */
156     uint8_t    *p_center;                                   /* output adress */
157     size_t      i_page_size;                                    /* page size */
158     int         i_bytes_per_pixel;                /* Bytes used by one pixel */
159     int         i_line_len;                   /* Length of one line in bytes */
160
161     /* X11 */
162     Display   *p_display;
163     vout_window_t *owner_window;
164     Window     window;
165     mtime_t    i_time_button_last_pressed;         /* To detect double click */
166
167     /* Dummy memory */
168     int        i_null_fd;
169     uint8_t   *p_null;
170
171 #ifdef HAVE_OSSO
172     osso_context_t      *p_octx;
173     int                 i_backlight_on_counter;
174 #endif
175 };
176
177 /*****************************************************************************
178  * Create: allocates omapfb video thread output method
179  *****************************************************************************
180  * This function allocates and initializes a FB vout method.
181  *****************************************************************************/
182 static int Create( vlc_object_t *p_this )
183 {
184     vout_thread_t *p_vout = (vout_thread_t *)p_this;
185     vout_sys_t    *p_sys;
186
187     if( p_vout->fmt_in.i_chroma != VLC_CODEC_I420 &&
188         p_vout->fmt_in.i_chroma != VLC_CODEC_YV12 )
189         return VLC_EGENERIC;
190
191     /* Allocate instance and initialize some members */
192     p_vout->p_sys = p_sys = calloc( 1, sizeof( vout_sys_t ) );
193     if( p_vout->p_sys == NULL )
194         return VLC_ENOMEM;
195
196     p_vout->pf_init = Init;
197     p_vout->pf_end = End;
198     p_vout->pf_manage = Manage;
199     p_vout->pf_render = NULL;
200     p_vout->pf_display = DisplayVideo;
201     p_vout->pf_control = Control;
202     p_sys->b_video_enabled = true;
203
204     if( OpenDisplay( p_vout ) )
205     {
206         free( p_vout->p_sys );
207         return VLC_EGENERIC;
208     }
209
210     if( InitWindow( p_vout ) )
211     {
212         free( p_vout->p_sys );
213         return VLC_EGENERIC;
214     }
215
216 #ifdef HAVE_OSSO
217     p_vout->p_sys->i_backlight_on_counter = i_backlight_on_interval;
218     p_vout->p_sys->p_octx = osso_initialize( "vlc", VERSION, 0, NULL );
219     if ( p_vout->p_sys->p_octx == NULL ) {
220         msg_Err( p_vout, "Could not get osso context" );
221     } else {
222         msg_Dbg( p_vout, "Initialized osso context" );
223     }
224 #endif
225
226     return VLC_SUCCESS;
227 }
228
229 /*****************************************************************************
230  * Destroy: destroy omapfb video thread output method
231  *****************************************************************************
232  * Terminate an output method created by Create
233  *****************************************************************************/
234 static void Destroy( vlc_object_t *p_this )
235 {
236     vout_thread_t *p_vout = (vout_thread_t *)p_this;
237
238     CloseDisplay( p_vout );
239
240     if( p_vout->p_sys->owner_window )
241     {
242         vout_window_Delete( p_vout->p_sys->owner_window );
243         XCloseDisplay( p_vout->p_sys->p_display );
244     }
245
246 #ifdef HAVE_OSSO
247     if ( p_vout->p_sys->p_octx != NULL ) {
248         msg_Dbg( p_vout, "Deinitializing osso context" );
249         osso_deinitialize( p_vout->p_sys->p_octx );
250     }
251 #endif
252
253     /* Destroy structure */
254     free( p_vout->p_sys );
255 }
256
257
258 /*****************************************************************************
259  * Init: initialize omap framebuffer video thread output method
260  *****************************************************************************/
261 static int Init( vout_thread_t *p_vout )
262 {
263     vout_sys_t *p_sys = (vout_sys_t *)p_vout->p_sys;
264
265     // We want to keep the same aspect
266     p_vout->fmt_out.i_aspect = p_vout->output.i_aspect = p_vout->render.i_aspect;
267     // We ask where the video should be displayed in the video area
268     vout_PlacePicture( p_vout, p_sys->main_window.i_width,
269                        p_sys->main_window.i_height,
270                        &p_sys->output_window.i_x,
271                        &p_sys->output_window.i_y,
272                        &p_sys->output_window.i_width,
273                        &p_sys->output_window.i_height );
274     p_sys->output_window.i_x = ( p_sys->output_window.i_x +
275                                  p_sys->main_window.i_x ) & ~1;
276     p_sys->output_window.i_y = ( p_sys->output_window.i_y +
277                                  p_sys->main_window.i_y ) & ~1;
278
279     // Hardware upscaling better than software
280     if( p_vout->fmt_render.i_width <= p_sys->main_window.i_width &&
281         p_vout->fmt_render.i_height <= p_sys->main_window.i_height )
282     {
283         p_sys->i_video_width =
284         p_vout->output.i_width =
285         p_vout->fmt_out.i_width =
286         p_vout->fmt_out.i_visible_width = p_vout->fmt_render.i_width;
287         p_sys->i_video_height =
288         p_vout->output.i_height =
289         p_vout->fmt_out.i_height =
290         p_vout->fmt_out.i_visible_height = p_vout->fmt_render.i_height;
291     }
292     else
293     {
294         p_sys->i_video_width =
295         p_vout->output.i_width =
296         p_vout->fmt_out.i_width =
297         p_vout->fmt_out.i_visible_width = p_sys->output_window.i_width;
298         p_sys->i_video_height =
299         p_vout->output.i_height =
300         p_vout->fmt_out.i_height =
301         p_vout->fmt_out.i_visible_height = p_sys->output_window.i_height;
302     }
303
304     p_vout->output.i_chroma =
305     p_vout->fmt_out.i_chroma = VLC_CODEC_I420;
306     p_sys->i_color_format = OMAPFB_COLOR_YUV420;
307
308     // place in the framebuffer where we have to write
309     p_sys->p_center = p_sys->p_video + p_sys->output_window.i_x*p_sys->i_bytes_per_pixel
310                       + p_sys->output_window.i_y*p_sys->i_line_len;
311
312     // We get and set a direct render vlc picture
313     I_OUTPUTPICTURES = 0;
314     picture_t *p_pic = NULL;
315     int i_index;
316
317     for( i_index = 0 ; i_index < VOUT_MAX_PICTURES ; i_index++ )
318     {
319         if( p_vout->p_picture[ i_index ].i_status == FREE_PICTURE )
320         {
321             p_pic = p_vout->p_picture + i_index;
322             break;
323         }
324     }
325
326     /* Allocate the picture */
327     if( p_pic == NULL )
328     {
329         return VLC_EGENERIC;
330     }
331
332     p_sys->p_output_picture = p_pic;
333     p_pic->p->p_pixels = p_vout->p_sys->p_center;
334     p_pic->p->i_pixel_pitch = p_vout->p_sys->i_bytes_per_pixel;
335     p_pic->p->i_lines = p_sys->i_video_height;
336     p_pic->p->i_visible_lines = p_sys->i_video_height;
337     p_pic->p->i_pitch = p_sys->i_line_len;
338     p_pic->p->i_visible_pitch = p_sys->i_line_len;
339     p_pic->i_planes = 1;
340     p_pic->i_status = DESTROYED_PICTURE;
341     p_pic->i_type   = DIRECT_PICTURE;
342
343     PP_OUTPUTPICTURE[ I_OUTPUTPICTURES ] = p_pic;
344
345     I_OUTPUTPICTURES++;
346
347     return VLC_SUCCESS;
348 }
349
350 /*****************************************************************************
351  * End: terminate omap framebuffer video thread output method
352  *****************************************************************************/
353 static void End( vout_thread_t *p_vout )
354 {
355     /* Clear the screen */
356     UpdateScreen( p_vout, 0, 0,
357                   p_vout->p_sys->fb_vinfo.xres,
358                   p_vout->p_sys->fb_vinfo.yres,
359                   p_vout->p_sys->fb_vinfo.xres,
360                   p_vout->p_sys->fb_vinfo.yres,
361                   OMAPFB_COLOR_RGB565 );
362 }
363
364 /*****************************************************************************
365  * Control: control facility for the vout
366  *****************************************************************************/
367 static int Control( vout_thread_t *p_vout, int i_query, va_list args )
368 {
369     return VLC_EGENERIC;
370 }
371
372 /*****************************************************************************
373 * FreePicture: Destroy the picture and make it free again
374 ******************************************************************************/
375 static void FreePicture( vout_thread_t *p_vout, picture_t *p_pic )
376 {
377     p_pic->p->p_pixels = NULL;
378     p_pic->i_status = FREE_PICTURE;
379 }
380
381 /*****************************************************************************
382  * Manage: handle omapfb events
383  *****************************************************************************
384  * This function should be called regularly by video output thread.
385  *****************************************************************************/
386 static int Manage( vout_thread_t *p_vout )
387 {
388     XEvent xevent;
389
390     while( XPending( p_vout->p_sys->p_display ) )
391     {
392         XNextEvent( p_vout->p_sys->p_display, &xevent );
393
394         if( xevent.type == ButtonPress &&
395             ((XButtonEvent *)&xevent)->button == Button1 )
396         {
397             /* detect double-clicks */
398             if( ( ((XButtonEvent *)&xevent)->time -
399                     p_vout->p_sys->i_time_button_last_pressed ) < 300 )
400             {
401                 p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
402             }
403
404             p_vout->p_sys->i_time_button_last_pressed =
405                         ((XButtonEvent *)&xevent)->time;
406         }
407         else if( xevent.type == KeyPress )
408         {
409             KeySym x_key_symbol;
410             vlc_value_t val;
411
412             x_key_symbol = XKeycodeToKeysym( p_vout->p_sys->p_display,
413                                              xevent.xkey.keycode, 0 );
414
415             switch( x_key_symbol )
416             {
417             case XK_Return:
418                 val.i_int = ACTIONID_PLAY_PAUSE; break;
419             case XK_Escape:
420                 val.i_int = ACTIONID_QUIT; break;
421             case XK_Down:
422                 val.i_int = ACTIONID_JUMP_BACKWARD_MEDIUM; break;
423             case XK_Up:
424                 val.i_int = ACTIONID_JUMP_FORWARD_MEDIUM; break;
425             case XK_Right:
426                 val.i_int = ACTIONID_JUMP_FORWARD_SHORT; break;
427             case XK_Left:
428                 val.i_int = ACTIONID_JUMP_BACKWARD_SHORT; break;
429             case XK_F6:
430                 val.i_int = ACTIONID_TOGGLE_FULLSCREEN; break;
431             case XK_F7:
432                 val.i_int = ACTIONID_VOL_UP; break;
433             case XK_F8:
434                 val.i_int = ACTIONID_VOL_DOWN; break;
435             }
436             var_SetInteger( p_vout->p_libvlc, "key-action", val.i_int );
437         }
438         else if( ( xevent.type == VisibilityNotify &&
439                  xevent.xvisibility.state == VisibilityUnobscured ) ||
440                  xevent.type == FocusIn )
441         {
442             p_vout->p_sys->b_video_enabled = true;
443             p_vout->p_sys->p_output_picture->p->p_pixels =
444                 p_vout->p_sys->p_center;
445             XSetInputFocus( p_vout->p_sys->p_display, p_vout->p_sys->window,
446                             RevertToParent, CurrentTime );
447         }
448         else if( ( xevent.type == VisibilityNotify &&
449                  xevent.xvisibility.state != VisibilityUnobscured ) ||
450                  xevent.type == FocusOut || xevent.type == UnmapNotify )
451         {
452             UpdateScreen( p_vout, 0, 0,
453                           p_vout->p_sys->fb_vinfo.xres,
454                           p_vout->p_sys->fb_vinfo.yres,
455                           p_vout->p_sys->fb_vinfo.xres,
456                           p_vout->p_sys->fb_vinfo.yres,
457                           OMAPFB_COLOR_RGB565 );
458             p_vout->p_sys->b_video_enabled = false;
459             p_vout->p_sys->p_output_picture->p->p_pixels =
460                 p_vout->p_sys->p_null;
461          }
462     }
463
464     if( p_vout->i_changes & VOUT_FULLSCREEN_CHANGE )
465     {
466         /* Update the object variable and trigger callback */
467         p_vout->b_fullscreen = !p_vout->b_fullscreen;
468         var_SetBool( p_vout, "fullscreen", p_vout->b_fullscreen );
469
470         if( p_vout->p_sys->owner_window )
471             vout_window_SetFullscreen( p_vout->p_sys->owner_window,
472                                        p_vout->b_fullscreen );
473         p_vout->i_changes &= ~VOUT_FULLSCREEN_CHANGE;
474     }
475
476     if( p_vout->i_changes & VOUT_SIZE_CHANGE )
477     {
478         FreePicture( p_vout, p_vout->p_sys->p_output_picture );
479         if( Init( p_vout ) )
480         {
481             msg_Err( p_vout, "cannot reinit framebuffer screen" );
482             return VLC_EGENERIC;
483         }
484     }
485
486
487 #ifdef HAVE_OSSO
488     if ( p_vout->p_sys->p_octx != NULL ) {
489         if ( p_vout->p_sys->i_backlight_on_counter == i_backlight_on_interval ) {
490             if ( osso_display_blanking_pause( p_vout->p_sys->p_octx ) != OSSO_OK ) {
491                 msg_Err( p_vout, "Could not disable backlight blanking" );
492         } else {
493                 msg_Dbg( p_vout, "Backlight blanking disabled" );
494             }
495             p_vout->p_sys->i_backlight_on_counter = 0;
496         } else {
497             p_vout->p_sys->i_backlight_on_counter ++;
498         }
499     }
500 #endif
501
502     return VLC_SUCCESS;
503 }
504
505 /*****************************************************************************
506  * DisplayVideo: displays previously rendered output
507  *****************************************************************************
508  * This function update the screen resulting in the display of the last
509  * rendered picture.
510  *****************************************************************************/
511 static void DisplayVideo( vout_thread_t *p_vout, picture_t *p_pic )
512 {
513     VLC_UNUSED( p_pic );
514
515     if( !p_vout->p_sys->b_video_enabled )
516         return;
517
518     UpdateScreen( p_vout,
519                   p_vout->p_sys->output_window.i_x,
520                   p_vout->p_sys->output_window.i_y,
521                   p_vout->p_sys->i_video_width,
522                   p_vout->p_sys->i_video_height,
523                   p_vout->p_sys->output_window.i_width,
524                   p_vout->p_sys->output_window.i_height,
525                   p_vout->p_sys->i_color_format );
526
527     // Wait for the window to be fully displayed
528     ioctl( p_vout->p_sys->i_fd, OMAPFB_SYNC_GFX);
529 }
530
531 /*****************************************************************************
532  * OpenDisplay: initialize framebuffer
533  *****************************************************************************/
534 static int OpenDisplay( vout_thread_t *p_vout )
535 {
536     vout_sys_t *p_sys = (vout_sys_t *) p_vout->p_sys;
537     char *psz_device;                             /* framebuffer device path */
538
539     /* Open framebuffer device */
540     if( !(psz_device = var_CreateGetNonEmptyString( p_vout, FB_DEV_VAR )) )
541     {
542         msg_Err( p_vout, "don't know which fb device to open" );
543         return VLC_EGENERIC;
544     }
545
546     p_sys->i_fd = open( psz_device, O_RDWR );
547     if( p_sys->i_fd == -1 )
548     {
549         msg_Err( p_vout, "cannot open %s (%m)", psz_device );
550         free( psz_device );
551         return VLC_EGENERIC;
552     }
553     free( psz_device );
554
555     // Get caps, try older interface if needed
556     if( ioctl( p_sys->i_fd, OMAPFB_GET_CAPS, &p_sys->caps ) != 0 )
557     {
558         if( ioctl( p_sys->i_fd, OMAP_IOR( 42, unsigned long ), &p_sys->caps ) != 0 )
559         {
560             msg_Err( p_vout, "OMAPFB_GET_CAPS ioctl failed" );
561             close( p_sys->i_fd );
562             return VLC_EGENERIC;
563         }
564     }
565
566     if( ( p_sys->caps.ctrl & OMAPFB_CAPS_TEARSYNC ) != 0 )
567         p_sys->b_tearsync = true;
568
569     if( ioctl( p_sys->i_fd, FBIOGET_VSCREENINFO, &p_sys->fb_vinfo ) )
570     {
571         msg_Err( p_vout, "Can't get VSCREENINFO: %s", strerror(errno) );
572         close( p_sys->i_fd );
573         return VLC_EGENERIC;
574     }
575     if( ioctl( p_sys->i_fd, FBIOGET_FSCREENINFO, &p_sys->fb_finfo ) )
576     {
577         msg_Err( p_vout, "Can't get FSCREENINFO: %s", strerror(errno) );
578         close( p_sys->i_fd );
579         return VLC_EGENERIC;
580     }
581
582     p_sys->i_bytes_per_pixel = 2;
583     p_sys->i_page_size = p_sys->fb_finfo.smem_len;
584     p_sys->i_line_len = p_sys->fb_finfo.line_length;
585
586     if( (p_sys->p_video = (uint8_t *)mmap( 0, p_sys->i_page_size, PROT_READ | PROT_WRITE, MAP_SHARED,
587                                             p_sys->i_fd, 0 )) == MAP_FAILED )
588     {
589         msg_Err( p_vout, "Can't mmap: %s", strerror(errno) );
590         close( p_sys->i_fd );
591         return VLC_EGENERIC;
592     }
593
594     p_sys->p_display = XOpenDisplay( NULL );
595
596     /* Open /dev/null and map it */
597     p_sys->i_null_fd = open( "/dev/zero", O_RDWR );
598     if( p_sys->i_null_fd == -1 )
599     {
600         msg_Err( p_vout, "cannot open /dev/zero (%m)" );
601         munmap( p_sys->p_video, p_sys->i_page_size );
602         close( p_sys->i_fd );
603         return VLC_EGENERIC;
604     }
605
606     if( (p_sys->p_null = (uint8_t *)mmap( 0, p_sys->i_page_size, PROT_READ | PROT_WRITE,
607                                           MAP_PRIVATE, p_sys->i_null_fd, 0 )) == MAP_FAILED )
608     {
609         msg_Err( p_vout, "Can't mmap 2: %s", strerror(errno) );
610         munmap( p_sys->p_video, p_sys->i_page_size );
611         close( p_sys->i_null_fd );
612         close( p_sys->i_fd );
613         return VLC_EGENERIC;
614     }
615
616     return VLC_SUCCESS;
617 }
618
619 /*****************************************************************************
620  * CloseDisplay: terminate FB video thread output method
621  *****************************************************************************/
622 static void CloseDisplay( vout_thread_t *p_vout )
623 {
624     munmap( p_vout->p_sys->p_video, p_vout->p_sys->i_page_size );
625     munmap( p_vout->p_sys->p_null,  p_vout->p_sys->i_page_size );
626
627     close( p_vout->p_sys->i_fd );
628     close( p_vout->p_sys->i_null_fd );
629 }
630
631 /*****************************************************************************
632  * UpdateScreen: update the screen of the omapfb
633  *****************************************************************************/
634 static void UpdateScreen( vout_thread_t *p_vout, int i_x, int i_y,
635                           int i_width, int i_height,
636                           int i_out_width, int i_out_height, int i_format )
637 {
638     struct omapfb_update_window update;
639     update.x = i_x;
640     update.y = i_y;
641     update.width = i_width;
642     update.height = i_height;
643     update.out_x = i_x;
644     update.out_y = i_y;
645     update.out_width = i_out_width;
646     update.out_height = i_out_height;
647     update.format = i_format;
648     if( p_vout->p_sys->b_tearsync )
649         update.format |= OMAPFB_FORMAT_FLAG_TEARSYNC;
650     ioctl( p_vout->p_sys->i_fd, OMAPFB_UPDATE_WINDOW, &update );
651 }
652
653 /*****************************************************************************
654  * InitWindow: get embedded window and init X11
655  *****************************************************************************/
656 static int InitWindow( vout_thread_t *p_vout )
657 {
658     vout_sys_t *p_sys = (vout_sys_t *)p_vout->p_sys;
659
660     if( var_CreateGetBool( p_vout, "omap-embedded" ) )
661     {
662         p_sys->p_display = XOpenDisplay( NULL );
663
664         // Request window from interface
665         vout_window_cfg_t wnd_cfg;
666
667         memset( &wnd_cfg, 0, sizeof(wnd_cfg) );
668         wnd_cfg.type   = VOUT_WINDOW_TYPE_XID;
669         wnd_cfg.x      = p_sys->embedded_window.i_x;
670         wnd_cfg.y      = p_sys->embedded_window.i_y;
671         wnd_cfg.width  = p_sys->embedded_window.i_width;
672         wnd_cfg.height = p_sys->embedded_window.i_height;
673
674         p_sys->owner_window = vout_window_New( VLC_OBJECT(p_vout), NULL, &wnd_cfg );
675         p_sys->main_window = p_sys->embedded_window;
676
677         // We have to create a new window to get some events
678         // (ButtonPress for example)
679         CreateWindow( p_sys );
680     }
681     else
682     {
683         // No embedding, fullscreen framebuffer overlay with no events handling
684         p_sys->main_window.i_x = p_sys->main_window.i_y = 0;
685         p_sys->main_window.i_width = p_sys->fb_vinfo.xres;
686         p_sys->main_window.i_height = p_sys->fb_vinfo.yres;
687         p_vout->b_fullscreen = true;
688         var_SetBool( p_vout, "fullscreen", p_vout->b_fullscreen );
689     }
690
691     return VLC_SUCCESS;
692 }
693
694 static void CreateWindow( vout_sys_t *p_sys )
695 {
696     XSetWindowAttributes    xwindow_attributes;
697     xwindow_attributes.backing_store = Always;
698     xwindow_attributes.background_pixel =
699         BlackPixel( p_sys->p_display, DefaultScreen(p_sys->p_display) );
700     xwindow_attributes.event_mask = ExposureMask | StructureNotifyMask;
701     p_sys->window = XCreateWindow( p_sys->p_display,
702                                    p_sys->owner_window->handle.xid,
703                                    0, 0,
704                                    p_sys->main_window.i_width,
705                                    p_sys->main_window.i_height,
706                                    0,
707                                    0, InputOutput, 0,
708                                    CWBackingStore | CWBackPixel | CWEventMask,
709                                    &xwindow_attributes );
710
711     XMapWindow( p_sys->p_display, p_sys->window );
712     XSelectInput( p_sys->p_display, p_sys->window,
713                   KeyPressMask | ButtonPressMask | StructureNotifyMask |
714                   VisibilityChangeMask | FocusChangeMask );
715     XSelectInput( p_sys->p_display, p_sys->owner_window->handle.xid,
716                   StructureNotifyMask );
717     XSetInputFocus( p_sys->p_display, p_sys->window, RevertToParent, CurrentTime );
718 }
719