1 /*****************************************************************************
2 * fb.c : framebuffer plugin for vlc
3 *****************************************************************************
4 * Copyright (C) 2000, 2001 VideoLAN
5 * $Id: fb.c,v 1.6 2003/04/20 21:15:31 sam Exp $
7 * Authors: Samuel Hocevar <sam@zoy.org>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
27 #include <errno.h> /* ENOMEM */
28 #include <signal.h> /* SIGUSR1, SIGUSR2 */
29 #include <stdlib.h> /* free() */
30 #include <string.h> /* strerror() */
31 #include <fcntl.h> /* open() */
32 #include <unistd.h> /* close() */
34 #include <termios.h> /* struct termios */
35 #include <sys/ioctl.h>
36 #include <sys/mman.h> /* mmap() */
39 #include <linux/vt.h> /* VT_* */
40 #include <linux/kd.h> /* KD* */
45 /*****************************************************************************
47 *****************************************************************************/
48 static int Create ( vlc_object_t * );
49 static void Destroy ( vlc_object_t * );
51 static int Init ( vout_thread_t * );
52 static void End ( vout_thread_t * );
53 static int Manage ( vout_thread_t * );
54 static void Display ( vout_thread_t *, picture_t * );
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 );
62 /*****************************************************************************
64 *****************************************************************************/
65 #define FB_DEV_VAR "fbdev"
68 add_category_hint( N_("Frame Buffer"), NULL, VLC_FALSE );
69 add_file( FB_DEV_VAR, "/dev/fb0", NULL, N_("framebuffer device"), NULL, VLC_FALSE );
70 set_description( _("Linux console framebuffer video output") );
71 set_capability( "video output", 30 );
72 set_callbacks( Create, Destroy );
75 /*****************************************************************************
76 * vout_sys_t: video output framebuffer method descriptor
77 *****************************************************************************
78 * This structure is part of the video output thread descriptor.
79 * It describes the FB specific properties of an output thread.
80 *****************************************************************************/
83 /* System informations */
84 int i_tty; /* tty device handle */
85 struct termios old_termios;
87 /* Original configuration informations */
88 struct sigaction sig_usr1; /* USR1 previous handler */
89 struct sigaction sig_usr2; /* USR2 previous handler */
90 struct vt_mode vt_mode; /* previous VT mode */
92 /* Framebuffer information */
93 int i_fd; /* device handle */
94 struct fb_var_screeninfo old_info; /* original mode informations */
95 struct fb_var_screeninfo var_info; /* current mode informations */
96 vlc_bool_t b_pan; /* does device supports panning ? */
97 struct fb_cmap fb_cmap; /* original colormap */
98 uint16_t *p_palette; /* original palette */
100 /* Video information */
103 int i_bytes_per_pixel;
106 byte_t * p_video; /* base adress */
107 size_t i_page_size; /* page size */
110 /*****************************************************************************
111 * Create: allocates FB video thread output method
112 *****************************************************************************
113 * This function allocates and initializes a FB vout method.
114 *****************************************************************************/
115 static int Create( vlc_object_t *p_this )
117 vout_thread_t *p_vout = (vout_thread_t *)p_this;
119 struct sigaction sig_tty; /* sigaction for tty change */
120 struct vt_mode vt_mode; /* vt current mode */
121 struct termios new_termios;
123 /* Allocate instance and initialize some members */
124 p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
125 if( p_vout->p_sys == NULL )
130 p_vout->pf_init = Init;
131 p_vout->pf_end = End;
132 p_vout->pf_manage = Manage;
133 p_vout->pf_render = NULL;
134 p_vout->pf_display = Display;
136 /* Set tty and fb devices */
137 p_vout->p_sys->i_tty = 0; /* 0 == /dev/tty0 == current console */
139 GfxMode( p_vout->p_sys->i_tty );
141 /* Set keyboard settings */
142 if (tcgetattr(0, &p_vout->p_sys->old_termios) == -1)
144 msg_Err( p_vout, "tcgetattr failed" );
147 if (tcgetattr(0, &new_termios) == -1)
149 msg_Err( p_vout, "tcgetattr failed" );
152 /* new_termios.c_lflag &= ~ (ICANON | ISIG);
153 new_termios.c_lflag |= (ECHO | ECHOCTL); */
154 new_termios.c_lflag &= ~ (ICANON);
155 new_termios.c_lflag &= ~(ECHO | ECHOCTL);
156 new_termios.c_iflag = 0;
157 new_termios.c_cc[VMIN] = 1;
158 new_termios.c_cc[VTIME] = 0;
160 if (tcsetattr(0, TCSAFLUSH, &new_termios) == -1)
162 msg_Err( p_vout, "tcsetattr failed" );
165 ioctl( p_vout->p_sys->i_tty, VT_RELDISP, VT_ACKACQ );
167 /* Set-up tty signal handler to be aware of tty changes */
168 memset( &sig_tty, 0, sizeof( sig_tty ) );
169 sig_tty.sa_handler = SwitchDisplay;
170 sigemptyset( &sig_tty.sa_mask );
171 if( sigaction( SIGUSR1, &sig_tty, &p_vout->p_sys->sig_usr1 ) ||
172 sigaction( SIGUSR2, &sig_tty, &p_vout->p_sys->sig_usr2 ) )
174 msg_Err( p_vout, "cannot set signal handler (%s)", strerror(errno) );
175 tcsetattr(0, 0, &p_vout->p_sys->old_termios);
176 TextMode( p_vout->p_sys->i_tty );
177 free( p_vout->p_sys );
181 /* Set-up tty according to new signal handler */
182 if( -1 == ioctl( p_vout->p_sys->i_tty,
183 VT_GETMODE, &p_vout->p_sys->vt_mode ) )
185 msg_Err( p_vout, "cannot get terminal mode (%s)", strerror(errno) );
186 sigaction( SIGUSR1, &p_vout->p_sys->sig_usr1, NULL );
187 sigaction( SIGUSR2, &p_vout->p_sys->sig_usr2, NULL );
188 tcsetattr(0, 0, &p_vout->p_sys->old_termios);
189 TextMode( p_vout->p_sys->i_tty );
190 free( p_vout->p_sys );
193 memcpy( &vt_mode, &p_vout->p_sys->vt_mode, sizeof( vt_mode ) );
194 vt_mode.mode = VT_PROCESS;
196 vt_mode.relsig = SIGUSR1;
197 vt_mode.acqsig = SIGUSR2;
199 if( -1 == ioctl( p_vout->p_sys->i_tty, VT_SETMODE, &vt_mode ) )
201 msg_Err( p_vout, "cannot set terminal mode (%s)", strerror(errno) );
202 sigaction( SIGUSR1, &p_vout->p_sys->sig_usr1, NULL );
203 sigaction( SIGUSR2, &p_vout->p_sys->sig_usr2, NULL );
204 tcsetattr(0, 0, &p_vout->p_sys->old_termios);
205 TextMode( p_vout->p_sys->i_tty );
206 free( p_vout->p_sys );
210 if( OpenDisplay( p_vout ) )
212 ioctl( p_vout->p_sys->i_tty, VT_SETMODE, &p_vout->p_sys->vt_mode );
213 sigaction( SIGUSR1, &p_vout->p_sys->sig_usr1, NULL );
214 sigaction( SIGUSR2, &p_vout->p_sys->sig_usr2, NULL );
215 tcsetattr(0, 0, &p_vout->p_sys->old_termios);
216 TextMode( p_vout->p_sys->i_tty );
217 free( p_vout->p_sys );
224 /*****************************************************************************
225 * Init: initialize framebuffer video thread output method
226 *****************************************************************************/
227 static int Init( vout_thread_t *p_vout )
232 I_OUTPUTPICTURES = 0;
234 /* Initialize the output structure: RGB with square pixels, whatever
235 * the input format is, since it's the only format we know */
236 switch( p_vout->p_sys->var_info.bits_per_pixel )
238 case 8: /* FIXME: set the palette */
239 p_vout->output.i_chroma = VLC_FOURCC('R','G','B','2'); break;
241 p_vout->output.i_chroma = VLC_FOURCC('R','V','1','5'); break;
243 p_vout->output.i_chroma = VLC_FOURCC('R','V','1','6'); break;
245 p_vout->output.i_chroma = VLC_FOURCC('R','V','2','4'); break;
247 p_vout->output.i_chroma = VLC_FOURCC('R','V','3','2'); break;
249 msg_Err( p_vout, "unknown screen depth %i",
250 p_vout->p_sys->var_info.bits_per_pixel );
254 /* Only useful for p_vout->p_sys->var_info.bits_per_pixel != 8 */
255 p_vout->output.i_rmask = ( (1 << p_vout->p_sys->var_info.red.length) - 1 )
256 << p_vout->p_sys->var_info.red.offset;
257 p_vout->output.i_gmask = ( (1 << p_vout->p_sys->var_info.green.length) - 1 )
258 << p_vout->p_sys->var_info.green.offset;
259 p_vout->output.i_bmask = ( (1 << p_vout->p_sys->var_info.blue.length) - 1 )
260 << p_vout->p_sys->var_info.blue.offset;
262 p_vout->output.i_width = p_vout->p_sys->i_width;
263 p_vout->output.i_height = p_vout->p_sys->i_height;
265 /* Assume we have square pixels */
266 p_vout->output.i_aspect = p_vout->p_sys->i_width
267 * VOUT_ASPECT_FACTOR / p_vout->p_sys->i_height;
269 /* Clear the screen */
270 memset( p_vout->p_sys->p_video, 0, p_vout->p_sys->i_page_size );
272 /* Try to initialize 1 direct buffer */
275 /* Find an empty picture slot */
276 for( i_index = 0 ; i_index < VOUT_MAX_PICTURES ; i_index++ )
278 if( p_vout->p_picture[ i_index ].i_status == FREE_PICTURE )
280 p_pic = p_vout->p_picture + i_index;
285 /* Allocate the picture */
291 /* We know the chroma, allocate a buffer which will be used
292 * directly by the decoder */
293 p_pic->p->p_pixels = p_vout->p_sys->p_video;
294 p_pic->p->i_pixel_pitch = p_vout->p_sys->i_bytes_per_pixel;
295 p_pic->p->i_lines = p_vout->p_sys->var_info.yres;
297 if( p_vout->p_sys->var_info.xres_virtual )
299 p_pic->p->i_pitch = p_vout->p_sys->var_info.xres_virtual
300 * p_vout->p_sys->i_bytes_per_pixel;
304 p_pic->p->i_pitch = p_vout->p_sys->var_info.xres
305 * p_vout->p_sys->i_bytes_per_pixel;
308 p_pic->p->i_visible_pitch = p_vout->p_sys->var_info.xres
309 * p_vout->p_sys->i_bytes_per_pixel;
313 p_pic->i_status = DESTROYED_PICTURE;
314 p_pic->i_type = DIRECT_PICTURE;
316 PP_OUTPUTPICTURE[ I_OUTPUTPICTURES ] = p_pic;
323 /*****************************************************************************
324 * End: terminate framebuffer video thread output method
325 *****************************************************************************/
326 static void End( vout_thread_t *p_vout )
328 /* Clear the screen */
329 memset( p_vout->p_sys->p_video, 0, p_vout->p_sys->i_page_size );
332 /*****************************************************************************
333 * Destroy: destroy FB video thread output method
334 *****************************************************************************
335 * Terminate an output method created by Create
336 *****************************************************************************/
337 static void Destroy( vlc_object_t *p_this )
339 vout_thread_t *p_vout = (vout_thread_t *)p_this;
341 CloseDisplay( p_vout );
343 /* Reset the terminal */
344 ioctl( p_vout->p_sys->i_tty, VT_SETMODE, &p_vout->p_sys->vt_mode );
346 /* Remove signal handlers */
347 sigaction( SIGUSR1, &p_vout->p_sys->sig_usr1, NULL );
348 sigaction( SIGUSR2, &p_vout->p_sys->sig_usr2, NULL );
350 /* Reset the keyboard state */
351 tcsetattr( 0, 0, &p_vout->p_sys->old_termios );
353 /* Return to text mode */
354 TextMode( p_vout->p_sys->i_tty );
356 /* Destroy structure */
357 free( p_vout->p_sys );
360 /*****************************************************************************
361 * Manage: handle FB events
362 *****************************************************************************
363 * This function should be called regularly by video output thread. It manages
364 * console events. It returns a non null value on error.
365 *****************************************************************************/
366 static int Manage( vout_thread_t *p_vout )
371 if ( read(0, &buf, 1) == 1)
376 p_vout->p_vlc->b_die = 1;
388 if( p_vout->i_changes & VOUT_SIZE_CHANGE )
390 msg_Dbg( p_vout, "reinitializing framebuffer screen" );
391 p_vout->i_changes &= ~VOUT_SIZE_CHANGE;
393 /* Destroy XImages to change their size */
396 /* Recreate XImages. If SysInit failed, the thread can't go on. */
399 msg_Err( p_vout, "cannot reinit framebuffer screen" );
404 memset( p_vout->p_sys->p_video, 0, p_vout->p_sys->i_page_size );
407 /* Tell the video output thread that it will need to rebuild YUV
408 * tables. This is needed since conversion buffer size may have changed */
409 p_vout->i_changes |= VOUT_YUV_CHANGE;
416 /*****************************************************************************
417 * Display: displays previously rendered output
418 *****************************************************************************
419 * This function send the currently rendered image to FB image, waits until
420 * it is displayed and switch the two rendering buffers, preparing next frame.
421 *****************************************************************************/
422 static void Display( vout_thread_t *p_vout, picture_t *p_pic )
425 /* swap the two Y offsets if the drivers supports panning */
426 if( p_vout->p_sys->b_pan )
428 p_vout->p_sys->var_info.yoffset = 0;
429 /*p_vout->p_sys->var_info.yoffset = p_vout->p_sys->var_info.yres; */
431 /* the X offset should be 0, but who knows ...
432 * some other app might have played with the framebuffer */
433 p_vout->p_sys->var_info.xoffset = 0;
436 ioctl( p_vout->p_sys->i_fd,
437 FBIOPAN_DISPLAY, &p_vout->p_sys->var_info );
444 static void SetPalette( vout_thread_t *p_vout,
445 u16 *red, u16 *green, u16 *blue, u16 *transp )
447 struct fb_cmap cmap = { 0, 256, red, green, blue, transp };
449 ioctl( p_vout->p_sys->i_fd, FBIOPUTCMAP, &cmap );
453 /* following functions are local */
455 /*****************************************************************************
456 * OpenDisplay: initialize framebuffer
457 *****************************************************************************/
458 static int OpenDisplay( vout_thread_t *p_vout )
460 char *psz_device; /* framebuffer device path */
461 struct fb_fix_screeninfo fix_info; /* framebuffer fix information */
463 /* Open framebuffer device */
464 if( !(psz_device = config_GetPsz( p_vout, FB_DEV_VAR )) )
466 msg_Err( p_vout, "don't know which fb device to open" );
470 p_vout->p_sys->i_fd = open( psz_device, O_RDWR);
472 if( p_vout->p_sys->i_fd == -1 )
474 msg_Err( p_vout, "cannot open %s (%s)", psz_device, strerror(errno) );
480 /* Get framebuffer device informations */
481 if( ioctl( p_vout->p_sys->i_fd,
482 FBIOGET_VSCREENINFO, &p_vout->p_sys->var_info ) )
484 msg_Err( p_vout, "cannot get fb info (%s)", strerror(errno) );
485 close( p_vout->p_sys->i_fd );
489 memcpy( &p_vout->p_sys->old_info, &p_vout->p_sys->var_info,
490 sizeof( struct fb_var_screeninfo ) );
492 /* Set some attributes */
493 p_vout->p_sys->var_info.activate = FB_ACTIVATE_NXTOPEN;
494 p_vout->p_sys->var_info.xoffset = 0;
495 p_vout->p_sys->var_info.yoffset = 0;
497 if( ioctl( p_vout->p_sys->i_fd,
498 FBIOPUT_VSCREENINFO, &p_vout->p_sys->var_info ) )
500 msg_Err( p_vout, "cannot set fb info (%s)", strerror(errno) );
501 close( p_vout->p_sys->i_fd );
505 /* Get some informations again, in the definitive configuration */
506 if( ioctl( p_vout->p_sys->i_fd, FBIOGET_FSCREENINFO, &fix_info )
507 || ioctl( p_vout->p_sys->i_fd,
508 FBIOGET_VSCREENINFO, &p_vout->p_sys->var_info ) )
510 msg_Err( p_vout, "cannot get additional fb info (%s)",
513 /* Restore fb config */
514 ioctl( p_vout->p_sys->i_fd,
515 FBIOPUT_VSCREENINFO, &p_vout->p_sys->old_info );
517 close( p_vout->p_sys->i_fd );
521 /* FIXME: if the image is full-size, it gets cropped on the left
522 * because of the xres / xres_virtual slight difference */
523 msg_Dbg( p_vout, "%ix%i (virtual %ix%i)",
524 p_vout->p_sys->var_info.xres, p_vout->p_sys->var_info.yres,
525 p_vout->p_sys->var_info.xres_virtual,
526 p_vout->p_sys->var_info.yres_virtual );
528 p_vout->p_sys->i_height = p_vout->p_sys->var_info.yres;
529 p_vout->p_sys->i_width = p_vout->p_sys->var_info.xres_virtual
530 ? p_vout->p_sys->var_info.xres_virtual
531 : p_vout->p_sys->var_info.xres;
533 p_vout->p_sys->p_palette = NULL;
534 p_vout->p_sys->b_pan = ( fix_info.ypanstep || fix_info.ywrapstep );
536 switch( p_vout->p_sys->var_info.bits_per_pixel )
539 p_vout->p_sys->p_palette = malloc( 8 * 256 * sizeof( u16 ) );
540 p_vout->p_sys->fb_cmap.start = 0;
541 p_vout->p_sys->fb_cmap.len = 256;
542 p_vout->p_sys->fb_cmap.red = p_vout->p_sys->p_palette;
543 p_vout->p_sys->fb_cmap.green = p_vout->p_sys->p_palette + 256 * sizeof( u16 );
544 p_vout->p_sys->fb_cmap.blue = p_vout->p_sys->p_palette + 2 * 256 * sizeof( u16 );
545 p_vout->p_sys->fb_cmap.transp = p_vout->p_sys->p_palette + 3 * 256 * sizeof( u16 );
547 /* Save the colormap */
548 ioctl( p_vout->p_sys->i_fd, FBIOGETCMAP, &p_vout->p_sys->fb_cmap );
550 p_vout->p_sys->i_bytes_per_pixel = 1;
555 p_vout->p_sys->i_bytes_per_pixel = 2;
559 p_vout->p_sys->i_bytes_per_pixel = 3;
563 p_vout->p_sys->i_bytes_per_pixel = 4;
567 msg_Err( p_vout, "screen depth %d is not supported",
568 p_vout->p_sys->var_info.bits_per_pixel );
570 /* Restore fb config */
571 ioctl( p_vout->p_sys->i_fd,
572 FBIOPUT_VSCREENINFO, &p_vout->p_sys->old_info );
574 close( p_vout->p_sys->i_fd );
578 p_vout->p_sys->i_page_size = p_vout->p_sys->i_width *
579 p_vout->p_sys->i_height * p_vout->p_sys->i_bytes_per_pixel;
581 /* Map a framebuffer at the beginning */
582 p_vout->p_sys->p_video = mmap( 0, p_vout->p_sys->i_page_size,
583 PROT_READ | PROT_WRITE, MAP_SHARED,
584 p_vout->p_sys->i_fd, 0 );
586 if( p_vout->p_sys->p_video == ((void*)-1) )
588 msg_Err( p_vout, "cannot map video memory (%s)", strerror(errno) );
590 if( p_vout->p_sys->var_info.bits_per_pixel == 8 )
592 free( p_vout->p_sys->p_palette );
595 /* Restore fb config */
596 ioctl( p_vout->p_sys->i_fd,
597 FBIOPUT_VSCREENINFO, &p_vout->p_sys->old_info );
599 close( p_vout->p_sys->i_fd );
603 msg_Dbg( p_vout, "framebuffer type=%d, visual=%d, ypanstep=%d, "
604 "ywrap=%d, accel=%d", fix_info.type, fix_info.visual,
605 fix_info.ypanstep, fix_info.ywrapstep, fix_info.accel );
609 /*****************************************************************************
610 * CloseDisplay: terminate FB video thread output method
611 *****************************************************************************/
612 static void CloseDisplay( vout_thread_t *p_vout )
615 memset( p_vout->p_sys->p_video, 0, p_vout->p_sys->i_page_size );
617 /* Restore palette */
618 if( p_vout->p_sys->var_info.bits_per_pixel == 8 )
620 ioctl( p_vout->p_sys->i_fd,
621 FBIOPUTCMAP, &p_vout->p_sys->fb_cmap );
622 free( p_vout->p_sys->p_palette );
625 /* Restore fb config */
626 ioctl( p_vout->p_sys->i_fd,
627 FBIOPUT_VSCREENINFO, &p_vout->p_sys->old_info );
630 close( p_vout->p_sys->i_fd );
633 /*****************************************************************************
634 * SwitchDisplay: VT change signal handler
635 *****************************************************************************
636 * This function activates or deactivates the output of the thread. It is
637 * called by the VT driver, on terminal change.
638 *****************************************************************************/
639 static void SwitchDisplay(int i_signal)
642 vout_thread_t *p_vout;
644 vlc_mutex_lock( &p_vout_bank->lock );
646 /* XXX: only test the first video output */
647 if( p_vout_bank->i_count )
649 p_vout = p_vout_bank->pp_vout[0];
653 case SIGUSR1: /* vt has been released */
654 p_vout->b_active = 0;
655 ioctl( p_vout->p_sys->i_tty, VT_RELDISP, 1 );
657 case SIGUSR2: /* vt has been acquired */
658 p_vout->b_active = 1;
659 ioctl( p_vout->p_sys->i_tty, VT_RELDISP, VT_ACTIVATE );
660 /* handle blanking */
661 vlc_mutex_lock( &p_vout->change_lock );
662 p_vout->i_changes |= VOUT_SIZE_CHANGE;
663 vlc_mutex_unlock( &p_vout->change_lock );
668 vlc_mutex_unlock( &p_vout_bank->lock );
672 /*****************************************************************************
673 * TextMode and GfxMode : switch tty to text/graphic mode
674 *****************************************************************************
675 * These functions toggle the tty mode.
676 *****************************************************************************/
677 static void TextMode( int i_tty )
679 /* return to text mode */
680 if( -1 == ioctl(i_tty, KDSETMODE, KD_TEXT) )
682 /*msg_Err( p_vout, "failed ioctl KDSETMODE KD_TEXT" );*/
686 static void GfxMode( int i_tty )
688 /* switch to graphic mode */
689 if( -1 == ioctl(i_tty, KDSETMODE, KD_GRAPHICS) )
691 /*msg_Err( p_vout, "failed ioctl KDSETMODE KD_GRAPHICS" );*/