File: display_list.c

package info (click to toggle)
coolmail 1.3-15
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 312 kB
  • sloc: ansic: 1,527; makefile: 69
file content (499 lines) | stat: -rw-r--r-- 16,191 bytes parent folder | download | duplicates (7)
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
/* display_list.c
 *
 * By Byron C. Darrah
 * 4/28/94, Thursday
 *
 * This module is for actually drawing a prepared list of polygons into a
 * window on a screen.
 */
/* Added Sound support: Randall K. Sharpe 09-01-95 */

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include <ctype.h>

#include <X11/StringDefs.h>
#include <X11/Intrinsic.h>

/*   #include <X11/Xmu/Xmu.h>
     #include <X11/Xmu/WidgetNode.h>
*/

#define DISPLAY_LIST_MODULE

#include "colors.h"
#include "../../display_list.h"

#define DEFAULT_GFX_WIDTH  100
#define DEFAULT_GFX_HEIGHT 125

/* The origin is an offset from (0, 0).  If the default width and
 * height are not used, the default origin will automatically
 * be adjusted as well.
 */
#define DEFAULT_X_ORIGIN    52
#define DEFAULT_Y_ORIGIN    40


extern char command_str[];  /* Hack to let coolmail use X resource mgr */
extern char mailfile_str[]; /* to obtain the command and mail file.    */
extern unsigned int frames;

#ifdef AUDIO
extern char *sndfile; /* same hack as above */
extern int  cool_vol;	
void audio_beep(void);
#endif


unsigned      long color_table[NUMCOLORS];
int           color_mode = 1;
Widget        toplevel, main_gfx_w;
Pixmap        draw_pixbuff, refresh_pixbuff;
GC            main_gfx_gc;
XtAppContext  xtcontext;
char          TimedOut = TRUE;
char          Resized  = FALSE;
char          Selected = FALSE;
const char   *timeout_property = "TimerExpiration";
Atom          xa_timeout;

void disp_expose_handler(Widget w, caddr_t client_data, XEvent *event);
void disp_config_handler(Widget w, caddr_t client_data, XEvent *event);
void disp_select_handler(Widget w, caddr_t client_data, XEvent *event);
void disp_nomask_handler(Widget w, caddr_t client_data, XEvent *event);
void disp_timeout_handler(XtPointer client_data, XtIntervalId *id);
void disp_refresh(void);
void disp_config(void);
int put_points(polygon_t *cur_poly);
int disp_checkgc(char *str);

/*---------------------------------------------------------------------------*/
/*--------------------------- EXPORTED FUNCTIONS ----------------------------*/
/*---------------------------------------------------------------------------*/

/* disp_init initializes the graphics output window and the drawing
 * area buffer (ie: scratch pixmap)
 */

void disp_init(int *argc, char *argv[], int color)
{
   Arg       wargs[2];
   XGCValues gcv;

   /* Initialize the X Intrinsics */

   toplevel = XtInitialize(argv[0], "Coolmail", NULL, 0, argc, argv);

   disp_config();  /* Make use of X Resources, if any */

   /* Create a widgit so we can draw in it's window. */

   /* First, set arguments for the size of the widget */
   XtSetArg(wargs[0], XtNwidth,  (Dimension) DEFAULT_GFX_WIDTH);
   XtSetArg(wargs[1], XtNheight, (Dimension) DEFAULT_GFX_HEIGHT);

   /* Now create the widget -- Any plain old widget will do, so use
    * a regular "widgetClass" widget.
    */
   main_gfx_w = XtCreateManagedWidget("picture",
                                     widgetClass,
                                     toplevel, wargs, 2);

   /* Realize the widget tree */
   XtRealizeWidget(toplevel);

   /* Now find out what geometry was ACTUALLY used */
   disp_getsize(&Gfx_Width, &Gfx_Height);

   /* Set size variables and allocate memory for drawing */
   disp_setsize(Gfx_Width, Gfx_Height);

   /* Allocate a bunch of colors */
   if(color == 0) color_mode = 0;
   init_colors(XtDisplay(main_gfx_w), color_table, color_mode);


   /* Create the graphics context for the main_gfx widget */
   main_gfx_gc = XCreateGC(XtDisplay(main_gfx_w),
                           XtWindow(main_gfx_w), 0, &gcv);
   XSetFillRule(XtDisplay(main_gfx_w), main_gfx_gc, EvenOddRule);

   /* Register event handlers to handle exposures, resizes, and
    * mouse clicks. */
   XtAddEventHandler(main_gfx_w, ExposureMask, FALSE,
                     (XtEventHandler)disp_expose_handler, NULL);
   XtAddEventHandler(main_gfx_w, StructureNotifyMask, FALSE,
                     (XtEventHandler)disp_config_handler, NULL);
   XtAddEventHandler(main_gfx_w, ButtonPressMask, FALSE,
                     (XtEventHandler)disp_select_handler, NULL);
   XtAddEventHandler(main_gfx_w, NoEventMask, True,
                     (XtEventHandler)disp_nomask_handler, NULL);
   xa_timeout = XInternAtom(XtDisplay(main_gfx_w), timeout_property, FALSE);
}

