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 report 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 ClearScreen(vout_display_sys_t *sys)
158 switch (sys->chroma) {
159 /* XXX: add other chromas */
160 case VLC_CODEC_UYVY: {
161 unsigned int j, size = sys->video_size / 4;
162 uint32_t *ptr = (uint32_t*)((uintptr_t)(sys->video_ptr + 3) & ~3);
163 for(j=0; j < size; j++)
164 ptr[j] = 0x10801080; /* U = V = 16, Y = 128 */
168 memset(sys->video_ptr, 0, sys->video_size);
173 * This function allocates and initializes a FB vout method.
175 static int Open(vlc_object_t *object)
177 vout_display_t *vd = (vout_display_t *)object;
178 vout_display_sys_t *sys;
180 /* Allocate instance and initialize some members */
181 vd->sys = sys = calloc(1, sizeof(*sys));
185 /* Does the framebuffer uses hw acceleration? */
186 sys->is_hw_accel = var_CreateGetBool(vd, "fb-hw-accel");
188 /* Set tty and fb devices */
189 sys->tty = 0; /* 0 == /dev/tty0 == current console */
190 sys->is_tty = var_CreateGetBool(vd, "fb-tty");
191 #if !defined(WIN32) && defined(HAVE_ISATTY)
192 /* Check that stdin is a TTY */
193 if (sys->is_tty && !isatty(0)) {
194 msg_Warn(vd, "fd 0 is not a TTY");
198 msg_Warn(vd, "disabling tty handling, use with caution because "
199 "there is no way to return to the tty.");
202 const int mode = var_CreateGetInteger(vd, "fb-mode");
203 bool force_resolution = true;
223 force_resolution = false;
227 char *chroma = var_CreateGetNonEmptyString(vd, "fb-chroma");
229 sys->chroma = vlc_fourcc_GetCodecFromString(VIDEO_ES, chroma);
232 msg_Dbg(vd, "forcing chroma '%s'", chroma);
234 msg_Warn(vd, "chroma %s invalid, using default", chroma);
242 if (sys->is_tty && TtyInit(vd)) {
248 sys->video_ptr = MAP_FAILED;
252 if (OpenDisplay(vd, force_resolution)) {
253 Close(VLC_OBJECT(vd));
258 video_format_t fmt = vd->fmt;
261 fmt.i_chroma = sys->chroma;
266 msg_Dbg(vd, "%d bppd", sys->var_info.bits_per_pixel);
267 switch (sys->var_info.bits_per_pixel) {
268 case 8: /* FIXME: set the palette */
269 fmt.i_chroma = VLC_CODEC_RGB8;
272 fmt.i_chroma = VLC_CODEC_RGB15;
275 fmt.i_chroma = VLC_CODEC_RGB16;
278 fmt.i_chroma = VLC_CODEC_RGB24;
281 fmt.i_chroma = VLC_CODEC_RGB32;
284 msg_Err(vd, "unknown screendepth %i", sys->var_info.bits_per_pixel);
285 Close(VLC_OBJECT(vd));
288 if (sys->var_info.bits_per_pixel != 8) {
289 fmt.i_rmask = ((1 << sys->var_info.red.length) - 1)
290 << sys->var_info.red.offset;
291 fmt.i_gmask = ((1 << sys->var_info.green.length) - 1)
292 << sys->var_info.green.offset;
293 fmt.i_bmask = ((1 << sys->var_info.blue.length) - 1)
294 << sys->var_info.blue.offset;
298 fmt.i_width = sys->width;
299 fmt.i_height = sys->height;
302 vout_display_info_t info = vd->info;
303 info.has_hide_mouse = true;
310 vd->display = Display;
311 vd->control = Control;
315 vout_display_SendEventFullscreen(vd, true);
316 vout_display_SendEventDisplaySize(vd, fmt.i_width, fmt.i_height, true);
321 * Terminate an output method created by Open
323 static void Close(vlc_object_t *object)
325 vout_display_t *vd = (vout_display_t *)object;
326 vout_display_sys_t *sys = vd->sys;
329 picture_pool_Delete(sys->pool);
330 if (!sys->is_hw_accel && sys->picture)
331 picture_Release(sys->picture);
342 static picture_pool_t *Pool(vout_display_t *vd, unsigned count)
344 vout_display_sys_t *sys = vd->sys;
348 picture_resource_t rsc;
350 memset(&rsc, 0, sizeof(rsc));
351 rsc.p[0].p_pixels = sys->video_ptr;
352 rsc.p[0].i_lines = sys->var_info.yres;
353 rsc.p[0].i_pitch = sys->line_length;
355 sys->picture = picture_NewFromResource(&vd->fmt, &rsc);
360 if (sys->is_hw_accel)
361 sys->pool = picture_pool_New(1, &sys->picture);
363 sys->pool = picture_pool_NewFromFormat(&vd->fmt, count);
367 static void Display(vout_display_t *vd, picture_t *picture)
369 vout_display_sys_t *sys = vd->sys;
371 /* swap the two Y offsets if the drivers supports panning */
373 sys->var_info.yoffset = 0;
374 /*vd->sys->var_info.yoffset = vd->sys->var_info.yres; */
376 /* the X offset should be 0, but who knows ...
377 * some other app might have played with the framebuffer */
378 sys->var_info.xoffset = 0;
380 /* FIXME 'static' is damn wrong and it's dead code ... */
381 static int panned = 0;
383 ioctl(sys->fd, FBIOPAN_DISPLAY, &sys->var_info);
388 if (!sys->is_hw_accel)
389 picture_Copy(sys->picture, picture);
391 picture_Release(picture);
393 static int Control(vout_display_t *vd, int query, va_list args)
395 vout_display_sys_t *sys = vd->sys;
398 case VOUT_DISPLAY_CHANGE_DISPLAY_SIZE: {
399 const vout_display_cfg_t *cfg = va_arg(args, const vout_display_cfg_t *);
400 if (cfg->display.width != sys->width ||
401 cfg->display.height != sys->height)
406 msg_Err(vd, "Unsupported query in vout display fb");
410 static void Manage (vout_display_t *vd)
417 if (vd->i_changes & VOUT_SIZE_CHANGE)
419 msg_Dbg(vd, "reinitializing framebuffer screen");
420 vd->i_changes &= ~VOUT_SIZE_CHANGE;
422 vout_display_SendEventDisplaySize();
424 ClearScreen(vd->sys);
429 /* following functions are local */
430 static int TtyInit(vout_display_t *vd)
432 vout_display_sys_t *sys = vd->sys;
434 struct termios new_termios;
438 /* Set keyboard settings */
439 if (tcgetattr(0, &sys->old_termios) == -1) {
440 msg_Err(vd, "tcgetattr failed");
443 if (tcgetattr(0, &new_termios) == -1) {
444 msg_Err(vd, "tcgetattr failed");
447 /* new_termios.c_lflag &= ~ (ICANON | ISIG);
448 new_termios.c_lflag |= (ECHO | ECHOCTL); */
449 new_termios.c_lflag &= ~ (ICANON);
450 new_termios.c_lflag &= ~(ECHO | ECHOCTL);
451 new_termios.c_iflag = 0;
452 new_termios.c_cc[VMIN] = 1;
453 new_termios.c_cc[VTIME] = 0;
455 if (tcsetattr(0, TCSAFLUSH, &new_termios) == -1) {
456 msg_Err(vd, "tcsetattr failed");
459 ioctl(sys->tty, VT_RELDISP, VT_ACKACQ);
461 /* Set-up tty signal handler to be aware of tty changes */
462 struct sigaction sig_tty;
463 memset(&sig_tty, 0, sizeof(sig_tty));
464 sig_tty.sa_handler = SwitchDisplay;
465 sigemptyset(&sig_tty.sa_mask);
466 if (sigaction(SIGUSR1, &sig_tty, &sys->sig_usr1) ||
467 sigaction(SIGUSR2, &sig_tty, &sys->sig_usr2)) {
468 msg_Err(vd, "cannot set signal handler (%m)");
469 /* FIXME SIGUSR1 could have succeed */
473 /* Set-up tty according to new signal handler */
474 if (-1 == ioctl(sys->tty, VT_GETMODE, &sys->vt_mode)) {
475 msg_Err(vd, "cannot get terminal mode (%m)");
478 struct vt_mode vt_mode = sys->vt_mode;
479 vt_mode.mode = VT_PROCESS;
481 vt_mode.relsig = SIGUSR1;
482 vt_mode.acqsig = SIGUSR2;
484 if (-1 == ioctl(sys->tty, VT_SETMODE, &vt_mode)) {
485 msg_Err(vd, "cannot set terminal mode (%m)");
491 sigaction(SIGUSR1, &sys->sig_usr1, NULL);
492 sigaction(SIGUSR2, &sys->sig_usr2, NULL);
494 tcsetattr(0, 0, &sys->old_termios);
498 static void TtyExit(vout_display_t *vd)
500 vout_display_sys_t *sys = vd->sys;
502 /* Reset the terminal */
503 ioctl(sys->tty, VT_SETMODE, &sys->vt_mode);
505 /* Remove signal handlers */
506 sigaction(SIGUSR1, &sys->sig_usr1, NULL);
507 sigaction(SIGUSR2, &sys->sig_usr2, NULL);
509 /* Reset the keyboard state */
510 tcsetattr(0, 0, &sys->old_termios);
512 /* Return to text mode */
516 /*****************************************************************************
517 * OpenDisplay: initialize framebuffer
518 *****************************************************************************/
519 static int OpenDisplay(vout_display_t *vd, bool force_resolution)
521 vout_display_sys_t *sys = vd->sys;
522 char *psz_device; /* framebuffer device path */
524 /* Open framebuffer device */
525 if (!(psz_device = config_GetPsz(vd, FB_DEV_VAR))) {
526 msg_Err(vd, "don't know which fb device to open");
530 sys->fd = utf8_open(psz_device, O_RDWR);
532 msg_Err(vd, "cannot open %s (%m)", psz_device);
538 /* Get framebuffer device information */
539 if (ioctl(sys->fd, FBIOGET_VSCREENINFO, &sys->var_info)) {
540 msg_Err(vd, "cannot get fb info (%m)");
544 sys->old_info = sys->var_info;
546 /* Get some info on the framebuffer itself */
547 if (force_resolution) {
548 sys->var_info.xres = sys->var_info.xres_virtual = sys->width;
549 sys->var_info.yres = sys->var_info.yres_virtual = sys->height;
552 /* Set some attributes */
553 sys->var_info.activate = sys->is_tty ? FB_ACTIVATE_NXTOPEN :
555 sys->var_info.xoffset = 0;
556 sys->var_info.yoffset = 0;
558 if (ioctl(sys->fd, FBIOPUT_VSCREENINFO, &sys->var_info)) {
559 msg_Err(vd, "cannot set fb info (%m)");
564 struct fb_fix_screeninfo fix_info;
565 /* Get some information again, in the definitive configuration */
566 if (ioctl(sys->fd, FBIOGET_FSCREENINFO, &fix_info) ||
567 ioctl(sys->fd, FBIOGET_VSCREENINFO, &sys->var_info)) {
568 msg_Err(vd, "cannot get additional fb info (%m)");
570 /* Restore fb config */
571 ioctl(sys->fd, FBIOPUT_VSCREENINFO, &sys->old_info);
577 /* If the fb has limitations on mode change,
578 * then keep the resolution of the fb */
579 if ((sys->height != sys->var_info.yres) ||
580 (sys->width != sys->var_info.xres)) {
582 "using framebuffer native resolution instead of requested (%ix%i)",
583 sys->width, sys->height);
585 sys->height = sys->var_info.yres;
586 sys->width = sys->var_info.xres_virtual ? sys->var_info.xres_virtual :
588 sys->line_length = fix_info.line_length;
590 /* FIXME: if the image is full-size, it gets cropped on the left
591 * because of the xres / xres_virtual slight difference */
592 msg_Dbg(vd, "%ix%i (virtual %ix%i) (request %ix%i)",
593 sys->var_info.xres, sys->var_info.yres,
594 sys->var_info.xres_virtual,
595 sys->var_info.yres_virtual,
596 sys->width, sys->height);
599 sys->has_pan = (fix_info.ypanstep || fix_info.ywrapstep);
601 switch (sys->var_info.bits_per_pixel) {
603 sys->palette = malloc(8 * 256 * sizeof(uint16_t));
605 /* Restore fb config */
606 ioctl(sys->fd, FBIOPUT_VSCREENINFO, &sys->old_info);
611 sys->fb_cmap.start = 0;
612 sys->fb_cmap.len = 256;
613 sys->fb_cmap.red = sys->palette;
614 sys->fb_cmap.green = sys->palette + 256 * sizeof(uint16_t);
615 sys->fb_cmap.blue = sys->palette + 2 * 256 * sizeof(uint16_t);
616 sys->fb_cmap.transp = sys->palette + 3 * 256 * sizeof(uint16_t);
618 /* Save the colormap */
619 ioctl(sys->fd, FBIOGETCMAP, &sys->fb_cmap);
621 sys->bytes_per_pixel = 1;
626 sys->bytes_per_pixel = 2;
630 sys->bytes_per_pixel = 3;
634 sys->bytes_per_pixel = 4;
638 msg_Err(vd, "screen depth %d is not supported",
639 sys->var_info.bits_per_pixel);
641 /* Restore fb config */
642 ioctl(sys->fd, FBIOPUT_VSCREENINFO, &sys->old_info);
648 sys->video_size = sys->line_length * sys->var_info.yres_virtual;
650 /* Map a framebuffer at the beginning */
651 sys->video_ptr = mmap(NULL, sys->video_size,
652 PROT_READ | PROT_WRITE, MAP_SHARED, sys->fd, 0);
654 if (sys->video_ptr == MAP_FAILED) {
655 msg_Err(vd, "cannot map video memory (%m)");
657 if (sys->var_info.bits_per_pixel == 8) {
662 /* Restore fb config */
663 ioctl(sys->fd, FBIOPUT_VSCREENINFO, &sys->old_info);
672 "framebuffer type=%d, visual=%d, ypanstep=%d, ywrap=%d, accel=%d",
673 fix_info.type, fix_info.visual,
674 fix_info.ypanstep, fix_info.ywrapstep, fix_info.accel);
678 /*****************************************************************************
679 * CloseDisplay: terminate FB video thread output method
680 *****************************************************************************/
681 static void CloseDisplay(vout_display_t *vd)
683 vout_display_sys_t *sys = vd->sys;
685 if (sys->video_ptr != MAP_FAILED) {
687 munmap(sys->video_ptr, sys->video_size);
691 /* Restore palette */
692 if (sys->var_info.bits_per_pixel == 8) {
693 ioctl(sys->fd, FBIOPUTCMAP, &sys->fb_cmap);
698 /* Restore fb config */
699 ioctl(sys->fd, FBIOPUT_VSCREENINFO, &sys->old_info);
706 /*****************************************************************************
707 * SwitchDisplay: VT change signal handler
708 *****************************************************************************
709 * This function activates or deactivates the output of the thread. It is
710 * called by the VT driver, on terminal change.
711 *****************************************************************************/
712 static void SwitchDisplay(int i_signal)
714 VLC_UNUSED(i_signal);
718 vlc_mutex_lock(&p_vout_bank->lock);
720 /* XXX: only test the first video output */
721 if (p_vout_bank->i_count)
723 vd = p_vout_bank->pp_vout[0];
727 case SIGUSR1: /* vt has been released */
729 ioctl(sys->tty, VT_RELDISP, 1);
731 case SIGUSR2: /* vt has been acquired */
733 ioctl(sys->tty, VT_RELDISP, VT_ACTIVATE);
734 /* handle blanking */
735 vlc_mutex_lock(&vd->change_lock);
736 vd->i_changes |= VOUT_SIZE_CHANGE;
737 vlc_mutex_unlock(&vd->change_lock);
742 vlc_mutex_unlock(&p_vout_bank->lock);
746 /*****************************************************************************
747 * TextMode and GfxMode : switch tty to text/graphic mode
748 *****************************************************************************
749 * These functions toggle the tty mode.
750 *****************************************************************************/
751 static void TextMode(int tty)
753 /* return to text mode */
754 if (-1 == ioctl(tty, KDSETMODE, KD_TEXT)) {
755 /*msg_Err(vd, "failed ioctl KDSETMODE KD_TEXT");*/
759 static void GfxMode(int tty)
761 /* switch to graphic mode */
762 if (-1 == ioctl(tty, KDSETMODE, KD_GRAPHICS)) {
763 /*msg_Err(vd, "failed ioctl KDSETMODE KD_GRAPHICS");*/