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.1 by root, Sun Aug 15 00:37:04 2004 UTC vs.
Revision 1.21 by root, Sun Jan 8 07:55:36 2006 UTC

1/*--------------------------------*-C-*---------------------------------* 1/*--------------------------------*-C-*---------------------------------*
2 * File: rxvtcolor.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-2004 Marc Lehmann <pcg@goof.com>
7 * 7 *
32# include <sys/un.h> 32# include <sys/un.h>
33#endif 33#endif
34 34
35refcounted::refcounted (const char *id) 35refcounted::refcounted (const char *id)
36{ 36{
37 this->id = STRDUP (id); 37 this->id = strdup (id);
38} 38}
39 39
40refcounted::~refcounted () 40refcounted::~refcounted ()
41{ 41{
42 free (id); 42 free (id);
82 delete obj; 82 delete obj;
83 } 83 }
84} 84}
85 85
86template<class T> 86template<class T>
87refcache<T>::~refcache () 87void refcache<T>::clear ()
88{ 88{
89 while (this->size ()) 89 while (this->size ())
90 put (*this->begin ()); 90 put (*this->begin ());
91} 91}
92 92
101#endif 101#endif
102{ 102{
103 rxvt_xim *xim = (rxvt_xim *)client_data; 103 rxvt_xim *xim = (rxvt_xim *)client_data;
104 rxvt_display *display = xim->display; 104 rxvt_display *display = xim->display;
105 105
106 xim->xim = 0;
107
106 display->xims.erase (find (display->xims.begin (), display->xims.end (), xim)); 108 display->xims.erase (find (display->xims.begin (), display->xims.end (), xim));
107
108 display->im_change_cb (); 109 display->im_change_cb ();
109} 110}
110 111
111bool rxvt_xim::init () 112bool rxvt_xim::init ()
112{ 113{
142{ 143{
143} 144}
144 145
145bool rxvt_display::init () 146bool rxvt_display::init ()
146{ 147{
148#ifdef LOCAL_X_IS_UNIX
149 if (id[0] == ':')
150 {
151 val = rxvt_malloc (5 + strlen (id) + 1);
152 strcpy (val, "unix/");
153 strcat (val, id);
154 display = XOpenDisplay (val);
155 free (val);
156 }
157 else
158#endif
159 display = 0;
160
161 if (!display)
147 display = XOpenDisplay (id); 162 display = XOpenDisplay (id);
148 163
149 if (!display) 164 if (!display)
150 return false; 165 return false;
151 166
152 screen = DefaultScreen (display); 167 screen = DefaultScreen (display);
156 depth = DefaultDepth (display, screen); 171 depth = DefaultDepth (display, screen);
157 172
158 int fd = XConnectionNumber (display); 173 int fd = XConnectionNumber (display);
159 174
160#ifndef NO_SLOW_LINK_SUPPORT 175#ifndef NO_SLOW_LINK_SUPPORT
161 // try to detetc wether we have a local connection. 176 // try to detect wether we have a local connection.
162 // assume unix domains socket == local, everything else not 177 // assume unix domains socket == local, everything else not
163 // TODO: might want to check for inet/127.0.0.1 178 // TODO: might want to check for inet/127.0.0.1
164 is_local = 0; 179 is_local = 0;
165 sockaddr_un sa; 180 sockaddr_un sa;
166 socklen_t sl = sizeof (sa); 181 socklen_t sl = sizeof (sa);
167 182
168 if (!getsockname (fd, (sockaddr *)&sa, &sl)) 183 if (!getsockname (fd, (sockaddr *)&sa, &sl))
169 is_local = sa.sun_family == AF_LOCAL; 184 is_local = sa.sun_family == AF_LOCAL;
185#endif
186
187#ifdef POINTER_BLANK
188 XColor blackcolour;
189 blackcolour.red = 0;
190 blackcolour.green = 0;
191 blackcolour.blue = 0;
192 Font f = XLoadFont (display, "fixed");
193 blank_cursor = XCreateGlyphCursor (display, f, f, ' ', ' ',
194 &blackcolour, &blackcolour);
195 XUnloadFont (display, f);
170#endif 196#endif
171 197
172#ifdef PREFER_24BIT 198#ifdef PREFER_24BIT
173 /* 199 /*
174 * If depth is not 24, look for a 24bit visual. 200 * If depth is not 24, look for a 24bit visual.
201 return true; 227 return true;
202} 228}
203 229
204rxvt_display::~rxvt_display () 230rxvt_display::~rxvt_display ()
205{ 231{
232#ifdef POINTER_BLANK
233 XFreeCursor (display, blank_cursor);
234#endif
206 x_ev.stop (); 235 x_ev.stop ();
236#ifdef USE_XIM
237 xims.clear ();
238#endif
207 239
208 if (display) 240 if (display)
209 XCloseDisplay (display); 241 XCloseDisplay (display);
210} 242}
211 243
213void rxvt_display::im_change_cb () 245void rxvt_display::im_change_cb ()
214{ 246{
215 for (im_watcher **i = imw.begin (); i != imw.end (); ++i) 247 for (im_watcher **i = imw.begin (); i != imw.end (); ++i)
216 (*i)->call (); 248 (*i)->call ();
217} 249}
250
251void rxvt_display::im_change_check ()
252{
253 // try to only call im_change_cb when a new input method
254 // registers, as xlib crashes due to a race otherwise.
255 Atom actual_type, *atoms;
256 int actual_format;
257 unsigned long nitems, bytes_after;
258
259 if (XGetWindowProperty (display, root, xa_xim_servers, 0L, 1000000L,
260 False, XA_ATOM, &actual_type, &actual_format,
261 &nitems, &bytes_after, (unsigned char **)&atoms)
262 != Success )
263 return;
264
265 if (actual_type == XA_ATOM && actual_format == 32)
266 for (int i = 0; i < nitems; i++)
267 if (XGetSelectionOwner (display, atoms[i]))
268 {
269 im_change_cb ();
270 break;
271 }
272
273 XFree (atoms);
274}
218#endif 275#endif
219 276
220void rxvt_display::x_cb (io_watcher &w, short revents) 277void rxvt_display::x_cb (io_watcher &w, short revents)
221{ 278{
222 do 279 do
223 { 280 {
224 XEvent xev; 281 XEvent xev;
225 XNextEvent (display, &xev); 282 XNextEvent (display, &xev);
226 283
227 //printf ("T %d w %lx\n", xev.type, xev.xany.window);//D
228
229#ifdef USE_XIM 284#ifdef USE_XIM
230 if (xev.type == PropertyNotify 285 if (!XFilterEvent (&xev, None))
231 && xev.xany.window == root
232 && xev.xproperty.atom == xa_xim_servers)
233 im_change_cb ();
234#endif
235
236 for (int i = xw.size (); i--; )
237 { 286 {
287 if (xev.type == PropertyNotify
288 && xev.xany.window == root
289 && xev.xproperty.atom == xa_xim_servers)
290 im_change_check ();
291#endif
292 for (int i = xw.size (); i--; )
293 {
238 if (!xw[i]) 294 if (!xw[i])
239 xw.erase_unordered (i); 295 xw.erase_unordered (i);
240 else if (xw[i]->window == xev.xany.window) 296 else if (xw[i]->window == xev.xany.window)
241 xw[i]->call (xev); 297 xw[i]->call (xev);
298 }
299#ifdef USE_XIM
242 } 300 }
301#endif
243 } 302 }
244 while (XPending (display)); 303 while (XEventsQueued (display, QueuedAlready));
245 304
246 flush (); 305 XFlush (display);
247} 306}
248 307
249void rxvt_display::flush () 308void rxvt_display::flush ()
250{ 309{
251 for (;;) 310 if (XEventsQueued (display, QueuedAlready))
252 { 311 x_cb (x_ev, EVENT_READ);
253 if (!XPending (display))
254 break;
255 312
256 x_cb (x_ev, 0); 313 XFlush (display);
257 }
258} 314}
259 315
260void rxvt_display::reg (xevent_watcher *w) 316void rxvt_display::reg (xevent_watcher *w)
261{ 317{
262 xw.push_back (w); 318 xw.push_back (w);
309 return xim; 365 return xim;
310} 366}
311 367
312void rxvt_display::put_xim (rxvt_xim *xim) 368void rxvt_display::put_xim (rxvt_xim *xim)
313{ 369{
370#if XLIB_IS_RACEFREE
314 xims.put (xim); 371 xims.put (xim);
372#endif
315} 373}
316#endif 374#endif
317 375
318Atom rxvt_display::atom (const char *name) 376Atom rxvt_display::atom (const char *name)
319{ 377{
357} 415}
358 416
359bool 417bool
360rxvt_color::set (rxvt_display *display, const char *name) 418rxvt_color::set (rxvt_display *display, const char *name)
361{ 419{
420#if XFT
421 return XftColorAllocName (display->display, display->visual, display->cmap,
422 name, &c);
423#else
362 XColor xc; 424 XColor xc;
363 425
364 if (XParseColor (display->display, display->cmap, name, &xc)) 426 if (XParseColor (display->display, display->cmap, name, &xc))
365 return set (display, xc.red, xc.green, xc.blue); 427 return set (display, xc.red, xc.green, xc.blue);
366 428
367 return false; 429 return false;
430#endif
368} 431}
369 432
370bool 433bool
371rxvt_color::set (rxvt_display *display, unsigned short cr, unsigned short cg, unsigned short cb) 434rxvt_color::set (rxvt_display *display, unsigned short cr, unsigned short cg, unsigned short cb)
372{ 435{
413} 476}
414 477
415rxvt_color 478rxvt_color
416rxvt_color::fade (rxvt_display *display, int percent) 479rxvt_color::fade (rxvt_display *display, int percent)
417{ 480{
481 percent = 100 - percent;
482
418 unsigned short cr, cg, cb; 483 unsigned short cr, cg, cb;
419 rxvt_color faded; 484 rxvt_color faded;
420 485
421 get (display, cr, cg, cb); 486 get (display, cr, cg, cb);
487
422 faded.set (display, 488 faded.set (
489 display,
423 cr * percent / 100, 490 cr * percent / 100,
424 cg * percent / 100, 491 cg * percent / 100,
425 cb * percent / 100); 492 cb * percent / 100
493 );
426 494
427 return faded; 495 return faded;
428} 496}
429 497
498#define LERP(a,b,p) (a * p + b * (100 - p)) / 100
499
500rxvt_color
501rxvt_color::fade (rxvt_display *display, int percent, rxvt_color &fadeto)
502{
503 percent = 100 - percent;
504
505 unsigned short cr, cg, cb;
506 unsigned short fcr, fcg, fcb;
507 rxvt_color faded;
508
509 get (display, cr, cg, cb);
510 fadeto.get(display, fcr, fcg, fcb);
511
512 faded.set (
513 display,
514 LERP (cr, fcr, percent),
515 LERP (cg, fcg, percent),
516 LERP (cb, fcb, percent)
517 );
518
519 return faded;
520}
521

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines