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
| #include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/keysym.h>
#include <X11/keysymdef.h>
#include <stdio.h>
#include <stdlib.h>
static Display* dpy; /* X Display */
static GC gc[2]; /* Graphic Context */
static Window root; /* Main root window */
static Window** box; /* box (NYxNX) window array address */
static int X0=0; /* main root wintow x-location */
static int Y0=0; /* main root wintow y-location */
static unsigned NX=8; /* Default Boxes' number in each row */
static unsigned NY=8; /* Default Row's number in game desk */
static XRectangle cell; /* Box cell */
static char* mark[] = { "0", "X" }; /* Marker symbols */
static int posY[5] = {1, 2, 3, 4, 5};
static int posX[5] = {5, 2, 4, 6, 3};
static int QCHESS = 5;
int alloc(unsigned _nx, unsigned _ny) {
void** b; /* box array pointer */
int i, j; /* row & position array index */
NX = _nx; NY = _ny;
box = calloc(NY, sizeof(unsigned long*));
for(i=0; i < NY; i++) {
box[i] = calloc(NX, sizeof(unsigned long));
} /* for */
return(0);
} /* alloc */
int rebox(XEvent* ev) {
int i, j;
for(i=0; i < QCHESS; i++)
if(ev->xexpose.window == box[posY[i]][posX[i]])
XDrawRectangle(dpy,box[posY[i]][posX[i]], gc[0], cell.x-6, cell.y-15,21, 21);
return(0);
} /* rebox */
int reset(XEvent* ev) {
int i, y, x;
for(i=0; i < QCHESS; i++) /* check box in row */
if(ev->xbutton.window == box[posY[i]][posX[i]]){
printf("ok\n");
break;
}
//printf("%d==%d\n", posY[i], posX[i]);
if(i == QCHESS) return(1);
//Xt=i; Yt=i;
XDrawRectangle(dpy, box[posY[i]][posX[i]], gc[1], cell.x-6, cell.y-15,21, 21);
return 0;
} /* reset */
int xcustom() {
static unsigned XSP=0; /* x-space between 2 boxes in any row */
static unsigned YSP=0; /* y-space between 2 rows */
static unsigned BW=32; /* Box width */
static unsigned BH=32; /* Box height */
int x, y; /* windows' location */
unsigned w, h; /* windows' size */
int dx, dy; /* interwindow distance */
int src; /* screen number */
int depth; /* screen depth */
XSetWindowAttributes attr; /* created Window attributes */
XSizeHints hint; /* WM connection structure */
XFontStruct* fn; /* Font parameters structure */
char* fontname = "9x15"; /* default font name */
int i,j; /* windows' index */
/* get X defaults */
src = DefaultScreen(dpy);
depth = DefaultDepth(dpy, src);
gc[0] = DefaultGC(dpy, src);
/* Font custom */
if((fn = XLoadQueryFont(dpy, fontname)) == NULL)
return(puts("Incorrect FontStruct id"));
XSetFont(dpy, gc[0], fn->fid);
/* box cell geometry */
cell.width = fn->max_bounds.width;
cell.height = fn->max_bounds.ascent + fn->max_bounds.descent;
cell.x = (BW - fn->max_bounds.width)/2;
cell.y = BH/2 + (fn->max_bounds.ascent - fn->max_bounds.descent)/2;
/* Main root window */
w = NX*BW + 9;
h = NY*BH + 9;
attr.override_redirect = False;
attr.background_pixel = WhitePixel(dpy, src);
x = X0; y = Y0;
root = XCreateWindow(dpy, DefaultRootWindow(dpy), x, y, w, h,
1, depth, InputOutput, CopyFromParent,
(CWOverrideRedirect | CWBackPixel), &attr);
hint.flags = (PMinSize | PMaxSize | PPosition);
hint.min_width = hint.max_width = w;
hint.min_height = hint.max_height = h;
hint.x = x; hint.y = y;
XSetNormalHints(dpy, root, &hint);
/* Box root's subwindows */
attr.override_redirect = True;
attr.background_pixel = WhitePixel(dpy, src); /* WhitePixel(dpy, src); */
attr.event_mask = (KeyPressMask | ExposureMask);
w = BW; h = BH; dx = w+1; dy=h+1;
for(i=0, y=0; i < NY; i++, y += dy)
for(j=0, x=0; j < NX; j++, x += dx)
{
box[i][j] = XCreateWindow(dpy, root, x, y, w, h, 1,
depth, InputOutput, CopyFromParent,
(CWOverrideRedirect | CWBackPixel | CWEventMask), &attr);
}
/* Display windows */
XMapWindow(dpy, root);
XMapSubwindows(dpy, root);
///for(i=0; i<NY; i++)
// for(j=0; j<NY; j++)
// XMapSubwindows(dpy, box[i][j]);
XStoreName(dpy, root, "xpat");
/* Create clear GC */
gc[1] = XCreateGC(dpy, root, 0, 0);
XCopyGC(dpy, gc[0], GCFont, gc[1]);
XSetForeground(dpy, gc[1], WhitePixel(dpy, src));
return(0);
}
int move(XEvent* ev) {
int k, l;
int x, y;
printf("%d %d\n", x, y);
for(k=0; k< NY; k++)
for(l=0;l< NX;l++){
if(ev->xbutton.window == box[k][l]){
printf("i-%d====j-%d\n", k, l);
XClearWindow(dpy,box[k][l]);
XDrawRectangle(dpy,box[k][l], gc[0], ev->xmotion.x-11, ev->xmotion.y-11,21, 21);
}
}
return 0;
}
int dispatch() {
XEvent event; /* Event structure */
int flag = 0; /* exit flag */
int m=0,i,j;
for(i=0;i<NY; i++)
for(j=0; j<NX; j++)
XSelectInput ( dpy, box[i][j], ExposureMask | ButtonPressMask | ButtonReleaseMask | PointerMotionMask | Button1MotionMask | Button2MotionMask );
while(flag == 0) {
XNextEvent(dpy, &event);
switch(event.type) {
case Expose: rebox(&event);
break;
case ButtonPress: if(reset(&event)==0) m=0;
case MotionNotify: if((event.xkey.state & Button1Mask) && m==0)
move(&event);
break;
case ButtonRelease: m=1; break;
// case KeyPress: flag = kbdrive(&event);;
// break;
//default: break;
} /* switch */
} /* while */
return(0);
} /* dispatch */
int main(int argc, char* argv[]) {
alloc(NX, NY);
dpy = XOpenDisplay(NULL);
if(xcustom() > 0)
return(1);
dispatch();
XDestroySubwindows(dpy, root);
XDestroyWindow(dpy, root);
XCloseDisplay(dpy);
//dealloc(pos, box);
return(0);
} |