File: SwissWatch.c

package info (click to toggle)
swisswatch 0.6-8
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 160 kB
  • ctags: 307
  • sloc: ansic: 1,511; makefile: 50
file content (534 lines) | stat: -rw-r--r-- 14,673 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
/*****************************************************************************
 * File Name:	 SwissWatch.c
 * Description:	 Swisswatch widget class -- implementation
 * Author:	 Simon Leinen (simon@liasun5)
 * Date Created:  6-May-92
 * RCS $Header$	 
 * RCS $Log$	 
 ****************************************************************************/

#include <X11/IntrinsicP.h>
#include <X11/StringDefs.h>
#include <X11/Xmu/Xmu.h>
#include <X11/Xos.h>
#ifdef SHAPE
#include <X11/extensions/shape.h>
#endif /* SHAPE */
#include "SwissWatcP.h"
#include "HandOrMarP.h"
#include "Hand.h"
#include "Mark.h"
#include <math.h>
#include <time.h>

#if NeedFunctionPrototypes
static void Initialize(Widget,Widget,ArgList,Cardinal*);
static void Realize(Widget,XtValueMask*,XSetWindowAttributes*);
static void Redisplay(Widget,XEvent*,Region);
static void Resize(Widget);
static void Destroy(Widget);
static Boolean SetValues(Widget, Widget, Widget, ArgList, Cardinal *);
static void ClassInitialize(void);
static void convert_string_to_widget_class(XrmValue*,Cardinal*,XrmValue*,XrmValue*);
static void adapt_size(SwissWatchWidget);
static void tock(SwissWatchWidget,int);
static void tick(XtPointer,XtIntervalId *);
static void drawhands(SwissWatchWidget);
static void drawmarks(SwissWatchWidget);
static void destroy_gcs(SwissWatchWidget);
static void get_gcs(SwissWatchWidget);
#else /* not NeedFunctionPrototypes */
static void Initialize();
static void Realize();
static void Redisplay();
static void Resize();
static void Destroy();
static Boolean SetValues();
static void ClassInitialize();
static void convert_string_to_widget_class();
static void adapt_size();
static void tick();
static void tock();
static void drawhands();
static void drawmarks();
static void destroy_gcs();
static void get_gcs();
#endif /* not NeedFunctionPrototypes */

#if !defined(__STDC__)
extern int sprintf();
extern int gettimeofday();
#endif /* __STDC__ */

#define offset(slot) XtOffsetOf(SwissWatchRec,swiss_watch.slot)

static XtResource resources[] =
{
  {XtNnChildren, XtCNChildren, XtRInt,sizeof(Cardinal),
     offset(n_children),
     XtRImmediate, (XtPointer)0},
  {XtNtickTime, XtCTickTime, XtRFloat,sizeof(float),
     offset(tick_time),
     XtRString, (XtPointer)"2.0"},
#ifdef SHAPE
  {XtNshapeWindow,XtCShapeWindow,XtRBoolean,sizeof(Boolean),
     offset(shape_window),XtRString,"true"},
#endif
  {XtNcircular,XtCCircular,XtRBoolean,sizeof(Boolean),
     offset(circular),XtRString,"false"},
  {XtNrailroad,XtCRailroad,XtRBoolean,sizeof(Boolean),
     offset(swiss_mode),XtRString,"false"},
};

#undef offset

static CompositeClassExtensionRec extension_rec = {
    /* next_extension */  NULL,
    /* record_type */     NULLQUARK,
    /* version */         XtCompositeExtensionVersion,
    /* record_size */     sizeof(CompositeClassExtensionRec),
    /* accepts_objects */ TRUE,
};

#define superclass (&compositeClassRec)

