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