experimental new callbacks, totally standards compliant, faster, smaller
[dana/urxvt.git] / src / rxvttoolkit.h
1 /*----------------------------------------------------------------------*
2  * File:        rxvttoolkit.h - provide toolkit-functionality for rxvt.
3  *----------------------------------------------------------------------*
4  *
5  * All portions of code are copyright by their respective author/s.
6  * Copyright (c) 2003-2006 Marc Lehmann <pcg@goof.com>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21  *----------------------------------------------------------------------*/
22
23 #ifndef RXVT_TOOLKIT_H
24 #define RXVT_TOOLKIT_H
25
26 #include <X11/Xlib.h>
27
28 #if XFT
29 # include <X11/Xft/Xft.h>
30 #endif
31
32 #include "ev_cpp.h"
33
34 #include "rxvtlib.h"
35 #include "rxvtutil.h"
36
37 #include "callback.h"
38
39 // see rxvttoolkit.C:xa_names, which must be kept in sync
40 enum {
41   XA_TEXT,
42   XA_COMPOUND_TEXT,
43   XA_UTF8_STRING,
44   XA_MULTIPLE,
45   XA_TARGETS,
46   XA_TIMESTAMP,
47   XA_VT_SELECTION,
48   XA_INCR,
49   XA_WM_PROTOCOLS,
50   XA_WM_DELETE_WINDOW,
51   XA_CLIPBOARD,
52   XA_AVERAGE_WIDTH,
53   XA_WEIGHT_NAME,
54   XA_SLANT,
55   XA_CHARSET_REGISTRY,
56   XA_CHARSET_ENCODING,
57 #if ENABLE_FRILLS
58   XA_MOTIF_WM_HINTS,
59 #endif
60 #if ENABLE_EWMH
61   XA_NET_WM_PID,
62   XA_NET_WM_NAME,
63   XA_NET_WM_ICON_NAME,
64   XA_NET_WM_PING,
65 #endif
66 #if USE_XIM
67   XA_WM_LOCALE_NAME,
68   XA_XIM_SERVERS,
69 #endif
70 #if ENABLE_TRANSPARENCY
71   XA_XROOTPMAP_ID,
72   XA_ESETROOT_PMAP_ID,
73 #endif
74 #if ENABLE_XEMBED
75   XA_XEMBED,
76   XA_XEMBED_INFO,
77 #endif
78 #if !ENABLE_MINIMAL
79   // these are usually allocated by other subsystens, but we do it
80   // here to avoid a server roundtrip.
81   XA_SCREEN_RESOURCES,
82   XA_XDCCC_LINEAR_RGB_CORRECTION,
83   XA_XDCCC_LINEAR_RGB_MATRICES,
84   XA_WM_COLORMAP_WINDOWS,
85   XA_WM_STATE,
86   XA_cursor,
87 # if USE_XIM
88   // various selection targets used by XIM
89   XA_TRANSPORT,
90   XA_LOCALES,
91   XA__XIM_PROTOCOL,
92   XA__XIM_XCONNECT,
93   XA__XIM_MOREDATA,
94 # endif
95 #endif
96   NUM_XA
97 };
98
99 struct rxvt_term;
100 struct rxvt_display;
101
102 struct im_watcher;
103 struct xevent_watcher;
104
105 template<class watcher>
106 struct event_vec : vector<watcher *> {
107   void erase_unordered (unsigned int pos)
108   {
109     watcher *w = (*this)[this->size () - 1];
110     this->pop_back ();
111
112     if (!this->empty ())
113       if (((*this)[pos] = w)) // '=' is correct!
114         w->active = pos + 1;
115   }
116 };
117
118 struct rxvt_watcher {
119   int active; /* 0 == inactive, else index into respective vector */
120
121   bool is_active () { return active; }
122
123   rxvt_watcher () : active (0) { }
124 };
125
126 struct refcounted {
127   int referenced;
128   char *id;
129
130   refcounted (const char *id);
131   bool ref_init () { return false; }
132   void ref_next () { }
133   ~refcounted ();
134 };
135
136 template<class T>
137 struct refcache : vector<T *> {
138   T *get (const char *id);
139   void put (T *obj);
140   void clear ();
141
142   ~refcache ()
143   {
144     clear ();
145   }
146 };
147
148 /////////////////////////////////////////////////////////////////////////////
149
150 struct rxvt_screen;
151
152 struct rxvt_drawable {
153   rxvt_screen *screen;
154   Drawable drawable;
155   operator Drawable() { return drawable; }
156
157 #if XFT
158   XftDraw *xftdrawable;
159   operator XftDraw *();
160 #endif
161
162   rxvt_drawable (rxvt_screen *screen, Drawable drawable)
163   : screen(screen),
164 #if XFT
165     xftdrawable(0),
166 #endif
167     drawable(drawable)
168   { }
169
170 #if XFT
171   ~rxvt_drawable ();
172 #endif
173 };
174
175 /////////////////////////////////////////////////////////////////////////////
176
177 #ifdef USE_XIM
178 struct rxvt_xim : refcounted {
179   void destroy ();
180   rxvt_display *display;
181
182 //public
183   XIM xim;
184
185   rxvt_xim (const char *id) : refcounted (id) { }
186   bool ref_init ();
187   ~rxvt_xim ();
188 };
189 #endif
190
191 struct rxvt_screen {
192   rxvt_display *display;
193   Display *dpy;
194   int depth;
195   Visual *visual;
196   Colormap cmap;
197
198 #if XFT
199   // scratch pixmap
200   rxvt_drawable *scratch_area;
201   int scratch_w, scratch_h;
202
203   rxvt_drawable &scratch_drawable (int w, int h);
204
205   rxvt_screen ();
206 #endif
207
208   void set (rxvt_display *disp);
209   void select_visual (int bitdepth);
210   void clear ();
211 };
212
213 struct rxvt_display : refcounted {
214   event_vec<xevent_watcher> xw;
215
216   ev::io x_ev; void x_cb (ev::io &w, int revents);
217
218 #ifdef USE_XIM
219   refcache<rxvt_xim> xims;
220   vector<im_watcher *> imw;
221
222   void im_change_cb ();
223   void im_change_check ();
224 #endif
225
226 //public
227   Display   *dpy;
228   int       screen;
229   Window    root;
230   rxvt_term *selection_owner;
231   Atom      xa[NUM_XA];
232   bool      is_local;
233 #ifdef POINTER_BLANK
234   Cursor    blank_cursor;
235 #endif
236
237   rxvt_display (const char *id);
238   XrmDatabase get_resources (bool refresh);
239   bool ref_init ();
240   void ref_next ();
241   ~rxvt_display ();
242
243   void flush ();
244   Atom atom (const char *name);
245   void set_selection_owner (rxvt_term *owner);
246
247   void reg (xevent_watcher *w);
248   void unreg (xevent_watcher *w);
249
250 #ifdef USE_XIM
251   void reg (im_watcher *w);
252   void unreg (im_watcher *w);
253
254   rxvt_xim *get_xim (const char *locale, const char *modifiers);
255   void put_xim (rxvt_xim *xim);
256 #endif
257 };
258
259 #ifdef USE_XIM
260 struct im_watcher : rxvt_watcher, callback<void (void)> {
261   void start (rxvt_display *display)
262   {
263     display->reg (this);
264   }
265
266   void stop (rxvt_display *display)
267   {
268     display->unreg (this);
269   }
270 };
271 #endif
272
273 struct xevent_watcher : rxvt_watcher, callback<void (XEvent &)> {
274   Window window;
275
276   void start (rxvt_display *display, Window window)
277   {
278     this->window = window;
279     display->reg (this);
280   }
281
282   void stop (rxvt_display *display)
283   {
284     display->unreg (this);
285   }
286 };
287
288 extern refcache<rxvt_display> displays;
289
290 /////////////////////////////////////////////////////////////////////////////
291
292 typedef unsigned long Pixel;
293
294 struct rgba {
295   unsigned short r, g, b, a;
296
297   enum { MIN_CC = 0x0000, MAX_CC  = 0xffff };
298
299   rgba ()
300   { }
301
302   rgba (unsigned short r, unsigned short g, unsigned short b, unsigned short a = MAX_CC)
303   : r(r), g(g), b(b), a(a)
304   { }
305 };
306
307 struct rxvt_color {
308 #if XFT
309   XftColor c;
310 #else
311   XColor c;
312 #endif
313
314   operator Pixel () const { return c.pixel; }
315
316   bool operator == (const rxvt_color &b) const { return Pixel (*this) == Pixel (b); }
317   bool operator != (const rxvt_color &b) const { return Pixel (*this) != Pixel (b); }
318
319   bool alloc (rxvt_screen *screen, const rgba &color);
320   void free (rxvt_screen *screen);
321
322   void get (rgba &color);
323   void get (XColor &color);
324
325   bool set (rxvt_screen *screen, const char *name);
326   bool set (rxvt_screen *screen, const rgba &color);
327
328   void fade (rxvt_screen *screen, int percent, rxvt_color &result, const rgba &to = rgba (0, 0, 0));
329 };
330
331 #if TRACE_PIXMAPS
332 Pixmap trace_XCreatePixmap (const char *file, int line, Display *dpy, Window r, unsigned int w, unsigned int h, unsigned int d);
333 void trace_XFreePixmap (const char *file, int line, Display *dpy, Pixmap p);
334
335 # define XCreatePixmap(dpy,r,w,h,d) trace_XCreatePixmap (__FILE__,__LINE__,dpy,r,w,h,d)
336 # define XFreePixmap(dpy,p) trace_XFreePixmap (__FILE__,__LINE__,dpy,p)
337 #endif
338
339 #endif
340