KEYBOARD.CPP 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533
  1. //
  2. // Copyright 2020 Electronic Arts Inc.
  3. //
  4. // TiberianDawn.DLL and RedAlert.dll and corresponding source code is free
  5. // software: you can redistribute it and/or modify it under the terms of
  6. // the GNU General Public License as published by the Free Software Foundation,
  7. // either version 3 of the License, or (at your option) any later version.
  8. // TiberianDawn.DLL and RedAlert.dll and corresponding source code is distributed
  9. // in the hope that it will be useful, but with permitted additional restrictions
  10. // under Section 7 of the GPL. See the GNU General Public License in LICENSE.TXT
  11. // distributed with this program. You should have received a copy of the
  12. // GNU General Public License along with permitted additional restrictions
  13. // with this program. If not, see https://github.com/electronicarts/CnC_Remastered_Collection
  14. /***********************************************************************************************
  15. * *
  16. * Project Name : Westwood Keyboard Library *
  17. * *
  18. * File Name : KEYBOARD.CPP *
  19. * *
  20. * Programmer : Philip W. Gorrow *
  21. * *
  22. * Start Date : 10/16/95 *
  23. * *
  24. * Last Update : October 26, 1995 [] *
  25. * *
  26. *---------------------------------------------------------------------------------------------*
  27. * Functions: *
  28. * WWKeyboardClass::Put -- Logic to insert a key into the keybuffer] *
  29. * WWKeyboardClass::Get -- Logic to get a metakey from the buffer *
  30. * WWKeyboardClass::Check -- Checks to see if a key is in the buffer *
  31. * WWKeyboardClass::Put_Key_Message -- Translates and inserts wParam into Keyboard Buffer *
  32. * WWKeyboardClass::Buff_Get -- Lowlevel function to get a key from key buffer *
  33. * WWKeyboardClass::Get_Mouse_X -- Returns the mouses current x position in pixels *
  34. * WWKeyboardClass::Get_Mouse_Y -- returns the mouses current y position in pixels *
  35. * WWKeyboardClass::Get_Mouse_XY -- Returns the mouses x,y position via reference vars *
  36. * Check_Key -- compatability routine for old 32 bit library *
  37. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  38. #include "keyboard.h"
  39. #include "timer.h"
  40. #include "mono.h"
  41. void Message_Loop(void);
  42. WWKeyboardClass *_Kbd;
  43. /***********************************************************************************************
  44. * WWKeyboardClass::WWKeyBoardClass -- Construction for Westwood Keyboard Class *
  45. * *
  46. * INPUT: none *
  47. * *
  48. * OUTPUT: none *
  49. * *
  50. * HISTORY: *
  51. * 10/16/1995 PWG : Created. *
  52. *=============================================================================================*/
  53. WWKeyboardClass::WWKeyboardClass(void)
  54. {
  55. _Kbd = this;
  56. //
  57. // Initialize the keyboard remap table for our system (note it would be bad if someone
  58. // switched keyboard modes after this happened.
  59. //
  60. memset(VKRemap, 0, 256);
  61. memset(AsciiRemap, 0, 2048);
  62. short lp;
  63. for (lp = 31; lp < 255; lp ++) {
  64. if (isprint(lp)) {
  65. int vk_key = VkKeyScan((unsigned char)lp);
  66. if (vk_key > 0 && vk_key < 2048) {
  67. AsciiRemap[vk_key] = (unsigned char)lp;
  68. VKRemap[lp] = (unsigned char)(vk_key & 0xFF);
  69. }
  70. }
  71. }
  72. //
  73. // Build a remap table of the different keys which are affected by the caps lock and
  74. // the num lock.
  75. //
  76. memset(ToggleKeys, 0, 256);
  77. for (lp = 0; lp < 255; lp++ ) {
  78. if (isalpha(lp) && isupper(lp)) {
  79. ToggleKeys[lp] = 1;
  80. }
  81. if (lp >= VK_NUMPAD0 && lp <= VK_DIVIDE) {
  82. ToggleKeys[lp] = 2;
  83. }
  84. }
  85. //
  86. // Our buffer should start devoid of keys.
  87. //
  88. memset(Buffer, 0, 256);
  89. Head = 0;
  90. Tail = 0;
  91. //
  92. // There should be no starting queued mouse events for us to have to worry
  93. // about.
  94. //
  95. MouseQX = 0;
  96. MouseQY = 0;
  97. MState = 0;
  98. Conditional = 0;
  99. CurrentCursor = NULL;
  100. }
  101. /***********************************************************************************************
  102. * WWKeyboardClass::Buff_Get -- Lowlevel function to get a key from key buffer *
  103. * *
  104. * INPUT: none *
  105. * *
  106. * OUTPUT: int - the key value that was pulled from buffer (includes bits) * *
  107. * *
  108. * WARNINGS: If the key was a mouse event MouseQX and MouseQY will be updated *
  109. * *
  110. * HISTORY: *
  111. * 10/17/1995 PWG : Created. *
  112. *=============================================================================================*/
  113. int WWKeyboardClass::Buff_Get(void)
  114. {
  115. while (!Check()) {} // wait for key in buffer
  116. int temp = Buffer[Head]; // get key out of the buffer
  117. int newhead = Head; // save off head for manipulation
  118. if (Is_Mouse_Key(temp)) { // if key is a mouse then
  119. MouseQX = Buffer[(Head + 1) & 255]; // get the x and y pos
  120. MouseQY = Buffer[(Head + 2) & 255]; // from the buffer
  121. newhead += 3; // adjust head forward
  122. } else {
  123. newhead += 1; // adjust head forward
  124. }
  125. newhead &= 255;
  126. Head = newhead;
  127. return(temp);
  128. }
  129. BOOL WWKeyboardClass::Is_Mouse_Key(int key)
  130. {
  131. key &= 0xFF;
  132. return (key == VK_LBUTTON || key == VK_MBUTTON || key == VK_RBUTTON);
  133. }
  134. /***********************************************************************************************
  135. * WWKeyboardClass::Check -- Checks to see if a key is in the buffer *
  136. * *
  137. * INPUT: *
  138. * *
  139. * OUTPUT: *
  140. * *
  141. * WARNINGS: *
  142. * *
  143. * HISTORY: *
  144. * 10/16/1995 PWG : Created. *
  145. *=============================================================================================*/
  146. BOOL WWKeyboardClass::Check(void)
  147. {
  148. Message_Loop();
  149. unsigned short temp; // store temp holding spot for key
  150. if (Head == Tail) return(FALSE); // if no keys in buff then get out
  151. temp = Buffer[Head]; // get key out of the buffer
  152. return(temp); // send it back to main program
  153. }
  154. /***********************************************************************************************
  155. * WWKeyboardClass::Get -- Logic to get a metakey from the buffer *
  156. * *
  157. * INPUT: none *
  158. * *
  159. * OUTPUT: int - the meta key taken from the buffer. *
  160. * *
  161. * WARNINGS: This routine will not return until a keypress is received *
  162. * *
  163. * HISTORY: *
  164. * 10/16/1995 PWG : Created. *
  165. *=============================================================================================*/
  166. int WWKeyboardClass::Get(void)
  167. {
  168. int temp,bits; // store temp holding spot for key
  169. while (!Check()) {} // wait for key in buffer
  170. temp = Buff_Get(); // get key from the buffer
  171. bits = temp & 0xFF00; // save of keyboard bits
  172. if (!(bits & WWKEY_VK_BIT)) { // if its not a virtual key
  173. temp = AsciiRemap[temp&0x1FF] | bits; // convert to ascii equivalent
  174. }
  175. return(temp); // return the key that we pulled out
  176. }
  177. /***********************************************************************************************
  178. * WWKeyboardClass::Put -- Logic to insert a key into the keybuffer] *
  179. * *
  180. * INPUT: int - the key to insert into the buffer *
  181. * *
  182. * OUTPUT: bool - true if key is sucessfuly inserted. *
  183. * *
  184. * WARNINGS: none *
  185. * *
  186. * HISTORY: *
  187. * 10/16/1995 PWG : Created. *
  188. *=============================================================================================*/
  189. BOOL WWKeyboardClass::Put(int key)
  190. {
  191. int temp = (Tail + 1) & 255;
  192. if (temp != Head)
  193. {
  194. Buffer[Tail] = (short)key;
  195. //
  196. // Critical Line
  197. //
  198. Tail = temp;
  199. return(TRUE);
  200. }
  201. return(FALSE);
  202. }
  203. /***********************************************************************************************
  204. * WWKeyboardClass::Put_Key_Message -- Translates and inserts wParam into Keyboard Buffer *
  205. * *
  206. * INPUT: *
  207. * *
  208. * OUTPUT: *
  209. * *
  210. * WARNINGS: *
  211. * *
  212. * HISTORY: *
  213. * 10/16/1995 PWG : Created. *
  214. *=============================================================================================*/
  215. BOOL WWKeyboardClass::Put_Key_Message(UINT vk_key, BOOL release, BOOL dbl)
  216. {
  217. int bits = 0;
  218. //
  219. // Get the status of all of the different keyboard modifiers. Note, only pay attention
  220. // to numlock and caps lock if we are dealing with a key that is affected by them. Note
  221. // that we do not want to set the shift, ctrl and alt bits for Mouse keypresses as this
  222. // would be incompatible with the dos version.
  223. //
  224. if (vk_key != VK_LBUTTON && vk_key != VK_MBUTTON && vk_key != VK_RBUTTON) {
  225. int shift = (GetKeyState(VK_SHIFT) & 0xFF00) != 0;
  226. int ctrl = (GetKeyState(VK_CONTROL) & 0xFF00) != 0;
  227. int alt = (GetKeyState(VK_MENU) & 0xFF00) != 0;
  228. int caps = ((GetKeyState(VK_CAPITAL) & 0x00FF) != 0) && (ToggleKeys[vk_key] == 1);
  229. int nums = ((GetKeyState(VK_NUMLOCK) & 0x00FF) != 0) && (ToggleKeys[vk_key] == 2);
  230. //
  231. // Set the proper bits for whatever the key we got is.
  232. //
  233. if (shift || caps || nums) {
  234. bits |= WWKEY_SHIFT_BIT;
  235. }
  236. if (ctrl) {
  237. bits |= WWKEY_CTRL_BIT;
  238. }
  239. if (alt) {
  240. bits |= WWKEY_ALT_BIT;
  241. }
  242. }
  243. if (!AsciiRemap[vk_key|bits]) {
  244. bits |= WWKEY_VK_BIT;
  245. }
  246. if (release) {
  247. bits |= WWKEY_RLS_BIT;
  248. }
  249. if (dbl) {
  250. bits |= WWKEY_DBL_BIT;
  251. }
  252. //
  253. // Finally use the put command to enter the key into the keyboard
  254. // system.
  255. //
  256. return(Put(vk_key|bits));
  257. }
  258. void WWKeyboardClass::Clear(void)
  259. {
  260. Head = Tail;
  261. }
  262. int WWKeyboardClass::To_ASCII(int key)
  263. {
  264. if ( key && WWKEY_RLS_BIT)
  265. return(KN_NONE);
  266. return(key);
  267. }
  268. int WWKeyboardClass::Down(int key)
  269. {
  270. return(GetAsyncKeyState(key&0xFF));
  271. }
  272. VOID WWKeyboardClass::Split(int &key, int &shift, int &ctrl, int &alt, int &rls, int &dbl)
  273. {
  274. shift = (key & WWKEY_SHIFT_BIT) != 0;
  275. ctrl = (key & WWKEY_CTRL_BIT) != 0;
  276. alt = (key & WWKEY_ALT_BIT) != 0;
  277. rls = (key & WWKEY_RLS_BIT) != 0;
  278. dbl = (key & WWKEY_DBL_BIT) != 0;
  279. key = (key & 0xFF);
  280. }
  281. extern "C" {
  282. void __cdecl Stop_Execution (void);
  283. }
  284. //#pragma off(unreferenced)
  285. void WWKeyboardClass::Message_Handler(HWND , UINT message, UINT wParam, LONG lParam)
  286. {
  287. return;
  288. #if (0) // ST - 12/20/2018 11:27AM
  289. switch (message) {
  290. case WM_SYSKEYDOWN:
  291. case WM_KEYDOWN:
  292. if ( wParam==VK_SCROLL ){
  293. Stop_Execution();
  294. } else {
  295. Put_Key_Message(wParam);
  296. }
  297. break;
  298. case WM_SYSKEYUP:
  299. case WM_KEYUP:
  300. Put_Key_Message(wParam, TRUE);
  301. break;
  302. case WM_LBUTTONDOWN:
  303. Put_Key_Message(VK_LBUTTON);
  304. Put(LOWORD(lParam));
  305. Put(HIWORD(lParam));
  306. break;
  307. case WM_LBUTTONUP:
  308. Put_Key_Message(VK_LBUTTON, TRUE);
  309. Put(LOWORD(lParam));
  310. Put(HIWORD(lParam));
  311. break;
  312. case WM_LBUTTONDBLCLK:
  313. Put_Key_Message(VK_LBUTTON, TRUE, TRUE);
  314. Put(LOWORD(lParam));
  315. Put(HIWORD(lParam));
  316. break;
  317. case WM_MBUTTONDOWN:
  318. Put_Key_Message(VK_MBUTTON);
  319. Put(LOWORD(lParam));
  320. Put(HIWORD(lParam));
  321. break;
  322. case WM_MBUTTONUP:
  323. Put_Key_Message(VK_MBUTTON, TRUE);
  324. Put(LOWORD(lParam));
  325. Put(HIWORD(lParam));
  326. break;
  327. case WM_MBUTTONDBLCLK:
  328. Put_Key_Message(VK_MBUTTON, TRUE, TRUE);
  329. Put(LOWORD(lParam));
  330. Put(HIWORD(lParam));
  331. break;
  332. case WM_RBUTTONDOWN:
  333. Put_Key_Message(VK_RBUTTON);
  334. Put(LOWORD(lParam));
  335. Put(HIWORD(lParam));
  336. break;
  337. case WM_RBUTTONUP:
  338. Put_Key_Message(VK_RBUTTON, TRUE);
  339. Put(LOWORD(lParam));
  340. Put(HIWORD(lParam));
  341. break;
  342. case WM_RBUTTONDBLCLK:
  343. Put_Key_Message(VK_RBUTTON, TRUE, TRUE);
  344. Put(LOWORD(lParam));
  345. Put(HIWORD(lParam));
  346. break;
  347. case WM_MOUSEMOVE:
  348. if (CurrentCursor)
  349. SetCursor(CurrentCursor);
  350. break;
  351. }
  352. #endif
  353. }
  354. //#pragma on(unreferenced)
  355. void Message_Loop(void)
  356. {
  357. MSG msg;
  358. while (PeekMessage( &msg, NULL, 0, 0, PM_NOREMOVE )) {
  359. if( !GetMessage( &msg, NULL, 0, 0 ) ){
  360. return;
  361. }
  362. TranslateMessage(&msg);
  363. DispatchMessage(&msg);
  364. }
  365. }
  366. /***************************************************************************
  367. * CHECK_KEY -- compatability routine for old 32 bit library *
  368. * *
  369. * This routine checks to see if there is a key in the keyboard buffer *
  370. * and returns it to the sender if there is. It does not remove the key *
  371. * from the buffer. *
  372. * *
  373. * INPUT: none *
  374. * *
  375. * OUTPUT: The key that was pressed. *
  376. * *
  377. * WARNINGS: You must declare a WWKeyboardClass object before calling *
  378. * this routine. *
  379. * *
  380. * HISTORY: *
  381. * 10/26/1995 : Created. *
  382. *=========================================================================*/
  383. int Check_Key(void)
  384. {
  385. if (!_Kbd) return(KA_NONE);
  386. return(_Kbd->Check() & ~WWKEY_SHIFT_BIT);
  387. }
  388. void Clear_KeyBuffer(void)
  389. {
  390. if (!_Kbd) return;
  391. _Kbd->Clear();
  392. }
  393. int Check_Key_Num(void)
  394. {
  395. if (!_Kbd) return(KN_NONE);
  396. int key = _Kbd->Check();
  397. int flags = key & 0xFF00;
  398. key = key & 0x00FF;
  399. if (isupper(key)) {
  400. key = tolower(key);
  401. if ( !(flags & WWKEY_VK_BIT) ) {
  402. flags |= WWKEY_SHIFT_BIT;
  403. }
  404. }
  405. return(key | flags);
  406. }
  407. int Get_Key_Num(void)
  408. {
  409. if (!_Kbd) return(KN_NONE);
  410. int key = _Kbd->Get();
  411. int flags = key & 0xFF00;
  412. key = key & 0x00FF;
  413. if (isupper(key)) {
  414. key = tolower(key);
  415. if ( !(flags & WWKEY_VK_BIT) ) {
  416. flags |= WWKEY_SHIFT_BIT;
  417. }
  418. }
  419. return(key | flags);
  420. }
  421. int KN_To_KA(int key)
  422. {
  423. if ( key & WWKEY_RLS_BIT) {
  424. return(KA_NONE);
  425. }
  426. if (!(key & WWKEY_VK_BIT)) {
  427. int flags = key & 0xFF00;
  428. key = key & 0x00FF;
  429. if (flags & WWKEY_SHIFT_BIT) {
  430. key = toupper(key);
  431. flags &= ~WWKEY_SHIFT_BIT;
  432. }
  433. }else{
  434. /*
  435. ** If its a numeric keypad key then fix it up
  436. */
  437. if ((key & 0xff) >=VK_NUMPAD0 && (key & 0xff) <=VK_NUMPAD9){
  438. key = (key & 0xff) - VK_NUMPAD0 + KA_0;
  439. }
  440. }
  441. return(key);
  442. }
  443. int KN_To_VK(int key)
  444. {
  445. if (!_Kbd) return(KN_NONE);
  446. if ( key & WWKEY_RLS_BIT) {
  447. return(VK_NONE);
  448. }
  449. int flags = key & 0xFF00;
  450. if (!(flags & WWKEY_VK_BIT)) {
  451. key = _Kbd->VKRemap[key & 0x00FF] | flags;
  452. }
  453. key &= ~WWKEY_VK_BIT;
  454. return(key);
  455. }
  456. int Key_Down(int key)
  457. {
  458. if (!_Kbd) return(FALSE);
  459. return(_Kbd->Down(key));
  460. }
  461. int Get_Key(void)
  462. {
  463. int retval;
  464. if (!_Kbd) return(KN_NONE);
  465. retval = _Kbd->Get() & ~WWKEY_SHIFT_BIT;
  466. if (retval & WWKEY_RLS_BIT) {
  467. retval = KN_NONE;
  468. }
  469. return(retval);
  470. }