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