KEYBOARD.CPP 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  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. void Message_Loop(void);
  44. WWKeyboardClass *_Kbd;
  45. /***********************************************************************************************
  46. * WWKeyboardClass::WWKeyBoardClass -- Construction for Westwood Keyboard Class *
  47. * *
  48. * INPUT: none *
  49. * *
  50. * OUTPUT: none *
  51. * *
  52. * HISTORY: *
  53. * 10/16/1995 PWG : Created. *
  54. *=============================================================================================*/
  55. WWKeyboardClass::WWKeyboardClass(void)
  56. {
  57. _Kbd = this;
  58. //
  59. // Initialize the keyboard remap table for our system (note it would be bad if someone
  60. // switched keyboard modes after this happened.
  61. //
  62. memset(VKRemap, 0, 256);
  63. memset(AsciiRemap, 0, 2048);
  64. for (short lp = 31; lp < 255; 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. // Build a remap table of the different keys which are affected by the caps lock and
  73. // the num lock.
  74. //
  75. memset(ToggleKeys, 0, 256);
  76. for (lp = 0; lp < 255; lp++ ) {
  77. if (isalpha(lp) && isupper(lp)) {
  78. ToggleKeys[lp] = 1;
  79. }
  80. if (lp >= VK_NUMPAD0 && lp <= VK_DIVIDE) {
  81. ToggleKeys[lp] = 2;
  82. }
  83. }
  84. //
  85. // Our buffer should start devoid of keys.
  86. //
  87. memset(Buffer, 0, 256);
  88. Head = 0;
  89. Tail = 0;
  90. //
  91. // There should be no starting queued mouse events for us to have to worry
  92. // about.
  93. //
  94. MouseQX = 0;
  95. MouseQY = 0;
  96. MState = 0;
  97. Conditional = 0;
  98. CurrentCursor = NULL;
  99. }
  100. /***********************************************************************************************
  101. * WWKeyboardClass::Buff_Get -- Lowlevel function to get a key from key buffer *
  102. * *
  103. * INPUT: none *
  104. * *
  105. * OUTPUT: int - the key value that was pulled from buffer (includes bits) * *
  106. * *
  107. * WARNINGS: If the key was a mouse event MouseQX and MouseQY will be updated *
  108. * *
  109. * HISTORY: *
  110. * 10/17/1995 PWG : Created. *
  111. *=============================================================================================*/
  112. int WWKeyboardClass::Buff_Get(void)
  113. {
  114. while (!Check()) {} // wait for key in buffer
  115. int temp = Buffer[Head]; // get key out of the buffer
  116. int newhead = Head; // save off head for manipulation
  117. if (Is_Mouse_Key(temp)) { // if key is a mouse then
  118. MouseQX = Buffer[Head+1]; // get the x and y pos
  119. MouseQY = Buffer[Head+2]; // from the buffer
  120. newhead += 3; // adjust head forward
  121. } else {
  122. newhead += 1; // adjust head forward
  123. }
  124. newhead &= 255;
  125. Head = newhead;
  126. return(temp);
  127. }
  128. BOOL WWKeyboardClass::Is_Mouse_Key(int key)
  129. {
  130. key &= 0xFF;
  131. return (key == VK_LBUTTON || key == VK_MBUTTON || key == VK_RBUTTON);
  132. }
  133. /***********************************************************************************************
  134. * WWKeyboardClass::Check -- Checks to see if a key is in the buffer *
  135. * *
  136. * INPUT: *
  137. * *
  138. * OUTPUT: *
  139. * *
  140. * WARNINGS: *
  141. * *
  142. * HISTORY: *
  143. * 10/16/1995 PWG : Created. *
  144. *=============================================================================================*/
  145. BOOL WWKeyboardClass::Check(void)
  146. {
  147. Message_Loop();
  148. unsigned short temp; // store temp holding spot for key
  149. if (Head == Tail) return(FALSE); // if no keys in buff then get out
  150. temp = Buffer[Head]; // get key out of the buffer
  151. return(temp); // send it back to main program
  152. }
  153. /***********************************************************************************************
  154. * WWKeyboardClass::Get -- Logic to get a metakey from the buffer *
  155. * *
  156. * INPUT: none *
  157. * *
  158. * OUTPUT: int - the meta key taken from the buffer. *
  159. * *
  160. * WARNINGS: This routine will not return until a keypress is received *
  161. * *
  162. * HISTORY: *
  163. * 10/16/1995 PWG : Created. *
  164. *=============================================================================================*/
  165. int WWKeyboardClass::Get(void)
  166. {
  167. int temp,bits; // store temp holding spot for key
  168. while (!Check()) {} // wait for key in buffer
  169. temp = Buff_Get(); // get key from the buffer
  170. bits = temp & 0xFF00; // save of keyboard bits
  171. if (!(bits & WWKEY_VK_BIT)) { // if its not a virtual key
  172. temp = AsciiRemap[temp&0x1FF] | bits; // convert to ascii equivalent
  173. }
  174. return(temp); // return the key that we pulled out
  175. }
  176. /***********************************************************************************************
  177. * WWKeyboardClass::Put -- Logic to insert a key into the keybuffer] *
  178. * *
  179. * INPUT: int - the key to insert into the buffer *
  180. * *
  181. * OUTPUT: bool - true if key is sucessfuly inserted. *
  182. * *
  183. * WARNINGS: none *
  184. * *
  185. * HISTORY: *
  186. * 10/16/1995 PWG : Created. *
  187. *=============================================================================================*/
  188. BOOL WWKeyboardClass::Put(int key)
  189. {
  190. int temp = (Tail + 1) & 255;
  191. if (temp != Head)
  192. {
  193. Buffer[Tail] = (short)key;
  194. //
  195. // Critical Line
  196. //
  197. Tail = temp;
  198. return(TRUE);
  199. }
  200. return(FALSE);
  201. }
  202. /***********************************************************************************************
  203. * WWKeyboardClass::Put_Key_Message -- Translates and inserts wParam into Keyboard Buffer *
  204. * *
  205. * INPUT: *
  206. * *
  207. * OUTPUT: *
  208. * *
  209. * WARNINGS: *
  210. * *
  211. * HISTORY: *
  212. * 10/16/1995 PWG : Created. *
  213. *=============================================================================================*/
  214. WWKeyboardClass::Put_Key_Message(UINT vk_key, BOOL release, BOOL dbl)
  215. {
  216. int bits = 0;
  217. //
  218. // Get the status of all of the different keyboard modifiers. Note, only pay attention
  219. // to numlock and caps lock if we are dealing with a key that is affected by them.
  220. //
  221. int shift = (GetKeyState(VK_SHIFT) & 0xFF00) != 0;
  222. int ctrl = (GetKeyState(VK_CONTROL) & 0xFF00) != 0;
  223. int alt = (GetKeyState(VK_MENU) & 0xFF00) != 0;
  224. int caps = ((GetKeyState(VK_CAPITAL) & 0x00FF) != 0) && (ToggleKeys[vk_key] == 1);
  225. int nums = ((GetKeyState(VK_NUMLOCK) & 0x00FF) != 0) && (ToggleKeys[vk_key] == 2);
  226. //
  227. // Set the proper bits for whatever the key we got is.
  228. //
  229. if (shift || caps || nums) {
  230. bits |= WWKEY_SHIFT_BIT;
  231. }
  232. if (ctrl) {
  233. bits |= WWKEY_CTRL_BIT;
  234. }
  235. if (alt) {
  236. bits |= WWKEY_ALT_BIT;
  237. }
  238. if (!AsciiRemap[vk_key|bits]) {
  239. bits |= WWKEY_VK_BIT;
  240. }
  241. if (release) {
  242. bits |= WWKEY_RLS_BIT;
  243. }
  244. if (dbl) {
  245. bits |= WWKEY_DBL_BIT;
  246. }
  247. //
  248. // Finally use the put command to enter the key into the keyboard
  249. // system.
  250. //
  251. return(Put(vk_key|bits));
  252. }
  253. void WWKeyboardClass::Clear(void)
  254. {
  255. Head = Tail;
  256. }
  257. int WWKeyboardClass::To_ASCII(int key)
  258. {
  259. return(key);
  260. }
  261. WWKeyboardClass::Down(int key)
  262. {
  263. return(GetAsyncKeyState(key&0xFF));
  264. }
  265. VOID WWKeyboardClass::Split(int &key, int &shift, int &ctrl, int &alt, int &rls, int &dbl)
  266. {
  267. shift = (key & WWKEY_SHIFT_BIT) != 0;
  268. ctrl = (key & WWKEY_CTRL_BIT) != 0;
  269. alt = (key & WWKEY_ALT_BIT) != 0;
  270. rls = (key & WWKEY_RLS_BIT) != 0;
  271. dbl = (key & WWKEY_DBL_BIT) != 0;
  272. key = (key & 0xFF);
  273. }
  274. extern "C" {
  275. void __cdecl Stop_Execution (void);
  276. }
  277. #pragma off(unreferenced)
  278. void WWKeyboardClass::Message_Handler(HWND , UINT message, UINT wParam, LONG lParam)
  279. {
  280. switch (message) {
  281. case WM_SYSKEYDOWN:
  282. case WM_KEYDOWN:
  283. if ( wParam==VK_SCROLL ){
  284. Stop_Execution();
  285. } else {
  286. Put_Key_Message(wParam);
  287. }
  288. break;
  289. case WM_SYSKEYUP:
  290. case WM_KEYUP:
  291. Put_Key_Message(wParam, TRUE);
  292. break;
  293. case WM_LBUTTONDOWN:
  294. Put_Key_Message(VK_LBUTTON);
  295. Put(LOWORD(lParam));
  296. Put(HIWORD(lParam));
  297. break;
  298. case WM_LBUTTONUP:
  299. Put_Key_Message(VK_LBUTTON, TRUE);
  300. Put(LOWORD(lParam));
  301. Put(HIWORD(lParam));
  302. break;
  303. case WM_LBUTTONDBLCLK:
  304. Put_Key_Message(VK_LBUTTON, TRUE, TRUE);
  305. Put(LOWORD(lParam));
  306. Put(HIWORD(lParam));
  307. break;
  308. case WM_MBUTTONDOWN:
  309. Put_Key_Message(VK_MBUTTON);
  310. Put(LOWORD(lParam));
  311. Put(HIWORD(lParam));
  312. break;
  313. case WM_MBUTTONUP:
  314. Put_Key_Message(VK_MBUTTON, TRUE);
  315. Put(LOWORD(lParam));
  316. Put(HIWORD(lParam));
  317. break;
  318. case WM_MBUTTONDBLCLK:
  319. Put_Key_Message(VK_MBUTTON, TRUE, TRUE);
  320. Put(LOWORD(lParam));
  321. Put(HIWORD(lParam));
  322. break;
  323. case WM_RBUTTONDOWN:
  324. Put_Key_Message(VK_RBUTTON);
  325. Put(LOWORD(lParam));
  326. Put(HIWORD(lParam));
  327. break;
  328. case WM_RBUTTONUP:
  329. Put_Key_Message(VK_RBUTTON, TRUE);
  330. Put(LOWORD(lParam));
  331. Put(HIWORD(lParam));
  332. break;
  333. case WM_RBUTTONDBLCLK:
  334. Put_Key_Message(VK_RBUTTON, TRUE, TRUE);
  335. Put(LOWORD(lParam));
  336. Put(HIWORD(lParam));
  337. break;
  338. case WM_MOUSEMOVE:
  339. if (CurrentCursor)
  340. SetCursor(CurrentCursor);
  341. break;
  342. }
  343. }
  344. #pragma on(unreferenced)
  345. extern BOOL GameInFocus;
  346. void Message_Loop(void)
  347. {
  348. MSG msg;
  349. do {
  350. while (PeekMessage( &msg, NULL, 0, 0, PM_NOREMOVE )) {
  351. if( !GetMessage( &msg, NULL, 0, 0 ) ){
  352. return;
  353. }
  354. TranslateMessage(&msg);
  355. DispatchMessage(&msg);
  356. }
  357. } while (!GameInFocus);
  358. }
  359. /***************************************************************************
  360. * CHECK_KEY -- compatability routine for old 32 bit library *
  361. * *
  362. * This routine checks to see if there is a key in the keyboard buffer *
  363. * and returns it to the sender if there is. It does not remove the key *
  364. * from the buffer. *
  365. * *
  366. * INPUT: none *
  367. * *
  368. * OUTPUT: The key that was pressed. *
  369. * *
  370. * WARNINGS: You must declare a WWKeyboardClass object before calling *
  371. * this routine. *
  372. * *
  373. * HISTORY: *
  374. * 10/26/1995 : Created. *
  375. *=========================================================================*/
  376. int Check_Key(void)
  377. {
  378. if (!_Kbd) return(KA_NONE);
  379. return(_Kbd->Check() & ~WWKEY_SHIFT_BIT);
  380. }
  381. void Clear_KeyBuffer(void)
  382. {
  383. if (!_Kbd) return;
  384. _Kbd->Clear();
  385. }
  386. int Check_Key_Num(void)
  387. {
  388. if (!_Kbd) return(KN_NONE);
  389. int key = _Kbd->Check();
  390. int flags = key & 0xFF00;
  391. key = key & 0x00FF;
  392. if (isupper(key)) {
  393. key = tolower(key);
  394. flags |= WWKEY_SHIFT_BIT;
  395. }
  396. return(key | flags);
  397. }
  398. int Get_Key_Num(void)
  399. {
  400. if (!_Kbd) return(KN_NONE);
  401. int key = _Kbd->Get();
  402. int flags = key & 0xFF00;
  403. key = key & 0x00FF;
  404. if (isupper(key)) {
  405. key = tolower(key);
  406. flags |= WWKEY_SHIFT_BIT;
  407. }
  408. return(key | flags);
  409. }
  410. int KN_To_KA(int key)
  411. {
  412. if (!(key & WWKEY_VK_BIT)) {
  413. int flags = key & 0xFF00;
  414. key = key & 0x00FF;
  415. if (flags & WWKEY_SHIFT_BIT) {
  416. key = toupper(key);
  417. flags &= ~WWKEY_SHIFT_BIT;
  418. }
  419. }
  420. return(key);
  421. }
  422. int Key_Down(int key)
  423. {
  424. if (!_Kbd) return(FALSE);
  425. return(_Kbd->Down(key));
  426. }
  427. int Get_Key(void)
  428. {
  429. if (!_Kbd) return(KN_NONE);
  430. return(_Kbd->Get() & ~WWKEY_SHIFT_BIT);
  431. }