File: linger.c

package info (click to toggle)
xbanner 1.31-13
  • links: PTS
  • area: main
  • in suites: hamm
  • size: 1,108 kB
  • ctags: 832
  • sloc: ansic: 5,084; sh: 263; csh: 168; makefile: 150
file content (393 lines) | stat: -rw-r--r-- 12,525 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
#include "xb_config.h"

#ifndef vms
#include <unistd.h>
#else
#include <ssdef.h>
#endif
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <signal.h>

#include <X11/Xlib.h>
#include <X11/Xatom.h>

/*
#ifdef vms
#define fork() vfork()
#endif
*/

#include "xbanner.h"

/* globals which go external */
int  CornerMask   = DownRight|DownLeft|UpRight|UpLeft; /* corner types to glint on */
int  GlintSizeMin = GLINTSIZE_MINVAL;   /* glint stars are at least this size */
int  GlintSizeMax = GLINTSIZE_MAXVAL;      /* and no larger than this size  */
int  glint_speed  = GLINTSPEED_DEFVAL;     /* speed of stars		    */
int  CycleSpeed   = CYCLESPEED_DEFVAL;     /* divisor of LINGER_DELAY       */
Bool XB_Linger    = XB_LINGER_DEFVAL;      /* linger after drawing is done? */
Bool XB_Glint     = XB_GLINT_DEFVAL;       /* do we do the stars thingy?    */
Bool XB_Expose	  = XB_EXPOSE_DEFVAL;	   /* do we handle expose events?   */
long GlintMaxTime = GLINTMAXTIME_DEFVAL;   /* max time between glint sparks */
long GlintMinTime = GLINTMINTIME_DEFVAL;   /* min time between glint sparks */

/* Glinting stuff */
Corner *CornerList;
int NumCorners = 0;

/* For now it is a patch, so it's here */
extern Pixmap exposepix;

#if 0
/* cycle the red-component. Not yet adapted to the new cycling method */
void RedCycle(Display *disp,Bool store)
{
  static int i;
  static unsigned short r;

  r = CycleColors[0].red;
  for(i=0;i<NumCycleColors-1;i++)
    CycleColors[i].red = CycleColors[i+1].red;
  CycleColors[i].red = r;
  if(store)
    XStoreColors(disp,cmap,CycleColors,NumCycleColors);
}

/* cycle the green-component. Not yet adapted to the new cycling method */
void GreenCycle(Display *disp,Bool store)
{
  static int i;
  static unsigned short g;

  g = CycleColors[0].green;
  for(i=0;i<NumCycleColors-1;i++)
    CycleColors[i].green = CycleColors[i+1].green;
  CycleColors[i].green = g;
  if(store)
    XStoreColors(disp,cmap,CycleColors,NumCycleColors);
}

/* cycle the blue-component. Not yet adapted to the new cycling method */
void BlueCycle(Display *disp,Bool store)
{
  static int i;
  static unsigned short b;

  b = CycleColors[0].blue;
  for(i=0;i<NumCycleColors-1;i++)
    CycleColors[i].blue = CycleColors[i+1].blue;
  CycleColors[i].blue = b;
  if(store)
    XStoreColors(disp,cmap,CycleColors,NumCycleColors);
}
#endif

