POSIXState.h 8.5 KB

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