1 /*****************************************************************************
2 * fb.c : framebuffer plugin for vlc
3 *****************************************************************************
4 * Copyright (C) 2000-2009 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>
48 #include <vlc_vout_display.h>
49 #include <vlc_picture_pool.h>
51 /*****************************************************************************
53 *****************************************************************************/
54 #define FB_DEV_VAR "fbdev"
56 #define DEVICE_TEXT N_("Framebuffer device")
57 #define DEVICE_LONGTEXT N_(\
58 "Framebuffer device to use for rendering (usually /dev/fb0).")
60 #define TTY_TEXT N_("Run fb on current tty.")
61 #define TTY_LONGTEXT N_(\
62 "Run framebuffer on current TTY device (default enabled). " \
63 "(disable tty handling with caution)")
65 #define FB_MODE_TEXT N_("Framebuffer resolution to use.")
66 #define FB_MODE_LONGTEXT N_(\
67 "Select the resolution for the framebuffer. Currently it supports " \
68 "the values 0=QCIF 1=CIF 2=NTSC 3=PAL, 4=auto (default 4=auto)")
70 #define HW_ACCEL_TEXT N_("Framebuffer uses hw acceleration.")
71 #define HW_ACCEL_LONGTEXT N_(\
72 "If your framebuffer supports hardware acceleration or does double buffering " \
73 "in hardware then you must disable this option. It then does double buffering " \
76 static int Open (vlc_object_t *);
77 static void Close(vlc_object_t *);
80 set_shortname("Framebuffer")
81 set_category(CAT_VIDEO)
82 set_subcategory(SUBCAT_VIDEO_VOUT)
83 add_file(FB_DEV_VAR, "/dev/fb0", NULL, DEVICE_TEXT, DEVICE_LONGTEXT,
85 add_bool("fb-tty", true, NULL, TTY_TEXT, TTY_LONGTEXT, true)
86 add_obsolete_string("fb-chroma")
87 add_obsolete_string("fb-aspect-ratio")
88 add_integer("fb-mode", 4, NULL, FB_MODE_TEXT, FB_MODE_LONGTEXT,
90 add_bool("fb-hw-accel", true, NULL, HW_ACCEL_TEXT, HW_ACCEL_LONGTEXT,
92 set_description(N_("GNU/Linux framebuffer video output"))
93 set_capability("vout display", 30)
94 set_callbacks(Open, Close)
97 /*****************************************************************************
99 *****************************************************************************/
100 static picture_t *Get (vout_display_t *);
101 static void Display(vout_display_t *, picture_t *);
102 static int Control(vout_display_t *, int, va_list);
103 static void Manage (vout_display_t *);
106 static int OpenDisplay (vout_display_t *, bool force_resolution);
107 static void CloseDisplay (vout_display_t *);
108 static void SwitchDisplay(int i_signal);
109 static void TextMode (int tty);
110 static void GfxMode (int tty);
112 static int TtyInit(vout_display_t *);
113 static void TtyExit(vout_display_t *);
116 struct vout_display_sys_t {
117 /* System information */
118 int tty; /* tty device handle */
120 struct termios old_termios;
122 /* Original configuration information */
123 struct sigaction sig_usr1; /* USR1 previous handler */
124 struct sigaction sig_usr2; /* USR2 previous handler */
125 struct vt_mode vt_mode; /* previous VT mode */
127 /* Framebuffer information */
128 int fd; /* device handle */
129 struct fb_var_screeninfo old_info; /* original mode information */
130 struct fb_var_screeninfo var_info; /* current mode information */
131 bool has_pan; /* does device supports panning ? */
132 struct fb_cmap fb_cmap; /* original colormap */
133 uint16_t *palette; /* original palette */
134 bool is_hw_accel; /* has hardware support */
136 /* Video information */
142 uint8_t *video_ptr; /* base adress */
143 size_t video_size; /* page size */
146 picture_pool_t *pool;
150 * This function allocates and initializes a FB vout method.
152 static int Open(vlc_object_t *object)
154 vout_display_t *vd = (vout_display_t *)object;
155 vout_display_sys_t *sys;
157 /* Allocate instance and initialize some members */
158 vd->sys = sys = calloc(1, sizeof(*sys));
162 /* Does the framebuffer uses hw acceleration? */
163 sys->is_hw_accel = var_CreateGetBool(vd, "fb-hw-accel");
165 /* Set tty and fb devices */
166 sys->tty = 0; /* 0 == /dev/tty0 == current console */
167 sys->is_tty = var_CreateGetBool(vd, "fb-tty");
168 #if !defined(WIN32) && defined(HAVE_ISATTY)
169 /* Check that stdin is a TTY */
170 if (sys->is_tty && !isatty(0)) {
171 msg_Warn(vd, "fd 0 is not a TTY");
175 msg_Warn(vd, "disabling tty handling, use with caution because "
176 "there is no way to return to the tty.");
179 const int mode = var_CreateGetInteger(vd, "fb-mode");
180 bool force_resolution = true;
200 force_resolution = false;
205 if (sys->is_tty && TtyInit(vd)) {
211 sys->video_ptr = MAP_FAILED;
215 if (OpenDisplay(vd, force_resolution)) {
216 Close(VLC_OBJECT(vd));
221 video_format_t fmt = vd->fmt;
223 msg_Err(vd, "var_info.bits_per_pixel = %d", sys->var_info.bits_per_pixel);
224 switch (sys->var_info.bits_per_pixel) {
225 case 8: /* FIXME: set the palette */
226 fmt.i_chroma = VLC_CODEC_RGB8;
229 fmt.i_chroma = VLC_CODEC_RGB15;
232 fmt.i_chroma = VLC_CODEC_RGB16;
235 fmt.i_chroma = VLC_CODEC_RGB24;
238 fmt.i_chroma = VLC_CODEC_RGB32;
241 msg_Err(vd, "unknown screen depth %i",
242 sys->var_info.bits_per_pixel);
243 Close(VLC_OBJECT(vd));
246 if (sys->var_info.bits_per_pixel != 8) {
247 fmt.i_rmask = ((1 << sys->var_info.red.length) - 1)
248 << sys->var_info.red.offset;
249 fmt.i_gmask = ((1 << sys->var_info.green.length) - 1)
250 << sys->var_info.green.offset;
251 fmt.i_bmask = ((1 << sys->var_info.blue.length) - 1)
252 << sys->var_info.blue.offset;
254 fmt.i_width = sys->width;
255 fmt.i_height = sys->height;
258 vout_display_info_t info = vd->info;
259 info.has_hide_mouse = true;
266 vd->display = Display;
267 vd->control = Control;
271 vout_display_SendEventFullscreen(vd, true);
272 vout_display_SendEventDisplaySize(vd, fmt.i_width, fmt.i_height);
277 * Terminate an output method created by Open
279 static void Close(vlc_object_t *object)
281 vout_display_t *vd = (vout_display_t *)object;
282 vout_display_sys_t *sys = vd->sys;
285 picture_pool_Delete(sys->pool);
286 if (!sys->is_hw_accel && sys->picture)
287 picture_Release(sys->picture);
298 static picture_t *Get(vout_display_t *vd)
300 vout_display_sys_t *sys = vd->sys;
304 picture_resource_t rsc;
306 memset(&rsc, 0, sizeof(rsc));
307 rsc.p[0].p_pixels = sys->video_ptr;
308 rsc.p[0].i_lines = sys->var_info.yres;
309 if (sys->var_info.xres_virtual)
310 rsc.p[0].i_pitch = sys->var_info.xres_virtual *
311 sys->bytes_per_pixel;
313 rsc.p[0].i_pitch = sys->var_info.xres *
314 sys->bytes_per_pixel;
316 sys->picture = picture_NewFromResource(&vd->fmt, &rsc);
321 if (sys->is_hw_accel)
322 sys->pool = picture_pool_New(1, &sys->picture);
324 sys->pool = picture_pool_NewFromFormat(&vd->fmt, 1);
328 return picture_pool_Get(sys->pool);
330 static void Display(vout_display_t *vd, picture_t *picture)
332 vout_display_sys_t *sys = vd->sys;
334 /* swap the two Y offsets if the drivers supports panning */
336 sys->var_info.yoffset = 0;
337 /*vd->sys->var_info.yoffset = vd->sys->var_info.yres; */
339 /* the X offset should be 0, but who knows ...
340 * some other app might have played with the framebuffer */
341 sys->var_info.xoffset = 0;
343 /* FIXME 'static' is damn wrong and it's dead code ... */
344 static int panned = 0;
346 ioctl(sys->fd, FBIOPAN_DISPLAY, &sys->var_info);
351 if (!sys->is_hw_accel)
352 picture_Copy(sys->picture, picture);
354 picture_Release(picture);
356 static int Control(vout_display_t *vd, int query, va_list args)
358 vout_display_sys_t *sys = vd->sys;
361 case VOUT_DISPLAY_CHANGE_DISPLAY_SIZE: {
362 const vout_display_cfg_t *cfg = va_arg(args, const vout_display_cfg_t *);
363 if (cfg->display.width != sys->width ||
364 cfg->display.height != sys->height)
369 msg_Err(vd, "Unsupported query in vout display fb");
373 static void Manage (vout_display_t *vd)
380 if (vd->i_changes & VOUT_SIZE_CHANGE)
382 msg_Dbg(vd, "reinitializing framebuffer screen");
383 vd->i_changes &= ~VOUT_SIZE_CHANGE;
385 vout_display_SendEventDisplaySize();
388 memset(vd->sys->video_ptr, 0, vd->sys->video_size);
394 /* following functions are local */
395 static int TtyInit(vout_display_t *vd)
397 vout_display_sys_t *sys = vd->sys;
399 struct termios new_termios;
403 /* Set keyboard settings */
404 if (tcgetattr(0, &sys->old_termios) == -1) {
405 msg_Err(vd, "tcgetattr failed");
408 if (tcgetattr(0, &new_termios) == -1) {
409 msg_Err(vd, "tcgetattr failed");
412 /* new_termios.c_lflag &= ~ (ICANON | ISIG);
413 new_termios.c_lflag |= (ECHO | ECHOCTL); */
414 new_termios.c_lflag &= ~ (ICANON);
415 new_termios.c_lflag &= ~(ECHO | ECHOCTL);
416 new_termios.c_iflag = 0;
417 new_termios.c_cc[VMIN] = 1;
418 new_termios.c_cc[VTIME] = 0;
420 if (tcsetattr(0, TCSAFLUSH, &new_termios) == -1) {
421 msg_Err(vd, "tcsetattr failed");
424 ioctl(sys->tty, VT_RELDISP, VT_ACKACQ);
426 /* Set-up tty signal handler to be aware of tty changes */
427 struct sigaction sig_tty;
428 memset(&sig_tty, 0, sizeof(sig_tty));
429 sig_tty.sa_handler = SwitchDisplay;
430 sigemptyset(&sig_tty.sa_mask);
431 if (sigaction(SIGUSR1, &sig_tty, &sys->sig_usr1) ||
432 sigaction(SIGUSR2, &sig_tty, &sys->sig_usr2)) {
433 msg_Err(vd, "cannot set signal handler (%m)");
434 /* FIXME SIGUSR1 could have succeed */
438 /* Set-up tty according to new signal handler */
439 if (-1 == ioctl(sys->tty, VT_GETMODE, &sys->vt_mode)) {
440 msg_Err(vd, "cannot get terminal mode (%m)");
443 struct vt_mode vt_mode = sys->vt_mode;
444 vt_mode.mode = VT_PROCESS;
446 vt_mode.relsig = SIGUSR1;
447 vt_mode.acqsig = SIGUSR2;
449 if (-1 == ioctl(sys->tty, VT_SETMODE, &vt_mode)) {
450 msg_Err(vd, "cannot set terminal mode (%m)");
456 sigaction(SIGUSR1, &sys->sig_usr1, NULL);
457 sigaction(SIGUSR2, &sys->sig_usr2, NULL);
459 tcsetattr(0, 0, &sys->old_termios);
463 static void TtyExit(vout_display_t *vd)
465 vout_display_sys_t *sys = vd->sys;
467 /* Reset the terminal */
468 ioctl(sys->tty, VT_SETMODE, &sys->vt_mode);
470 /* Remove signal handlers */
471 sigaction(SIGUSR1, &sys->sig_usr1, NULL);
472 sigaction(SIGUSR2, &sys->sig_usr2, NULL);
474 /* Reset the keyboard state */
475 tcsetattr(0, 0, &sys->old_termios);
477 /* Return to text mode */
481 /*****************************************************************************
482 * OpenDisplay: initialize framebuffer
483 *****************************************************************************/
484 static int OpenDisplay(vout_display_t *vd, bool force_resolution)
486 vout_display_sys_t *sys = vd->sys;
487 char *psz_device; /* framebuffer device path */
488 struct fb_fix_screeninfo fix_info; /* framebuffer fix information */
490 /* Open framebuffer device */
491 if (!(psz_device = config_GetPsz(vd, FB_DEV_VAR))) {
492 msg_Err(vd, "don't know which fb device to open");
496 sys->fd = open(psz_device, O_RDWR);
498 msg_Err(vd, "cannot open %s (%m)", psz_device);
504 /* Get framebuffer device information */
505 if (ioctl(sys->fd, FBIOGET_VSCREENINFO, &sys->var_info)) {
506 msg_Err(vd, "cannot get fb info (%m)");
510 sys->old_info = sys->var_info;
512 /* Get some info on the framebuffer itself */
513 if (force_resolution) {
514 sys->var_info.xres = sys->var_info.xres_virtual = sys->width;
515 sys->var_info.yres = sys->var_info.yres_virtual = sys->height;
518 /* Set some attributes */
519 sys->var_info.activate = sys->is_tty ? FB_ACTIVATE_NXTOPEN :
521 sys->var_info.xoffset = 0;
522 sys->var_info.yoffset = 0;
524 if (ioctl(sys->fd, FBIOPUT_VSCREENINFO, &sys->var_info)) {
525 msg_Err(vd, "cannot set fb info (%m)");
530 /* Get some information again, in the definitive configuration */
531 if (ioctl(sys->fd, FBIOGET_FSCREENINFO, &fix_info) ||
532 ioctl(sys->fd, FBIOGET_VSCREENINFO, &sys->var_info)) {
533 msg_Err(vd, "cannot get additional fb info (%m)");
535 /* Restore fb config */
536 ioctl(sys->fd, FBIOPUT_VSCREENINFO, &sys->old_info);
542 /* If the fb has limitations on mode change,
543 * then keep the resolution of the fb */
544 if ((sys->height != sys->var_info.yres) ||
545 (sys->width != sys->var_info.xres)) {
547 "using framebuffer native resolution instead of requested (%ix%i)",
548 sys->width, sys->height);
550 sys->height = sys->var_info.yres;
551 sys->width = sys->var_info.xres_virtual ? sys->var_info.xres_virtual :
554 /* FIXME: if the image is full-size, it gets cropped on the left
555 * because of the xres / xres_virtual slight difference */
556 msg_Dbg(vd, "%ix%i (virtual %ix%i) (request %ix%i)",
557 sys->var_info.xres, sys->var_info.yres,
558 sys->var_info.xres_virtual,
559 sys->var_info.yres_virtual,
560 sys->width, sys->height);
563 sys->has_pan = (fix_info.ypanstep || fix_info.ywrapstep);
565 switch (sys->var_info.bits_per_pixel) {
567 sys->palette = malloc(8 * 256 * sizeof(uint16_t));
569 /* Restore fb config */
570 ioctl(sys->fd, FBIOPUT_VSCREENINFO, &sys->old_info);
575 sys->fb_cmap.start = 0;
576 sys->fb_cmap.len = 256;
577 sys->fb_cmap.red = sys->palette;
578 sys->fb_cmap.green = sys->palette + 256 * sizeof(uint16_t);
579 sys->fb_cmap.blue = sys->palette + 2 * 256 * sizeof(uint16_t);
580 sys->fb_cmap.transp = sys->palette + 3 * 256 * sizeof(uint16_t);
582 /* Save the colormap */
583 ioctl(sys->fd, FBIOGETCMAP, &sys->fb_cmap);
585 sys->bytes_per_pixel = 1;
590 sys->bytes_per_pixel = 2;
594 sys->bytes_per_pixel = 3;
598 sys->bytes_per_pixel = 4;
602 msg_Err(vd, "screen depth %d is not supported",
603 sys->var_info.bits_per_pixel);
605 /* Restore fb config */
606 ioctl(sys->fd, FBIOPUT_VSCREENINFO, &sys->old_info);
612 sys->video_size = sys->width * sys->height * sys->bytes_per_pixel;
614 /* Map a framebuffer at the beginning */
615 sys->video_ptr = mmap(NULL, sys->video_size,
616 PROT_READ | PROT_WRITE, MAP_SHARED, sys->fd, 0);
618 if (sys->video_ptr == MAP_FAILED) {
619 msg_Err(vd, "cannot map video memory (%m)");
621 if (sys->var_info.bits_per_pixel == 8) {
626 /* Restore fb config */
627 ioctl(sys->fd, FBIOPUT_VSCREENINFO, &sys->old_info);
632 /* Clear the screen */
633 memset(sys->video_ptr, 0, sys->video_size);
636 "framebuffer type=%d, visual=%d, ypanstep=%d, ywrap=%d, accel=%d",
637 fix_info.type, fix_info.visual,
638 fix_info.ypanstep, fix_info.ywrapstep, fix_info.accel);
642 /*****************************************************************************
643 * CloseDisplay: terminate FB video thread output method
644 *****************************************************************************/
645 static void CloseDisplay(vout_display_t *vd)
647 vout_display_sys_t *sys = vd->sys;
649 if (sys->video_ptr != MAP_FAILED) {
651 memset(sys->video_ptr, 0, sys->video_size);
652 munmap(sys->video_ptr, sys->video_size);
656 /* Restore palette */
657 if (sys->var_info.bits_per_pixel == 8) {
658 ioctl(sys->fd, FBIOPUTCMAP, &sys->fb_cmap);
663 /* Restore fb config */
664 ioctl(sys->fd, FBIOPUT_VSCREENINFO, &sys->old_info);
671 /*****************************************************************************
672 * SwitchDisplay: VT change signal handler
673 *****************************************************************************
674 * This function activates or deactivates the output of the thread. It is
675 * called by the VT driver, on terminal change.
676 *****************************************************************************/
677 static void SwitchDisplay(int i_signal)
679 VLC_UNUSED(i_signal);
683 vlc_mutex_lock(&p_vout_bank->lock);
685 /* XXX: only test the first video output */
686 if (p_vout_bank->i_count)
688 vd = p_vout_bank->pp_vout[0];
692 case SIGUSR1: /* vt has been released */
694 ioctl(sys->tty, VT_RELDISP, 1);
696 case SIGUSR2: /* vt has been acquired */
698 ioctl(sys->tty, VT_RELDISP, VT_ACTIVATE);
699 /* handle blanking */
700 vlc_mutex_lock(&vd->change_lock);
701 vd->i_changes |= VOUT_SIZE_CHANGE;
702 vlc_mutex_unlock(&vd->change_lock);
707 vlc_mutex_unlock(&p_vout_bank->lock);
711 /*****************************************************************************
712 * TextMode and GfxMode : switch tty to text/graphic mode
713 *****************************************************************************
714 * These functions toggle the tty mode.
715 *****************************************************************************/
716 static void TextMode(int tty)
718 /* return to text mode */
719 if (-1 == ioctl(tty, KDSETMODE, KD_TEXT)) {
720 /*msg_Err(vd, "failed ioctl KDSETMODE KD_TEXT");*/
724 static void GfxMode(int tty)
726 /* switch to graphic mode */
727 if (-1 == ioctl(tty, KDSETMODE, KD_GRAPHICS)) {
728 /*msg_Err(vd, "failed ioctl KDSETMODE KD_GRAPHICS");*/