x86UNIXWindow.client.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859
  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 "console/console.h"
  23. #include "core/fileStream.h"
  24. #include "game/resource.h"
  25. #include "game/version.h"
  26. #include "math/mRandom.h"
  27. #include "platformX86UNIX/platformX86UNIX.h"
  28. #include "platformX86UNIX/x86UNIXStdConsole.h"
  29. #include "platform/event.h"
  30. #include "platform/gameInterface.h"
  31. #include "platform/platform.h"
  32. #include "platform/platformAL.h"
  33. #include "platform/platformInput.h"
  34. #include "platform/platformVideo.h"
  35. #include "platform/profiler.h"
  36. #include "platformX86UNIX/platformGL.h"
  37. #include "platformX86UNIX/x86UNIXOGLVideo.h"
  38. #include "platformX86UNIX/x86UNIXState.h"
  39. #ifndef TORQUE_DEDICATED
  40. #include "platformX86UNIX/x86UNIXMessageBox.h"
  41. #include "platformX86UNIX/x86UNIXInputManager.h"
  42. #endif
  43. #include <errno.h>
  44. #include <signal.h>
  45. #include <stdlib.h>
  46. #include <unistd.h> // fork, execvp, chdir
  47. #include <time.h> // nanosleep
  48. #ifndef TORQUE_DEDICATED
  49. #include <X11/Xlib.h>
  50. #include <X11/Xos.h>
  51. #include <SDL/SDL.h>
  52. #include <SDL/SDL_syswm.h>
  53. #include <SDL/SDL_version.h>
  54. #endif
  55. x86UNIXPlatformState *x86UNIXState;
  56. bool DisplayPtrManager::sgDisplayLocked = false;
  57. LockFunc_t DisplayPtrManager::sgLockFunc = NULL;
  58. LockFunc_t DisplayPtrManager::sgUnlockFunc = NULL;
  59. static U32 lastTimeTick;
  60. static MRandomLCG sgPlatRandom;
  61. #ifndef TORQUE_DEDICATED
  62. extern void InstallRedBookDevices();
  63. extern void PollRedbookDevices();
  64. extern bool InitOpenGL();
  65. // This is called when some X client sends
  66. // a selection event (e.g. SelectionRequest)
  67. // to the window
  68. extern void NotifySelectionEvent(XEvent& event);
  69. #endif
  70. //------------------------------------------------------------------------------
  71. static S32 ParseCommandLine(S32 argc, const char **argv,
  72. Vector<char*>& newCommandLine)
  73. {
  74. x86UNIXState->setExePathName(argv[0]);
  75. bool foundDedicated = false;
  76. for ( int i=0; i < argc; i++ )
  77. {
  78. // look for platform specific args
  79. if (dStrcmp(argv[i], "-version") == 0)
  80. {
  81. dPrintf("%s (built on %s)\n", getVersionString(), getCompileTimeString());
  82. dPrintf("gcc: %s\n", __VERSION__);
  83. return 1;
  84. }
  85. if (dStrcmp(argv[i], "-cdaudio") == 0)
  86. {
  87. x86UNIXState->setCDAudioEnabled(true);
  88. continue;
  89. }
  90. if (dStrcmp(argv[i], "-dedicated") == 0)
  91. {
  92. foundDedicated = true;
  93. // no continue because dedicated is also handled by script
  94. }
  95. if (dStrcmp(argv[i], "-dsleep") == 0)
  96. {
  97. x86UNIXState->setDSleep(true);
  98. continue;
  99. }
  100. if (dStrcmp(argv[i], "-nohomedir") == 0)
  101. {
  102. x86UNIXState->setUseRedirect(false);
  103. continue;
  104. }
  105. if (dStrcmp(argv[i], "-chdir") == 0)
  106. {
  107. if ( ++i >= argc )
  108. {
  109. dPrintf("Follow -chdir option with the desired working directory.\n");
  110. return 1;
  111. }
  112. if (chdir(argv[i]) == -1)
  113. {
  114. dPrintf("Unable to chdir to %s: %s\n", argv[i], strerror(errno));
  115. return 1;
  116. }
  117. continue;
  118. }
  119. // copy the arg into newCommandLine
  120. int argLen = dStrlen(argv[i]) + 1;
  121. char* argBuf = new char[argLen]; // this memory is deleted in main()
  122. dStrncpy(argBuf, argv[i], argLen);
  123. newCommandLine.push_back(argBuf);
  124. }
  125. x86UNIXState->setDedicated(foundDedicated);
  126. #if defined(DEDICATED) && !defined(TORQUE_ENGINE)
  127. if (!foundDedicated)
  128. {
  129. dPrintf("This is a dedicated server build. You must supply the -dedicated command line parameter.\n");
  130. return 1;
  131. }
  132. #endif
  133. return 0;
  134. }
  135. static void DetectWindowingSystem()
  136. {
  137. #ifndef TORQUE_DEDICATED
  138. Display* dpy = XOpenDisplay(NULL);
  139. if (dpy != NULL)
  140. {
  141. x86UNIXState->setXWindowsRunning(true);
  142. XCloseDisplay(dpy);
  143. }
  144. #endif
  145. }
  146. //------------------------------------------------------------------------------
  147. static void InitWindow(const Point2I &initialSize, const char *name)
  148. {
  149. x86UNIXState->setWindowSize(initialSize);
  150. x86UNIXState->setWindowName(name);
  151. }
  152. #ifndef TORQUE_DEDICATED
  153. //------------------------------------------------------------------------------
  154. static bool InitSDL()
  155. {
  156. if (SDL_Init(SDL_INIT_VIDEO) != 0)
  157. return false;
  158. atexit(SDL_Quit);
  159. SDL_SysWMinfo sysinfo;
  160. SDL_VERSION(&sysinfo.version);
  161. if (SDL_GetWMInfo(&sysinfo) == 0)
  162. return false;
  163. x86UNIXState->setDisplayPointer(sysinfo.info.x11.display);
  164. DisplayPtrManager::setDisplayLockFunction(sysinfo.info.x11.lock_func);
  165. DisplayPtrManager::setDisplayUnlockFunction(sysinfo.info.x11.unlock_func);
  166. DisplayPtrManager xdisplay;
  167. Display* display = xdisplay.getDisplayPointer();
  168. x86UNIXState->setScreenNumber(
  169. DefaultScreen( display ) );
  170. x86UNIXState->setScreenPointer(
  171. DefaultScreenOfDisplay( display ) );
  172. x86UNIXState->setDesktopSize(
  173. (S32) DisplayWidth(
  174. display,
  175. x86UNIXState->getScreenNumber()),
  176. (S32) DisplayHeight(
  177. display,
  178. x86UNIXState->getScreenNumber())
  179. );
  180. x86UNIXState->setDesktopBpp(
  181. (S32) DefaultDepth(
  182. display,
  183. x86UNIXState->getScreenNumber()));
  184. // indicate that we want sys WM messages
  185. SDL_EventState(SDL_SYSWMEVENT, SDL_ENABLE);
  186. return true;
  187. }
  188. //------------------------------------------------------------------------------
  189. static void ProcessSYSWMEvent(const SDL_Event& event)
  190. {
  191. XEvent& xevent = event.syswm.msg->event.xevent;
  192. //Con::printf("xevent : %d", xevent.type);
  193. switch (xevent.type)
  194. {
  195. case SelectionRequest:
  196. // somebody wants our clipboard
  197. NotifySelectionEvent(xevent);
  198. break;
  199. }
  200. }
  201. //------------------------------------------------------------------------------
  202. static void SetAppState()
  203. {
  204. U8 state = SDL_GetAppState();
  205. // if we're not active but we have appactive and inputfocus, set window
  206. // active and reactivate input
  207. if ((!x86UNIXState->windowActive() || !Input::isActive()) &&
  208. state & SDL_APPACTIVE &&
  209. state & SDL_APPINPUTFOCUS)
  210. {
  211. x86UNIXState->setWindowActive(true);
  212. Input::reactivate();
  213. }
  214. // if we are active, but we don't have appactive or input focus,
  215. // deactivate input (if window not locked) and clear windowActive
  216. else if (x86UNIXState->windowActive() &&
  217. !(state & SDL_APPACTIVE && state & SDL_APPINPUTFOCUS))
  218. {
  219. if (x86UNIXState->windowLocked())
  220. Input::deactivate();
  221. x86UNIXState->setWindowActive(false);
  222. }
  223. }
  224. //------------------------------------------------------------------------------
  225. static S32 NumEventsPending()
  226. {
  227. static const int MaxEvents = 255;
  228. static SDL_Event events[MaxEvents];
  229. SDL_PumpEvents();
  230. return SDL_PeepEvents(events, MaxEvents, SDL_PEEKEVENT, SDL_ALLEVENTS);
  231. }
  232. //------------------------------------------------------------------------------
  233. static void PrintSDLEventQueue()
  234. {
  235. static const int MaxEvents = 255;
  236. static SDL_Event events[MaxEvents];
  237. SDL_PumpEvents();
  238. S32 numEvents = SDL_PeepEvents(
  239. events, MaxEvents, SDL_PEEKEVENT, SDL_ALLEVENTS);
  240. if (numEvents <= 0)
  241. {
  242. dPrintf("SDL Event Queue is empty\n");
  243. return;
  244. }
  245. dPrintf("SDL Event Queue:\n");
  246. for (int i = 0; i < numEvents; ++i)
  247. {
  248. const char *eventType;
  249. switch (events[i].type)
  250. {
  251. case SDL_NOEVENT: eventType = "SDL_NOEVENT"; break;
  252. case SDL_ACTIVEEVENT: eventType = "SDL_ACTIVEEVENT"; break;
  253. case SDL_KEYDOWN: eventType = "SDL_KEYDOWN"; break;
  254. case SDL_KEYUP: eventType = "SDL_KEYUP"; break;
  255. case SDL_MOUSEMOTION: eventType = "SDL_MOUSEMOTION"; break;
  256. case SDL_MOUSEBUTTONDOWN: eventType = "SDL_MOUSEBUTTONDOWN"; break;
  257. case SDL_MOUSEBUTTONUP: eventType = "SDL_MOUSEBUTTONUP"; break;
  258. case SDL_JOYAXISMOTION: eventType = "SDL_JOYAXISMOTION"; break;
  259. case SDL_JOYBALLMOTION: eventType = "SDL_JOYBALLMOTION"; break;
  260. case SDL_JOYHATMOTION: eventType = "SDL_JOYHATMOTION"; break;
  261. case SDL_JOYBUTTONDOWN: eventType = "SDL_JOYBUTTONDOWN"; break;
  262. case SDL_JOYBUTTONUP: eventType = "SDL_JOYBUTTONUP"; break;
  263. case SDL_QUIT: eventType = "SDL_QUIT"; break;
  264. case SDL_SYSWMEVENT: eventType = "SDL_SYSWMEVENT"; break;
  265. case SDL_VIDEORESIZE: eventType = "SDL_VIDEORESIZE"; break;
  266. case SDL_VIDEOEXPOSE: eventType = "SDL_VIDEOEXPOSE"; break;
  267. /* Events SDL_USEREVENT through SDL_MAXEVENTS-1 are for your use */
  268. case SDL_USEREVENT: eventType = "SDL_USEREVENT"; break;
  269. default: eventType = "UNKNOWN!"; break;
  270. }
  271. dPrintf("Event %d: %s\n", i, eventType);
  272. }
  273. }
  274. //------------------------------------------------------------------------------
  275. static bool ProcessMessages()
  276. {
  277. static const int MaxEvents = 255;
  278. static const U32 Mask =
  279. SDL_QUITMASK | SDL_VIDEORESIZEMASK | SDL_VIDEOEXPOSEMASK |
  280. SDL_ACTIVEEVENTMASK | SDL_SYSWMEVENTMASK |
  281. SDL_EVENTMASK(SDL_USEREVENT);
  282. static SDL_Event events[MaxEvents];
  283. SDL_PumpEvents();
  284. S32 numEvents = SDL_PeepEvents(events, MaxEvents, SDL_GETEVENT, Mask);
  285. if (numEvents == 0)
  286. return true;
  287. for (int i = 0; i < numEvents; ++i)
  288. {
  289. SDL_Event& event = events[i];
  290. switch (event.type)
  291. {
  292. case SDL_QUIT:
  293. return false;
  294. break;
  295. case SDL_VIDEORESIZE:
  296. case SDL_VIDEOEXPOSE:
  297. Game->refreshWindow();
  298. break;
  299. case SDL_USEREVENT:
  300. if (event.user.code == TORQUE_SETVIDEOMODE)
  301. {
  302. SetAppState();
  303. // SDL will send a motion event to restore the mouse position
  304. // on the new window. Ignore that if the window is locked.
  305. if (x86UNIXState->windowLocked())
  306. {
  307. SDL_Event tempEvent;
  308. SDL_PeepEvents(&tempEvent, 1, SDL_GETEVENT,
  309. SDL_MOUSEMOTIONMASK);
  310. }
  311. }
  312. break;
  313. case SDL_ACTIVEEVENT:
  314. SetAppState();
  315. break;
  316. case SDL_SYSWMEVENT:
  317. ProcessSYSWMEvent(event);
  318. break;
  319. }
  320. }
  321. return true;
  322. }
  323. //------------------------------------------------------------------------------
  324. // send a destroy window event to the window. assumes
  325. // window is created.
  326. void SendQuitEvent()
  327. {
  328. SDL_Event quitevent;
  329. quitevent.type = SDL_QUIT;
  330. SDL_PushEvent(&quitevent);
  331. }
  332. #endif // DEDICATED
  333. //------------------------------------------------------------------------------
  334. static inline void Sleep(int secs, int nanoSecs)
  335. {
  336. timespec sleeptime;
  337. sleeptime.tv_sec = secs;
  338. sleeptime.tv_nsec = nanoSecs;
  339. nanosleep(&sleeptime, NULL);
  340. }
  341. #ifndef TORQUE_DEDICATED
  342. struct AlertWinState
  343. {
  344. bool fullScreen;
  345. bool cursorHidden;
  346. bool inputGrabbed;
  347. };
  348. //------------------------------------------------------------------------------
  349. void DisplayErrorAlert(const char* errMsg, bool showSDLError)
  350. {
  351. char fullErrMsg[2048];
  352. dStrncpy(fullErrMsg, errMsg, sizeof(fullErrMsg));
  353. if (showSDLError)
  354. {
  355. char* sdlerror = SDL_GetError();
  356. if (sdlerror != NULL && dStrlen(sdlerror) > 0)
  357. {
  358. dStrcat(fullErrMsg, " (Error: ");
  359. dStrcat(fullErrMsg, sdlerror);
  360. dStrcat(fullErrMsg, ")");
  361. }
  362. }
  363. Platform::AlertOK("Error", fullErrMsg);
  364. }
  365. //------------------------------------------------------------------------------
  366. static inline void AlertDisableVideo(AlertWinState& state)
  367. {
  368. state.fullScreen = Video::isFullScreen();
  369. state.cursorHidden = (SDL_ShowCursor(SDL_QUERY) == SDL_DISABLE);
  370. state.inputGrabbed = (SDL_WM_GrabInput(SDL_GRAB_QUERY) == SDL_GRAB_ON);
  371. if (state.fullScreen)
  372. SDL_WM_ToggleFullScreen(SDL_GetVideoSurface());
  373. if (state.cursorHidden)
  374. SDL_ShowCursor(SDL_ENABLE);
  375. if (state.inputGrabbed)
  376. SDL_WM_GrabInput(SDL_GRAB_OFF);
  377. }
  378. //------------------------------------------------------------------------------
  379. static inline void AlertEnableVideo(AlertWinState& state)
  380. {
  381. if (state.fullScreen)
  382. SDL_WM_ToggleFullScreen(SDL_GetVideoSurface());
  383. if (state.cursorHidden)
  384. SDL_ShowCursor(SDL_DISABLE);
  385. if (state.inputGrabbed)
  386. SDL_WM_GrabInput(SDL_GRAB_ON);
  387. }
  388. #endif // DEDICATED
  389. //------------------------------------------------------------------------------
  390. void Platform::AlertOK(const char *windowTitle, const char *message)
  391. {
  392. #ifndef TORQUE_DEDICATED
  393. if (x86UNIXState->isXWindowsRunning())
  394. {
  395. AlertWinState state;
  396. AlertDisableVideo(state);
  397. DisplayPtrManager xdisplay;
  398. XMessageBox mBox(xdisplay.getDisplayPointer());
  399. mBox.alertOK(windowTitle, message);
  400. AlertEnableVideo(state);
  401. }
  402. else
  403. #endif
  404. {
  405. if (Con::isActive() && StdConsole::isEnabled())
  406. Con::printf("Alert: %s %s", windowTitle, message);
  407. else
  408. dPrintf("Alert: %s %s\n", windowTitle, message);
  409. }
  410. }
  411. //------------------------------------------------------------------------------
  412. bool Platform::AlertOKCancel(const char *windowTitle, const char *message)
  413. {
  414. #ifndef TORQUE_DEDICATED
  415. if (x86UNIXState->isXWindowsRunning())
  416. {
  417. AlertWinState state;
  418. AlertDisableVideo(state);
  419. DisplayPtrManager xdisplay;
  420. XMessageBox mBox(xdisplay.getDisplayPointer());
  421. bool val =
  422. mBox.alertOKCancel(windowTitle, message) == XMessageBox::OK;
  423. AlertEnableVideo(state);
  424. return val;
  425. }
  426. else
  427. #endif
  428. {
  429. if (Con::isActive() && StdConsole::isEnabled())
  430. Con::printf("Alert: %s %s", windowTitle, message);
  431. else
  432. dPrintf("Alert: %s %s\n", windowTitle, message);
  433. return false;
  434. }
  435. }
  436. //------------------------------------------------------------------------------
  437. bool Platform::AlertRetry(const char *windowTitle, const char *message)
  438. {
  439. #ifndef TORQUE_DEDICATED
  440. if (x86UNIXState->isXWindowsRunning())
  441. {
  442. AlertWinState state;
  443. AlertDisableVideo(state);
  444. DisplayPtrManager xdisplay;
  445. XMessageBox mBox(xdisplay.getDisplayPointer());
  446. bool val =
  447. mBox.alertRetryCancel(windowTitle, message) == XMessageBox::Retry;
  448. AlertEnableVideo(state);
  449. return val;
  450. }
  451. else
  452. #endif
  453. {
  454. if (Con::isActive() && StdConsole::isEnabled())
  455. Con::printf("Alert: %s %s", windowTitle, message);
  456. else
  457. dPrintf("Alert: %s %s\n", windowTitle, message);
  458. return false;
  459. }
  460. }
  461. //------------------------------------------------------------------------------
  462. bool Platform::excludeOtherInstances(const char *mutexName)
  463. {
  464. return AcquireProcessMutex(mutexName);
  465. }
  466. //------------------------------------------------------------------------------
  467. void Platform::enableKeyboardTranslation(void)
  468. {
  469. #ifndef TORQUE_DEDICATED
  470. // JMQ: not sure if this is needed for i18n keyboards
  471. //SDL_EnableUNICODE( 1 );
  472. // SDL_EnableKeyRepeat(
  473. // SDL_DEFAULT_REPEAT_DELAY,
  474. // SDL_DEFAULT_REPEAT_INTERVAL);
  475. #endif
  476. }
  477. //------------------------------------------------------------------------------
  478. void Platform::disableKeyboardTranslation(void)
  479. {
  480. #ifndef TORQUE_DEDICATED
  481. //SDL_EnableUNICODE( 0 );
  482. // SDL_EnableKeyRepeat(0, 0);
  483. #endif
  484. }
  485. //------------------------------------------------------------------------------
  486. void Platform::setWindowLocked(bool locked)
  487. {
  488. #ifndef TORQUE_DEDICATED
  489. x86UNIXState->setWindowLocked(locked);
  490. UInputManager* uInputManager =
  491. dynamic_cast<UInputManager*>( Input::getManager() );
  492. if ( uInputManager && uInputManager->isEnabled() &&
  493. Input::isActive() )
  494. uInputManager->setWindowLocked(locked);
  495. #endif
  496. }
  497. //------------------------------------------------------------------------------
  498. void Platform::minimizeWindow()
  499. {
  500. #ifndef TORQUE_DEDICATED
  501. if (x86UNIXState->windowCreated())
  502. SDL_WM_IconifyWindow();
  503. #endif
  504. }
  505. //------------------------------------------------------------------------------
  506. void Platform::process()
  507. {
  508. PROFILE_START(XUX_PlatformProcess);
  509. stdConsole->process();
  510. if (x86UNIXState->windowCreated())
  511. {
  512. #ifndef TORQUE_DEDICATED
  513. // process window events
  514. PROFILE_START(XUX_ProcessMessages);
  515. bool quit = !ProcessMessages();
  516. PROFILE_END();
  517. if(quit)
  518. {
  519. // generate a quit event
  520. Event quitEvent;
  521. quitEvent.type = QuitEventType;
  522. Game->postEvent(quitEvent);
  523. }
  524. // process input events
  525. PROFILE_START(XUX_InputProcess);
  526. Input::process();
  527. PROFILE_END();
  528. // poll redbook state
  529. PROFILE_START(XUX_PollRedbookDevices);
  530. PollRedbookDevices();
  531. PROFILE_END();
  532. // if we're not the foreground window, sleep for 1 ms
  533. if (!x86UNIXState->windowActive())
  534. Sleep(0, getBackgroundSleepTime() * 1000000);
  535. #endif
  536. }
  537. else
  538. {
  539. // no window
  540. // if we're not in journal mode, sleep for 1 ms
  541. // JMQ: since linux's minimum sleep latency seems to be 20ms, this can
  542. // increase player pings by 10-20ms in the dedicated server. So
  543. // you have to use -dsleep to enable it. the server sleeps anyway when
  544. // there are no players connected.
  545. // JMQ: recent kernels (such as RH 8.0 2.4.18) reduce the latency
  546. // to 2-4 ms on average.
  547. if (!Game->isJournalReading() && (x86UNIXState->getDSleep() ||
  548. Con::getIntVariable("Server::PlayerCount") -
  549. Con::getIntVariable("Server::BotCount") <= 0))
  550. {
  551. PROFILE_START(XUX_Sleep);
  552. Sleep(0, getBackgroundSleepTime() * 1000000);
  553. PROFILE_END();
  554. }
  555. }
  556. #ifndef TORQUE_DEDICATED
  557. #if 0
  558. // JMQ: disabled this because it may fire mistakenly in some configurations.
  559. // sdl's default event handling scheme should be enough.
  560. // crude check to make sure that we're not loading up events. the sdl
  561. // event queue should never have more than (say) 25 events in it at this
  562. // point
  563. const int MaxEvents = 25;
  564. if (NumEventsPending() > MaxEvents)
  565. {
  566. PrintSDLEventQueue();
  567. AssertFatal(false, "The SDL event queue has too many events!");
  568. }
  569. #endif
  570. #endif
  571. PROFILE_END();
  572. }
  573. // extern U32 calculateCRC(void * buffer, S32 len, U32 crcVal );
  574. // #if defined(DEBUG) || defined(INTERNAL_RELEASE)
  575. // static U32 stubCRC = 0;
  576. // #else
  577. // static U32 stubCRC = 0xEA63F56C;
  578. // #endif
  579. //------------------------------------------------------------------------------
  580. const Point2I &Platform::getWindowSize()
  581. {
  582. return x86UNIXState->getWindowSize();
  583. }
  584. //------------------------------------------------------------------------------
  585. void Platform::setWindowSize( U32 newWidth, U32 newHeight )
  586. {
  587. x86UNIXState->setWindowSize( (S32) newWidth, (S32) newHeight );
  588. }
  589. //------------------------------------------------------------------------------
  590. void Platform::initWindow(const Point2I &initialSize, const char *name)
  591. {
  592. #ifndef TORQUE_DEDICATED
  593. // initialize window
  594. InitWindow(initialSize, name);
  595. if (!InitOpenGL())
  596. ImmediateShutdown(1);
  597. #endif
  598. }
  599. //------------------------------------------------------------------------------
  600. // Web browser function:
  601. //------------------------------------------------------------------------------
  602. bool Platform::openWebBrowser( const char* webAddress )
  603. {
  604. if (!webAddress || dStrlen(webAddress)==0)
  605. return false;
  606. // look for a browser preference variable
  607. // JMQTODO: be nice to implement some UI to customize this
  608. const char* webBrowser = Con::getVariable("Pref::Unix::WebBrowser");
  609. if (dStrlen(webBrowser) == 0)
  610. webBrowser = NULL;
  611. pid_t pid = fork();
  612. if (pid == -1)
  613. {
  614. Con::printf("WARNING: Platform::openWebBrowser failed to fork");
  615. return false;
  616. }
  617. else if (pid != 0)
  618. {
  619. // parent
  620. if (Video::isFullScreen())
  621. Video::toggleFullScreen();
  622. return true;
  623. }
  624. else if (pid == 0)
  625. {
  626. // child
  627. // try to exec konqueror, then netscape
  628. char* argv[3];
  629. argv[0] = "";
  630. argv[1] = const_cast<char*>(webAddress);
  631. argv[2] = 0;
  632. int ok = -1;
  633. // if execvp returns, it means it couldn't execute the program
  634. if (webBrowser != NULL)
  635. ok = execvp(webBrowser, argv);
  636. ok = execvp("konqueror", argv);
  637. ok = execvp("mozilla", argv);
  638. ok = execvp("netscape", argv);
  639. // use dPrintf instead of Con here since we're now in another process,
  640. dPrintf("WARNING: Platform::openWebBrowser: couldn't launch a web browser\n");
  641. _exit(-1);
  642. return false;
  643. }
  644. else
  645. {
  646. Con::printf("WARNING: Platform::openWebBrowser: forking problem");
  647. return false;
  648. }
  649. }
  650. //------------------------------------------------------------------------------
  651. // Login password routines:
  652. //------------------------------------------------------------------------------
  653. const char* Platform::getLoginPassword()
  654. {
  655. Con::printf("WARNING: Platform::getLoginPassword() is unimplemented");
  656. return "";
  657. }
  658. //------------------------------------------------------------------------------
  659. bool Platform::setLoginPassword( const char* password )
  660. {
  661. Con::printf("WARNING: Platform::setLoginPassword is unimplemented");
  662. return false;
  663. }
  664. //-------------------------------------------------------------------------------
  665. void TimeManager::process()
  666. {
  667. U32 curTime = Platform::getRealMilliseconds();
  668. TimeEvent event;
  669. event.elapsedTime = curTime - lastTimeTick;
  670. if(event.elapsedTime > sgTimeManagerProcessInterval)
  671. {
  672. lastTimeTick = curTime;
  673. Game->postEvent(event);
  674. }
  675. }
  676. //------------------------------------------------------------------------------
  677. ConsoleFunction( getDesktopResolution, const char*, 1, 1,
  678. "getDesktopResolution()" )
  679. {
  680. if (!x86UNIXState->windowCreated())
  681. return "0 0 0";
  682. char buffer[256];
  683. char* returnString = Con::getReturnBuffer( dStrlen( buffer ) + 1 );
  684. dSprintf( buffer, sizeof( buffer ), "%d %d %d",
  685. x86UNIXState->getDesktopSize().x,
  686. x86UNIXState->getDesktopSize().y,
  687. x86UNIXState->getDesktopBpp() );
  688. dStrcpy( returnString, buffer );
  689. return( returnString );
  690. }
  691. //------------------------------------------------------------------------------
  692. // Silly Korean registry key checker:
  693. //------------------------------------------------------------------------------
  694. ConsoleFunction( isKoreanBuild, bool, 1, 1, "isKoreanBuild()" )
  695. {
  696. Con::printf("WARNING: isKoreanBuild() is unimplemented");
  697. return false;
  698. }
  699. //------------------------------------------------------------------------------
  700. int main(S32 argc, const char **argv)
  701. {
  702. // init platform state
  703. x86UNIXState = new x86UNIXPlatformState;
  704. // parse the command line for unix-specific params
  705. Vector<char *> newCommandLine;
  706. S32 returnVal = ParseCommandLine(argc, argv, newCommandLine);
  707. if (returnVal != 0)
  708. return returnVal;
  709. // init lastTimeTick for TimeManager::process()
  710. lastTimeTick = Platform::getRealMilliseconds();
  711. // init process control stuff
  712. ProcessControlInit();
  713. // check to see if X is running
  714. DetectWindowingSystem();
  715. // run the game
  716. returnVal = Game->main(newCommandLine.size(),
  717. const_cast<const char**>(newCommandLine.address()));
  718. // dispose of command line
  719. for(U32 i = 0; i < newCommandLine.size(); i++)
  720. delete [] newCommandLine[i];
  721. // dispose of state
  722. delete x86UNIXState;
  723. return returnVal;
  724. }
  725. void Platform::setWindowTitle( const char* title )
  726. {
  727. #ifndef TORQUE_DEDICATED
  728. x86UNIXState->setWindowName(title);
  729. SDL_WM_SetCaption(x86UNIXState->getWindowName(), NULL);
  730. #endif
  731. }
  732. Resolution Video::getDesktopResolution()
  733. {
  734. Resolution Result;
  735. Result.h = x86UNIXState->getDesktopSize().x;
  736. Result.w = x86UNIXState->getDesktopSize().y;
  737. Result.bpp = x86UNIXState->getDesktopBpp();
  738. return Result;
  739. }
  740. //-----------------------------------------------------------------------------
  741. void Platform::restartInstance()
  742. {
  743. if (Game->isRunning() )
  744. {
  745. //Con::errorf( "Error restarting Instance. Game is Still running!");
  746. return;
  747. }
  748. char cmd[2048];
  749. sprintf(cmd, "%s &", x86UNIXState->getExePathName());
  750. system(cmd);
  751. exit(0);
  752. }