-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcmap.c
More file actions
356 lines (297 loc) · 9.49 KB
/
Copy pathcmap.c
File metadata and controls
356 lines (297 loc) · 9.49 KB
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
/* cmap.c */
#include "stdlib.h"
#include "stdio.h"
#include <Xm/DrawingA.h>
#include <Xm/Form.h>
#include "color.h"
#include "rgb.h"
#include "log.h"
#include "cmap.h"
#include "time.h"
extern Display *theDisplay;
extern Visual *theVisual;
extern Colormap theColormap;
extern Screen *theScreen;
extern GC theGC;
extern Pixel thePixel;
static Pixel theFreeCells [256];
static int numFreeCells = 0;
static int verbose = 1;
void logVerbose (int on) {
verbose = on;
}
static void freeCell (Pixel pix) {
XFreeColors (theDisplay, theColormap, &pix, 1, 0);
}
/* currently unused */
Boolean allocCell (Pixel pix) {
unsigned long plane_mask[1];
unsigned long pixels_return[256];
unsigned cells, numCells, found;
Pixel freePix;
found = FALSE;
cells = 1;
numCells = 0;
while ((cells != 0) && (!found)) {
cells =
XAllocColorCells(theDisplay, theColormap, TRUE, plane_mask,
0, &pixels_return[numCells], 1);
if (cells) {
if (pix == pixels_return[numCells])
found = TRUE;
numCells++;
}
}
for (freePix=0; freePix<numCells; freePix++) {
if (freePix != pix)
freeCell (pixels_return[freePix]);
}
return found;
}
Boolean setCell (int r, int g, int b) {
XColor xcolor;
int cells, numCells;
unsigned long plane_mask[1];
unsigned long pixels_return[256];
xcolor.pixel = thePixel;
xcolor.flags = DoRed | DoGreen | DoBlue ;
xcolor.red = r * 256;
xcolor.green = g * 256;
xcolor.blue = b * 256;
numCells = 0;
cells = 1;
while (cells != 0) {
cells =
XAllocColorCells(theDisplay, theColormap, TRUE, plane_mask,
0, &pixels_return[numCells], 1);
if (cells) {
if (thePixel == pixels_return[numCells])
XStoreColor (theDisplay, theColormap, &xcolor);
numCells++;
}
}
XFreeColors (theDisplay, theColormap, pixels_return, numCells, 0);
return TRUE;
}
static void countFreeCells () {
unsigned long plane_mask[1];
unsigned long pixels_return[256];
unsigned cells;
cells = 1;
numFreeCells = 0;
while ((cells != 0)) {
cells =
XAllocColorCells(theDisplay, theColormap, TRUE, plane_mask,
0, &pixels_return[numFreeCells], 1);
if (cells) {
theFreeCells[numFreeCells] = pixels_return[numFreeCells];
numFreeCells++;
}
}
XFreeColors (theDisplay, theColormap, pixels_return, numFreeCells, 0);
}
/* allocates storage for s1 + s2, cats them together, and frees the original */
static void appendString (char **s1, char *s2) {
char *tmp;
tmp = (char *) malloc(strlen(*s1)+strlen(s2)+1);
strcpy (tmp, *s1);
strcat (tmp, s2);
free (*s1);
*s1 = tmp;
}
void checkCells () {
XColor theColors[256];
static XColor oldColors[256];
static int firstTime = TRUE;
int i, changed = 0;
char text [256];
char *totalLog = NULL;
time_t t ;
struct tm *today = localtime(&t);
t = time(0);
today = localtime(&t);
for (i = 0; i < 256; i++) {
theColors[i].pixel = (Pixel) i;
theColors[i].flags = DoRed | DoGreen | DoBlue;
}
XQueryColors (theDisplay, theColormap, theColors, 256);
for (i = 0; i < 256; i++) {
if (!firstTime)
if ( (theColors[i].red != oldColors[i].red) ||
(theColors[i].green != oldColors[i].green) ||
(theColors[i].blue != oldColors[i].blue) ) {
changed++;
if (verbose) {
sprintf(text, "pixel %3d changed to $%04X, $%04X, $%04X at %s\n",
i, theColors[i].red, theColors[i].green, theColors[i].blue, theTime());
if (!totalLog)
totalLog = strdup ("");
appendString (&totalLog, text);
}
}
oldColors[i].red = theColors[i].red;
oldColors[i].green = theColors[i].green;
oldColors[i].blue = theColors[i].blue;
}
firstTime = FALSE;
if (!verbose)
if (changed) {
if (!totalLog)
totalLog = strdup ("");
sprintf (text, "%d pixels changed at %s\n", changed, theTime());
appendString (&totalLog, text);
}
sprintf (text, "---\n");
if (totalLog)
appendString (&totalLog, text);
logString (totalLog);
}
void blackOutCmap () {
XColor xcolor;
int cells, cellNum;
unsigned long plane_mask[1];
unsigned long pixels_return[256];
xcolor.flags = DoRed | DoGreen | DoBlue ;
xcolor.red = 0;
xcolor.green = 0;
xcolor.blue = 0;
cells = 1;
cellNum = 0;
while ((cells != 0)) {
cells =
XAllocColorCells(theDisplay, theColormap, TRUE, plane_mask,
0, &pixels_return[cellNum], 1);
if (cells) {
xcolor.pixel = pixels_return[cellNum];
XStoreColor (theDisplay, theColormap, &xcolor);
cellNum++;
}
}
XFreeColors (theDisplay, theColormap, pixels_return, cellNum, 0);
}
Boolean isFree (Pixel pix) {
int i = 0;
Boolean found = FALSE;
while ((i < numFreeCells) && (!found))
if (theFreeCells[i++] == pix)
found = TRUE;
return found;
}
int cellsFree() {
countFreeCells();
return numFreeCells;
}
void cmapDraw (Widget theWidget, int windowWidth, int windowHeight) {
int width_diff, height_diff, rect_width, rect_height;
int i, x, y;
unsigned int width, height;
XGCValues theValues;
static GC gc = NULL;
if (!gc) {
gc = XCreateGC (theDisplay, RootWindowOfScreen(theScreen), 0, 0);
XSetForeground(theDisplay, gc, XBlackPixelOfScreen(theScreen));
XSetBackground(theDisplay, gc, XWhitePixelOfScreen(theScreen));
}
/* save the foreground */
XGetGCValues (theDisplay, gc, GCForeground, &theValues);
width_diff = windowWidth % 16;
height_diff = windowHeight % 16;
rect_width = windowWidth / 16;
rect_height = windowHeight / 16;
for (i=0; i<256; i++) {
XSetForeground(theDisplay, gc, i);
if ((i%16) < width_diff) {
x = (i%16)*rect_width+(i%16);
width = rect_width+1;
} else {
x = (i%16)*rect_width+width_diff;
width = rect_width;
}
if ((i/16) < height_diff) {
y = (i/16)*rect_height+(i/16);
height = rect_height+1;
} else {
y = (i/16)*rect_height+height_diff;
height = rect_height;
}
XFillRectangle
(theDisplay, XtWindow(theWidget), gc, x, y, width, height);
}
/* restore the foreground */
XSetForeground (theDisplay, gc, theValues.foreground);
}
void
cmapExposeResize(Widget w, XtPointer unused, XmDrawingAreaCallbackStruct *cbs) {
Status s;
XWindowAttributes winAttr;
if (cbs->window) {
s = XGetWindowAttributes (theDisplay, cbs->window, &winAttr);
cmapDraw(w, winAttr.width, winAttr.height);
}
}
void cmapReport (Window w, int x, int y) {
Status s;
XWindowAttributes winAttr;
static Pixel lastPix = 0;
s = XGetWindowAttributes (theDisplay, w, &winAttr);
thePixel = ( (y / 16) * 16 ) + (x / 16);
if (thePixel != lastPix)
rgbReset();
lastPix = thePixel;
}
static void
cmapInput (Widget w, XtPointer xtp, XmDrawingAreaCallbackStruct *cbs) {
if (((XAnyEvent *)cbs->event)->type == 4) {
XButtonEvent *e = (XButtonEvent *) cbs->event;
cmapReport (e->window, e->x, e->y);
}
}
static void cmapMotion (Widget w, XtPointer idp, XEvent *e, Boolean *cont) {
XMotionEvent *event = (XMotionEvent *) e;
*cont = True;
cmapReport (event->window, event->x, event->y);
}
static void cmapCmapChanged (Widget w, XtPointer idp, XEvent *e, Boolean *cont)
{
char buf[512];
XColormapEvent *event = (XColormapEvent *) e;
*cont = True;
if (event->colormap == None) {
sprintf (buf, "Colormap freed\n", event->colormap);
} else {
if (event->state == ColormapInstalled)
sprintf (buf, "Colormap %x installed at %s\n",
event->colormap, theTime());
else
sprintf (buf, "Colormap %x uninstalled at %s\n",
event->colormap, theTime());
}
logString (buf);
}
void cmapCreate (Widget parent) {
Arg args[16];
Widget cmapCanvas;
int n;
n = 0;
XtSetArg (args[n], XmNtopAttachment, XmATTACH_FORM); n++;
XtSetArg (args[n], XmNtopOffset, 0); n++;
XtSetArg (args[n], XmNbottomAttachment, XmATTACH_FORM); n++;
XtSetArg (args[n], XmNbottomOffset, 0); n++;
XtSetArg (args[n], XmNleftAttachment, XmATTACH_FORM); n++;
XtSetArg (args[n], XmNleftOffset, 0); n++;
XtSetArg (args[n], XmNrightAttachment, XmATTACH_FORM); n++;
XtSetArg (args[n], XmNrightOffset, 0); n++;
cmapCanvas = XtCreateManagedWidget ("cmapCanvas", xmDrawingAreaWidgetClass,
parent, args, n);
XtAddCallback (cmapCanvas, XmNexposeCallback,
(XtCallbackProc) cmapExposeResize, NULL);
XtAddCallback (cmapCanvas, XmNresizeCallback,
(XtCallbackProc) cmapExposeResize, NULL);
XtAddCallback (cmapCanvas, XmNinputCallback,
(XtCallbackProc) cmapInput, NULL);
XtAddEventHandler (cmapCanvas, Button1MotionMask,
False, (XtEventHandler) cmapMotion, NULL);
XtAddEventHandler (cmapCanvas, ColormapChangeMask,
False, (XtEventHandler) cmapCmapChanged, NULL);
countFreeCells();
}