cocoa_common: fix problems with alt-tab window changes
[mplayer.git] / libvo / vo_fbdev.c
blob6d70cca0fe3e05cb38cea818d96838934e199c20
1 /*
2 * video driver for framebuffer device
3 * copyright (C) 2001 Szabolcs Berecz <szabi@inf.elte.hu>
5 * Some idea and code borrowed from Chris Lawrence's ppmtofb-0.27
6 * Some fixes and small improvements by Joey Parrish <joey@nicewarrior.org>
8 * This file is part of MPlayer.
10 * MPlayer 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 * MPlayer 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 along
21 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <fcntl.h>
29 #include <unistd.h>
30 #include <errno.h>
31 #include <ctype.h>
33 #include <sys/mman.h>
34 #include <sys/ioctl.h>
35 #include <sys/kd.h>
36 #include <linux/fb.h>
37 #include <libavutil/common.h>
39 #include "config.h"
40 #include "video_out.h"
41 #include "video_out_internal.h"
42 #include "fastmemcpy.h"
43 #include "sub/sub.h"
44 #include "geometry.h"
45 #include "aspect.h"
46 #include "mp_msg.h"
48 static const vo_info_t info = {
49 "Framebuffer Device",
50 "fbdev",
51 "Szabolcs Berecz <szabi@inf.elte.hu>",
55 LIBVO_EXTERN(fbdev)
57 static signed int pre_init_err = -2;
58 /******************************
59 * fb.modes support *
60 ******************************/
62 static range_t *monitor_hfreq = NULL;
63 static range_t *monitor_vfreq = NULL;
64 static range_t *monitor_dotclock = NULL;
66 typedef struct {
67 char *name;
68 uint32_t xres, yres, vxres, vyres, depth;
69 uint32_t pixclock, left, right, upper, lower, hslen, vslen;
70 uint32_t sync;
71 uint32_t vmode;
72 } fb_mode_t;
74 #define MAX_NR_TOKEN 16
76 #define MAX_LINE_LEN 1000
78 #define RET_EOF -1
79 #define RET_EOL -2
81 static int validate_mode(fb_mode_t *m)
83 if (!m->xres) {
84 mp_msg(MSGT_VO, MSGL_V, "needs geometry ");
85 return 0;
87 if (!m->pixclock) {
88 mp_msg(MSGT_VO, MSGL_V, "needs timings ");
89 return 0;
91 return 1;
94 static FILE *fp;
95 static int line_num = 0;
96 static char *line;
97 static char *token[MAX_NR_TOKEN];
99 static int get_token(int num)
101 static int read_nextline = 1;
102 static int line_pos;
103 int i;
104 char c;
106 if (num >= MAX_NR_TOKEN) {
107 mp_msg(MSGT_VO, MSGL_V, "get_token(): max >= MAX_NR_TOKEN!\n");
108 goto out_eof;
111 if (read_nextline) {
112 if (!fgets(line, MAX_LINE_LEN, fp))
113 goto out_eof;
114 line_pos = 0;
115 ++line_num;
116 read_nextline = 0;
118 for (i = 0; i < num; i++) {
119 while (isspace(line[line_pos]))
120 ++line_pos;
121 if (line[line_pos] == '\0' || line[line_pos] == '#') {
122 read_nextline = 1;
123 goto out_eol;
125 token[i] = line + line_pos;
126 c = line[line_pos];
127 if (c == '"' || c == '\'') {
128 token[i]++;
129 while (line[++line_pos] != c && line[line_pos])
130 /* NOTHING */;
131 if (!line[line_pos])
132 goto out_eol;
133 line[line_pos] = ' ';
134 } else {
135 for (/* NOTHING */; !isspace(line[line_pos]) &&
136 line[line_pos]; line_pos++)
137 /* NOTHING */;
139 if (!line[line_pos]) {
140 read_nextline = 1;
141 if (i == num - 1)
142 goto out_ok;
143 goto out_eol;
145 line[line_pos++] = '\0';
147 out_ok:
148 return i;
149 out_eof:
150 return RET_EOF;
151 out_eol:
152 return RET_EOL;
155 static fb_mode_t *fb_modes = NULL;
156 static int nr_modes = 0;
158 static int parse_fbmode_cfg(char *cfgfile)
160 #define CHECK_IN_MODE_DEF\
161 if (!in_mode_def) {\
162 mp_msg(MSGT_VO, MSGL_V, "'needs 'mode' first");\
163 goto err_out_print_linenum;\
165 fb_mode_t *mode = NULL;
166 char *endptr; // strtoul()...
167 int in_mode_def = 0;
168 int tmp, i;
170 /* If called more than once, reuse parsed data */
171 if (nr_modes)
172 return nr_modes;
174 mp_msg(MSGT_VO, MSGL_V, "Reading %s: ", cfgfile);
176 if ((fp = fopen(cfgfile, "r")) == NULL) {
177 mp_msg(MSGT_VO, MSGL_V, "can't open '%s': %s\n", cfgfile, strerror(errno));
178 return -1;
181 if ((line = malloc(MAX_LINE_LEN + 1)) == NULL) {
182 mp_msg(MSGT_VO, MSGL_V, "can't get memory for 'line': %s\n", strerror(errno));
183 return -2;
187 * check if the cfgfile starts with 'mode'
189 while ((tmp = get_token(1)) == RET_EOL)
190 /* NOTHING */;
191 if (tmp == RET_EOF)
192 goto out;
193 if (!strcmp(token[0], "mode"))
194 goto loop_enter;
195 goto err_out_parse_error;
197 while ((tmp = get_token(1)) != RET_EOF) {
198 if (tmp == RET_EOL)
199 continue;
200 if (!strcmp(token[0], "mode")) {
201 if (in_mode_def) {
202 mp_msg(MSGT_VO, MSGL_V, "'endmode' required");
203 goto err_out_print_linenum;
205 if (!validate_mode(mode))
206 goto err_out_not_valid;
207 loop_enter:
208 if (!(fb_modes =
209 realloc(fb_modes, sizeof(fb_mode_t) * (nr_modes + 1)))) {
210 mp_msg(MSGT_VO, MSGL_V, "can't realloc 'fb_modes' (nr_modes = %d):"
211 " %s\n", nr_modes, strerror(errno));
212 goto err_out;
214 mode = fb_modes + nr_modes;
215 ++nr_modes;
216 memset(mode, 0, sizeof(fb_mode_t));
218 if (get_token(1) < 0)
219 goto err_out_parse_error;
220 for (i = 0; i < nr_modes - 1; i++) {
221 if (!strcmp(token[0], fb_modes[i].name)) {
222 mp_msg(MSGT_VO, MSGL_V, "mode name '%s' isn't unique", token[0]);
223 goto err_out_print_linenum;
226 if (!(mode->name = strdup(token[0]))) {
227 mp_msg(MSGT_VO, MSGL_V, "can't strdup -> 'name': %s\n", strerror(errno));
228 goto err_out;
230 in_mode_def = 1;
231 } else if (!strcmp(token[0], "geometry")) {
232 CHECK_IN_MODE_DEF;
233 if (get_token(5) < 0)
234 goto err_out_parse_error;
235 mode->xres = strtoul(token[0], &endptr, 0);
236 if (*endptr)
237 goto err_out_parse_error;
238 mode->yres = strtoul(token[1], &endptr, 0);
239 if (*endptr)
240 goto err_out_parse_error;
241 mode->vxres = strtoul(token[2], &endptr, 0);
242 if (*endptr)
243 goto err_out_parse_error;
244 mode->vyres = strtoul(token[3], &endptr, 0);
245 if (*endptr)
246 goto err_out_parse_error;
247 mode->depth = strtoul(token[4], &endptr, 0);
248 if (*endptr)
249 goto err_out_parse_error;
250 } else if (!strcmp(token[0], "timings")) {
251 CHECK_IN_MODE_DEF;
252 if (get_token(7) < 0)
253 goto err_out_parse_error;
254 mode->pixclock = strtoul(token[0], &endptr, 0);
255 if (*endptr)
256 goto err_out_parse_error;
257 mode->left = strtoul(token[1], &endptr, 0);
258 if (*endptr)
259 goto err_out_parse_error;
260 mode->right = strtoul(token[2], &endptr, 0);
261 if (*endptr)
262 goto err_out_parse_error;
263 mode->upper = strtoul(token[3], &endptr, 0);
264 if (*endptr)
265 goto err_out_parse_error;
266 mode->lower = strtoul(token[4], &endptr, 0);
267 if (*endptr)
268 goto err_out_parse_error;
269 mode->hslen = strtoul(token[5], &endptr, 0);
270 if (*endptr)
271 goto err_out_parse_error;
272 mode->vslen = strtoul(token[6], &endptr, 0);
273 if (*endptr)
274 goto err_out_parse_error;
275 } else if (!strcmp(token[0], "endmode")) {
276 CHECK_IN_MODE_DEF;
277 in_mode_def = 0;
278 } else if (!strcmp(token[0], "accel")) {
279 CHECK_IN_MODE_DEF;
280 if (get_token(1) < 0)
281 goto err_out_parse_error;
283 * it's only used for text acceleration
284 * so we just ignore it.
286 } else if (!strcmp(token[0], "hsync")) {
287 CHECK_IN_MODE_DEF;
288 if (get_token(1) < 0)
289 goto err_out_parse_error;
290 if (!strcmp(token[0], "low"))
291 mode->sync &= ~FB_SYNC_HOR_HIGH_ACT;
292 else if (!strcmp(token[0], "high"))
293 mode->sync |= FB_SYNC_HOR_HIGH_ACT;
294 else
295 goto err_out_parse_error;
296 } else if (!strcmp(token[0], "vsync")) {
297 CHECK_IN_MODE_DEF;
298 if (get_token(1) < 0)
299 goto err_out_parse_error;
300 if (!strcmp(token[0], "low"))
301 mode->sync &= ~FB_SYNC_VERT_HIGH_ACT;
302 else if (!strcmp(token[0], "high"))
303 mode->sync |= FB_SYNC_VERT_HIGH_ACT;
304 else
305 goto err_out_parse_error;
306 } else if (!strcmp(token[0], "csync")) {
307 CHECK_IN_MODE_DEF;
308 if (get_token(1) < 0)
309 goto err_out_parse_error;
310 if (!strcmp(token[0], "low"))
311 mode->sync &= ~FB_SYNC_COMP_HIGH_ACT;
312 else if (!strcmp(token[0], "high"))
313 mode->sync |= FB_SYNC_COMP_HIGH_ACT;
314 else
315 goto err_out_parse_error;
316 } else if (!strcmp(token[0], "extsync")) {
317 CHECK_IN_MODE_DEF;
318 if (get_token(1) < 0)
319 goto err_out_parse_error;
320 if (!strcmp(token[0], "false"))
321 mode->sync &= ~FB_SYNC_EXT;
322 else if (!strcmp(token[0], "true"))
323 mode->sync |= FB_SYNC_EXT;
324 else
325 goto err_out_parse_error;
326 } else if (!strcmp(token[0], "laced")) {
327 CHECK_IN_MODE_DEF;
328 if (get_token(1) < 0)
329 goto err_out_parse_error;
330 if (!strcmp(token[0], "false"))
331 mode->vmode = FB_VMODE_NONINTERLACED;
332 else if (!strcmp(token[0], "true"))
333 mode->vmode = FB_VMODE_INTERLACED;
334 else
335 goto err_out_parse_error;
336 } else if (!strcmp(token[0], "double")) {
337 CHECK_IN_MODE_DEF;
338 if (get_token(1) < 0)
339 goto err_out_parse_error;
340 if (!strcmp(token[0], "false"))
342 else if (!strcmp(token[0], "true"))
343 mode->vmode = FB_VMODE_DOUBLE;
344 else
345 goto err_out_parse_error;
346 } else
347 goto err_out_parse_error;
349 if (!validate_mode(mode))
350 goto err_out_not_valid;
351 out:
352 mp_msg(MSGT_VO, MSGL_V, "%d modes\n", nr_modes);
353 free(line);
354 fclose(fp);
355 return nr_modes;
356 err_out_parse_error:
357 mp_msg(MSGT_VO, MSGL_V, "parse error");
358 err_out_print_linenum:
359 mp_msg(MSGT_VO, MSGL_V, " at line %d\n", line_num);
360 err_out:
361 free(fb_modes);
362 fb_modes = NULL;
363 nr_modes = 0;
364 free(line);
365 free(fp);
366 return -2;
367 err_out_not_valid:
368 mp_msg(MSGT_VO, MSGL_V, "previous mode is not correct");
369 goto err_out_print_linenum;
372 static fb_mode_t *find_mode_by_name(char *name)
374 int i;
376 for (i = 0; i < nr_modes; i++)
377 if (!strcmp(name, fb_modes[i].name))
378 return fb_modes + i;
379 return NULL;
382 static float dcf(fb_mode_t *m) //driving clock frequency
384 return 1e12f / m->pixclock;
387 static float hsf(fb_mode_t *m) //horizontal scan frequency
389 int htotal = m->left + m->xres + m->right + m->hslen;
390 return dcf(m) / htotal;
393 static float vsf(fb_mode_t *m) //vertical scan frequency
395 int vtotal = m->upper + m->yres + m->lower + m->vslen;
396 return hsf(m) / vtotal;
400 static int mode_works(fb_mode_t *m, range_t *hfreq, range_t *vfreq,
401 range_t *dotclock)
403 float h = hsf(m);
404 float v = vsf(m);
405 float d = dcf(m);
406 int ret = 1;
408 mp_msg(MSGT_VO, MSGL_DBG2, "mode %dx%d:", m->xres, m->yres);
409 if (!in_range(hfreq, h)) {
410 ret = 0;
411 mp_msg(MSGT_VO, MSGL_DBG2, " hsync out of range.");
413 if (!in_range(vfreq, v)) {
414 ret = 0;
415 mp_msg(MSGT_VO, MSGL_DBG2, " vsync out of range.");
417 if (!in_range(dotclock, d)) {
418 ret = 0;
419 mp_msg(MSGT_VO, MSGL_DBG2, " dotclock out of range.");
421 if (ret)
422 mp_msg(MSGT_VO, MSGL_DBG2, " hsync, vsync, dotclock ok.\n");
423 else
424 mp_msg(MSGT_VO, MSGL_DBG2, "\n");
426 return ret;
429 static fb_mode_t *find_best_mode(int xres, int yres, range_t *hfreq,
430 range_t *vfreq, range_t *dotclock)
432 int i;
433 fb_mode_t *best = fb_modes;
434 fb_mode_t *curr;
436 mp_msg(MSGT_VO, MSGL_DBG2, "Searching for first working mode\n");
438 for (i = 0; i < nr_modes; i++, best++)
439 if (mode_works(best, hfreq, vfreq, dotclock))
440 break;
442 if (i == nr_modes)
443 return NULL;
444 if (i == nr_modes - 1)
445 return best;
447 mp_msg(MSGT_VO, MSGL_DBG2, "First working mode: %dx%d\n", best->xres, best->yres);
448 mp_msg(MSGT_VO, MSGL_DBG2, "Searching for better modes\n");
450 for (curr = best + 1; i < nr_modes - 1; i++, curr++) {
451 if (!mode_works(curr, hfreq, vfreq, dotclock))
452 continue;
454 if (best->xres < xres || best->yres < yres) {
455 if (curr->xres > best->xres || curr->yres > best->yres) {
456 mp_msg(MSGT_VO, MSGL_DBG2, "better than %dx%d, which is too small.\n",
457 best->xres, best->yres);
458 best = curr;
459 } else
460 mp_msg(MSGT_VO, MSGL_DBG2, "too small.\n");
461 } else if (curr->xres == best->xres && curr->yres == best->yres &&
462 vsf(curr) > vsf(best)) {
463 mp_msg(MSGT_VO, MSGL_DBG2, "faster screen refresh.\n");
464 best = curr;
465 } else if ((curr->xres <= best->xres && curr->yres <= best->yres) &&
466 (curr->xres >= xres && curr->yres >= yres)) {
467 mp_msg(MSGT_VO, MSGL_DBG2, "better than %dx%d, which is too large.\n",
468 best->xres, best->yres);
469 best = curr;
470 } else {
471 if (curr->xres < xres || curr->yres < yres)
472 mp_msg(MSGT_VO, MSGL_DBG2, "too small.\n");
473 else if (curr->xres > best->xres || curr->yres > best->yres)
474 mp_msg(MSGT_VO, MSGL_DBG2, "too large.\n");
475 else
476 mp_msg(MSGT_VO, MSGL_DBG2, "it's worse, don't know why.\n");
480 return best;
483 static void set_bpp(struct fb_var_screeninfo *p, int bpp, int rgb)
485 p->bits_per_pixel = FFALIGN(bpp, 2);
486 p->red.msb_right = p->green.msb_right = p->blue.msb_right = p->transp.msb_right = 0;
487 p->transp.offset = p->transp.length = 0;
488 p->blue.offset = 0;
489 switch (bpp) {
490 case 32:
491 p->transp.offset = 24;
492 p->transp.length = 8;
493 case 24:
494 p->red.offset = 16;
495 p->red.length = 8;
496 p->green.offset = 8;
497 p->green.length = 8;
498 p->blue.length = 8;
499 break;
500 case 16:
501 p->red.offset = 11;
502 p->green.length = 6;
503 p->red.length = 5;
504 p->green.offset = 5;
505 p->blue.length = 5;
506 break;
507 case 15:
508 p->red.offset = 10;
509 p->green.length = 5;
510 p->red.length = 5;
511 p->green.offset = 5;
512 p->blue.length = 5;
513 break;
514 case 12:
515 p->red.offset = 8;
516 p->green.length = 4;
517 p->red.length = 4;
518 p->green.offset = 4;
519 p->blue.length = 4;
520 break;
522 if (rgb) {
523 p->blue.offset = p->red.offset;
524 p->red.offset = 0;
528 static void fb_mode2fb_vinfo(fb_mode_t *m, struct fb_var_screeninfo *v, int rgb)
530 v->xres = m->xres;
531 v->yres = m->yres;
532 v->xres_virtual = m->vxres;
533 v->yres_virtual = m->vyres;
534 set_bpp(v, m->depth, rgb);
535 v->pixclock = m->pixclock;
536 v->left_margin = m->left;
537 v->right_margin = m->right;
538 v->upper_margin = m->upper;
539 v->lower_margin = m->lower;
540 v->hsync_len = m->hslen;
541 v->vsync_len = m->vslen;
542 v->sync = m->sync;
543 v->vmode = m->vmode;
547 /******************************
548 * vo_fbdev *
549 ******************************/
551 /* command line/config file options */
552 static char *fb_dev_name = NULL;
553 char *fb_mode_cfgfile = NULL;
554 char *fb_mode_name = NULL;
556 static fb_mode_t *fb_mode = NULL;
558 /* vo_fbdev related variables */
559 static int fb_dev_fd;
560 static int fb_tty_fd = -1;
561 static size_t fb_size;
562 static uint8_t *frame_buffer;
563 static uint8_t *center;
564 static struct fb_fix_screeninfo fb_finfo;
565 static struct fb_var_screeninfo fb_orig_vinfo;
566 static struct fb_var_screeninfo fb_vinfo;
567 static unsigned short fb_ored[256], fb_ogreen[256], fb_oblue[256];
568 static struct fb_cmap fb_oldcmap = { 0, 256, fb_ored, fb_ogreen, fb_oblue };
569 static int fb_cmap_changed = 0;
570 static int fb_rgb;
571 static int fb_pixel_size; // 32: 4 24: 3 16: 2 15: 2
572 static int fb_bpp; // 32: 32 24: 24 16: 16 15: 15
573 static int fb_bpp_we_want; // 32: 32 24: 24 16: 16 15: 15
574 static int fb_line_len;
575 static int fb_xres;
576 static int fb_yres;
577 static int fb_page;
578 static void (*draw_alpha_p)(int w, int h, unsigned char *src,
579 unsigned char *srca, int stride,
580 unsigned char *dst, int dstride);
582 static int in_width;
583 static int in_height;
584 static int out_width;
585 static int out_height;
586 static int first_row;
587 static int last_row;
588 static uint32_t pixel_format;
589 static int fs;
592 * Note: this function is completely cut'n'pasted from
593 * Chris Lawrence's code.
594 * (modified a bit to fit in my code...)
596 static struct fb_cmap *make_directcolor_cmap(struct fb_var_screeninfo *var)
598 /* Hopefully any DIRECTCOLOR device will have a big enough palette
599 * to handle mapping the full color depth.
600 * e.g. 8 bpp -> 256 entry palette
602 * We could handle some sort of gamma here
604 int i, cols, rcols, gcols, bcols;
605 uint16_t *red, *green, *blue;
606 struct fb_cmap *cmap;
608 rcols = 1 << var->red.length;
609 gcols = 1 << var->green.length;
610 bcols = 1 << var->blue.length;
612 /* Make our palette the length of the deepest color */
613 cols = FFMAX3(rcols, gcols, bcols);
615 red = malloc(cols * sizeof(red[0]));
616 if (!red) {
617 mp_msg(MSGT_VO, MSGL_V, "Can't allocate red palette with %d entries.\n", cols);
618 return NULL;
620 for (i = 0; i < rcols; i++)
621 red[i] = (65535 / (rcols - 1)) * i;
623 green = malloc(cols * sizeof(green[0]));
624 if (!green) {
625 mp_msg(MSGT_VO, MSGL_V, "Can't allocate green palette with %d entries.\n", cols);
626 free(red);
627 return NULL;
629 for (i = 0; i < gcols; i++)
630 green[i] = (65535 / (gcols - 1)) * i;
632 blue = malloc(cols * sizeof(blue[0]));
633 if (!blue) {
634 mp_msg(MSGT_VO, MSGL_V, "Can't allocate blue palette with %d entries.\n", cols);
635 free(red);
636 free(green);
637 return NULL;
639 for (i = 0; i < bcols; i++)
640 blue[i] = (65535 / (bcols - 1)) * i;
642 cmap = malloc(sizeof(struct fb_cmap));
643 if (!cmap) {
644 mp_msg(MSGT_VO, MSGL_V, "Can't allocate color map\n");
645 free(red);
646 free(green);
647 free(blue);
648 return NULL;
650 cmap->start = 0;
651 cmap->transp = 0;
652 cmap->len = cols;
653 cmap->red = red;
654 cmap->blue = blue;
655 cmap->green = green;
656 cmap->transp = NULL;
658 return cmap;
662 static int fb_preinit(int reset)
664 static int fb_preinit_done = 0;
665 static int fb_works = 0;
667 if (reset) {
668 fb_preinit_done = 0;
669 return 0;
672 if (fb_preinit_done)
673 return fb_works;
675 fb_dev_fd = fb_tty_fd = -1;
677 if (!fb_dev_name && !(fb_dev_name = getenv("FRAMEBUFFER")))
678 fb_dev_name = strdup("/dev/fb0");
679 mp_msg(MSGT_VO, MSGL_V, "using %s\n", fb_dev_name);
681 if ((fb_dev_fd = open(fb_dev_name, O_RDWR)) == -1) {
682 mp_msg(MSGT_VO, MSGL_ERR, "Can't open %s: %s\n", fb_dev_name, strerror(errno));
683 goto err_out;
685 if (ioctl(fb_dev_fd, FBIOGET_VSCREENINFO, &fb_vinfo)) {
686 mp_msg(MSGT_VO, MSGL_ERR, "Can't get VSCREENINFO: %s\n", strerror(errno));
687 goto err_out;
689 fb_orig_vinfo = fb_vinfo;
691 if ((fb_tty_fd = open("/dev/tty", O_RDWR)) < 0) {
692 mp_msg(MSGT_VO, MSGL_ERR, "notice: Can't open /dev/tty: %s\n", strerror(errno));
695 fb_rgb = !fb_vinfo.red.offset;
696 fb_bpp = fb_vinfo.bits_per_pixel;
697 if (fb_bpp == 16)
698 fb_bpp = fb_vinfo.red.length + fb_vinfo.green.length + fb_vinfo.blue.length;
700 if (fb_bpp == 8 && !vo_dbpp) {
701 mp_msg(MSGT_VO, MSGL_ERR, "8 bpp output is not supported.\n");
702 goto err_out;
705 if (vo_dbpp) {
706 if (vo_dbpp != 12 && vo_dbpp != 15 && vo_dbpp != 16
707 && vo_dbpp != 24 && vo_dbpp != 32) {
708 mp_msg(MSGT_VO, MSGL_ERR, "can't switch to %d bpp\n", vo_dbpp);
709 goto err_out;
711 fb_bpp = vo_dbpp;
714 if (!fb_mode_cfgfile)
715 fb_mode_cfgfile = strdup("/etc/fb.modes");
717 fb_preinit_done = 1;
718 fb_works = 1;
719 return 1;
720 err_out:
721 if (fb_tty_fd != -1)
722 close(fb_tty_fd);
723 fb_tty_fd = -1;
724 if (fb_dev_fd != -1)
725 close(fb_dev_fd);
726 fb_dev_fd = -1;
727 fb_preinit_done = 1;
728 fb_works = 0;
729 return 0;
732 static void vt_set_textarea(int u, int l)
734 /* how can I determine the font height?
735 * just use 16 for now
737 int urow = ((u + 15) / 16) + 1;
738 int lrow = l / 16;
740 mp_msg(MSGT_VO, MSGL_DBG2, "vt_set_textarea(%d,%d): %d,%d\n", u, l, urow, lrow);
741 if (fb_tty_fd >= 0) {
742 char modestring[100];
743 snprintf(modestring, sizeof(modestring), "\33[%d;%dr\33[%d;%dH", urow, lrow, lrow, 0);
744 write(fb_tty_fd, modestring, strlen(modestring));
745 fsync(fb_tty_fd);
749 static int config(uint32_t width, uint32_t height, uint32_t d_width,
750 uint32_t d_height, uint32_t flags, char *title,
751 uint32_t format)
753 struct fb_cmap *cmap;
754 int vm = flags & VOFLAG_MODESWITCHING;
756 fs = flags & VOFLAG_FULLSCREEN;
758 if (pre_init_err == -2) {
759 mp_msg(MSGT_VO, MSGL_ERR, "Internal fatal error: config() was called before preinit()\n");
760 return -1;
763 if (pre_init_err)
764 return 1;
766 if (fb_mode_name && !vm) {
767 mp_msg(MSGT_VO, MSGL_ERR, "-fbmode can only be used with -vm\n");
768 return 1;
770 if (vm && parse_fbmode_cfg(fb_mode_cfgfile) < 0)
771 return 1;
772 if (d_width && (fs || vm)) {
773 out_width = d_width;
774 out_height = d_height;
775 } else {
776 out_width = width;
777 out_height = height;
779 in_width = width;
780 in_height = height;
781 pixel_format = format;
783 if (fb_mode_name) {
784 if (!(fb_mode = find_mode_by_name(fb_mode_name))) {
785 mp_msg(MSGT_VO, MSGL_ERR, "can't find requested video mode\n");
786 return 1;
788 fb_mode2fb_vinfo(fb_mode, &fb_vinfo, fb_rgb);
789 } else if (vm) {
790 monitor_hfreq = str2range(monitor_hfreq_str);
791 monitor_vfreq = str2range(monitor_vfreq_str);
792 monitor_dotclock = str2range(monitor_dotclock_str);
793 if (!monitor_hfreq || !monitor_vfreq || !monitor_dotclock) {
794 mp_msg(MSGT_VO, MSGL_ERR, "you have to specify the capabilities of"
795 " the monitor.\n");
796 return 1;
798 if (!(fb_mode = find_best_mode(out_width, out_height, monitor_hfreq,
799 monitor_vfreq, monitor_dotclock))) {
800 mp_msg(MSGT_VO, MSGL_ERR, "can't find best video mode\n");
801 return 1;
803 mp_msg(MSGT_VO, MSGL_V, "using mode %dx%d @ %.1fHz\n", fb_mode->xres,
804 fb_mode->yres, vsf(fb_mode));
805 fb_mode2fb_vinfo(fb_mode, &fb_vinfo, fb_rgb);
807 fb_bpp_we_want = fb_bpp;
808 set_bpp(&fb_vinfo, fb_bpp, fb_rgb);
809 fb_vinfo.xres_virtual = fb_vinfo.xres;
810 fb_vinfo.yres_virtual = fb_vinfo.yres;
811 fb_page = 0;
812 if (vo_doublebuffering) {
813 fb_vinfo.yres_virtual <<= 1;
814 fb_vinfo.yoffset = 0;
815 fb_page = 1; // start writing into the page we don't display
818 if (fb_tty_fd >= 0 && ioctl(fb_tty_fd, KDSETMODE, KD_GRAPHICS) < 0) {
819 mp_msg(MSGT_VO, MSGL_V, "Can't set graphics mode: %s\n", strerror(errno));
820 close(fb_tty_fd);
821 fb_tty_fd = -1;
824 if (ioctl(fb_dev_fd, FBIOPUT_VSCREENINFO, &fb_vinfo))
825 // Intel drivers fail if we request a transparency channel
826 fb_vinfo.transp.length = fb_vinfo.transp.offset = 0;
827 if (ioctl(fb_dev_fd, FBIOPUT_VSCREENINFO, &fb_vinfo)) {
828 mp_msg(MSGT_VO, MSGL_ERR, "Can't put VSCREENINFO: %s\n", strerror(errno));
829 if (fb_tty_fd >= 0 && ioctl(fb_tty_fd, KDSETMODE, KD_TEXT) < 0) {
830 mp_msg(MSGT_VO, MSGL_ERR, "Can't restore text mode: %s\n", strerror(errno));
832 return 1;
835 fb_pixel_size = fb_vinfo.bits_per_pixel / 8;
836 fb_bpp = fb_vinfo.bits_per_pixel;
837 if (fb_bpp == 16)
838 fb_bpp = fb_vinfo.red.length + fb_vinfo.green.length + fb_vinfo.blue.length;
839 if (fb_bpp_we_want != fb_bpp)
840 mp_msg(MSGT_VO, MSGL_WARN, "requested %d bpp, got %d bpp!!!\n",
841 fb_bpp_we_want, fb_bpp);
843 switch (fb_bpp) {
844 case 32:
845 draw_alpha_p = vo_draw_alpha_rgb32;
846 break;
847 case 24:
848 draw_alpha_p = vo_draw_alpha_rgb24;
849 break;
850 case 16:
851 draw_alpha_p = vo_draw_alpha_rgb16;
852 break;
853 case 15:
854 draw_alpha_p = vo_draw_alpha_rgb15;
855 break;
856 case 12:
857 draw_alpha_p = vo_draw_alpha_rgb12;
858 break;
859 default:
860 return 1;
863 fb_xres = fb_vinfo.xres;
864 fb_yres = fb_vinfo.yres;
866 if (vm || fs) {
867 out_width = fb_xres;
868 out_height = fb_yres;
870 if (out_width < in_width || out_height < in_height) {
871 mp_msg(MSGT_VO, MSGL_ERR, "screensize is smaller than video size\n");
872 return 1;
875 first_row = (out_height - in_height) / 2;
876 last_row = (out_height + in_height) / 2;
878 if (ioctl(fb_dev_fd, FBIOGET_FSCREENINFO, &fb_finfo)) {
879 mp_msg(MSGT_VO, MSGL_ERR, "Can't get FSCREENINFO: %s\n", strerror(errno));
880 return 1;
883 if (fb_finfo.type != FB_TYPE_PACKED_PIXELS) {
884 mp_msg(MSGT_VO, MSGL_ERR, "type %d not supported\n", fb_finfo.type);
885 return 1;
888 switch (fb_finfo.visual) {
889 case FB_VISUAL_TRUECOLOR:
890 break;
891 case FB_VISUAL_DIRECTCOLOR:
892 mp_msg(MSGT_VO, MSGL_V, "creating cmap for directcolor\n");
893 if (ioctl(fb_dev_fd, FBIOGETCMAP, &fb_oldcmap)) {
894 mp_msg(MSGT_VO, MSGL_ERR, "can't get cmap: %s\n",
895 strerror(errno));
896 return 1;
898 if (!(cmap = make_directcolor_cmap(&fb_vinfo)))
899 return 1;
900 if (ioctl(fb_dev_fd, FBIOPUTCMAP, cmap)) {
901 mp_msg(MSGT_VO, MSGL_ERR, "can't put cmap: %s\n",
902 strerror(errno));
903 return 1;
905 fb_cmap_changed = 1;
906 free(cmap->red);
907 free(cmap->green);
908 free(cmap->blue);
909 free(cmap);
910 break;
911 default:
912 mp_msg(MSGT_VO, MSGL_ERR, "visual: %d not yet supported\n",
913 fb_finfo.visual);
914 return 1;
917 fb_line_len = fb_finfo.line_length;
918 fb_size = fb_finfo.smem_len;
919 if (vo_doublebuffering && fb_size < 2 * fb_yres * fb_line_len)
921 mp_msg(MSGT_VO, MSGL_WARN, "framebuffer too small for double-buffering, disabling\n");
922 vo_doublebuffering = 0;
923 fb_page = 0;
927 int x_offset = 0, y_offset = 0;
928 geometry(&x_offset, &y_offset, &out_width, &out_height, fb_xres, fb_yres);
930 frame_buffer = mmap(0, fb_size, PROT_READ | PROT_WRITE,
931 MAP_SHARED, fb_dev_fd, 0);
932 if (frame_buffer == (uint8_t *) -1) {
933 mp_msg(MSGT_VO, MSGL_ERR, "Can't mmap %s: %s\n", fb_dev_name, strerror(errno));
934 return 1;
937 center = frame_buffer +
938 ( (out_width - in_width) / 2 ) * fb_pixel_size +
939 ( (out_height - in_height) / 2 ) * fb_line_len +
940 x_offset * fb_pixel_size + y_offset * fb_line_len +
941 fb_page * fb_yres * fb_line_len;
943 mp_msg(MSGT_VO, MSGL_DBG2, "frame_buffer @ %p\n", frame_buffer);
944 mp_msg(MSGT_VO, MSGL_DBG2, "center @ %p\n", center);
945 mp_msg(MSGT_VO, MSGL_V, "pixel per line: %d\n", fb_line_len / fb_pixel_size);
947 if (fs || vm) {
948 int clear_size = fb_line_len * fb_yres;
949 if (vo_doublebuffering)
950 clear_size <<= 1;
951 memset(frame_buffer, 0, clear_size);
955 vt_set_textarea(last_row, fb_yres);
957 return 0;
960 static int query_format(uint32_t format)
962 if (!fb_preinit(0))
963 return 0;
964 if ((format & IMGFMT_BGR_MASK) == (fb_rgb ? IMGFMT_RGB : IMGFMT_BGR)) {
965 int bpp = format & 0xff;
967 if (bpp == fb_bpp)
968 return VFCAP_ACCEPT_STRIDE | VFCAP_CSP_SUPPORTED | VFCAP_CSP_SUPPORTED_BY_HW;
970 return 0;
973 static void draw_alpha(int x0, int y0, int w, int h, unsigned char *src,
974 unsigned char *srca, int stride)
976 unsigned char *dst;
978 dst = center + fb_line_len * y0 + fb_pixel_size * x0;
980 (*draw_alpha_p)(w, h, src, srca, stride, dst, fb_line_len);
983 static int draw_frame(uint8_t *src[])
985 return 1;
988 static int draw_slice(uint8_t *src[], int stride[], int w, int h, int x, int y)
990 uint8_t *d;
992 d = center + fb_line_len * y + fb_pixel_size * x;
994 memcpy_pic2(d, src[0], w * fb_pixel_size, h, fb_line_len, stride[0], 1);
996 return 0;
999 static void check_events(void)
1003 static void flip_page(void)
1005 int next_page = !fb_page;
1006 int page_delta = next_page - fb_page;
1007 if (!vo_doublebuffering)
1008 return;
1010 fb_vinfo.yoffset = fb_page * fb_yres;
1011 ioctl(fb_dev_fd, FBIOPAN_DISPLAY, &fb_vinfo);
1013 center += page_delta * fb_yres * fb_line_len;
1014 fb_page = next_page;
1017 static void draw_osd(void)
1019 vo_draw_text(in_width, in_height, draw_alpha);
1022 static void uninit(void)
1024 if (fb_cmap_changed) {
1025 if (ioctl(fb_dev_fd, FBIOPUTCMAP, &fb_oldcmap))
1026 mp_msg(MSGT_VO, MSGL_WARN, "Can't restore original cmap\n");
1027 fb_cmap_changed = 0;
1029 if (ioctl(fb_dev_fd, FBIOGET_VSCREENINFO, &fb_vinfo))
1030 mp_msg(MSGT_VO, MSGL_WARN, "ioctl FBIOGET_VSCREENINFO: %s\n", strerror(errno));
1031 fb_orig_vinfo.xoffset = fb_vinfo.xoffset;
1032 fb_orig_vinfo.yoffset = fb_vinfo.yoffset;
1033 if (ioctl(fb_dev_fd, FBIOPUT_VSCREENINFO, &fb_orig_vinfo))
1034 mp_msg(MSGT_VO, MSGL_WARN, "Can't reset original fb_var_screeninfo: %s\n", strerror(errno));
1035 if (fb_tty_fd >= 0) {
1036 if (ioctl(fb_tty_fd, KDSETMODE, KD_TEXT) < 0)
1037 mp_msg(MSGT_VO, MSGL_WARN, "Can't restore text mode: %s\n", strerror(errno));
1039 vt_set_textarea(0, fb_orig_vinfo.yres);
1040 close(fb_tty_fd);
1041 close(fb_dev_fd);
1042 if (frame_buffer)
1043 munmap(frame_buffer, fb_size);
1044 frame_buffer = NULL;
1045 fb_preinit(1);
1048 static int preinit(const char *vo_subdevice)
1050 pre_init_err = 0;
1052 if (vo_subdevice) {
1054 free(fb_dev_name);
1055 fb_dev_name = strdup(vo_subdevice);
1058 if (!pre_init_err)
1059 return pre_init_err = fb_preinit(0) ? 0 : -1;
1060 return -1;
1063 static uint32_t get_image(mp_image_t *mpi)
1065 if (!IMGFMT_IS_BGR(mpi->imgfmt) ||
1066 IMGFMT_BGR_DEPTH(mpi->imgfmt) != fb_bpp ||
1067 (mpi->type != MP_IMGTYPE_STATIC && mpi->type != MP_IMGTYPE_TEMP) ||
1068 (mpi->flags & MP_IMGFLAG_PLANAR) ||
1069 (mpi->flags & MP_IMGFLAG_YUV) ||
1070 mpi->width != in_width ||
1071 mpi->height != in_height
1073 return VO_FALSE;
1075 mpi->planes[0] = center;
1076 mpi->stride[0] = fb_line_len;
1077 mpi->flags |= MP_IMGFLAG_DIRECT;
1078 return VO_TRUE;
1081 static int control(uint32_t request, void *data)
1083 switch (request) {
1084 case VOCTRL_GET_IMAGE:
1085 return get_image(data);
1086 case VOCTRL_QUERY_FORMAT:
1087 return query_format(*(uint32_t*)data);
1090 return VO_NOTIMPL;