-
Notifications
You must be signed in to change notification settings - Fork 4
/
colormaps.c
700 lines (612 loc) · 15.8 KB
/
colormaps.c
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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
/*
* Colormap handling
*/
#include "ctwm.h"
#include <stdio.h>
#include <stdlib.h>
#include "colormaps.h"
#include "screen.h"
/*
* From events.c; imported manually since I'm not listing it in events.h
* because nowhere but here needs it.
*/
extern bool ColortableThrashing;
static Bool UninstallRootColormapQScanner(Display *display, XEvent *ev,
char *args);
/***********************************************************************
*
* Procedure:
* InstallWindowColormaps - install the colormaps for one twm window
*
* Inputs:
* type - type of event that caused the installation
* tmp - for a subset of event types, the address of the
* window structure, whose colormaps are to be installed.
*
***********************************************************************
*
* Previously in events.c
*/
bool
InstallWindowColormaps(int type, TwmWindow *tmp)
{
if(tmp) {
return InstallColormaps(type, &tmp->cmaps);
}
else {
return InstallColormaps(type, NULL);
}
}
bool
InstallColormaps(int type, Colormaps *cmaps)
{
int i, j, n, number_cwins, state;
ColormapWindow **cwins, *cwin, **maxcwin = NULL;
TwmColormap *cmap;
char *row, *scoreboard;
switch(type) {
case EnterNotify:
case LeaveNotify:
case DestroyNotify:
default:
/* Save the colormap to be loaded for when force loading of
* root colormap(s) ends.
*/
Scr->cmapInfo.pushed_cmaps = cmaps;
/* Don't load any new colormap if root colormap(s) has been
* force loaded.
*/
if(Scr->cmapInfo.root_pushes) {
return false;
}
/* Don't reload the current window colormap list.
if (Scr->cmapInfo.cmaps == cmaps)
return false;
*/
if(Scr->cmapInfo.cmaps) {
for(i = Scr->cmapInfo.cmaps->number_cwins,
cwins = Scr->cmapInfo.cmaps->cwins; i-- > 0; cwins++) {
(*cwins)->colormap->state &= ~CM_INSTALLABLE;
}
}
Scr->cmapInfo.cmaps = cmaps;
break;
case PropertyNotify:
case VisibilityNotify:
case ColormapNotify:
break;
}
number_cwins = Scr->cmapInfo.cmaps->number_cwins;
cwins = Scr->cmapInfo.cmaps->cwins;
scoreboard = Scr->cmapInfo.cmaps->scoreboard;
ColortableThrashing = false; /* in case installation aborted */
state = CM_INSTALLED;
for(i = 0; i < number_cwins; i++) {
cwins[i]->colormap->state &= ~CM_INSTALL;
}
for(i = n = 0; i < number_cwins && n < Scr->cmapInfo.maxCmaps; i++) {
cwin = cwins[i];
cmap = cwin->colormap;
if(cmap->state & CM_INSTALL) {
continue;
}
cmap->state |= CM_INSTALLABLE;
cmap->w = cwin->w;
if(cwin->visibility != VisibilityFullyObscured) {
row = scoreboard + (i * (i - 1) / 2);
for(j = 0; j < i; j++)
if(row[j] && (cwins[j]->colormap->state & CM_INSTALL)) {
break;
}
if(j != i) {
continue;
}
n++;
maxcwin = &cwins[i];
state &= (cmap->state & CM_INSTALLED);
cmap->state |= CM_INSTALL;
}
}
// Hack: special-case startup
if(!dpy) {
return true;
}
Scr->cmapInfo.first_req = NextRequest(dpy);
for(; n > 0 && maxcwin >= &cwins[0]; maxcwin--) {
cmap = (*maxcwin)->colormap;
if(cmap->state & CM_INSTALL) {
cmap->state &= ~CM_INSTALL;
if(!(state & CM_INSTALLED)) {
cmap->install_req = NextRequest(dpy);
/* printf ("XInstallColormap : %x, %x\n", cmap, cmap->c); */
XInstallColormap(dpy, cmap->c);
}
cmap->state |= CM_INSTALLED;
n--;
}
}
return true;
}
/***********************************************************************
*
* Procedures:
* <Uni/I>nstallRootColormap - Force (un)loads root colormap(s)
*
* These matching routines provide a mechanism to insure that
* the root colormap(s) is installed during operations like
* rubber banding or menu display that require colors from
* that colormap. Calls may be nested arbitrarily deeply,
* as long as there is one UninstallRootColormap call per
* InstallRootColormap call.
*
* The final UninstallRootColormap will cause the colormap list
* which would otherwise have be loaded to be loaded, unless
* Enter or Leave Notify events are queued, indicating some
* other colormap list would potentially be loaded anyway.
***********************************************************************
*
* Previously in events.c
*/
void
InstallRootColormap(void)
{
Colormaps *tmp;
if(Scr->cmapInfo.root_pushes == 0) {
/*
* The saving and restoring of cmapInfo.pushed_window here
* is a slimy way to remember the actual pushed list and
* not that of the root window.
*/
tmp = Scr->cmapInfo.pushed_cmaps;
InstallColormaps(0, &Scr->RootColormaps);
Scr->cmapInfo.pushed_cmaps = tmp;
}
Scr->cmapInfo.root_pushes++;
}
/* ARGSUSED*/
static Bool
UninstallRootColormapQScanner(Display *display, XEvent *ev,
char *args)
{
if(!*args) {
if(ev->type == EnterNotify) {
if(ev->xcrossing.mode != NotifyGrab) {
*args = 1;
}
}
else if(ev->type == LeaveNotify) {
if(ev->xcrossing.mode == NotifyNormal) {
*args = 1;
}
}
}
return (False);
}
void
UninstallRootColormap(void)
{
char args;
XEvent dummy;
if(Scr->cmapInfo.root_pushes) {
Scr->cmapInfo.root_pushes--;
}
if(!Scr->cmapInfo.root_pushes) {
/*
* If we have subsequent Enter or Leave Notify events,
* we can skip the reload of pushed colormaps.
*/
XSync(dpy, 0);
args = 0;
XCheckIfEvent(dpy, &dummy, UninstallRootColormapQScanner, &args);
if(!args) {
InstallColormaps(0, Scr->cmapInfo.pushed_cmaps);
}
}
}
/*
* Create a TwmColormap struct and tie it to an [X] Colormap. Places
* that need to mess with colormaps and look up the metainfo we hang off
* them need to look this up and find it via the X Context.
*
* Previously in add_window.c
*/
TwmColormap *
CreateTwmColormap(Colormap c)
{
TwmColormap *cmap;
cmap = malloc(sizeof(TwmColormap));
if(!cmap) {
return NULL;
}
cmap->c = c;
cmap->state = 0;
cmap->install_req = 0;
cmap->w = None;
cmap->refcnt = 1;
if(XSaveContext(dpy, c, ColormapContext, (XPointer) cmap)) {
free(cmap);
return NULL;
}
return (cmap);
}
/*
* Put together a ColormapWindow struct. This is a thing we hang off a
* TwmWindow for some colormap tracking stuff.
*
* Previously in add_window.c
*/
ColormapWindow *
CreateColormapWindow(Window w, bool creating_parent, bool property_window)
{
ColormapWindow *cwin;
TwmColormap *cmap;
XWindowAttributes attributes;
cwin = malloc(sizeof(ColormapWindow));
if(cwin == NULL) {
return NULL;
}
// Common
cwin->w = w;
/*
* Assume that windows in colormap list are
* obscured if we are creating the parent window.
* Otherwise, we assume they are unobscured.
*/
cwin->visibility = creating_parent ?
VisibilityPartiallyObscured : VisibilityUnobscured;
cwin->refcnt = 1;
// Stub for special cases
if(dpy == NULL) {
cwin->colormap = NULL;
cwin->colormap = calloc(1, sizeof(TwmColormap));
cwin->colormap->refcnt = 1;
return cwin;
}
if(!XGetWindowAttributes(dpy, w, &attributes) ||
XSaveContext(dpy, w, ColormapContext, (XPointer) cwin)) {
free(cwin);
return (NULL);
}
if(XFindContext(dpy, attributes.colormap, ColormapContext,
(XPointer *)&cwin->colormap) == XCNOENT) {
cwin->colormap = cmap = CreateTwmColormap(attributes.colormap);
if(!cmap) {
XDeleteContext(dpy, w, ColormapContext);
free(cwin);
return (NULL);
}
}
else {
cwin->colormap->refcnt++;
}
/*
* If this is a ColormapWindow property window and we
* are not monitoring ColormapNotify or VisibilityNotify
* events, we need to.
*/
if(property_window &&
(attributes.your_event_mask &
(ColormapChangeMask | VisibilityChangeMask)) !=
(ColormapChangeMask | VisibilityChangeMask)) {
XSelectInput(dpy, w, attributes.your_event_mask |
(ColormapChangeMask | VisibilityChangeMask));
}
return (cwin);
}
/*
* Do something with looking up stuff from WM_COLORMAPS_WINDOWS (relating
* to windows with their own colormap) and finding or putting this window
* into it.
*
* XXX Someone should figure it out better than that...
*
* Previously in add_window.c
*/
void
FetchWmColormapWindows(TwmWindow *tmp)
{
int i, j;
Window *cmap_windows = NULL;
bool can_free_cmap_windows = false;
int number_cmap_windows = 0;
ColormapWindow **cwins = NULL;
bool previnst;
number_cmap_windows = 0;
previnst = (Scr->cmapInfo.cmaps == &tmp->cmaps && tmp->cmaps.number_cwins);
if(previnst) {
cwins = tmp->cmaps.cwins;
for(i = 0; i < tmp->cmaps.number_cwins; i++) {
cwins[i]->colormap->state = 0;
}
}
if(XGetWMColormapWindows(dpy, tmp->w, &cmap_windows,
&number_cmap_windows) &&
number_cmap_windows > 0) {
/*
* check if the top level is in the list, add to front if not
*/
for(i = 0; i < number_cmap_windows; i++) {
if(cmap_windows[i] == tmp->w) {
break;
}
}
if(i == number_cmap_windows) { /* not in list */
Window *new_cmap_windows =
calloc((number_cmap_windows + 1), sizeof(Window));
if(!new_cmap_windows) {
fprintf(stderr,
"%s: unable to allocate %d element colormap window array\n",
ProgramName, number_cmap_windows + 1);
goto done;
}
new_cmap_windows[0] = tmp->w; /* add to front */
for(i = 0; i < number_cmap_windows; i++) { /* append rest */
new_cmap_windows[i + 1] = cmap_windows[i];
}
XFree(cmap_windows);
can_free_cmap_windows = true; /* do not use XFree any more */
cmap_windows = new_cmap_windows;
number_cmap_windows++;
}
cwins = calloc(number_cmap_windows, sizeof(ColormapWindow *));
if(cwins) {
for(i = 0; i < number_cmap_windows; i++) {
/*
* Copy any existing entries into new list.
*/
for(j = 0; j < tmp->cmaps.number_cwins; j++) {
if(tmp->cmaps.cwins[j]->w == cmap_windows[i]) {
cwins[i] = tmp->cmaps.cwins[j];
cwins[i]->refcnt++;
break;
}
}
/*
* If the colormap window is not being pointed by
* some other applications colormap window list,
* create a new entry.
*/
if(j == tmp->cmaps.number_cwins) {
if(XFindContext(dpy, cmap_windows[i], ColormapContext,
(XPointer *)&cwins[i]) == XCNOENT) {
if((cwins[i] = CreateColormapWindow(cmap_windows[i],
tmp->cmaps.number_cwins == 0,
true)) == NULL) {
int k;
for(k = i + 1; k < number_cmap_windows; k++) {
cmap_windows[k - 1] = cmap_windows[k];
}
i--;
number_cmap_windows--;
}
}
else {
cwins[i]->refcnt++;
}
}
}
}
}
/* No else here, in case we bailed out of clause above.
*/
if(number_cmap_windows == 0) {
number_cmap_windows = 1;
cwins = malloc(sizeof(ColormapWindow *));
if(XFindContext(dpy, tmp->w, ColormapContext, (XPointer *)&cwins[0]) ==
XCNOENT)
cwins[0] = CreateColormapWindow(tmp->w,
tmp->cmaps.number_cwins == 0, false);
else {
cwins[0]->refcnt++;
}
}
if(tmp->cmaps.number_cwins) {
free_cwins(tmp);
}
tmp->cmaps.cwins = cwins;
tmp->cmaps.number_cwins = number_cmap_windows;
if(number_cmap_windows > 1)
tmp->cmaps.scoreboard =
calloc(1, ColormapsScoreboardLength(&tmp->cmaps));
if(previnst) {
InstallColormaps(PropertyNotify, NULL);
}
done:
if(cmap_windows) {
if(can_free_cmap_windows) {
free(cmap_windows);
}
else {
XFree(cmap_windows);
}
}
}
/*
* BumpWindowColormap - rotate our internal copy of WM_COLORMAP_WINDOWS.
* This is the backend for f.colormap.
*
* Previously in functions.c
*/
void
BumpWindowColormap(TwmWindow *tmp, int inc)
{
int i, j;
bool previously_installed;
ColormapWindow **cwins;
if(!tmp) {
return;
}
if(inc && tmp->cmaps.number_cwins > 0) {
cwins = calloc(tmp->cmaps.number_cwins, sizeof(ColormapWindow *));
if(cwins) {
previously_installed = (Scr->cmapInfo.cmaps == &tmp->cmaps &&
tmp->cmaps.number_cwins);
if(previously_installed) {
for(i = tmp->cmaps.number_cwins; i-- > 0;) {
tmp->cmaps.cwins[i]->colormap->state = 0;
}
}
for(i = 0; i < tmp->cmaps.number_cwins; i++) {
j = i - inc;
if(j >= tmp->cmaps.number_cwins) {
j -= tmp->cmaps.number_cwins;
}
else if(j < 0) {
j += tmp->cmaps.number_cwins;
}
cwins[j] = tmp->cmaps.cwins[i];
}
free(tmp->cmaps.cwins);
tmp->cmaps.cwins = cwins;
if(tmp->cmaps.number_cwins > 1)
memset(tmp->cmaps.scoreboard, 0,
ColormapsScoreboardLength(&tmp->cmaps));
if(previously_installed) {
InstallColormaps(PropertyNotify, NULL);
}
}
}
else {
FetchWmColormapWindows(tmp);
}
return;
}
/*
* Handlers for creating and linkin in, as well as deleting, a StdCmap
* for a given XStandardColormap.
*
* Previously in util.c
*/
void
InsertRGBColormap(Atom a, XStandardColormap *maps, int nmaps,
bool replace)
{
StdCmap *sc = NULL;
if(replace) { /* locate existing entry */
for(sc = Scr->StdCmapInfo.head; sc; sc = sc->next) {
if(sc->atom == a) {
break;
}
}
}
if(!sc) { /* no existing, allocate new */
sc = calloc(1, sizeof(StdCmap));
if(!sc) {
fprintf(stderr, "%s: unable to allocate %lu bytes for StdCmap\n",
ProgramName, (unsigned long) sizeof(StdCmap));
return;
}
replace = false; // Didn't find one, can't replace
}
if(replace) { /* just update contents */
if(sc->maps) {
XFree(sc->maps);
}
if(sc == Scr->StdCmapInfo.mru) {
Scr->StdCmapInfo.mru = NULL;
}
}
else { /* else appending */
sc->next = NULL;
sc->atom = a;
if(Scr->StdCmapInfo.tail) {
Scr->StdCmapInfo.tail->next = sc;
}
else {
Scr->StdCmapInfo.head = sc;
}
Scr->StdCmapInfo.tail = sc;
}
sc->nmaps = nmaps;
sc->maps = maps;
return;
}
void
RemoveRGBColormap(Atom a)
{
StdCmap *sc, *prev;
prev = NULL;
for(sc = Scr->StdCmapInfo.head; sc; sc = sc->next) {
if(sc->atom == a) {
break;
}
prev = sc;
}
if(sc) { /* found one */
if(sc->maps) {
XFree(sc->maps);
}
if(prev) {
prev->next = sc->next;
}
if(Scr->StdCmapInfo.head == sc) {
Scr->StdCmapInfo.head = sc->next;
}
if(Scr->StdCmapInfo.tail == sc) {
Scr->StdCmapInfo.tail = prev;
}
if(Scr->StdCmapInfo.mru == sc) {
Scr->StdCmapInfo.mru = NULL;
}
}
return;
}
/*
* Go through all the properties of the root window and setup
* XStandardColormap's and our StdCmap's for any of them that are
* actually RGB_COLORMAP types. We're not actually _checking_ the types,
* just letting XGetRGBColormaps() refuse to handle probably most of
* them. Called during startup.
*
* Previouly in util.c
*/
void
LocateStandardColormaps(void)
{
Atom *atoms;
int natoms;
int i;
atoms = XListProperties(dpy, Scr->Root, &natoms);
for(i = 0; i < natoms; i++) {
XStandardColormap *maps = NULL;
int nmaps;
if(XGetRGBColormaps(dpy, Scr->Root, &maps, &nmaps, atoms[i])) {
/* if got one, then append to current list */
InsertRGBColormap(atoms[i], maps, nmaps, false);
}
}
if(atoms) {
XFree(atoms);
}
return;
}
/*
* Clear out and free TwmWindow.cmaps (struct Colormaps) bits for a window.
*
* Previously in events.c
*/
void
free_cwins(TwmWindow *tmp)
{
int i;
TwmColormap *cmap;
if(tmp->cmaps.number_cwins) {
for(i = 0; i < tmp->cmaps.number_cwins; i++) {
if(--tmp->cmaps.cwins[i]->refcnt == 0) {
cmap = tmp->cmaps.cwins[i]->colormap;
if(--cmap->refcnt == 0) {
XDeleteContext(dpy, cmap->c, ColormapContext);
free(cmap);
}
XDeleteContext(dpy, tmp->cmaps.cwins[i]->w, ColormapContext);
free(tmp->cmaps.cwins[i]);
}
}
free(tmp->cmaps.cwins);
if(tmp->cmaps.number_cwins > 1) {
free(tmp->cmaps.scoreboard);
tmp->cmaps.scoreboard = NULL;
}
tmp->cmaps.number_cwins = 0;
}
}