ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/rxvt-unicode/src/rxvtcolor.C
Revision: 1.4
Committed: Mon Feb 9 07:11:49 2004 UTC (20 years, 4 months ago) by pcg
Content type: text/plain
Branch: MAIN
Changes since 1.3: +155 -16 lines
Log Message:
*** empty log message ***

File Contents

# Content
1 #include "../config.h"
2 #include <rxvt.h>
3 #include <rxvtcolor.h>
4
5 #include <unistd.h>
6 #include <fcntl.h>
7
8 /////////////////////////////////////////////////////////////////////////////
9
10 rxvt_display::rxvt_display (const char *name)
11 : x_watcher (this, &rxvt_display::x_event)
12 {
13 this->name = STRDUP (name);
14 }
15
16 rxvt_display::~rxvt_display ()
17 {
18 free (name);
19 }
20
21 bool rxvt_display::open ()
22 {
23 display = XOpenDisplay (name);
24
25 screen = DefaultScreen (display);
26 root = DefaultRootWindow (display);
27 visual = DefaultVisual (display, screen);
28 cmap = DefaultColormap (display, screen);
29 depth = DefaultDepth (display, screen);
30
31 #ifdef PREFER_24BIT
32 /*
33 * If depth is not 24, look for a 24bit visual.
34 */
35 if (depth != 24)
36 {
37 XVisualInfo vinfo;
38
39 if (XMatchVisualInfo (display, screen, 24, TrueColor, &vinfo))
40 {
41 depth = 24;
42 visual = vinfo.visual;
43 cmap = XCreateColormap (display,
44 RootWindow (display, screen),
45 visual, AllocNone);
46 }
47 }
48 #endif
49
50 int fd = XConnectionNumber (display);
51 x_watcher.start (fd, EVENT_READ);
52 fcntl (fd, F_SETFL, FD_CLOEXEC);
53
54 return true;
55 }
56
57 void rxvt_display::close ()
58 {
59 x_watcher.stop ();
60
61 XCloseDisplay (display);
62 }
63
64 void rxvt_display::x_event (io_watcher &w, short revents)
65 {
66 do
67 {
68 XEvent xev;
69 XNextEvent (display, &xev);
70
71 for (int i = xw.size (); i--; )
72 {
73 if (!xw[i])
74 xw.erase_unordered (i);
75 else if (xw[i]->window == xev.xany.window)
76 xw[i]->call (xev);
77 }
78 }
79 while (XPending (display));
80 }
81
82 void rxvt_display::reg (xevent_watcher *w)
83 {
84 xw.push_back (w);
85 w->active = xw.size ();
86 }
87
88 void rxvt_display::unreg (xevent_watcher *w)
89 {
90 if (w->active)
91 xw[w->active - 1] = 0;
92 }
93
94 /////////////////////////////////////////////////////////////////////////////
95
96 rxvt_displays displays;
97
98 rxvt_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
124 void 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
134 /////////////////////////////////////////////////////////////////////////////
135
136 bool
137 rxvt_color::set (rxvt_display *display, Pixel p)
138 {
139 #if XFT
140 XColor xc;
141
142 xc.pixel = p;
143 if (!XQueryColor (display->display, display->cmap, &xc))
144 return false;
145
146 XRenderColor d;
147
148 d.red = xc.red;
149 d.green = xc.green;
150 d.blue = xc.blue;
151 d.alpha = 0xffff;
152
153 return
154 XftColorAllocValue (display->display,
155 display->visual,
156 display->cmap,
157 &d, &c);
158 #else
159 this->p = p;
160 #endif
161
162 return true;
163 }
164
165 bool
166 rxvt_color::set (rxvt_display *display, const char *name)
167 {
168 XColor xc;
169
170 if (XParseColor (display->display, display->cmap, name, &xc))
171 return set (display, xc.red, xc.green, xc.blue);
172
173 return false;
174 }
175
176 bool
177 rxvt_color::set (rxvt_display *display, unsigned short cr, unsigned short cg, unsigned short cb)
178 {
179 XColor xc;
180
181 xc.red = cr;
182 xc.green = cg;
183 xc.blue = cb;
184 xc.flags = DoRed | DoGreen | DoBlue;
185
186 if (XAllocColor (display->display, display->cmap, &xc))
187 return set (display, xc.pixel);
188
189 return false;
190 }
191
192 void
193 rxvt_color::get (rxvt_display *display, unsigned short &cr, unsigned short &cg, unsigned short &cb)
194 {
195 #if XFT
196 cr = c.color.red;
197 cg = c.color.green;
198 cb = c.color.blue;
199 #else
200 XColor c;
201
202 c.pixel = p;
203 XQueryColor (display->display, display->cmap, &c);
204
205 cr = c.red;
206 cg = c.green;
207 cb = c.blue;
208 #endif
209 }
210
211 void
212 rxvt_color::free (rxvt_display *display)
213 {
214 #if XFT
215 XftColorFree (display->display, display->visual, display->cmap, &c);
216 #else
217 XFreeColors (display->display, display->cmap, &c, 1, AllPlanes);
218 #endif
219 }
220