Add config.h/config.mak bzlib variables missed in last commit.
[mplayer/glamo.git] / libvo / vo_fbdev.c
blob7ae113171d8accd95e29c67609cb7e4d7051ad9a
1 /*
2 * Video driver for Framebuffer device
3 * by Szabolcs Berecz <szabi@inf.elte.hu>
4 * (C) 2001
6 * Some idea and code borrowed from Chris Lawrence's ppmtofb-0.27
7 * Some fixes and small improvements by Joey Parrish <joey@nicewarrior.org>
8 */
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <string.h>
13 #include <fcntl.h>
14 #include <unistd.h>
15 #include <errno.h>
16 #include <ctype.h>
18 #include <sys/mman.h>
19 #include <sys/ioctl.h>
20 #include <sys/kd.h>
21 #include <linux/fb.h>
23 #include "config.h"
24 #include "video_out.h"
25 #include "video_out_internal.h"
26 #include "fastmemcpy.h"
27 #include "sub.h"
28 #include "geometry.h"
29 #ifdef CONFIG_VIDIX
30 #include "vosub_vidix.h"
31 #endif
32 #include "aspect.h"
33 #include "mp_msg.h"
35 static const vo_info_t info = {
36 "Framebuffer Device",
37 "fbdev",
38 "Szabolcs Berecz <szabi@inf.elte.hu>",
42 LIBVO_EXTERN(fbdev)
44 #ifdef CONFIG_VIDIX
45 /* Name of VIDIX driver */
46 static const char *vidix_name = NULL;
47 static vidix_grkey_t gr_key;
48 #endif
49 static signed int pre_init_err = -2;
50 /******************************
51 * fb.modes support *
52 ******************************/
54 static range_t *monitor_hfreq = NULL;
55 static range_t *monitor_vfreq = NULL;
56 static range_t *monitor_dotclock = NULL;
58 typedef struct {
59 char *name;
60 uint32_t xres, yres, vxres, vyres, depth;
61 uint32_t pixclock, left, right, upper, lower, hslen, vslen;
62 uint32_t sync;
63 uint32_t vmode;
64 } fb_mode_t;
66 #define MAX_NR_TOKEN 16
68 #define MAX_LINE_LEN 1000
70 #define RET_EOF -1
71 #define RET_EOL -2
73 static int validate_mode(fb_mode_t *m)
75 if (!m->xres) {
76 mp_msg(MSGT_VO, MSGL_V, "needs geometry ");
77 return 0;
79 if (!m->pixclock) {
80 mp_msg(MSGT_VO, MSGL_V, "needs timings ");
81 return 0;
83 return 1;
86 static FILE *fp;
87 static int line_num = 0;
88 static char *line;
89 static char *token[MAX_NR_TOKEN];
91 static int get_token(int num)
93 static int read_nextline = 1;
94 static int line_pos;
95 int i;
96 char c;
98 if (num >= MAX_NR_TOKEN) {
99 mp_msg(MSGT_VO, MSGL_V, "get_token(): max >= MAX_NR_TOKEN!\n");
100 goto out_eof;
103 if (read_nextline) {
104 if (!fgets(line, MAX_LINE_LEN, fp))
105 goto out_eof;
106 line_pos = 0;
107 ++line_num;
108 read_nextline = 0;
110 for (i = 0; i < num; i++) {
111 while (isspace(line[line_pos]))
112 ++line_pos;
113 if (line[line_pos] == '\0' || line[line_pos] == '#') {
114 read_nextline = 1;
115 goto out_eol;
117 token[i] = line + line_pos;
118 c = line[line_pos];
119 if (c == '"' || c == '\'') {
120 token[i]++;
121 while (line[++line_pos] != c && line[line_pos])
122 /* NOTHING */;
123 if (!line[line_pos])
124 goto out_eol;
125 line[line_pos] = ' ';
126 } else {
127 for (/* NOTHING */; !isspace(line[line_pos]) &&
128 line[line_pos]; line_pos++)
129 /* NOTHING */;
131 if (!line[line_pos]) {
132 read_nextline = 1;
133 if (i == num - 1)
134 goto out_ok;
135 goto out_eol;
137 line[line_pos++] = '\0';
139 out_ok:
140 return i;
141 out_eof:
142 return RET_EOF;
143 out_eol:
144 return RET_EOL;
147 static fb_mode_t *fb_modes = NULL;
148 static int nr_modes = 0;
150 static int parse_fbmode_cfg(char *cfgfile)
152 #define CHECK_IN_MODE_DEF\
153 if (!in_mode_def) {\
154 mp_msg(MSGT_VO, MSGL_V, "'needs 'mode' first");\
155 goto err_out_print_linenum;\
157 fb_mode_t *mode = NULL;
158 char *endptr; // strtoul()...
159 int in_mode_def = 0;
160 int tmp, i;
162 /* If called more than once, reuse parsed data */
163 if (nr_modes)
164 return nr_modes;
166 mp_msg(MSGT_VO, MSGL_V, "Reading %s: ", cfgfile);
168 if ((fp = fopen(cfgfile, "r")) == NULL) {
169 mp_msg(MSGT_VO, MSGL_V, "can't open '%s': %s\n", cfgfile, strerror(errno));
170 return -1;
173 if ((line = (char *) malloc(MAX_LINE_LEN + 1)) == NULL) {
174 mp_msg(MSGT_VO, MSGL_V, "can't get memory for 'line': %s\n", strerror(errno));
175 return -2;
179 * check if the cfgfile starts with 'mode'
181 while ((tmp = get_token(1)) == RET_EOL)
182 /* NOTHING */;
183 if (tmp == RET_EOF)
184 goto out;
185 if (!strcmp(token[0], "mode"))
186 goto loop_enter;
187 goto err_out_parse_error;
189 while ((tmp = get_token(1)) != RET_EOF) {
190 if (tmp == RET_EOL)
191 continue;
192 if (!strcmp(token[0], "mode")) {
193 if (in_mode_def) {
194 mp_msg(MSGT_VO, MSGL_V, "'endmode' required");
195 goto err_out_print_linenum;
197 if (!validate_mode(mode))
198 goto err_out_not_valid;
199 loop_enter:
200 if (!(fb_modes = (fb_mode_t *)
201 realloc(fb_modes, sizeof(fb_mode_t) * (nr_modes + 1)))) {
202 mp_msg(MSGT_VO, MSGL_V, "can't realloc 'fb_modes' (nr_modes = %d):"
203 " %s\n", nr_modes, strerror(errno));
204 goto err_out;
206 mode = fb_modes + nr_modes;
207 ++nr_modes;
208 memset(mode, 0, sizeof(fb_mode_t));
210 if (get_token(1) < 0)
211 goto err_out_parse_error;
212 for (i = 0; i < nr_modes - 1; i++) {
213 if (!strcmp(token[0], fb_modes[i].name)) {
214 mp_msg(MSGT_VO, MSGL_V, "mode name '%s' isn't unique", token[0]);
215 goto err_out_print_linenum;
218 if (!(mode->name = strdup(token[0]))) {
219 mp_msg(MSGT_VO, MSGL_V, "can't strdup -> 'name': %s\n", strerror(errno));
220 goto err_out;
222 in_mode_def = 1;
223 } else if (!strcmp(token[0], "geometry")) {
224 CHECK_IN_MODE_DEF;
225 if (get_token(5) < 0)
226 goto err_out_parse_error;
227 mode->xres = strtoul(token[0], &endptr, 0);
228 if (*endptr)
229 goto err_out_parse_error;
230 mode->yres = strtoul(token[1], &endptr, 0);
231 if (*endptr)
232 goto err_out_parse_error;
233 mode->vxres = strtoul(token[2], &endptr, 0);
234 if (*endptr)
235 goto err_out_parse_error;
236 mode->vyres = strtoul(token[3], &endptr, 0);
237 if (*endptr)
238 goto err_out_parse_error;
239 mode->depth = strtoul(token[4], &endptr, 0);
240 if (*endptr)
241 goto err_out_parse_error;
242 } else if (!strcmp(token[0], "timings")) {
243 CHECK_IN_MODE_DEF;
244 if (get_token(7) < 0)
245 goto err_out_parse_error;
246 mode->pixclock = strtoul(token[0], &endptr, 0);
247 if (*endptr)
248 goto err_out_parse_error;
249 mode->left = strtoul(token[1], &endptr, 0);
250 if (*endptr)
251 goto err_out_parse_error;
252 mode->right = strtoul(token[2], &endptr, 0);
253 if (*endptr)
254 goto err_out_parse_error;
255 mode->upper = strtoul(token[3], &endptr, 0);
256 if (*endptr)
257 goto err_out_parse_error;
258 mode->lower = strtoul(token[4], &endptr, 0);
259 if (*endptr)
260 goto err_out_parse_error;
261 mode->hslen = strtoul(token[5], &endptr, 0);
262 if (*endptr)
263 goto err_out_parse_error;
264 mode->vslen = strtoul(token[6], &endptr, 0);
265 if (*endptr)
266 goto err_out_parse_error;
267 } else if (!strcmp(token[0], "endmode")) {
268 CHECK_IN_MODE_DEF;
269 in_mode_def = 0;
270 } else if (!strcmp(token[0], "accel")) {
271 CHECK_IN_MODE_DEF;
272 if (get_token(1) < 0)
273 goto err_out_parse_error;
275 * it's only used for text acceleration
276 * so we just ignore it.
278 } else if (!strcmp(token[0], "hsync")) {
279 CHECK_IN_MODE_DEF;
280 if (get_token(1) < 0)
281 goto err_out_parse_error;
282 if (!strcmp(token[0], "low"))
283 mode->sync &= ~FB_SYNC_HOR_HIGH_ACT;
284 else if (!strcmp(token[0], "high"))
285 mode->sync |= FB_SYNC_HOR_HIGH_ACT;
286 else
287 goto err_out_parse_error;
288 } else if (!strcmp(token[0], "vsync")) {
289 CHECK_IN_MODE_DEF;
290 if (get_token(1) < 0)
291 goto err_out_parse_error;
292 if (!strcmp(token[0], "low"))
293 mode->sync &= ~FB_SYNC_VERT_HIGH_ACT;
294 else if (!strcmp(token[0], "high"))
295 mode->sync |= FB_SYNC_VERT_HIGH_ACT;
296 else
297 goto err_out_parse_error;
298 } else if (!strcmp(token[0], "csync")) {
299 CHECK_IN_MODE_DEF;
300 if (get_token(1) < 0)
301 goto err_out_parse_error;
302 if (!strcmp(token[0], "low"))
303 mode->sync &= ~FB_SYNC_COMP_HIGH_ACT;
304 else if (!strcmp(token[0], "high"))
305 mode->sync |= FB_SYNC_COMP_HIGH_ACT;
306 else
307 goto err_out_parse_error;
308 } else if (!strcmp(token[0], "extsync")) {
309 CHECK_IN_MODE_DEF;
310 if (get_token(1) < 0)
311 goto err_out_parse_error;
312 if (!strcmp(token[0], "false"))
313 mode->sync &= ~FB_SYNC_EXT;
314 else if (!strcmp(token[0], "true"))
315 mode->sync |= FB_SYNC_EXT;
316 else
317 goto err_out_parse_error;
318 } else if (!strcmp(token[0], "laced")) {
319 CHECK_IN_MODE_DEF;
320 if (get_token(1) < 0)
321 goto err_out_parse_error;
322 if (!strcmp(token[0], "false"))
323 mode->vmode = FB_VMODE_NONINTERLACED;
324 else if (!strcmp(token[0], "true"))
325 mode->vmode = FB_VMODE_INTERLACED;
326 else
327 goto err_out_parse_error;
328 } else if (!strcmp(token[0], "double")) {
329 CHECK_IN_MODE_DEF;
330 if (get_token(1) < 0)
331 goto err_out_parse_error;
332 if (!strcmp(token[0], "false"))
334 else if (!strcmp(token[0], "true"))
335 mode->vmode = FB_VMODE_DOUBLE;
336 else
337 goto err_out_parse_error;
338 } else
339 goto err_out_parse_error;
341 if (!validate_mode(mode))
342 goto err_out_not_valid;
343 out:
344 mp_msg(MSGT_VO, MSGL_V, "%d modes\n", nr_modes);
345 free(line);
346 fclose(fp);
347 return nr_modes;
348 err_out_parse_error:
349 mp_msg(MSGT_VO, MSGL_V, "parse error");
350 err_out_print_linenum:
351 mp_msg(MSGT_VO, MSGL_V, " at line %d\n", line_num);
352 err_out:
353 if (fb_modes) {
354 free(fb_modes);
355 fb_modes = NULL;
357 nr_modes = 0;
358 free(line);
359 free(fp);
360 return -2;
361 err_out_not_valid:
362 mp_msg(MSGT_VO, MSGL_V, "previous mode is not correct");
363 goto err_out_print_linenum;
366 static fb_mode_t *find_mode_by_name(char *name)
368 int i;
370 for (i = 0; i < nr_modes; i++)
371 if (!strcmp(name, fb_modes[i].name))
372 return fb_modes + i;
373 return NULL;
376 static float dcf(fb_mode_t *m) //driving clock frequency
378 return 1e12f / m->pixclock;
381 static float hsf(fb_mode_t *m) //horizontal scan frequency
383 int htotal = m->left + m->xres + m->right + m->hslen;
384 return dcf(m) / htotal;
387 static float vsf(fb_mode_t *m) //vertical scan frequency
389 int vtotal = m->upper + m->yres + m->lower + m->vslen;
390 return hsf(m) / vtotal;
394 static int mode_works(fb_mode_t *m, range_t *hfreq, range_t *vfreq,
395 range_t *dotclock)
397 float h = hsf(m);
398 float v = vsf(m);
399 float d = dcf(m);
400 int ret = 1;
402 mp_msg(MSGT_VO, MSGL_DBG2, "mode %dx%d:", m->xres, m->yres);
403 if (!in_range(hfreq, h)) {
404 ret = 0;
405 mp_msg(MSGT_VO, MSGL_DBG2, " hsync out of range.");
407 if (!in_range(vfreq, v)) {
408 ret = 0;
409 mp_msg(MSGT_VO, MSGL_DBG2, " vsync out of range.");
411 if (!in_range(dotclock, d)) {
412 ret = 0;
413 mp_msg(MSGT_VO, MSGL_DBG2, " dotclock out of range.");
415 if (ret)
416 mp_msg(MSGT_VO, MSGL_DBG2, " hsync, vsync, dotclock ok.\n");
417 else
418 mp_msg(MSGT_VO, MSGL_DBG2, "\n");
420 return ret;
423 static fb_mode_t *find_best_mode(int xres, int yres, range_t *hfreq,
424 range_t *vfreq, range_t *dotclock)
426 int i;
427 fb_mode_t *best = fb_modes;
428 fb_mode_t *curr;
430 mp_msg(MSGT_VO, MSGL_DBG2, "Searching for first working mode\n");
432 for (i = 0; i < nr_modes; i++, best++)
433 if (mode_works(best, hfreq, vfreq, dotclock))
434 break;
436 if (i == nr_modes)
437 return NULL;
438 if (i == nr_modes - 1)
439 return best;
441 mp_msg(MSGT_VO, MSGL_DBG2, "First working mode: %dx%d\n", best->xres, best->yres);
442 mp_msg(MSGT_VO, MSGL_DBG2, "Searching for better modes\n");
444 for (curr = best + 1; i < nr_modes - 1; i++, curr++) {
445 if (!mode_works(curr, hfreq, vfreq, dotclock))
446 continue;
448 if (best->xres < xres || best->yres < yres) {
449 if (curr->xres > best->xres || curr->yres > best->yres) {
450 mp_msg(MSGT_VO, MSGL_DBG2, "better than %dx%d, which is too small.\n",
451 best->xres, best->yres);
452 best = curr;
453 } else
454 mp_msg(MSGT_VO, MSGL_DBG2, "too small.\n");
455 } else if (curr->xres == best->xres && curr->yres == best->yres &&
456 vsf(curr) > vsf(best)) {
457 mp_msg(MSGT_VO, MSGL_DBG2, "faster screen refresh.\n");
458 best = curr;
459 } else if ((curr->xres <= best->xres && curr->yres <= best->yres) &&
460 (curr->xres >= xres && curr->yres >= yres)) {
461 mp_msg(MSGT_VO, MSGL_DBG2, "better than %dx%d, which is too large.\n",
462 best->xres, best->yres);
463 best = curr;
464 } else {
465 if (curr->xres < xres || curr->yres < yres)
466 mp_msg(MSGT_VO, MSGL_DBG2, "too small.\n");
467 else if (curr->xres > best->xres || curr->yres > best->yres)
468 mp_msg(MSGT_VO, MSGL_DBG2, "too large.\n");
469 else
470 mp_msg(MSGT_VO, MSGL_DBG2, "it's worse, don't know why.\n");
474 return best;
477 static void set_bpp(struct fb_var_screeninfo *p, int bpp)
479 p->bits_per_pixel = (bpp + 1) & ~1;
480 p->red.msb_right = p->green.msb_right = p->blue.msb_right = p->transp.msb_right = 0;
481 p->transp.offset = p->transp.length = 0;
482 p->blue.offset = 0;
483 switch (bpp) {
484 case 32:
485 p->transp.offset = 24;
486 p->transp.length = 8;
487 case 24:
488 p->red.offset = 16;
489 p->red.length = 8;
490 p->green.offset = 8;
491 p->green.length = 8;
492 p->blue.length = 8;
493 break;
494 case 16:
495 p->red.offset = 11;
496 p->green.length = 6;
497 p->red.length = 5;
498 p->green.offset = 5;
499 p->blue.length = 5;
500 break;
501 case 15:
502 p->red.offset = 10;
503 p->green.length = 5;
504 p->red.length = 5;
505 p->green.offset = 5;
506 p->blue.length = 5;
507 break;
511 static void fb_mode2fb_vinfo(fb_mode_t *m, struct fb_var_screeninfo *v)
513 v->xres = m->xres;
514 v->yres = m->yres;
515 v->xres_virtual = m->vxres;
516 v->yres_virtual = m->vyres;
517 set_bpp(v, m->depth);
518 v->pixclock = m->pixclock;
519 v->left_margin = m->left;
520 v->right_margin = m->right;
521 v->upper_margin = m->upper;
522 v->lower_margin = m->lower;
523 v->hsync_len = m->hslen;
524 v->vsync_len = m->vslen;
525 v->sync = m->sync;
526 v->vmode = m->vmode;
530 /******************************
531 * vo_fbdev *
532 ******************************/
534 /* command line/config file options */
535 static char *fb_dev_name = NULL;
536 char *fb_mode_cfgfile = NULL;
537 char *fb_mode_name = NULL;
539 static fb_mode_t *fb_mode = NULL;
541 /* vt related variables */
542 static FILE *vt_fp = NULL;
543 static int vt_doit = 1;
545 /* vo_fbdev related variables */
546 static int fb_dev_fd;
547 static int fb_tty_fd = -1;
548 static size_t fb_size;
549 static uint8_t *frame_buffer;
550 static uint8_t *center;
551 static struct fb_fix_screeninfo fb_finfo;
552 static struct fb_var_screeninfo fb_orig_vinfo;
553 static struct fb_var_screeninfo fb_vinfo;
554 static unsigned short fb_ored[256], fb_ogreen[256], fb_oblue[256];
555 static struct fb_cmap fb_oldcmap = { 0, 256, fb_ored, fb_ogreen, fb_oblue };
556 static int fb_cmap_changed = 0;
557 static int fb_pixel_size; // 32: 4 24: 3 16: 2 15: 2
558 static int fb_bpp; // 32: 32 24: 24 16: 16 15: 15
559 static int fb_bpp_we_want; // 32: 32 24: 24 16: 16 15: 15
560 static int fb_line_len;
561 static int fb_xres;
562 static int fb_yres;
563 static void (*draw_alpha_p)(int w, int h, unsigned char *src,
564 unsigned char *srca, int stride,
565 unsigned char *dst, int dstride);
567 static int in_width;
568 static int in_height;
569 static int out_width;
570 static int out_height;
571 static int first_row;
572 static int last_row;
573 static uint32_t pixel_format;
574 static int fs;
577 * Note: this function is completely cut'n'pasted from
578 * Chris Lawrence's code.
579 * (modified a bit to fit in my code...)
581 static struct fb_cmap *make_directcolor_cmap(struct fb_var_screeninfo *var)
583 /* Hopefully any DIRECTCOLOR device will have a big enough palette
584 * to handle mapping the full color depth.
585 * e.g. 8 bpp -> 256 entry palette
587 * We could handle some sort of gamma here
589 int i, cols, rcols, gcols, bcols;
590 uint16_t *red, *green, *blue;
591 struct fb_cmap *cmap;
593 rcols = 1 << var->red.length;
594 gcols = 1 << var->green.length;
595 bcols = 1 << var->blue.length;
597 /* Make our palette the length of the deepest color */
598 cols = (rcols > gcols ? rcols : gcols);
599 cols = (cols > bcols ? cols : bcols);
601 red = malloc(cols * sizeof(red[0]));
602 if (!red) {
603 mp_msg(MSGT_VO, MSGL_V, "Can't allocate red palette with %d entries.\n", cols);
604 return NULL;
606 for (i = 0; i < rcols; i++)
607 red[i] = (65535 / (rcols - 1)) * i;
609 green = malloc(cols * sizeof(green[0]));
610 if (!green) {
611 mp_msg(MSGT_VO, MSGL_V, "Can't allocate green palette with %d entries.\n", cols);
612 free(red);
613 return NULL;
615 for (i = 0; i < gcols; i++)
616 green[i] = (65535 / (gcols - 1)) * i;
618 blue = malloc(cols * sizeof(blue[0]));
619 if (!blue) {
620 mp_msg(MSGT_VO, MSGL_V, "Can't allocate blue palette with %d entries.\n", cols);
621 free(red);
622 free(green);
623 return NULL;
625 for (i = 0; i < bcols; i++)
626 blue[i] = (65535 / (bcols - 1)) * i;
628 cmap = malloc(sizeof(struct fb_cmap));
629 if (!cmap) {
630 mp_msg(MSGT_VO, MSGL_V, "Can't allocate color map\n");
631 free(red);
632 free(green);
633 free(blue);
634 return NULL;
636 cmap->start = 0;
637 cmap->transp = 0;
638 cmap->len = cols;
639 cmap->red = red;
640 cmap->blue = blue;
641 cmap->green = green;
642 cmap->transp = NULL;
644 return cmap;
648 static int fb_preinit(int reset)
650 static int fb_preinit_done = 0;
651 static int fb_works = 0;
653 if (reset) {
654 fb_preinit_done = 0;
655 return 0;
658 if (fb_preinit_done)
659 return fb_works;
661 if (!fb_dev_name && !(fb_dev_name = getenv("FRAMEBUFFER")))
662 fb_dev_name = strdup("/dev/fb0");
663 mp_msg(MSGT_VO, MSGL_V, "using %s\n", fb_dev_name);
665 if ((fb_dev_fd = open(fb_dev_name, O_RDWR)) == -1) {
666 mp_msg(MSGT_VO, MSGL_ERR, "Can't open %s: %s\n", fb_dev_name, strerror(errno));
667 goto err_out;
669 if (ioctl(fb_dev_fd, FBIOGET_VSCREENINFO, &fb_vinfo)) {
670 mp_msg(MSGT_VO, MSGL_ERR, "Can't get VSCREENINFO: %s\n", strerror(errno));
671 goto err_out_fd;
673 fb_orig_vinfo = fb_vinfo;
675 if ((fb_tty_fd = open("/dev/tty", O_RDWR)) < 0) {
676 mp_msg(MSGT_VO, MSGL_ERR, "notice: Can't open /dev/tty: %s\n", strerror(errno));
679 fb_bpp = fb_vinfo.red.length + fb_vinfo.green.length +
680 fb_vinfo.blue.length + fb_vinfo.transp.length;
682 if (fb_bpp == 8 && !vo_dbpp) {
683 mp_msg(MSGT_VO, MSGL_ERR, "8 bpp output is not supported.\n");
684 goto err_out_tty_fd;
687 if (vo_dbpp) {
688 if (vo_dbpp != 15 && vo_dbpp != 16 && vo_dbpp != 24 && vo_dbpp != 32) {
689 mp_msg(MSGT_VO, MSGL_ERR, "can't switch to %d bpp\n", vo_dbpp);
690 goto err_out_fd;
692 fb_bpp = vo_dbpp;
695 if (!fb_mode_cfgfile)
696 fb_mode_cfgfile = strdup("/etc/fb.modes");
698 fb_preinit_done = 1;
699 fb_works = 1;
700 return 1;
701 err_out_tty_fd:
702 close(fb_tty_fd);
703 fb_tty_fd = -1;
704 err_out_fd:
705 close(fb_dev_fd);
706 fb_dev_fd = -1;
707 err_out:
708 fb_preinit_done = 1;
709 fb_works = 0;
710 return 0;
713 static void vt_set_textarea(int u, int l)
715 /* how can I determine the font height?
716 * just use 16 for now
718 int urow = ((u + 15) / 16) + 1;
719 int lrow = l / 16;
721 mp_msg(MSGT_VO, MSGL_DBG2, "vt_set_textarea(%d,%d): %d,%d\n", u, l, urow, lrow);
722 if (vt_fp) {
723 fprintf(vt_fp, "\33[%d;%dr\33[%d;%dH", urow, lrow, lrow, 0);
724 fflush(vt_fp);
728 static int config(uint32_t width, uint32_t height, uint32_t d_width,
729 uint32_t d_height, uint32_t flags, char *title,
730 uint32_t format)
732 struct fb_cmap *cmap;
733 int vm = flags & VOFLAG_MODESWITCHING;
734 int zoom = flags & VOFLAG_SWSCALE;
735 int vt_fd;
737 fs = flags & VOFLAG_FULLSCREEN;
739 if (pre_init_err == -2) {
740 mp_msg(MSGT_VO, MSGL_ERR, "Internal fatal error: config() was called before preinit()\n");
741 return -1;
744 if (pre_init_err)
745 return 1;
747 if (fb_mode_name && !vm) {
748 mp_msg(MSGT_VO, MSGL_ERR, "-fbmode can only be used with -vm\n");
749 return 1;
751 if (vm && (parse_fbmode_cfg(fb_mode_cfgfile) < 0))
752 return 1;
753 if (d_width && (fs || vm)) {
754 out_width = d_width;
755 out_height = d_height;
756 } else {
757 out_width = width;
758 out_height = height;
760 in_width = width;
761 in_height = height;
762 pixel_format = format;
764 if (fb_mode_name) {
765 if (!(fb_mode = find_mode_by_name(fb_mode_name))) {
766 mp_msg(MSGT_VO, MSGL_ERR, "can't find requested video mode\n");
767 return 1;
769 fb_mode2fb_vinfo(fb_mode, &fb_vinfo);
770 } else if (vm) {
771 monitor_hfreq = str2range(monitor_hfreq_str);
772 monitor_vfreq = str2range(monitor_vfreq_str);
773 monitor_dotclock = str2range(monitor_dotclock_str);
774 if (!monitor_hfreq || !monitor_vfreq || !monitor_dotclock) {
775 mp_msg(MSGT_VO, MSGL_ERR, "you have to specify the capabilities of"
776 " the monitor.\n");
777 return 1;
779 if (!(fb_mode = find_best_mode(out_width, out_height, monitor_hfreq,
780 monitor_vfreq, monitor_dotclock))) {
781 mp_msg(MSGT_VO, MSGL_ERR, "can't find best video mode\n");
782 return 1;
784 mp_msg(MSGT_VO, MSGL_V, "using mode %dx%d @ %.1fHz\n", fb_mode->xres,
785 fb_mode->yres, vsf(fb_mode));
786 fb_mode2fb_vinfo(fb_mode, &fb_vinfo);
788 fb_bpp_we_want = fb_bpp;
789 set_bpp(&fb_vinfo, fb_bpp);
790 fb_vinfo.xres_virtual = fb_vinfo.xres;
791 fb_vinfo.yres_virtual = fb_vinfo.yres;
793 if (fb_tty_fd >= 0 && ioctl(fb_tty_fd, KDSETMODE, KD_GRAPHICS) < 0) {
794 mp_msg(MSGT_VO, MSGL_V, "Can't set graphics mode: %s\n", strerror(errno));
795 close(fb_tty_fd);
796 fb_tty_fd = -1;
799 if (ioctl(fb_dev_fd, FBIOPUT_VSCREENINFO, &fb_vinfo)) {
800 mp_msg(MSGT_VO, MSGL_ERR, "Can't put VSCREENINFO: %s\n", strerror(errno));
801 if (fb_tty_fd >= 0 && ioctl(fb_tty_fd, KDSETMODE, KD_TEXT) < 0) {
802 mp_msg(MSGT_VO, MSGL_ERR, "Can't restore text mode: %s\n", strerror(errno));
804 return 1;
807 fb_pixel_size = fb_vinfo.bits_per_pixel / 8;
808 fb_bpp = fb_vinfo.red.length + fb_vinfo.green.length +
809 fb_vinfo.blue.length + fb_vinfo.transp.length;
810 if (fb_bpp_we_want != fb_bpp)
811 mp_msg(MSGT_VO, MSGL_WARN, "requested %d bpp, got %d bpp!!!\n",
812 fb_bpp_we_want, fb_bpp);
814 switch (fb_bpp) {
815 case 32:
816 draw_alpha_p = vo_draw_alpha_rgb32;
817 break;
818 case 24:
819 draw_alpha_p = vo_draw_alpha_rgb24;
820 break;
821 case 16:
822 draw_alpha_p = vo_draw_alpha_rgb16;
823 break;
824 case 15:
825 draw_alpha_p = vo_draw_alpha_rgb15;
826 break;
827 default:
828 return 1;
831 fb_xres = fb_vinfo.xres;
832 fb_yres = fb_vinfo.yres;
834 if (vm || fs) {
835 out_width = fb_xres;
836 out_height = fb_yres;
838 if (out_width < in_width || out_height < in_height) {
839 mp_msg(MSGT_VO, MSGL_ERR, "screensize is smaller than video size\n");
840 return 1;
843 first_row = (out_height - in_height) / 2;
844 last_row = (out_height + in_height) / 2;
846 if (ioctl(fb_dev_fd, FBIOGET_FSCREENINFO, &fb_finfo)) {
847 mp_msg(MSGT_VO, MSGL_ERR, "Can't get FSCREENINFO: %s\n", strerror(errno));
848 return 1;
851 if (fb_finfo.type != FB_TYPE_PACKED_PIXELS) {
852 mp_msg(MSGT_VO, MSGL_ERR, "type %d not supported\n", fb_finfo.type);
853 return 1;
856 switch (fb_finfo.visual) {
857 case FB_VISUAL_TRUECOLOR:
858 break;
859 case FB_VISUAL_DIRECTCOLOR:
860 mp_msg(MSGT_VO, MSGL_V, "creating cmap for directcolor\n");
861 if (ioctl(fb_dev_fd, FBIOGETCMAP, &fb_oldcmap)) {
862 mp_msg(MSGT_VO, MSGL_ERR, "can't get cmap: %s\n",
863 strerror(errno));
864 return 1;
866 if (!(cmap = make_directcolor_cmap(&fb_vinfo)))
867 return 1;
868 if (ioctl(fb_dev_fd, FBIOPUTCMAP, cmap)) {
869 mp_msg(MSGT_VO, MSGL_ERR, "can't put cmap: %s\n",
870 strerror(errno));
871 return 1;
873 fb_cmap_changed = 1;
874 free(cmap->red);
875 free(cmap->green);
876 free(cmap->blue);
877 free(cmap);
878 break;
879 default:
880 mp_msg(MSGT_VO, MSGL_ERR, "visual: %d not yet supported\n",
881 fb_finfo.visual);
882 return 1;
885 fb_line_len = fb_finfo.line_length;
886 fb_size = fb_finfo.smem_len;
888 #ifdef CONFIG_VIDIX
889 if (vidix_name) {
890 unsigned image_width, image_height, x_offset, y_offset;
891 if (zoom || fs) {
892 aspect_save_orig(width, height);
893 aspect_save_prescale(d_width, d_height);
894 aspect_save_screenres(fb_xres, fb_yres);
895 aspect(&image_width, &image_height, fs ? A_ZOOM : A_NOZOOM);
896 } else {
897 image_width = width;
898 image_height = height;
901 if (fb_xres > image_width)
902 x_offset = (fb_xres - image_width) / 2;
903 else
904 x_offset = 0;
905 if (fb_yres > image_height)
906 y_offset = (fb_yres - image_height) / 2;
907 else
908 y_offset = 0;
910 if (vidix_init(width, height, x_offset, y_offset, image_width,
911 image_height, format, fb_bpp, fb_xres, fb_yres) != 0) {
912 mp_msg(MSGT_VO, MSGL_ERR, "Can't initialize VIDIX driver\n");
913 vidix_name = NULL;
914 vidix_term();
915 return -1;
916 } else
917 mp_msg(MSGT_VO, MSGL_V, "Using VIDIX\n");
918 vidix_start();
919 if (vidix_grkey_support()) {
920 vidix_grkey_get(&gr_key);
921 gr_key.key_op = KEYS_PUT;
922 if (!(vo_colorkey & 0xff000000)) {
923 gr_key.ckey.op = CKEY_TRUE;
924 gr_key.ckey.red = (vo_colorkey & 0x00ff0000) >> 16;
925 gr_key.ckey.green = (vo_colorkey & 0x0000ff00) >> 8;
926 gr_key.ckey.blue = vo_colorkey & 0x000000ff;
927 } else
928 gr_key.ckey.op = CKEY_FALSE;
929 vidix_grkey_set(&gr_key);
931 } else
932 #endif
934 int x_offset = 0, y_offset = 0;
935 geometry(&x_offset, &y_offset, &out_width, &out_height, fb_xres, fb_yres);
937 frame_buffer = (uint8_t *) mmap(0, fb_size, PROT_READ | PROT_WRITE,
938 MAP_SHARED, fb_dev_fd, 0);
939 if (frame_buffer == (uint8_t *) -1) {
940 mp_msg(MSGT_VO, MSGL_ERR, "Can't mmap %s: %s\n", fb_dev_name, strerror(errno));
941 return 1;
944 center = frame_buffer +
945 ( (out_width - in_width) / 2 ) * fb_pixel_size +
946 ( (out_height - in_height) / 2 ) * fb_line_len +
947 x_offset * fb_pixel_size + y_offset * fb_line_len;
949 mp_msg(MSGT_VO, MSGL_DBG2, "frame_buffer @ %p\n", frame_buffer);
950 mp_msg(MSGT_VO, MSGL_DBG2, "center @ %p\n", center);
951 mp_msg(MSGT_VO, MSGL_V, "pixel per line: %d\n", fb_line_len / fb_pixel_size);
953 if (fs || vm)
954 memset(frame_buffer, '\0', fb_line_len * fb_yres);
956 if (vt_doit && (vt_fd = open("/dev/tty", O_WRONLY)) == -1) {
957 mp_msg(MSGT_VO, MSGL_ERR, "can't open /dev/tty: %s\n", strerror(errno));
958 vt_doit = 0;
960 if (vt_doit && !(vt_fp = fdopen(vt_fd, "w"))) {
961 mp_msg(MSGT_VO, MSGL_ERR, "can't fdopen /dev/tty: %s\n", strerror(errno));
962 vt_doit = 0;
965 if (vt_doit)
966 vt_set_textarea(last_row, fb_yres);
968 return 0;
971 static int query_format(uint32_t format)
973 if (!fb_preinit(0))
974 return 0;
975 #ifdef CONFIG_VIDIX
976 if (vidix_name)
977 return vidix_query_fourcc(format);
978 #endif
979 if ((format & IMGFMT_BGR_MASK) == IMGFMT_BGR) {
980 int bpp = format & 0xff;
982 if (bpp == fb_bpp)
983 return VFCAP_ACCEPT_STRIDE | VFCAP_CSP_SUPPORTED | VFCAP_CSP_SUPPORTED_BY_HW;
985 return 0;
988 static void draw_alpha(int x0, int y0, int w, int h, unsigned char *src,
989 unsigned char *srca, int stride)
991 unsigned char *dst;
993 dst = center + fb_line_len * y0 + fb_pixel_size * x0;
995 (*draw_alpha_p)(w, h, src, srca, stride, dst, fb_line_len);
998 static int draw_frame(uint8_t *src[])
1000 return 1;
1003 static int draw_slice(uint8_t *src[], int stride[], int w, int h, int x, int y)
1005 uint8_t *d, *s;
1007 d = center + fb_line_len * y + fb_pixel_size * x;
1009 s = src[0];
1010 while (h) {
1011 fast_memcpy(d, s, w * fb_pixel_size);
1012 d += fb_line_len;
1013 s += stride[0];
1014 h--;
1017 return 0;
1020 static void check_events(void)
1024 static void flip_page(void)
1028 static void draw_osd(void)
1030 vo_draw_text(in_width, in_height, draw_alpha);
1033 static void uninit(void)
1035 if (fb_cmap_changed) {
1036 if (ioctl(fb_dev_fd, FBIOPUTCMAP, &fb_oldcmap))
1037 mp_msg(MSGT_VO, MSGL_WARN, "Can't restore original cmap\n");
1038 fb_cmap_changed = 0;
1040 if (ioctl(fb_dev_fd, FBIOGET_VSCREENINFO, &fb_vinfo))
1041 mp_msg(MSGT_VO, MSGL_WARN, "ioctl FBIOGET_VSCREENINFO: %s\n", strerror(errno));
1042 fb_orig_vinfo.xoffset = fb_vinfo.xoffset;
1043 fb_orig_vinfo.yoffset = fb_vinfo.yoffset;
1044 if (ioctl(fb_dev_fd, FBIOPUT_VSCREENINFO, &fb_orig_vinfo))
1045 mp_msg(MSGT_VO, MSGL_WARN, "Can't reset original fb_var_screeninfo: %s\n", strerror(errno));
1046 if (fb_tty_fd >= 0) {
1047 if (ioctl(fb_tty_fd, KDSETMODE, KD_TEXT) < 0)
1048 mp_msg(MSGT_VO, MSGL_WARN, "Can't restore text mode: %s\n", strerror(errno));
1050 if (vt_doit)
1051 vt_set_textarea(0, fb_orig_vinfo.yres);
1052 close(fb_tty_fd);
1053 close(fb_dev_fd);
1054 if (frame_buffer)
1055 munmap(frame_buffer, fb_size);
1056 frame_buffer = NULL;
1057 #ifdef CONFIG_VIDIX
1058 if (vidix_name)
1059 vidix_term();
1060 #endif
1061 fb_preinit(1);
1064 static int preinit(const char *vo_subdevice)
1066 pre_init_err = 0;
1068 if (vo_subdevice) {
1069 #ifdef CONFIG_VIDIX
1070 if (memcmp(vo_subdevice, "vidix", 5) == 0)
1071 vidix_name = &vo_subdevice[5];
1072 if (vidix_name)
1073 pre_init_err = vidix_preinit(vidix_name, &video_out_fbdev);
1074 else
1075 #endif
1077 if (fb_dev_name)
1078 free(fb_dev_name);
1079 fb_dev_name = strdup(vo_subdevice);
1082 if (!pre_init_err)
1083 return pre_init_err = (fb_preinit(0) ? 0 : -1);
1084 return -1;
1087 static uint32_t get_image(mp_image_t *mpi)
1089 if (!IMGFMT_IS_BGR(mpi->imgfmt) ||
1090 (IMGFMT_BGR_DEPTH(mpi->imgfmt) != fb_bpp) ||
1091 ((mpi->type != MP_IMGTYPE_STATIC) && (mpi->type != MP_IMGTYPE_TEMP)) ||
1092 (mpi->flags & MP_IMGFLAG_PLANAR) ||
1093 (mpi->flags & MP_IMGFLAG_YUV) ||
1094 (mpi->width != in_width) ||
1095 (mpi->height != in_height)
1097 return VO_FALSE;
1099 mpi->planes[0] = center;
1100 mpi->stride[0] = fb_line_len;
1101 mpi->flags |= MP_IMGFLAG_DIRECT;
1102 return VO_TRUE;
1105 static int control(uint32_t request, void *data, ...)
1107 switch (request) {
1108 case VOCTRL_GET_IMAGE:
1109 return get_image(data);
1110 case VOCTRL_QUERY_FORMAT:
1111 return query_format(*((uint32_t*)data));
1114 #ifdef CONFIG_VIDIX
1115 if (vidix_name) {
1116 switch (request) {
1117 case VOCTRL_SET_EQUALIZER:
1119 va_list ap;
1120 int value;
1122 va_start(ap, data);
1123 value = va_arg(ap, int);
1124 va_end(ap);
1126 return vidix_control(request, data, value);
1128 case VOCTRL_GET_EQUALIZER:
1130 va_list ap;
1131 int *value;
1133 va_start(ap, data);
1134 value = va_arg(ap, int*);
1135 va_end(ap);
1137 return vidix_control(request, data, value);
1141 #endif
1143 return VO_NOTIMPL;