scx/c/xlib/colormap.c

89 lines
2.5 KiB
C

#include "xlib.h"
Generic_Predicate (Colormap)
Generic_Equal_Dpy (Colormap, COLORMAP, cm)
Generic_Print (Colormap, "#[colormap %lu]", COLORMAP(x)->cm)
Generic_Get_Display (Colormap, COLORMAP)
s48_value Make_Colormap (finalize, dpy, cmap) Display *dpy; Colormap cmap; {
s48_value cm;
if (cmap == None)
return Sym_None;
cm = Find_Object (T_Colormap, (GENERIC)dpy, Match_X_Obj, cmap);
if (S48_NULL_P (cm)) {
cm = Alloc_Object (sizeof (struct S_Colormap), T_Colormap, 0);
COLORMAP(cm)->tag = S48_NULL;
COLORMAP(cm)->cm = cmap;
COLORMAP(cm)->dpy = dpy;
COLORMAP(cm)->free = 0;
Register_Object (cm, (GENERIC)dpy, finalize ? P_Free_Colormap :
(PFO)0, 0);
}
return cm;
}
Colormap Get_Colormap (c) s48_value c; {
Check_Type (c, T_Colormap);
return COLORMAP(c)->cm;
}
s48_value P_Free_Colormap (c) s48_value c; {
Check_Type (c, T_Colormap);
if (!COLORMAP(c)->free)
XFreeColormap (COLORMAP(c)->dpy, COLORMAP(c)->cm);
Deregister_Object (c);
COLORMAP(c)->free = 1;
return Void;
}
static s48_value P_Alloc_Color (cmap, color) s48_value cmap, color; {
XColor c;
Colormap cm = Get_Colormap (cmap);
int r;
c = *Get_Color (color);
Disable_Interrupts;
r = XAllocColor (COLORMAP(cmap)->dpy, cm, &c);
Enable_Interrupts;
if (!r)
return S48_FALSE;
return Make_Pixel (c.pixel);
}
static s48_value P_Alloc_Named_Color (cmap, name) s48_value cmap, name; {
Colormap cm = Get_Colormap (cmap);
XColor screen, exact;
int r;
s48_value ret, t, x;
S48_DECLARE_GC_PROTECT(2);
Disable_Interrupts;
r = XAllocNamedColor (COLORMAP(cmap)->dpy, cm, Get_Strsym (name),
&screen, &exact);
Enable_Interrupts;
if (!r)
return S48_FALSE;
t = ret = P_Make_List (s48_enter_integer (3), S48_NULL);
S48_GC_PROTECT_2 (t, ret);
x = Make_Pixel (screen.pixel);
S48_CAR (t) = x; t = S48_CDR (t);
x = Make_Color (screen.red, screen.green, screen.blue);
S48_CAR (t) = x; t = S48_CDR (t);
x = Make_Color (exact.red, exact.green, exact.blue);
S48_CAR (t) = x;
S48_GC_UNPROTECT;
return ret;
}
elk_init_xlib_colormap () {
Generic_Define (Colormap, "colormap", "colormap?");
Define_Primitive (P_Colormap_Display, "colormap-display", 1, 1, EVAL);
Define_Primitive (P_Free_Colormap, "free-colormap", 1, 1, EVAL);
Define_Primitive (P_Alloc_Color, "alloc-color", 2, 2, EVAL);
Define_Primitive (P_Alloc_Named_Color,"alloc-named-color",2, 2, EVAL);
}