Debian Codegraph @

clock.cxx

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