-
Notifications
You must be signed in to change notification settings - Fork 0
/
dcal.c
408 lines (356 loc) · 11.4 KB
/
dcal.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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
/* See LICENSE file for copyright and license details. */
#include <ctype.h>
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <unistd.h>
#include <X11/Xlib.h>
#include <X11/Xatom.h>
#include <X11/Xutil.h>
#ifdef XINERAMA
#include <X11/extensions/Xinerama.h>
#endif
#include "draw.h"
#define HILIGHT_MAX 32
#define INTERSECT(x,y,w,h,r) (MAX(0, MIN((x)+(w),(r).x_org+(r).width) - MAX((x),(r).x_org)) \
* MAX(0, MIN((y)+(h),(r).y_org+(r).height) - MAX((y),(r).y_org)))
#define MIN(a,b) ((a) < (b) ? (a) : (b))
#define MAX(a,b) ((a) > (b) ? (a) : (b))
static void adddays(time_t *d, int count);
static void addmonths(time_t *d, int count);
static void drawcal(void);
static void grabkeyboard(void);
static void keypress(XKeyEvent *ev);
static void run(void);
static void setup(void);
static void usage(void);
static unsigned long* pickcolor(const char *date, const char *current);
static int ch, cw; /* Calendar height and width */
static time_t current; /* Currently displayed time */
static const char *font = NULL; /* font */
static const char *bgcolor = "#cccccc"; /* colors */
static const char *bdcolor = "#000000";
static const char *curfgcolor = "#000000";
static const char *othfgcolor = "#ffffff";
static const char *todfgcolor = "#ff0000";
static const char *hltfgcolor = "#0000ff";
static unsigned long curcol[ColLast];
static unsigned long othcol[ColLast];
static unsigned long todcol[ColLast];
static unsigned long hltcol[ColLast];
static unsigned long invcol[ColLast];
static Atom utf8;
static Bool topbar = True; /* Draw on top */
static Bool leftbar = False; /* Draw on left */
static Bool keyboard = True; /* Use keyboard controls */
static int offset_x = 0; /* x offset */
static int offset_y = 0; /* y offset */
static DC *dc; /* Drawing context */
static Window win; /* Window */
static XIC xic;
static char today[9]; /* Current date when run */
static char hilight[HILIGHT_MAX][9]; /* Date for hilighting */
static int hilight_count = 0; /* Number of dates to hilight */
int main(int argc, char *argv[]) {
int i;
for(i = 1; i < argc; i++) {
/* these options take no arguments */
if(!strcmp(argv[i], "-v")) { /* prints version information */
puts("dcal-"VERSION", © 2012 Alan Berndt, see LICENSE for details");
exit(EXIT_SUCCESS);
}
else if(!strcmp(argv[i], "-h")) { /* prints usage information */
usage();
exit(EXIT_SUCCESS);
}
else if(!strcmp(argv[i], "-b")) /* appears at the bottom of the screen */
topbar = False;
else if(!strcmp(argv[i], "-l")) /* appears at the left of the screen */
leftbar = True;
else if(!strcmp(argv[i], "-k")) /* suppress keyboard controls */
keyboard = False;
/* these options take one argument */
else if(!strcmp(argv[i], "-fn")) /* font or font set */
font = argv[++i];
else if(!strcmp(argv[i], "-bg")) /* background color */
bgcolor = argv[++i];
else if(!strcmp(argv[i], "-bd")) /* border color */
bdcolor = argv[++i];
else if(!strcmp(argv[i], "-cf")) /* current month foreground color */
curfgcolor = argv[++i];
else if(!strcmp(argv[i], "-of")) /* other month foreground color */
othfgcolor = argv[++i];
else if(!strcmp(argv[i], "-tf")) /* today foreground color */
todfgcolor = argv[++i];
else if(!strcmp(argv[i], "-hf")) /* hilight foreground color */
hltfgcolor = argv[++i];
else if(!strcmp(argv[i], "-x")) /* x offset */
offset_x = atoi(argv[++i]);
else if(!strcmp(argv[i], "-y")) /* y offset */
offset_y = atoi(argv[++i]);
/* the rest are dates to hilight */
else if(hilight_count < HILIGHT_MAX)
strncpy(hilight[hilight_count++], argv[i], 9);
}
dc = initdc();
initfont(dc, font);
if (keyboard) grabkeyboard();
setup();
run();
return EXIT_FAILURE; /* unreachable */
}
void adddays(time_t *d, int count) {
struct tm *t;
t = localtime(d);
t->tm_mday += count;
*d = mktime(t);
}
void addmonths(time_t *d, int count) {
struct tm *t;
t = localtime(d);
t->tm_mon += count;
*d = mktime(t);
}
void drawcal(void) {
time_t day, end;
struct tm *ti;
unsigned long *color;
char text[20];
char curdate[10];
/* set up drawing context */
dc->x = 0;
dc->y = 0;
dc->h = ch;
dc->w = cw;
/* clear background and draw border */
drawrect(dc, 0, 0, cw, ch, True, BG(dc, curcol));
/* draw header */
ti = localtime(¤t);
strftime(text, 20, "%B %Y", ti);
dc->x = cw / 2 - textw(dc, text) / 2;
dc->y = 1;
drawtext(dc, text, curcol);
/* save selected date */
strftime(curdate, 10, "%Y%m%d", ti);
/* find first of month */
ti->tm_mday = 1;
mktime(ti);
/* find first day of first week */
ti->tm_mday -= ti->tm_wday;
day = mktime(ti);
fprintf(stderr, "Drawing from %s", asctime(ti));
ti->tm_mday += 42;
end = mktime(ti);
fprintf(stderr, "Drawing thru %s", asctime(ti));
/* draw the calendar */
dc->y = 9;
while (day < end) {
ti = localtime(&day);
/* new line on sunday */
if (ti->tm_wday == 0) {
dc->x = 1;
dc->y += dc->font.height + 2;
}
/* pick color */
strftime(text, 20, "%Y%m%d", ti);
color = pickcolor(text, curdate);
/* write the date */
strftime(text, 20, "%d", ti);
drawtext(dc, text, color);
/* increment */
dc->x += 3 * (dc->font.width + 2);
adddays(&day, 1);
}
/* draw border */
dc->x = 0;
dc->y = 0;
drawrect(dc, 0, 0, cw, ch, False, getcolor(dc, bdcolor));
mapdc(dc, win, cw, ch);
}
void grabkeyboard(void) {
int i;
/* try to grab keyboard, we may have to wait for another process to ungrab */
for(i = 0; i < 1000; i++) {
if(XGrabKeyboard(dc->dpy, DefaultRootWindow(dc->dpy), True,
GrabModeAsync, GrabModeAsync, CurrentTime) == GrabSuccess)
return;
usleep(1000);
}
eprintf("cannot grab keyboard\n");
}
void keypress(XKeyEvent *ev) {
char buf[32];
KeySym ksym = NoSymbol;
Status status;
XmbLookupString(xic, ev, buf, sizeof buf, &ksym, &status);
if(status == XBufferOverflow)
return;
switch(ksym) {
case XK_Return:
case XK_KP_Enter:
case XK_Escape:
case XK_Q:
case XK_q:
exit(EXIT_SUCCESS);
case XK_space: /* jump to today */
time(¤t);
break;
case XK_Left: /* back one day */
case XK_h:
adddays(¤t, -1);
break;
case XK_Right: /* forward one day */
case XK_l:
adddays(¤t, 1);
break;
case XK_Up: /* back one week */
case XK_k:
adddays(¤t, -7);
break;
case XK_Down: /* forward one week */
case XK_j:
adddays(¤t, 7);
break;
case XK_K: /* back one month */
addmonths(¤t, -1);
break;
case XK_J: /* forward one month */
addmonths(¤t, 1);
break;
}
drawcal();
}
void buttonpress(XButtonEvent *ev) {
switch (ev->button) {
case Button3:
exit(EXIT_SUCCESS);
case Button4:
addmonths(¤t, -1);
break;
case Button5:
addmonths(¤t, 1);
break;
}
drawcal();
}
void run(void) {
XEvent ev;
while(!XNextEvent(dc->dpy, &ev)) {
if(XFilterEvent(&ev, win))
continue;
switch(ev.type) {
case Expose:
if(ev.xexpose.count == 0)
mapdc(dc, win, cw, ch);
break;
case KeyPress:
keypress(&ev.xkey);
break;
case ButtonPress:
buttonpress(&ev.xbutton);
break;
case VisibilityNotify:
if(ev.xvisibility.state != VisibilityUnobscured)
XRaiseWindow(dc->dpy, win);
break;
}
}
}
void setup(void) {
int x, y, screen = DefaultScreen(dc->dpy);
struct tm* ti;
Window root = RootWindow(dc->dpy, screen);
XSetWindowAttributes swa;
XIM xim;
#ifdef XINERAMA
int n;
XineramaScreenInfo *info;
#endif
curcol[ColBG] = getcolor(dc, bgcolor);
curcol[ColFG] = getcolor(dc, curfgcolor);
othcol[ColBG] = getcolor(dc, bgcolor);
othcol[ColFG] = getcolor(dc, othfgcolor);
todcol[ColFG] = getcolor(dc, todfgcolor);
todcol[ColBG] = getcolor(dc, bgcolor);
hltcol[ColFG] = getcolor(dc, hltfgcolor);
hltcol[ColBG] = getcolor(dc, bgcolor);
invcol[ColBG] = getcolor(dc, curfgcolor);
invcol[ColFG] = getcolor(dc, bgcolor);
utf8 = XInternAtom(dc->dpy, "UTF8_STRING", False);
/* default to and save current time */
time(¤t);
ti = localtime(¤t);
strftime(today, 9, "%Y%m%d", ti);
/* calculate calendar geometry */
ch = 7 * (dc->font.height + 2) + 10;
cw = 21 * (dc->font.width + 2) + 2;
#ifdef XINERAMA
if((info = XineramaQueryScreens(dc->dpy, &n))) {
int a, j, di, i = 0, area = 0;
unsigned int du;
Window w, pw, dw, *dws;
XWindowAttributes wa;
XGetInputFocus(dc->dpy, &w, &di);
if(w != root && w != PointerRoot && w != None) {
/* find top-level window containing current input focus */
do {
if(XQueryTree(dc->dpy, (pw = w), &dw, &w, &dws, &du) && dws)
XFree(dws);
} while(w != root && w != pw);
/* find xinerama screen with which the window intersects most */
if(XGetWindowAttributes(dc->dpy, pw, &wa))
for(j = 0; j < n; j++)
if((a = INTERSECT(wa.x, wa.y, wa.width, wa.height, info[j])) > area) {
area = a;
i = j;
}
}
/* no focused window is on screen, so use pointer location instead */
if(!area && XQueryPointer(dc->dpy, root, &dw, &dw, &x, &y, &di, &di, &du))
for(i = 0; i < n; i++)
if(INTERSECT(x, y, 1, 1, info[i]))
break;
x = info[i].x_org + (leftbar ? 0 : info[i].width - cw);
y = info[i].y_org + (topbar ? 0 : info[i].height - ch);
XFree(info);
}
else
#endif
{
x = leftbar ? 0 : DisplayWidth( dc->dpy, screen) - cw;
y = topbar ? 0 : DisplayHeight(dc->dpy, screen) - ch;
}
/* apply offsets */
x += offset_x;
y += offset_y;
/* create calendar window */
swa.override_redirect = True;
swa.background_pixmap = ParentRelative;
swa.event_mask = ExposureMask | KeyPressMask | ButtonPressMask | VisibilityChangeMask;
win = XCreateWindow(dc->dpy, root, x, y, cw, ch, 0,
DefaultDepth(dc->dpy, screen), CopyFromParent,
DefaultVisual(dc->dpy, screen),
CWOverrideRedirect | CWBackPixmap | CWEventMask, &swa);
/* open input methods */
xim = XOpenIM(dc->dpy, NULL, NULL, NULL);
xic = XCreateIC(xim, XNInputStyle, XIMPreeditNothing | XIMStatusNothing,
XNClientWindow, win, XNFocusWindow, win, NULL);
XMapRaised(dc->dpy, win);
resizedc(dc, cw, ch);
drawcal();
}
void usage(void) {
fputs("usage: dcal [-b] [-l] [-fn font] [-bg color] [-cf color] [-of color] [-tf color] [-hf color] [-v]\n", stderr);
exit(EXIT_FAILURE);
}
unsigned long* pickcolor(const char *date, const char *current) {
int i;
if (keyboard && !strcmp(date, current)) return invcol;
for (i = 0; i < hilight_count; ++i)
if (!strcmp(date, hilight[i])) return hltcol;
if (!strcmp(date, today)) return todcol;
if (!strncmp(date, current, 6)) return curcol;
return othcol;
}