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.24 by root, Wed Jan 25 00:42:21 2006 UTC vs.
Revision 1.148 by root, Wed Dec 17 14:25:01 2014 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#include <sys/utsname.h> 33#include <sys/utsname.h>
31
32#ifndef NO_SLOW_LINK_SUPPORT
33# include <sys/socket.h> 34#include <sys/socket.h>
34# 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",
35#endif 94# endif
95#endif
96};
97
98/////////////////////////////////////////////////////////////////////////////
36 99
37refcounted::refcounted (const char *id) 100refcounted::refcounted (const char *id)
38{ 101{
39 this->id = strdup (id); 102 this->id = strdup (id);
40} 103}
92 put (*this->begin ()); 155 put (*this->begin ());
93} 156}
94 157
95///////////////////////////////////////////////////////////////////////////// 158/////////////////////////////////////////////////////////////////////////////
96 159
97#ifdef USE_XIM 160#if USE_XIM
98 161
99static void 162static void
100#if XIMCB_PROTO_BROKEN 163#if XIMCB_PROTO_BROKEN
101im_destroy_cb (XIC unused1, XPointer client_data, XPointer unused3) 164im_destroy_cb (XIC unused1, XPointer client_data, XPointer unused3)
102#else 165#else
115bool 178bool
116rxvt_xim::ref_init () 179rxvt_xim::ref_init ()
117{ 180{
118 display = GET_R->display; //HACK: TODO 181 display = GET_R->display; //HACK: TODO
119 182
120 xim = XOpenIM (display->display, NULL, NULL, NULL); 183 xim = XOpenIM (display->dpy, 0, 0, 0);
121 184
122 if (!xim) 185 if (!xim)
123 return false; 186 return false;
124 187
125 XIMCallback ximcallback; 188 XIMCallback ximcallback;
126 ximcallback.client_data = (XPointer)this; 189 ximcallback.client_data = (XPointer)this;
127 ximcallback.callback = im_destroy_cb; 190 ximcallback.callback = im_destroy_cb;
128 191
129 XSetIMValues (xim, XNDestroyCallback, &ximcallback, NULL); 192 XSetIMValues (xim, XNDestroyCallback, &ximcallback, (char *)0);
130 193
131 return true; 194 return true;
132} 195}
133 196
134rxvt_xim::~rxvt_xim () 197rxvt_xim::~rxvt_xim ()
139 202
140#endif 203#endif
141 204
142///////////////////////////////////////////////////////////////////////////// 205/////////////////////////////////////////////////////////////////////////////
143 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}
316
317/////////////////////////////////////////////////////////////////////////////
318
144rxvt_display::rxvt_display (const char *id) 319rxvt_display::rxvt_display (const char *id)
145: refcounted (id) 320: refcounted (id)
146, x_ev (this, &rxvt_display::x_cb)
147, selection_owner (0) 321, selection_owner (0)
322, clipboard_owner (0)
148{ 323{
324 x_ev .set<rxvt_display, &rxvt_display::x_cb > (this);
325 flush_ev.set<rxvt_display, &rxvt_display::flush_cb> (this);
149} 326}
150 327
151XrmDatabase 328XrmDatabase
152rxvt_display::get_resources () 329rxvt_display::get_resources (bool refresh)
153{ 330{
154 char *homedir = (char *)getenv ("HOME"); 331 char *homedir = getenv ("HOME");
155 char fname[1024]; 332 char fname[1024];
156 333
157 /* 334 /*
158 * get resources using the X library function 335 * get resources using the X library function
159 */ 336 */
160 char *displayResource, *xe; 337 char *displayResource, *xe;
161 XrmDatabase database, rdb1; 338 XrmDatabase rdb1, database = 0;
162 339
163 database = NULL; 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
164 344
165 // for ordering, see for example http://www.faqs.org/faqs/Xt-FAQ/ Subject: 20 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
166 347
167 // 6. System wide per application default file. 348 // 6. System wide per application default file.
168 349
169 /* Add in $XAPPLRESDIR/Rxvt only; not bothering with XUSERFILESEARCHPATH */ 350 /* Add in $XAPPLRESDIR/Rxvt only; not bothering with XUSERFILESEARCHPATH */
170 if ((xe = (char *)getenv ("XAPPLRESDIR"))) 351 if ((xe = getenv ("XAPPLRESDIR")))
171 { 352 {
172 snprintf (fname, sizeof (fname), "%s/%s", xe, RESCLASS); 353 snprintf (fname, sizeof (fname), "%s/%s", xe, RESCLASS);
173 354
174 if ((rdb1 = XrmGetFileDatabase (fname))) 355 if ((rdb1 = XrmGetFileDatabase (fname)))
175 XrmMergeDatabases (rdb1, &database); 356 XrmMergeDatabases (rdb1, &database);
177 358
178 // 5. User's per application default file. 359 // 5. User's per application default file.
179 // none 360 // none
180 361
181 // 4. User's defaults file. 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
182 /* Get any Xserver defaults */ 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
183 displayResource = XResourceManagerString (display); 406 displayResource = XResourceManagerString (dpy);
184 407
185 if (displayResource != NULL) 408 if (displayResource)
186 { 409 {
187 if ((rdb1 = XrmGetStringDatabase (displayResource))) 410 if ((rdb1 = XrmGetStringDatabase (displayResource)))
188 XrmMergeDatabases (rdb1, &database); 411 XrmMergeDatabases (rdb1, &database);
189 } 412 }
190 else if (homedir)
191 {
192 snprintf (fname, sizeof (fname), "%s/.Xdefaults", homedir);
193 413
194 if ((rdb1 = XrmGetFileDatabase (fname))) 414#if !XLIB_ILLEGAL_ACCESS
195 XrmMergeDatabases (rdb1, &database); 415 if (refresh && displayResource)
196 } 416 XFree (displayResource);
417#endif
197 418
198 /* Get screen specific resources */ 419 /* Get screen specific resources */
199 displayResource = XScreenResourceString (ScreenOfDisplay (display, screen)); 420 displayResource = XScreenResourceString (ScreenOfDisplay (dpy, screen));
200 421
201 if (displayResource != NULL) 422 if (displayResource)
202 { 423 {
203 if ((rdb1 = XrmGetStringDatabase (displayResource))) 424 if ((rdb1 = XrmGetStringDatabase (displayResource)))
204 /* Merge with screen-independent resources */ 425 /* Merge with screen-independent resources */
205 XrmMergeDatabases (rdb1, &database); 426 XrmMergeDatabases (rdb1, &database);
206 427
207 XFree (displayResource); 428 XFree (displayResource);
208 } 429 }
209 430
210 // 3. User's per host defaults file 431 // 3. User's per host defaults file
211 /* Add in XENVIRONMENT file */ 432 /* Add in XENVIRONMENT file */
212 if ((xe = (char *)getenv ("XENVIRONMENT")) 433 if ((xe = getenv ("XENVIRONMENT"))
213 && (rdb1 = XrmGetFileDatabase (xe))) 434 && (rdb1 = XrmGetFileDatabase (xe)))
214 XrmMergeDatabases (rdb1, &database); 435 XrmMergeDatabases (rdb1, &database);
215 else if (homedir) 436 else if (homedir)
216 { 437 {
217 struct utsname un; 438 struct utsname un;
231bool rxvt_display::ref_init () 452bool rxvt_display::ref_init ()
232{ 453{
233#ifdef LOCAL_X_IS_UNIX 454#ifdef LOCAL_X_IS_UNIX
234 if (id[0] == ':') 455 if (id[0] == ':')
235 { 456 {
236 val = rxvt_malloc (5 + strlen (id) + 1); 457 char *val = rxvt_temp_buf<char> (5 + strlen (id) + 1);
458
237 strcpy (val, "unix/"); 459 strcpy (val, "unix/");
238 strcat (val, id); 460 strcat (val, id);
461
239 display = XOpenDisplay (val); 462 dpy = XOpenDisplay (val);
240 free (val);
241 } 463 }
242 else 464 else
243#endif 465#endif
244 display = 0; 466 dpy = 0;
245 467
246 if (!display) 468 if (!dpy)
247 display = XOpenDisplay (id); 469 dpy = XOpenDisplay (id);
248 470
249 if (!display) 471 if (!dpy)
250 return false; 472 return false;
251 473
252 screen = DefaultScreen (display); 474 screen = DefaultScreen (dpy);
253 root = DefaultRootWindow (display); 475 root = DefaultRootWindow (dpy);
254 visual = DefaultVisual (display, screen);
255 cmap = DefaultColormap (display, screen);
256 depth = DefaultDepth (display, screen);
257 476
477 assert (ecb_array_length (xa_names) == NUM_XA);
478 XInternAtoms (dpy, (char **)xa_names, NUM_XA, False, xa);
479
258 XrmSetDatabase (display, get_resources ()); 480 XrmSetDatabase (dpy, get_resources (false));
259 481
260#ifdef POINTER_BLANK 482#ifdef POINTER_BLANK
261 XColor blackcolour; 483 XColor blackcolour;
262 blackcolour.red = 0; 484 blackcolour.red = 0;
263 blackcolour.green = 0; 485 blackcolour.green = 0;
264 blackcolour.blue = 0; 486 blackcolour.blue = 0;
265 Font f = XLoadFont (display, "fixed"); 487 Font f = XLoadFont (dpy, "fixed");
266 blank_cursor = XCreateGlyphCursor (display, f, f, ' ', ' ', 488 blank_cursor = XCreateGlyphCursor (dpy, f, f, ' ', ' ',
267 &blackcolour, &blackcolour); 489 &blackcolour, &blackcolour);
268 XUnloadFont (display, f); 490 XUnloadFont (dpy, f);
269#endif 491#endif
270 492
271#ifdef PREFER_24BIT 493 flags = 0;
272 /* 494#if XRENDER
273 * If depth is not 24, look for a 24bit visual. 495 int major, minor;
274 */ 496 if (XRenderQueryVersion (dpy, &major, &minor))
275 if (depth != 24) 497 if (major > 0 || (major == 0 && minor >= 11))
276 { 498 {
277 XVisualInfo vinfo; 499 flags |= DISPLAY_HAS_RENDER;
278 500
279 if (XMatchVisualInfo (display, screen, 24, TrueColor, &vinfo)) 501 if (XFilters *filters = XRenderQueryFilters (dpy, root))
280 { 502 {
281 depth = 24; 503 for (int i = 0; i < filters->nfilter; i++)
282 visual = vinfo.visual; 504 if (!strcmp (filters->filter [i], FilterConvolution))
283 cmap = XCreateColormap (display, 505 flags |= DISPLAY_HAS_RENDER_CONV;
284 RootWindow (display, screen), 506
285 visual, AllocNone); 507 XFree (filters);
286 } 508 }
287 } 509 }
288#endif 510#endif
289 511
290 int fd = XConnectionNumber (display); 512 int fd = XConnectionNumber (dpy);
291 513
292#ifndef NO_SLOW_LINK_SUPPORT
293 // try to detect wether we have a local connection. 514 // try to detect whether we have a local connection.
294 // assume unix domains socket == local, everything else not 515 // assume unix domain socket == local, everything else not
295 // TODO: might want to check for inet/127.0.0.1 516 // TODO: might want to check for inet/127.0.0.1
296 is_local = 0; 517 is_local = 0;
297 sockaddr_un sa; 518 sockaddr_un sa;
298 socklen_t sl = sizeof (sa); 519 socklen_t sl = sizeof (sa);
299 520
300 if (!getsockname (fd, (sockaddr *)&sa, &sl)) 521 if (!getsockname (fd, (sockaddr *)&sa, &sl))
301 is_local = sa.sun_family == AF_LOCAL; 522 is_local = sa.sun_family == AF_UNIX;
302#endif
303 523
524 flush_ev.start ();
304 x_ev.start (fd, EVENT_READ); 525 x_ev.start (fd, ev::READ);
305 fcntl (fd, F_SETFD, FD_CLOEXEC); 526 fcntl (fd, F_SETFD, FD_CLOEXEC);
306 527
307 XSelectInput (display, root, PropertyChangeMask); 528 XSelectInput (dpy, root, PropertyChangeMask);
308#ifdef USE_XIM
309 xa_xim_servers = XInternAtom (display, "XIM_SERVERS", 0);
310#endif
311 529
312 flush (); 530 flush ();
313 531
314 return true; 532 return true;
315} 533}
316 534
317void 535void
318rxvt_display::ref_next () 536rxvt_display::ref_next ()
319{ 537{
320 // TODO: somehow check wether the database files/resources changed 538 // TODO: somehow check whether the database files/resources changed
321 // before re-loading/parsing 539 // before affording re-loading/parsing
322 XrmDestroyDatabase (XrmGetDatabase (display)); 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
323 XrmSetDatabase (display, get_resources ()); 545 XrmSetDatabase (dpy, get_resources (true));
324} 546}
325 547
326rxvt_display::~rxvt_display () 548rxvt_display::~rxvt_display ()
327{ 549{
328 if (!display) 550 if (!dpy)
329 return; 551 return;
330 552
331#ifdef POINTER_BLANK 553#ifdef POINTER_BLANK
332 XFreeCursor (display, blank_cursor); 554 XFreeCursor (dpy, blank_cursor);
333#endif 555#endif
334 x_ev.stop (); 556 x_ev.stop ();
557 flush_ev.stop ();
335#ifdef USE_XIM 558#if USE_XIM
336 xims.clear (); 559 xims.clear ();
337#endif 560#endif
561 XrmDestroyDatabase (XrmGetDatabase (dpy));
338 XCloseDisplay (display); 562 XCloseDisplay (dpy);
339} 563}
340 564
341#ifdef USE_XIM 565#if USE_XIM
342void rxvt_display::im_change_cb () 566void rxvt_display::im_change_cb ()
343{ 567{
344 for (im_watcher **i = imw.begin (); i != imw.end (); ++i) 568 for (im_watcher **i = imw.begin (); i != imw.end (); ++i)
345 (*i)->call (); 569 (*i)->call ();
346} 570}
351 // registers, as xlib crashes due to a race otherwise. 575 // registers, as xlib crashes due to a race otherwise.
352 Atom actual_type, *atoms; 576 Atom actual_type, *atoms;
353 int actual_format; 577 int actual_format;
354 unsigned long nitems, bytes_after; 578 unsigned long nitems, bytes_after;
355 579
356 if (XGetWindowProperty (display, root, xa_xim_servers, 0L, 1000000L, 580 if (XGetWindowProperty (dpy, root, xa[XA_XIM_SERVERS], 0L, 1000000L,
357 False, XA_ATOM, &actual_type, &actual_format, 581 False, XA_ATOM, &actual_type, &actual_format,
358 &nitems, &bytes_after, (unsigned char **)&atoms) 582 &nitems, &bytes_after, (unsigned char **)&atoms)
359 != Success ) 583 != Success)
360 return; 584 return;
361 585
362 if (actual_type == XA_ATOM && actual_format == 32) 586 if (actual_type == XA_ATOM && actual_format == 32)
363 for (int i = 0; i < nitems; i++) 587 for (int i = 0; i < nitems; i++)
364 if (XGetSelectionOwner (display, atoms[i])) 588 if (XGetSelectionOwner (dpy, atoms[i]))
365 { 589 {
366 im_change_cb (); 590 im_change_cb ();
367 break; 591 break;
368 } 592 }
369 593
370 XFree (atoms); 594 XFree (atoms);
371} 595}
372#endif 596#endif
373 597
374void rxvt_display::x_cb (io_watcher &w, short revents) 598void rxvt_display::x_cb (ev::io &w, int revents)
375{ 599{
600 flush_ev.start ();
601}
602
603void rxvt_display::flush_cb (ev::prepare &w, int revents)
604{
605 while (XEventsQueued (dpy, QueuedAfterFlush))
376 do 606 do
377 { 607 {
378 XEvent xev; 608 XEvent xev;
379 XNextEvent (display, &xev); 609 XNextEvent (dpy, &xev);
380 610
381#ifdef USE_XIM 611#if USE_XIM
382 if (!XFilterEvent (&xev, None)) 612 if (!XFilterEvent (&xev, None))
383 { 613 {
384 if (xev.type == PropertyNotify 614 if (xev.type == PropertyNotify
385 && xev.xany.window == root 615 && xev.xany.window == root
386 && xev.xproperty.atom == xa_xim_servers) 616 && xev.xproperty.atom == xa[XA_XIM_SERVERS])
387 im_change_check (); 617 im_change_check ();
388#endif 618#endif
619 if (xev.type == MappingNotify)
620 XRefreshKeyboardMapping (&xev.xmapping);
621
389 for (int i = xw.size (); i--; ) 622 for (int i = xw.size (); i--; )
390 { 623 {
391 if (!xw[i]) 624 if (!xw[i])
392 xw.erase_unordered (i); 625 xw.erase_unordered (i);
393 else if (xw[i]->window == xev.xany.window) 626 else if (xw[i]->window == xev.xany.window)
394 xw[i]->call (xev); 627 xw[i]->call (xev);
395 } 628 }
396#ifdef USE_XIM 629#if USE_XIM
397 } 630 }
398#endif 631#endif
399 } 632 }
400 while (XEventsQueued (display, QueuedAlready)); 633 while (XEventsQueued (dpy, QueuedAlready));
401 634
402 XFlush (display); 635 w.stop ();
403}
404
405void rxvt_display::flush ()
406{
407 if (XEventsQueued (display, QueuedAlready))
408 x_cb (x_ev, EVENT_READ);
409
410 XFlush (display);
411} 636}
412 637
413void rxvt_display::reg (xevent_watcher *w) 638void rxvt_display::reg (xevent_watcher *w)
414{ 639{
640 if (!w->active)
641 {
415 xw.push_back (w); 642 xw.push_back (w);
416 w->active = xw.size (); 643 w->active = xw.size ();
644 }
417} 645}
418 646
419void rxvt_display::unreg (xevent_watcher *w) 647void rxvt_display::unreg (xevent_watcher *w)
420{ 648{
421 if (w->active) 649 if (w->active)
650 {
422 xw[w->active - 1] = 0; 651 xw[w->active - 1] = 0;
652 w->active = 0;
653 }
423} 654}
424 655
425void rxvt_display::set_selection_owner (rxvt_term *owner) 656void rxvt_display::set_selection_owner (rxvt_term *owner, bool clipboard)
426{ 657{
658 rxvt_term * &cur_owner = !clipboard ? selection_owner : clipboard_owner;
659
427 if (selection_owner && selection_owner != owner) 660 if (cur_owner && cur_owner != owner)
428 selection_owner->selection_clear (); 661 {
662 rxvt_term *term = cur_owner;
663 term->selection_clear (clipboard);
664 term->flush ();
665 }
429 666
430 selection_owner = owner; 667 cur_owner = owner;
431} 668}
432 669
433#ifdef USE_XIM 670#if USE_XIM
671
434void rxvt_display::reg (im_watcher *w) 672void rxvt_display::reg (im_watcher *w)
435{ 673{
436 imw.push_back (w); 674 imw.push_back (w);
437} 675}
438 676
447 int l, m; 685 int l, m;
448 686
449 l = strlen (locale); 687 l = strlen (locale);
450 m = strlen (modifiers); 688 m = strlen (modifiers);
451 689
452 if (!(id = (char *)malloc (l + m + 2))) 690 if (!(id = rxvt_temp_buf<char> (l + m + 2)))
453 return 0; 691 return 0;
454 692
455 memcpy (id, locale, l); id[l] = '\n'; 693 memcpy (id, locale, l); id[l] = '\n';
456 memcpy (id + l + 1, modifiers, m); id[l + m + 1] = 0; 694 memcpy (id + l + 1, modifiers, m); id[l + m + 1] = 0;
457 695
458 rxvt_xim *xim = xims.get (id); 696 rxvt_xim *xim = xims.get (id);
459 697
460 free (id);
461
462 return xim; 698 return xim;
463} 699}
464 700
465void rxvt_display::put_xim (rxvt_xim *xim) 701void rxvt_display::put_xim (rxvt_xim *xim)
466{ 702{
467#if XLIB_IS_RACEFREE 703# if XLIB_IS_RACEFREE
468 xims.put (xim); 704 xims.put (xim);
469#endif 705# endif
470} 706}
707
471#endif 708#endif
472 709
473Atom rxvt_display::atom (const char *name) 710Atom rxvt_display::atom (const char *name)
474{ 711{
475 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;
476} 735}
477 736
478///////////////////////////////////////////////////////////////////////////// 737/////////////////////////////////////////////////////////////////////////////
479 738
480template class refcache<rxvt_display>; 739template class refcache<rxvt_display>;
481refcache<rxvt_display> displays; 740refcache<rxvt_display> displays;
482 741
483///////////////////////////////////////////////////////////////////////////// 742/////////////////////////////////////////////////////////////////////////////
484 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
485bool 751bool
486rxvt_color::set (rxvt_display *display, Pixel p) 752rxvt_color::alloc (rxvt_screen *screen, const rgba &color)
487{ 753{
754 //TODO: only supports 24 bit
755 unsigned int alpha = color.a >= 0xff00 ? 0xffff : color.a;
756
488#if XFT 757#if XFT
489 XColor xc; 758 XRenderPictFormat *format;
490 759
491 xc.pixel = p; 760 // not needed by XftColorAlloc, but by the other paths (ours
492 if (!XQueryColor (display->display, display->cmap, &xc)) 761 // and fallback), so just set all components here.
493 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;
494 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 {
495 XRenderColor d; 790 XRenderColor d;
496 791
497 d.red = xc.red; 792 d.red = color.r;
498 d.green = xc.green; 793 d.green = color.g;
499 d.blue = xc.blue; 794 d.blue = color.b;
500 d.alpha = 0xffff; 795 d.alpha = alpha;
501 796
502 return 797 // XftColorAlloc always returns 100% transparent pixels(!)
503 XftColorAllocValue (display->display, 798 if (XftColorAllocValue (screen->dpy, screen->visual, screen->cmap, &d, &c))
504 display->visual, 799 return true;
505 display->cmap, 800 }
506 &d, &c);
507#else 801#else
508 this->p = p; 802 c.red = color.r;
509#endif 803 c.green = color.g;
804 c.blue = color.b;
510 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))
511 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;
512} 823}
513 824
514bool 825bool
515rxvt_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;
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
516{ 925{
517#if XFT 926#if XFT
518 return XftColorAllocName (display->display, display->visual, display->cmap, 927 //TODO premultiplied alpha??
519 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;
520#else 932#else
521 XColor xc; 933 color.r = c.red;
934 color.g = c.green;
935 color.b = c.blue;
936 color.a = rgba::MAX_CC;
937#endif
938}
522 939
523 if (XParseColor (display->display, display->cmap, name, &xc)) 940void
524 return set (display, xc.red, xc.green, xc.blue); 941rxvt_color::get (XColor &color) const
942{
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
952void
953rxvt_color::free (rxvt_screen *screen)
954{
955 if (screen->visual->c_class == TrueColor)
956 return; // nothing to do
957
958#if XFT
959 XftColorFree (screen->dpy, screen->visual, screen->cmap, &c);
960#else
961 XFreeColors (screen->dpy, screen->cmap, &c.pixel, 1, AllPlanes);
962#endif
963}
964
965void
966rxvt_color::fade (rxvt_screen *screen, int percent, rxvt_color &result, const rgba &to)
967{
968 rgba c;
969 get (c);
970
971 result.set (
972 screen,
973 rgba (
974 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 )
979 );
980}
981
982rxvt_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{
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 selection_type = selnum;
995 cb_sv = 0;
996}
997
998void
999rxvt_selection::stop ()
1000{
1001 free (incr_buf);
1002 incr_buf = 0;
1003 timer_ev.stop ();
1004 x_ev.stop (display);
1005}
1006
1007rxvt_selection::~rxvt_selection ()
1008{
1009 stop ();
1010}
1011
1012void
1013rxvt_selection::run ()
1014{
1015 int selnum = selection_type;
1016
1017#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#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
1042void
1043rxvt_selection::finish (char *data, unsigned int len)
1044{
1045 if (!cb_sv)
1046 {
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 stop (); // we do not really trust perl callbacks
1057 rxvt_perl.selection_finish (this, data, len);
1058 }
1059#endif
1060}
1061
1062bool
1063rxvt_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 }
525 1084
526 return false; 1085 return false;
527#endif
528} 1086}
529 1087
530bool
531rxvt_color::set (rxvt_display *display, unsigned short cr, unsigned short cg, unsigned short cb)
532{
533 XColor xc;
534
535 xc.red = cr;
536 xc.green = cg;
537 xc.blue = cb;
538 xc.flags = DoRed | DoGreen | DoBlue;
539
540 if (XAllocColor (display->display, display->cmap, &xc))
541 return set (display, xc.pixel);
542
543 return false;
544}
545
546void 1088void
547rxvt_color::get (rxvt_display *display, unsigned short &cr, unsigned short &cg, unsigned short &cb) 1089rxvt_selection::handle_selection (Window win, Atom prop, bool delete_prop)
548{ 1090{
549#if XFT 1091 Display *dpy = display->dpy;
550 cr = c.color.red; 1092 char *data = 0;
551 cg = c.color.green; 1093 unsigned int data_len = 0;
552 cb = c.color.blue; 1094 unsigned long bytes_after;
553#else 1095 XTextProperty ct;
554 XColor c;
555 1096
556 c.pixel = p; 1097 // check for failed XConvertSelection
557 XQueryColor (display->display, display->cmap, &c); 1098 if (prop == None)
1099 {
1100 bool error = true;
1101 int selnum = selection_type & Sel_whereMask;
558 1102
559 cr = c.red; 1103 if (selection_type & Sel_CompoundText)
560 cg = c.green; 1104 {
561 cb = c.blue; 1105 selection_type = 0;
562#endif 1106 error = !request (XA_STRING, selnum);
563} 1107 }
564 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
1229bailout:
1230 XFree (ct.value);
1231
1232 if (selection_wait == Sel_normal)
1233 {
1234 finish (data, data_len);
1235 free (data);
1236 }
1237}
1238
565void 1239void
566rxvt_color::free (rxvt_display *display) 1240rxvt_selection::timer_cb (ev::timer &w, int revents)
567{ 1241{
568#if XFT 1242 if (selection_wait == Sel_incr)
569 XftColorFree (display->display, display->visual, display->cmap, &c); 1243 rxvt_warn ("data loss: timeout on INCR selection paste, ignoring.\n");
570#else
571 XFreeColors (display->display, display->cmap, &p, 1, AllPlanes);
572#endif
573}
574 1244
575rxvt_color 1245 finish ();
576rxvt_color::fade (rxvt_display *display, int percent)
577{
578 percent = 100 - percent;
579
580 unsigned short cr, cg, cb;
581 rxvt_color faded;
582
583 get (display, cr, cg, cb);
584
585 faded.set (
586 display,
587 cr * percent / 100,
588 cg * percent / 100,
589 cb * percent / 100
590 );
591
592 return faded;
593} 1246}
594 1247
595#define LERP(a,b,p) (a * p + b * (100 - p)) / 100 1248void
596 1249rxvt_selection::x_cb (XEvent &xev)
597rxvt_color
598rxvt_color::fade (rxvt_display *display, int percent, rxvt_color &fadeto)
599{ 1250{
600 percent = 100 - percent; 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;
601 1259
602 unsigned short cr, cg, cb; 1260 case SelectionNotify:
603 unsigned short fcr, fcg, fcb; 1261 if (selection_wait == Sel_normal
604 rxvt_color faded; 1262 && xev.xselection.time == request_time)
605 1263 {
606 get (display, cr, cg, cb); 1264 timer_ev.stop ();
607 fadeto.get(display, fcr, fcg, fcb); 1265 handle_selection (xev.xselection.requestor, xev.xselection.property, true);
608 1266 }
609 faded.set ( 1267 break;
610 display, 1268 }
611 LERP (cr, fcr, percent),
612 LERP (cg, fcg, percent),
613 LERP (cb, fcb, percent)
614 );
615
616 return faded;
617} 1269}
618

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines