mirror of
https://github.com/gryf/wmdocklib.git
synced 2026-03-25 06:03:34 +01:00
Compare commits
2 Commits
baae528274
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
| bab1b793d9 | |||
| f9869d2100 |
@@ -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):
|
||||
@@ -123,14 +117,14 @@ def redraw_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."""
|
||||
(sourceX, sourceY, width, height, targetX,
|
||||
targetY) = (int(sourceX), int(sourceY), int(width), int(height),
|
||||
int(targetX), int(targetY))
|
||||
(source_x, source_y, width, height, target_x,
|
||||
target_y) = (int(source_x), int(source_y), int(width), int(height),
|
||||
int(target_x), int(target_y))
|
||||
if width > 0 or height > 0:
|
||||
pywmgeneral.copy_xpm_area(sourceX, sourceY, width, height,
|
||||
targetX, targetY)
|
||||
pywmgeneral.copy_xpm_area(source_x, source_y, width, height,
|
||||
target_x, target_y)
|
||||
|
||||
|
||||
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()
|
||||
|
||||
|
||||
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):
|
||||
for char in range(40, 126):
|
||||
char = chr(char)
|
||||
if char not in dict_to_check:
|
||||
return char
|
||||
_char = chr(char)
|
||||
if _char not in dict_to_check:
|
||||
return _char
|
||||
return None
|
||||
|
||||
|
||||
def normalize_color(color):
|
||||
if color.startswith('#'):
|
||||
return color
|
||||
else:
|
||||
return get_color_code(color)
|
||||
|
||||
return get_color_code(color)
|
||||
|
||||
|
||||
def merge_palettes(pal1, pal2, bitmap_list):
|
||||
@@ -264,10 +227,7 @@ def merge_palettes(pal1, pal2, bitmap_list):
|
||||
rerun = True
|
||||
else:
|
||||
# color not found, add new replacement
|
||||
temp = []
|
||||
for line in bitmap_list:
|
||||
temp.append(line.replace(char, new_char))
|
||||
bitmap_list = temp
|
||||
bitmap_list = [x.replace(char, new_char) for x in bitmap_list]
|
||||
elif char in pal1:
|
||||
# color not found, check if the char already exists in first
|
||||
# palette
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user