winWindow.cpp 16 KB

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