x86UNIXState.h 8.7 KB

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