7 typedef struct KbMap KbMap;
25 char *dir = "/sys/lib/kbmap";
28 erealloc(void *v, ulong n)
32 sysfatal("out of memory reallocating %lud", n);
43 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 map[i].file = emalloc(strlen(dir) + strlen(pd[i].name) + 2);
76 sprint(map[i].file, "%s/%s", dir, pd[i].name);
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);
109 r = Rect(0,0,(Dx(screen->r)-2*MARGIN), font->height);
110 for(i=0; i<nmap; i++)
111 map[i].r = rectaddpt(rectaddpt(r, Pt(MARGIN+(PAD+Dx(r))*(i/rows),
112 MARGIN+(PAD+Dy(r))*(i%rows))), screen->r.min);
117 redraw(Image *screen)
121 draw(screen, screen->r, lightblue, nil, ZP);
122 for(i=0; i<nmap; i++)
124 flushimage(display, 1);
130 if(new && getwindow(display, Refmesg) < 0)
131 fprint(2,"can't reattach to window");
142 if((fd = open(file, OREAD)) < 0){
143 fprint(2, "cannot open %s: %r", file);
146 if((ofd = open("/dev/kbmap", OWRITE)) < 0) {
147 fprint(2, "cannot open /dev/kbmap: %r");
151 while((i = read(fd, buf, sizeof buf)) > 0)
152 if(write(ofd, buf, i) != i){
153 fprint(2, "writing /dev/kbmap: %r");
168 if(m.buttons == 0 || (m.buttons & ~4))
171 for(i=0; i<nmap; i++)
172 if(ptinrect(m.xy, map[i].r))
179 while(m.buttons == 4);
188 for(j=0; j<nmap; j++)
189 if(ptinrect(m.xy, map[j].r))
194 /* since maps are often just a delta of the distributed map... */
195 snprint(buf, sizeof buf, "%s/ascii", dir);
197 writemap(map[i].file);
199 /* clean the previous current map */
200 for(j=0; j<nmap; j++)
211 fprint(2, "usage: kbmap [file...]\n");
216 main(int argc, char **argv)
223 map = emalloc((argc)*sizeof(KbMap));
225 map[argc].file = estrdup(argv[argc]);
226 c = strrchr(map[argc].file, '/');
227 map[argc].name = (c == nil ? map[argc].file : c+1);
228 map[argc].current = 0;
234 if(initdraw(0, 0, "kbmap") < 0){
235 fprint(2, "kbmap: initdraw failed: %r\n");
238 lightblue = allocimagemix(display, DPalebluegreen, DWhite);
240 sysfatal("allocimagemix: %r");
241 justblue = allocimagemix(display, DBlue, DWhite);
243 sysfatal("allocimagemix: %r");
246 einit(Emouse|Ekeyboard);
249 switch(eread(Emouse|Ekeyboard, &e)){
251 if(e.kbdc==Kdel || e.kbdc=='q')