|
|
1.1 root 1: #include <stdio.h>
2: #include <X/Xlib.h>
3: #include <signal.h>
4: #include <math.h>
5:
6: #define MAXWIN 24
7:
8: /* Bounce, by Steven Grady */
9:
10: #define abs(x) ((x) > 0 ? x : (-x))
11: struct window {
12: Window win;
13: WindowInfo winf;
14: float v;
15: } windows[MAXWIN], savedwins[MAXWIN];
16:
17: int totwin;
18: float elast = 0.5, el, gravity = 2.0;
19: int bottom;
20: int beep;
21: int dontfix = 0;
22:
23: int bye();
24:
25: main(argc, argv)
26: int argc;
27: char **argv;
28: {
29: int e, i;
30: struct window *wind;
31: int c;
32: extern int optind;
33: extern char *optarg;
34: int errflg = 0;
35:
36:
37: while ((c = getopt(argc, argv, "sbg:e:")) != EOF)
38: switch (c) {
39: case 'b':
40: beep++;
41: break;
42: case 'g':
43: gravity = (float) atoi(optarg)/ 10.0;
44: if (gravity < 0.0) {
45: fprintf(stderr,
46: "Try to control yourself, please. \n");
47: exit(1);
48: }
49: break;
50: case 'e':
51: elast = (float) atoi(optarg)/ 10.0;
52: break;
53: case 's':
54: dontfix++;
55: break;
56: case '?':
57: errflg++;
58: break;
59: }
60: if (errflg) {
61: fprintf(stderr, "usage: %s [-b] [-g gravity] [-e elasticity]",
62: argv[0]);
63: fprintf(stderr, " [-s] [display]\n");
64: fprintf(stderr, "\t-b: turn on beep -s: don't restore ");
65: fprintf(stderr, "screen\n\tdefault g=20");
66: fprintf(stderr, "\tdefault e=5\n");
67: exit(1);
68: }
69:
70: XOpenDisplay((optind < argc) ? argv[optind] : (char *) NULL);
71:
72: totwin = get_windows();
73: signal(SIGINT, bye);
74: for (i = 0; i < totwin; i++) {
75: wind = &windows[i];
76: el = elast * 1.2; /* Make up for some of the rounding */
77: do {
78: calc_windows(wind);
79: disp_windows(wind);
80: XSync(0);
81: } while (!stopped(wind));
82: }
83: fix();
84: exit();
85: /*
86: if (!dontfix) {
87: printf("Waiting for an interrupt..\n");
88: pause();
89: /* Never returns -- bye() does an exit()
90: }
91: */
92: }
93:
94: stopped(w)
95: struct window *w;
96: {
97:
98: if (abs(w->v) < (float) gravity) {
99: return((int) abs(bottom - (w->winf.y + w->winf.height))
100: < 10);
101: } else
102: return(0);
103: }
104:
105: calc_windows(win)
106: struct window *win;
107: {
108: int new_y;
109: float new_v;
110:
111: new_y = win->winf.y + win->v;
112: new_v = win->v + gravity;
113: if (new_y+win->winf.height > bottom) {
114: new_y = 2*bottom-(new_y+2*win->winf.height);
115: if (beep) {
116: XFeep((int) log((double) (win->v*win->v *
117: sqrt((float) win->winf.height) / 30)) - 6);
118: }
119: new_v = -new_v*el;
120: el *= 0.8;
121: }
122: win->winf.y = new_y;
123: win->v = new_v;
124: }
125:
126: int
127: get_windows()
128: {
129: int n, i, j;
130: Window *children, par;
131: WindowInfo winf;
132:
133: XQueryTree(RootWindow, &par, &n, &children);
134: for (i = 0, j = 0; i < n; i++) {
135: XQueryWindow(children[i], &winf);
136: if (winf.mapped == IsMapped) {
137: windows[j].winf = winf;
138: windows[j].win = children[i];
139: windows[j].v = 0;
140: savedwins[j] = windows[j];
141: j++;
142: }
143: }
144: bottom = DisplayHeight() - 3; /*Give allowance for a reasonable borde*/
145: return(j);
146: }
147:
148: disp_windows(win)
149: struct window *win;
150: {
151: int i;
152:
153: XMoveWindow(win->win, win->winf.x, win->winf.y);
154: XMapWindow(win->win);
155: XFlush();
156: }
157:
158: bye()
159: {
160: if (!dontfix) {
161: fix();
162: }
163: exit(0);
164: }
165:
166: fix()
167: {
168: int i;
169: struct window sw;
170:
171: for (i = 0; i < totwin; i++) {
172: sw = savedwins[i];
173: XMoveWindow(sw.win, sw.winf.x, sw.winf.y);
174: XMapWindow(sw.win);
175: XFlush();
176: }
177: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.