Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Examples  

gadget::EventWindowXWin Class Reference

X Winndow System event window class. More...

#include <EventWindowXWin.h>

Inheritance diagram for gadget::EventWindowXWin:

Inheritance graph
[legend]
Collaboration diagram for gadget::EventWindowXWin:

Collaboration graph
[legend]
List of all members.

Public Types

enum  lockState { Unlocked, Lock_LockKey, Lock_KeyDown }
 Enum to keep track of current lock state for state machine. More...


Public Methods

 EventWindowXWin ()
 ~EventWindowXWin ()
virtual bool config (jccl::ConfigElementPtr e)
 Constructor. More...

void controlLoop (void *nullParam)
 Main thread of control for this active object. More...

bool sample ()
 Processes the current x-events. More...

void updateData ()
 Update the data. More...

int isKeyPressed (int Key)
 Returns the number of times the key was pressed during the last frame. More...

void operator delete (void *p)
 Invokes the global scope delete operator. More...

Pure Virtuals required by Input
bool startSampling ()
 Start a device sampling. More...

bool stopSampling ()
 StopSampling. More...


Static Public Methods

std::string getElementType ()
 Returns the string rep of the element type used to config this device. More...


Protected Methods

void destroy ()
 Deletes this object. More...

virtual void processEvent (XEvent event)
 Do any extra event processing needed. More...

void createEmptyCursor (Display *display, Window root)

Protected Attributes

bool mWeOwnTheWindow
 True if this class owns the window (is reposible for opening, closing, and event processing). More...

::Window mWindow
::XVisualInfo * mVisual
::Display * mDisplay
::XSetWindowAttributes mSWA
int mScreen
int mX
int mY
 screen id, x-origin, y-origin. More...

unsigned int mWidth
unsigned int mHeight
Cursor mEmptyCursor
bool mEmptyCursorSet
std::string mXDisplayString
 The display string to use from systemDisplay config info. More...

float mMouseSensitivity
int mSleepTimeMS
 Amount of time to sleep in milliseconds between updates. More...

int mPrevX
int mPrevY
 Previous mouse location. More...

EventWindow state holders
Note:
This driver does not use the normal triple buffering mechanism. Instead, it just uses a modified double buffering system.


