-
Notifications
You must be signed in to change notification settings - Fork 14
/
xclock.c
2589 lines (2189 loc) · 78.8 KB
/
xclock.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
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* $Header: /nfs/kodak/kc1/pjs/src/X11/catclock/motif/RCS/xclock.c,v 1.7 91/11/26 11:46:54 pjs Exp Locker: pjs $ */
/* Copyright 1985 Massachusetts Institute of Technology */
/*
* xclock.c MIT Project Athena, X Window system clock.
*
* This program provides the user with a small
* window contining a digital clock with day and date.
* Parameters are variable from the command line.
*
* Author: Tony Della Fera, DEC
* September, 1984
* Hacked up by a cast of thousands....
*
* Ported to X11 & Motif :
* Philip J. Schneider, DEC
* 1990
*/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <pwd.h>
#include <unistd.h>
/*
* X11 includes
*/
#include <X11/Xlib.h>
#include <X11/Xos.h>
#include <X11/Xutil.h>
#include <X11/cursorfont.h>
#include <X11/Intrinsic.h>
#include <X11/Shell.h>
#include <X11/StringDefs.h>
/*
* Motif includes
*/
#include <Xm/Xm.h>
#include <Xm/Form.h>
#include <Xm/DrawingA.h>
#include <Xm/PushB.h>
#include <Xm/ToggleB.h>
#include <Xm/MenuShell.h>
#include <Xm/RowColumn.h>
#include <Xm/Separator.h>
#include "alarm.h"
/*
* Cat bitmap includes
*/
#include "catback.xbm"
#include "catwhite.xbm"
#include "cattie.xbm"
#include "eyes.xbm"
#include "tail.xbm"
/*
* Icon pixmap includes
*/
#include "analog.xbm"
#include "digital.xbm"
#include "cat.xbm"
/*
* Patch info
*/
#include "patchlevel.h"
/*
* Tempo tracker dependencies
*/
#if WITH_TEMPO_TRACKER
#include <sys/time.h>
#include <pthread.h>
#include <pulse/simple.h>
#include <pulse/error.h>
#include <aubio/aubio.h>
#define BUFSIZE 256
#define HOPSIZE 256
#define SAMPLERATE 44100
#endif
/*
* Clock Mode -- type of clock displayed
*/
#define ANALOG_CLOCK 0 /* Ye olde X10 xclock face */
#define DIGITAL_CLOCK 1 /* ASCII clock */
#define CAT_CLOCK 2 /* KitCat (R) clock face */
static int clockMode; /* One of the above :-) */
/*
* Cat body part pixmaps
*/
static Pixmap *eyePixmap = (Pixmap *)NULL; /* Array of eyes */
static Pixmap *tailPixmap = (Pixmap *)NULL; /* Array of tails */
/*
* Cat GC's
*/
static GC catGC; /* For drawing cat's body */
static GC tailGC; /* For drawing cat's tail */
static GC eyeGC; /* For drawing cat's eyes */
/*
* Default cat dimension stuff -- don't change sizes!!!!
*/
#define DEF_N_TAILS 16 /* Default resolution */
#define TAIL_HEIGHT 89 /* Tail pixmap height */
#define DEF_CAT_WIDTH 150 /* Cat body pixmap width */
#define DEF_CAT_HEIGHT 300 /* Cat body pixmap height */
#define DEF_CAT_BOTTOM 210 /* Distance to cat's butt */
/*
* Analog clock width and height
*/
#define DEF_ANALOG_WIDTH 164 /* Chosen for historical */
#define DEF_ANALOG_HEIGHT 164 /* reasons :-) */
/*
* Digital time string origin
*/
static int digitalX, digitalY;
/*
* Clock hand stuff
*/
#define VERTICES_IN_HANDS 4 /* Hands are triangles */
#define SECOND_HAND_FRACT 90 /* Percentages of radius */
#define MINUTE_HAND_FRACT 70
#define HOUR_HAND_FRACT 40
#define HAND_WIDTH_FRACT 7
#define SECOND_WIDTH_FRACT 5
#define SECOND_HAND_TIME 30 /* Update limit for second hand*/
static int centerX = 0; /* Window coord origin of */
static int centerY = 0; /* clock hands. */
static int radius; /* Radius of clock face */
static int secondHandLength; /* Current lengths and widths */
static int minuteHandLength;
static int hourHandLength;
static int handWidth;
static int secondHandWidth;
#define SEG_BUFF_SIZE 128 /* Max buffer size */
static int numSegs = 0; /* Segments in buffer */
static XPoint segBuf[SEG_BUFF_SIZE]; /* Buffer */
static XPoint *segBufPtr = (XPoint *)NULL; /* Current pointer */
/*
* Default font for digital display
*/
#define DEF_DIGITAL_FONT "fixed"
/*
* Padding defaults
*/
#define DEF_DIGITAL_PADDING 10 /* Space around time display */
#define DEF_ANALOG_PADDING 8 /* Radius padding for analog */
/*
* Alarm stuff
*/
#define DEF_ALARM_FILE "/.Xclock" /* Alarm settings file */
#define DEF_ALARM_PERIOD 60
static Boolean alarmOn = False; /* Alarm set? */
static Boolean alarmState = False; /* Seems to be unused */
static char alarmBuf[BUFSIZ]; /* Path name to alarm file */
/*
* Time stuff
*/
static struct tm tm; /* What time is it? */
static struct tm otm; /* What time was it? */
/*
* X11 Stuff
*/
static Window clockWindow = (Window)NULL;
static XtAppContext appContext;
Display *dpy;
Window root;
int screen;
GC gc; /* For tick-marks, text, etc. */
static GC handGC; /* For drawing hands */
static GC eraseGC; /* For erasing hands */
static GC highGC; /* For hand borders */
#define UNINIT -1
static int winWidth = UNINIT; /* Global window width */
static int winHeight = UNINIT; /* Global window height */
/*
* Resources user can set in addition to normal Xt resources
*/
typedef struct {
XFontStruct *font; /* For alarm & analog */
Pixel foreground; /* Foreground */
Pixel background; /* Background */
Pixel highlightColor; /* Hand outline/border */
Pixel handColor; /* Hand interior */
Pixel catColor; /* Cat's body */
Pixel detailColor; /* Cat's paws, belly, */
/* face, and eyes. */
Pixel tieColor; /* Cat's tie color */
int nTails; /* Tail/eye resolution */
int padding; /* Font spacing */
char *modeString; /* Display mode */
int update; /* Seconds between */
/* updates */
Boolean alarmSet; /* Alarm on? */
Boolean alarmBell; /* Audible alarm? */
char *alarmFile; /* Alarm setting file */
int alarmBellPeriod; /* How long to ring */
/* alarm */
Boolean chime; /* Chime on hour? */
int help; /* Display syntax */
} ApplicationData, *ApplicationDataPtr;
static ApplicationData appData;
/*
* Miscellaneous stuff
*/
#define TWOPI (2.0 * M_PI) /* 2.0 * M_PI */
#define DEF_UPDATE 60
#define min(a, b) ((a) < (b) ? (a) : (b))
#define max(a, b) ((a) > (b) ? (a) : (b))
static Boolean noSeconds = False; /* Update time >= 1 minute? */
static Boolean evenUpdate = False; /* Even update interval? */
static Boolean showSecondHand = False; /* Display second hand? */
static Boolean iconified = False; /* Clock iconified? */
#if WITH_TEMPO_TRACKER
static float phase = 0.5;
static float bpm = 120.0;
static float correction = 0.0;
Boolean last_time_initialized = True;
struct timeval last_time;
static int direction = 1;
#endif
static void ParseGeometry(Widget, int, int, int);
static int Round(double);
static void DigitalString(char *);
static void Syntax(char *);
static void InitializeCat(Pixel, Pixel, Pixel);
static GC CreateTailGC(void);
static GC CreateEyeGC(void);
static Pixmap CreateTailPixmap(double);
static Pixmap CreateEyePixmap(double);
static void EraseHands(Widget, struct tm *);
static void HandleExpose(Widget, XtPointer, XtPointer);
static void HandleInput(Widget, XtPointer, XtPointer);
static void HandleResize(Widget, XtPointer, XtPointer);
static void Tick(Widget, int);
static void AlarmSetCallback(Widget, XtPointer, XtPointer);
static void AlarmBellCallback(Widget, XtPointer, XtPointer);
static void ChimeCallback(Widget, XtPointer, XtPointer);
static void AckAlarmCallback(Widget, XtPointer, XtPointer);
static void RereadAlarmCallback(Widget, XtPointer, XtPointer);
static void EditAlarmCallback(Widget, XtPointer, XtPointer);
static void ExitCallback(Widget, XtPointer, XtPointer);
static void MapCallback(Widget, XtPointer, XEvent *, Boolean *);
static void SetSeg(int, int, int, int);
#if WITH_TEMPO_TRACKER
static void *TempoTrackerThread();
#endif
int main(argc, argv)
int argc;
char *argv[];
{
int n;
Arg args[10];
Widget topLevel, form, canvas;
int stringWidth = 0,
stringAscent = 0, stringDescent = 0;
XGCValues gcv;
u_long valueMask;
XmFontList fontList = (XmFontList)NULL;
static XtResource resources[] = {
{ XtNfont, XtCFont, XtRFontStruct, sizeof(XFontStruct *),
XtOffset(ApplicationDataPtr, font),
XtRString, (XtPointer)DEF_DIGITAL_FONT },
{ XtNforeground, XtCForeground, XtRPixel, sizeof(Pixel),
XtOffset(ApplicationDataPtr, foreground),
XtRString, (XtPointer)"XtdefaultForeground" },
{ XtNbackground, XtCBackground, XtRPixel, sizeof(Pixel),
XtOffset(ApplicationDataPtr, background),
XtRString, (XtPointer)"XtdefaultBackground" },
{ "highlight", "HighlightColor", XtRPixel, sizeof(Pixel),
XtOffset(ApplicationDataPtr, highlightColor),
XtRString, (XtPointer)"XtdefaultForeground" },
{ "hands", "Hands", XtRPixel, sizeof(Pixel),
XtOffset(ApplicationDataPtr, handColor),
XtRString, (XtPointer)"XtdefaultForeground" },
{ "catColor", "CatColor", XtRPixel, sizeof(Pixel),
XtOffset(ApplicationDataPtr, catColor),
XtRString, (XtPointer)"XtdefaultForeground" },
{ "detailColor", "DetailColor", XtRPixel, sizeof(Pixel),
XtOffset(ApplicationDataPtr, detailColor),
XtRString, (XtPointer)"XtdefaultBackground" },
{ "tieColor", "TieColor", XtRPixel, sizeof(Pixel),
XtOffset(ApplicationDataPtr, tieColor),
XtRString, (XtPointer)"XtdefaultBackground" },
{ "padding", "Padding", XtRInt, sizeof(int),
XtOffset(ApplicationDataPtr, padding),
XtRImmediate, (XtPointer)UNINIT },
{ "nTails", "NTails", XtRInt, sizeof(int),
XtOffset(ApplicationDataPtr, nTails),
XtRImmediate, (XtPointer)DEF_N_TAILS },
{ "update", "Update", XtRInt, sizeof(int),
XtOffset(ApplicationDataPtr, update),
XtRImmediate, (XtPointer)DEF_UPDATE },
{ "alarm", "Alarm", XtRBoolean, sizeof(Boolean),
XtOffset(ApplicationDataPtr, alarmSet),
XtRImmediate, (XtPointer)False },
{ "bell", "Bell", XtRBoolean, sizeof(Boolean),
XtOffset(ApplicationDataPtr, alarmBell),
XtRImmediate, (XtPointer)False },
{ "file", "File", XtRString, sizeof(char *),
XtOffset(ApplicationDataPtr, alarmFile),
XtRString, (XtPointer)NULL },
{ "period", "Period", XtRInt, sizeof(int),
XtOffset(ApplicationDataPtr, alarmBellPeriod),
XtRImmediate, (XtPointer)DEF_ALARM_PERIOD },
{ "chime", "Chime", XtRBoolean, sizeof(Boolean),
XtOffset(ApplicationDataPtr, chime),
XtRImmediate, (XtPointer)False },
{ "mode", "Mode", XtRString, sizeof(char *),
XtOffset(ApplicationDataPtr, modeString),
XtRImmediate, (XtPointer)"cat"},
{ "help", "Help", XtRBoolean, sizeof(Boolean),
XtOffset(ApplicationDataPtr, help),
XtRImmediate, (XtPointer)False},
};
static XrmOptionDescRec options[] = {
{ "-bg", "*background", XrmoptionSepArg, (XtPointer)NULL },
{ "-background", "*background", XrmoptionSepArg, (XtPointer)NULL },
{ "-fg", "*foreground", XrmoptionSepArg, (XtPointer)NULL },
{ "-foreground", "*foreground", XrmoptionSepArg, (XtPointer)NULL },
{ "-fn", "*font", XrmoptionSepArg, (XtPointer)NULL },
{ "-font", "*font", XrmoptionSepArg, (XtPointer)NULL },
{ "-hl", "*highlight", XrmoptionSepArg, (XtPointer)NULL },
{ "-highlight", "*highlight", XrmoptionSepArg, (XtPointer)NULL },
{ "-hd", "*hands", XrmoptionSepArg, (XtPointer)NULL },
{ "-hands", "*hands", XrmoptionSepArg, (XtPointer)NULL },
{ "-catcolor", "*catColor", XrmoptionSepArg, (XtPointer)NULL },
{ "-detailcolor", "*detailColor", XrmoptionSepArg, (XtPointer)NULL },
{ "-tiecolor", "*tieColor", XrmoptionSepArg, (XtPointer)NULL },
{ "-padding", "*padding", XrmoptionSepArg, (XtPointer)NULL },
{ "-mode", "*mode", XrmoptionSepArg, (XtPointer)NULL },
{ "-ntails", "*nTails", XrmoptionSepArg, (XtPointer)NULL },
{ "-update", "*update", XrmoptionSepArg, (XtPointer)NULL },
{ "-alarm", "*alarm", XrmoptionNoArg, (XtPointer)"on" },
{ "-bell", "*bell", XrmoptionNoArg, (XtPointer)"on" },
{ "-file", "*file", XrmoptionSepArg, (XtPointer)NULL },
{ "-period", "*period", XrmoptionSepArg, (XtPointer)NULL },
{ "-chime", "*chime", XrmoptionNoArg, (XtPointer)"on" },
{ "-help", "*help", XrmoptionNoArg, (XtPointer)"on" },
};
/*
* Hack to make Saber-C work with Xt correctly
*/
argv[0] = "xclock";
/*
* Open up the system
*/
topLevel = XtAppInitialize(&appContext, "Catclock",
(XrmOptionDescList)(&options[0]),
XtNumber(options),
&argc, argv, NULL,
NULL, 0);
/*
* Get resources . . .
*/
XtGetApplicationResources(topLevel, &appData, resources,
XtNumber(resources), NULL, 0);
/*
* Print help message and exit if asked
*/
if (appData.help) {
Syntax(argv[0]);
}
/*
* Save away often-used X stuff
*/
dpy = XtDisplay(topLevel);
screen = DefaultScreen(dpy);
root = DefaultRootWindow(dpy);
/*
* See if user specified iconic startup for clock
*/
n = 0;
XtSetArg(args[n], XmNiconic, &iconified); n++;
XtGetValues(topLevel, args, n);
/*
* Get/set clock mode
*/
if (strcmp(appData.modeString, "cat") == 0) {
clockMode = CAT_CLOCK;
} else if (strcmp(appData.modeString, "analog") == 0) {
clockMode = ANALOG_CLOCK;
} else if (strcmp(appData.modeString, "digital") == 0) {
clockMode = DIGITAL_CLOCK;
} else {
clockMode = ANALOG_CLOCK;
}
/*
* Create icon pixmap
*/
{
Pixmap iconPixmap;
char *data;
u_int width = 0, height = 0;
switch (clockMode) {
case ANALOG_CLOCK : {
data = analog_bits;
width = analog_width;
height = analog_height;
break;
}
case CAT_CLOCK : {
data = cat_bits;
width = cat_width;
height = cat_height;
break;
}
case DIGITAL_CLOCK : {
data = digital_bits;
width = digital_width;
height = digital_height;
break;
}
}
iconPixmap = XCreateBitmapFromData(dpy, root,
data, width, height);
n = 0;
XtSetArg(args[n], XmNiconPixmap, iconPixmap); n++;
XtSetValues(topLevel, args, n);
}
/*
* Get the font info
*/
if (appData.font == (XFontStruct *)NULL) {
appData.font = XLoadQueryFont(dpy, DEF_DIGITAL_FONT);
if (appData.font == (XFontStruct *)NULL) {
fprintf(stderr,
"xclock : Unable to load default font %s. Please re-run with another font\n",
DEF_DIGITAL_FONT);
exit(-1);
}
}
fontList = XmFontListCreate(appData.font, XmSTRING_ISO8859_1);
if (fontList == (XmFontList)NULL) {
fprintf(stderr, "xclock : Unable to create font list -- exiting\n");
exit(-1);
}
/*
* Set mode-dependent stuff
*/
switch (clockMode) {
case ANALOG_CLOCK : {
/*
* Padding is space between clock face and
* edge of window
*/
if (appData.padding == UNINIT) {
appData.padding = DEF_ANALOG_PADDING;
}
/*
* Check if we should show second hand --
* if greater than threshhold, don't show it
*/
if (appData.update <= SECOND_HAND_TIME) {
showSecondHand = True;
}
break;
}
case CAT_CLOCK : {
/*
* Padding is space between clock face and
* edge of cat's belly.
*/
if (appData.padding == UNINIT) {
appData.padding = DEF_ANALOG_PADDING;
}
/*
* Bound the number of tails
*/
if (appData.nTails < 1) {
appData.nTails = 1;
}
if (appData.nTails > 60) {
appData.nTails = 60;
}
/*
* Update rate depends on number of tails,
* so tail swings at approximately 60 hz.
*/
appData.update = (int)(0.5 + 1000.0 / appData.nTails);
/*
* Drawing the second hand on the cat is ugly.
*/
showSecondHand = False;
break;
}
case DIGITAL_CLOCK : {
char *timePtr;
time_t timeValue;
int stringDir;
XCharStruct xCharStr;
/*
* Padding is around time text
*/
if (appData.padding == UNINIT) {
appData.padding = DEF_DIGITAL_PADDING;
}
/*
* Check if we should show second hand --
* if greater than threshhold, don't show it
*/
if (appData.update >= 60) {
noSeconds = True;
}
/*
* Get font dependent information and determine window
* size from a test string.
*/
time(&timeValue);
timePtr = ctime(&timeValue);
DigitalString(timePtr);
XTextExtents(appData.font, timePtr, (int)strlen(timePtr),
&stringDir, &stringAscent, &stringDescent, &xCharStr);
stringWidth = XTextWidth(appData.font, timePtr, (int)strlen(timePtr));
break;
}
}
/*
* "ParseGeometry" looks at the user-specified geometry
* specification string, and attempts to apply it in a rational
* fashion to the clock in whatever mode it happens to be in.
* For example, for analog mode, any sort of geometry is OK, but
* the cat must be a certain size, although the x and y origins
* should not be ignored, etc.
*/
ParseGeometry(topLevel, stringWidth, stringAscent, stringDescent);
/*
* Create widgets for xclock :
*
*
* Outermost widget is a form widget.
*/
n = 0;
form = XmCreateForm(topLevel, "form", args, n);
XtManageChild(form);
/*
* "canvas" is the display widget
*/
n = 0;
XtSetArg(args[n], XmNtopAttachment, XmATTACH_FORM); n++;
XtSetArg(args[n], XmNbottomAttachment, XmATTACH_FORM); n++;
XtSetArg(args[n], XmNleftAttachment, XmATTACH_FORM); n++;
XtSetArg(args[n], XmNrightAttachment, XmATTACH_FORM); n++;
XtSetArg(args[n], XmNforeground, appData.foreground); n++;
XtSetArg(args[n], XmNbackground, appData.background); n++;
canvas = XmCreateDrawingArea(form, "drawingArea", args, n);
XtManageChild(canvas);
/*
* Make all the windows, etc.
*/
XtRealizeWidget(topLevel);
/*
* When clock is first mapped, we can start
* the ticking. This handler is removed following
* that first map event. This approach is
* necessary (I think) to make the clock
* start up correctly with different window
* managers, or without one, for that matter.
*/
XtAddEventHandler(topLevel, StructureNotifyMask,
False, MapCallback, (XtPointer)NULL);
/*
* Cache the window associated with the XmDrawingArea
*/
clockWindow = XtWindow(canvas);
/*
* Supply a cursor for this application
*/
{
Cursor arrow = XCreateFontCursor(dpy, XC_top_left_arrow);
XDefineCursor(dpy, clockWindow, arrow);
}
/*
* Check if update interval is even or odd # of seconds
*/
if (appData.update > 1 &&
((60 / appData.update) * appData.update == 60)) {
evenUpdate = True;
}
/*
* Set the sizes of the hands for analog and cat mode
*/
switch (clockMode) {
case ANALOG_CLOCK : {
radius = (min(winWidth,
winHeight) - (2 * appData.padding)) / 2;
secondHandLength = ((SECOND_HAND_FRACT * radius) / 100);
minuteHandLength = ((MINUTE_HAND_FRACT * radius) / 100);
hourHandLength = ((HOUR_HAND_FRACT * radius) / 100);
handWidth = ((HAND_WIDTH_FRACT * radius) / 100);
secondHandWidth = ((SECOND_WIDTH_FRACT * radius) / 100);
centerX = winWidth / 2;
centerY = winHeight / 2;
break;
}
case CAT_CLOCK : {
winWidth = DEF_CAT_WIDTH;
winHeight = DEF_CAT_HEIGHT;
radius = Round((min(winWidth,
winHeight)-(2 * appData.padding)) / 3.45);
secondHandLength = ((SECOND_HAND_FRACT * radius) / 100);
minuteHandLength = ((MINUTE_HAND_FRACT * radius) / 100);
hourHandLength = ((HOUR_HAND_FRACT * radius) / 100);
handWidth = ((HAND_WIDTH_FRACT * radius) / 100) * 2;
secondHandWidth = ((SECOND_WIDTH_FRACT * radius) / 100);
centerX = winWidth / 2;
centerY = winHeight / 2;
break;
}
}
/*
* Create the GC's
*/
valueMask = GCForeground | GCBackground | GCFont |
GCLineWidth | GCGraphicsExposures;
gcv.background = appData.background;
gcv.foreground = appData.foreground;
gcv.graphics_exposures = False;
gcv.line_width = 0;
if (appData.font != NULL) {
gcv.font = appData.font->fid;
} else {
valueMask &= ~GCFont; /* use server default font */
}
gc = XCreateGC(dpy, clockWindow, valueMask, &gcv);
valueMask = GCForeground | GCLineWidth ;
gcv.foreground = appData.background;
eraseGC = XCreateGC(dpy, clockWindow, valueMask, &gcv);
gcv.foreground = appData.highlightColor;
highGC = XCreateGC(dpy, clockWindow, valueMask, &gcv);
valueMask = GCForeground;
gcv.foreground = appData.handColor;
handGC = XCreateGC(dpy, clockWindow, valueMask, &gcv);
/*
* Make sure we have an alarm file. If not
* specified in command line or .Xdefaults file, then
* use default of "$HOME/DEF_ALARM_FILE"
*/
if (!appData.alarmFile) {
char *cp;
struct passwd *pw;
if ((cp = getenv("HOME"))) {
strcpy(alarmBuf, cp);
} else if ((pw = getpwuid(getuid()))) {
strcpy(alarmBuf, pw->pw_dir);
} else {
*alarmBuf = 0;
}
strcat(appData.alarmFile = alarmBuf, DEF_ALARM_FILE);
}
/*
* Set up the alarm and alarm bell
*/
InitBellAlarm(clockWindow,
winWidth, winHeight,
appData.font, fontList,
appData.foreground, appData.background,
&alarmState, &alarmOn);
SetAlarm(appData.alarmSet ? appData.alarmFile : NULL);
SetBell(appData.alarmBell ? appData.alarmBellPeriod : 0);
/*
* Create cat pixmaps, etc. if in CAT_CLOCK mode
*/
if (clockMode == CAT_CLOCK) {
InitializeCat(appData.catColor,
appData.detailColor,
appData.tieColor);
}
/*
* Finally, install necessary callbacks
*/
{
XtAddCallback(canvas, XmNexposeCallback, HandleExpose, NULL);
XtAddCallback(canvas, XmNresizeCallback, HandleResize, NULL);
XtAddCallback(canvas, XmNinputCallback, HandleInput, NULL);
}
#if WITH_TEMPO_TRACKER
if (clockMode == CAT_CLOCK) {
pthread_t thread;
void *arg;
pthread_create(&thread, NULL, TempoTrackerThread, arg);
}
#endif
/*
* Start processing events
*/
XtAppMainLoop(appContext);
return 0;
}
#if WITH_TEMPO_TRACKER
static void *TempoTrackerThread() {
static const pa_sample_spec ss = {
.format = PA_SAMPLE_FLOAT32,
.rate = SAMPLERATE,
.channels = 1
};
pa_simple *s = NULL;
int error;
if (!(s = pa_simple_new(NULL, "Cat Clock", PA_STREAM_RECORD, NULL, "record", &ss, NULL, NULL, &error))) {
fprintf(stderr, __FILE__": pa_simple_new() failed: %s\n", pa_strerror(error));
pthread_exit(NULL);
}
aubio_tempo_t *tempo = new_aubio_tempo("default", BUFSIZE, HOPSIZE, SAMPLERATE);
fvec_t *tempo_out = new_fvec(2);
fvec_t *aubio_buf = new_fvec(BUFSIZE);
for (;;) {
pa_simple_read(s, aubio_buf->data, sizeof(float) * BUFSIZE, &error);
aubio_tempo_do(tempo, aubio_buf, tempo_out);
float confidence = aubio_tempo_get_confidence(tempo);
Boolean is_beat = tempo_out->data[0] != 0.0;
if (confidence > 0.1) {
bpm = aubio_tempo_get_bpm(tempo);
}
// Time delta
struct timeval now;
gettimeofday(&now, NULL);
if (!last_time_initialized) {
last_time = now;
last_time_initialized = True;
}
struct timeval time_delta;
timersub(&now, &last_time, &time_delta);
last_time = now;
// Speed correction
//
// We want beat to hit at 0.0, 0.5 or "1.0"
// so we trying to detect if it was too late this time
// and attenuate the speed to make animation sync
// with the beat better when it occur next time.
if (confidence > 0.1) {
if (is_beat) {
float phase_delta = 0;
if (phase < 0.25) {
phase_delta = phase - 0.0;
} else if (phase > 0.25 && phase < 0.75) {
phase_delta = phase - 0.5;
} else if (phase > 0.75) {
phase_delta = phase - 1.0;
}
correction = phase_delta * -direction;
}
}
float time_delta_s = ((float)time_delta.tv_usec * 0.000001);
float correction_amplification = 20.0;
float speed = bpm / 60.0 * 0.5 + (correction * correction_amplification * time_delta_s);
// Move phase
phase += time_delta_s * speed * direction;
if (direction == 1 && phase >= 1.0) {
phase = 1.0;
direction = -1;
} else if (direction == -1 && phase < 0.0) {
phase = 0.0;
direction = 1;
}
}
pthread_exit(NULL);
}
#endif
static void ParseGeometry(topLevel, stringWidth, stringAscent, stringDescent)
Widget topLevel;
int stringWidth, stringAscent, stringDescent;
{
int n;
Arg args[10];
char *geomString = NULL;
char *geometry,
xString[80], yString[80],
widthString[80], heightString[80];
/*
* Grab the geometry string from the topLevel widget
*/
n = 0;
XtSetArg(args[n], XmNgeometry, &geomString); n++;
XtGetValues(topLevel, args, n);
/*
* Static analysis tags this as a memory leak...
* Must be dynamically allocated; otherwise, the geom string
* would be out of scope by the time XtRealizeWidget is called,
* and you would get a garbage geometry specification, at best.
*/
geometry = malloc(80);
switch (clockMode) {
case ANALOG_CLOCK : {
if (geomString == NULL) {
/*
* User didn't specify any geometry, so we
* use the default.
*/
sprintf(geometry, "%dx%d",
DEF_ANALOG_WIDTH, DEF_ANALOG_HEIGHT);
} else {
/*
* Gotta do some work.
*/
int x, y;
unsigned int width, height;
int geomMask;
/*
* Find out what's been set
*/
geomMask = XParseGeometry(geomString,
&x, &y, &width, &height);
if (geomMask == AllValues) {
/*
* If width, height, x, and y have been set,
* start off with those.
*/
strcpy(geometry, geomString);
} else if (geomMask & NoValue) {
/*
* If none have been set (null geometry string???)
* then start with default width and height.
*/
sprintf(geometry,
"%dx%d", DEF_ANALOG_WIDTH, DEF_ANALOG_HEIGHT);
} else {
/*
* One or more things have been set . . .
*
* Check width . . .
*/
if (!(geomMask & WidthValue)) {
width = DEF_ANALOG_WIDTH;
}
sprintf(widthString, "%d", width);
/*
* Check height . . .
*/
if (!(geomMask & HeightValue)) {
height = DEF_ANALOG_HEIGHT;
}
sprintf(heightString, "x%d", height);
/*
* Check x origin . . .
*/
if (!(geomMask & XValue)) {
strcpy(xString, "");
} else {
/* There is an x value - gotta do something with it */
if (geomMask & XNegative) {
sprintf(xString, "-%d", x);
} else {
sprintf(xString, "+%d", x);
}
}
/*
* Check y origin . . .
*/
if (!(geomMask & YValue)) {
strcpy(yString, "");
} else {
/* There is a y value - gotta do something with it */
if (geomMask & YNegative) {
sprintf(yString, "-%d", y);