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