Debian Codegraph @

clock.cxx

raw symbols ◀ ▶
Source reconstructed at patch state 1 of 3 — not the installed (fully patched) source.
3 Debian patches touch this file
1 /* clock.cxx
2 $Id: clock.cxx,v 1.28 2001/10/31 04:55:54 elf Exp $
3
4 written by Marc Singer
5 3 April 1997
6
7 This file is part of the project Buici. See the file README for
8 more information.
9
10 Copyright (C) 1997 Marc Singer
11
12 This program is free software; you can redistribute it and/or
13 modify it under the terms of the GNU General Public License as
14 published by the Free Software Foundation; either version 2 of the
15 License, or (at your option) any later version.
16
17 This program is distributed in the hope that it will be useful, but
18 WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 General Public License for more details.
21
22 You should have received a copy of the GNU General Public License
23 in a file called COPYING along with this program; if not, write to
24 the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA
25 02139, USA.
26
27 -----------
28 DESCRIPTION
29 -----------
30
31 ---------
32 Resources
33 ---------
34
35 buiciClock.showSecondHand: {true|false|yes|no|0|1} [true]
36
37 Controls the display of the second hand. Disabling the second
38 hand will not prevent an update every second. Such a feature
39 requires a little more work to determine the position of the
40 hands and re-render when these values change.
41
42
43 */
44
45 /* ----- Includes */
46
47 #include "standard.h"
48 #if defined (STDC_HEADERS)
49 # include <stdarg.h>
50 #endif
51 #include <math.h>
52
53 #if TIME_WITH_SYS_TIME
54 # include <sys/time.h>
55 # include <time.h>
56 #else
57 # if HAVE_SYS_TIME_H
58 # include <sys/time.h>
59 # else
60 # include <time.h>
61 # endif
62 #endif
63
64 #include "version.h"
65
66 #include <X11/Xlib.h>
67 #include <X11/Xutil.h>
68 #include <X11/Xresource.h>
69 #include <X11/extensions/shape.h>
70
71 #include "ldisplay.h"
72 #include "lwindow.h"
73 #include "lres.h"
74 #include "signal.h" // Our signal wrapper
75
76 #include "buici.xbm" // Our name as a bitmap
77
78 // FACE_BACKING uses a pixmap to construct the clock face before
79 // sending the image to the display. It is the only method that
80 // will show the Buici name on the clock. It is considered to be
81 // the best.
82 // BATCH_LINES sends the clock face markings to the server in one
83 // protocol dispatch. This is done for efficiency and is probably the
84 // most frugal use of bandwidth. It doesn't display the Buici name.
85 // <default> sends the face markings one at a time to the server.
86 // Slow, and simple.
87
88 #define USE_FACE_BACKING
89 //#define USE_BATCH_LINES
90
91 //#define USE_LOCAL_GC // Cache our own GC
92
93 extern char* g_szApplication; // Name of application
94 extern char* g_szTimeZone;
95 bool g_fQuit;
96 extern int g_fAsDesktop;
97 extern int g_fAsToolbar;
98 extern int g_fAsDock;
99 extern int g_fOverrideRedirect;
100 extern int g_showSecondHand;
101
102 int do_clock (void);
103 void signal_alarm (LSignalHandle handle, void* pv);
104 int x_after (Display*);
105 time_t calc_delta (time_t);
106
107 void draw_dial (Display* display, Visual* visual,
108 Pixmap pixmap, int dx, int dy);
109 void draw_hands (Display* display, Visual* visual,
110 Pixmap pixmap, int dx, int dy, int seconds,
111 bool showSecondHand);
112 void draw_dial_shape (Display* display, Pixmap pixmap, int dx, int dy);
113
114 class WTopLevel : public LWindow {
115 protected:
116 // bool m_fSharedMemory;
117 // XShmSegmentInfo m_shmInfo;
118 Pixmap m_pixmap; // X drawable for rendering clock face
119 #if defined (USE_FACE_BACKING)
120 Pixmap m_pixmapFace; // X drawable for backing clock face
121 #endif
122 time_t m_seconds; // Last seconds value rendered
123 bool m_fExpose; // Exposure event forces redraw
124
125 int m_width; // Width we report
126 int m_height; // Width we report
127
128 unsigned32 m_pixelGray; // Gray pixel used for anti-aliasing
129
130 #if defined (USE_LOCAL_GC)
131 GC m_gc; // We cache our own GC to speed drawing
132 #endif
133
134 GC gc (void) {
135 #if defined (USE_LOCAL_GC)
136 if (m_gc)
137 return m_gc;
138 XGCValues xgcv;
139 return m_gc = XCreateGC (xdisplay (), window (), 0, &xgcv);
140 #else
141 return display ()->gc ();
142 #endif
143 }
144
145 public:
146 bool m_fSecondHand; // true: display second hand
147
148 WTopLevel (LDisplay* pDisplay) : LWindow (pDisplay) { zero (); }
149 WTopLevel (LWindow* pWindow) : LWindow (pWindow) { zero (); }
150 ~WTopLevel () {
151 release_this (); }
152 void release_this (void);
153 void zero (void) {
154 memset (((LWindow*) this) + 1, 0, sizeof (*this) - sizeof (LWindow)); }
155
156 void allocate_colors (void);
157 void buttondown (XButtonEvent* pEvent);
158 int compute_segment (XSegment* pseg, double theta,
159 float outside, float inside, float width);
160 void draw (void);
161 void expose (XExposeEvent* pEvent);
162 bool is_expose (void) {
163 return m_fExpose; }
164 bool render (void);
165 void render_buici (GC gc, Pixmap pixmap);
166 void render_circle (GC gc, Pixmap pixmap, double theta,
167 float offset, float diameter);
168 void render_line (GC gc, Pixmap pixmap, double theta,
169 float outside, float inside, float width);
170 void setup_time (void);
171 void shape (void);
172
173 Pixmap get_pixmap ();
174 #if defined (USE_FACE_BACKING)
175 Pixmap get_pixmap_face ();
176 #endif
177 int line_width (double r) {
178 int line_width = 1;
179 if (r)
180 line_width = int ((width () - 1.0 + height () - 1.0)/2.0*r);
181 if (!line_width)
182 line_width = 1;
183 return line_width; }
184
185 int width () {
186 return m_width ? m_width
187 : (m_width = ((LWindow::width () + 1)&~1) - 1); }
188 int height () {
189 return m_height ? m_height
190 : (m_height = ((LWindow::height () + 1)&~1) - 1); }
191 };
192
193
194 typedef enum {
195 markLine = 1,
196 markCircle = 2,
197 } eMarkShape;
198
199 typedef struct {
200 int count; // Number of strokes, hands are zero
201 int shape; // Shape of the mark
202 float rInside; // Inside, outside, and stroke width as
203 float rOutside; // fractions of the radius
204 float rWidth; // Zero widths are one pixel
205 int modSeconds; // Modulus for computing hand angle
206 unsigned32 color; // Color (not presently implemented)
207 unsigned32 pixel; // Pixel value returned by server
208 } Mark;
209
210 #define WIDTH_BORDER 0.05
211 #define WIDTH_BUICI 0.22
212 //#define THRESHOLD_BLACK 40
213 //#define THRESHOLD_GRAY 10
214 #define THRESHOLD_BLACK (40)
215 #define THRESHOLD_GRAY (20)
216
217 Mark g_rgMarks[] = {
218 { 60, markLine, 0.82, 0.76, 0, 0, 0x000000 }, // Second marks
219 { 12, markLine, 0.82, 0.62, 0.040, 0, 0x000000 }, // Hour marks
220 { 0, markLine, 0.48, -0.12, 0.030, 43200, 0x000000 }, // Hour hand
221 { 0, markLine, 0.70, -0.12, 0.030, 3600, 0x000000 }, // Minute hand
222 { 0, markLine, 0.58, -0.2, 0, 60, 0xff0000 }, // Second hand
223 { 0, markCircle, 0.58, 0.058, 0, 60, 0xff0000 }, // Second hand
224 { -1 }
225 };
226
227 EventMap g_rgEMTop[] = {
228 { ButtonPress, (PFNEvent) &WTopLevel::buttondown },
229 { Expose, (PFNEvent) &WTopLevel::expose },
230 { 0, NULL },
231 };
232
233
234 /* ----- Methods */
235
236 /* calc_delta
237
238 calculates/caches the timezone delta. It performs some caching
239 checks to make sure it isn't calculated needlessly.
240
241 It computes the delta when timeNow
242
243 */
244
245 time_t calc_delta (time_t timeNow)
246 {
247 static time_t timeLastCalc; // Time delta was last computed
248 static time_t timeDelta; // Last computed delta
249 const int timeRecalc = 60*60; // Interval for recalc, aligned to 12:00:00am
250
251 if (timeNow == 0)
252 time (&timeNow);
253 if (timeNow != timeLastCalc
254 && ( timeNow - timeLastCalc > timeRecalc
255 || (timeNow%timeRecalc) == 0)) {
256 // fprintf (stderr, "recalc delta\n");
257
258 // -- Determine timezone correction
259 struct tm tmGMT = *gmtime (&timeNow);
260 struct tm tmLocal = *localtime (&timeNow);
261 tmGMT.tm_isdst = 0; // We want the absolute difference
262 tmLocal.tm_isdst = 0;
263 timeDelta = mktime (&tmLocal) - mktime (&tmGMT);
264 // fprintf (stderr, "deltaZone is %ld seconds or %d hours\n",
265 // timeDelta, timeDelta/3600);
266 timeLastCalc = timeNow;
267 }
268
269 return timeDelta;
270 }
271
272
273 int round_at (double r, double c, bool fInvert = false)
274 {
275 #if 1
276 if ((!fInvert && r < 0) || (fInvert && r > 0))
277 r -= 0.5;
278 else
279 r += 0.5;
280 return int (r + c);
281 #else
282 return int (r + 0.5 + c);
283 #endif
284 }
285
286 double round_up (double r)
287 {
288 if (r < 0)
289 return double (int (r*1e8 - 5)/10)/1e7;
290 else
291 return double (int (r*1e8 + 5)/10)/1e7;
292 }
293
294
295 inline int point_on_circle (double v, int r)
296 {
297 if (v < 0)
298 v -= 0.5;
299 else
300 v += 0.5;
301 return int (v + r/2.0);
302 }
303
304
305 void exit_error (const char* sz, ...)
306 {
307 fprintf (stderr, "%s: ", g_szApplication);
308
309 va_list ap;
310 va_start (ap, sz);
311 vfprintf (stderr, sz, ap);
312 va_end (ap);
313
314 fprintf (stderr, "\n");
315
316 exit (1);
317 }
318
319
320 int x_after (Display* /* pDisplay */)
321 {
322 static time_t timeStart;
323
324 if (!timeStart)
325 time (&timeStart);
326
327 // fprintf (stderr, "protocol request %ld\n", time (NULL) - timeStart);
328 return 0;
329 }
330
331
332 void WTopLevel::allocate_colors (void)
333 {
334 Colormap colormap = XDefaultColormap (xdisplay (), 0);
335
336 XColor xcolor;
337 memset (&xcolor, 0, sizeof (xcolor));
338
339 for (Mark* pMark = g_rgMarks; pMark->count != -1; ++pMark) {
340 xcolor.red = (((pMark->color >> 16) & 0xff) << 8);
341 xcolor.green = (((pMark->color >> 8) & 0xff) << 8);
342 xcolor.blue = (((pMark->color >> 0) & 0xff) << 8);
343 if (XAllocColor (xdisplay (), colormap, &xcolor))
344 pMark->pixel = xcolor.pixel;
345 else
346 pMark->pixel = XBlackPixel (xdisplay (), 0);
347 }
348
349 xcolor.red = xcolor.green = xcolor.blue = 0x8000;
350 if (XAllocColor (xdisplay (), colormap, &xcolor))
351 m_pixelGray = xcolor.pixel;
352 }
353
354
355 void WTopLevel::buttondown (XButtonEvent* /* pEvent */)
356 {
357 // fprintf (stderr, "click\n");
358 // XUnmapWindow (pEvent->display, pEvent->window);
359 // g_fQuit = true;
360 }
361
362
363 /* WTopLevel::compute_segment
364
365 computes a line segment for clock marks or hands. It is a
366 duplicate of the render_line code that writes the segment
367 information to an array so that the segments may be submitted to
368 the server in one command.
369
370 The outside and inside parameters are real number representing the
371 proportion of the outside and inside points of the line with
372 respect to the radius of the clock face. A value of 0 is the
373 center and a value of 1.0 is the outside edge of the face.
374
375 The theta parameter a number from 0 to 1.0 that describes the
376 angle of the line as a fraction of the circle. Zero is noon or
377 midnight. 0.25 is three o'clock. This routine converts the number
378 of radians for calculation--unless we choose to use a lookup table
379 for angles.
380
381 */
382
383 int WTopLevel::compute_segment (XSegment* pseg, double theta,
384 float outside, float inside,
385 float /*line_width_fraction */)
386 {
387 theta = M_PI_2 - theta*2.0*M_PI;
388 double sin_t = -sin (theta);
389 double cos_t = cos (theta);
390 double rx = (width () - 0)/2.0; // radius along X axis
391 double ry = (height () - 0)/2.0; // radius along Y axis
392
393 if (outside*inside < 0) {
394 pseg->x1 = round_at (outside*rx*cos_t, (width () + 1)/2.0);
395 pseg->y1 = round_at (outside*ry*sin_t, (height () + 1)/2.0);
396 pseg->x2 = (width () + 1)/2;
397 pseg->y2 = (height () + 1)/2;
398 pseg[1].x1 = pseg->x2;
399 pseg[1].y1 = pseg->y2;
400 ++pseg;
401 pseg->x2 = round_at (inside*rx*cos_t, (width () + 1)/2.0, true);
402 pseg->y2 = round_at (inside*ry*sin_t, (height () + 1)/2.0, true);
403 return 2;
404 }
405 else {
406 pseg->x1 = round_at (outside*rx*cos_t, (width () + 1)/2.0);
407 pseg->y1 = round_at (outside*ry*sin_t, (height () + 1)/2.0);
408 pseg->x2 = round_at (inside*rx*cos_t, (width () + 1)/2.0);
409 pseg->y2 = round_at (inside*ry*sin_t, (height () + 1)/2.0);
410 return 1;
411 }
412 }
413
414
415 void WTopLevel::draw (void)
416 {
417 GC _gc = gc ();
418 Pixmap pixmap = get_pixmap ();
419
420 XCopyArea (xdisplay (), pixmap, window (),
421 _gc, 0, 0, width (), height (), 0, 0);
422 m_fExpose = false;
423 }
424
425
426 void WTopLevel::expose (XExposeEvent* /* pEvent */)
427 {
428 m_fExpose = true; // Force redraw next time.
429 }
430
431
432 Pixmap WTopLevel::get_pixmap (void)
433 {
434 if (m_pixmap)
435 return m_pixmap;
436 if (!window ())
437 return 0;
438
439 m_pixmap = XCreatePixmap (xdisplay (), window (),
440 width (), height (), display ()->depth ());
441
442 return m_pixmap;
443 }
444
445 #if defined (USE_FACE_BACKING)
446 Pixmap WTopLevel::get_pixmap_face (void)
447 {
448 if (m_pixmapFace)
449 return m_pixmapFace;
450 if (!window ())
451 return 0;
452
453 m_pixmapFace = XCreatePixmap (xdisplay (), window (),
454 width (), height (), display ()->depth ());
455
456 return m_pixmapFace;
457 }
458 #endif
459
460
461 bool WTopLevel::render (void)
462 {
463 // -- Check time
464 #if 0
465 struct timeval tv;
466 struct timezone tz;
467 memset (&tv, 0, sizeof (tv));
468 memset (&tz, 0, sizeof (tz));
469 gettimeofday (&tv, &tz);
470 time_t seconds = tv.tv_sec - tz.tz_minuteswest*60;
471 if (tz.tz_dsttime == DST_USA)
472 seconds += 3600;
473 #else
474 time_t seconds;
475 time (&seconds);
476 seconds += calc_delta (seconds);
477 #endif
478
479 // Return immediately if there is
480 // nothing new to render.
481 if (seconds == m_seconds)
482 return false;
483 m_seconds = seconds;
484
485 // fprintf (stderr, "rendering\n");
486
487 // -- Do the rendering
488 GC _gc = gc ();
489
490 if (0) {
491 XGCValues values;
492 values.graphics_exposures = False;
493 XChangeGC (display ()->display (), _gc, GCGraphicsExposures, &values);
494 }
495
496 #if defined (USE_FACE_BACKING)
497 if (!m_pixmapFace) {
498 Pixmap pixmap = get_pixmap_face ();
499
500 // printf ("drawing the backing face\n");
501 draw_dial (xdisplay (), xvisual (), pixmap, width (), height ());
502
503 #if 0
504 XSetForeground (xdisplay (), _gc, XWhitePixel (xdisplay (), 0));
505 XFillRectangle (xdisplay (), pixmap, _gc, 0, 0, width (), height ());
506 XSetForeground (xdisplay (), _gc, XBlackPixel (xdisplay (), 0));
507 XSetBackground (xdisplay (), _gc, XWhitePixel (xdisplay (), 0));
508
509 // -- Draw name 'Buici'
510 render_buici (_gc, pixmap);
511
512 // -- Draw perimeter
513 int width_border = line_width (WIDTH_BORDER);
514 XSetLineAttributes (xdisplay (), _gc, width_border,
515 LineSolid, CapButt, JoinMiter);
516 --width_border;
517 XDrawArc (xdisplay (), pixmap, _gc, width_border, width_border,
518 width () - 1 - width_border*2, height () - 1 - width_border*2,
519 0*64, 360*64);
520
521 // -- Draw marks
522 for (Mark* pMark = g_rgMarks; pMark->count != -1; ++pMark) {
523 if (!pMark->count)
524 continue;
525 XSetForeground (xdisplay (), _gc, pMark->pixel);
526 for (int i = 0; i < pMark->count; ++i)
527 render_line (_gc, pixmap, i/float (pMark->count),
528 pMark->rInside, pMark->rOutside, pMark->rWidth);
529 }
530 #endif
531 }
532 else {
533 XSetForeground (xdisplay (), _gc, XBlackPixel (xdisplay (), 0));
534 XSetBackground (xdisplay (), _gc, XWhitePixel (xdisplay (), 0));
535 }
536
537 Pixmap pixmap = get_pixmap ();
538
539 XCopyArea (xdisplay (), get_pixmap_face (), pixmap,
540 _gc, 0, 0, width (), height (), 0, 0);
541
542
543 draw_hands (xdisplay (), xvisual (), pixmap, width (), height (), seconds, m_fSecondHand);
544
545 #if 0
546 // -- Draw hands
547 for (Mark* pMark = g_rgMarks; pMark->count != -1; ++pMark) {
548 if (pMark->count)
549 continue;
550 if (pMark->modSeconds == 60 && !m_fSecondHand)
551 continue;
552 XSetForeground (xdisplay (), _gc, pMark->pixel);
553 switch (pMark->shape) {
554 default:
555 case markLine:
556 render_line (_gc, pixmap, (seconds%pMark->modSeconds)
557 /float (pMark->modSeconds),
558 pMark->rInside, pMark->rOutside, pMark->rWidth);
559 break;
560 case markCircle:
561 render_circle (_gc, pixmap, (seconds%pMark->modSeconds)
562 /float (pMark->modSeconds),
563 pMark->rInside, pMark->rOutside);
564 break;
565 }
566 }
567 #endif
568
569 #else
570
571 Pixmap pixmap = get_pixmap ();
572
573 XSetForeground (xdisplay (), _gc, XWhitePixel (xdisplay (), 0));
574 XFillRectangle (xdisplay (), pixmap, _gc, 0, 0, width (), height ());
575 XSetForeground (xdisplay (), _gc, XBlackPixel (xdisplay (), 0));
576 XSetBackground (xdisplay (), _gc, XWhitePixel (xdisplay (), 0));
577
578 // -- Draw perimeter
579 int width_border = line_width (WIDTH_BORDER);
580 XSetLineAttributes (xdisplay (), _gc, width_border,
581 LineSolid, CapButt, JoinMiter);
582 --width_border;
583 XDrawArc (xdisplay (), pixmap, _gc, width_border, width_border,
584 width () - 1 - width_border*2, height () - 1 - width_border*2,
585 0*64, 360*64);
586
587 # if defined (USE_BATCH_LINES)
588 XSegment rgseg[100]; // FIXME: bad hard limit
589 int iSegment = 0;
590
591 printf ("redrawing the marks batch\n");
592
593 // -- Draw marks
594 for (Mark* pMark = g_rgMarks; pMark->count != -1; ++pMark, iSegment = 0) {
595 if (pMark->count)
596 for (int i = 0; i < pMark->count; ++i)
597 iSegment += compute_segment (&rgseg[iSegment], i/float (pMark->count),
598 pMark->rInside, pMark->rOutside,
599 pMark->rWidth);
600 else {
601 if (pMark->modSeconds == 60 && !m_fSecondHand)
602 continue;
603 iSegment += compute_segment (&rgseg[iSegment],
604 (seconds%pMark->modSeconds)
605 /float (pMark->modSeconds),
606 pMark->rInside, pMark->rOutside,
607 pMark->rWidth);
608 }
609
610 XSetLineAttributes (xdisplay (), _gc, line_width (pMark->rWidth),
611 LineSolid, CapButt, JoinMiter);
612 XDrawSegments (xdisplay (), pixmap, _gc, rgseg, iSegment);
613 }
614
615 # else
616
617 // printf ("redrawing the marks\n");
618
619 // -- Draw marks
620 for (Mark* pMark = g_rgMarks; pMark->count != -1; ++pMark) {
621 if (pMark->count)
622 for (int i = 0; i < pMark->count; ++i)
623 render_line (_gc, pixmap, i/float (pMark->count),
624 pMark->rInside, pMark->rOutside, pMark->rWidth);
625 else {
626 if (pMark->modSeconds == 60 && !m_fSecondHand)
627 continue;
628 render_line (_gc, pixmap, (seconds%pMark->modSeconds)
629 /float (pMark->modSeconds),
630 pMark->rInside, pMark->rOutside, pMark->rWidth);
631 }
632 }
633 # endif
634 #endif
635
636 return true;
637 }
638
639 void WTopLevel::release_this (void)
640 {
641 if (m_pixmap) {
642 XFreePixmap (xdisplay (), m_pixmap);
643 m_pixmap = 0;
644 }
645 #if defined (USE_FACE_BACKING)
646 if (m_pixmapFace) {
647 XFreePixmap (xdisplay (), m_pixmapFace);
648 m_pixmapFace = 0;
649 }
650 #endif
651 }
652
653
654 void WTopLevel::render_buici (GC gc, Pixmap pixmap)
655 {
656 Pixmap pixmapBuici = 0;
657 int dx = line_width (WIDTH_BUICI);
658 int dy = (buici_height*dx + buici_width/2)/buici_width;
659 int x = width ()/2 - dx/2;
660 int y = height ()/3 - dy/2;
661 int cbRowSrc = (buici_width + 7)/8;
662 int cbRowDst = (dx + 7)/8;
663
664 unsigned8* rgb = (unsigned8*) malloc (cbRowDst*dy);
665 unsigned8* rgbGray = (unsigned8*) malloc (cbRowDst*dy);
666 memset (rgb, 0, cbRowDst*dy);
667 memset (rgbGray, 0, cbRowDst*dy);
668 int rowOutput = 0;
669 unsigned8 rgCount[800]; // FIXME: duh
670 int countMax = (buici_width*buici_height)/(dx*dy);
671 // fprintf (stderr, "countMax %d %dx%d -> %dx%d\n", countMax,
672 // buici_width, buici_height, dx, dy);
673 memset (rgCount, 0, sizeof (rgCount));
674 for (int j = 0; j < buici_height; ++j) {
675 for (int i = 0; i < buici_width; ++i) {
676 if (!(buici_bits[j*cbRowSrc + i/8] & (1 << (i % 8))))
677 continue;
678 int x = (i*dx)/buici_width;
679 ++rgCount[x];
680 }
681 int y = ((j + 1)*dy)/buici_height;
682 if (y != rowOutput) {
683 for (int i = 0; i < dx; ++i) {
684 if (rgCount[i] < countMax*THRESHOLD_GRAY/100)
685 continue;
686 rgbGray[rowOutput*cbRowDst + i/8] |= (1 << (i % 8));
687 if (rgCount[i] < countMax*THRESHOLD_BLACK/100)
688 continue;
689 rgb[rowOutput*cbRowDst + i/8] |= (1 << (i % 8));
690 }
691 memset (rgCount, 0, sizeof (rgCount));
692 rowOutput = y;
693 }
694 }
695 // dmalloc_validate (); // Paranoid check
696
697 pixmapBuici = XCreateBitmapFromData (xdisplay (), window (),
698 (const char*) rgbGray, dx, dy);
699 XSetState (xdisplay (), gc, m_pixelGray, XWhitePixel (xdisplay (), 0),
700 GXcopy, AllPlanes);
701 XCopyPlane (xdisplay (), pixmapBuici, pixmap, gc,
702 0, 0, dx, dy, x, y, 0x1);
703 XFreePixmap (xdisplay (), pixmapBuici);
704
705 pixmapBuici = XCreateBitmapFromData (xdisplay (), window (),
706 (const char*) rgb, dx, dy);
707 XSetState (xdisplay (), gc, ~0, 0, GXandInverted, AllPlanes);
708 XCopyPlane (xdisplay (),pixmapBuici, pixmap, gc,
709 0, 0, dx, dy, x, y, 0x1);
710 XSetState (xdisplay (), gc,
711 XBlackPixel (xdisplay (), 0),
712 XWhitePixel (xdisplay (), 0), GXor, AllPlanes);
713 XCopyPlane (xdisplay (),pixmapBuici, pixmap, gc,
714 0, 0, dx, dy, x, y, 0x1);
715 XFreePixmap (xdisplay (), pixmapBuici);
716
717 XSetState (xdisplay (), gc,
718 XBlackPixel (xdisplay (), 0),
719 XWhitePixel (xdisplay (), 0), GXcopy, AllPlanes);
720
721 free (rgb);
722 free (rgbGray);
723 }
724
725
726 /* WTopLevel::render_circle
727
728 renders a circle for clock marks or hands. The offset and diameter
729 parameters are real numbers representing the proportion of the
730 circle center and diameter with respect to the radius of the clock
731 face. An offset value of 0 is the center and a value of 1.0 is the
732 outside edge of the face.
733
734 The theta parameter a number from 0 to 1.0 that describes the
735 angle of the line as a fraction of the circle. Zero is noon or
736 midnight. 0.25 is three o'clock. This routine converts the number
737 of radians for calculation--unless we choose to use a lookup table
738 for angles.
739
740 */
741
742 void WTopLevel::render_circle (GC gc, Pixmap pixmap, double theta,
743 float offset, float diameter)
744 {
745 theta = M_PI_2 - theta*2.0*M_PI;
746 double sin_t = -sin (theta);
747 double cos_t = cos (theta);
748 double rx = (width () - 0)/2.0; // radius along X axis
749 double ry = (height () - 0)/2.0; // radius along Y axis
750 double cx = offset*rx*cos_t;
751 double cy = offset*ry*sin_t;
752 double dx = diameter*width ();
753 double dy = diameter*height ();
754 int x = round_at (cx, (width () + 1)/2.0 - (dx + 0.5)/2.0);
755 int y = round_at (cy, (height () + 1)/2.0 - (dy + 0.5)/2.0);
756
757 XFillArc (xdisplay (), pixmap, gc, x, y,
758 int (dx + 0.5), int (dy + 0.5),
759 0*64, 360*64);
760 }
761
762
763 /* WTopLevel::render_line
764
765 renders a line for clock marks or hands. The outside and inside
766 parameters are real number representing the proportion of the
767 outside and inside points of the line with respect to the radius of
768 the clock face. A value of 0 is the center and a value of 1.0 is
769 the outside edge of the face.
770
771 The theta parameter a number from 0 to 1.0 that describes the
772 angle of the line as a fraction of the circle. Zero is noon or
773 midnight. 0.25 is three o'clock. This routine converts the number
774 of radians for calculation--unless we choose to use a lookup table
775 for angles.
776
777 */
778
779 void WTopLevel::render_line (GC gc, Pixmap pixmap, double theta,
780 float outside, float inside,
781 float line_width_fraction)
782 {
783 theta = M_PI_2 - theta*2.0*M_PI;
784 double sin_t = -sin (theta);
785 double cos_t = cos (theta);
786 double rx = (width () - 0)/2.0; // radius along X axis
787 double ry = (height () - 0)/2.0; // radius along Y axis
788
789 XSetLineAttributes (xdisplay (), gc, line_width (line_width_fraction),
790 LineSolid, CapButt, JoinMiter);
791 if (outside*inside < 0) {
792 XPoint rgPt[3];
793 rgPt[0].x = round_at (outside*rx*cos_t, (width () + 1)/2.0);
794 rgPt[0].y = round_at (outside*ry*sin_t, (height () + 1)/2.0);
795 rgPt[1].x = (width () + 1)/2;
796 rgPt[1].y = (height () + 1)/2;
797 rgPt[2].x = round_at (inside*rx*cos_t, (width () + 1)/2.0);
798 rgPt[2].y = round_at (inside*ry*sin_t, (height () + 1)/2.0);
799 XDrawLines (xdisplay (), pixmap, gc, rgPt, 3, CoordModeOrigin);
800 }
801 else
802 XDrawLine (xdisplay (), pixmap, gc,
803 round_at (outside*rx*cos_t, (width () + 1)/2.0),
804 round_at (outside*ry*sin_t, (height () + 1)/2.0),
805 round_at (inside*rx*cos_t, (width () + 1)/2.0),
806 round_at (inside*ry*sin_t, (height () + 1)/2.0));
807 }
808
809
810 void WTopLevel::setup_time (void)
811 {
812 // -- Configure interval timer
813 LSignal::accept (SIGALRM, signal_alarm, (void*) this, 0, 0);
814 {
815 struct itimerval value;
816 memset (&value, 0, sizeof (value));
817 value.it_interval.tv_sec = 0;
818 value.it_interval.tv_usec = 100000;
819 value.it_value.tv_usec = 1;
820 // value.it_value = value.it_interval;
821 setitimer (ITIMER_REAL, &value, NULL);
822 }
823 }
824
825
826 void WTopLevel::shape (void)
827 {
828 int i;
829 if (!XShapeQueryExtension (xdisplay (), &i, &i))
830 return;
831
832 Pixmap pixmap
833 = XCreatePixmap (xdisplay (), window (), width (), height (), 1);
834
835 #if 0
836 XGCValues xgcv;
837 GC gc = XCreateGC (xdisplay (), pixmap, 0, &xgcv);
838 XSetForeground (xdisplay (), gc, 0);
839 XFillRectangle (xdisplay (), pixmap, gc, 0, 0, width (), height ());
840
841 # if 0
842 XSetForeground (xdisplay (), gc, 1);
843 int width_border = line_width (WIDTH_BORDER);
844 XSetLineAttributes (xdisplay (), gc, width_border,
845 LineSolid, CapRound, JoinMiter);
846 --width_border;
847 XFillArc (xdisplay (), pixmap, gc, width_border, width_border,
848 width () - 1 - width_border*2, height () - 1 - width_border*2,
849 0*64, 360*64);
850 XDrawArc (xdisplay (), pixmap, gc, width_border, width_border,
851 width () - 1 - width_border*2, height () - 1 - width_border*2,
852 0*64, 360*64);
853 # endif
854 XFreeGC (xdisplay (), gc);
855 #else
856 draw_dial_shape (xdisplay (), pixmap, width (), height ());
857 #endif
858
859 XShapeCombineMask (xdisplay (), window (), ShapeBounding, 0, 0,
860 pixmap, ShapeSet);
861 XShapeCombineMask (xdisplay (), window (), ShapeClip, 0, 0,
862 pixmap, ShapeSet);
863 XFreePixmap (xdisplay (), pixmap);
864 }
865
866
867 void register_base_classes (LDisplay* pDisplay)
868 {
869 LWindow* pWindow = new LWindow (pDisplay);
870 pWindow->event_map (g_rgEMTop);
871 pWindow->select_events (ButtonPressMask | ExposureMask);
872 // pWindow->set_background_pixel (XBlackPixel (pDisplay->display (), 0));
873 pWindow->set_bit_gravity (NorthWestGravity);
874 if (g_fOverrideRedirect)
875 pWindow->set_override_redirect (true);
876 // pWindow->notify ((PFNEvent) WTopLevel::child);
877 if (!pDisplay->hash_template ("top-level", pWindow))
878 return;
879
880 // WButton::register_template (pDisplay);
881 // WTextEdit::register_template (pDisplay);
882 // WDialog::register_template (pDisplay);
883 }
884
885
886 int do_clock (void)
887 {
888 extern char* g_szGeometry;
889 int x = 0;
890 int y = 0;
891 unsigned int dx = 100;
892 unsigned int dy = 100;
893
894 if (g_szTimeZone) {
895 setenv("TZ",g_szTimeZone , 1);
896 tzset();
897 }
898
899
900 {
901 LDisplay display;
902 display.set_visual_class (PseudoColor);
903 if (!display.open ())
904 exit_error ("unable to open display");
905
906 register_base_classes (&display);
907 WTopLevel* pWindow = new WTopLevel (display.find_template ("top-level"));
908
909 switch (g_showSecondHand) {
910 default:
911 case -1:
912 {
913 XrmValue value;
914 pWindow->m_fSecondHand =
915 display.find_resource ("buiciClock.showSecondHand",
916 NULL, NULL, &value) ? as_bool (value, true)
917 : true;
918 }
919 break;
920 case 0:
921 pWindow->m_fSecondHand = false;
922 break;
923 case 1:
924 pWindow->m_fSecondHand = true;
925 break;
926 }
927
928 pWindow->position (x, y, dx, dy);
929 pWindow->geometry (g_szGeometry);
930 pWindow->qualify ("buiciClock", "buiciClock\0BuiciClock\0");
931 if (!pWindow->create (0))
932 exit_error ("unable to open window");
933 pWindow->shape ();
934
935 pWindow->transient_for (pWindow);
936 if (g_fAsDesktop)
937 pWindow->net_window_type (windowTypeDesktop);
938 if (g_fAsToolbar)
939 pWindow->net_window_type (windowTypeToolbar);
940 if (g_fAsDock)
941 pWindow->net_window_type (windowTypeDock);
942
943 pWindow->allocate_colors (); // First, create the colormap
944
945 pWindow->setup_time ();
946 pWindow->render ();
947 pWindow->map ();
948
949 // display.set_after_function (x_after);
950
951 // fprintf (stderr, "starting dispatch\n");
952
953 while (!g_fQuit) {
954 display.dispatch_next_event ();
955 }
956
957 fprintf (stderr, "exiting\n");
958
959 pWindow->unmap (); // Make a clean (looking) exit
960 display.flush ();
961
962 delete pWindow;
963 }
964 // dmalloc_exit (); // See what is left allocated
965
966 return 0;
967 }
968
969 void signal_alarm (LSignalHandle /* handle */, void* pv)
970 {
971 // fprintf (stderr, "tick\n");
972 WTopLevel* pWindow = (WTopLevel*) pv;
973 if (pWindow->render () || pWindow->is_expose ()) {
974 pWindow->draw ();
975 pWindow->display ()->flush ();
976 }
977 // fprintf (stderr, "tock\n");
978 }