-
Notifications
You must be signed in to change notification settings - Fork 0
/
xcalim_alarm.c
822 lines (753 loc) · 21.1 KB
/
xcalim_alarm.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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
#include <X11/IntrinsicP.h> /* for toolkit stuff */
#include <X11/Intrinsic.h>
#include <X11/Xos.h>
#include <X11/StringDefs.h> /* for useful atom names */
#include <X11/Shell.h>
#include <X11/Xaw/AsciiText.h>
#include <X11/Xaw/Text.h>
#include <X11/Xaw/Command.h>
#include <X11/Xaw/Label.h>
#include <X11/Xaw/Form.h>
#include <X11/Xaw/Paned.h>
#include <stdio.h> /* for printing error messages */
#include <stdlib.h>
#include <ctype.h>
#include <sys/stat.h> /* for stat() */
#include <pwd.h> /* for getting username */
#include <errno.h>
#include "xcalim.h"
#ifndef wordBreak
#define wordBreak 0x01
#define scrollVertical 0x02
#endif
static char *LocalMapStem;
static int LocalMapStemMade; /* day when LocalMapStem made last */
static struct stat localstat;
static char *TodayFile;
static struct stat todaystat;
static Alarm head;
static int *countDown;
static int countDownCt;
static XtCallbackRec callbacks[] = {
{NULL, NULL},
{NULL, NULL}
};
#define dprintf if (appResources.alarmScan) printf
#define MINUTES(hr, mn) ((hr)*60 + (mn))
/*
* Local routines
*/
static void AlarmScan();
static void FreeAlarmList();
static void setCall();
static void AddAlarm();
static void DisplayAlarmWindow();
static void DestroyAlarm();
static void HoldAlarm();
static void AutoDestroy();
static void AlarmEvent();
static void ClockTick();
static void AlarmsOff();
static int readDataLine();
static String CreateCommand();
static int ReloadAlarms();
/*
* Initialise variables for the alarm mechanism
*/
void
InitAlarms()
{
register char *str;
register char *wk;
register ct;
register int *p;
char *XtMalloc();
if (!MyCalendar)
return;
if (appResources.alarms == False) {
dprintf("Alarms not enabled\n");
return;
}
dprintf("Alarms on\n");
/*
* Interpret the countDown string established
* by user - turn this into a vector
* countDown -> points to a vector of integers
* indicating the number of mins each alarm will be set
* countDownCt - the number of integers in the vector
*/
if (str = appResources.countdown) {
for (wk = str, ct = 0; *wk; wk++)
if (*wk == ',')
ct++;
ct++; /* no of things to store */
countDown = (int *) XtMalloc(sizeof(int) * (ct + 2));
p = countDown;
while (*str)
if (!isdigit(*str))
str++;
else
break;
while (*str) {
*p = 0;
ct = 0;
while (isdigit(*str))
ct = ct * 10 + *str++ - '0';
*p++ = ct;
countDownCt++;
while (*str && !isdigit(*str))
str++;
}
}
if (appResources.update)
ClockTick(0, 0);
else
AlarmFilePoll(0);
}
/*
* ClockTick is polled every minute (update) to see if the data file has
* altered - if so then a new data list is built
*/
static void
ClockTick(client_data, id)
void * client_data;
XtIntervalId *id;
{
long secs;
time_t ti;
struct tm *tm;
ti = time(0);
tm = localtime(&ti);
dprintf("ClockTick %d:%02d:%02d\n", tm->tm_hour, tm->tm_min, tm->tm_sec);
secs = ClockSync(tm, appResources.update);
(void) XtAppAddTimeOut(appContext, secs * 1000,
(XtTimerCallbackProc) ClockTick,
(void *) NULL);
AlarmFilePoll(tm);
MemoPoll();
}
void
AlarmFilePoll(tm)
struct tm *tm;
{
time_t ti;
int mnow;
int files, changed;
struct stat oldstat;
char *home;
char buf[256];
Alarm *old;
if (appResources.alarms == False)
return;
if (tm == NULL) {
ti = time(0);
tm = localtime(&ti);
}
/*
* Create the name of the data file in a cache to save energy
*/
if (LocalMapStemMade != tm->tm_mday) {
LocalMapStemMade = tm->tm_mday;
if (appResources.calCompat == False) {
(void) sprintf(buf, "%s/xy%d/xc%d%s%d",
MapStem,
tm->tm_year + 1900,
tm->tm_mday,
appResources.smon[tm->tm_mon],
tm->tm_year + 1900);
} else {
(void) sprintf(buf, "%s/xc%d%s%d",
MapStem,
tm->tm_mday,
appResources.smon[tm->tm_mon],
tm->tm_year + 1900);
}
if (LocalMapStem)
XtFree(LocalMapStem);
LocalMapStem = XtNewString(buf);
dprintf("Todays Daily Filename = %s\n", LocalMapStem);
if (TodayFile)
XtFree(TodayFile);
TodayFile = XtNewString(MakeWeeklyName(tm->tm_wday));
memset(&localstat, '\0', sizeof (struct stat));
memset(&todaystat, '\0', sizeof (struct stat));
}
files = 0;
changed = 0;
/*
* check for file existence
*/
if (access(LocalMapStem, F_OK) == 0) {
files = 1;
oldstat = localstat;
(void) stat(LocalMapStem, &localstat);
if (oldstat.st_mtime != localstat.st_mtime ||
oldstat.st_ctime != localstat.st_ctime)
changed = 1;
}
if (access(TodayFile, F_OK) == 0) {
files |= 2;
oldstat = todaystat;
(void) stat(TodayFile, &todaystat);
if (oldstat.st_mtime != todaystat.st_mtime ||
oldstat.st_ctime != todaystat.st_ctime)
changed |= 2;
}
old = NULL;
if (changed) {
old = head.next;
head.next = NULL;
}
if (files) {
if (changed) {
mnow = MINUTES(tm->tm_hour, tm->tm_min);
if (files & 1)
AlarmScan(LocalMapStem, tm, mnow);
if (files & 2)
AlarmScan(TodayFile, tm, mnow);
/* check on any extant alarm */
if (ReloadAlarms(old, mnow))
AlarmsOff();
if (appResources.interval_id == 0 && head.next)
setCall();
}
} else
dprintf("No files to scan\n");
if (old)
FreeAlarmList(old);
UpdateMemo();
}
static int
ReloadAlarms(old, mnow)
Alarm *old;
int mnow;
{
Alarm *prev;
dprintf("Checking old alarm details\n");
if (head.next == NULL) {
if (old) {
dprintf("No new alarms, stop old running\n");
return 1;
}
}
else if (head.next->alarm_mm <= mnow &&
(old == NULL ||
old->alarm_mm > head.next->alarm_mm)) {
/*
* this looks different but let's be careful
* if the entries on the new list are earlier
* than those on the old - then we have
* serviced those requests and we need to
* ensure that we don't fire them again
*/
while (head.next && head.next->alarm_mm <= mnow &&
(old == NULL ||
old->alarm_mm > head.next->alarm_mm)) {
dprintf("Discard alarm\n");
XtFree(head.next->alarm);
XtFree(head.next->what);
prev = head.next;
head.next = prev->next;
XtFree((char *)prev);
}
return (ReloadAlarms(old, mnow));
}
else if (old && old->alarm_mm != head.next->alarm_mm) {
dprintf("First new alarm differs from old, stop old alarm\n");
dprintf("Old: %d %s %s\n", old->alarm_mm, old->alarm, old->what);
dprintf("New: %d %s %s\n", head.next->alarm_mm, head.next->alarm, head.next->what);
return 1;
}
return 0;
}
static void
AlarmScan(file, tm, mnow)
String file;
struct tm *tm;
int mnow;
{
FILE *fp;
char hrstr[16];
char remline[256];
char *rem;
int hr, mn;
Boolean isAction;
dprintf("Scanning data file %s\n", file);
if ((fp = fopen(file, "r")) == NULL) {
fprintf(stderr, "Unexpected failure to open: %s\n", file);
exit(1);
}
while (readDataLine(fp, &hr, &mn, hrstr, remline)) {
/* see if we have an action string to do */
isAction = False;
rem = remline;
if (*rem == '!' || strncmp(remline, "%cron", 5) == 0) {
if (appResources.execalarms == False)
continue;
isAction = True;
if (*rem == '!')
rem++;
else rem += 5;
while (isspace(*rem))
rem++;
if (*rem == '\0')
continue;
}
AddAlarm(mnow, isAction, hr, mn, hrstr, rem);
}
(void) fclose(fp);
}
/*
* The idea here is to generate a sorted event list - one element for each
* event. We will discard anything that has already happened
*/
static void
AddAlarm(mnow, exec, hr, mn, hrstr, rem)
int mnow;
Boolean exec;
int hr;
int mn;
char *hrstr;
char *rem;
{
Alarm *al, *prev, *new;
char *XtMalloc();
int al_hr, al_mn, mm;
int loop;
int zero = 0;
int *p;
p = countDown;
for (loop = countDownCt; loop--; p++) {
al_hr = hr;
al_mn = mn;
if (*p) {
al_mn -= *p;
if (al_mn < 0) {
al_mn += 60;
al_hr--;
if (al_hr < 0)
continue;
}
}
if ((mm = MINUTES(al_hr, al_mn)) < mnow)
continue;
new = (Alarm *) XtMalloc(sizeof(Alarm));
new->alarm = XtNewString(hrstr);
new->what = XtNewString(rem);
new->alarm_mm = mm;
new->alarm_state = *p;
new->isAction = exec;
new->next = NULL;
/* now insert into correct place in List */
if (head.next == NULL) {
head.next = new;
dprintf("%s - %s; alarm at %02d:%02d\n",
hrstr, rem, al_hr, al_mn);
} else {
for (prev = &head, al = head.next; al; prev = al, al = prev->next)
if (mm < al->alarm_mm)
break;
prev->next = new;
new->next = al;
dprintf("%s - %s; alarm at %02d:%02d\n",
hrstr, rem, al_hr, al_mn);
}
}
}
/*
* read a line looking for a time spec and some data
* return 1 if found
* return 0 at end
* Time spec is: hhmm hmm
* hh:mm hh.mm
* h:mm h.mm
* all above may be optionally followed by:
* A a AM am Am aM P p PM pm Pm pM
* the string is terminated by a space or a tab
*/
static int
readDataLine(fin, hrp, minp, timestr, remline)
FILE *fin;
int *hrp;
int *minp;
char *timestr;
char *remline;
{
register enum readState {
Ignore, Hr1, Hr2, HrSep,
Mn1, Mn2, AmPm, LoseM,
LookSp, LoseSp, Store, AllDone
} state;
register int c = 0;
int hr = 0, mn = 0;
char *destp;
Boolean foundampm = False;
if (feof(fin))
return 0;
state = Hr1;
while (state != AllDone) {
if ((c = getc(fin)) == EOF) {
if (state == Store)
break;
return 0;
}
switch (state) {
case Ignore:
if (c == '\n')
state = Hr1;
continue;
case Hr1:
destp = timestr;
if (isdigit(c)) {
hr = c - '0';
mn = 0;
destp = timestr;
*destp = '\0';
state = Hr2;
break;
}
state = (c == '\n' ? Hr1 : Ignore);
continue;
case Hr2:
if (isdigit(c)) {
hr = hr * 10 + c - '0';
state = HrSep;
break;
}
/* Falls through to .. */
case HrSep:
if (c == ':' || c == '.') {
state = Mn1;
break;
}
/* Falls through to .. */
case Mn1:
if (isdigit(c)) {
mn = c - '0';
state = Mn2;
break;
}
/* Falls through to .. */
case Mn2:
if (isdigit(c)) {
mn = mn * 10 + c - '0';
state = AmPm;
break;
} else if (state == Mn2) {
state = (c == '\n' ? Hr1 : Ignore);
continue;
}
/* Falls through to .. */
case AmPm:
if (c == 'a' || c == 'A') {
if (hr == 12)
hr = 0;
foundampm = True;
state = LoseM;
break;
} else if (c == 'p' || c == 'P') {
if (hr < 12)
hr += 12;
foundampm = True;
state = LoseM;
break;
}
/* Falls through to .. */
case LoseM:
if (c == 'M' || c == 'm') {
state = LookSp;
break;
}
/* Falls through to .. */
case LookSp:
if (c == ' ' || c == '\t') {
state = LoseSp;
if (hr >= 24 || mn >= 60)
state = Ignore;
destp = remline;
*destp = '\0';
continue;
} else {
state = (c == '\n' ? Hr1 : Ignore);
continue;
}
break;
case LoseSp:
if (c == ' ' || c == '\t')
continue;
state = Store;
/* Falls through to .. */
case Store:
if (c == '\n') {
state = AllDone;
continue;
}
break;
}
*destp++ = c;
*destp = '\0';
}
*hrp = hr;
*minp = mn;
return 1;
}
/*
* set up the timeout for the next event
*/
static void
setCall()
{
time_t ti;
long mnow;
struct tm *tm;
Alarm *sc;
int togo;
ti = time(0);
tm = localtime(&ti);
mnow = MINUTES(tm->tm_hour, tm->tm_min);
for (sc = head.next; sc; sc = sc->next) {
togo = sc->alarm_mm - mnow;
if (togo == 0)
{ AlarmEvent(1, 0); /* don't miss any current alarms */
sc = &head;
continue;
}
if (togo < 0)
continue;
appResources.interval_id = XtAppAddTimeOut(appContext,
togo * 60 * 1000 - tm->tm_sec * 1000,
(XtTimerCallbackProc) AlarmEvent, (void *) NULL);
dprintf("Alarm in %d mins (less %d secs)\n", togo, tm->tm_sec);
break;
}
}
static void
AlarmEvent(client_data, id)
void * client_data;
XtIntervalId *id;
{
int tnow;
long ti;
struct tm *tm;
Alarm *al, *action, *actionend;
String cmd;
appResources.interval_id = 0;
ti = time(0);
tm = localtime(&ti);
tnow = MINUTES(tm->tm_hour, tm->tm_min);
dprintf("Alarm %d:%02d:%02d\n", tm->tm_hour, tm->tm_min, tm->tm_sec);
/*
* Look for alarms that have fired and remove them
* from the list
*/
actionend = NULL;
for (al = head.next; al; al = al->next) {
if (al->alarm_mm <= tnow)
actionend = al;
else break;
}
if (actionend) {
al = head.next;
head.next = actionend->next;
actionend->next = NULL;
/*
* now process the list
*/
while (al) {
if (tnow == al->alarm_mm) {
if (appResources.cmd != NULL) {
cmd = CreateCommand(appResources.cmd, al->what);
system(cmd);
XtFree(cmd);
}
if (al->isAction == False) {
DisplayAlarmWindow(al->alarm_state, al->alarm, al->what);
} else {
cmd = CreateCommand(al->what, NULL);
system(cmd);
XtFree(cmd);
}
}
actionend = al->next;
XtFree(al->alarm);
XtFree(al->what);
XtFree((char *) al);
al = actionend;
}
}
if (client_data == NULL)
setCall();
}
static String
CreateCommand(cmd, arg)
String cmd;
String arg;
{
String buf;
char *s, *d;
char *fmt;
int cmdlen;
int arglen;
cmdlen = strlen(cmd);
arglen = arg ? strlen(arg) : 0;
buf = XtMalloc(cmdlen + arglen + 200);
if (arglen == 0) fmt = "%s&";
else fmt = "%s '";
(void) sprintf(buf, fmt, cmd);
/*
* copy the arguments worrying about any embedded quote
*/
#define QUOTE '\''
#define BACKSLASH '\\'
if (arglen) {
d = buf + strlen(buf);
for(s = arg; *s; s++) {
if (*s == QUOTE) {
*d++ = QUOTE;
*d++ = BACKSLASH;
*d++ = QUOTE;
}
*d++ = *s;
}
*d++ = QUOTE;
*d++ = '&';
*d++ = '\0';
}
return buf;
}
static void
FreeAlarmList(al)
register Alarm *al;
{
register Alarm *nx;
for (; al; al = nx) {
nx = al->next;
XtFree(al->alarm);
XtFree(al->what);
XtFree((char *) al);
}
}
static void
AlarmsOff()
{
if (appResources.interval_id) {
XtRemoveTimeOut(appResources.interval_id);
appResources.interval_id = 0;
}
}
typedef struct {
Widget sa_top;
XtIntervalId sa_id;
} AlarmStatus;
static void
DisplayAlarmWindow(tleft, str1, str2)
int tleft;
String str1, str2;
{
Widget shell, form, title, ah;
char *fmt;
char buf[255];
AlarmStatus *als;
/*
* making the top level shell the override widget class causes it to
* popup without window manager interaction or window manager
* handles. this also means that it pops on the foreground of an
* xlocked terminal and is not resizable by the window manager. If
* any one finds that to be desirable behavior, then change the
* transient class below to override.
*
* For now transient class is much better behaved
*/
shell = XtVaCreatePopupShell("alarm", transientShellWidgetClass, toplevel,
XtNallowShellResize, True,
XtNinput, True,
XtNsaveUnder, TRUE,
NULL);
form = XtVaCreateManagedWidget("alarmPanel", panedWidgetClass, shell, NULL);
/*
* create alarm status save area
*/
als = (AlarmStatus *) XtMalloc(sizeof(AlarmStatus));
als->sa_top = shell;
als->sa_id = (XtIntervalId) NULL;
if (appResources.autoquit) {
als->sa_id = XtAppAddTimeOut(appContext,
appResources.autoquit * 1000,
AutoDestroy, (void *) als);
}
title = XtVaCreateManagedWidget("alarmForm", formWidgetClass, form,
XtNshowGrip, False,
XtNdefaultDistance, 2,
NULL);
/*
* Hold button Take "Hold" from resources
*/
callbacks[0].callback = HoldAlarm;
callbacks[0].closure = (void *) als;
ah = XtVaCreateManagedWidget("alarmHold", commandWidgetClass, title,
XtNleft, XtChainLeft,
XtNright, XtChainLeft,
XtNcallback, callbacks,
XtNsensitive, appResources.autoquit ? True : False,
NULL);
if (tleft == 0)
fmt = appResources.alarmnow;
else
fmt = appResources.alarmleft;
if (fmt && *fmt) {
(void) sprintf(buf, fmt, tleft);
(void) XtVaCreateManagedWidget("alarmTitle", labelWidgetClass, title,
XtNfromHoriz, ah,
XtNleft, XtChainLeft,
XtNright, XtChainLeft,
XtNborderWidth, 0,
XtNfromVert, NULL,
XtNvertDistance, 3,
XtNlabel, buf,
NULL);
}
/*
* Now the text which is the remainder of the panel
*/
(void) sprintf(buf, "%s\n%s\n", str1, str2);
(void) XtVaCreateManagedWidget("alarmText", asciiTextWidgetClass, form,
XtNshowGrip, False,
XtNstring, buf,
XtNdisplayCaret, False,
NULL);
XtOverrideTranslations(shell,
XtParseTranslationTable("<Message>WM_PROTOCOLS: PopDownShell()"));
XtPopup(shell, XtGrabNone);
(void) XSetWMProtocols(XtDisplay(shell), XtWindow(shell), &delWin, 1);
}
/* ARGSUSED */
static void
DestroyAlarm(w, als, call_data)
Widget w;
AlarmStatus *als;
void * call_data;
{
if (als->sa_id)
XtRemoveTimeOut(als->sa_id);
AutoDestroy(als, NULL);
}
/* ARGSUSED */
static void
HoldAlarm(w, als, call_data)
Widget w;
AlarmStatus *als;
void * call_data;
{
if (als->sa_id)
XtRemoveTimeOut(als->sa_id);
XtSetSensitive(w, FALSE);
}
/* ARGSUSED */
static void
AutoDestroy(als, id)
AlarmStatus *als;
XtIntervalId *id;
{
XtDestroyWidget(als->sa_top);
XtFree((char *) als);
}