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-2011 Marc Lehmann <schmorp@schmorp.de>
7  * Copyright (c) 2011      Emanuele Giaquinta <e.giaquinta@glauco.it>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 3 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22  *----------------------------------------------------------------------*/
23 
24 #ifndef RXVT_TOOLKIT_H
25 #define RXVT_TOOLKIT_H
26 
27 #include <X11/Xlib.h>
28 
29 #if XFT
30 # include <X11/Xft/Xft.h>
31 #endif
32 
33 #include "ev_cpp.h"
34 
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   XA_NET_WM_ICON,
66 #endif
67 #if USE_XIM
68   XA_WM_LOCALE_NAME,
69   XA_XIM_SERVERS,
70 #endif
71 #if HAVE_IMG || ENABLE_PERL
72   XA_XROOTPMAP_ID,
73   XA_ESETROOT_PMAP_ID,
74 #endif
75 #if ENABLE_XEMBED
76   XA_XEMBED,
77   XA_XEMBED_INFO,
78 #endif
79 #if !ENABLE_MINIMAL
80   // these are usually allocated by other subsystems, but we do it
81   // here to avoid a server roundtrip.
82   XA_SCREEN_RESOURCES,
83   XA_XDCCC_LINEAR_RGB_CORRECTION,
84   XA_XDCCC_LINEAR_RGB_MATRICES,
85   XA_WM_COLORMAP_WINDOWS,
86   XA_WM_STATE,
87   XA_cursor,
88 # if USE_XIM
89   // various selection targets used by XIM
90   XA_TRANSPORT,
91   XA_LOCALES,
92   XA__XIM_PROTOCOL,
93   XA__XIM_XCONNECT,
94   XA__XIM_MOREDATA,
95 # endif
96 #endif
97   NUM_XA
98 };
99 
100 struct rxvt_term;
101 struct rxvt_display;
102 
103 struct im_watcher;
104 struct xevent_watcher;
105 
106 template<class watcher>
107 struct event_vec : vector<watcher *>
108 {
erase_unorderedevent_vec109   void erase_unordered (unsigned int pos)
110   {
111     watcher *w = (*this)[this->size () - 1];
112     this->pop_back ();
113 
114     if (!this->empty ())
115       if (((*this)[pos] = w)) // '=' is correct!
116         w->active = pos + 1;
117   }
118 };
119 
120 struct rxvt_watcher
121 {
122   int active; /* 0 == inactive, else index into respective vector */
123 
is_activerxvt_watcher124   bool is_active () { return active; }
125 
rxvt_watcherrxvt_watcher126   rxvt_watcher () : active (0) { }
127 };
128 
129 struct refcounted
130 {
131   int referenced;
132   char *id;
133 
134   refcounted (const char *id);
ref_initrefcounted135   bool ref_init () { return false; }
ref_nextrefcounted136   void ref_next () { }
137   ~refcounted ();
138 };
139 
140 template<class T>
141 struct refcache : vector<T *>
142 {
143   T *get (const char *id);
144   void put (T *obj);
145   void clear ();
146 
~refcacherefcache147   ~refcache ()
148   {
149     clear ();
150   }
151 };
152 
153 /////////////////////////////////////////////////////////////////////////////
154 
155 struct rxvt_screen;
156 
157 struct rxvt_drawable
158 {
159   rxvt_screen *screen;
160   Drawable drawable;
Drawablerxvt_drawable161   operator Drawable() { return drawable; }
162 
163 #if XFT
164   XftDraw *xftdrawable;
165   operator XftDraw *();
166 #endif
167 
rxvt_drawablerxvt_drawable168   rxvt_drawable (rxvt_screen *screen, Drawable drawable)
169   : screen(screen),
170 #if XFT
171     xftdrawable(0),
172 #endif
173     drawable(drawable)
174   { }
175 
176 #if XFT
177   ~rxvt_drawable ();
178 #endif
179 };
180 
181 /////////////////////////////////////////////////////////////////////////////
182 
183 #if USE_XIM
184 struct rxvt_xim : refcounted
185 {
186   void destroy ();
187   rxvt_display *display;
188 
189 //public
190   XIM xim;
191 
rxvt_ximrxvt_xim192   rxvt_xim (const char *id) : refcounted (id) { }
193   bool ref_init ();
194   ~rxvt_xim ();
195 };
196 #endif
197 
198 struct rxvt_screen
199 {
200   rxvt_display *display;
201   Display *dpy;
202   int depth;
203   Visual *visual;
204   Colormap cmap;
205 
206 #if XFT
207   // scratch pixmap
208   rxvt_drawable *scratch_area;
209   int scratch_w, scratch_h;
210 
211   rxvt_drawable &scratch_drawable (int w, int h);
212 
213   rxvt_screen ();
214 #endif
215 
216   void set (rxvt_display *disp);
217   void select_visual (int id);
218   void select_depth (int bitdepth); // select visual by depth
219   void clear ();
220 };
221 
222 enum
223 {
224   DISPLAY_HAS_RENDER      = 1 << 0,
225   DISPLAY_HAS_RENDER_CONV = 1 << 1,
226 };
227 
228 struct rxvt_display : refcounted
229 {
230   event_vec<xevent_watcher> xw;
231 
232   ev::prepare flush_ev; void flush_cb (ev::prepare &w, int revents);
233   ev::io      x_ev    ; void x_cb     (ev::io      &w, int revents);
234 
235 #if USE_XIM
236   refcache<rxvt_xim> xims;
237   vector<im_watcher *> imw;
238 
239   void im_change_cb ();
240   void im_change_check ();
241 #endif
242 
243 //public
244   Display   *dpy;
245   int       screen;
246   Window    root;
247   rxvt_term *selection_owner;
248   rxvt_term *clipboard_owner;
249   Atom      xa[NUM_XA];
250   bool      is_local;
251 #ifdef POINTER_BLANK
252   Cursor    blank_cursor;
253 #endif
254   uint8_t   flags;
255 
256   rxvt_display (const char *id);
257   XrmDatabase get_resources (bool refresh);
258   bool ref_init ();
259   void ref_next ();
260   ~rxvt_display ();
261 
flushrxvt_display262   void flush ()
263   {
264     flush_ev.start ();
265   }
266 
267   Atom atom (const char *name);
268   Pixmap get_pixmap_property (Atom property);
269   void set_selection_owner (rxvt_term *owner, bool clipboard);
270 
271   void reg (xevent_watcher *w);
272   void unreg (xevent_watcher *w);
273 
274 #if USE_XIM
275   void reg (im_watcher *w);
276   void unreg (im_watcher *w);
277 
278   rxvt_xim *get_xim (const char *locale, const char *modifiers);
279   void put_xim (rxvt_xim *xim);
280 #endif
281 };
282 
283 #if USE_XIM
284 struct im_watcher : rxvt_watcher, callback<void (void)>
285 {
startim_watcher286   void start (rxvt_display *display)
287   {
288     display->reg (this);
289   }
290 
stopim_watcher291   void stop (rxvt_display *display)
292   {
293     display->unreg (this);
294   }
295 };
296 #endif
297 
298 struct xevent_watcher : rxvt_watcher, callback<void (XEvent &)>
299 {
300   Window window;
301 
startxevent_watcher302   void start (rxvt_display *display, Window window)
303   {
304     this->window = window;
305     display->reg (this);
306   }
307 
stopxevent_watcher308   void stop (rxvt_display *display)
309   {
310     display->unreg (this);
311   }
312 };
313 
314 extern refcache<rxvt_display> displays;
315 
316 /////////////////////////////////////////////////////////////////////////////
317 
318 typedef unsigned long Pixel;
319 
320 struct rgba
321 {
322   uint16_t r, g, b, a;
323 
324   enum { MIN_CC = 0x0000, MAX_CC  = 0xffff };
325 
rgbargba326   rgba ()
327   { }
328 
329   rgba (uint16_t r, uint16_t g, uint16_t b, uint16_t a = MAX_CC)
rrgba330   : r(r), g(g), b(b), a(a)
331   { }
332 };
333 
334 struct rxvt_color
335 {
336 #if XFT
337   XftColor c;
338 #else
339   XColor c;
340 #endif
341 
Pixelrxvt_color342   operator Pixel () const { return c.pixel; }
343 
344   bool operator == (const rxvt_color &b) const { return Pixel (*this) == Pixel (b); }
345   bool operator != (const rxvt_color &b) const { return Pixel (*this) != Pixel (b); }
346 
is_opaquerxvt_color347   bool is_opaque () const
348   {
349 #if XFT
350     return c.color.alpha == rgba::MAX_CC;
351 #else
352     return 1;
353 #endif
354   }
355 
356   bool alloc (rxvt_screen *screen, const rgba &color);
357   void free (rxvt_screen *screen);
358 
rgbarxvt_color359   operator rgba () const
360   {
361     rgba c;
362     get (c);
363     return c;
364   }
365   void get (rgba &color) const;
366   void get (XColor &color) const;
367 
368   bool set (rxvt_screen *screen, const char *name);
369   bool set (rxvt_screen *screen, const rgba &color);
370 
371   void fade (rxvt_screen *screen, int percent, rxvt_color &result, const rgba &to = rgba (0, 0, 0));
372 };
373 
374 #define Sel_normal              0x01    /* normal selection */
375 #define Sel_incr                0x02    /* incremental selection */
376 #define Sel_Primary             0x01
377 #define Sel_Secondary           0x02
378 #define Sel_Clipboard           0x03
379 #define Sel_whereMask           0x0f
380 #define Sel_CompoundText        0x10    /* last request was COMPOUND_TEXT */
381 #define Sel_UTF8String          0x20    /* last request was UTF8_STRING */
382 
383 struct rxvt_selection
384 {
385   rxvt_selection (rxvt_display *disp, int selnum, Time tm, Window win, Atom prop, rxvt_term *term);
386   void run ();
387   ~rxvt_selection ();
388 
389   rxvt_term *term; // terminal to paste to, may be 0
390   void *cb_sv;     // managed by perl
391 
392   rxvt_display *display;
393   Time request_time;
394   Window request_win;
395   Atom request_prop;
396 
397 private:
398   unsigned char selection_wait;
399   unsigned char selection_type;
400 
401   char *incr_buf;
402   size_t incr_buf_size, incr_buf_fill;
403 
404   void timer_cb (ev::timer &w, int revents); ev::timer timer_ev;
405   void x_cb (XEvent &xev); xevent_watcher x_ev;
406 
407   void finish (char *data = 0, unsigned int len = 0);
408   void stop ();
409   bool request (Atom target, int selnum);
410   void handle_selection (Window win, Atom prop, bool delete_prop);
411 };
412 
413 #endif
414 
415