ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/rxvt-unicode/src/rxvttoolkit.C
Revision: 1.148
Committed: Wed Dec 17 14:25:01 2014 UTC (9 years, 5 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.147: +7 -5 lines
Log Message:
*** empty log message ***

File Contents

# User Rev Content
1 root 1.57 /*----------------------------------------------------------------------*
2 root 1.5 * File: rxvttoolkit.C
3 root 1.1 *----------------------------------------------------------------------*
4     *
5     * All portions of code are copyright by their respective author/s.
6 root 1.121 * Copyright (c) 2003-2011 Marc Lehmann <schmorp@schmorp.de>
7 sf-exg 1.116 * Copyright (c) 2011 Emanuele Giaquinta <e.giaquinta@glauco.it>
8 root 1.1 *
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 root 1.142 * the Free Software Foundation; either version 3 of the License, or
12 root 1.1 * (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     #include "../config.h"
25     #include <rxvt.h>
26     #include <rxvttoolkit.h>
27    
28 root 1.146 #include <stdlib.h>
29    
30 root 1.1 #include <unistd.h>
31     #include <fcntl.h>
32    
33 root 1.23 #include <sys/utsname.h>
34 root 1.52 #include <sys/socket.h>
35     #include <sys/un.h>
36 root 1.1
37 root 1.31 #if XFT
38     # include <X11/extensions/Xrender.h>
39     #endif
40    
41 sf-exg 1.110 static const char *const xa_names[] =
42 root 1.34 {
43     "TEXT",
44     "COMPOUND_TEXT",
45     "UTF8_STRING",
46     "MULTIPLE",
47     "TARGETS",
48     "TIMESTAMP",
49     "VT_SELECTION",
50     "INCR",
51     "WM_PROTOCOLS",
52     "WM_DELETE_WINDOW",
53     "CLIPBOARD",
54     "AVERAGE_WIDTH",
55     "WEIGHT_NAME",
56     "SLANT",
57     "CHARSET_REGISTRY",
58     "CHARSET_ENCODING",
59 root 1.25 #if ENABLE_FRILLS
60 root 1.34 "_MOTIF_WM_HINTS",
61 root 1.25 #endif
62     #if ENABLE_EWMH
63 root 1.34 "_NET_WM_PID",
64     "_NET_WM_NAME",
65     "_NET_WM_ICON_NAME",
66     "_NET_WM_PING",
67 root 1.102 "_NET_WM_ICON",
68 root 1.25 #endif
69     #if USE_XIM
70 root 1.34 "WM_LOCALE_NAME",
71     "XIM_SERVERS",
72 root 1.25 #endif
73 sf-exg 1.141 #if HAVE_IMG || ENABLE_PERL
74 root 1.34 "_XROOTPMAP_ID",
75     "ESETROOT_PMAP_ID",
76 root 1.25 #endif
77     #if ENABLE_XEMBED
78 root 1.34 "_XEMBED",
79     "_XEMBED_INFO",
80     #endif
81     #if !ENABLE_MINIMAL
82     "SCREEN_RESOURCES",
83     "XDCCC_LINEAR_RGB_CORRECTION",
84     "XDCCC_LINEAR_RGB_MATRICES",
85     "WM_COLORMAP_WINDOWS",
86     "WM_STATE",
87 root 1.38 "cursor",
88     # if USE_XIM
89     "TRANSPORT",
90     "LOCALES",
91     "_XIM_PROTOCOL",
92     "_XIM_XCONNECT",
93     "_XIM_MOREDATA",
94     # endif
95 root 1.25 #endif
96 root 1.34 };
97 root 1.25
98     /////////////////////////////////////////////////////////////////////////////
99    
100 root 1.1 refcounted::refcounted (const char *id)
101     {
102 root 1.2 this->id = strdup (id);
103 root 1.1 }
104    
105     refcounted::~refcounted ()
106     {
107     free (id);
108     }
109    
110     template<class T>
111     T *refcache<T>::get (const char *id)
112     {
113     for (T **i = this->begin (); i < this->end (); ++i)
114     {
115     if (!strcmp (id, (*i)->id))
116     {
117 root 1.23 ++(*i)->referenced;
118     (*i)->ref_next ();
119 root 1.1 return *i;
120     }
121     }
122    
123     T *obj = new T (id);
124    
125 root 1.23 if (obj && obj->ref_init ())
126 root 1.1 {
127 root 1.23 obj->referenced = 1;
128 root 1.1 this->push_back (obj);
129     return obj;
130     }
131     else
132     {
133     delete obj;
134     return 0;
135     }
136     }
137    
138     template<class T>
139     void refcache<T>::put (T *obj)
140     {
141     if (!obj)
142     return;
143    
144     if (!--obj->referenced)
145     {
146     this->erase (find (this->begin (), this->end (), obj));
147     delete obj;
148     }
149     }
150    
151     template<class T>
152 root 1.12 void refcache<T>::clear ()
153 root 1.1 {
154     while (this->size ())
155     put (*this->begin ());
156     }
157    
158     /////////////////////////////////////////////////////////////////////////////
159    
160 root 1.134 #if USE_XIM
161 root 1.24
162 root 1.1 static void
163     #if XIMCB_PROTO_BROKEN
164     im_destroy_cb (XIC unused1, XPointer client_data, XPointer unused3)
165     #else
166     im_destroy_cb (XIM unused1, XPointer client_data, XPointer unused3)
167     #endif
168     {
169     rxvt_xim *xim = (rxvt_xim *)client_data;
170     rxvt_display *display = xim->display;
171    
172 root 1.5 xim->xim = 0;
173    
174 root 1.1 display->xims.erase (find (display->xims.begin (), display->xims.end (), xim));
175     display->im_change_cb ();
176     }
177    
178 root 1.23 bool
179     rxvt_xim::ref_init ()
180 root 1.1 {
181     display = GET_R->display; //HACK: TODO
182    
183 root 1.50 xim = XOpenIM (display->dpy, 0, 0, 0);
184 root 1.1
185     if (!xim)
186     return false;
187    
188     XIMCallback ximcallback;
189     ximcallback.client_data = (XPointer)this;
190     ximcallback.callback = im_destroy_cb;
191    
192 root 1.74 XSetIMValues (xim, XNDestroyCallback, &ximcallback, (char *)0);
193 root 1.1
194     return true;
195     }
196    
197     rxvt_xim::~rxvt_xim ()
198     {
199     if (xim)
200     XCloseIM (xim);
201     }
202 root 1.24
203 root 1.1 #endif
204    
205     /////////////////////////////////////////////////////////////////////////////
206    
207 root 1.51 #if XFT
208     rxvt_drawable::~rxvt_drawable ()
209     {
210     if (xftdrawable)
211     XftDrawDestroy (xftdrawable);
212     }
213    
214     rxvt_drawable::operator XftDraw *()
215     {
216     if (!xftdrawable)
217     xftdrawable = XftDrawCreate (screen->dpy, drawable, screen->visual, screen->cmap);
218    
219     return xftdrawable;
220     }
221     #endif
222    
223     /////////////////////////////////////////////////////////////////////////////
224    
225     #if XFT
226    
227     // not strictly necessary as it is only used with superclass of zero_initialised
228     rxvt_screen::rxvt_screen ()
229     : scratch_area (0)
230     {
231     }
232    
233     rxvt_drawable &rxvt_screen::scratch_drawable (int w, int h)
234     {
235 sf-exg 1.113 if (!scratch_area || w > scratch_w || h > scratch_h)
236 root 1.51 {
237     if (scratch_area)
238     {
239     XFreePixmap (dpy, scratch_area->drawable);
240     delete scratch_area;
241     }
242    
243     Pixmap pm = XCreatePixmap (dpy, RootWindowOfScreen (ScreenOfDisplay (dpy, display->screen)),
244     scratch_w = w, scratch_h = h, depth);
245    
246     scratch_area = new rxvt_drawable (this, pm);
247     }
248    
249     return *scratch_area;
250     }
251    
252     #endif
253    
254 root 1.26 void
255     rxvt_screen::set (rxvt_display *disp)
256     {
257     display = disp;
258 root 1.50 dpy = disp->dpy;
259 root 1.26
260 root 1.50 Screen *screen = ScreenOfDisplay (dpy, disp->screen);
261 root 1.26
262 root 1.27 depth = DefaultDepthOfScreen (screen);
263     visual = DefaultVisualOfScreen (screen);
264     cmap = DefaultColormapOfScreen (screen);
265 root 1.26 }
266    
267 root 1.137 #if ENABLE_FRILLS
268    
269 root 1.26 void
270 root 1.137 rxvt_screen::select_visual (int id)
271 root 1.26 {
272     XVisualInfo vinfo;
273 root 1.137 vinfo.visualid = id;
274     int n;
275 root 1.26
276 root 1.137 if (XVisualInfo *vi = XGetVisualInfo (dpy, VisualIDMask, &vinfo, &n))
277 root 1.26 {
278 root 1.137 depth = vi->depth;
279     visual = vi->visual;
280    
281     XFree (vi);
282    
283     cmap = XCreateColormap (dpy, display->root, visual, AllocNone);
284 root 1.26 }
285 root 1.137 else
286 sf-exg 1.143 rxvt_warn ("no visual found for requested id 0x%02x, using default visual.\n", id);
287 root 1.137 }
288    
289     void
290     rxvt_screen::select_depth (int bitdepth)
291     {
292     XVisualInfo vinfo;
293    
294     if (XMatchVisualInfo (dpy, display->screen, bitdepth, TrueColor, &vinfo))
295     select_visual (vinfo.visualid);
296     else
297     rxvt_warn ("no visual found for requested depth %d, using default visual.\n", bitdepth);
298     }
299    
300 root 1.31 #endif
301 root 1.26
302     void
303     rxvt_screen::clear ()
304     {
305 root 1.51 #if XFT
306     if (scratch_area)
307     {
308     XFreePixmap (dpy, scratch_area->drawable);
309     delete scratch_area;
310     }
311     #endif
312    
313 root 1.50 if (cmap != DefaultColormapOfScreen (ScreenOfDisplay (dpy, display->screen)))
314     XFreeColormap (dpy, cmap);
315 root 1.26 }
316    
317     /////////////////////////////////////////////////////////////////////////////
318    
319 root 1.1 rxvt_display::rxvt_display (const char *id)
320     : refcounted (id)
321     , selection_owner (0)
322 sf-exg 1.104 , clipboard_owner (0)
323 root 1.1 {
324 root 1.82 x_ev .set<rxvt_display, &rxvt_display::x_cb > (this);
325     flush_ev.set<rxvt_display, &rxvt_display::flush_cb> (this);
326 root 1.1 }
327    
328 root 1.23 XrmDatabase
329 root 1.49 rxvt_display::get_resources (bool refresh)
330 root 1.23 {
331 ayin 1.80 char *homedir = getenv ("HOME");
332 root 1.23 char fname[1024];
333    
334     /*
335     * get resources using the X library function
336     */
337     char *displayResource, *xe;
338 root 1.49 XrmDatabase rdb1, database = 0;
339 root 1.23
340 root 1.114 #if !XLIB_ILLEGAL_ACCESS
341     /* work around a bug in XrmSetDatabase where it frees the db, see ref_next */
342     database = XrmGetStringDatabase ("");
343     #endif
344    
345 root 1.23 // for ordering, see for example http://www.faqs.org/faqs/Xt-FAQ/ Subject: 20
346 root 1.60 // as opposed to "standard practise", we always read in ~/.Xdefaults
347 root 1.23
348     // 6. System wide per application default file.
349    
350     /* Add in $XAPPLRESDIR/Rxvt only; not bothering with XUSERFILESEARCHPATH */
351 ayin 1.80 if ((xe = getenv ("XAPPLRESDIR")))
352 root 1.23 {
353     snprintf (fname, sizeof (fname), "%s/%s", xe, RESCLASS);
354    
355     if ((rdb1 = XrmGetFileDatabase (fname)))
356     XrmMergeDatabases (rdb1, &database);
357     }
358    
359     // 5. User's per application default file.
360     // none
361    
362     // 4. User's defaults file.
363 root 1.60 if (homedir)
364     {
365     snprintf (fname, sizeof (fname), "%s/.Xdefaults", homedir);
366    
367     if ((rdb1 = XrmGetFileDatabase (fname)))
368     XrmMergeDatabases (rdb1, &database);
369     }
370    
371 root 1.23 /* Get any Xserver defaults */
372 root 1.49 if (refresh)
373     {
374     // fucking xlib keeps a copy of the rm string
375     Atom actual_type;
376     int actual_format;
377     unsigned long nitems, nremaining;
378     char *val = 0;
379    
380     #if XLIB_ILLEGAL_ACCESS
381 root 1.50 if (dpy->xdefaults)
382     XFree (dpy->xdefaults);
383 root 1.49 #endif
384    
385 root 1.55 if (XGetWindowProperty (dpy, RootWindow (dpy, 0), XA_RESOURCE_MANAGER,
386 root 1.49 0L, 100000000L, False,
387     XA_STRING, &actual_type, &actual_format,
388     &nitems, &nremaining,
389     (unsigned char **)&val) == Success
390     && actual_type == XA_STRING
391     && actual_format == 8)
392 sf-exg 1.109 displayResource = val;
393     else
394     {
395     displayResource = 0;
396    
397     if (val)
398     XFree (val);
399     }
400 root 1.49
401     #if XLIB_ILLEGAL_ACCESS
402 root 1.50 dpy->xdefaults = displayResource;
403 root 1.49 #endif
404     }
405     else
406 root 1.50 displayResource = XResourceManagerString (dpy);
407 root 1.23
408 root 1.49 if (displayResource)
409 root 1.23 {
410     if ((rdb1 = XrmGetStringDatabase (displayResource)))
411     XrmMergeDatabases (rdb1, &database);
412     }
413    
414 root 1.49 #if !XLIB_ILLEGAL_ACCESS
415     if (refresh && displayResource)
416     XFree (displayResource);
417     #endif
418    
419 root 1.23 /* Get screen specific resources */
420 root 1.50 displayResource = XScreenResourceString (ScreenOfDisplay (dpy, screen));
421 root 1.23
422 root 1.49 if (displayResource)
423 root 1.23 {
424     if ((rdb1 = XrmGetStringDatabase (displayResource)))
425     /* Merge with screen-independent resources */
426     XrmMergeDatabases (rdb1, &database);
427    
428     XFree (displayResource);
429     }
430    
431     // 3. User's per host defaults file
432     /* Add in XENVIRONMENT file */
433 ayin 1.80 if ((xe = getenv ("XENVIRONMENT"))
434 root 1.23 && (rdb1 = XrmGetFileDatabase (xe)))
435     XrmMergeDatabases (rdb1, &database);
436     else if (homedir)
437     {
438     struct utsname un;
439    
440     if (!uname (&un))
441     {
442     snprintf (fname, sizeof (fname), "%s/.Xdefaults-%s", homedir, un.nodename);
443    
444     if ((rdb1 = XrmGetFileDatabase (fname)))
445     XrmMergeDatabases (rdb1, &database);
446     }
447     }
448    
449     return database;
450     }
451    
452     bool rxvt_display::ref_init ()
453 root 1.1 {
454 root 1.14 #ifdef LOCAL_X_IS_UNIX
455     if (id[0] == ':')
456     {
457 root 1.131 char *val = rxvt_temp_buf<char> (5 + strlen (id) + 1);
458    
459 root 1.14 strcpy (val, "unix/");
460     strcat (val, id);
461 root 1.131
462 root 1.50 dpy = XOpenDisplay (val);
463 root 1.14 }
464     else
465 root 1.15 #endif
466 root 1.50 dpy = 0;
467 root 1.14
468 root 1.50 if (!dpy)
469     dpy = XOpenDisplay (id);
470 root 1.1
471 root 1.50 if (!dpy)
472 root 1.1 return false;
473    
474 root 1.50 screen = DefaultScreen (dpy);
475     root = DefaultRootWindow (dpy);
476 root 1.1
477 sf-exg 1.127 assert (ecb_array_length (xa_names) == NUM_XA);
478 root 1.50 XInternAtoms (dpy, (char **)xa_names, NUM_XA, False, xa);
479 root 1.25
480 root 1.50 XrmSetDatabase (dpy, get_resources (false));
481 root 1.1
482 root 1.12 #ifdef POINTER_BLANK
483     XColor blackcolour;
484     blackcolour.red = 0;
485     blackcolour.green = 0;
486     blackcolour.blue = 0;
487 root 1.50 Font f = XLoadFont (dpy, "fixed");
488     blank_cursor = XCreateGlyphCursor (dpy, f, f, ' ', ' ',
489 root 1.12 &blackcolour, &blackcolour);
490 root 1.50 XUnloadFont (dpy, f);
491 root 1.12 #endif
492    
493 sf-exg 1.132 flags = 0;
494     #if XRENDER
495     int major, minor;
496     if (XRenderQueryVersion (dpy, &major, &minor))
497 sf-exg 1.144 if (major > 0 || (major == 0 && minor >= 11))
498 root 1.138 {
499     flags |= DISPLAY_HAS_RENDER;
500 sf-exg 1.132
501 root 1.138 if (XFilters *filters = XRenderQueryFilters (dpy, root))
502     {
503     for (int i = 0; i < filters->nfilter; i++)
504     if (!strcmp (filters->filter [i], FilterConvolution))
505     flags |= DISPLAY_HAS_RENDER_CONV;
506 sf-exg 1.132
507 root 1.138 XFree (filters);
508     }
509     }
510 sf-exg 1.132 #endif
511    
512 root 1.50 int fd = XConnectionNumber (dpy);
513 root 1.23
514 sf-exg 1.107 // try to detect whether we have a local connection.
515 root 1.76 // assume unix domain socket == local, everything else not
516 root 1.23 // TODO: might want to check for inet/127.0.0.1
517     is_local = 0;
518     sockaddr_un sa;
519     socklen_t sl = sizeof (sa);
520    
521     if (!getsockname (fd, (sockaddr *)&sa, &sl))
522 ayin 1.67 is_local = sa.sun_family == AF_UNIX;
523 root 1.23
524 root 1.82 flush_ev.start ();
525 root 1.75 x_ev.start (fd, ev::READ);
526 root 1.1 fcntl (fd, F_SETFD, FD_CLOEXEC);
527    
528 root 1.50 XSelectInput (dpy, root, PropertyChangeMask);
529 root 1.1
530     flush ();
531    
532     return true;
533     }
534    
535 root 1.23 void
536     rxvt_display::ref_next ()
537     {
538 sf-exg 1.107 // TODO: somehow check whether the database files/resources changed
539 root 1.46 // before affording re-loading/parsing
540 root 1.50 XrmDestroyDatabase (XrmGetDatabase (dpy));
541 root 1.114 #if XLIB_ILLEGAL_ACCESS
542     /* work around a bug in XrmSetDatabase where it frees the db */
543     dpy->db = 0;
544     #endif
545 root 1.50 XrmSetDatabase (dpy, get_resources (true));
546 root 1.23 }
547    
548 root 1.1 rxvt_display::~rxvt_display ()
549     {
550 root 1.50 if (!dpy)
551 root 1.22 return;
552    
553 root 1.21 #ifdef POINTER_BLANK
554 root 1.50 XFreeCursor (dpy, blank_cursor);
555 root 1.21 #endif
556 root 1.1 x_ev.stop ();
557 root 1.85 flush_ev.stop ();
558 root 1.134 #if USE_XIM
559 root 1.12 xims.clear ();
560     #endif
561 sf-exg 1.112 XrmDestroyDatabase (XrmGetDatabase (dpy));
562 root 1.50 XCloseDisplay (dpy);
563 root 1.1 }
564    
565 root 1.134 #if USE_XIM
566 root 1.1 void rxvt_display::im_change_cb ()
567     {
568     for (im_watcher **i = imw.begin (); i != imw.end (); ++i)
569     (*i)->call ();
570     }
571 root 1.5
572     void rxvt_display::im_change_check ()
573     {
574 root 1.13 // try to only call im_change_cb when a new input method
575 root 1.5 // registers, as xlib crashes due to a race otherwise.
576     Atom actual_type, *atoms;
577     int actual_format;
578     unsigned long nitems, bytes_after;
579    
580 root 1.50 if (XGetWindowProperty (dpy, root, xa[XA_XIM_SERVERS], 0L, 1000000L,
581 root 1.5 False, XA_ATOM, &actual_type, &actual_format,
582     &nitems, &bytes_after, (unsigned char **)&atoms)
583 sf-exg 1.109 != Success)
584 root 1.5 return;
585    
586 sf-exg 1.109 if (actual_type == XA_ATOM && actual_format == 32)
587 root 1.5 for (int i = 0; i < nitems; i++)
588 root 1.50 if (XGetSelectionOwner (dpy, atoms[i]))
589 root 1.5 {
590     im_change_cb ();
591     break;
592     }
593    
594     XFree (atoms);
595     }
596 root 1.1 #endif
597    
598 root 1.75 void rxvt_display::x_cb (ev::io &w, int revents)
599 root 1.1 {
600 root 1.101 flush_ev.start ();
601     }
602    
603     void rxvt_display::flush_cb (ev::prepare &w, int revents)
604     {
605     while (XEventsQueued (dpy, QueuedAfterFlush))
606     do
607     {
608     XEvent xev;
609     XNextEvent (dpy, &xev);
610 root 1.1
611 root 1.134 #if USE_XIM
612 root 1.101 if (!XFilterEvent (&xev, None))
613     {
614     if (xev.type == PropertyNotify
615     && xev.xany.window == root
616     && xev.xproperty.atom == xa[XA_XIM_SERVERS])
617     im_change_check ();
618 root 1.99 #endif
619 root 1.101 if (xev.type == MappingNotify)
620     XRefreshKeyboardMapping (&xev.xmapping);
621 root 1.69
622 root 1.101 for (int i = xw.size (); i--; )
623     {
624     if (!xw[i])
625     xw.erase_unordered (i);
626     else if (xw[i]->window == xev.xany.window)
627     xw[i]->call (xev);
628     }
629 root 1.134 #if USE_XIM
630 root 1.101 }
631 root 1.99 #endif
632 root 1.101 }
633     while (XEventsQueued (dpy, QueuedAlready));
634 root 1.97
635 root 1.94 w.stop ();
636 root 1.1 }
637    
638     void rxvt_display::reg (xevent_watcher *w)
639     {
640 root 1.62 if (!w->active)
641 root 1.61 {
642     xw.push_back (w);
643     w->active = xw.size ();
644     }
645 root 1.1 }
646    
647     void rxvt_display::unreg (xevent_watcher *w)
648     {
649     if (w->active)
650 root 1.61 {
651     xw[w->active - 1] = 0;
652     w->active = 0;
653     }
654 root 1.1 }
655    
656 sf-exg 1.104 void rxvt_display::set_selection_owner (rxvt_term *owner, bool clipboard)
657 root 1.1 {
658 sf-exg 1.104 rxvt_term * &cur_owner = !clipboard ? selection_owner : clipboard_owner;
659    
660     if (cur_owner && cur_owner != owner)
661 root 1.86 {
662 sf-exg 1.106 rxvt_term *term = cur_owner;
663     term->selection_clear (clipboard);
664     term->flush ();
665 root 1.86 }
666 root 1.1
667 sf-exg 1.104 cur_owner = owner;
668 root 1.1 }
669    
670 root 1.134 #if USE_XIM
671 root 1.46
672 root 1.1 void rxvt_display::reg (im_watcher *w)
673     {
674     imw.push_back (w);
675     }
676    
677     void rxvt_display::unreg (im_watcher *w)
678     {
679     imw.erase (find (imw.begin (), imw.end (), w));
680     }
681    
682     rxvt_xim *rxvt_display::get_xim (const char *locale, const char *modifiers)
683     {
684     char *id;
685     int l, m;
686    
687     l = strlen (locale);
688     m = strlen (modifiers);
689    
690 sf-exg 1.105 if (!(id = rxvt_temp_buf<char> (l + m + 2)))
691 root 1.1 return 0;
692    
693     memcpy (id, locale, l); id[l] = '\n';
694     memcpy (id + l + 1, modifiers, m); id[l + m + 1] = 0;
695    
696     rxvt_xim *xim = xims.get (id);
697    
698     return xim;
699     }
700    
701     void rxvt_display::put_xim (rxvt_xim *xim)
702     {
703 root 1.46 # if XLIB_IS_RACEFREE
704 root 1.1 xims.put (xim);
705 root 1.46 # endif
706 root 1.1 }
707 root 1.46
708 root 1.1 #endif
709    
710     Atom rxvt_display::atom (const char *name)
711     {
712 root 1.50 return XInternAtom (dpy, name, False);
713 root 1.1 }
714    
715 sf-exg 1.133 Pixmap
716     rxvt_display::get_pixmap_property (Atom property)
717     {
718     Pixmap pixmap = None;
719    
720     int aformat;
721     unsigned long nitems, bytes_after;
722     Atom atype;
723     unsigned char *prop;
724     int result = XGetWindowProperty (dpy, root, property,
725     0L, 1L, False, XA_PIXMAP, &atype, &aformat,
726     &nitems, &bytes_after, &prop);
727     if (result == Success)
728     {
729     if (atype == XA_PIXMAP)
730     pixmap = *(Pixmap *)prop;
731     XFree (prop);
732     }
733    
734     return pixmap;
735     }
736    
737 root 1.1 /////////////////////////////////////////////////////////////////////////////
738    
739     template class refcache<rxvt_display>;
740     refcache<rxvt_display> displays;
741    
742     /////////////////////////////////////////////////////////////////////////////
743 root 1.58 //
744    
745     static unsigned int
746     insert_component (unsigned int value, unsigned int mask, unsigned int shift)
747     {
748     return (value * (mask + 1) >> 16) << shift;
749     }
750 ayin 1.73
751 root 1.1 bool
752 root 1.43 rxvt_color::alloc (rxvt_screen *screen, const rgba &color)
753 root 1.1 {
754 root 1.90 //TODO: only supports 24 bit
755 sf-exg 1.129 unsigned int alpha = color.a >= 0xff00 ? 0xffff : color.a;
756 root 1.88
757 root 1.28 #if XFT
758 root 1.31 XRenderPictFormat *format;
759 root 1.28
760 root 1.148 // not needed by XftColorAlloc, but by the other paths (ours
761     // and fallback), so just set all components here.
762     c.color.red = color.r;
763     c.color.green = color.g;
764     c.color.blue = color.b;
765     c.color.alpha = alpha;
766    
767 root 1.48 // FUCKING Xft gets it wrong, of course, so work around it.
768     // Transparency users should eat shit and die, and then
769 root 1.31 // XRenderQueryPictIndexValues themselves plenty.
770 root 1.48 if ((screen->visual->c_class == TrueColor)
771 root 1.50 && (format = XRenderFindVisualFormat (screen->dpy, screen->visual)))
772 root 1.31 {
773     // the fun lies in doing everything manually...
774 root 1.40
775 root 1.125 // Xft wants premultiplied alpha, but abuses the alpha channel
776     // as blend factor, and doesn't allow us to set the alpha channel
777     c.color.red = c.color.red * alpha / 0xffff;
778     c.color.green = c.color.green * alpha / 0xffff;
779     c.color.blue = c.color.blue * alpha / 0xffff;
780 sf-exg 1.124
781     c.pixel = insert_component (c.color.red , format->direct.redMask , format->direct.red )
782     | insert_component (c.color.green, format->direct.greenMask, format->direct.green)
783     | insert_component (c.color.blue , format->direct.blueMask , format->direct.blue )
784     | insert_component (alpha , format->direct.alphaMask, format->direct.alpha);
785 root 1.29
786     return true;
787     }
788 root 1.31 else
789     {
790     XRenderColor d;
791    
792 root 1.40 d.red = color.r;
793     d.green = color.g;
794     d.blue = color.b;
795 root 1.88 d.alpha = alpha;
796 root 1.31
797 root 1.147 // XftColorAlloc always returns 100% transparent pixels(!)
798 sf-exg 1.130 if (XftColorAllocValue (screen->dpy, screen->visual, screen->cmap, &d, &c))
799     return true;
800 root 1.31 }
801 root 1.28 #else
802 root 1.46 c.red = color.r;
803     c.green = color.g;
804     c.blue = color.b;
805    
806 root 1.48 if (screen->visual->c_class == TrueColor)
807 root 1.35 {
808 sf-exg 1.127 c.pixel = (color.r >> (16 - ecb_popcount32 (screen->visual->red_mask )) << ecb_ctz32 (screen->visual->red_mask ))
809     | (color.g >> (16 - ecb_popcount32 (screen->visual->green_mask)) << ecb_ctz32 (screen->visual->green_mask))
810     | (color.b >> (16 - ecb_popcount32 (screen->visual->blue_mask )) << ecb_ctz32 (screen->visual->blue_mask ));
811 root 1.1
812 root 1.35 return true;
813     }
814 root 1.50 else if (XAllocColor (screen->dpy, screen->cmap, &c))
815 root 1.46 return true;
816 root 1.38 #endif
817 root 1.1
818 root 1.146 c.pixel = (color.r * 2 + color.g * 3 + color.b) >= 0x8000 * 6
819 sf-exg 1.130 ? WhitePixelOfScreen (DefaultScreenOfDisplay (screen->dpy))
820     : BlackPixelOfScreen (DefaultScreenOfDisplay (screen->dpy));
821    
822 root 1.1 return false;
823 root 1.38 }
824    
825     bool
826     rxvt_color::set (rxvt_screen *screen, const char *name)
827     {
828 root 1.43 rgba c;
829 root 1.38 char eos;
830 root 1.39 int skip;
831 root 1.38
832 root 1.89 c.a = rgba::MAX_CC;
833    
834 root 1.46 // parse the nonstandard "[alphapercent]" prefix
835 root 1.41 if (1 <= sscanf (name, "[%hd]%n", &c.a, &skip))
836 root 1.38 {
837 root 1.43 c.a = lerp<int, int, int> (0, rgba::MAX_CC, c.a);
838 root 1.39 name += skip;
839 root 1.38 }
840 root 1.39
841 root 1.46 // parse the non-standard "rgba:rrrr/gggg/bbbb/aaaa" format
842 root 1.89 if (strlen (name) != 4+5*4 || 4 != sscanf (name, "rgba:%4hx/%4hx/%4hx/%4hx%c", &c.r, &c.g, &c.b, &c.a, &eos))
843 root 1.38 {
844 ayin 1.72 XColor xc;
845 root 1.39
846 root 1.50 if (XParseColor (screen->dpy, screen->cmap, name, &xc))
847 root 1.39 {
848     c.r = xc.red;
849     c.g = xc.green;
850     c.b = xc.blue;
851     }
852     else
853     {
854     c.r = 0xffff;
855     c.g = 0x6969;
856     c.b = 0xb4b4;
857    
858     rxvt_warn ("unable to parse color '%s', using pink instead.\n", name);
859     }
860 root 1.38 }
861    
862 root 1.39 return set (screen, c);
863 root 1.38 }
864    
865     bool
866 root 1.43 rxvt_color::set (rxvt_screen *screen, const rgba &color)
867 root 1.38 {
868 root 1.40 bool got = alloc (screen, color);
869 root 1.38
870     #if !ENABLE_MINIMAL
871     int cmap_size = screen->visual->map_entries;
872    
873     if (!got
874     && screen->visual->c_class == PseudoColor
875     && cmap_size < 4096)
876     {
877     XColor *colors = new XColor [screen->visual->map_entries];
878    
879     for (int i = 0; i < cmap_size; i++)
880     colors [i].pixel = i;
881 ayin 1.73
882 root 1.43 // many kilobytes transfer per colour, but pseudocolor isn't worth
883     // many extra optimisations.
884 root 1.50 XQueryColors (screen->dpy, screen->cmap, colors, cmap_size);
885 root 1.38
886 root 1.145 while (cmap_size)
887 root 1.38 {
888 root 1.146 int diff = 0x7fffffffL;
889 root 1.145 XColor *best = colors;
890 root 1.38
891 root 1.145 for (int i = 0; i < cmap_size; i++)
892 root 1.38 {
893 root 1.146 // simple weighted rgb distance sucks, but keeps it simple
894     int d = abs (color.r - colors [i].red ) * 2
895     + abs (color.g - colors [i].green) * 3
896     + abs (color.b - colors [i].blue );
897 root 1.145
898     if (d < diff)
899     {
900     diff = d;
901     best = colors + i;
902     }
903 root 1.38 }
904    
905 root 1.145 //rxvt_warn ("could not allocate %04x %04x %04x, getting %04x %04x %04x instead (%d,%d)\n",
906     // color.r, color.g, color.b, best->red, best->green, best->blue, diff, best - colors);
907    
908     got = alloc (screen, rgba (best->red, best->green, best->blue));
909    
910     if (got)
911     break;
912 ayin 1.73
913 root 1.145 *best = colors [--cmap_size];
914     }
915 root 1.38
916 root 1.63 delete [] colors;
917 root 1.38 }
918 root 1.28 #endif
919 root 1.38
920     return got;
921 root 1.1 }
922    
923 root 1.39 void
924 root 1.136 rxvt_color::get (rgba &color) const
925 root 1.1 {
926     #if XFT
927 root 1.146 //TODO premultiplied alpha??
928 root 1.40 color.r = c.color.red;
929     color.g = c.color.green;
930     color.b = c.color.blue;
931     color.a = c.color.alpha;
932 root 1.1 #else
933 root 1.40 color.r = c.red;
934     color.g = c.green;
935     color.b = c.blue;
936 root 1.43 color.a = rgba::MAX_CC;
937 root 1.1 #endif
938     }
939    
940 root 1.46 void
941 root 1.136 rxvt_color::get (XColor &color) const
942 root 1.46 {
943     rgba c;
944     get (c);
945    
946     color.red = c.r;
947     color.green = c.g;
948     color.blue = c.b;
949     color.pixel = (Pixel)*this;
950     }
951    
952 ayin 1.73 void
953 root 1.26 rxvt_color::free (rxvt_screen *screen)
954 root 1.1 {
955 root 1.53 if (screen->visual->c_class == TrueColor)
956     return; // nothing to do
957    
958 root 1.1 #if XFT
959 root 1.50 XftColorFree (screen->dpy, screen->visual, screen->cmap, &c);
960 root 1.1 #else
961 root 1.50 XFreeColors (screen->dpy, screen->cmap, &c.pixel, 1, AllPlanes);
962 root 1.1 #endif
963     }
964    
965 root 1.42 void
966 root 1.43 rxvt_color::fade (rxvt_screen *screen, int percent, rxvt_color &result, const rgba &to)
967 root 1.1 {
968 root 1.43 rgba c;
969 root 1.44 get (c);
970 root 1.1
971 root 1.42 result.set (
972 root 1.26 screen,
973 root 1.43 rgba (
974 root 1.42 lerp (c.r, to.r, percent),
975     lerp (c.g, to.g, percent),
976     lerp (c.b, to.b, percent),
977     lerp (c.a, to.a, percent)
978 root 1.28 )
979 root 1.18 );
980 root 1.1 }
981    
982 root 1.122 rxvt_selection::rxvt_selection (rxvt_display *disp, int selnum, Time tm, Window win, Atom prop, rxvt_term *term)
983     : display (disp), request_time (tm), request_win (win), request_prop (prop), term (term)
984 sf-exg 1.115 {
985     assert (selnum >= Sel_Primary && selnum <= Sel_Clipboard);
986    
987     timer_ev.set<rxvt_selection, &rxvt_selection::timer_cb> (this);
988     timer_ev.repeat = 10.;
989     x_ev.set<rxvt_selection, &rxvt_selection::x_cb> (this);
990    
991     incr_buf = 0;
992     incr_buf_size = incr_buf_fill = 0;
993     selection_wait = Sel_normal;
994 sf-exg 1.117 selection_type = selnum;
995 sf-exg 1.128 cb_sv = 0;
996 sf-exg 1.117 }
997    
998     void
999 root 1.120 rxvt_selection::stop ()
1000     {
1001     free (incr_buf);
1002     incr_buf = 0;
1003     timer_ev.stop ();
1004     x_ev.stop (display);
1005     }
1006    
1007     rxvt_selection::~rxvt_selection ()
1008     {
1009     stop ();
1010     }
1011    
1012     void
1013 sf-exg 1.117 rxvt_selection::run ()
1014     {
1015     int selnum = selection_type;
1016 sf-exg 1.115
1017 sf-exg 1.123 #if ENABLE_FRILLS
1018     if (selnum == Sel_Primary && display->selection_owner)
1019     {
1020     /* internal selection */
1021     char *str = rxvt_wcstombs (display->selection_owner->selection.text, display->selection_owner->selection.len);
1022     finish (str, strlen (str));
1023     free (str);
1024     return;
1025     }
1026     #endif
1027    
1028 sf-exg 1.115 #if X_HAVE_UTF8_STRING
1029     selection_type = Sel_UTF8String;
1030     if (request (display->xa[XA_UTF8_STRING], selnum))
1031     return;
1032     #else
1033     selection_type = Sel_CompoundText;
1034     if (request (display->xa[XA_COMPOUND_TEXT], selnum))
1035     return;
1036     #endif
1037    
1038     // fallback to CUT_BUFFER0 if the requested property has no owner
1039     handle_selection (display->root, XA_CUT_BUFFER0, false);
1040     }
1041    
1042     void
1043 root 1.119 rxvt_selection::finish (char *data, unsigned int len)
1044     {
1045 sf-exg 1.128 if (!cb_sv)
1046 root 1.119 {
1047     if (data)
1048     term->paste (data, len);
1049    
1050     term->selection_req = 0;
1051     delete this;
1052     }
1053     #if ENABLE_PERL
1054     else
1055     {
1056 root 1.122 stop (); // we do not really trust perl callbacks
1057     rxvt_perl.selection_finish (this, data, len);
1058 root 1.119 }
1059     #endif
1060     }
1061    
1062 sf-exg 1.115 bool
1063     rxvt_selection::request (Atom target, int selnum)
1064     {
1065     Atom sel;
1066    
1067     selection_type |= selnum;
1068    
1069     if (selnum == Sel_Primary)
1070     sel = XA_PRIMARY;
1071     else if (selnum == Sel_Secondary)
1072     sel = XA_SECONDARY;
1073     else
1074     sel = display->xa[XA_CLIPBOARD];
1075    
1076     if (XGetSelectionOwner (display->dpy, sel) != None)
1077     {
1078     XConvertSelection (display->dpy, sel, target, request_prop,
1079     request_win, request_time);
1080     x_ev.start (display, request_win);
1081     timer_ev.again ();
1082     return true;
1083     }
1084    
1085     return false;
1086     }
1087    
1088     void
1089     rxvt_selection::handle_selection (Window win, Atom prop, bool delete_prop)
1090     {
1091     Display *dpy = display->dpy;
1092     char *data = 0;
1093     unsigned int data_len = 0;
1094     unsigned long bytes_after;
1095     XTextProperty ct;
1096    
1097     // check for failed XConvertSelection
1098     if (prop == None)
1099     {
1100     bool error = true;
1101     int selnum = selection_type & Sel_whereMask;
1102    
1103     if (selection_type & Sel_CompoundText)
1104     {
1105     selection_type = 0;
1106     error = !request (XA_STRING, selnum);
1107     }
1108    
1109     if (selection_type & Sel_UTF8String)
1110     {
1111     selection_type = Sel_CompoundText;
1112     error = !request (display->xa[XA_COMPOUND_TEXT], selnum);
1113     }
1114    
1115     if (error)
1116     {
1117     ct.value = 0;
1118     goto bailout;
1119     }
1120    
1121     return;
1122     }
1123    
1124     // length == (2^31 - 1) / 4, as gdk
1125     if (XGetWindowProperty (dpy, win, prop,
1126     0, 0x1fffffff,
1127     delete_prop, AnyPropertyType,
1128     &ct.encoding, &ct.format,
1129     &ct.nitems, &bytes_after,
1130     &ct.value) != Success)
1131     {
1132     ct.value = 0;
1133     goto bailout;
1134     }
1135    
1136     if (ct.encoding == None)
1137     goto bailout;
1138    
1139     if (ct.value == 0)
1140     goto bailout;
1141    
1142     if (ct.encoding == display->xa[XA_INCR])
1143     {
1144     // INCR selection, start handshake
1145     if (!delete_prop)
1146     XDeleteProperty (dpy, win, prop);
1147    
1148     selection_wait = Sel_incr;
1149     timer_ev.again ();
1150    
1151     goto bailout;
1152     }
1153    
1154     if (ct.nitems == 0)
1155     {
1156     if (selection_wait == Sel_incr)
1157     {
1158     XFree (ct.value);
1159    
1160     // finally complete, now paste the whole thing
1161     selection_wait = Sel_normal;
1162     ct.value = (unsigned char *)incr_buf;
1163     ct.nitems = incr_buf_fill;
1164     incr_buf = 0;
1165     timer_ev.stop ();
1166     }
1167     else
1168     {
1169     // avoid recursion
1170     if (win != display->root || prop != XA_CUT_BUFFER0)
1171     {
1172     XFree (ct.value);
1173    
1174     // fallback to CUT_BUFFER0 if the requested property
1175     // has an owner but is empty
1176     handle_selection (display->root, XA_CUT_BUFFER0, False);
1177     return;
1178     }
1179    
1180     goto bailout;
1181     }
1182     }
1183     else if (selection_wait == Sel_incr)
1184     {
1185     timer_ev.again ();
1186    
1187     while (incr_buf_fill + ct.nitems > incr_buf_size)
1188     {
1189     incr_buf_size = incr_buf_size ? incr_buf_size * 2 : 128*1024;
1190     incr_buf = (char *)rxvt_realloc (incr_buf, incr_buf_size);
1191     }
1192    
1193     memcpy (incr_buf + incr_buf_fill, ct.value, ct.nitems);
1194     incr_buf_fill += ct.nitems;
1195    
1196     goto bailout;
1197     }
1198    
1199     char **cl;
1200     int cr;
1201    
1202     // we honour the first item only
1203    
1204     #if !ENABLE_MINIMAL
1205     // xlib is horribly broken with respect to UTF8_STRING, and nobody cares to fix it
1206     // so recode it manually
1207     if (ct.encoding == display->xa[XA_UTF8_STRING])
1208     {
1209     wchar_t *w = rxvt_utf8towcs ((const char *)ct.value, ct.nitems);
1210     data = rxvt_wcstombs (w);
1211     free (w);
1212     }
1213     else
1214     #endif
1215     if (XmbTextPropertyToTextList (dpy, &ct, &cl, &cr) >= 0
1216     && cl)
1217     {
1218     data = strdup (cl[0]);
1219     XFreeStringList (cl);
1220     }
1221     else
1222     {
1223     // paste raw
1224     data = strdup ((const char *)ct.value);
1225     }
1226    
1227     data_len = strlen (data);
1228    
1229     bailout:
1230     XFree (ct.value);
1231    
1232     if (selection_wait == Sel_normal)
1233     {
1234 root 1.119 finish (data, data_len);
1235 sf-exg 1.115 free (data);
1236     }
1237     }
1238    
1239     void
1240     rxvt_selection::timer_cb (ev::timer &w, int revents)
1241     {
1242     if (selection_wait == Sel_incr)
1243     rxvt_warn ("data loss: timeout on INCR selection paste, ignoring.\n");
1244    
1245 root 1.119 finish ();
1246 sf-exg 1.115 }
1247    
1248     void
1249     rxvt_selection::x_cb (XEvent &xev)
1250     {
1251     switch (xev.type)
1252     {
1253     case PropertyNotify:
1254     if (selection_wait == Sel_incr
1255     && xev.xproperty.atom == request_prop
1256     && xev.xproperty.state == PropertyNewValue)
1257     handle_selection (xev.xproperty.window, xev.xproperty.atom, true);
1258     break;
1259    
1260     case SelectionNotify:
1261     if (selection_wait == Sel_normal
1262 sf-exg 1.126 && xev.xselection.time == request_time)
1263 sf-exg 1.115 {
1264     timer_ev.stop ();
1265     handle_selection (xev.xselection.requestor, xev.xselection.property, true);
1266     }
1267     break;
1268     }
1269     }