1 /*****************************************************************************
2 * fb.c : framebuffer plugin for vlc
3 *****************************************************************************
4 * Copyright (C) 2000, 2001 VideoLAN
5 * $Id: fb.c,v 1.1 2002/08/13 11:59:36 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_("Miscellaneous"), NULL );
69 add_string( FB_DEV_VAR, "/dev/fb0", NULL, N_("framebuffer device"), NULL );
70 set_description( _("Linux console framebuffer module") );
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 u16 *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 )
424 /* swap the two Y offsets if the drivers supports panning */
425 if( p_vout->p_sys->b_pan )
427 p_vout->p_sys->var_info.yoffset = 0;
428 /*p_vout->p_sys->var_info.yoffset = p_vout->p_sys->var_info.yres; */
430 /* the X offset should be 0, but who knows ...
431 * some other app might have played with the framebuffer */
432 p_vout->p_sys->var_info.xoffset = 0;
434 ioctl( p_vout->p_sys->i_fd,
435 FBIOPAN_DISPLAY, &p_vout->p_sys->var_info );
440 static void SetPalette( vout_thread_t *p_vout,
441 u16 *red, u16 *green, u16 *blue, u16 *transp )
443 struct fb_cmap cmap = { 0, 256, red, green, blue, transp };
445 ioctl( p_vout->p_sys->i_fd, FBIOPUTCMAP, &cmap );
449 /* following functions are local */
451 /*****************************************************************************
452 * OpenDisplay: initialize framebuffer
453 *****************************************************************************/
454 static int OpenDisplay( vout_thread_t *p_vout )
456 char *psz_device; /* framebuffer device path */
457 struct fb_fix_screeninfo fix_info; /* framebuffer fix information */
459 /* Open framebuffer device */
460 if( !(psz_device = config_GetPsz( p_vout, FB_DEV_VAR )) )
462 msg_Err( p_vout, "don't know which fb device to open" );
466 p_vout->p_sys->i_fd = open( psz_device, O_RDWR);
468 if( p_vout->p_sys->i_fd == -1 )
470 msg_Err( p_vout, "cannot open %s (%s)", psz_device, strerror(errno) );
476 /* Get framebuffer device informations */
477 if( ioctl( p_vout->p_sys->i_fd,
478 FBIOGET_VSCREENINFO, &p_vout->p_sys->var_info ) )
480 msg_Err( p_vout, "cannot get fb info (%s)", strerror(errno) );
481 close( p_vout->p_sys->i_fd );
485 if( ioctl( p_vout->p_sys->i_fd,
486 FBIOGET_VSCREENINFO, &p_vout->p_sys->old_info ) )
488 msg_Err( p_vout, "cannot get 2nd fb info (%s)", strerror(errno) );
489 close( p_vout->p_sys->i_fd );
493 /* Set some attributes */
494 p_vout->p_sys->var_info.activate = FB_ACTIVATE_NXTOPEN;
495 p_vout->p_sys->var_info.xoffset = 0;
496 p_vout->p_sys->var_info.yoffset = 0;
498 if( ioctl( p_vout->p_sys->i_fd,
499 FBIOPUT_VSCREENINFO, &p_vout->p_sys->var_info ) )
501 msg_Err( p_vout, "cannot set fb info (%s)", strerror(errno) );
502 close( p_vout->p_sys->i_fd );
506 /* Get some informations again, in the definitive configuration */
507 if( ioctl( p_vout->p_sys->i_fd, FBIOGET_FSCREENINFO, &fix_info )
508 || ioctl( p_vout->p_sys->i_fd,
509 FBIOGET_VSCREENINFO, &p_vout->p_sys->var_info ) )
511 msg_Err( p_vout, "cannot get additional fb info (%s)",
514 /* Restore fb config */
515 ioctl( p_vout->p_sys->i_fd,
516 FBIOPUT_VSCREENINFO, &p_vout->p_sys->var_info );
518 close( p_vout->p_sys->i_fd );
522 /* FIXME: if the image is full-size, it gets cropped on the left
523 * because of the xres / xres_virtual slight difference */
524 msg_Dbg( p_vout, "%ix%i (virtual %ix%i)",
525 p_vout->p_sys->var_info.xres, p_vout->p_sys->var_info.yres,
526 p_vout->p_sys->var_info.xres_virtual,
527 p_vout->p_sys->var_info.yres_virtual );
529 p_vout->p_sys->i_height = p_vout->p_sys->var_info.yres;
530 p_vout->p_sys->i_width = p_vout->p_sys->var_info.xres_virtual
531 ? p_vout->p_sys->var_info.xres_virtual
532 : p_vout->p_sys->var_info.xres;
534 p_vout->p_sys->p_palette = NULL;
535 p_vout->p_sys->b_pan = ( fix_info.ypanstep || fix_info.ywrapstep );
537 switch( p_vout->p_sys->var_info.bits_per_pixel )
540 p_vout->p_sys->p_palette = malloc( 8 * 256 * sizeof( u16 ) );
541 p_vout->p_sys->fb_cmap.start = 0;
542 p_vout->p_sys->fb_cmap.len = 256;
543 p_vout->p_sys->fb_cmap.red = p_vout->p_sys->p_palette;
544 p_vout->p_sys->fb_cmap.green = p_vout->p_sys->p_palette + 256 * sizeof( u16 );
545 p_vout->p_sys->fb_cmap.blue = p_vout->p_sys->p_palette + 2 * 256 * sizeof( u16 );
546 p_vout->p_sys->fb_cmap.transp = p_vout->p_sys->p_palette + 3 * 256 * sizeof( u16 );
548 /* Save the colormap */
549 ioctl( p_vout->p_sys->i_fd, FBIOGETCMAP, &p_vout->p_sys->fb_cmap );
551 p_vout->p_sys->i_bytes_per_pixel = 1;
556 p_vout->p_sys->i_bytes_per_pixel = 2;
560 p_vout->p_sys->i_bytes_per_pixel = 3;
564 p_vout->p_sys->i_bytes_per_pixel = 4;
568 msg_Err( p_vout, "screen depth %d is not supported",
569 p_vout->p_sys->var_info.bits_per_pixel );
571 /* Restore fb config */
572 ioctl( p_vout->p_sys->i_fd,
573 FBIOPUT_VSCREENINFO, &p_vout->p_sys->var_info );
575 close( p_vout->p_sys->i_fd );
579 p_vout->p_sys->i_page_size = p_vout->p_sys->i_width *
580 p_vout->p_sys->i_height * p_vout->p_sys->i_bytes_per_pixel;
582 /* Map a framebuffer at the beginning */
583 p_vout->p_sys->p_video = mmap( 0, p_vout->p_sys->i_page_size,
584 PROT_READ | PROT_WRITE, MAP_SHARED,
585 p_vout->p_sys->i_fd, 0 );
587 if( p_vout->p_sys->p_video == ((void*)-1) )
589 msg_Err( p_vout, "cannot map video memory (%s)", strerror(errno) );
591 if( p_vout->p_sys->var_info.bits_per_pixel == 8 )
593 free( p_vout->p_sys->p_palette );
596 /* Restore fb config */
597 ioctl( p_vout->p_sys->i_fd,
598 FBIOPUT_VSCREENINFO, &p_vout->p_sys->var_info );
600 close( p_vout->p_sys->i_fd );
604 msg_Dbg( p_vout, "framebuffer type=%d, visual=%d, ypanstep=%d, "
605 "ywrap=%d, accel=%d", fix_info.type, fix_info.visual,
606 fix_info.ypanstep, fix_info.ywrapstep, fix_info.accel );
610 /*****************************************************************************
611 * CloseDisplay: terminate FB video thread output method
612 *****************************************************************************/
613 static void CloseDisplay( vout_thread_t *p_vout )
616 memset( p_vout->p_sys->p_video, 0, p_vout->p_sys->i_page_size );
618 /* Restore palette */
619 if( p_vout->p_sys->var_info.bits_per_pixel == 8 );
621 ioctl( p_vout->p_sys->i_fd,
622 FBIOPUTCMAP, &p_vout->p_sys->fb_cmap );
623 free( p_vout->p_sys->p_palette );
626 /* Restore fb config */
627 ioctl( p_vout->p_sys->i_fd,
628 FBIOPUT_VSCREENINFO, &p_vout->p_sys->var_info );
631 close( p_vout->p_sys->i_fd );
634 /*****************************************************************************
635 * SwitchDisplay: VT change signal handler
636 *****************************************************************************
637 * This function activates or deactivates the output of the thread. It is
638 * called by the VT driver, on terminal change.
639 *****************************************************************************/
640 static void SwitchDisplay(int i_signal)
643 vout_thread_t *p_vout;
645 vlc_mutex_lock( &p_vout_bank->lock );
647 /* XXX: only test the first video output */
648 if( p_vout_bank->i_count )
650 p_vout = p_vout_bank->pp_vout[0];
654 case SIGUSR1: /* vt has been released */
655 p_vout->b_active = 0;
656 ioctl( p_vout->p_sys->i_tty, VT_RELDISP, 1 );
658 case SIGUSR2: /* vt has been acquired */
659 p_vout->b_active = 1;
660 ioctl( p_vout->p_sys->i_tty, VT_RELDISP, VT_ACTIVATE );
661 /* handle blanking */
662 vlc_mutex_lock( &p_vout->change_lock );
663 p_vout->i_changes |= VOUT_SIZE_CHANGE;
664 vlc_mutex_unlock( &p_vout->change_lock );
669 vlc_mutex_unlock( &p_vout_bank->lock );
673 /*****************************************************************************
674 * TextMode and GfxMode : switch tty to text/graphic mode
675 *****************************************************************************
676 * These functions toggle the tty mode.
677 *****************************************************************************/
678 static void TextMode( int i_tty )
680 /* return to text mode */
681 if( -1 == ioctl(i_tty, KDSETMODE, KD_TEXT) )
683 /*msg_Err( p_vout, "failed ioctl KDSETMODE KD_TEXT" );*/
687 static void GfxMode( int i_tty )
689 /* switch to graphic mode */
690 if( -1 == ioctl(i_tty, KDSETMODE, KD_GRAPHICS) )
692 /*msg_Err( p_vout, "failed ioctl KDSETMODE KD_GRAPHICS" );*/