1
0
mirror of https://github.com/gryf/wmdocklib.git synced 2026-03-26 06:33:33 +01:00

Compare commits

...

2 Commits

Author SHA1 Message Date
bab1b793d9 Fixed couple minor findings. 2025-06-08 18:47:48 +02:00
f9869d2100 Use XParseColor function form xlib rather than rgb.txt file.
This way it's possible to expose GetColor as pywmgeneral function.
2025-06-08 17:42:34 +02:00
3 changed files with 47 additions and 68 deletions

View File

@@ -26,7 +26,6 @@ Some changes to handle the additional event handling in pywmgeneral
First workingish version First workingish version
""" """
import os
import re import re
from wmdocklib import pywmgeneral from wmdocklib import pywmgeneral
@@ -36,11 +35,6 @@ charset_start = None
charset_width = None charset_width = None
MARKER = 'xxxxxxxxxxx' 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): def get_font_char_size(font_name):
@@ -123,14 +117,14 @@ def redraw_xy(x, y):
pywmgeneral.redraw_window_xy(x, y) pywmgeneral.redraw_window_xy(x, y)
def copy_xpm_area(sourceX, sourceY, width, height, targetX, targetY): def copy_xpm_area(source_x, source_y, width, height, target_x, target_y):
"""Copy an area of the global XPM.""" """Copy an area of the global XPM."""
(sourceX, sourceY, width, height, targetX, (source_x, source_y, width, height, target_x,
targetY) = (int(sourceX), int(sourceY), int(width), int(height), target_y) = (int(source_x), int(source_y), int(width), int(height),
int(targetX), int(targetY)) int(target_x), int(target_y))
if width > 0 or height > 0: if width > 0 or height > 0:
pywmgeneral.copy_xpm_area(sourceX, sourceY, width, height, pywmgeneral.copy_xpm_area(source_x, source_y, width, height,
targetX, targetY) target_x, target_y)
def add_mouse_region(index, left, top, right=None, bottom=None, width=None, def add_mouse_region(index, left, top, right=None, bottom=None, width=None,
@@ -163,58 +157,27 @@ def get_event():
return pywmgeneral.check_for_events() return pywmgeneral.check_for_events()
def get_color_code(color_name, rgb_fname=None): def get_color_code(color_name):
"""Convert a color to rgb code usable in an xpm. """Convert a color to rgb code usable in an xpm."""
color = pywmgeneral.get_color(color_name)
We use the file rgb_fname for looking up the colors. Return None if we if color < 0:
find no match. The rgb_fname should be like the one found in return None
/usr/lib/X11R6/rgb.txt on most systems. return f"#{color:06x}"
"""
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_unique_key(dict_to_check): def get_unique_key(dict_to_check):
for char in range(40, 126): for char in range(40, 126):
char = chr(char) _char = chr(char)
if char not in dict_to_check: if _char not in dict_to_check:
return char return _char
return None
def normalize_color(color): def normalize_color(color):
if color.startswith('#'): if color.startswith('#'):
return color return color
else:
return get_color_code(color) return get_color_code(color)
def merge_palettes(pal1, pal2, bitmap_list): def merge_palettes(pal1, pal2, bitmap_list):
@@ -264,10 +227,7 @@ def merge_palettes(pal1, pal2, bitmap_list):
rerun = True rerun = True
else: else:
# color not found, add new replacement # color not found, add new replacement
temp = [] bitmap_list = [x.replace(char, new_char) for x in bitmap_list]
for line in bitmap_list:
temp.append(line.replace(char, new_char))
bitmap_list = temp
elif char in pal1: elif char in pal1:
# color not found, check if the char already exists in first # color not found, check if the char already exists in first
# palette # palette

View File

@@ -61,6 +61,7 @@ GC NormalGC;
XpmIcon wmgen; XpmIcon wmgen;
Pixmap pixmask; Pixmap pixmask;
Atom deleteAtom; /* Added 2003-06-24 for graceful shutdown. */ Atom deleteAtom; /* Added 2003-06-24 for graceful shutdown. */
Display *display = NULL;
/*****************************************************************************/ /*****************************************************************************/
/* The Python stuff */ /* The Python stuff */
@@ -245,6 +246,14 @@ pywmgeneral_checkForEvents(PyObject *self, PyObject *args) {
return Py_None; 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[] = { static PyMethodDef PyWmgeneralMethods[] = {
{"open_xwindow", pywmgeneral_openXwindow, METH_VARARGS, {"open_xwindow", pywmgeneral_openXwindow, METH_VARARGS,
"Open the X window containing everything."}, "Open the X window containing everything."},
@@ -262,6 +271,8 @@ static PyMethodDef PyWmgeneralMethods[] = {
"Copy an area of the global XPM."}, "Copy an area of the global XPM."},
{"check_for_events", pywmgeneral_checkForEvents, METH_VARARGS, {"check_for_events", pywmgeneral_checkForEvents, METH_VARARGS,
"Check for some Xevents"}, "Check for some Xevents"},
{"get_color", pywmgeneral_getColor, METH_VARARGS,
"Get X color code for the provided name."},
{NULL, NULL, 0, NULL} {NULL, NULL, 0, NULL}
}; };
@@ -446,7 +457,7 @@ MOUSE_REGION mouse_region[MAX_MOUSE_REGION];
/***********************/ /***********************/
static void GetXPM(XpmIcon *, char **); static void GetXPM(XpmIcon *, char **);
static Pixel GetColor(char *); unsigned long GetColor(char *);
void RedrawWindow(void); void RedrawWindow(void);
void AddMouseRegion(int, int, int, int, int); void AddMouseRegion(int, int, int, int, int);
int CheckMouseRegion(int, int); int CheckMouseRegion(int, int);
@@ -479,18 +490,25 @@ static void GetXPM(XpmIcon *wmgen, char *pixmap_bytes[]) {
|* GetColor *| |* GetColor *|
\*******************************************************************************/ \*******************************************************************************/
static Pixel GetColor(char *name) { unsigned long GetColor(char *name) {
XColor color; XColor color;
XWindowAttributes attributes; /* 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); if (!XParseColor(display, colormap, name, &color)) {
color.pixel = 0;
if (!XParseColor(display, attributes.colormap, name, &color)) {
fprintf(stderr, "wm.app: can't parse %s.\n", name); 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); fprintf(stderr, "wm.app: can't allocate %s.\n", name);
return -2;
} }
return color.pixel; return color.pixel;
} }

View File

@@ -53,6 +53,7 @@ Display *display;
void AddMouseRegion(int index, int left, int top, int right, int bottom); void AddMouseRegion(int index, int left, int top, int right, int bottom);
int CheckMouseRegion(int x, int y); int CheckMouseRegion(int x, int y);
unsigned long GetColor(char *);
void openXwindow(int argc, char *argv[], char **, char *, int, int); void openXwindow(int argc, char *argv[], char **, char *, int, int);
void RedrawWindow(void); void RedrawWindow(void);