2 * nxclock - Nano-X clock program
4 * Copyright (C) 2000 by Greg Haerr <greg@censoft.com>
5 * Copyright (C) 1999 Alistair Riddoch <ajr@ecs.soton.ac.uk>
13 #define MWINCLUDECOLORS
16 /* If you need a clock bigger than 200x200 you will need to re-write the trig *
17 * to use longs. (Only applies under ELKS I think. */
18 #define CWIDTH 100 /* Max 200 */
19 #define CHEIGHT 100 /* Max 200 */
22 * Definitions to make it easy to define cursors
24 #define _ ((unsigned) 0) /* off bits */
25 #define X ((unsigned) 1) /* on bits */
26 #define MASK(a,b,c,d,e,f,g) \
27 (((((((((((((a * 2) + b) * 2) + c) * 2) + d) * 2) \
28 + e) * 2) + f) * 2) + g) << 9)
30 static GR_WINDOW_ID w1; /* id for window */
31 static GR_GC_ID gc1; /* graphics context for text */
32 static GR_GC_ID gc2; /* graphics context for rectangle */
33 static int lh = -1, lm = -1, ls = -1;
36 static unsigned char trig[91] =
37 { 0, 4, 8, 13, 17, 22, 26, 31, 35, 40, 44, 48, 53, 57, 61, 66,
38 70, 74, 79, 83, 87, 91, 95, 100, 104, 108, 112, 116, 120, 124, 128,
39 131, 135, 139, 143, 146, 150, 154, 157, 161, 164, 167, 171, 174, 177,
40 181, 184, 187, 190, 193, 196, 198, 201, 204, 207, 209, 212, 214, 217,
41 219, 221, 223, 226, 228, 230, 232, 233, 235, 237, 238, 240, 242, 243,
42 244, 246, 247, 248, 249, 250, 251, 252, 252, 253, 254, 254, 255, 255,
48 void errorcatcher(); /* routine to handle errors */
51 main(int ac, char **av)
53 GR_EVENT event; /* current event */
54 GR_BITMAP bitmap1fg[7]; /* bitmaps for first cursor */
55 GR_BITMAP bitmap1bg[7];
58 fprintf(stderr, "cannot open graphics\n");
64 GR_WM_PROPS_NOAUTOMOVE|GR_WM_PROPS_BORDER|GR_WM_PROPS_CAPTION|
65 GR_WM_PROPS_CLOSEBOX, "nxclock", GR_ROOT_WINDOW_ID,
66 10, 10, CWIDTH, CHEIGHT, GrGetSysColor(GR_COLOR_WINDOW));
68 GrSelectEvents(w1, GR_EVENT_MASK_EXPOSURE | GR_EVENT_MASK_CLOSE_REQ);
73 GrSetGCForeground(gc1, GrGetSysColor(GR_COLOR_WINDOW));
74 GrSetGCBackground(gc1, GrGetSysColor(GR_COLOR_WINDOWTEXT));
75 GrSetGCForeground(gc2, GrGetSysColor(GR_COLOR_WINDOWTEXT));
76 GrSetGCBackground(gc2, GrGetSysColor(GR_COLOR_WINDOW));
78 bitmap1bg[0] = MASK(_,_,X,X,X,_,_);
79 bitmap1bg[1] = MASK(_,X,X,X,X,X,_);
80 bitmap1bg[2] = MASK(X,X,X,X,X,X,X);
81 bitmap1bg[3] = MASK(X,X,X,X,X,X,X);
82 bitmap1bg[4] = MASK(X,X,X,X,X,X,X);
83 bitmap1bg[5] = MASK(_,X,X,X,X,X,_);
84 bitmap1bg[6] = MASK(_,_,X,X,X,_,_);
86 bitmap1fg[0] = MASK(_,_,_,X,_,_,_);
87 bitmap1fg[1] = MASK(_,X,_,X,_,X,_);
88 bitmap1fg[2] = MASK(_,_,_,X,_,_,_);
89 bitmap1fg[3] = MASK(X,_,_,X,X,_,X);
90 bitmap1fg[4] = MASK(_,_,_,_,_,_,_);
91 bitmap1fg[5] = MASK(_,X,_,_,_,X,_);
92 bitmap1fg[6] = MASK(_,_,_,X,_,_,_);
94 GrSetCursor(w1, 7, 7, 3, 3, WHITE, BLACK, bitmap1fg, bitmap1bg);
98 GrGetNextEventTimeout(&event, 500L);
100 switch (event.type) {
101 case GR_EVENT_TYPE_EXPOSURE:
102 do_exposure(&event.exposure);
105 case GR_EVENT_TYPE_CLOSE_REQ:
109 case GR_EVENT_TYPE_TIMEOUT:
120 } else if (angle < 181) {
121 return trig[180 - angle];
122 } else if (angle < 271) {
123 return -trig[angle - 180];
124 } else if (angle < 361) {
125 return -trig[360 - angle];
134 return trig[90 - angle];
135 } else if (angle < 181) {
136 return -trig[angle - 90];
137 } else if (angle < 271) {
138 return -trig[270 - angle];
139 } else if (angle < 361) {
140 return trig[angle - 270];
147 * Here when an exposure event occurs.
151 GR_EVENT_EXPOSURE *ep;
153 GR_COORD midx = CWIDTH / 2;
154 GR_COORD midy = CHEIGHT / 2;
157 /* GrFillRect(w1, gc2, 0, 0, CWIDTH, CHEIGHT); */
158 /* GrFillEllipse(w1, gc1, midx, midy, midx, midy); */
159 GrEllipse(w1, gc2, midx, midy, midx - 1, midy - 1);
160 for(i = 0; i < 60; i++) {
167 midx + (((midx - 3) * bsin(i * 6)) >> 8),
168 midy + (((midy - 3) * bcos(i * 6)) >> 8),
169 midx + (((midx - 3 - l) * bsin(i * 6)) >> 8),
170 midy + (((midy - 3 - l) * bcos(i * 6)) >> 8));
173 lh = -1; lm = -1; ls = -1;
178 void draw_time(int hour, int minutes, int sec, GR_GC_ID gc )
180 GR_COORD midx = CWIDTH / 2;
181 GR_COORD midy = CHEIGHT / 2;
184 midx + (((midx - 8 - midx / 10) * bsin(ls)) >> 8),
185 midy - (((midy - 8 - midy / 10) * bcos(ls)) >> 8),
186 midx + (((midx - 8 - midx / 4) * bsin(ls)) >> 8),
187 midy - (((midy - 8 - midx / 4) * bcos(ls)) >> 8));
189 midx + (((midx - 8 - midx / 10) * bsin(sec)) >> 8),
190 midy - (((midy - 8 - midy / 10) * bcos(sec)) >> 8),
191 midx + (((midx - 8 - midx / 4) * bsin(sec)) >> 8),
192 midy - (((midy - 8 - midx / 4) * bcos(sec)) >> 8));
193 if ((lm != minutes) || (ls == minutes)) {
195 midx + (((midx - 8 - midx / 10) * bsin(lm)) >> 8),
196 midy - (((midy - 8 - midy / 10) * bcos(lm)) >> 8),
197 midx + (((midx / 5) * bsin(lm)) >> 8),
198 midy - (((midy / 5) * bcos(lm)) >> 8));
200 midx + (((midx - 8 - midx / 10) * bsin(minutes)) >> 8),
201 midy - (((midy - 8 - midy / 10) * bcos(minutes)) >> 8),
202 midx + (((midx / 5) * bsin(minutes)) >> 8),
203 midy - (((midy / 5) * bcos(minutes)) >> 8));
205 midx + (((midx - 8 - midx / 2) * bsin(lh)) >> 8),
206 midy - (((midy - 8 - midy / 2) * bcos(lh)) >> 8),
207 midx + (((midx / 5) * bsin(lh)) >> 8),
208 midy - (((midy / 5) * bcos(lh)) >> 8));
210 midx + (((midx - 8 - midx / 2) * bsin(hour)) >> 8),
211 midy - (((midy - 8 - midy / 2) * bcos(hour)) >> 8),
212 midx + (((midx / 5) * bsin(hour)) >> 8),
213 midy - (((midy / 5) * bcos(hour)) >> 8));
221 * Update the clock if the seconds have changed.
230 int minutes, hour, sec;
232 gettimeofday(&tv, &tz);
233 now = tv.tv_sec - (60 * tz.tz_minuteswest);
239 minutes = tp->tm_min * 6;
240 sec = tp->tm_sec * 6;
245 hour = hour*30 + minutes/12;
246 draw_time(hour, minutes, sec, gc2);
251 * Sleep a while to avoid using too much CPU time.
255 struct timespec idletime;
257 idletime.tv_nsec = 100000;
258 nanosleep(&idletime, NULL);