~mil/fbvncm

1212b611f529988e3a312e0e576a8eb239eae200 — Ali Gholami Rudi 1 year, 1 month ago 0903ceb
draw: read FBDEV environment variable
3 files changed, 38 insertions(+), 32 deletions(-)

M draw.c
M draw.h
M fbvnc.c
M draw.c => draw.c +25 -22
@@ 2,23 2,24 @@
#include <linux/fb.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <unistd.h>
#include <string.h>
#include "draw.h"

#define MIN(a, b)	((a) < (b) ? (a) : (b))
#define MAX(a, b)	((a) > (b) ? (a) : (b))
#define NLEVELS		(1 << 8)

static int fd;
static void *fb;
static struct fb_var_screeninfo vinfo;
static struct fb_fix_screeninfo finfo;
static int bpp;
static int nr, ng, nb;
static int rl, rr, gl, gr, bl, br;	/* fb_color() shifts */
static struct fb_var_screeninfo vinfo;	/* linux-specific FB structure */
static struct fb_fix_screeninfo finfo;	/* linux-specific FB structure */
static int fd;				/* FB device file descriptor */
static void *fb;			/* mmap()ed FB memory */
static int bpp;				/* bytes per pixel */
static int nr, ng, nb;			/* color levels */
static int rl, rr, gl, gr, bl, br;	/* shifts per color */
static int xres, yres, xoff, yoff;	/* drawing region */

static int fb_len(void)
{


@@ 67,7 68,8 @@ void fb_cmap(void)

unsigned fb_mode(void)
{
	return (bpp << 16) | (vinfo.red.length << 8) |
	return ((rl < gl) << 22) | ((rl < bl) << 21) | ((gl < bl) << 20) |
		(bpp << 16) | (vinfo.red.length << 8) |
		(vinfo.green.length << 4) | (vinfo.blue.length);
}



@@ 84,14 86,20 @@ static void init_colors(void)
	bl = vinfo.blue.offset;
}

int fb_init(void)
int fb_init(char *dev)
{
	fd = open(FBDEV_PATH, O_RDWR);
	if (fd == -1)
	char *path = dev ? dev : FBDEV;
	char *geom = dev ? strchr(dev, ':') : NULL;
	if (geom) {
		*geom = '\0';
		sscanf(geom + 1, "%dx%d%d%d", &xres, &yres, &xoff, &yoff);
	}
	fd = open(path, O_RDWR);
	if (fd < 0)
		goto failed;
	if (ioctl(fd, FBIOGET_VSCREENINFO, &vinfo) == -1)
	if (ioctl(fd, FBIOGET_VSCREENINFO, &vinfo) < 0)
		goto failed;
	if (ioctl(fd, FBIOGET_FSCREENINFO, &finfo) == -1)
	if (ioctl(fd, FBIOGET_FSCREENINFO, &finfo) < 0)
		goto failed;
	fcntl(fd, F_SETFD, fcntl(fd, F_GETFD) | FD_CLOEXEC);
	bpp = (vinfo.bits_per_pixel + 7) >> 3;


@@ 117,22 125,17 @@ void fb_free(void)

int fb_rows(void)
{
	return vinfo.yres;
	return yres ? yres : vinfo.yres;
}

int fb_cols(void)
{
	return vinfo.xres;
	return xres ? xres : vinfo.xres;
}

void *fb_mem(int r)
{
	return fb + (r + vinfo.yoffset) * finfo.line_length;
}

void fb_set(int r, int c, void *mem, int len)
{
	memcpy(fb_mem(r) + (c + vinfo.xoffset) * bpp, mem, len * bpp);
	return fb + (r + vinfo.yoffset + yoff) * finfo.line_length + (vinfo.xoffset + xoff) * bpp;
}

unsigned fb_val(int r, int g, int b)

M draw.h => draw.h +6 -8
@@ 1,19 1,17 @@
/* framebuffer device */
#define FBDEV_PATH	"/dev/fb0"
/* fbpad's framebuffer interface */
#define FBDEV		"/dev/fb0"

/* fb_mode() interpretation */
#define FBM_BPP(m)	(((m) >> 16) & 0x0f)
#define FBM_COLORS(m)	((m) & 0x0fff)
#define FBM_BPP(m)	(((m) >> 16) & 0x0f)	/* bytes per pixel (4 bits) */
#define FBM_CLR(m)	((m) & 0x0fff)		/* bits per color (12 bits) */
#define FBM_ORD(m)	(((m) >> 20) & 0x07)	/* color order (3 bits) */

/* main functions */
int fb_init(void);
int fb_init(char *dev);
void fb_free(void);
unsigned fb_mode(void);
void *fb_mem(int r);
int fb_rows(void);
int fb_cols(void);
void fb_cmap(void);

/* helper functions */
void fb_set(int r, int c, void *mem, int len);
unsigned fb_val(int r, int g, int b);

M fbvnc.c => fbvnc.c +7 -2
@@ 80,7 80,7 @@ static int vnc_connect(char *addr, char *port)

static void fbmode_bits(int *rr, int *rg, int *rb)
{
	int mode = FBM_COLORS(fb_mode());
	int mode = FBM_CLR(fb_mode());
	*rr = (mode >> 8) & 0xf;
	*rg = (mode >> 4) & 0xf;
	*rb = (mode >> 0) & 0xf;


@@ 137,7 137,7 @@ static int vnc_init(int fd)
	srv_rows = ntohs(serverinit.h);

	/* set up the framebuffer */
	if (fb_init())
	if (fb_init(getenv("FBDEV")))
		return -1;
	cols = MIN(srv_cols, fb_cols());
	rows = MIN(srv_rows, fb_rows());


@@ 189,6 189,11 @@ static int vnc_refresh(int fd, int inc)
	return vwrite(fd, &fbup_req, sizeof(fbup_req)) < 0 ? -1 : 0;
}

static void fb_set(int r, int c, void *mem, int len)
{
	memcpy(fb_mem(r) + c * bpp, mem, len * bpp);
}

static void drawfb(char *s, int x, int y, int w, int h)
{
	int sc;		/* screen column offset */