/*---------------------------------------------------------------------------*/

/* Hack to use the resource manager to optionally set the value of
 * some variables that we're not really supposed to know about */

void disp_config(void)
{
   char *str;
   Boolean mono = False;

   XtResource mailcmd  = {"mailCommand", "MailCommand", XtRString,
                          sizeof(str), 0, XtRString, command_str};
   XtResource mailfile = {"inbox", "Inbox", XtRString,
                          sizeof(str), 0, XtRString, mailfile_str};
   XtResource monoflag = {"monochrome", "Monochrome", XtRBoolean,
                          sizeof(mono), 0, XtRBoolean, &mono};
   XtResource frmcount = {"frameCount", "FrameCount", XtRInt,
                          sizeof(frames), 0, XtRInt, &frames};

#ifdef AUDIO
   XtResource soundfile  = {"soundFile", "SoundFile", XtRString,
                            sizeof(str), 0, XtRString, sndfile};
   XtResource volume   = {"volume", "Volume", XtRInt,
                          sizeof(cool_vol), 0, XtRInt, &cool_vol};
#endif

   XtGetApplicationResources(toplevel, &str, &mailcmd, 1, NULL, 0);
   strcpy(command_str, str);
   XtGetApplicationResources(toplevel, &str, &mailfile, 1, NULL, 0);
   strcpy(mailfile_str, str);
   XtGetApplicationResources(toplevel, &mono, &monoflag, 1, NULL, 0);
   if (mono) color_mode = 0;
   
   XtGetApplicationResources(toplevel, &frames, &frmcount, 1, NULL, 0);
   if (frames < 2)
      frames = 2;
   
#ifdef AUDIO
   XtGetApplicationResources(toplevel, &str, &soundfile, 1, NULL, 0);
   if(str)
      sndfile = (char *)strdup(str);

   XtGetApplicationResources(toplevel, &cool_vol, &volume, 1, NULL, 0);
   if      (cool_vol < 0)   cool_vol = 0;
   else if (cool_vol > 100) cool_vol = 100;
#endif

}

/*---------------------------------------------------------------------------*/

/* This can be filled in later -- for Unix systems, it is not crucial to
 * deallocate everything explicitly before exitting, since the OS will
 * reclaim resources automatically.
 */

void disp_end(void)
{
   ;
}

/*---------------------------------------------------------------------------*/

/* This is the exported function that is called to actually display a list of
 * polygons.
 */

void disp_polygons(polygon_node_t *first_pnode)
{
   polygon_node_t *curnode;
   polygon_t *cur_poly;


   /* Clear the drawing area before painting into it */
   XSetForeground(XtDisplay(main_gfx_w), main_gfx_gc,
                  BlackPixelOfScreen(XtScreen(main_gfx_w)));
   XFillRectangle(XtDisplay(main_gfx_w), draw_pixbuff, main_gfx_gc,
                  0, 0, Gfx_Width, Gfx_Height);

   /* Now draw each polygon */
   for(curnode = first_pnode; curnode; curnode = curnode->next)
   {
      cur_poly = curnode->polygon;

      /* Skip all polygons not in front of the focus (origin) */
      if (curnode->k <= 0.0) break;

      XSetForeground(XtDisplay(main_gfx_w), main_gfx_gc,
                     color_table[cur_poly->color]);

      XFillPolygon(XtDisplay(main_gfx_w), draw_pixbuff, main_gfx_gc,
                   (XPoint *)(cur_poly->int_points), cur_poly->npoints, Convex,
                   CoordModeOrigin);

      if(!color_mode)  /* Draw the outline of the polygon */
      {
         XSetForeground(XtDisplay(main_gfx_w), main_gfx_gc,
                        BlackPixelOfScreen(XtScreen(main_gfx_w)));
         XDrawLines(XtDisplay(main_gfx_w), draw_pixbuff, main_gfx_gc,
                   (XPoint *)(cur_poly->int_points), cur_poly->npoints,
                   CoordModeOrigin);
         XDrawLine(XtDisplay(main_gfx_w), draw_pixbuff, main_gfx_gc,
                   (int)(cur_poly->int_points[0].x),
                   (int)(cur_poly->int_points[0].y),
                   (int)(cur_poly->int_points[cur_poly->npoints-1].x),
                   (int)(cur_poly->int_points[cur_poly->npoints-1].y));
      }
   }

   /* Copy the drawing from the drawing buffer to the refresh buffer */
   XCopyArea(XtDisplay(main_gfx_w), draw_pixbuff, refresh_pixbuff,
             main_gfx_gc, 0, 0, Gfx_Width, Gfx_Height, 0, 0);

   disp_refresh();
}

