KEYBOARD.CPP 16 KB

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