x86UNIXState.h 8.9 KB

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