/* Show one star only. This draws a single star on a given location */
static void DoStar(Display *disp,Window w,GC mgc,int x,int y,int size,
			Pixel pixel)
{
  /* choose the color */
  XSetForeground(disp,mgc,pixel);

  /* make the central cross */
  XDrawLine(disp,w,mgc,x-size/2,y,x+size/2,y);
  XDrawLine(disp,w,mgc,x,y-size/2,x,y+size/2);
  if(size>12)	/* make the middle thicker */
  {
    XDrawLine(disp,w,mgc,x-size/4,y+1,x+size/4,y+1);
    XDrawLine(disp,w,mgc,x-size/4,y-1,x+size/4,y-1);
    XDrawLine(disp,w,mgc,x+1,y-size/4,x+1,y+size/4);
    XDrawLine(disp,w,mgc,x-1,y-size/4,x-1,y+size/4);
    XDrawPoint(disp,w,mgc,x-2,y-2);	/* the points are for smoothing */
    XDrawPoint(disp,w,mgc,x+2,y-2);
    XDrawPoint(disp,w,mgc,x-2,y+2);
    XDrawPoint(disp,w,mgc,x+2,y+2);
    if(size>24)	/* make the middle even thicker */
    {
      XDrawLine(disp,w,mgc,x-size/8,y+2,x+size/8,y+2);
      XDrawLine(disp,w,mgc,x-size/8,y-2,x+size/8,y-2);
      XDrawLine(disp,w,mgc,x+2,y-size/8,x+2,y+size/8);
      XDrawLine(disp,w,mgc,x-2,y-size/8,x-2,y+size/8);
      XDrawPoint(disp,w,mgc,x-3,y-3);
      XDrawPoint(disp,w,mgc,x+3,y-3);
      XDrawPoint(disp,w,mgc,x-3,y+3);
      XDrawPoint(disp,w,mgc,x+3,y+3);
    }
  }
  /* look out! no XSync() here! */
} /* DoStar() */

/* make a star appear and disappear, from small size to large and back */
void BlinkStar(Display *disp, Window w, int cx, int cy, int size, unsigned long pixel)
{
  Pixmap    bgpix;		/* to keep old area before star was drawn */
  XGCValues gcv;
  int       i,scrn_no;
  volatile  int j;	/* volatile so optimizers won't remove my idle-loop */

  /* bounds checks - don't try to draw where you shouldn't */
  if(cx-size/2-1 < 0 || cx+size/2+1 >= sc_w ||
     cy-size/2-1 < 0 || cy+size/2+1 >= sc_h)
    return;

  scrn_no = XDefaultScreen(disp);       /* get the default screen #     */
  mgc = XCreateGC(disp,w,0,&gcv);	/* create us a GC */
  /* make a pixmap and copy the area into it taking some margin too */
  bgpix = XCreatePixmap(disp,w,size+3,size+3,DefaultDepth(disp,scrn_no));
  XCopyArea(disp,w,bgpix,mgc,cx-size/2-1,cy-size/2-1,size+3,size+3,0,0);

  /* 
     Blinking one star is simple. I copy the area under it, draw it from
     size 3 to the requested size in glint_speed steps, then I return
     the copied area, and draw smaller until it disappears
  */

  /* 0 is a blinking speed - for really slow machines */
  if(glint_speed != 0)
  {
    if(glint_speed > 0)
    {
      for (i=3 ; i<size ; i += glint_speed)
      {
        DoStar(disp,w,mgc,cx,cy,i,pixel);
        XSync(disp,False);			/* must do that here! */
      } /* grow star */
      for ( ; i>2 ; i -= glint_speed)
      {
        XCopyArea(disp,bgpix,w,mgc,0,0,size+3,size+3,cx-size/2-1,cy-size/2-1);
        DoStar(disp,w,mgc,cx,cy,i-1,pixel);
        XSync(disp,False);
      } /* shrink star */
    }
    else /* glint_speed < 0 */
    {
      for (i=3 ; i<size ; i ++)
      {
        DoStar(disp,w,mgc,cx,cy,i,pixel);
        XSync(disp,False);			/* must do that here! */
        for(j=glint_speed;j;j++) ;		/* j is volatile - delay */
      } /* grow star */
      for ( ; i>2 ; i --)
      {
        XCopyArea(disp,bgpix,w,mgc,0,0,size+3,size+3,cx-size/2-1,cy-size/2-1);
        DoStar(disp,w,mgc,cx,cy,i-1,pixel);
        XSync(disp,False);
        for(j=glint_speed;j;j++) ;
      } /* shrink star */
    } /* else of if (glint_speed > 0) */
  }
  else /* glint_speed == 0! */
  {
    DoStar(disp,w,mgc,cx,cy,size,pixel);	/* show the star */
    XSync(disp,False);				/* must do that here! */
    usleep(BLINK_SPEED_TIME);			/* sleep a while */
  }
  /* restore normality */
  XCopyArea(disp,bgpix,w,mgc,0,0,size+3,size+3,cx-size/2-1,cy-size/2-1);
  XFreeGC(disp,mgc);		/* and free resources */
  XFreePixmap(disp,bgpix);
  XSync(disp,False);
} /* BlinkStar() */

