Skip to content

Instantly share code, notes, and snippets.

@tkrajina
Created December 13, 2013 10:20
Show Gist options
  • Save tkrajina/7942379 to your computer and use it in GitHub Desktop.
Save tkrajina/7942379 to your computer and use it in GitHub Desktop.
List of windows + their PIDs
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/Xatom.h>
#include <stdlib.h>
#include <stdio.h>
void print_window_properties(Display *display, Window *window) {
char *window_name;
int pid;
{
XTextProperty text_data;
Atom atom = XInternAtom(display, "WM_NAME", True);
int status = XGetTextProperty(display, window, &text_data, atom);
window_name = (char *)text_data.value;
}
{
XTextProperty text_data;
Atom atom = XInternAtom(display, "_NET_WM_PID", True);
int status = XGetTextProperty(display, window, &text_data, atom);
if(text_data.nitems) {
pid = (unsigned char *)text_data.value;
pid = text_data.value[1] * 256;
pid += text_data.value[0];
}
}
printf("pid=%5i wm_name=%s\n", pid, window_name);
}
void get_windows(Display *display, Window rootwin, int depth) {
Window parent;
Window *children;
Window *child;
unsigned int no_of_children;
Status status = XQueryTree(display, rootwin, &rootwin, &parent, &children, &no_of_children);
//printf("status=%i\n", status);
//printf("no_of_children=%i\n", no_of_children);
if(!status) {
//printf("Error retrieving windows\n");
return;
}
if(!no_of_children) {
//printf("No more windows\n");
return;
}
int i;
for(i = 0; i < no_of_children; i++) {
child = children[i];
print_window_properties(display, child);
//printf("depth=%i, child=%p\n", depth, child);
get_windows(display, child, depth + 1);
}
}
int main() {
Display *display;
display = XOpenDisplay(NULL);
if (display == NULL) {
fprintf(stderr, "Cannot connect to X server %s\n", "simey:0");
exit (-1);
}
int screen_num = DefaultScreen(display);
printf("screen_num %i\n", screen_num);
printf("display dimensions %i,%i\n", DisplayWidth(display, screen_num), DisplayHeight(display, screen_num));
Window rootwin = DefaultRootWindow(display);
int depth;
get_windows(display, rootwin, 0);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment