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 assert(!self
->allocated
);
80 // allocate color from rgb values
81 xcol
.red
= self
->red
| self
->red
<< 8;
82 xcol
.green
= self
->green
| self
->green
<< 8;
83 xcol
.blue
= self
->blue
| self
->blue
<< 8;
86 if (!XAllocColor(OBDisplay
->display
,
87 OtkDisplay_ScreenInfo(OBDisplay
, self
->screen
)->colormap
,
89 fprintf(stderr
, "OtkColor: color alloc error: rgb:%x/%x/%x\n",
90 self
->red
, self
->green
, self
->blue
);
94 self
->pixel
= xcol
.pixel
;
95 self
->allocated
= True
;
101 PyObject
*OtkColor_FromRGB(int r
, int g
, int b
, int screen
)
103 OtkColor
*self
= PyObject_New(OtkColor
, &OtkColor_Type
);
106 assert(screen
>= 0); assert(r
>= 0); assert(g
>= 0); assert(b
>= 0);
107 assert(r
<= 0xff); assert(g
<= 0xff); assert(b
<= 0xff);
109 if (!colorcache
) colorcache
= PyDict_New();
111 self
->allocated
= False
;
116 self
->screen
= screen
;
118 // does this color already exist in the cache?
119 cached
= PyDict_GetItem(colorcache
, (PyObject
*)self
);
125 // add it to the cache
126 PyDict_SetItem(colorcache
, (PyObject
*)self
, (PyObject
*)self
);
127 return (PyObject
*)self
;
130 PyObject
*OtkColor_FromName(const char *name
, int screen
)
132 OtkColor
*self
= PyObject_New(OtkColor
, &OtkColor_Type
);
135 assert(screen
>= 0); assert(name
);
137 if (!colorcache
) colorcache
= PyDict_New();
139 self
->allocated
= False
;
144 self
->screen
= screen
;
146 parseColorName(self
, name
);
148 // does this color already exist in the cache?
149 cached
= PyDict_GetItem(colorcache
, (PyObject
*)self
);
155 // add it to the cache
156 PyDict_SetItem(colorcache
, (PyObject
*)self
, (PyObject
*)self
);
157 return (PyObject
*)self
;
160 unsigned long OtkColor_Pixel(OtkColor
*self
)
162 if (!self
->allocated
)
167 void OtkColor_CleanupColorCache()
174 static void otkcolor_dealloc(OtkColor
* self
)
176 // when this is called, the color has already been cleaned out of the cache
177 PyObject_Del((PyObject
*)self
);
180 static int otkcolor_compare(OtkColor
*c1
, OtkColor
*c2
)
183 unsigned long p1
, p2
;
185 p1
= c1
->red
<< 16 | c1
->green
<< 8 | c1
->blue
;
186 p2
= c2
->red
<< 16 | c2
->green
<< 8 | c2
->blue
;
197 static PyObject
*otkcolor_repr(OtkColor
*self
)
199 return PyString_FromFormat("rgb:%x/%x/%x", self
->red
, self
->green
,
203 static long otkcolor_hash(OtkColor
*self
)
205 return self
->screen
<< 24 | self
->red
<< 16 | self
->green
<< 8 | self
->blue
;
208 PyTypeObject OtkColor_Type
= {
209 PyObject_HEAD_INIT(NULL
)
214 (destructor
)otkcolor_dealloc
, /*tp_dealloc*/
218 (cmpfunc
)otkcolor_compare
, /*tp_compare*/
219 (reprfunc
)otkcolor_repr
, /*tp_repr*/
221 0, /*tp_as_sequence*/
223 (hashfunc
)otkcolor_hash
, /*tp_hash */