]> git.sesse.net Git - vlc/blob - modules/video_output/omapfb.c
5732d04d560467e61f82d6ede45c0ba48309d801
[vlc] / modules / video_output / omapfb.c
1 /*****************************************************************************
2 * omapfb.c : omap framebuffer plugin for vlc
3 *****************************************************************************
4 * Copyright (C) 2008 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_playlist.h>
54
55 /*****************************************************************************
56 * Local prototypes
57 *****************************************************************************/
58 static int  Create           ( vlc_object_t * );
59 static void Destroy          ( vlc_object_t * );
60
61 static int  Init             ( vout_thread_t * );
62 static void End              ( vout_thread_t * );
63 static int  Manage           ( vout_thread_t * );
64 static void DisplayVideo     ( vout_thread_t *, picture_t * );
65 static int  Control          ( vout_thread_t *, int, va_list );
66
67 static void FreePicture      ( vout_thread_t *, picture_t * );
68
69 static int  OpenDisplay      ( vout_thread_t * );
70 static void CloseDisplay     ( vout_thread_t * );
71 static void UpdateScreen     ( vout_thread_t *,
72                                int, int, int, int, int, int, int );
73
74 static int  InitWindow       ( vout_thread_t * );
75 static void CreateWindow     ( vout_sys_t * );
76 static void ToggleFullScreen ( vout_thread_t * );
77
78 #ifdef HAVE_OSSO
79 static const int i_backlight_on_interval = 300;
80 #endif
81
82 /*****************************************************************************
83  * Module descriptor
84  *****************************************************************************/
85 #define FB_DEV_VAR "omapfbdev"
86
87 #define DEVICE_TEXT N_("OMAP Framebuffer device")
88 #define DEVICE_LONGTEXT N_( \
89     "OMAP Framebuffer device to use for rendering (usually /dev/fb0).")
90
91 #define CHROMA_TEXT N_("Chroma used.")
92 #define CHROMA_LONGTEXT N_( \
93     "Force use of a specific chroma for output. Default is Y420 (specific to N770/N8xx hardware)." )
94
95 #define OVERLAY_TEXT N_("Embed the overlay")
96 #define OVERLAY_LONGTEXT N_( \
97     "Embed the framebuffer overlay into a X11 window" )
98
99 vlc_module_begin();
100     set_shortname( "OMAP framebuffer" );
101     set_category( CAT_VIDEO );
102     set_subcategory( SUBCAT_VIDEO_VOUT );
103     add_file( FB_DEV_VAR, "/dev/fb0", NULL, DEVICE_TEXT, DEVICE_LONGTEXT,
104               false );
105     add_string( "omap-chroma", NULL, NULL, CHROMA_TEXT, CHROMA_LONGTEXT,
106                 true );
107     add_bool( "omap-embedded", true, NULL, OVERLAY_TEXT, OVERLAY_LONGTEXT,
108                  true );
109     set_description( N_("OMAP framebuffer video output") );
110     set_capability( "video output", 200 );
111     set_callbacks( Create, Destroy );
112 vlc_module_end();
113
114 /*****************************************************************************
115  * omap_window_t: simple structure with the geometry of a window
116  *****************************************************************************/
117 struct omap_window_t
118 {
119     uint32_t i_x;
120     uint32_t i_y;
121     uint32_t i_width;
122     uint32_t i_height;
123 };
124
125 /*****************************************************************************
126 * vout_sys_t: video output omap framebuffer method descriptor
127  *****************************************************************************
128  * This structure is part of the video output thread descriptor.
129  * It describes the FB specific properties of an output thread.
130  *****************************************************************************/
131 struct vout_sys_t
132 {
133     /* Framebuffer information */
134     int                         i_fd;                       /* device handle */
135     struct fb_var_screeninfo    fb_vinfo;        /* current mode information */
136     struct fb_fix_screeninfo    fb_finfo;
137     struct omapfb_caps          caps;
138     bool                        b_tearsync;
139
140     /* Window information */
141     struct omap_window_t output_window;    /* Size of the real output window */
142     struct omap_window_t main_window;  /* Size of the area we got to display */
143     struct omap_window_t embedded_window;     /* Size of the embedded window */
144
145     /* Video information */
146     uint32_t             i_video_width;                       /* video width */
147     uint32_t             i_video_height;                     /* video height */
148     vlc_fourcc_t         i_chroma;
149     int                  i_color_format;                   /* OMAPFB_COLOR_* */
150     bool                 b_embed;
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     Window     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_FOURCC('I','4','2','0') &&
188         p_vout->fmt_in.i_chroma != VLC_FOURCC('I','Y','U','V') &&
189         p_vout->fmt_in.i_chroma != VLC_FOURCC('Y','V','1','2') )
190         return VLC_EGENERIC;
191
192     /* Allocate instance and initialize some members */
193     p_vout->p_sys = p_sys = malloc( sizeof( vout_sys_t ) );
194     if( p_vout->p_sys == NULL )
195         return VLC_ENOMEM;
196     memset( p_sys, 0, sizeof(vout_sys_t) );
197
198     p_vout->pf_init = Init;
199     p_vout->pf_end = End;
200     p_vout->pf_manage = Manage;
201     p_vout->pf_render = NULL;
202     p_vout->pf_display = DisplayVideo;
203     p_vout->pf_control = Control;
204
205     p_sys->b_embed = var_CreateGetInteger( p_vout, "omap-embedded" );
206     p_sys->b_video_enabled = true;
207
208     if( OpenDisplay( p_vout ) )
209     {
210         free( p_vout->p_sys );
211         return VLC_EGENERIC;
212     }
213
214     if( InitWindow( p_vout ) )
215     {
216         free( p_vout->p_sys );
217         return VLC_EGENERIC;
218     }
219
220 #ifdef HAVE_OSSO
221     p_vout->p_sys->i_backlight_on_counter = i_backlight_on_interval;
222     p_vout->p_sys->p_octx = osso_initialize( "vlc", VERSION, 0, NULL );
223     if ( p_vout->p_sys->p_octx == NULL ) {
224         msg_Err( p_vout, "Could not get osso context" );
225     } else {
226         msg_Dbg( p_vout, "Initialized osso context" );
227     }
228 #endif
229
230     return VLC_SUCCESS;
231 }
232
233 /*****************************************************************************
234  * Destroy: destroy omapfb video thread output method
235  *****************************************************************************
236  * Terminate an output method created by Create
237  *****************************************************************************/
238 static void Destroy( vlc_object_t *p_this )
239 {
240     vout_thread_t *p_vout = (vout_thread_t *)p_this;
241
242     CloseDisplay( p_vout );
243
244     if( p_vout->p_sys->b_embed )
245     {
246         vout_ReleaseWindow( p_vout, (void *)p_vout->p_sys->owner_window );
247         if( p_vout->b_fullscreen )
248             XDestroyWindow( p_vout->p_sys->p_display, p_vout->p_sys->window );
249         XCloseDisplay( p_vout->p_sys->p_display );
250     }
251
252 #ifdef HAVE_OSSO
253     if ( p_vout->p_sys->p_octx != NULL ) {
254         msg_Dbg( p_vout, "Deinitializing osso context" );
255         osso_deinitialize( p_vout->p_sys->p_octx );
256     }
257 #endif
258
259     /* Destroy structure */
260     free( p_vout->p_sys );
261 }
262
263
264 /*****************************************************************************
265  * Init: initialize omap framebuffer video thread output method
266  *****************************************************************************/
267 static int Init( vout_thread_t *p_vout )
268 {
269     vout_sys_t *p_sys = (vout_sys_t *)p_vout->p_sys;
270
271     // We want to keep the same aspect
272     p_vout->fmt_out.i_aspect = p_vout->output.i_aspect = p_vout->render.i_aspect;
273     // We ask where the video should be displayed in the video area
274     vout_PlacePicture( p_vout, p_sys->main_window.i_width,
275                        p_sys->main_window.i_height,
276                        &p_sys->output_window.i_x,
277                        &p_sys->output_window.i_y,
278                        &p_sys->output_window.i_width,
279                        &p_sys->output_window.i_height );
280     p_sys->output_window.i_x = ( p_sys->output_window.i_x +
281                                  p_sys->main_window.i_x ) & ~1;
282     p_sys->output_window.i_y = ( p_sys->output_window.i_y +
283                                  p_sys->main_window.i_y ) & ~1;
284
285     // Hardware upscaling better than software
286     if( p_vout->fmt_render.i_width <= p_sys->main_window.i_width &&
287         p_vout->fmt_render.i_height <= p_sys->main_window.i_height )
288     {
289         p_sys->i_video_width =
290         p_vout->output.i_width =
291         p_vout->fmt_out.i_width =
292         p_vout->fmt_out.i_visible_width = p_vout->fmt_render.i_width;
293         p_sys->i_video_height =
294         p_vout->output.i_height =
295         p_vout->fmt_out.i_height =
296         p_vout->fmt_out.i_visible_height = p_vout->fmt_render.i_height;
297     }
298     else
299     {
300         p_sys->i_video_width =
301         p_vout->output.i_width =
302         p_vout->fmt_out.i_width =
303         p_vout->fmt_out.i_visible_width = p_sys->output_window.i_width;
304         p_sys->i_video_height =
305         p_vout->output.i_height =
306         p_vout->fmt_out.i_height =
307         p_vout->fmt_out.i_visible_height = p_sys->output_window.i_height;
308     }
309
310     p_vout->output.i_chroma =
311     p_vout->fmt_out.i_chroma = VLC_FOURCC( 'Y','4','2','0' );
312     p_sys->i_color_format = OMAPFB_COLOR_YUV420;
313
314     // place in the framebuffer where we have to write
315     p_sys->p_center = p_sys->p_video + p_sys->output_window.i_x*p_sys->i_bytes_per_pixel
316                       + p_sys->output_window.i_y*p_sys->i_line_len;
317
318     // We get and set a direct render vlc picture
319     I_OUTPUTPICTURES = 0;
320     picture_t *p_pic = NULL;
321     int i_index;
322
323     for( i_index = 0 ; i_index < VOUT_MAX_PICTURES ; i_index++ )
324     {
325         if( p_vout->p_picture[ i_index ].i_status == FREE_PICTURE )
326         {
327             p_pic = p_vout->p_picture + i_index;
328             break;
329         }
330     }
331
332     /* Allocate the picture */
333     if( p_pic == NULL )
334     {
335         return VLC_EGENERIC;
336     }
337
338     p_sys->p_output_picture = p_pic;
339     p_pic->p->p_pixels = p_vout->p_sys->p_center;
340     p_pic->p->i_pixel_pitch = p_vout->p_sys->i_bytes_per_pixel;
341     p_pic->p->i_lines = p_sys->i_video_height;
342     p_pic->p->i_visible_lines = p_sys->i_video_height;
343     p_pic->p->i_pitch = p_sys->i_line_len;
344     p_pic->p->i_visible_pitch = p_sys->i_line_len;
345     p_pic->i_planes = 1;
346     p_pic->i_status = DESTROYED_PICTURE;
347     p_pic->i_type   = DIRECT_PICTURE;
348
349     PP_OUTPUTPICTURE[ I_OUTPUTPICTURES ] = p_pic;
350
351     I_OUTPUTPICTURES++;
352
353     return VLC_SUCCESS;
354 }
355
356 /*****************************************************************************
357  * End: terminate omap framebuffer video thread output method
358  *****************************************************************************/
359 static void End( vout_thread_t *p_vout )
360 {
361     /* Clear the screen */
362     UpdateScreen( p_vout, 0, 0,
363                   p_vout->p_sys->fb_vinfo.xres,
364                   p_vout->p_sys->fb_vinfo.yres,
365                   p_vout->p_sys->fb_vinfo.xres,
366                   p_vout->p_sys->fb_vinfo.yres,
367                   OMAPFB_COLOR_RGB565 );
368 }
369
370 /*****************************************************************************
371  * Control: control facility for the vout
372  *****************************************************************************/
373 static int Control( vout_thread_t *p_vout, int i_query, va_list args )
374 {
375     switch( i_query )
376     {
377        default:
378             return vout_vaControlDefault( p_vout, i_query, args );
379     }
380 }
381
382 /*****************************************************************************
383 * FreePicture: Destroy the picture and make it free again
384 ******************************************************************************/
385 static void FreePicture( vout_thread_t *p_vout, picture_t *p_pic )
386 {
387     p_pic->p->p_pixels = NULL;
388     p_pic->i_status = FREE_PICTURE;
389 }
390
391 /*****************************************************************************
392  * Manage: handle omapfb events
393  *****************************************************************************
394  * This function should be called regularly by video output thread.
395  *****************************************************************************/
396 static int Manage( vout_thread_t *p_vout )
397 {
398     XEvent xevent;
399
400     while( XPending( p_vout->p_sys->p_display ) )
401     {
402         XNextEvent( p_vout->p_sys->p_display, &xevent );
403
404         if( xevent.type == ButtonPress &&
405             ((XButtonEvent *)&xevent)->button == Button1 )
406         {
407             /* detect double-clicks */
408             if( ( ((XButtonEvent *)&xevent)->time -
409                     p_vout->p_sys->i_time_button_last_pressed ) < 300 )
410             {
411                 p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
412             }
413
414             p_vout->p_sys->i_time_button_last_pressed =
415                         ((XButtonEvent *)&xevent)->time;
416         }
417         else if( xevent.type == KeyPress )
418         {
419             KeySym x_key_symbol;
420             vlc_value_t val;
421
422             x_key_symbol = XKeycodeToKeysym( p_vout->p_sys->p_display,
423                                              xevent.xkey.keycode, 0 );
424
425             switch( x_key_symbol )
426             {
427             case XK_Return:
428                 val.i_int = ACTIONID_PLAY_PAUSE; break;
429             case XK_Escape:
430                 val.i_int = ACTIONID_QUIT; break;
431             case XK_Down:
432                 val.i_int = ACTIONID_JUMP_BACKWARD_MEDIUM; break;
433             case XK_Up:
434                 val.i_int = ACTIONID_JUMP_FORWARD_MEDIUM; break;
435             case XK_Right:
436                 val.i_int = ACTIONID_JUMP_FORWARD_SHORT; break;
437             case XK_Left:
438                 val.i_int = ACTIONID_JUMP_BACKWARD_SHORT; break;
439             case XK_F6:
440                 val.i_int = ACTIONID_TOGGLE_FULLSCREEN; break;
441             case XK_F7:
442                 val.i_int = ACTIONID_VOL_UP; break;
443             case XK_F8:
444                 val.i_int = ACTIONID_VOL_DOWN; break;
445             }
446             var_SetInteger( p_vout->p_libvlc, "key-action", val.i_int );
447         }
448         else if( ( xevent.type == VisibilityNotify &&
449                  xevent.xvisibility.state == VisibilityUnobscured ) ||
450                  xevent.type == FocusIn )
451         {
452             p_vout->p_sys->b_video_enabled = true;
453             p_vout->p_sys->p_output_picture->p->p_pixels =
454                 p_vout->p_sys->p_center;
455             XSetInputFocus( p_vout->p_sys->p_display, p_vout->p_sys->window,
456                             RevertToParent, CurrentTime );
457         }
458         else if( ( xevent.type == VisibilityNotify &&
459                  xevent.xvisibility.state != VisibilityUnobscured ) ||
460                  xevent.type == FocusOut || xevent.type == UnmapNotify )
461         {
462             UpdateScreen( p_vout, 0, 0,
463                           p_vout->p_sys->fb_vinfo.xres,
464                           p_vout->p_sys->fb_vinfo.yres,
465                           p_vout->p_sys->fb_vinfo.xres,
466                           p_vout->p_sys->fb_vinfo.yres,
467                           OMAPFB_COLOR_RGB565 );
468             p_vout->p_sys->b_video_enabled = false;
469             p_vout->p_sys->p_output_picture->p->p_pixels =
470                 p_vout->p_sys->p_null;
471          }
472     }
473
474     if( p_vout->i_changes & VOUT_FULLSCREEN_CHANGE )
475     {
476         /* Update the object variable and trigger callback */
477         var_SetBool( p_vout, "fullscreen", !p_vout->b_fullscreen );
478
479         if( p_vout->p_sys->b_embed )
480             ToggleFullScreen( p_vout );
481         p_vout->i_changes &= ~VOUT_FULLSCREEN_CHANGE;
482     }
483
484     if( p_vout->i_changes & VOUT_SIZE_CHANGE )
485     {
486         FreePicture( p_vout, p_vout->p_sys->p_output_picture );
487         if( Init( p_vout ) )
488         {
489             msg_Err( p_vout, "cannot reinit framebuffer screen" );
490             return VLC_EGENERIC;
491         }
492     }
493
494
495 #ifdef HAVE_OSSO
496     if ( p_vout->p_sys->p_octx != NULL ) {
497         if ( p_vout->p_sys->i_backlight_on_counter == i_backlight_on_interval ) {
498             if ( osso_display_blanking_pause( p_vout->p_sys->p_octx ) != OSSO_OK ) {
499                 msg_Err( p_vout, "Could not disable backlight blanking" );
500         } else {
501                 msg_Dbg( p_vout, "Backlight blanking disabled" );
502             }
503             p_vout->p_sys->i_backlight_on_counter = 0;
504         } else {
505             p_vout->p_sys->i_backlight_on_counter ++;
506         }
507     }
508 #endif
509
510     return VLC_SUCCESS;
511 }
512
513 /*****************************************************************************
514  * DisplayVideo: displays previously rendered output
515  *****************************************************************************
516  * This function update the screen resulting in the display of the last
517  * rendered picture.
518  *****************************************************************************/
519 static void DisplayVideo( vout_thread_t *p_vout, picture_t *p_pic )
520 {
521     VLC_UNUSED( p_pic );
522
523     if( !p_vout->p_sys->b_video_enabled )
524         return;
525
526     UpdateScreen( p_vout,
527                   p_vout->p_sys->output_window.i_x,
528                   p_vout->p_sys->output_window.i_y,
529                   p_vout->p_sys->i_video_width,
530                   p_vout->p_sys->i_video_height,
531                   p_vout->p_sys->output_window.i_width,
532                   p_vout->p_sys->output_window.i_height,
533                   p_vout->p_sys->i_color_format );
534
535     // Wait for the window to be fully displayed
536     ioctl( p_vout->p_sys->i_fd, OMAPFB_SYNC_GFX);
537 }
538
539 /*****************************************************************************
540  * OpenDisplay: initialize framebuffer
541  *****************************************************************************/
542 static int OpenDisplay( vout_thread_t *p_vout )
543 {
544     vout_sys_t *p_sys = (vout_sys_t *) p_vout->p_sys;
545     char *psz_device;                             /* framebuffer device path */
546
547     /* Open framebuffer device */
548     if( !(psz_device = config_GetPsz( p_vout, FB_DEV_VAR )) )
549     {
550         msg_Err( p_vout, "don't know which fb device to open" );
551         return VLC_EGENERIC;
552     }
553
554     p_sys->i_fd = open( psz_device, O_RDWR );
555     if( p_sys->i_fd == -1 )
556     {
557         msg_Err( p_vout, "cannot open %s (%m)", psz_device );
558         free( psz_device );
559         return VLC_EGENERIC;
560     }
561     free( psz_device );
562     psz_device = NULL;
563
564     // Get caps, try older interface if needed
565     if( ioctl( p_sys->i_fd, OMAPFB_GET_CAPS, &p_sys->caps ) != 0 )
566     {
567         if( ioctl( p_sys->i_fd, OMAP_IOR( 42, unsigned long ), &p_sys->caps ) != 0 )
568         {
569             msg_Err( p_vout, "OMAPFB_GET_CAPS ioctl failed" );
570             close( p_sys->i_fd );
571             return VLC_EGENERIC;
572         }
573     }
574
575     if( ( p_sys->caps.ctrl & OMAPFB_CAPS_TEARSYNC ) != 0 )
576         p_sys->b_tearsync = true;
577
578     if( ioctl( p_sys->i_fd, FBIOGET_VSCREENINFO, &p_sys->fb_vinfo ) )
579     {
580         msg_Err( p_vout, "Can't get VSCREENINFO: %s", strerror(errno) );
581         return VLC_EGENERIC;
582     }
583     if( ioctl( p_sys->i_fd, FBIOGET_FSCREENINFO, &p_sys->fb_finfo ) )
584     {
585         msg_Err( p_vout, "Can't get FSCREENINFO: %s", strerror(errno) );
586         return VLC_EGENERIC;
587     }
588
589     p_sys->i_bytes_per_pixel = 2;
590     p_sys->i_page_size = p_sys->fb_finfo.smem_len;
591     p_sys->i_line_len = p_sys->fb_finfo.line_length;
592
593     if( (p_sys->p_video = (uint8_t *)mmap( 0, p_sys->i_page_size, PROT_READ | PROT_WRITE, MAP_SHARED,
594                                             p_sys->i_fd, 0 )) == MAP_FAILED )
595     {
596         msg_Err( p_vout, "Can't mmap: %s\n", strerror(errno) );
597         return VLC_EGENERIC;
598     }
599
600     p_sys->p_display = XOpenDisplay( NULL );
601
602     /* Open /dev/null and map it */
603     p_sys->i_null_fd = open( "/dev/zero", O_RDWR );
604     if( p_sys->i_null_fd == -1 )
605     {
606         msg_Err( p_vout, "cannot open /dev/zero (%m)" );
607         return VLC_EGENERIC;
608     }
609
610     if( (p_sys->p_null = (uint8_t *)mmap( 0, p_sys->i_page_size, PROT_READ | PROT_WRITE,
611                                           MAP_PRIVATE, p_sys->i_null_fd, 0 )) == MAP_FAILED )
612     {
613         msg_Err( p_vout, "Can't mmap 2: %s\n", strerror(errno) );
614         return VLC_EGENERIC;
615     }
616
617     return VLC_SUCCESS;
618 }
619
620 /*****************************************************************************
621  * CloseDisplay: terminate FB video thread output method
622  *****************************************************************************/
623 static void CloseDisplay( vout_thread_t *p_vout )
624 {
625     munmap( p_vout->p_sys->p_video, p_vout->p_sys->i_page_size );
626     munmap( p_vout->p_sys->p_null,  p_vout->p_sys->i_page_size );
627
628     close( p_vout->p_sys->i_fd );
629     close( p_vout->p_sys->i_null_fd );
630 }
631
632 /*****************************************************************************
633  * UpdateScreen: update the screen of the omapfb
634  *****************************************************************************/
635 static void UpdateScreen( vout_thread_t *p_vout, int i_x, int i_y,
636                           int i_width, int i_height,
637                           int i_out_width, int i_out_height, int i_format )
638 {
639     struct omapfb_update_window update;
640     update.x = i_x;
641     update.y = i_y;
642     update.width = i_width;
643     update.height = i_height;
644     update.out_x = i_x;
645     update.out_y = i_y;
646     update.out_width = i_out_width;
647     update.out_height = i_out_height;
648     update.format = i_format;
649     if( p_vout->p_sys->b_tearsync )
650         update.format |= OMAPFB_FORMAT_FLAG_TEARSYNC;
651     ioctl( p_vout->p_sys->i_fd, OMAPFB_UPDATE_WINDOW, &update );
652 }
653
654 /*****************************************************************************
655  * InitWindow: get embedded window and init X11
656  *****************************************************************************/
657 static int InitWindow( vout_thread_t *p_vout )
658 {
659     vout_sys_t *p_sys = (vout_sys_t *)p_vout->p_sys;
660
661     if( p_sys->b_embed )
662     {
663         p_sys->p_display = XOpenDisplay( NULL );
664
665         // Request window from interface
666         p_sys->owner_window =
667             vout_RequestWindow( p_vout,
668                                 &p_sys->embedded_window.i_x,
669                                 &p_sys->embedded_window.i_y,
670                                 &p_sys->embedded_window.i_width,
671                                 &p_sys->embedded_window.i_height );
672         p_sys->main_window = p_sys->embedded_window;
673
674         // We have to create a new window to get some events
675         // (ButtonPress for example)
676         CreateWindow( p_sys );
677     }
678     else
679     {
680         // No embedding, fullscreen framebuffer overlay with no events handling
681         p_sys->main_window.i_x = p_sys->main_window.i_y = 0;
682         p_sys->main_window.i_width = p_sys->fb_vinfo.xres;
683         p_sys->main_window.i_height = p_sys->fb_vinfo.yres;
684         p_vout->b_fullscreen = true;
685         var_SetBool( p_vout, "fullscreen", p_vout->b_fullscreen );
686     }
687
688     return VLC_SUCCESS;
689 }
690
691 static void CreateWindow( vout_sys_t *p_sys )
692 {
693     XSetWindowAttributes    xwindow_attributes;
694     xwindow_attributes.backing_store = Always;
695     xwindow_attributes.background_pixel =
696         BlackPixel( p_sys->p_display, DefaultScreen(p_sys->p_display) );
697     xwindow_attributes.event_mask = ExposureMask | StructureNotifyMask;
698     p_sys->window = XCreateWindow( p_sys->p_display,
699                                    p_sys->owner_window,
700                                    0, 0,
701                                    p_sys->main_window.i_width,
702                                    p_sys->main_window.i_height,
703                                    0,
704                                    0, InputOutput, 0,
705                                    CWBackingStore | CWBackPixel | CWEventMask,
706                                    &xwindow_attributes );
707
708     XMapWindow( p_sys->p_display, p_sys->window );
709     XSelectInput( p_sys->p_display, p_sys->window,
710                   KeyPressMask | ButtonPressMask | StructureNotifyMask |
711                   VisibilityChangeMask | FocusChangeMask );
712     XSelectInput( p_sys->p_display, p_sys->owner_window,
713                   StructureNotifyMask );
714     XSetInputFocus( p_sys->p_display, p_sys->window, RevertToParent, CurrentTime );
715 }
716
717 static void ToggleFullScreen ( vout_thread_t * p_vout )
718 {
719     p_vout->b_fullscreen = !p_vout->b_fullscreen;
720
721     if( p_vout->b_fullscreen )
722     {
723         msg_Dbg( p_vout, "Entering fullscreen mode" );
724         XReparentWindow( p_vout->p_sys->p_display,
725                          p_vout->p_sys->window,
726                          DefaultRootWindow( p_vout->p_sys->p_display ),
727                          0, 0 );
728
729         XEvent xev;
730
731         /* init X event structure for _NET_WM_FULLSCREEN client msg */
732         xev.xclient.type = ClientMessage;
733         xev.xclient.serial = 0;
734         xev.xclient.send_event = True;
735         xev.xclient.message_type = XInternAtom( p_vout->p_sys->p_display,
736                                                 "_NET_WM_STATE", False );
737         xev.xclient.window = p_vout->p_sys->window;
738         xev.xclient.format = 32;
739         if( p_vout->b_fullscreen )
740             xev.xclient.data.l[0] = 1;
741         else
742             xev.xclient.data.l[0] = 0;
743         xev.xclient.data.l[1] = XInternAtom( p_vout->p_sys->p_display,
744                                             "_NET_WM_STATE_FULLSCREEN", False );
745         xev.xclient.data.l[2] = 0;
746         xev.xclient.data.l[3] = 0;
747         xev.xclient.data.l[4] = 0;
748
749         XSendEvent( p_vout->p_sys->p_display,
750                     DefaultRootWindow( p_vout->p_sys->p_display ), False,
751                     SubstructureRedirectMask | SubstructureNotifyMask, &xev);
752
753         p_vout->p_sys->main_window.i_x = p_vout->p_sys->main_window.i_y = 0;
754         p_vout->p_sys->main_window.i_width = p_vout->p_sys->fb_vinfo.xres;
755         p_vout->p_sys->main_window.i_height = p_vout->p_sys->fb_vinfo.yres;
756     }
757     else
758     {
759         msg_Dbg( p_vout, "Leaving fullscreen mode" );
760
761         XDestroyWindow( p_vout->p_sys->p_display, p_vout->p_sys->window );
762
763         p_vout->p_sys->main_window = p_vout->p_sys->embedded_window;
764         CreateWindow( p_vout->p_sys );
765     }
766
767     XSync( p_vout->p_sys->p_display, False);
768
769     /* signal that the size needs to be updated */
770     p_vout->i_changes |= VOUT_SIZE_CHANGE;
771 }