get linux font reprogramming working once more

pull/1813/head
nick black 3 years ago committed by Nick Black
parent fc62956bf7
commit 1f67b13370

@ -7,13 +7,13 @@
// each row is a contiguous set of bits, starting at the msb // each row is a contiguous set of bits, starting at the msb
static inline size_t static inline size_t
row_bytes(const struct console_font_op* cfo){ row_bytes(const struct console_font_op* cfo){
return cfo->width + 7 / 8; return (cfo->width + 7) / 8;
} }
// is this constant? lock it down. need 128B for 32*32 FIXME
static inline size_t static inline size_t
glyph_bytes(const struct console_font_op* cfo){ glyph_bytes(const struct console_font_op* cfo){
return row_bytes(cfo) * cfo->height; size_t minb = row_bytes(cfo) * cfo->height;
return (minb + 31) / 32 * 32;
} }
static unsigned char* static unsigned char*
@ -40,13 +40,14 @@ shim_quad_block(struct console_font_op* cfo, unsigned idx, unsigned qbits){
unsigned char mask = 0x80; unsigned char mask = 0x80;
unsigned char* row = glyph + row_bytes(cfo) * r; unsigned char* row = glyph + row_bytes(cfo) * r;
unsigned x; unsigned x;
*row = 0;
for(x = 0 ; x < cfo->width / 2 ; ++x){ for(x = 0 ; x < cfo->width / 2 ; ++x){
if(qbits & 0x8){ if(qbits & 0x8){
*row |= mask; *row |= mask;
} }
if((mask >>= 1) == 0){ if((mask >>= 1) == 0){
mask = 0x80; mask = 0x80;
++row; *++row = 0;
} }
} }
while(x < cfo->width){ while(x < cfo->width){
@ -55,7 +56,7 @@ shim_quad_block(struct console_font_op* cfo, unsigned idx, unsigned qbits){
} }
if((mask >>= 1) == 0){ if((mask >>= 1) == 0){
mask = 0x80; mask = 0x80;
++row; *++row = 0;
} }
++x; ++x;
} }
@ -64,13 +65,14 @@ shim_quad_block(struct console_font_op* cfo, unsigned idx, unsigned qbits){
unsigned char mask = 0x80; unsigned char mask = 0x80;
unsigned char* row = glyph + row_bytes(cfo) * r; unsigned char* row = glyph + row_bytes(cfo) * r;
unsigned x; unsigned x;
*row = 0;
for(x = 0 ; x < cfo->width / 2 ; ++x){ for(x = 0 ; x < cfo->width / 2 ; ++x){
if(qbits & 0x2){ if(qbits & 0x2){
*row |= mask; *row |= mask;
} }
if((mask >>= 1) == 0){ if((mask >>= 1) == 0){
mask = 0x80; mask = 0x80;
++row; *++row = 0;
} }
} }
while(x < cfo->width){ while(x < cfo->width){
@ -79,7 +81,7 @@ shim_quad_block(struct console_font_op* cfo, unsigned idx, unsigned qbits){
} }
if((mask >>= 1) == 0){ if((mask >>= 1) == 0){
mask = 0x80; mask = 0x80;
++row; *++row = 0;
} }
++x; ++x;
} }
@ -384,11 +386,11 @@ static int
reprogram_console_font(const notcurses* nc){ reprogram_console_font(const notcurses* nc){
struct console_font_op cfo = { struct console_font_op cfo = {
.op = KD_FONT_OP_GET, .op = KD_FONT_OP_GET,
.charcount = 256, .charcount = 512,
.height = 32, .height = 32,
.width = 32, .width = 32,
}; };
size_t totsize = 256 * cfo.charcount; // FIXME enough? size_t totsize = 128 * cfo.charcount; // FIXME enough?
cfo.data = malloc(totsize); cfo.data = malloc(totsize);
if(cfo.data == NULL){ if(cfo.data == NULL){
logwarn("Error acquiring %zub for font descriptors (%s)\n", totsize, strerror(errno)); logwarn("Error acquiring %zub for font descriptors (%s)\n", totsize, strerror(errno));

@ -12,6 +12,25 @@
#include <linux/kd.h> #include <linux/kd.h>
#include <sys/ioctl.h> #include <sys/ioctl.h>
static inline size_t
row_bytes(const struct console_font_op* cfo){
return (cfo->width + 7) / 8;
}
static inline size_t
Bper(const struct console_font_op* cfo){
size_t minb = row_bytes(cfo) * cfo->height;
return (minb + 31) / 32 * 32;
}
static unsigned char*
get_glyph(struct console_font_op* cfo, unsigned idx){
if(idx >= cfo->charcount){
return NULL;
}
return (unsigned char*)cfo->data + Bper(cfo) * idx;
}
static void static void
usage(const char* argv){ usage(const char* argv){
fprintf(stderr, "usage: %s [ ttydev ]\n", argv); fprintf(stderr, "usage: %s [ ttydev ]\n", argv);
@ -42,58 +61,97 @@ is_linux_console(int fd){
return true; return true;
} }
// FIXME assumes a width of 8. it is apparently possible to have widths // each row is laid out as bytes, one bit per pixel, rounded up to the
// other than 8, but they don't work properly with GIO_FONTX according to // lowest sufficient number of bytes. each character is thus
// the showconsolefont source code. use the KDFONTOP ioctl to learn true //
// font width. // height * rowbytes, where rowbytes = width + 7 / 8
static int
explode_glyph_row(const unsigned char** row){
printf("%s%s%s%s%s%s%s%s ",
**row & 0x80 ? "*": " ",
**row & 0x40 ? "*": " ",
**row & 0x20 ? "*": " ",
**row & 0x10 ? "*": " ",
**row & 0x08 ? "*": " ",
**row & 0x04 ? "*": " ",
**row & 0x02 ? "*": " ",
**row & 0x01 ? "*": " ");
++*row;
return 0;
}
static int static int
shim_upper_half_block(struct consolefontdesc* cfd, unsigned idx){ explode_glyph_row(const unsigned char** row, unsigned width){
if(idx >= cfd->charcount){ unsigned char mask = 0x80;
return -1; while(width--){
} printf("%s", **row & mask ? "*" : " ");
unsigned char* glyph = (unsigned char*)cfd->chardata + 32 * idx; if((mask >>= 1) == 0 && width){
unsigned r; mask = 0x80;
for(r = 0 ; r < cfd->charheight / 2 ; ++r){ ++*row;
*glyph = 0xff; }
++glyph;
}
while(r < cfd->charheight){
*glyph = 0;
++glyph;
++r;
} }
printf(" ");
++*row;
return 0; return 0;
} }
// idx is the glyph index within cfo->data. qbits are the occupied quadrants:
// 0x8 = upper left
// 0x4 = upper right
// 0x2 = lower left
// 0x1 = lower right
static int static int
shim_lower_half_block(struct consolefontdesc* cfd, unsigned idx){ shim_quad_block(struct console_font_op* cfo, unsigned idx, unsigned qbits){
if(idx >= cfd->charcount){ fprintf(stderr, "REWRITING %u with 0x%01x\n", idx, qbits);
unsigned char* glyph = get_glyph(cfo, idx);
if(glyph == NULL){
return -1; return -1;
} }
unsigned char* glyph = (unsigned char*)cfd->chardata + 32 * idx;
unsigned r; unsigned r;
for(r = 0 ; r < cfd->charheight / 2 ; ++r){ for(r = 0 ; r < cfo->height / 2 ; ++r){
*glyph = 0; unsigned char mask = 0x80;
++glyph; unsigned char* row = glyph + row_bytes(cfo) * r;
unsigned x;
*row = 0;
for(x = 0 ; x < cfo->width / 2 ; ++x){
if(qbits & 0x8){
*row |= mask;
fprintf(stderr, "*");
}
else fprintf(stderr, " ");
if((mask >>= 1) == 0){
mask = 0x80;
*++row = 0;
}
}
while(x < cfo->width){
if(qbits & 0x4){
*row |= mask;
fprintf(stderr, "*");
}
else fprintf(stderr, " ");
if((mask >>= 1) == 0){
mask = 0x80;
*++row = 0;
}
++x;
}
fprintf(stderr, "\n");
} }
while(r < cfd->charheight){ while(r < cfo->height){
*glyph = 0xff; unsigned char mask = 0x80;
++glyph; unsigned char* row = glyph + row_bytes(cfo) * r;
unsigned x;
*row = 0;
for(x = 0 ; x < cfo->width / 2 ; ++x){
if(qbits & 0x2){
*row |= mask;
fprintf(stderr, "*");
}
else fprintf(stderr, " ");
if((mask >>= 1) == 0){
mask = 0x80;
*++row = 0;
}
}
while(x < cfo->width){
if(qbits & 0x1){
*row |= mask;
fprintf(stderr, "*");
}
else fprintf(stderr, " ");
if((mask >>= 1) == 0){
mask = 0x80;
*++row = 0;
}
++x;
}
fprintf(stderr, "\n");
++r; ++r;
} }
return 0; return 0;
@ -103,72 +161,60 @@ shim_lower_half_block(struct consolefontdesc* cfd, unsigned idx){
// positions in |*upper| and |*lower|. // positions in |*upper| and |*lower|.
static int static int
jam_linux_consolefont(int fd, unsigned showglyphs, unsigned* upper, unsigned* lower){ jam_linux_consolefont(int fd, unsigned showglyphs, unsigned* upper, unsigned* lower){
struct consolefontdesc cfd = {}; struct console_font_op cfo = {};
cfd.charcount = 512; cfo.op = KD_FONT_OP_GET;
cfd.chardata = malloc(32 * cfd.charcount); cfo.charcount = 512;
if(cfd.chardata == NULL){ cfo.data = malloc(128 * cfo.charcount);
cfo.width = 32;
cfo.height = 32;
if(cfo.data == NULL){
return -1; return -1;
} }
if(ioctl(fd, GIO_FONTX, &cfd)){ if(ioctl(fd, KDFONTOP, &cfo)){
fprintf(stderr, "Error reading Linux kernelfont (%s)\n", strerror(errno)); fprintf(stderr, "Error reading Linux kernelfont (%s)\n", strerror(errno));
free(cfd.chardata); free(cfo.data);
return -1; return -1;
} }
printf("Kernel font size (glyphcount): %hu\n", cfd.charcount); printf("Kernel font size (glyphcount): %hu\n", cfo.charcount);
printf("Kernel font character height: %hu\n", cfd.charheight); printf("Kernel font character geometry: %hux%hu\n", cfo.height, cfo.width);
if(cfd.charcount > 512){ if(cfo.charcount > 512){
fprintf(stderr, "Warning: kernel returned excess charcount\n"); fprintf(stderr, "Warning: kernel returned excess charcount\n");
free(cfd.chardata); free(cfo.data);
return -1; return -1;
} }
*upper = cfd.charcount - 2; *upper = cfo.charcount - 2;
*lower = cfd.charcount - 1; *lower = cfo.charcount - 1;
// FIXME find best place. could be whatever the fewest unicodes map to, or // FIXME find best place. could be whatever the fewest unicodes map to, or
// something similar to our target, or who knows... // something similar to our target, or who knows...
if(shim_upper_half_block(&cfd, *upper) || shim_lower_half_block(&cfd, *lower)){ if(shim_quad_block(&cfo, *upper, 0xc) || shim_quad_block(&cfo, *lower, 0x3)){
fprintf(stderr, "Failed to shim font\n"); fprintf(stderr, "Failed to shim font\n");
free(cfd.chardata); free(cfo.data);
return -1; return -1;
} }
if(ioctl(fd, PIO_FONTX, &cfd)){ cfo.op = KD_FONT_OP_SET;
if(ioctl(fd, KDFONTOP, &cfo)){
fprintf(stderr, "Failed to set font (%s)\n", strerror(errno)); fprintf(stderr, "Failed to set font (%s)\n", strerror(errno));
free(cfd.chardata); free(cfo.data);
return -1; return -1;
} }
if(showglyphs){ if(showglyphs){
for(unsigned i = 0 ; i < cfd.charcount ; i += 7){ // FIXME get real screen width
const unsigned char* g1 = (unsigned char*)cfd.chardata + 32 * i; const int atonce = 80 / (cfo.width + 1);
const unsigned char* g2 = g1 + 32; const unsigned char* g[atonce];
const unsigned char* g3 = g2 + 32; for(unsigned i = 0 ; i < cfo.charcount ; i += atonce){
const unsigned char* g4 = g3 + 32; for(int o = 0 ; o < atonce ; ++o){
const unsigned char* g5 = g4 + 32; g[o] = (unsigned char*)cfo.data + Bper(&cfo) * (i + o);
const unsigned char* g6 = g5 + 32; fprintf(stderr, "Bper: %zu %d at %p\n", Bper(&cfo), i + o, g[o]);
const unsigned char* g7 = g6 + 32; }
for(unsigned row = 0 ; row < cfd.charheight ; ++row){ for(unsigned row = 0 ; row < cfo.height ; ++row){
explode_glyph_row(&g1); for(int o = 0 ; o < atonce ; ++o){
if(i < cfd.charcount - 1u){ explode_glyph_row(&g[o], cfo.width);
explode_glyph_row(&g2);
if(i < cfd.charcount - 2u){
explode_glyph_row(&g3);
if(i < cfd.charcount - 3u){
explode_glyph_row(&g4);
if(i < cfd.charcount - 4u){
explode_glyph_row(&g5);
if(i < cfd.charcount - 5u){
explode_glyph_row(&g6);
if(i < cfd.charcount - 6u){
explode_glyph_row(&g7);
}
}
}
}
}
} }
printf("\n"); printf("\n");
} }
} }
} }
free(cfd.chardata); free(cfo.data);
return 0; return 0;
} }

Loading…
Cancel
Save