[comp.windows.x] is the server broken?

jam@elrond.CalComp.COM (Julie A. Melbin) (04/14/88)

This is going to be hard to explain, but here goes....

I took bounce.c which was posted to the group a while back and hacked it
up to bounce the windows around the screen sort-of like the OLD video game
'pong'. Anyway, the idea is that all windows should bounce around the screen
forever. So, when I start the 5 xclocks like:

        uwm&			(to place the windows randomly on the screen)
	xclock -update 1&	(five times)

and then start xpong:

	xpong&

the windows bounce around the screen. Fine. But, after a bit, the images of the
clocks become damaged - for instance, multiple second hands appear on some 
clocks, while other fail to refresh after another window has bounced over top 
and then moved off.  The source for xpong follows; question - is this code doing
something wrong? or is there something wrong with my server (Sun 360 with
patches 1-10 installed)? Does uwm have anything to do with this - I have to
admit that as I write this, I never checked it without uwm. If that's my
problem, sorry - but if so, why?

Anyway, thanks anyone who even bothers to see if their server has the problem,
let along who fixes it ! :)

Thanks!

...{harvard, decvax}!elrond!jam or jam@elrond.CalComp.COM
(603) 885-8139


#### cut here #####

/* compile :   cc -o xpong xpong.c -lX11 
/* X11 Bounce, by Steven Grady 
* (hacked by jam)
*/
#include <stdio.h>
#include <X11/Xlib.h>
#include <signal.h>

struct window {
	Window win;
	XWindowAttributes winf;
	int xdir, ydir;
}*windows, *savedwins;

Display *dpy;

int totwin;
int gravity = 10;
int bottom, side;

int bye();

main(argc, argv)
int argc;
char **argv;
{
	int i;
	struct window *wind;

	
	dpy = XOpenDisplay(argv[1]);
	if (argv[2] && argc >= 3)
		gravity = atoi(argv[2]);
		
	totwin = get_windows();
	signal(SIGINT, bye);
        while (1) {
	    for (i = 0; i < totwin; i++) {
		    wind = &windows[i];
		    calc_windows(wind);
		    disp_windows(wind);
		    }
	XFlush(dpy);
	sleep(1);  /* give other windows a chance to update */
	}
	
}

calc_windows(win)
struct window *win;
{
	if (win->winf.y + win->winf.height >= (bottom - gravity) ||
		hits_others(win)) 
			win->ydir = win->ydir * -1;
	else if (win->winf.y <= 3 || hits_others(win))
		win->ydir = win->ydir * -1;
        win->winf.y += gravity * win->ydir;

        if (win->winf.x + win->winf.width >= side || hits_others(win))
		win->xdir = -1;
	else if (win->winf.x <= 3 || hits_others(win))
	        win->xdir = 1;
        win->winf.x += win->xdir * gravity;
    
}

int
get_windows()
{
	int n, i, j;
	Window *children, par, root;
	XWindowAttributes winf;
	int p = 1, q = -1;

	XQueryTree(dpy, DefaultRootWindow(dpy), &root, &par, &children, &n);
	/* Malloc() never fails.  Heh heh.. */
	windows = (struct window *)malloc(n * sizeof(*windows));
	savedwins = (struct window *)malloc(n * sizeof(*savedwins));
	for (i = 0, j = 0; i < n; i++) {
		XGetWindowAttributes(dpy, children[i], &winf);
		if (winf.map_state == IsViewable) {
			windows[j].winf = winf;
			windows[j].win = children[i];
			windows[j].xdir = (j % 2 ?  p : q);
			windows[j].ydir = (j % 2 ?  p : q);
			p *= -1;
			q *= -1;
			savedwins[j] = windows[j];
			j++;
		}
	}
		/* Give allowance for a reasonable border */
	bottom = DisplayHeight(dpy, DefaultScreen(dpy)) - 3;
	side = DisplayWidth(dpy, DefaultScreen(dpy)) - 3;
	return(j);
}

disp_windows(win)
struct window *win;
{
	XMoveWindow(dpy, win->win, win->winf.x, win->winf.y);
	XMapWindow(dpy, win->win);
	XFlush(dpy);
}

bye()
{
	fix();
	exit(0);
}

fix()
{
	int i;
	struct window sw;

	for (i = 0; i < totwin; i++) {
		sw = savedwins[i];
		XMoveWindow(dpy, sw.win, sw.winf.x, sw.winf.y);
		XMapWindow(dpy, sw.win);
		XFlush(dpy);
	}
}

hits_others(w, d)	/* someday */
   struct window *w;
   int  d;
   {
   return (0);
   }