omapfb.c
上传用户:kjfoods
上传日期:2020-07-06
资源大小:29949k
文件大小:28k
源码类别:

midi

开发平台:

Unix_Linux

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