Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
#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;
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)
if (ev.xcreatewindow.parent != rw)
Window w = ev.xcreatewindow.window;
pid_t pid = get_window_pid(d, w);
if (pid < 0)
warnx("CreateNotify for window %x pid %d", w, pid);

This comment has been minimized.

Copy link

@ratozumbi ratozumbi commented Feb 1, 2018

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


This comment has been minimized.

Copy link
Owner Author

@grawity 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