SwissWatchClassRec swissWatchClassRec = {
  { /******* CorePart *******/
    /* superclass	    */	(WidgetClass) superclass,
    /* class_name	    */	"SwissWatch",
    /* widget_size	    */	sizeof(SwissWatchRec),
    /* class_initialize     */  ClassInitialize,
    /* class_part_initialize*/	NULL,
    /* class_inited	    */	FALSE,
    /* initialize	    */	Initialize,
    /* initialize_hook      */	NULL,		
    /* realize		    */	Realize,
    /* actions		    */	NULL,
    /* num_actions	    */	0,
    /* resources	    */	resources,
    /* num_resources	    */	XtNumber(resources),
    /* xrm_class	    */	NULLQUARK,
    /* compress_motion      */	FALSE,
    /* compress_exposure    */	TRUE,
    /* compress_enterleave  */  FALSE,
    /* visible_interest     */	FALSE,
    /* destroy		    */	Destroy,
    /* resize		    */	Resize,
    /* expose		    */	Redisplay,
    /* set_values	    */	SetValues,
    /* set_values_hook      */	NULL,			
    /* set_values_almost    */	XtInheritSetValuesAlmost,  
    /* get_values_hook      */	NULL,			
    /* accept_focus	    */	NULL,
    /* version		    */	XtVersion,
    /* callback_offsets     */  NULL,
    /* tm_table		    */  NULL,
    /* query_geometry	    */  XtInheritQueryGeometry,
    /* display_accelerator  */	XtInheritDisplayAccelerator,
    /* extension	    */  NULL
  },
  { /**** CompositePart *****/
    /* geometry_handler     */  XtInheritGeometryManager,
    /* change_managed       */  XtInheritChangeManaged,
    /* insert_child	    */  XtInheritInsertChild,
    /* delete_child	    */  XtInheritDeleteChild,
    /* extension	    */  (XtPointer) & extension_rec,
    },
  {
    /* dummy		    */	0,
  }
};

#define WIDTH(widget) ((widget)->core.width)
#define HEIGHT(widget) ((widget)->core.height)
#define RAD_X(w)	((w)->swiss_watch.rad_x)
#define RAD_Y(w)	((w)->swiss_watch.rad_y)
#define CX(w)		((w)->swiss_watch.cx)
#define CY(w)		((w)->swiss_watch.cy)

WidgetClass swissWatchWidgetClass = (WidgetClass) &swissWatchClassRec;

typedef struct _ChildResources
{
  WidgetClass		class;
}
ChildResources;

static XtResource child_resources[] =
{
  {XtNclass, XtCClass, XtRWidgetClass,sizeof(WidgetClass),
     XtOffsetOf(ChildResources,class),
     XtRString, (XtPointer)"mark"}
};

static void
Initialize(req, new, args, num_args)
     Widget req;
     Widget new;
     ArgList args;
     Cardinal * num_args;
{
#define w_new ((SwissWatchWidget) new)
  unsigned k;
  char child_name[12];
  ChildResources resource_struct;

  for (k = 0; k < w_new->swiss_watch.n_children; ++k)
    {
      sprintf(child_name, "child_%d", k);
      XtGetSubresources(req, & resource_struct,
			child_name, "Child",
			child_resources, XtNumber(child_resources),
			NULL, 0);
      XtVaCreateManagedWidget(child_name, resource_struct.class,
			      new, NULL);
    }
#ifdef SHAPE
  {
    int shape_event_base, shape_error_base;
    if (w_new->swiss_watch.shape_window && !XShapeQueryExtension(XtDisplay(new), 
						       &shape_event_base, 
						       &shape_error_base))
      w_new->swiss_watch.shape_window = False;
    w_new->swiss_watch.shape_mask = 0;
    w_new->swiss_watch.shape_gc = 0;
    w_new->swiss_watch.shape_width = 0;
    w_new->swiss_watch.shape_height = 0;
  }
#endif				/* SHAPE */
#undef w_new
}

static void
ClassInitialize()
{
  XtAddConverter(XtRString, XtRWidgetClass, convert_string_to_widget_class, NULL, 0);
}

