diff --git a/wmdocklib/helpers.py b/wmdocklib/helpers.py index 4730990..37348b0 100644 --- a/wmdocklib/helpers.py +++ b/wmdocklib/helpers.py @@ -26,7 +26,6 @@ Some changes to handle the additional event handling in pywmgeneral First workingish version """ -import os import re from wmdocklib import pywmgeneral @@ -36,11 +35,6 @@ charset_start = None charset_width = None MARKER = 'xxxxxxxxxxx' -RGB_FILE_LIST = ['/etc/X11/rgb.txt', - '/usr/lib/X11/rgb.txt', - '/usr/share/X11/rgb.txt', - '/usr/X11R6/lib/X11/rgb.txt', - '/usr/lib/X11/rgb.txt'] def get_font_char_size(font_name): @@ -163,44 +157,12 @@ def get_event(): return pywmgeneral.check_for_events() -def get_color_code(color_name, rgb_fname=None): - """Convert a color to rgb code usable in an xpm. - - We use the file rgb_fname for looking up the colors. Return None if we - find no match. The rgb_fname should be like the one found in - /usr/lib/X11R6/rgb.txt on most systems. - """ - if color_name.startswith('#'): - return color_name - - if rgb_fname is None: - for fn in RGB_FILE_LIST: - if os.access(fn, os.R_OK): - rgb_fname = fn - break - - if rgb_fname is None: - raise ValueError('Cannot find RGB file') - - with open(rgb_fname, 'r') as fobj: - lines = fobj.readlines() - - for line in lines: - if line[0] != '!': - words = line.split() - if len(words) > 3: - name = ' '.join(words[3:]) - if color_name.lower() == name.lower(): - # Found the right color, get it's code - try: - r = int(words[0]) - g = int(words[1]) - b = int(words[2]) - except ValueError: - continue - - return f'#{r:02x}{g:02x}{b:02x}' - return None +def get_color_code(color_name): + """Convert a color to rgb code usable in an xpm.""" + color = pywmgeneral.get_color(color_name) + if color < 0: + return None + return f"#{color:06x}" def get_unique_key(dict_to_check): diff --git a/wmdocklib/pywmgeneral.c b/wmdocklib/pywmgeneral.c index bb75a9b..b3ca80b 100644 --- a/wmdocklib/pywmgeneral.c +++ b/wmdocklib/pywmgeneral.c @@ -61,6 +61,7 @@ GC NormalGC; XpmIcon wmgen; Pixmap pixmask; Atom deleteAtom; /* Added 2003-06-24 for graceful shutdown. */ +Display *display = NULL; /*****************************************************************************/ /* The Python stuff */ @@ -245,6 +246,14 @@ pywmgeneral_checkForEvents(PyObject *self, PyObject *args) { return Py_None; } +static PyObject * +pywmgeneral_getColor(PyObject *self, PyObject *args) { + char *color_name; + if (!PyArg_ParseTuple(args, "s", &color_name)) + return NULL; + return PyLong_FromLong((long) GetColor(color_name)); +} + static PyMethodDef PyWmgeneralMethods[] = { {"open_xwindow", pywmgeneral_openXwindow, METH_VARARGS, "Open the X window containing everything."}, @@ -262,6 +271,8 @@ static PyMethodDef PyWmgeneralMethods[] = { "Copy an area of the global XPM."}, {"check_for_events", pywmgeneral_checkForEvents, METH_VARARGS, "Check for some Xevents"}, + {"get_color", pywmgeneral_getColor, METH_VARARGS, + "Get X color code for the provided name."}, {NULL, NULL, 0, NULL} }; @@ -446,7 +457,7 @@ MOUSE_REGION mouse_region[MAX_MOUSE_REGION]; /***********************/ static void GetXPM(XpmIcon *, char **); -static Pixel GetColor(char *); +unsigned long GetColor(char *); void RedrawWindow(void); void AddMouseRegion(int, int, int, int, int); int CheckMouseRegion(int, int); @@ -479,18 +490,25 @@ static void GetXPM(XpmIcon *wmgen, char *pixmap_bytes[]) { |* GetColor *| \*******************************************************************************/ -static Pixel GetColor(char *name) { +unsigned long GetColor(char *name) { - XColor color; - XWindowAttributes attributes; + XColor color; + /* Open default display, if it is not opened already. This is needed for + * using this function before openXwindow is called, when display is + * initialized. This might select wrong X session, or segfault on not + * having DISPLAY env variable around, but meh. In context of this + * function this is best effort */ + if (!display) + display = XOpenDisplay(NULL); + screen = DefaultScreen(display); + Colormap colormap = DefaultColormap(display, screen); - XGetWindowAttributes(display, Root, &attributes); - - color.pixel = 0; - if (!XParseColor(display, attributes.colormap, name, &color)) { + if (!XParseColor(display, colormap, name, &color)) { fprintf(stderr, "wm.app: can't parse %s.\n", name); - } else if (!XAllocColor(display, attributes.colormap, &color)) { + return -1; + } else if (!XAllocColor(display, colormap, &color)) { fprintf(stderr, "wm.app: can't allocate %s.\n", name); + return -2; } return color.pixel; } diff --git a/wmdocklib/pywmgeneral.h b/wmdocklib/pywmgeneral.h index f532437..95c26c6 100644 --- a/wmdocklib/pywmgeneral.h +++ b/wmdocklib/pywmgeneral.h @@ -53,6 +53,7 @@ Display *display; void AddMouseRegion(int index, int left, int top, int right, int bottom); int CheckMouseRegion(int x, int y); +unsigned long GetColor(char *); void openXwindow(int argc, char *argv[], char **, char *, int, int); void RedrawWindow(void);