KEYBOARD.CPP 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  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. /* $Header: F:\projects\c&c0\vcs\code\conquer.cpv 4.74 23 Sep 1996 12:36:00 JOE_BOSTIC $ */
  19. /***********************************************************************************************
  20. * *
  21. * Project Name : Westwood Keyboard Library *
  22. * *
  23. * File Name : KEYBOARD.CPP *
  24. * *
  25. * Programmer : Philip W. Gorrow *
  26. * *
  27. * Start Date : 10/16/95 *
  28. * *
  29. * Last Update : September 24, 1996 [JLB] *
  30. * *
  31. *---------------------------------------------------------------------------------------------*
  32. * Functions: *
  33. * WWKeyboardClass::Put -- Logic to insert a key into the keybuffer] *
  34. * WWKeyboardClass::Get -- Logic to get a metakey from the buffer *
  35. * WWKeyboardClass::Check -- Checks to see if a key is in the buffer *
  36. * WWKeyboardClass::Put_Key_Message -- Translates and inserts wParam into Keyboard Buffer *
  37. * WWKeyboardClass::Buff_Get -- Lowlevel function to get a key from key buffer *
  38. * WWKeyboardClass::Get_Mouse_X -- Returns the mouses current x position in pixels *
  39. * WWKeyboardClass::Get_Mouse_Y -- returns the mouses current y position in pixels *
  40. * WWKeyboardClass::Get_Mouse_XY -- Returns the mouses x,y position via reference vars *
  41. * Check_Key -- compatability routine for old 32 bit library *
  42. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  43. #include "keyboard.h"
  44. #include "monoc.h"
  45. //void Message_Loop(void);
  46. WWKeyboardClass * _Kbd = NULL;
  47. #define ARRAY_SIZE(x) int(sizeof(x)/sizeof(x[0]))
  48. /***********************************************************************************************
  49. * WWKeyboardClass::WWKeyBoardClass -- Construction for Westwood Keyboard Class *
  50. * *
  51. * INPUT: none *
  52. * *
  53. * OUTPUT: none *
  54. * *
  55. * HISTORY: *
  56. * 10/16/1995 PWG : Created. *
  57. *=============================================================================================*/
  58. WWKeyboardClass::WWKeyboardClass(void) :
  59. MouseQX(0),
  60. MouseQY(0),
  61. Head(0),
  62. Tail(0)
  63. // MState(0),
  64. // Conditional(0),
  65. // CurrentCursor(0)
  66. {
  67. _Kbd = this;
  68. memset(KeyState, '\0', sizeof(KeyState));
  69. }
  70. /***********************************************************************************************
  71. * WWKeyboardClass::Buff_Get -- Lowlevel function to get a key from key buffer *
  72. * *
  73. * INPUT: none *
  74. * *
  75. * OUTPUT: int - the key value that was pulled from buffer (includes bits) * *
  76. * *
  77. * WARNINGS: If the key was a mouse event MouseQX and MouseQY will be updated *
  78. * *
  79. * HISTORY: *
  80. * 10/17/1995 PWG : Created. *
  81. *=============================================================================================*/
  82. unsigned short WWKeyboardClass::Buff_Get(void)
  83. {
  84. while (!Check()) {} // wait for key in buffer
  85. unsigned short temp = Fetch_Element();
  86. if (Is_Mouse_Key(temp)) {
  87. MouseQX = Fetch_Element();
  88. MouseQY = Fetch_Element();
  89. }
  90. return(temp);
  91. }
  92. bool WWKeyboardClass::Is_Mouse_Key(unsigned short key)
  93. {
  94. key &= 0xFF;
  95. return (key == VK_LBUTTON || key == VK_MBUTTON || key == VK_RBUTTON);
  96. }
  97. /***********************************************************************************************
  98. * WWKeyboardClass::Check -- Checks to see if a key is in the buffer *
  99. * *
  100. * INPUT: *
  101. * *
  102. * OUTPUT: *
  103. * *
  104. * WARNINGS: *
  105. * *
  106. * HISTORY: *
  107. * 10/16/1995 PWG : Created. *
  108. * 09/24/1996 JLB : Converted to new style keyboard system. *
  109. *=============================================================================================*/
  110. unsigned short WWKeyboardClass::Check(void) const
  111. {
  112. ((WWKeyboardClass *)this)->Fill_Buffer_From_System();
  113. if (Is_Buffer_Empty()) return(false);
  114. return(Peek_Element());
  115. }
  116. /***********************************************************************************************
  117. * WWKeyboardClass::Get -- Logic to get a metakey from the buffer *
  118. * *
  119. * INPUT: none *
  120. * *
  121. * OUTPUT: int - the meta key taken from the buffer. *
  122. * *
  123. * WARNINGS: This routine will not return until a keypress is received *
  124. * *
  125. * HISTORY: *
  126. * 10/16/1995 PWG : Created. *
  127. *=============================================================================================*/
  128. unsigned short WWKeyboardClass::Get(void)
  129. {
  130. while (!Check()) {} // wait for key in buffer
  131. return (Buff_Get());
  132. }
  133. /***********************************************************************************************
  134. * WWKeyboardClass::Put -- Logic to insert a key into the keybuffer] *
  135. * *
  136. * INPUT: int - the key to insert into the buffer *
  137. * *
  138. * OUTPUT: bool - true if key is sucessfuly inserted. *
  139. * *
  140. * WARNINGS: none *
  141. * *
  142. * HISTORY: *
  143. * 10/16/1995 PWG : Created. *
  144. *=============================================================================================*/
  145. bool WWKeyboardClass::Put(unsigned short key)
  146. {
  147. if (!Is_Buffer_Full()) {
  148. Put_Element(key);
  149. return(true);
  150. }
  151. return(false);
  152. }
  153. /***********************************************************************************************
  154. * WWKeyboardClass::Put_Key_Message -- Translates and inserts wParam into Keyboard Buffer *
  155. * *
  156. * INPUT: *
  157. * *
  158. * OUTPUT: *
  159. * *
  160. * WARNINGS: *
  161. * *
  162. * HISTORY: *
  163. * 10/16/1995 PWG : Created. *
  164. *=============================================================================================*/
  165. bool WWKeyboardClass::Put_Key_Message(unsigned short vk_key, bool release)
  166. {
  167. //
  168. // Get the status of all of the different keyboard modifiers. Note, only pay attention
  169. // to numlock and caps lock if we are dealing with a key that is affected by them. Note
  170. // that we do not want to set the shift, ctrl and alt bits for Mouse keypresses as this
  171. // would be incompatible with the dos version.
  172. //
  173. if (!Is_Mouse_Key(vk_key)) {
  174. if (((GetKeyState(VK_SHIFT) & 0x8000) != 0) ||
  175. ((GetKeyState(VK_CAPITAL) & 0x0008) != 0) ||
  176. ((GetKeyState(VK_NUMLOCK) & 0x0008) != 0)) {
  177. vk_key |= WWKEY_SHIFT_BIT;
  178. }
  179. if ((GetKeyState(VK_CONTROL) & 0x8000) != 0) {
  180. vk_key |= WWKEY_CTRL_BIT;
  181. }
  182. if ((GetKeyState(VK_MENU) & 0x8000) != 0) {
  183. vk_key |= WWKEY_ALT_BIT;
  184. }
  185. }
  186. if (release) {
  187. vk_key |= WWKEY_RLS_BIT;
  188. }
  189. //
  190. // Finally use the put command to enter the key into the keyboard
  191. // system.
  192. //
  193. return(Put(vk_key));
  194. }
  195. char WWKeyboardClass::To_ASCII(unsigned short key)
  196. {
  197. /*
  198. ** Released keys never translate into an ASCII value.
  199. */
  200. if (key & WWKEY_RLS_BIT) {
  201. return('\0');
  202. }
  203. /*
  204. ** Set the KeyState buffer to reflect the shift bits stored in the key value.
  205. */
  206. if (key & WWKEY_SHIFT_BIT) {
  207. KeyState[VK_SHIFT] = 0x80;
  208. }
  209. if (key & WWKEY_CTRL_BIT) {
  210. KeyState[VK_CONTROL] = 0x80;
  211. }
  212. if (key & WWKEY_ALT_BIT) {
  213. KeyState[VK_MENU] = 0x80;
  214. }
  215. /*
  216. ** Ask windows to translate the key into an ASCII equivalent.
  217. */
  218. char buffer[10];
  219. int result = 1;
  220. int scancode = 0;
  221. char override = '\0';
  222. switch (key & 0xFF) {
  223. // case KN_RETURN:
  224. // override = KA_RETURN;
  225. // break;
  226. // case KN_BACKSPACE:
  227. // override = KA_BACKSPACE;
  228. // break;
  229. default:
  230. scancode = MapVirtualKey(key & 0xFF, 0);
  231. result = ToAscii((UINT)(key & 0xFF), (UINT)scancode, (PBYTE)KeyState, (LPWORD)buffer, (UINT)0);
  232. break;
  233. }
  234. /*
  235. ** Restore the KeyState buffer back to pristine condition.
  236. */
  237. if (key & WWKEY_SHIFT_BIT) {
  238. KeyState[VK_SHIFT] = 0;
  239. }
  240. if (key & WWKEY_CTRL_BIT) {
  241. KeyState[VK_CONTROL] = 0;
  242. }
  243. if (key & WWKEY_ALT_BIT) {
  244. KeyState[VK_MENU] = 0;
  245. }
  246. /*
  247. ** If Windows could not perform the translation as expected, then
  248. ** return with a null ASCII value.
  249. */
  250. if (result != 1) {
  251. return('\0');
  252. }
  253. if (override != 0) {
  254. return(override);
  255. }
  256. return(buffer[0]);
  257. }
  258. WWKeyboardClass::Down(unsigned short key)
  259. {
  260. return(GetAsyncKeyState(key & 0xFF));
  261. }
  262. extern "C" {
  263. void __cdecl Stop_Execution (void);
  264. }
  265. unsigned short WWKeyboardClass::Fetch_Element(void)
  266. {
  267. unsigned short val = 0;
  268. if (Head != Tail) {
  269. val = Buffer[Head];
  270. Head = (Head + 1) % ARRAY_SIZE(Buffer);
  271. }
  272. return(val);
  273. }
  274. unsigned short WWKeyboardClass::Peek_Element(void) const
  275. {
  276. if (!Is_Buffer_Empty()) {
  277. return(Buffer[Head]);
  278. }
  279. return(0);
  280. }
  281. bool WWKeyboardClass::Put_Element(unsigned short val)
  282. {
  283. if (!Is_Buffer_Full()) {
  284. int temp = (Tail+1) % ARRAY_SIZE(Buffer);
  285. Buffer[Tail] = val;
  286. Tail = temp;
  287. return(true);
  288. }
  289. return(false);
  290. }
  291. bool WWKeyboardClass::Is_Buffer_Full(void) const
  292. {
  293. if ((Tail + 1) % ARRAY_SIZE(Buffer) == Head) {
  294. return(true);
  295. }
  296. return(false);
  297. }
  298. bool WWKeyboardClass::Is_Buffer_Empty(void) const
  299. {
  300. if (Head == Tail) {
  301. return(true);
  302. }
  303. return(false);
  304. }
  305. void WWKeyboardClass::Fill_Buffer_From_System(void)
  306. {
  307. if (!Is_Buffer_Full()) {
  308. MSG msg;
  309. while (PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE)) {
  310. if (!GetMessage( &msg, NULL, 0, 0 )) {
  311. return;
  312. }
  313. TranslateMessage(&msg);
  314. DispatchMessage(&msg);
  315. }
  316. }
  317. }
  318. void WWKeyboardClass::Clear(void)
  319. {
  320. Head = Tail;
  321. }
  322. void WWKeyboardClass::Message_Handler(HWND , UINT message, UINT wParam, LONG lParam)
  323. {
  324. switch (message) {
  325. case WM_SYSKEYDOWN:
  326. case WM_KEYDOWN:
  327. if ( wParam==VK_SCROLL ){
  328. Stop_Execution();
  329. } else {
  330. Put_Key_Message((unsigned short)wParam);
  331. }
  332. break;
  333. case WM_SYSKEYUP:
  334. case WM_KEYUP:
  335. Put_Key_Message((unsigned short)wParam, true);
  336. break;
  337. case WM_LBUTTONDOWN:
  338. Put_Key_Message(VK_LBUTTON);
  339. Put(LOWORD(lParam));
  340. Put(HIWORD(lParam));
  341. break;
  342. case WM_LBUTTONUP:
  343. Put_Key_Message(VK_LBUTTON, true);
  344. Put(LOWORD(lParam));
  345. Put(HIWORD(lParam));
  346. break;
  347. case WM_LBUTTONDBLCLK:
  348. Put_Key_Message(VK_LBUTTON, true);
  349. Put(LOWORD(lParam));
  350. Put(HIWORD(lParam));
  351. Put_Key_Message(VK_LBUTTON, true);
  352. Put(LOWORD(lParam));
  353. Put(HIWORD(lParam));
  354. break;
  355. case WM_MBUTTONDOWN:
  356. Put_Key_Message(VK_MBUTTON);
  357. Put(LOWORD(lParam));
  358. Put(HIWORD(lParam));
  359. break;
  360. case WM_MBUTTONUP:
  361. Put_Key_Message(VK_MBUTTON, true);
  362. Put(LOWORD(lParam));
  363. Put(HIWORD(lParam));
  364. break;
  365. case WM_MBUTTONDBLCLK:
  366. Put_Key_Message(VK_MBUTTON, true);
  367. Put(LOWORD(lParam));
  368. Put(HIWORD(lParam));
  369. Put_Key_Message(VK_MBUTTON, true);
  370. Put(LOWORD(lParam));
  371. Put(HIWORD(lParam));
  372. break;
  373. case WM_RBUTTONDOWN:
  374. Put_Key_Message(VK_RBUTTON);
  375. Put(LOWORD(lParam));
  376. Put(HIWORD(lParam));
  377. break;
  378. case WM_RBUTTONUP:
  379. Put_Key_Message(VK_RBUTTON, true);
  380. Put(LOWORD(lParam));
  381. Put(HIWORD(lParam));
  382. break;
  383. case WM_RBUTTONDBLCLK:
  384. Put_Key_Message(VK_RBUTTON, true);
  385. Put(LOWORD(lParam));
  386. Put(HIWORD(lParam));
  387. Put_Key_Message(VK_RBUTTON, true);
  388. Put(LOWORD(lParam));
  389. Put(HIWORD(lParam));
  390. break;
  391. // case WM_MOUSEMOVE:
  392. // if (CurrentCursor)
  393. // SetCursor(CurrentCursor);
  394. // break;
  395. }
  396. }