#define ForAllChildren(w, child) \
  for ( (child) = (Widget *) (w)->composite.children ; \
        (child) < (Widget *) ( (w)->composite.children + \
				 (w)->composite.num_children ) ; \
        (child)++ )


static void
Redisplay(w, event, region)
     Widget w;
     XEvent * event;
     Region region;
{
#define ww ((SwissWatchWidget) w)
#define ev ((XExposeEvent *) event)
  if (event==0)
    XCopyArea(XtDisplay(w),ww->swiss_watch.pic,XtWindow(w),ww->swiss_watch.bg_gc,
	      0,0,w->core.width,w->core.height,0,0);
  else
    XCopyArea(XtDisplay(w),ww->swiss_watch.pic,XtWindow(w),ww->swiss_watch.bg_gc,
	      ev->x,ev->y,ev->width,ev->height,ev->x,ev->y);
#undef ev
#undef ww
}

static void
convert_string_to_widget_class(args, num_args, from, to)
     XrmValue* args;
     Cardinal* num_args;
     XrmValue* from;
     XrmValue* to;
{
  char lower_name[1000];
  to->size = sizeof(WidgetClass);
  XmuCopyISOLatin1Lowered(lower_name, (char *) from->addr);
  if (!strcmp(lower_name, "hand"))
    { to->addr = (XtPointer) & handWidgetClass; return; }
  if (!strcmp(lower_name, "mark"))
    { to->addr = (XtPointer) & markWidgetClass; return; }
  to->size = 0;
  to->addr = NULL;
}

static void
Realize(w, mask, attributes)
     Widget w;
     XtValueMask* mask;
     XSetWindowAttributes* attributes;
{
#define ww ((SwissWatchWidget) w)

  Widget * childp;
  (*superclass->core_class.realize) (w, mask, attributes);
  get_gcs(ww);
  tock(ww,TRUE);
  adapt_size(ww);
  ForAllChildren(ww, childp)
    {
      RealizeHandOrMark((HandOrMarkWidget) (* childp));
    }
#undef ww
}

static void
tock(w, init_p)
     SwissWatchWidget w;
     int init_p;
{
  double now;
  double next;
  double tick_time = w->swiss_watch.tick_time;

  struct tm * tm_now;
  struct timeval tvnow;

  gettimeofday(& tvnow, (struct timezone *) 0);
  tm_now = localtime(&tvnow.tv_sec);
#if HAVE_GMTOFF
  w->swiss_watch.now_local_sec = tvnow.tv_sec+tm_now->tm_gmtoff;
#else /* not HAVE_GMTOFF */
  w->swiss_watch.now_local_sec
    = tm_now->tm_sec+tm_now->tm_min*60+tm_now->tm_hour*3600+tm_now->tm_mday*86400;
  w->swiss_watch.frac_sec = tvnow.tv_usec*1e-6;
#endif /* not HAVE_GMTOFF */

  now = tvnow.tv_sec+1e-6*tvnow.tv_usec;
  if (init_p)
    {
      int int_tick = (int) tick_time;

      if (int_tick==0)
	int_tick = 1;
      if (int_tick > 1)
	{
	  int next_sec = ((tm_now->tm_sec + int_tick) / int_tick);
	  next_sec *= int_tick;
	  next = tvnow.tv_sec + next_sec - tm_now->tm_sec;
	}
      else
	next = (tvnow.tv_sec/int_tick+1)*int_tick;
    }
  else
    {
      next = w->swiss_watch.expected+tick_time;
      while(next < now)
	next += tick_time;
    }
  w->swiss_watch.expected = next;
#ifdef DEBUG
  printf("%8f %8f %6d\n", tick_time, next-now, (int) ((next-now)*1000));
#endif /* 0 */
  XtAppAddTimeOut(XtWidgetToApplicationContext((Widget)w),
		  (int) ((next-now)*1000),
		  tick,
		  (XtPointer)w);
}

