… | |
… | |
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 | refcounted::refcounted (const char *id) |
|
|
9 | { |
|
|
10 | this->id = STRDUP (id); |
|
|
11 | } |
|
|
12 | |
|
|
13 | refcounted::~refcounted () |
|
|
14 | { |
|
|
15 | free (id); |
|
|
16 | } |
|
|
17 | |
|
|
18 | template<class T> |
|
|
19 | T *refcache<T>::get (const char *id) |
|
|
20 | { |
|
|
21 | for (T **i = begin (); i < end (); ++i) |
|
|
22 | { |
|
|
23 | if (!strcmp (id, (*i)->id)) |
|
|
24 | { |
|
|
25 | (*i)->referenced++; |
|
|
26 | return *i; |
|
|
27 | } |
|
|
28 | } |
|
|
29 | |
|
|
30 | T *obj = new T (id); |
|
|
31 | |
|
|
32 | obj->referenced = 1; |
|
|
33 | |
|
|
34 | if (obj && obj->init ()) |
|
|
35 | { |
|
|
36 | push_back (obj); |
|
|
37 | return obj; |
|
|
38 | } |
|
|
39 | else |
|
|
40 | { |
|
|
41 | delete obj; |
|
|
42 | return 0; |
|
|
43 | } |
|
|
44 | } |
|
|
45 | |
|
|
46 | template<class T> |
|
|
47 | void refcache<T>::put (T *obj) |
|
|
48 | { |
|
|
49 | if (!obj) |
|
|
50 | return; |
|
|
51 | |
|
|
52 | if (!--obj->referenced) |
|
|
53 | { |
|
|
54 | erase (find (begin (), end (), obj)); |
|
|
55 | delete obj; |
|
|
56 | } |
|
|
57 | } |
|
|
58 | |
|
|
59 | template<class T> |
|
|
60 | refcache<T>::~refcache () |
|
|
61 | { |
|
|
62 | while (size ()) |
|
|
63 | put (*begin ()); |
|
|
64 | } |
|
|
65 | |
8 | ///////////////////////////////////////////////////////////////////////////// |
66 | ///////////////////////////////////////////////////////////////////////////// |
9 | |
67 | |
|
|
68 | static void |
|
|
69 | im_destroy_cb (XIM unused1, XPointer client_data, XPointer unused3) |
|
|
70 | { |
|
|
71 | rxvt_xim *xim = (rxvt_xim *)client_data; |
|
|
72 | rxvt_display *display = xim->display; |
|
|
73 | |
|
|
74 | display->xims.erase (find (display->xims.begin (), display->xims.end (), xim)); |
|
|
75 | |
|
|
76 | display->im_change_cb (); |
|
|
77 | } |
|
|
78 | |
|
|
79 | bool rxvt_xim::init () |
|
|
80 | { |
|
|
81 | display = GET_R->display; //HACK: TODO |
|
|
82 | |
|
|
83 | xim = XOpenIM (display->display, NULL, NULL, NULL); |
|
|
84 | |
|
|
85 | if (!xim) |
|
|
86 | return false; |
|
|
87 | |
|
|
88 | XIMCallback ximcallback; |
|
|
89 | ximcallback.client_data = (XPointer)this; |
|
|
90 | ximcallback.callback = im_destroy_cb; |
|
|
91 | |
|
|
92 | XSetIMValues (xim, XNDestroyCallback, &ximcallback, NULL); |
|
|
93 | |
|
|
94 | return true; |
|
|
95 | } |
|
|
96 | |
|
|
97 | rxvt_xim::~rxvt_xim () |
|
|
98 | { |
|
|
99 | if (xim) |
|
|
100 | XCloseIM (xim); |
|
|
101 | } |
|
|
102 | |
|
|
103 | ///////////////////////////////////////////////////////////////////////////// |
|
|
104 | |
10 | rxvt_display::rxvt_display (const char *name) |
105 | rxvt_display::rxvt_display (const char *id) |
|
|
106 | : refcounted (id) |
11 | : x_watcher (this, &rxvt_display::x_event) |
107 | , x_ev (this, &rxvt_display::x_cb) |
12 | , selection_owner (0) |
108 | , selection_owner (0) |
13 | { |
109 | { |
14 | this->name = STRDUP (name); |
|
|
15 | } |
110 | } |
16 | |
111 | |
17 | rxvt_display::~rxvt_display () |
|
|
18 | { |
|
|
19 | free (name); |
|
|
20 | } |
|
|
21 | |
|
|
22 | bool rxvt_display::open () |
112 | bool rxvt_display::init () |
23 | { |
113 | { |
24 | display = XOpenDisplay (name); |
114 | display = XOpenDisplay (id); |
25 | |
115 | |
26 | screen = DefaultScreen (display); |
116 | screen = DefaultScreen (display); |
27 | root = DefaultRootWindow (display); |
117 | root = DefaultRootWindow (display); |
28 | visual = DefaultVisual (display, screen); |
118 | visual = DefaultVisual (display, screen); |
29 | cmap = DefaultColormap (display, screen); |
119 | cmap = DefaultColormap (display, screen); |
… | |
… | |
47 | } |
137 | } |
48 | } |
138 | } |
49 | #endif |
139 | #endif |
50 | |
140 | |
51 | int fd = XConnectionNumber (display); |
141 | int fd = XConnectionNumber (display); |
52 | x_watcher.start (fd, EVENT_READ); |
142 | x_ev.start (fd, EVENT_READ); |
53 | fcntl (fd, F_SETFL, FD_CLOEXEC); |
143 | fcntl (fd, F_SETFL, FD_CLOEXEC); |
54 | |
144 | |
|
|
145 | XSelectInput (display, root, PropertyChangeMask); |
|
|
146 | xa_xim_servers = XInternAtom (display, "XIM_SERVERS", 0); |
|
|
147 | |
|
|
148 | flush (); |
|
|
149 | |
55 | return true; |
150 | return true; |
56 | } |
151 | } |
57 | |
152 | |
58 | void rxvt_display::close () |
153 | rxvt_display::~rxvt_display () |
59 | { |
154 | { |
60 | x_watcher.stop (); |
155 | x_ev.stop (); |
61 | |
156 | |
62 | XCloseDisplay (display); |
157 | XCloseDisplay (display); |
63 | } |
158 | } |
64 | |
159 | |
|
|
160 | void rxvt_display::im_change_cb () |
|
|
161 | { |
|
|
162 | for (im_watcher **i = imw.begin (); i != imw.end (); ++i) |
|
|
163 | (*i)->call (); |
|
|
164 | } |
|
|
165 | |
65 | void rxvt_display::x_event (io_watcher &w, short revents) |
166 | void rxvt_display::x_cb (io_watcher &w, short revents) |
66 | { |
167 | { |
67 | do |
168 | do |
68 | { |
169 | { |
69 | XEvent xev; |
170 | XEvent xev; |
70 | XNextEvent (display, &xev); |
171 | XNextEvent (display, &xev); |
|
|
172 | |
|
|
173 | //printf ("T %d w %lx\n", xev.type, xev.xany.window);//D |
|
|
174 | |
|
|
175 | if (xev.type == PropertyNotify |
|
|
176 | && xev.xany.window == root |
|
|
177 | && xev.xproperty.atom == xa_xim_servers) |
|
|
178 | im_change_cb (); |
71 | |
179 | |
72 | for (int i = xw.size (); i--; ) |
180 | for (int i = xw.size (); i--; ) |
73 | { |
181 | { |
74 | if (!xw[i]) |
182 | if (!xw[i]) |
75 | xw.erase_unordered (i); |
183 | xw.erase_unordered (i); |
76 | else if (xw[i]->window == xev.xany.window) |
184 | else if (xw[i]->window == xev.xany.window) |
77 | xw[i]->call (xev); |
185 | xw[i]->call (xev); |
78 | } |
186 | } |
79 | } |
187 | } |
80 | while (XPending (display)); |
188 | while (XPending (display)); |
|
|
189 | |
|
|
190 | flush (); |
|
|
191 | } |
|
|
192 | |
|
|
193 | void rxvt_display::flush () |
|
|
194 | { |
|
|
195 | for (;;) |
|
|
196 | { |
|
|
197 | XFlush (display); |
|
|
198 | |
|
|
199 | if (!XPending (display)) |
|
|
200 | break; |
|
|
201 | |
|
|
202 | x_cb (x_ev, 0); |
|
|
203 | } |
81 | } |
204 | } |
82 | |
205 | |
83 | void rxvt_display::reg (xevent_watcher *w) |
206 | void rxvt_display::reg (xevent_watcher *w) |
84 | { |
207 | { |
85 | xw.push_back (w); |
208 | xw.push_back (w); |
… | |
… | |
90 | { |
213 | { |
91 | if (w->active) |
214 | if (w->active) |
92 | xw[w->active - 1] = 0; |
215 | xw[w->active - 1] = 0; |
93 | } |
216 | } |
94 | |
217 | |
|
|
218 | void rxvt_display::reg (im_watcher *w) |
|
|
219 | { |
|
|
220 | imw.push_back (w); |
|
|
221 | } |
|
|
222 | |
|
|
223 | void rxvt_display::unreg (im_watcher *w) |
|
|
224 | { |
|
|
225 | imw.erase (find (imw.begin (), imw.end (), w)); |
|
|
226 | } |
|
|
227 | |
95 | void rxvt_display::set_selection_owner (rxvt_term *owner) |
228 | void rxvt_display::set_selection_owner (rxvt_term *owner) |
96 | { |
229 | { |
97 | if (selection_owner && selection_owner != owner) |
230 | if (selection_owner && selection_owner != owner) |
98 | selection_owner->selection_clear (); |
231 | selection_owner->selection_clear (); |
99 | |
232 | |
100 | selection_owner = owner; |
233 | selection_owner = owner; |
101 | } |
234 | } |
102 | |
235 | |
|
|
236 | rxvt_xim *rxvt_display::get_xim (const char *locale, const char *modifiers) |
|
|
237 | { |
|
|
238 | // asprintf is a GNU and *BSD extension.. sorry... |
|
|
239 | char *id; |
|
|
240 | |
|
|
241 | if (asprintf (&id, "%s\n%s", locale, modifiers) < 0) |
|
|
242 | return 0; |
|
|
243 | |
|
|
244 | rxvt_xim *xim = xims.get (id); |
|
|
245 | |
|
|
246 | free (id); |
|
|
247 | |
|
|
248 | return xim; |
|
|
249 | } |
|
|
250 | |
|
|
251 | void rxvt_display::put_xim (rxvt_xim *xim) |
|
|
252 | { |
|
|
253 | xims.put (xim); |
|
|
254 | } |
|
|
255 | |
103 | ///////////////////////////////////////////////////////////////////////////// |
256 | ///////////////////////////////////////////////////////////////////////////// |
104 | |
257 | |
|
|
258 | template refcache<rxvt_display>; |
105 | rxvt_displays displays; |
259 | 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 | |
260 | |
143 | ///////////////////////////////////////////////////////////////////////////// |
261 | ///////////////////////////////////////////////////////////////////////////// |
144 | |
262 | |
145 | bool |
263 | bool |
146 | rxvt_color::set (rxvt_display *display, Pixel p) |
264 | rxvt_color::set (rxvt_display *display, Pixel p) |