This repository has been archived by the owner on Nov 21, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
pixelclock.c
355 lines (291 loc) · 8.15 KB
/
pixelclock.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
355
/* vim:ts=8
* $Id: pixelclock.c,v 1.8 2009/03/09 06:35:26 jcs Exp $
*
* pixelclock
* a different way of looking at time
*
* Copyright (c) 2005,2008-2009 joshua stein <[email protected]>
* Copyright (c) 2005 Federico G. Schwindt
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <err.h>
#include <getopt.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <sys/types.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
/* default clock size */
#define DEFSIZE 3
/* default position is along the right side */
#define DEFPOS 'r'
/* so our window manager knows us */
char* win_name = "pixelclock";
/* default hours to highlight (9am, noon, 5pm) */
const float defhours[3] = { 9.0, 12.0, 17.0 };
struct xinfo {
Display* dpy;
int dpy_width, dpy_height;
int screen;
Window win;
int size;
char position;
GC gc;
Colormap win_colormap;
} x;
const struct option longopts[] = {
{ "display", required_argument, NULL, 'd' },
{ "size", required_argument, NULL, 's' },
{ "left", no_argument, NULL, 'l' },
{ "right", no_argument, NULL, 'r' },
{ "top", no_argument, NULL, 't' },
{ "bottom", no_argument, NULL, 'b' },
{ NULL, 0, NULL, 0 }
};
extern char *__progname;
long getcolor(const char *);
void handler(int sig);
void init_x(const char *);
void usage(void);
int
main(int argc, char* argv[])
{
char *display = NULL, *p;
int c, i, y;
int hourtick, lastpos = -1, newpos = 0;
struct timeval tv[2];
time_t now;
struct tm *t;
float *hihours;
int nhihours;
XEvent event;
bzero(&x, sizeof(struct xinfo));
x.size = DEFSIZE;
x.position = NULL;
while ((c = getopt_long_only(argc, argv, "", longopts, NULL)) != -1) {
switch (c) {
case 'd':
display = optarg;
break;
case 'b':
case 't':
case 'l':
case 'r':
if (x.position)
errx(1, "only one of -top, -bottom, -left, "
"-right allowed");
/* NOTREACHED */
x.position = c;
break;
case 's':
x.size = strtol(optarg, &p, 10);
if (*p || x.size < 1)
errx(1, "illegal value -- %s", optarg);
/* NOTREACHED */
break;
default:
usage();
/* NOTREACHED */
}
}
if (!x.position)
x.position = DEFPOS;
argc -= optind;
argv += optind;
if (argc == 0) {
/* use default times */
nhihours = sizeof(defhours) / sizeof(defhours[0]);
if ((hihours = alloca(sizeof(defhours))) == NULL)
err(1, NULL);
for (i = 0; i < nhihours; i++)
hihours[i] = defhours[i];
} else {
/* get times from args */
nhihours = argc;
if ((hihours = alloca(nhihours * sizeof(float))) == NULL)
err(1, NULL);
for (i = 0; i < argc; ++i) {
int h, m;
char *p = argv[i];
/* parse times like 14:12 */
h = atoi(p);
if ((p = strchr(p, ':')) == NULL)
errx(1, "invalid time %s", argv[i]);
m = atoi(p + 1);
if (h > 23 || h < 0 || m > 59 || m < 0)
errx(1, "Invalid time %s", argv[i]);
hihours[i] = h + (m / 60.0);
}
}
init_x(display);
signal(SIGINT, handler);
signal(SIGTERM, handler);
/* each hour will be this many pixels away */
hourtick = ((x.position == 'b' || x.position == 't') ? x.dpy_width :
x.dpy_height) / 24;
for (;;) {
if (gettimeofday(&tv[0], NULL))
errx(1, "gettimeofday");
/* NOTREACHED */
now = tv[0].tv_sec;
if ((t = localtime(&now)) == NULL)
errx(1, "localtime");
/* NOTREACHED */
newpos = (hourtick * t->tm_hour) +
(float)(((float)t->tm_min / 60.0) * hourtick) - 3;
/* check if we just got exposed */
bzero(&event, sizeof(XEvent));
XCheckWindowEvent(x.dpy, x.win, ExposureMask, &event);
/* only redraw if our time changed enough to move the box or if
* we were just exposed */
if ((newpos != lastpos) || (event.type == Expose)) {
XClearWindow(x.dpy, x.win);
/* draw the current time */
XSetForeground(x.dpy, x.gc, getcolor("yellow"));
if (x.position == 'b' || x.position == 't')
XFillRectangle(x.dpy, x.win, x.gc,
newpos, 0, 6, x.size);
else
XFillRectangle(x.dpy, x.win, x.gc,
0, newpos, x.size, 6);
/* draw the hour ticks */
XSetForeground(x.dpy, x.gc, getcolor("blue"));
for (y = 1; y <= 23; y++)
if (x.position == 'b' || x.position == 't')
XFillRectangle(x.dpy, x.win, x.gc,
(y * hourtick), 0, 2, x.size);
else
XFillRectangle(x.dpy, x.win, x.gc,
0, (y * hourtick), x.size, 2);
/* highlight requested times */
XSetForeground(x.dpy, x.gc, getcolor("green"));
for (i = 0; i < nhihours; i++)
if (x.position == 'b' || x.position == 't')
XFillRectangle(x.dpy, x.win, x.gc,
(hihours[i] * hourtick), 0,
2, x.size);
else
XFillRectangle(x.dpy, x.win, x.gc,
0, (hihours[i] * hourtick),
x.size, 2);
lastpos = newpos;
XFlush(x.dpy);
}
sleep(1);
}
exit(1);
}
void
init_x(const char *display)
{
int rc;
int left = 0, top = 0, width = 0, height = 0;
XGCValues values;
XSetWindowAttributes attributes;
XTextProperty win_name_prop;
if (!(x.dpy = XOpenDisplay(display)))
errx(1, "unable to open display %s", XDisplayName(display));
/* NOTREACHED */
x.screen = DefaultScreen(x.dpy);
x.dpy_width = DisplayWidth(x.dpy, x.screen);
x.dpy_height = DisplayHeight(x.dpy, x.screen);
x.win_colormap = DefaultColormap(x.dpy, DefaultScreen(x.dpy));
switch (x.position) {
case 'b':
left = 0;
height = x.size;
top = x.dpy_height - height;
width = x.dpy_width;
break;
case 't':
left = 0;
top = 0;
height = x.size;
width = x.dpy_width;
break;
case 'l':
left = 0;
top = 0;
height = x.dpy_height;
width = x.size;
break;
case 'r':
width = x.size;
left = x.dpy_width - width;
top = 0;
height = x.dpy_height;
break;
}
x.win = XCreateSimpleWindow(x.dpy, RootWindow(x.dpy, x.screen),
left, top, width, height,
0,
BlackPixel(x.dpy, x.screen),
BlackPixel(x.dpy, x.screen));
if (!(rc = XStringListToTextProperty(&win_name, 1, &win_name_prop)))
errx(1, "XStringListToTextProperty");
/* NOTREACHED */
XSetWMName(x.dpy, x.win, &win_name_prop);
/* remove all window manager decorations and force our position/size */
/* XXX: apparently this is not very nice */
attributes.override_redirect = True;
XChangeWindowAttributes(x.dpy, x.win, CWOverrideRedirect, &attributes);
if (!(x.gc = XCreateGC(x.dpy, x.win, 0, &values)))
errx(1, "XCreateGC");
/* NOTREACHED */
XMapWindow(x.dpy, x.win);
/* we want to know when we're exposed */
XSelectInput(x.dpy, x.win, ExposureMask);
XFlush(x.dpy);
XSync(x.dpy, False);
}
long
getcolor(const char *color)
{
int rc;
XColor tcolor;
if (!(rc = XAllocNamedColor(x.dpy, x.win_colormap, color, &tcolor,
&tcolor)))
errx(1, "can't allocate %s", color);
return tcolor.pixel;
}
void
handler(int sig)
{
XCloseDisplay(x.dpy);
exit(0);
/* NOTREACHED */
}
void
usage(void)
{
fprintf(stderr, "usage: %s %s\n", __progname,
"[-display host:dpy] [-left|-right|-top|-bottom] [-size <pixels>] "
"[time time2 ...]");
exit(1);
}