Skip to content

Instantly share code, notes, and snippets.

@trappedinspacetime
Created October 8, 2019 20:51
Show Gist options
  • Select an option

  • Save trappedinspacetime/ccf32f46a32e6573d5c7be3af539a069 to your computer and use it in GitHub Desktop.

Select an option

Save trappedinspacetime/ccf32f46a32e6573d5c7be3af539a069 to your computer and use it in GitHub Desktop.
#define _GNU_SOURCE
#include <X11/Xlib.h>
#include <X11/Xatom.h>
#include <err.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int get_prop_card32(Display *d, Window w, Atom p) {
Atom actual_type;
int actual_format;
unsigned long nitems, bytes;
unsigned char *prop;
int status, value;
status = XGetWindowProperty(d, w, p,
/* long_offset */ 0L,
/* long_length */ 1024L,
/* delete */ False,
/* req_type */ XA_CARDINAL,
/* actual_type_return */ &actual_type,
/* actual_format_return */ &actual_format,
/* nitems_return */ &nitems,
/* bytes_after_return */ &bytes,
/* prop_return */ &prop);
if (status != 0)
return -1;
if (nitems < 1)
return -1;
value = prop[0]
| prop[1] << 8
| prop[2] << 16
| prop[3] << 24;
XFree(prop);
return value;
}
pid_t get_window_pid(Display *d, Window w) {
/* we could use XResQueryClientIds but this usually works fine */
static Atom am_wm_pid;
if (!am_wm_pid)
am_wm_pid = XInternAtom(d, "_NET_WM_PID", False);
return get_prop_card32(d, w, am_wm_pid);
}
int main(void) {
Display *d;
int s;
Window rw;
XSetWindowAttributes at;
Atom am_wm_pid;
XEvent ev;
d = XOpenDisplay(NULL);
if (!d)
errx(1, "cannot open display");
s = DefaultScreen(d);
rw = RootWindow(d, s);
XSelectInput(d, rw, SubstructureNotifyMask);
for (;;) {
XNextEvent(d, &ev);
if (ev.type != CreateNotify)
continue;
if (ev.xcreatewindow.parent != rw)
continue;
Window w = ev.xcreatewindow.window;
pid_t pid = get_window_pid(d, w);
if (pid < 0)
continue;
warnx("CreateNotify for window %x pid %d", w, pid);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment