Add explanatory comments to the #endif part of multiple inclusion guards.
[mplayer/greg.git] / libvo / vo_fbdev.c
blob82e6344e418ab7172ad37b5e8967797279fc5765
1 /*
2 * Video driver for Framebuffer device
3 * by Szabolcs Berecz <szabi@inf.elte.hu>
4 * (C) 2001
5 *
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 #ifdef CONFIG_VIDIX
29 #include "vosub_vidix.h"
30 #endif
31 #include "aspect.h"
32 #include "mp_msg.h"
34 static const vo_info_t info = {
35 "Framebuffer Device",
36 "fbdev",
37 "Szabolcs Berecz <szabi@inf.elte.hu>",
41 LIBVO_EXTERN(fbdev)
43 #ifdef CONFIG_VIDIX
44 /* Name of VIDIX driver */
45 static const char *vidix_name = NULL;
46 static vidix_grkey_t gr_key;
47 #endif
48 static signed int pre_init_err = -2;
49 /******************************
50 * fb.modes support *
51 ******************************/
53 static range_t *monitor_hfreq = NULL;
54 static range_t *monitor_vfreq = NULL;
55 static range_t *monitor_dotclock = NULL;
57 typedef struct {
58 char *name;
59 uint32_t xres, yres, vxres, vyres, depth;
60 uint32_t pixclock, left, right, upper, lower, hslen, vslen;
61 uint32_t sync;
62 uint32_t vmode;
63 } fb_mode_t;
65 #define MAX_NR_TOKEN 16
67 #define MAX_LINE_LEN 1000
69 #define RET_EOF -1
70 #define RET_EOL -2
72 static int validate_mode(fb_mode_t *m)
74 if (!m->xres) {
75 mp_msg(MSGT_VO, MSGL_V, "needs geometry ");
76 return 0;
78 if (!m->pixclock) {
79 mp_msg(MSGT_VO, MSGL_V, "needs timings ");
80 return 0;
82 return 1;
85 static FILE *fp;
86 static int line_num = 0;
87 static char *line;
88 static char *token[MAX_NR_TOKEN];
90 static int get_token(int num)
92 static int read_nextline = 1;
93 static int line_pos;
94 int i;
95 char c;
97 if (num >= MAX_NR_TOKEN) {
98 mp_msg(MSGT_VO, MSGL_V, "get_token(): max >= MAX_NR_TOKEN!\n");
99 goto out_eof;
102 if (read_nextline) {
103 if (!fgets(line, MAX_LINE_LEN, fp))
104 goto out_eof;
105 line_pos = 0;
106 ++line_num;
107 read_nextline = 0;
109 for (i = 0; i < num; i++) {
110 while (isspace(line[line_pos]))
111 ++line_pos;
112 if (line[line_pos] == '\0' || line[line_pos] == '#') {
113 read_nextline = 1;
114 goto out_eol;
116 token[i] = line + line_pos;
117 c = line[line_pos];
118 if (c == '"' || c == '\'') {
119 token[i]++;
120 while (line[++line_pos] != c && line[line_pos])
121 /* NOTHING */;
122 if (!line[line_pos])
123 goto out_eol;
124 line[line_pos] = ' ';
125 } else {
126 for (/* NOTHING */; !isspace(line[line_pos]) &&
127 line[line_pos]; line_pos++)
128 /* NOTHING */;
130 if (!line[line_pos]) {
131 read_nextline = 1;
132 if (i == num - 1)
133 goto out_ok;
134 goto out_eol;
136 line[line_pos++] = '\0';
138 out_ok:
139 return i;
140 out_eof:
141 return RET_EOF;
142 out_eol:
143 return RET_EOL;
146 static fb_mode_t *fb_modes = NULL;
147 static int nr_modes = 0;
149 static int parse_fbmode_cfg(char *cfgfile)
151 #define CHECK_IN_MODE_DEF\
152 if (!in_mode_def) {\
153 mp_msg(MSGT_VO, MSGL_V, "'needs 'mode' first");\
154 goto err_out_print_linenum;\
156 fb_mode_t *mode = NULL;
157 char *endptr; // strtoul()...
158 int in_mode_def = 0;
159 int tmp, i;
161 /* If called more than once, reuse parsed data */
162 if (nr_modes)
163 return nr_modes;
165 mp_msg(MSGT_VO, MSGL_V, "Reading %s: ", cfgfile);
167 if ((fp = fopen(cfgfile, "r")) == NULL) {
168 mp_msg(MSGT_VO, MSGL_V, "can't open '%s': %s\n", cfgfile, strerror(errno));
169 return -1;
172 if ((line = (char *) malloc(MAX_LINE_LEN + 1)) == NULL) {
173 mp_msg(MSGT_VO, MSGL_V, "can't get memory for 'line': %s\n", strerror(errno));
174 return -2;
178 * check if the cfgfile starts with 'mode'
180 while ((tmp = get_token(1)) == RET_EOL)
181 /* NOTHING */;
182 if (tmp == RET_EOF)
183 goto out;
184 if (!strcmp(token[0], "mode"))
185 goto loop_enter;
186 goto err_out_parse_error;
188 while ((tmp = get_token(1)) != RET_EOF) {
189 if (tmp == RET_EOL)
190 continue;
191 if (!strcmp(token[0], "mode")) {
192 if (in_mode_def) {
193 mp_msg(MSGT_VO, MSGL_V, "'endmode' required");
194 goto err_out_print_linenum;
196 if (!validate_mode(mode))
197 goto err_out_not_valid;
198 loop_enter:
199 if (!(fb_modes = (fb_mode_t *) realloc(fb_modes,
200 sizeof(fb_mode_t) * (nr_modes + 1)))) {
201 mp_msg(MSGT_VO, MSGL_V, "can't realloc 'fb_modes' (nr_modes = %d):"
202 " %s\n", nr_modes, strerror(errno));
203 goto err_out;
205 mode=fb_modes + nr_modes;
206 ++nr_modes;
207 memset(mode,0,sizeof(fb_mode_t));
209 if (get_token(1) < 0)
210 goto err_out_parse_error;
211 for (i = 0; i < nr_modes - 1; i++) {
212 if (!strcmp(token[0], fb_modes[i].name)) {
213 mp_msg(MSGT_VO, MSGL_V, "mode name '%s' isn't unique", token[0]);
214 goto err_out_print_linenum;
217 if (!(mode->name = strdup(token[0]))) {
218 mp_msg(MSGT_VO, MSGL_V, "can't strdup -> 'name': %s\n", strerror(errno));
219 goto err_out;
221 in_mode_def = 1;
222 } else if (!strcmp(token[0], "geometry")) {
223 CHECK_IN_MODE_DEF;
224 if (get_token(5) < 0)
225 goto err_out_parse_error;
226 mode->xres = strtoul(token[0], &endptr, 0);
227 if (*endptr)
228 goto err_out_parse_error;
229 mode->yres = strtoul(token[1], &endptr, 0);
230 if (*endptr)
231 goto err_out_parse_error;
232 mode->vxres = strtoul(token[2], &endptr, 0);
233 if (*endptr)
234 goto err_out_parse_error;
235 mode->vyres = strtoul(token[3], &endptr, 0);
236 if (*endptr)
237 goto err_out_parse_error;
238 mode->depth = strtoul(token[4], &endptr, 0);
239 if (*endptr)
240 goto err_out_parse_error;
241 } else if (!strcmp(token[0], "timings")) {
242 CHECK_IN_MODE_DEF;
243 if (get_token(7) < 0)
244 goto err_out_parse_error;
245 mode->pixclock = strtoul(token[0], &endptr, 0);
246 if (*endptr)
247 goto err_out_parse_error;
248 mode->left = strtoul(token[1], &endptr, 0);
249 if (*endptr)
250 goto err_out_parse_error;
251 mode->right = strtoul(token[2], &endptr, 0);
252 if (*endptr)
253 goto err_out_parse_error;
254 mode->upper = strtoul(token[3], &endptr, 0);
255 if (*endptr)
256 goto err_out_parse_error;
257 mode->lower = strtoul(token[4], &endptr, 0);
258 if (*endptr)
259 goto err_out_parse_error;
260 mode->hslen = strtoul(token[5], &endptr, 0);
261 if (*endptr)
262 goto err_out_parse_error;
263 mode->vslen = strtoul(token[6], &endptr, 0);
264 if (*endptr)
265 goto err_out_parse_error;
266 } else if (!strcmp(token[0], "endmode")) {
267 CHECK_IN_MODE_DEF;
268 in_mode_def = 0;
269 } else if (!strcmp(token[0], "accel")) {
270 CHECK_IN_MODE_DEF;
271 if (get_token(1) < 0)
272 goto err_out_parse_error;
274 * it's only used for text acceleration
275 * so we just ignore it.
277 } else if (!strcmp(token[0], "hsync")) {
278 CHECK_IN_MODE_DEF;
279 if (get_token(1) < 0)
280 goto err_out_parse_error;
281 if (!strcmp(token[0], "low"))
282 mode->sync &= ~FB_SYNC_HOR_HIGH_ACT;
283 else if(!strcmp(token[0], "high"))
284 mode->sync |= FB_SYNC_HOR_HIGH_ACT;
285 else
286 goto err_out_parse_error;
287 } else if (!strcmp(token[0], "vsync")) {
288 CHECK_IN_MODE_DEF;
289 if (get_token(1) < 0)
290 goto err_out_parse_error;
291 if (!strcmp(token[0], "low"))
292 mode->sync &= ~FB_SYNC_VERT_HIGH_ACT;
293 else if(!strcmp(token[0], "high"))
294 mode->sync |= FB_SYNC_VERT_HIGH_ACT;
295 else
296 goto err_out_parse_error;
297 } else if (!strcmp(token[0], "csync")) {
298 CHECK_IN_MODE_DEF;
299 if (get_token(1) < 0)
300 goto err_out_parse_error;
301 if (!strcmp(token[0], "low"))
302 mode->sync &= ~FB_SYNC_COMP_HIGH_ACT;
303 else if(!strcmp(token[0], "high"))
304 mode->sync |= FB_SYNC_COMP_HIGH_ACT;
305 else
306 goto err_out_parse_error;
307 } else if (!strcmp(token[0], "extsync")) {
308 CHECK_IN_MODE_DEF;
309 if (get_token(1) < 0)
310 goto err_out_parse_error;
311 if (!strcmp(token[0], "false"))
312 mode->sync &= ~FB_SYNC_EXT;
313 else if(!strcmp(token[0], "true"))
314 mode->sync |= FB_SYNC_EXT;
315 else
316 goto err_out_parse_error;
317 } else if (!strcmp(token[0], "laced")) {
318 CHECK_IN_MODE_DEF;
319 if (get_token(1) < 0)
320 goto err_out_parse_error;
321 if (!strcmp(token[0], "false"))
322 mode->vmode = FB_VMODE_NONINTERLACED;
323 else if (!strcmp(token[0], "true"))
324 mode->vmode = FB_VMODE_INTERLACED;
325 else
326 goto err_out_parse_error;
327 } else if (!strcmp(token[0], "double")) {
328 CHECK_IN_MODE_DEF;
329 if (get_token(1) < 0)
330 goto err_out_parse_error;
331 if (!strcmp(token[0], "false"))
333 else if (!strcmp(token[0], "true"))
334 mode->vmode = FB_VMODE_DOUBLE;
335 else
336 goto err_out_parse_error;
337 } else
338 goto err_out_parse_error;
340 if (!validate_mode(mode))
341 goto err_out_not_valid;
342 out:
343 mp_msg(MSGT_VO, MSGL_V, "%d modes\n", nr_modes);
344 free(line);
345 fclose(fp);
346 return nr_modes;
347 err_out_parse_error:
348 mp_msg(MSGT_VO, MSGL_V, "parse error");
349 err_out_print_linenum:
350 mp_msg(MSGT_VO, MSGL_V, " at line %d\n", line_num);
351 err_out:
352 if (fb_modes) {
353 free(fb_modes);
354 fb_modes = NULL;
356 nr_modes = 0;
357 free(line);
358 free(fp);
359 return -2;
360 err_out_not_valid:
361 mp_msg(MSGT_VO, MSGL_V, "previous mode is not correct");
362 goto err_out_print_linenum;
365 static fb_mode_t *find_mode_by_name(char *name)
367 int i;
369 for (i = 0; i < nr_modes; i++)
370 if (!strcmp(name, fb_modes[i].name))
371 return fb_modes + i;
372 return NULL;
375 static float dcf(fb_mode_t *m) //driving clock frequency
377 return 1e12f / m->pixclock;
380 static float hsf(fb_mode_t *m) //horizontal scan frequency
382 int htotal = m->left + m->xres + m->right + m->hslen;
383 return dcf(m) / htotal;
386 static float vsf(fb_mode_t *m) //vertical scan frequency
388 int vtotal = m->upper + m->yres + m->lower + m->vslen;
389 return hsf(m) / vtotal;
393 static int mode_works(fb_mode_t *m, range_t *hfreq, range_t *vfreq,
394 range_t *dotclock)
396 float h = hsf(m);
397 float v = vsf(m);
398 float d = dcf(m);
399 int ret = 1;
401 mp_msg(MSGT_VO, MSGL_DBG2, "mode %dx%d:", m->xres, m->yres);
402 if (!in_range(hfreq, h)) {
403 ret = 0;
404 mp_msg(MSGT_VO, MSGL_DBG2, " hsync out of range.");
406 if (!in_range(vfreq, v)) {
407 ret = 0;
408 mp_msg(MSGT_VO, MSGL_DBG2, " vsync out of range.");
410 if (!in_range(dotclock, d)) {
411 ret = 0;
412 mp_msg(MSGT_VO, MSGL_DBG2, " dotclock out of range.");
414 if (ret)
415 mp_msg(MSGT_VO, MSGL_DBG2, " hsync, vsync, dotclock ok.\n");
416 else
417 mp_msg(MSGT_VO, MSGL_DBG2, "\n");
419 return ret;
422 static fb_mode_t *find_best_mode(int xres, int yres, range_t *hfreq,
423 range_t *vfreq, range_t *dotclock)
425 int i;
426 fb_mode_t *best = fb_modes;
427 fb_mode_t *curr;
429 mp_msg(MSGT_VO, MSGL_DBG2, "Searching for first working mode\n");
431 for (i = 0; i < nr_modes; i++, best++)
432 if (mode_works(best, hfreq, vfreq, dotclock))
433 break;
435 if (i == nr_modes)
436 return NULL;
437 if (i == nr_modes - 1)
438 return best;
440 mp_msg(MSGT_VO, MSGL_DBG2, "First working mode: %dx%d\n", best->xres, best->yres);
441 mp_msg(MSGT_VO, MSGL_DBG2, "Searching for better modes\n");
443 for (curr = best + 1; i < nr_modes - 1; i++, curr++) {
444 if (!mode_works(curr, hfreq, vfreq, dotclock))
445 continue;
447 if (best->xres < xres || best->yres < yres) {
448 if (curr->xres > best->xres || curr->yres > best->yres) {
449 mp_msg(MSGT_VO, MSGL_DBG2, "better than %dx%d, which is too small.\n",
450 best->xres, best->yres);
451 best = curr;
452 } else
453 mp_msg(MSGT_VO, MSGL_DBG2, "too small.\n");
454 } else if (curr->xres == best->xres && curr->yres == best->yres &&
455 vsf(curr) > vsf(best)) {
456 mp_msg(MSGT_VO, MSGL_DBG2, "faster screen refresh.\n");
457 best = curr;
458 } else if ((curr->xres <= best->xres && curr->yres <= best->yres) &&
459 (curr->xres >= xres && curr->yres >= yres)) {
460 mp_msg(MSGT_VO, MSGL_DBG2, "better than %dx%d, which is too large.\n",
461 best->xres, best->yres);
462 best = curr;
463 } else {
464 if (curr->xres < xres || curr->yres < yres)
465 mp_msg(MSGT_VO, MSGL_DBG2, "too small.\n");
466 else if (curr->xres > best->xres || curr->yres > best->yres)
467 mp_msg(MSGT_VO, MSGL_DBG2, "too large.\n");
468 else mp_msg(MSGT_VO, MSGL_DBG2, "it's worse, don't know why.\n");
472 return best;
475 static void set_bpp(struct fb_var_screeninfo *p, int bpp)
477 p->bits_per_pixel = (bpp + 1) & ~1;
478 p->red.msb_right = p->green.msb_right = p->blue.msb_right = p->transp.msb_right = 0;
479 p->transp.offset = p->transp.length = 0;
480 p->blue.offset = 0;
481 switch (bpp) {
482 case 32:
483 p->transp.offset = 24;
484 p->transp.length = 8;
485 case 24:
486 p->red.offset = 16;
487 p->red.length = 8;
488 p->green.offset = 8;
489 p->green.length = 8;
490 p->blue.length = 8;
491 break;
492 case 16:
493 p->red.offset = 11;
494 p->green.length = 6;
495 p->red.length = 5;
496 p->green.offset = 5;
497 p->blue.length = 5;
498 break;
499 case 15:
500 p->red.offset = 10;
501 p->green.length = 5;
502 p->red.length = 5;
503 p->green.offset = 5;
504 p->blue.length = 5;
505 break;
509 static void fb_mode2fb_vinfo(fb_mode_t *m, struct fb_var_screeninfo *v)
511 v->xres = m->xres;
512 v->yres = m->yres;
513 v->xres_virtual = m->vxres;
514 v->yres_virtual = m->vyres;
515 set_bpp(v, m->depth);
516 v->pixclock = m->pixclock;
517 v->left_margin = m->left;
518 v->right_margin = m->right;
519 v->upper_margin = m->upper;
520 v->lower_margin = m->lower;
521 v->hsync_len = m->hslen;
522 v->vsync_len = m->vslen;
523 v->sync = m->sync;
524 v->vmode = m->vmode;
528 /******************************
529 * vo_fbdev *
530 ******************************/
532 /* command line/config file options */
533 char *fb_dev_name = NULL;
534 char *fb_mode_cfgfile = NULL;
535 char *fb_mode_name = NULL;
537 static fb_mode_t *fb_mode = NULL;
539 /* vt related variables */
540 static FILE *vt_fp = NULL;
541 static int vt_doit = 1;
543 /* vo_fbdev related variables */
544 static int fb_dev_fd;
545 static int fb_tty_fd = -1;
546 static size_t fb_size;
547 static uint8_t *frame_buffer;
548 static uint8_t *center; /* thx .so :) */
549 static struct fb_fix_screeninfo fb_finfo;
550 static struct fb_var_screeninfo fb_orig_vinfo;
551 static struct fb_var_screeninfo fb_vinfo;
552 static unsigned short fb_ored[256], fb_ogreen[256], fb_oblue[256];
553 static struct fb_cmap fb_oldcmap = { 0, 256, fb_ored, fb_ogreen, fb_oblue };
554 static int fb_cmap_changed = 0;
555 static int fb_pixel_size; // 32: 4 24: 3 16: 2 15: 2
556 static int fb_bpp; // 32: 32 24: 24 16: 16 15: 15
557 static int fb_bpp_we_want; // 32: 32 24: 24 16: 16 15: 15
558 static int fb_line_len;
559 static int fb_xres;
560 static int fb_yres;
561 static void (*draw_alpha_p)(int w, int h, unsigned char *src,
562 unsigned char *srca, int stride, unsigned char *dst,
563 int dstride);
565 static int in_width;
566 static int in_height;
567 static int out_width;
568 static int out_height;
569 static int first_row;
570 static int last_row;
571 static uint32_t pixel_format;
572 static int fs;
575 * Note: this function is completely cut'n'pasted from
576 * Chris Lawrence's code.
577 * (modified a bit to fit in my code...)
579 static struct fb_cmap *make_directcolor_cmap(struct fb_var_screeninfo *var)
581 /* Hopefully any DIRECTCOLOR device will have a big enough palette
582 * to handle mapping the full color depth.
583 * e.g. 8 bpp -> 256 entry palette
585 * We could handle some sort of gamma here
587 int i, cols, rcols, gcols, bcols;
588 uint16_t *red, *green, *blue;
589 struct fb_cmap *cmap;
591 rcols = 1 << var->red.length;
592 gcols = 1 << var->green.length;
593 bcols = 1 << var->blue.length;
595 /* Make our palette the length of the deepest color */
596 cols = (rcols > gcols ? rcols : gcols);
597 cols = (cols > bcols ? cols : bcols);
599 red = malloc(cols * sizeof(red[0]));
600 if(!red) {
601 mp_msg(MSGT_VO, MSGL_V, "Can't allocate red palette with %d entries.\n", cols);
602 return NULL;
604 for(i=0; i< rcols; i++)
605 red[i] = (65535/(rcols-1)) * i;
607 green = malloc(cols * sizeof(green[0]));
608 if(!green) {
609 mp_msg(MSGT_VO, MSGL_V, "Can't allocate green palette with %d entries.\n", cols);
610 free(red);
611 return NULL;
613 for(i=0; i< gcols; i++)
614 green[i] = (65535/(gcols-1)) * i;
616 blue = malloc(cols * sizeof(blue[0]));
617 if(!blue) {
618 mp_msg(MSGT_VO, MSGL_V, "Can't allocate blue palette with %d entries.\n", cols);
619 free(red);
620 free(green);
621 return NULL;
623 for(i=0; i< bcols; i++)
624 blue[i] = (65535/(bcols-1)) * i;
626 cmap = malloc(sizeof(struct fb_cmap));
627 if(!cmap) {
628 mp_msg(MSGT_VO, MSGL_V, "Can't allocate color map\n");
629 free(red);
630 free(green);
631 free(blue);
632 return NULL;
634 cmap->start = 0;
635 cmap->transp = 0;
636 cmap->len = cols;
637 cmap->red = red;
638 cmap->blue = blue;
639 cmap->green = green;
640 cmap->transp = NULL;
642 return cmap;
646 static int fb_preinit(int reset)
648 static int fb_preinit_done = 0;
649 static int fb_works = 0;
651 if (reset)
653 fb_preinit_done = 0;
654 return 0;
657 if (fb_preinit_done)
658 return fb_works;
660 if (!fb_dev_name && !(fb_dev_name = getenv("FRAMEBUFFER")))
661 fb_dev_name = strdup("/dev/fb0");
662 mp_msg(MSGT_VO, MSGL_V, "using %s\n", fb_dev_name);
664 if ((fb_dev_fd = open(fb_dev_name, O_RDWR)) == -1) {
665 mp_msg(MSGT_VO, MSGL_ERR, "Can't open %s: %s\n", fb_dev_name, strerror(errno));
666 goto err_out;
668 if (ioctl(fb_dev_fd, FBIOGET_VSCREENINFO, &fb_vinfo)) {
669 mp_msg(MSGT_VO, MSGL_ERR, "Can't get VSCREENINFO: %s\n", strerror(errno));
670 goto err_out_fd;
672 fb_orig_vinfo = fb_vinfo;
674 if ((fb_tty_fd = open("/dev/tty", O_RDWR)) < 0) {
675 mp_msg(MSGT_VO, MSGL_ERR, "notice: Can't open /dev/tty: %s\n", strerror(errno));
678 fb_bpp = fb_vinfo.red.length + fb_vinfo.green.length +
679 fb_vinfo.blue.length + fb_vinfo.transp.length;
681 if (fb_bpp == 8 && !vo_dbpp) {
682 mp_msg(MSGT_VO, MSGL_ERR, "8 bpp output is not supported.\n");
683 goto err_out_tty_fd;
686 if (vo_dbpp) {
687 if (vo_dbpp != 15 && vo_dbpp != 16 && vo_dbpp != 24 &&
688 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 lots_of_printf(void)
715 mp_msg(MSGT_VO, MSGL_V, "var info:\n");
716 mp_msg(MSGT_VO, MSGL_V, "xres: %u\n", fb_vinfo.xres);
717 mp_msg(MSGT_VO, MSGL_V, "yres: %u\n", fb_vinfo.yres);
718 mp_msg(MSGT_VO, MSGL_V, "xres_virtual: %u\n", fb_vinfo.xres_virtual);
719 mp_msg(MSGT_VO, MSGL_V, "yres_virtual: %u\n", fb_vinfo.yres_virtual);
720 mp_msg(MSGT_VO, MSGL_V, "xoffset: %u\n", fb_vinfo.xoffset);
721 mp_msg(MSGT_VO, MSGL_V, "yoffset: %u\n", fb_vinfo.yoffset);
722 mp_msg(MSGT_VO, MSGL_V, "bits_per_pixel: %u\n", fb_vinfo.bits_per_pixel);
723 mp_msg(MSGT_VO, MSGL_V, "grayscale: %u\n", fb_vinfo.grayscale);
724 mp_msg(MSGT_VO, MSGL_V, "red: %lu %lu %lu\n",
725 (unsigned long) fb_vinfo.red.offset,
726 (unsigned long) fb_vinfo.red.length,
727 (unsigned long) fb_vinfo.red.msb_right);
728 mp_msg(MSGT_VO, MSGL_V, "green: %lu %lu %lu\n",
729 (unsigned long) fb_vinfo.green.offset,
730 (unsigned long) fb_vinfo.green.length,
731 (unsigned long) fb_vinfo.green.msb_right);
732 mp_msg(MSGT_VO, MSGL_V, "blue: %lu %lu %lu\n",
733 (unsigned long) fb_vinfo.blue.offset,
734 (unsigned long) fb_vinfo.blue.length,
735 (unsigned long) fb_vinfo.blue.msb_right);
736 mp_msg(MSGT_VO, MSGL_V, "transp: %lu %lu %lu\n",
737 (unsigned long) fb_vinfo.transp.offset,
738 (unsigned long) fb_vinfo.transp.length,
739 (unsigned long) fb_vinfo.transp.msb_right);
740 mp_msg(MSGT_VO, MSGL_V, "nonstd: %u\n", fb_vinfo.nonstd);
741 mp_msg(MSGT_VO, MSGL_DBG2, "activate: %u\n", fb_vinfo.activate);
742 mp_msg(MSGT_VO, MSGL_DBG2, "height: %u\n", fb_vinfo.height);
743 mp_msg(MSGT_VO, MSGL_DBG2, "width: %u\n", fb_vinfo.width);
744 mp_msg(MSGT_VO, MSGL_DBG2, "accel_flags: %u\n", fb_vinfo.accel_flags);
745 mp_msg(MSGT_VO, MSGL_DBG2, "timing:\n");
746 mp_msg(MSGT_VO, MSGL_DBG2, "pixclock: %u\n", fb_vinfo.pixclock);
747 mp_msg(MSGT_VO, MSGL_DBG2, "left_margin: %u\n", fb_vinfo.left_margin);
748 mp_msg(MSGT_VO, MSGL_DBG2, "right_margin: %u\n", fb_vinfo.right_margin);
749 mp_msg(MSGT_VO, MSGL_DBG2, "upper_margin: %u\n", fb_vinfo.upper_margin);
750 mp_msg(MSGT_VO, MSGL_DBG2, "lower_margin: %u\n", fb_vinfo.lower_margin);
751 mp_msg(MSGT_VO, MSGL_DBG2, "hsync_len: %u\n", fb_vinfo.hsync_len);
752 mp_msg(MSGT_VO, MSGL_DBG2, "vsync_len: %u\n", fb_vinfo.vsync_len);
753 mp_msg(MSGT_VO, MSGL_DBG2, "sync: %u\n", fb_vinfo.sync);
754 mp_msg(MSGT_VO, MSGL_DBG2, "vmode: %u\n", fb_vinfo.vmode);
755 mp_msg(MSGT_VO, MSGL_V, "fix info:\n");
756 mp_msg(MSGT_VO, MSGL_V, "framebuffer size: %d bytes\n", fb_finfo.smem_len);
757 mp_msg(MSGT_VO, MSGL_V, "type: %lu\n", (unsigned long) fb_finfo.type);
758 mp_msg(MSGT_VO, MSGL_V, "type_aux: %lu\n", (unsigned long) fb_finfo.type_aux);
759 mp_msg(MSGT_VO, MSGL_V, "visual: %lu\n", (unsigned long) fb_finfo.visual);
760 mp_msg(MSGT_VO, MSGL_V, "line_length: %lu bytes\n", (unsigned long) fb_finfo.line_length);
761 mp_msg(MSGT_VO, MSGL_DBG2, "id: %.16s\n", fb_finfo.id);
762 mp_msg(MSGT_VO, MSGL_DBG2, "smem_start: %p\n", (void *) fb_finfo.smem_start);
763 mp_msg(MSGT_VO, MSGL_DBG2, "xpanstep: %u\n", fb_finfo.xpanstep);
764 mp_msg(MSGT_VO, MSGL_DBG2, "ypanstep: %u\n", fb_finfo.ypanstep);
765 mp_msg(MSGT_VO, MSGL_DBG2, "ywrapstep: %u\n", fb_finfo.ywrapstep);
766 mp_msg(MSGT_VO, MSGL_DBG2, "mmio_start: %p\n", (void *) fb_finfo.mmio_start);
767 mp_msg(MSGT_VO, MSGL_DBG2, "mmio_len: %u bytes\n", fb_finfo.mmio_len);
768 mp_msg(MSGT_VO, MSGL_DBG2, "accel: %u\n", fb_finfo.accel);
769 mp_msg(MSGT_VO, MSGL_V, "fb_bpp: %d\n", fb_bpp);
770 mp_msg(MSGT_VO, MSGL_V, "fb_pixel_size: %d bytes\n", fb_pixel_size);
771 mp_msg(MSGT_VO, MSGL_V, "other:\n");
772 mp_msg(MSGT_VO, MSGL_V, "in_width: %d\n", in_width);
773 mp_msg(MSGT_VO, MSGL_V, "in_height: %d\n", in_height);
774 mp_msg(MSGT_VO, MSGL_V, "out_width: %d\n", out_width);
775 mp_msg(MSGT_VO, MSGL_V, "out_height: %d\n", out_height);
776 mp_msg(MSGT_VO, MSGL_V, "first_row: %d\n", first_row);
777 mp_msg(MSGT_VO, MSGL_V, "last_row: %d\n", last_row);
778 mp_msg(MSGT_VO, MSGL_DBG2, "draw_alpha_p:%dbpp = %p\n", fb_bpp, draw_alpha_p);
781 static void vt_set_textarea(int u, int l)
783 /* how can I determine the font height?
784 * just use 16 for now
786 int urow = ((u + 15) / 16) + 1;
787 int lrow = l / 16;
789 mp_msg(MSGT_VO, MSGL_DBG2, "vt_set_textarea(%d,%d): %d,%d\n", u, l, urow, lrow);
790 if(vt_fp) {
791 fprintf(vt_fp, "\33[%d;%dr\33[%d;%dH", urow, lrow, lrow, 0);
792 fflush(vt_fp);
796 static int config(uint32_t width, uint32_t height, uint32_t d_width,
797 uint32_t d_height, uint32_t flags, char *title,
798 uint32_t format)
800 struct fb_cmap *cmap;
801 int vm = flags & VOFLAG_MODESWITCHING;
802 int zoom = flags & VOFLAG_SWSCALE;
803 int vt_fd;
805 fs = flags & VOFLAG_FULLSCREEN;
807 if(pre_init_err == -2)
809 mp_msg(MSGT_VO, MSGL_ERR, "Internal fatal error: config() was called before preinit()\n");
810 return -1;
813 if (pre_init_err) return 1;
815 if (fb_mode_name && !vm) {
816 mp_msg(MSGT_VO, MSGL_ERR, "-fbmode can only be used with -vm\n");
817 return 1;
819 if (vm && (parse_fbmode_cfg(fb_mode_cfgfile) < 0))
820 return 1;
821 if (d_width && (fs || vm)) {
822 out_width = d_width;
823 out_height = d_height;
824 } else {
825 out_width = width;
826 out_height = height;
828 in_width = width;
829 in_height = height;
830 pixel_format = format;
832 if (fb_mode_name) {
833 if (!(fb_mode = find_mode_by_name(fb_mode_name))) {
834 mp_msg(MSGT_VO, MSGL_ERR, "can't find requested video mode\n");
835 return 1;
837 fb_mode2fb_vinfo(fb_mode, &fb_vinfo);
838 } else if (vm) {
839 monitor_hfreq = str2range(monitor_hfreq_str);
840 monitor_vfreq = str2range(monitor_vfreq_str);
841 monitor_dotclock = str2range(monitor_dotclock_str);
842 if (!monitor_hfreq || !monitor_vfreq || !monitor_dotclock) {
843 mp_msg(MSGT_VO, MSGL_ERR, "you have to specify the capabilities of"
844 " the monitor.\n");
845 return 1;
847 if (!(fb_mode = find_best_mode(out_width, out_height,
848 monitor_hfreq, monitor_vfreq,
849 monitor_dotclock))) {
850 mp_msg(MSGT_VO, MSGL_ERR, "can't find best video mode\n");
851 return 1;
853 mp_msg(MSGT_VO, MSGL_V, "using mode %dx%d @ %.1fHz\n", fb_mode->xres,
854 fb_mode->yres, vsf(fb_mode));
855 fb_mode2fb_vinfo(fb_mode, &fb_vinfo);
857 fb_bpp_we_want = fb_bpp;
858 set_bpp(&fb_vinfo, fb_bpp);
859 fb_vinfo.xres_virtual = fb_vinfo.xres;
860 fb_vinfo.yres_virtual = fb_vinfo.yres;
862 if (fb_tty_fd >= 0 && ioctl(fb_tty_fd, KDSETMODE, KD_GRAPHICS) < 0) {
863 mp_msg(MSGT_VO, MSGL_V, "Can't set graphics mode: %s\n", strerror(errno));
864 close(fb_tty_fd);
865 fb_tty_fd = -1;
868 if (ioctl(fb_dev_fd, FBIOPUT_VSCREENINFO, &fb_vinfo)) {
869 mp_msg(MSGT_VO, MSGL_ERR, "Can't put VSCREENINFO: %s\n", strerror(errno));
870 if (fb_tty_fd >= 0 && ioctl(fb_tty_fd, KDSETMODE, KD_TEXT) < 0) {
871 mp_msg(MSGT_VO, MSGL_ERR, "Can't restore text mode: %s\n", strerror(errno));
873 return 1;
876 fb_pixel_size = fb_vinfo.bits_per_pixel / 8;
877 fb_bpp = fb_vinfo.red.length + fb_vinfo.green.length +
878 fb_vinfo.blue.length + fb_vinfo.transp.length;
879 if (fb_bpp_we_want != fb_bpp)
880 mp_msg(MSGT_VO, MSGL_WARN, "requested %d bpp, got %d bpp!!!\n",
881 fb_bpp_we_want, fb_bpp);
883 switch (fb_bpp) {
884 case 32: draw_alpha_p = vo_draw_alpha_rgb32; break;
885 case 24: draw_alpha_p = vo_draw_alpha_rgb24; break;
886 case 16: draw_alpha_p = vo_draw_alpha_rgb16; break;
887 case 15: draw_alpha_p = vo_draw_alpha_rgb15; break;
888 default: return 1;
891 fb_xres = fb_vinfo.xres;
892 fb_yres = fb_vinfo.yres;
894 if (vm || fs) {
895 out_width = fb_xres;
896 out_height = fb_yres;
898 if (out_width < in_width || out_height < in_height) {
899 mp_msg(MSGT_VO, MSGL_ERR, "screensize is smaller than video size\n");
900 return 1;
903 first_row = (out_height - in_height) / 2;
904 last_row = (out_height + in_height) / 2;
906 if (ioctl(fb_dev_fd, FBIOGET_FSCREENINFO, &fb_finfo)) {
907 mp_msg(MSGT_VO, MSGL_ERR, "Can't get FSCREENINFO: %s\n", strerror(errno));
908 return 1;
911 lots_of_printf();
913 if (fb_finfo.type != FB_TYPE_PACKED_PIXELS) {
914 mp_msg(MSGT_VO, MSGL_ERR, "type %d not supported\n", fb_finfo.type);
915 return 1;
918 switch (fb_finfo.visual) {
919 case FB_VISUAL_TRUECOLOR:
920 break;
921 case FB_VISUAL_DIRECTCOLOR:
922 mp_msg(MSGT_VO, MSGL_V, "creating cmap for directcolor\n");
923 if (ioctl(fb_dev_fd, FBIOGETCMAP, &fb_oldcmap)) {
924 mp_msg(MSGT_VO, MSGL_ERR, "can't get cmap: %s\n",
925 strerror(errno));
926 return 1;
928 if (!(cmap = make_directcolor_cmap(&fb_vinfo)))
929 return 1;
930 if (ioctl(fb_dev_fd, FBIOPUTCMAP, cmap)) {
931 mp_msg(MSGT_VO, MSGL_ERR, "can't put cmap: %s\n",
932 strerror(errno));
933 return 1;
935 fb_cmap_changed = 1;
936 free(cmap->red);
937 free(cmap->green);
938 free(cmap->blue);
939 free(cmap);
940 break;
941 default:
942 mp_msg(MSGT_VO, MSGL_ERR, "visual: %d not yet supported\n",
943 fb_finfo.visual);
944 return 1;
947 fb_line_len = fb_finfo.line_length;
948 fb_size = fb_finfo.smem_len;
949 frame_buffer = NULL;
950 #ifdef CONFIG_VIDIX
951 if(vidix_name)
953 unsigned image_width,image_height,x_offset,y_offset;
954 if(zoom || fs){
955 aspect_save_orig(width,height);
956 aspect_save_prescale(d_width,d_height);
957 aspect_save_screenres(fb_xres,fb_yres);
958 aspect(&image_width,&image_height,fs ? A_ZOOM : A_NOZOOM);
959 } else {
960 image_width=width;
961 image_height=height;
964 if(fb_xres > image_width)
965 x_offset = (fb_xres - image_width) / 2;
966 else x_offset = 0;
967 if(fb_yres > image_height)
968 y_offset = (fb_yres - image_height) / 2;
969 else y_offset = 0;
971 if(vidix_init(width,height,x_offset,y_offset,image_width,
972 image_height,format,fb_bpp,
973 fb_xres,fb_yres) != 0)
975 mp_msg(MSGT_VO, MSGL_ERR, "Can't initialize VIDIX driver\n");
976 vidix_name = NULL;
977 vidix_term();
978 return -1;
980 else mp_msg(MSGT_VO, MSGL_V, "Using VIDIX\n");
981 vidix_start();
982 if (vidix_grkey_support())
984 vidix_grkey_get(&gr_key);
985 gr_key.key_op = KEYS_PUT;
986 if (!(vo_colorkey & 0xff000000))
988 gr_key.ckey.op = CKEY_TRUE;
989 gr_key.ckey.red = (vo_colorkey & 0x00ff0000) >> 16;
990 gr_key.ckey.green = (vo_colorkey & 0x0000ff00) >> 8;
991 gr_key.ckey.blue = vo_colorkey & 0x000000ff;
993 else
994 gr_key.ckey.op = CKEY_FALSE;
995 vidix_grkey_set(&gr_key);
998 else
999 #endif
1001 int x_offset=0,y_offset=0;
1002 if ((frame_buffer = (uint8_t *) mmap(0, fb_size, PROT_READ | PROT_WRITE,
1003 MAP_SHARED, fb_dev_fd, 0)) == (uint8_t *) -1) {
1004 mp_msg(MSGT_VO, MSGL_ERR, "Can't mmap %s: %s\n", fb_dev_name, strerror(errno));
1005 return 1;
1008 center = frame_buffer +
1009 ( (out_width - in_width) / 2 ) * fb_pixel_size +
1010 ( (out_height - in_height) / 2 ) * fb_line_len +
1011 x_offset * fb_pixel_size + y_offset * fb_line_len;
1013 mp_msg(MSGT_VO, MSGL_DBG2, "frame_buffer @ %p\n", frame_buffer);
1014 mp_msg(MSGT_VO, MSGL_DBG2, "center @ %p\n", center);
1015 mp_msg(MSGT_VO, MSGL_V, "pixel per line: %d\n", fb_line_len / fb_pixel_size);
1017 if (fs || vm)
1018 memset(frame_buffer, '\0', fb_line_len * fb_yres);
1020 if (vt_doit && (vt_fd = open("/dev/tty", O_WRONLY)) == -1) {
1021 mp_msg(MSGT_VO, MSGL_ERR, "can't open /dev/tty: %s\n", strerror(errno));
1022 vt_doit = 0;
1024 if (vt_doit && !(vt_fp = fdopen(vt_fd, "w"))) {
1025 mp_msg(MSGT_VO, MSGL_ERR, "can't fdopen /dev/tty: %s\n", strerror(errno));
1026 vt_doit = 0;
1029 if (vt_doit)
1030 vt_set_textarea(last_row, fb_yres);
1032 return 0;
1035 static int query_format(uint32_t format)
1037 if (!fb_preinit(0))
1038 return 0;
1039 #ifdef CONFIG_VIDIX
1040 if(vidix_name)
1041 return (vidix_query_fourcc(format));
1042 #endif
1043 if ((format & IMGFMT_BGR_MASK) == IMGFMT_BGR) {
1044 int bpp = format & 0xff;
1046 if (bpp == fb_bpp)
1047 return VFCAP_ACCEPT_STRIDE | VFCAP_CSP_SUPPORTED | VFCAP_CSP_SUPPORTED_BY_HW;
1049 return 0;
1052 static void draw_alpha(int x0, int y0, int w, int h, unsigned char *src,
1053 unsigned char *srca, int stride)
1055 unsigned char *dst;
1057 dst = center + fb_line_len * y0 + fb_pixel_size * x0;
1059 (*draw_alpha_p)(w, h, src, srca, stride, dst, fb_line_len);
1062 static int draw_frame(uint8_t *src[]) { return 1; }
1064 static int draw_slice(uint8_t *src[], int stride[], int w, int h, int x,
1065 int y)
1067 uint8_t *d;
1068 uint8_t *s;
1070 d = center + fb_line_len * y + fb_pixel_size * x;
1072 s = src[0];
1073 while (h) {
1074 fast_memcpy(d, s, w * fb_pixel_size);
1075 d += fb_line_len;
1076 s += stride[0];
1077 h--;
1080 return 0;
1083 static void check_events(void)
1087 static void flip_page(void)
1091 static void draw_osd(void)
1093 vo_draw_text(in_width, in_height, draw_alpha);
1096 static void uninit(void)
1098 if (fb_cmap_changed) {
1099 if (ioctl(fb_dev_fd, FBIOPUTCMAP, &fb_oldcmap))
1100 mp_msg(MSGT_VO, MSGL_WARN, "Can't restore original cmap\n");
1101 fb_cmap_changed = 0;
1103 if (ioctl(fb_dev_fd, FBIOGET_VSCREENINFO, &fb_vinfo))
1104 mp_msg(MSGT_VO, MSGL_WARN, "ioctl FBIOGET_VSCREENINFO: %s\n", strerror(errno));
1105 fb_orig_vinfo.xoffset = fb_vinfo.xoffset;
1106 fb_orig_vinfo.yoffset = fb_vinfo.yoffset;
1107 if (ioctl(fb_dev_fd, FBIOPUT_VSCREENINFO, &fb_orig_vinfo))
1108 mp_msg(MSGT_VO, MSGL_WARN, "Can't reset original fb_var_screeninfo: %s\n", strerror(errno));
1109 if (fb_tty_fd >= 0) {
1110 if (ioctl(fb_tty_fd, KDSETMODE, KD_TEXT) < 0)
1111 mp_msg(MSGT_VO, MSGL_WARN, "Can't restore text mode: %s\n", strerror(errno));
1113 if (vt_doit)
1114 vt_set_textarea(0, fb_orig_vinfo.yres);
1115 close(fb_tty_fd);
1116 close(fb_dev_fd);
1117 if(frame_buffer) munmap(frame_buffer, fb_size);
1118 frame_buffer = NULL;
1119 #ifdef CONFIG_VIDIX
1120 if(vidix_name) vidix_term();
1121 #endif
1122 fb_preinit(1);
1125 static int preinit(const char *vo_subdevice)
1127 pre_init_err = 0;
1129 if(vo_subdevice)
1131 #ifdef CONFIG_VIDIX
1132 if (memcmp(vo_subdevice, "vidix", 5) == 0)
1133 vidix_name = &vo_subdevice[5];
1134 if(vidix_name)
1135 pre_init_err = vidix_preinit(vidix_name,&video_out_fbdev);
1136 else
1137 #endif
1139 if (fb_dev_name) free(fb_dev_name);
1140 fb_dev_name = strdup(vo_subdevice);
1143 if(!pre_init_err) return (pre_init_err=(fb_preinit(0)?0:-1));
1144 return(-1);
1147 static uint32_t get_image(mp_image_t *mpi)
1149 if (
1150 !IMGFMT_IS_BGR(mpi->imgfmt) ||
1151 (IMGFMT_BGR_DEPTH(mpi->imgfmt) != fb_bpp) ||
1152 ((mpi->type != MP_IMGTYPE_STATIC) && (mpi->type != MP_IMGTYPE_TEMP)) ||
1153 (mpi->flags & MP_IMGFLAG_PLANAR) ||
1154 (mpi->flags & MP_IMGFLAG_YUV) ||
1155 (mpi->width != in_width) ||
1156 (mpi->height != in_height)
1158 return(VO_FALSE);
1160 mpi->planes[0] = center;
1161 mpi->stride[0] = fb_line_len;
1162 mpi->flags |= MP_IMGFLAG_DIRECT;
1163 return(VO_TRUE);
1166 static int control(uint32_t request, void *data, ...)
1168 switch (request) {
1169 case VOCTRL_GET_IMAGE:
1170 return get_image(data);
1171 case VOCTRL_QUERY_FORMAT:
1172 return query_format(*((uint32_t*)data));
1175 #ifdef CONFIG_VIDIX
1176 if (vidix_name) {
1177 switch (request) {
1178 case VOCTRL_SET_EQUALIZER:
1180 va_list ap;
1181 int value;
1183 va_start(ap, data);
1184 value = va_arg(ap, int);
1185 va_end(ap);
1187 return vidix_control(request, data, value);
1189 case VOCTRL_GET_EQUALIZER:
1191 va_list ap;
1192 int *value;
1194 va_start(ap, data);
1195 value = va_arg(ap, int*);
1196 va_end(ap);
1198 return vidix_control(request, data, value);
1202 #endif
1204 return VO_NOTIMPL;