-
Notifications
You must be signed in to change notification settings - Fork 13
/
get-current-window.c
64 lines (52 loc) · 1.75 KB
/
get-current-window.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include <X11/Xlib.h>
#include <X11/Xatom.h>
#include <stdio.h>
#include <stdlib.h>
#define MAXSTR 1000
Display *display;
unsigned long window;
unsigned char *prop;
void check_status(int status, unsigned long window)
{
if (status == BadWindow) {
printf("window id # 0x%lx does not exists!", window);
exit(1);
}
if (status != Success) {
printf("XGetWindowProperty failed!");
exit(2);
}
}
unsigned char* get_string_property(char* property_name)
{
Atom actual_type, filter_atom;
int actual_format, status;
unsigned long nitems, bytes_after;
filter_atom = XInternAtom(display, property_name, True);
status = XGetWindowProperty(display, window, filter_atom, 0, MAXSTR, False, AnyPropertyType,
&actual_type, &actual_format, &nitems, &bytes_after, &prop);
check_status(status, window);
return prop;
}
unsigned long get_long_property(char* property_name)
{
get_string_property(property_name);
unsigned long long_property = prop[0] + (prop[1]<<8) + (prop[2]<<16) + (prop[3]<<24);
return long_property;
}
int main(int argc, char** argv)
{
char *display_name = NULL; // could be the value of $DISPLAY
display = XOpenDisplay(display_name);
if (display == NULL) {
fprintf (stderr, "%s: unable to open display '%s'\n", argv[0], XDisplayName (display_name));
}
int screen = XDefaultScreen(display);
window = RootWindow(display, screen);
window = get_long_property("_NET_ACTIVE_WINDOW");
printf("_NET_WM_PID: %lu\n", get_long_property("_NET_WM_PID"));
printf("WM_CLASS: %s\n", get_string_property("WM_CLASS"));
printf("_NET_WM_NAME: %s\n", get_string_property("_NET_WM_NAME"));
XCloseDisplay(display);
return 0;
}