1 // -*- mode: C; indent-tabs-mode: nil; -*-
6 #include "screeninfo.h"
12 static Bool cleancache = False;
13 static PyObject *colorcache = NULL;
15 static void otkcolor_dealloc(OtkColor* self)
17 // when this is called, the color has already been cleaned out of the cache
18 PyObject_Del((PyObject*)self);
21 static int otkcolor_compare(OtkColor *c1, OtkColor *c2)
26 p1 = c1->red << 16 | c1->green << 8 | c1->blue;
27 p2 = c2->red << 16 | c2->green << 8 | c2->blue;
38 static PyObject *otkcolor_repr(OtkColor *self)
40 return PyString_FromFormat("rgb:%x/%x/%x", self->red, self->green,
44 static long otkcolor_hash(OtkColor *self)
46 return self->screen << 24 | self->red << 16 | self->green << 8 | self->blue;
49 static PyTypeObject OtkColor_Type = {
50 PyObject_HEAD_INIT(NULL)
55 (destructor)otkcolor_dealloc, /*tp_dealloc*/
59 (cmpfunc)otkcolor_compare, /*tp_compare*/
60 (reprfunc)otkcolor_repr, /*tp_repr*/
64 (hashfunc)otkcolor_hash, /*tp_hash */
68 static void parseColorName(OtkColor *self, const char *name) {
71 // get rgb values from colorname
77 if (!XParseColor(OBDisplay->display,
78 OtkDisplay_ScreenInfo(OBDisplay, self->screen)->colormap,
80 fprintf(stderr, "OtkColor: color parse error: \"%s\"\n", name);
81 self->red = self->green = self->blue = 0;
83 self->red = xcol.red >> 8;
84 self->green = xcol.green >> 8;
85 self->blue = xcol.blue >> 8;
89 static void doCacheCleanup() {
90 unsigned long *pixels;
93 PyObject *key; // this is a color too, but i dont need to use it as such
96 // ### TODO - support multiple displays!
97 if (!PyDict_Size(colorcache)) return; // nothing to do
99 pixels = malloc(sizeof(unsigned long) * PyDict_Size(colorcache));
101 for (i = 0; i < ScreenCount(OBDisplay->display); i++) {
105 while (PyDict_Next(colorcache, &ppos, &key, (PyObject**)&color)) {
106 // get the screen from the hash
107 if (color->screen != i) continue; // wrong screen
109 // does someone other than the cache have a reference? (the cache gets 2)
110 if (color->ob_refcnt > 2)
113 pixels[count++] = color->pixel;
114 PyDict_DelItem(colorcache, key);
115 --ppos; // back up one in the iteration
119 XFreeColors(OBDisplay->display,
120 OtkDisplay_ScreenInfo(OBDisplay, i)->colormap,
128 static void allocate(OtkColor *self) {
131 assert(!self->allocated);
133 // allocate color from rgb values
134 xcol.red = self->red | self->red << 8;
135 xcol.green = self->green | self->green << 8;
136 xcol.blue = self->blue | self->blue << 8;
139 if (!XAllocColor(OBDisplay->display,
140 OtkDisplay_ScreenInfo(OBDisplay, self->screen)->colormap,
142 fprintf(stderr, "OtkColor: color alloc error: rgb:%x/%x/%x\n",
143 self->red, self->green, self->blue);
147 self->pixel = xcol.pixel;
148 self->allocated = True;
154 PyObject *OtkColor_FromRGB(int r, int g, int b, int screen)
156 OtkColor *self = PyObject_New(OtkColor, &OtkColor_Type);
159 assert(screen >= 0); assert(r >= 0); assert(g >= 0); assert(b >= 0);
160 assert(r <= 0xff); assert(g <= 0xff); assert(b <= 0xff);
162 if (!colorcache) colorcache = PyDict_New();
164 self->allocated = False;
169 self->screen = screen;
171 // does this color already exist in the cache?
172 cached = PyDict_GetItem(colorcache, (PyObject*)self);
178 // add it to the cache
179 PyDict_SetItem(colorcache, (PyObject*)self, (PyObject*)self);
180 return (PyObject*)self;
183 PyObject *OtkColor_FromName(const char *name, int screen)
185 OtkColor *self = PyObject_New(OtkColor, &OtkColor_Type);
188 assert(screen >= 0); assert(name);
190 if (!colorcache) colorcache = PyDict_New();
192 self->allocated = False;
197 self->screen = screen;
199 parseColorName(self, name);
201 // does this color already exist in the cache?
202 cached = PyDict_GetItem(colorcache, (PyObject*)self);
208 // add it to the cache
209 PyDict_SetItem(colorcache, (PyObject*)self, (PyObject*)self);
210 return (PyObject*)self;
213 unsigned long OtkColor_Pixel(OtkColor *self)
215 if (!self->allocated)
220 void OtkColor_CleanupColorCache()