3 Copyright (C) Teemu Suutari (temisu@utu.fi) Feb 1998
5 Permission is hereby granted, free of charge, to any person obtaining
6 a copy of this software and associated documentation files (the
7 "Software"), to deal in the Software without restriction, including
8 without limitation the rights to use, copy, modify, merge, publish,
9 distribute, sublicense, and/or sell copies of the Software, and to
10 permit persons to whom the Software is furnished to do so, subject to
11 the following conditions:
13 The above copyright notice and this permission notice shall be included
14 in all copies or substantial portions of the Software.
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19 IN NO EVENT SHALL THE X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 OTHER DEALINGS IN THE SOFTWARE.
24 Except as contained in this notice, the name of the X Consortium shall
25 not be used in advertising or otherwise to promote the sale, use or
26 other dealings in this Software without prior written authorization
27 from the X Consortium.
36 *** This is contest-version. Don't look any further, code is *very* ugly.
42 #include "screenhack.h"
44 #ifdef HAVE_XDBE_EXTENSION
45 # include <X11/extensions/Xdbe.h>
46 #endif /* HAVE_XDBE_EXTENSION */
48 char *progclass="Kumppa";
57 XrmOptionDescRec options [] = {
58 {"-delay",".delay",XrmoptionSepArg,0},
59 {"-speed",".speed",XrmoptionSepArg,0},
60 {"-random",".random",XrmoptionIsArg,0},
61 #ifdef HAVE_XDBE_EXTENSION
62 {"-dbuf",".dbuf",XrmoptionIsArg,0},
63 #endif /* HAVE_XDBE_EXTENSION */
67 const char colors[96]=
68 {0,0,255, 0,51,255, 0,102,255, 0,153,255, 0,204,255,
69 0,255,255,0,255,204, 0,255,153, 0,255,102, 0,255,51,
70 0,255,0, 51,255,0, 102,255,0, 153,255,0, 204,255,0,
71 255,255,0, 255,204,0, 255,153,0, 255,102,0, 255,51,0,
72 255,0,0, 255,0,51, 255,0,102, 255,0,153, 255,0,204,
73 255,0,255, 219,0,255, 182,0,255, 146,0,255, 109,0,255,
75 const float cosinus[8][6]={{-0.07,0.12,-0.06,32,25,37},{0.08,-0.03,0.05,51,46,32},{0.12,0.07,-0.13,27,45,36},
76 {0.05,-0.04,-0.07,36,27,39},{-0.02,-0.07,0.1,21,43,42},{-0.11,0.06,0.02,51,25,34},{0.04,-0.15,0.02,42,32,25},
77 {-0.02,-0.04,-0.13,34,20,15}};
79 static float acosinus[8][3]={{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0}};
81 static int ocoords[8]={0,0,0,0,0,0,0,0};
89 static unsigned long delay;
90 static Bool cosilines=True;
91 #ifdef HAVE_XDBE_EXTENSION
92 static Bool usedouble=False;
93 #endif /* HAVE_XDBE_EXTENSION */
95 static int *Xrotations;
96 static int *Yrotations;
97 static int *Xrottable;
98 static int *Yrottable;
103 static int rotsizeX,rotsizeY;
104 static int stateX,stateY;
111 return (int)(maxi*frand(1));
115 void palaRotate(int x,int y)
117 int ax,ay,bx,by,cx,cy;
123 cx=rotateX[x]-(y-ry)+x-rx;
124 cy=rotateY[y]+(x-rx)+y-ry;
135 if (cx+bx-ax>sizx) bx=ax-cx+sizx;
136 if (cy+by-ay>sizy) by=ay-cy+sizy;
138 XCopyArea(dpy,win[0],win[1],cgc,ax,ay,bx-ax,by-ay,cx,cy);
147 rx=Xrottable[stateX+1]-Xrottable[stateX];
148 ry=Yrottable[stateX+1]-Yrottable[stateY];
152 rotateX[x]=(x)?midx-1-Xrotations[Xrottable[stateX+1]-x]:0;
154 rotateX[x+rx+1]=(x==rx)?sizx-1:midx+Xrotations[Xrottable[stateX]+x];
156 rotateY[y]=(y)?midy-1-Yrotations[Yrottable[stateY+1]-y]:0;
158 rotateY[y+ry+1]=(y==ry)?sizy-1:midy+Yrotations[Yrottable[stateY]+y];
161 for (dy=0;dy<(x+1)<<1;dy++)
162 for (dx=0;dx<(x+1)<<1;dx++)
165 if (dy+y>=0 && dy<(ry+1)<<1 && dx<(rx+1)<<1)
166 if (dy+y+dx<=ry+rx && dy+y-dx<=ry-rx)
168 palaRotate((rx<<1)+1-dx,dy+y);
169 palaRotate(dx,(ry<<1)+1-dy-y);
172 if (dy+y>=0 && dx<(ry+1)<<1 && dy<(rx+1)<<1)
173 if (dy+y+dx<=ry+rx && dx-dy-y>=ry-rx)
176 palaRotate((rx<<1)+1-dy-y,(ry<<1)+1-dx);
180 if (stateX==rotsizeX) stateX=0;
182 if (stateY==rotsizeY) stateY=0;
187 Bool make_rots(double xspeed,double yspeed)
196 rotsizeX=(int)(2/xspeed+1);
197 ix=(double)(midx+1)/(double)(rotsizeX);
198 rotsizeY=(int)(2/yspeed+1);
199 iy=(double)(midy+1)/(double)(rotsizeY);
201 Xrotations=malloc((midx+2)*sizeof(unsigned int));
202 Xrottable=malloc((rotsizeX+1)*sizeof(unsigned int));
203 Yrotations=malloc((midy+2)*sizeof(unsigned int));
204 Yrottable=malloc((rotsizeY+1)*sizeof(unsigned int));
205 chks=malloc(((midx>midy)?midx:midy)*sizeof(Bool));
206 if (!Xrottable || !Yrottable || !Xrotations || !Yrotations || !chks) return False;
213 for (a=0;a<midx;a++) chks[a]=True;
214 for (a=0;a<rotsizeX;a++)
217 f=(int)(d+ix)-g; /*viivojen lkm.*/
227 for (j=0;j<midx;j++) /*testi*/
234 while (j+l<midx && om+12*ok>m)
236 if (j-l>=0) if (chks[j-l]) om+=ok;
237 else; else if (chks[l-j]) om+=ok;
238 if (chks[j+l]) om+=ok;
251 while (l>=Xrottable[a])
253 if (l!=Xrottable[a]) Xrotations[l]=Xrotations[l-1];
254 if (k>Xrotations[l] || l==Xrottable[a])
264 if (maxi<c-Xrottable[a]) maxi=c-Xrottable[a];
267 rotateX=malloc((maxi+2)*sizeof(int)<<1);
268 if (!rotateX) return False;
274 for (a=0;a<midy;a++) chks[a]=True;
275 for (a=0;a<rotsizeY;a++)
278 f=(int)(d+iy)-g; /*viivojen lkm.*/
288 for (j=0;j<midy;j++) /*testi*/
295 while (j+l<midy && om+12*ok>m)
297 if (j-l>=0) if (chks[j-l]) om+=ok;
298 else; else if (chks[l-j]) om+=ok;
299 if (chks[j+l]) om+=ok;
312 while (l>=Yrottable[a])
314 if (l!=Yrottable[a]) Yrotations[l]=Yrotations[l-1];
315 if (k>Yrotations[l] || l==Yrottable[a])
326 if (maxi<c-Yrottable[a]) maxi=c-Yrottable[a];
329 rotateY=malloc((maxi+2)*sizeof(int)<<1);
330 if (!rotateY) return False;
337 Bool InitializeAll(void)
340 XWindowAttributes xgwa;
341 XSetWindowAttributes xswa;
347 XGetWindowAttributes(dpy,win[0],&xgwa);
349 xswa.backing_store=Always;
350 XChangeWindowAttributes(dpy,win[0],CWBackingStore,&xswa);
351 xgcv.function=GXcopy;
353 xgcv.foreground=get_pixel_resource ("background", "Background", dpy, cmap);
354 fgc[32]=XCreateGC(dpy,win[0],GCForeground|GCFunction,&xgcv);
360 xgcv.foreground=get_pixel_resource ("foreground", "Foreground", dpy, cmap);
361 fgc[1]=XCreateGC(dpy,win[0],GCForeground|GCFunction,&xgcv);
362 for (i=0;i<32;i+=2) fgc[i]=fgc[0];
363 for (i=1;i<32;i+=2) fgc[i]=fgc[1];
367 color.red=colors[n++]<<8;
368 color.green=colors[n++]<<8;
369 color.blue=colors[n++]<<8;
370 color.flags=DoRed|DoGreen|DoBlue;
371 XAllocColor(dpy,cmap,&color);
372 xgcv.foreground=color.pixel;
373 fgc[i]=XCreateGC(dpy,win[0],GCForeground|GCFunction,&xgcv);
375 cgc=XCreateGC(dpy,win[0],GCForeground|GCFunction,&xgcv);
376 XSetGraphicsExposures(dpy,cgc,False);
378 if (get_string_resource("random","String")!=NULL && get_string_resource("random","String")[0]!=0) cosilines=False;
380 #ifdef HAVE_XDBE_EXTENSION
381 if (get_string_resource("dbuf","String")!=NULL && get_string_resource("dbuf","String")[0]!=0) usedouble=True;
384 XdbeQueryExtension(dpy,&n,&i);
387 fprintf(stderr,"Double buffer extension not supported!\n");
391 if (usedouble) win[1]=XdbeAllocateBackBufferName(dpy,win[0],XdbeUndefined);
392 #endif /* HAVE_XDBE_EXTENSION */
394 delay=get_integer_resource("delay","Integer");
395 rspeed=get_float_resource("speed","Float");
396 if (rspeed<0.0001 || rspeed>0.2)
398 fprintf(stderr,"Speed not in valid range! (0.0001 - 0.2), using 0.1 \n");
409 if (!make_rots(rspeed,rspeed))
411 fprintf(stderr,"Not enough memory for tables!\n");
418 void screenhack(Display *d, Window w)
420 #ifdef HAVE_XDBE_EXTENSION
422 #endif /* HAVE_XDBE_EXTENSION */
428 if (!InitializeAll()) return;
430 #ifdef HAVE_XDBE_EXTENSION
433 xdswp.swap_action=XdbeUndefined;
434 xdswp.swap_window=win[0];
437 #endif /* HAVE_XDBE_EXTENSION */
450 acosinus[a][b]+=cosinus[a][b];
451 f+=cosinus[a][b+3]*sin((double)acosinus[a][b]);
457 XDrawLine(dpy,win[0],(mono_p)?fgc[1]:fgc[((a<<2)+c)&31],midx+ocoords[a<<1],midy+ocoords[(a<<1)+1]
458 ,midx+coords[a<<1],midy+coords[(a<<1)+1]);
459 ocoords[a<<1]=coords[a<<1];
460 ocoords[(a<<1)+1]=coords[(a<<1)+1];
468 b=Satnum(32)-16+midx;
469 c=Satnum(32)-16+midy;
470 XFillRectangle(dpy,win[0],fgc[a],b,c,2,2);
473 XFillRectangle(dpy,win[0],fgc[32],midx-2,midy-2,4,4);
475 #ifdef HAVE_XDBE_EXTENSION
476 if (usedouble) XdbeSwapBuffers(dpy,&xdswp,1);
477 #endif /* HAVE_XDBE_EXTENSION */
479 if (delay) usleep (delay);