keyboard.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  1. /****************************************************************************
  2. *
  3. * SciTech OS Portability Manager Library
  4. *
  5. * ========================================================================
  6. *
  7. * The contents of this file are subject to the SciTech MGL Public
  8. * License Version 1.0 (the "License"); you may not use this file
  9. * except in compliance with the License. You may obtain a copy of
  10. * the License at http://www.scitechsoft.com/mgl-license.txt
  11. *
  12. * Software distributed under the License is distributed on an
  13. * "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
  14. * implied. See the License for the specific language governing
  15. * rights and limitations under the License.
  16. *
  17. * The Original Code is Copyright (C) 1991-1998 SciTech Software, Inc.
  18. *
  19. * The Initial Developer of the Original Code is SciTech Software, Inc.
  20. * All Rights Reserved.
  21. *
  22. * ========================================================================
  23. *
  24. * Language: ANSI C
  25. * Environment: Any
  26. *
  27. * Description: Direct keyboard event handling module. This module contains
  28. * code to process raw scan code information, convert it to
  29. * virtual scan codes and do code page translation to ASCII
  30. * for different international keyboard layouts.
  31. *
  32. ****************************************************************************/
  33. /*---------------------------- Implementation -----------------------------*/
  34. /****************************************************************************
  35. PARAMETERS:
  36. scanCode - Keyboard scan code to translate
  37. table - Code page table to search
  38. count - Number of entries in the code page table
  39. REMARKS:
  40. This function translates the scan codes from keyboard scan codes to ASCII
  41. codes using a binary search on the code page table.
  42. ****************************************************************************/
  43. static uchar translateScan(
  44. uchar scanCode,
  45. codepage_entry_t *table,
  46. int count)
  47. {
  48. codepage_entry_t *test;
  49. int n,pivot,val;
  50. for (n = count; n > 0; ) {
  51. pivot = n >> 1;
  52. test = table + pivot;
  53. val = scanCode - test->scanCode;
  54. if (val < 0)
  55. n = pivot;
  56. else if (val == 0)
  57. return test->asciiCode;
  58. else {
  59. table = test + 1;
  60. n -= pivot + 1;
  61. }
  62. }
  63. return 0;
  64. }
  65. /****************************************************************************
  66. REMARKS:
  67. This macro/function is used to converts the scan codes reported by the
  68. keyboard to our event libraries normalised format. We only have one scan
  69. code for the 'A' key, and use shift modifiers to determine if it is a
  70. Ctrl-F1, Alt-F1 etc. The raw scan codes from the keyboard work this way,
  71. but the OS gives us 'cooked' scan codes, we have to translate them back
  72. to the raw format.
  73. {secret}
  74. ****************************************************************************/
  75. void _EVT_maskKeyCode(
  76. event_t *evt)
  77. {
  78. int ascii,scan = EVT_scanCode(evt->message);
  79. evt->message &= ~0xFF;
  80. if (evt->modifiers & EVT_NUMLOCK) {
  81. if ((ascii = translateScan(scan,EVT.codePage->numPad,EVT.codePage->numPadLen)) != 0) {
  82. evt->message |= ascii;
  83. return;
  84. }
  85. }
  86. if (evt->modifiers & EVT_CTRLSTATE) {
  87. evt->message |= translateScan(scan,EVT.codePage->ctrl,EVT.codePage->ctrlLen);
  88. return;
  89. }
  90. if (evt->modifiers & EVT_CAPSLOCK) {
  91. if (evt->modifiers & EVT_SHIFTKEY) {
  92. if ((ascii = translateScan(scan,EVT.codePage->shiftCaps,EVT.codePage->shiftCapsLen)) != 0) {
  93. evt->message |= ascii;
  94. return;
  95. }
  96. }
  97. else {
  98. if ((ascii = translateScan(scan,EVT.codePage->caps,EVT.codePage->capsLen)) != 0) {
  99. evt->message |= ascii;
  100. return;
  101. }
  102. }
  103. }
  104. if (evt->modifiers & EVT_SHIFTKEY) {
  105. if ((ascii = translateScan(scan,EVT.codePage->shift,EVT.codePage->shiftLen)) != 0) {
  106. evt->message |= ascii;
  107. return;
  108. }
  109. }
  110. evt->message |= translateScan(scan,EVT.codePage->normal,EVT.codePage->normalLen);
  111. }
  112. /****************************************************************************
  113. REMARKS:
  114. Returns true if the key with the specified scan code is being held down.
  115. ****************************************************************************/
  116. static ibool _EVT_isKeyDown(
  117. uchar scanCode)
  118. {
  119. if (scanCode > 0x7F)
  120. return false;
  121. else
  122. return EVT.keyTable[scanCode] != 0;
  123. }
  124. /****************************************************************************
  125. PARAMETERS:
  126. what - Event code
  127. message - Event message (ASCII code and scan code)
  128. REMARKS:
  129. Adds a new keyboard event to the event queue. This routine is called from
  130. within the keyboard interrupt subroutine!
  131. NOTE: Interrupts are OFF when this routine is called by the keyboard ISR,
  132. and we leave them OFF the entire time.
  133. ****************************************************************************/
  134. static void addKeyEvent(
  135. uint what,
  136. uint message)
  137. {
  138. event_t evt;
  139. if (EVT.count < EVENTQSIZE) {
  140. /* Save information in event record */
  141. evt.when = _EVT_getTicks();
  142. evt.what = what;
  143. evt.message = message | 0x10000UL;
  144. evt.where_x = 0;
  145. evt.where_y = 0;
  146. evt.relative_x = 0;
  147. evt.relative_y = 0;
  148. evt.modifiers = EVT.keyModifiers;
  149. if (evt.what == EVT_KEYREPEAT) {
  150. if (EVT.oldKey != -1)
  151. EVT.evtq[EVT.oldKey].message += 0x10000UL;
  152. else {
  153. EVT.oldKey = EVT.freeHead;
  154. addEvent(&evt); /* Add to tail of event queue */
  155. }
  156. }
  157. else {
  158. #ifdef __QNX__
  159. _EVT_maskKeyCode(&evt);
  160. #endif
  161. addEvent(&evt); /* Add to tail of event queue */
  162. }
  163. EVT.oldMove = -1;
  164. }
  165. }
  166. /****************************************************************************
  167. REMARKS:
  168. This function waits for the keyboard controller to set the ready-for-write
  169. bit.
  170. ****************************************************************************/
  171. static int kbWaitForWriteReady(void)
  172. {
  173. int timeout = 8192;
  174. while ((timeout > 0) && (PM_inpb(0x64) & 0x02))
  175. timeout--;
  176. return (timeout > 0);
  177. }
  178. /****************************************************************************
  179. REMARKS:
  180. This function waits for the keyboard controller to set the ready-for-read
  181. bit.
  182. ****************************************************************************/
  183. static int kbWaitForReadReady(void)
  184. {
  185. int timeout = 8192;
  186. while ((timeout > 0) && (!(PM_inpb(0x64) & 0x01)))
  187. timeout--;
  188. return (timeout > 0);
  189. }
  190. /****************************************************************************
  191. PARAMETERS:
  192. data - Data to send to the keyboard
  193. REMARKS:
  194. This function sends a data byte to the keyboard controller.
  195. ****************************************************************************/
  196. static int kbSendData(
  197. uchar data)
  198. {
  199. int resends = 4;
  200. int timeout, temp;
  201. do {
  202. if (!kbWaitForWriteReady())
  203. return 0;
  204. PM_outpb(0x60,data);
  205. timeout = 8192;
  206. while (--timeout > 0) {
  207. if (!kbWaitForReadReady())
  208. return 0;
  209. temp = PM_inpb(0x60);
  210. if (temp == 0xFA)
  211. return 1;
  212. if (temp == 0xFE)
  213. break;
  214. }
  215. } while ((resends-- > 0) && (timeout > 0));
  216. return 0;
  217. }
  218. /****************************************************************************
  219. PARAMETERS:
  220. modifiers - Keyboard modifier flags
  221. REMARKS:
  222. This function re-programs the LED's on the keyboard to the values stored
  223. in the passed in modifier flags. If the 'allowLEDS' flag is false, this
  224. function does nothing.
  225. ****************************************************************************/
  226. static void setLEDS(
  227. uint modifiers)
  228. {
  229. if (EVT.allowLEDS) {
  230. if (!kbSendData(0xED) || !kbSendData((modifiers>>9) & 7)) {
  231. kbSendData(0xF4);
  232. }
  233. }
  234. }
  235. /****************************************************************************
  236. REMARKS:
  237. Function to process raw scan codes read from the keyboard controller.
  238. NOTE: Interrupts are OFF when this routine is called by the keyboard ISR,
  239. and we leave them OFF the entire time.
  240. {secret}
  241. ****************************************************************************/
  242. void processRawScanCode(
  243. int scan)
  244. {
  245. static int pauseLoop = 0;
  246. static int extended = 0;
  247. int what;
  248. if (pauseLoop) {
  249. /* Skip scan codes until the pause key sequence has been read */
  250. pauseLoop--;
  251. }
  252. else if (scan == 0xE0) {
  253. /* This signals the start of an extended scan code sequence */
  254. extended = 1;
  255. }
  256. else if (scan == 0xE1) {
  257. /* The Pause key sends a strange scan code sequence, which is:
  258. *
  259. * E1 1D 52 E1 9D D2
  260. *
  261. * However there is never any release code nor any auto-repeat for
  262. * this key. For this reason we simply ignore the key and skip the
  263. * next 5 scan codes read from the keyboard.
  264. */
  265. pauseLoop = 5;
  266. }
  267. else {
  268. /* Process the scan code normally (it may be an extended code
  269. * however!). Bit 7 means key was released, and bits 0-6 are the
  270. * scan code.
  271. */
  272. what = (scan & 0x80) ? EVT_KEYUP : EVT_KEYDOWN;
  273. scan &= 0x7F;
  274. if (extended) {
  275. extended = 0;
  276. if (scan == 0x2A || scan == 0x36) {
  277. /* Ignore these extended scan code sequences. These are
  278. * used by the keyboard controller to wrap around certain
  279. * key sequences for the keypad (and when NUMLOCK is down
  280. * internally).
  281. */
  282. return;
  283. }
  284. /* Convert extended codes for key sequences that we map to
  285. * virtual scan codes so the user can detect them in their
  286. * code.
  287. */
  288. switch (scan) {
  289. case KB_leftCtrl: scan = KB_rightCtrl; break;
  290. case KB_leftAlt: scan = KB_rightAlt; break;
  291. case KB_divide: scan = KB_padDivide; break;
  292. case KB_enter: scan = KB_padEnter; break;
  293. case KB_padTimes: scan = KB_sysReq; break;
  294. }
  295. }
  296. else {
  297. /* Convert regular scan codes for key sequences that we map to
  298. * virtual scan codes so the user can detect them in their
  299. * code.
  300. */
  301. switch (scan) {
  302. case KB_left: scan = KB_padLeft; break;
  303. case KB_right: scan = KB_padRight; break;
  304. case KB_up: scan = KB_padUp; break;
  305. case KB_down: scan = KB_padDown; break;
  306. case KB_insert: scan = KB_padInsert; break;
  307. case KB_delete: scan = KB_padDelete; break;
  308. case KB_home: scan = KB_padHome; break;
  309. case KB_end: scan = KB_padEnd; break;
  310. case KB_pageUp: scan = KB_padPageUp; break;
  311. case KB_pageDown: scan = KB_padPageDown; break;
  312. }
  313. }
  314. /* Determine if the key is an UP, DOWN or REPEAT and maintain the
  315. * up/down status of all keys in our global key array.
  316. */
  317. if (what == EVT_KEYDOWN) {
  318. if (EVT.keyTable[scan])
  319. what = EVT_KEYREPEAT;
  320. else
  321. EVT.keyTable[scan] = scan;
  322. }
  323. else {
  324. EVT.keyTable[scan] = 0;
  325. }
  326. /* Handle shift key modifiers */
  327. if (what != EVT_KEYREPEAT) {
  328. switch (scan) {
  329. case KB_capsLock:
  330. if (what == EVT_KEYDOWN)
  331. EVT.keyModifiers ^= EVT_CAPSLOCK;
  332. setLEDS(EVT.keyModifiers);
  333. break;
  334. case KB_numLock:
  335. if (what == EVT_KEYDOWN)
  336. EVT.keyModifiers ^= EVT_NUMLOCK;
  337. setLEDS(EVT.keyModifiers);
  338. break;
  339. case KB_scrollLock:
  340. if (what == EVT_KEYDOWN)
  341. EVT.keyModifiers ^= EVT_SCROLLLOCK;
  342. setLEDS(EVT.keyModifiers);
  343. break;
  344. case KB_leftShift:
  345. if (what == EVT_KEYUP)
  346. EVT.keyModifiers &= ~EVT_LEFTSHIFT;
  347. else
  348. EVT.keyModifiers |= EVT_LEFTSHIFT;
  349. break;
  350. case KB_rightShift:
  351. if (what == EVT_KEYUP)
  352. EVT.keyModifiers &= ~EVT_RIGHTSHIFT;
  353. else
  354. EVT.keyModifiers |= EVT_RIGHTSHIFT;
  355. break;
  356. case KB_leftCtrl:
  357. if (what == EVT_KEYUP)
  358. EVT.keyModifiers &= ~EVT_LEFTCTRL;
  359. else
  360. EVT.keyModifiers |= EVT_LEFTCTRL;
  361. break;
  362. case KB_rightCtrl:
  363. if (what == EVT_KEYUP)
  364. EVT.keyModifiers &= ~EVT_RIGHTCTRL;
  365. else
  366. EVT.keyModifiers |= EVT_RIGHTCTRL;
  367. break;
  368. case KB_leftAlt:
  369. if (what == EVT_KEYUP)
  370. EVT.keyModifiers &= ~EVT_LEFTALT;
  371. else
  372. EVT.keyModifiers |= EVT_LEFTALT;
  373. break;
  374. case KB_rightAlt:
  375. if (what == EVT_KEYUP)
  376. EVT.keyModifiers &= ~EVT_RIGHTALT;
  377. else
  378. EVT.keyModifiers |= EVT_RIGHTALT;
  379. break;
  380. #ifdef SUPPORT_CTRL_ALT_DEL
  381. case KB_delete:
  382. if ((EVT.keyModifiers & EVT_CTRLSTATE) && (EVT.keyModifiers & EVT_ALTSTATE))
  383. Reboot();
  384. break;
  385. #endif
  386. }
  387. }
  388. /* Add the untranslated key code to the event queue. All
  389. * translation to ASCII from the key codes occurs when the key
  390. * is extracted from the queue, saving time in the low level
  391. * interrupt handler.
  392. */
  393. addKeyEvent(what,scan << 8);
  394. }
  395. }
  396. /****************************************************************************
  397. DESCRIPTION:
  398. Enables/disables the update of the keyboard LED status indicators.
  399. HEADER:
  400. event.h
  401. PARAMETERS:
  402. enable - True to enable, false to disable
  403. REMARKS:
  404. Enables the update of the keyboard LED status indicators. Sometimes it may
  405. be convenient in the application to turn off the updating of the LED
  406. status indicators (such as if a game is using the CAPSLOCK key for some
  407. function). Passing in a value of FALSE to this function will turn off all
  408. the LEDS, and stop updating them when the internal status changes (note
  409. however that internally we still keep track of the toggle key status!).
  410. ****************************************************************************/
  411. void EVTAPI EVT_allowLEDS(
  412. ibool enable)
  413. {
  414. EVT.allowLEDS = true;
  415. if (enable)
  416. setLEDS(EVT.keyModifiers);
  417. else
  418. setLEDS(0);
  419. EVT.allowLEDS = enable;
  420. }