/*
  The Linger() algorithm:
  
  First client writes a property named XPROPERTY, and gives it the value
  "Li%d" where %d is a single digit stating this client's number. In this
  case it will be 0 because it is the first.
  
  Each additional client simply adds one to the number, and adds the new
  digit to the end of the property.
  
  When freetemp runs, it reads the property, and selects the last client,
  writes the first 2 chars of the property as "X%d", to ask that client
  to exit. The client will respond with "D%d". That's X for eXit, and D
  for Done.
*/
static void Linger(Display *disp)
{
  int            cnt=0,idx,format;
  Bool           done=False;
  Atom           xprop,proptype;
  unsigned long	 items,bytesleft;
  unsigned char *propval;
  unsigned char  newval[21];
  int		 my_number=0;
  int            scrn_no;
  GC		 mgc;
  XEvent	 evnt;
  XExposeEvent	 *expose;

  /* some initializations */
  scrn_no = XDefaultScreen(disp);       /* get the default screen #     */
  mgc     = DefaultGC(disp,scrn_no);    /* get the root window GC	*/
  GlintMaxTime *= 1000;			/* user specifies in millisecs	*/
  GlintMinTime *= 1000;			/* but usleep needs it in usec	*/

  /* Get an Atom for the property, or make one */
  xprop = XInternAtom(disp,XPROPERTY,False);

  /* check if the property already exists */
  if(XBPropExists(disp,root,xprop))
  {
    /* check if it is our property */
    XGetWindowProperty(disp,	/* Display	 */
    		root,		/* Window	 */
    		xprop,		/* Property	 */
    		0,20,		/* offset,len	 */
    		False,		/* delete	 */
    		XA_STRING,	/* type		 */
    		&proptype,	/* actual type	 */
    		&format,	/* actual format */
    		&items,		/* items read	 */
    		&bytesleft,	/* bytes left	 */
    		&propval);	/* data return	 */
    /* check the type to see it really is ours */
    if(proptype != XA_STRING || format != 8 || bytesleft > 0)
      error(disp,mgc,"Can't linger, property conflict",True);

    /* check to make sure no race conditions occur */
    if(propval[0]!='L' || propval[1]!='i')	/* Freetemp already running? */
      error(disp,mgc,"Can't linger. Freetemp is running",True);

    /* add our number into the linger-list */
    my_number = 1 + propval[strlen((char*)propval)-1] - '0';
    if(my_number>9)
      error(disp,mgc,"Too many linger processes",True);
    sprintf((char*)newval,"%s%d",propval,my_number);
    XFree(propval);		/* free it up */
  } /* XBPropExists() */
  else
    strcpy((char*)newval,"Li0");

  /* set the property (or change it to show we are waiting also) */
  XChangeProperty(disp,root,		/* disp & win	*/
  		xprop,			/* property	*/
  		XA_STRING,		/* type		*/
  		8,			/* format	*/
  		PropModeReplace,	/* mode		*/
  		newval,			/* data		*/
  		strlen((char*)newval));	/* num elem	*/

  /* prepare to do stars - determine first random time */
  if((XB_Glint==True) && (NumCorners > 0))
    cnt = (rand()%(GlintMaxTime-GlintMinTime)+GlintMinTime)/LINGER_DELAY;

  /* preparation to linger includes selecting input for expose... */
  if(XB_Expose)
    XSelectInput(disp,root,ExposureMask);

  /* main linger loop */
  while(!done)
  {
    /* first let's see if we were asked to exit */
    XGetWindowProperty(disp,	/* Display	 */
    		root,		/* Window	 */
    		xprop,		/* Property	 */
    		0,20,		/* offset,len	 */
    		False,		/* delete	 */
    		XA_STRING,	/* type		 */
    		&proptype,	/* actual type	 */
    		&format,	/* actual format */
    		&items,		/* items read	 */
    		&bytesleft,	/* bytes left	 */
    		&propval);	/* data return	 */
    /* no need to check type, we assume nobody will have changed it */
    if(propval[0]=='X' && propval[1]-'0' == my_number)	/* X for eXit */
      break;	/* exit the while() loop */

    /* Last thing is to free up the propval thingy */
    XFree(propval);

    /* Let's remove events from the queue so we don't get swollen */
    while(XPending(disp))
    {
      XNextEvent(disp,(XEvent*)(&evnt));
      if(XB_Expose==True && evnt.type==Expose)
      {
        expose = (XExposeEvent*)(&evnt);
        XCopyArea(expose->display,exposepix,expose->window,mgc,
        	expose->x,expose->y,
        	expose->width,expose->height,
        	expose->x,expose->y);
      }
    }

    /* done checking if we need to exit - so sleep a while */
    usleep(LINGER_DELAY / CycleSpeed);

    /* ======= from here we start checking the effects ==================== */

    /* let's see if there's anything to glint on */
    if((XB_Glint==True) && (NumCorners > 0))
    {
      if(--cnt==0)
      {
        /* choose next time randomly */
        cnt = (rand()%(GlintMaxTime-GlintMinTime)+GlintMinTime)/(LINGER_DELAY/CycleSpeed);
	/* choose a corner randomly */
        idx = rand() % NumCorners;
	/* and blink it */
        BlinkStar(disp,root,
        	CornerList[idx].x,CornerList[idx].y,
        	(rand()%(GlintSizeMax-GlintSizeMin))+GlintSizeMin,
        	WhitePixel(disp,scrn_no));
      }
    } /* glint = true, corners>0 */

    /* color-cycle. This func checks if there's anything to do at all */
    DoColorCycling(disp);

  } /* while (!done) */

  /* so we are exiting, eh? Let's tell freetemp */
  propval[0]='D';		/* D for Done */
  propval[1]=my_number+'0';	/* just for safety */

  /* re-set the property to signify we are exiting */
  XChangeProperty(disp,root,		/* disp & win	*/
		xprop,			/* property	*/
		XA_STRING,		/* type		*/
		8,			/* format	*/
		PropModeReplace,	/* mode		*/
		propval,		/* data		*/
		strlen((char*)propval));/* num elem	*/
  /* free the propval */
  XFree(propval);

  /* free the exposepix */
  if(XB_Expose)
    XFreePixmap(disp,exposepix);

  /* just for safety - only freetemp will really free those resources */
  XSetCloseDownMode(disp,RetainTemporary);
  XCloseDisplay(disp);
#ifdef vms
  exit(SS$_NORMAL);
#else
  exit(0);
#endif
} /* Linger() */

/* This is the function that calls fork() - to make the child */
void DoLinger(void)
{
  Display *disp;

  /* Must open the display for the lingering child before the parent closes */
  if(dispname[0]=='\0')
    disp = XOpenDisplay(NULL);
  else
    disp = XOpenDisplay(dispname);
  if(disp==NULL)
  {
    fprintf(stderr,"%s: DoLinger() failed to open the display!\n",PRGCLASS);
    return;
  }

  if(fork() == 0)	/* the child lingers... */
    Linger(disp);
  /* the parent returns to main() and exits */
}