1 /*****************************************************************************
2 * fb.c : framebuffer plugin for vlc
3 *****************************************************************************
4 * Copyright (C) 2000, 2001 the VideoLAN team
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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, 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 "Framebuffer device to use for rendering (usually /dev/fb0).")
72 set_shortname( "Framebuffer" );
73 set_category( CAT_VIDEO );
74 set_subcategory( SUBCAT_VIDEO_VOUT );
75 add_file( FB_DEV_VAR, "/dev/fb0", NULL, DEVICE_TEXT, DEVICE_LONGTEXT,
77 set_description( _("GNU/Linux console framebuffer video output") );
78 set_capability( "video output", 30 );
79 set_callbacks( Create, Destroy );
82 /*****************************************************************************
83 * vout_sys_t: video output framebuffer method descriptor
84 *****************************************************************************
85 * This structure is part of the video output thread descriptor.
86 * It describes the FB specific properties of an output thread.
87 *****************************************************************************/
90 /* System information */
91 int i_tty; /* tty device handle */
92 struct termios old_termios;
94 /* Original configuration information */
95 struct sigaction sig_usr1; /* USR1 previous handler */
96 struct sigaction sig_usr2; /* USR2 previous handler */
97 struct vt_mode vt_mode; /* previous VT mode */
99 /* Framebuffer information */
100 int i_fd; /* device handle */
101 struct fb_var_screeninfo old_info; /* original mode information */
102 struct fb_var_screeninfo var_info; /* current mode information */
103 vlc_bool_t b_pan; /* does device supports panning ? */
104 struct fb_cmap fb_cmap; /* original colormap */
105 uint16_t *p_palette; /* original palette */
107 /* Video information */
110 int i_bytes_per_pixel;
113 byte_t * p_video; /* base adress */
114 size_t i_page_size; /* page size */
117 /*****************************************************************************
118 * Create: allocates FB video thread output method
119 *****************************************************************************
120 * This function allocates and initializes a FB vout method.
121 *****************************************************************************/
122 static int Create( vlc_object_t *p_this )
124 vout_thread_t *p_vout = (vout_thread_t *)p_this;
126 struct sigaction sig_tty; /* sigaction for tty change */
127 struct vt_mode vt_mode; /* vt current mode */
128 struct termios new_termios;
130 /* Allocate instance and initialize some members */
131 p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
132 if( p_vout->p_sys == NULL )
137 p_vout->pf_init = Init;
138 p_vout->pf_end = End;
139 p_vout->pf_manage = Manage;
140 p_vout->pf_render = NULL;
141 p_vout->pf_display = Display;
143 /* Set tty and fb devices */
144 p_vout->p_sys->i_tty = 0; /* 0 == /dev/tty0 == current console */
146 GfxMode( p_vout->p_sys->i_tty );
148 /* Set keyboard settings */
149 if (tcgetattr(0, &p_vout->p_sys->old_termios) == -1)
151 msg_Err( p_vout, "tcgetattr failed" );
154 if (tcgetattr(0, &new_termios) == -1)
156 msg_Err( p_vout, "tcgetattr failed" );
159 /* new_termios.c_lflag &= ~ (ICANON | ISIG);
160 new_termios.c_lflag |= (ECHO | ECHOCTL); */
161 new_termios.c_lflag &= ~ (ICANON);
162 new_termios.c_lflag &= ~(ECHO | ECHOCTL);
163 new_termios.c_iflag = 0;
164 new_termios.c_cc[VMIN] = 1;
165 new_termios.c_cc[VTIME] = 0;
167 if (tcsetattr(0, TCSAFLUSH, &new_termios) == -1)
169 msg_Err( p_vout, "tcsetattr failed" );
172 ioctl( p_vout->p_sys->i_tty, VT_RELDISP, VT_ACKACQ );
174 /* Set-up tty signal handler to be aware of tty changes */
175 memset( &sig_tty, 0, sizeof( sig_tty ) );
176 sig_tty.sa_handler = SwitchDisplay;
177 sigemptyset( &sig_tty.sa_mask );
178 if( sigaction( SIGUSR1, &sig_tty, &p_vout->p_sys->sig_usr1 ) ||
179 sigaction( SIGUSR2, &sig_tty, &p_vout->p_sys->sig_usr2 ) )
181 msg_Err( p_vout, "cannot set signal handler (%s)", strerror(errno) );
182 tcsetattr(0, 0, &p_vout->p_sys->old_termios);
183 TextMode( p_vout->p_sys->i_tty );
184 free( p_vout->p_sys );
188 /* Set-up tty according to new signal handler */
189 if( -1 == ioctl( p_vout->p_sys->i_tty,
190 VT_GETMODE, &p_vout->p_sys->vt_mode ) )
192 msg_Err( p_vout, "cannot get terminal mode (%s)", strerror(errno) );
193 sigaction( SIGUSR1, &p_vout->p_sys->sig_usr1, NULL );
194 sigaction( SIGUSR2, &p_vout->p_sys->sig_usr2, NULL );
195 tcsetattr(0, 0, &p_vout->p_sys->old_termios);
196 TextMode( p_vout->p_sys->i_tty );
197 free( p_vout->p_sys );
200 memcpy( &vt_mode, &p_vout->p_sys->vt_mode, sizeof( vt_mode ) );
201 vt_mode.mode = VT_PROCESS;
203 vt_mode.relsig = SIGUSR1;
204 vt_mode.acqsig = SIGUSR2;
206 if( -1 == ioctl( p_vout->p_sys->i_tty, VT_SETMODE, &vt_mode ) )
208 msg_Err( p_vout, "cannot set terminal mode (%s)", strerror(errno) );
209 sigaction( SIGUSR1, &p_vout->p_sys->sig_usr1, NULL );
210 sigaction( SIGUSR2, &p_vout->p_sys->sig_usr2, NULL );
211 tcsetattr(0, 0, &p_vout->p_sys->old_termios);
212 TextMode( p_vout->p_sys->i_tty );
213 free( p_vout->p_sys );
217 if( OpenDisplay( p_vout ) )
219 ioctl( p_vout->p_sys->i_tty, VT_SETMODE, &p_vout->p_sys->vt_mode );
220 sigaction( SIGUSR1, &p_vout->p_sys->sig_usr1, NULL );
221 sigaction( SIGUSR2, &p_vout->p_sys->sig_usr2, NULL );
222 tcsetattr(0, 0, &p_vout->p_sys->old_termios);
223 TextMode( p_vout->p_sys->i_tty );
224 free( p_vout->p_sys );
231 /*****************************************************************************
232 * Init: initialize framebuffer video thread output method
233 *****************************************************************************/
234 static int Init( vout_thread_t *p_vout )
239 I_OUTPUTPICTURES = 0;
241 /* Initialize the output structure: RGB with square pixels, whatever
242 * the input format is, since it's the only format we know */
243 switch( p_vout->p_sys->var_info.bits_per_pixel )
245 case 8: /* FIXME: set the palette */
246 p_vout->output.i_chroma = VLC_FOURCC('R','G','B','2'); break;
248 p_vout->output.i_chroma = VLC_FOURCC('R','V','1','5'); break;
250 p_vout->output.i_chroma = VLC_FOURCC('R','V','1','6'); break;
252 p_vout->output.i_chroma = VLC_FOURCC('R','V','2','4'); break;
254 p_vout->output.i_chroma = VLC_FOURCC('R','V','3','2'); break;
256 msg_Err( p_vout, "unknown screen depth %i",
257 p_vout->p_sys->var_info.bits_per_pixel );
261 /* Only useful for p_vout->p_sys->var_info.bits_per_pixel != 8 */
262 p_vout->output.i_rmask = ( (1 << p_vout->p_sys->var_info.red.length) - 1 )
263 << p_vout->p_sys->var_info.red.offset;
264 p_vout->output.i_gmask = ( (1 << p_vout->p_sys->var_info.green.length) - 1 )
265 << p_vout->p_sys->var_info.green.offset;
266 p_vout->output.i_bmask = ( (1 << p_vout->p_sys->var_info.blue.length) - 1 )
267 << p_vout->p_sys->var_info.blue.offset;
269 p_vout->output.i_width = p_vout->p_sys->i_width;
270 p_vout->output.i_height = p_vout->p_sys->i_height;
272 /* Assume we have square pixels */
273 p_vout->output.i_aspect = p_vout->p_sys->i_width
274 * VOUT_ASPECT_FACTOR / p_vout->p_sys->i_height;
276 /* Clear the screen */
277 memset( p_vout->p_sys->p_video, 0, p_vout->p_sys->i_page_size );
279 /* Try to initialize 1 direct buffer */
282 /* Find an empty picture slot */
283 for( i_index = 0 ; i_index < VOUT_MAX_PICTURES ; i_index++ )
285 if( p_vout->p_picture[ i_index ].i_status == FREE_PICTURE )
287 p_pic = p_vout->p_picture + i_index;
292 /* Allocate the picture */
298 /* We know the chroma, allocate a buffer which will be used
299 * directly by the decoder */
300 p_pic->p->p_pixels = p_vout->p_sys->p_video;
301 p_pic->p->i_pixel_pitch = p_vout->p_sys->i_bytes_per_pixel;
302 p_pic->p->i_lines = p_vout->p_sys->var_info.yres;
303 p_pic->p->i_visible_lines = p_vout->p_sys->var_info.yres;
305 if( p_vout->p_sys->var_info.xres_virtual )
307 p_pic->p->i_pitch = p_vout->p_sys->var_info.xres_virtual
308 * p_vout->p_sys->i_bytes_per_pixel;
312 p_pic->p->i_pitch = p_vout->p_sys->var_info.xres
313 * p_vout->p_sys->i_bytes_per_pixel;
316 p_pic->p->i_visible_pitch = p_vout->p_sys->var_info.xres
317 * p_vout->p_sys->i_bytes_per_pixel;
321 p_pic->i_status = DESTROYED_PICTURE;
322 p_pic->i_type = DIRECT_PICTURE;
324 PP_OUTPUTPICTURE[ I_OUTPUTPICTURES ] = p_pic;
331 /*****************************************************************************
332 * End: terminate framebuffer video thread output method
333 *****************************************************************************/
334 static void End( vout_thread_t *p_vout )
336 /* Clear the screen */
337 memset( p_vout->p_sys->p_video, 0, p_vout->p_sys->i_page_size );
340 /*****************************************************************************
341 * Destroy: destroy FB video thread output method
342 *****************************************************************************
343 * Terminate an output method created by Create
344 *****************************************************************************/
345 static void Destroy( vlc_object_t *p_this )
347 vout_thread_t *p_vout = (vout_thread_t *)p_this;
349 CloseDisplay( p_vout );
351 /* Reset the terminal */
352 ioctl( p_vout->p_sys->i_tty, VT_SETMODE, &p_vout->p_sys->vt_mode );
354 /* Remove signal handlers */
355 sigaction( SIGUSR1, &p_vout->p_sys->sig_usr1, NULL );
356 sigaction( SIGUSR2, &p_vout->p_sys->sig_usr2, NULL );
358 /* Reset the keyboard state */
359 tcsetattr( 0, 0, &p_vout->p_sys->old_termios );
361 /* Return to text mode */
362 TextMode( p_vout->p_sys->i_tty );
364 /* Destroy structure */
365 free( p_vout->p_sys );
368 /*****************************************************************************
369 * Manage: handle FB events
370 *****************************************************************************
371 * This function should be called regularly by video output thread. It manages
372 * console events. It returns a non null value on error.
373 *****************************************************************************/
374 static int Manage( vout_thread_t *p_vout )
379 if ( read(0, &buf, 1) == 1)
384 p_vout->p_libvlc->b_die = 1;
396 if( p_vout->i_changes & VOUT_SIZE_CHANGE )
398 msg_Dbg( p_vout, "reinitializing framebuffer screen" );
399 p_vout->i_changes &= ~VOUT_SIZE_CHANGE;
401 /* Destroy XImages to change their size */
404 /* Recreate XImages. If SysInit failed, the thread can't go on. */
407 msg_Err( p_vout, "cannot reinit framebuffer screen" );
412 memset( p_vout->p_sys->p_video, 0, p_vout->p_sys->i_page_size );
415 /* Tell the video output thread that it will need to rebuild YUV
416 * tables. This is needed since conversion buffer size may have changed */
417 p_vout->i_changes |= VOUT_YUV_CHANGE;
424 /*****************************************************************************
425 * Display: displays previously rendered output
426 *****************************************************************************
427 * This function send the currently rendered image to FB image, waits until
428 * it is displayed and switch the two rendering buffers, preparing next frame.
429 *****************************************************************************/
430 static void Display( vout_thread_t *p_vout, picture_t *p_pic )
433 /* swap the two Y offsets if the drivers supports panning */
434 if( p_vout->p_sys->b_pan )
436 p_vout->p_sys->var_info.yoffset = 0;
437 /*p_vout->p_sys->var_info.yoffset = p_vout->p_sys->var_info.yres; */
439 /* the X offset should be 0, but who knows ...
440 * some other app might have played with the framebuffer */
441 p_vout->p_sys->var_info.xoffset = 0;
444 ioctl( p_vout->p_sys->i_fd,
445 FBIOPAN_DISPLAY, &p_vout->p_sys->var_info );
452 static void SetPalette( vout_thread_t *p_vout, uint16_t *red, uint16_t *green,
453 uint16_t *blue, uint16_t *transp )
455 struct fb_cmap cmap = { 0, 256, red, green, blue, transp };
457 ioctl( p_vout->p_sys->i_fd, FBIOPUTCMAP, &cmap );
461 /* following functions are local */
463 /*****************************************************************************
464 * OpenDisplay: initialize framebuffer
465 *****************************************************************************/
466 static int OpenDisplay( vout_thread_t *p_vout )
468 char *psz_device; /* framebuffer device path */
469 struct fb_fix_screeninfo fix_info; /* framebuffer fix information */
471 /* Open framebuffer device */
472 if( !(psz_device = config_GetPsz( p_vout, FB_DEV_VAR )) )
474 msg_Err( p_vout, "don't know which fb device to open" );
478 p_vout->p_sys->i_fd = open( psz_device, O_RDWR);
480 if( p_vout->p_sys->i_fd == -1 )
482 msg_Err( p_vout, "cannot open %s (%s)", psz_device, strerror(errno) );
488 /* Get framebuffer device information */
489 if( ioctl( p_vout->p_sys->i_fd,
490 FBIOGET_VSCREENINFO, &p_vout->p_sys->var_info ) )
492 msg_Err( p_vout, "cannot get fb info (%s)", strerror(errno) );
493 close( p_vout->p_sys->i_fd );
497 memcpy( &p_vout->p_sys->old_info, &p_vout->p_sys->var_info,
498 sizeof( struct fb_var_screeninfo ) );
500 /* Set some attributes */
501 p_vout->p_sys->var_info.activate = FB_ACTIVATE_NXTOPEN;
502 p_vout->p_sys->var_info.xoffset = 0;
503 p_vout->p_sys->var_info.yoffset = 0;
505 if( ioctl( p_vout->p_sys->i_fd,
506 FBIOPUT_VSCREENINFO, &p_vout->p_sys->var_info ) )
508 msg_Err( p_vout, "cannot set fb info (%s)", strerror(errno) );
509 close( p_vout->p_sys->i_fd );
513 /* Get some information again, in the definitive configuration */
514 if( ioctl( p_vout->p_sys->i_fd, FBIOGET_FSCREENINFO, &fix_info )
515 || ioctl( p_vout->p_sys->i_fd,
516 FBIOGET_VSCREENINFO, &p_vout->p_sys->var_info ) )
518 msg_Err( p_vout, "cannot get additional fb info (%s)",
521 /* Restore fb config */
522 ioctl( p_vout->p_sys->i_fd,
523 FBIOPUT_VSCREENINFO, &p_vout->p_sys->old_info );
525 close( p_vout->p_sys->i_fd );
529 /* FIXME: if the image is full-size, it gets cropped on the left
530 * because of the xres / xres_virtual slight difference */
531 msg_Dbg( p_vout, "%ix%i (virtual %ix%i)",
532 p_vout->p_sys->var_info.xres, p_vout->p_sys->var_info.yres,
533 p_vout->p_sys->var_info.xres_virtual,
534 p_vout->p_sys->var_info.yres_virtual );
536 p_vout->p_sys->i_height = p_vout->p_sys->var_info.yres;
537 p_vout->p_sys->i_width = p_vout->p_sys->var_info.xres_virtual
538 ? p_vout->p_sys->var_info.xres_virtual
539 : p_vout->p_sys->var_info.xres;
541 p_vout->p_sys->p_palette = NULL;
542 p_vout->p_sys->b_pan = ( fix_info.ypanstep || fix_info.ywrapstep );
544 switch( p_vout->p_sys->var_info.bits_per_pixel )
547 p_vout->p_sys->p_palette = malloc( 8 * 256 * sizeof( uint16_t ) );
548 p_vout->p_sys->fb_cmap.start = 0;
549 p_vout->p_sys->fb_cmap.len = 256;
550 p_vout->p_sys->fb_cmap.red = p_vout->p_sys->p_palette;
551 p_vout->p_sys->fb_cmap.green = p_vout->p_sys->p_palette + 256 * sizeof( uint16_t );
552 p_vout->p_sys->fb_cmap.blue = p_vout->p_sys->p_palette + 2 * 256 * sizeof( uint16_t );
553 p_vout->p_sys->fb_cmap.transp = p_vout->p_sys->p_palette + 3 * 256 * sizeof( uint16_t );
555 /* Save the colormap */
556 ioctl( p_vout->p_sys->i_fd, FBIOGETCMAP, &p_vout->p_sys->fb_cmap );
558 p_vout->p_sys->i_bytes_per_pixel = 1;
563 p_vout->p_sys->i_bytes_per_pixel = 2;
567 p_vout->p_sys->i_bytes_per_pixel = 3;
571 p_vout->p_sys->i_bytes_per_pixel = 4;
575 msg_Err( p_vout, "screen depth %d is not supported",
576 p_vout->p_sys->var_info.bits_per_pixel );
578 /* Restore fb config */
579 ioctl( p_vout->p_sys->i_fd,
580 FBIOPUT_VSCREENINFO, &p_vout->p_sys->old_info );
582 close( p_vout->p_sys->i_fd );
586 p_vout->p_sys->i_page_size = p_vout->p_sys->i_width *
587 p_vout->p_sys->i_height * p_vout->p_sys->i_bytes_per_pixel;
589 /* Map a framebuffer at the beginning */
590 p_vout->p_sys->p_video = mmap( 0, p_vout->p_sys->i_page_size,
591 PROT_READ | PROT_WRITE, MAP_SHARED,
592 p_vout->p_sys->i_fd, 0 );
594 if( p_vout->p_sys->p_video == ((void*)-1) )
596 msg_Err( p_vout, "cannot map video memory (%s)", strerror(errno) );
598 if( p_vout->p_sys->var_info.bits_per_pixel == 8 )
600 free( p_vout->p_sys->p_palette );
603 /* Restore fb config */
604 ioctl( p_vout->p_sys->i_fd,
605 FBIOPUT_VSCREENINFO, &p_vout->p_sys->old_info );
607 close( p_vout->p_sys->i_fd );
611 msg_Dbg( p_vout, "framebuffer type=%d, visual=%d, ypanstep=%d, "
612 "ywrap=%d, accel=%d", fix_info.type, fix_info.visual,
613 fix_info.ypanstep, fix_info.ywrapstep, fix_info.accel );
617 /*****************************************************************************
618 * CloseDisplay: terminate FB video thread output method
619 *****************************************************************************/
620 static void CloseDisplay( vout_thread_t *p_vout )
623 memset( p_vout->p_sys->p_video, 0, p_vout->p_sys->i_page_size );
625 /* Restore palette */
626 if( p_vout->p_sys->var_info.bits_per_pixel == 8 )
628 ioctl( p_vout->p_sys->i_fd,
629 FBIOPUTCMAP, &p_vout->p_sys->fb_cmap );
630 free( p_vout->p_sys->p_palette );
633 /* Restore fb config */
634 ioctl( p_vout->p_sys->i_fd,
635 FBIOPUT_VSCREENINFO, &p_vout->p_sys->old_info );
638 close( p_vout->p_sys->i_fd );
641 /*****************************************************************************
642 * SwitchDisplay: VT change signal handler
643 *****************************************************************************
644 * This function activates or deactivates the output of the thread. It is
645 * called by the VT driver, on terminal change.
646 *****************************************************************************/
647 static void SwitchDisplay(int i_signal)
650 vout_thread_t *p_vout;
652 vlc_mutex_lock( &p_vout_bank->lock );
654 /* XXX: only test the first video output */
655 if( p_vout_bank->i_count )
657 p_vout = p_vout_bank->pp_vout[0];
661 case SIGUSR1: /* vt has been released */
662 p_vout->b_active = 0;
663 ioctl( p_vout->p_sys->i_tty, VT_RELDISP, 1 );
665 case SIGUSR2: /* vt has been acquired */
666 p_vout->b_active = 1;
667 ioctl( p_vout->p_sys->i_tty, VT_RELDISP, VT_ACTIVATE );
668 /* handle blanking */
669 vlc_mutex_lock( &p_vout->change_lock );
670 p_vout->i_changes |= VOUT_SIZE_CHANGE;
671 vlc_mutex_unlock( &p_vout->change_lock );
676 vlc_mutex_unlock( &p_vout_bank->lock );
680 /*****************************************************************************
681 * TextMode and GfxMode : switch tty to text/graphic mode
682 *****************************************************************************
683 * These functions toggle the tty mode.
684 *****************************************************************************/
685 static void TextMode( int i_tty )
687 /* return to text mode */
688 if( -1 == ioctl(i_tty, KDSETMODE, KD_TEXT) )
690 /*msg_Err( p_vout, "failed ioctl KDSETMODE KD_TEXT" );*/
694 static void GfxMode( int i_tty )
696 /* switch to graphic mode */
697 if( -1 == ioctl(i_tty, KDSETMODE, KD_GRAPHICS) )
699 /*msg_Err( p_vout, "failed ioctl KDSETMODE KD_GRAPHICS" );*/