KEYBOARD.CPP 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533
  1. /*
  2. ** Command & Conquer Red Alert(tm)
  3. ** Copyright 2025 Electronic Arts Inc.
  4. **
  5. ** This program is free software: you can redistribute it and/or modify
  6. ** it under the terms of the GNU General Public License as published by
  7. ** the Free Software Foundation, either version 3 of the License, or
  8. ** (at your option) any later version.
  9. **
  10. ** This program is distributed in the hope that it will be useful,
  11. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ** GNU General Public License for more details.
  14. **
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. /***********************************************************************************************
  19. * *
  20. * Project Name : Westwood Keyboard Library *
  21. * *
  22. * File Name : KEYBOARD.CPP *
  23. * *
  24. * Programmer : Philip W. Gorrow *
  25. * *
  26. * Start Date : 10/16/95 *
  27. * *
  28. * Last Update : October 26, 1995 [] *
  29. * *
  30. *---------------------------------------------------------------------------------------------*
  31. * Functions: *
  32. * WWKeyboardClass::Put -- Logic to insert a key into the keybuffer] *
  33. * WWKeyboardClass::Get -- Logic to get a metakey from the buffer *
  34. * WWKeyboardClass::Check -- Checks to see if a key is in the buffer *
  35. * WWKeyboardClass::Put_Key_Message -- Translates and inserts wParam into Keyboard Buffer *
  36. * WWKeyboardClass::Buff_Get -- Lowlevel function to get a key from key buffer *
  37. * WWKeyboardClass::Get_Mouse_X -- Returns the mouses current x position in pixels *
  38. * WWKeyboardClass::Get_Mouse_Y -- returns the mouses current y position in pixels *
  39. * WWKeyboardClass::Get_Mouse_XY -- Returns the mouses x,y position via reference vars *
  40. * Check_Key -- compatability routine for old 32 bit library *
  41. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  42. #include "keyboard.h"
  43. #include "timer.h"
  44. #include "mono.h"
  45. void Message_Loop(void);
  46. WWKeyboardClass *_Kbd;
  47. /***********************************************************************************************
  48. * WWKeyboardClass::WWKeyBoardClass -- Construction for Westwood Keyboard Class *
  49. * *
  50. * INPUT: none *
  51. * *
  52. * OUTPUT: none *
  53. * *
  54. * HISTORY: *
  55. * 10/16/1995 PWG : Created. *
  56. *=============================================================================================*/
  57. WWKeyboardClass::WWKeyboardClass(void)
  58. {
  59. _Kbd = this;
  60. //
  61. // Initialize the keyboard remap table for our system (note it would be bad if someone
  62. // switched keyboard modes after this happened.
  63. //
  64. memset(VKRemap, 0, 256);
  65. memset(AsciiRemap, 0, 2048);
  66. for (short lp = 31; lp < 255; lp ++) {
  67. if (isprint(lp)) {
  68. int vk_key = VkKeyScan((unsigned char)lp);
  69. if (vk_key > 0 && vk_key < 2048) {
  70. AsciiRemap[vk_key] = (unsigned char)lp;
  71. VKRemap[lp] = (unsigned char)(vk_key & 0xFF);
  72. }
  73. }
  74. }
  75. //
  76. // Build a remap table of the different keys which are affected by the caps lock and
  77. // the num lock.
  78. //
  79. memset(ToggleKeys, 0, 256);
  80. for (lp = 0; lp < 255; lp++ ) {
  81. if (isalpha(lp) && isupper(lp)) {
  82. ToggleKeys[lp] = 1;
  83. }
  84. if (lp >= VK_NUMPAD0 && lp <= VK_DIVIDE) {
  85. ToggleKeys[lp] = 2;
  86. }
  87. }
  88. //
  89. // Our buffer should start devoid of keys.
  90. //
  91. memset(Buffer, 0, 256);
  92. Head = 0;
  93. Tail = 0;
  94. //
  95. // There should be no starting queued mouse events for us to have to worry
  96. // about.
  97. //
  98. MouseQX = 0;
  99. MouseQY = 0;
  100. MState = 0;
  101. Conditional = 0;
  102. CurrentCursor = NULL;
  103. }
  104. /***********************************************************************************************
  105. * WWKeyboardClass::Buff_Get -- Lowlevel function to get a key from key buffer *
  106. * *
  107. * INPUT: none *
  108. * *
  109. * OUTPUT: int - the key value that was pulled from buffer (includes bits) * *
  110. * *
  111. * WARNINGS: If the key was a mouse event MouseQX and MouseQY will be updated *
  112. * *
  113. * HISTORY: *
  114. * 10/17/1995 PWG : Created. *
  115. *=============================================================================================*/
  116. int WWKeyboardClass::Buff_Get(void)
  117. {
  118. while (!Check()) {} // wait for key in buffer
  119. int temp = Buffer[Head]; // get key out of the buffer
  120. int newhead = Head; // save off head for manipulation
  121. if (Is_Mouse_Key(temp)) { // if key is a mouse then
  122. MouseQX = Buffer[(Head + 1) & 255]; // get the x and y pos
  123. MouseQY = Buffer[(Head + 2) & 255]; // from the buffer
  124. newhead += 3; // adjust head forward
  125. } else {
  126. newhead += 1; // adjust head forward
  127. }
  128. newhead &= 255;
  129. Head = newhead;
  130. return(temp);
  131. }
  132. BOOL WWKeyboardClass::Is_Mouse_Key(int key)
  133. {
  134. key &= 0xFF;
  135. return (key == VK_LBUTTON || key == VK_MBUTTON || key == VK_RBUTTON);
  136. }
  137. /***********************************************************************************************
  138. * WWKeyboardClass::Check -- Checks to see if a key is in the buffer *
  139. * *
  140. * INPUT: *
  141. * *
  142. * OUTPUT: *
  143. * *
  144. * WARNINGS: *
  145. * *
  146. * HISTORY: *
  147. * 10/16/1995 PWG : Created. *
  148. *=============================================================================================*/
  149. BOOL WWKeyboardClass::Check(void)
  150. {
  151. Message_Loop();
  152. unsigned short temp; // store temp holding spot for key
  153. if (Head == Tail) return(FALSE); // if no keys in buff then get out
  154. temp = Buffer[Head]; // get key out of the buffer
  155. return(temp); // send it back to main program
  156. }
  157. /***********************************************************************************************
  158. * WWKeyboardClass::Get -- Logic to get a metakey from the buffer *
  159. * *
  160. * INPUT: none *
  161. * *
  162. * OUTPUT: int - the meta key taken from the buffer. *
  163. * *
  164. * WARNINGS: This routine will not return until a keypress is received *
  165. * *
  166. * HISTORY: *
  167. * 10/16/1995 PWG : Created. *
  168. *=============================================================================================*/
  169. int WWKeyboardClass::Get(void)
  170. {
  171. int temp,bits; // store temp holding spot for key
  172. while (!Check()) {} // wait for key in buffer
  173. temp = Buff_Get(); // get key from the buffer
  174. bits = temp & 0xFF00; // save of keyboard bits
  175. if (!(bits & WWKEY_VK_BIT)) { // if its not a virtual key
  176. temp = AsciiRemap[temp&0x1FF] | bits; // convert to ascii equivalent
  177. }
  178. return(temp); // return the key that we pulled out
  179. }
  180. /***********************************************************************************************
  181. * WWKeyboardClass::Put -- Logic to insert a key into the keybuffer] *
  182. * *
  183. * INPUT: int - the key to insert into the buffer *
  184. * *
  185. * OUTPUT: bool - true if key is sucessfuly inserted. *
  186. * *
  187. * WARNINGS: none *
  188. * *
  189. * HISTORY: *
  190. * 10/16/1995 PWG : Created. *
  191. *=============================================================================================*/
  192. BOOL WWKeyboardClass::Put(int key)
  193. {
  194. int temp = (Tail + 1) & 255;
  195. if (temp != Head)
  196. {
  197. Buffer[Tail] = (short)key;
  198. //
  199. // Critical Line
  200. //
  201. Tail = temp;
  202. return(TRUE);
  203. }
  204. return(FALSE);
  205. }
  206. /***********************************************************************************************
  207. * WWKeyboardClass::Put_Key_Message -- Translates and inserts wParam into Keyboard Buffer *
  208. * *
  209. * INPUT: *
  210. * *
  211. * OUTPUT: *
  212. * *
  213. * WARNINGS: *
  214. * *
  215. * HISTORY: *
  216. * 10/16/1995 PWG : Created. *
  217. *=============================================================================================*/
  218. WWKeyboardClass::Put_Key_Message(UINT vk_key, BOOL release, BOOL dbl)
  219. {
  220. int bits = 0;
  221. //
  222. // Get the status of all of the different keyboard modifiers. Note, only pay attention
  223. // to numlock and caps lock if we are dealing with a key that is affected by them. Note
  224. // that we do not want to set the shift, ctrl and alt bits for Mouse keypresses as this
  225. // would be incompatible with the dos version.
  226. //
  227. if (vk_key != VK_LBUTTON && vk_key != VK_MBUTTON && vk_key != VK_RBUTTON) {
  228. int shift = (GetKeyState(VK_SHIFT) & 0xFF00) != 0;
  229. int ctrl = (GetKeyState(VK_CONTROL) & 0xFF00) != 0;
  230. int alt = (GetKeyState(VK_MENU) & 0xFF00) != 0;
  231. int caps = ((GetKeyState(VK_CAPITAL) & 0x00FF) != 0) && (ToggleKeys[vk_key] == 1);
  232. int nums = ((GetKeyState(VK_NUMLOCK) & 0x00FF) != 0) && (ToggleKeys[vk_key] == 2);
  233. //
  234. // Set the proper bits for whatever the key we got is.
  235. //
  236. if (shift || caps || nums) {
  237. bits |= WWKEY_SHIFT_BIT;
  238. }
  239. if (ctrl) {
  240. bits |= WWKEY_CTRL_BIT;
  241. }
  242. if (alt) {
  243. bits |= WWKEY_ALT_BIT;
  244. }
  245. }
  246. if (!AsciiRemap[vk_key|bits]) {
  247. bits |= WWKEY_VK_BIT;
  248. }
  249. if (release) {
  250. bits |= WWKEY_RLS_BIT;
  251. }
  252. if (dbl) {
  253. bits |= WWKEY_DBL_BIT;
  254. }
  255. //
  256. // Finally use the put command to enter the key into the keyboard
  257. // system.
  258. //
  259. return(Put(vk_key|bits));
  260. }
  261. void WWKeyboardClass::Clear(void)
  262. {
  263. Head = Tail;
  264. }
  265. int WWKeyboardClass::To_ASCII(int key)
  266. {
  267. if ( key && WWKEY_RLS_BIT)
  268. return(KN_NONE);
  269. return(key);
  270. }
  271. WWKeyboardClass::Down(int key)
  272. {
  273. return(GetAsyncKeyState(key&0xFF));
  274. }
  275. VOID WWKeyboardClass::Split(int &key, int &shift, int &ctrl, int &alt, int &rls, int &dbl)
  276. {
  277. shift = (key & WWKEY_SHIFT_BIT) != 0;
  278. ctrl = (key & WWKEY_CTRL_BIT) != 0;
  279. alt = (key & WWKEY_ALT_BIT) != 0;
  280. rls = (key & WWKEY_RLS_BIT) != 0;
  281. dbl = (key & WWKEY_DBL_BIT) != 0;
  282. key = (key & 0xFF);
  283. }
  284. extern "C" {
  285. void __cdecl Stop_Execution (void);
  286. }
  287. #pragma off(unreferenced)
  288. void WWKeyboardClass::Message_Handler(HWND , UINT message, UINT wParam, LONG lParam)
  289. {
  290. switch (message) {
  291. case WM_SYSKEYDOWN:
  292. case WM_KEYDOWN:
  293. if ( wParam==VK_SCROLL ){
  294. Stop_Execution();
  295. } else {
  296. Put_Key_Message(wParam);
  297. }
  298. break;
  299. case WM_SYSKEYUP:
  300. case WM_KEYUP:
  301. Put_Key_Message(wParam, TRUE);
  302. break;
  303. case WM_LBUTTONDOWN:
  304. Put_Key_Message(VK_LBUTTON);
  305. Put(LOWORD(lParam));
  306. Put(HIWORD(lParam));
  307. break;
  308. case WM_LBUTTONUP:
  309. Put_Key_Message(VK_LBUTTON, TRUE);
  310. Put(LOWORD(lParam));
  311. Put(HIWORD(lParam));
  312. break;
  313. case WM_LBUTTONDBLCLK:
  314. Put_Key_Message(VK_LBUTTON, TRUE, TRUE);
  315. Put(LOWORD(lParam));
  316. Put(HIWORD(lParam));
  317. break;
  318. case WM_MBUTTONDOWN:
  319. Put_Key_Message(VK_MBUTTON);
  320. Put(LOWORD(lParam));
  321. Put(HIWORD(lParam));
  322. break;
  323. case WM_MBUTTONUP:
  324. Put_Key_Message(VK_MBUTTON, TRUE);
  325. Put(LOWORD(lParam));
  326. Put(HIWORD(lParam));
  327. break;
  328. case WM_MBUTTONDBLCLK:
  329. Put_Key_Message(VK_MBUTTON, TRUE, TRUE);
  330. Put(LOWORD(lParam));
  331. Put(HIWORD(lParam));
  332. break;
  333. case WM_RBUTTONDOWN:
  334. Put_Key_Message(VK_RBUTTON);
  335. Put(LOWORD(lParam));
  336. Put(HIWORD(lParam));
  337. break;
  338. case WM_RBUTTONUP:
  339. Put_Key_Message(VK_RBUTTON, TRUE);
  340. Put(LOWORD(lParam));
  341. Put(HIWORD(lParam));
  342. break;
  343. case WM_RBUTTONDBLCLK:
  344. Put_Key_Message(VK_RBUTTON, TRUE, TRUE);
  345. Put(LOWORD(lParam));
  346. Put(HIWORD(lParam));
  347. break;
  348. case WM_MOUSEMOVE:
  349. if (CurrentCursor)
  350. SetCursor(CurrentCursor);
  351. break;
  352. }
  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. }