00001
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
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
00049
00050 namespace Gizmod {
00051
00053
00055
00060 typedef enum {
00061 X11FOCUSEVENT_IN,
00062 X11FOCUSEVENT_OUT
00063 } X11FocusEventType;
00064
00066
00068
00082 class X11FocusEvent {
00083 public:
00084
00085 X11FocusEventType EventType;
00086 std::string WindowName;
00087 std::string WindowNameFormal;
00088 std::string WindowClass;
00089
00090
00091 bool isNull();
00092
00093
00094 bool operator != (X11FocusEvent const & Event);
00095 bool operator == (X11FocusEvent const & Event);
00096
00097
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
00105
00106
00107
00108 private:
00109
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
00123
00128 class X11FocusWatcher {
00129 public:
00130
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
00139 X11FocusWatcher();
00140 virtual ~X11FocusWatcher();
00141
00142
00143 static int x11ErrorHandler(Display * display, XErrorEvent * error);
00144 static int x11IOErrorHandler(Display * display);
00145
00146 private:
00147
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
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 }
00188
00189 #endif // __X11FocusWatcher_h