… | |
… | |
177 | bool |
177 | bool |
178 | rxvt_xim::ref_init () |
178 | rxvt_xim::ref_init () |
179 | { |
179 | { |
180 | display = GET_R->display; //HACK: TODO |
180 | display = GET_R->display; //HACK: TODO |
181 | |
181 | |
182 | xim = XOpenIM (display->display, NULL, NULL, NULL); |
182 | xim = XOpenIM (display->dpy, 0, 0, 0); |
183 | |
183 | |
184 | if (!xim) |
184 | if (!xim) |
185 | return false; |
185 | return false; |
186 | |
186 | |
187 | XIMCallback ximcallback; |
187 | XIMCallback ximcallback; |
188 | ximcallback.client_data = (XPointer)this; |
188 | ximcallback.client_data = (XPointer)this; |
189 | ximcallback.callback = im_destroy_cb; |
189 | ximcallback.callback = im_destroy_cb; |
190 | |
190 | |
191 | XSetIMValues (xim, XNDestroyCallback, &ximcallback, NULL); |
191 | XSetIMValues (xim, XNDestroyCallback, &ximcallback, 0); |
192 | |
192 | |
193 | return true; |
193 | return true; |
194 | } |
194 | } |
195 | |
195 | |
196 | rxvt_xim::~rxvt_xim () |
196 | rxvt_xim::~rxvt_xim () |
… | |
… | |
205 | |
205 | |
206 | void |
206 | void |
207 | rxvt_screen::set (rxvt_display *disp) |
207 | rxvt_screen::set (rxvt_display *disp) |
208 | { |
208 | { |
209 | display = disp; |
209 | display = disp; |
210 | xdisp = disp->display; |
210 | dpy = disp->dpy; |
211 | |
211 | |
212 | Screen *screen = ScreenOfDisplay (xdisp, disp->screen); |
212 | Screen *screen = ScreenOfDisplay (dpy, disp->screen); |
213 | |
213 | |
214 | depth = DefaultDepthOfScreen (screen); |
214 | depth = DefaultDepthOfScreen (screen); |
215 | visual = DefaultVisualOfScreen (screen); |
215 | visual = DefaultVisualOfScreen (screen); |
216 | cmap = DefaultColormapOfScreen (screen); |
216 | cmap = DefaultColormapOfScreen (screen); |
217 | } |
217 | } |
… | |
… | |
222 | set (disp); |
222 | set (disp); |
223 | |
223 | |
224 | #if XFT |
224 | #if XFT |
225 | XVisualInfo vinfo; |
225 | XVisualInfo vinfo; |
226 | |
226 | |
227 | if (XMatchVisualInfo (xdisp, display->screen, bitdepth, TrueColor, &vinfo)) |
227 | if (XMatchVisualInfo (dpy, display->screen, bitdepth, TrueColor, &vinfo)) |
228 | { |
228 | { |
229 | depth = bitdepth; |
229 | depth = bitdepth; |
230 | visual = vinfo.visual; |
230 | visual = vinfo.visual; |
231 | cmap = XCreateColormap (xdisp, disp->root, visual, AllocNone); |
231 | cmap = XCreateColormap (dpy, disp->root, visual, AllocNone); |
232 | } |
232 | } |
233 | #endif |
233 | #endif |
234 | } |
234 | } |
235 | |
235 | |
236 | void |
236 | void |
237 | rxvt_screen::clear () |
237 | rxvt_screen::clear () |
238 | { |
238 | { |
239 | if (cmap != DefaultColormapOfScreen (ScreenOfDisplay (xdisp, display->screen))) |
239 | if (cmap != DefaultColormapOfScreen (ScreenOfDisplay (dpy, display->screen))) |
240 | XFreeColormap (xdisp, cmap); |
240 | XFreeColormap (dpy, cmap); |
241 | } |
241 | } |
242 | |
242 | |
243 | ///////////////////////////////////////////////////////////////////////////// |
243 | ///////////////////////////////////////////////////////////////////////////// |
244 | |
244 | |
245 | rxvt_display::rxvt_display (const char *id) |
245 | rxvt_display::rxvt_display (const char *id) |
… | |
… | |
248 | , selection_owner (0) |
248 | , selection_owner (0) |
249 | { |
249 | { |
250 | } |
250 | } |
251 | |
251 | |
252 | XrmDatabase |
252 | XrmDatabase |
253 | rxvt_display::get_resources () |
253 | rxvt_display::get_resources (bool refresh) |
254 | { |
254 | { |
255 | char *homedir = (char *)getenv ("HOME"); |
255 | char *homedir = (char *)getenv ("HOME"); |
256 | char fname[1024]; |
256 | char fname[1024]; |
257 | |
257 | |
258 | /* |
258 | /* |
259 | * get resources using the X library function |
259 | * get resources using the X library function |
260 | */ |
260 | */ |
261 | char *displayResource, *xe; |
261 | char *displayResource, *xe; |
262 | XrmDatabase database, rdb1; |
262 | XrmDatabase rdb1, database = 0; |
263 | |
|
|
264 | database = NULL; |
|
|
265 | |
263 | |
266 | // for ordering, see for example http://www.faqs.org/faqs/Xt-FAQ/ Subject: 20 |
264 | // for ordering, see for example http://www.faqs.org/faqs/Xt-FAQ/ Subject: 20 |
267 | |
265 | |
268 | // 6. System wide per application default file. |
266 | // 6. System wide per application default file. |
269 | |
267 | |
… | |
… | |
279 | // 5. User's per application default file. |
277 | // 5. User's per application default file. |
280 | // none |
278 | // none |
281 | |
279 | |
282 | // 4. User's defaults file. |
280 | // 4. User's defaults file. |
283 | /* Get any Xserver defaults */ |
281 | /* Get any Xserver defaults */ |
|
|
282 | if (refresh) |
|
|
283 | { |
|
|
284 | // fucking xlib keeps a copy of the rm string |
|
|
285 | Atom actual_type; |
|
|
286 | int actual_format; |
|
|
287 | unsigned long nitems, nremaining; |
|
|
288 | char *val = 0; |
|
|
289 | |
|
|
290 | #if XLIB_ILLEGAL_ACCESS |
|
|
291 | if (dpy->xdefaults) |
|
|
292 | XFree (dpy->xdefaults); |
|
|
293 | #endif |
|
|
294 | |
|
|
295 | if (XGetWindowProperty (dpy, root, XA_RESOURCE_MANAGER, |
|
|
296 | 0L, 100000000L, False, |
|
|
297 | XA_STRING, &actual_type, &actual_format, |
|
|
298 | &nitems, &nremaining, |
|
|
299 | (unsigned char **)&val) == Success |
|
|
300 | && actual_type == XA_STRING |
|
|
301 | && actual_format == 8) |
|
|
302 | displayResource = val; |
|
|
303 | else |
|
|
304 | { |
|
|
305 | displayResource = 0; |
|
|
306 | if (val) |
|
|
307 | XFree(val); |
|
|
308 | } |
|
|
309 | |
|
|
310 | #if XLIB_ILLEGAL_ACCESS |
|
|
311 | dpy->xdefaults = displayResource; |
|
|
312 | #endif |
|
|
313 | } |
|
|
314 | else |
284 | displayResource = XResourceManagerString (display); |
315 | displayResource = XResourceManagerString (dpy); |
285 | |
316 | |
286 | if (displayResource != NULL) |
317 | if (displayResource) |
287 | { |
318 | { |
288 | if ((rdb1 = XrmGetStringDatabase (displayResource))) |
319 | if ((rdb1 = XrmGetStringDatabase (displayResource))) |
289 | XrmMergeDatabases (rdb1, &database); |
320 | XrmMergeDatabases (rdb1, &database); |
290 | } |
321 | } |
291 | else if (homedir) |
322 | else if (homedir) |
… | |
… | |
294 | |
325 | |
295 | if ((rdb1 = XrmGetFileDatabase (fname))) |
326 | if ((rdb1 = XrmGetFileDatabase (fname))) |
296 | XrmMergeDatabases (rdb1, &database); |
327 | XrmMergeDatabases (rdb1, &database); |
297 | } |
328 | } |
298 | |
329 | |
|
|
330 | #if !XLIB_ILLEGAL_ACCESS |
|
|
331 | if (refresh && displayResource) |
|
|
332 | XFree (displayResource); |
|
|
333 | #endif |
|
|
334 | |
299 | /* Get screen specific resources */ |
335 | /* Get screen specific resources */ |
300 | displayResource = XScreenResourceString (ScreenOfDisplay (display, screen)); |
336 | displayResource = XScreenResourceString (ScreenOfDisplay (dpy, screen)); |
301 | |
337 | |
302 | if (displayResource != NULL) |
338 | if (displayResource) |
303 | { |
339 | { |
304 | if ((rdb1 = XrmGetStringDatabase (displayResource))) |
340 | if ((rdb1 = XrmGetStringDatabase (displayResource))) |
305 | /* Merge with screen-independent resources */ |
341 | /* Merge with screen-independent resources */ |
306 | XrmMergeDatabases (rdb1, &database); |
342 | XrmMergeDatabases (rdb1, &database); |
307 | |
343 | |
… | |
… | |
335 | if (id[0] == ':') |
371 | if (id[0] == ':') |
336 | { |
372 | { |
337 | val = rxvt_malloc (5 + strlen (id) + 1); |
373 | val = rxvt_malloc (5 + strlen (id) + 1); |
338 | strcpy (val, "unix/"); |
374 | strcpy (val, "unix/"); |
339 | strcat (val, id); |
375 | strcat (val, id); |
340 | display = XOpenDisplay (val); |
376 | dpy = XOpenDisplay (val); |
341 | free (val); |
377 | free (val); |
342 | } |
378 | } |
343 | else |
379 | else |
344 | #endif |
380 | #endif |
345 | display = 0; |
381 | dpy = 0; |
346 | |
382 | |
347 | if (!display) |
383 | if (!dpy) |
348 | display = XOpenDisplay (id); |
384 | dpy = XOpenDisplay (id); |
349 | |
385 | |
350 | if (!display) |
386 | if (!dpy) |
351 | return false; |
387 | return false; |
352 | |
388 | |
353 | screen = DefaultScreen (display); |
389 | screen = DefaultScreen (dpy); |
354 | root = DefaultRootWindow (display); |
390 | root = DefaultRootWindow (dpy); |
355 | |
391 | |
356 | assert (sizeof (xa_names) / sizeof (char *) == NUM_XA); |
392 | assert (sizeof (xa_names) / sizeof (char *) == NUM_XA); |
357 | XInternAtoms (display, (char **)xa_names, NUM_XA, False, xa); |
393 | XInternAtoms (dpy, (char **)xa_names, NUM_XA, False, xa); |
358 | |
394 | |
359 | XrmSetDatabase (display, get_resources ()); |
395 | XrmSetDatabase (dpy, get_resources (false)); |
360 | |
396 | |
361 | #ifdef POINTER_BLANK |
397 | #ifdef POINTER_BLANK |
362 | XColor blackcolour; |
398 | XColor blackcolour; |
363 | blackcolour.red = 0; |
399 | blackcolour.red = 0; |
364 | blackcolour.green = 0; |
400 | blackcolour.green = 0; |
365 | blackcolour.blue = 0; |
401 | blackcolour.blue = 0; |
366 | Font f = XLoadFont (display, "fixed"); |
402 | Font f = XLoadFont (dpy, "fixed"); |
367 | blank_cursor = XCreateGlyphCursor (display, f, f, ' ', ' ', |
403 | blank_cursor = XCreateGlyphCursor (dpy, f, f, ' ', ' ', |
368 | &blackcolour, &blackcolour); |
404 | &blackcolour, &blackcolour); |
369 | XUnloadFont (display, f); |
405 | XUnloadFont (dpy, f); |
370 | #endif |
406 | #endif |
371 | |
407 | |
372 | int fd = XConnectionNumber (display); |
408 | int fd = XConnectionNumber (dpy); |
373 | |
409 | |
374 | #ifndef NO_SLOW_LINK_SUPPORT |
410 | #ifndef NO_SLOW_LINK_SUPPORT |
375 | // try to detect wether we have a local connection. |
411 | // try to detect wether we have a local connection. |
376 | // assume unix domains socket == local, everything else not |
412 | // assume unix domains socket == local, everything else not |
377 | // TODO: might want to check for inet/127.0.0.1 |
413 | // TODO: might want to check for inet/127.0.0.1 |
… | |
… | |
384 | #endif |
420 | #endif |
385 | |
421 | |
386 | x_ev.start (fd, EVENT_READ); |
422 | x_ev.start (fd, EVENT_READ); |
387 | fcntl (fd, F_SETFD, FD_CLOEXEC); |
423 | fcntl (fd, F_SETFD, FD_CLOEXEC); |
388 | |
424 | |
389 | XSelectInput (display, root, PropertyChangeMask); |
425 | XSelectInput (dpy, root, PropertyChangeMask); |
390 | |
426 | |
391 | flush (); |
427 | flush (); |
392 | |
428 | |
393 | return true; |
429 | return true; |
394 | } |
430 | } |
395 | |
431 | |
396 | void |
432 | void |
397 | rxvt_display::ref_next () |
433 | rxvt_display::ref_next () |
398 | { |
434 | { |
399 | // TODO: somehow check wether the database files/resources changed |
435 | // TODO: somehow check wether the database files/resources changed |
400 | // before re-loading/parsing |
436 | // before affording re-loading/parsing |
401 | XrmDestroyDatabase (XrmGetDatabase (display)); |
437 | XrmDestroyDatabase (XrmGetDatabase (dpy)); |
402 | XrmSetDatabase (display, get_resources ()); |
438 | XrmSetDatabase (dpy, get_resources (true)); |
403 | } |
439 | } |
404 | |
440 | |
405 | rxvt_display::~rxvt_display () |
441 | rxvt_display::~rxvt_display () |
406 | { |
442 | { |
407 | if (!display) |
443 | if (!dpy) |
408 | return; |
444 | return; |
409 | |
445 | |
410 | #ifdef POINTER_BLANK |
446 | #ifdef POINTER_BLANK |
411 | XFreeCursor (display, blank_cursor); |
447 | XFreeCursor (dpy, blank_cursor); |
412 | #endif |
448 | #endif |
413 | x_ev.stop (); |
449 | x_ev.stop (); |
414 | #ifdef USE_XIM |
450 | #ifdef USE_XIM |
415 | xims.clear (); |
451 | xims.clear (); |
416 | #endif |
452 | #endif |
417 | XCloseDisplay (display); |
453 | XCloseDisplay (dpy); |
418 | } |
454 | } |
419 | |
455 | |
420 | #ifdef USE_XIM |
456 | #ifdef USE_XIM |
421 | void rxvt_display::im_change_cb () |
457 | void rxvt_display::im_change_cb () |
422 | { |
458 | { |
… | |
… | |
430 | // registers, as xlib crashes due to a race otherwise. |
466 | // registers, as xlib crashes due to a race otherwise. |
431 | Atom actual_type, *atoms; |
467 | Atom actual_type, *atoms; |
432 | int actual_format; |
468 | int actual_format; |
433 | unsigned long nitems, bytes_after; |
469 | unsigned long nitems, bytes_after; |
434 | |
470 | |
435 | if (XGetWindowProperty (display, root, xa[XA_XIM_SERVERS], 0L, 1000000L, |
471 | if (XGetWindowProperty (dpy, root, xa[XA_XIM_SERVERS], 0L, 1000000L, |
436 | False, XA_ATOM, &actual_type, &actual_format, |
472 | False, XA_ATOM, &actual_type, &actual_format, |
437 | &nitems, &bytes_after, (unsigned char **)&atoms) |
473 | &nitems, &bytes_after, (unsigned char **)&atoms) |
438 | != Success ) |
474 | != Success ) |
439 | return; |
475 | return; |
440 | |
476 | |
441 | if (actual_type == XA_ATOM && actual_format == 32) |
477 | if (actual_type == XA_ATOM && actual_format == 32) |
442 | for (int i = 0; i < nitems; i++) |
478 | for (int i = 0; i < nitems; i++) |
443 | if (XGetSelectionOwner (display, atoms[i])) |
479 | if (XGetSelectionOwner (dpy, atoms[i])) |
444 | { |
480 | { |
445 | im_change_cb (); |
481 | im_change_cb (); |
446 | break; |
482 | break; |
447 | } |
483 | } |
448 | |
484 | |
… | |
… | |
453 | void rxvt_display::x_cb (io_watcher &w, short revents) |
489 | void rxvt_display::x_cb (io_watcher &w, short revents) |
454 | { |
490 | { |
455 | do |
491 | do |
456 | { |
492 | { |
457 | XEvent xev; |
493 | XEvent xev; |
458 | XNextEvent (display, &xev); |
494 | XNextEvent (dpy, &xev); |
459 | |
495 | |
460 | #ifdef USE_XIM |
496 | #ifdef USE_XIM |
461 | if (!XFilterEvent (&xev, None)) |
497 | if (!XFilterEvent (&xev, None)) |
462 | { |
498 | { |
463 | if (xev.type == PropertyNotify |
499 | if (xev.type == PropertyNotify |
… | |
… | |
474 | } |
510 | } |
475 | #ifdef USE_XIM |
511 | #ifdef USE_XIM |
476 | } |
512 | } |
477 | #endif |
513 | #endif |
478 | } |
514 | } |
479 | while (XEventsQueued (display, QueuedAlready)); |
515 | while (XEventsQueued (dpy, QueuedAlready)); |
480 | |
516 | |
481 | XFlush (display); |
517 | XFlush (dpy); |
482 | } |
518 | } |
483 | |
519 | |
484 | void rxvt_display::flush () |
520 | void rxvt_display::flush () |
485 | { |
521 | { |
486 | if (XEventsQueued (display, QueuedAlready)) |
522 | if (XEventsQueued (dpy, QueuedAlready)) |
487 | x_cb (x_ev, EVENT_READ); |
523 | x_cb (x_ev, EVENT_READ); |
488 | |
524 | |
489 | XFlush (display); |
525 | XFlush (dpy); |
490 | } |
526 | } |
491 | |
527 | |
492 | void rxvt_display::reg (xevent_watcher *w) |
528 | void rxvt_display::reg (xevent_watcher *w) |
493 | { |
529 | { |
494 | xw.push_back (w); |
530 | xw.push_back (w); |
… | |
… | |
508 | |
544 | |
509 | selection_owner = owner; |
545 | selection_owner = owner; |
510 | } |
546 | } |
511 | |
547 | |
512 | #ifdef USE_XIM |
548 | #ifdef USE_XIM |
|
|
549 | |
513 | void rxvt_display::reg (im_watcher *w) |
550 | void rxvt_display::reg (im_watcher *w) |
514 | { |
551 | { |
515 | imw.push_back (w); |
552 | imw.push_back (w); |
516 | } |
553 | } |
517 | |
554 | |
… | |
… | |
541 | return xim; |
578 | return xim; |
542 | } |
579 | } |
543 | |
580 | |
544 | void rxvt_display::put_xim (rxvt_xim *xim) |
581 | void rxvt_display::put_xim (rxvt_xim *xim) |
545 | { |
582 | { |
546 | #if XLIB_IS_RACEFREE |
583 | # if XLIB_IS_RACEFREE |
547 | xims.put (xim); |
584 | xims.put (xim); |
548 | #endif |
585 | # endif |
549 | } |
586 | } |
|
|
587 | |
550 | #endif |
588 | #endif |
551 | |
589 | |
552 | Atom rxvt_display::atom (const char *name) |
590 | Atom rxvt_display::atom (const char *name) |
553 | { |
591 | { |
554 | return XInternAtom (display, name, False); |
592 | return XInternAtom (dpy, name, False); |
555 | } |
593 | } |
556 | |
594 | |
557 | ///////////////////////////////////////////////////////////////////////////// |
595 | ///////////////////////////////////////////////////////////////////////////// |
558 | |
596 | |
559 | template class refcache<rxvt_display>; |
597 | template class refcache<rxvt_display>; |
560 | refcache<rxvt_display> displays; |
598 | refcache<rxvt_display> displays; |
561 | |
599 | |
562 | ///////////////////////////////////////////////////////////////////////////// |
600 | ///////////////////////////////////////////////////////////////////////////// |
563 | |
601 | |
564 | bool |
602 | bool |
565 | rxvt_color::alloc (rxvt_screen *screen, const rxvt_rgba &color) |
603 | rxvt_color::alloc (rxvt_screen *screen, const rgba &color) |
566 | { |
604 | { |
567 | #if XFT |
605 | #if XFT |
568 | XRenderPictFormat *format; |
606 | XRenderPictFormat *format; |
569 | |
607 | |
570 | // FUCKING Xft gets it wrong, of course, so work around it |
608 | // FUCKING Xft gets it wrong, of course, so work around it. |
571 | // transparency users should eat shit and die, and then |
609 | // Transparency users should eat shit and die, and then |
572 | // XRenderQueryPictIndexValues themselves plenty. |
610 | // XRenderQueryPictIndexValues themselves plenty. |
573 | if ((screen->visual->c_class == TrueColor || screen->visual->c_class == DirectColor) |
611 | if ((screen->visual->c_class == TrueColor) |
574 | && (format = XRenderFindVisualFormat (screen->xdisp, screen->visual))) |
612 | && (format = XRenderFindVisualFormat (screen->dpy, screen->visual))) |
575 | { |
613 | { |
576 | // the fun lies in doing everything manually... |
614 | // the fun lies in doing everything manually... |
577 | c.color.red = color.r; |
615 | c.color.red = color.r; |
578 | c.color.green = color.g; |
616 | c.color.green = color.g; |
579 | c.color.blue = color.b; |
617 | c.color.blue = color.b; |
580 | c.color.alpha = color.a; |
618 | c.color.alpha = color.a; |
581 | |
619 | |
582 | c.pixel = ((color.r * format->direct.redMask / rxvt_rgba::MAX_CC) << format->direct.red ) |
620 | c.pixel = ((color.r * format->direct.redMask / rgba::MAX_CC) << format->direct.red ) |
583 | | ((color.g * format->direct.greenMask / rxvt_rgba::MAX_CC) << format->direct.green) |
621 | | ((color.g * format->direct.greenMask / rgba::MAX_CC) << format->direct.green) |
584 | | ((color.b * format->direct.blueMask / rxvt_rgba::MAX_CC) << format->direct.blue ) |
622 | | ((color.b * format->direct.blueMask / rgba::MAX_CC) << format->direct.blue ) |
585 | | ((color.a * format->direct.alphaMask / rxvt_rgba::MAX_CC) << format->direct.alpha); |
623 | | ((color.a * format->direct.alphaMask / rgba::MAX_CC) << format->direct.alpha); |
586 | |
624 | |
587 | return true; |
625 | return true; |
588 | } |
626 | } |
589 | else |
627 | else |
590 | { |
628 | { |
… | |
… | |
593 | d.red = color.r; |
631 | d.red = color.r; |
594 | d.green = color.g; |
632 | d.green = color.g; |
595 | d.blue = color.b; |
633 | d.blue = color.b; |
596 | d.alpha = color.a; |
634 | d.alpha = color.a; |
597 | |
635 | |
598 | return XftColorAllocValue (screen->xdisp, screen->visual, screen->cmap, &d, &c); |
636 | return XftColorAllocValue (screen->dpy, screen->visual, screen->cmap, &d, &c); |
599 | } |
637 | } |
600 | #else |
638 | #else |
601 | if (screen->visual->c_class == TrueColor || screen->visual->c_class == DirectColor) |
639 | c.red = color.r; |
|
|
640 | c.green = color.g; |
|
|
641 | c.blue = color.b; |
|
|
642 | |
|
|
643 | if (screen->visual->c_class == TrueColor) |
602 | { |
644 | { |
603 | p = (color.r * (screen->visual->red_mask >> ctz (screen->visual->red_mask )) |
645 | c.pixel = (color.r * (screen->visual->red_mask >> ctz (screen->visual->red_mask )) |
604 | / rxvt_rgba::MAX_CC) << ctz (screen->visual->red_mask ) |
646 | / rgba::MAX_CC) << ctz (screen->visual->red_mask ) |
605 | | (color.g * (screen->visual->green_mask >> ctz (screen->visual->green_mask)) |
647 | | (color.g * (screen->visual->green_mask >> ctz (screen->visual->green_mask)) |
606 | / rxvt_rgba::MAX_CC) << ctz (screen->visual->green_mask) |
648 | / rgba::MAX_CC) << ctz (screen->visual->green_mask) |
607 | | (color.b * (screen->visual->blue_mask >> ctz (screen->visual->blue_mask )) |
649 | | (color.b * (screen->visual->blue_mask >> ctz (screen->visual->blue_mask )) |
608 | / rxvt_rgba::MAX_CC) << ctz (screen->visual->blue_mask ); |
650 | / rgba::MAX_CC) << ctz (screen->visual->blue_mask ); |
609 | |
651 | |
610 | return true; |
652 | return true; |
611 | } |
653 | } |
|
|
654 | else if (XAllocColor (screen->dpy, screen->cmap, &c)) |
|
|
655 | return true; |
612 | else |
656 | else |
613 | { |
|
|
614 | XColor xc; |
|
|
615 | |
|
|
616 | xc.red = color.r; |
|
|
617 | xc.green = color.g; |
|
|
618 | xc.blue = color.b; |
|
|
619 | |
|
|
620 | if (XAllocColor (screen->xdisp, screen->cmap, &xc)) |
|
|
621 | { |
|
|
622 | p = xc.pixel; |
|
|
623 | return true; |
|
|
624 | } |
|
|
625 | else |
|
|
626 | p = (color.r + color.g + color.b) > 128*3 |
657 | c.pixel = (color.r + color.g + color.b) > 128*3 |
627 | ? WhitePixelOfScreen (DefaultScreenOfDisplay (screen->xdisp)) |
658 | ? WhitePixelOfScreen (DefaultScreenOfDisplay (screen->dpy)) |
628 | : BlackPixelOfScreen (DefaultScreenOfDisplay (screen->xdisp)); |
659 | : BlackPixelOfScreen (DefaultScreenOfDisplay (screen->dpy)); |
629 | } |
|
|
630 | #endif |
660 | #endif |
631 | |
661 | |
632 | return false; |
662 | return false; |
633 | } |
663 | } |
634 | |
664 | |
635 | bool |
665 | bool |
636 | rxvt_color::set (rxvt_screen *screen, const char *name) |
666 | rxvt_color::set (rxvt_screen *screen, const char *name) |
637 | { |
667 | { |
638 | rxvt_rgba c; |
668 | rgba c; |
639 | char eos; |
669 | char eos; |
640 | int skip; |
670 | int skip; |
641 | |
671 | |
|
|
672 | // parse the nonstandard "[alphapercent]" prefix |
642 | if (1 <= sscanf (name, "[%hd]%n", &c.a, &skip)) |
673 | if (1 <= sscanf (name, "[%hd]%n", &c.a, &skip)) |
643 | { |
674 | { |
644 | printf ("X %d\n", c.a); |
|
|
645 | c.a = lerp<int, int, int> (0, rxvt_rgba::MAX_CC, c.a); |
675 | c.a = lerp<int, int, int> (0, rgba::MAX_CC, c.a); |
646 | name += skip; |
676 | name += skip; |
647 | printf ("y %04x\n", c.a); |
|
|
648 | } |
677 | } |
649 | else |
678 | else |
650 | c.a = rxvt_rgba::MAX_CC; |
679 | c.a = rgba::MAX_CC; |
651 | |
680 | |
652 | // parse the non-standard rgba format |
681 | // parse the non-standard "rgba:rrrr/gggg/bbbb/aaaa" format |
653 | if (strlen (name) != 4+5*4 || 4 != sscanf (name, "rgba:%hx/%hx/%hx/%hx%c", &c.r, &c.g, &c.b, &c.a, &eos)) |
682 | if (strlen (name) != 4+5*4 || 4 != sscanf (name, "rgba:%4hx/%4hx/%4hx/%4hx%c", &c.r, &c.g, &c.b, &c.a, &eos)) |
654 | { |
683 | { |
655 | XColor xc, xc_exact; |
684 | XColor xc, xc_exact; |
656 | |
685 | |
657 | if (XParseColor (screen->xdisp, screen->cmap, name, &xc)) |
686 | if (XParseColor (screen->dpy, screen->cmap, name, &xc)) |
658 | { |
687 | { |
659 | c.r = xc.red; |
688 | c.r = xc.red; |
660 | c.g = xc.green; |
689 | c.g = xc.green; |
661 | c.b = xc.blue; |
690 | c.b = xc.blue; |
662 | } |
691 | } |
… | |
… | |
672 | |
701 | |
673 | return set (screen, c); |
702 | return set (screen, c); |
674 | } |
703 | } |
675 | |
704 | |
676 | bool |
705 | bool |
677 | rxvt_color::set (rxvt_screen *screen, const rxvt_rgba &color) |
706 | rxvt_color::set (rxvt_screen *screen, const rgba &color) |
678 | { |
707 | { |
679 | bool got = alloc (screen, color); |
708 | bool got = alloc (screen, color); |
680 | |
709 | |
681 | #if !ENABLE_MINIMAL |
710 | #if !ENABLE_MINIMAL |
682 | int cmap_size = screen->visual->map_entries; |
711 | int cmap_size = screen->visual->map_entries; |
… | |
… | |
688 | XColor *colors = new XColor [screen->visual->map_entries]; |
717 | XColor *colors = new XColor [screen->visual->map_entries]; |
689 | |
718 | |
690 | for (int i = 0; i < cmap_size; i++) |
719 | for (int i = 0; i < cmap_size; i++) |
691 | colors [i].pixel = i; |
720 | colors [i].pixel = i; |
692 | |
721 | |
|
|
722 | // many kilobytes transfer per colour, but pseudocolor isn't worth |
|
|
723 | // many extra optimisations. |
693 | XQueryColors (screen->xdisp, screen->cmap, colors, cmap_size); |
724 | XQueryColors (screen->dpy, screen->cmap, colors, cmap_size); |
694 | |
725 | |
695 | int diff = 0x7fffffffUL; |
726 | int diff = 0x7fffffffUL; |
696 | XColor *best = colors; |
727 | XColor *best = colors; |
697 | |
728 | |
698 | for (int i = 0; i < cmap_size; i++) |
729 | for (int i = 0; i < cmap_size; i++) |
… | |
… | |
709 | } |
740 | } |
710 | |
741 | |
711 | //rxvt_warn ("could not allocate %04x %04x %04x, getting %04x %04x %04x instead (%d)\n", |
742 | //rxvt_warn ("could not allocate %04x %04x %04x, getting %04x %04x %04x instead (%d)\n", |
712 | // color.r, color.g, color.b, best->red, best->green, best->blue, diff); |
743 | // color.r, color.g, color.b, best->red, best->green, best->blue, diff); |
713 | |
744 | |
714 | got = alloc (screen, rxvt_rgba (best->red, best->green, best->blue)); |
745 | got = alloc (screen, rgba (best->red, best->green, best->blue)); |
715 | |
746 | |
716 | delete colors; |
747 | delete colors; |
717 | } |
748 | } |
718 | #endif |
749 | #endif |
719 | |
750 | |
720 | return got; |
751 | return got; |
721 | } |
752 | } |
722 | |
753 | |
723 | void |
754 | void |
724 | rxvt_color::get (rxvt_screen *screen, rxvt_rgba &color) |
755 | rxvt_color::get (rgba &color) |
725 | { |
756 | { |
726 | #if XFT |
757 | #if XFT |
727 | color.r = c.color.red; |
758 | color.r = c.color.red; |
728 | color.g = c.color.green; |
759 | color.g = c.color.green; |
729 | color.b = c.color.blue; |
760 | color.b = c.color.blue; |
730 | color.a = c.color.alpha; |
761 | color.a = c.color.alpha; |
731 | #else |
762 | #else |
732 | XColor c; |
|
|
733 | |
|
|
734 | c.pixel = p; |
|
|
735 | XQueryColor (screen->xdisp, screen->cmap, &c); |
|
|
736 | |
|
|
737 | color.r = c.red; |
763 | color.r = c.red; |
738 | color.g = c.green; |
764 | color.g = c.green; |
739 | color.b = c.blue; |
765 | color.b = c.blue; |
740 | color.a = rxvt_rgba::MAX_CC; |
766 | color.a = rgba::MAX_CC; |
741 | #endif |
767 | #endif |
|
|
768 | } |
|
|
769 | |
|
|
770 | void |
|
|
771 | rxvt_color::get (XColor &color) |
|
|
772 | { |
|
|
773 | rgba c; |
|
|
774 | get (c); |
|
|
775 | |
|
|
776 | color.red = c.r; |
|
|
777 | color.green = c.g; |
|
|
778 | color.blue = c.b; |
|
|
779 | color.pixel = (Pixel)*this; |
742 | } |
780 | } |
743 | |
781 | |
744 | void |
782 | void |
745 | rxvt_color::free (rxvt_screen *screen) |
783 | rxvt_color::free (rxvt_screen *screen) |
746 | { |
784 | { |
747 | #if XFT |
785 | #if XFT |
748 | XftColorFree (screen->xdisp, screen->visual, screen->cmap, &c); |
786 | XftColorFree (screen->dpy, screen->visual, screen->cmap, &c); |
749 | #else |
787 | #else |
750 | XFreeColors (screen->xdisp, screen->cmap, &p, 1, AllPlanes); |
788 | XFreeColors (screen->dpy, screen->cmap, &c.pixel, 1, AllPlanes); |
751 | #endif |
789 | #endif |
752 | } |
790 | } |
753 | |
791 | |
754 | void |
792 | void |
755 | rxvt_color::fade (rxvt_screen *screen, int percent, rxvt_color &result, const rxvt_rgba &to) |
793 | rxvt_color::fade (rxvt_screen *screen, int percent, rxvt_color &result, const rgba &to) |
756 | { |
794 | { |
757 | rxvt_rgba c; |
795 | rgba c; |
758 | get (screen, c); |
796 | get (c); |
759 | |
797 | |
760 | result.set ( |
798 | result.set ( |
761 | screen, |
799 | screen, |
762 | rxvt_rgba ( |
800 | rgba ( |
763 | lerp (c.r, to.r, percent), |
801 | lerp (c.r, to.r, percent), |
764 | lerp (c.g, to.g, percent), |
802 | lerp (c.g, to.g, percent), |
765 | lerp (c.b, to.b, percent), |
803 | lerp (c.b, to.b, percent), |
766 | lerp (c.a, to.a, percent) |
804 | lerp (c.a, to.a, percent) |
767 | ) |
805 | ) |