#include #include int bitbltfd; Bitmap screen; Font *font; intern char bbuf[8192]; char _btmp[8192]; intern char *bbufp = bbuf; intern void (*onerr)(char*); intern char oldlabel[128+1]; intern char *label; char* bneed(int n) { if(!bwrite()) berror("write to /dev/bitblt"); 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, bbuf, bbufp-bbuf) == bbufp-bbuf); bbufp = bbuf; return r; } int Rconv(Printspec *ps) { Rectangle *r; char buf[128]; r = ps->o; sprint(buf, "%P %P", r->min, r->max); strconv(ps, buf); return sizeof(Rectangle); } int Pconv(Printspec *ps) { Point *p; char buf[64]; p = ps->o; sprint(buf, "[%ld %ld]", p->x, p->y); strconv(ps, buf); return sizeof(Point); } void binit(void (*f)(char *), char *s, char *nlabel) { char *buf, *p; char fontname[128]; int fd, n, m, j; Fontchar *info; Subfont *subfont; if(s == nil){ fd = open("/env/font", OREAD); if(fd >= 0){ n = read(fd, fontname, sizeof(fontname)); if(n > 0){ fontname[n] = 0; s = fontname; } close(fd); } } onerr = f; if(access("/dev/bitblt", 0) < 0) bind("#b", "/dev", MAFTER); bitbltfd = open("/dev/bitblt", 2); 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 == nil){ m += 3*12; n += 3*12+1300*6; /* 1300 charinfos: big enough */ } buf = malloc(n); if(buf == nil){ free(buf); berror("binit alloc"); } j = read(bitbltfd, 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 == nil) goto Err; } free(buf); label = nlabel; if(label){ fd = open("/dev/label", OREAD); if(fd >= 0){ read(fd, oldlabel, sizeof oldlabel-1); close(fd); fd = create("/dev/label", OWRITE, 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) { /* bwrite(); */ close(bitbltfd); atexitdont(bexit); } void bexit(void) { int fd; bflush(); if(label){ fd = open("/dev/label", OWRITE); if(fd >= 0){ write(fd, oldlabel, strlen(oldlabel)); close(fd); } } } void berror(char *s) { char err[ERRLEN]; if(onerr) (*onerr)(s); else{ errstr(err); fprint(2, "bitblt: %s: %s\n", s, err); exits(s); } }