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.6 by pcg, Wed Feb 11 08:13:45 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
8refcounted::refcounted (const char *id) 52refcounted::refcounted (const char *id)
9{ 53{
10 this->id = STRDUP (id); 54 this->id = STRDUP (id);
11} 55}
12 56
16} 60}
17 61
18template<class T> 62template<class T>
19T *refcache<T>::get (const char *id) 63T *refcache<T>::get (const char *id)
20{ 64{
21 for (T **i = begin (); i < end (); ++i) 65 for (T **i = this->begin (); i < this->end (); ++i)
22 { 66 {
23 if (!strcmp (id, (*i)->id)) 67 if (!strcmp (id, (*i)->id))
24 { 68 {
25 (*i)->referenced++; 69 (*i)->referenced++;
26 return *i; 70 return *i;
31 75
32 obj->referenced = 1; 76 obj->referenced = 1;
33 77
34 if (obj && obj->init ()) 78 if (obj && obj->init ())
35 { 79 {
36 push_back (obj); 80 this->push_back (obj);
37 return obj; 81 return obj;
38 } 82 }
39 else 83 else
40 { 84 {
41 delete obj; 85 delete obj;
49 if (!obj) 93 if (!obj)
50 return; 94 return;
51 95
52 if (!--obj->referenced) 96 if (!--obj->referenced)
53 { 97 {
54 erase (find (begin (), end (), obj)); 98 this->erase (find (this->begin (), this->end (), obj));
55 delete obj; 99 delete obj;
56 } 100 }
57} 101}
58 102
59template<class T> 103template<class T>
60refcache<T>::~refcache () 104refcache<T>::~refcache ()
61{ 105{
62 while (size ()) 106 while (this->size ())
63 put (*begin ()); 107 put (*this->begin ());
64} 108}
65 109
66///////////////////////////////////////////////////////////////////////////// 110/////////////////////////////////////////////////////////////////////////////
67 111
112#ifdef USE_XIM
68static void 113static void
114#if XIMCB_PROTO_BROKEN
115im_destroy_cb (XIC unused1, XPointer client_data, XPointer unused3)
116#else
69im_destroy_cb (XIM unused1, XPointer client_data, XPointer unused3) 117im_destroy_cb (XIM unused1, XPointer client_data, XPointer unused3)
118#endif
70{ 119{
71 rxvt_xim *xim = (rxvt_xim *)client_data; 120 rxvt_xim *xim = (rxvt_xim *)client_data;
72 rxvt_display *display = xim->display; 121 rxvt_display *display = xim->display;
73 122
74 display->xims.erase (find (display->xims.begin (), display->xims.end (), xim)); 123 display->xims.erase (find (display->xims.begin (), display->xims.end (), xim));
97rxvt_xim::~rxvt_xim () 146rxvt_xim::~rxvt_xim ()
98{ 147{
99 if (xim) 148 if (xim)
100 XCloseIM (xim); 149 XCloseIM (xim);
101} 150}
151#endif
102 152
103///////////////////////////////////////////////////////////////////////////// 153/////////////////////////////////////////////////////////////////////////////
104 154
105rxvt_display::rxvt_display (const char *id) 155rxvt_display::rxvt_display (const char *id)
106: refcounted (id) 156: refcounted (id)
107, x_watcher (this, &rxvt_display::x_event) 157, x_ev (this, &rxvt_display::x_cb)
108, selection_owner (0) 158, selection_owner (0)
109{ 159{
110} 160}
111 161
112bool rxvt_display::init () 162bool rxvt_display::init ()
113{ 163{
114 display = XOpenDisplay (id); 164 display = XOpenDisplay (id);
165
166 if (!display)
167 return false;
115 168
116 screen = DefaultScreen (display); 169 screen = DefaultScreen (display);
117 root = DefaultRootWindow (display); 170 root = DefaultRootWindow (display);
118 visual = DefaultVisual (display, screen); 171 visual = DefaultVisual (display, screen);
119 cmap = DefaultColormap (display, screen); 172 cmap = DefaultColormap (display, screen);
120 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
121 188
122#ifdef PREFER_24BIT 189#ifdef PREFER_24BIT
123 /* 190 /*
124 * If depth is not 24, look for a 24bit visual. 191 * If depth is not 24, look for a 24bit visual.
125 */ 192 */
136 visual, AllocNone); 203 visual, AllocNone);
137 } 204 }
138 } 205 }
139#endif 206#endif
140 207
141 int fd = XConnectionNumber (display);
142 x_watcher.start (fd, EVENT_READ); 208 x_ev.start (fd, EVENT_READ);
143 fcntl (fd, F_SETFL, FD_CLOEXEC); 209 fcntl (fd, F_SETFD, FD_CLOEXEC);
144 210
145 XSelectInput (display, root, PropertyChangeMask); 211 XSelectInput (display, root, PropertyChangeMask);
212#ifdef USE_XIM
146 xa_xim_servers = XInternAtom (display, "XIM_SERVERS", 0); 213 xa_xim_servers = XInternAtom (display, "XIM_SERVERS", 0);
214#endif
215
216 flush ();
147 217
148 return true; 218 return true;
149} 219}
150 220
151rxvt_display::~rxvt_display () 221rxvt_display::~rxvt_display ()
152{ 222{
153 x_watcher.stop (); 223 x_ev.stop ();
154 224
225 if (display)
155 XCloseDisplay (display); 226 XCloseDisplay (display);
156} 227}
157 228
229#ifdef USE_XIM
158void rxvt_display::im_change_cb () 230void rxvt_display::im_change_cb ()
159{ 231{
160 for (im_watcher **i = imw.begin (); i != imw.end (); ++i) 232 for (im_watcher **i = imw.begin (); i != imw.end (); ++i)
161 (*i)->call (); 233 (*i)->call ();
162} 234}
235#endif
163 236
164void rxvt_display::x_event (io_watcher &w, short revents) 237void rxvt_display::x_cb (io_watcher &w, short revents)
165{ 238{
166 do 239 do
167 { 240 {
168 XEvent xev; 241 XEvent xev;
169 XNextEvent (display, &xev); 242 XNextEvent (display, &xev);
170 243
171 //printf ("T %d w %lx\n", xev.type, xev.xany.window);//D 244 //printf ("T %d w %lx\n", xev.type, xev.xany.window);//D
172 245
246#ifdef USE_XIM
173 if (xev.type == PropertyNotify 247 if (xev.type == PropertyNotify
174 && xev.xany.window == root 248 && xev.xany.window == root
175 && xev.xproperty.atom == xa_xim_servers) 249 && xev.xproperty.atom == xa_xim_servers)
176 im_change_cb (); 250 im_change_cb ();
251#endif
177 252
178 for (int i = xw.size (); i--; ) 253 for (int i = xw.size (); i--; )
179 { 254 {
180 if (!xw[i]) 255 if (!xw[i])
181 xw.erase_unordered (i); 256 xw.erase_unordered (i);
182 else if (xw[i]->window == xev.xany.window) 257 else if (xw[i]->window == xev.xany.window)
183 xw[i]->call (xev); 258 xw[i]->call (xev);
184 } 259 }
185 } 260 }
186 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 }
187} 275}
188 276
189void rxvt_display::reg (xevent_watcher *w) 277void rxvt_display::reg (xevent_watcher *w)
190{ 278{
191 xw.push_back (w); 279 xw.push_back (w);
196{ 284{
197 if (w->active) 285 if (w->active)
198 xw[w->active - 1] = 0; 286 xw[w->active - 1] = 0;
199} 287}
200 288
201void rxvt_display::reg (im_watcher *w)
202{
203 imw.push_back (w);
204}
205
206void rxvt_display::unreg (im_watcher *w)
207{
208 imw.erase (find (imw.begin (), imw.end (), w));
209}
210
211void rxvt_display::set_selection_owner (rxvt_term *owner) 289void rxvt_display::set_selection_owner (rxvt_term *owner)
212{ 290{
213 if (selection_owner && selection_owner != owner) 291 if (selection_owner && selection_owner != owner)
214 selection_owner->selection_clear (); 292 selection_owner->selection_clear ();
215 293
216 selection_owner = owner; 294 selection_owner = owner;
217} 295}
218 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
219rxvt_xim *rxvt_display::get_xim (const char *locale, const char *modifiers) 308rxvt_xim *rxvt_display::get_xim (const char *locale, const char *modifiers)
220{ 309{
221 // asprintf is a GNU and *BSD extension.. sorry...
222 char *id; 310 char *id;
311 int l, m;
223 312
224 if (asprintf (&id, "%s\n%s", locale, modifiers) < 0) 313 l = strlen (locale);
314 m = strlen (modifiers);
315
316 if (!(id = (char *)malloc (l + m + 2)))
225 return 0; 317 return 0;
226 318
319 memcpy (id, locale, l); id[l] = '\n';
320 memcpy (id + l + 1, modifiers, m); id[l + m + 1] = 0;
321
227 rxvt_xim *xim = xims.get (id); 322 rxvt_xim *xim = xims.get (id);
228 323
229 free (id); 324 free (id);
230 325
231 return xim; 326 return xim;
233 328
234void rxvt_display::put_xim (rxvt_xim *xim) 329void rxvt_display::put_xim (rxvt_xim *xim)
235{ 330{
236 xims.put (xim); 331 xims.put (xim);
237} 332}
333#endif
334
335Atom rxvt_display::atom (const char *name)
336{
337 return XInternAtom (display, name, False);
338}
238 339
239///////////////////////////////////////////////////////////////////////////// 340/////////////////////////////////////////////////////////////////////////////
240 341
241template refcache<rxvt_display>; 342template class refcache<rxvt_display>;
242refcache<rxvt_display> displays; 343refcache<rxvt_display> displays;
243 344
244///////////////////////////////////////////////////////////////////////////// 345/////////////////////////////////////////////////////////////////////////////
245 346
246bool 347bool
322rxvt_color::free (rxvt_display *display) 423rxvt_color::free (rxvt_display *display)
323{ 424{
324#if XFT 425#if XFT
325 XftColorFree (display->display, display->visual, display->cmap, &c); 426 XftColorFree (display->display, display->visual, display->cmap, &c);
326#else 427#else
327 XFreeColors (display->display, display->cmap, &c, 1, AllPlanes); 428 XFreeColors (display->display, display->cmap, &p, 1, AllPlanes);
328#endif 429#endif
329} 430}
330 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