-
Notifications
You must be signed in to change notification settings - Fork 33
/
atomx.c
145 lines (112 loc) · 2.43 KB
/
atomx.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/* See LICENSE file for copyright and license details. */
#include <err.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <xcb/xcb.h>
#include <xcb/xcb_aux.h>
#include "util.h"
#include "arg.h"
#define MAXLEN 512
static xcb_connection_t *conn;
void
usage(char *name)
{
fprintf(stderr, "%s [-d] atom[=value] wid\n", name);
}
xcb_atom_t
add_atom(xcb_atom_t type, char *name, size_t len)
{
xcb_atom_t atom;
xcb_intern_atom_cookie_t c;
xcb_intern_atom_reply_t *r;
c = xcb_intern_atom(conn, 0, len, name);
r = xcb_intern_atom_reply(conn, c, NULL);
if (!r)
return 0;
atom = r->atom;
free(r);
return atom;
}
int
set_atom(xcb_window_t wid, xcb_atom_t atom, xcb_atom_t type, size_t len, void *data)
{
int errcode;
xcb_void_cookie_t c;
xcb_generic_error_t *e;
c = xcb_change_property_checked(conn, XCB_PROP_MODE_REPLACE,
wid, atom, type, 8, len, data);
e = xcb_request_check(conn, c);
if (!e)
return 0;
errcode = e->error_code;
free(e);
return errcode;
}
int
get_atom(xcb_window_t wid, xcb_atom_t atom, char *data, xcb_atom_t *type)
{
size_t n;
xcb_get_property_cookie_t c;
xcb_get_property_reply_t *r;
c = xcb_get_property(conn, 0, wid, atom, XCB_ATOM_ANY, 0, MAXLEN);
r = xcb_get_property_reply(conn, c, NULL);
if (!r)
return -1;
if (!(n = xcb_get_property_value_length(r))) {
free(r);
return -1;
}
strncpy(data, xcb_get_property_value(r), n);
data[n] = 0;
*type = r->type;
free(r);
return 0;
}
int
main(int argc, char **argv)
{
int i, dflag = 0;
char *key, *val, *argv0;
char data[MAXLEN];
xcb_window_t wid;
xcb_atom_t atom;
ARGBEGIN {
case 'd':
dflag = 1;
break;
default:
usage(argv0);
return -1;
} ARGEND;
if (argc < 1)
return -1;
key = strtok(argv[0], "=");
val = strtok(NULL, "=");
init_xcb(&conn);
for (i = 0; i < argc - 1; i++) {
wid = strtoul(argv[i+1], NULL, 16);
/* retrieve atom ID from server */
atom = add_atom(XCB_ATOM_STRING, key, strlen(key));
if (!atom)
return -1;
/* set property on window (must be a string) */
if (val)
set_atom(wid, atom, XCB_ATOM_STRING, strlen(val), val);
/* remove property from window */
if (dflag)
xcb_delete_property(conn, wid, atom);
/* retrieve and print atom value to stdout */
xcb_atom_t type;
if (!get_atom(wid, atom, data, &type))
switch (type) {
case XCB_ATOM_INTEGER:
printf("%d\n", *data);
break;
default:
printf("%s\n", data);
}
}
kill_xcb(&conn);
return 0;
}