winWindow.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "platform/platform.h"
  23. #include "platform/event.h"
  24. #include "platformWin32/platformWin32.h"
  25. #include "platformWin32/winConsole.h"
  26. #include "platformWin32/winDirectInput.h"
  27. #include "windowManager/win32/win32Window.h"
  28. #include "console/console.h"
  29. #include "math/mRandom.h"
  30. #include "core/stream/fileStream.h"
  31. #include "T3D/resource.h"
  32. #include <d3d9.h>
  33. #include "gfx/gfxInit.h"
  34. #include "gfx/gfxDevice.h"
  35. #include "core/strings/unicode.h"
  36. #include "gui/core/guiCanvas.h"
  37. extern void createFontInit();
  38. extern void createFontShutdown();
  39. extern void installRedBookDevices();
  40. extern void handleRedBookCallback(U32, U32);
  41. static MRandomLCG sgPlatRandom;
  42. static bool sgQueueEvents;
  43. // is keyboard input a standard (non-changing) VK keycode
  44. #define dIsStandardVK(c) (((0x08 <= (c)) && ((c) <= 0x12)) || \
  45. ((c) == 0x1b) || \
  46. ((0x20 <= (c)) && ((c) <= 0x2e)) || \
  47. ((0x30 <= (c)) && ((c) <= 0x39)) || \
  48. ((0x41 <= (c)) && ((c) <= 0x5a)) || \
  49. ((0x70 <= (c)) && ((c) <= 0x7B)))
  50. extern InputObjectInstances DIK_to_Key( U8 dikCode );
  51. // static helper variables
  52. static HANDLE gMutexHandle = NULL;
  53. static bool sgDoubleByteEnabled = false;
  54. // track window states
  55. Win32PlatState winState;
  56. //-----------------------------------------------------------------------------------------------------------------------------------------------------------
  57. //
  58. // Microsoft Layer for Unicode
  59. // http://msdn.microsoft.com/library/default.asp?url=/library/en-us/mslu/winprog/compiling_your_application_with_the_microsoft_layer_for_unicode.asp
  60. //
  61. //-----------------------------------------------------------------------------------------------------------------------------------------------------------
  62. #ifdef UNICODE
  63. HMODULE LoadUnicowsProc(void)
  64. {
  65. return(LoadLibraryA("unicows.dll"));
  66. }
  67. #ifdef _cplusplus
  68. extern "C" {
  69. #endif
  70. extern FARPROC _PfnLoadUnicows = (FARPROC) &LoadUnicowsProc;
  71. #ifdef _cplusplus
  72. }
  73. #endif
  74. #endif
  75. //--------------------------------------
  76. Win32PlatState::Win32PlatState()
  77. {
  78. log_fp = NULL;
  79. hinstOpenGL = NULL;
  80. hinstGLU = NULL;
  81. hinstOpenAL = NULL;
  82. appDC = NULL;
  83. appInstance = NULL;
  84. currentTime = 0;
  85. processId = 0;
  86. }
  87. //--------------------------------------
  88. bool Platform::excludeOtherInstances(const char *mutexName)
  89. {
  90. #ifdef UNICODE
  91. UTF16 b[512];
  92. convertUTF8toUTF16((UTF8 *)mutexName, b, sizeof(b));
  93. gMutexHandle = CreateMutex(NULL, true, b);
  94. #else
  95. gMutexHandle = CreateMutex(NULL, true, mutexName);
  96. #endif
  97. if(!gMutexHandle)
  98. return false;
  99. if(GetLastError() == ERROR_ALREADY_EXISTS)
  100. {
  101. CloseHandle(gMutexHandle);
  102. gMutexHandle = NULL;
  103. return false;
  104. }
  105. return true;
  106. }
  107. void Platform::restartInstance()
  108. {
  109. STARTUPINFO si;
  110. PROCESS_INFORMATION pi;
  111. ZeroMemory( &si, sizeof(si) );
  112. si.cb = sizeof(si);
  113. ZeroMemory( &pi, sizeof(pi) );
  114. TCHAR cen_buf[2048];
  115. GetModuleFileName( NULL, cen_buf, 2047);
  116. // Start the child process.
  117. if( CreateProcess( cen_buf,
  118. NULL, // Command line
  119. NULL, // Process handle not inheritable
  120. NULL, // Thread handle not inheritable
  121. FALSE, // Set handle inheritance to FALSE
  122. 0, // No creation flags
  123. NULL, // Use parent's environment block
  124. NULL, // Use parent's starting directory
  125. &si, // Pointer to STARTUPINFO structure
  126. &pi ) // Pointer to PROCESS_INFORMATION structure
  127. != false )
  128. {
  129. WaitForInputIdle( pi.hProcess, 5000 );
  130. CloseHandle( pi.hProcess );
  131. CloseHandle( pi.hThread );
  132. }
  133. }
  134. ///just check if the app's global mutex exists, and if so,
  135. ///return true - otherwise, false. Should be called before ExcludeOther
  136. /// at very start of app execution.
  137. bool Platform::checkOtherInstances(const char *mutexName)
  138. {
  139. #ifdef TORQUE_MULTITHREAD
  140. HANDLE pMutex = NULL;
  141. #ifdef UNICODE
  142. UTF16 b[512];
  143. convertUTF8toUTF16((UTF8 *)mutexName, b, sizeof(b));
  144. pMutex = CreateMutex(NULL, true, b);
  145. #else
  146. pMutex = CreateMutex(NULL, true, mutexName);
  147. #endif
  148. if(!pMutex)
  149. return false;
  150. if(GetLastError() == ERROR_ALREADY_EXISTS)
  151. {
  152. //another mutex of the same name exists
  153. //close ours
  154. CloseHandle(pMutex);
  155. pMutex = NULL;
  156. return true;
  157. }
  158. CloseHandle(pMutex);
  159. pMutex = NULL;
  160. #endif
  161. //we don;t care, always false
  162. return false;
  163. }
  164. //--------------------------------------
  165. void Platform::AlertOK(const char *windowTitle, const char *message)
  166. {
  167. ShowCursor(true);
  168. #ifdef UNICODE
  169. UTF16 m[1024], t[512];
  170. convertUTF8toUTF16((UTF8 *)windowTitle, t, sizeof(t));
  171. convertUTF8toUTF16((UTF8 *)message, m, sizeof(m));
  172. MessageBox(NULL, m, t, MB_ICONINFORMATION | MB_SETFOREGROUND | MB_TASKMODAL | MB_OK);
  173. #else
  174. MessageBox(NULL, message, windowTitle, MB_ICONINFORMATION | MB_SETFOREGROUND | MB_TASKMODAL | MB_OK);
  175. #endif
  176. }
  177. //--------------------------------------
  178. bool Platform::AlertOKCancel(const char *windowTitle, const char *message)
  179. {
  180. ShowCursor(true);
  181. #ifdef UNICODE
  182. UTF16 m[1024], t[512];
  183. convertUTF8toUTF16((UTF8 *)windowTitle, t, sizeof(t));
  184. convertUTF8toUTF16((UTF8 *)message, m, sizeof(m));
  185. return MessageBox(NULL, m, t, MB_ICONINFORMATION | MB_SETFOREGROUND | MB_TASKMODAL | MB_OKCANCEL) == IDOK;
  186. #else
  187. return MessageBox(NULL, message, windowTitle, MB_ICONINFORMATION | MB_SETFOREGROUND | MB_TASKMODAL | MB_OKCANCEL) == IDOK;
  188. #endif
  189. }
  190. //--------------------------------------
  191. bool Platform::AlertRetry(const char *windowTitle, const char *message)
  192. {
  193. ShowCursor(true);
  194. #ifdef UNICODE
  195. UTF16 m[1024], t[512];
  196. convertUTF8toUTF16((UTF8 *)windowTitle, t, sizeof(t));
  197. convertUTF8toUTF16((UTF8 *)message, m, sizeof(m));
  198. return (MessageBox(NULL, m, t, MB_ICONINFORMATION | MB_SETFOREGROUND | MB_TASKMODAL | MB_RETRYCANCEL) == IDRETRY);
  199. #else
  200. return (MessageBox(NULL, message, windowTitle, MB_ICONINFORMATION | MB_SETFOREGROUND | MB_TASKMODAL | MB_RETRYCANCEL) == IDRETRY);
  201. #endif
  202. }
  203. //--------------------------------------
  204. HIMC gIMEContext;
  205. static void InitInput()
  206. {
  207. #ifndef TORQUE_LIB
  208. #ifdef UNICODE
  209. //gIMEContext = ImmGetContext(getWin32WindowHandle());
  210. //ImmReleaseContext( getWin32WindowHandle(), gIMEContext );
  211. #endif
  212. #endif
  213. }
  214. //--------------------------------------
  215. void Platform::init()
  216. {
  217. Con::printf("Initializing platform...");
  218. // Set the platform variable for the scripts
  219. Con::setVariable( "$platform", "windows" );
  220. WinConsole::create();
  221. if ( !WinConsole::isEnabled() )
  222. Input::init();
  223. InitInput(); // in case DirectInput falls through
  224. installRedBookDevices();
  225. sgDoubleByteEnabled = GetSystemMetrics( SM_DBCSENABLED );
  226. sgQueueEvents = true;
  227. Con::printf("Done");
  228. }
  229. //--------------------------------------
  230. void Platform::shutdown()
  231. {
  232. sgQueueEvents = false;
  233. if(gMutexHandle)
  234. CloseHandle(gMutexHandle);
  235. Input::destroy();
  236. GFXDevice::destroy();
  237. WinConsole::destroy();
  238. }
  239. extern bool LinkConsoleFunctions;
  240. #ifndef TORQUE_SHARED
  241. extern S32 TorqueMain(S32 argc, const char **argv);
  242. //--------------------------------------
  243. static S32 run(S32 argc, const char **argv)
  244. {
  245. // Console hack to ensure consolefunctions get linked in
  246. LinkConsoleFunctions=true;
  247. createFontInit();
  248. S32 ret = TorqueMain(argc, argv);
  249. createFontShutdown();
  250. return ret;
  251. }
  252. //--------------------------------------
  253. S32 main(S32 argc, const char **argv)
  254. {
  255. winState.appInstance = GetModuleHandle(NULL);
  256. return run(argc, argv);
  257. }
  258. //--------------------------------------
  259. #include "unit/test.h"
  260. #include "app/mainLoop.h"
  261. S32 PASCAL WinMain( HINSTANCE hInstance, HINSTANCE, LPSTR lpszCmdLine, S32)
  262. {
  263. #if 0
  264. // Run a unit test.
  265. StandardMainLoop::initCore();
  266. UnitTesting::TestRun tr;
  267. tr.test("Platform", true);
  268. #else
  269. Vector<char *> argv( __FILE__, __LINE__ );
  270. char moduleName[256];
  271. #ifdef TORQUE_UNICODE
  272. {
  273. TCHAR buf[ 256 ];
  274. GetModuleFileNameW( NULL, buf, sizeof( buf ) );
  275. convertUTF16toUTF8( buf, moduleName, sizeof( moduleName ) );
  276. }
  277. #else
  278. GetModuleFileNameA(NULL, moduleName, sizeof(moduleName));
  279. #endif
  280. argv.push_back(moduleName);
  281. for (const char* word,*ptr = lpszCmdLine; *ptr; )
  282. {
  283. // Eat white space
  284. for (; dIsspace(*ptr) && *ptr; ptr++)
  285. ;
  286. // Pick out the next word
  287. for (word = ptr; !dIsspace(*ptr) && *ptr; ptr++)
  288. ;
  289. // Add the word to the argument list.
  290. if (*word)
  291. {
  292. int len = ptr - word;
  293. char *arg = (char *) dMalloc(len + 1);
  294. dStrncpy(arg, word, len);
  295. arg[len] = 0;
  296. argv.push_back(arg);
  297. }
  298. }
  299. winState.appInstance = hInstance;
  300. S32 retVal = run(argv.size(), (const char **) argv.address());
  301. for(U32 j = 1; j < argv.size(); j++)
  302. dFree(argv[j]);
  303. return retVal;
  304. #endif
  305. }
  306. #else //TORQUE_SHARED
  307. extern "C"
  308. {
  309. bool torque_engineinit(int argc, const char **argv);
  310. int torque_enginetick();
  311. bool torque_engineshutdown();
  312. };
  313. int TorqueMain(int argc, const char **argv)
  314. {
  315. if (!torque_engineinit(argc, argv))
  316. return 1;
  317. while(torque_enginetick())
  318. {
  319. }
  320. torque_engineshutdown();
  321. return 0;
  322. }
  323. extern "C" {
  324. S32 torque_winmain( HINSTANCE hInstance, HINSTANCE, LPSTR lpszCmdLine, S32)
  325. {
  326. Vector<char *> argv( __FILE__, __LINE__ );
  327. char moduleName[256];
  328. #ifdef TORQUE_UNICODE
  329. {
  330. TCHAR buf[ 256 ];
  331. GetModuleFileNameW( NULL, buf, sizeof( buf ) );
  332. convertUTF16toUTF8( buf, moduleName, sizeof( moduleName ) );
  333. }
  334. #else
  335. GetModuleFileNameA(NULL, moduleName, sizeof(moduleName));
  336. #endif
  337. argv.push_back(moduleName);
  338. for (const char* word,*ptr = lpszCmdLine; *ptr; )
  339. {
  340. // Eat white space
  341. for (; dIsspace(*ptr) && *ptr; ptr++)
  342. ;
  343. // Test for quotes
  344. bool withinQuotes = dIsquote(*ptr);
  345. if (!withinQuotes)
  346. {
  347. // Pick out the next word
  348. for (word = ptr; !dIsspace(*ptr) && *ptr; ptr++)
  349. ;
  350. }
  351. else
  352. {
  353. // Advance past the first quote. We don't want to include it.
  354. ptr++;
  355. // Pick out the next quote
  356. for (word = ptr; !dIsquote(*ptr) && *ptr; ptr++)
  357. ;
  358. }
  359. // Add the word to the argument list.
  360. if (*word)
  361. {
  362. int len = ptr - word;
  363. char *arg = (char *) dMalloc(len + 1);
  364. dStrncpy(arg, word, len);
  365. arg[len] = 0;
  366. argv.push_back(arg);
  367. }
  368. // If we had a quote, skip past it for the next arg
  369. if (withinQuotes && *ptr)
  370. {
  371. ptr++;
  372. }
  373. }
  374. winState.appInstance = hInstance;
  375. S32 retVal = TorqueMain(argv.size(), (const char **) argv.address());
  376. for(U32 j = 1; j < argv.size(); j++)
  377. dFree(argv[j]);
  378. return retVal;
  379. }
  380. } // extern "C"
  381. #endif
  382. //--------------------------------------
  383. F32 Platform::getRandom()
  384. {
  385. return sgPlatRandom.randF();
  386. }
  387. ////--------------------------------------
  388. /// Spawn the default Operating System web browser with a URL
  389. /// @param webAddress URL to pass to browser
  390. /// @return true if browser successfully spawned
  391. bool Platform::openWebBrowser( const char* webAddress )
  392. {
  393. static bool sHaveKey = false;
  394. static wchar_t sWebKey[512];
  395. char utf8WebKey[512];
  396. {
  397. HKEY regKey;
  398. DWORD size = sizeof( sWebKey );
  399. if ( RegOpenKeyEx( HKEY_CLASSES_ROOT, dT("\\http\\shell\\open\\command"), 0, KEY_QUERY_VALUE, &regKey ) != ERROR_SUCCESS )
  400. {
  401. Con::errorf( ConsoleLogEntry::General, "Platform::openWebBrowser - Failed to open the HKCR\\http registry key!!!");
  402. return( false );
  403. }
  404. if ( RegQueryValueEx( regKey, dT(""), NULL, NULL, (unsigned char *)sWebKey, &size ) != ERROR_SUCCESS )
  405. {
  406. Con::errorf( ConsoleLogEntry::General, "Platform::openWebBrowser - Failed to query the open command registry key!!!" );
  407. return( false );
  408. }
  409. RegCloseKey( regKey );
  410. sHaveKey = true;
  411. convertUTF16toUTF8(sWebKey,utf8WebKey,512);
  412. #ifdef UNICODE
  413. char *p = dStrstr((const char *)utf8WebKey, "%1");
  414. #else
  415. char *p = strstr( (const char *) sWebKey , "%1");
  416. #endif
  417. if (p) *p = 0;
  418. }
  419. STARTUPINFO si;
  420. dMemset( &si, 0, sizeof( si ) );
  421. si.cb = sizeof( si );
  422. char buf[1024];
  423. #ifdef UNICODE
  424. dSprintf( buf, sizeof( buf ), "%s %s", utf8WebKey, webAddress );
  425. UTF16 b[1024];
  426. convertUTF8toUTF16((UTF8 *)buf, b, sizeof(b));
  427. #else
  428. dSprintf( buf, sizeof( buf ), "%s %s", sWebKey, webAddress );
  429. #endif
  430. //Con::errorf( ConsoleLogEntry::General, "** Web browser command = %s **", buf );
  431. PROCESS_INFORMATION pi;
  432. dMemset( &pi, 0, sizeof( pi ) );
  433. CreateProcess( NULL,
  434. #ifdef UNICODE
  435. b,
  436. #else
  437. buf,
  438. #endif
  439. NULL,
  440. NULL,
  441. false,
  442. CREATE_NEW_CONSOLE | CREATE_NEW_PROCESS_GROUP,
  443. NULL,
  444. NULL,
  445. &si,
  446. &pi );
  447. return( true );
  448. }
  449. //--------------------------------------
  450. // Login password routines:
  451. //--------------------------------------
  452. #ifdef UNICODE
  453. static const UTF16* TorqueRegKey = dT("SOFTWARE\\GarageGames\\Torque");
  454. #else
  455. static const char* TorqueRegKey = "SOFTWARE\\GarageGames\\Torque";
  456. #endif
  457. const char* Platform::getLoginPassword()
  458. {
  459. HKEY regKey;
  460. char* returnString = NULL;
  461. if ( RegOpenKeyEx( HKEY_LOCAL_MACHINE, TorqueRegKey, 0, KEY_QUERY_VALUE, &regKey ) == ERROR_SUCCESS )
  462. {
  463. U8 buf[32];
  464. DWORD size = sizeof( buf );
  465. if ( RegQueryValueEx( regKey, dT("LoginPassword"), NULL, NULL, buf, &size ) == ERROR_SUCCESS )
  466. {
  467. returnString = Con::getReturnBuffer( size + 1 );
  468. dStrcpy( returnString, (const char*) buf );
  469. }
  470. RegCloseKey( regKey );
  471. }
  472. if ( returnString )
  473. return( returnString );
  474. else
  475. return( "" );
  476. }
  477. //--------------------------------------
  478. bool Platform::setLoginPassword( const char* password )
  479. {
  480. HKEY regKey;
  481. if ( RegOpenKeyEx( HKEY_LOCAL_MACHINE, TorqueRegKey, 0, KEY_WRITE, &regKey ) == ERROR_SUCCESS )
  482. {
  483. if ( RegSetValueEx( regKey, dT("LoginPassword"), 0, REG_SZ, (const U8*) password, dStrlen( password ) + 1 ) != ERROR_SUCCESS )
  484. Con::errorf( ConsoleLogEntry::General, "setLoginPassword - Failed to set the subkey value!" );
  485. RegCloseKey( regKey );
  486. return( true );
  487. }
  488. else
  489. Con::errorf( ConsoleLogEntry::General, "setLoginPassword - Failed to open the Torque registry key!" );
  490. return( false );
  491. }
  492. //--------------------------------------
  493. // Silly Korean registry key checker:
  494. //
  495. // NOTE: "Silly" refers to the nature of this hack, and is not intended
  496. // as commentary on Koreans as a nationality. Thank you for your
  497. // attention.
  498. //--------------------------------------
  499. ConsoleFunction( isKoreanBuild, bool, 1, 1, "isKoreanBuild()" )
  500. {
  501. argc; argv;
  502. HKEY regKey;
  503. bool result = false;
  504. if ( RegOpenKeyEx( HKEY_LOCAL_MACHINE, TorqueRegKey, 0, KEY_QUERY_VALUE, &regKey ) == ERROR_SUCCESS )
  505. {
  506. DWORD val;
  507. DWORD size = sizeof( val );
  508. if ( RegQueryValueEx( regKey, dT("Korean"), NULL, NULL, (U8*) &val, &size ) == ERROR_SUCCESS )
  509. result = ( val > 0 );
  510. RegCloseKey( regKey );
  511. }
  512. return( result );
  513. }