2003-01-20 07:07:13 +00:00
|
|
|
// -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 2; -*-
|
|
|
|
|
2003-02-09 23:07:54 +00:00
|
|
|
#include "config.h"
|
2003-01-20 07:07:13 +00:00
|
|
|
|
|
|
|
#include "rendercolor.hh"
|
|
|
|
#include "display.hh"
|
|
|
|
#include "screeninfo.hh"
|
|
|
|
|
2003-02-09 23:07:54 +00:00
|
|
|
#include <cstdio>
|
2003-01-23 02:16:07 +00:00
|
|
|
|
2003-01-20 07:07:13 +00:00
|
|
|
namespace otk {
|
|
|
|
|
|
|
|
std::map<unsigned long, RenderColor::CacheItem*> *RenderColor::_cache = 0;
|
|
|
|
|
|
|
|
void RenderColor::initialize()
|
|
|
|
{
|
|
|
|
_cache = new std::map<unsigned long, CacheItem*>[ScreenCount(**display)];
|
|
|
|
}
|
|
|
|
|
|
|
|
void RenderColor::destroy()
|
|
|
|
{
|
|
|
|
delete [] _cache;
|
|
|
|
}
|
|
|
|
|
|
|
|
RenderColor::RenderColor(int screen, unsigned char red,
|
|
|
|
unsigned char green, unsigned char blue)
|
|
|
|
: _screen(screen),
|
|
|
|
_red(red),
|
|
|
|
_green(green),
|
|
|
|
_blue(blue),
|
2003-02-13 05:12:01 +00:00
|
|
|
_allocated(false),
|
|
|
|
_created(false)
|
2003-01-22 20:14:28 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
RenderColor::RenderColor(int screen, RGB rgb)
|
|
|
|
: _screen(screen),
|
|
|
|
_red(rgb.r),
|
|
|
|
_green(rgb.g),
|
|
|
|
_blue(rgb.b),
|
2003-02-13 05:12:01 +00:00
|
|
|
_allocated(false),
|
|
|
|
_created(false)
|
2003-01-22 20:14:28 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2003-02-13 04:50:57 +00:00
|
|
|
void RenderColor::create() const
|
2003-01-20 07:07:13 +00:00
|
|
|
{
|
|
|
|
unsigned long color = _blue | _green << 8 | _red << 16;
|
|
|
|
|
|
|
|
// try get a gc from the cache
|
|
|
|
CacheItem *item = _cache[_screen][color];
|
|
|
|
|
|
|
|
if (item) {
|
|
|
|
_gc = item->gc;
|
2003-01-22 23:16:49 +00:00
|
|
|
_pixel = item->pixel;
|
2003-01-20 07:07:13 +00:00
|
|
|
++item->count;
|
|
|
|
} else {
|
|
|
|
XGCValues gcv;
|
|
|
|
|
|
|
|
// allocate a color and GC from the server
|
|
|
|
const ScreenInfo *info = display->screenInfo(_screen);
|
|
|
|
|
|
|
|
XColor xcol; // convert from 0-0xff to 0-0xffff
|
2003-02-12 01:22:51 +00:00
|
|
|
xcol.red = (_red << 8) | _red;
|
|
|
|
xcol.green = (_green << 8) | _green;
|
|
|
|
xcol.blue = (_blue << 8) | _blue;
|
2003-01-20 07:07:13 +00:00
|
|
|
xcol.pixel = 0;
|
|
|
|
|
2003-02-13 05:18:41 +00:00
|
|
|
if (!XAllocColor(**display, info->colormap(), &xcol)) {
|
2003-01-20 07:07:13 +00:00
|
|
|
fprintf(stderr, "RenderColor: color alloc error: rgb:%x/%x/%x\n",
|
|
|
|
_red, _green, _blue);
|
|
|
|
xcol.pixel = 0;
|
2003-02-13 05:12:01 +00:00
|
|
|
} else
|
|
|
|
_allocated = true;
|
2003-01-20 07:07:13 +00:00
|
|
|
|
2003-01-22 22:46:16 +00:00
|
|
|
_pixel = xcol.pixel;
|
|
|
|
gcv.foreground = _pixel;
|
2003-01-20 17:07:42 +00:00
|
|
|
gcv.cap_style = CapProjecting;
|
|
|
|
_gc = XCreateGC(**display, info->rootWindow(),
|
|
|
|
GCForeground | GCCapStyle, &gcv);
|
2003-01-20 07:07:13 +00:00
|
|
|
assert(_gc);
|
|
|
|
|
|
|
|
// insert into the cache
|
2003-01-22 23:16:49 +00:00
|
|
|
item = new CacheItem(_gc, _pixel);
|
2003-01-22 23:14:42 +00:00
|
|
|
_cache[_screen][color] = item;
|
|
|
|
++item->count;
|
2003-01-20 07:07:13 +00:00
|
|
|
}
|
2003-02-13 04:50:57 +00:00
|
|
|
|
2003-02-13 05:12:01 +00:00
|
|
|
_created = true;
|
2003-02-13 04:50:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
unsigned long RenderColor::pixel() const
|
|
|
|
{
|
2003-02-13 05:12:01 +00:00
|
|
|
if (!_created) create();
|
2003-02-13 04:50:57 +00:00
|
|
|
return _pixel;
|
|
|
|
}
|
|
|
|
|
|
|
|
GC RenderColor::gc() const
|
|
|
|
{
|
2003-02-13 05:12:01 +00:00
|
|
|
if (!_created) create();
|
2003-02-13 04:50:57 +00:00
|
|
|
return _gc;
|
2003-01-20 07:07:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
RenderColor::~RenderColor()
|
|
|
|
{
|
|
|
|
unsigned long color = _blue | _green << 8 | _red << 16;
|
2003-01-22 23:14:42 +00:00
|
|
|
|
2003-02-13 05:12:01 +00:00
|
|
|
if (_created) {
|
2003-02-13 04:54:54 +00:00
|
|
|
CacheItem *item = _cache[_screen][color];
|
|
|
|
assert(item); // better be...
|
2003-01-20 07:07:13 +00:00
|
|
|
|
2003-02-13 04:50:57 +00:00
|
|
|
if (--item->count <= 0) {
|
|
|
|
// remove from the cache
|
|
|
|
XFreeGC(**display, _gc);
|
|
|
|
_cache[_screen][color] = 0;
|
|
|
|
delete item;
|
|
|
|
|
2003-02-13 05:12:01 +00:00
|
|
|
if (_allocated) {
|
|
|
|
const ScreenInfo *info = display->screenInfo(_screen);
|
|
|
|
XFreeColors(**display, info->colormap(), &_pixel, 1, 0);
|
|
|
|
}
|
2003-02-13 04:50:57 +00:00
|
|
|
}
|
2003-01-20 07:07:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|