… | |
… | |
3 | #include <rxvtcolor.h> |
3 | #include <rxvtcolor.h> |
4 | |
4 | |
5 | #include <unistd.h> |
5 | #include <unistd.h> |
6 | #include <fcntl.h> |
6 | #include <fcntl.h> |
7 | |
7 | |
|
|
8 | class byteorder byteorder; |
|
|
9 | |
|
|
10 | byteorder::byteorder () |
|
|
11 | { |
|
|
12 | union { |
|
|
13 | uint32_t u; |
|
|
14 | uint8_t b[4]; |
|
|
15 | } w; |
|
|
16 | |
|
|
17 | w.b[0] = 0x11; |
|
|
18 | w.b[1] = 0x22; |
|
|
19 | w.b[2] = 0x33; |
|
|
20 | w.b[3] = 0x44; |
|
|
21 | |
|
|
22 | e = w.u; |
|
|
23 | } |
|
|
24 | |
|
|
25 | refcounted::refcounted (const char *id) |
|
|
26 | { |
|
|
27 | this->id = STRDUP (id); |
|
|
28 | } |
|
|
29 | |
|
|
30 | refcounted::~refcounted () |
|
|
31 | { |
|
|
32 | free (id); |
|
|
33 | } |
|
|
34 | |
|
|
35 | template<class T> |
|
|
36 | T *refcache<T>::get (const char *id) |
|
|
37 | { |
|
|
38 | for (T **i = begin (); i < end (); ++i) |
|
|
39 | { |
|
|
40 | if (!strcmp (id, (*i)->id)) |
|
|
41 | { |
|
|
42 | (*i)->referenced++; |
|
|
43 | return *i; |
|
|
44 | } |
|
|
45 | } |
|
|
46 | |
|
|
47 | T *obj = new T (id); |
|
|
48 | |
|
|
49 | obj->referenced = 1; |
|
|
50 | |
|
|
51 | if (obj && obj->init ()) |
|
|
52 | { |
|
|
53 | push_back (obj); |
|
|
54 | return obj; |
|
|
55 | } |
|
|
56 | else |
|
|
57 | { |
|
|
58 | delete obj; |
|
|
59 | return 0; |
|
|
60 | } |
|
|
61 | } |
|
|
62 | |
|
|
63 | template<class T> |
|
|
64 | void refcache<T>::put (T *obj) |
|
|
65 | { |
|
|
66 | if (!obj) |
|
|
67 | return; |
|
|
68 | |
|
|
69 | if (!--obj->referenced) |
|
|
70 | { |
|
|
71 | erase (find (begin (), end (), obj)); |
|
|
72 | delete obj; |
|
|
73 | } |
|
|
74 | } |
|
|
75 | |
|
|
76 | template<class T> |
|
|
77 | refcache<T>::~refcache () |
|
|
78 | { |
|
|
79 | while (size ()) |
|
|
80 | put (*begin ()); |
|
|
81 | } |
|
|
82 | |
8 | ///////////////////////////////////////////////////////////////////////////// |
83 | ///////////////////////////////////////////////////////////////////////////// |
9 | |
84 | |
|
|
85 | static void |
|
|
86 | im_destroy_cb (XIM unused1, XPointer client_data, XPointer unused3) |
|
|
87 | { |
|
|
88 | rxvt_xim *xim = (rxvt_xim *)client_data; |
|
|
89 | rxvt_display *display = xim->display; |
|
|
90 | |
|
|
91 | display->xims.erase (find (display->xims.begin (), display->xims.end (), xim)); |
|
|
92 | |
|
|
93 | display->im_change_cb (); |
|
|
94 | } |
|
|
95 | |
|
|
96 | bool rxvt_xim::init () |
|
|
97 | { |
|
|
98 | display = GET_R->display; //HACK: TODO |
|
|
99 | |
|
|
100 | xim = XOpenIM (display->display, NULL, NULL, NULL); |
|
|
101 | |
|
|
102 | if (!xim) |
|
|
103 | return false; |
|
|
104 | |
|
|
105 | XIMCallback ximcallback; |
|
|
106 | ximcallback.client_data = (XPointer)this; |
|
|
107 | ximcallback.callback = im_destroy_cb; |
|
|
108 | |
|
|
109 | XSetIMValues (xim, XNDestroyCallback, &ximcallback, NULL); |
|
|
110 | |
|
|
111 | return true; |
|
|
112 | } |
|
|
113 | |
|
|
114 | rxvt_xim::~rxvt_xim () |
|
|
115 | { |
|
|
116 | if (xim) |
|
|
117 | XCloseIM (xim); |
|
|
118 | } |
|
|
119 | |
|
|
120 | ///////////////////////////////////////////////////////////////////////////// |
|
|
121 | |
10 | rxvt_display::rxvt_display (const char *name) |
122 | rxvt_display::rxvt_display (const char *id) |
|
|
123 | : refcounted (id) |
11 | : x_watcher (this, &rxvt_display::x_event) |
124 | , x_ev (this, &rxvt_display::x_cb) |
12 | , selection_owner (0) |
125 | , selection_owner (0) |
13 | { |
126 | { |
14 | this->name = STRDUP (name); |
|
|
15 | } |
127 | } |
16 | |
128 | |
17 | rxvt_display::~rxvt_display () |
|
|
18 | { |
|
|
19 | free (name); |
|
|
20 | } |
|
|
21 | |
|
|
22 | bool rxvt_display::open () |
129 | bool rxvt_display::init () |
23 | { |
130 | { |
24 | display = XOpenDisplay (name); |
131 | display = XOpenDisplay (id); |
|
|
132 | |
|
|
133 | if (!display) |
|
|
134 | return false; |
25 | |
135 | |
26 | screen = DefaultScreen (display); |
136 | screen = DefaultScreen (display); |
27 | root = DefaultRootWindow (display); |
137 | root = DefaultRootWindow (display); |
28 | visual = DefaultVisual (display, screen); |
138 | visual = DefaultVisual (display, screen); |
29 | cmap = DefaultColormap (display, screen); |
139 | cmap = DefaultColormap (display, screen); |
… | |
… | |
47 | } |
157 | } |
48 | } |
158 | } |
49 | #endif |
159 | #endif |
50 | |
160 | |
51 | int fd = XConnectionNumber (display); |
161 | int fd = XConnectionNumber (display); |
52 | x_watcher.start (fd, EVENT_READ); |
162 | x_ev.start (fd, EVENT_READ); |
53 | fcntl (fd, F_SETFL, FD_CLOEXEC); |
163 | fcntl (fd, F_SETFL, FD_CLOEXEC); |
54 | |
164 | |
|
|
165 | XSelectInput (display, root, PropertyChangeMask); |
|
|
166 | xa_xim_servers = XInternAtom (display, "XIM_SERVERS", 0); |
|
|
167 | |
|
|
168 | flush (); |
|
|
169 | |
55 | return true; |
170 | return true; |
56 | } |
171 | } |
57 | |
172 | |
58 | void rxvt_display::close () |
173 | rxvt_display::~rxvt_display () |
59 | { |
174 | { |
60 | x_watcher.stop (); |
175 | x_ev.stop (); |
61 | |
176 | |
|
|
177 | if (display) |
62 | XCloseDisplay (display); |
178 | XCloseDisplay (display); |
63 | } |
179 | } |
64 | |
180 | |
|
|
181 | void rxvt_display::im_change_cb () |
|
|
182 | { |
|
|
183 | for (im_watcher **i = imw.begin (); i != imw.end (); ++i) |
|
|
184 | (*i)->call (); |
|
|
185 | } |
|
|
186 | |
65 | void rxvt_display::x_event (io_watcher &w, short revents) |
187 | void rxvt_display::x_cb (io_watcher &w, short revents) |
66 | { |
188 | { |
67 | do |
189 | do |
68 | { |
190 | { |
69 | XEvent xev; |
191 | XEvent xev; |
70 | XNextEvent (display, &xev); |
192 | XNextEvent (display, &xev); |
|
|
193 | |
|
|
194 | //printf ("T %d w %lx\n", xev.type, xev.xany.window);//D |
|
|
195 | |
|
|
196 | if (xev.type == PropertyNotify |
|
|
197 | && xev.xany.window == root |
|
|
198 | && xev.xproperty.atom == xa_xim_servers) |
|
|
199 | im_change_cb (); |
71 | |
200 | |
72 | for (int i = xw.size (); i--; ) |
201 | for (int i = xw.size (); i--; ) |
73 | { |
202 | { |
74 | if (!xw[i]) |
203 | if (!xw[i]) |
75 | xw.erase_unordered (i); |
204 | xw.erase_unordered (i); |
76 | else if (xw[i]->window == xev.xany.window) |
205 | else if (xw[i]->window == xev.xany.window) |
77 | xw[i]->call (xev); |
206 | xw[i]->call (xev); |
78 | } |
207 | } |
79 | } |
208 | } |
80 | while (XPending (display)); |
209 | while (XPending (display)); |
|
|
210 | |
|
|
211 | flush (); |
|
|
212 | } |
|
|
213 | |
|
|
214 | void rxvt_display::flush () |
|
|
215 | { |
|
|
216 | for (;;) |
|
|
217 | { |
|
|
218 | XFlush (display); |
|
|
219 | |
|
|
220 | if (!XPending (display)) |
|
|
221 | break; |
|
|
222 | |
|
|
223 | x_cb (x_ev, 0); |
|
|
224 | } |
81 | } |
225 | } |
82 | |
226 | |
83 | void rxvt_display::reg (xevent_watcher *w) |
227 | void rxvt_display::reg (xevent_watcher *w) |
84 | { |
228 | { |
85 | xw.push_back (w); |
229 | xw.push_back (w); |
… | |
… | |
90 | { |
234 | { |
91 | if (w->active) |
235 | if (w->active) |
92 | xw[w->active - 1] = 0; |
236 | xw[w->active - 1] = 0; |
93 | } |
237 | } |
94 | |
238 | |
|
|
239 | void rxvt_display::reg (im_watcher *w) |
|
|
240 | { |
|
|
241 | imw.push_back (w); |
|
|
242 | } |
|
|
243 | |
|
|
244 | void rxvt_display::unreg (im_watcher *w) |
|
|
245 | { |
|
|
246 | imw.erase (find (imw.begin (), imw.end (), w)); |
|
|
247 | } |
|
|
248 | |
95 | void rxvt_display::set_selection_owner (rxvt_term *owner) |
249 | void rxvt_display::set_selection_owner (rxvt_term *owner) |
96 | { |
250 | { |
97 | if (selection_owner && selection_owner != owner) |
251 | if (selection_owner && selection_owner != owner) |
98 | selection_owner->selection_clear (); |
252 | selection_owner->selection_clear (); |
99 | |
253 | |
100 | selection_owner = owner; |
254 | selection_owner = owner; |
101 | } |
255 | } |
102 | |
256 | |
|
|
257 | rxvt_xim *rxvt_display::get_xim (const char *locale, const char *modifiers) |
|
|
258 | { |
|
|
259 | char *id; |
|
|
260 | int l, m; |
|
|
261 | |
|
|
262 | l = strlen (locale); |
|
|
263 | m = strlen (modifiers); |
|
|
264 | |
|
|
265 | if (!(id = (char *)malloc (l + m + 2))) |
|
|
266 | return 0; |
|
|
267 | |
|
|
268 | memcpy (id, locale, l); id[l] = '\n'; |
|
|
269 | memcpy (id + l + 1, modifiers, m); id[l + m + 1] = 0; |
|
|
270 | |
|
|
271 | rxvt_xim *xim = xims.get (id); |
|
|
272 | |
|
|
273 | free (id); |
|
|
274 | |
|
|
275 | return xim; |
|
|
276 | } |
|
|
277 | |
|
|
278 | void rxvt_display::put_xim (rxvt_xim *xim) |
|
|
279 | { |
|
|
280 | xims.put (xim); |
|
|
281 | } |
|
|
282 | |
|
|
283 | Atom rxvt_display::atom (const char *name) |
|
|
284 | { |
|
|
285 | return XInternAtom (display, name, False); |
|
|
286 | } |
|
|
287 | |
103 | ///////////////////////////////////////////////////////////////////////////// |
288 | ///////////////////////////////////////////////////////////////////////////// |
104 | |
289 | |
|
|
290 | template refcache<rxvt_display>; |
105 | rxvt_displays displays; |
291 | refcache<rxvt_display> 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 | |
292 | |
143 | ///////////////////////////////////////////////////////////////////////////// |
293 | ///////////////////////////////////////////////////////////////////////////// |
144 | |
294 | |
145 | bool |
295 | bool |
146 | rxvt_color::set (rxvt_display *display, Pixel p) |
296 | rxvt_color::set (rxvt_display *display, Pixel p) |
… | |
… | |
221 | rxvt_color::free (rxvt_display *display) |
371 | rxvt_color::free (rxvt_display *display) |
222 | { |
372 | { |
223 | #if XFT |
373 | #if XFT |
224 | XftColorFree (display->display, display->visual, display->cmap, &c); |
374 | XftColorFree (display->display, display->visual, display->cmap, &c); |
225 | #else |
375 | #else |
226 | XFreeColors (display->display, display->cmap, &c, 1, AllPlanes); |
376 | XFreeColors (display->display, display->cmap, &p, 1, AllPlanes); |
227 | #endif |
377 | #endif |
228 | } |
378 | } |
229 | |
379 | |