kbd.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735
  1. /*
  2. * keyboard input
  3. */
  4. #include "u.h"
  5. #include "../port/lib.h"
  6. #include "mem.h"
  7. #include "dat.h"
  8. #include "fns.h"
  9. #include "io.h"
  10. #include "../port/error.h"
  11. enum {
  12. Data= 0x60, /* data port */
  13. Status= 0x64, /* status port */
  14. Inready= 0x01, /* input character ready */
  15. Outbusy= 0x02, /* output busy */
  16. Sysflag= 0x04, /* system flag */
  17. Cmddata= 0x08, /* cmd==0, data==1 */
  18. Inhibit= 0x10, /* keyboard/mouse inhibited */
  19. Minready= 0x20, /* mouse character ready */
  20. Rtimeout= 0x40, /* general timeout */
  21. Parity= 0x80,
  22. Cmd= 0x64, /* command port (write only) */
  23. Spec= 0xF800, /* Unicode private space */
  24. PF= Spec|0x20, /* num pad function key */
  25. View= Spec|0x00, /* view (shift window up) */
  26. KF= 0xF000, /* function key (begin Unicode private space) */
  27. Shift= Spec|0x60,
  28. Break= Spec|0x61,
  29. Ctrl= Spec|0x62,
  30. Latin= Spec|0x63,
  31. Caps= Spec|0x64,
  32. Num= Spec|0x65,
  33. Middle= Spec|0x66,
  34. Altgr= Spec|0x67,
  35. Kmouse= Spec|0x100,
  36. No= 0x00, /* peter */
  37. Home= KF|13,
  38. Up= KF|14,
  39. Pgup= KF|15,
  40. Print= KF|16,
  41. Left= KF|17,
  42. Right= KF|18,
  43. End= KF|24,
  44. Down= View,
  45. Pgdown= KF|19,
  46. Ins= KF|20,
  47. Del= 0x7F,
  48. Scroll= KF|21,
  49. Nscan= 128,
  50. Int= 0, /* kbscans indices */
  51. Ext,
  52. Nscans,
  53. };
  54. /*
  55. * The codes at 0x79 and 0x7b are produced by the PFU Happy Hacking keyboard.
  56. * A 'standard' keyboard doesn't produce anything above 0x58.
  57. */
  58. Rune kbtab[Nscan] =
  59. {
  60. [0x00] No, 0x1b, '1', '2', '3', '4', '5', '6',
  61. [0x08] '7', '8', '9', '0', '-', '=', '\b', '\t',
  62. [0x10] 'q', 'w', 'e', 'r', 't', 'y', 'u', 'i',
  63. [0x18] 'o', 'p', '[', ']', '\n', Ctrl, 'a', 's',
  64. [0x20] 'd', 'f', 'g', 'h', 'j', 'k', 'l', ';',
  65. [0x28] '\'', '`', Shift, '\\', 'z', 'x', 'c', 'v',
  66. [0x30] 'b', 'n', 'm', ',', '.', '/', Shift, '*',
  67. [0x38] Latin, ' ', Ctrl, KF|1, KF|2, KF|3, KF|4, KF|5,
  68. [0x40] KF|6, KF|7, KF|8, KF|9, KF|10, Num, Scroll, '7',
  69. [0x48] '8', '9', '-', '4', '5', '6', '+', '1',
  70. [0x50] '2', '3', '0', '.', No, No, No, KF|11,
  71. [0x58] KF|12, No, No, No, No, No, No, No,
  72. [0x60] No, No, No, No, No, No, No, No,
  73. [0x68] No, No, No, No, No, No, No, No,
  74. [0x70] No, No, No, No, No, No, No, No,
  75. [0x78] No, View, No, Up, No, No, No, No,
  76. };
  77. Rune kbtabshift[Nscan] =
  78. {
  79. [0x00] No, 0x1b, '!', '@', '#', '$', '%', '^',
  80. [0x08] '&', '*', '(', ')', '_', '+', '\b', '\t',
  81. [0x10] 'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I',
  82. [0x18] 'O', 'P', '{', '}', '\n', Ctrl, 'A', 'S',
  83. [0x20] 'D', 'F', 'G', 'H', 'J', 'K', 'L', ':',
  84. [0x28] '"', '~', Shift, '|', 'Z', 'X', 'C', 'V',
  85. [0x30] 'B', 'N', 'M', '<', '>', '?', Shift, '*',
  86. [0x38] Latin, ' ', Ctrl, KF|1, KF|2, KF|3, KF|4, KF|5,
  87. [0x40] KF|6, KF|7, KF|8, KF|9, KF|10, Num, Scroll, '7',
  88. [0x48] '8', '9', '-', '4', '5', '6', '+', '1',
  89. [0x50] '2', '3', '0', '.', No, No, No, KF|11,
  90. [0x58] KF|12, No, No, No, No, No, No, No,
  91. [0x60] No, No, No, No, No, No, No, No,
  92. [0x68] No, No, No, No, No, No, No, No,
  93. [0x70] No, No, No, No, No, No, No, No,
  94. [0x78] No, Up, No, Up, No, No, No, No,
  95. };
  96. Rune kbtabesc1[Nscan] =
  97. {
  98. [0x00] No, No, No, No, No, No, No, No,
  99. [0x08] No, No, No, No, No, No, No, No,
  100. [0x10] No, No, No, No, No, No, No, No,
  101. [0x18] No, No, No, No, '\n', Ctrl, No, No,
  102. [0x20] No, No, No, No, No, No, No, No,
  103. [0x28] No, No, Shift, No, No, No, No, No,
  104. [0x30] No, No, No, No, No, '/', No, Print,
  105. [0x38] Altgr, No, No, No, No, No, No, No,
  106. [0x40] No, No, No, No, No, No, Break, Home,
  107. [0x48] Up, Pgup, No, Left, No, Right, No, End,
  108. [0x50] Down, Pgdown, Ins, Del, No, No, No, No,
  109. [0x58] No, No, No, No, No, No, No, No,
  110. [0x60] No, No, No, No, No, No, No, No,
  111. [0x68] No, No, No, No, No, No, No, No,
  112. [0x70] No, No, No, No, No, No, No, No,
  113. [0x78] No, Up, No, No, No, No, No, No,
  114. };
  115. Rune kbtabaltgr[Nscan] =
  116. {
  117. [0x00] No, No, No, No, No, No, No, No,
  118. [0x08] No, No, No, No, No, No, No, No,
  119. [0x10] No, No, No, No, No, No, No, No,
  120. [0x18] No, No, No, No, '\n', Ctrl, No, No,
  121. [0x20] No, No, No, No, No, No, No, No,
  122. [0x28] No, No, Shift, No, No, No, No, No,
  123. [0x30] No, No, No, No, No, '/', No, Print,
  124. [0x38] Altgr, No, No, No, No, No, No, No,
  125. [0x40] No, No, No, No, No, No, Break, Home,
  126. [0x48] Up, Pgup, No, Left, No, Right, No, End,
  127. [0x50] Down, Pgdown, Ins, Del, No, No, No, No,
  128. [0x58] No, No, No, No, No, No, No, No,
  129. [0x60] No, No, No, No, No, No, No, No,
  130. [0x68] No, No, No, No, No, No, No, No,
  131. [0x70] No, No, No, No, No, No, No, No,
  132. [0x78] No, Up, No, No, No, No, No, No,
  133. };
  134. Rune kbtabctrl[Nscan] =
  135. {
  136. [0x00] No, '', '', '', '', '', '', '',
  137. [0x08] '', '', '', '', ' ', '', '\b', '\t',
  138. [0x10] '', '', '', '', '', '', '', '\t',
  139. [0x18] '', '', '', '', '\n', Ctrl, '', '',
  140. [0x20] '', '', '', '\b', '\n', ' ', ' ', '',
  141. [0x28] '', No, Shift, '', '', '', '', '',
  142. [0x30] '', '', ' ', ' ', '', '', Shift, '\n',
  143. [0x38] Latin, No, Ctrl, '', '', '', '', '',
  144. [0x40] '', '', ' ', ' ', '', '', '', '',
  145. [0x48] '', '', ' ', '', '', '', ' ', '',
  146. [0x50] '', '', '', '', No, No, No, '',
  147. [0x58] ' ', No, No, No, No, No, No, No,
  148. [0x60] No, No, No, No, No, No, No, No,
  149. [0x68] No, No, No, No, No, No, No, No,
  150. [0x70] No, No, No, No, No, No, No, No,
  151. [0x78] No, '', No, '\b', No, No, No, No,
  152. };
  153. enum
  154. {
  155. /* controller command byte */
  156. Cscs1= (1<<6), /* scan code set 1 */
  157. Cauxdis= (1<<5), /* mouse disable */
  158. Ckbddis= (1<<4), /* kbd disable */
  159. Csf= (1<<2), /* system flag */
  160. Cauxint= (1<<1), /* mouse interrupt enable */
  161. Ckbdint= (1<<0), /* kbd interrupt enable */
  162. };
  163. int mouseshifted;
  164. void (*kbdmouse)(int);
  165. static Lock i8042lock;
  166. static uchar ccc;
  167. static void (*auxputc)(int, int);
  168. static int nokbd = 1; /* flag: no PS/2 keyboard */
  169. /*
  170. * wait for output no longer busy
  171. */
  172. static int
  173. outready(void)
  174. {
  175. int tries;
  176. for(tries = 0; (inb(Status) & Outbusy); tries++){
  177. if(tries > 500)
  178. return -1;
  179. delay(2);
  180. }
  181. return 0;
  182. }
  183. /*
  184. * wait for input
  185. */
  186. static int
  187. inready(void)
  188. {
  189. int tries;
  190. for(tries = 0; !(inb(Status) & Inready); tries++){
  191. if(tries > 500)
  192. return -1;
  193. delay(2);
  194. }
  195. return 0;
  196. }
  197. /*
  198. * ask 8042 to enable the use of address bit 20
  199. */
  200. void
  201. i8042a20(void)
  202. {
  203. outready();
  204. outb(Cmd, 0xD1);
  205. outready();
  206. outb(Data, 0xDF);
  207. outready();
  208. }
  209. /*
  210. * ask 8042 to reset the machine
  211. */
  212. void
  213. i8042reset(void)
  214. {
  215. int i, x;
  216. if(nokbd)
  217. return;
  218. *((ushort*)KADDR(0x472)) = 0x1234; /* BIOS warm-boot flag */
  219. /*
  220. * newer reset the machine command
  221. */
  222. outready();
  223. outb(Cmd, 0xFE);
  224. outready();
  225. /*
  226. * Pulse it by hand (old somewhat reliable)
  227. */
  228. x = 0xDF;
  229. for(i = 0; i < 5; i++){
  230. x ^= 1;
  231. outready();
  232. outb(Cmd, 0xD1);
  233. outready();
  234. outb(Data, x); /* toggle reset */
  235. delay(100);
  236. }
  237. }
  238. int
  239. i8042auxcmd(int cmd)
  240. {
  241. unsigned int c;
  242. int tries;
  243. static int badkbd;
  244. if(badkbd)
  245. return -1;
  246. c = 0;
  247. tries = 0;
  248. ilock(&i8042lock);
  249. do{
  250. if(tries++ > 2)
  251. break;
  252. if(outready() < 0)
  253. break;
  254. outb(Cmd, 0xD4);
  255. if(outready() < 0)
  256. break;
  257. outb(Data, cmd);
  258. if(outready() < 0)
  259. break;
  260. if(inready() < 0)
  261. break;
  262. c = inb(Data);
  263. } while(c == 0xFE || c == 0);
  264. iunlock(&i8042lock);
  265. if(c != 0xFA){
  266. print("i8042: %2.2ux returned to the %2.2ux command\n", c, cmd);
  267. badkbd = 1; /* don't keep trying; there might not be one */
  268. return -1;
  269. }
  270. return 0;
  271. }
  272. int
  273. i8042auxcmds(uchar *cmd, int ncmd)
  274. {
  275. int i;
  276. ilock(&i8042lock);
  277. for(i=0; i<ncmd; i++){
  278. if(outready() < 0)
  279. break;
  280. outb(Cmd, 0xD4);
  281. if(outready() < 0)
  282. break;
  283. outb(Data, cmd[i]);
  284. }
  285. iunlock(&i8042lock);
  286. return i;
  287. }
  288. typedef struct Kbscan Kbscan;
  289. struct Kbscan {
  290. int esc1;
  291. int esc2;
  292. int alt;
  293. int altgr;
  294. int caps;
  295. int ctl;
  296. int num;
  297. int shift;
  298. int collecting;
  299. int nk;
  300. Rune kc[5];
  301. int buttons;
  302. };
  303. Kbscan kbscans[Nscans]; /* kernel and external scan code state */
  304. static int kdebug;
  305. /*
  306. * set keyboard's leds for lock states (scroll, numeric, caps).
  307. *
  308. * at least one keyboard (from Qtronics) also sets its numeric-lock
  309. * behaviour to match the led state, though it has no numeric keypad,
  310. * and some BIOSes bring the system up with numeric-lock set and no
  311. * setting to change that. this combination steals the keys for these
  312. * characters and makes it impossible to generate them: uiolkjm&*().
  313. * thus we'd like to be able to force the numeric-lock led (and behaviour) off.
  314. */
  315. static void
  316. setleds(Kbscan *kbscan)
  317. {
  318. int leds;
  319. if(nokbd || kbscan != &kbscans[Int])
  320. return;
  321. leds = 0;
  322. if(kbscan->num)
  323. leds |= 1<<1;
  324. if(0 && kbscan->caps) /* we don't implement caps lock */
  325. leds |= 1<<2;
  326. ilock(&i8042lock);
  327. outready();
  328. outb(Data, 0xed); /* `reset keyboard lock states' */
  329. if(inready() == 0)
  330. inb(Data);
  331. outready();
  332. outb(Data, leds);
  333. if(inready() == 0)
  334. inb(Data);
  335. outready();
  336. iunlock(&i8042lock);
  337. }
  338. /*
  339. * Scan code processing
  340. */
  341. void
  342. kbdputsc(int c, int external)
  343. {
  344. int i, keyup;
  345. Kbscan *kbscan;
  346. if(external)
  347. kbscan = &kbscans[Ext];
  348. else
  349. kbscan = &kbscans[Int];
  350. if(kdebug)
  351. print("sc %x ms %d\n", c, mouseshifted);
  352. /*
  353. * e0's is the first of a 2 character sequence, e1 the first
  354. * of a 3 character sequence (on the safari)
  355. */
  356. if(c == 0xe0){
  357. kbscan->esc1 = 1;
  358. return;
  359. } else if(c == 0xe1){
  360. kbscan->esc2 = 2;
  361. return;
  362. }
  363. keyup = c & 0x80;
  364. c &= 0x7f;
  365. if(c > sizeof kbtab){
  366. c |= keyup;
  367. if(c != 0xFF) /* these come fairly often: CAPSLOCK U Y */
  368. print("unknown key %ux\n", c);
  369. return;
  370. }
  371. if(kbscan->esc1){
  372. c = kbtabesc1[c];
  373. kbscan->esc1 = 0;
  374. } else if(kbscan->esc2){
  375. kbscan->esc2--;
  376. return;
  377. } else if(kbscan->shift)
  378. c = kbtabshift[c];
  379. else if(kbscan->altgr)
  380. c = kbtabaltgr[c];
  381. else if(kbscan->ctl)
  382. c = kbtabctrl[c];
  383. else
  384. c = kbtab[c];
  385. if(kbscan->caps && c<='z' && c>='a')
  386. c += 'A' - 'a';
  387. /*
  388. * keyup only important for shifts
  389. */
  390. if(keyup){
  391. switch(c){
  392. case Latin:
  393. kbscan->alt = 0;
  394. break;
  395. case Shift:
  396. kbscan->shift = 0;
  397. mouseshifted = 0;
  398. if(kdebug)
  399. print("shiftclr\n");
  400. break;
  401. case Ctrl:
  402. kbscan->ctl = 0;
  403. break;
  404. case Altgr:
  405. kbscan->altgr = 0;
  406. break;
  407. case Kmouse|1:
  408. case Kmouse|2:
  409. case Kmouse|3:
  410. case Kmouse|4:
  411. case Kmouse|5:
  412. kbscan->buttons &= ~(1<<(c-Kmouse-1));
  413. if(kbdmouse)
  414. kbdmouse(kbscan->buttons);
  415. break;
  416. }
  417. return;
  418. }
  419. /*
  420. * normal character
  421. */
  422. if(!(c & (Spec|KF))){
  423. if(kbscan->ctl)
  424. if(kbscan->alt && c == Del)
  425. exit(0);
  426. if(!kbscan->collecting){
  427. kbdputc(kbdq, c);
  428. return;
  429. }
  430. kbscan->kc[kbscan->nk++] = c;
  431. c = latin1(kbscan->kc, kbscan->nk);
  432. if(c < -1) /* need more keystrokes */
  433. return;
  434. if(c != -1) /* valid sequence */
  435. kbdputc(kbdq, c);
  436. else /* dump characters */
  437. for(i=0; i<kbscan->nk; i++)
  438. kbdputc(kbdq, kbscan->kc[i]);
  439. kbscan->nk = 0;
  440. kbscan->collecting = 0;
  441. return;
  442. } else {
  443. switch(c){
  444. case Caps:
  445. kbscan->caps ^= 1;
  446. return;
  447. case Num:
  448. kbscan->num ^= 1;
  449. if(!external)
  450. setleds(kbscan);
  451. return;
  452. case Shift:
  453. kbscan->shift = 1;
  454. if(kdebug)
  455. print("shift\n");
  456. mouseshifted = 1;
  457. return;
  458. case Latin:
  459. kbscan->alt = 1;
  460. /*
  461. * VMware and Qemu use Ctl-Alt as the key combination
  462. * to make the VM give up keyboard and mouse focus.
  463. * This has the unfortunate side effect that when you
  464. * come back into focus, Plan 9 thinks you want to type
  465. * a compose sequence (you just typed alt).
  466. *
  467. * As a clumsy hack around this, we look for ctl-alt
  468. * and don't treat it as the start of a compose sequence.
  469. */
  470. if(!kbscan->ctl){
  471. kbscan->collecting = 1;
  472. kbscan->nk = 0;
  473. }
  474. return;
  475. case Ctrl:
  476. kbscan->ctl = 1;
  477. return;
  478. case Altgr:
  479. kbscan->altgr = 1;
  480. return;
  481. case Kmouse|1:
  482. case Kmouse|2:
  483. case Kmouse|3:
  484. case Kmouse|4:
  485. case Kmouse|5:
  486. kbscan->buttons |= 1<<(c-Kmouse-1);
  487. if(kbdmouse)
  488. kbdmouse(kbscan->buttons);
  489. return;
  490. case KF|11:
  491. print("kbd debug on, F12 turns it off\n");
  492. kdebug = 1;
  493. break;
  494. case KF|12:
  495. kdebug = 0;
  496. break;
  497. }
  498. }
  499. kbdputc(kbdq, c);
  500. }
  501. /*
  502. * keyboard interrupt
  503. */
  504. static void
  505. i8042intr(Ureg*, void*)
  506. {
  507. int s, c;
  508. /*
  509. * get status
  510. */
  511. ilock(&i8042lock);
  512. s = inb(Status);
  513. if(!(s&Inready)){
  514. iunlock(&i8042lock);
  515. return;
  516. }
  517. /*
  518. * get the character
  519. */
  520. c = inb(Data);
  521. iunlock(&i8042lock);
  522. /*
  523. * if it's the aux port...
  524. */
  525. if(s & Minready){
  526. if(auxputc != nil)
  527. auxputc(c, kbscans[Int].shift);
  528. return;
  529. }
  530. kbdputsc(c, Int);
  531. }
  532. void
  533. i8042auxenable(void (*putc)(int, int))
  534. {
  535. char *err = "i8042: aux init failed\n";
  536. /* enable kbd/aux xfers and interrupts */
  537. ccc &= ~Cauxdis;
  538. ccc |= Cauxint;
  539. ilock(&i8042lock);
  540. if(outready() < 0)
  541. print(err);
  542. outb(Cmd, 0x60); /* write control register */
  543. if(outready() < 0)
  544. print(err);
  545. outb(Data, ccc);
  546. if(outready() < 0)
  547. print(err);
  548. outb(Cmd, 0xA8); /* auxiliary device enable */
  549. if(outready() < 0){
  550. iunlock(&i8042lock);
  551. return;
  552. }
  553. auxputc = putc;
  554. intrenable(IrqAUX, i8042intr, 0, BUSUNKNOWN, "kbdaux");
  555. iunlock(&i8042lock);
  556. }
  557. static char *initfailed = "i8042: kbdinit failed\n";
  558. static int
  559. outbyte(int port, int c)
  560. {
  561. outb(port, c);
  562. if(outready() < 0) {
  563. print(initfailed);
  564. return -1;
  565. }
  566. return 0;
  567. }
  568. void
  569. kbdinit(void)
  570. {
  571. int c, try;
  572. /* wait for a quiescent controller */
  573. try = 1000;
  574. while(try-- > 0 && (c = inb(Status)) & (Outbusy | Inready)) {
  575. if(c & Inready)
  576. inb(Data);
  577. delay(1);
  578. }
  579. if (try <= 0) {
  580. print(initfailed);
  581. return;
  582. }
  583. /* get current controller command byte */
  584. outb(Cmd, 0x20);
  585. if(inready() < 0){
  586. print("i8042: kbdinit can't read ccc\n");
  587. ccc = 0;
  588. } else
  589. ccc = inb(Data);
  590. /* enable kbd xfers and interrupts */
  591. ccc &= ~Ckbddis;
  592. ccc |= Csf | Ckbdint | Cscs1;
  593. if(outready() < 0) {
  594. print(initfailed);
  595. return;
  596. }
  597. nokbd = 0;
  598. /* disable mouse */
  599. if (outbyte(Cmd, 0x60) < 0 || outbyte(Data, ccc) < 0)
  600. print("i8042: kbdinit mouse disable failed\n");
  601. }
  602. void
  603. kbdenable(void)
  604. {
  605. kbdq = qopen(4*1024, 0, 0, 0);
  606. if(kbdq == nil)
  607. panic("kbdinit");
  608. qnoblock(kbdq, 1);
  609. ioalloc(Data, 1, 0, "kbd");
  610. ioalloc(Cmd, 1, 0, "kbd");
  611. intrenable(IrqKBD, i8042intr, 0, BUSUNKNOWN, "kbd");
  612. kbscans[Int].num = 0;
  613. setleds(&kbscans[Int]);
  614. }
  615. void
  616. kbdputmap(ushort m, ushort scanc, Rune r)
  617. {
  618. if(scanc >= Nscan)
  619. error(Ebadarg);
  620. switch(m) {
  621. default:
  622. error(Ebadarg);
  623. case 0:
  624. kbtab[scanc] = r;
  625. break;
  626. case 1:
  627. kbtabshift[scanc] = r;
  628. break;
  629. case 2:
  630. kbtabesc1[scanc] = r;
  631. break;
  632. case 3:
  633. kbtabaltgr[scanc] = r;
  634. break;
  635. case 4:
  636. kbtabctrl[scanc] = r;
  637. break;
  638. }
  639. }
  640. int
  641. kbdgetmap(uint offset, int *t, int *sc, Rune *r)
  642. {
  643. if ((int)offset < 0)
  644. error(Ebadarg);
  645. *t = offset/Nscan;
  646. *sc = offset%Nscan;
  647. switch(*t) {
  648. default:
  649. return 0;
  650. case 0:
  651. *r = kbtab[*sc];
  652. return 1;
  653. case 1:
  654. *r = kbtabshift[*sc];
  655. return 1;
  656. case 2:
  657. *r = kbtabesc1[*sc];
  658. return 1;
  659. case 3:
  660. *r = kbtabaltgr[*sc];
  661. return 1;
  662. case 4:
  663. *r = kbtabctrl[*sc];
  664. return 1;
  665. }
  666. }