1 #ifndef YXAPP_H
2 #define YXAPP_H
3 
4 #include "yapp.h"
5 #include "ywindow.h"
6 #include "ycursor.h"
7 #include <X11/Xutil.h>
8 
9 #define KEY_MODMASK(x) ((x) & (xapp->KeyMask))
10 
11 class YAtom {
12     const char* name;
13     bool screen;
14     Atom atom;
15     void atomize();
16 public:
17     explicit YAtom(const char* name, bool screen = false) :
name(name)18         name(name), screen(screen), atom(None) { }
19     operator Atom();
str()20     const char* str() const { return name; }
21 };
22 
23 class YAtomName {
24 public:
25     Atom* atom;
26     const char* name;
27 };
28 
29 class YTextProperty : public XTextProperty {
30 public:
31     YTextProperty(const char* str);
32     ~YTextProperty();
33 };
34 
35 enum YFormat { F8 = 8, F16 = 16, F32 = 32 };
36 
37 class YProperty {
38 public:
39     YProperty(YWindow* window, Atom prop, YFormat format = F32,
40               long limit = 1L, Atom type = AnyPropertyType, bool remove = False):
41         fWind(window->handle()), fData(nullptr), fProp(prop), fKind(type),
42         fType(None), fLimit(limit), fSize(None), fMore(None), fBits(format),
43         fDelete(remove)
44     { update(); }
45 
46     YProperty(Window handle, Atom prop, YFormat format = F32,
47               long limit = 1L, Atom type = AnyPropertyType, bool remove = False):
fWind(handle)48         fWind(handle), fData(nullptr), fProp(prop), fKind(type), fType(None),
49         fLimit(limit), fSize(None), fMore(None), fBits(format), fDelete(remove)
50     { update(); }
51 
~YProperty()52     ~YProperty() { discard(); }
53 
54     void append(void const* data, int count) const;
55     void replace(void const* data, int count) const;
56     void discard();
57     const YProperty& update();
property()58     Atom property() const { return fProp; }
type()59     Atom type() const { return fType; }
bits()60     YFormat bits() const { return fBits; }
size()61     unsigned long size() const { return fSize; }
more()62     unsigned long more() const { return fMore; }
63     operator bool() const { return fData; }
typed(Atom type)64     bool typed(Atom type) const { return fData && type == fType; }
65 
data()66     template<class T> T* data() const { return reinterpret_cast<T*>(fData); }
retrieve()67     template<class T> T* retrieve() { T* t(data<T>()); fData = nullptr; return t; }
68     long operator[](int i) const { return data<long>()[i]; }
69     long operator*() const { return *data<long>(); }
70     template<class T> T* operator->() const { return data<T>(); }
71 
string()72     char* string() const { return data<char>(); }
begin()73     Atom* begin() const { return data<Atom>(); }
end()74     Atom* end() const { return begin() + fSize; }
75 
76 private:
77     Window fWind;
78     unsigned char* fData;
79     Atom fProp;
80     Atom fKind;
81     Atom fType;
82     long fLimit;
83     unsigned long fSize;
84     unsigned long fMore;
85     YFormat fBits;
86     bool fDelete;
87 };
88 
89 class YXPoll: public YPoll<class YXApplication> {
90 public:
YXPoll(YXApplication * owner)91     explicit YXPoll(YXApplication* owner) : YPoll(owner) { }
92     virtual void notifyRead();
forRead()93     virtual bool forRead() { return true; }
94 };
95 
96 class YXApplication: public YApplication {
97 public:
98     YXApplication(int *argc, char ***argv, const char *displayName = nullptr);
99     virtual ~YXApplication();
100 
display()101     Display * display()   const { return fDisplay; }
screen()102     int screen()          const { return fScreen; }
root()103     Window root()         const { return fRoot; }
visual()104     Visual * visual()     const { return fVisual; }
depth()105     unsigned depth()      const { return fDepth; }
colormap()106     Colormap colormap()   const { return fColormap; }
black()107     unsigned long black() const { return fBlack; }
white()108     unsigned long white() const { return fWhite; }
displayWidth()109     int displayWidth() { return DisplayWidth(display(), screen()); }
displayHeight()110     int displayHeight() { return DisplayHeight(display(), screen()); }
atom(const char * name)111     Atom atom(const char* name) { return XInternAtom(display(), name, False); }
sync()112     void sync() const { XSync(display(), False); }
113     void send(XClientMessageEvent& ev, Window win, long mask = NoEventMask) const;
114     Window parent(Window child) const;
115     bool children(Window win, Window** data, unsigned* num) const;
116 
hasColormap()117     bool hasColormap() const { return fHasColormaps; }
synchronized()118     bool synchronized() const { return synchronizeX11; }
alpha()119     bool alpha() const { return fAlpha; }
120     Visual* visualForDepth(unsigned depth) const;
121     Colormap colormapForDepth(unsigned depth) const;
122     Colormap colormapForVisual(Visual* visual) const;
123     XRenderPictFormat* formatForDepth(unsigned depth) const;
format()124     XRenderPictFormat* format() const { return formatForDepth(fDepth); }
125 
126     void saveEventTime(const XEvent &xev);
127     Time getEventTime(const char *debug) const;
128 
129     bool grabEvents(YWindow *win, Cursor ptr, unsigned long eventMask,
130                     bool grabMouse = true, bool grabKeyboard = true,
131                     bool grabTree = false);
132     bool releaseEvents();
133     void handleGrabEvent(YWindow *win, XEvent &xev);
134     virtual bool handleIdle();
135     void handleWindowEvent(Window xwindow, XEvent &xev);
136 
137     void replayEvent();
138 
139     void captureGrabEvents(YWindow *win);
140     void releaseGrabEvents(YWindow *win);
141 
142     virtual bool filterEvent(const XEvent &xev);
143 
144     void dispatchEvent(YWindow *win, XEvent &e);
145     virtual void afterWindowEvent(XEvent &xev);
146 
popup()147     YPopupWindow *popup() const { return fPopup; }
148     bool popup(YWindow *forWindow, YPopupWindow *popup);
149     void popdown(YPopupWindow *popdown);
150 
grabWindow()151     YWindow *grabWindow() const { return fGrabWindow; }
152 
153     void alert();
154 
155     void setClipboardText(mstring data);
156     void dropClipboard();
157 
158     static bool alphaBlending;
159     static bool synchronizeX11;
160 
161     unsigned int AltMask;
162     unsigned int MetaMask;
163     unsigned int NumLockMask;
164     unsigned int ScrollLockMask;
165     unsigned int SuperMask;
166     unsigned int HyperMask;
167     unsigned int ModeSwitchMask;
168     unsigned int WinMask;
169     unsigned int Win_L;
170     unsigned int Win_R;
171     unsigned int KeyMask;
172     unsigned int ButtonMask;
173     unsigned int ButtonKeyMask;
174 
175     bool hasControlAlt(unsigned state) const;
176     bool hasWinMask(unsigned state) const;
buttonMask(unsigned state)177     unsigned buttonMask(unsigned state) const {
178         return state & ButtonMask;
179     }
buttonModMask(unsigned state)180     unsigned buttonModMask(unsigned state) const {
181         return state & ButtonKeyMask;
182     }
isButton(unsigned state,unsigned mask)183     bool isButton(unsigned state, unsigned mask) const {
184         return mask == buttonModMask(state);
185     }
186 
187     static const char* getHelpText();
188 
189 protected:
190     virtual int handleError(XErrorEvent* xev);
getRightPointer()191     virtual Cursor getRightPointer() const { return None; }
192 
193 private:
194     XRenderPictFormat* findFormat(int depth) const;
195     Visual* findVisual(int depth) const;
196     Colormap getColormap(int depth) const;
197 
198     Display* const fDisplay;
199     int const fScreen;
200     Window const fRoot;
201     XRenderPictFormat* fFormat32;
202     XRenderPictFormat* fFormat24;
203     Visual* const fVisual32;
204     Visual* const fVisual24;
205     Colormap const fColormap32;
206     Colormap const fColormap24;
207     bool const fAlpha;
208 
209     unsigned const fDepth;
210     Visual* const fVisual;
211     Colormap const fColormap;
212     bool const fHasColormaps;
213     unsigned long const fBlack;
214     unsigned long const fWhite;
215 
216     Time lastEventTime;
217     YPopupWindow *fPopup;
218     friend class YXPoll;
219     YXPoll xfd;
220 
221     lazy<class YClipboard> fClip;
222     YWindow *fXGrabWindow;
223     YWindow *fGrabWindow;
224 
225     bool fGrabTree;
226     bool fGrabMouse;
227     bool fReplayEvent;
228 
229     virtual bool handleXEvents();
230     virtual void flushXEvents();
231 
232     void initModifiers();
233     static void initAtoms();
234 
235     static const char* parseArgs(int argc, char **argv, const char *displayName);
236     static Display* openDisplay(const char* displayName);
237     static void initExtensions(Display* dpy);
238     static bool haveColormaps(Display* dpy);
239     static int errorHandler(Display* display, XErrorEvent* xev);
240     static int cmapError(Display* display, XErrorEvent* xev);
241     static int sortAtoms(const void* p1, const void* p2);
242     static YAtomName atom_info[];
243 public:
244     static const char* atomName(Atom atom);
245     static Window ignorable;
246 };
247 
248 extern YXApplication *xapp;
249 
250 #endif
251 
252 // vim: set sw=4 ts=4 et:
253