1 /*--------------------------------*-C-*---------------------------------*
3 *----------------------------------------------------------------------*
5 * All portions of code are copyright by their respective author/s.
6 * Copyright (c) 2003-2004 Marc Lehmann <pcg@goof.com>
8 * 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 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 *----------------------------------------------------------------------*/
23 #include "../config.h"
25 #include <rxvttoolkit.h>
30 #include <sys/utsname.h>
32 #ifndef NO_SLOW_LINK_SUPPORT
33 # include <sys/socket.h>
37 const char *const xa_names[] =
73 /////////////////////////////////////////////////////////////////////////////
75 refcounted::refcounted (const char *id)
77 this->id = strdup (id);
80 refcounted::~refcounted ()
86 T *refcache<T>::get (const char *id)
88 for (T **i = this->begin (); i < this->end (); ++i)
90 if (!strcmp (id, (*i)->id))
100 if (obj && obj->ref_init ())
103 this->push_back (obj);
114 void refcache<T>::put (T *obj)
119 if (!--obj->referenced)
121 this->erase (find (this->begin (), this->end (), obj));
127 void refcache<T>::clear ()
129 while (this->size ())
130 put (*this->begin ());
133 /////////////////////////////////////////////////////////////////////////////
138 #if XIMCB_PROTO_BROKEN
139 im_destroy_cb (XIC unused1, XPointer client_data, XPointer unused3)
141 im_destroy_cb (XIM unused1, XPointer client_data, XPointer unused3)
144 rxvt_xim *xim = (rxvt_xim *)client_data;
145 rxvt_display *display = xim->display;
149 display->xims.erase (find (display->xims.begin (), display->xims.end (), xim));
150 display->im_change_cb ();
154 rxvt_xim::ref_init ()
156 display = GET_R->display; //HACK: TODO
158 xim = XOpenIM (display->display, NULL, NULL, NULL);
163 XIMCallback ximcallback;
164 ximcallback.client_data = (XPointer)this;
165 ximcallback.callback = im_destroy_cb;
167 XSetIMValues (xim, XNDestroyCallback, &ximcallback, NULL);
172 rxvt_xim::~rxvt_xim ()
180 /////////////////////////////////////////////////////////////////////////////
183 rxvt_screen::set (rxvt_display *disp)
186 xdisp = disp->display;
188 Screen *screen = ScreenOfDisplay (xdisp, disp->screen);
190 depth = DefaultDepthOfScreen (screen);
191 visual = DefaultVisualOfScreen (screen);
192 cmap = DefaultColormapOfScreen (screen);
196 rxvt_screen::set (rxvt_display *disp, int depth)
202 if (XMatchVisualInfo (xdisp, display->screen, depth, TrueColor, &vinfo))
205 this->visual = vinfo.visual;
206 this->cmap = XCreateColormap (xdisp, disp->root, visual, AllocNone);
211 rxvt_screen::clear ()
213 if (cmap != DefaultColormapOfScreen (ScreenOfDisplay (xdisp, display->screen)))
214 XFreeColormap (xdisp, cmap);
217 /////////////////////////////////////////////////////////////////////////////
219 rxvt_display::rxvt_display (const char *id)
221 , x_ev (this, &rxvt_display::x_cb)
222 , selection_owner (0)
227 rxvt_display::get_resources ()
229 char *homedir = (char *)getenv ("HOME");
233 * get resources using the X library function
235 char *displayResource, *xe;
236 XrmDatabase database, rdb1;
240 // for ordering, see for example http://www.faqs.org/faqs/Xt-FAQ/ Subject: 20
242 // 6. System wide per application default file.
244 /* Add in $XAPPLRESDIR/Rxvt only; not bothering with XUSERFILESEARCHPATH */
245 if ((xe = (char *)getenv ("XAPPLRESDIR")))
247 snprintf (fname, sizeof (fname), "%s/%s", xe, RESCLASS);
249 if ((rdb1 = XrmGetFileDatabase (fname)))
250 XrmMergeDatabases (rdb1, &database);
253 // 5. User's per application default file.
256 // 4. User's defaults file.
257 /* Get any Xserver defaults */
258 displayResource = XResourceManagerString (display);
260 if (displayResource != NULL)
262 if ((rdb1 = XrmGetStringDatabase (displayResource)))
263 XrmMergeDatabases (rdb1, &database);
267 snprintf (fname, sizeof (fname), "%s/.Xdefaults", homedir);
269 if ((rdb1 = XrmGetFileDatabase (fname)))
270 XrmMergeDatabases (rdb1, &database);
273 /* Get screen specific resources */
274 displayResource = XScreenResourceString (ScreenOfDisplay (display, screen));
276 if (displayResource != NULL)
278 if ((rdb1 = XrmGetStringDatabase (displayResource)))
279 /* Merge with screen-independent resources */
280 XrmMergeDatabases (rdb1, &database);
282 XFree (displayResource);
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);
296 snprintf (fname, sizeof (fname), "%s/.Xdefaults-%s", homedir, un.nodename);
298 if ((rdb1 = XrmGetFileDatabase (fname)))
299 XrmMergeDatabases (rdb1, &database);
306 bool rxvt_display::ref_init ()
308 #ifdef LOCAL_X_IS_UNIX
311 val = rxvt_malloc (5 + strlen (id) + 1);
312 strcpy (val, "unix/");
314 display = XOpenDisplay (val);
322 display = XOpenDisplay (id);
327 screen = DefaultScreen (display);
328 root = DefaultRootWindow (display);
330 assert (sizeof (xa_names) / sizeof (char *) == NUM_XA);
331 XInternAtoms (display, (char **)xa_names, NUM_XA, False, xa);
333 XrmSetDatabase (display, get_resources ());
338 blackcolour.green = 0;
339 blackcolour.blue = 0;
340 Font f = XLoadFont (display, "fixed");
341 blank_cursor = XCreateGlyphCursor (display, f, f, ' ', ' ',
342 &blackcolour, &blackcolour);
343 XUnloadFont (display, f);
346 int fd = XConnectionNumber (display);
348 #ifndef NO_SLOW_LINK_SUPPORT
349 // try to detect wether we have a local connection.
350 // assume unix domains socket == local, everything else not
351 // TODO: might want to check for inet/127.0.0.1
354 socklen_t sl = sizeof (sa);
356 if (!getsockname (fd, (sockaddr *)&sa, &sl))
357 is_local = sa.sun_family == AF_LOCAL;
360 x_ev.start (fd, EVENT_READ);
361 fcntl (fd, F_SETFD, FD_CLOEXEC);
363 XSelectInput (display, root, PropertyChangeMask);
371 rxvt_display::ref_next ()
373 // TODO: somehow check wether the database files/resources changed
374 // before re-loading/parsing
375 XrmDestroyDatabase (XrmGetDatabase (display));
376 XrmSetDatabase (display, get_resources ());
379 rxvt_display::~rxvt_display ()
385 XFreeCursor (display, blank_cursor);
391 XCloseDisplay (display);
395 void rxvt_display::im_change_cb ()
397 for (im_watcher **i = imw.begin (); i != imw.end (); ++i)
401 void rxvt_display::im_change_check ()
403 // try to only call im_change_cb when a new input method
404 // registers, as xlib crashes due to a race otherwise.
405 Atom actual_type, *atoms;
407 unsigned long nitems, bytes_after;
409 if (XGetWindowProperty (display, root, xa[XA_XIM_SERVERS], 0L, 1000000L,
410 False, XA_ATOM, &actual_type, &actual_format,
411 &nitems, &bytes_after, (unsigned char **)&atoms)
415 if (actual_type == XA_ATOM && actual_format == 32)
416 for (int i = 0; i < nitems; i++)
417 if (XGetSelectionOwner (display, atoms[i]))
427 void rxvt_display::x_cb (io_watcher &w, short revents)
432 XNextEvent (display, &xev);
435 if (!XFilterEvent (&xev, None))
437 if (xev.type == PropertyNotify
438 && xev.xany.window == root
439 && xev.xproperty.atom == xa[XA_XIM_SERVERS])
442 for (int i = xw.size (); i--; )
445 xw.erase_unordered (i);
446 else if (xw[i]->window == xev.xany.window)
453 while (XEventsQueued (display, QueuedAlready));
458 void rxvt_display::flush ()
460 if (XEventsQueued (display, QueuedAlready))
461 x_cb (x_ev, EVENT_READ);
466 void rxvt_display::reg (xevent_watcher *w)
469 w->active = xw.size ();
472 void rxvt_display::unreg (xevent_watcher *w)
475 xw[w->active - 1] = 0;
478 void rxvt_display::set_selection_owner (rxvt_term *owner)
480 if (selection_owner && selection_owner != owner)
481 selection_owner->selection_clear ();
483 selection_owner = owner;
487 void rxvt_display::reg (im_watcher *w)
492 void rxvt_display::unreg (im_watcher *w)
494 imw.erase (find (imw.begin (), imw.end (), w));
497 rxvt_xim *rxvt_display::get_xim (const char *locale, const char *modifiers)
503 m = strlen (modifiers);
505 if (!(id = (char *)malloc (l + m + 2)))
508 memcpy (id, locale, l); id[l] = '\n';
509 memcpy (id + l + 1, modifiers, m); id[l + m + 1] = 0;
511 rxvt_xim *xim = xims.get (id);
518 void rxvt_display::put_xim (rxvt_xim *xim)
526 Atom rxvt_display::atom (const char *name)
528 return XInternAtom (display, name, False);
531 /////////////////////////////////////////////////////////////////////////////
533 template class refcache<rxvt_display>;
534 refcache<rxvt_display> displays;
536 /////////////////////////////////////////////////////////////////////////////
539 rxvt_color::set (rxvt_screen *screen, Pixel p)
545 if (!XQueryColor (screen->xdisp, screen->cmap, &xc))
556 XftColorAllocValue (screen->xdisp,
568 rxvt_color::set (rxvt_screen *screen, const char *name)
571 return XftColorAllocName (screen->xdisp, screen->visual, screen->cmap, name, &c);
575 if (XParseColor (screen->xdisp, screen->cmap, name, &xc))
576 return set (screen, xc.red, xc.green, xc.blue);
583 rxvt_color::set (rxvt_screen *screen, unsigned short cr, unsigned short cg, unsigned short cb)
590 xc.flags = DoRed | DoGreen | DoBlue;
592 if (XAllocColor (screen->xdisp, screen->cmap, &xc))
593 return set (screen, xc.pixel);
599 rxvt_color::get (rxvt_screen *screen, unsigned short &cr, unsigned short &cg, unsigned short &cb)
609 XQueryColor (screen->xdisp, screen->cmap, &c);
618 rxvt_color::free (rxvt_screen *screen)
621 XftColorFree (screen->xdisp, screen->visual, screen->cmap, &c);
623 XFreeColors (screen->xdisp, screen->cmap, &p, 1, AllPlanes);
628 rxvt_color::fade (rxvt_screen *screen, int percent)
630 percent = 100 - percent;
632 unsigned short cr, cg, cb;
635 get (screen, cr, cg, cb);
647 #define LERP(a,b,p) (a * p + b * (100 - p)) / 100
650 rxvt_color::fade (rxvt_screen *screen, int percent, rxvt_color &fadeto)
652 percent = 100 - percent;
654 unsigned short cr, cg, cb;
655 unsigned short fcr, fcg, fcb;
658 get (screen, cr, cg, cb);
659 fadeto.get (screen, fcr, fcg, fcb);
663 LERP (cr, fcr, percent),
664 LERP (cg, fcg, percent),
665 LERP (cb, fcb, percent)