1 /*****************************************************************************
2 * fb.c : framebuffer plugin for vlc
3 *****************************************************************************
4 * Copyright (C) 2000, 2001 VideoLAN
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"
67 #define DEVICE_TEXT N_("Framebuffer device")
68 #define DEVICE_LONGTEXT N_( \
69 "You can select here the framebuffer device that will be used " \
70 "for rendering (usually /dev/fb0).")
73 add_file( FB_DEV_VAR, "/dev/fb0", NULL, DEVICE_TEXT, DEVICE_LONGTEXT,
75 set_description( _("GNU/Linux console framebuffer video output") );
76 set_capability( "video output", 30 );
77 set_callbacks( Create, Destroy );
80 /*****************************************************************************
81 * vout_sys_t: video output framebuffer method descriptor
82 *****************************************************************************
83 * This structure is part of the video output thread descriptor.
84 * It describes the FB specific properties of an output thread.
85 *****************************************************************************/
88 /* System information */
89 int i_tty; /* tty device handle */
90 struct termios old_termios;
92 /* Original configuration information */
93 struct sigaction sig_usr1; /* USR1 previous handler */
94 struct sigaction sig_usr2; /* USR2 previous handler */
95 struct vt_mode vt_mode; /* previous VT mode */
97 /* Framebuffer information */
98 int i_fd; /* device handle */
99 struct fb_var_screeninfo old_info; /* original mode information */
100 struct fb_var_screeninfo var_info; /* current mode information */
101 vlc_bool_t b_pan; /* does device supports panning ? */
102 struct fb_cmap fb_cmap; /* original colormap */
103 uint16_t *p_palette; /* original palette */
105 /* Video information */
108 int i_bytes_per_pixel;
111 byte_t * p_video; /* base adress */
112 size_t i_page_size; /* page size */
115 /*****************************************************************************
116 * Create: allocates FB video thread output method
117 *****************************************************************************
118 * This function allocates and initializes a FB vout method.
119 *****************************************************************************/
120 static int Create( vlc_object_t *p_this )
122 vout_thread_t *p_vout = (vout_thread_t *)p_this;
124 struct sigaction sig_tty; /* sigaction for tty change */
125 struct vt_mode vt_mode; /* vt current mode */
126 struct termios new_termios;
128 /* Allocate instance and initialize some members */
129 p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
130 if( p_vout->p_sys == NULL )
135 p_vout->pf_init = Init;
136 p_vout->pf_end = End;
137 p_vout->pf_manage = Manage;
138 p_vout->pf_render = NULL;
139 p_vout->pf_display = Display;
141 /* Set tty and fb devices */
142 p_vout->p_sys->i_tty = 0; /* 0 == /dev/tty0 == current console */
144 GfxMode( p_vout->p_sys->i_tty );
146 /* Set keyboard settings */
147 if (tcgetattr(0, &p_vout->p_sys->old_termios) == -1)
149 msg_Err( p_vout, "tcgetattr failed" );
152 if (tcgetattr(0, &new_termios) == -1)
154 msg_Err( p_vout, "tcgetattr failed" );
157 /* new_termios.c_lflag &= ~ (ICANON | ISIG);
158 new_termios.c_lflag |= (ECHO | ECHOCTL); */
159 new_termios.c_lflag &= ~ (ICANON);
160 new_termios.c_lflag &= ~(ECHO | ECHOCTL);
161 new_termios.c_iflag = 0;
162 new_termios.c_cc[VMIN] = 1;
163 new_termios.c_cc[VTIME] = 0;
165 if (tcsetattr(0, TCSAFLUSH, &new_termios) == -1)
167 msg_Err( p_vout, "tcsetattr failed" );
170 ioctl( p_vout->p_sys->i_tty, VT_RELDISP, VT_ACKACQ );
172 /* Set-up tty signal handler to be aware of tty changes */
173 memset( &sig_tty, 0, sizeof( sig_tty ) );
174 sig_tty.sa_handler = SwitchDisplay;
175 sigemptyset( &sig_tty.sa_mask );
176 if( sigaction( SIGUSR1, &sig_tty, &p_vout->p_sys->sig_usr1 ) ||
177 sigaction( SIGUSR2, &sig_tty, &p_vout->p_sys->sig_usr2 ) )
179 msg_Err( p_vout, "cannot set signal handler (%s)", strerror(errno) );
180 tcsetattr(0, 0, &p_vout->p_sys->old_termios);
181 TextMode( p_vout->p_sys->i_tty );
182 free( p_vout->p_sys );
186 /* Set-up tty according to new signal handler */
187 if( -1 == ioctl( p_vout->p_sys->i_tty,
188 VT_GETMODE, &p_vout->p_sys->vt_mode ) )
190 msg_Err( p_vout, "cannot get terminal mode (%s)", strerror(errno) );
191 sigaction( SIGUSR1, &p_vout->p_sys->sig_usr1, NULL );
192 sigaction( SIGUSR2, &p_vout->p_sys->sig_usr2, NULL );
193 tcsetattr(0, 0, &p_vout->p_sys->old_termios);
194 TextMode( p_vout->p_sys->i_tty );
195 free( p_vout->p_sys );
198 memcpy( &vt_mode, &p_vout->p_sys->vt_mode, sizeof( vt_mode ) );
199 vt_mode.mode = VT_PROCESS;
201 vt_mode.relsig = SIGUSR1;
202 vt_mode.acqsig = SIGUSR2;
204 if( -1 == ioctl( p_vout->p_sys->i_tty, VT_SETMODE, &vt_mode ) )
206 msg_Err( p_vout, "cannot set terminal mode (%s)", strerror(errno) );
207 sigaction( SIGUSR1, &p_vout->p_sys->sig_usr1, NULL );
208 sigaction( SIGUSR2, &p_vout->p_sys->sig_usr2, NULL );
209 tcsetattr(0, 0, &p_vout->p_sys->old_termios);
210 TextMode( p_vout->p_sys->i_tty );
211 free( p_vout->p_sys );
215 if( OpenDisplay( p_vout ) )
217 ioctl( p_vout->p_sys->i_tty, VT_SETMODE, &p_vout->p_sys->vt_mode );
218 sigaction( SIGUSR1, &p_vout->p_sys->sig_usr1, NULL );
219 sigaction( SIGUSR2, &p_vout->p_sys->sig_usr2, NULL );
220 tcsetattr(0, 0, &p_vout->p_sys->old_termios);
221 TextMode( p_vout->p_sys->i_tty );
222 free( p_vout->p_sys );
229 /*****************************************************************************
230 * Init: initialize framebuffer video thread output method
231 *****************************************************************************/
232 static int Init( vout_thread_t *p_vout )
237 I_OUTPUTPICTURES = 0;
239 /* Initialize the output structure: RGB with square pixels, whatever
240 * the input format is, since it's the only format we know */
241 switch( p_vout->p_sys->var_info.bits_per_pixel )
243 case 8: /* FIXME: set the palette */
244 p_vout->output.i_chroma = VLC_FOURCC('R','G','B','2'); break;
246 p_vout->output.i_chroma = VLC_FOURCC('R','V','1','5'); break;
248 p_vout->output.i_chroma = VLC_FOURCC('R','V','1','6'); break;
250 p_vout->output.i_chroma = VLC_FOURCC('R','V','2','4'); break;
252 p_vout->output.i_chroma = VLC_FOURCC('R','V','3','2'); break;
254 msg_Err( p_vout, "unknown screen depth %i",
255 p_vout->p_sys->var_info.bits_per_pixel );
259 /* Only useful for p_vout->p_sys->var_info.bits_per_pixel != 8 */
260 p_vout->output.i_rmask = ( (1 << p_vout->p_sys->var_info.red.length) - 1 )
261 << p_vout->p_sys->var_info.red.offset;
262 p_vout->output.i_gmask = ( (1 << p_vout->p_sys->var_info.green.length) - 1 )
263 << p_vout->p_sys->var_info.green.offset;
264 p_vout->output.i_bmask = ( (1 << p_vout->p_sys->var_info.blue.length) - 1 )
265 << p_vout->p_sys->var_info.blue.offset;
267 p_vout->output.i_width = p_vout->p_sys->i_width;
268 p_vout->output.i_height = p_vout->p_sys->i_height;
270 /* Assume we have square pixels */
271 p_vout->output.i_aspect = p_vout->p_sys->i_width
272 * VOUT_ASPECT_FACTOR / p_vout->p_sys->i_height;
274 /* Clear the screen */
275 memset( p_vout->p_sys->p_video, 0, p_vout->p_sys->i_page_size );
277 /* Try to initialize 1 direct buffer */
280 /* Find an empty picture slot */
281 for( i_index = 0 ; i_index < VOUT_MAX_PICTURES ; i_index++ )
283 if( p_vout->p_picture[ i_index ].i_status == FREE_PICTURE )
285 p_pic = p_vout->p_picture + i_index;
290 /* Allocate the picture */
296 /* We know the chroma, allocate a buffer which will be used
297 * directly by the decoder */
298 p_pic->p->p_pixels = p_vout->p_sys->p_video;
299 p_pic->p->i_pixel_pitch = p_vout->p_sys->i_bytes_per_pixel;
300 p_pic->p->i_lines = p_vout->p_sys->var_info.yres;
302 if( p_vout->p_sys->var_info.xres_virtual )
304 p_pic->p->i_pitch = p_vout->p_sys->var_info.xres_virtual
305 * p_vout->p_sys->i_bytes_per_pixel;
309 p_pic->p->i_pitch = p_vout->p_sys->var_info.xres
310 * p_vout->p_sys->i_bytes_per_pixel;
313 p_pic->p->i_visible_pitch = p_vout->p_sys->var_info.xres
314 * p_vout->p_sys->i_bytes_per_pixel;
318 p_pic->i_status = DESTROYED_PICTURE;
319 p_pic->i_type = DIRECT_PICTURE;
321 PP_OUTPUTPICTURE[ I_OUTPUTPICTURES ] = p_pic;
328 /*****************************************************************************
329 * End: terminate framebuffer video thread output method
330 *****************************************************************************/
331 static void End( vout_thread_t *p_vout )
333 /* Clear the screen */
334 memset( p_vout->p_sys->p_video, 0, p_vout->p_sys->i_page_size );
337 /*****************************************************************************
338 * Destroy: destroy FB video thread output method
339 *****************************************************************************
340 * Terminate an output method created by Create
341 *****************************************************************************/
342 static void Destroy( vlc_object_t *p_this )
344 vout_thread_t *p_vout = (vout_thread_t *)p_this;
346 CloseDisplay( p_vout );
348 /* Reset the terminal */
349 ioctl( p_vout->p_sys->i_tty, VT_SETMODE, &p_vout->p_sys->vt_mode );
351 /* Remove signal handlers */
352 sigaction( SIGUSR1, &p_vout->p_sys->sig_usr1, NULL );
353 sigaction( SIGUSR2, &p_vout->p_sys->sig_usr2, NULL );
355 /* Reset the keyboard state */
356 tcsetattr( 0, 0, &p_vout->p_sys->old_termios );
358 /* Return to text mode */
359 TextMode( p_vout->p_sys->i_tty );
361 /* Destroy structure */
362 free( p_vout->p_sys );
365 /*****************************************************************************
366 * Manage: handle FB events
367 *****************************************************************************
368 * This function should be called regularly by video output thread. It manages
369 * console events. It returns a non null value on error.
370 *****************************************************************************/
371 static int Manage( vout_thread_t *p_vout )
376 if ( read(0, &buf, 1) == 1)
381 p_vout->p_vlc->b_die = 1;
393 if( p_vout->i_changes & VOUT_SIZE_CHANGE )
395 msg_Dbg( p_vout, "reinitializing framebuffer screen" );
396 p_vout->i_changes &= ~VOUT_SIZE_CHANGE;
398 /* Destroy XImages to change their size */
401 /* Recreate XImages. If SysInit failed, the thread can't go on. */
404 msg_Err( p_vout, "cannot reinit framebuffer screen" );
409 memset( p_vout->p_sys->p_video, 0, p_vout->p_sys->i_page_size );
412 /* Tell the video output thread that it will need to rebuild YUV
413 * tables. This is needed since conversion buffer size may have changed */
414 p_vout->i_changes |= VOUT_YUV_CHANGE;
421 /*****************************************************************************
422 * Display: displays previously rendered output
423 *****************************************************************************
424 * This function send the currently rendered image to FB image, waits until
425 * it is displayed and switch the two rendering buffers, preparing next frame.
426 *****************************************************************************/
427 static void Display( vout_thread_t *p_vout, picture_t *p_pic )
430 /* swap the two Y offsets if the drivers supports panning */
431 if( p_vout->p_sys->b_pan )
433 p_vout->p_sys->var_info.yoffset = 0;
434 /*p_vout->p_sys->var_info.yoffset = p_vout->p_sys->var_info.yres; */
436 /* the X offset should be 0, but who knows ...
437 * some other app might have played with the framebuffer */
438 p_vout->p_sys->var_info.xoffset = 0;
441 ioctl( p_vout->p_sys->i_fd,
442 FBIOPAN_DISPLAY, &p_vout->p_sys->var_info );
449 static void SetPalette( vout_thread_t *p_vout, uint16_t *red, uint16_t *green,
450 uint16_t *blue, uint16_t *transp )
452 struct fb_cmap cmap = { 0, 256, red, green, blue, transp };
454 ioctl( p_vout->p_sys->i_fd, FBIOPUTCMAP, &cmap );
458 /* following functions are local */
460 /*****************************************************************************
461 * OpenDisplay: initialize framebuffer
462 *****************************************************************************/
463 static int OpenDisplay( vout_thread_t *p_vout )
465 char *psz_device; /* framebuffer device path */
466 struct fb_fix_screeninfo fix_info; /* framebuffer fix information */
468 /* Open framebuffer device */
469 if( !(psz_device = config_GetPsz( p_vout, FB_DEV_VAR )) )
471 msg_Err( p_vout, "don't know which fb device to open" );
475 p_vout->p_sys->i_fd = open( psz_device, O_RDWR);
477 if( p_vout->p_sys->i_fd == -1 )
479 msg_Err( p_vout, "cannot open %s (%s)", psz_device, strerror(errno) );
485 /* Get framebuffer device information */
486 if( ioctl( p_vout->p_sys->i_fd,
487 FBIOGET_VSCREENINFO, &p_vout->p_sys->var_info ) )
489 msg_Err( p_vout, "cannot get fb info (%s)", strerror(errno) );
490 close( p_vout->p_sys->i_fd );
494 memcpy( &p_vout->p_sys->old_info, &p_vout->p_sys->var_info,
495 sizeof( struct fb_var_screeninfo ) );
497 /* Set some attributes */
498 p_vout->p_sys->var_info.activate = FB_ACTIVATE_NXTOPEN;
499 p_vout->p_sys->var_info.xoffset = 0;
500 p_vout->p_sys->var_info.yoffset = 0;
502 if( ioctl( p_vout->p_sys->i_fd,
503 FBIOPUT_VSCREENINFO, &p_vout->p_sys->var_info ) )
505 msg_Err( p_vout, "cannot set fb info (%s)", strerror(errno) );
506 close( p_vout->p_sys->i_fd );
510 /* Get some information again, in the definitive configuration */
511 if( ioctl( p_vout->p_sys->i_fd, FBIOGET_FSCREENINFO, &fix_info )
512 || ioctl( p_vout->p_sys->i_fd,
513 FBIOGET_VSCREENINFO, &p_vout->p_sys->var_info ) )
515 msg_Err( p_vout, "cannot get additional fb info (%s)",
518 /* Restore fb config */
519 ioctl( p_vout->p_sys->i_fd,
520 FBIOPUT_VSCREENINFO, &p_vout->p_sys->old_info );
522 close( p_vout->p_sys->i_fd );
526 /* FIXME: if the image is full-size, it gets cropped on the left
527 * because of the xres / xres_virtual slight difference */
528 msg_Dbg( p_vout, "%ix%i (virtual %ix%i)",
529 p_vout->p_sys->var_info.xres, p_vout->p_sys->var_info.yres,
530 p_vout->p_sys->var_info.xres_virtual,
531 p_vout->p_sys->var_info.yres_virtual );
533 p_vout->p_sys->i_height = p_vout->p_sys->var_info.yres;
534 p_vout->p_sys->i_width = p_vout->p_sys->var_info.xres_virtual
535 ? p_vout->p_sys->var_info.xres_virtual
536 : p_vout->p_sys->var_info.xres;
538 p_vout->p_sys->p_palette = NULL;
539 p_vout->p_sys->b_pan = ( fix_info.ypanstep || fix_info.ywrapstep );
541 switch( p_vout->p_sys->var_info.bits_per_pixel )
544 p_vout->p_sys->p_palette = malloc( 8 * 256 * sizeof( uint16_t ) );
545 p_vout->p_sys->fb_cmap.start = 0;
546 p_vout->p_sys->fb_cmap.len = 256;
547 p_vout->p_sys->fb_cmap.red = p_vout->p_sys->p_palette;
548 p_vout->p_sys->fb_cmap.green = p_vout->p_sys->p_palette + 256 * sizeof( uint16_t );
549 p_vout->p_sys->fb_cmap.blue = p_vout->p_sys->p_palette + 2 * 256 * sizeof( uint16_t );
550 p_vout->p_sys->fb_cmap.transp = p_vout->p_sys->p_palette + 3 * 256 * sizeof( uint16_t );
552 /* Save the colormap */
553 ioctl( p_vout->p_sys->i_fd, FBIOGETCMAP, &p_vout->p_sys->fb_cmap );
555 p_vout->p_sys->i_bytes_per_pixel = 1;
560 p_vout->p_sys->i_bytes_per_pixel = 2;
564 p_vout->p_sys->i_bytes_per_pixel = 3;
568 p_vout->p_sys->i_bytes_per_pixel = 4;
572 msg_Err( p_vout, "screen depth %d is not supported",
573 p_vout->p_sys->var_info.bits_per_pixel );
575 /* Restore fb config */
576 ioctl( p_vout->p_sys->i_fd,
577 FBIOPUT_VSCREENINFO, &p_vout->p_sys->old_info );
579 close( p_vout->p_sys->i_fd );
583 p_vout->p_sys->i_page_size = p_vout->p_sys->i_width *
584 p_vout->p_sys->i_height * p_vout->p_sys->i_bytes_per_pixel;
586 /* Map a framebuffer at the beginning */
587 p_vout->p_sys->p_video = mmap( 0, p_vout->p_sys->i_page_size,
588 PROT_READ | PROT_WRITE, MAP_SHARED,
589 p_vout->p_sys->i_fd, 0 );
591 if( p_vout->p_sys->p_video == ((void*)-1) )
593 msg_Err( p_vout, "cannot map video memory (%s)", strerror(errno) );
595 if( p_vout->p_sys->var_info.bits_per_pixel == 8 )
597 free( p_vout->p_sys->p_palette );
600 /* Restore fb config */
601 ioctl( p_vout->p_sys->i_fd,
602 FBIOPUT_VSCREENINFO, &p_vout->p_sys->old_info );
604 close( p_vout->p_sys->i_fd );
608 msg_Dbg( p_vout, "framebuffer type=%d, visual=%d, ypanstep=%d, "
609 "ywrap=%d, accel=%d", fix_info.type, fix_info.visual,
610 fix_info.ypanstep, fix_info.ywrapstep, fix_info.accel );
614 /*****************************************************************************
615 * CloseDisplay: terminate FB video thread output method
616 *****************************************************************************/
617 static void CloseDisplay( vout_thread_t *p_vout )
620 memset( p_vout->p_sys->p_video, 0, p_vout->p_sys->i_page_size );
622 /* Restore palette */
623 if( p_vout->p_sys->var_info.bits_per_pixel == 8 )
625 ioctl( p_vout->p_sys->i_fd,
626 FBIOPUTCMAP, &p_vout->p_sys->fb_cmap );
627 free( p_vout->p_sys->p_palette );
630 /* Restore fb config */
631 ioctl( p_vout->p_sys->i_fd,
632 FBIOPUT_VSCREENINFO, &p_vout->p_sys->old_info );
635 close( p_vout->p_sys->i_fd );
638 /*****************************************************************************
639 * SwitchDisplay: VT change signal handler
640 *****************************************************************************
641 * This function activates or deactivates the output of the thread. It is
642 * called by the VT driver, on terminal change.
643 *****************************************************************************/
644 static void SwitchDisplay(int i_signal)
647 vout_thread_t *p_vout;
649 vlc_mutex_lock( &p_vout_bank->lock );
651 /* XXX: only test the first video output */
652 if( p_vout_bank->i_count )
654 p_vout = p_vout_bank->pp_vout[0];
658 case SIGUSR1: /* vt has been released */
659 p_vout->b_active = 0;
660 ioctl( p_vout->p_sys->i_tty, VT_RELDISP, 1 );
662 case SIGUSR2: /* vt has been acquired */
663 p_vout->b_active = 1;
664 ioctl( p_vout->p_sys->i_tty, VT_RELDISP, VT_ACTIVATE );
665 /* handle blanking */
666 vlc_mutex_lock( &p_vout->change_lock );
667 p_vout->i_changes |= VOUT_SIZE_CHANGE;
668 vlc_mutex_unlock( &p_vout->change_lock );
673 vlc_mutex_unlock( &p_vout_bank->lock );
677 /*****************************************************************************
678 * TextMode and GfxMode : switch tty to text/graphic mode
679 *****************************************************************************
680 * These functions toggle the tty mode.
681 *****************************************************************************/
682 static void TextMode( int i_tty )
684 /* return to text mode */
685 if( -1 == ioctl(i_tty, KDSETMODE, KD_TEXT) )
687 /*msg_Err( p_vout, "failed ioctl KDSETMODE KD_TEXT" );*/
691 static void GfxMode( int i_tty )
693 /* switch to graphic mode */
694 if( -1 == ioctl(i_tty, KDSETMODE, KD_GRAPHICS) )
696 /*msg_Err( p_vout, "failed ioctl KDSETMODE KD_GRAPHICS" );*/