make the icons program a C app.

This commit is contained in:
Dana Jansens 2003-02-11 13:17:07 +00:00
parent c735d49979
commit 0e06aab532

View file

@ -2,28 +2,29 @@
#include <X11/Xutil.h> #include <X11/Xutil.h>
#include <X11/Xatom.h> #include <X11/Xatom.h>
#include <X11/cursorfont.h> #include <X11/cursorfont.h>
#include <cstdlib> #include <stdlib.h>
#include <cstdio> #include <stdio.h>
#include <cassert> #include <assert.h>
Window findClient(Display *d, Window win) Window findClient(Display *d, Window win)
{ {
Window r, *children; Window r, *children;
unsigned int n; unsigned int n, i;
Atom state = XInternAtom(d, "WM_STATE", True);
Atom ret_type;
int ret_format;
unsigned long ret_items, ret_bytesleft;
unsigned long *prop_return;
XQueryTree(d, win, &r, &r, &children, &n); XQueryTree(d, win, &r, &r, &children, &n);
for (unsigned int i = 0; i < n; ++i) { for (i = 0; i < n; ++i) {
Window w = findClient(d, children[i]); Window w = findClient(d, children[i]);
if (w) return w; if (w) return w;
} }
// try me // try me
Atom state = XInternAtom(d, "WM_STATE", true);
Atom ret_type;
int ret_format;
unsigned long ret_items, ret_bytesleft;
unsigned long *prop_return;
XGetWindowProperty(d, win, state, 0, 1, XGetWindowProperty(d, win, state, 0, 1,
false, state, &ret_type, &ret_format, False, state, &ret_type, &ret_format,
&ret_items, &ret_bytesleft, &ret_items, &ret_bytesleft,
(unsigned char**) &prop_return); (unsigned char**) &prop_return);
if (ret_type == None || ret_items < 1) if (ret_type == None || ret_items < 1)
@ -35,7 +36,7 @@ int main(int argc, char **argv)
{ {
Display *d = XOpenDisplay(NULL); Display *d = XOpenDisplay(NULL);
int s = DefaultScreen(d); int s = DefaultScreen(d);
Atom net_wm_icon = XInternAtom(d, "_NET_WM_ICON", true); Atom net_wm_icon = XInternAtom(d, "_NET_WM_ICON", True);
Atom ret_type; Atom ret_type;
unsigned int winw = 0, winh = 0; unsigned int winw = 0, winh = 0;
int ret_format; int ret_format;
@ -43,56 +44,57 @@ int main(int argc, char **argv)
const int MAX_IMAGES = 10; const int MAX_IMAGES = 10;
unsigned long *prop_return[MAX_IMAGES]; unsigned long *prop_return[MAX_IMAGES];
XImage *i[MAX_IMAGES]; XImage *i[MAX_IMAGES];
long offset = 0;
unsigned int image = 0;
unsigned int j; // loop counter
printf("Click on a window with an icon...\n"); printf("Click on a window with an icon...\n");
//int id = strtol(argv[1], NULL, 16); //int id = strtol(argv[1], NULL, 16);
XUngrabPointer(d, CurrentTime); XUngrabPointer(d, CurrentTime);
Window id; Window id;
Cursor cur = XCreateFontCursor(d, XC_crosshair); Cursor cur = XCreateFontCursor(d, XC_crosshair);
XGrabPointer(d, RootWindow(d, s), false, ButtonPressMask, GrabModeAsync, XGrabPointer(d, RootWindow(d, s), False, ButtonPressMask, GrabModeAsync,
GrabModeAsync, None, cur, CurrentTime); GrabModeAsync, None, cur, CurrentTime);
XEvent ev; XEvent ev;
while (true) { while (1) {
XNextEvent(d, &ev); XNextEvent(d, &ev);
if (ev.type == ButtonPress) { if (ev.type == ButtonPress) {
XUngrabPointer(d, CurrentTime); XUngrabPointer(d, CurrentTime);
id = ev.xbutton.subwindow; id = findClient(d, ev.xbutton.subwindow);
id = findClient(d, id);
break; break;
} }
} }
printf("Using window 0x%lx\n", id); printf("Using window 0x%lx\n", id);
long offset = 0;
int image = 0;
do { do {
unsigned int w, h;
XGetWindowProperty(d, id, net_wm_icon, offset++, 1, XGetWindowProperty(d, id, net_wm_icon, offset++, 1,
false, XA_CARDINAL, &ret_type, &ret_format, False, XA_CARDINAL, &ret_type, &ret_format,
&ret_items, &ret_bytesleft, &ret_items, &ret_bytesleft,
(unsigned char**) &prop_return[image]); (unsigned char**) &prop_return[image]);
if (ret_type == None || ret_items < 1) { if (ret_type == None || ret_items < 1) {
printf("No icon found\n"); printf("No icon found\n");
return 1; return 1;
} }
unsigned int w = prop_return[image][0]; w = prop_return[image][0];
XFree(prop_return[image]); XFree(prop_return[image]);
XGetWindowProperty(d, id, net_wm_icon, offset++, 1, XGetWindowProperty(d, id, net_wm_icon, offset++, 1,
false, XA_CARDINAL, &ret_type, &ret_format, False, XA_CARDINAL, &ret_type, &ret_format,
&ret_items, &ret_bytesleft, &ret_items, &ret_bytesleft,
(unsigned char**) &prop_return[image]); (unsigned char**) &prop_return[image]);
if (ret_type == None || ret_items < 1) { if (ret_type == None || ret_items < 1) {
printf("Failed to get height\n"); printf("Failed to get height\n");
return 1; return 1;
} }
unsigned int h = prop_return[image][0]; h = prop_return[image][0];
XFree(prop_return[image]); XFree(prop_return[image]);
XGetWindowProperty(d, id, net_wm_icon, offset, w*h, XGetWindowProperty(d, id, net_wm_icon, offset, w*h,
false, XA_CARDINAL, &ret_type, &ret_format, False, XA_CARDINAL, &ret_type, &ret_format,
&ret_items, &ret_bytesleft, &ret_items, &ret_bytesleft,
(unsigned char**) &prop_return[image]); (unsigned char**) &prop_return[image]);
if (ret_type == None || ret_items < w*h) { if (ret_type == None || ret_items < w*h) {
@ -101,23 +103,23 @@ int main(int argc, char **argv)
} }
offset += w*h; offset += w*h;
printf("Image dimentions: %d, %d\n", w, h); printf("Found icon with size %dx%d\n", w, h);
i[image] = XCreateImage(d, DefaultVisual(d, s), DefaultDepth(d, s), i[image] = XCreateImage(d, DefaultVisual(d, s), DefaultDepth(d, s),
ZPixmap, 0, NULL, w, h, 32, 0); ZPixmap, 0, NULL, w, h, 32, 0);
assert(i[image]); assert(i[image]);
i[image]->byte_order = LSBFirst; i[image]->byte_order = LSBFirst;
i[image]->data = (char*)prop_return[image]; i[image]->data = (char*)prop_return[image];
for (unsigned int j = 0; j < w*h; j++) { for (j = 0; j < w*h; j++) {
unsigned char alpha = (unsigned char)i[image]->data[j*4+3]; unsigned char alpha = (unsigned char)i[image]->data[j*4+3];
unsigned char r = (unsigned char) i[image]->data[j*4+0]; unsigned char r = (unsigned char) i[image]->data[j*4+0];
unsigned char g = (unsigned char) i[image]->data[j*4+1]; unsigned char g = (unsigned char) i[image]->data[j*4+1];
unsigned char b = (unsigned char) i[image]->data[j*4+2]; unsigned char b = (unsigned char) i[image]->data[j*4+2];
// background color // background color
unsigned char bgr = 40; unsigned char bgr = 0;
unsigned char bgg = 0x8f; unsigned char bgg = 0;
unsigned char bgb = 40; unsigned char bgb = 0;
r = bgr + (r - bgr) * alpha / 256; r = bgr + (r - bgr) * alpha / 256;
g = bgg + (g - bgg) * alpha / 256; g = bgg + (g - bgg) * alpha / 256;
@ -133,7 +135,7 @@ int main(int argc, char **argv)
++image; ++image;
} while (ret_bytesleft > 0 && image < MAX_IMAGES); } while (ret_bytesleft > 0 && image < MAX_IMAGES);
Window win = XCreateSimpleWindow(d, RootWindow(d, s), 0, 0, winw, winh, Window win = XCreateSimpleWindow(d, RootWindow(d, s), 0, 0, winw, winh,
0, 0, 0); 0, 0, 0);
assert(win); assert(win);
@ -143,11 +145,10 @@ int main(int argc, char **argv)
XFillRectangle(d, p, DefaultGC(d, s), 0, 0, winw, winh); XFillRectangle(d, p, DefaultGC(d, s), 0, 0, winw, winh);
unsigned int x = 0; unsigned int x = 0;
for (int j = 0; j < image; ++j) { for (j = 0; j < image; ++j) {
XPutImage(d, p, DefaultGC(d, s), i[j], 0, 0, x, 0, XPutImage(d, p, DefaultGC(d, s), i[j], 0, 0, x, 0,
i[j]->width, i[j]->height); i[j]->width, i[j]->height);
x += i[j]->width; x += i[j]->width;
XFree(prop_return[j]);
XDestroyImage(i[j]); XDestroyImage(i[j]);
} }