1 /*****************************************************************************
2 * fb.c : framebuffer plugin for vlc
3 *****************************************************************************
4 * Copyright (C) 2000, 2001 the VideoLAN team
7 * Authors: Samuel Hocevar <sam@zoy.org>
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.
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.
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 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
33 #include <errno.h> /* ENOMEM */
34 #include <signal.h> /* SIGUSR1, SIGUSR2 */
35 #include <fcntl.h> /* open() */
36 #include <unistd.h> /* close() */
38 #include <termios.h> /* struct termios */
39 #include <sys/ioctl.h>
40 #include <sys/mman.h> /* mmap() */
43 #include <linux/vt.h> /* VT_* */
44 #include <linux/kd.h> /* KD* */
46 #include <vlc_common.h>
47 #include <vlc_plugin.h>
50 /*****************************************************************************
52 *****************************************************************************/
53 static int Create ( vlc_object_t * );
54 static void Destroy ( vlc_object_t * );
56 static int Init ( vout_thread_t * );
57 static void End ( vout_thread_t * );
58 static int Manage ( vout_thread_t * );
59 static void Display ( vout_thread_t *, picture_t * );
60 static int Control ( vout_thread_t *, int, va_list );
62 static int NewPicture ( vout_thread_t *, picture_t * );
63 static void FreePicture ( vout_thread_t *, picture_t * );
65 static int OpenDisplay ( vout_thread_t * );
66 static void CloseDisplay ( vout_thread_t * );
67 static void SwitchDisplay ( int i_signal );
68 static void TextMode ( int i_tty );
69 static void GfxMode ( int i_tty );
71 #define MAX_DIRECTBUFFERS 1
73 /*****************************************************************************
75 *****************************************************************************/
76 #define FB_DEV_VAR "fbdev"
78 #define DEVICE_TEXT N_("Framebuffer device")
79 #define DEVICE_LONGTEXT N_( \
80 "Framebuffer device to use for rendering (usually /dev/fb0).")
82 #define TTY_TEXT N_("Run fb on current tty.")
83 #define TTY_LONGTEXT N_( \
84 "Run framebuffer on current TTY device (default enabled). " \
85 "(disable tty handling with caution)" )
87 #define CHROMA_TEXT N_("Chroma used.")
88 #define CHROMA_LONGTEXT N_( \
89 "Force use of a specific chroma for output. Default is I420." )
91 #define ASPECT_RATIO_TEXT N_("Video aspect ratio")
92 #define ASPECT_RATIO_LONGTEXT N_( \
93 "Aspect ratio of the video image (4:3, 16:9). Default is square pixels." )
95 #define FB_MODE_TEXT N_("Framebuffer resolution to use.")
96 #define FB_MODE_LONGTEXT N_( \
97 "Select the resolution for the framebuffer. Currently it supports " \
98 "the values 0=QCIF 1=CIF 2=NTSC 3=PAL, 4=auto (default 4=auto)" )
100 #define HW_ACCEL_TEXT N_("Framebuffer uses hw acceleration.")
101 #define HW_ACCEL_LONGTEXT N_( \
102 "If your framebuffer supports hardware acceleration or does double buffering " \
103 "in hardware then you must disable this option. It then does double buffering " \
107 set_shortname( "Framebuffer" );
108 set_category( CAT_VIDEO );
109 set_subcategory( SUBCAT_VIDEO_VOUT );
110 add_file( FB_DEV_VAR, "/dev/fb0", NULL, DEVICE_TEXT, DEVICE_LONGTEXT,
112 add_bool( "fb-tty", 1, NULL, TTY_TEXT, TTY_LONGTEXT, true );
113 add_string( "fb-chroma", NULL, NULL, CHROMA_TEXT, CHROMA_LONGTEXT,
115 add_string( "fb-aspect-ratio", NULL, NULL, ASPECT_RATIO_TEXT,
116 ASPECT_RATIO_LONGTEXT, true );
117 add_integer( "fb-mode", 4, NULL, FB_MODE_TEXT, FB_MODE_LONGTEXT,
119 add_bool( "fb-hw-accel", true, NULL, HW_ACCEL_TEXT, HW_ACCEL_LONGTEXT,
121 set_description( N_("GNU/Linux console framebuffer video output") );
122 set_capability( "video output", 30 );
123 set_callbacks( Create, Destroy );
126 /*****************************************************************************
127 * vout_sys_t: video output framebuffer method descriptor
128 *****************************************************************************
129 * This structure is part of the video output thread descriptor.
130 * It describes the FB specific properties of an output thread.
131 *****************************************************************************/
134 /* System information */
135 int i_tty; /* tty device handle */
137 struct termios old_termios;
139 /* Original configuration information */
140 struct sigaction sig_usr1; /* USR1 previous handler */
141 struct sigaction sig_usr2; /* USR2 previous handler */
142 struct vt_mode vt_mode; /* previous VT mode */
144 /* Framebuffer information */
145 int i_fd; /* device handle */
146 struct fb_var_screeninfo old_info; /* original mode information */
147 struct fb_var_screeninfo var_info; /* current mode information */
148 bool b_pan; /* does device supports panning ? */
149 struct fb_cmap fb_cmap; /* original colormap */
150 uint16_t *p_palette; /* original palette */
151 bool b_hw_accel; /* has hardware support */
153 /* Video information */
157 int i_bytes_per_pixel;
158 bool b_auto; /* Automatically adjust video size to fb size */
159 vlc_fourcc_t i_chroma;
162 uint8_t *p_video; /* base adress */
163 size_t i_page_size; /* page size */
168 uint8_t * p_data; /* base adress */
171 /*****************************************************************************
172 * Create: allocates FB video thread output method
173 *****************************************************************************
174 * This function allocates and initializes a FB vout method.
175 *****************************************************************************/
176 static int Create( vlc_object_t *p_this )
178 vout_thread_t *p_vout = (vout_thread_t *)p_this;
183 struct sigaction sig_tty; /* sigaction for tty change */
184 struct vt_mode vt_mode; /* vt current mode */
185 struct termios new_termios;
187 /* Allocate instance and initialize some members */
188 p_vout->p_sys = p_sys = malloc( sizeof( vout_sys_t ) );
189 if( p_vout->p_sys == NULL )
191 memset( p_sys, 0, sizeof(vout_sys_t) );
193 p_vout->pf_init = Init;
194 p_vout->pf_end = End;
195 p_vout->pf_manage = Manage;
196 p_vout->pf_render = NULL;
197 p_vout->pf_display = Display;
198 p_vout->pf_control = Control;
200 /* Does the framebuffer uses hw acceleration? */
201 p_sys->b_hw_accel = var_CreateGetBool( p_vout, "fb-hw-accel" );
203 /* Set tty and fb devices */
204 p_sys->i_tty = 0; /* 0 == /dev/tty0 == current console */
205 p_sys->b_tty = var_CreateGetBool( p_vout, "fb-tty" );
207 #if defined(HAVE_ISATTY)
208 /* Check that stdin is a TTY */
209 if( p_sys->b_tty && !isatty( 0 ) )
211 msg_Warn( p_vout, "fd 0 is not a TTY" );
216 msg_Warn( p_vout, "disabling tty handling, use with caution because "
217 "there is no way to return to the tty." );
222 psz_chroma = var_CreateGetNonEmptyString( p_vout, "fb-chroma" );
225 if( strlen( psz_chroma ) == 4 )
227 p_sys->i_chroma = VLC_FOURCC( psz_chroma[0],
231 msg_Dbg( p_vout, "forcing chroma '%s'", psz_chroma );
235 msg_Warn( p_vout, "invalid chroma (%s), using defaults.",
242 p_sys->i_aspect = -1;
243 psz_aspect = var_CreateGetNonEmptyString( p_vout, "fb-aspect-ratio" );
246 char *psz_parser = strchr( psz_aspect, ':' );
250 *psz_parser++ = '\0';
251 p_sys->i_aspect = ( atoi( psz_aspect )
252 * VOUT_ASPECT_FACTOR ) / atoi( psz_parser );
254 msg_Dbg( p_vout, "using aspect ratio %d:%d",
255 atoi( psz_aspect ), atoi( psz_parser ) );
261 p_sys->b_auto = false;
262 i_mode = var_CreateGetInteger( p_vout, "fb-mode" );
266 p_sys->i_width = 176;
267 p_sys->i_height = 144;
270 p_sys->i_width = 352;
271 p_sys->i_height = 288;
274 p_sys->i_width = 640;
275 p_sys->i_height = 480;
278 p_sys->i_width = 704;
279 p_sys->i_height = 576;
283 p_sys->b_auto = true;
290 GfxMode( p_sys->i_tty );
292 /* Set keyboard settings */
293 if( tcgetattr(0, &p_vout->p_sys->old_termios) == -1 )
295 msg_Err( p_vout, "tcgetattr failed" );
298 if( tcgetattr(0, &new_termios) == -1 )
300 msg_Err( p_vout, "tcgetattr failed" );
303 /* new_termios.c_lflag &= ~ (ICANON | ISIG);
304 new_termios.c_lflag |= (ECHO | ECHOCTL); */
305 new_termios.c_lflag &= ~ (ICANON);
306 new_termios.c_lflag &= ~(ECHO | ECHOCTL);
307 new_termios.c_iflag = 0;
308 new_termios.c_cc[VMIN] = 1;
309 new_termios.c_cc[VTIME] = 0;
311 if( tcsetattr(0, TCSAFLUSH, &new_termios) == -1 )
313 msg_Err( p_vout, "tcsetattr failed" );
316 ioctl( p_sys->i_tty, VT_RELDISP, VT_ACKACQ );
318 /* Set-up tty signal handler to be aware of tty changes */
319 memset( &sig_tty, 0, sizeof( sig_tty ) );
320 sig_tty.sa_handler = SwitchDisplay;
321 sigemptyset( &sig_tty.sa_mask );
322 if( sigaction( SIGUSR1, &sig_tty, &p_vout->p_sys->sig_usr1 ) ||
323 sigaction( SIGUSR2, &sig_tty, &p_vout->p_sys->sig_usr2 ) )
325 msg_Err( p_vout, "cannot set signal handler (%m)" );
326 tcsetattr(0, 0, &p_vout->p_sys->old_termios);
327 TextMode( p_sys->i_tty );
328 free( p_vout->p_sys );
332 /* Set-up tty according to new signal handler */
333 if( -1 == ioctl( p_sys->i_tty, VT_GETMODE, &p_vout->p_sys->vt_mode ) )
335 msg_Err( p_vout, "cannot get terminal mode (%m)" );
336 sigaction( SIGUSR1, &p_vout->p_sys->sig_usr1, NULL );
337 sigaction( SIGUSR2, &p_vout->p_sys->sig_usr2, NULL );
338 tcsetattr(0, 0, &p_vout->p_sys->old_termios);
339 TextMode( p_sys->i_tty );
340 free( p_vout->p_sys );
343 memcpy( &vt_mode, &p_vout->p_sys->vt_mode, sizeof( vt_mode ) );
344 vt_mode.mode = VT_PROCESS;
346 vt_mode.relsig = SIGUSR1;
347 vt_mode.acqsig = SIGUSR2;
349 if( -1 == ioctl( p_sys->i_tty, VT_SETMODE, &vt_mode ) )
351 msg_Err( p_vout, "cannot set terminal mode (%m)" );
352 sigaction( SIGUSR1, &p_vout->p_sys->sig_usr1, NULL );
353 sigaction( SIGUSR2, &p_vout->p_sys->sig_usr2, NULL );
354 tcsetattr(0, 0, &p_vout->p_sys->old_termios);
355 TextMode( p_sys->i_tty );
356 free( p_vout->p_sys );
361 if( OpenDisplay( p_vout ) )
365 ioctl( p_sys->i_tty, VT_SETMODE, &p_vout->p_sys->vt_mode );
366 sigaction( SIGUSR1, &p_vout->p_sys->sig_usr1, NULL );
367 sigaction( SIGUSR2, &p_vout->p_sys->sig_usr2, NULL );
368 tcsetattr(0, 0, &p_vout->p_sys->old_termios);
369 TextMode( p_sys->i_tty );
371 free( p_vout->p_sys );
379 /*****************************************************************************
380 * Destroy: destroy FB video thread output method
381 *****************************************************************************
382 * Terminate an output method created by Create
383 *****************************************************************************/
384 static void Destroy( vlc_object_t *p_this )
386 vout_thread_t *p_vout = (vout_thread_t *)p_this;
388 CloseDisplay( p_vout );
390 if( p_vout->p_sys->b_tty )
392 /* Reset the terminal */
393 ioctl( p_vout->p_sys->i_tty, VT_SETMODE, &p_vout->p_sys->vt_mode );
395 /* Remove signal handlers */
396 sigaction( SIGUSR1, &p_vout->p_sys->sig_usr1, NULL );
397 sigaction( SIGUSR2, &p_vout->p_sys->sig_usr2, NULL );
399 /* Reset the keyboard state */
400 tcsetattr( 0, 0, &p_vout->p_sys->old_termios );
402 /* Return to text mode */
403 TextMode( p_vout->p_sys->i_tty );
406 /* Destroy structure */
407 free( p_vout->p_sys );
410 /*****************************************************************************
411 * NewPicture: allocate a picture
412 *****************************************************************************
413 * Returns 0 on success, -1 otherwise
414 *****************************************************************************/
415 static int NewPicture( vout_thread_t *p_vout, picture_t *p_pic )
417 /* We know the chroma, allocate a buffer which will be used
418 * directly by the decoder */
419 p_pic->p_sys = malloc( sizeof( picture_sys_t ) );
420 if( p_pic->p_sys == NULL )
425 /* Fill in picture_t fields */
426 vout_InitPicture( VLC_OBJECT(p_vout), p_pic, p_vout->output.i_chroma,
427 p_vout->output.i_width, p_vout->output.i_height,
428 p_vout->output.i_aspect );
430 p_pic->p_sys->p_data = malloc( p_vout->p_sys->i_page_size );
431 if( !p_pic->p_sys->p_data )
433 free( p_pic->p_sys );
438 p_pic->p->p_pixels = (uint8_t*) p_pic->p_sys->p_data;
440 p_pic->p->i_pixel_pitch = p_vout->p_sys->i_bytes_per_pixel;
441 p_pic->p->i_lines = p_vout->p_sys->var_info.yres;
442 p_pic->p->i_visible_lines = p_vout->p_sys->var_info.yres;
444 if( p_vout->p_sys->var_info.xres_virtual )
446 p_pic->p->i_pitch = p_vout->p_sys->var_info.xres_virtual
447 * p_vout->p_sys->i_bytes_per_pixel;
451 p_pic->p->i_pitch = p_vout->p_sys->var_info.xres
452 * p_vout->p_sys->i_bytes_per_pixel;
455 p_pic->p->i_visible_pitch = p_vout->p_sys->var_info.xres
456 * p_vout->p_sys->i_bytes_per_pixel;
462 /*****************************************************************************
463 * FreePicture: destroy a picture allocated with NewPicture
464 *****************************************************************************
465 * Destroy Image AND associated data.
466 *****************************************************************************/
467 static void FreePicture( vout_thread_t *p_vout, picture_t *p_pic )
469 free( p_pic->p_sys->p_data );
470 free( p_pic->p_sys );
474 /*****************************************************************************
475 * Init: initialize framebuffer video thread output method
476 *****************************************************************************/
477 static int Init( vout_thread_t *p_vout )
479 vout_sys_t *p_sys = p_vout->p_sys;
481 picture_t *p_pic = NULL;
483 I_OUTPUTPICTURES = 0;
485 p_vout->output.i_width = p_vout->render.i_width;
486 p_vout->output.i_height = p_vout->render.i_height;
487 p_vout->output.i_aspect = p_vout->render.i_aspect;
489 p_vout->fmt_out = p_vout->fmt_in;
490 if( p_sys->i_chroma == 0 )
492 /* Initialize the output structure: RGB with square pixels, whatever
493 * the input format is, since it's the only format we know */
494 switch( p_sys->var_info.bits_per_pixel )
496 case 8: /* FIXME: set the palette */
497 p_vout->output.i_chroma = VLC_FOURCC('R','G','B','2'); break;
499 p_vout->output.i_chroma = VLC_FOURCC('R','V','1','5'); break;
501 p_vout->output.i_chroma = VLC_FOURCC('R','V','1','6'); break;
503 p_vout->output.i_chroma = VLC_FOURCC('R','V','2','4'); break;
505 p_vout->output.i_chroma = VLC_FOURCC('R','V','3','2'); break;
507 msg_Err( p_vout, "unknown screen depth %i",
508 p_vout->p_sys->var_info.bits_per_pixel );
512 if( p_sys->var_info.bits_per_pixel != 8 )
514 p_vout->output.i_rmask = ( (1 << p_sys->var_info.red.length) - 1 )
515 << p_sys->var_info.red.offset;
516 p_vout->output.i_gmask = ( (1 << p_sys->var_info.green.length) - 1 )
517 << p_sys->var_info.green.offset;
518 p_vout->output.i_bmask = ( (1 << p_sys->var_info.blue.length) - 1 )
519 << p_sys->var_info.blue.offset;
524 p_vout->output.i_chroma = p_sys->i_chroma;
526 p_vout->fmt_out.i_chroma = p_vout->output.i_chroma;
528 p_vout->output.i_width =
529 p_vout->fmt_out.i_width =
530 p_vout->fmt_out.i_visible_width = p_sys->i_width;
531 p_vout->output.i_height =
532 p_vout->fmt_out.i_height =
533 p_vout->fmt_out.i_visible_height = p_sys->i_height;
535 /* Assume we have square pixels */
536 if( p_sys->i_aspect < 0 )
538 p_vout->output.i_aspect = ( p_sys->i_width
539 * VOUT_ASPECT_FACTOR ) / p_sys->i_height;
541 else p_vout->output.i_aspect = p_sys->i_aspect;
543 p_vout->fmt_out.i_sar_num = p_vout->fmt_out.i_sar_den = 1;
544 p_vout->fmt_out.i_aspect = p_vout->render.i_aspect = p_vout->output.i_aspect;
545 p_vout->fmt_out.i_x_offset= p_vout->fmt_out.i_y_offset = 0;
547 /* Clear the screen */
548 memset( p_sys->p_video, 0, p_sys->i_page_size );
550 if( !p_sys->b_hw_accel )
552 /* Try to initialize up to MAX_DIRECTBUFFERS direct buffers */
553 while( I_OUTPUTPICTURES < MAX_DIRECTBUFFERS )
557 /* Find an empty picture slot */
558 for( i_index = 0 ; i_index < VOUT_MAX_PICTURES ; i_index++ )
560 if( p_vout->p_picture[ i_index ].i_status == FREE_PICTURE )
562 p_pic = p_vout->p_picture + i_index;
567 /* Allocate the picture */
568 if( p_pic == NULL || NewPicture( p_vout, p_pic ) )
573 p_pic->i_status = DESTROYED_PICTURE;
574 p_pic->i_type = DIRECT_PICTURE;
576 PP_OUTPUTPICTURE[ I_OUTPUTPICTURES ] = p_pic;
583 /* Try to initialize 1 direct buffer */
586 /* Find an empty picture slot */
587 for( i_index = 0 ; i_index < VOUT_MAX_PICTURES ; i_index++ )
589 if( p_vout->p_picture[ i_index ].i_status == FREE_PICTURE )
591 p_pic = p_vout->p_picture + i_index;
596 /* Allocate the picture */
602 /* We know the chroma, allocate a buffer which will be used
603 * directly by the decoder */
604 p_pic->p->p_pixels = p_vout->p_sys->p_video;
605 p_pic->p->i_pixel_pitch = p_vout->p_sys->i_bytes_per_pixel;
606 p_pic->p->i_lines = p_vout->p_sys->var_info.yres;
607 p_pic->p->i_visible_lines = p_vout->p_sys->var_info.yres;
609 if( p_vout->p_sys->var_info.xres_virtual )
611 p_pic->p->i_pitch = p_vout->p_sys->var_info.xres_virtual
612 * p_vout->p_sys->i_bytes_per_pixel;
616 p_pic->p->i_pitch = p_vout->p_sys->var_info.xres
617 * p_vout->p_sys->i_bytes_per_pixel;
620 p_pic->p->i_visible_pitch = p_vout->p_sys->var_info.xres
621 * p_vout->p_sys->i_bytes_per_pixel;
623 p_pic->i_status = DESTROYED_PICTURE;
624 p_pic->i_type = DIRECT_PICTURE;
626 PP_OUTPUTPICTURE[ I_OUTPUTPICTURES ] = p_pic;
634 /*****************************************************************************
635 * End: terminate framebuffer video thread output method
636 *****************************************************************************/
637 static void End( vout_thread_t *p_vout )
639 if( !p_vout->p_sys->b_hw_accel )
643 /* Free the direct buffers we allocated */
644 for( i_index = I_OUTPUTPICTURES ; i_index ; )
647 FreePicture( p_vout, PP_OUTPUTPICTURE[ i_index ] );
651 /* Clear the screen */
652 memset( p_vout->p_sys->p_video, 0, p_vout->p_sys->i_page_size );
655 /*****************************************************************************
656 * Control: control facility for the vout
657 *****************************************************************************/
658 static int Control( vout_thread_t *p_vout, int i_query, va_list args )
663 return vout_vaControlDefault( p_vout, i_query, args );
667 /*****************************************************************************
668 * Manage: handle FB events
669 *****************************************************************************
670 * This function should be called regularly by video output thread. It manages
671 * console events. It returns a non null value on error.
672 *****************************************************************************/
673 static int Manage( vout_thread_t *p_vout )
678 if ( read(0, &buf, 1) == 1)
683 vlc_object_kill( p_vout->p_libvlc );
695 if( p_vout->i_changes & VOUT_SIZE_CHANGE )
697 msg_Dbg( p_vout, "reinitializing framebuffer screen" );
698 p_vout->i_changes &= ~VOUT_SIZE_CHANGE;
700 /* Destroy XImages to change their size */
703 /* Recreate XImages. If SysInit failed, the thread can't go on. */
706 msg_Err( p_vout, "cannot reinit framebuffer screen" );
711 memset( p_vout->p_sys->p_video, 0, p_vout->p_sys->i_page_size );
714 /* Tell the video output thread that it will need to rebuild YUV
715 * tables. This is needed since conversion buffer size may have changed */
716 p_vout->i_changes |= VOUT_YUV_CHANGE;
723 /*****************************************************************************
724 * Display: displays previously rendered output
725 *****************************************************************************
726 * This function send the currently rendered image to FB image, waits until
727 * it is displayed and switch the two rendering buffers, preparing next frame.
728 *****************************************************************************/
729 static void Display( vout_thread_t *p_vout, picture_t *p_pic )
732 /* swap the two Y offsets if the drivers supports panning */
733 if( p_vout->p_sys->b_pan )
735 p_vout->p_sys->var_info.yoffset = 0;
736 /*p_vout->p_sys->var_info.yoffset = p_vout->p_sys->var_info.yres; */
738 /* the X offset should be 0, but who knows ...
739 * some other app might have played with the framebuffer */
740 p_vout->p_sys->var_info.xoffset = 0;
744 ioctl( p_vout->p_sys->i_fd,
745 FBIOPAN_DISPLAY, &p_vout->p_sys->var_info );
750 if( !p_vout->p_sys->b_hw_accel )
752 vlc_memcpy( p_vout->p_sys->p_video, p_pic->p->p_pixels,
753 p_vout->p_sys->i_page_size );
758 static void SetPalette( vout_thread_t *p_vout, uint16_t *red, uint16_t *green,
759 uint16_t *blue, uint16_t *transp )
761 struct fb_cmap cmap = { 0, 256, red, green, blue, transp };
763 ioctl( p_vout->p_sys->i_fd, FBIOPUTCMAP, &cmap );
767 /* following functions are local */
769 /*****************************************************************************
770 * OpenDisplay: initialize framebuffer
771 *****************************************************************************/
772 static int OpenDisplay( vout_thread_t *p_vout )
774 vout_sys_t *p_sys = (vout_sys_t *) p_vout->p_sys;
775 char *psz_device; /* framebuffer device path */
776 struct fb_fix_screeninfo fix_info; /* framebuffer fix information */
778 /* Open framebuffer device */
779 if( !(psz_device = config_GetPsz( p_vout, FB_DEV_VAR )) )
781 msg_Err( p_vout, "don't know which fb device to open" );
785 p_sys->i_fd = open( psz_device, O_RDWR);
786 if( p_sys->i_fd == -1 )
788 msg_Err( p_vout, "cannot open %s (%m)", psz_device );
795 /* Get framebuffer device information */
796 if( ioctl( p_sys->i_fd, FBIOGET_VSCREENINFO, &p_sys->var_info ) )
798 msg_Err( p_vout, "cannot get fb info (%m)" );
799 close( p_sys->i_fd );
803 memcpy( &p_sys->old_info, &p_sys->var_info,
804 sizeof( struct fb_var_screeninfo ) );
806 /* Get some info on the framebuffer itself */
809 p_sys->var_info.xres = p_sys->var_info.xres_virtual = p_sys->i_width;
810 p_sys->var_info.yres = p_sys->var_info.yres_virtual = p_sys->i_height;
811 p_vout->fmt_out.i_width = p_sys->i_width;
812 p_vout->fmt_out.i_height = p_sys->i_height;
815 /* Set some attributes */
816 p_sys->var_info.activate = p_sys->b_tty
817 ? FB_ACTIVATE_NXTOPEN
819 p_sys->var_info.xoffset = 0;
820 p_sys->var_info.yoffset = 0;
822 if( ioctl( p_sys->i_fd, FBIOPUT_VSCREENINFO, &p_sys->var_info ) )
824 msg_Err( p_vout, "cannot set fb info (%m)" );
825 close( p_sys->i_fd );
829 /* Get some information again, in the definitive configuration */
830 if( ioctl( p_sys->i_fd, FBIOGET_FSCREENINFO, &fix_info )
831 || ioctl( p_sys->i_fd, FBIOGET_VSCREENINFO, &p_sys->var_info ) )
833 msg_Err( p_vout, "cannot get additional fb info (%m)" );
835 /* Restore fb config */
836 ioctl( p_sys->i_fd, FBIOPUT_VSCREENINFO, &p_sys->old_info );
838 close( p_sys->i_fd );
842 /* If the fb has limitations on mode change,
843 * then keep the resolution of the fb */
844 if( (p_sys->i_height != p_sys->var_info.yres) ||
845 (p_sys->i_width != p_sys->var_info.xres) )
847 p_sys->b_auto = true;
849 "using framebuffer native resolution instead of requested (%ix%i)",
850 p_sys->i_width, p_sys->i_height );
852 p_sys->i_height = p_sys->var_info.yres;
853 p_sys->i_width = p_sys->var_info.xres_virtual
854 ? p_sys->var_info.xres_virtual
855 : p_sys->var_info.xres;
857 /* FIXME: if the image is full-size, it gets cropped on the left
858 * because of the xres / xres_virtual slight difference */
859 msg_Dbg( p_vout, "%ix%i (virtual %ix%i) (request %ix%i)",
860 p_sys->var_info.xres, p_sys->var_info.yres,
861 p_sys->var_info.xres_virtual,
862 p_sys->var_info.yres_virtual,
863 p_sys->i_width, p_sys->i_height );
865 p_sys->p_palette = NULL;
866 p_sys->b_pan = ( fix_info.ypanstep || fix_info.ywrapstep );
868 switch( p_sys->var_info.bits_per_pixel )
871 p_sys->p_palette = malloc( 8 * 256 * sizeof( uint16_t ) );
872 if( !p_sys->p_palette )
874 /* Restore fb config */
875 ioctl( p_sys->i_fd, FBIOPUT_VSCREENINFO, &p_sys->old_info );
877 close( p_sys->i_fd );
880 p_sys->fb_cmap.start = 0;
881 p_sys->fb_cmap.len = 256;
882 p_sys->fb_cmap.red = p_sys->p_palette;
883 p_sys->fb_cmap.green = p_sys->p_palette + 256 * sizeof( uint16_t );
884 p_sys->fb_cmap.blue = p_sys->p_palette + 2 * 256 * sizeof( uint16_t );
885 p_sys->fb_cmap.transp = p_sys->p_palette + 3 * 256 * sizeof( uint16_t );
887 /* Save the colormap */
888 ioctl( p_sys->i_fd, FBIOGETCMAP, &p_sys->fb_cmap );
890 p_sys->i_bytes_per_pixel = 1;
895 p_sys->i_bytes_per_pixel = 2;
899 p_sys->i_bytes_per_pixel = 3;
903 p_sys->i_bytes_per_pixel = 4;
907 msg_Err( p_vout, "screen depth %d is not supported",
908 p_sys->var_info.bits_per_pixel );
910 /* Restore fb config */
911 ioctl( p_sys->i_fd, FBIOPUT_VSCREENINFO, &p_sys->old_info );
913 close( p_sys->i_fd );
917 p_sys->i_page_size = p_sys->i_width * p_sys->i_height *
918 p_sys->i_bytes_per_pixel;
920 /* Map a framebuffer at the beginning */
921 p_sys->p_video = mmap( 0, p_sys->i_page_size,
922 PROT_READ | PROT_WRITE, MAP_SHARED,
925 if( p_sys->p_video == ((void*)-1) )
927 msg_Err( p_vout, "cannot map video memory (%m)" );
929 if( p_sys->var_info.bits_per_pixel == 8 )
931 free( p_sys->p_palette );
932 p_sys->p_palette = NULL;
935 /* Restore fb config */
936 ioctl( p_sys->i_fd, FBIOPUT_VSCREENINFO, &p_vout->p_sys->old_info );
938 close( p_sys->i_fd );
942 msg_Dbg( p_vout, "framebuffer type=%d, visual=%d, ypanstep=%d, "
943 "ywrap=%d, accel=%d", fix_info.type, fix_info.visual,
944 fix_info.ypanstep, fix_info.ywrapstep, fix_info.accel );
948 /*****************************************************************************
949 * CloseDisplay: terminate FB video thread output method
950 *****************************************************************************/
951 static void CloseDisplay( vout_thread_t *p_vout )
954 memset( p_vout->p_sys->p_video, 0, p_vout->p_sys->i_page_size );
956 /* Restore palette */
957 if( p_vout->p_sys->var_info.bits_per_pixel == 8 )
959 ioctl( p_vout->p_sys->i_fd,
960 FBIOPUTCMAP, &p_vout->p_sys->fb_cmap );
961 free( p_vout->p_sys->p_palette );
962 p_vout->p_sys->p_palette = NULL;
965 /* Restore fb config */
966 ioctl( p_vout->p_sys->i_fd,
967 FBIOPUT_VSCREENINFO, &p_vout->p_sys->old_info );
970 close( p_vout->p_sys->i_fd );
973 /*****************************************************************************
974 * SwitchDisplay: VT change signal handler
975 *****************************************************************************
976 * This function activates or deactivates the output of the thread. It is
977 * called by the VT driver, on terminal change.
978 *****************************************************************************/
979 static void SwitchDisplay( int i_signal )
982 vout_thread_t *p_vout;
984 vlc_mutex_lock( &p_vout_bank->lock );
986 /* XXX: only test the first video output */
987 if( p_vout_bank->i_count )
989 p_vout = p_vout_bank->pp_vout[0];
993 case SIGUSR1: /* vt has been released */
994 p_vout->b_active = 0;
995 ioctl( p_sys->i_tty, VT_RELDISP, 1 );
997 case SIGUSR2: /* vt has been acquired */
998 p_vout->b_active = 1;
999 ioctl( p_sys->i_tty, VT_RELDISP, VT_ACTIVATE );
1000 /* handle blanking */
1001 vlc_mutex_lock( &p_vout->change_lock );
1002 p_vout->i_changes |= VOUT_SIZE_CHANGE;
1003 vlc_mutex_unlock( &p_vout->change_lock );
1008 vlc_mutex_unlock( &p_vout_bank->lock );
1012 /*****************************************************************************
1013 * TextMode and GfxMode : switch tty to text/graphic mode
1014 *****************************************************************************
1015 * These functions toggle the tty mode.
1016 *****************************************************************************/
1017 static void TextMode( int i_tty )
1019 /* return to text mode */
1020 if( -1 == ioctl(i_tty, KDSETMODE, KD_TEXT) )
1022 /*msg_Err( p_vout, "failed ioctl KDSETMODE KD_TEXT" );*/
1026 static void GfxMode( int i_tty )
1028 /* switch to graphic mode */
1029 if( -1 == ioctl(i_tty, KDSETMODE, KD_GRAPHICS) )
1031 /*msg_Err( p_vout, "failed ioctl KDSETMODE KD_GRAPHICS" );*/