Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <X11/Xlib.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- Display *display;
- Window window;
- XEvent event;
- int screen;
- Atom type;
- int format;
- unsigned char **clientlist, *titletext;
- unsigned long nitems, bytes;
- int clientcount, activewin;
- void *getproperty (Window win, Atom at, Atom type, int *num_results) {
- Atom type_ret;
- int format = 0;
- unsigned long items = 0;
- unsigned long bytes = 0;
- unsigned char **prop_value;
- int result;
- result = XGetWindowProperty(display, win, at, 0, 0x7fffffff, False, type, &type_ret, &format, &nitems, &bytes, (unsigned char **)&prop_value);
- if (result != 0) {
- printf ("result from XGetWindowProperty not 0, %d instead!\n", result);
- }
- *num_results = nitems;
- return prop_value;
- }
- char *getwindowname(Window window) {
- char *windowname;
- int items;
- windowname = getproperty(window, XInternAtom(display, "WM_ICON_NAME", True), AnyPropertyType, &items);
- printf("getwindowname(): name for ID %u is ", window); if (windowname) { printf("\"%s\"", windowname); } else { printf("(null)"); } printf("\n");
- return (char *)(windowname ? windowname : "Unnamed Window");
- }
- int find_active_window() {
- int *activewin;
- int items;
- activewin = getproperty(RootWindow(display, 0), XInternAtom(display, "_NET_ACTIVE_WINDOW", True), AnyPropertyType, &items);
- printf("find_active_window(): active window ID is %d\n", activewin);
- return *activewin;
- }
- unsigned char **getwindowlist(int *clientcount) {
- int i;
- clientlist = getproperty(RootWindow(display, 0), XInternAtom(display, "_NET_CLIENT_LIST", True), AnyPropertyType, clientcount);
- for (i = 0; i < *clientcount; i++) { printf("getwindowlist(): window %d of %d has ID %d\n", i + 1, *clientcount, clientlist[i]); }
- return clientlist;
- }
- redraw() {
- int i;
- printf("redraw window\n");
- return;
- for (i = 0; i < clientcount; i++) {
- printf("%s\n", getwindowname((int)clientlist[i]));
- }
- }
- /*
- update on:
- - focus
- - map/unmap
- - title change
- focus/map:
- - title known
- unmap:
- - title unknown
- */
- int main(void) {
- display = XOpenDisplay(NULL);
- if (display == NULL) {
- fprintf(stderr, "Cannot open display\n");
- exit(1);
- }
- int i, knownwin = 0;
- activewin = find_active_window();
- clientlist = getwindowlist((int *)&clientcount);
- for (i = 0; i < clientcount; i++) {
- XSelectInput (display, (int)clientlist[i], StructureNotifyMask | FocusChangeMask | VisibilityChangeMask);
- printf("%s\n", getwindowname((int)clientlist[i]));
- }
- screen = DefaultScreen(display);
- window = XCreateSimpleWindow(display, RootWindow(display, screen), 10, 10, 100, 100, 1, BlackPixel(display, screen), WhitePixel(display, screen));
- XSelectInput(display, window, ExposureMask | KeyPressMask | ButtonPressMask | StructureNotifyMask);
- Atom wmDeleteMessage = XInternAtom(display, "WM_DELETE_WINDOW", False);
- XSetWMProtocols(display, window, &wmDeleteMessage, 1);
- XMapWindow(display, window);
- redraw();
- XSelectInput(display, RootWindow(display, screen), PropertyChangeMask | SubstructureNotifyMask);
- while (1) {
- XNextEvent(display, &event);
- //printf("%d\n", event);
- switch (event.type) {
- case ClientMessage:
- if (event.xclient.data.l[0] == wmDeleteMessage) {
- printf("close window\n");
- XCloseDisplay(display);
- exit(0);
- }
- break;
- case ButtonPress:
- printf("buttonpress\n");
- break;
- //case DestroyNotify:
- //printf("destroynotify\n");
- //break;
- //case Expose:
- //printf("expose\n");
- //break;
- case PropertyNotify:
- //printf("propertynotify\n");
- //printf("%s\n", XGetAtomName(display, event.xproperty.atom));
- if (event.xproperty.atom == XInternAtom (display, "_NET_CLIENT_LIST", False)) {
- printf("window created or deleted\n");
- } else if (event.xproperty.atom == XInternAtom (display, "_NET_ACTIVE_WINDOW", False)) {
- printf("active window changed\n");
- }
- break;
- case CreateNotify:
- printf("create window: %d\n", event.xcreatewindow.window);
- break;
- case FocusIn: //skip FocusOut since it'd only generate another event, and thinking about it, it's unneccessary here
- printf("focusin: window %u\n", event.xfocus.window);
- break;
- default:
- break;
- }
- }
- }
- /*
- typedef struct {
- long flags;
- int x, y;
- int width, height;
- int min_width, min_height;
- int max_width, max_height;
- int width_inc, height_inc;
- struct {
- int x;
- int y;
- } min_aspect, max_aspect;
- int base_width, base_height;
- int win_gravity;
- } XSizeHints;
- */
Add Comment
Please, Sign In to add comment