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