]> Dogcows Code - chaz/openbox/blob - otk/rendercolor.cc
1a95ded992c8063f5beb286271a6973695d81317
[chaz/openbox] / otk / rendercolor.cc
1 // -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 2; -*-
2
3 #ifdef HAVE_CONFIG_H
4 # include "../config.h"
5 #endif // HAVE_CONFIG_H
6
7 #include "rendercolor.hh"
8 #include "display.hh"
9 #include "screeninfo.hh"
10
11 namespace otk {
12
13 std::map<unsigned long, RenderColor::CacheItem*> *RenderColor::_cache = 0;
14
15 void RenderColor::initialize()
16 {
17 _cache = new std::map<unsigned long, CacheItem*>[ScreenCount(**display)];
18 }
19
20 void RenderColor::destroy()
21 {
22 delete [] _cache;
23 }
24
25 RenderColor::RenderColor(int screen, unsigned char red,
26 unsigned char green, unsigned char blue)
27 : _screen(screen),
28 _red(red),
29 _green(green),
30 _blue(blue),
31 _gc(0)
32 {
33 unsigned long color = _blue | _green << 8 | _red << 16;
34
35 // try get a gc from the cache
36 CacheItem *item = _cache[_screen][color];
37
38 if (item) {
39 _gc = item->gc;
40 ++item->count;
41 } else {
42 XGCValues gcv;
43
44 // allocate a color and GC from the server
45 const ScreenInfo *info = display->screenInfo(_screen);
46
47 XColor xcol; // convert from 0-0xff to 0-0xffff
48 xcol.red = _red; xcol.red |= xcol.red << 8;
49 xcol.green = _green; xcol.green |= xcol.green << 8;
50 xcol.blue = _blue; xcol.blue |= xcol.blue << 8;
51 xcol.pixel = 0;
52
53 if (! XAllocColor(**display, info->colormap(), &xcol)) {
54 fprintf(stderr, "RenderColor: color alloc error: rgb:%x/%x/%x\n",
55 _red, _green, _blue);
56 xcol.pixel = 0;
57 }
58
59 gcv.foreground = xcol.pixel;
60 gcv.cap_style = CapProjecting;
61 _gc = XCreateGC(**display, info->rootWindow(),
62 GCForeground | GCCapStyle, &gcv);
63 assert(_gc);
64
65 // insert into the cache
66 _cache[_screen][color] = new CacheItem(_gc);
67 }
68 }
69
70 RenderColor::~RenderColor()
71 {
72 unsigned long color = _blue | _green << 8 | _red << 16;
73
74 CacheItem *item = _cache[_screen][color];
75 assert(item); // it better be in the cache ...
76
77 if (--item->count <= 0) {
78 // remove from the cache
79 XFreeGC(**display, _gc);
80 _cache[_screen][color] = 0;
81 delete item;
82 }
83 }
84
85 }
This page took 0.041485 seconds and 3 git commands to generate.