Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

567 lines
12 KiB

  1. /* KBD.C */
  2. //#define WINVER 0x0300
  3. #include <string.h>
  4. #include <stdlib.h>
  5. #include "windows.h"
  6. //#include "winstric.h" /* added for win 3.1 compatibility 1/92 */
  7. #include "gidei.h"
  8. #include "vars.h"
  9. #include "gide.h"
  10. #include "kbd.h"
  11. #include "tables.h"
  12. #include "sk_ex.h"
  13. BOOL IsInBuff(char *buf, unsigned char SerchChar, int Len);
  14. void sendDownKeyCode(unsigned char cKeyCode)
  15. {
  16. int scanCode;
  17. if (cKeyCode == NOKEY)
  18. return;
  19. if ((scanCode = IBMextendedScanCodeSet1[cKeyCode]) == 0)
  20. return;
  21. SkEx_SendKeyDown(scanCode);
  22. }
  23. void sendUpKeyCode(unsigned char cKeyCode)
  24. {
  25. int scanCode;
  26. if (cKeyCode == NOKEY)
  27. return;
  28. if ((scanCode = IBMextendedScanCodeSet1[cKeyCode]) == 0)
  29. return;
  30. SkEx_SendKeyUp(scanCode);
  31. }
  32. void sendExtendedKey(unsigned char cKeyCode)
  33. {
  34. unsigned char Key[4], Tmp;
  35. // Start with the Alt Key
  36. Key[0] = ralt_key;
  37. Tmp = cKeyCode/10; // Calc One's
  38. Key[3] = cKeyCode - (Tmp * 10);
  39. cKeyCode = Tmp; // Calc Ten's
  40. Tmp /= 10;
  41. Key[2] = cKeyCode - (Tmp * 10);
  42. Key[1] = Tmp; // Calc Hundreds
  43. // Translate Numbers into ScanCodes
  44. Key[1] = xlateNumToScanCode(Key[1]);
  45. Key[2] = xlateNumToScanCode(Key[2]);
  46. Key[3] = xlateNumToScanCode(Key[3]);
  47. // Send Keys to Host
  48. sendDownKeyCode(Key[0]); // Send Alt Key Down
  49. sendDownKeyCode(Key[1]); // Send Hundreds Key Down
  50. sendDownKeyCode(Key[2]); // Send Tens Key Down
  51. sendDownKeyCode(Key[3]); // Send Ones Key Down
  52. sendUpKeyCode(Key[3]);
  53. sendUpKeyCode(Key[2]);
  54. sendUpKeyCode(Key[1]);
  55. sendUpKeyCode(Key[0]);
  56. }
  57. unsigned char xlateNumToScanCode(unsigned char Value)
  58. {
  59. switch (Value)
  60. {
  61. case 0: return(kp0_key);
  62. case 1: return(kp1_key);
  63. case 2: return(kp2_key);
  64. case 3: return(kp3_key);
  65. case 4: return(kp4_key);
  66. case 5: return(kp5_key);
  67. case 6: return(kp6_key);
  68. case 7: return(kp7_key);
  69. case 8: return(kp8_key);
  70. case 9: return(kp9_key);
  71. }
  72. // should never be reached
  73. return 0;
  74. }
  75. void sendPressList(void)
  76. {
  77. int i;
  78. for (i=0; i < keyHoldList.len; sendDownKeyCode(keyHoldList.list[i++]));
  79. for (i=0; i < tempList.len; i++) {
  80. sendDownKeyCode(tempList.list[i]);
  81. sendUpKeyCode(tempList.list[i]);
  82. }
  83. for (i=keyHoldList.len; i > 0; sendUpKeyCode(keyHoldList.list[--i]));
  84. keyHoldList.len = tempList.len = 0;
  85. return;
  86. }
  87. void sendCombineList(void)
  88. {
  89. int i;
  90. for (i=0; i < keyHoldList.len; sendDownKeyCode(keyHoldList.list[i++]));
  91. for (i=0; i < tempList.len; sendDownKeyCode(tempList.list[i++]));
  92. for (i=tempList.len; i > 0; sendUpKeyCode(tempList.list[--i]));
  93. for (i=keyHoldList.len; i > 0; sendUpKeyCode(keyHoldList.list[--i]));
  94. keyHoldList.len = tempList.len = 0;
  95. return;
  96. }
  97. int inLockList(unsigned char searchChar)
  98. {
  99. return(IsInBuff(keyLockList.list,searchChar,keyLockList.len));
  100. }
  101. int inHoldList(unsigned char searchChar)
  102. {
  103. return(IsInBuff(keyHoldList.list,searchChar,keyHoldList.len));
  104. }
  105. int inTempList(unsigned char searchChar)
  106. {
  107. return(IsInBuff(tempList.list,searchChar,tempList.len));
  108. }
  109. BOOL IsInBuff(char *buf, unsigned char SearchChar, int Len)
  110. {
  111. int x = 0;
  112. if (!Len) // Are there any Chars to search?
  113. return(FALSE); // No - Return False
  114. while (x < Len) // Loop until num chars reached
  115. {
  116. if (*buf == SearchChar) // Does buffer and search char match?
  117. return(TRUE); // Yes - Return found it.
  118. buf++; // Inc buffer;
  119. x++; // Inc byte count
  120. }
  121. return(FALSE); // character not found in buffer
  122. }
  123. void releaseKeysFromHoldList(void)
  124. {
  125. unsigned char chRemove;
  126. int iScan,iSrc,iDst;
  127. if (tempList.len)
  128. {
  129. // Go through each character in the list of items to remove...
  130. for (iScan=0; (iScan<tempList.len) && ((chRemove = tempList.list[iScan]) != DEFAULTCODE); iScan++)
  131. {
  132. // For each character to remove, copy the hold list to itself...
  133. iDst = 0;
  134. for (iSrc=0; iSrc < keyHoldList.len; iSrc++)
  135. {
  136. // ... unless it's the character we're removing. This removes that char,
  137. // and shuffles all the other items down...
  138. if (keyHoldList.list[iSrc] != chRemove)
  139. {
  140. keyHoldList.list[iDst] = keyHoldList.list[iSrc];
  141. iDst++;
  142. }
  143. }
  144. // Update length of hold array to reflect number of chars that survived removal...
  145. keyHoldList.len = iDst;
  146. }
  147. if (tempList.list[iScan] == DEFAULTCODE)
  148. {
  149. keyHoldList.len = 0;
  150. }
  151. }
  152. return;
  153. }
  154. void removeKeyFromHoldList(unsigned char cTheKey)
  155. {
  156. // unsigned char cTemp;
  157. int j,k;
  158. if (cTheKey != NOKEY) {
  159. k = 0;
  160. for (j=0; j < keyHoldList.len; j++)
  161. if ((keyHoldList.list[k] = keyHoldList.list[j]) != cTheKey) k++;
  162. keyHoldList.len = k;
  163. }
  164. return;
  165. }
  166. void releaseKeysFromLockList(void)
  167. {
  168. int iScan,iSrc,iDst;
  169. unsigned char chRemove;
  170. if (tempList.len)
  171. {
  172. // Go through each character in the list of items to remove...
  173. for (iScan=0; (iScan<tempList.len) && ((chRemove = tempList.list[iScan]) != DEFAULTCODE); iScan++)
  174. {
  175. // For each character to remove, copy the lock list to itself...
  176. iDst = 0;
  177. for (iSrc=0; iSrc < keyLockList.len; iSrc++)
  178. {
  179. // ... unless it's the character we're removing. This removes that char,
  180. // and shuffles all the other items down...
  181. if (keyLockList.list[iSrc] != chRemove)
  182. {
  183. keyLockList.list[iDst] = keyLockList.list[iSrc];
  184. iDst++;
  185. }
  186. else
  187. {
  188. sendUpKeyCode(chRemove);
  189. }
  190. }
  191. // Update length of lock array to reflect number of chars that survived removal...
  192. keyLockList.len = iDst;
  193. }
  194. if (tempList.list[iScan] == DEFAULTCODE)
  195. {
  196. for (iScan=0; iScan < keyLockList.len; iScan++)
  197. {
  198. sendUpKeyCode(keyLockList.list[iScan]);
  199. }
  200. tempList.len = keyLockList.len = 0;
  201. }
  202. }
  203. return;
  204. }
  205. void processKbdIndicator(unsigned char cGideiCode)
  206. {
  207. return;
  208. }
  209. void processKbdVersion(unsigned char cGideiCode)
  210. {
  211. return;
  212. }
  213. void processKbdDescription(unsigned char cGideiCode)
  214. {
  215. return;
  216. }
  217. void processKbdUnknown(unsigned char cGideiCode)
  218. {
  219. return;
  220. }
  221. void processKbdModel(unsigned char cGideiCode)
  222. {
  223. switch (cGideiCode) {
  224. case TERMCODE:
  225. break;
  226. default:
  227. break;
  228. }
  229. return;
  230. }
  231. void processKbdRel(unsigned char cGideiCode)
  232. {
  233. unsigned char iKeyNumber;
  234. switch (cGideiCode)
  235. {
  236. case TERMCODE:
  237. if (!tempList.len)
  238. {
  239. tempList.list[0] = DEFAULTCODE;
  240. ++tempList.len;
  241. }
  242. releaseKeysFromLockList();
  243. releaseKeysFromHoldList();
  244. tempList.len = 0;
  245. break;
  246. case UNKNOWNCODE:
  247. handleErrorReport();
  248. commandVector = noOpRoutine;
  249. tempList.len = 0;
  250. beginOK = TRUE;
  251. break;
  252. default:
  253. if ((cGideiCode >= LOWESTGIDEICODE) && (cGideiCode != DEFAULTCODE))
  254. {
  255. handleFatalError();
  256. break;
  257. }
  258. if (tempList.len >= MAXLISTLENGTH)
  259. {
  260. handleErrorReport();
  261. commandVector = noOpRoutine;
  262. tempList.len = 0;
  263. break;
  264. }
  265. if (cGideiCode == DEFAULTCODE)
  266. iKeyNumber = DEFAULTCODE;
  267. else
  268. {
  269. if ((iKeyNumber=cGideiCode) == NOKEY)
  270. {
  271. handleErrorReport();
  272. commandVector = noOpRoutine;
  273. tempList.len = 0;
  274. break;
  275. }
  276. if ((inLockList(iKeyNumber)) || (inHoldList(iKeyNumber)))
  277. iKeyNumber = NOKEY;
  278. }
  279. if (!inTempList(iKeyNumber)) tempList.list[tempList.len++] = iKeyNumber;
  280. beginOK = FALSE;
  281. break;
  282. }
  283. return;
  284. }
  285. void processKbdLock(unsigned char cGideiCode)
  286. {
  287. int i;
  288. unsigned char iKeyNumber;
  289. unsigned char temp;
  290. switch (cGideiCode) {
  291. case TERMCODE:
  292. for (i=0; i < tempList.len; i++) {
  293. if (keyLockList.len<MAXLISTLENGTH &&
  294. (temp = tempList.list[i]) != NOKEY) {
  295. keyLockList.list[keyLockList.len++] = temp;
  296. sendDownKeyCode(temp);
  297. if (inHoldList(temp)) removeKeyFromHoldList(temp);
  298. }
  299. }
  300. if (tempList.len == 0) handleErrorReport();
  301. tempList.len = 0;
  302. break;
  303. case UNKNOWNCODE:
  304. handleErrorReport();
  305. commandVector = noOpRoutine;
  306. tempList.len = 0;
  307. beginOK = TRUE;
  308. break;
  309. default:
  310. if (cGideiCode >= (int)LOWESTGIDEICODE) {
  311. handleFatalError();
  312. break;
  313. }
  314. if ((keyLockList.len + tempList.len) >= MAXLISTLENGTH) {
  315. handleErrorReport();
  316. commandVector = noOpRoutine;
  317. tempList.len = 0;
  318. break;
  319. }
  320. if ((iKeyNumber=cGideiCode) == NOKEY) {
  321. handleErrorReport();
  322. commandVector = noOpRoutine;
  323. tempList.len = 0;
  324. break;
  325. }
  326. if (inLockList(iKeyNumber)) iKeyNumber = NOKEY;
  327. if (!inTempList(iKeyNumber)) tempList.list[tempList.len++] = iKeyNumber;
  328. beginOK = FALSE;
  329. break;
  330. }
  331. }
  332. void processKbdHold(unsigned char cGideiCode)
  333. {
  334. int i;
  335. unsigned char iKeyNumber;
  336. switch (cGideiCode) {
  337. case TERMCODE:
  338. for (i=0; i < tempList.len; i++)
  339. if (keyHoldList.len<MAXLISTLENGTH &&
  340. (keyHoldList.list[keyHoldList.len] = tempList.list[i]) != NOKEY)
  341. ++(keyHoldList.len);
  342. if (tempList.len == 0) handleErrorReport();
  343. tempList.len = 0;
  344. break;
  345. case UNKNOWNCODE:
  346. handleErrorReport();
  347. commandVector = noOpRoutine;
  348. tempList.len = 0;
  349. beginOK = TRUE;
  350. break;
  351. default:
  352. if (cGideiCode >= (int)LOWESTGIDEICODE) {
  353. handleFatalError();
  354. break;
  355. }
  356. if ((keyHoldList.len + tempList.len) >= MAXLISTLENGTH) {
  357. handleErrorReport();
  358. commandVector = noOpRoutine;
  359. tempList.len = 0;
  360. break;
  361. }
  362. if ((iKeyNumber=cGideiCode) == NOKEY) {
  363. handleErrorReport();
  364. commandVector = noOpRoutine;
  365. tempList.len = 0;
  366. break;
  367. }
  368. if ((inLockList(iKeyNumber)) || (inHoldList(iKeyNumber))) iKeyNumber = NOKEY;
  369. if (!inTempList(iKeyNumber)) tempList.list[tempList.len++] = iKeyNumber;
  370. beginOK = FALSE;
  371. break;
  372. }
  373. }
  374. void processKbdCombine(unsigned char cGideiCode)
  375. {
  376. unsigned char iKeyNumber;
  377. switch (cGideiCode) {
  378. case TERMCODE:
  379. sendCombineList();
  380. keyHoldList.len = tempList.len = 0;
  381. break;
  382. case UNKNOWNCODE:
  383. handleErrorReport();
  384. commandVector = noOpRoutine;
  385. tempList.len = 0;
  386. beginOK = TRUE;
  387. break;
  388. default:
  389. if (cGideiCode >= LOWESTGIDEICODE) {
  390. handleFatalError();
  391. break;
  392. }
  393. if (tempList.len >= MAXLISTLENGTH) {
  394. handleErrorReport();
  395. commandVector = noOpRoutine;
  396. tempList.len = 0;
  397. break;
  398. }
  399. if ((iKeyNumber=cGideiCode) == NOKEY) {
  400. handleErrorReport();
  401. commandVector = noOpRoutine;
  402. tempList.len = 0;
  403. break;
  404. }
  405. if ((inLockList(iKeyNumber)) || (inHoldList(iKeyNumber))) iKeyNumber = NOKEY;
  406. if (!inTempList(iKeyNumber)) tempList.list[tempList.len++] = iKeyNumber;
  407. beginOK = FALSE;
  408. break;
  409. }
  410. }
  411. void processKbdPress(unsigned char cGideiCode)
  412. {
  413. unsigned char iKeyNumber;
  414. switch (cGideiCode) {
  415. case TERMCODE:
  416. sendPressList();
  417. keyHoldList.len = tempList.len = 0;
  418. break;
  419. case UNKNOWNCODE:
  420. handleErrorReport();
  421. commandVector = noOpRoutine;
  422. tempList.len = 0;
  423. beginOK = TRUE;
  424. break;
  425. default:
  426. if (cGideiCode >= LOWESTGIDEICODE) {
  427. handleFatalError();
  428. break;
  429. }
  430. if (tempList.len >= MAXLISTLENGTH) {
  431. handleErrorReport();
  432. commandVector = noOpRoutine;
  433. tempList.len = 0;
  434. break;
  435. }
  436. if ((iKeyNumber=cGideiCode) == NOKEY) {
  437. handleErrorReport();
  438. commandVector = noOpRoutine;
  439. tempList.len = 0;
  440. break;
  441. }
  442. if ((inLockList(iKeyNumber)) || (inHoldList(iKeyNumber))) iKeyNumber = NOKEY;
  443. tempList.list[tempList.len++] = iKeyNumber;
  444. beginOK = FALSE;
  445. break;
  446. }
  447. }
  448. void processKbd(unsigned char cGideiCode)
  449. {
  450. switch (cGideiCode) {
  451. case KBDINDICATORCODE:
  452. commandVector = processKbdIndicator;
  453. aliasPtr = kbdIndicatorAliasTable;
  454. beginOK = TRUE;
  455. break;
  456. case KBDVERSIONCODE:
  457. commandVector = processKbdVersion;
  458. aliasPtr = kbdVersionAliasTable;
  459. beginOK = TRUE;
  460. break;
  461. case KBDMODELCODE:
  462. commandVector = processKbdModel;
  463. aliasPtr = kbdModelAliasTable;
  464. beginOK = TRUE;
  465. break;
  466. case KBDDESCRIPTIONCODE:
  467. commandVector = processKbdDescription;
  468. aliasPtr = kbdDescriptionAliasTable;
  469. beginOK = TRUE;
  470. break;
  471. /* case KBDUNKNOWNCODE:
  472. commandVector = processKbdUnknown;
  473. aliasPtr = kbdUnknownAliasTable;
  474. beginOK = TRUE;
  475. break;
  476. */
  477. default:
  478. if (cGideiCode < LOWESTGIDEICODE) handleFatalError();
  479. else {
  480. handleErrorReport();
  481. commandVector = noOpRoutine;
  482. beginOK = TRUE;
  483. }
  484. break;
  485. }
  486. return;
  487. }