Debian Codegraph @

clock.cxx

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