static void
tick(closure, interval)
     XtPointer closure;
     XtIntervalId * interval;
{
  tock((SwissWatchWidget) closure, FALSE);
  drawhands((SwissWatchWidget) closure);
  Redisplay((Widget) closure, (XEvent*) 0, (Region) 0);
}

static void
Resize(w)
     Widget w;
{
#define ww ((SwissWatchWidget) w)
  Display * display = XtDisplay(w);

  if (XtIsRealized (w))
    {
      XFreePixmap(display,ww->swiss_watch.mark_pic);
      XFreePixmap(display,ww->swiss_watch.pic);
      adapt_size(ww);
    }
#undef ww
}

static void
adapt_size(w)
     SwissWatchWidget w;
{
  Display * display = XtDisplay(w);
  int depth = DefaultDepthOfScreen(XtScreen(w));
  Widget * childp;
  double scale;

  w->swiss_watch.mark_pic = XCreatePixmap(display,XtWindow(w),WIDTH(w),HEIGHT(w),depth);
  w->swiss_watch.pic = XCreatePixmap(display,XtWindow(w),WIDTH(w),HEIGHT(w),depth);
  w->swiss_watch.cx = WIDTH(w)/2;
  w->swiss_watch.cy = HEIGHT(w)/2;
  if (w->swiss_watch.circular)
    w->swiss_watch.rad_x = w->swiss_watch.rad_y = scale
      = (CX(w) < CY(w)) ? CX(w) : CY(w);
  else
    w->swiss_watch.rad_x = CX(w), w->swiss_watch.rad_y = CY(w),
    scale = sqrt((double) (CX(w)*CY(w)));
#ifdef SHAPE
  if (w->swiss_watch.shape_window)
    {
      Pixmap shape_mask;
      XGCValues xgcv;
      int x;
      int y;
      Widget parent;

      shape_mask = XCreatePixmap (XtDisplay (w), XtWindow (w),
				  WIDTH(w), HEIGHT(w), 1);
      if (!w->swiss_watch.shape_gc)
	w->swiss_watch.shape_gc = XCreateGC (XtDisplay((Widget) w), shape_mask, 0, &xgcv);

      /* erase the pixmap */
      XSetForeground (XtDisplay (w), w->swiss_watch.shape_gc, 0);
      XFillRectangle (XtDisplay (w), shape_mask, w->swiss_watch.shape_gc,
		      0, 0, WIDTH(w), HEIGHT(w));
      XSetForeground (XtDisplay (w), w->swiss_watch.shape_gc, 1);
      XFillArc (XtDisplay (w), shape_mask,
		w->swiss_watch.shape_gc,
		CX(w)-RAD_X(w),CY(w)-RAD_Y(w),
		2*RAD_X(w),2*RAD_Y(w),
		0,360*64);
      /* Find the highest enclosing widget and shape it */
      x = 0;
      y = 0;
      for (parent = (Widget) w; XtParent (parent); parent = XtParent (parent))
	{
	  x = x + parent->core.x + parent->core.border_width;
	  y = y + parent->core.y + parent->core.border_width;
	}

      XShapeCombineMask (XtDisplay (parent), XtWindow (parent), ShapeBounding,
			 x, y, shape_mask, ShapeSet);

      /* erase the pixmap */
      XSetForeground (XtDisplay (w), w->swiss_watch.shape_gc, 0);
      XFillRectangle (XtDisplay (w), shape_mask, w->swiss_watch.shape_gc,
		      0, 0, WIDTH(w), HEIGHT(w));
      XSetForeground (XtDisplay (w), w->swiss_watch.shape_gc, 1);
      /* draw the clip shape */
      XFillArc (XtDisplay (w),shape_mask,w->swiss_watch.shape_gc,
		CX(w)-RAD_X(w),CY(w)-RAD_Y(w),
		2*RAD_X(w),2*RAD_Y(w),
		0,360*64);
      XShapeCombineMask (XtDisplay (w), XtWindow (w), ShapeClip, 
			 0, 0, shape_mask, ShapeSet);
      XFreePixmap (XtDisplay (w), shape_mask);
    }
#endif /* SHAPE */
  ForAllChildren(w, childp)
    {
      ResizeHandOrMark((HandOrMarkWidget) (* childp), RAD_X(w), RAD_Y(w));
    }
  if (XtIsRealized((Widget) w))
    {
      drawmarks(w);
      drawhands(w);
    }
}

