Skip to content

Instantly share code, notes, and snippets.

@grawity
Last active October 8, 2019 20:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save grawity/03ce2b1c37cff5578610555d088a7ca9 to your computer and use it in GitHub Desktop.
Save grawity/03ce2b1c37cff5578610555d088a7ca9 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);
}
}
@ratozumbi
Copy link

why do you return comm in get_parent_pid if there is no change to it?

@grawity
Copy link
Author

grawity commented May 16, 2018

@ratozumbi: That function is unused, and missing the actual "read comm string from path" code. Originally I wanted the events to show the program name as well as the PID.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment