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