-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathxutil.c
229 lines (200 loc) · 3.87 KB
/
xutil.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include "xutil.h"
#include <X11/extensions/Xrender.h>
Display *dpy;
Window root;
Atom atoms[NATOMS];
int screen;
int
max(int x, int y)
{
return x > y ? x : y;
}
int
min(int x, int y)
{
return x < y ? x : y;
}
void *
emalloc(size_t size)
{
void *p;
if ((p = malloc(size)) == NULL)
err(1, "malloc");
return p;
}
void *
ecalloc(size_t nmemb, size_t size)
{
void *p;
if ((p = calloc(nmemb, size)) == NULL)
err(1, "calloc");
return p;
}
char *
estrndup(const char *s, size_t maxlen)
{
char *p;
if ((p = strndup(s, maxlen)) == NULL)
err(1, "strndup");
return p;
}
unsigned long
getwinsprop(Window win, Atom prop, Window **wins)
{
unsigned char *list;
unsigned long len;
unsigned long dl; /* dummy variable */
int di; /* dummy variable */
Atom da; /* dummy variable */
list = NULL;
if (XGetWindowProperty(dpy, win, prop, 0L, 1024, False, XA_WINDOW,
&da, &di, &len, &dl, &list) != Success || list == NULL) {
*wins = NULL;
return 0;
}
*wins = (Window *)list;
return len;
}
Window
getwinprop(Window win, Atom prop)
{
Window *wins;
Window ret = None;
getwinsprop(win, prop, &wins);
if (wins != NULL)
ret = *wins;
XFree(wins);
return ret;
}
unsigned long
getcardsprop(Window win, Atom prop, Atom **array)
{
unsigned char *p;
unsigned long len;
unsigned long dl;
int di;
Atom da;
p = NULL;
if (XGetWindowProperty(dpy, win, prop, 0L, 1024, False, XA_CARDINAL, &da, &di, &len, &dl, &p) != Success || p == NULL) {
*array = NULL;
XFree(p);
return 0;
}
*array = (Atom *)p;
return len;
}
unsigned long
getcardprop(Window win, Atom prop)
{
unsigned long *array;
unsigned long card = 0;
getcardsprop(win, prop, &array);
if (array != NULL)
card = *array;
XFree(array);
return card;
}
unsigned long
getatomsprop(Window win, Atom prop, Atom **atoms)
{
unsigned char *p;
unsigned long len;
unsigned long dl; /* dummy variable */
int di; /* dummy variable */
Atom da; /* dummy variable */
p = NULL;
if (XGetWindowProperty(dpy, win, prop, 0L, 1024, False, XA_ATOM, &da, &di, &len, &dl, &p) != Success || p == NULL) {
*atoms = NULL;
XFree(p);
return 0;
}
*atoms = (Atom *)p;
return len;
}
Atom
getatomprop(Window win, Atom prop)
{
Atom *atoms;
Atom atom = None;
getatomsprop(win, prop, &atoms);
if (atoms != NULL)
atom = *atoms;
XFree(atoms);
return atom;
}
char *
getwinname(Window win)
{
#define MAXLEN 512
char *name = NULL;
unsigned long length, remain;
int format;
Atom props[] = {atoms[_NET_WM_NAME], XA_WM_NAME};
Atom type;
for (size_t i = 0; i < LEN(props); i++) {
if (XGetWindowProperty(
dpy, win, atoms[_NET_WM_NAME],
0L, 1024, False,
AnyPropertyType, &type, &format,
&length, &remain, (void*)&name
) == Success && format == 8)
return name;
XFree(name);
}
return NULL;
}
void
settitle(Window win, const char *title)
{
struct {
Atom prop, type;
} props[] = {
{ atoms[_NET_WM_NAME], atoms[UTF8_STRING] },
{ atoms[_NET_WM_ICON_NAME], atoms[UTF8_STRING] },
{ XA_WM_NAME, XA_STRING },
{ XA_WM_ICON_NAME, XA_STRING },
};
size_t len, i;
len = strlen(title);
for (i = 0; i < LEN(props); i++) {
XChangeProperty(
dpy, win,
props[i].prop,
props[i].type,
8,
PropModeReplace,
(unsigned char *)title,
len
);
}
}
void
initatoms(void)
{
static char *atomnames[NATOMS] = {
#define X(atom) [atom] = #atom,
ATOMS
#undef X
};
XInternAtoms(dpy, atomnames, NATOMS, False, atoms);
}
void
xinit(void)
{
int fd;
if ((dpy = XOpenDisplay(NULL)) == NULL)
errx(1, "could not open display");
screen = DefaultScreen(dpy);
root = RootWindow(dpy, screen);
fd = XConnectionNumber(dpy);
while (fcntl(fd, F_SETFD, FD_CLOEXEC) == -1) {
if (errno == EINTR)
continue;
err(EXIT_FAILURE, "fcntl");
}
}