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.12 by root, Mon Jan 17 00:59:24 2005 UTC vs.
Revision 1.27 by root, Wed Jan 25 21:09:21 2006 UTC

25#include <rxvttoolkit.h> 25#include <rxvttoolkit.h>
26 26
27#include <unistd.h> 27#include <unistd.h>
28#include <fcntl.h> 28#include <fcntl.h>
29 29
30#include <sys/utsname.h>
31
30#ifndef NO_SLOW_LINK_SUPPORT 32#ifndef NO_SLOW_LINK_SUPPORT
31# include <sys/socket.h> 33# include <sys/socket.h>
32# include <sys/un.h> 34# include <sys/un.h>
33#endif 35#endif
34 36
37const char *const xa_names[] =
38 {
39 "TEXT",
40 "COMPOUND_TEXT",
41 "UTF8_STRING",
42 "MULTIPLE",
43 "TARGETS",
44 "TIMESTAMP",
45 "VT_SELECTION",
46 "INCR",
47 "WM_PROTOCOLS",
48 "WM_DELETE_WINDOW",
49 "CLIPBOARD",
50#if ENABLE_FRILLS
51 "_MOTIF_WM_HINTS",
52#endif
53#if ENABLE_EWMH
54 "_NET_WM_PID",
55 "_NET_WM_NAME",
56 "_NET_WM_ICON_NAME",
57 "_NET_WM_PING",
58#endif
59#if USE_XIM
60 "WM_LOCALE_NAME",
61 "XIM_SERVERS",
62#endif
63#ifdef TRANSPARENT
64 "_XROOTPMAP_ID",
65 "ESETROOT_PMAP_ID",
66#endif
67#if ENABLE_XEMBED
68 "_XEMBED",
69 "_XEMBED_INFO",
70#endif
71 };
72
73/////////////////////////////////////////////////////////////////////////////
74
35refcounted::refcounted (const char *id) 75refcounted::refcounted (const char *id)
36{ 76{
37 this->id = strdup (id); 77 this->id = strdup (id);
38} 78}
39 79
47{ 87{
48 for (T **i = this->begin (); i < this->end (); ++i) 88 for (T **i = this->begin (); i < this->end (); ++i)
49 { 89 {
50 if (!strcmp (id, (*i)->id)) 90 if (!strcmp (id, (*i)->id))
51 { 91 {
52 (*i)->referenced++; 92 ++(*i)->referenced;
93 (*i)->ref_next ();
53 return *i; 94 return *i;
54 } 95 }
55 } 96 }
56 97
57 T *obj = new T (id); 98 T *obj = new T (id);
58 99
59 obj->referenced = 1;
60
61 if (obj && obj->init ()) 100 if (obj && obj->ref_init ())
62 { 101 {
102 obj->referenced = 1;
63 this->push_back (obj); 103 this->push_back (obj);
64 return obj; 104 return obj;
65 } 105 }
66 else 106 else
67 { 107 {
91} 131}
92 132
93///////////////////////////////////////////////////////////////////////////// 133/////////////////////////////////////////////////////////////////////////////
94 134
95#ifdef USE_XIM 135#ifdef USE_XIM
136
96static void 137static void
97#if XIMCB_PROTO_BROKEN 138#if XIMCB_PROTO_BROKEN
98im_destroy_cb (XIC unused1, XPointer client_data, XPointer unused3) 139im_destroy_cb (XIC unused1, XPointer client_data, XPointer unused3)
99#else 140#else
100im_destroy_cb (XIM unused1, XPointer client_data, XPointer unused3) 141im_destroy_cb (XIM unused1, XPointer client_data, XPointer unused3)
107 148
108 display->xims.erase (find (display->xims.begin (), display->xims.end (), xim)); 149 display->xims.erase (find (display->xims.begin (), display->xims.end (), xim));
109 display->im_change_cb (); 150 display->im_change_cb ();
110} 151}
111 152
153bool
112bool rxvt_xim::init () 154rxvt_xim::ref_init ()
113{ 155{
114 display = GET_R->display; //HACK: TODO 156 display = GET_R->display; //HACK: TODO
115 157
116 xim = XOpenIM (display->display, NULL, NULL, NULL); 158 xim = XOpenIM (display->display, NULL, NULL, NULL);
117 159
130rxvt_xim::~rxvt_xim () 172rxvt_xim::~rxvt_xim ()
131{ 173{
132 if (xim) 174 if (xim)
133 XCloseIM (xim); 175 XCloseIM (xim);
134} 176}
177
135#endif 178#endif
179
180/////////////////////////////////////////////////////////////////////////////
181
182void
183rxvt_screen::set (rxvt_display *disp)
184{
185 display = disp;
186 xdisp = disp->display;
187
188 Screen *screen = ScreenOfDisplay (xdisp, disp->screen);
189
190 depth = DefaultDepthOfScreen (screen);
191 visual = DefaultVisualOfScreen (screen);
192 cmap = DefaultColormapOfScreen (screen);
193}
194
195void
196rxvt_screen::set (rxvt_display *disp, int depth)
197{
198 set (disp);
199
200 XVisualInfo vinfo;
201
202 if (XMatchVisualInfo (xdisp, display->screen, depth, TrueColor, &vinfo))
203 {
204 this->depth = depth;
205 this->visual = vinfo.visual;
206 this->cmap = XCreateColormap (xdisp, disp->root, visual, AllocNone);
207 }
208}
209
210void
211rxvt_screen::clear ()
212{
213 if (cmap != DefaultColormapOfScreen (ScreenOfDisplay (xdisp, display->screen)))
214 XFreeColormap (xdisp, cmap);
215}
136 216
137///////////////////////////////////////////////////////////////////////////// 217/////////////////////////////////////////////////////////////////////////////
138 218
139rxvt_display::rxvt_display (const char *id) 219rxvt_display::rxvt_display (const char *id)
140: refcounted (id) 220: refcounted (id)
141, x_ev (this, &rxvt_display::x_cb) 221, x_ev (this, &rxvt_display::x_cb)
142, selection_owner (0) 222, selection_owner (0)
143{ 223{
144} 224}
145 225
226XrmDatabase
227rxvt_display::get_resources ()
228{
229 char *homedir = (char *)getenv ("HOME");
230 char fname[1024];
231
232 /*
233 * get resources using the X library function
234 */
235 char *displayResource, *xe;
236 XrmDatabase database, rdb1;
237
238 database = NULL;
239
240 // for ordering, see for example http://www.faqs.org/faqs/Xt-FAQ/ Subject: 20
241
242 // 6. System wide per application default file.
243
244 /* Add in $XAPPLRESDIR/Rxvt only; not bothering with XUSERFILESEARCHPATH */
245 if ((xe = (char *)getenv ("XAPPLRESDIR")))
246 {
247 snprintf (fname, sizeof (fname), "%s/%s", xe, RESCLASS);
248
249 if ((rdb1 = XrmGetFileDatabase (fname)))
250 XrmMergeDatabases (rdb1, &database);
251 }
252
253 // 5. User's per application default file.
254 // none
255
256 // 4. User's defaults file.
257 /* Get any Xserver defaults */
258 displayResource = XResourceManagerString (display);
259
260 if (displayResource != NULL)
261 {
262 if ((rdb1 = XrmGetStringDatabase (displayResource)))
263 XrmMergeDatabases (rdb1, &database);
264 }
265 else if (homedir)
266 {
267 snprintf (fname, sizeof (fname), "%s/.Xdefaults", homedir);
268
269 if ((rdb1 = XrmGetFileDatabase (fname)))
270 XrmMergeDatabases (rdb1, &database);
271 }
272
273 /* Get screen specific resources */
274 displayResource = XScreenResourceString (ScreenOfDisplay (display, screen));
275
276 if (displayResource != NULL)
277 {
278 if ((rdb1 = XrmGetStringDatabase (displayResource)))
279 /* Merge with screen-independent resources */
280 XrmMergeDatabases (rdb1, &database);
281
282 XFree (displayResource);
283 }
284
285 // 3. User's per host defaults file
286 /* Add in XENVIRONMENT file */
287 if ((xe = (char *)getenv ("XENVIRONMENT"))
288 && (rdb1 = XrmGetFileDatabase (xe)))
289 XrmMergeDatabases (rdb1, &database);
290 else if (homedir)
291 {
292 struct utsname un;
293
294 if (!uname (&un))
295 {
296 snprintf (fname, sizeof (fname), "%s/.Xdefaults-%s", homedir, un.nodename);
297
298 if ((rdb1 = XrmGetFileDatabase (fname)))
299 XrmMergeDatabases (rdb1, &database);
300 }
301 }
302
303 return database;
304}
305
146bool rxvt_display::init () 306bool rxvt_display::ref_init ()
147{ 307{
308#ifdef LOCAL_X_IS_UNIX
309 if (id[0] == ':')
310 {
311 val = rxvt_malloc (5 + strlen (id) + 1);
312 strcpy (val, "unix/");
313 strcat (val, id);
314 display = XOpenDisplay (val);
315 free (val);
316 }
317 else
318#endif
319 display = 0;
320
321 if (!display)
148 display = XOpenDisplay (id); 322 display = XOpenDisplay (id);
149 323
150 if (!display) 324 if (!display)
151 return false; 325 return false;
152 326
153 screen = DefaultScreen (display); 327 screen = DefaultScreen (display);
154 root = DefaultRootWindow (display); 328 root = DefaultRootWindow (display);
155 visual = DefaultVisual (display, screen);
156 cmap = DefaultColormap (display, screen);
157 depth = DefaultDepth (display, screen);
158 329
159 int fd = XConnectionNumber (display); 330 assert (sizeof (xa_names) / sizeof (char *) == NUM_XA);
331 XInternAtoms (display, (char **)xa_names, NUM_XA, False, xa);
160 332
161#ifndef NO_SLOW_LINK_SUPPORT 333 XrmSetDatabase (display, get_resources ());
162 // try to detetc wether we have a local connection.
163 // assume unix domains socket == local, everything else not
164 // TODO: might want to check for inet/127.0.0.1
165 is_local = 0;
166 sockaddr_un sa;
167 socklen_t sl = sizeof (sa);
168
169 if (!getsockname (fd, (sockaddr *)&sa, &sl))
170 is_local = sa.sun_family == AF_LOCAL;
171#endif
172 334
173#ifdef POINTER_BLANK 335#ifdef POINTER_BLANK
174 XColor blackcolour; 336 XColor blackcolour;
175 blackcolour.red = 0; 337 blackcolour.red = 0;
176 blackcolour.green = 0; 338 blackcolour.green = 0;
179 blank_cursor = XCreateGlyphCursor (display, f, f, ' ', ' ', 341 blank_cursor = XCreateGlyphCursor (display, f, f, ' ', ' ',
180 &blackcolour, &blackcolour); 342 &blackcolour, &blackcolour);
181 XUnloadFont (display, f); 343 XUnloadFont (display, f);
182#endif 344#endif
183 345
184#ifdef PREFER_24BIT 346 int fd = XConnectionNumber (display);
185 /*
186 * If depth is not 24, look for a 24bit visual.
187 */
188 if (depth != 24)
189 {
190 XVisualInfo vinfo;
191 347
192 if (XMatchVisualInfo (display, screen, 24, TrueColor, &vinfo)) 348#ifndef NO_SLOW_LINK_SUPPORT
193 { 349 // try to detect wether we have a local connection.
194 depth = 24; 350 // assume unix domains socket == local, everything else not
195 visual = vinfo.visual; 351 // TODO: might want to check for inet/127.0.0.1
196 cmap = XCreateColormap (display, 352 is_local = 0;
197 RootWindow (display, screen), 353 sockaddr_un sa;
198 visual, AllocNone); 354 socklen_t sl = sizeof (sa);
199 } 355
200 } 356 if (!getsockname (fd, (sockaddr *)&sa, &sl))
357 is_local = sa.sun_family == AF_LOCAL;
201#endif 358#endif
202 359
203 x_ev.start (fd, EVENT_READ); 360 x_ev.start (fd, EVENT_READ);
204 fcntl (fd, F_SETFD, FD_CLOEXEC); 361 fcntl (fd, F_SETFD, FD_CLOEXEC);
205 362
206 XSelectInput (display, root, PropertyChangeMask); 363 XSelectInput (display, root, PropertyChangeMask);
207#ifdef USE_XIM
208 xa_xim_servers = XInternAtom (display, "XIM_SERVERS", 0);
209#endif
210 364
211 flush (); 365 flush ();
212 366
213 return true; 367 return true;
214} 368}
215 369
370void
371rxvt_display::ref_next ()
372{
373 // TODO: somehow check wether the database files/resources changed
374 // before re-loading/parsing
375 XrmDestroyDatabase (XrmGetDatabase (display));
376 XrmSetDatabase (display, get_resources ());
377}
378
216rxvt_display::~rxvt_display () 379rxvt_display::~rxvt_display ()
217{ 380{
381 if (!display)
382 return;
383
384#ifdef POINTER_BLANK
385 XFreeCursor (display, blank_cursor);
386#endif
218 x_ev.stop (); 387 x_ev.stop ();
219#ifdef USE_XIM 388#ifdef USE_XIM
220 xims.clear (); 389 xims.clear ();
221#endif 390#endif
222
223 if (display)
224 XCloseDisplay (display); 391 XCloseDisplay (display);
225} 392}
226 393
227#ifdef USE_XIM 394#ifdef USE_XIM
228void rxvt_display::im_change_cb () 395void rxvt_display::im_change_cb ()
229{ 396{
231 (*i)->call (); 398 (*i)->call ();
232} 399}
233 400
234void rxvt_display::im_change_check () 401void rxvt_display::im_change_check ()
235{ 402{
236 // make sure we only call im_change_cb when a new input method 403 // try to only call im_change_cb when a new input method
237 // registers, as xlib crashes due to a race otherwise. 404 // registers, as xlib crashes due to a race otherwise.
238 Atom actual_type, *atoms; 405 Atom actual_type, *atoms;
239 int actual_format; 406 int actual_format;
240 unsigned long nitems, bytes_after; 407 unsigned long nitems, bytes_after;
241 408
242 if (XGetWindowProperty (display, root, xa_xim_servers, 0L, 1000000L, 409 if (XGetWindowProperty (display, root, xa[XA_XIM_SERVERS], 0L, 1000000L,
243 False, XA_ATOM, &actual_type, &actual_format, 410 False, XA_ATOM, &actual_type, &actual_format,
244 &nitems, &bytes_after, (unsigned char **)&atoms) 411 &nitems, &bytes_after, (unsigned char **)&atoms)
245 != Success ) 412 != Success )
246 return; 413 return;
247 414
265 XNextEvent (display, &xev); 432 XNextEvent (display, &xev);
266 433
267#ifdef USE_XIM 434#ifdef USE_XIM
268 if (!XFilterEvent (&xev, None)) 435 if (!XFilterEvent (&xev, None))
269 { 436 {
270
271 if (xev.type == PropertyNotify 437 if (xev.type == PropertyNotify
272 && xev.xany.window == root 438 && xev.xany.window == root
273 && xev.xproperty.atom == xa_xim_servers) 439 && xev.xproperty.atom == xa[XA_XIM_SERVERS])
274 im_change_check (); 440 im_change_check ();
275#endif 441#endif
276 for (int i = xw.size (); i--; ) 442 for (int i = xw.size (); i--; )
277 { 443 {
278 if (!xw[i]) 444 if (!xw[i])
368refcache<rxvt_display> displays; 534refcache<rxvt_display> displays;
369 535
370///////////////////////////////////////////////////////////////////////////// 536/////////////////////////////////////////////////////////////////////////////
371 537
372bool 538bool
373rxvt_color::set (rxvt_display *display, Pixel p) 539rxvt_color::set (rxvt_screen *screen, Pixel p)
374{ 540{
375#if XFT 541#if XFT
376 XColor xc; 542 XColor xc;
377 543
378 xc.pixel = p; 544 xc.pixel = p;
379 if (!XQueryColor (display->display, display->cmap, &xc)) 545 if (!XQueryColor (screen->xdisp, screen->cmap, &xc))
380 return false; 546 return false;
381 547
382 XRenderColor d; 548 XRenderColor d;
383 549
384 d.red = xc.red; 550 d.red = xc.red;
385 d.green = xc.green; 551 d.green = xc.green;
386 d.blue = xc.blue; 552 d.blue = xc.blue;
387 d.alpha = 0xffff; 553 d.alpha = 0xffff;
388 554
389 return 555 return
390 XftColorAllocValue (display->display, 556 XftColorAllocValue (screen->xdisp,
391 display->visual, 557 screen->visual,
392 display->cmap, 558 screen->cmap,
393 &d, &c); 559 &d, &c);
394#else 560#else
395 this->p = p; 561 this->p = p;
396#endif 562#endif
397 563
398 return true; 564 return true;
399} 565}
400 566
401bool 567bool
402rxvt_color::set (rxvt_display *display, const char *name) 568rxvt_color::set (rxvt_screen *screen, const char *name)
403{ 569{
404#if XFT 570#if XFT
405 return XftColorAllocName (display->display, display->visual, display->cmap, 571 return XftColorAllocName (screen->xdisp, screen->visual, screen->cmap, name, &c);
406 name, &c);
407#else 572#else
408 XColor xc; 573 XColor xc;
409 574
410 if (XParseColor (display->display, display->cmap, name, &xc)) 575 if (XParseColor (screen->xdisp, screen->cmap, name, &xc))
411 return set (display, xc.red, xc.green, xc.blue); 576 return set (screen, xc.red, xc.green, xc.blue);
412 577
413 return false; 578 return false;
414#endif 579#endif
415} 580}
416 581
417bool 582bool
418rxvt_color::set (rxvt_display *display, unsigned short cr, unsigned short cg, unsigned short cb) 583rxvt_color::set (rxvt_screen *screen, unsigned short cr, unsigned short cg, unsigned short cb)
419{ 584{
420 XColor xc; 585 XColor xc;
421 586
422 xc.red = cr; 587 xc.red = cr;
423 xc.green = cg; 588 xc.green = cg;
424 xc.blue = cb; 589 xc.blue = cb;
425 xc.flags = DoRed | DoGreen | DoBlue; 590 xc.flags = DoRed | DoGreen | DoBlue;
426 591
427 if (XAllocColor (display->display, display->cmap, &xc)) 592 if (XAllocColor (screen->xdisp, screen->cmap, &xc))
428 return set (display, xc.pixel); 593 return set (screen, xc.pixel);
429 594
430 return false; 595 return false;
431} 596}
432 597
433void 598void
434rxvt_color::get (rxvt_display *display, unsigned short &cr, unsigned short &cg, unsigned short &cb) 599rxvt_color::get (rxvt_screen *screen, unsigned short &cr, unsigned short &cg, unsigned short &cb)
435{ 600{
436#if XFT 601#if XFT
437 cr = c.color.red; 602 cr = c.color.red;
438 cg = c.color.green; 603 cg = c.color.green;
439 cb = c.color.blue; 604 cb = c.color.blue;
440#else 605#else
441 XColor c; 606 XColor c;
442 607
443 c.pixel = p; 608 c.pixel = p;
444 XQueryColor (display->display, display->cmap, &c); 609 XQueryColor (screen->xdisp, screen->cmap, &c);
445 610
446 cr = c.red; 611 cr = c.red;
447 cg = c.green; 612 cg = c.green;
448 cb = c.blue; 613 cb = c.blue;
449#endif 614#endif
450} 615}
451 616
452void 617void
453rxvt_color::free (rxvt_display *display) 618rxvt_color::free (rxvt_screen *screen)
454{ 619{
455#if XFT 620#if XFT
456 XftColorFree (display->display, display->visual, display->cmap, &c); 621 XftColorFree (screen->xdisp, screen->visual, screen->cmap, &c);
457#else 622#else
458 XFreeColors (display->display, display->cmap, &p, 1, AllPlanes); 623 XFreeColors (screen->xdisp, screen->cmap, &p, 1, AllPlanes);
459#endif 624#endif
460} 625}
461 626
462rxvt_color 627rxvt_color
463rxvt_color::fade (rxvt_display *display, int percent) 628rxvt_color::fade (rxvt_screen *screen, int percent)
464{ 629{
630 percent = 100 - percent;
631
465 unsigned short cr, cg, cb; 632 unsigned short cr, cg, cb;
466 rxvt_color faded; 633 rxvt_color faded;
467 634
468 get (display, cr, cg, cb); 635 get (screen, cr, cg, cb);
636
469 faded.set (display, 637 faded.set (
638 screen,
470 cr * percent / 100, 639 cr * percent / 100,
471 cg * percent / 100, 640 cg * percent / 100,
472 cb * percent / 100); 641 cb * percent / 100
642 );
473 643
474 return faded; 644 return faded;
475} 645}
476 646
647#define LERP(a,b,p) (a * p + b * (100 - p)) / 100
648
649rxvt_color
650rxvt_color::fade (rxvt_screen *screen, int percent, rxvt_color &fadeto)
651{
652 percent = 100 - percent;
653
654 unsigned short cr, cg, cb;
655 unsigned short fcr, fcg, fcb;
656 rxvt_color faded;
657
658 get (screen, cr, cg, cb);
659 fadeto.get (screen, fcr, fcg, fcb);
660
661 faded.set (
662 screen,
663 LERP (cr, fcr, percent),
664 LERP (cg, fcg, percent),
665 LERP (cb, fcb, percent)
666 );
667
668 return faded;
669}
670

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines