vbeinfo

diff src/main.c @ 0:4b33fa83e381

vbeinfo initial commit
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 05 Dec 2015 07:28:47 +0200
parents
children d2d777a5da95
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/main.c	Sat Dec 05 07:28:47 2015 +0200
     1.3 @@ -0,0 +1,96 @@
     1.4 +#include <stdio.h>
     1.5 +#include <stdlib.h>
     1.6 +#include "vbe.h"
     1.7 +
     1.8 +#define REALPTR(s, o)	(void*)(((uint32_t)(s) << 4) + (uint32_t)(o))
     1.9 +#define VBEPTR(x)		REALPTR(((x) & 0xffff0000) >> 16, (x) & 0xffff)
    1.10 +#define VBEPTR_SEG(x)	(((x) & 0xffff0000) >> 16)
    1.11 +#define VBEPTR_OFF(x)	((x) & 0xffff)
    1.12 +
    1.13 +struct video_mode {
    1.14 +	int xres, yres, bpp;
    1.15 +};
    1.16 +
    1.17 +void sort_modes(struct video_mode *arr, int sz, int field);
    1.18 +/*int modecmp(const void *pa, const void *pb);*/
    1.19 +
    1.20 +int main(int argc, char **argv)
    1.21 +{
    1.22 +	int i, nmodes;
    1.23 +	struct vbe_info *vbe;
    1.24 +	struct vbe_mode_info *mode;
    1.25 +	uint16_t *modes;
    1.26 +	struct video_mode *vmodes;
    1.27 +
    1.28 +	if(!(vbe = vbe_get_info())) {
    1.29 +		fprintf(stderr, "VBE not found\n");
    1.30 +		return 1;
    1.31 +	}
    1.32 +	printf("VBE version: %x.%x\n", vbe->version >> 8, vbe->version & 0xff);
    1.33 +	printf("Graphics adapter: %s, %s (%s)\n", VBEPTR(vbe->oem_vendor_name_ptr),
    1.34 +				VBEPTR(vbe->oem_product_name_ptr), VBEPTR(vbe->oem_product_rev_ptr));
    1.35 +	printf("Video memory: %dmb\n", vbe->total_mem << 6);
    1.36 +
    1.37 +	modes = VBEPTR(vbe->vid_mode_ptr);
    1.38 +	nmodes = 0;
    1.39 +	for(i=0; i<1024; i++) {
    1.40 +		if(modes[i] == 0xffff) break;
    1.41 +		nmodes++;
    1.42 +	}
    1.43 +	printf("%d video modes found:\n", nmodes);
    1.44 +
    1.45 +	if(!(vmodes = malloc(nmodes * sizeof *vmodes))) {
    1.46 +		fprintf(stderr, "failed to allocate video modes array\n");
    1.47 +		return 1;
    1.48 +	}
    1.49 +
    1.50 +	for(i=0; i<nmodes; i++) {
    1.51 +		if(!(mode = vbe_get_mode_info(modes[i]))) {
    1.52 +			fprintf(stderr, "failed to get mode %d info\n", i);
    1.53 +		}
    1.54 +		vmodes[i].xres = mode->xres;
    1.55 +		vmodes[i].yres = mode->yres;
    1.56 +		vmodes[i].bpp = mode->bpp;
    1.57 +	}
    1.58 +
    1.59 +	/*qsort(vmodes, nmodes, sizeof *vmodes, modecmp);*/
    1.60 +	sort_modes(vmodes, nmodes, 2);
    1.61 +	sort_modes(vmodes, nmodes, 1);
    1.62 +
    1.63 +	for(i=0; i<nmodes; i++) {
    1.64 +		if(i == 0 || vmodes[i].xres != vmodes[i - 1].xres ||
    1.65 +				vmodes[i].yres != vmodes[i - 1].yres) {
    1.66 +			printf("\n%4dx%d - depth:", vmodes[i].xres, vmodes[i].yres);
    1.67 +		}
    1.68 +		printf(" %d", vmodes[i].bpp);
    1.69 +	}
    1.70 +	putchar('\n');
    1.71 +
    1.72 +	free(vmodes);
    1.73 +	return 0;
    1.74 +}
    1.75 +/*
    1.76 +int modecmp(const void *pa, const void *pb)
    1.77 +{
    1.78 +	const struct video_mode *va = pa;
    1.79 +	const struct video_mode *vb = pb;
    1.80 +
    1.81 +	return va->xres * va->yres < vb->xres * vb->yres;
    1.82 +}
    1.83 +*/
    1.84 +
    1.85 +void sort_modes(struct video_mode *arr, int sz, int field)
    1.86 +{
    1.87 +	int i, j;
    1.88 +	struct video_mode tmp;
    1.89 +
    1.90 +	for(i=0; i<sz; i++) {
    1.91 +		for(j=i+1; j<sz; j++) {
    1.92 +			if(((int*)(arr + i))[field] >= ((int*)(arr + j))[field]) {
    1.93 +				tmp = arr[i];
    1.94 +				arr[i] = arr[j];
    1.95 +				arr[j] = tmp;
    1.96 +			}
    1.97 +		}
    1.98 +	}
    1.99 +}