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_pool_t *Pool (vout_display_t *, unsigned);
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 */
140 uint32_t line_length;
144 uint8_t *video_ptr; /* base adress */
145 size_t video_size; /* page size */
148 picture_pool_t *pool;
152 * This function allocates and initializes a FB vout method.
154 static int Open(vlc_object_t *object)
156 vout_display_t *vd = (vout_display_t *)object;
157 vout_display_sys_t *sys;
159 /* Allocate instance and initialize some members */
160 vd->sys = sys = calloc(1, sizeof(*sys));
164 /* Does the framebuffer uses hw acceleration? */
165 sys->is_hw_accel = var_CreateGetBool(vd, "fb-hw-accel");
167 /* Set tty and fb devices */
168 sys->tty = 0; /* 0 == /dev/tty0 == current console */
169 sys->is_tty = var_CreateGetBool(vd, "fb-tty");
170 #if !defined(WIN32) && defined(HAVE_ISATTY)
171 /* Check that stdin is a TTY */
172 if (sys->is_tty && !isatty(0)) {
173 msg_Warn(vd, "fd 0 is not a TTY");
177 msg_Warn(vd, "disabling tty handling, use with caution because "
178 "there is no way to return to the tty.");
181 const int mode = var_CreateGetInteger(vd, "fb-mode");
182 bool force_resolution = true;
202 force_resolution = false;
207 if (sys->is_tty && TtyInit(vd)) {
213 sys->video_ptr = MAP_FAILED;
217 if (OpenDisplay(vd, force_resolution)) {
218 Close(VLC_OBJECT(vd));
223 video_format_t fmt = vd->fmt;
225 msg_Err(vd, "var_info.bits_per_pixel = %d", sys->var_info.bits_per_pixel);
226 switch (sys->var_info.bits_per_pixel) {
227 case 8: /* FIXME: set the palette */
228 fmt.i_chroma = VLC_CODEC_RGB8;
231 fmt.i_chroma = VLC_CODEC_RGB15;
234 fmt.i_chroma = VLC_CODEC_RGB16;
237 fmt.i_chroma = VLC_CODEC_RGB24;
240 fmt.i_chroma = VLC_CODEC_RGB32;
243 msg_Err(vd, "unknown screen depth %i",
244 sys->var_info.bits_per_pixel);
245 Close(VLC_OBJECT(vd));
248 if (sys->var_info.bits_per_pixel != 8) {
249 fmt.i_rmask = ((1 << sys->var_info.red.length) - 1)
250 << sys->var_info.red.offset;
251 fmt.i_gmask = ((1 << sys->var_info.green.length) - 1)
252 << sys->var_info.green.offset;
253 fmt.i_bmask = ((1 << sys->var_info.blue.length) - 1)
254 << sys->var_info.blue.offset;
256 fmt.i_width = sys->width;
257 fmt.i_height = sys->height;
260 vout_display_info_t info = vd->info;
261 info.has_hide_mouse = true;
268 vd->display = Display;
269 vd->control = Control;
273 vout_display_SendEventFullscreen(vd, true);
274 vout_display_SendEventDisplaySize(vd, fmt.i_width, fmt.i_height, true);
279 * Terminate an output method created by Open
281 static void Close(vlc_object_t *object)
283 vout_display_t *vd = (vout_display_t *)object;
284 vout_display_sys_t *sys = vd->sys;
287 picture_pool_Delete(sys->pool);
288 if (!sys->is_hw_accel && sys->picture)
289 picture_Release(sys->picture);
300 static picture_pool_t *Pool(vout_display_t *vd, unsigned count)
302 vout_display_sys_t *sys = vd->sys;
306 picture_resource_t rsc;
308 memset(&rsc, 0, sizeof(rsc));
309 rsc.p[0].p_pixels = sys->video_ptr;
310 rsc.p[0].i_lines = sys->var_info.yres;
311 rsc.p[0].i_pitch = sys->line_length;
313 sys->picture = picture_NewFromResource(&vd->fmt, &rsc);
318 if (sys->is_hw_accel)
319 sys->pool = picture_pool_New(1, &sys->picture);
321 sys->pool = picture_pool_NewFromFormat(&vd->fmt, count);
325 static void Display(vout_display_t *vd, picture_t *picture)
327 vout_display_sys_t *sys = vd->sys;
329 /* swap the two Y offsets if the drivers supports panning */
331 sys->var_info.yoffset = 0;
332 /*vd->sys->var_info.yoffset = vd->sys->var_info.yres; */
334 /* the X offset should be 0, but who knows ...
335 * some other app might have played with the framebuffer */
336 sys->var_info.xoffset = 0;
338 /* FIXME 'static' is damn wrong and it's dead code ... */
339 static int panned = 0;
341 ioctl(sys->fd, FBIOPAN_DISPLAY, &sys->var_info);
346 if (!sys->is_hw_accel)
347 picture_Copy(sys->picture, picture);
349 picture_Release(picture);
351 static int Control(vout_display_t *vd, int query, va_list args)
353 vout_display_sys_t *sys = vd->sys;
356 case VOUT_DISPLAY_CHANGE_DISPLAY_SIZE: {
357 const vout_display_cfg_t *cfg = va_arg(args, const vout_display_cfg_t *);
358 if (cfg->display.width != sys->width ||
359 cfg->display.height != sys->height)
364 msg_Err(vd, "Unsupported query in vout display fb");
368 static void Manage (vout_display_t *vd)
375 if (vd->i_changes & VOUT_SIZE_CHANGE)
377 msg_Dbg(vd, "reinitializing framebuffer screen");
378 vd->i_changes &= ~VOUT_SIZE_CHANGE;
380 vout_display_SendEventDisplaySize();
383 memset(vd->sys->video_ptr, 0, vd->sys->video_size);
389 /* following functions are local */
390 static int TtyInit(vout_display_t *vd)
392 vout_display_sys_t *sys = vd->sys;
394 struct termios new_termios;
398 /* Set keyboard settings */
399 if (tcgetattr(0, &sys->old_termios) == -1) {
400 msg_Err(vd, "tcgetattr failed");
403 if (tcgetattr(0, &new_termios) == -1) {
404 msg_Err(vd, "tcgetattr failed");
407 /* new_termios.c_lflag &= ~ (ICANON | ISIG);
408 new_termios.c_lflag |= (ECHO | ECHOCTL); */
409 new_termios.c_lflag &= ~ (ICANON);
410 new_termios.c_lflag &= ~(ECHO | ECHOCTL);
411 new_termios.c_iflag = 0;
412 new_termios.c_cc[VMIN] = 1;
413 new_termios.c_cc[VTIME] = 0;
415 if (tcsetattr(0, TCSAFLUSH, &new_termios) == -1) {
416 msg_Err(vd, "tcsetattr failed");
419 ioctl(sys->tty, VT_RELDISP, VT_ACKACQ);
421 /* Set-up tty signal handler to be aware of tty changes */
422 struct sigaction sig_tty;
423 memset(&sig_tty, 0, sizeof(sig_tty));
424 sig_tty.sa_handler = SwitchDisplay;
425 sigemptyset(&sig_tty.sa_mask);
426 if (sigaction(SIGUSR1, &sig_tty, &sys->sig_usr1) ||
427 sigaction(SIGUSR2, &sig_tty, &sys->sig_usr2)) {
428 msg_Err(vd, "cannot set signal handler (%m)");
429 /* FIXME SIGUSR1 could have succeed */
433 /* Set-up tty according to new signal handler */
434 if (-1 == ioctl(sys->tty, VT_GETMODE, &sys->vt_mode)) {
435 msg_Err(vd, "cannot get terminal mode (%m)");
438 struct vt_mode vt_mode = sys->vt_mode;
439 vt_mode.mode = VT_PROCESS;
441 vt_mode.relsig = SIGUSR1;
442 vt_mode.acqsig = SIGUSR2;
444 if (-1 == ioctl(sys->tty, VT_SETMODE, &vt_mode)) {
445 msg_Err(vd, "cannot set terminal mode (%m)");
451 sigaction(SIGUSR1, &sys->sig_usr1, NULL);
452 sigaction(SIGUSR2, &sys->sig_usr2, NULL);
454 tcsetattr(0, 0, &sys->old_termios);
458 static void TtyExit(vout_display_t *vd)
460 vout_display_sys_t *sys = vd->sys;
462 /* Reset the terminal */
463 ioctl(sys->tty, VT_SETMODE, &sys->vt_mode);
465 /* Remove signal handlers */
466 sigaction(SIGUSR1, &sys->sig_usr1, NULL);
467 sigaction(SIGUSR2, &sys->sig_usr2, NULL);
469 /* Reset the keyboard state */
470 tcsetattr(0, 0, &sys->old_termios);
472 /* Return to text mode */
476 /*****************************************************************************
477 * OpenDisplay: initialize framebuffer
478 *****************************************************************************/
479 static int OpenDisplay(vout_display_t *vd, bool force_resolution)
481 vout_display_sys_t *sys = vd->sys;
482 char *psz_device; /* framebuffer device path */
484 /* Open framebuffer device */
485 if (!(psz_device = config_GetPsz(vd, FB_DEV_VAR))) {
486 msg_Err(vd, "don't know which fb device to open");
490 sys->fd = utf8_open(psz_device, O_RDWR);
492 msg_Err(vd, "cannot open %s (%m)", psz_device);
498 /* Get framebuffer device information */
499 if (ioctl(sys->fd, FBIOGET_VSCREENINFO, &sys->var_info)) {
500 msg_Err(vd, "cannot get fb info (%m)");
504 sys->old_info = sys->var_info;
506 /* Get some info on the framebuffer itself */
507 if (force_resolution) {
508 sys->var_info.xres = sys->var_info.xres_virtual = sys->width;
509 sys->var_info.yres = sys->var_info.yres_virtual = sys->height;
512 /* Set some attributes */
513 sys->var_info.activate = sys->is_tty ? FB_ACTIVATE_NXTOPEN :
515 sys->var_info.xoffset = 0;
516 sys->var_info.yoffset = 0;
518 if (ioctl(sys->fd, FBIOPUT_VSCREENINFO, &sys->var_info)) {
519 msg_Err(vd, "cannot set fb info (%m)");
524 struct fb_fix_screeninfo fix_info;
525 /* Get some information again, in the definitive configuration */
526 if (ioctl(sys->fd, FBIOGET_FSCREENINFO, &fix_info) ||
527 ioctl(sys->fd, FBIOGET_VSCREENINFO, &sys->var_info)) {
528 msg_Err(vd, "cannot get additional fb info (%m)");
530 /* Restore fb config */
531 ioctl(sys->fd, FBIOPUT_VSCREENINFO, &sys->old_info);
537 /* If the fb has limitations on mode change,
538 * then keep the resolution of the fb */
539 if ((sys->height != sys->var_info.yres) ||
540 (sys->width != sys->var_info.xres)) {
542 "using framebuffer native resolution instead of requested (%ix%i)",
543 sys->width, sys->height);
545 sys->height = sys->var_info.yres;
546 sys->width = sys->var_info.xres_virtual ? sys->var_info.xres_virtual :
548 sys->line_length = fix_info.line_length;
550 /* FIXME: if the image is full-size, it gets cropped on the left
551 * because of the xres / xres_virtual slight difference */
552 msg_Dbg(vd, "%ix%i (virtual %ix%i) (request %ix%i)",
553 sys->var_info.xres, sys->var_info.yres,
554 sys->var_info.xres_virtual,
555 sys->var_info.yres_virtual,
556 sys->width, sys->height);
559 sys->has_pan = (fix_info.ypanstep || fix_info.ywrapstep);
561 switch (sys->var_info.bits_per_pixel) {
563 sys->palette = malloc(8 * 256 * sizeof(uint16_t));
565 /* Restore fb config */
566 ioctl(sys->fd, FBIOPUT_VSCREENINFO, &sys->old_info);
571 sys->fb_cmap.start = 0;
572 sys->fb_cmap.len = 256;
573 sys->fb_cmap.red = sys->palette;
574 sys->fb_cmap.green = sys->palette + 256 * sizeof(uint16_t);
575 sys->fb_cmap.blue = sys->palette + 2 * 256 * sizeof(uint16_t);
576 sys->fb_cmap.transp = sys->palette + 3 * 256 * sizeof(uint16_t);
578 /* Save the colormap */
579 ioctl(sys->fd, FBIOGETCMAP, &sys->fb_cmap);
581 sys->bytes_per_pixel = 1;
586 sys->bytes_per_pixel = 2;
590 sys->bytes_per_pixel = 3;
594 sys->bytes_per_pixel = 4;
598 msg_Err(vd, "screen depth %d is not supported",
599 sys->var_info.bits_per_pixel);
601 /* Restore fb config */
602 ioctl(sys->fd, FBIOPUT_VSCREENINFO, &sys->old_info);
608 sys->video_size = sys->line_length * sys->var_info.yres_virtual;
610 /* Map a framebuffer at the beginning */
611 sys->video_ptr = mmap(NULL, sys->video_size,
612 PROT_READ | PROT_WRITE, MAP_SHARED, sys->fd, 0);
614 if (sys->video_ptr == MAP_FAILED) {
615 msg_Err(vd, "cannot map video memory (%m)");
617 if (sys->var_info.bits_per_pixel == 8) {
622 /* Restore fb config */
623 ioctl(sys->fd, FBIOPUT_VSCREENINFO, &sys->old_info);
628 /* Clear the screen */
629 memset(sys->video_ptr, 0, sys->video_size);
632 "framebuffer type=%d, visual=%d, ypanstep=%d, ywrap=%d, accel=%d",
633 fix_info.type, fix_info.visual,
634 fix_info.ypanstep, fix_info.ywrapstep, fix_info.accel);
638 /*****************************************************************************
639 * CloseDisplay: terminate FB video thread output method
640 *****************************************************************************/
641 static void CloseDisplay(vout_display_t *vd)
643 vout_display_sys_t *sys = vd->sys;
645 if (sys->video_ptr != MAP_FAILED) {
647 memset(sys->video_ptr, 0, sys->video_size);
648 munmap(sys->video_ptr, sys->video_size);
652 /* Restore palette */
653 if (sys->var_info.bits_per_pixel == 8) {
654 ioctl(sys->fd, FBIOPUTCMAP, &sys->fb_cmap);
659 /* Restore fb config */
660 ioctl(sys->fd, FBIOPUT_VSCREENINFO, &sys->old_info);
667 /*****************************************************************************
668 * SwitchDisplay: VT change signal handler
669 *****************************************************************************
670 * This function activates or deactivates the output of the thread. It is
671 * called by the VT driver, on terminal change.
672 *****************************************************************************/
673 static void SwitchDisplay(int i_signal)
675 VLC_UNUSED(i_signal);
679 vlc_mutex_lock(&p_vout_bank->lock);
681 /* XXX: only test the first video output */
682 if (p_vout_bank->i_count)
684 vd = p_vout_bank->pp_vout[0];
688 case SIGUSR1: /* vt has been released */
690 ioctl(sys->tty, VT_RELDISP, 1);
692 case SIGUSR2: /* vt has been acquired */
694 ioctl(sys->tty, VT_RELDISP, VT_ACTIVATE);
695 /* handle blanking */
696 vlc_mutex_lock(&vd->change_lock);
697 vd->i_changes |= VOUT_SIZE_CHANGE;
698 vlc_mutex_unlock(&vd->change_lock);
703 vlc_mutex_unlock(&p_vout_bank->lock);
707 /*****************************************************************************
708 * TextMode and GfxMode : switch tty to text/graphic mode
709 *****************************************************************************
710 * These functions toggle the tty mode.
711 *****************************************************************************/
712 static void TextMode(int tty)
714 /* return to text mode */
715 if (-1 == ioctl(tty, KDSETMODE, KD_TEXT)) {
716 /*msg_Err(vd, "failed ioctl KDSETMODE KD_TEXT");*/
720 static void GfxMode(int tty)
722 /* switch to graphic mode */
723 if (-1 == ioctl(tty, KDSETMODE, KD_GRAPHICS)) {
724 /*msg_Err(vd, "failed ioctl KDSETMODE KD_GRAPHICS");*/