x86UNIXState.h

Engine/source/platformX86UNIX/x86UNIXState.h

More...

Classes:

Public Typedefs

void(*
LockFunc_t )(void)

Detailed Description

Public Typedefs

typedef void(* LockFunc_t )(void)

Public Variables

x86UNIXPlatformState * x86UNIXState 
  1
  2//-----------------------------------------------------------------------------
  3// Copyright (c) 2012 GarageGames, LLC
  4//
  5// Permission is hereby granted, free of charge, to any person obtaining a copy
  6// of this software and associated documentation files (the "Software"), to
  7// deal in the Software without restriction, including without limitation the
  8// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  9// sell copies of the Software, and to permit persons to whom the Software is
 10// furnished to do so, subject to the following conditions:
 11//
 12// The above copyright notice and this permission notice shall be included in
 13// all copies or substantial portions of the Software.
 14//
 15// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 16// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 17// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 18// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 19// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 20// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 21// IN THE SOFTWARE.
 22//-----------------------------------------------------------------------------
 23
 24#include "math/mPoint2.h"
 25#include "platformX86UNIX/platformX86UNIX.h"
 26//#include "platformX86UNIX/platformGL.h"
 27#include "core/strings/stringFunctions.h"
 28
 29#ifndef TORQUE_DEDICATED
 30#include <X11/Xlib.h> // for Display, Window and other X mojo
 31#else
 32#define Display int
 33#define Window int
 34#define Screen int
 35#endif
 36
 37#include <libgen.h> // for basename
 38
 39typedef void (*LockFunc_t)(void);
 40
 41class DisplayPtrManager;
 42
 43class x86UNIXPlatformState
 44{
 45      friend class DisplayPtrManager;
 46
 47   private:
 48      Point2I              mDesktopSize;
 49      Point2I              mWindowSize;
 50      S32                  mDesktopBpp;
 51      Display              *mDisplay;
 52      Window               mCurrentWindow;
 53      Screen               *mScreenPointer;
 54      int                  mScreenNumber;
 55      char                 mWindowName[40];
 56      char                 mExePathName[4096];
 57      char                 mExeName[40];
 58      bool                 mWindowCreated;
 59      bool                 mWindowActive;
 60      bool                 mWindowLocked;
 61      bool                 mXWindowsRunning;
 62      bool                 mDedicated;
 63      bool                 mCDAudioEnabled;
 64      bool                 mDSleep;
 65      bool                 mUseRedirect;
 66
 67      // Access to the display* needs to be controlled because the SDL event
 68      // loop runs in a separate thread.  If you need the display pointer,
 69      // use the DisplayPtrManager class.  See the clipboard functions in 
 70      // x86unixinput.cc for an example.
 71      //Display *getDisplayPointer() { return mDisplay; }
 72
 73   public:
 74      U32      currentTime;
 75
 76      void setDisplayPointer( Display *displayPointer )     { mDisplay = displayPointer; }
 77      Display* getDisplayPointer()                          { return mDisplay; }
 78
 79      void setScreenNumber( int newNumber ) { mScreenNumber = newNumber; }
 80      int getScreenNumber() { return mScreenNumber; }
 81      
 82      void setScreenPointer( Screen *newScreenPointer ) 
 83          { mScreenPointer = newScreenPointer; }
 84      Screen * getScreenPointer() { return mScreenPointer; }
 85
 86      // for compatibility, convert 24 bpp to 32
 87      void setDesktopBpp( S32 bpp ) 
 88      { 
 89         if (bpp == 24) 
 90            mDesktopBpp = 32;
 91         else 
 92            mDesktopBpp = bpp; 
 93      }     
 94      S32 getDesktopBpp() { return mDesktopBpp; }
 95
 96      void setDesktopSize( S32 horizontal, S32 vertical )
 97          { mDesktopSize.set( horizontal, vertical ); }
 98      Point2I getDesktopSize() { return mDesktopSize; }
 99
100      void setWindow( Window newWindow ) { mCurrentWindow = newWindow; }
101      Window getWindow() { return mCurrentWindow; }
102
103      void setWindowSize (S32 horizontal, S32 vertical ) 
104          { mWindowSize.set ( horizontal, vertical ); }
105      void setWindowSize( Point2I size ) { mWindowSize = size; }       
106      Point2I& getWindowSize() { return ( mWindowSize ); }
107 
108      void setWindowName (const char * windowName) 
109      {
110         if (windowName == NULL)
111            dStrncpy( mWindowName, "", sizeof( mWindowName ));
112         else
113            dStrncpy( mWindowName, windowName, sizeof( mWindowName ) );
114      }
115      const char * getWindowName() { return mWindowName; }
116
117      void setExePathName(const char* exePathName)
118      {
119         if (exePathName == NULL)
120            dStrncpy(mExePathName, "", sizeof(mExePathName));
121         else
122            dStrncpy(mExePathName, exePathName, sizeof(mExePathName));
123
124         // set the base exe name field
125         char tempBuf[2048];
126         dStrncpy(tempBuf, mExePathName, 2048);
127         dStrncpy(mExeName, basename(tempBuf), sizeof(mExeName));
128      }
129      const char * getExePathName() { return mExePathName; }
130      const char * getExeName() { return mExeName; }
131
132      bool windowCreated() { return mWindowCreated; }
133      bool windowActive() { return mWindowActive; }
134      bool windowLocked() { return mWindowLocked; }
135      void setWindowCreated(bool windowCreated) 
136          { mWindowCreated = windowCreated; }
137      void setWindowActive(bool windowActive) 
138          { mWindowActive = windowActive; }
139      void setWindowLocked(bool windowLocked) 
140          { mWindowLocked = windowLocked; }
141
142      bool isXWindowsRunning() { return mXWindowsRunning; }
143      void setXWindowsRunning(bool running) { mXWindowsRunning = running; }
144
145      bool isDedicated() { return mDedicated; }
146      void setDedicated(bool dedicated) { mDedicated = dedicated; }
147
148      bool getCDAudioEnabled() { return mCDAudioEnabled; }
149      void setCDAudioEnabled(bool enabled) { mCDAudioEnabled = enabled; }
150
151      bool getDSleep() { return mDSleep; }
152      void setDSleep(bool enabled) { mDSleep = enabled; }
153
154      bool getUseRedirect() { return mUseRedirect; }
155      void setUseRedirect(bool enabled) { mUseRedirect = enabled; }
156      
157      x86UNIXPlatformState()
158      {
159         currentTime = 0;
160         mDesktopBpp = 16;
161         mDesktopSize.set( 0, 0 );
162         mWindowSize.set( 800, 600 );
163         setWindowName("Torque");
164         setExePathName(NULL);
165         mWindowCreated = mWindowActive = mWindowLocked = false;
166         mXWindowsRunning = false;
167         mDedicated = false;
168         mCDAudioEnabled = false;
169         mDSleep = false;
170#ifdef USE_FILE_REDIRECT
171         mUseRedirect = true;
172#else
173         mUseRedirect = false;
174#endif
175      }
176};
177
178extern x86UNIXPlatformState  * x86UNIXState;
179
180class DisplayPtrManager
181{
182      // static interface
183   private:
184      static bool sgDisplayLocked;
185      static LockFunc_t sgLockFunc;
186      static LockFunc_t sgUnlockFunc;
187
188      static bool lockDisplay() 
189      { 
190         if (!sgDisplayLocked && sgLockFunc) 
191         {
192            sgLockFunc(); 
193            sgDisplayLocked = true;
194            return true;
195         }
196         else 
197            return false;
198      }
199      static void unlockDisplay() 
200      {
201         if (sgDisplayLocked && sgUnlockFunc) 
202         {
203            sgUnlockFunc();
204            sgDisplayLocked = false;
205         }
206      }
207
208      //friend Display* x86UNIXPlatformState::getDisplayPointer();
209     
210   public:
211      static void setDisplayLockFunction(LockFunc_t lockFunc) 
212          { sgLockFunc = lockFunc; }
213      static void setDisplayUnlockFunction(LockFunc_t unlockFunc) 
214          { sgUnlockFunc = unlockFunc; }
215
216      // nonstatic interface
217   private:
218      bool mAcquiredLock; // true if this instance acquired the display lock
219      // (multiple instances of DisplayPtrManager can coexist, but only 
220      // the first to access the display pointer will be responsible for 
221      // acquiring and releasing the lock)
222      bool mOpenedDisplay; // true if this instance created a display pointer
223      // because the one in platform state was null.
224      Display* mDisplay;
225
226   private:
227      Display* openDisplay()
228      {
229#ifndef TORQUE_DEDICATED
230         mDisplay = XOpenDisplay(NULL);
231         if (mDisplay != NULL)
232            mOpenedDisplay = true;
233#endif
234         return mDisplay;
235      }
236
237      void closeDisplay()
238      {
239         if (mOpenedDisplay)
240         {
241#ifndef TORQUE_DEDICATED
242            XCloseDisplay(mDisplay);
243            mDisplay = NULL;
244            mOpenedDisplay = false;
245#endif
246         }
247      }
248   public:
249      DisplayPtrManager() 
250      {
251         mAcquiredLock = false;
252         mOpenedDisplay = false;
253         mDisplay = NULL;
254      }
255
256      ~DisplayPtrManager()
257      {
258         if (mAcquiredLock)
259         {
260            DisplayPtrManager::unlockDisplay();
261            mAcquiredLock = false;
262         }
263         closeDisplay();
264      }
265
266      Display* getDisplayPointer()
267      {
268         Display* display = x86UNIXState->getDisplayPointer();
269         if (display == NULL)
270            return openDisplay();
271
272         mAcquiredLock = DisplayPtrManager::lockDisplay();
273         return display;
274      }
275};
276