1 // -*- mode: C; indent-tabs-mode: nil; -*-
6 #include "screeninfo.h"
12 static Bool cleancache
= False
;
13 static PyObject
*colorcache
= NULL
;
15 static void parseColorName(OtkColor
*self
, const char *name
) {
18 // get rgb values from colorname
24 if (!XParseColor(OBDisplay
->display
,
25 OtkDisplay_ScreenInfo(OBDisplay
, self
->screen
)->colormap
,
27 fprintf(stderr
, "OtkColor: color parse error: \"%s\"\n", name
);
28 self
->red
= self
->green
= self
->blue
= 0;
30 self
->red
= xcol
.red
>> 8;
31 self
->green
= xcol
.green
>> 8;
32 self
->blue
= xcol
.blue
>> 8;
36 static void doCacheCleanup() {
37 unsigned long *pixels
;
40 PyObject
*key
; // this is a color too, but i dont need to use it as such
43 // ### TODO - support multiple displays!
44 if (!PyDict_Size(colorcache
)) return; // nothing to do
46 pixels
= malloc(sizeof(unsigned long) * PyDict_Size(colorcache
));
48 for (i
= 0; i
< ScreenCount(OBDisplay
->display
); i
++) {
52 while (PyDict_Next(colorcache
, &ppos
, &key
, (PyObject
**)&color
)) {
53 // get the screen from the hash
54 if (color
->screen
!= i
) continue; // wrong screen
56 // does someone other than the cache have a reference? (the cache gets 2)
57 if (color
->ob_refcnt
> 2)
60 pixels
[count
++] = color
->pixel
;
61 PyDict_DelItem(colorcache
, key
);
62 --ppos
; // back up one in the iteration
66 XFreeColors(OBDisplay
->display
,
67 OtkDisplay_ScreenInfo(OBDisplay
, i
)->colormap
,
75 static void allocate(OtkColor
*self
) {
78 // allocate color from rgb values
79 xcol
.red
= self
->red
| self
->red
<< 8;
80 xcol
.green
= self
->green
| self
->green
<< 8;
81 xcol
.blue
= self
->blue
| self
->blue
<< 8;
84 if (!XAllocColor(OBDisplay
->display
,
85 OtkDisplay_ScreenInfo(OBDisplay
, self
->screen
)->colormap
,
87 fprintf(stderr
, "OtkColor: color alloc error: rgb:%x/%x/%x\n",
88 self
->red
, self
->green
, self
->blue
);
92 self
->pixel
= xcol
.pixel
;
98 PyObject
*OtkColor_FromRGB(int r
, int g
, int b
, int screen
)
100 OtkColor
*self
= PyObject_New(OtkColor
, &OtkColor_Type
);
103 assert(screen
>= 0); assert(r
>= 0); assert(g
>= 0); assert(b
>= 0);
104 assert(r
<= 0xff); assert(g
<= 0xff); assert(b
<= 0xff);
106 if (!colorcache
) colorcache
= PyDict_New();
111 self
->screen
= screen
;
113 // does this color already exist in the cache?
114 cached
= PyDict_GetItem(colorcache
, (PyObject
*)self
);
120 // add it to the cache
121 PyDict_SetItem(colorcache
, (PyObject
*)self
, (PyObject
*)self
);
123 return (PyObject
*)self
;
126 PyObject
*OtkColor_FromName(const char *name
, int screen
)
128 OtkColor
*self
= PyObject_New(OtkColor
, &OtkColor_Type
);
131 assert(screen
>= 0); assert(name
);
133 if (!colorcache
) colorcache
= PyDict_New();
138 self
->screen
= screen
;
140 parseColorName(self
, name
);
142 // does this color already exist in the cache?
143 cached
= PyDict_GetItem(colorcache
, (PyObject
*)self
);
149 // add it to the cache
150 PyDict_SetItem(colorcache
, (PyObject
*)self
, (PyObject
*)self
);
152 return (PyObject
*)self
;
155 void OtkColor_CleanupColorCache()
162 static void otkcolor_dealloc(OtkColor
* self
)
164 // when this is called, the color has already been cleaned out of the cache
165 PyObject_Del((PyObject
*)self
);
168 static int otkcolor_compare(OtkColor
*c1
, OtkColor
*c2
)
171 unsigned long p1
, p2
;
173 p1
= c1
->red
<< 16 | c1
->green
<< 8 | c1
->blue
;
174 p2
= c2
->red
<< 16 | c2
->green
<< 8 | c2
->blue
;
185 static PyObject
*otkcolor_repr(OtkColor
*self
)
187 return PyString_FromFormat("rgb:%x/%x/%x", self
->red
, self
->green
,
191 static long otkcolor_hash(OtkColor
*self
)
193 return self
->screen
<< 24 | self
->red
<< 16 | self
->green
<< 8 | self
->blue
;
196 PyTypeObject OtkColor_Type
= {
197 PyObject_HEAD_INIT(NULL
)
202 (destructor
)otkcolor_dealloc
, /*tp_dealloc*/
206 (cmpfunc
)otkcolor_compare
, /*tp_compare*/
207 (reprfunc
)otkcolor_repr
, /*tp_repr*/
209 0, /*tp_as_sequence*/
211 (hashfunc
)otkcolor_hash
, /*tp_hash */