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>
49 #include <vlc_interface.h>
51 /*****************************************************************************
53 *****************************************************************************/
54 static int Create ( vlc_object_t * );
55 static void Destroy ( vlc_object_t * );
57 static int Init ( vout_thread_t * );
58 static void End ( vout_thread_t * );
59 static int Manage ( vout_thread_t * );
60 static void Display ( vout_thread_t *, picture_t * );
61 static int Control ( vout_thread_t *, int, va_list );
63 static int NewPicture ( vout_thread_t *, picture_t * );
64 static void FreePicture ( vout_thread_t *, picture_t * );
66 static int OpenDisplay ( vout_thread_t * );
67 static void CloseDisplay ( vout_thread_t * );
68 static void SwitchDisplay ( int i_signal );
69 static void TextMode ( int i_tty );
70 static void GfxMode ( int i_tty );
72 #define MAX_DIRECTBUFFERS 1
74 /*****************************************************************************
76 *****************************************************************************/
77 #define FB_DEV_VAR "fbdev"
79 #define DEVICE_TEXT N_("Framebuffer device")
80 #define DEVICE_LONGTEXT N_( \
81 "Framebuffer device to use for rendering (usually /dev/fb0).")
83 #define TTY_TEXT N_("Run fb on current tty.")
84 #define TTY_LONGTEXT N_( \
85 "Run framebuffer on current TTY device (default enabled). " \
86 "(disable tty handling with caution)" )
88 #define CHROMA_TEXT N_("Chroma used.")
89 #define CHROMA_LONGTEXT N_( \
90 "Force use of a specific chroma for output. Default is I420." )
92 #define ASPECT_RATIO_TEXT N_("Video aspect ratio")
93 #define ASPECT_RATIO_LONGTEXT N_( \
94 "Aspect ratio of the video image (4:3, 16:9). Default is square pixels." )
96 #define FB_MODE_TEXT N_("Framebuffer resolution to use.")
97 #define FB_MODE_LONGTEXT N_( \
98 "Select the resolution for the framebuffer. Currently it supports " \
99 "the values 0=QCIF 1=CIF 2=NTSC 3=PAL, 4=auto (default 4=auto)" )
101 #define HW_ACCEL_TEXT N_("Framebuffer uses hw acceleration.")
102 #define HW_ACCEL_LONGTEXT N_( \
103 "If your framebuffer supports hardware acceleration or does double buffering " \
104 "in hardware then you must disable this option. It then does double buffering " \
108 set_shortname( "Framebuffer" )
109 set_category( CAT_VIDEO )
110 set_subcategory( SUBCAT_VIDEO_VOUT )
111 add_file( FB_DEV_VAR, "/dev/fb0", NULL, DEVICE_TEXT, DEVICE_LONGTEXT,
113 add_bool( "fb-tty", 1, NULL, TTY_TEXT, TTY_LONGTEXT, true )
114 add_string( "fb-chroma", NULL, NULL, CHROMA_TEXT, CHROMA_LONGTEXT,
116 add_string( "fb-aspect-ratio", NULL, NULL, ASPECT_RATIO_TEXT,
117 ASPECT_RATIO_LONGTEXT, true )
118 add_integer( "fb-mode", 4, NULL, FB_MODE_TEXT, FB_MODE_LONGTEXT,
120 add_bool( "fb-hw-accel", true, NULL, HW_ACCEL_TEXT, HW_ACCEL_LONGTEXT,
122 set_description( N_("GNU/Linux framebuffer video output") )
123 set_capability( "video output", 30 )
124 set_callbacks( Create, Destroy )
127 /*****************************************************************************
128 * vout_sys_t: video output framebuffer method descriptor
129 *****************************************************************************
130 * This structure is part of the video output thread descriptor.
131 * It describes the FB specific properties of an output thread.
132 *****************************************************************************/
135 /* System information */
136 int i_tty; /* tty device handle */
138 struct termios old_termios;
140 /* Original configuration information */
141 struct sigaction sig_usr1; /* USR1 previous handler */
142 struct sigaction sig_usr2; /* USR2 previous handler */
143 struct vt_mode vt_mode; /* previous VT mode */
145 /* Framebuffer information */
146 int i_fd; /* device handle */
147 struct fb_var_screeninfo old_info; /* original mode information */
148 struct fb_var_screeninfo var_info; /* current mode information */
149 bool b_pan; /* does device supports panning ? */
150 struct fb_cmap fb_cmap; /* original colormap */
151 uint16_t *p_palette; /* original palette */
152 bool b_hw_accel; /* has hardware support */
154 /* Video information */
158 int i_bytes_per_pixel;
159 bool b_auto; /* Automatically adjust video size to fb size */
160 vlc_fourcc_t i_chroma;
163 uint8_t *p_video; /* base adress */
164 size_t i_page_size; /* page size */
169 uint8_t * p_data; /* base adress */
172 /*****************************************************************************
173 * Create: allocates FB video thread output method
174 *****************************************************************************
175 * This function allocates and initializes a FB vout method.
176 *****************************************************************************/
177 static int Create( vlc_object_t *p_this )
179 vout_thread_t *p_vout = (vout_thread_t *)p_this;
184 struct sigaction sig_tty; /* sigaction for tty change */
185 struct vt_mode vt_mode; /* vt current mode */
186 struct termios new_termios;
188 /* Allocate instance and initialize some members */
189 p_vout->p_sys = p_sys = malloc( sizeof( vout_sys_t ) );
190 if( p_vout->p_sys == NULL )
192 memset( p_sys, 0, sizeof(vout_sys_t) );
194 p_vout->pf_init = Init;
195 p_vout->pf_end = End;
196 p_vout->pf_manage = Manage;
197 p_vout->pf_render = NULL;
198 p_vout->pf_display = Display;
199 p_vout->pf_control = Control;
201 /* Does the framebuffer uses hw acceleration? */
202 p_sys->b_hw_accel = var_CreateGetBool( p_vout, "fb-hw-accel" );
204 /* Set tty and fb devices */
205 p_sys->i_tty = 0; /* 0 == /dev/tty0 == current console */
206 p_sys->b_tty = var_CreateGetBool( p_vout, "fb-tty" );
208 #if defined(HAVE_ISATTY)
209 /* Check that stdin is a TTY */
210 if( p_sys->b_tty && !isatty( 0 ) )
212 msg_Warn( p_vout, "fd 0 is not a TTY" );
217 msg_Warn( p_vout, "disabling tty handling, use with caution because "
218 "there is no way to return to the tty." );
223 psz_chroma = var_CreateGetNonEmptyString( p_vout, "fb-chroma" );
226 if( strlen( psz_chroma ) == 4 )
228 p_sys->i_chroma = VLC_FOURCC( psz_chroma[0],
232 msg_Dbg( p_vout, "forcing chroma '%s'", psz_chroma );
236 msg_Warn( p_vout, "invalid chroma (%s), using defaults.",
243 p_sys->i_aspect = -1;
244 psz_aspect = var_CreateGetNonEmptyString( p_vout, "fb-aspect-ratio" );
247 char *psz_parser = strchr( psz_aspect, ':' );
251 *psz_parser++ = '\0';
252 p_sys->i_aspect = ( atoi( psz_aspect )
253 * VOUT_ASPECT_FACTOR ) / atoi( psz_parser );
255 msg_Dbg( p_vout, "using aspect ratio %d:%d",
256 atoi( psz_aspect ), atoi( psz_parser ) );
262 p_sys->b_auto = false;
263 i_mode = var_CreateGetInteger( p_vout, "fb-mode" );
267 p_sys->i_width = 176;
268 p_sys->i_height = 144;
271 p_sys->i_width = 352;
272 p_sys->i_height = 288;
275 p_sys->i_width = 640;
276 p_sys->i_height = 480;
279 p_sys->i_width = 704;
280 p_sys->i_height = 576;
284 p_sys->b_auto = true;
291 GfxMode( p_sys->i_tty );
293 /* Set keyboard settings */
294 if( tcgetattr(0, &p_vout->p_sys->old_termios) == -1 )
296 msg_Err( p_vout, "tcgetattr failed" );
299 if( tcgetattr(0, &new_termios) == -1 )
301 msg_Err( p_vout, "tcgetattr failed" );
304 /* new_termios.c_lflag &= ~ (ICANON | ISIG);
305 new_termios.c_lflag |= (ECHO | ECHOCTL); */
306 new_termios.c_lflag &= ~ (ICANON);
307 new_termios.c_lflag &= ~(ECHO | ECHOCTL);
308 new_termios.c_iflag = 0;
309 new_termios.c_cc[VMIN] = 1;
310 new_termios.c_cc[VTIME] = 0;
312 if( tcsetattr(0, TCSAFLUSH, &new_termios) == -1 )
314 msg_Err( p_vout, "tcsetattr failed" );
317 ioctl( p_sys->i_tty, VT_RELDISP, VT_ACKACQ );
319 /* Set-up tty signal handler to be aware of tty changes */
320 memset( &sig_tty, 0, sizeof( sig_tty ) );
321 sig_tty.sa_handler = SwitchDisplay;
322 sigemptyset( &sig_tty.sa_mask );
323 if( sigaction( SIGUSR1, &sig_tty, &p_vout->p_sys->sig_usr1 ) ||
324 sigaction( SIGUSR2, &sig_tty, &p_vout->p_sys->sig_usr2 ) )
326 msg_Err( p_vout, "cannot set signal handler (%m)" );
327 tcsetattr(0, 0, &p_vout->p_sys->old_termios);
328 TextMode( p_sys->i_tty );
329 free( p_vout->p_sys );
333 /* Set-up tty according to new signal handler */
334 if( -1 == ioctl( p_sys->i_tty, VT_GETMODE, &p_vout->p_sys->vt_mode ) )
336 msg_Err( p_vout, "cannot get terminal mode (%m)" );
337 sigaction( SIGUSR1, &p_vout->p_sys->sig_usr1, NULL );
338 sigaction( SIGUSR2, &p_vout->p_sys->sig_usr2, NULL );
339 tcsetattr(0, 0, &p_vout->p_sys->old_termios);
340 TextMode( p_sys->i_tty );
341 free( p_vout->p_sys );
344 memcpy( &vt_mode, &p_vout->p_sys->vt_mode, sizeof( vt_mode ) );
345 vt_mode.mode = VT_PROCESS;
347 vt_mode.relsig = SIGUSR1;
348 vt_mode.acqsig = SIGUSR2;
350 if( -1 == ioctl( p_sys->i_tty, VT_SETMODE, &vt_mode ) )
352 msg_Err( p_vout, "cannot set terminal mode (%m)" );
353 sigaction( SIGUSR1, &p_vout->p_sys->sig_usr1, NULL );
354 sigaction( SIGUSR2, &p_vout->p_sys->sig_usr2, NULL );
355 tcsetattr(0, 0, &p_vout->p_sys->old_termios);
356 TextMode( p_sys->i_tty );
357 free( p_vout->p_sys );
362 if( OpenDisplay( p_vout ) )
366 ioctl( p_sys->i_tty, VT_SETMODE, &p_vout->p_sys->vt_mode );
367 sigaction( SIGUSR1, &p_vout->p_sys->sig_usr1, NULL );
368 sigaction( SIGUSR2, &p_vout->p_sys->sig_usr2, NULL );
369 tcsetattr(0, 0, &p_vout->p_sys->old_termios);
370 TextMode( p_sys->i_tty );
372 free( p_vout->p_sys );
380 /*****************************************************************************
381 * Destroy: destroy FB video thread output method
382 *****************************************************************************
383 * Terminate an output method created by Create
384 *****************************************************************************/
385 static void Destroy( vlc_object_t *p_this )
387 vout_thread_t *p_vout = (vout_thread_t *)p_this;
389 CloseDisplay( p_vout );
391 if( p_vout->p_sys->b_tty )
393 /* Reset the terminal */
394 ioctl( p_vout->p_sys->i_tty, VT_SETMODE, &p_vout->p_sys->vt_mode );
396 /* Remove signal handlers */
397 sigaction( SIGUSR1, &p_vout->p_sys->sig_usr1, NULL );
398 sigaction( SIGUSR2, &p_vout->p_sys->sig_usr2, NULL );
400 /* Reset the keyboard state */
401 tcsetattr( 0, 0, &p_vout->p_sys->old_termios );
403 /* Return to text mode */
404 TextMode( p_vout->p_sys->i_tty );
407 /* Destroy structure */
408 free( p_vout->p_sys );
411 /*****************************************************************************
412 * NewPicture: allocate a picture
413 *****************************************************************************
414 * Returns 0 on success, -1 otherwise
415 *****************************************************************************/
416 static int NewPicture( vout_thread_t *p_vout, picture_t *p_pic )
418 /* We know the chroma, allocate a buffer which will be used
419 * directly by the decoder */
420 p_pic->p_sys = malloc( sizeof( picture_sys_t ) );
421 if( p_pic->p_sys == NULL )
426 /* Fill in picture_t fields */
427 vout_InitPicture( VLC_OBJECT(p_vout), p_pic, p_vout->output.i_chroma,
428 p_vout->output.i_width, p_vout->output.i_height,
429 p_vout->output.i_aspect );
431 p_pic->p_sys->p_data = malloc( p_vout->p_sys->i_page_size );
432 if( !p_pic->p_sys->p_data )
434 free( p_pic->p_sys );
439 p_pic->p->p_pixels = (uint8_t*) p_pic->p_sys->p_data;
441 p_pic->p->i_pixel_pitch = p_vout->p_sys->i_bytes_per_pixel;
442 p_pic->p->i_lines = p_vout->p_sys->var_info.yres;
443 p_pic->p->i_visible_lines = p_vout->p_sys->var_info.yres;
445 if( p_vout->p_sys->var_info.xres_virtual )
447 p_pic->p->i_pitch = p_vout->p_sys->var_info.xres_virtual
448 * p_vout->p_sys->i_bytes_per_pixel;
452 p_pic->p->i_pitch = p_vout->p_sys->var_info.xres
453 * p_vout->p_sys->i_bytes_per_pixel;
456 p_pic->p->i_visible_pitch = p_vout->p_sys->var_info.xres
457 * p_vout->p_sys->i_bytes_per_pixel;
463 /*****************************************************************************
464 * FreePicture: destroy a picture allocated with NewPicture
465 *****************************************************************************
466 * Destroy Image AND associated data.
467 *****************************************************************************/
468 static void FreePicture( vout_thread_t *p_vout, picture_t *p_pic )
471 free( p_pic->p_sys->p_data );
472 free( p_pic->p_sys );
476 /*****************************************************************************
477 * Init: initialize framebuffer video thread output method
478 *****************************************************************************/
479 static int Init( vout_thread_t *p_vout )
481 vout_sys_t *p_sys = p_vout->p_sys;
483 picture_t *p_pic = NULL;
485 I_OUTPUTPICTURES = 0;
487 p_vout->output.i_width = p_vout->render.i_width;
488 p_vout->output.i_height = p_vout->render.i_height;
489 p_vout->output.i_aspect = p_vout->render.i_aspect;
491 p_vout->fmt_out = p_vout->fmt_in;
492 if( p_sys->i_chroma == 0 )
494 /* Initialize the output structure: RGB with square pixels, whatever
495 * the input format is, since it's the only format we know */
496 switch( p_sys->var_info.bits_per_pixel )
498 case 8: /* FIXME: set the palette */
499 p_vout->output.i_chroma = VLC_FOURCC('R','G','B','2'); break;
501 p_vout->output.i_chroma = VLC_FOURCC('R','V','1','5'); break;
503 p_vout->output.i_chroma = VLC_FOURCC('R','V','1','6'); break;
505 p_vout->output.i_chroma = VLC_FOURCC('R','V','2','4'); break;
507 p_vout->output.i_chroma = VLC_FOURCC('R','V','3','2'); break;
509 msg_Err( p_vout, "unknown screen depth %i",
510 p_vout->p_sys->var_info.bits_per_pixel );
514 if( p_sys->var_info.bits_per_pixel != 8 )
516 p_vout->output.i_rmask = ( (1 << p_sys->var_info.red.length) - 1 )
517 << p_sys->var_info.red.offset;
518 p_vout->output.i_gmask = ( (1 << p_sys->var_info.green.length) - 1 )
519 << p_sys->var_info.green.offset;
520 p_vout->output.i_bmask = ( (1 << p_sys->var_info.blue.length) - 1 )
521 << p_sys->var_info.blue.offset;
526 p_vout->output.i_chroma = p_sys->i_chroma;
528 p_vout->fmt_out.i_chroma = p_vout->output.i_chroma;
530 p_vout->output.i_width =
531 p_vout->fmt_out.i_width =
532 p_vout->fmt_out.i_visible_width = p_sys->i_width;
533 p_vout->output.i_height =
534 p_vout->fmt_out.i_height =
535 p_vout->fmt_out.i_visible_height = p_sys->i_height;
537 /* Assume we have square pixels */
538 if( p_sys->i_aspect < 0 )
540 p_vout->output.i_aspect = ( p_sys->i_width
541 * VOUT_ASPECT_FACTOR ) / p_sys->i_height;
543 else p_vout->output.i_aspect = p_sys->i_aspect;
545 p_vout->fmt_out.i_sar_num = p_vout->fmt_out.i_sar_den = 1;
546 p_vout->fmt_out.i_aspect = p_vout->render.i_aspect = p_vout->output.i_aspect;
547 p_vout->fmt_out.i_x_offset= p_vout->fmt_out.i_y_offset = 0;
549 /* Clear the screen */
550 memset( p_sys->p_video, 0, p_sys->i_page_size );
552 if( !p_sys->b_hw_accel )
554 /* Try to initialize up to MAX_DIRECTBUFFERS direct buffers */
555 while( I_OUTPUTPICTURES < MAX_DIRECTBUFFERS )
559 /* Find an empty picture slot */
560 for( i_index = 0 ; i_index < VOUT_MAX_PICTURES ; i_index++ )
562 if( p_vout->p_picture[ i_index ].i_status == FREE_PICTURE )
564 p_pic = p_vout->p_picture + i_index;
569 /* Allocate the picture */
570 if( p_pic == NULL || NewPicture( p_vout, p_pic ) )
575 p_pic->i_status = DESTROYED_PICTURE;
576 p_pic->i_type = DIRECT_PICTURE;
578 PP_OUTPUTPICTURE[ I_OUTPUTPICTURES ] = p_pic;
585 /* Try to initialize 1 direct buffer */
588 /* Find an empty picture slot */
589 for( i_index = 0 ; i_index < VOUT_MAX_PICTURES ; i_index++ )
591 if( p_vout->p_picture[ i_index ].i_status == FREE_PICTURE )
593 p_pic = p_vout->p_picture + i_index;
598 /* Allocate the picture */
604 /* We know the chroma, allocate a buffer which will be used
605 * directly by the decoder */
606 p_pic->p->p_pixels = p_vout->p_sys->p_video;
607 p_pic->p->i_pixel_pitch = p_vout->p_sys->i_bytes_per_pixel;
608 p_pic->p->i_lines = p_vout->p_sys->var_info.yres;
609 p_pic->p->i_visible_lines = p_vout->p_sys->var_info.yres;
611 if( p_vout->p_sys->var_info.xres_virtual )
613 p_pic->p->i_pitch = p_vout->p_sys->var_info.xres_virtual
614 * p_vout->p_sys->i_bytes_per_pixel;
618 p_pic->p->i_pitch = p_vout->p_sys->var_info.xres
619 * p_vout->p_sys->i_bytes_per_pixel;
622 p_pic->p->i_visible_pitch = p_vout->p_sys->var_info.xres
623 * p_vout->p_sys->i_bytes_per_pixel;
625 p_pic->i_status = DESTROYED_PICTURE;
626 p_pic->i_type = DIRECT_PICTURE;
628 PP_OUTPUTPICTURE[ I_OUTPUTPICTURES ] = p_pic;
636 /*****************************************************************************
637 * End: terminate framebuffer video thread output method
638 *****************************************************************************/
639 static void End( vout_thread_t *p_vout )
641 if( !p_vout->p_sys->b_hw_accel )
645 /* Free the direct buffers we allocated */
646 for( i_index = I_OUTPUTPICTURES ; i_index ; )
649 FreePicture( p_vout, PP_OUTPUTPICTURE[ i_index ] );
653 /* Clear the screen */
654 memset( p_vout->p_sys->p_video, 0, p_vout->p_sys->i_page_size );
657 /*****************************************************************************
658 * Control: control facility for the vout
659 *****************************************************************************/
660 static int Control( vout_thread_t *p_vout, int i_query, va_list args )
665 return vout_vaControlDefault( p_vout, i_query, args );
669 /*****************************************************************************
670 * Manage: handle FB events
671 *****************************************************************************
672 * This function should be called regularly by video output thread. It manages
673 * console events. It returns a non null value on error.
674 *****************************************************************************/
675 static int Manage( vout_thread_t *p_vout )
680 if ( read(0, &buf, 1) == 1)
685 libvlc_Quit( p_vout->p_libvlc );
697 if( p_vout->i_changes & VOUT_SIZE_CHANGE )
699 msg_Dbg( p_vout, "reinitializing framebuffer screen" );
700 p_vout->i_changes &= ~VOUT_SIZE_CHANGE;
702 /* Destroy XImages to change their size */
705 /* Recreate XImages. If SysInit failed, the thread can't go on. */
708 msg_Err( p_vout, "cannot reinit framebuffer screen" );
713 memset( p_vout->p_sys->p_video, 0, p_vout->p_sys->i_page_size );
716 /* Tell the video output thread that it will need to rebuild YUV
717 * tables. This is needed since conversion buffer size may have changed */
718 p_vout->i_changes |= VOUT_YUV_CHANGE;
725 /*****************************************************************************
726 * Display: displays previously rendered output
727 *****************************************************************************
728 * This function send the currently rendered image to FB image, waits until
729 * it is displayed and switch the two rendering buffers, preparing next frame.
730 *****************************************************************************/
731 static void Display( vout_thread_t *p_vout, picture_t *p_pic )
734 /* swap the two Y offsets if the drivers supports panning */
735 if( p_vout->p_sys->b_pan )
737 p_vout->p_sys->var_info.yoffset = 0;
738 /*p_vout->p_sys->var_info.yoffset = p_vout->p_sys->var_info.yres; */
740 /* the X offset should be 0, but who knows ...
741 * some other app might have played with the framebuffer */
742 p_vout->p_sys->var_info.xoffset = 0;
746 ioctl( p_vout->p_sys->i_fd,
747 FBIOPAN_DISPLAY, &p_vout->p_sys->var_info );
752 if( !p_vout->p_sys->b_hw_accel )
754 vlc_memcpy( p_vout->p_sys->p_video, p_pic->p->p_pixels,
755 p_vout->p_sys->i_page_size );
760 static void SetPalette( vout_thread_t *p_vout, uint16_t *red, uint16_t *green,
761 uint16_t *blue, uint16_t *transp )
763 struct fb_cmap cmap = { 0, 256, red, green, blue, transp };
765 ioctl( p_vout->p_sys->i_fd, FBIOPUTCMAP, &cmap );
769 /* following functions are local */
771 /*****************************************************************************
772 * OpenDisplay: initialize framebuffer
773 *****************************************************************************/
774 static int OpenDisplay( vout_thread_t *p_vout )
776 vout_sys_t *p_sys = (vout_sys_t *) p_vout->p_sys;
777 char *psz_device; /* framebuffer device path */
778 struct fb_fix_screeninfo fix_info; /* framebuffer fix information */
780 /* Open framebuffer device */
781 if( !(psz_device = config_GetPsz( p_vout, FB_DEV_VAR )) )
783 msg_Err( p_vout, "don't know which fb device to open" );
787 p_sys->i_fd = open( psz_device, O_RDWR);
788 if( p_sys->i_fd == -1 )
790 msg_Err( p_vout, "cannot open %s (%m)", psz_device );
797 /* Get framebuffer device information */
798 if( ioctl( p_sys->i_fd, FBIOGET_VSCREENINFO, &p_sys->var_info ) )
800 msg_Err( p_vout, "cannot get fb info (%m)" );
801 close( p_sys->i_fd );
805 memcpy( &p_sys->old_info, &p_sys->var_info,
806 sizeof( struct fb_var_screeninfo ) );
808 /* Get some info on the framebuffer itself */
811 p_sys->var_info.xres = p_sys->var_info.xres_virtual = p_sys->i_width;
812 p_sys->var_info.yres = p_sys->var_info.yres_virtual = p_sys->i_height;
813 p_vout->fmt_out.i_width = p_sys->i_width;
814 p_vout->fmt_out.i_height = p_sys->i_height;
817 /* Set some attributes */
818 p_sys->var_info.activate = p_sys->b_tty
819 ? FB_ACTIVATE_NXTOPEN
821 p_sys->var_info.xoffset = 0;
822 p_sys->var_info.yoffset = 0;
824 if( ioctl( p_sys->i_fd, FBIOPUT_VSCREENINFO, &p_sys->var_info ) )
826 msg_Err( p_vout, "cannot set fb info (%m)" );
827 close( p_sys->i_fd );
831 /* Get some information again, in the definitive configuration */
832 if( ioctl( p_sys->i_fd, FBIOGET_FSCREENINFO, &fix_info )
833 || ioctl( p_sys->i_fd, FBIOGET_VSCREENINFO, &p_sys->var_info ) )
835 msg_Err( p_vout, "cannot get additional fb info (%m)" );
837 /* Restore fb config */
838 ioctl( p_sys->i_fd, FBIOPUT_VSCREENINFO, &p_sys->old_info );
840 close( p_sys->i_fd );
844 /* If the fb has limitations on mode change,
845 * then keep the resolution of the fb */
846 if( (p_sys->i_height != p_sys->var_info.yres) ||
847 (p_sys->i_width != p_sys->var_info.xres) )
849 p_sys->b_auto = true;
851 "using framebuffer native resolution instead of requested (%ix%i)",
852 p_sys->i_width, p_sys->i_height );
854 p_sys->i_height = p_sys->var_info.yres;
855 p_sys->i_width = p_sys->var_info.xres_virtual
856 ? p_sys->var_info.xres_virtual
857 : p_sys->var_info.xres;
859 /* FIXME: if the image is full-size, it gets cropped on the left
860 * because of the xres / xres_virtual slight difference */
861 msg_Dbg( p_vout, "%ix%i (virtual %ix%i) (request %ix%i)",
862 p_sys->var_info.xres, p_sys->var_info.yres,
863 p_sys->var_info.xres_virtual,
864 p_sys->var_info.yres_virtual,
865 p_sys->i_width, p_sys->i_height );
867 p_sys->p_palette = NULL;
868 p_sys->b_pan = ( fix_info.ypanstep || fix_info.ywrapstep );
870 switch( p_sys->var_info.bits_per_pixel )
873 p_sys->p_palette = malloc( 8 * 256 * sizeof( uint16_t ) );
874 if( !p_sys->p_palette )
876 /* Restore fb config */
877 ioctl( p_sys->i_fd, FBIOPUT_VSCREENINFO, &p_sys->old_info );
879 close( p_sys->i_fd );
882 p_sys->fb_cmap.start = 0;
883 p_sys->fb_cmap.len = 256;
884 p_sys->fb_cmap.red = p_sys->p_palette;
885 p_sys->fb_cmap.green = p_sys->p_palette + 256 * sizeof( uint16_t );
886 p_sys->fb_cmap.blue = p_sys->p_palette + 2 * 256 * sizeof( uint16_t );
887 p_sys->fb_cmap.transp = p_sys->p_palette + 3 * 256 * sizeof( uint16_t );
889 /* Save the colormap */
890 ioctl( p_sys->i_fd, FBIOGETCMAP, &p_sys->fb_cmap );
892 p_sys->i_bytes_per_pixel = 1;
897 p_sys->i_bytes_per_pixel = 2;
901 p_sys->i_bytes_per_pixel = 3;
905 p_sys->i_bytes_per_pixel = 4;
909 msg_Err( p_vout, "screen depth %d is not supported",
910 p_sys->var_info.bits_per_pixel );
912 /* Restore fb config */
913 ioctl( p_sys->i_fd, FBIOPUT_VSCREENINFO, &p_sys->old_info );
915 close( p_sys->i_fd );
919 p_sys->i_page_size = p_sys->i_width * p_sys->i_height *
920 p_sys->i_bytes_per_pixel;
922 /* Map a framebuffer at the beginning */
923 p_sys->p_video = mmap( 0, p_sys->i_page_size,
924 PROT_READ | PROT_WRITE, MAP_SHARED,
927 if( p_sys->p_video == ((void*)-1) )
929 msg_Err( p_vout, "cannot map video memory (%m)" );
931 if( p_sys->var_info.bits_per_pixel == 8 )
933 free( p_sys->p_palette );
934 p_sys->p_palette = NULL;
937 /* Restore fb config */
938 ioctl( p_sys->i_fd, FBIOPUT_VSCREENINFO, &p_vout->p_sys->old_info );
940 close( p_sys->i_fd );
944 msg_Dbg( p_vout, "framebuffer type=%d, visual=%d, ypanstep=%d, "
945 "ywrap=%d, accel=%d", fix_info.type, fix_info.visual,
946 fix_info.ypanstep, fix_info.ywrapstep, fix_info.accel );
950 /*****************************************************************************
951 * CloseDisplay: terminate FB video thread output method
952 *****************************************************************************/
953 static void CloseDisplay( vout_thread_t *p_vout )
956 memset( p_vout->p_sys->p_video, 0, p_vout->p_sys->i_page_size );
958 /* Restore palette */
959 if( p_vout->p_sys->var_info.bits_per_pixel == 8 )
961 ioctl( p_vout->p_sys->i_fd,
962 FBIOPUTCMAP, &p_vout->p_sys->fb_cmap );
963 free( p_vout->p_sys->p_palette );
964 p_vout->p_sys->p_palette = NULL;
967 /* Restore fb config */
968 ioctl( p_vout->p_sys->i_fd,
969 FBIOPUT_VSCREENINFO, &p_vout->p_sys->old_info );
972 close( p_vout->p_sys->i_fd );
975 /*****************************************************************************
976 * SwitchDisplay: VT change signal handler
977 *****************************************************************************
978 * This function activates or deactivates the output of the thread. It is
979 * called by the VT driver, on terminal change.
980 *****************************************************************************/
981 static void SwitchDisplay( int i_signal )
983 VLC_UNUSED( i_signal );
985 vout_thread_t *p_vout;
987 vlc_mutex_lock( &p_vout_bank->lock );
989 /* XXX: only test the first video output */
990 if( p_vout_bank->i_count )
992 p_vout = p_vout_bank->pp_vout[0];
996 case SIGUSR1: /* vt has been released */
997 p_vout->b_active = 0;
998 ioctl( p_sys->i_tty, VT_RELDISP, 1 );
1000 case SIGUSR2: /* vt has been acquired */
1001 p_vout->b_active = 1;
1002 ioctl( p_sys->i_tty, VT_RELDISP, VT_ACTIVATE );
1003 /* handle blanking */
1004 vlc_mutex_lock( &p_vout->change_lock );
1005 p_vout->i_changes |= VOUT_SIZE_CHANGE;
1006 vlc_mutex_unlock( &p_vout->change_lock );
1011 vlc_mutex_unlock( &p_vout_bank->lock );
1015 /*****************************************************************************
1016 * TextMode and GfxMode : switch tty to text/graphic mode
1017 *****************************************************************************
1018 * These functions toggle the tty mode.
1019 *****************************************************************************/
1020 static void TextMode( int i_tty )
1022 /* return to text mode */
1023 if( -1 == ioctl(i_tty, KDSETMODE, KD_TEXT) )
1025 /*msg_Err( p_vout, "failed ioctl KDSETMODE KD_TEXT" );*/
1029 static void GfxMode( int i_tty )
1031 /* switch to graphic mode */
1032 if( -1 == ioctl(i_tty, KDSETMODE, KD_GRAPHICS) )
1034 /*msg_Err( p_vout, "failed ioctl KDSETMODE KD_GRAPHICS" );*/