all repos — openbox @ 104c1a164b1e4e881e141d14263895401779d453

openbox fork - make it a bit more like ryudo

otk/color.cc (raw)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
// -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 2; -*-

#ifdef HAVE_CONFIG_H
#  include "../config.h"
#endif // HAVE_CONFIG_H

extern "C" {
#include <stdio.h>
}

#include <assert.h>

#include "color.hh"
#include "display.hh"
#include "screeninfo.hh"

namespace otk {

Color::ColorCache Color::colorcache;
bool Color::cleancache = false;

Color::Color(unsigned int _screen)
  : allocated(false), r(-1), g(-1), b(-1), p(0), scrn(_screen)
{}

Color::Color(int _r, int _g, int _b, unsigned int _screen)
  : allocated(false), r(_r), g(_g), b(_b), p(0), scrn(_screen)
{}


Color::Color(const std::string &_name, unsigned int _screen)
  : allocated(false), r(-1), g(-1), b(-1), p(0), scrn(_screen),
    colorname(_name) {
  parseColorName();
}


Color::~Color(void) {
  deallocate();
}


void Color::setScreen(unsigned int _screen) {
  if (_screen == screen()) {
    // nothing to do
    return;
  }

  deallocate();

  scrn = _screen;

  if (! colorname.empty()) {
    parseColorName();
  }
}


unsigned long Color::pixel(void) const {
  if (! allocated) {
    // mutable
    Color *that = (Color *) this;
    that->allocate();
  }

  return p;
}


void Color::parseColorName(void) {
  if (colorname.empty()) {
    fprintf(stderr, "Color: empty colorname, cannot parse (using black)\n");
    setRGB(0, 0, 0);
  }

  if (scrn == ~(0u))
    scrn = DefaultScreen(**display);
  Colormap colormap = display->screenInfo(scrn)->colormap();

  // get rgb values from colorname
  XColor xcol;
  xcol.red = 0;
  xcol.green = 0;
  xcol.blue = 0;
  xcol.pixel = 0;

  if (! XParseColor(**display, colormap,
                    colorname.c_str(), &xcol)) {
    fprintf(stderr, "Color::allocate: color parse error: \"%s\"\n",
            colorname.c_str());
    setRGB(0, 0, 0);
    return;
  }

  setRGB(xcol.red >> 8, xcol.green >> 8, xcol.blue >> 8);
}


void Color::allocate(void) {
  if (scrn == ~(0u)) scrn = DefaultScreen(**display);
  Colormap colormap = display->screenInfo(scrn)->colormap();

  if (! isValid()) {
    if (colorname.empty()) {
      fprintf(stderr, "Color: cannot allocate invalid color (using black)\n");
      setRGB(0, 0, 0);
    } else {
      parseColorName();
    }
  }

  // see if we have allocated this color before
  RGB rgb(scrn, r, g, b);
  ColorCache::iterator it = colorcache.find(rgb);
  if (it != colorcache.end()) {
    // found
    allocated = true;
    p = (*it).second.p;
    (*it).second.count++;
    return;
  }

  // allocate color from rgb values
  XColor xcol;
  xcol.red =   r | r << 8;
  xcol.green = g | g << 8;
  xcol.blue =  b | b << 8;
  xcol.pixel = 0;

  if (! XAllocColor(**display, colormap, &xcol)) {
    fprintf(stderr, "Color::allocate: color alloc error: rgb:%x/%x/%x\n",
            r, g, b);
    xcol.pixel = 0;
  }

  p = xcol.pixel;
  allocated = true;

  colorcache.insert(ColorCacheItem(rgb, PixelRef(p)));

  if (cleancache)
    doCacheCleanup();
}


void Color::deallocate(void) {
  if (! allocated)
    return;

  ColorCache::iterator it = colorcache.find(RGB(scrn, r, g, b));
  if (it != colorcache.end()) {
    if ((*it).second.count >= 1)
      (*it).second.count--;
  }

  if (cleancache)
    doCacheCleanup();

  allocated = false;
}


Color &Color::operator=(const Color &c) {
  deallocate();

  setRGB(c.r, c.g, c.b);
  colorname = c.colorname;
  scrn = c.scrn;
  return *this;
}


void Color::cleanupColorCache(void) {
  cleancache = true;
}


void Color::doCacheCleanup(void) {
  // ### TODO - support multiple displays!
  ColorCache::iterator it = colorcache.begin();
  if (it == colorcache.end()) {
    // nothing to do
    return;
  }

  unsigned long *pixels = new unsigned long[ colorcache.size() ];
  int i;
  unsigned count;

  for (i = 0; i < ScreenCount(**display); i++) {
    count = 0;
    it = colorcache.begin();

    while (it != colorcache.end()) {
      if ((*it).second.count != 0 || (*it).first.screen != i) {
        ++it;
        continue;
      }

      pixels[ count++ ] = (*it).second.p;
      ColorCache::iterator it2 = it;
      ++it;
      colorcache.erase(it2);
    }

    if (count > 0)
      XFreeColors(**display, display->screenInfo(i)->colormap(),
                  pixels, count, 0);
  }

  delete [] pixels;
  cleancache = false;
}

}