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);
69 if((fd = open(dir, OREAD)) < 0)
72 nmap = nr = dirreadall(fd, &pd);
73 map = emalloc(nr * sizeof(KbMap));
75 sprint(buf, "%s/%s", dir, pd[i].name);
76 map[i].file = estrdup(buf);
77 map[i].name = estrdup(pd[i].name);
89 draw(screen, map[i].r, justblue, nil, ZP);
91 draw(screen, map[i].r, lightblue, nil, ZP);
93 _string(screen, addpt(map[i].r.min, Pt(2,0)), display->black, ZP,
94 font, map[i].name, nil, strlen(map[i].name),
95 map[i].r, nil, ZP, SoverD);
96 border(screen, map[i].r, 1, display->black, ZP);
105 rows = (Dy(screen->r)-2*MARGIN+PAD)/(font->height+PAD);
107 r = Rect(0,0,(Dx(screen->r)-2*MARGIN), font->height);
108 for(i=0; i<nmap; i++)
109 map[i].r = rectaddpt(rectaddpt(r, Pt(MARGIN+(PAD+Dx(r))*(i/rows),
110 MARGIN+(PAD+Dy(r))*(i%rows))), screen->r.min);
115 redraw(Image *screen)
119 draw(screen, screen->r, lightblue, nil, ZP);
120 for(i=0; i<nmap; i++)
122 flushimage(display, 1);
128 if(new && getwindow(display, Refmesg) < 0)
129 fprint(2,"can't reattach to window");
140 if((fd = open(file, OREAD)) < 0){
141 fprint(2, "cannot open %s: %r", file);
144 if((ofd = open("/dev/kbmap", OWRITE)) < 0) {
145 fprint(2, "cannot open /dev/kbmap: %r");
149 while((i = read(fd, buf, sizeof buf)) > 0)
150 if(write(ofd, buf, i) != i){
151 fprint(2, "writing /dev/kbmap: %r");
166 if(m.buttons == 0 || (m.buttons & ~4))
169 for(i=0; i<nmap; i++)
170 if(ptinrect(m.xy, map[i].r))
177 while(m.buttons == 4);
186 for(j=0; j<nmap; j++)
187 if(ptinrect(m.xy, map[j].r))
192 /* since maps are often just a delta of the distributed map... */
193 snprint(buf, sizeof buf, "%s/ascii", dir);
195 writemap(map[i].file);
197 /* clean the previous current map */
198 for(j=0; j<nmap; j++)
209 fprint(2, "usage: kbmap [file...]\n");
214 main(int argc, char **argv)
221 map = emalloc((argc)*sizeof(KbMap));
223 map[argc].file = estrdup(argv[argc]);
224 c = strrchr(map[argc].file, '/');
225 map[argc].name = (c == nil ? map[argc].file : c+1);
226 map[argc].current = 0;
232 if(initdraw(0, 0, "kbmap") < 0){
233 fprint(2, "kbmap: initdraw failed: %r\n");
236 lightblue = allocimagemix(display, DPalebluegreen, DWhite);
238 sysfatal("allocimagemix: %r");
239 justblue = allocimagemix(display, DBlue, DWhite);
241 sysfatal("allocimagemix: %r");
244 einit(Emouse|Ekeyboard);
247 switch(eread(Emouse|Ekeyboard, &e)){
249 if(e.kbdc==0x7F || e.kbdc=='q')