]> Dogcows Code - chaz/openbox/blob - otk/rendercolor.cc
add stdio.h
[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 printf("CREATE COLOR: %lx\n", color);
57
58 // try get a gc from the cache
59 CacheItem *item = _cache[_screen][color];
60
61 if (item) {
62 _gc = item->gc;
63 _pixel = item->pixel;
64 ++item->count;
65 } else {
66 XGCValues gcv;
67
68 // allocate a color and GC from the server
69 const ScreenInfo *info = display->screenInfo(_screen);
70
71 XColor xcol; // convert from 0-0xff to 0-0xffff
72 xcol.red = _red; xcol.red |= xcol.red << 8;
73 xcol.green = _green; xcol.green |= xcol.green << 8;
74 xcol.blue = _blue; xcol.blue |= xcol.blue << 8;
75 xcol.pixel = 0;
76
77 if (! XAllocColor(**display, info->colormap(), &xcol)) {
78 fprintf(stderr, "RenderColor: color alloc error: rgb:%x/%x/%x\n",
79 _red, _green, _blue);
80 xcol.pixel = 0;
81 }
82
83 _pixel = xcol.pixel;
84 gcv.foreground = _pixel;
85 gcv.cap_style = CapProjecting;
86 _gc = XCreateGC(**display, info->rootWindow(),
87 GCForeground | GCCapStyle, &gcv);
88 assert(_gc);
89
90 // insert into the cache
91 item = new CacheItem(_gc, _pixel);
92 _cache[_screen][color] = item;
93 ++item->count;
94 }
95 }
96
97 RenderColor::~RenderColor()
98 {
99 unsigned long color = _blue | _green << 8 | _red << 16;
100
101 CacheItem *item = _cache[_screen][color];
102 assert(item); // it better be in the cache ...
103
104 printf("DESTROY COLOR: %lx %d\n", color, item->count);
105
106 if (--item->count <= 0) {
107 // remove from the cache
108 XFreeGC(**display, _gc);
109 _cache[_screen][color] = 0;
110 delete item;
111 }
112 }
113
114 }
This page took 0.038787 seconds and 5 git commands to generate.