7 #define SAME_BPP(a, b) \
8 ((a) == (b) || ((a) == 16 && (b) == 15) || ((a) == 15 && (b) == 16) || \
9 ((a) == 32 && (b) == 24) || ((a) == 24 && (b) == 32))
11 static uint32_t calc_mask(int sz, int pos);
13 static struct video_mode *vmodes;
14 static int num_vmodes;
16 static int vbe_init_ver;
17 static struct vbe_info vbe;
20 static struct video_mode *curmode;
21 static void *vpgaddr[2];
23 static int pgcount, pgsize, fbsize;
27 int i, num, max_modes;
28 struct video_mode *vmptr;
30 if(vbe_info(&vbe) == -1) {
31 fprintf(stderr, "failed to retrieve VBE information\n");
34 vbe_print_info(stdout, &vbe);
37 max_modes = 4; /* TODO change */
38 if(!(vmodes = malloc(max_modes * sizeof *vmodes))) {
39 fprintf(stderr, "failed to allocate video modes list\n");
43 num = vbe_num_modes(&vbe);
44 for(i=0; i<num; i++) {
45 struct vbe_mode_info minf;
47 if(vbe_mode_info(vbe.modes[i], &minf) == -1) {
51 if(num_vmodes >= max_modes) {
52 int newmax = max_modes ? (max_modes << 1) : 16;
53 if(!(vmptr = realloc(vmodes, newmax * sizeof *vmodes))) {
54 fprintf(stderr, "failed to grow video mode list (%d)\n", newmax);
62 vmptr = vmodes + num_vmodes++;
63 memset(vmptr, 0, sizeof *vmptr);
64 vmptr->mode = vbe.modes[i];
65 vmptr->xsz = minf.xres;
66 vmptr->ysz = minf.yres;
67 vmptr->bpp = minf.bpp;
68 vmptr->pitch = minf.scanline_bytes;
69 if(minf.mem_model == VBE_TYPE_DIRECT) {
70 vmptr->rbits = minf.rsize;
71 vmptr->gbits = minf.gsize;
72 vmptr->bbits = minf.bsize;
73 vmptr->rshift = minf.rpos;
74 vmptr->gshift = minf.gpos;
75 vmptr->bshift = minf.bpos;
76 vmptr->rmask = calc_mask(minf.rsize, minf.rpos);
77 vmptr->gmask = calc_mask(minf.gsize, minf.gpos);
78 vmptr->bmask = calc_mask(minf.bsize, minf.bpos);
80 vmptr->fb_addr = minf.fb_addr;
81 vmptr->max_pages = minf.num_img_pages;
83 printf("%04x: ", vbe.modes[i]);
84 vbe_print_mode_info(stdout, &minf);
88 vbe_init_ver = VBE_VER_MAJOR(vbe.ver);
89 if(vbe_init_ver < 2) {
90 fprintf(stderr, "VBE 2.0 required. Upgrade your video card or run univbe.\n");
98 void cleanup_video(void)
103 struct video_mode *video_modes(void)
108 int num_video_modes(void)
113 int match_video_mode(int xsz, int ysz, int bpp)
116 struct video_mode *vm;
118 for(i=0; i<num_vmodes; i++) {
120 if(vm->xsz != xsz || vm->ysz != ysz) continue;
121 if(SAME_BPP(vm->bpp, bpp)) {
124 if(vm->bpp == bpp) break;
128 fprintf(stderr, "failed to find video mode %dx%d %d bpp)\n", xsz, ysz, bpp);
134 int find_video_mode(int mode)
137 struct video_mode *vm;
140 for(i=0; i<num_vmodes; i++) {
141 if(vm->mode == mode) return i;
146 void *set_video_mode(int idx, int nbuf)
149 struct video_mode *vm = vmodes + idx;
151 printf("setting video mode %x (%dx%d %d bpp)\n", (unsigned int)vm->mode,
152 vm->xsz, vm->ysz, vm->bpp);
155 mode = vm->mode | VBE_MODE_LFB;
156 if(vbe_setmode(mode) == -1) {
158 if(vbe_setmode(mode) == -1) {
159 fprintf(stderr, "failed to set video mode %x\n", (unsigned int)vm->mode);
162 printf("Warning: failed to get a linear framebuffer. falling back to banked mode\n");
166 if(nbuf < 1) nbuf = 1;
167 if(nbuf > 2) nbuf = 2;
168 pgcount = nbuf > vm->max_pages ? vm->max_pages : nbuf;
169 pgsize = (vm->xsz * vm->bpp / 8) * vm->ysz;
170 fbsize = pgcount * pgsize;
172 printf("pgcount: %d, pgsize: %d, fbsize: %d\n", pgcount, pgsize, fbsize);
173 printf("phys addr: %p\n", (void*)vm->fb_addr);
175 vpgaddr[0] = (void*)dpmi_mmap(vm->fb_addr, fbsize);
177 fprintf(stderr, "failed to map framebuffer (phys: %lx, size: %d)\n",
178 (unsigned long)vm->fb_addr, fbsize);
182 //memset(vpgaddr[0], 0xaa, fbsize);
184 printf("vaddr: %p\n", vpgaddr[0]);
188 vpgaddr[1] = (char*)vpgaddr[0] + pgsize;
190 page_flip(FLIP_NOW); /* start with the second page visible */
198 int set_text_mode(void)
205 void *page_flip(int vsync)
208 /* page flipping not supported */
212 vbe_swap(fbidx ? pgsize : 0, vsync ? VBE_SWAP_VBLANK : VBE_SWAP_NOW);
213 fbidx = (fbidx + 1) & 1;
215 return vpgaddr[fbidx];
218 static uint32_t calc_mask(int sz, int pos)
223 mask = (mask << 1) | 1;