-
Notifications
You must be signed in to change notification settings - Fork 0
/
disp.c
220 lines (184 loc) · 4.39 KB
/
disp.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
#include "clock.h"
char * display_string = 0;
/* Dispatcher for main event loop. */
typedef struct Disp Disp;
struct Disp {
int type;
void (*handler)(XEvent *);
};
static void update_display_string();
static void do_command(XEvent *);
static char * pipe_command(char *);
static void expose(XEvent *);
static void enter(XEvent *);
static void leave(XEvent *);
static void mappingnotify(XEvent *);
static void visibilitynotify(XEvent *);
static Disp disps[] = {
{ButtonRelease, do_command},
{Expose, expose},
{EnterNotify, enter},
{LeaveNotify, leave},
{MappingNotify, mappingnotify},
{VisibilityNotify, visibilitynotify},
};
extern void
dispatch(XEvent *ev) {
Disp *p;
for (p = disps; p < disps + sizeof disps / sizeof disps[0]; p++)
if (p->type == ev->type) {
if (p->handler != 0)
p->handler(ev);
return;
}
}
static void
update_display_string() {
if (display_string != 0)
free(display_string);
if (view_command != 0) {
display_string = pipe_command(view_command);
} else {
/* Fall back to computing the date & time internally. */
time_t t;
struct tm tm;
char time_string[17]; /* 1234-67-90 23:56 */
time(&t);
tm = *localtime(&t);
strftime(time_string, sizeof time_string, "%Y-%m-%d %H:%M", &tm);
display_string = sdup(time_string);
}
/* Recompute the window width. */
window_width = 4 * XTextWidth(font, display_string,
strlen(display_string)) / 3;
}
static void
expose(XEvent * ev) {
int width;
/* Only handle the last in a group of Expose events. */
if ((ev && ev->xexpose.count != 0) || (display_string == 0))
return;
/* Do the redraw. */
width = XTextWidth(font, display_string, strlen(display_string));
XDrawString(dpy, window, gc, (window_width - width)/2,
window_height / 4 + font->ascent,
display_string, strlen(display_string));
}
static void
mappingnotify(XEvent * ev) {
XRefreshKeyboardMapping((XMappingEvent *) ev);
}
static void
enter(XEvent * ev) {
update_display_string();
XMoveResizeWindow(dpy, window,
sinister ? 0 : display_width-window_width, 0,
window_width, window_height);
}
static void
leave(XEvent * ev) {
XMoveResizeWindow(dpy, window,
sinister ? 0 : display_width-2, 0, 2, 2);
}
static void
do_command(XEvent * ev) {
int button = ev->xbutton.button;
static char * sh;
if (command[button] == 0)
return;
if (sh == 0) {
sh = getenv("SHELL");
if (sh == 0) sh = "/bin/sh";
}
switch (fork()) {
case 0: /* Child. */
close(ConnectionNumber(dpy));
switch (fork()) {
case 0:
execl(sh, sh, "-c", command[button], 0);
fprintf(stderr, "%s: can't exec \"%s -c %s\"\n", argv0, sh,
command[button]);
exit(EXIT_FAILURE);
case -1:
fprintf(stderr, "%s: couldn't fork\n", argv0);
exit(EXIT_FAILURE);
default:
exit(EXIT_SUCCESS);
}
case -1: /* Error. */
fprintf(stderr, "%s: couldn't fork\n", argv0);
break;
default:
wait(0);
}
}
static char *
pipe_command(char * command) {
static char * sh;
int fds[2];
char * string;
if (sh == 0) {
sh = getenv("SHELL");
if (sh == 0) sh = "/bin/sh";
}
if (pipe(fds) == -1)
return "Execution failed";
switch (fork()) {
case 0: /* Child. */
close(ConnectionNumber(dpy));
switch (fork()) {
case 0:
close(0);
close(fds[0]);
dup2(fds[1], 1);
dup2(1, 2);
execl(sh, sh, "-c", command, 0);
fprintf(stderr, "%s: can't exec \"%s -c %s\"\n", argv0, sh,
command);
exit(EXIT_FAILURE);
case -1:
fprintf(stderr, "%s: couldn't fork\n", argv0);
exit(EXIT_FAILURE);
default:
exit(EXIT_SUCCESS);
}
case -1: /* Error. */
fprintf(stderr, "%s: couldn't fork\n", argv0);
break;
default: {
/* Read from the pipe. */
char buf[BUFSIZ];
int n;
close(fds[1]);
while ((n = read(fds[0], buf, BUFSIZ)) > 0) {
int valid = 0;
char * p = buf;
/* How many of the characters do we want? */
while (*p++ >= ' ') valid++;
string = (char *) malloc(valid + 1);
if (string != 0)
strncpy(string, buf, valid);
else
string = "Out of memory";
}
wait(0);
}
}
return string;
}
static void
visibilitynotify(XEvent * ev) {
char * name;
Window root_return, parent_return;
Window * wins;
unsigned int nwins;
if(ev->xvisibility.state == VisibilityUnobscured)
return;
/* Is it lock? */
XQueryTree(dpy, root, &root_return, &parent_return, &wins, &nwins);
XFetchName(dpy, wins[nwins-1], &name);
if(name == 0 || strcmp(name, "lock"))
XRaiseWindow(dpy, window);
XFree(wins);
XFree(name);
}