]> Dogcows Code - chaz/openbox/blob - otk/rendercolor.cc
only store what we need to in the class
[chaz/openbox] / otk / rendercolor.cc
1 // -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 2; -*-
2
3 #include "config.h"
4
5 #include "rendercolor.hh"
6 #include "display.hh"
7 #include "screeninfo.hh"
8
9 #include <cstdio>
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 _allocated(false)
32 {
33 }
34
35 RenderColor::RenderColor(int screen, RGB rgb)
36 : _screen(screen),
37 _red(rgb.r),
38 _green(rgb.g),
39 _blue(rgb.b),
40 _allocated(false)
41 {
42 }
43
44 void RenderColor::create() const
45 {
46 unsigned long color = _blue | _green << 8 | _red << 16;
47
48 // try get a gc from the cache
49 CacheItem *item = _cache[_screen][color];
50
51 if (item) {
52 _gc = item->gc;
53 _pixel = item->pixel;
54 ++item->count;
55 } else {
56 XGCValues gcv;
57
58 // allocate a color and GC from the server
59 const ScreenInfo *info = display->screenInfo(_screen);
60
61 XColor xcol; // convert from 0-0xff to 0-0xffff
62 xcol.red = (_red << 8) | _red;
63 xcol.green = (_green << 8) | _green;
64 xcol.blue = (_blue << 8) | _blue;
65 xcol.pixel = 0;
66
67 if (! XAllocColor(**display, info->colormap(), &xcol)) {
68 fprintf(stderr, "RenderColor: color alloc error: rgb:%x/%x/%x\n",
69 _red, _green, _blue);
70 xcol.pixel = 0;
71 }
72
73 _pixel = xcol.pixel;
74 gcv.foreground = _pixel;
75 gcv.cap_style = CapProjecting;
76 _gc = XCreateGC(**display, info->rootWindow(),
77 GCForeground | GCCapStyle, &gcv);
78 assert(_gc);
79
80 // insert into the cache
81 item = new CacheItem(_gc, _pixel);
82 _cache[_screen][color] = item;
83 ++item->count;
84 }
85
86 _allocated = true;
87 }
88
89 unsigned long RenderColor::pixel() const
90 {
91 if (!_allocated) create();
92 return _pixel;
93 }
94
95 GC RenderColor::gc() const
96 {
97 if (!_allocated) create();
98 return _gc;
99 }
100
101 RenderColor::~RenderColor()
102 {
103 unsigned long color = _blue | _green << 8 | _red << 16;
104
105 CacheItem *item = _cache[_screen][color];
106
107 if (item) {
108 if (--item->count <= 0) {
109 // remove from the cache
110 XFreeGC(**display, _gc);
111 _cache[_screen][color] = 0;
112 delete item;
113
114 const ScreenInfo *info = display->screenInfo(_screen);
115 XFreeColors(**display, info->colormap(), &_pixel, 1, 0);
116 }
117 }
118 }
119
120 }
This page took 0.03699 seconds and 4 git commands to generate.