/*---------------------------------------------------------------------------*/

/* Pause execution until graphics processing has caught up. */

void disp_sync(void)
{
   XSync(XtDisplay(main_gfx_w), 0);
}

/*---------------------------------------------------------------------------*/

/* This function is called to freeze the display.
 * It returns when either: (1) the specified interval (in milliseconds)
 * has expired, (2) the display area is selected with the mouse, or
 * (3) the window is resized.  Note that another timer even is not requested
 * if an earlier requesten one has not yet occurred.
 */

int disp_freeze(unsigned long interval)
{
   XEvent event;

   if (TimedOut && interval > 0)  /* Have at most one TimeOut at a time */
   {
      TimedOut = FALSE;  /* Critical - do not move this out of the `if' */
      XtAddTimeOut(interval, disp_timeout_handler, NULL);
   }

   /* Enter the endless event loop */
   while (!((interval > 0 && TimedOut) || Selected || Resized))
   {
      XtNextEvent(&event);

      switch (event.type)
      {
       case ClientMessage:
         disp_nomask_handler(main_gfx_w, NULL, &event);
         break;
       default:
         XtDispatchEvent(&event);
      }
   }
   if      (interval > 0 && TimedOut)    return (TIMEOUT);
   else if (Resized)  {Resized  = FALSE; return (RESIZE);}
   else               {Selected = FALSE; return (SELECTION);}
}
/*---------------------------------------------------------------------------*/

void disp_bell(void)
{
/* HERE change this to go along with beep */
#ifdef AUDIO
   if (sndfile)
      /* play the soundfile */
      audio_beep();
   else	/* no sound file chosen then use system bell */
      XBell(XtDisplay(main_gfx_w), (2*cool_vol)-100);
#else
   XBell(XtDisplay(main_gfx_w), 0);
#endif
}

/*---------------------------------------------------------------------------*/

/* Perform operations necessary to change the size of the drawing area.
 * This involves changing the window size variables, as well as the
 * offset for the cartesian origin, and reallocating the pixmaps associated
 * with drawing and displaying.
 */

void disp_setsize(unsigned short width, unsigned short height)
{
   static char first_time = TRUE;

   /* Check to see if setting the size is really necessary */
   if (Gfx_Width == width && Gfx_Height == height && !first_time)
      return;

   Gfx_Width    = width;
   Gfx_Height   = height;

   disp_x_scale = ((float)Gfx_Width)  / ((float)(DEFAULT_GFX_WIDTH));
   disp_y_scale = ((float)Gfx_Height) / ((float)(DEFAULT_GFX_HEIGHT));

   Gfx_X_Origin = (int)(0.5 + disp_x_scale * (double)DEFAULT_X_ORIGIN);
   Gfx_Y_Origin = (int)(0.5 + disp_y_scale * (double)DEFAULT_Y_ORIGIN);

   /* Free up the pixmaps (Unless they haven't yet been allocated!) */
   if (first_time)
      first_time = FALSE;
   else
   {
      XFreePixmap(XtDisplay(main_gfx_w), draw_pixbuff);
      XFreePixmap(XtDisplay(main_gfx_w), refresh_pixbuff);
   }

   /* Allocate the draw_pixbuff memory for rendering into */
   draw_pixbuff = XCreatePixmap(XtDisplay(main_gfx_w),
                                   DefaultRootWindow(XtDisplay(main_gfx_w)),
                                   Gfx_Width, Gfx_Height,
                                   DefaultDepthOfScreen(XtScreen(main_gfx_w)));

   /* Allocate the refresh_pixbuff for storing finished frames.  This
    * Buffer will drive the actual display.
    */
   refresh_pixbuff = XCreatePixmap(XtDisplay(main_gfx_w),
                                DefaultRootWindow(XtDisplay(main_gfx_w)),
                                Gfx_Width, Gfx_Height,
                                DefaultDepthOfScreen(XtScreen(main_gfx_w)));
}

