]> Dogcows Code - chaz/openbox/blob - otk/rendercolor.cc
kill some whack extra whitespace
[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 #include "rendercontrol.hh"
9
10 #include <cstdio>
11
12 namespace otk {
13
14 std::map<unsigned long, RenderColor::CacheItem*> *RenderColor::_cache = 0;
15
16 void RenderColor::initialize()
17 {
18 _cache = new std::map<unsigned long, CacheItem*>[ScreenCount(**display)];
19 }
20
21 void RenderColor::destroy()
22 {
23 delete [] _cache;
24 }
25
26 RenderColor::RenderColor(int screen, unsigned char red,
27 unsigned char green, unsigned char blue)
28 : _screen(screen),
29 _red(red),
30 _green(green),
31 _blue(blue)
32 {
33 create();
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 {
42 create();
43 }
44
45 void RenderColor::create()
46 {
47 unsigned long color = _blue | _green << 8 | _red << 16;
48
49 // try get a gc from the cache
50 CacheItem *item = _cache[_screen][color];
51
52 if (item) {
53 _gc = item->gc;
54 _pixel = item->pixel;
55 ++item->count;
56 } else {
57 XGCValues gcv;
58
59 // allocate a color and GC from the server
60 const ScreenInfo *info = display->screenInfo(_screen);
61
62 XColor xcol; // convert from 0-0xff to 0-0xffff
63 xcol.red = (_red << 8) | _red;
64 xcol.green = (_green << 8) | _green;
65 xcol.blue = (_blue << 8) | _blue;
66
67 display->renderControl(_screen)->allocateColor(&xcol);
68
69 _pixel = xcol.pixel;
70 gcv.foreground = _pixel;
71 gcv.cap_style = CapProjecting;
72 _gc = XCreateGC(**display, info->rootWindow(),
73 GCForeground | GCCapStyle, &gcv);
74 assert(_gc);
75
76 // insert into the cache
77 item = new CacheItem(_gc, _pixel);
78 _cache[_screen][color] = item;
79 ++item->count;
80 }
81 }
82
83 RenderColor::~RenderColor()
84 {
85 unsigned long color = _blue | _green << 8 | _red << 16;
86
87 CacheItem *item = _cache[_screen][color];
88 assert(item); // better be...
89
90 if (--item->count <= 0) {
91 // remove from the cache
92 XFreeGC(**display, _gc);
93 _cache[_screen][color] = 0;
94 delete item;
95
96 const ScreenInfo *info = display->screenInfo(_screen);
97 XFreeColors(**display, info->colormap(), &_pixel, 1, 0);
98 }
99 }
100
101 }
This page took 0.040772 seconds and 4 git commands to generate.