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

Comparing rxvt-unicode/src/rxvtcolor.C (file contents):
Revision 1.4 by pcg, Mon Feb 9 07:11:49 2004 UTC vs.
Revision 1.22 by root, Wed Aug 4 03:29:28 2004 UTC

1/*--------------------------------*-C-*---------------------------------*
2 * File: rxvtcolor.C
3 *----------------------------------------------------------------------*
4 *
5 * All portions of code are copyright by their respective author/s.
6 * Copyright (c) 2003-2004 Marc Lehmann <pcg@goof.com>
7 *
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.
12 *
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.
17 *
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 *----------------------------------------------------------------------*/
22
1#include "../config.h" 23#include "../config.h"
2#include <rxvt.h> 24#include <rxvt.h>
3#include <rxvtcolor.h> 25#include <rxvtcolor.h>
4 26
5#include <unistd.h> 27#include <unistd.h>
6#include <fcntl.h> 28#include <fcntl.h>
7 29
30#ifndef NO_SLOW_LINK_SUPPORT
31# include <sys/socket.h>
32# include <sys/un.h>
33#endif
34
35class byteorder byteorder;
36
37byteorder::byteorder ()
38{
39 union {
40 uint32_t u;
41 uint8_t b[4];
42 } w;
43
44 w.b[0] = 0x11;
45 w.b[1] = 0x22;
46 w.b[2] = 0x33;
47 w.b[3] = 0x44;
48
49 e = w.u;
50}
51
52refcounted::refcounted (const char *id)
53{
54 this->id = STRDUP (id);
55}
56
57refcounted::~refcounted ()
58{
59 free (id);
60}
61
62template<class T>
63T *refcache<T>::get (const char *id)
64{
65 for (T **i = this->begin (); i < this->end (); ++i)
66 {
67 if (!strcmp (id, (*i)->id))
68 {
69 (*i)->referenced++;
70 return *i;
71 }
72 }
73
74 T *obj = new T (id);
75
76 obj->referenced = 1;
77
78 if (obj && obj->init ())
79 {
80 this->push_back (obj);
81 return obj;
82 }
83 else
84 {
85 delete obj;
86 return 0;
87 }
88}
89
90template<class T>
91void refcache<T>::put (T *obj)
92{
93 if (!obj)
94 return;
95
96 if (!--obj->referenced)
97 {
98 this->erase (find (this->begin (), this->end (), obj));
99 delete obj;
100 }
101}
102
103template<class T>
104refcache<T>::~refcache ()
105{
106 while (this->size ())
107 put (*this->begin ());
108}
109
8///////////////////////////////////////////////////////////////////////////// 110/////////////////////////////////////////////////////////////////////////////
9 111
112#ifdef USE_XIM
113static void
114#if XIMCB_PROTO_BROKEN
115im_destroy_cb (XIC unused1, XPointer client_data, XPointer unused3)
116#else
117im_destroy_cb (XIM unused1, XPointer client_data, XPointer unused3)
118#endif
119{
120 rxvt_xim *xim = (rxvt_xim *)client_data;
121 rxvt_display *display = xim->display;
122
123 display->xims.erase (find (display->xims.begin (), display->xims.end (), xim));
124
125 display->im_change_cb ();
126}
127
128bool rxvt_xim::init ()
129{
130 display = GET_R->display; //HACK: TODO
131
132 xim = XOpenIM (display->display, NULL, NULL, NULL);
133
134 if (!xim)
135 return false;
136
137 XIMCallback ximcallback;
138 ximcallback.client_data = (XPointer)this;
139 ximcallback.callback = im_destroy_cb;
140
141 XSetIMValues (xim, XNDestroyCallback, &ximcallback, NULL);
142
143 return true;
144}
145
146rxvt_xim::~rxvt_xim ()
147{
148 if (xim)
149 XCloseIM (xim);
150}
151#endif
152
153/////////////////////////////////////////////////////////////////////////////
154
10rxvt_display::rxvt_display (const char *name) 155rxvt_display::rxvt_display (const char *id)
156: refcounted (id)
11: x_watcher (this, &rxvt_display::x_event) 157, x_ev (this, &rxvt_display::x_cb)
158, selection_owner (0)
12{ 159{
13 this->name = STRDUP (name);
14} 160}
15 161
16rxvt_display::~rxvt_display ()
17{
18 free (name);
19}
20
21bool rxvt_display::open () 162bool rxvt_display::init ()
22{ 163{
23 display = XOpenDisplay (name); 164 display = XOpenDisplay (id);
165
166 if (!display)
167 return false;
24 168
25 screen = DefaultScreen (display); 169 screen = DefaultScreen (display);
26 root = DefaultRootWindow (display); 170 root = DefaultRootWindow (display);
27 visual = DefaultVisual (display, screen); 171 visual = DefaultVisual (display, screen);
28 cmap = DefaultColormap (display, screen); 172 cmap = DefaultColormap (display, screen);
29 depth = DefaultDepth (display, screen); 173 depth = DefaultDepth (display, screen);
174
175 int fd = XConnectionNumber (display);
176
177#ifndef NO_SLOW_LINK_SUPPORT
178 // try to detetc wether we have a local connection.
179 // assume unix domains socket == local, everything else not
180 // TODO: might want to check for inet/127.0.0.1
181 is_local = 0;
182 sockaddr_un sa;
183 socklen_t sl = sizeof (sa);
184
185 if (!getsockname (fd, (sockaddr *)&sa, &sl))
186 is_local = sa.sun_family == AF_LOCAL;
187#endif
30 188
31#ifdef PREFER_24BIT 189#ifdef PREFER_24BIT
32 /* 190 /*
33 * If depth is not 24, look for a 24bit visual. 191 * If depth is not 24, look for a 24bit visual.
34 */ 192 */
45 visual, AllocNone); 203 visual, AllocNone);
46 } 204 }
47 } 205 }
48#endif 206#endif
49 207
50 int fd = XConnectionNumber (display);
51 x_watcher.start (fd, EVENT_READ); 208 x_ev.start (fd, EVENT_READ);
52 fcntl (fd, F_SETFL, FD_CLOEXEC); 209 fcntl (fd, F_SETFD, FD_CLOEXEC);
210
211 XSelectInput (display, root, PropertyChangeMask);
212#ifdef USE_XIM
213 xa_xim_servers = XInternAtom (display, "XIM_SERVERS", 0);
214#endif
215
216 flush ();
53 217
54 return true; 218 return true;
55} 219}
56 220
57void rxvt_display::close () 221rxvt_display::~rxvt_display ()
58{ 222{
59 x_watcher.stop (); 223 x_ev.stop ();
60 224
225 if (display)
61 XCloseDisplay (display); 226 XCloseDisplay (display);
62} 227}
63 228
229#ifdef USE_XIM
230void rxvt_display::im_change_cb ()
231{
232 for (im_watcher **i = imw.begin (); i != imw.end (); ++i)
233 (*i)->call ();
234}
235#endif
236
64void rxvt_display::x_event (io_watcher &w, short revents) 237void rxvt_display::x_cb (io_watcher &w, short revents)
65{ 238{
66 do 239 do
67 { 240 {
68 XEvent xev; 241 XEvent xev;
69 XNextEvent (display, &xev); 242 XNextEvent (display, &xev);
243
244 //printf ("T %d w %lx\n", xev.type, xev.xany.window);//D
245
246#ifdef USE_XIM
247 if (xev.type == PropertyNotify
248 && xev.xany.window == root
249 && xev.xproperty.atom == xa_xim_servers)
250 im_change_cb ();
251#endif
70 252
71 for (int i = xw.size (); i--; ) 253 for (int i = xw.size (); i--; )
72 { 254 {
73 if (!xw[i]) 255 if (!xw[i])
74 xw.erase_unordered (i); 256 xw.erase_unordered (i);
75 else if (xw[i]->window == xev.xany.window) 257 else if (xw[i]->window == xev.xany.window)
76 xw[i]->call (xev); 258 xw[i]->call (xev);
77 } 259 }
78 } 260 }
79 while (XPending (display)); 261 while (XPending (display));
262
263 flush ();
264}
265
266void rxvt_display::flush ()
267{
268 for (;;)
269 {
270 if (!XPending (display))
271 break;
272
273 x_cb (x_ev, 0);
274 }
80} 275}
81 276
82void rxvt_display::reg (xevent_watcher *w) 277void rxvt_display::reg (xevent_watcher *w)
83{ 278{
84 xw.push_back (w); 279 xw.push_back (w);
89{ 284{
90 if (w->active) 285 if (w->active)
91 xw[w->active - 1] = 0; 286 xw[w->active - 1] = 0;
92} 287}
93 288
289void rxvt_display::set_selection_owner (rxvt_term *owner)
290{
291 if (selection_owner && selection_owner != owner)
292 selection_owner->selection_clear ();
293
294 selection_owner = owner;
295}
296
297#ifdef USE_XIM
298void rxvt_display::reg (im_watcher *w)
299{
300 imw.push_back (w);
301}
302
303void rxvt_display::unreg (im_watcher *w)
304{
305 imw.erase (find (imw.begin (), imw.end (), w));
306}
307
308rxvt_xim *rxvt_display::get_xim (const char *locale, const char *modifiers)
309{
310 char *id;
311 int l, m;
312
313 l = strlen (locale);
314 m = strlen (modifiers);
315
316 if (!(id = (char *)malloc (l + m + 2)))
317 return 0;
318
319 memcpy (id, locale, l); id[l] = '\n';
320 memcpy (id + l + 1, modifiers, m); id[l + m + 1] = 0;
321
322 rxvt_xim *xim = xims.get (id);
323
324 free (id);
325
326 return xim;
327}
328
329void rxvt_display::put_xim (rxvt_xim *xim)
330{
331 xims.put (xim);
332}
333#endif
334
335Atom rxvt_display::atom (const char *name)
336{
337 return XInternAtom (display, name, False);
338}
339
94///////////////////////////////////////////////////////////////////////////// 340/////////////////////////////////////////////////////////////////////////////
95 341
342template class refcache<rxvt_display>;
96rxvt_displays displays; 343refcache<rxvt_display> displays;
97
98rxvt_display *rxvt_displays::get (const char *name)
99{
100 for (rxvt_display **i = list.begin (); i < list.end (); ++i)
101 {
102 if (!strcmp (name, (*i)->name))
103 {
104 (*i)->referenced++;
105 return *i;
106 }
107 }
108
109 rxvt_display *display = new rxvt_display (name);
110
111 display->referenced = 1;
112
113 if (display && display->open ())
114 list.push_back (display);
115 else
116 {
117 delete display;
118 display = 0;
119 }
120
121 return display;
122}
123
124void rxvt_displays::release (rxvt_display *display)
125{
126 if (!--display->referenced)
127 {
128 display->close ();
129 delete display;
130 list.erase (find (list.begin (), list.end (), display));
131 }
132}
133 344
134///////////////////////////////////////////////////////////////////////////// 345/////////////////////////////////////////////////////////////////////////////
135 346
136bool 347bool
137rxvt_color::set (rxvt_display *display, Pixel p) 348rxvt_color::set (rxvt_display *display, Pixel p)
212rxvt_color::free (rxvt_display *display) 423rxvt_color::free (rxvt_display *display)
213{ 424{
214#if XFT 425#if XFT
215 XftColorFree (display->display, display->visual, display->cmap, &c); 426 XftColorFree (display->display, display->visual, display->cmap, &c);
216#else 427#else
217 XFreeColors (display->display, display->cmap, &c, 1, AllPlanes); 428 XFreeColors (display->display, display->cmap, &p, 1, AllPlanes);
218#endif 429#endif
219} 430}
220 431
432rxvt_color
433rxvt_color::fade (rxvt_display *display, int percent)
434{
435 unsigned short cr, cg, cb;
436 rxvt_color faded;
437
438 get (display, cr, cg, cb);
439 faded.set (display,
440 cr * percent / 100,
441 cg * percent / 100,
442 cb * percent / 100);
443
444 return faded;
445}
446

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines