ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/rxvt-unicode/src/rxvttoolkit.C
(Generate patch)

Comparing rxvt-unicode/src/rxvttoolkit.C (file contents):
Revision 1.8 by root, Wed Dec 15 05:30:40 2004 UTC vs.
Revision 1.150 by sf-exg, Sat Sep 12 13:47:58 2015 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines