6 typedef struct KbMap KbMap;
24 char *dir = "/sys/lib/kbmap";
27 erealloc(void *v, ulong n)
31 sysfatal("out of memory reallocating %lud", n);
42 sysfatal("out of memory allocating %lud", n);
68 if((fd = open(dir, OREAD)) < 0)
71 nmap = nr = dirreadall(fd, &pd);
72 map = emalloc(nr * sizeof(KbMap));
74 map[i].file = emalloc(strlen(dir) + strlen(pd[i].name) + 2);
75 sprint(map[i].file, "%s/%s", dir, pd[i].name);
76 map[i].name = estrdup(pd[i].name);
88 draw(screen, map[i].r, justblue, nil, ZP);
90 draw(screen, map[i].r, lightblue, nil, ZP);
92 _string(screen, addpt(map[i].r.min, Pt(2,0)), display->black, ZP,
93 font, map[i].name, nil, strlen(map[i].name),
94 map[i].r, nil, ZP, SoverD);
95 border(screen, map[i].r, 1, display->black, ZP);
104 rows = (Dy(screen->r)-2*MARGIN+PAD)/(font->height+PAD);
106 r = Rect(0,0,(Dx(screen->r)-2*MARGIN), font->height);
107 for(i=0; i<nmap; i++)
108 map[i].r = rectaddpt(rectaddpt(r, Pt(MARGIN+(PAD+Dx(r))*(i/rows),
109 MARGIN+(PAD+Dy(r))*(i%rows))), screen->r.min);
114 redraw(Image *screen)
118 draw(screen, screen->r, lightblue, nil, ZP);
119 for(i=0; i<nmap; i++)
121 flushimage(display, 1);
127 if(new && getwindow(display, Refmesg) < 0)
128 fprint(2,"can't reattach to window");
139 if((fd = open(file, OREAD)) < 0){
140 fprint(2, "cannot open %s: %r", file);
143 if((ofd = open("/dev/kbmap", OWRITE)) < 0) {
144 fprint(2, "cannot open /dev/kbmap: %r");
148 while((i = read(fd, buf, sizeof buf)) > 0)
149 if(write(ofd, buf, i) != i){
150 fprint(2, "writing /dev/kbmap: %r");
165 if(m.buttons == 0 || (m.buttons & ~4))
168 for(i=0; i<nmap; i++)
169 if(ptinrect(m.xy, map[i].r))
176 while(m.buttons == 4);
185 for(j=0; j<nmap; j++)
186 if(ptinrect(m.xy, map[j].r))
191 /* since maps are often just a delta of the distributed map... */
192 snprint(buf, sizeof buf, "%s/ascii", dir);
194 writemap(map[i].file);
196 /* clean the previous current map */
197 for(j=0; j<nmap; j++)
208 fprint(2, "usage: kbmap [file...]\n");
213 main(int argc, char **argv)
220 map = emalloc((argc)*sizeof(KbMap));
222 map[argc].file = estrdup(argv[argc]);
223 c = strrchr(map[argc].file, '/');
224 map[argc].name = (c == nil ? map[argc].file : c+1);
225 map[argc].current = 0;
231 if(initdraw(0, 0, "kbmap") < 0){
232 fprint(2, "kbmap: initdraw failed: %r\n");
235 lightblue = allocimagemix(display, DPalebluegreen, DWhite);
237 sysfatal("allocimagemix: %r");
238 justblue = allocimagemix(display, DBlue, DWhite);
240 sysfatal("allocimagemix: %r");
243 einit(Emouse|Ekeyboard);
246 switch(eread(Emouse|Ekeyboard, &e)){
248 if(e.kbdc==0x7F || e.kbdc=='q')