]> Dogcows Code - chaz/openbox/blob - otk/rendercolor.cc
use otk objects in the ob scripts by importing otk
[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 extern "C" {
12 #ifdef HAVE_STDIO_H
13 # include <stdio.h>
14 #endif
15 }
16
17 namespace otk {
18
19 std::map<unsigned long, RenderColor::CacheItem*> *RenderColor::_cache = 0;
20
21 void RenderColor::initialize()
22 {
23 _cache = new std::map<unsigned long, CacheItem*>[ScreenCount(**display)];
24 }
25
26 void RenderColor::destroy()
27 {
28 delete [] _cache;
29 }
30
31 RenderColor::RenderColor(int screen, unsigned char red,
32 unsigned char green, unsigned char blue)
33 : _screen(screen),
34 _red(red),
35 _green(green),
36 _blue(blue),
37 _gc(0)
38 {
39 create();
40 }
41
42 RenderColor::RenderColor(int screen, RGB rgb)
43 : _screen(screen),
44 _red(rgb.r),
45 _green(rgb.g),
46 _blue(rgb.b),
47 _gc(0)
48 {
49 create();
50 }
51
52 void RenderColor::create()
53 {
54 unsigned long color = _blue | _green << 8 | _red << 16;
55
56 // try get a gc from the cache
57 CacheItem *item = _cache[_screen][color];
58
59 if (item) {
60 _gc = item->gc;
61 _pixel = item->pixel;
62 ++item->count;
63 } else {
64 XGCValues gcv;
65
66 // allocate a color and GC from the server
67 const ScreenInfo *info = display->screenInfo(_screen);
68
69 XColor xcol; // convert from 0-0xff to 0-0xffff
70 xcol.red = _red; xcol.red |= xcol.red << 8;
71 xcol.green = _green; xcol.green |= xcol.green << 8;
72 xcol.blue = _blue; xcol.blue |= xcol.blue << 8;
73 xcol.pixel = 0;
74
75 if (! XAllocColor(**display, info->colormap(), &xcol)) {
76 fprintf(stderr, "RenderColor: color alloc error: rgb:%x/%x/%x\n",
77 _red, _green, _blue);
78 xcol.pixel = 0;
79 }
80
81 _pixel = xcol.pixel;
82 gcv.foreground = _pixel;
83 gcv.cap_style = CapProjecting;
84 _gc = XCreateGC(**display, info->rootWindow(),
85 GCForeground | GCCapStyle, &gcv);
86 assert(_gc);
87
88 // insert into the cache
89 item = new CacheItem(_gc, _pixel);
90 _cache[_screen][color] = item;
91 ++item->count;
92 }
93 }
94
95 RenderColor::~RenderColor()
96 {
97 unsigned long color = _blue | _green << 8 | _red << 16;
98
99 CacheItem *item = _cache[_screen][color];
100 assert(item); // it better be in the cache ...
101
102 if (--item->count <= 0) {
103 // remove from the cache
104 XFreeGC(**display, _gc);
105 _cache[_screen][color] = 0;
106 delete item;
107 }
108 }
109
110 }
This page took 0.035947 seconds and 4 git commands to generate.