Debian Codegraph @

clock.cxx

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