… | |
… | |
25 | #include <rxvttoolkit.h> |
25 | #include <rxvttoolkit.h> |
26 | |
26 | |
27 | #include <unistd.h> |
27 | #include <unistd.h> |
28 | #include <fcntl.h> |
28 | #include <fcntl.h> |
29 | |
29 | |
|
|
30 | #include <sys/utsname.h> |
|
|
31 | |
30 | #ifndef NO_SLOW_LINK_SUPPORT |
32 | #ifndef NO_SLOW_LINK_SUPPORT |
31 | # include <sys/socket.h> |
33 | # include <sys/socket.h> |
32 | # include <sys/un.h> |
34 | # include <sys/un.h> |
33 | #endif |
35 | #endif |
34 | |
36 | |
|
|
37 | const char *const xa_names[] = |
|
|
38 | { |
|
|
39 | "TEXT", |
|
|
40 | "COMPOUND_TEXT", |
|
|
41 | "UTF8_STRING", |
|
|
42 | "MULTIPLE", |
|
|
43 | "TARGETS", |
|
|
44 | "TIMESTAMP", |
|
|
45 | "VT_SELECTION", |
|
|
46 | "INCR", |
|
|
47 | "WM_PROTOCOLS", |
|
|
48 | "WM_DELETE_WINDOW", |
|
|
49 | "CLIPBOARD", |
|
|
50 | #if ENABLE_FRILLS |
|
|
51 | "_MOTIF_WM_HINTS", |
|
|
52 | #endif |
|
|
53 | #if ENABLE_EWMH |
|
|
54 | "_NET_WM_PID", |
|
|
55 | "_NET_WM_NAME", |
|
|
56 | "_NET_WM_ICON_NAME", |
|
|
57 | "_NET_WM_PING", |
|
|
58 | #endif |
|
|
59 | #if USE_XIM |
|
|
60 | "WM_LOCALE_NAME", |
|
|
61 | "XIM_SERVERS", |
|
|
62 | #endif |
|
|
63 | #ifdef TRANSPARENT |
|
|
64 | "_XROOTPMAP_ID", |
|
|
65 | "ESETROOT_PMAP_ID", |
|
|
66 | #endif |
|
|
67 | #if ENABLE_XEMBED |
|
|
68 | "_XEMBED", |
|
|
69 | "_XEMBED_INFO", |
|
|
70 | #endif |
|
|
71 | }; |
|
|
72 | |
|
|
73 | ///////////////////////////////////////////////////////////////////////////// |
|
|
74 | |
35 | refcounted::refcounted (const char *id) |
75 | refcounted::refcounted (const char *id) |
36 | { |
76 | { |
37 | this->id = strdup (id); |
77 | this->id = strdup (id); |
38 | } |
78 | } |
39 | |
79 | |
… | |
… | |
47 | { |
87 | { |
48 | for (T **i = this->begin (); i < this->end (); ++i) |
88 | for (T **i = this->begin (); i < this->end (); ++i) |
49 | { |
89 | { |
50 | if (!strcmp (id, (*i)->id)) |
90 | if (!strcmp (id, (*i)->id)) |
51 | { |
91 | { |
52 | (*i)->referenced++; |
92 | ++(*i)->referenced; |
|
|
93 | (*i)->ref_next (); |
53 | return *i; |
94 | return *i; |
54 | } |
95 | } |
55 | } |
96 | } |
56 | |
97 | |
57 | T *obj = new T (id); |
98 | T *obj = new T (id); |
58 | |
99 | |
59 | obj->referenced = 1; |
|
|
60 | |
|
|
61 | if (obj && obj->init ()) |
100 | if (obj && obj->ref_init ()) |
62 | { |
101 | { |
|
|
102 | obj->referenced = 1; |
63 | this->push_back (obj); |
103 | this->push_back (obj); |
64 | return obj; |
104 | return obj; |
65 | } |
105 | } |
66 | else |
106 | else |
67 | { |
107 | { |
… | |
… | |
91 | } |
131 | } |
92 | |
132 | |
93 | ///////////////////////////////////////////////////////////////////////////// |
133 | ///////////////////////////////////////////////////////////////////////////// |
94 | |
134 | |
95 | #ifdef USE_XIM |
135 | #ifdef USE_XIM |
|
|
136 | |
96 | static void |
137 | static void |
97 | #if XIMCB_PROTO_BROKEN |
138 | #if XIMCB_PROTO_BROKEN |
98 | im_destroy_cb (XIC unused1, XPointer client_data, XPointer unused3) |
139 | im_destroy_cb (XIC unused1, XPointer client_data, XPointer unused3) |
99 | #else |
140 | #else |
100 | im_destroy_cb (XIM unused1, XPointer client_data, XPointer unused3) |
141 | im_destroy_cb (XIM unused1, XPointer client_data, XPointer unused3) |
… | |
… | |
107 | |
148 | |
108 | display->xims.erase (find (display->xims.begin (), display->xims.end (), xim)); |
149 | display->xims.erase (find (display->xims.begin (), display->xims.end (), xim)); |
109 | display->im_change_cb (); |
150 | display->im_change_cb (); |
110 | } |
151 | } |
111 | |
152 | |
|
|
153 | bool |
112 | bool rxvt_xim::init () |
154 | rxvt_xim::ref_init () |
113 | { |
155 | { |
114 | display = GET_R->display; //HACK: TODO |
156 | display = GET_R->display; //HACK: TODO |
115 | |
157 | |
116 | xim = XOpenIM (display->display, NULL, NULL, NULL); |
158 | xim = XOpenIM (display->display, NULL, NULL, NULL); |
117 | |
159 | |
… | |
… | |
130 | rxvt_xim::~rxvt_xim () |
172 | rxvt_xim::~rxvt_xim () |
131 | { |
173 | { |
132 | if (xim) |
174 | if (xim) |
133 | XCloseIM (xim); |
175 | XCloseIM (xim); |
134 | } |
176 | } |
|
|
177 | |
135 | #endif |
178 | #endif |
|
|
179 | |
|
|
180 | ///////////////////////////////////////////////////////////////////////////// |
|
|
181 | |
|
|
182 | void |
|
|
183 | rxvt_screen::set (rxvt_display *disp) |
|
|
184 | { |
|
|
185 | display = disp; |
|
|
186 | xdisp = disp->display; |
|
|
187 | |
|
|
188 | Screen *screen = ScreenOfDisplay (xdisp, disp->screen); |
|
|
189 | |
|
|
190 | depth = DefaultDepthOfScreen (screen); |
|
|
191 | visual = DefaultVisualOfScreen (screen); |
|
|
192 | cmap = DefaultColormapOfScreen (screen); |
|
|
193 | } |
|
|
194 | |
|
|
195 | void |
|
|
196 | rxvt_screen::set (rxvt_display *disp, int bitdepth) |
|
|
197 | { |
|
|
198 | set (disp); |
|
|
199 | |
|
|
200 | XVisualInfo vinfo; |
|
|
201 | |
|
|
202 | if (XMatchVisualInfo (xdisp, display->screen, bitdepth, TrueColor, &vinfo)) |
|
|
203 | { |
|
|
204 | depth = bitdepth; |
|
|
205 | visual = vinfo.visual; |
|
|
206 | cmap = XCreateColormap (xdisp, disp->root, visual, AllocNone); |
|
|
207 | } |
|
|
208 | } |
|
|
209 | |
|
|
210 | void |
|
|
211 | rxvt_screen::clear () |
|
|
212 | { |
|
|
213 | if (cmap != DefaultColormapOfScreen (ScreenOfDisplay (xdisp, display->screen))) |
|
|
214 | XFreeColormap (xdisp, cmap); |
|
|
215 | } |
136 | |
216 | |
137 | ///////////////////////////////////////////////////////////////////////////// |
217 | ///////////////////////////////////////////////////////////////////////////// |
138 | |
218 | |
139 | rxvt_display::rxvt_display (const char *id) |
219 | rxvt_display::rxvt_display (const char *id) |
140 | : refcounted (id) |
220 | : refcounted (id) |
141 | , x_ev (this, &rxvt_display::x_cb) |
221 | , x_ev (this, &rxvt_display::x_cb) |
142 | , selection_owner (0) |
222 | , selection_owner (0) |
143 | { |
223 | { |
144 | } |
224 | } |
145 | |
225 | |
|
|
226 | XrmDatabase |
|
|
227 | rxvt_display::get_resources () |
|
|
228 | { |
|
|
229 | char *homedir = (char *)getenv ("HOME"); |
|
|
230 | char fname[1024]; |
|
|
231 | |
|
|
232 | /* |
|
|
233 | * get resources using the X library function |
|
|
234 | */ |
|
|
235 | char *displayResource, *xe; |
|
|
236 | XrmDatabase database, rdb1; |
|
|
237 | |
|
|
238 | database = NULL; |
|
|
239 | |
|
|
240 | // for ordering, see for example http://www.faqs.org/faqs/Xt-FAQ/ Subject: 20 |
|
|
241 | |
|
|
242 | // 6. System wide per application default file. |
|
|
243 | |
|
|
244 | /* Add in $XAPPLRESDIR/Rxvt only; not bothering with XUSERFILESEARCHPATH */ |
|
|
245 | if ((xe = (char *)getenv ("XAPPLRESDIR"))) |
|
|
246 | { |
|
|
247 | snprintf (fname, sizeof (fname), "%s/%s", xe, RESCLASS); |
|
|
248 | |
|
|
249 | if ((rdb1 = XrmGetFileDatabase (fname))) |
|
|
250 | XrmMergeDatabases (rdb1, &database); |
|
|
251 | } |
|
|
252 | |
|
|
253 | // 5. User's per application default file. |
|
|
254 | // none |
|
|
255 | |
|
|
256 | // 4. User's defaults file. |
|
|
257 | /* Get any Xserver defaults */ |
|
|
258 | displayResource = XResourceManagerString (display); |
|
|
259 | |
|
|
260 | if (displayResource != NULL) |
|
|
261 | { |
|
|
262 | if ((rdb1 = XrmGetStringDatabase (displayResource))) |
|
|
263 | XrmMergeDatabases (rdb1, &database); |
|
|
264 | } |
|
|
265 | else if (homedir) |
|
|
266 | { |
|
|
267 | snprintf (fname, sizeof (fname), "%s/.Xdefaults", homedir); |
|
|
268 | |
|
|
269 | if ((rdb1 = XrmGetFileDatabase (fname))) |
|
|
270 | XrmMergeDatabases (rdb1, &database); |
|
|
271 | } |
|
|
272 | |
|
|
273 | /* Get screen specific resources */ |
|
|
274 | displayResource = XScreenResourceString (ScreenOfDisplay (display, screen)); |
|
|
275 | |
|
|
276 | if (displayResource != NULL) |
|
|
277 | { |
|
|
278 | if ((rdb1 = XrmGetStringDatabase (displayResource))) |
|
|
279 | /* Merge with screen-independent resources */ |
|
|
280 | XrmMergeDatabases (rdb1, &database); |
|
|
281 | |
|
|
282 | XFree (displayResource); |
|
|
283 | } |
|
|
284 | |
|
|
285 | // 3. User's per host defaults file |
|
|
286 | /* Add in XENVIRONMENT file */ |
|
|
287 | if ((xe = (char *)getenv ("XENVIRONMENT")) |
|
|
288 | && (rdb1 = XrmGetFileDatabase (xe))) |
|
|
289 | XrmMergeDatabases (rdb1, &database); |
|
|
290 | else if (homedir) |
|
|
291 | { |
|
|
292 | struct utsname un; |
|
|
293 | |
|
|
294 | if (!uname (&un)) |
|
|
295 | { |
|
|
296 | snprintf (fname, sizeof (fname), "%s/.Xdefaults-%s", homedir, un.nodename); |
|
|
297 | |
|
|
298 | if ((rdb1 = XrmGetFileDatabase (fname))) |
|
|
299 | XrmMergeDatabases (rdb1, &database); |
|
|
300 | } |
|
|
301 | } |
|
|
302 | |
|
|
303 | return database; |
|
|
304 | } |
|
|
305 | |
146 | bool rxvt_display::init () |
306 | bool rxvt_display::ref_init () |
147 | { |
307 | { |
148 | #ifdef LOCAL_X_IS_UNIX |
308 | #ifdef LOCAL_X_IS_UNIX |
149 | if (id[0] == ':') |
309 | if (id[0] == ':') |
150 | { |
310 | { |
151 | val = rxvt_malloc (5 + strlen (id) + 1); |
311 | val = rxvt_malloc (5 + strlen (id) + 1); |
… | |
… | |
164 | if (!display) |
324 | if (!display) |
165 | return false; |
325 | return false; |
166 | |
326 | |
167 | screen = DefaultScreen (display); |
327 | screen = DefaultScreen (display); |
168 | root = DefaultRootWindow (display); |
328 | root = DefaultRootWindow (display); |
169 | visual = DefaultVisual (display, screen); |
|
|
170 | cmap = DefaultColormap (display, screen); |
|
|
171 | depth = DefaultDepth (display, screen); |
|
|
172 | |
329 | |
173 | int fd = XConnectionNumber (display); |
330 | assert (sizeof (xa_names) / sizeof (char *) == NUM_XA); |
|
|
331 | XInternAtoms (display, (char **)xa_names, NUM_XA, False, xa); |
174 | |
332 | |
175 | #ifndef NO_SLOW_LINK_SUPPORT |
333 | XrmSetDatabase (display, get_resources ()); |
176 | // try to detect wether we have a local connection. |
|
|
177 | // assume unix domains socket == local, everything else not |
|
|
178 | // TODO: might want to check for inet/127.0.0.1 |
|
|
179 | is_local = 0; |
|
|
180 | sockaddr_un sa; |
|
|
181 | socklen_t sl = sizeof (sa); |
|
|
182 | |
|
|
183 | if (!getsockname (fd, (sockaddr *)&sa, &sl)) |
|
|
184 | is_local = sa.sun_family == AF_LOCAL; |
|
|
185 | #endif |
|
|
186 | |
334 | |
187 | #ifdef POINTER_BLANK |
335 | #ifdef POINTER_BLANK |
188 | XColor blackcolour; |
336 | XColor blackcolour; |
189 | blackcolour.red = 0; |
337 | blackcolour.red = 0; |
190 | blackcolour.green = 0; |
338 | blackcolour.green = 0; |
… | |
… | |
193 | blank_cursor = XCreateGlyphCursor (display, f, f, ' ', ' ', |
341 | blank_cursor = XCreateGlyphCursor (display, f, f, ' ', ' ', |
194 | &blackcolour, &blackcolour); |
342 | &blackcolour, &blackcolour); |
195 | XUnloadFont (display, f); |
343 | XUnloadFont (display, f); |
196 | #endif |
344 | #endif |
197 | |
345 | |
198 | #ifdef PREFER_24BIT |
346 | int fd = XConnectionNumber (display); |
199 | /* |
|
|
200 | * If depth is not 24, look for a 24bit visual. |
|
|
201 | */ |
|
|
202 | if (depth != 24) |
|
|
203 | { |
|
|
204 | XVisualInfo vinfo; |
|
|
205 | |
347 | |
206 | if (XMatchVisualInfo (display, screen, 24, TrueColor, &vinfo)) |
348 | #ifndef NO_SLOW_LINK_SUPPORT |
207 | { |
349 | // try to detect wether we have a local connection. |
208 | depth = 24; |
350 | // assume unix domains socket == local, everything else not |
209 | visual = vinfo.visual; |
351 | // TODO: might want to check for inet/127.0.0.1 |
210 | cmap = XCreateColormap (display, |
352 | is_local = 0; |
211 | RootWindow (display, screen), |
353 | sockaddr_un sa; |
212 | visual, AllocNone); |
354 | socklen_t sl = sizeof (sa); |
213 | } |
355 | |
214 | } |
356 | if (!getsockname (fd, (sockaddr *)&sa, &sl)) |
|
|
357 | is_local = sa.sun_family == AF_LOCAL; |
215 | #endif |
358 | #endif |
216 | |
359 | |
217 | x_ev.start (fd, EVENT_READ); |
360 | x_ev.start (fd, EVENT_READ); |
218 | fcntl (fd, F_SETFD, FD_CLOEXEC); |
361 | fcntl (fd, F_SETFD, FD_CLOEXEC); |
219 | |
362 | |
220 | XSelectInput (display, root, PropertyChangeMask); |
363 | XSelectInput (display, root, PropertyChangeMask); |
221 | #ifdef USE_XIM |
|
|
222 | xa_xim_servers = XInternAtom (display, "XIM_SERVERS", 0); |
|
|
223 | #endif |
|
|
224 | |
364 | |
225 | flush (); |
365 | flush (); |
226 | |
366 | |
227 | return true; |
367 | return true; |
228 | } |
368 | } |
229 | |
369 | |
|
|
370 | void |
|
|
371 | rxvt_display::ref_next () |
|
|
372 | { |
|
|
373 | // TODO: somehow check wether the database files/resources changed |
|
|
374 | // before re-loading/parsing |
|
|
375 | XrmDestroyDatabase (XrmGetDatabase (display)); |
|
|
376 | XrmSetDatabase (display, get_resources ()); |
|
|
377 | } |
|
|
378 | |
230 | rxvt_display::~rxvt_display () |
379 | rxvt_display::~rxvt_display () |
231 | { |
380 | { |
|
|
381 | if (!display) |
|
|
382 | return; |
|
|
383 | |
|
|
384 | #ifdef POINTER_BLANK |
|
|
385 | XFreeCursor (display, blank_cursor); |
|
|
386 | #endif |
232 | x_ev.stop (); |
387 | x_ev.stop (); |
233 | #ifdef USE_XIM |
388 | #ifdef USE_XIM |
234 | xims.clear (); |
389 | xims.clear (); |
235 | #endif |
390 | #endif |
236 | |
|
|
237 | if (display) |
|
|
238 | XCloseDisplay (display); |
391 | XCloseDisplay (display); |
239 | } |
392 | } |
240 | |
393 | |
241 | #ifdef USE_XIM |
394 | #ifdef USE_XIM |
242 | void rxvt_display::im_change_cb () |
395 | void rxvt_display::im_change_cb () |
243 | { |
396 | { |
… | |
… | |
251 | // registers, as xlib crashes due to a race otherwise. |
404 | // registers, as xlib crashes due to a race otherwise. |
252 | Atom actual_type, *atoms; |
405 | Atom actual_type, *atoms; |
253 | int actual_format; |
406 | int actual_format; |
254 | unsigned long nitems, bytes_after; |
407 | unsigned long nitems, bytes_after; |
255 | |
408 | |
256 | if (XGetWindowProperty (display, root, xa_xim_servers, 0L, 1000000L, |
409 | if (XGetWindowProperty (display, root, xa[XA_XIM_SERVERS], 0L, 1000000L, |
257 | False, XA_ATOM, &actual_type, &actual_format, |
410 | False, XA_ATOM, &actual_type, &actual_format, |
258 | &nitems, &bytes_after, (unsigned char **)&atoms) |
411 | &nitems, &bytes_after, (unsigned char **)&atoms) |
259 | != Success ) |
412 | != Success ) |
260 | return; |
413 | return; |
261 | |
414 | |
… | |
… | |
279 | XNextEvent (display, &xev); |
432 | XNextEvent (display, &xev); |
280 | |
433 | |
281 | #ifdef USE_XIM |
434 | #ifdef USE_XIM |
282 | if (!XFilterEvent (&xev, None)) |
435 | if (!XFilterEvent (&xev, None)) |
283 | { |
436 | { |
284 | |
|
|
285 | if (xev.type == PropertyNotify |
437 | if (xev.type == PropertyNotify |
286 | && xev.xany.window == root |
438 | && xev.xany.window == root |
287 | && xev.xproperty.atom == xa_xim_servers) |
439 | && xev.xproperty.atom == xa[XA_XIM_SERVERS]) |
288 | im_change_check (); |
440 | im_change_check (); |
289 | #endif |
441 | #endif |
290 | for (int i = xw.size (); i--; ) |
442 | for (int i = xw.size (); i--; ) |
291 | { |
443 | { |
292 | if (!xw[i]) |
444 | if (!xw[i]) |
… | |
… | |
382 | refcache<rxvt_display> displays; |
534 | refcache<rxvt_display> displays; |
383 | |
535 | |
384 | ///////////////////////////////////////////////////////////////////////////// |
536 | ///////////////////////////////////////////////////////////////////////////// |
385 | |
537 | |
386 | bool |
538 | bool |
387 | rxvt_color::set (rxvt_display *display, Pixel p) |
539 | rxvt_color::set (rxvt_screen *screen, const char *name) |
388 | { |
540 | { |
389 | #if XFT |
541 | #if XFT |
390 | XColor xc; |
542 | int l = strlen (name); |
|
|
543 | rxvt_rgba r; |
|
|
544 | char eos; |
|
|
545 | int mult; |
391 | |
546 | |
392 | xc.pixel = p; |
547 | if ( l == 1+4*1 && 4 == sscanf (name, "#%1hx%1hx%1hx%1hx%c", &r.a, &r.r, &r.g, &r.b, &eos)) |
393 | if (!XQueryColor (display->display, display->cmap, &xc)) |
548 | mult = rxvt_rgba::MAX_CC / 0x000f; |
394 | return false; |
549 | else if (l == 1+4*2 && 4 == sscanf (name, "#%2hx%2hx%2hx%2hx%c", &r.a, &r.r, &r.g, &r.b, &eos)) |
|
|
550 | mult = rxvt_rgba::MAX_CC / 0x00ff; |
|
|
551 | else if (l == 1+4*4 && 4 == sscanf (name, "#%4hx%4hx%4hx%4hx%c", &r.a, &r.r, &r.g, &r.b, &eos)) |
|
|
552 | mult = rxvt_rgba::MAX_CC / 0xffff; |
|
|
553 | else if (l == 4+5*4 && 4 == sscanf (name, "rgba:%hx/%hx/%hx/%hx%c", &r.r, &r.g, &r.b, &r.a, &eos)) |
|
|
554 | mult = rxvt_rgba::MAX_CC / 0xffff; |
|
|
555 | else |
|
|
556 | return XftColorAllocName (screen->xdisp, screen->visual, screen->cmap, name, &c); |
395 | |
557 | |
396 | XRenderColor d; |
558 | r.r *= mult; r.g *= mult; r.b *= mult; r.a *= mult; |
397 | |
559 | return set (screen, r); |
398 | d.red = xc.red; |
|
|
399 | d.green = xc.green; |
|
|
400 | d.blue = xc.blue; |
|
|
401 | d.alpha = 0xffff; |
|
|
402 | |
|
|
403 | return |
|
|
404 | XftColorAllocValue (display->display, |
|
|
405 | display->visual, |
|
|
406 | display->cmap, |
|
|
407 | &d, &c); |
|
|
408 | #else |
|
|
409 | this->p = p; |
|
|
410 | #endif |
|
|
411 | |
|
|
412 | return true; |
|
|
413 | } |
|
|
414 | |
|
|
415 | bool |
|
|
416 | rxvt_color::set (rxvt_display *display, const char *name) |
|
|
417 | { |
|
|
418 | #if XFT |
|
|
419 | return XftColorAllocName (display->display, display->visual, display->cmap, |
|
|
420 | name, &c); |
|
|
421 | #else |
560 | #else |
422 | XColor xc; |
561 | XColor xc; |
423 | |
562 | |
424 | if (XParseColor (display->display, display->cmap, name, &xc)) |
563 | if (XParseColor (screen->xdisp, screen->cmap, name, &xc)) |
425 | return set (display, xc.red, xc.green, xc.blue); |
564 | return set (screen, rxvt_rgba (xc.red, xc.green, xc.blue)); |
426 | |
565 | |
427 | return false; |
566 | return false; |
428 | #endif |
567 | #endif |
429 | } |
568 | } |
430 | |
569 | |
431 | bool |
570 | bool |
432 | rxvt_color::set (rxvt_display *display, unsigned short cr, unsigned short cg, unsigned short cb) |
571 | rxvt_color::set (rxvt_screen *screen, rxvt_rgba rgba) |
433 | { |
572 | { |
|
|
573 | #if XFT |
|
|
574 | XRenderColor d; |
|
|
575 | |
|
|
576 | d.red = rgba.r; |
|
|
577 | d.green = rgba.g; |
|
|
578 | d.blue = rgba.b; |
|
|
579 | d.alpha = rgba.a; |
|
|
580 | |
|
|
581 | if (XftColorAllocValue (screen->xdisp, screen->visual, screen->cmap, &d, &c)) |
|
|
582 | { |
|
|
583 | // FUCKING Xft gets it wrong, of course, fix it for the common case |
|
|
584 | // transparency users should eat shit and die, and then |
|
|
585 | // XRenderQueryPictIndexValues themselves plenty. |
|
|
586 | if (screen->depth == 32 |
|
|
587 | && screen->visual->c_class == TrueColor |
|
|
588 | && screen->visual->red_mask == 0x00ff0000 |
|
|
589 | && screen->visual->green_mask == 0x0000ff00 |
|
|
590 | && screen->visual->blue_mask == 0x000000ff) |
|
|
591 | c.pixel = c.pixel & 0x00ffffffUL | ((rgba.a >> 8) << 24); |
|
|
592 | |
|
|
593 | return true; |
|
|
594 | } |
|
|
595 | |
|
|
596 | return false; |
|
|
597 | #else |
434 | XColor xc; |
598 | XColor xc; |
435 | |
599 | |
436 | xc.red = cr; |
600 | xc.red = rgba.r; |
437 | xc.green = cg; |
601 | xc.green = rgba.g; |
438 | xc.blue = cb; |
602 | xc.blue = rgba.b; |
439 | xc.flags = DoRed | DoGreen | DoBlue; |
603 | xc.flags = DoRed | DoGreen | DoBlue; |
440 | |
604 | |
441 | if (XAllocColor (display->display, display->cmap, &xc)) |
605 | if (XAllocColor (screen->xdisp, screen->cmap, &xc)) |
442 | return set (display, xc.pixel); |
606 | { |
|
|
607 | p = xc.pixel; |
|
|
608 | return true; |
|
|
609 | } |
443 | |
610 | |
444 | return false; |
611 | return false; |
|
|
612 | #endif |
445 | } |
613 | } |
446 | |
614 | |
447 | void |
615 | void |
448 | rxvt_color::get (rxvt_display *display, unsigned short &cr, unsigned short &cg, unsigned short &cb) |
616 | rxvt_color::get (rxvt_screen *screen, rxvt_rgba &rgba) |
449 | { |
617 | { |
450 | #if XFT |
618 | #if XFT |
451 | cr = c.color.red; |
619 | rgba.r = c.color.red; |
452 | cg = c.color.green; |
620 | rgba.g = c.color.green; |
453 | cb = c.color.blue; |
621 | rgba.b = c.color.blue; |
|
|
622 | rgba.a = c.color.alpha; |
454 | #else |
623 | #else |
455 | XColor c; |
624 | XColor c; |
456 | |
625 | |
457 | c.pixel = p; |
626 | c.pixel = p; |
458 | XQueryColor (display->display, display->cmap, &c); |
627 | XQueryColor (screen->xdisp, screen->cmap, &c); |
459 | |
628 | |
460 | cr = c.red; |
629 | rgba.r = c.red; |
461 | cg = c.green; |
630 | rgba.g = c.green; |
462 | cb = c.blue; |
631 | rgba.b = c.blue; |
|
|
632 | rgba.a = rxvt_rgba::MAX_CC; |
463 | #endif |
633 | #endif |
464 | } |
634 | } |
465 | |
635 | |
466 | void |
636 | void |
467 | rxvt_color::free (rxvt_display *display) |
637 | rxvt_color::free (rxvt_screen *screen) |
468 | { |
638 | { |
469 | #if XFT |
639 | #if XFT |
470 | XftColorFree (display->display, display->visual, display->cmap, &c); |
640 | XftColorFree (screen->xdisp, screen->visual, screen->cmap, &c); |
471 | #else |
641 | #else |
472 | XFreeColors (display->display, display->cmap, &p, 1, AllPlanes); |
642 | XFreeColors (screen->xdisp, screen->cmap, &p, 1, AllPlanes); |
473 | #endif |
643 | #endif |
474 | } |
644 | } |
475 | |
645 | |
476 | rxvt_color |
646 | rxvt_color |
477 | rxvt_color::fade (rxvt_display *display, int percent) |
647 | rxvt_color::fade (rxvt_screen *screen, int percent) |
478 | { |
648 | { |
479 | percent = 100 - percent; |
|
|
480 | |
|
|
481 | unsigned short cr, cg, cb; |
|
|
482 | rxvt_color faded; |
649 | rxvt_color faded; |
483 | |
650 | |
484 | get (display, cr, cg, cb); |
651 | rxvt_rgba c; |
|
|
652 | get (screen, c); |
485 | |
653 | |
486 | faded.set ( |
654 | c.r = lerp (0, c.r, percent); |
487 | display, |
655 | c.g = lerp (0, c.g, percent); |
488 | cr * percent / 100, |
656 | c.b = lerp (0, c.b, percent); |
489 | cg * percent / 100, |
657 | |
490 | cb * percent / 100 |
658 | faded.set (screen, c); |
491 | ); |
|
|
492 | |
659 | |
493 | return faded; |
660 | return faded; |
494 | } |
661 | } |
495 | |
662 | |
496 | #define LERP(a,b,p) (a * p / 100 + b * (100 - p) / 100) |
|
|
497 | |
|
|
498 | rxvt_color |
663 | rxvt_color |
499 | rxvt_color::fade (rxvt_display *display, int percent, rxvt_color &fadeto) |
664 | rxvt_color::fade (rxvt_screen *screen, int percent, rxvt_color &fadeto) |
500 | { |
665 | { |
501 | percent = 100 - percent; |
666 | rxvt_rgba c, fc; |
502 | |
|
|
503 | unsigned short cr, cg, cb; |
|
|
504 | unsigned short fcr, fcg, fcb; |
|
|
505 | rxvt_color faded; |
667 | rxvt_color faded; |
506 | |
668 | |
507 | get (display, cr, cg, cb); |
669 | get (screen, c); |
508 | fadeto.get(display, fcr, fcg, fcb); |
670 | fadeto.get (screen, fc); |
509 | |
671 | |
510 | faded.set ( |
672 | faded.set ( |
511 | display, |
673 | screen, |
|
|
674 | rxvt_rgba ( |
512 | LERP (cr, fcr, percent), |
675 | lerp (fc.r, c.r, percent), |
513 | LERP (cg, fcg, percent), |
676 | lerp (fc.g, c.g, percent), |
514 | LERP (cb, fcb, percent) |
677 | lerp (fc.b, c.b, percent), |
|
|
678 | lerp (fc.a, c.a, percent) |
|
|
679 | ) |
515 | ); |
680 | ); |
516 | |
681 | |
517 | return faded; |
682 | return faded; |
518 | } |
683 | } |
519 | |
684 | |