x86UNIXState.h 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2013 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 "math/mPoint.h"
  23. #include "platformX86UNIX/platformX86UNIX.h"
  24. #include "platformX86UNIX/platformGL.h"
  25. #ifndef DEDICATED
  26. #include <X11/Xlib.h> // for Display, Window and other X mojo
  27. #else
  28. #define Display int
  29. #define Window int
  30. #define Screen int
  31. #endif
  32. #include <libgen.h> // for basename
  33. typedef void (*LockFunc_t)(void);
  34. class DisplayPtrManager;
  35. class x86UNIXPlatformState
  36. {
  37. friend class DisplayPtrManager;
  38. private:
  39. Point2I mDesktopSize;
  40. Point2I mWindowSize;
  41. S32 mDesktopBpp;
  42. Display *mDisplay;
  43. Window mCurrentWindow;
  44. Screen *mScreenPointer;
  45. int mScreenNumber;
  46. char mWindowName[40];
  47. char mExePathName[4096];
  48. char mExeName[40];
  49. bool mWindowCreated;
  50. bool mWindowActive;
  51. bool mWindowLocked;
  52. bool mXWindowsRunning;
  53. bool mDedicated;
  54. bool mDSleep;
  55. bool mUseRedirect;
  56. // Access to the display* needs to be controlled because the SDL event
  57. // loop runs in a separate thread. If you need the display pointer,
  58. // use the DisplayPtrManager class. See the clipboard functions in
  59. // x86unixinput.cc for an example.
  60. Display *getDisplayPointer() { return mDisplay; }
  61. public:
  62. U32 currentTime;
  63. void setDisplayPointer( Display *displayPointer )
  64. { mDisplay = displayPointer; }
  65. void setScreenNumber( int newNumber ) { mScreenNumber = newNumber; }
  66. int getScreenNumber() { return mScreenNumber; }
  67. void setScreenPointer( Screen *newScreenPointer )
  68. { mScreenPointer = newScreenPointer; }
  69. Screen * getScreenPointer() { return mScreenPointer; }
  70. // for compatibility, convert 24 bpp to 32
  71. void setDesktopBpp( S32 bpp )
  72. {
  73. if (bpp == 24)
  74. mDesktopBpp = 32;
  75. else
  76. mDesktopBpp = bpp;
  77. }
  78. S32 getDesktopBpp() { return mDesktopBpp; }
  79. void setDesktopSize( S32 horizontal, S32 vertical )
  80. { mDesktopSize.set( horizontal, vertical ); }
  81. Point2I getDesktopSize() { return mDesktopSize; }
  82. void setWindow( Window newWindow ) { mCurrentWindow = newWindow; }
  83. Window getWindow() { return mCurrentWindow; }
  84. void setWindowSize (S32 horizontal, S32 vertical )
  85. { mWindowSize.set ( horizontal, vertical ); }
  86. void setWindowSize( Point2I size ) { mWindowSize = size; }
  87. Point2I& getWindowSize() { return ( mWindowSize ); }
  88. void setWindowName (const char * windowName)
  89. {
  90. if (windowName == NULL)
  91. dStrncpy( mWindowName, "", sizeof( mWindowName ));
  92. else
  93. dStrncpy( mWindowName, windowName, sizeof( mWindowName ) );
  94. }
  95. const char * getWindowName() { return mWindowName; }
  96. void setExePathName(const char* exePathName)
  97. {
  98. if (exePathName == NULL)
  99. dStrncpy(mExePathName, "", sizeof(mExePathName));
  100. else
  101. dStrncpy(mExePathName, exePathName, sizeof(mExePathName));
  102. // set the base exe name field
  103. char tempBuf[2048];
  104. dStrncpy(tempBuf, mExePathName, 2048);
  105. dStrncpy(mExeName, basename(tempBuf), sizeof(mExeName));
  106. }
  107. const char * getExePathName() { return mExePathName; }
  108. const char * getExeName() { return mExeName; }
  109. bool windowCreated() { return mWindowCreated; }
  110. bool windowActive() { return mWindowActive; }
  111. bool windowLocked() { return mWindowLocked; }
  112. void setWindowCreated(bool windowCreated)
  113. { mWindowCreated = windowCreated; }
  114. void setWindowActive(bool windowActive)
  115. { mWindowActive = windowActive; }
  116. void setWindowLocked(bool windowLocked)
  117. { mWindowLocked = windowLocked; }
  118. bool isXWindowsRunning() { return mXWindowsRunning; }
  119. void setXWindowsRunning(bool running) { mXWindowsRunning = running; }
  120. bool isDedicated() { return mDedicated; }
  121. void setDedicated(bool dedicated) { mDedicated = dedicated; }
  122. bool getDSleep() { return mDSleep; }
  123. void setDSleep(bool enabled) { mDSleep = enabled; }
  124. bool getUseRedirect() { return mUseRedirect; }
  125. void setUseRedirect(bool enabled) { mUseRedirect = enabled; }
  126. x86UNIXPlatformState()
  127. {
  128. currentTime = 0;
  129. mDesktopBpp = 16;
  130. mDesktopSize.set( 0, 0 );
  131. mWindowSize.set( 800, 600 );
  132. setWindowName("Torque");
  133. setExePathName(NULL);
  134. mWindowCreated = mWindowActive = mWindowLocked = false;
  135. mXWindowsRunning = false;
  136. mDedicated = false;
  137. mDSleep = false;
  138. #ifdef USE_FILE_REDIRECT
  139. mUseRedirect = true;
  140. #else
  141. mUseRedirect = false;
  142. #endif
  143. }
  144. };
  145. extern x86UNIXPlatformState * x86UNIXState;
  146. class DisplayPtrManager
  147. {
  148. // static interface
  149. private:
  150. static bool sgDisplayLocked;
  151. static LockFunc_t sgLockFunc;
  152. static LockFunc_t sgUnlockFunc;
  153. static bool lockDisplay()
  154. {
  155. if (!sgDisplayLocked && sgLockFunc)
  156. {
  157. sgLockFunc();
  158. sgDisplayLocked = true;
  159. return true;
  160. }
  161. else
  162. return false;
  163. }
  164. static void unlockDisplay()
  165. {
  166. if (sgDisplayLocked && sgUnlockFunc)
  167. {
  168. sgUnlockFunc();
  169. sgDisplayLocked = false;
  170. }
  171. }
  172. //friend Display* x86UNIXPlatformState::getDisplayPointer();
  173. public:
  174. static void setDisplayLockFunction(LockFunc_t lockFunc)
  175. { sgLockFunc = lockFunc; }
  176. static void setDisplayUnlockFunction(LockFunc_t unlockFunc)
  177. { sgUnlockFunc = unlockFunc; }
  178. // nonstatic interface
  179. private:
  180. bool mAcquiredLock; // true if this instance acquired the display lock
  181. // (multiple instances of DisplayPtrManager can coexist, but only
  182. // the first to access the display pointer will be responsible for
  183. // acquiring and releasing the lock)
  184. bool mOpenedDisplay; // true if this instance created a display pointer
  185. // because the one in platform state was null.
  186. Display* mDisplay;
  187. private:
  188. Display* openDisplay()
  189. {
  190. #ifndef DEDICATED
  191. mDisplay = XOpenDisplay(NULL);
  192. if (mDisplay != NULL)
  193. mOpenedDisplay = true;
  194. #endif
  195. return mDisplay;
  196. }
  197. void closeDisplay()
  198. {
  199. if (mOpenedDisplay)
  200. {
  201. #ifndef DEDICATED
  202. XCloseDisplay(mDisplay);
  203. mDisplay = NULL;
  204. mOpenedDisplay = false;
  205. #endif
  206. }
  207. }
  208. public:
  209. DisplayPtrManager()
  210. {
  211. mAcquiredLock = false;
  212. mOpenedDisplay = false;
  213. mDisplay = NULL;
  214. }
  215. ~DisplayPtrManager()
  216. {
  217. if (mAcquiredLock)
  218. {
  219. DisplayPtrManager::unlockDisplay();
  220. mAcquiredLock = false;
  221. }
  222. closeDisplay();
  223. }
  224. Display* getDisplayPointer()
  225. {
  226. Display* display = x86UNIXState->getDisplayPointer();
  227. if (display == NULL)
  228. return openDisplay();
  229. mAcquiredLock = DisplayPtrManager::lockDisplay();
  230. return display;
  231. }
  232. };