int mKeys [gadget::LAST_KEY]
 (0,*): The num key presses during an UpdateData (ie. More...

int mRealkeys [gadget::LAST_KEY]
 (0,1): The real keyboard state, all events processed (ie. More...

vpr::Mutex mKeysLock
 Must hold this lock when accessing m_keys. More...

bool mExitFlag
 Should we exit? More...

lockState mLockState
 The current state of locking. More...

int mLockStoredKey
 The key that was pressed down. More...

int mLockToggleKey
 The key that toggles the locking. More...


Detailed Description

X Winndow System event window class.

This device is a source of keyboard events. The device should not be used directly, it should be referenced through proxies.

Mouse Locking:
This device recieves input from the XWindows display. As such, the xwindow must have focus to generate events. In order to help users keep the window in focus, there are two cases where the driver will "lock" the mouse to the window, thus preventing loss of focus.
CASE 1: The user holds down any key. (ie. a,b, ctrl, shift, etc)
CASE 2: The user can toggle locking using a special "locking" key defined in the configuration element.

See also:
EventWindow, EventWindowProxy

Definition at line 72 of file EventWindowXWin.h.


Member Enumeration Documentation

enum gadget::EventWindowXWin::lockState
 

Enum to keep track of current lock state for state machine.

Enumeration values:
Unlocked  The mouse is free.
Lock_LockKey  The mouse is locked due to lock toggle key press.
Lock_KeyDown  The mouse is locked due to a key being held down.

Definition at line 76 of file EventWindowXWin.h.

00077    {
00078       Unlocked,     
00079       Lock_LockKey, 
00080       Lock_KeyDown  
00081    };


Constructor & Destructor Documentation

gadget::EventWindowXWin::EventWindowXWin   [inline]
 

Definition at line 83 of file EventWindowXWin.h.

References mDisplay, mEmptyCursorSet, mExitFlag, mLockState, mPrevX, mPrevY, mVisual, mWeOwnTheWindow, and Unlocked.

00084       : mWeOwnTheWindow(true), mVisual(NULL), mDisplay(NULL),
00085         mEmptyCursorSet(false), mExitFlag(false),
00086         mLockState(Unlocked), mPrevX(0), mPrevY(0)
00087    {
00088       //mThread = NULL; -- Should be done in base constructor
00089    }

gadget::EventWindowXWin::~EventWindowXWin   [inline]
 

Definition at line 91 of file EventWindowXWin.h.

References stopSampling.

00092    {
00093       stopSampling();
00094    }


Member Function Documentation

bool gadget::EventWindowXWin::config jccl::ConfigElementPtr    e [virtual]
 

Constructor.

Reimplemented from gadget::EventWindow.

Definition at line 59 of file EventWindowXWin.cpp.

References gadgetDBG_INPUT_MGR, Lock_LockKey, gadget::EventWindow::mCurKeys, mHeight, mKeys, mLockState, mLockToggleKey, mMouseSensitivity, mRealkeys, mSleepTimeMS, mWeOwnTheWindow, mWidth, mX, mXDisplayString, and mY.

00060 {
00061    if ( ! (Input::config(e) && EventWindow::config(e)) )
00062    {
00063       return false;
00064    }
00065 
00066    const char neg_one_STRING[] = "-1";
00067 
00068    for ( int i = 0; i < gadget::LAST_KEY; ++i )
00069    {
00070       mRealkeys[i] = mKeys[i] = mCurKeys[i];
00071    }
00072 
00073    // Get size and position
00074    mWidth  = e->getProperty<int>("width");
00075    mHeight = e->getProperty<int>("height");
00076 
00077    // Sanity checks.
00078    if (mWidth == 0) mWidth = 400;
00079    if (mHeight == 0) mHeight = 400;
00080 
00081    mX = e->getProperty<int>("origin", 0);
00082    mY = e->getProperty<int>("origin", 1);
00083 
00084    // Get the X display string
00085    int x_disp_num = e->getProperty<int>("display_number");
00086    jccl::ConfigElementPtr disp_sys_elt =
00087       gadget::InputManager::instance()->getDisplaySystemElement();
00088 
00089    if ((x_disp_num >= 0) && (disp_sys_elt.get() != NULL) )
00090    {
00091       mXDisplayString = disp_sys_elt->getProperty<std::string>("x11_pipes",
00092                                                                x_disp_num);
00093    }
00094    else
00095    {
00096       mXDisplayString = std::string("-1");
00097    }
00098 
00099    if ((mXDisplayString.empty()) || (strcmp(mXDisplayString.c_str(), neg_one_STRING) == 0))    // Use display env
00100    {
00101       const std::string DISPLAY_str("DISPLAY");
00102       vpr::System::getenv(DISPLAY_str, mXDisplayString);
00103    }
00104 
00105    // Get the lock information
00106    mLockToggleKey = e->getProperty<int>("lock_key");
00107    bool start_locked = e->getProperty<bool>("start_locked");
00108 
00109    if (start_locked)
00110    {
00111       mLockState = Lock_LockKey;      // Initialize to the locked state
00112    }
00113 
00114    mMouseSensitivity = e->getProperty<float>("mouse_sensitivity");
00115    if (0.0f == mMouseSensitivity)
00116    {
00117       mMouseSensitivity = 0.5;
00118    }
00119 
00120    vprDEBUG(gadgetDBG_INPUT_MGR, vprDBG_STATE_LVL)
00121       << "Mouse Sensititivty: " << mMouseSensitivity << std::endl
00122       << vprDEBUG_FLUSH;
00123 
00124    mSleepTimeMS = e->getProperty<int>("sleep_time");
00125 
00126    // Sanity check.
00127    if (mSleepTimeMS == 0)
00128    {
00129       mSleepTimeMS = 50;
00130    }
00131 
00132    // Default to owning the window
00133    mWeOwnTheWindow = true;
00134 
00135    return true;
00136 }

void gadget::EventWindowXWin::controlLoop void *    nullParam
 

Main thread of control for this active object.

Definition at line 139 of file EventWindowXWin.cpp.

References gadgetDBG_INPUT_MGR, Lock_LockKey, mDisplay, mEmptyCursor, mExitFlag, mLockState, mSleepTimeMS, gadget::Input::mThread, mWeOwnTheWindow, mWindow, and sample.

00140 {
00141    boost::ignore_unused_variable_warning(nullParam);
00142 
00143    vprDEBUG(gadgetDBG_INPUT_MGR,vprDBG_STATE_LVL)
00144       << "gadget::EventWindowXWin::controlLoop: Thread started.\n"
00145       << vprDEBUG_FLUSH;
00146 
00147    while (NULL == vpr::Thread::self())
00148    {
00149       vpr::System::usleep(50);
00150       vprDEBUG(vprDBG_ALL,vprDBG_VERB_LVL)
00151          << "gadget::EventWindowXWin: Waiting for (thread::self() != NULL)\n"
00152          << vprDEBUG_FLUSH;
00153    }
00154    mThread = (vpr::Thread*) vpr::Thread::self();
00155 
00156    vprASSERT(mWeOwnTheWindow && "control loop should not be called if we don't own window. Update in owning thread.");
00157 
00158    // Open the x-window
00159    openTheWindow();
00160 
00161    // Sync up with window
00162    XSync(mDisplay, 0);
00163 
00164    // If we have initial locked, then we need to lock the system
00165    if(mLockState == Lock_LockKey)      // Means that we are in the initially locked state
00166    {
00167       vprDEBUG(gadgetDBG_INPUT_MGR,vprDBG_STATE_LVL)
00168          << "gadget::EventWindowXWin::controlLoop: Mouse set to initial lock. Locking it now.\n"
00169          << vprDEBUG_FLUSH;
00170       lockMouse();                     // Lock the mouse
00171    }
00172 
00173    // Loop on updating
00174    while(!mExitFlag)
00175    {
00176       sample();
00177       long usleep_time(1); // to be set...
00178 
00179       usleep_time = mSleepTimeMS*1000;
00180 
00181       vpr::System::usleep(usleep_time);
00182    }
00183 
00184    XResizeWindow(mDisplay, mWindow, 1,1); //Dummy event
00185    XFlush(mDisplay);
00186 
00187    if ( mEmptyCursorSet )
00188    {
00189       XFreeCursor(mDisplay, mEmptyCursor);
00190    }
00191 
00192    // Exit, cleanup code
00193    XDestroyWindow(mDisplay, mWindow);
00194    XCloseDisplay((::Display*) mDisplay);
00195 
00196 }

bool gadget::EventWindowXWin::startSampling   [virtual]
 

Start a device sampling.

Start the device sampling, normally this will spawn a thread which will just repeatedly call Sample(). This function should return true when it sucessfully starts, false otherwise.

Implements gadget::Input.

Definition at line 199 of file EventWindowXWin.cpp.

References mExitFlag, and gadget::Input::mThread.

00200 {
00201    if(mThread != NULL)
00202    {
00203       vprDEBUG(vprDBG_ERROR,vprDBG_CRITICAL_LVL)
00204          << clrOutNORM(clrRED,"ERROR:")
00205          << "gadget::EventWindowXWin: startSampling called, when already sampling.\n"
00206          << vprDEBUG_FLUSH;
00207       vprASSERT(false);
00208    }
00209    
00210    mExitFlag =false;
00211    // Create a new thread to handle the control
00212    vpr::ThreadMemberFunctor<EventWindowXWin>* memberFunctor =
00213       new vpr::ThreadMemberFunctor<EventWindowXWin>(this, &EventWindowXWin::controlLoop, NULL);
00214 
00215    mThread = new vpr::Thread(memberFunctor);
00216 
00217    // return success value...
00218    if ( ! mThread->valid() )
00219    {
00220       return 0; // fail
00221    }
00222    else
00223    {
00224       return 1; // success
00225    }
00226 }

bool gadget::EventWindowXWin::stopSampling   [virtual]
 

StopSampling.

Reverse the effects of StartSampling().

Implements gadget::Input.

Definition at line 680 of file EventWindowXWin.cpp.

References mExitFlag, gadget::Input::mThread, and mWeOwnTheWindow.

Referenced by ~EventWindowXWin.

00681 {
00682    // If there is a thread for us and we actually own the window
00683    if ((mThread != NULL) && mWeOwnTheWindow)
00684    {
00685       mExitFlag = true;
00686 
00687       mThread->join();
00688 
00689       delete mThread;
00690       mThread = NULL;
00691    }
00692 
00693    return 1;
00694 }

bool gadget::EventWindowXWin::sample   [inline, virtual]
 

Processes the current x-events.

Called repetatively by the controlLoop.

Implements gadget::Input.

Definition at line 111 of file EventWindowXWin.h.

Referenced by controlLoop.

00112    {
00113       HandleEvents();
00114       return 1;
00115    }

void gadget::EventWindowXWin::updateData   [virtual]
 

Update the data.

After this function is called subsequent calls to GetData(d) will return the most recent data at the time of THIS function call. Data is guaranteed to be valid and static until the next call to UpdateData.

Implements gadget::Input.

Definition at line 246 of file EventWindowXWin.cpp.

References gadget::EventWindow::mCurKeys, mKeys, mKeysLock, mMouseSensitivity, mRealkeys, and gadget::EventWindow::updateEventQueue.

00247 {
00248 vpr::Guard<vpr::Mutex> guard(mKeysLock);      // Lock access to the mKeys array
00249 
00250 // Scale mouse values based on sensitivity
00251    mKeys[gadget::MOUSE_POSX] = int(float(mKeys[gadget::MOUSE_POSX]) * mMouseSensitivity);
00252    mKeys[gadget::MOUSE_NEGX] = int(float(mKeys[gadget::MOUSE_NEGX]) * mMouseSensitivity);
00253    mKeys[gadget::MOUSE_POSY] = int(float(mKeys[gadget::MOUSE_POSY]) * mMouseSensitivity);
00254    mKeys[gadget::MOUSE_NEGY] = int(float(mKeys[gadget::MOUSE_NEGY]) * mMouseSensitivity);
00255 
00256    /*
00257    vprDEBUG(vprDBG_ALL, vprDBG_VERB_LVL)
00258       << "gadget::EventWindowXWin::updateData:" << mInstName << " -- "
00259       << "mouse_keys: px:" << mKeys[gadget::MOUSE_POSX]
00260       << " nx:" << mKeys[gadget::MOUSE_NEGX]
00261       << " py:" << mKeys[gadget::MOUSE_POSY]
00262       << " ny:" << mKeys[gadget::MOUSE_NEGY]
00263       << std::endl << vprDEBUG_FLUSH;
00264    */
00265 
00266    // Copy over values
00267    for ( unsigned int i = 0; i < gadget::LAST_KEY; ++i )
00268    {
00269       mCurKeys[i] = mKeys[i];
00270    }
00271 
00272    // Re-initialize the mKeys based on current key state in realKeys
00273    // Set the initial state of the mKey key counts based on the current state
00274    // of the system this is to ensure that if a key is still down, we get at
00275    // least one event for it.
00276    for ( unsigned int j = 0; j < gadget::LAST_KEY; ++j )
00277    {
00278       mKeys[j] = mRealkeys[j];
00279    }
00280 
00281    updateEventQueue();
00282 }

std::string gadget::EventWindowXWin::getElementType   [static]
 

Returns the string rep of the element type used to config this device.

This string is used by the device factory to look up device drivers based up the type of element it is trying to load.

Reimplemented from gadget::Input.

Definition at line 53 of file EventWindowXWin.cpp.

00054 {
00055    return "event_window";
00056 }

int gadget::EventWindowXWin::isKeyPressed int    Key [inline]
 

Returns the number of times the key was pressed during the last frame.

You can put this in an if to check if was pressed at all, or if you are doing processing based on this catch the actual number.

Definition at line 126 of file EventWindowXWin.h.

References gadget::EventWindow::mCurKeys.

00127    {
00128       return mCurKeys[Key];
00129    }

void gadget::EventWindowXWin::operator delete void *    p [inline]
 

Invokes the global scope delete operator.

This is required for proper releasing of memory in DLLs on Win32.

Definition at line 135 of file EventWindowXWin.h.

00136    {
00137       ::operator delete(p);
00138    }

void gadget::EventWindowXWin::destroy   [inline, protected, virtual]
 

Deletes this object.

This is an implementation of the pure virtual gadget::Input::destroy() method.

Implements gadget::Input.

Definition at line 145 of file EventWindowXWin.h.

00146    {
00147       delete this;
00148    }

virtual void gadget::EventWindowXWin::processEvent XEvent    event [inline, protected, virtual]
 

Do any extra event processing needed.

Definition at line 151 of file EventWindowXWin.h.

00152    {
00153       boost::ignore_unused_variable_warning(event);
00154    }

void gadget::EventWindowXWin::createEmptyCursor Display *    display,
Window    root
[protected]
 

Definition at line 991 of file EventWindowXWin.cpp.

References mEmptyCursor, and mEmptyCursorSet.

00992 {
00993    Pixmap cursormask;
00994    XGCValues xgc;
00995    GC gc;
00996    XColor dummycolour;
00997 
00998    cursormask = XCreatePixmap(display, root, 1, 1, 1/*depth*/);
00999    xgc.function = GXclear;
01000    gc =  XCreateGC(display, cursormask, GCFunction, &xgc);
01001    XFillRectangle(display, cursormask, gc, 0, 0, 1, 1);
01002    dummycolour.pixel = 0;
01003    dummycolour.red = 0;
01004    dummycolour.flags = 04;
01005    mEmptyCursor = XCreatePixmapCursor(display, cursormask, cursormask,
01006                                       &dummycolour,&dummycolour, 0,0);
01007    XFreePixmap(display,cursormask);
01008    XFreeGC(display,gc);
01009 
01010    mEmptyCursorSet = true;
01011 }


Member Data Documentation

bool gadget::EventWindowXWin::mWeOwnTheWindow [protected]
 

True if this class owns the window (is reposible for opening, closing, and event processing).

Definition at line 244 of file EventWindowXWin.h.

Referenced by config, controlLoop, EventWindowXWin, and stopSampling.

::Window gadget::EventWindowXWin::mWindow [protected]
 

Definition at line 246 of file EventWindowXWin.h.

Referenced by controlLoop.

::XVisualInfo* gadget::EventWindowXWin::mVisual [protected]
 

Definition at line 247 of file EventWindowXWin.h.

Referenced by EventWindowXWin.

::Display* gadget::EventWindowXWin::mDisplay [protected]
 

Definition at line 248 of file EventWindowXWin.h.

Referenced by controlLoop, and EventWindowXWin.

::XSetWindowAttributes gadget::EventWindowXWin::mSWA [protected]
 

Definition at line 249 of file EventWindowXWin.h.

int gadget::EventWindowXWin::mScreen [protected]
 

Definition at line 250 of file EventWindowXWin.h.

int gadget::EventWindowXWin::mX [protected]
 

Definition at line 250 of file EventWindowXWin.h.

Referenced by config.

int gadget::EventWindowXWin::mY [protected]
 

screen id, x-origin, y-origin.

Definition at line 250 of file EventWindowXWin.h.

Referenced by config.

unsigned int gadget::EventWindowXWin::mWidth [protected]
 

Definition at line 251 of file EventWindowXWin.h.

Referenced by config.

unsigned int gadget::EventWindowXWin::mHeight [protected]
 

Definition at line 251 of file EventWindowXWin.h.

Referenced by config.

Cursor gadget::EventWindowXWin::mEmptyCursor [protected]
 

Definition at line 253 of file EventWindowXWin.h.

Referenced by controlLoop, and createEmptyCursor.

bool gadget::EventWindowXWin::mEmptyCursorSet [protected]
 

Definition at line 254 of file EventWindowXWin.h.

Referenced by createEmptyCursor, and EventWindowXWin.

int gadget::EventWindowXWin::mKeys[gadget::LAST_KEY] [protected]
 

(0,*): The num key presses during an UpdateData (ie.

How many keypress events).

Definition at line 261 of file EventWindowXWin.h.

Referenced by config, and updateData.

int gadget::EventWindowXWin::mRealkeys[gadget::LAST_KEY] [protected]
 

(0,1): The real keyboard state, all events processed (ie.

what is the key now).

Definition at line 263 of file EventWindowXWin.h.

Referenced by config, and updateData.

vpr::Mutex gadget::EventWindowXWin::mKeysLock [protected]
 

Must hold this lock when accessing m_keys.

Definition at line 264 of file EventWindowXWin.h.

Referenced by updateData.

bool gadget::EventWindowXWin::mExitFlag [protected]
 

Should we exit?

Definition at line 265 of file EventWindowXWin.h.

Referenced by controlLoop, EventWindowXWin, startSampling, and stopSampling.

lockState gadget::EventWindowXWin::mLockState [protected]
 

The current state of locking.

Definition at line 267 of file EventWindowXWin.h.

Referenced by config, controlLoop, and EventWindowXWin.

int gadget::EventWindowXWin::mLockStoredKey [protected]
 

The key that was pressed down.

Definition at line 268 of file EventWindowXWin.h.

int gadget::EventWindowXWin::mLockToggleKey [protected]
 

The key that toggles the locking.

Definition at line 269 of file EventWindowXWin.h.

Referenced by config.

std::string gadget::EventWindowXWin::mXDisplayString [protected]
 

The display string to use from systemDisplay config info.

Definition at line 272 of file EventWindowXWin.h.

Referenced by config.

float gadget::EventWindowXWin::mMouseSensitivity [protected]
 

Definition at line 274 of file EventWindowXWin.h.

Referenced by config, and updateData.

int gadget::EventWindowXWin::mSleepTimeMS [protected]
 

Amount of time to sleep in milliseconds between updates.

Definition at line 275 of file EventWindowXWin.h.

Referenced by config, and controlLoop.

int gadget::EventWindowXWin::mPrevX [protected]
 

Definition at line 276 of file EventWindowXWin.h.

Referenced by EventWindowXWin.

int gadget::EventWindowXWin::mPrevY [protected]
 

Previous mouse location.

Definition at line 276 of file EventWindowXWin.h.

Referenced by EventWindowXWin.


The documentation for this class was generated from the following files:
Generated on Sun May 2 14:26:48 2004 for Gadgeteer by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002