-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathumess.c
354 lines (313 loc) · 7.53 KB
/
umess.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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <getopt.h>
#include <poll.h>
#include <signal.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/Xft/Xft.h>
#include <X11/extensions/Xrandr.h>
#define OFFSET 2
#define MARGIN .5
#define ISIN(x,y,x0,y0,w,h) x>=x0 && x<=x0+w && y>=y0 && y<=y0+h
/* Micromess specific variables */
char *cmd;
int done;
int die_on_click;
int loc_hor, loc_ver;
char *mon_name;
int mx, my, mw, mh;
/* General variables */
char *appname = "umess";
int screen;
Display *dpy;
Window root, win;
Visual *vis;
Colormap cmap;
XftDraw *draw;
XftColor color;
XftFont *font;
void usage(FILE *output);
void finisher(int signal);
int init_x();
int get_monitor();
int init_xft(const char *fontdescr, const char *colordescr);
void spawn_window(const char *colordescr);
void redraw_window(const char *text, const int text_size);
void event_loop();
void
usage(FILE *output)
{
fprintf(output, "usage: %s [-{b|t}{l|r}|d|h] [-f font] "
"[-F fgcolor] [-B bgcolor] [-m monitor]\n", cmd);
}
void
finisher(int signal)
{
done = 1;
return;
}
int
init_x()
{
if (!(dpy = XOpenDisplay(NULL)))
{
fprintf(stderr, "Failed to start X session.\n");
return 1;
}
screen = DefaultScreen(dpy);
root = RootWindow(dpy, screen);
vis = XDefaultVisual(dpy, screen);
cmap = XDefaultColormap(dpy, screen);
return 0;
}
int
get_monitor()
{
XRRMonitorInfo *mons;
int nmons;
Window winr, rootr;
int rx, ry, wx, wy;
unsigned int mask;
int i;
int match;
int name_not_given;
int ind = -1;
/* Get monitors data */
mons = XRRGetMonitors(dpy, root, 1, &nmons);
if (nmons == -1)
{
fprintf(stderr, "Failed to detect monitors.\n");
return 1;
}
/* Get monitor by name or by mouse pointer */
XQueryPointer(dpy, root, &rootr, &winr, &rx, &ry, &wx, &wy, &mask);
name_not_given = !strcmp(mon_name, "");
for (i = 0; i < nmons; i++)
{
if (name_not_given)
match = ISIN(rx, ry, mons[i].x, mons[i].y, mons[i].width, mons[i].height);
else
match = !strcmp(XGetAtomName(dpy, mons[i].name), mon_name);
if (match)
{
ind = i;
break;
}
}
/* Write monitor position and dimensions */
mx = mons[ind].x;
my = mons[ind].y;
mw = mons[ind].width;
mh = mons[ind].height;
return 0;
}
int
init_xft(const char *fontdescr, const char *colordescr)
{
font = XftFontOpenName(dpy, screen, fontdescr);
if (!font)
{
fprintf(stderr, "Failed to loar font.\n");
return 1;
}
if (!XftColorAllocName(dpy, vis, cmap, colordescr, &color))
{
fprintf(stderr, "Failed to allocate color.\n");
return 1;
}
draw = XftDrawCreate(dpy, win, vis, cmap);
return 0;
}
void
spawn_window(const char *colordescr)
{
XColor wincolor;
XSetWindowAttributes swa;
XClassHint *class_hint;
swa.override_redirect = True; // unmanageable from wm
swa.background_pixel = XWhitePixel(dpy, screen);
win = XCreateWindow(dpy, root,
0, 0, 1, 1, 1, // geometry now doesn't matter
CopyFromParent, CopyFromParent, CopyFromParent,
CWOverrideRedirect | CWBackPixel, &swa);
XParseColor(dpy, cmap, colordescr, &wincolor);
XAllocColor(dpy, cmap, &wincolor);
XSetWindowBackground(dpy, win, wincolor.pixel);
/* Set WM_NAME property, name and class hints */
XStoreName(dpy, win, appname);
class_hint = XAllocClassHint();
if (class_hint) {
class_hint->res_name = appname;
class_hint->res_class = appname;
}
XSetClassHint(dpy, win, class_hint);
XFree(class_hint);
XSelectInput(dpy, win, ExposureMask|ButtonPressMask);
XMapRaised(dpy, win);
XFlush(dpy);
}
void
redraw_window(const char *text, const int text_size)
{
XGlyphInfo text_info;
int width, height, px, py;
int xmargin, ymargin;
xmargin = MARGIN * font->max_advance_width;
ymargin = MARGIN * font->height;
XftTextExtentsUtf8(dpy, font, (const FcChar8 *)text, text_size, &text_info);
width = text_info.width + 2 * xmargin;
height = text_info.height + 2 * ymargin;
px = mx + .5 * loc_hor * (mw - width) + OFFSET * (1 - loc_hor);
py = my + .5 * loc_ver * (mh - height) + OFFSET * (1 - loc_ver);
XMoveWindow(dpy, win, px, py);
XResizeWindow(dpy, win, width, height);
XftDrawStringUtf8(draw, &color, font, xmargin, height - ymargin,
(const FcChar8 *)text, text_size);
XFlush(dpy);
}
void
event_loop()
{
int redraw = 0;
ssize_t length = 0;
char input [1024] = {0,};
XEvent ev;
/* Prepare file descriptors for two types of events */
nfds_t nfds = 2;
struct pollfd fds[] = {
{.fd = STDIN_FILENO, .events = POLLIN},
{.fd = ConnectionNumber(dpy), .events = POLLIN},
};
/* Start event loop */
while (!done)
{
if (poll(fds, nfds, -1) <= 0) continue; // event catcher
/* Input event */
if (fds[0].revents & POLLHUP) // eof!
done = 1;
else if (fds[0].revents & POLLIN) // handle input
{
length = read(STDIN_FILENO, input, sizeof input);
if (length < 0) // this means something bad
{
fprintf(stderr, "Error while reading input data\n");
break;
}
else if (length == 0) // eof!
done = 1;
/* Display only the first line of the input string */
char *eol = memchr(input, '\n', length);
if (eol)
{
*eol = '\0';
length = (eol - input);
}
redraw = 1;
}
/* X11 event */
while (fds[1].revents & POLLIN)
{
XNextEvent(dpy, &ev);
if (ev.type == Expose && ev.xexpose.count == 0)
redraw = 1;
if (!die_on_click)
break;
if (ev.type == ButtonPress && ev.xbutton.button == Button1)
{
done = 1;
break;
}
}
if (done) break;
/* Update popup */
if (redraw) {
redraw_window(input, length);
redraw = 0;
}
}
}
int
main(int argc, char **argv)
{
struct sigaction sa = {0};
char ch; // arg char
done = 0; // loop natural stopper
char *fontname;
char *fghex, *bghex;
/* Parachute */
memset(&sa, 0, sizeof(struct sigaction));
sa.sa_handler = finisher;
if (sigaction(SIGINT, &sa, NULL) || sigaction(SIGTERM, &sa, NULL))
{
fprintf(stderr, "Failed to set interuption handler.\n");
return 1;
}
/* Parse args */
cmd = argv[0];
loc_hor = 1; // left: 0, center: 1, right: 2
loc_ver = 1; // top: 0, center: 1, bottom: 2
mon_name = "";
die_on_click = 0;
fontname = "monospace";
fghex = "#000000";
bghex = "#ffffff";
while ((ch = getopt(argc, argv, "tblrf:F:B:m:dh")) != -1)
{
switch (ch)
{
case 'h':
usage(stdout);
return 0;
case 'd':
die_on_click = 1;
break;
case 'l':
loc_hor = 0;
break;
case 'r':
loc_hor = 2;
break;
case 't':
loc_ver = 0;
break;
case 'b':
loc_ver = 2;
break;
case 'f':
fontname = optarg;
break;
case 'F':
fghex = optarg;
break;
case 'B':
bghex = optarg;
break;
case 'm':
mon_name = optarg;
break;
default:
usage(stderr);
return 1;
}
}
/* Prepare X, window, Xft */
if (init_x())
goto nowin;
if (get_monitor())
goto nowin;
spawn_window(bghex);
if (init_xft(fontname, fghex))
goto nodraw;
/* Handle events */
event_loop();
/* Clean */
XftDrawDestroy(draw);
XftColorFree(dpy, vis, cmap, &color);
nodraw:
XDestroyWindow(dpy, win);
nowin:
XCloseDisplay(dpy);
return !done;
}