]> git.sesse.net Git - vlc/blob - modules/video_output/fb.c
8880b36a31c295ff3251882a284787718078f55f
[vlc] / modules / video_output / fb.c
1 /*****************************************************************************
2  * fb.c : framebuffer plugin for vlc
3  *****************************************************************************
4  * Copyright (C) 2000, 2001 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Samuel Hocevar <sam@zoy.org>
8  *          Jean-Paul Saman
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28
29 #ifdef HAVE_CONFIG_H
30 # include "config.h"
31 #endif
32
33 #include <errno.h>                                                 /* ENOMEM */
34 #include <signal.h>                                      /* SIGUSR1, SIGUSR2 */
35 #include <fcntl.h>                                                 /* open() */
36 #include <unistd.h>                                               /* close() */
37
38 #include <termios.h>                                       /* struct termios */
39 #include <sys/ioctl.h>
40 #include <sys/mman.h>                                              /* mmap() */
41
42 #include <linux/fb.h>
43 #include <linux/vt.h>                                                /* VT_* */
44 #include <linux/kd.h>                                                 /* KD* */
45
46 #include <vlc_common.h>
47 #include <vlc_plugin.h>
48 #include <vlc_vout.h>
49 #include <vlc_interface.h>
50
51 /*****************************************************************************
52  * Local prototypes
53  *****************************************************************************/
54 static int  Create    ( vlc_object_t * );
55 static void Destroy   ( vlc_object_t * );
56
57 static int  Init      ( vout_thread_t * );
58 static void End       ( vout_thread_t * );
59 static int  Manage    ( vout_thread_t * );
60 static void Display   ( vout_thread_t *, picture_t * );
61 static int  Control   ( vout_thread_t *, int, va_list );
62
63 static int  NewPicture     ( vout_thread_t *, picture_t * );
64 static void FreePicture    ( vout_thread_t *, picture_t * );
65
66 static int  OpenDisplay    ( vout_thread_t * );
67 static void CloseDisplay   ( vout_thread_t * );
68 static void SwitchDisplay  ( int i_signal );
69 static void TextMode       ( int i_tty );
70 static void GfxMode        ( int i_tty );
71
72 #define MAX_DIRECTBUFFERS 1
73
74 /*****************************************************************************
75  * Module descriptor
76  *****************************************************************************/
77 #define FB_DEV_VAR "fbdev"
78
79 #define DEVICE_TEXT N_("Framebuffer device")
80 #define DEVICE_LONGTEXT N_( \
81     "Framebuffer device to use for rendering (usually /dev/fb0).")
82
83 #define TTY_TEXT N_("Run fb on current tty.")
84 #define TTY_LONGTEXT N_( \
85     "Run framebuffer on current TTY device (default enabled). " \
86     "(disable tty handling with caution)" )
87
88 #define CHROMA_TEXT N_("Chroma used.")
89 #define CHROMA_LONGTEXT N_( \
90     "Force use of a specific chroma for output. Default is I420." )
91
92 #define ASPECT_RATIO_TEXT N_("Video aspect ratio")
93 #define ASPECT_RATIO_LONGTEXT N_( \
94     "Aspect ratio of the video image (4:3, 16:9). Default is square pixels." )
95
96 #define FB_MODE_TEXT N_("Framebuffer resolution to use.")
97 #define FB_MODE_LONGTEXT N_( \
98     "Select the resolution for the framebuffer. Currently it supports " \
99     "the values 0=QCIF 1=CIF 2=NTSC 3=PAL, 4=auto (default 4=auto)" )
100
101 #define HW_ACCEL_TEXT N_("Framebuffer uses hw acceleration.")
102 #define HW_ACCEL_LONGTEXT N_( \
103     "If your framebuffer supports hardware acceleration or does double buffering " \
104     "in hardware then you must disable this option. It then does double buffering " \
105     "in software." )
106
107 vlc_module_begin ()
108     set_shortname( "Framebuffer" )
109     set_category( CAT_VIDEO )
110     set_subcategory( SUBCAT_VIDEO_VOUT )
111     add_file( FB_DEV_VAR, "/dev/fb0", NULL, DEVICE_TEXT, DEVICE_LONGTEXT,
112               false )
113     add_bool( "fb-tty", 1, NULL, TTY_TEXT, TTY_LONGTEXT, true )
114     add_string( "fb-chroma", NULL, NULL, CHROMA_TEXT, CHROMA_LONGTEXT,
115                 true )
116     add_string( "fb-aspect-ratio", NULL, NULL, ASPECT_RATIO_TEXT,
117                 ASPECT_RATIO_LONGTEXT, true )
118     add_integer( "fb-mode", 4, NULL, FB_MODE_TEXT, FB_MODE_LONGTEXT,
119                  true )
120     add_bool( "fb-hw-accel", true, NULL, HW_ACCEL_TEXT, HW_ACCEL_LONGTEXT,
121               true )
122     set_description( N_("GNU/Linux framebuffer video output") )
123     set_capability( "video output", 30 )
124     set_callbacks( Create, Destroy )
125 vlc_module_end ()
126
127 /*****************************************************************************
128  * vout_sys_t: video output framebuffer method descriptor
129  *****************************************************************************
130  * This structure is part of the video output thread descriptor.
131  * It describes the FB specific properties of an output thread.
132  *****************************************************************************/
133 struct vout_sys_t
134 {
135     /* System information */
136     int                 i_tty;                          /* tty device handle */
137     bool                b_tty;
138     struct termios      old_termios;
139
140     /* Original configuration information */
141     struct sigaction            sig_usr1;           /* USR1 previous handler */
142     struct sigaction            sig_usr2;           /* USR2 previous handler */
143     struct vt_mode              vt_mode;                 /* previous VT mode */
144
145     /* Framebuffer information */
146     int                         i_fd;                       /* device handle */
147     struct fb_var_screeninfo    old_info;       /* original mode information */
148     struct fb_var_screeninfo    var_info;        /* current mode information */
149     bool                        b_pan;     /* does device supports panning ? */
150     struct fb_cmap              fb_cmap;                /* original colormap */
151     uint16_t                    *p_palette;              /* original palette */
152     bool                        b_hw_accel;          /* has hardware support */
153
154     /* Video information */
155     uint32_t i_width;
156     uint32_t i_height;
157     int      i_aspect;
158     int      i_bytes_per_pixel;
159     bool     b_auto;       /* Automatically adjust video size to fb size */
160     vlc_fourcc_t i_chroma;
161
162     /* Video memory */
163     uint8_t    *p_video;                                      /* base adress */
164     size_t      i_page_size;                                    /* page size */
165 };
166
167 struct picture_sys_t
168 {
169     uint8_t *    p_data;                                      /* base adress */
170 };
171
172 /*****************************************************************************
173  * Create: allocates FB video thread output method
174  *****************************************************************************
175  * This function allocates and initializes a FB vout method.
176  *****************************************************************************/
177 static int Create( vlc_object_t *p_this )
178 {
179     vout_thread_t *p_vout = (vout_thread_t *)p_this;
180     vout_sys_t    *p_sys;
181     char          *psz_chroma;
182     char          *psz_aspect;
183     int           i_mode;
184     struct sigaction    sig_tty;                 /* sigaction for tty change */
185     struct vt_mode      vt_mode;                          /* vt current mode */
186     struct termios      new_termios;
187
188     /* Allocate instance and initialize some members */
189     p_vout->p_sys = p_sys = malloc( sizeof( vout_sys_t ) );
190     if( p_vout->p_sys == NULL )
191         return VLC_ENOMEM;
192     memset( p_sys, 0, sizeof(vout_sys_t) );
193
194     p_sys->p_video = MAP_FAILED;
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 = Display;
201     p_vout->pf_control = Control;
202
203     /* Does the framebuffer uses hw acceleration? */
204     p_sys->b_hw_accel = var_CreateGetBool( p_vout, "fb-hw-accel" );
205
206     /* Set tty and fb devices */
207     p_sys->i_tty = 0; /* 0 == /dev/tty0 == current console */
208     p_sys->b_tty = var_CreateGetBool( p_vout, "fb-tty" );
209 #ifndef WIN32
210 #if defined(HAVE_ISATTY)
211     /* Check that stdin is a TTY */
212     if( p_sys->b_tty && !isatty( 0 ) )
213     {
214         msg_Warn( p_vout, "fd 0 is not a TTY" );
215         return VLC_EGENERIC;
216     }
217     else
218     {
219         msg_Warn( p_vout, "disabling tty handling, use with caution because "
220                           "there is no way to return to the tty." );
221     }
222 #endif
223 #endif
224
225     psz_chroma = var_CreateGetNonEmptyString( p_vout, "fb-chroma" );
226     if( psz_chroma )
227     {
228         if( strlen( psz_chroma ) == 4 )
229         {
230             p_sys->i_chroma = VLC_FOURCC( psz_chroma[0],
231                                    psz_chroma[1],
232                                    psz_chroma[2],
233                                    psz_chroma[3] );
234             msg_Dbg( p_vout, "forcing chroma '%s'", psz_chroma );
235         }
236         else
237         {
238             msg_Warn( p_vout, "invalid chroma (%s), using defaults.",
239                       psz_chroma );
240         }
241         free( psz_chroma );
242         psz_chroma = NULL;
243     }
244
245     p_sys->i_aspect = -1;
246     psz_aspect = var_CreateGetNonEmptyString( p_vout, "fb-aspect-ratio" );
247     if( psz_aspect )
248     {
249         char *psz_parser = strchr( psz_aspect, ':' );
250
251         if( psz_parser )
252         {
253             *psz_parser++ = '\0';
254             p_sys->i_aspect = ( atoi( psz_aspect )
255                               * VOUT_ASPECT_FACTOR ) / atoi( psz_parser );
256         }
257         msg_Dbg( p_vout, "using aspect ratio %d:%d",
258                   atoi( psz_aspect ), atoi( psz_parser ) );
259
260         free( psz_aspect );
261         psz_aspect = NULL;
262     }
263
264     p_sys->b_auto = false;
265     i_mode = var_CreateGetInteger( p_vout, "fb-mode" );
266     switch( i_mode )
267     {
268         case 0: /* QCIF */
269             p_sys->i_width  = 176;
270             p_sys->i_height = 144;
271             break;
272         case 1: /* CIF */
273             p_sys->i_width  = 352;
274             p_sys->i_height = 288;
275             break;
276         case 2: /* NTSC */
277             p_sys->i_width  = 640;
278             p_sys->i_height = 480;
279             break;
280         case 3: /* PAL */
281             p_sys->i_width  = 704;
282             p_sys->i_height = 576;
283             break;
284         case 4:
285         default:
286             p_sys->b_auto = true;
287             break;
288      }
289
290     /* tty handling */
291     if( p_sys->b_tty )
292     {
293         GfxMode( p_sys->i_tty );
294
295         /* Set keyboard settings */
296         if( tcgetattr(0, &p_vout->p_sys->old_termios) == -1 )
297         {
298             msg_Err( p_vout, "tcgetattr failed" );
299         }
300
301         if( tcgetattr(0, &new_termios) == -1 )
302         {
303             msg_Err( p_vout, "tcgetattr failed" );
304         }
305
306         /* new_termios.c_lflag &= ~ (ICANON | ISIG);
307         new_termios.c_lflag |= (ECHO | ECHOCTL); */
308         new_termios.c_lflag &= ~ (ICANON);
309         new_termios.c_lflag &= ~(ECHO | ECHOCTL);
310         new_termios.c_iflag = 0;
311         new_termios.c_cc[VMIN] = 1;
312         new_termios.c_cc[VTIME] = 0;
313
314         if( tcsetattr(0, TCSAFLUSH, &new_termios) == -1 )
315         {
316             msg_Err( p_vout, "tcsetattr failed" );
317         }
318
319         ioctl( p_sys->i_tty, VT_RELDISP, VT_ACKACQ );
320
321         /* Set-up tty signal handler to be aware of tty changes */
322         memset( &sig_tty, 0, sizeof( sig_tty ) );
323         sig_tty.sa_handler = SwitchDisplay;
324         sigemptyset( &sig_tty.sa_mask );
325         if( sigaction( SIGUSR1, &sig_tty, &p_vout->p_sys->sig_usr1 ) ||
326             sigaction( SIGUSR2, &sig_tty, &p_vout->p_sys->sig_usr2 ) )
327         {
328             msg_Err( p_vout, "cannot set signal handler (%m)" );
329             tcsetattr(0, 0, &p_vout->p_sys->old_termios);
330             TextMode( p_sys->i_tty );
331             free( p_vout->p_sys );
332             return VLC_EGENERIC;
333         }
334
335         /* Set-up tty according to new signal handler */
336         if( -1 == ioctl( p_sys->i_tty, VT_GETMODE, &p_vout->p_sys->vt_mode ) )
337         {
338             msg_Err( p_vout, "cannot get terminal mode (%m)" );
339             sigaction( SIGUSR1, &p_vout->p_sys->sig_usr1, NULL );
340             sigaction( SIGUSR2, &p_vout->p_sys->sig_usr2, NULL );
341             tcsetattr(0, 0, &p_vout->p_sys->old_termios);
342             TextMode( p_sys->i_tty );
343             free( p_vout->p_sys );
344             return VLC_EGENERIC;
345         }
346         memcpy( &vt_mode, &p_vout->p_sys->vt_mode, sizeof( vt_mode ) );
347         vt_mode.mode   = VT_PROCESS;
348         vt_mode.waitv  = 0;
349         vt_mode.relsig = SIGUSR1;
350         vt_mode.acqsig = SIGUSR2;
351
352         if( -1 == ioctl( p_sys->i_tty, VT_SETMODE, &vt_mode ) )
353         {
354             msg_Err( p_vout, "cannot set terminal mode (%m)" );
355             sigaction( SIGUSR1, &p_vout->p_sys->sig_usr1, NULL );
356             sigaction( SIGUSR2, &p_vout->p_sys->sig_usr2, NULL );
357             tcsetattr(0, 0, &p_vout->p_sys->old_termios);
358             TextMode( p_sys->i_tty );
359             free( p_vout->p_sys );
360             return VLC_EGENERIC;
361         }
362     }
363
364     if( OpenDisplay( p_vout ) )
365     {
366         Destroy( VLC_OBJECT(p_vout) );
367         return VLC_EGENERIC;
368     }
369
370     return VLC_SUCCESS;
371 }
372
373
374 /*****************************************************************************
375  * Destroy: destroy FB video thread output method
376  *****************************************************************************
377  * Terminate an output method created by Create
378  *****************************************************************************/
379 static void Destroy( vlc_object_t *p_this )
380 {
381     vout_thread_t *p_vout = (vout_thread_t *)p_this;
382
383     CloseDisplay( p_vout );
384
385     if( p_vout->p_sys->b_tty )
386     {
387         /* Reset the terminal */
388         ioctl( p_vout->p_sys->i_tty, VT_SETMODE, &p_vout->p_sys->vt_mode );
389
390         /* Remove signal handlers */
391         sigaction( SIGUSR1, &p_vout->p_sys->sig_usr1, NULL );
392         sigaction( SIGUSR2, &p_vout->p_sys->sig_usr2, NULL );
393
394         /* Reset the keyboard state */
395         tcsetattr( 0, 0, &p_vout->p_sys->old_termios );
396
397         /* Return to text mode */
398         TextMode( p_vout->p_sys->i_tty );
399     }
400
401     /* Destroy structure */
402     free( p_vout->p_sys );
403 }
404
405 /*****************************************************************************
406  * NewPicture: allocate a picture
407  *****************************************************************************
408  * Returns 0 on success, -1 otherwise
409  *****************************************************************************/
410 static int NewPicture( vout_thread_t *p_vout, picture_t *p_pic )
411 {
412     /* We know the chroma, allocate a buffer which will be used
413      * directly by the decoder */
414     p_pic->p_sys = malloc( sizeof( picture_sys_t ) );
415     if( p_pic->p_sys == NULL )
416     {
417         return VLC_ENOMEM;
418     }
419
420     /* Fill in picture_t fields */
421     vout_InitPicture( VLC_OBJECT(p_vout), p_pic, p_vout->output.i_chroma,
422                       p_vout->output.i_width, p_vout->output.i_height,
423                       p_vout->output.i_aspect );
424
425     p_pic->p_sys->p_data = malloc( p_vout->p_sys->i_page_size );
426     if( !p_pic->p_sys->p_data )
427     {
428         free( p_pic->p_sys );
429         p_pic->p_sys = NULL;
430         return VLC_ENOMEM;
431     }
432
433     p_pic->p->p_pixels = (uint8_t*) p_pic->p_sys->p_data;
434
435     p_pic->p->i_pixel_pitch = p_vout->p_sys->i_bytes_per_pixel;
436     p_pic->p->i_lines = p_vout->p_sys->var_info.yres;
437     p_pic->p->i_visible_lines = p_vout->p_sys->var_info.yres;
438
439     if( p_vout->p_sys->var_info.xres_virtual )
440     {
441         p_pic->p->i_pitch = p_vout->p_sys->var_info.xres_virtual
442                              * p_vout->p_sys->i_bytes_per_pixel;
443     }
444     else
445     {
446         p_pic->p->i_pitch = p_vout->p_sys->var_info.xres
447                              * p_vout->p_sys->i_bytes_per_pixel;
448     }
449
450     p_pic->p->i_visible_pitch = p_vout->p_sys->var_info.xres
451                                  * p_vout->p_sys->i_bytes_per_pixel;
452     p_pic->i_planes = 1;
453
454     return VLC_SUCCESS;
455 }
456
457 /*****************************************************************************
458  * FreePicture: destroy a picture allocated with NewPicture
459  *****************************************************************************
460  * Destroy Image AND associated data.
461  *****************************************************************************/
462 static void FreePicture( vout_thread_t *p_vout, picture_t *p_pic )
463 {
464     VLC_UNUSED(p_vout);
465     free( p_pic->p_sys->p_data );
466     free( p_pic->p_sys );
467     p_pic->p_sys = NULL;
468 }
469
470 /*****************************************************************************
471  * Init: initialize framebuffer video thread output method
472  *****************************************************************************/
473 static int Init( vout_thread_t *p_vout )
474 {
475     vout_sys_t *p_sys = p_vout->p_sys;
476     int i_index;
477     picture_t *p_pic = NULL;
478
479     I_OUTPUTPICTURES = 0;
480
481     p_vout->output.i_width  = p_vout->render.i_width;
482     p_vout->output.i_height = p_vout->render.i_height;
483     p_vout->output.i_aspect = p_vout->render.i_aspect;
484
485     p_vout->fmt_out = p_vout->fmt_in;
486     if( p_sys->i_chroma == 0 )
487     {
488         /* Initialize the output structure: RGB with square pixels, whatever
489          * the input format is, since it's the only format we know */
490         switch( p_sys->var_info.bits_per_pixel )
491         {
492         case 8: /* FIXME: set the palette */
493             p_vout->output.i_chroma = VLC_FOURCC('R','G','B','2'); break;
494         case 15:
495             p_vout->output.i_chroma = VLC_FOURCC('R','V','1','5'); break;
496         case 16:
497             p_vout->output.i_chroma = VLC_FOURCC('R','V','1','6'); break;
498         case 24:
499             p_vout->output.i_chroma = VLC_FOURCC('R','V','2','4'); break;
500         case 32:
501             p_vout->output.i_chroma = VLC_FOURCC('R','V','3','2'); break;
502         default:
503             msg_Err( p_vout, "unknown screen depth %i",
504                      p_vout->p_sys->var_info.bits_per_pixel );
505             return VLC_EGENERIC;
506         }
507
508         if( p_sys->var_info.bits_per_pixel != 8 )
509         {
510             p_vout->output.i_rmask = ( (1 << p_sys->var_info.red.length) - 1 )
511                                  << p_sys->var_info.red.offset;
512             p_vout->output.i_gmask = ( (1 << p_sys->var_info.green.length) - 1 )
513                                  << p_sys->var_info.green.offset;
514             p_vout->output.i_bmask = ( (1 << p_sys->var_info.blue.length) - 1 )
515                                  << p_sys->var_info.blue.offset;
516         }
517     }
518     else
519     {
520         p_vout->output.i_chroma = p_sys->i_chroma;
521     }
522     p_vout->fmt_out.i_chroma = p_vout->output.i_chroma;
523
524     p_vout->output.i_width =
525     p_vout->fmt_out.i_width =
526     p_vout->fmt_out.i_visible_width = p_sys->i_width;
527     p_vout->output.i_height =
528     p_vout->fmt_out.i_height =
529     p_vout->fmt_out.i_visible_height = p_sys->i_height;
530
531     /* Assume we have square pixels */
532     if( p_sys->i_aspect < 0 )
533     {
534         p_vout->output.i_aspect = ( p_sys->i_width
535                                   * VOUT_ASPECT_FACTOR ) / p_sys->i_height;
536     }
537     else p_vout->output.i_aspect = p_sys->i_aspect;
538
539     p_vout->fmt_out.i_sar_num = p_vout->fmt_out.i_sar_den = 1;
540     p_vout->fmt_out.i_aspect  = p_vout->render.i_aspect = p_vout->output.i_aspect;
541     p_vout->fmt_out.i_x_offset= p_vout->fmt_out.i_y_offset = 0;
542
543     /* Clear the screen */
544     memset( p_sys->p_video, 0, p_sys->i_page_size );
545
546     if( !p_sys->b_hw_accel )
547     {
548         /* Try to initialize up to MAX_DIRECTBUFFERS direct buffers */
549         while( I_OUTPUTPICTURES < MAX_DIRECTBUFFERS )
550         {
551             p_pic = NULL;
552
553             /* Find an empty picture slot */
554             for( i_index = 0 ; i_index < VOUT_MAX_PICTURES ; i_index++ )
555             {
556                 if( p_vout->p_picture[ i_index ].i_status == FREE_PICTURE )
557                 {
558                     p_pic = p_vout->p_picture + i_index;
559                     break;
560                 }
561             }
562
563             /* Allocate the picture */
564             if( p_pic == NULL || NewPicture( p_vout, p_pic ) )
565             {
566                 break;
567             }
568
569             p_pic->i_status = DESTROYED_PICTURE;
570             p_pic->i_type   = DIRECT_PICTURE;
571
572             PP_OUTPUTPICTURE[ I_OUTPUTPICTURES ] = p_pic;
573
574             I_OUTPUTPICTURES++;
575         }
576     }
577     else
578     {
579         /* Try to initialize 1 direct buffer */
580         p_pic = NULL;
581
582         /* Find an empty picture slot */
583         for( i_index = 0 ; i_index < VOUT_MAX_PICTURES ; i_index++ )
584         {
585             if( p_vout->p_picture[ i_index ].i_status == FREE_PICTURE )
586             {
587                 p_pic = p_vout->p_picture + i_index;
588                 break;
589             }
590         }
591
592         /* Allocate the picture */
593         if( p_pic == NULL )
594         {
595             return VLC_EGENERIC;
596         }
597
598         /* We know the chroma, allocate a buffer which will be used
599         * directly by the decoder */
600         p_pic->p->p_pixels = p_vout->p_sys->p_video;
601         p_pic->p->i_pixel_pitch = p_vout->p_sys->i_bytes_per_pixel;
602         p_pic->p->i_lines = p_vout->p_sys->var_info.yres;
603         p_pic->p->i_visible_lines = p_vout->p_sys->var_info.yres;
604
605         if( p_vout->p_sys->var_info.xres_virtual )
606         {
607             p_pic->p->i_pitch = p_vout->p_sys->var_info.xres_virtual
608                                 * p_vout->p_sys->i_bytes_per_pixel;
609         }
610         else
611         {
612             p_pic->p->i_pitch = p_vout->p_sys->var_info.xres
613                                 * p_vout->p_sys->i_bytes_per_pixel;
614         }
615
616         p_pic->p->i_visible_pitch = p_vout->p_sys->var_info.xres
617                                     * p_vout->p_sys->i_bytes_per_pixel;
618         p_pic->i_planes = 1;
619         p_pic->i_status = DESTROYED_PICTURE;
620         p_pic->i_type   = DIRECT_PICTURE;
621
622         PP_OUTPUTPICTURE[ I_OUTPUTPICTURES ] = p_pic;
623
624         I_OUTPUTPICTURES++;
625     }
626
627     return VLC_SUCCESS;
628 }
629
630 /*****************************************************************************
631  * End: terminate framebuffer video thread output method
632  *****************************************************************************/
633 static void End( vout_thread_t *p_vout )
634 {
635     if( !p_vout->p_sys->b_hw_accel )
636     {
637         int i_index;
638
639         /* Free the direct buffers we allocated */
640         for( i_index = I_OUTPUTPICTURES ; i_index ; )
641         {
642             i_index--;
643             FreePicture( p_vout, PP_OUTPUTPICTURE[ i_index ] );
644         }
645
646     }
647     /* Clear the screen */
648     memset( p_vout->p_sys->p_video, 0, p_vout->p_sys->i_page_size );
649 }
650
651 /*****************************************************************************
652  * Control: control facility for the vout
653  *****************************************************************************/
654 static int Control( vout_thread_t *p_vout, int i_query, va_list args )
655 {
656     (void) p_vout; (void) i_query; (void) args;
657     return VLC_EGENERIC;
658 }
659
660 /*****************************************************************************
661  * Manage: handle FB events
662  *****************************************************************************
663  * This function should be called regularly by video output thread. It manages
664  * console events. It returns a non null value on error.
665  *****************************************************************************/
666 static int Manage( vout_thread_t *p_vout )
667 {
668 #if 0
669     uint8_t buf;
670
671     if ( read(0, &buf, 1) == 1)
672     {
673         switch( buf )
674         {
675         case 'q':
676             libvlc_Quit( p_vout->p_libvlc );
677             break;
678
679         default:
680             break;
681         }
682     }
683 #endif
684
685     /*
686      * Size change
687      */
688     if( p_vout->i_changes & VOUT_SIZE_CHANGE )
689     {
690         msg_Dbg( p_vout, "reinitializing framebuffer screen" );
691         p_vout->i_changes &= ~VOUT_SIZE_CHANGE;
692
693         /* Destroy XImages to change their size */
694         End( p_vout );
695
696         /* Recreate XImages. If SysInit failed, the thread can't go on. */
697         if( Init( p_vout ) )
698         {
699             msg_Err( p_vout, "cannot reinit framebuffer screen" );
700             return VLC_EGENERIC;
701         }
702
703         /* Clear screen */
704         memset( p_vout->p_sys->p_video, 0, p_vout->p_sys->i_page_size );
705
706 #if 0
707         /* Tell the video output thread that it will need to rebuild YUV
708          * tables. This is needed since conversion buffer size may have changed */
709         p_vout->i_changes |= VOUT_YUV_CHANGE;
710 #endif
711     }
712
713     return VLC_SUCCESS;
714 }
715
716 /*****************************************************************************
717  * Display: displays previously rendered output
718  *****************************************************************************
719  * This function send the currently rendered image to FB image, waits until
720  * it is displayed and switch the two rendering buffers, preparing next frame.
721  *****************************************************************************/
722 static void Display( vout_thread_t *p_vout, picture_t *p_pic )
723 {
724 static int panned=0;
725     /* swap the two Y offsets if the drivers supports panning */
726     if( p_vout->p_sys->b_pan )
727     {
728         p_vout->p_sys->var_info.yoffset = 0;
729         /*p_vout->p_sys->var_info.yoffset = p_vout->p_sys->var_info.yres; */
730
731         /* the X offset should be 0, but who knows ...
732          * some other app might have played with the framebuffer */
733         p_vout->p_sys->var_info.xoffset = 0;
734
735         if( panned < 0 )
736         {
737             ioctl( p_vout->p_sys->i_fd,
738                    FBIOPAN_DISPLAY, &p_vout->p_sys->var_info );
739             panned++;
740         }
741     }
742
743     if( !p_vout->p_sys->b_hw_accel )
744     {
745         vlc_memcpy( p_vout->p_sys->p_video, p_pic->p->p_pixels,
746                     p_vout->p_sys->i_page_size );
747     }
748 }
749
750 #if 0
751 static void SetPalette( vout_thread_t *p_vout, uint16_t *red, uint16_t *green,
752                                                uint16_t *blue, uint16_t *transp )
753 {
754     struct fb_cmap cmap = { 0, 256, red, green, blue, transp };
755
756     ioctl( p_vout->p_sys->i_fd, FBIOPUTCMAP, &cmap );
757 }
758 #endif
759
760 /* following functions are local */
761
762 /*****************************************************************************
763  * OpenDisplay: initialize framebuffer
764  *****************************************************************************/
765 static int OpenDisplay( vout_thread_t *p_vout )
766 {
767     vout_sys_t *p_sys = (vout_sys_t *) p_vout->p_sys;
768     char *psz_device;                             /* framebuffer device path */
769     struct fb_fix_screeninfo    fix_info;     /* framebuffer fix information */
770
771     /* Open framebuffer device */
772     if( !(psz_device = config_GetPsz( p_vout, FB_DEV_VAR )) )
773     {
774         msg_Err( p_vout, "don't know which fb device to open" );
775         return VLC_EGENERIC;
776     }
777
778     p_sys->i_fd = open( psz_device, O_RDWR);
779     if( p_sys->i_fd == -1 )
780     {
781         msg_Err( p_vout, "cannot open %s (%m)", psz_device );
782         free( psz_device );
783         return VLC_EGENERIC;
784     }
785     free( psz_device );
786     psz_device = NULL;
787
788     /* Get framebuffer device information */
789     if( ioctl( p_sys->i_fd, FBIOGET_VSCREENINFO, &p_sys->var_info ) )
790     {
791         msg_Err( p_vout, "cannot get fb info (%m)" );
792         close( p_sys->i_fd );
793         return VLC_EGENERIC;
794     }
795
796     memcpy( &p_sys->old_info, &p_sys->var_info,
797             sizeof( struct fb_var_screeninfo ) );
798
799     /* Get some info on the framebuffer itself */
800     if( !p_sys->b_auto )
801     {
802         p_sys->var_info.xres = p_sys->var_info.xres_virtual = p_sys->i_width;
803         p_sys->var_info.yres = p_sys->var_info.yres_virtual = p_sys->i_height;
804         p_vout->fmt_out.i_width = p_sys->i_width;
805         p_vout->fmt_out.i_height = p_sys->i_height;
806     }
807
808     /* Set some attributes */
809     p_sys->var_info.activate = p_sys->b_tty
810                                ? FB_ACTIVATE_NXTOPEN
811                                : FB_ACTIVATE_NOW;
812     p_sys->var_info.xoffset =  0;
813     p_sys->var_info.yoffset =  0;
814
815     if( ioctl( p_sys->i_fd, FBIOPUT_VSCREENINFO, &p_sys->var_info ) )
816     {
817         msg_Err( p_vout, "cannot set fb info (%m)" );
818         close( p_sys->i_fd );
819         return VLC_EGENERIC;
820     }
821
822     /* Get some information again, in the definitive configuration */
823     if( ioctl( p_sys->i_fd, FBIOGET_FSCREENINFO, &fix_info )
824          || ioctl( p_sys->i_fd, FBIOGET_VSCREENINFO, &p_sys->var_info ) )
825     {
826         msg_Err( p_vout, "cannot get additional fb info (%m)" );
827
828         /* Restore fb config */
829         ioctl( p_sys->i_fd, FBIOPUT_VSCREENINFO, &p_sys->old_info );
830
831         close( p_sys->i_fd );
832         return VLC_EGENERIC;
833     }
834
835     /* If the fb has limitations on mode change,
836      * then keep the resolution of the fb */
837     if( (p_sys->i_height != p_sys->var_info.yres) ||
838         (p_sys->i_width != p_sys->var_info.xres) )
839     {
840         p_sys->b_auto = true;
841         msg_Warn( p_vout,
842                   "using framebuffer native resolution instead of requested (%ix%i)",
843                   p_sys->i_width, p_sys->i_height );
844     }
845     p_sys->i_height = p_sys->var_info.yres;
846     p_sys->i_width  = p_sys->var_info.xres_virtual
847                                ? p_sys->var_info.xres_virtual
848                                : p_sys->var_info.xres;
849
850     /* FIXME: if the image is full-size, it gets cropped on the left
851      * because of the xres / xres_virtual slight difference */
852     msg_Dbg( p_vout, "%ix%i (virtual %ix%i) (request %ix%i)",
853              p_sys->var_info.xres, p_sys->var_info.yres,
854              p_sys->var_info.xres_virtual,
855              p_sys->var_info.yres_virtual,
856              p_sys->i_width, p_sys->i_height );
857
858     p_sys->p_palette = NULL;
859     p_sys->b_pan = ( fix_info.ypanstep || fix_info.ywrapstep );
860
861     switch( p_sys->var_info.bits_per_pixel )
862     {
863     case 8:
864         p_sys->p_palette = malloc( 8 * 256 * sizeof( uint16_t ) );
865         if( !p_sys->p_palette )
866         {
867             /* Restore fb config */
868             ioctl( p_sys->i_fd, FBIOPUT_VSCREENINFO, &p_sys->old_info );
869
870             close( p_sys->i_fd );
871             return VLC_ENOMEM;
872         }
873         p_sys->fb_cmap.start = 0;
874         p_sys->fb_cmap.len = 256;
875         p_sys->fb_cmap.red = p_sys->p_palette;
876         p_sys->fb_cmap.green = p_sys->p_palette + 256 * sizeof( uint16_t );
877         p_sys->fb_cmap.blue = p_sys->p_palette + 2 * 256 * sizeof( uint16_t );
878         p_sys->fb_cmap.transp = p_sys->p_palette + 3 * 256 * sizeof( uint16_t );
879
880         /* Save the colormap */
881         ioctl( p_sys->i_fd, FBIOGETCMAP, &p_sys->fb_cmap );
882
883         p_sys->i_bytes_per_pixel = 1;
884         break;
885
886     case 15:
887     case 16:
888         p_sys->i_bytes_per_pixel = 2;
889         break;
890
891     case 24:
892         p_sys->i_bytes_per_pixel = 3;
893         break;
894
895     case 32:
896         p_sys->i_bytes_per_pixel = 4;
897         break;
898
899     default:
900         msg_Err( p_vout, "screen depth %d is not supported",
901                          p_sys->var_info.bits_per_pixel );
902
903         /* Restore fb config */
904         ioctl( p_sys->i_fd, FBIOPUT_VSCREENINFO, &p_sys->old_info );
905
906         close( p_sys->i_fd );
907         return VLC_EGENERIC;
908     }
909
910     p_sys->i_page_size = p_sys->i_width * p_sys->i_height *
911                          p_sys->i_bytes_per_pixel;
912
913     /* Map a framebuffer at the beginning */
914     p_sys->p_video = mmap( NULL, p_sys->i_page_size,
915                               PROT_READ | PROT_WRITE, MAP_SHARED,
916                               p_sys->i_fd, 0 );
917
918     if( p_sys->p_video == MAP_FAILED )
919     {
920         msg_Err( p_vout, "cannot map video memory (%m)" );
921
922         if( p_sys->var_info.bits_per_pixel == 8 )
923         {
924             free( p_sys->p_palette );
925             p_sys->p_palette = NULL;
926         }
927
928         /* Restore fb config */
929         ioctl( p_sys->i_fd, FBIOPUT_VSCREENINFO, &p_vout->p_sys->old_info );
930
931         close( p_sys->i_fd );
932         return VLC_EGENERIC;
933     }
934
935     msg_Dbg( p_vout, "framebuffer type=%d, visual=%d, ypanstep=%d, "
936              "ywrap=%d, accel=%d", fix_info.type, fix_info.visual,
937              fix_info.ypanstep, fix_info.ywrapstep, fix_info.accel );
938     return VLC_SUCCESS;
939 }
940
941 /*****************************************************************************
942  * CloseDisplay: terminate FB video thread output method
943  *****************************************************************************/
944 static void CloseDisplay( vout_thread_t *p_vout )
945 {
946     if( p_vout->p_sys->p_video != MAP_FAILED )
947     {
948         /* Clear display */
949         memset( p_vout->p_sys->p_video, 0, p_vout->p_sys->i_page_size );
950         munmap( p_vout->p_sys->p_video, p_vout->p_sys->i_page_size );
951     }
952
953     if( p_vout->p_sys->i_fd >= 0 )
954     {
955         /* Restore palette */
956         if( p_vout->p_sys->var_info.bits_per_pixel == 8 )
957         {
958             ioctl( p_vout->p_sys->i_fd,
959                    FBIOPUTCMAP, &p_vout->p_sys->fb_cmap );
960             free( p_vout->p_sys->p_palette );
961             p_vout->p_sys->p_palette = NULL;
962         }
963
964         /* Restore fb config */
965         ioctl( p_vout->p_sys->i_fd,
966                FBIOPUT_VSCREENINFO, &p_vout->p_sys->old_info );
967
968         /* Close fb */
969         close( p_vout->p_sys->i_fd );
970     }
971 }
972
973 /*****************************************************************************
974  * SwitchDisplay: VT change signal handler
975  *****************************************************************************
976  * This function activates or deactivates the output of the thread. It is
977  * called by the VT driver, on terminal change.
978  *****************************************************************************/
979 static void SwitchDisplay( int i_signal )
980 {
981     VLC_UNUSED( i_signal );
982 #if 0
983     vout_thread_t *p_vout;
984
985     vlc_mutex_lock( &p_vout_bank->lock );
986
987     /* XXX: only test the first video output */
988     if( p_vout_bank->i_count )
989     {
990         p_vout = p_vout_bank->pp_vout[0];
991
992         switch( i_signal )
993         {
994         case SIGUSR1:                                /* vt has been released */
995             p_vout->b_active = 0;
996             ioctl( p_sys->i_tty, VT_RELDISP, 1 );
997             break;
998         case SIGUSR2:                                /* vt has been acquired */
999             p_vout->b_active = 1;
1000             ioctl( p_sys->i_tty, VT_RELDISP, VT_ACTIVATE );
1001             /* handle blanking */
1002             vlc_mutex_lock( &p_vout->change_lock );
1003             p_vout->i_changes |= VOUT_SIZE_CHANGE;
1004             vlc_mutex_unlock( &p_vout->change_lock );
1005             break;
1006         }
1007     }
1008
1009     vlc_mutex_unlock( &p_vout_bank->lock );
1010 #endif
1011 }
1012
1013 /*****************************************************************************
1014  * TextMode and GfxMode : switch tty to text/graphic mode
1015  *****************************************************************************
1016  * These functions toggle the tty mode.
1017  *****************************************************************************/
1018 static void TextMode( int i_tty )
1019 {
1020     /* return to text mode */
1021     if( -1 == ioctl(i_tty, KDSETMODE, KD_TEXT) )
1022     {
1023         /*msg_Err( p_vout, "failed ioctl KDSETMODE KD_TEXT" );*/
1024     }
1025 }
1026
1027 static void GfxMode( int i_tty )
1028 {
1029     /* switch to graphic mode */
1030     if( -1 == ioctl(i_tty, KDSETMODE, KD_GRAPHICS) )
1031     {
1032         /*msg_Err( p_vout, "failed ioctl KDSETMODE KD_GRAPHICS" );*/
1033     }
1034 }