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 #define CHROMA_TEXT N_("Image format (default RGB).")
78 #define CHROMA_LONGTEXT N_("Chroma fourcc used by the framebuffer. Default is RGB since the fb device has no way to repot its chroma.")
80 static int Open (vlc_object_t *);
81 static void Close(vlc_object_t *);
84 set_shortname("Framebuffer")
85 set_category(CAT_VIDEO)
86 set_subcategory(SUBCAT_VIDEO_VOUT)
87 add_file(FB_DEV_VAR, "/dev/fb0", NULL, DEVICE_TEXT, DEVICE_LONGTEXT,
89 add_bool("fb-tty", true, NULL, TTY_TEXT, TTY_LONGTEXT, true)
90 add_string( "fb-chroma", NULL, NULL, CHROMA_TEXT, CHROMA_LONGTEXT, true )
91 add_obsolete_string("fb-aspect-ratio")
92 add_integer("fb-mode", 4, NULL, FB_MODE_TEXT, FB_MODE_LONGTEXT,
94 add_bool("fb-hw-accel", true, NULL, HW_ACCEL_TEXT, HW_ACCEL_LONGTEXT,
96 set_description(N_("GNU/Linux framebuffer video output"))
97 set_capability("vout display", 30)
98 set_callbacks(Open, Close)
101 /*****************************************************************************
103 *****************************************************************************/
104 static picture_pool_t *Pool (vout_display_t *, unsigned);
105 static void Display(vout_display_t *, picture_t *);
106 static int Control(vout_display_t *, int, va_list);
107 static void Manage (vout_display_t *);
110 static int OpenDisplay (vout_display_t *, bool force_resolution);
111 static void CloseDisplay (vout_display_t *);
112 static void SwitchDisplay(int i_signal);
113 static void TextMode (int tty);
114 static void GfxMode (int tty);
116 static int TtyInit(vout_display_t *);
117 static void TtyExit(vout_display_t *);
120 struct vout_display_sys_t {
121 /* System information */
122 int tty; /* tty device handle */
124 struct termios old_termios;
126 /* Original configuration information */
127 struct sigaction sig_usr1; /* USR1 previous handler */
128 struct sigaction sig_usr2; /* USR2 previous handler */
129 struct vt_mode vt_mode; /* previous VT mode */
131 /* Framebuffer information */
132 int fd; /* device handle */
133 struct fb_var_screeninfo old_info; /* original mode information */
134 struct fb_var_screeninfo var_info; /* current mode information */
135 bool has_pan; /* does device supports panning ? */
136 struct fb_cmap fb_cmap; /* original colormap */
137 uint16_t *palette; /* original palette */
138 bool is_hw_accel; /* has hardware support */
140 /* Video information */
143 uint32_t line_length;
148 uint8_t *video_ptr; /* base adress */
149 size_t video_size; /* page size */
152 picture_pool_t *pool;
156 static void clear_screen(vout_display_sys_t *sys)
160 /* XXX: add other chromas */
163 unsigned int j, size = sys->video_size / 4;
164 uint32_t *ptr = (uint32_t*)((uintptr_t)(sys->video_ptr + 3) & ~3);
165 for(j=0; j < size; j++)
166 ptr[j] = 0x10801080; /* U = V = 16, Y = 128 */
170 memset(sys->video_ptr, 0, sys->video_size);
175 * This function allocates and initializes a FB vout method.
177 static int Open(vlc_object_t *object)
179 vout_display_t *vd = (vout_display_t *)object;
180 vout_display_sys_t *sys;
182 /* Allocate instance and initialize some members */
183 vd->sys = sys = calloc(1, sizeof(*sys));
187 /* Does the framebuffer uses hw acceleration? */
188 sys->is_hw_accel = var_CreateGetBool(vd, "fb-hw-accel");
190 /* Set tty and fb devices */
191 sys->tty = 0; /* 0 == /dev/tty0 == current console */
192 sys->is_tty = var_CreateGetBool(vd, "fb-tty");
193 #if !defined(WIN32) && defined(HAVE_ISATTY)
194 /* Check that stdin is a TTY */
195 if (sys->is_tty && !isatty(0)) {
196 msg_Warn(vd, "fd 0 is not a TTY");
200 msg_Warn(vd, "disabling tty handling, use with caution because "
201 "there is no way to return to the tty.");
204 const int mode = var_CreateGetInteger(vd, "fb-mode");
205 bool force_resolution = true;
225 force_resolution = false;
229 char *psz_chroma = var_CreateGetNonEmptyString (vd, "fb-chroma");
232 sys->chroma = vlc_fourcc_GetCodecFromString (VIDEO_ES, psz_chroma);
235 msg_Dbg (vd, "forcing chroma '%s'", psz_chroma);
237 msg_Warn (vd, "chroma %s invalid, using default", psz_chroma);
245 if (sys->is_tty && TtyInit(vd)) {
251 sys->video_ptr = MAP_FAILED;
255 if (OpenDisplay(vd, force_resolution)) {
256 Close(VLC_OBJECT(vd));
261 video_format_t fmt = vd->fmt;
265 fmt.i_chroma = sys->chroma;
271 msg_Dbg (vd, "%d bppd", sys->var_info.bits_per_pixel);
272 switch (sys->var_info.bits_per_pixel) {
273 case 8: /* FIXME: set the palette */
274 fmt.i_chroma = VLC_CODEC_RGB8;
277 fmt.i_chroma = VLC_CODEC_RGB15;
280 fmt.i_chroma = VLC_CODEC_RGB16;
283 fmt.i_chroma = VLC_CODEC_RGB24;
286 fmt.i_chroma = VLC_CODEC_RGB32;
289 msg_Err(vd, "unknown screendepth %i", sys->var_info.bits_per_pixel);
290 Close(VLC_OBJECT(vd));
293 if (sys->var_info.bits_per_pixel != 8) {
294 fmt.i_rmask = ((1 << sys->var_info.red.length) - 1)
295 << sys->var_info.red.offset;
296 fmt.i_gmask = ((1 << sys->var_info.green.length) - 1)
297 << sys->var_info.green.offset;
298 fmt.i_bmask = ((1 << sys->var_info.blue.length) - 1)
299 << sys->var_info.blue.offset;
303 fmt.i_width = sys->width;
304 fmt.i_height = sys->height;
307 vout_display_info_t info = vd->info;
308 info.has_hide_mouse = true;
315 vd->display = Display;
316 vd->control = Control;
320 vout_display_SendEventFullscreen(vd, true);
321 vout_display_SendEventDisplaySize(vd, fmt.i_width, fmt.i_height, true);
326 * Terminate an output method created by Open
328 static void Close(vlc_object_t *object)
330 vout_display_t *vd = (vout_display_t *)object;
331 vout_display_sys_t *sys = vd->sys;
334 picture_pool_Delete(sys->pool);
335 if (!sys->is_hw_accel && sys->picture)
336 picture_Release(sys->picture);
347 static picture_pool_t *Pool(vout_display_t *vd, unsigned count)
349 vout_display_sys_t *sys = vd->sys;
353 picture_resource_t rsc;
355 memset(&rsc, 0, sizeof(rsc));
356 rsc.p[0].p_pixels = sys->video_ptr;
357 rsc.p[0].i_lines = sys->var_info.yres;
358 rsc.p[0].i_pitch = sys->line_length;
360 sys->picture = picture_NewFromResource(&vd->fmt, &rsc);
365 if (sys->is_hw_accel)
366 sys->pool = picture_pool_New(1, &sys->picture);
368 sys->pool = picture_pool_NewFromFormat(&vd->fmt, count);
372 static void Display(vout_display_t *vd, picture_t *picture)
374 vout_display_sys_t *sys = vd->sys;
376 /* swap the two Y offsets if the drivers supports panning */
378 sys->var_info.yoffset = 0;
379 /*vd->sys->var_info.yoffset = vd->sys->var_info.yres; */
381 /* the X offset should be 0, but who knows ...
382 * some other app might have played with the framebuffer */
383 sys->var_info.xoffset = 0;
385 /* FIXME 'static' is damn wrong and it's dead code ... */
386 static int panned = 0;
388 ioctl(sys->fd, FBIOPAN_DISPLAY, &sys->var_info);
393 if (!sys->is_hw_accel)
394 picture_Copy(sys->picture, picture);
396 picture_Release(picture);
398 static int Control(vout_display_t *vd, int query, va_list args)
400 vout_display_sys_t *sys = vd->sys;
403 case VOUT_DISPLAY_CHANGE_DISPLAY_SIZE: {
404 const vout_display_cfg_t *cfg = va_arg(args, const vout_display_cfg_t *);
405 if (cfg->display.width != sys->width ||
406 cfg->display.height != sys->height)
411 msg_Err(vd, "Unsupported query in vout display fb");
415 static void Manage (vout_display_t *vd)
422 if (vd->i_changes & VOUT_SIZE_CHANGE)
424 msg_Dbg(vd, "reinitializing framebuffer screen");
425 vd->i_changes &= ~VOUT_SIZE_CHANGE;
427 vout_display_SendEventDisplaySize();
429 clear_screen (vd->sys);
434 /* following functions are local */
435 static int TtyInit(vout_display_t *vd)
437 vout_display_sys_t *sys = vd->sys;
439 struct termios new_termios;
443 /* Set keyboard settings */
444 if (tcgetattr(0, &sys->old_termios) == -1) {
445 msg_Err(vd, "tcgetattr failed");
448 if (tcgetattr(0, &new_termios) == -1) {
449 msg_Err(vd, "tcgetattr failed");
452 /* new_termios.c_lflag &= ~ (ICANON | ISIG);
453 new_termios.c_lflag |= (ECHO | ECHOCTL); */
454 new_termios.c_lflag &= ~ (ICANON);
455 new_termios.c_lflag &= ~(ECHO | ECHOCTL);
456 new_termios.c_iflag = 0;
457 new_termios.c_cc[VMIN] = 1;
458 new_termios.c_cc[VTIME] = 0;
460 if (tcsetattr(0, TCSAFLUSH, &new_termios) == -1) {
461 msg_Err(vd, "tcsetattr failed");
464 ioctl(sys->tty, VT_RELDISP, VT_ACKACQ);
466 /* Set-up tty signal handler to be aware of tty changes */
467 struct sigaction sig_tty;
468 memset(&sig_tty, 0, sizeof(sig_tty));
469 sig_tty.sa_handler = SwitchDisplay;
470 sigemptyset(&sig_tty.sa_mask);
471 if (sigaction(SIGUSR1, &sig_tty, &sys->sig_usr1) ||
472 sigaction(SIGUSR2, &sig_tty, &sys->sig_usr2)) {
473 msg_Err(vd, "cannot set signal handler (%m)");
474 /* FIXME SIGUSR1 could have succeed */
478 /* Set-up tty according to new signal handler */
479 if (-1 == ioctl(sys->tty, VT_GETMODE, &sys->vt_mode)) {
480 msg_Err(vd, "cannot get terminal mode (%m)");
483 struct vt_mode vt_mode = sys->vt_mode;
484 vt_mode.mode = VT_PROCESS;
486 vt_mode.relsig = SIGUSR1;
487 vt_mode.acqsig = SIGUSR2;
489 if (-1 == ioctl(sys->tty, VT_SETMODE, &vt_mode)) {
490 msg_Err(vd, "cannot set terminal mode (%m)");
496 sigaction(SIGUSR1, &sys->sig_usr1, NULL);
497 sigaction(SIGUSR2, &sys->sig_usr2, NULL);
499 tcsetattr(0, 0, &sys->old_termios);
503 static void TtyExit(vout_display_t *vd)
505 vout_display_sys_t *sys = vd->sys;
507 /* Reset the terminal */
508 ioctl(sys->tty, VT_SETMODE, &sys->vt_mode);
510 /* Remove signal handlers */
511 sigaction(SIGUSR1, &sys->sig_usr1, NULL);
512 sigaction(SIGUSR2, &sys->sig_usr2, NULL);
514 /* Reset the keyboard state */
515 tcsetattr(0, 0, &sys->old_termios);
517 /* Return to text mode */
521 /*****************************************************************************
522 * OpenDisplay: initialize framebuffer
523 *****************************************************************************/
524 static int OpenDisplay(vout_display_t *vd, bool force_resolution)
526 vout_display_sys_t *sys = vd->sys;
527 char *psz_device; /* framebuffer device path */
529 /* Open framebuffer device */
530 if (!(psz_device = config_GetPsz(vd, FB_DEV_VAR))) {
531 msg_Err(vd, "don't know which fb device to open");
535 sys->fd = utf8_open(psz_device, O_RDWR);
537 msg_Err(vd, "cannot open %s (%m)", psz_device);
543 /* Get framebuffer device information */
544 if (ioctl(sys->fd, FBIOGET_VSCREENINFO, &sys->var_info)) {
545 msg_Err(vd, "cannot get fb info (%m)");
549 sys->old_info = sys->var_info;
551 /* Get some info on the framebuffer itself */
552 if (force_resolution) {
553 sys->var_info.xres = sys->var_info.xres_virtual = sys->width;
554 sys->var_info.yres = sys->var_info.yres_virtual = sys->height;
557 /* Set some attributes */
558 sys->var_info.activate = sys->is_tty ? FB_ACTIVATE_NXTOPEN :
560 sys->var_info.xoffset = 0;
561 sys->var_info.yoffset = 0;
563 if (ioctl(sys->fd, FBIOPUT_VSCREENINFO, &sys->var_info)) {
564 msg_Err(vd, "cannot set fb info (%m)");
569 struct fb_fix_screeninfo fix_info;
570 /* Get some information again, in the definitive configuration */
571 if (ioctl(sys->fd, FBIOGET_FSCREENINFO, &fix_info) ||
572 ioctl(sys->fd, FBIOGET_VSCREENINFO, &sys->var_info)) {
573 msg_Err(vd, "cannot get additional fb info (%m)");
575 /* Restore fb config */
576 ioctl(sys->fd, FBIOPUT_VSCREENINFO, &sys->old_info);
582 /* If the fb has limitations on mode change,
583 * then keep the resolution of the fb */
584 if ((sys->height != sys->var_info.yres) ||
585 (sys->width != sys->var_info.xres)) {
587 "using framebuffer native resolution instead of requested (%ix%i)",
588 sys->width, sys->height);
590 sys->height = sys->var_info.yres;
591 sys->width = sys->var_info.xres_virtual ? sys->var_info.xres_virtual :
593 sys->line_length = fix_info.line_length;
595 /* FIXME: if the image is full-size, it gets cropped on the left
596 * because of the xres / xres_virtual slight difference */
597 msg_Dbg(vd, "%ix%i (virtual %ix%i) (request %ix%i)",
598 sys->var_info.xres, sys->var_info.yres,
599 sys->var_info.xres_virtual,
600 sys->var_info.yres_virtual,
601 sys->width, sys->height);
604 sys->has_pan = (fix_info.ypanstep || fix_info.ywrapstep);
606 switch (sys->var_info.bits_per_pixel) {
608 sys->palette = malloc(8 * 256 * sizeof(uint16_t));
610 /* Restore fb config */
611 ioctl(sys->fd, FBIOPUT_VSCREENINFO, &sys->old_info);
616 sys->fb_cmap.start = 0;
617 sys->fb_cmap.len = 256;
618 sys->fb_cmap.red = sys->palette;
619 sys->fb_cmap.green = sys->palette + 256 * sizeof(uint16_t);
620 sys->fb_cmap.blue = sys->palette + 2 * 256 * sizeof(uint16_t);
621 sys->fb_cmap.transp = sys->palette + 3 * 256 * sizeof(uint16_t);
623 /* Save the colormap */
624 ioctl(sys->fd, FBIOGETCMAP, &sys->fb_cmap);
626 sys->bytes_per_pixel = 1;
631 sys->bytes_per_pixel = 2;
635 sys->bytes_per_pixel = 3;
639 sys->bytes_per_pixel = 4;
643 msg_Err(vd, "screen depth %d is not supported",
644 sys->var_info.bits_per_pixel);
646 /* Restore fb config */
647 ioctl(sys->fd, FBIOPUT_VSCREENINFO, &sys->old_info);
653 sys->video_size = sys->line_length * sys->var_info.yres_virtual;
655 /* Map a framebuffer at the beginning */
656 sys->video_ptr = mmap(NULL, sys->video_size,
657 PROT_READ | PROT_WRITE, MAP_SHARED, sys->fd, 0);
659 if (sys->video_ptr == MAP_FAILED) {
660 msg_Err(vd, "cannot map video memory (%m)");
662 if (sys->var_info.bits_per_pixel == 8) {
667 /* Restore fb config */
668 ioctl(sys->fd, FBIOPUT_VSCREENINFO, &sys->old_info);
677 "framebuffer type=%d, visual=%d, ypanstep=%d, ywrap=%d, accel=%d",
678 fix_info.type, fix_info.visual,
679 fix_info.ypanstep, fix_info.ywrapstep, fix_info.accel);
683 /*****************************************************************************
684 * CloseDisplay: terminate FB video thread output method
685 *****************************************************************************/
686 static void CloseDisplay(vout_display_t *vd)
688 vout_display_sys_t *sys = vd->sys;
690 if (sys->video_ptr != MAP_FAILED) {
692 munmap(sys->video_ptr, sys->video_size);
696 /* Restore palette */
697 if (sys->var_info.bits_per_pixel == 8) {
698 ioctl(sys->fd, FBIOPUTCMAP, &sys->fb_cmap);
703 /* Restore fb config */
704 ioctl(sys->fd, FBIOPUT_VSCREENINFO, &sys->old_info);
711 /*****************************************************************************
712 * SwitchDisplay: VT change signal handler
713 *****************************************************************************
714 * This function activates or deactivates the output of the thread. It is
715 * called by the VT driver, on terminal change.
716 *****************************************************************************/
717 static void SwitchDisplay(int i_signal)
719 VLC_UNUSED(i_signal);
723 vlc_mutex_lock(&p_vout_bank->lock);
725 /* XXX: only test the first video output */
726 if (p_vout_bank->i_count)
728 vd = p_vout_bank->pp_vout[0];
732 case SIGUSR1: /* vt has been released */
734 ioctl(sys->tty, VT_RELDISP, 1);
736 case SIGUSR2: /* vt has been acquired */
738 ioctl(sys->tty, VT_RELDISP, VT_ACTIVATE);
739 /* handle blanking */
740 vlc_mutex_lock(&vd->change_lock);
741 vd->i_changes |= VOUT_SIZE_CHANGE;
742 vlc_mutex_unlock(&vd->change_lock);
747 vlc_mutex_unlock(&p_vout_bank->lock);
751 /*****************************************************************************
752 * TextMode and GfxMode : switch tty to text/graphic mode
753 *****************************************************************************
754 * These functions toggle the tty mode.
755 *****************************************************************************/
756 static void TextMode(int tty)
758 /* return to text mode */
759 if (-1 == ioctl(tty, KDSETMODE, KD_TEXT)) {
760 /*msg_Err(vd, "failed ioctl KDSETMODE KD_TEXT");*/
764 static void GfxMode(int tty)
766 /* switch to graphic mode */
767 if (-1 == ioctl(tty, KDSETMODE, KD_GRAPHICS)) {
768 /*msg_Err(vd, "failed ioctl KDSETMODE KD_GRAPHICS");*/