1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <ctype.h>
4 #include <X11/Xlib.h>
5 #include <X11/Xutil.h>
6 #include <math.h>
7 #include <sys/types.h>
8 #include <sys/socket.h>
9 #include <sys/time.h>
10 #include "struct.h"
11 
12 #define TITLESIZE (7*X_PIECE_SIZE)
13 
14 #ifndef FD_SET
15 
16 #define MAXSELFD	64
17 typedef long	fd_mask;
18 #define NFDBITS	(sizeof(fd_mask) * NBBY)	/* bits per mask (power of 2!)*/
19 #define NFDSHIFT 5				/* Shift based on above */
20 #ifndef howmany
21 #define	howmany(x, y)	(((x)+((y)-1))/(y))
22 #endif
23 
24 #define	NBBY	8		/* number of bits in a byte */
25 
26 
27 #define	FD_SETSIZE	64
28 
29 
30 #define	FD_SET(n, p)	((p)->fds_bits[(n)/NFDBITS] |= (1 << ((n) % NFDBITS)))
31 #define	FD_CLR(n, p)	((p)->fds_bits[(n)/NFDBITS] &= ~(1 << ((n) % NFDBITS)))
32 #define	FD_ISSET(n, p)	((p)->fds_bits[(n)/NFDBITS] & (1 << ((n) % NFDBITS)))
33 #define FD_ZERO(p)	bzero((char *)(p), sizeof(*(p)))
34 
35 #endif
36 
37 extern Display *dpy, *odpy, *currdpy;
38 extern Window win, owin, currwin;
39 extern GC gc, ogc, currgc;
40 extern int xsize, ysize, obstacle_color, scr, oscr;
41 extern int botmode;
42 extern Pixmap bamp, obamp, currbamp;
43 extern int xwin,ywin;
44 Pixmap mapw, mapb, omapw, omapb, wblock, bblock, owblock, obblock;
45 int oldw, oldb;
46 Cursor upc, downc, oupc, odownc;
47 Bool perfection;
48 
49 int waitferkey();
50 
51 
msec_wait(millsecs)52 msec_wait(millsecs)
53 int millsecs;
54 {
55 	struct timeval tv;
56 
57 	tv.tv_sec = 0;
58 	tv.tv_usec = millsecs;
59 	select(0, 0, 0, 0, &tv);
60 }
61 
waitferkey(tm)62 waitferkey(tm) /* returns nonzero on timeout, zero on key or click */
63 int tm;
64 {
65   fd_set readbits, rb2;
66   int sock1, sock2, sockh, tsec=0;
67   long t1, t2;
68   XEvent event;
69   Bool ploop = True;
70   char c;
71   struct timeval timeout;
72 
73   timeout.tv_sec = (long) tm;
74   timeout.tv_usec = 0;
75   FD_ZERO(&readbits);
76   sock1 = ConnectionNumber(dpy);
77   if (botmode)
78     sock2 = ConnectionNumber(odpy);
79   if (sock1 > sock2)
80     sockh = sock1;
81   else
82     sockh = sock2;
83   if (!botmode)
84     sockh = sock1;
85   FD_SET(sock1, &readbits);
86   if (botmode)
87     FD_SET(sock2, &readbits);
88   sockh++;
89   rb2 = readbits;
90  while (ploop) {
91   readbits = rb2;
92   timeout.tv_sec = ((long) tm - tsec);
93   t1 = (long) time((long *) 0);
94   if (select (sockh, &readbits, NULL, NULL, &timeout)) {
95   t2 = (long) time((long *) 0);
96   tsec += (t2-t1);
97   if(XCheckWindowEvent(dpy, win, ButtonPressMask | KeyPressMask | ButtonReleaseMask |
98   	    ExposureMask, &event)) {
99 	switch(event.type) {
100 		case ButtonPress:
101 		break;
102 		case ButtonRelease:
103 			ploop = False;
104 		break;
105 		case Expose:
106 		break;
107 		case KeyPress:
108 			XLookupString(&event, &c, 1, NULL, NULL);
109 			if (c == 'q')
110 				quit();
111 			if (isalpha(c) || isspace(c))
112 			   ploop = False;
113 		break;
114                 default:
115 		break;
116 	}
117   }
118   if(botmode)
119     if(XCheckWindowEvent(odpy, owin, ButtonPressMask | KeyPressMask | ButtonReleaseMask |
120   	    ExposureMask, &event)) {
121 	switch(event.type) {
122 		case ButtonPress:
123 		break;
124 		case ButtonRelease:
125 			ploop = False;
126 		break;
127 		case Expose:
128 		break;
129 		case KeyPress:
130 			XLookupString(&event, &c, 1, NULL, NULL);
131 			if (c == 'q')
132 				quit();
133 			if (isalpha(c) || isspace(c))
134 			   ploop = False;
135 		break;
136                 default:
137 		break;
138 	}
139    }
140   }
141   else { /* we have timedout */
142     return(1);
143   }
144  }
145  return(0);
146 }
147 
quit()148 quit()
149 {
150 	XCloseDisplay(dpy);
151 	if (botmode)
152 		XCloseDisplay(odpy);
153 	exit(0);
154 }
155 
draw_circle(x,y,color)156 draw_circle(x,y,color)
157      int x,y,color;
158 {
159   Pixmap wmap, bmap;
160 
161   if (currdpy == dpy) {
162 	wmap = mapw;
163 	bmap = mapb;
164   }
165   else {
166 	wmap = omapw;
167 	bmap = omapb;
168   }
169 
170   if (color==WHITE)
171     XCopyArea(currdpy, wmap, currbamp, currgc,0,0,X_PIECE_SIZE,
172 	     Y_PIECE_SIZE, x*X_PIECE_SIZE,y*Y_PIECE_SIZE);
173   else
174     XCopyArea(currdpy, bmap, currbamp, currgc,0,0,X_PIECE_SIZE,
175 	     Y_PIECE_SIZE, x*X_PIECE_SIZE,y*Y_PIECE_SIZE);
176 }
177 
draw_obstacle(x,y)178 draw_obstacle(x,y)
179      int x,y;
180 {
181   Pixmap bbl, wbl;
182 
183   if (currdpy == dpy) {
184 	bbl = wblock;
185 	wbl = bblock;
186   }
187   else {
188 	bbl = owblock;
189 	wbl = obblock;
190   }
191 
192   if (obstacle_color==WHITE)
193     XCopyArea(currdpy, wbl, currbamp, currgc,0,0,X_PIECE_SIZE,
194 	     Y_PIECE_SIZE, x*X_PIECE_SIZE,y*Y_PIECE_SIZE);
195   else
196     XCopyArea(currdpy, bbl, currbamp, currgc,0,0,X_PIECE_SIZE,
197 	     Y_PIECE_SIZE, x*X_PIECE_SIZE,y*Y_PIECE_SIZE);
198 }
199 
grid(cdpy,cmap,cgc)200 grid(cdpy, cmap, cgc)
201 Display *cdpy;
202 Pixmap cmap;
203 GC cgc;
204 {
205   short x;
206 
207   XDrawRectangle(cdpy, cmap, cgc, 0, 0, xwin-1, ywin-1);
208   for (x=0;x<return_x_size();x++)
209 	XDrawLine(cdpy, cmap, cgc, x*X_PIECE_SIZE, 0, x*X_PIECE_SIZE, ywin-1);
210   for(x=0;x<return_y_size();x++)
211 	XDrawLine(cdpy, cmap, cgc, 0, x*Y_PIECE_SIZE, xwin-1, x*Y_PIECE_SIZE);
212 }
213 
redraw_win(cdpy,cwin,cmap,cgc)214 redraw_win(cdpy, cwin, cmap, cgc)
215 Display *cdpy;
216 Window cwin;
217 Pixmap cmap;
218 GC cgc;
219 {
220 
221   XCopyArea(cdpy, cmap, cwin, cgc, 0, 0, xwin, ywin, 0, 0);
222   XFlush(cdpy);
223 }
224