libGizmod/X11FocusWatcher.hpp

Go to the documentation of this file.
00001 
00012 /*
00013   
00014   Copyright (c) 2007, Tim Burrell
00015   Licensed under the Apache License, Version 2.0 (the "License");
00016   you may not use this file except in compliance with the License.
00017   You may obtain a copy of the License at 
00018 
00019         http://www.apache.org/licenses/LICENSE-2.0
00020 
00021   Unless required by applicable law or agreed to in writing, software
00022   distributed under the License is distributed on an "AS IS" BASIS,
00023   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00024   See the License for the specific language governing permissions and 
00025   limitations under the License. 
00026   
00027 */
00028 
00029 #ifndef __X11FocusWatcher_h
00030 #define __X11FocusWatcher_h
00031 
00032 #if HAVE_CONFIG_H
00033 #include "config.h"
00034 #endif
00035 
00036 #include <X11/Xlib.h>
00037 #include <X11/Xutil.h>
00038 #include <X11/keysym.h>
00039 #include <X11/Xatom.h>
00040 #include <string>
00041 #include <boost/tuple/tuple.hpp>
00042 #include <boost/archive/text_oarchive.hpp>
00043 #include <boost/archive/text_iarchive.hpp>
00044 #include <boost/serialization/base_object.hpp>
00045 
00047 // Namespace
00049 
00050 namespace Gizmod {
00051 
00053 // Typedefs
00055 
00060 typedef enum {
00061         X11FOCUSEVENT_IN,
00062         X11FOCUSEVENT_OUT
00063 } X11FocusEventType;
00064 
00066 // X11FocusEvent Class Definition
00068 
00082 class X11FocusEvent {
00083 public: 
00084         // public member variables
00085         X11FocusEventType               EventType;              
00086         std::string                     WindowName;             
00087         std::string                     WindowNameFormal;       
00088         std::string                     WindowClass;            
00089         
00090         // public functions
00091         bool                            isNull();               
00092 
00093         // operators
00094         bool                            operator != (X11FocusEvent const & Event); 
00095         bool                            operator == (X11FocusEvent const & Event); 
00096 
00097         // construction / deconstruction
00098         X11FocusEvent();
00099         X11FocusEvent(X11FocusEventType eventType, std::string windowName, std::string windowNameFormal, std::string windowClass);
00100         X11FocusEvent(X11FocusEvent const & Event);
00101         virtual ~X11FocusEvent();
00102 
00103 private:
00104         // private functions
00105                 
00106         // private member variables
00107         
00108 private: 
00109         // serialization
00110         friend class boost::serialization::access;
00111         template<class Archive>
00112         void serialize(Archive & ar, const unsigned int version) {
00113                 ar & EventType;
00114                 ar & WindowName;
00115                 ar & WindowNameFormal;
00116                 ar & WindowClass;
00117         }                               
00118 };
00119 
00121 // X11FocusWatcher Class Definition
00123 
00128 class X11FocusWatcher {
00129 public: 
00130         // public functions
00131         void                            init();                 
00132         bool                            isApplicationRunning(std::string WindowTitle); 
00133         virtual void                    onFocusIn(X11FocusEvent const & Event); 
00134         virtual void                    onFocusOut(X11FocusEvent const & Event); 
00135         bool                            setInputFocus(std::string WindowTitle); 
00136         void                            shutdown();             
00137 
00138         // construction / deconstruction
00139         X11FocusWatcher();
00140         virtual ~X11FocusWatcher();
00141         
00142         // public static functions
00143         static int                      x11ErrorHandler(Display * display, XErrorEvent * error); 
00144         static int                      x11IOErrorHandler(Display * display); 
00145 
00146 private:
00147         // private functions
00148         void                            closeDisplay();         
00149         X11FocusEvent                   createFocusEvent(Window const & window, X11FocusEventType EventType); 
00150         static boost::tuple<std::string, std::string, std::string> getWindowName(Display * dpy, Window const & window, bool recurse = true); 
00151         bool                            openDisplay(std::string DisplayName); 
00152         void                            setFocusEventMasks();   
00153         void                            threadProc();           
00154                 
00155         // private member variables
00156         Window                          mCurrentWindow;         
00157         Display *                       mDisplay;               
00158         std::string                     mDisplayName;           
00159         X11FocusEvent                   mLastEventIn;           
00160         X11FocusEvent                   mLastEventOut;          
00161         int                             mScreen;                
00162         bool                            mWatching;              
00163         
00167         struct X11FocusWatcherThreadProc {
00168                 X11FocusWatcherThreadProc(X11FocusWatcher * pX11FocusWatcher) : mpX11FocusWatcher(pX11FocusWatcher) {
00169                         mpX11FocusWatcher->mThreading = false;
00170                 };
00171                 
00173                 void operator()() {
00174                         mpX11FocusWatcher->mThreading = true;
00175                         mpX11FocusWatcher->threadProc();
00176                         mpX11FocusWatcher->mThreading = false;
00177                 }
00178                 
00179                 X11FocusWatcher *               mpX11FocusWatcher;
00180         };              
00181         bool                                    mThreading;     
00182         X11FocusWatcherThreadProc               mThreadProc;    
00183 };
00184 
00186 
00187 } // Gizmod namespace
00188 
00189 #endif // __X11FocusWatcher_h

Generated on Wed Nov 7 10:04:16 2007 for gizmod by  doxygen 1.5.3