static void
Destroy(w)
     Widget w;
{
#define ww ((SwissWatchWidget) w)
#ifdef SHAPE
  destroy_gcs(ww);
  XFreePixmap(XtDisplay(w), ww->swiss_watch.shape_mask);
#endif /* SHAPE */
  XtReleaseGC(w, ww->swiss_watch.bg_gc);
  XFreePixmap(XtDisplay(w), ww->swiss_watch.mark_pic);
  XFreePixmap(XtDisplay(w), ww->swiss_watch.pic);
  if (ww->swiss_watch.timeout)
    XtRemoveTimeOut(ww->swiss_watch.timeout);
#undef ww
}

static void
drawhands(ww)
     SwissWatchWidget ww;
{
  Widget * childp;
#define w ((Widget) ww)
  XCopyArea(XtDisplay(w), ww->swiss_watch.mark_pic,ww->swiss_watch.pic,ww->swiss_watch.bg_gc,
	    0,0,WIDTH(w),HEIGHT(w),0,0);
  ForAllChildren(ww, childp)
    {
      if (XtIsSubclass(*childp, handWidgetClass))
	DrawHandOrMark((HandOrMarkWidget)*childp, ww->swiss_watch.pic);
    }
#undef w
}

static void
drawmarks(ww)
     SwissWatchWidget ww;
{
  Widget * childp;
#define w ((Widget) ww)
  XFillRectangle(XtDisplay(w),ww->swiss_watch.mark_pic,ww->swiss_watch.bg_gc,
		 0,0,ww->core.width,ww->core.height);
  ForAllChildren(ww, childp)
    {
      if (XtIsSubclass(*childp, markWidgetClass))
	DrawHandOrMark((HandOrMarkWidget)*childp, ww->swiss_watch.mark_pic);
    }
#undef w
}

static Boolean
SetValues(current, request, new, args, num_args)
     Widget current;
     Widget request;
     Widget new;
     ArgList args;
     Cardinal * num_args;
{
#define w_cur ((SwissWatchWidget) current)
#define w_new ((SwissWatchWidget) new)
#define CHANGED(FIELD) (w_cur->FIELD != w_new->FIELD)
  Boolean needs_redisplay = FALSE;
  Boolean needs_reset = FALSE;
  Boolean needs_new_gcs = FALSE;

  if (CHANGED (core.background_pixel))
    needs_redisplay = needs_reset = needs_new_gcs = TRUE;

  if (needs_new_gcs && XtIsRealized (current))
    {
      destroy_gcs(w_cur);
      get_gcs(w_new);
    }
  if (needs_reset)
    SwissWatchReset(w_new);
  return needs_redisplay;
#undef CHANGED
#undef w_cur
#undef w_new
}

static void
destroy_gcs(w)
     SwissWatchWidget w;
{
  XtReleaseGC((Widget) w, w->swiss_watch.shape_gc);
}

static void
get_gcs(w)
     SwissWatchWidget w;
{
  XGCValues values;
  values.foreground = w->core.background_pixel;
  w->swiss_watch.bg_gc = XtGetGC ((Widget) w, GCForeground, & values);
}

extern void
SwissWatchReset(w)
     SwissWatchWidget w;
{
  drawmarks(w);
  drawhands(w);
}