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