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