Win32DIKeyboard.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  1. /*
  2. ** Command & Conquer Generals Zero Hour(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. // (c) 2001-2003 Electronic Arts Inc. //
  21. // //
  22. ////////////////////////////////////////////////////////////////////////////////
  23. // FILE: Win32DIKeyboard.cpp //////////////////////////////////////////////////////////////////////
  24. // Created: Colin Day, June 2001
  25. // Desc: Device implementation of the keyboard interface on Win32
  26. // using Microsoft Direct Input
  27. ///////////////////////////////////////////////////////////////////////////////////////////////////
  28. #include <windows.h>
  29. #include <assert.h>
  30. #include "Common/Debug.h"
  31. #include "Common/Language.h"
  32. #include "GameClient/KeyDefs.h"
  33. #include "GameClient/Keyboard.h"
  34. #include "Win32Device/GameClient/Win32DIKeyboard.h"
  35. #include "WinMain.h"
  36. // DEFINES ////////////////////////////////////////////////////////////////////////////////////////
  37. enum { KEYBOARD_BUFFER_SIZE = 256 };
  38. // PRIVATE DATA ///////////////////////////////////////////////////////////////////////////////////
  39. struct ErrorLookup
  40. {
  41. HRESULT error;
  42. char *string;
  43. };
  44. static ErrorLookup errorLookup[] =
  45. {
  46. { DIERR_ACQUIRED, "DIERR_ACQUIRED" },
  47. { DIERR_ALREADYINITIALIZED, "DIERR_ALREADYINITIALIZED" },
  48. { DIERR_BADDRIVERVER, "DIERR_BADDRIVERVER" },
  49. { DIERR_BETADIRECTINPUTVERSION, "DIERR_BETADIRECTINPUTVERSION" },
  50. { DIERR_DEVICEFULL, "DIERR_DEVICEFULL" },
  51. { DIERR_DEVICENOTREG, "DIERR_DEVICENOTREG" },
  52. { DIERR_EFFECTPLAYING, "DIERR_EFFECTPLAYING" },
  53. { DIERR_GENERIC, "DIERR_GENERIC" },
  54. { DIERR_HANDLEEXISTS, "DIERR_HANDLEEXISTS" },
  55. { DIERR_HASEFFECTS, "DIERR_HASEFFECTS" },
  56. { DIERR_INCOMPLETEEFFECT, "DIERR_INCOMPLETEEFFECT" },
  57. { DIERR_INPUTLOST, "DIERR_INPUTLOST" },
  58. { DIERR_INVALIDPARAM, "DIERR_INVALIDPARAM" },
  59. { DIERR_MAPFILEFAIL, "DIERR_MAPFILEFAIL" },
  60. { DIERR_MOREDATA, "DIERR_MOREDATA" },
  61. { DIERR_NOAGGREGATION, "DIERR_NOAGGREGATION" },
  62. { DIERR_NOINTERFACE, "DIERR_NOINTERFACE" },
  63. { DIERR_NOTACQUIRED, "DIERR_NOTACQUIRED" },
  64. { DIERR_NOTBUFFERED, "DIERR_NOTBUFFERED" },
  65. { DIERR_NOTDOWNLOADED, "DIERR_NOTDOWNLOADED" },
  66. { DIERR_NOTEXCLUSIVEACQUIRED, "DIERR_NOTEXCLUSIVEACQUIRED" },
  67. { DIERR_NOTFOUND, "DIERR_NOTFOUND" },
  68. { DIERR_NOTINITIALIZED, "DIERR_NOTINITIALIZED" },
  69. { DIERR_OBJECTNOTFOUND, "DIERR_OBJECTNOTFOUND" },
  70. { DIERR_OLDDIRECTINPUTVERSION, "DIERR_OLDDIRECTINPUTVERSION" },
  71. { DIERR_OTHERAPPHASPRIO, "DIERR_OTHERAPPHASPRIO" },
  72. { DIERR_OUTOFMEMORY, "DIERR_OUTOFMEMORY" },
  73. { DIERR_READONLY, "DIERR_READONLY" },
  74. { DIERR_REPORTFULL, "DIERR_REPORTFULL" },
  75. { DIERR_UNPLUGGED, "DIERR_UNPLUGGED" },
  76. { DIERR_UNSUPPORTED, "DIERR_UNSUPPORTED" },
  77. { 0, NULL }
  78. };
  79. ///////////////////////////////////////////////////////////////////////////////
  80. // PRIVATE FUNCTIONS //////////////////////////////////////////////////////////
  81. ///////////////////////////////////////////////////////////////////////////////
  82. //-------------------------------------------------------------------------------------------------
  83. /** For debugging, prints the return code using direct input errors */
  84. //-------------------------------------------------------------------------------------------------
  85. static void printReturnCode( char *label, HRESULT hr )
  86. {
  87. ErrorLookup *error = errorLookup;
  88. while( error->string != NULL )
  89. {
  90. if( error->error == hr )
  91. {
  92. DEBUG_LOG(( "%s: '%s' - '0x%08x'\n", label, error->string, hr ));
  93. break;
  94. }
  95. error++;
  96. }
  97. } // end printReturnCode
  98. //-------------------------------------------------------------------------------------------------
  99. /** create our interface to the direct input keybard */
  100. //-------------------------------------------------------------------------------------------------
  101. void DirectInputKeyboard::openKeyboard( void )
  102. {
  103. HRESULT hr;
  104. // create our interface to direct input
  105. hr = DirectInput8Create( ApplicationHInstance,
  106. DIRECTINPUT_VERSION,
  107. IID_IDirectInput8,
  108. (void **)&m_pDirectInput,
  109. NULL );
  110. if( FAILED( hr ) )
  111. {
  112. DEBUG_LOG(( "ERROR - openKeyboard: DirectInputCreate failed\r\n" ));
  113. assert( 0 );
  114. closeKeyboard();
  115. return;
  116. } // end if
  117. // obtain an interface to the system keyboard device
  118. hr = m_pDirectInput->CreateDevice( GUID_SysKeyboard,
  119. &m_pKeyboardDevice,
  120. NULL );
  121. if( FAILED( hr ) )
  122. {
  123. DEBUG_LOG(( "ERROR - openKeyboard: Unabled to create keyboard device\n" ));
  124. assert( 0 );
  125. closeKeyboard();
  126. return;
  127. } // end if
  128. // set the data format for the keyboard
  129. hr = m_pKeyboardDevice->SetDataFormat( &c_dfDIKeyboard );
  130. if( FAILED( hr ) )
  131. {
  132. DEBUG_LOG(( "ERROR - openKeyboard: Unabled to set data format for keyboard\n" ));
  133. assert( 0 );
  134. closeKeyboard();
  135. return;
  136. } // end if
  137. /// @todo Check the cooperative level of keyboard for NT, 2000, DX8 etc ...
  138. // set the cooperative level for the keyboard, must be non-exclusive for
  139. // NT support, but we should check with the latest versions of DirectX
  140. // on 2000 etc
  141. //
  142. hr = m_pKeyboardDevice->SetCooperativeLevel( ApplicationHWnd,
  143. DISCL_FOREGROUND |
  144. DISCL_NONEXCLUSIVE );
  145. if( FAILED( hr ) )
  146. {
  147. DEBUG_LOG(( "ERROR - openKeyboard: Unabled to set cooperative level\n" ));
  148. assert( 0 );
  149. closeKeyboard();
  150. return;
  151. } // end if
  152. // set the keyboard buffer size
  153. DIPROPDWORD prop;
  154. prop.diph.dwSize = sizeof( DIPROPDWORD );
  155. prop.diph.dwHeaderSize = sizeof( DIPROPHEADER );
  156. prop.diph.dwObj = 0;
  157. prop.diph.dwHow = DIPH_DEVICE;
  158. prop.dwData = KEYBOARD_BUFFER_SIZE;
  159. hr = m_pKeyboardDevice->SetProperty( DIPROP_BUFFERSIZE, &prop.diph );
  160. if( FAILED( hr ) )
  161. {
  162. DEBUG_LOG(( "ERROR - openKeyboard: Unable to set keyboard buffer size property\n" ));
  163. assert( 0 );
  164. closeKeyboard();
  165. return;
  166. } // end if
  167. // acquire the keyboard
  168. hr = m_pKeyboardDevice->Acquire();
  169. if( FAILED( hr ) )
  170. {
  171. DEBUG_LOG(( "ERROR - openKeyboard: Unable to acquire keyboard device\n" ));
  172. // Note - This can happen in windowed mode, and we can re-acquire later. So don't
  173. // close the keyboard. jba.
  174. // closeKeyboard();
  175. return;
  176. } // end if
  177. DEBUG_LOG(( "OK - Keyboard initialized successfully.\n" ));
  178. } // end openKeyboard
  179. //-------------------------------------------------------------------------------------------------
  180. /** close the direct input keyboard */
  181. //-------------------------------------------------------------------------------------------------
  182. void DirectInputKeyboard::closeKeyboard( void )
  183. {
  184. if( m_pKeyboardDevice )
  185. {
  186. m_pKeyboardDevice->Unacquire();
  187. m_pKeyboardDevice->Release();
  188. m_pKeyboardDevice = NULL;
  189. DEBUG_LOG(( "OK - Keyboard deviced closed\n" ));
  190. } // end if
  191. if( m_pDirectInput )
  192. {
  193. m_pDirectInput->Release();
  194. m_pDirectInput = NULL;
  195. DEBUG_LOG(( "OK - Keyboard direct input interface closed\n" ));
  196. } // end if
  197. DEBUG_LOG(( "OK - Keyboard shutdown complete\n" ));
  198. } // end closeKeyboard
  199. //-------------------------------------------------------------------------------------------------
  200. /** Get a single keyboard event from direct input */
  201. //-------------------------------------------------------------------------------------------------
  202. void DirectInputKeyboard::getKey( KeyboardIO *key )
  203. {
  204. static int errs = 0;
  205. DIDEVICEOBJECTDATA kbdat;
  206. DWORD num = 0;
  207. // int done = 0;
  208. HRESULT hr;
  209. assert( key );
  210. key->sequence = 0;
  211. key->key = KEY_NONE;
  212. if( m_pKeyboardDevice )
  213. {
  214. // get 1 key, if available
  215. num = 1;
  216. hr = m_pKeyboardDevice->Acquire();
  217. if (hr == DI_OK || hr == S_FALSE)
  218. hr = m_pKeyboardDevice->GetDeviceData( sizeof( DIDEVICEOBJECTDATA ),
  219. &kbdat, &num, 0 );
  220. switch( hr )
  221. {
  222. // ----------------------------------------------------------------------
  223. case DI_OK:
  224. break;
  225. // ----------------------------------------------------------------------
  226. case DIERR_INPUTLOST:
  227. case DIERR_NOTACQUIRED:
  228. // if we lost focus, attempt to re-acquire
  229. hr = m_pKeyboardDevice->Acquire();
  230. switch( hr )
  231. {
  232. // ------------------------------------------------------------------
  233. //If an error occurs return KEY_NONE
  234. case DIERR_INVALIDPARAM:
  235. case DIERR_NOTINITIALIZED:
  236. case DIERR_OTHERAPPHASPRIO:
  237. break;
  238. // ------------------------------------------------------------------
  239. // If successful... tell system to loop back
  240. case DI_OK:
  241. case S_FALSE:
  242. {
  243. // this will tell the system to loop again
  244. key->key = KEY_LOST;
  245. break;
  246. } // end, got the keyboard back OK
  247. } // end switch
  248. return;
  249. // ----------------------------------------------------------------------
  250. default:
  251. return;
  252. } // end switch( hr )
  253. // no keys returned
  254. if( num == 0 )
  255. return;
  256. // set the key
  257. key->key = (UnsignedByte)(kbdat.dwOfs & 0xFF);
  258. // sequence
  259. key->sequence = kbdat.dwSequence;
  260. //
  261. // state of key, note we are setting the key state here with an assignment
  262. // and not a bit set of the up/down state, this is the "start"
  263. // of building this "key"
  264. //
  265. key->state = (( kbdat.dwData & 0x0080 ) ? KEY_STATE_DOWN : KEY_STATE_UP);
  266. // set status as unused (unprocessed)
  267. key->status = KeyboardIO::STATUS_UNUSED;
  268. } // end if, we have a DI keyboard device
  269. } // end getKey
  270. ///////////////////////////////////////////////////////////////////////////////
  271. // PUBLIC FUNCTIONS ///////////////////////////////////////////////////////////
  272. ///////////////////////////////////////////////////////////////////////////////
  273. //-------------------------------------------------------------------------------------------------
  274. //-------------------------------------------------------------------------------------------------
  275. DirectInputKeyboard::DirectInputKeyboard( void )
  276. {
  277. m_pDirectInput = NULL;
  278. m_pKeyboardDevice = NULL;
  279. if( GetKeyState( VK_CAPITAL ) & 0x01 )
  280. {
  281. m_modifiers |= KEY_STATE_CAPSLOCK;
  282. }
  283. else
  284. {
  285. m_modifiers &= ~KEY_STATE_CAPSLOCK;
  286. }
  287. } // end DirectInputKeyboard
  288. //-------------------------------------------------------------------------------------------------
  289. //-------------------------------------------------------------------------------------------------
  290. DirectInputKeyboard::~DirectInputKeyboard( void )
  291. {
  292. // close keyboard and release all resource
  293. closeKeyboard();
  294. } // end ~DirectInputKeyboard
  295. //-------------------------------------------------------------------------------------------------
  296. /** initialize the keyboard */
  297. //-------------------------------------------------------------------------------------------------
  298. void DirectInputKeyboard::init( void )
  299. {
  300. // extending functionality
  301. Keyboard::init();
  302. // open the direct input keyboard
  303. openKeyboard();
  304. } // end init
  305. //-------------------------------------------------------------------------------------------------
  306. /** Reset keyboard system */
  307. //-------------------------------------------------------------------------------------------------
  308. void DirectInputKeyboard::reset( void )
  309. {
  310. // extend functionality
  311. Keyboard::reset();
  312. } // end reset
  313. //-------------------------------------------------------------------------------------------------
  314. /** called once per frame to update the keyboard state */
  315. //-------------------------------------------------------------------------------------------------
  316. void DirectInputKeyboard::update( void )
  317. {
  318. // extending functionality
  319. Keyboard::update();
  320. /*
  321. // make sure the keyboard buffer is flushed
  322. if( m_pKeyboardDevice )
  323. {
  324. DWORD items = INFINITE;
  325. m_pKeyboardDevice->GetDeviceData( sizeof( DIDEVICEOBJECTDATA ),
  326. NULL, &items, 0 );
  327. } // end if
  328. */
  329. } // end update
  330. //-------------------------------------------------------------------------------------------------
  331. /** Return TRUE if the caps lock key is down/hilighted */
  332. //-------------------------------------------------------------------------------------------------
  333. Bool DirectInputKeyboard::getCapsState( void )
  334. {
  335. return BitTest( GetKeyState( VK_CAPITAL ), 0X01);
  336. } // end getCapsState