ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/rxvt-unicode/src/rxvtcolor.C
(Generate patch)

Comparing rxvt-unicode/src/rxvtcolor.C (file contents):
Revision 1.5 by pcg, Tue Feb 10 00:40:39 2004 UTC vs.
Revision 1.14 by pcg, Thu Apr 8 20:31:45 2004 UTC

1/*--------------------------------*-C-*---------------------------------*
2 * File: rxvtcolor.C
3 *----------------------------------------------------------------------*
4 *
5 * All portions of code are copyright by their respective author/s.
6 * Copyright (c) 2003-2004 Marc Lehmann <pcg@goof.com>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 *----------------------------------------------------------------------*/
22
1#include "../config.h" 23#include "../config.h"
2#include <rxvt.h> 24#include <rxvt.h>
3#include <rxvtcolor.h> 25#include <rxvtcolor.h>
4 26
5#include <unistd.h> 27#include <unistd.h>
6#include <fcntl.h> 28#include <fcntl.h>
7 29
30class byteorder byteorder;
31
32byteorder::byteorder ()
33{
34 union {
35 uint32_t u;
36 uint8_t b[4];
37 } w;
38
39 w.b[0] = 0x11;
40 w.b[1] = 0x22;
41 w.b[2] = 0x33;
42 w.b[3] = 0x44;
43
44 e = w.u;
45}
46
47refcounted::refcounted (const char *id)
48{
49 this->id = STRDUP (id);
50}
51
52refcounted::~refcounted ()
53{
54 free (id);
55}
56
57template<class T>
58T *refcache<T>::get (const char *id)
59{
60 for (T **i = begin (); i < end (); ++i)
61 {
62 if (!strcmp (id, (*i)->id))
63 {
64 (*i)->referenced++;
65 return *i;
66 }
67 }
68
69 T *obj = new T (id);
70
71 obj->referenced = 1;
72
73 if (obj && obj->init ())
74 {
75 push_back (obj);
76 return obj;
77 }
78 else
79 {
80 delete obj;
81 return 0;
82 }
83}
84
85template<class T>
86void refcache<T>::put (T *obj)
87{
88 if (!obj)
89 return;
90
91 if (!--obj->referenced)
92 {
93 erase (find (begin (), end (), obj));
94 delete obj;
95 }
96}
97
98template<class T>
99refcache<T>::~refcache ()
100{
101 while (size ())
102 put (*begin ());
103}
104
8///////////////////////////////////////////////////////////////////////////// 105/////////////////////////////////////////////////////////////////////////////
9 106
107static void
108im_destroy_cb (XIM unused1, XPointer client_data, XPointer unused3)
109{
110 rxvt_xim *xim = (rxvt_xim *)client_data;
111 rxvt_display *display = xim->display;
112
113 display->xims.erase (find (display->xims.begin (), display->xims.end (), xim));
114
115 display->im_change_cb ();
116}
117
118bool rxvt_xim::init ()
119{
120 display = GET_R->display; //HACK: TODO
121
122 xim = XOpenIM (display->display, NULL, NULL, NULL);
123
124 if (!xim)
125 return false;
126
127 XIMCallback ximcallback;
128 ximcallback.client_data = (XPointer)this;
129 ximcallback.callback = im_destroy_cb;
130
131 XSetIMValues (xim, XNDestroyCallback, &ximcallback, NULL);
132
133 return true;
134}
135
136rxvt_xim::~rxvt_xim ()
137{
138 if (xim)
139 XCloseIM (xim);
140}
141
142/////////////////////////////////////////////////////////////////////////////
143
10rxvt_display::rxvt_display (const char *name) 144rxvt_display::rxvt_display (const char *id)
145: refcounted (id)
11: x_watcher (this, &rxvt_display::x_event) 146, x_ev (this, &rxvt_display::x_cb)
12, selection_owner (0) 147, selection_owner (0)
13{ 148{
14 this->name = STRDUP (name);
15} 149}
16 150
17rxvt_display::~rxvt_display ()
18{
19 free (name);
20}
21
22bool rxvt_display::open () 151bool rxvt_display::init ()
23{ 152{
24 display = XOpenDisplay (name); 153 display = XOpenDisplay (id);
154
155 if (!display)
156 return false;
25 157
26 screen = DefaultScreen (display); 158 screen = DefaultScreen (display);
27 root = DefaultRootWindow (display); 159 root = DefaultRootWindow (display);
28 visual = DefaultVisual (display, screen); 160 visual = DefaultVisual (display, screen);
29 cmap = DefaultColormap (display, screen); 161 cmap = DefaultColormap (display, screen);
47 } 179 }
48 } 180 }
49#endif 181#endif
50 182
51 int fd = XConnectionNumber (display); 183 int fd = XConnectionNumber (display);
52 x_watcher.start (fd, EVENT_READ); 184 x_ev.start (fd, EVENT_READ);
53 fcntl (fd, F_SETFL, FD_CLOEXEC); 185 fcntl (fd, F_SETFD, FD_CLOEXEC);
186
187 XSelectInput (display, root, PropertyChangeMask);
188 xa_xim_servers = XInternAtom (display, "XIM_SERVERS", 0);
189
190 flush ();
54 191
55 return true; 192 return true;
56} 193}
57 194
58void rxvt_display::close () 195rxvt_display::~rxvt_display ()
59{ 196{
60 x_watcher.stop (); 197 x_ev.stop ();
61 198
199 if (display)
62 XCloseDisplay (display); 200 XCloseDisplay (display);
63} 201}
64 202
203void rxvt_display::im_change_cb ()
204{
205 for (im_watcher **i = imw.begin (); i != imw.end (); ++i)
206 (*i)->call ();
207}
208
65void rxvt_display::x_event (io_watcher &w, short revents) 209void rxvt_display::x_cb (io_watcher &w, short revents)
66{ 210{
67 do 211 do
68 { 212 {
69 XEvent xev; 213 XEvent xev;
70 XNextEvent (display, &xev); 214 XNextEvent (display, &xev);
215
216 //printf ("T %d w %lx\n", xev.type, xev.xany.window);//D
217
218 if (xev.type == PropertyNotify
219 && xev.xany.window == root
220 && xev.xproperty.atom == xa_xim_servers)
221 im_change_cb ();
71 222
72 for (int i = xw.size (); i--; ) 223 for (int i = xw.size (); i--; )
73 { 224 {
74 if (!xw[i]) 225 if (!xw[i])
75 xw.erase_unordered (i); 226 xw.erase_unordered (i);
76 else if (xw[i]->window == xev.xany.window) 227 else if (xw[i]->window == xev.xany.window)
77 xw[i]->call (xev); 228 xw[i]->call (xev);
78 } 229 }
79 } 230 }
80 while (XPending (display)); 231 while (XPending (display));
232
233 flush ();
234}
235
236void rxvt_display::flush ()
237{
238 for (;;)
239 {
240 XFlush (display);
241
242 if (!XPending (display))
243 break;
244
245 x_cb (x_ev, 0);
246 }
81} 247}
82 248
83void rxvt_display::reg (xevent_watcher *w) 249void rxvt_display::reg (xevent_watcher *w)
84{ 250{
85 xw.push_back (w); 251 xw.push_back (w);
90{ 256{
91 if (w->active) 257 if (w->active)
92 xw[w->active - 1] = 0; 258 xw[w->active - 1] = 0;
93} 259}
94 260
261void rxvt_display::reg (im_watcher *w)
262{
263 imw.push_back (w);
264}
265
266void rxvt_display::unreg (im_watcher *w)
267{
268 imw.erase (find (imw.begin (), imw.end (), w));
269}
270
95void rxvt_display::set_selection_owner (rxvt_term *owner) 271void rxvt_display::set_selection_owner (rxvt_term *owner)
96{ 272{
97 if (selection_owner && selection_owner != owner) 273 if (selection_owner && selection_owner != owner)
98 selection_owner->selection_clear (); 274 selection_owner->selection_clear ();
99 275
100 selection_owner = owner; 276 selection_owner = owner;
101} 277}
102 278
279rxvt_xim *rxvt_display::get_xim (const char *locale, const char *modifiers)
280{
281 char *id;
282 int l, m;
283
284 l = strlen (locale);
285 m = strlen (modifiers);
286
287 if (!(id = (char *)malloc (l + m + 2)))
288 return 0;
289
290 memcpy (id, locale, l); id[l] = '\n';
291 memcpy (id + l + 1, modifiers, m); id[l + m + 1] = 0;
292
293 rxvt_xim *xim = xims.get (id);
294
295 free (id);
296
297 return xim;
298}
299
300void rxvt_display::put_xim (rxvt_xim *xim)
301{
302 xims.put (xim);
303}
304
305Atom rxvt_display::atom (const char *name)
306{
307 return XInternAtom (display, name, False);
308}
309
103///////////////////////////////////////////////////////////////////////////// 310/////////////////////////////////////////////////////////////////////////////
104 311
312template refcache<rxvt_display>;
105rxvt_displays displays; 313refcache<rxvt_display> displays;
106
107rxvt_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
133void 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 314
143///////////////////////////////////////////////////////////////////////////// 315/////////////////////////////////////////////////////////////////////////////
144 316
145bool 317bool
146rxvt_color::set (rxvt_display *display, Pixel p) 318rxvt_color::set (rxvt_display *display, Pixel p)
221rxvt_color::free (rxvt_display *display) 393rxvt_color::free (rxvt_display *display)
222{ 394{
223#if XFT 395#if XFT
224 XftColorFree (display->display, display->visual, display->cmap, &c); 396 XftColorFree (display->display, display->visual, display->cmap, &c);
225#else 397#else
226 XFreeColors (display->display, display->cmap, &c, 1, AllPlanes); 398 XFreeColors (display->display, display->cmap, &p, 1, AllPlanes);
227#endif 399#endif
228} 400}
229 401

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines