1 | #include "../config.h" |
1 | #include "../config.h" |
2 | #include <rxvt.h> |
2 | #include <rxvt.h> |
|
|
3 | #include <rxvtcolor.h> |
3 | |
4 | |
4 | // TODO: free colors again |
5 | #include <unistd.h> |
|
|
6 | #include <fcntl.h> |
5 | |
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 | |
6 | bool |
145 | bool |
7 | rxvt_color::set (rxvt_term *t, Pixel p) |
146 | rxvt_color::set (rxvt_display *display, Pixel p) |
8 | { |
147 | { |
9 | #if XFT |
148 | #if XFT |
10 | XColor xc; |
149 | XColor xc; |
11 | |
150 | |
12 | xc.pixel = p; |
151 | xc.pixel = p; |
13 | if (!XQueryColor (t->Xdisplay, t->Xcmap, &xc)) |
152 | if (!XQueryColor (display->display, display->cmap, &xc)) |
14 | return false; |
153 | return false; |
15 | |
154 | |
16 | XRenderColor d; |
155 | XRenderColor d; |
17 | |
156 | |
18 | d.red = xc.red; |
157 | d.red = xc.red; |
19 | d.green = xc.green; |
158 | d.green = xc.green; |
20 | d.blue = xc.blue; |
159 | d.blue = xc.blue; |
21 | d.alpha = 0xffff; |
160 | d.alpha = 0xffff; |
22 | |
161 | |
23 | return |
162 | return |
24 | XftColorAllocValue (t->Xdisplay, |
163 | XftColorAllocValue (display->display, |
25 | t->Xvisual, |
164 | display->visual, |
26 | t->Xcmap, |
165 | display->cmap, |
27 | &d, |
|
|
28 | &c); |
166 | &d, &c); |
29 | #else |
167 | #else |
30 | this->p = p; |
168 | this->p = p; |
31 | #endif |
169 | #endif |
32 | |
170 | |
33 | return true; |
171 | return true; |
34 | } |
172 | } |
35 | |
173 | |
36 | bool |
174 | bool |
37 | rxvt_color::set (rxvt_term *t, const char *name) |
175 | rxvt_color::set (rxvt_display *display, const char *name) |
38 | { |
176 | { |
39 | XColor xc; |
177 | XColor xc; |
40 | |
178 | |
41 | if (XParseColor (t->Xdisplay, t->Xcmap, name, &xc)) |
179 | if (XParseColor (display->display, display->cmap, name, &xc)) |
42 | return set (t, xc.red, xc.green, xc.blue); |
180 | return set (display, xc.red, xc.green, xc.blue); |
43 | |
181 | |
44 | return false; |
182 | return false; |
45 | } |
183 | } |
46 | |
184 | |
47 | bool |
185 | bool |
48 | rxvt_color::set (rxvt_term *t, unsigned short cr, unsigned short cg, unsigned short cb) |
186 | rxvt_color::set (rxvt_display *display, unsigned short cr, unsigned short cg, unsigned short cb) |
49 | { |
187 | { |
50 | XColor xc; |
188 | XColor xc; |
51 | |
189 | |
52 | xc.red = cr; |
190 | xc.red = cr; |
53 | xc.green = cg; |
191 | xc.green = cg; |
54 | xc.blue = cb; |
192 | xc.blue = cb; |
55 | xc.flags = DoRed | DoGreen | DoBlue; |
193 | xc.flags = DoRed | DoGreen | DoBlue; |
56 | |
194 | |
57 | if (XAllocColor (t->Xdisplay, t->Xcmap, &xc)) |
195 | if (XAllocColor (display->display, display->cmap, &xc)) |
58 | return set (t, xc.pixel); |
196 | return set (display, xc.pixel); |
59 | |
197 | |
60 | return false; |
198 | return false; |
61 | } |
199 | } |
62 | |
200 | |
63 | void |
201 | void |
64 | rxvt_color::get (rxvt_term *t, unsigned short &cr, unsigned short &cg, unsigned short &cb) |
202 | rxvt_color::get (rxvt_display *display, unsigned short &cr, unsigned short &cg, unsigned short &cb) |
65 | { |
203 | { |
66 | #if XFT |
204 | #if XFT |
67 | cr = c.color.red; |
205 | cr = c.color.red; |
68 | cg = c.color.green; |
206 | cg = c.color.green; |
69 | cb = c.color.blue; |
207 | cb = c.color.blue; |
70 | #else |
208 | #else |
71 | XColor c; |
209 | XColor c; |
72 | |
210 | |
73 | c.pixel = p; |
211 | c.pixel = p; |
74 | XQueryColor (t->Xdisplay, t->Xcmap, &c); |
212 | XQueryColor (display->display, display->cmap, &c); |
75 | |
213 | |
76 | cr = c.red; |
214 | cr = c.red; |
77 | cg = c.green; |
215 | cg = c.green; |
78 | cb = c.blue; |
216 | cb = c.blue; |
79 | #endif |
217 | #endif |
80 | } |
218 | } |
81 | |
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 | |