kbmap.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. #include <u.h>
  10. #include <libc.h>
  11. #include <draw.h>
  12. #include <event.h>
  13. typedef struct KbMap KbMap;
  14. struct KbMap {
  15. char *name;
  16. char *file;
  17. Rectangle r;
  18. int current;
  19. };
  20. KbMap *map;
  21. int nmap;
  22. Image *lightblue;
  23. Image *justblue;
  24. enum {
  25. PAD = 3,
  26. MARGIN = 5
  27. };
  28. char *dir = "/sys/lib/kbmap";
  29. void*
  30. erealloc(void *v, uint32_t n)
  31. {
  32. v = realloc(v, n);
  33. if(v == nil)
  34. sysfatal("out of memory reallocating %lu", n);
  35. return v;
  36. }
  37. void*
  38. emalloc(uint32_t n)
  39. {
  40. void *v;
  41. v = malloc(n);
  42. if(v == nil)
  43. sysfatal("out of memory allocating %lu", n);
  44. memset(v, 0, n);
  45. return v;
  46. }
  47. char*
  48. estrdup(char *s)
  49. {
  50. int l;
  51. char *t;
  52. if (s == nil)
  53. return nil;
  54. l = strlen(s)+1;
  55. t = emalloc(l);
  56. memcpy(t, s, l);
  57. return t;
  58. }
  59. void
  60. init(void)
  61. {
  62. int i, fd, nr;
  63. Dir *pd;
  64. char buf[128];
  65. if((fd = open(dir, OREAD)) < 0)
  66. return;
  67. nmap = nr = dirreadall(fd, &pd);
  68. map = emalloc(nr * sizeof(KbMap));
  69. for(i=0; i<nr; i++){
  70. sprint(buf, "%s/%s", dir, pd[i].name);
  71. map[i].file = estrdup(buf);
  72. map[i].name = estrdup(pd[i].name);
  73. map[i].current = 0;
  74. }
  75. free(pd);
  76. close(fd);
  77. }
  78. void
  79. drawmap(int i)
  80. {
  81. if(map[i].current)
  82. draw(screen, map[i].r, justblue, nil, ZP);
  83. else
  84. draw(screen, map[i].r, lightblue, nil, ZP);
  85. _string(screen, addpt(map[i].r.min, Pt(2,0)), display->black, ZP,
  86. font, map[i].name, nil, strlen(map[i].name),
  87. map[i].r, nil, ZP, SoverD);
  88. border(screen, map[i].r, 1, display->black, ZP);
  89. }
  90. void
  91. geometry(void)
  92. {
  93. int i, rows;
  94. Rectangle r;
  95. rows = (Dy(screen->r)-2*MARGIN+PAD)/(font->height+PAD);
  96. r = Rect(0,0,(Dx(screen->r)-2*MARGIN), font->height);
  97. for(i=0; i<nmap; i++)
  98. map[i].r = rectaddpt(rectaddpt(r, Pt(MARGIN+(PAD+Dx(r))*(i/rows),
  99. MARGIN+(PAD+Dy(r))*(i%rows))), screen->r.min);
  100. }
  101. void
  102. redraw(Image *screen)
  103. {
  104. int i;
  105. draw(screen, screen->r, lightblue, nil, ZP);
  106. for(i=0; i<nmap; i++)
  107. drawmap(i);
  108. flushimage(display, 1);
  109. }
  110. void
  111. eresized(int new)
  112. {
  113. if(new && getwindow(display, Refmesg) < 0)
  114. fprint(2,"can't reattach to window");
  115. geometry();
  116. redraw(screen);
  117. }
  118. int
  119. writemap(char *file)
  120. {
  121. int i, fd, ofd;
  122. char buf[8192];
  123. if((fd = open(file, OREAD)) < 0){
  124. fprint(2, "cannot open %s: %r", file);
  125. return -1;
  126. }
  127. if((ofd = open("/dev/kbmap", OWRITE)) < 0) {
  128. fprint(2, "cannot open /dev/kbmap: %r");
  129. close(fd);
  130. return -1;
  131. }
  132. while((i = read(fd, buf, sizeof buf)) > 0)
  133. if(write(ofd, buf, i) != i){
  134. fprint(2, "writing /dev/kbmap: %r");
  135. break;
  136. }
  137. close(fd);
  138. close(ofd);
  139. return 0;
  140. }
  141. void
  142. click(Mouse m)
  143. {
  144. int i, j;
  145. char buf[128];
  146. if(m.buttons == 0 || (m.buttons & ~4))
  147. return;
  148. for(i=0; i<nmap; i++)
  149. if(ptinrect(m.xy, map[i].r))
  150. break;
  151. if(i == nmap)
  152. return;
  153. do
  154. m = emouse();
  155. while(m.buttons == 4);
  156. if(m.buttons != 0){
  157. do
  158. m = emouse();
  159. while(m.buttons);
  160. return;
  161. }
  162. for(j=0; j<nmap; j++)
  163. if(ptinrect(m.xy, map[j].r))
  164. break;
  165. if(j != i)
  166. return;
  167. /* since maps are often just a delta of the distributed map... */
  168. snprint(buf, sizeof buf, "%s/ascii", dir);
  169. writemap(buf);
  170. writemap(map[i].file);
  171. /* clean the previous current map */
  172. for(j=0; j<nmap; j++)
  173. map[j].current = 0;
  174. map[i].current = 1;
  175. redraw(screen);
  176. }
  177. void
  178. usage(void)
  179. {
  180. fprint(2, "usage: kbmap [file...]\n");
  181. exits("usage");
  182. }
  183. void
  184. main(int argc, char **argv)
  185. {
  186. Event e;
  187. char *c;
  188. if(argc > 1) {
  189. argv++; argc--;
  190. map = emalloc((argc)*sizeof(KbMap));
  191. while(argc--) {
  192. map[argc].file = estrdup(argv[argc]);
  193. c = strrchr(map[argc].file, '/');
  194. map[argc].name = (c == nil ? map[argc].file : c+1);
  195. map[argc].current = 0;
  196. nmap++;
  197. }
  198. } else
  199. init();
  200. initdraw(0, 0, "kbmap");
  201. lightblue = allocimagemix(display, DPalebluegreen, DWhite);
  202. if(lightblue == nil)
  203. sysfatal("allocimagemix: %r");
  204. justblue = allocimagemix(display, DBlue, DWhite);
  205. if(justblue == nil)
  206. sysfatal("allocimagemix: %r");
  207. eresized(0);
  208. einit(Emouse|Ekeyboard);
  209. for(;;){
  210. switch(eread(Emouse|Ekeyboard, &e)){
  211. case Ekeyboard:
  212. if(e.kbdc==0x7F || e.kbdc=='q')
  213. exits(0);
  214. break;
  215. case Emouse:
  216. if(e.mouse.buttons)
  217. click(e.mouse);
  218. break;
  219. }
  220. }
  221. }