]> icculus.org git repositories - dana/openbox.git/blob - otk/rendercolor.cc
rm a stupid bb format
[dana/openbox.git] / 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     _created(false)
33 {
34 }
35
36 RenderColor::RenderColor(int screen, RGB rgb)
37   : _screen(screen),
38     _red(rgb.r),
39     _green(rgb.g),
40     _blue(rgb.b),
41     _allocated(false),
42     _created(false)
43 {
44 }
45
46 void RenderColor::create() const
47 {
48   unsigned long color = _blue | _green << 8 | _red << 16;
49   
50   // try get a gc from the cache
51   CacheItem *item = _cache[_screen][color];
52
53   if (item) {
54     _gc = item->gc;
55     _pixel = item->pixel;
56     ++item->count;
57   } else {
58     XGCValues gcv;
59
60     // allocate a color and GC from the server
61     const ScreenInfo *info = display->screenInfo(_screen);
62
63     XColor xcol;    // convert from 0-0xff to 0-0xffff
64     xcol.red = (_red << 8) | _red;
65     xcol.green = (_green << 8) | _green;
66     xcol.blue = (_blue << 8) | _blue;
67     xcol.pixel = 0;
68
69     if (!XAllocColor(**display, info->colormap(), &xcol)) {
70       fprintf(stderr, "RenderColor: color alloc error: rgb:%x/%x/%x\n",
71               _red, _green, _blue);
72       xcol.pixel = 0;
73     } else
74       _allocated = true;
75
76     _pixel = xcol.pixel;
77     gcv.foreground = _pixel;
78     gcv.cap_style = CapProjecting;
79     _gc = XCreateGC(**display, info->rootWindow(),
80                     GCForeground | GCCapStyle, &gcv);
81     assert(_gc);
82
83     // insert into the cache
84     item = new CacheItem(_gc, _pixel);
85     _cache[_screen][color] = item;
86     ++item->count;
87   }
88
89   _created = true;
90 }
91
92 unsigned long RenderColor::pixel() const
93 {
94   if (!_created) create();
95   return _pixel;
96 }
97
98 GC RenderColor::gc() const
99 {
100   if (!_created) create();
101   return _gc;
102 }
103
104 RenderColor::~RenderColor()
105 {
106   unsigned long color = _blue | _green << 8 | _red << 16;
107
108   if (_created) {
109     CacheItem *item = _cache[_screen][color];
110     assert(item); // better be...
111
112     if (--item->count <= 0) {
113       // remove from the cache
114       XFreeGC(**display, _gc);
115       _cache[_screen][color] = 0;
116       delete item;
117
118       if (_allocated) {
119         const ScreenInfo *info = display->screenInfo(_screen);
120         XFreeColors(**display, info->colormap(), &_pixel, 1, 0);
121       }
122     }
123   }
124 }
125
126 }