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>
50 #include <vlc_charset.h>
52 /*****************************************************************************
54 *****************************************************************************/
55 #define FB_DEV_VAR "fbdev"
57 #define DEVICE_TEXT N_("Framebuffer device")
58 #define DEVICE_LONGTEXT N_(\
59 "Framebuffer device to use for rendering (usually /dev/fb0).")
61 #define TTY_TEXT N_("Run fb on current tty.")
62 #define TTY_LONGTEXT N_(\
63 "Run framebuffer on current TTY device (default enabled). " \
64 "(disable tty handling with caution)")
66 #define FB_MODE_TEXT N_("Framebuffer resolution to use.")
67 #define FB_MODE_LONGTEXT N_(\
68 "Select the resolution for the framebuffer. Currently it supports " \
69 "the values 0=QCIF 1=CIF 2=NTSC 3=PAL, 4=auto (default 4=auto)")
71 #define HW_ACCEL_TEXT N_("Framebuffer uses hw acceleration.")
72 #define HW_ACCEL_LONGTEXT N_(\
73 "If your framebuffer supports hardware acceleration or does double buffering " \
74 "in hardware then you must disable this option. It then does double buffering " \
77 static int Open (vlc_object_t *);
78 static void Close(vlc_object_t *);
81 set_shortname("Framebuffer")
82 set_category(CAT_VIDEO)
83 set_subcategory(SUBCAT_VIDEO_VOUT)
84 add_file(FB_DEV_VAR, "/dev/fb0", NULL, DEVICE_TEXT, DEVICE_LONGTEXT,
86 add_bool("fb-tty", true, NULL, TTY_TEXT, TTY_LONGTEXT, true)
87 add_obsolete_string("fb-chroma")
88 add_obsolete_string("fb-aspect-ratio")
89 add_integer("fb-mode", 4, NULL, FB_MODE_TEXT, FB_MODE_LONGTEXT,
91 add_bool("fb-hw-accel", true, NULL, HW_ACCEL_TEXT, HW_ACCEL_LONGTEXT,
93 set_description(N_("GNU/Linux framebuffer video output"))
94 set_capability("vout display", 30)
95 set_callbacks(Open, Close)
98 /*****************************************************************************
100 *****************************************************************************/
101 static picture_t *Get (vout_display_t *);
102 static void Display(vout_display_t *, picture_t *);
103 static int Control(vout_display_t *, int, va_list);
104 static void Manage (vout_display_t *);
107 static int OpenDisplay (vout_display_t *, bool force_resolution);
108 static void CloseDisplay (vout_display_t *);
109 static void SwitchDisplay(int i_signal);
110 static void TextMode (int tty);
111 static void GfxMode (int tty);
113 static int TtyInit(vout_display_t *);
114 static void TtyExit(vout_display_t *);
117 struct vout_display_sys_t {
118 /* System information */
119 int tty; /* tty device handle */
121 struct termios old_termios;
123 /* Original configuration information */
124 struct sigaction sig_usr1; /* USR1 previous handler */
125 struct sigaction sig_usr2; /* USR2 previous handler */
126 struct vt_mode vt_mode; /* previous VT mode */
128 /* Framebuffer information */
129 int fd; /* device handle */
130 struct fb_var_screeninfo old_info; /* original mode information */
131 struct fb_var_screeninfo var_info; /* current mode information */
132 bool has_pan; /* does device supports panning ? */
133 struct fb_cmap fb_cmap; /* original colormap */
134 uint16_t *palette; /* original palette */
135 bool is_hw_accel; /* has hardware support */
137 /* Video information */
143 uint8_t *video_ptr; /* base adress */
144 size_t video_size; /* page size */
147 picture_pool_t *pool;
151 * This function allocates and initializes a FB vout method.
153 static int Open(vlc_object_t *object)
155 vout_display_t *vd = (vout_display_t *)object;
156 vout_display_sys_t *sys;
158 /* Allocate instance and initialize some members */
159 vd->sys = sys = calloc(1, sizeof(*sys));
163 /* Does the framebuffer uses hw acceleration? */
164 sys->is_hw_accel = var_CreateGetBool(vd, "fb-hw-accel");
166 /* Set tty and fb devices */
167 sys->tty = 0; /* 0 == /dev/tty0 == current console */
168 sys->is_tty = var_CreateGetBool(vd, "fb-tty");
169 #if !defined(WIN32) && defined(HAVE_ISATTY)
170 /* Check that stdin is a TTY */
171 if (sys->is_tty && !isatty(0)) {
172 msg_Warn(vd, "fd 0 is not a TTY");
176 msg_Warn(vd, "disabling tty handling, use with caution because "
177 "there is no way to return to the tty.");
180 const int mode = var_CreateGetInteger(vd, "fb-mode");
181 bool force_resolution = true;
201 force_resolution = false;
206 if (sys->is_tty && TtyInit(vd)) {
212 sys->video_ptr = MAP_FAILED;
216 if (OpenDisplay(vd, force_resolution)) {
217 Close(VLC_OBJECT(vd));
222 video_format_t fmt = vd->fmt;
224 msg_Err(vd, "var_info.bits_per_pixel = %d", sys->var_info.bits_per_pixel);
225 switch (sys->var_info.bits_per_pixel) {
226 case 8: /* FIXME: set the palette */
227 fmt.i_chroma = VLC_CODEC_RGB8;
230 fmt.i_chroma = VLC_CODEC_RGB15;
233 fmt.i_chroma = VLC_CODEC_RGB16;
236 fmt.i_chroma = VLC_CODEC_RGB24;
239 fmt.i_chroma = VLC_CODEC_RGB32;
242 msg_Err(vd, "unknown screen depth %i",
243 sys->var_info.bits_per_pixel);
244 Close(VLC_OBJECT(vd));
247 if (sys->var_info.bits_per_pixel != 8) {
248 fmt.i_rmask = ((1 << sys->var_info.red.length) - 1)
249 << sys->var_info.red.offset;
250 fmt.i_gmask = ((1 << sys->var_info.green.length) - 1)
251 << sys->var_info.green.offset;
252 fmt.i_bmask = ((1 << sys->var_info.blue.length) - 1)
253 << sys->var_info.blue.offset;
255 fmt.i_width = sys->width;
256 fmt.i_height = sys->height;
259 vout_display_info_t info = vd->info;
260 info.has_hide_mouse = true;
267 vd->display = Display;
268 vd->control = Control;
272 vout_display_SendEventFullscreen(vd, true);
273 vout_display_SendEventDisplaySize(vd, fmt.i_width, fmt.i_height, true);
278 * Terminate an output method created by Open
280 static void Close(vlc_object_t *object)
282 vout_display_t *vd = (vout_display_t *)object;
283 vout_display_sys_t *sys = vd->sys;
286 picture_pool_Delete(sys->pool);
287 if (!sys->is_hw_accel && sys->picture)
288 picture_Release(sys->picture);
299 static picture_t *Get(vout_display_t *vd)
301 vout_display_sys_t *sys = vd->sys;
305 picture_resource_t rsc;
307 memset(&rsc, 0, sizeof(rsc));
308 rsc.p[0].p_pixels = sys->video_ptr;
309 rsc.p[0].i_lines = sys->var_info.yres;
310 if (sys->var_info.xres_virtual)
311 rsc.p[0].i_pitch = sys->var_info.xres_virtual *
312 sys->bytes_per_pixel;
314 rsc.p[0].i_pitch = sys->var_info.xres *
315 sys->bytes_per_pixel;
317 sys->picture = picture_NewFromResource(&vd->fmt, &rsc);
322 if (sys->is_hw_accel)
323 sys->pool = picture_pool_New(1, &sys->picture);
325 sys->pool = picture_pool_NewFromFormat(&vd->fmt, 1);
329 return picture_pool_Get(sys->pool);
331 static void Display(vout_display_t *vd, picture_t *picture)
333 vout_display_sys_t *sys = vd->sys;
335 /* swap the two Y offsets if the drivers supports panning */
337 sys->var_info.yoffset = 0;
338 /*vd->sys->var_info.yoffset = vd->sys->var_info.yres; */
340 /* the X offset should be 0, but who knows ...
341 * some other app might have played with the framebuffer */
342 sys->var_info.xoffset = 0;
344 /* FIXME 'static' is damn wrong and it's dead code ... */
345 static int panned = 0;
347 ioctl(sys->fd, FBIOPAN_DISPLAY, &sys->var_info);
352 if (!sys->is_hw_accel)
353 picture_Copy(sys->picture, picture);
355 picture_Release(picture);
357 static int Control(vout_display_t *vd, int query, va_list args)
359 vout_display_sys_t *sys = vd->sys;
362 case VOUT_DISPLAY_CHANGE_DISPLAY_SIZE: {
363 const vout_display_cfg_t *cfg = va_arg(args, const vout_display_cfg_t *);
364 if (cfg->display.width != sys->width ||
365 cfg->display.height != sys->height)
370 msg_Err(vd, "Unsupported query in vout display fb");
374 static void Manage (vout_display_t *vd)
381 if (vd->i_changes & VOUT_SIZE_CHANGE)
383 msg_Dbg(vd, "reinitializing framebuffer screen");
384 vd->i_changes &= ~VOUT_SIZE_CHANGE;
386 vout_display_SendEventDisplaySize();
389 memset(vd->sys->video_ptr, 0, vd->sys->video_size);
395 /* following functions are local */
396 static int TtyInit(vout_display_t *vd)
398 vout_display_sys_t *sys = vd->sys;
400 struct termios new_termios;
404 /* Set keyboard settings */
405 if (tcgetattr(0, &sys->old_termios) == -1) {
406 msg_Err(vd, "tcgetattr failed");
409 if (tcgetattr(0, &new_termios) == -1) {
410 msg_Err(vd, "tcgetattr failed");
413 /* new_termios.c_lflag &= ~ (ICANON | ISIG);
414 new_termios.c_lflag |= (ECHO | ECHOCTL); */
415 new_termios.c_lflag &= ~ (ICANON);
416 new_termios.c_lflag &= ~(ECHO | ECHOCTL);
417 new_termios.c_iflag = 0;
418 new_termios.c_cc[VMIN] = 1;
419 new_termios.c_cc[VTIME] = 0;
421 if (tcsetattr(0, TCSAFLUSH, &new_termios) == -1) {
422 msg_Err(vd, "tcsetattr failed");
425 ioctl(sys->tty, VT_RELDISP, VT_ACKACQ);
427 /* Set-up tty signal handler to be aware of tty changes */
428 struct sigaction sig_tty;
429 memset(&sig_tty, 0, sizeof(sig_tty));
430 sig_tty.sa_handler = SwitchDisplay;
431 sigemptyset(&sig_tty.sa_mask);
432 if (sigaction(SIGUSR1, &sig_tty, &sys->sig_usr1) ||
433 sigaction(SIGUSR2, &sig_tty, &sys->sig_usr2)) {
434 msg_Err(vd, "cannot set signal handler (%m)");
435 /* FIXME SIGUSR1 could have succeed */
439 /* Set-up tty according to new signal handler */
440 if (-1 == ioctl(sys->tty, VT_GETMODE, &sys->vt_mode)) {
441 msg_Err(vd, "cannot get terminal mode (%m)");
444 struct vt_mode vt_mode = sys->vt_mode;
445 vt_mode.mode = VT_PROCESS;
447 vt_mode.relsig = SIGUSR1;
448 vt_mode.acqsig = SIGUSR2;
450 if (-1 == ioctl(sys->tty, VT_SETMODE, &vt_mode)) {
451 msg_Err(vd, "cannot set terminal mode (%m)");
457 sigaction(SIGUSR1, &sys->sig_usr1, NULL);
458 sigaction(SIGUSR2, &sys->sig_usr2, NULL);
460 tcsetattr(0, 0, &sys->old_termios);
464 static void TtyExit(vout_display_t *vd)
466 vout_display_sys_t *sys = vd->sys;
468 /* Reset the terminal */
469 ioctl(sys->tty, VT_SETMODE, &sys->vt_mode);
471 /* Remove signal handlers */
472 sigaction(SIGUSR1, &sys->sig_usr1, NULL);
473 sigaction(SIGUSR2, &sys->sig_usr2, NULL);
475 /* Reset the keyboard state */
476 tcsetattr(0, 0, &sys->old_termios);
478 /* Return to text mode */
482 /*****************************************************************************
483 * OpenDisplay: initialize framebuffer
484 *****************************************************************************/
485 static int OpenDisplay(vout_display_t *vd, bool force_resolution)
487 vout_display_sys_t *sys = vd->sys;
488 char *psz_device; /* framebuffer device path */
489 struct fb_fix_screeninfo fix_info; /* framebuffer fix information */
491 /* Open framebuffer device */
492 if (!(psz_device = config_GetPsz(vd, FB_DEV_VAR))) {
493 msg_Err(vd, "don't know which fb device to open");
497 sys->fd = utf8_open(psz_device, O_RDWR);
499 msg_Err(vd, "cannot open %s (%m)", psz_device);
505 /* Get framebuffer device information */
506 if (ioctl(sys->fd, FBIOGET_VSCREENINFO, &sys->var_info)) {
507 msg_Err(vd, "cannot get fb info (%m)");
511 sys->old_info = sys->var_info;
513 /* Get some info on the framebuffer itself */
514 if (force_resolution) {
515 sys->var_info.xres = sys->var_info.xres_virtual = sys->width;
516 sys->var_info.yres = sys->var_info.yres_virtual = sys->height;
519 /* Set some attributes */
520 sys->var_info.activate = sys->is_tty ? FB_ACTIVATE_NXTOPEN :
522 sys->var_info.xoffset = 0;
523 sys->var_info.yoffset = 0;
525 if (ioctl(sys->fd, FBIOPUT_VSCREENINFO, &sys->var_info)) {
526 msg_Err(vd, "cannot set fb info (%m)");
531 /* Get some information again, in the definitive configuration */
532 if (ioctl(sys->fd, FBIOGET_FSCREENINFO, &fix_info) ||
533 ioctl(sys->fd, FBIOGET_VSCREENINFO, &sys->var_info)) {
534 msg_Err(vd, "cannot get additional fb info (%m)");
536 /* Restore fb config */
537 ioctl(sys->fd, FBIOPUT_VSCREENINFO, &sys->old_info);
543 /* If the fb has limitations on mode change,
544 * then keep the resolution of the fb */
545 if ((sys->height != sys->var_info.yres) ||
546 (sys->width != sys->var_info.xres)) {
548 "using framebuffer native resolution instead of requested (%ix%i)",
549 sys->width, sys->height);
551 sys->height = sys->var_info.yres;
552 sys->width = sys->var_info.xres_virtual ? sys->var_info.xres_virtual :
555 /* FIXME: if the image is full-size, it gets cropped on the left
556 * because of the xres / xres_virtual slight difference */
557 msg_Dbg(vd, "%ix%i (virtual %ix%i) (request %ix%i)",
558 sys->var_info.xres, sys->var_info.yres,
559 sys->var_info.xres_virtual,
560 sys->var_info.yres_virtual,
561 sys->width, sys->height);
564 sys->has_pan = (fix_info.ypanstep || fix_info.ywrapstep);
566 switch (sys->var_info.bits_per_pixel) {
568 sys->palette = malloc(8 * 256 * sizeof(uint16_t));
570 /* Restore fb config */
571 ioctl(sys->fd, FBIOPUT_VSCREENINFO, &sys->old_info);
576 sys->fb_cmap.start = 0;
577 sys->fb_cmap.len = 256;
578 sys->fb_cmap.red = sys->palette;
579 sys->fb_cmap.green = sys->palette + 256 * sizeof(uint16_t);
580 sys->fb_cmap.blue = sys->palette + 2 * 256 * sizeof(uint16_t);
581 sys->fb_cmap.transp = sys->palette + 3 * 256 * sizeof(uint16_t);
583 /* Save the colormap */
584 ioctl(sys->fd, FBIOGETCMAP, &sys->fb_cmap);
586 sys->bytes_per_pixel = 1;
591 sys->bytes_per_pixel = 2;
595 sys->bytes_per_pixel = 3;
599 sys->bytes_per_pixel = 4;
603 msg_Err(vd, "screen depth %d is not supported",
604 sys->var_info.bits_per_pixel);
606 /* Restore fb config */
607 ioctl(sys->fd, FBIOPUT_VSCREENINFO, &sys->old_info);
613 sys->video_size = sys->width * sys->height * sys->bytes_per_pixel;
615 /* Map a framebuffer at the beginning */
616 sys->video_ptr = mmap(NULL, sys->video_size,
617 PROT_READ | PROT_WRITE, MAP_SHARED, sys->fd, 0);
619 if (sys->video_ptr == MAP_FAILED) {
620 msg_Err(vd, "cannot map video memory (%m)");
622 if (sys->var_info.bits_per_pixel == 8) {
627 /* Restore fb config */
628 ioctl(sys->fd, FBIOPUT_VSCREENINFO, &sys->old_info);
633 /* Clear the screen */
634 memset(sys->video_ptr, 0, sys->video_size);
637 "framebuffer type=%d, visual=%d, ypanstep=%d, ywrap=%d, accel=%d",
638 fix_info.type, fix_info.visual,
639 fix_info.ypanstep, fix_info.ywrapstep, fix_info.accel);
643 /*****************************************************************************
644 * CloseDisplay: terminate FB video thread output method
645 *****************************************************************************/
646 static void CloseDisplay(vout_display_t *vd)
648 vout_display_sys_t *sys = vd->sys;
650 if (sys->video_ptr != MAP_FAILED) {
652 memset(sys->video_ptr, 0, sys->video_size);
653 munmap(sys->video_ptr, sys->video_size);
657 /* Restore palette */
658 if (sys->var_info.bits_per_pixel == 8) {
659 ioctl(sys->fd, FBIOPUTCMAP, &sys->fb_cmap);
664 /* Restore fb config */
665 ioctl(sys->fd, FBIOPUT_VSCREENINFO, &sys->old_info);
672 /*****************************************************************************
673 * SwitchDisplay: VT change signal handler
674 *****************************************************************************
675 * This function activates or deactivates the output of the thread. It is
676 * called by the VT driver, on terminal change.
677 *****************************************************************************/
678 static void SwitchDisplay(int i_signal)
680 VLC_UNUSED(i_signal);
684 vlc_mutex_lock(&p_vout_bank->lock);
686 /* XXX: only test the first video output */
687 if (p_vout_bank->i_count)
689 vd = p_vout_bank->pp_vout[0];
693 case SIGUSR1: /* vt has been released */
695 ioctl(sys->tty, VT_RELDISP, 1);
697 case SIGUSR2: /* vt has been acquired */
699 ioctl(sys->tty, VT_RELDISP, VT_ACTIVATE);
700 /* handle blanking */
701 vlc_mutex_lock(&vd->change_lock);
702 vd->i_changes |= VOUT_SIZE_CHANGE;
703 vlc_mutex_unlock(&vd->change_lock);
708 vlc_mutex_unlock(&p_vout_bank->lock);
712 /*****************************************************************************
713 * TextMode and GfxMode : switch tty to text/graphic mode
714 *****************************************************************************
715 * These functions toggle the tty mode.
716 *****************************************************************************/
717 static void TextMode(int tty)
719 /* return to text mode */
720 if (-1 == ioctl(tty, KDSETMODE, KD_TEXT)) {
721 /*msg_Err(vd, "failed ioctl KDSETMODE KD_TEXT");*/
725 static void GfxMode(int tty)
727 /* switch to graphic mode */
728 if (-1 == ioctl(tty, KDSETMODE, KD_GRAPHICS)) {
729 /*msg_Err(vd, "failed ioctl KDSETMODE KD_GRAPHICS");*/