#include #include #include #include #include #include #include typedef unsigned char uchar; int dontbexit; int bitbltfd; Bitmap screen; Font *font; static uchar bbuf[8000]; uchar _btmp[8000]; static uchar *bbufp = bbuf; static void (*onerr)(char*); static char oldlabel[128+1]; static char *label; uchar* bneed(int n) { if(n < 0) berror("negative count in bneed"); if(n==0 || bbufp-bbuf>sizeof bbuf-n){ if(!bwrite()) berror("write to /dev/bitblt"); if(n > sizeof bbuf) berror("write to /dev/bitblt too big"); } bbufp += n; return bbufp-n; } void bflush(void) { bneed(0); } int bwrite(void) { int r; if(bbufp == bbuf) return 1; r = (write(bitbltfd, (char *)bbuf, bbufp-bbuf) == bbufp-bbuf); bbufp = bbuf; return r; } void binit(void (*f)(char *), char *s, char *nlabel) { uchar *buf, *p; char fontname[128]; int fd, n, m, j; Fontchar *info; Subfont *subfont; if(s == 0){ fd = open("/env/font", O_RDONLY); if(fd >= 0){ n = read(fd, fontname, sizeof(fontname)); if(n > 0){ fontname[n] = 0; s = fontname; } close(fd); } } onerr = f; bitbltfd = open("/dev/bitblt", O_RDWR); if(bitbltfd < 0) berror("open /dev/bitblt"); if(write(bitbltfd, "i", 1) != 1) berror("binit write /dev/bitblt"); n = 18+16; m = 18+16; if(s == 0){ m += 3*12; n += 3*12+1300*6; /* 1300 charinfos: big enough? */ } buf = malloc(n); if(buf == 0){ free(buf); berror("binit alloc"); } j = read(bitbltfd, (char *)buf, n); if(jn = n; subfont->height = atoi((char*)buf+18+16+12); subfont->ascent = atoi((char*)buf+18+16+24); subfont->info = info; subfont->id = 0; font = mkfont(subfont, 0); if(font == 0) goto Err; } free(buf); label = nlabel; if(label){ fd = open("/dev/label", O_RDONLY); if(fd >= 0){ read(fd, oldlabel, sizeof oldlabel-1); close(fd); fd = creat("/dev/label", 0666); if(fd >= 0){ n = strlen(label); if(n > sizeof(oldlabel)-1) n = sizeof(oldlabel)-1; write(fd, label, n); close(fd); } } } atexit(bexit); } void bclose(void) { close(bitbltfd); dontbexit = 1; } void bexit(void) { int fd; if(dontbexit) return; bflush(); if(label){ fd = creat("/dev/label", 0666); if(fd >= 0){ write(fd, oldlabel, strlen(oldlabel)); close(fd); } } } void berror(char *s) { char errbuf[150]; if(onerr) (*onerr)(s); else{ sprintf(errbuf, "bitblt: %s", s); perror(errbuf); exit(1); } }