/*---------------------------------------------------------------------------*/

/* Get the current output window's size */

void disp_getsize(unsigned short *width, unsigned short *height)
{

   Arg wargs[2];

   XtSetArg(wargs[0], XtNwidth,  width);
   XtSetArg(wargs[1], XtNheight, height);
   XtGetValues(main_gfx_w, wargs, 2);

}

/*---------------------------------------------------------------------------*/
/*------------------------ INTERNALLY USED FUNCTIONS ------------------------*/
/*---------------------------------------------------------------------------*/

/* Handle exposure of the VR area.
 * The display is simply refreshed.
 */

void disp_expose_handler(Widget w, caddr_t client_data, XEvent *event)
{
   if (((XExposeEvent *)event)->count > 0) return;

   disp_refresh();
}

/*---------------------------------------------------------------------------*/

/* This handler is invoked when the output window's structure has
 * changed.  Check to see if the changes involved a window resizing.
 * If so, trip the Resized flag.
 */

void disp_config_handler(Widget w, caddr_t client_data, XEvent *event)
{
   XConfigureEvent *config_event = (XConfigureEvent *) event;

   if (config_event->width != Gfx_Width || config_event->height != Gfx_Height)
      Resized = TRUE;
}

/*---------------------------------------------------------------------------*/

/* Handle mouse button presses in the display area.
 */

void disp_select_handler(Widget w, caddr_t client_data, XEvent *event)
{
   Selected = TRUE;
}

/*---------------------------------------------------------------------------*/

/* Handle nonmaskable events, such as ClentMessage events.  Currently, I
 * am only interested in timeout events, so I'll just ingore anything else.
 */

void disp_nomask_handler(Widget w, caddr_t client_data, XEvent *event)
{

   switch(event->type)
   {
    case ClientMessage:
      if(event->xclient.message_type == xa_timeout)
         TimedOut = TRUE;
      break;
    default:
      break;
   }
}
/*---------------------------------------------------------------------------*/

/* Handle a timeout.  Since Timeouts are not X events, we need to generate an
 * X event to wake up the X event loop, which is waiting for an X event
 * to tell it that something has happenned.
 */

void disp_timeout_handler(XtPointer  client_data, XtIntervalId *id)
{
     static XEvent dummy_event;

     dummy_event.type = ClientMessage;
     dummy_event.xclient.message_type = xa_timeout;
     dummy_event.xclient.format       = 8;
     dummy_event.xclient.data.b[0]    = (char) 0;

     XSendEvent(XtDisplay(main_gfx_w), XtWindow(main_gfx_w), FALSE,
                ExposureMask, &dummy_event);
}

/*---------------------------------------------------------------------------*/

/* Copy whatever is in the main_pixbuff to the display area */

void disp_refresh(void)
{
   XCopyArea(XtDisplay(main_gfx_w), refresh_pixbuff, XtWindow(main_gfx_w),
             main_gfx_gc, 0, 0, Gfx_Width, Gfx_Height, 0, 0);
   XFlush(XtDisplay(main_gfx_w));
}

/*---------------------------------------------------------------------------*/
/*--------------------------- DEBUGGING FUNCTIONS ---------------------------*/
/*---------------------------------------------------------------------------*/

/* Check that the gc is okay by using it to set the foreground color */

int disp_checkgc(char *str)
{
   fprintf(stderr, "GC Check (%s) ... ", str);
   XSetForeground(XtDisplay(main_gfx_w), main_gfx_gc,
                  BlackPixelOfScreen(XtScreen(main_gfx_w)));
   fprintf(stderr, "passed.\n");
   return(1);
}

/*---------------------------------------------------------------------------*/

int put_points(polygon_t *cur_poly)
{
   int i;
   for(i = 0; i < cur_poly->npoints; i++)
   {
      printf("         (%d, %d)\n", cur_poly->int_points[i].x,
                                    cur_poly->int_points[i].y);
   }
   return(0);
}

/*---------------------------------------------------------------------------*/

/* EOF: display_list.c */