forked from robclark/xserver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
colormap.c
2764 lines (2533 loc) · 71.3 KB
/
colormap.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
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/***********************************************************
Copyright 1987, 1998 The Open Group
Permission to use, copy, modify, distribute, and sell this software and its
documentation for any purpose is hereby granted without fee, provided that
the above copyright notice appear in all copies and that both that
copyright notice and this permission notice appear in supporting
documentation.
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Except as contained in this notice, the name of The Open Group shall not be
used in advertising or otherwise to promote the sale, use or other dealings
in this Software without prior written authorization from The Open Group.
Copyright 1987 by Digital Equipment Corporation, Maynard, Massachusetts.
All Rights Reserved
Permission to use, copy, modify, and distribute this software and its
documentation for any purpose and without fee is hereby granted,
provided that the above copyright notice appear in all copies and that
both that copyright notice and this permission notice appear in
supporting documentation, and that the name of Digital not be
used in advertising or publicity pertaining to distribution of the
software without specific, written prior permission.
DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
SOFTWARE.
******************************************************************/
#ifdef HAVE_DIX_CONFIG_H
#include <dix-config.h>
#endif
#include <X11/X.h>
#include <X11/Xproto.h>
#include <stdio.h>
#include <string.h>
#include <strings.h>
#include "misc.h"
#include "dix.h"
#include "dixstruct.h"
#include "colormapst.h"
#include "os.h"
#include "scrnintstr.h"
#include "resource.h"
#include "windowstr.h"
#include "privates.h"
#include "xace.h"
static Pixel FindBestPixel(
EntryPtr /*pentFirst*/,
int /*size*/,
xrgb * /*prgb*/,
int /*channel*/
);
static int AllComp(
EntryPtr /*pent*/,
xrgb * /*prgb*/
);
static int RedComp(
EntryPtr /*pent*/,
xrgb * /*prgb*/
);
static int GreenComp(
EntryPtr /*pent*/,
xrgb * /*prgb*/
);
static int BlueComp(
EntryPtr /*pent*/,
xrgb * /*prgb*/
);
static void FreePixels(
ColormapPtr /*pmap*/,
int /*client*/
);
static void CopyFree(
int /*channel*/,
int /*client*/,
ColormapPtr /*pmapSrc*/,
ColormapPtr /*pmapDst*/
);
static void FreeCell(
ColormapPtr /*pmap*/,
Pixel /*i*/,
int /*channel*/
);
static void UpdateColors(
ColormapPtr /*pmap*/
);
static int AllocDirect(
int /*client*/,
ColormapPtr /*pmap*/,
int /*c*/,
int /*r*/,
int /*g*/,
int /*b*/,
Bool /*contig*/,
Pixel * /*pixels*/,
Pixel * /*prmask*/,
Pixel * /*pgmask*/,
Pixel * /*pbmask*/
);
static int AllocPseudo(
int /*client*/,
ColormapPtr /*pmap*/,
int /*c*/,
int /*r*/,
Bool /*contig*/,
Pixel * /*pixels*/,
Pixel * /*pmask*/,
Pixel ** /*pppixFirst*/
);
static Bool AllocCP(
ColormapPtr /*pmap*/,
EntryPtr /*pentFirst*/,
int /*count*/,
int /*planes*/,
Bool /*contig*/,
Pixel * /*pixels*/,
Pixel * /*pMask*/
);
static Bool AllocShared(
ColormapPtr /*pmap*/,
Pixel * /*ppix*/,
int /*c*/,
int /*r*/,
int /*g*/,
int /*b*/,
Pixel /*rmask*/,
Pixel /*gmask*/,
Pixel /*bmask*/,
Pixel * /*ppixFirst*/
);
static int FreeCo(
ColormapPtr /*pmap*/,
int /*client*/,
int /*color*/,
int /*npixIn*/,
Pixel * /*ppixIn*/,
Pixel /*mask*/
);
static int TellNoMap(
WindowPtr /*pwin*/,
Colormap * /*pmid*/
);
static void FindColorInRootCmap (
ColormapPtr /* pmap */,
EntryPtr /* pentFirst */,
int /* size */,
xrgb* /* prgb */,
Pixel* /* pPixel */,
int /* channel */,
ColorCompareProcPtr /* comp */
);
#define NUMRED(vis) ((vis->redMask >> vis->offsetRed) + 1)
#define NUMGREEN(vis) ((vis->greenMask >> vis->offsetGreen) + 1)
#define NUMBLUE(vis) ((vis->blueMask >> vis->offsetBlue) + 1)
#if COMPOSITE
#define ALPHAMASK(vis) ((vis)->nplanes < 32 ? 0 : \
(CARD32) ~((vis)->redMask|(vis)->greenMask|(vis)->blueMask))
#else
#define ALPHAMASK(vis) 0
#endif
#define RGBMASK(vis) (vis->redMask | vis->greenMask | vis->blueMask | ALPHAMASK(vis))
/* GetNextBitsOrBreak(bits, mask, base) --
* (Suggestion: First read the macro, then read this explanation.
*
* Either generate the next value to OR in to a pixel or break out of this
* while loop
*
* This macro is used when we're trying to generate all 2^n combinations of
* bits in mask. What we're doing here is counting in binary, except that
* the bits we use to count may not be contiguous. This macro will be
* called 2^n times, returning a different value in bits each time. Then
* it will cause us to break out of a surrounding loop. (It will always be
* called from within a while loop.)
* On call: mask is the value we want to find all the combinations for
* base has 1 bit set where the least significant bit of mask is set
*
* For example,if mask is 01010, base should be 0010 and we count like this:
* 00010 (see this isn't so hard),
* then we add base to bits and get 0100. (bits & ~mask) is (0100 & 0100) so
* we add that to bits getting (0100 + 0100) =
* 01000 for our next value.
* then we add 0010 to get
* 01010 and we're done (easy as 1, 2, 3)
*/
#define GetNextBitsOrBreak(bits, mask, base) \
if((bits) == (mask)) \
break; \
(bits) += (base); \
while((bits) & ~(mask)) \
(bits) += ((bits) & ~(mask));
/* ID of server as client */
#define SERVER_ID 0
typedef struct _colorResource
{
Colormap mid;
int client;
} colorResource;
/* Invariants:
* refcnt == 0 means entry is empty
* refcnt > 0 means entry is useable by many clients, so it can't be changed
* refcnt == AllocPrivate means entry owned by one client only
* fShared should only be set if refcnt == AllocPrivate, and only in red map
*/
/**
* Create and initialize the color map
*
* \param mid resource to use for this colormap
* \param alloc 1 iff all entries are allocated writable
*/
int
CreateColormap (Colormap mid, ScreenPtr pScreen, VisualPtr pVisual,
ColormapPtr *ppcmap, int alloc, int client)
{
int class, size;
unsigned long sizebytes;
ColormapPtr pmap;
EntryPtr pent;
int i;
Pixel *ppix, **pptr;
class = pVisual->class;
if(!(class & DynamicClass) && (alloc != AllocNone) && (client != SERVER_ID))
return BadMatch;
size = pVisual->ColormapEntries;
sizebytes = (size * sizeof(Entry)) +
(MAXCLIENTS * sizeof(Pixel *)) +
(MAXCLIENTS * sizeof(int));
if ((class | DynamicClass) == DirectColor)
sizebytes *= 3;
sizebytes += sizeof(ColormapRec);
if (mid == pScreen->defColormap) {
pmap = malloc(sizebytes);
if (!pmap)
return BadAlloc;
if (!dixAllocatePrivates(&pmap->devPrivates, PRIVATE_COLORMAP)) {
free (pmap);
return BadAlloc;
}
} else {
pmap = _dixAllocateObjectWithPrivates(sizebytes, sizebytes,
offsetof(ColormapRec, devPrivates), PRIVATE_COLORMAP);
if (!pmap)
return BadAlloc;
}
pmap->red = (EntryPtr)((char *)pmap + sizeof(ColormapRec));
sizebytes = size * sizeof(Entry);
pmap->clientPixelsRed = (Pixel **)((char *)pmap->red + sizebytes);
pmap->numPixelsRed = (int *)((char *)pmap->clientPixelsRed +
(MAXCLIENTS * sizeof(Pixel *)));
pmap->mid = mid;
pmap->flags = 0; /* start out with all flags clear */
if(mid == pScreen->defColormap)
pmap->flags |= IsDefault;
pmap->pScreen = pScreen;
pmap->pVisual = pVisual;
pmap->class = class;
if ((class | DynamicClass) == DirectColor)
size = NUMRED(pVisual);
pmap->freeRed = size;
memset((char *) pmap->red, 0, (int)sizebytes);
memset((char *) pmap->numPixelsRed, 0, MAXCLIENTS * sizeof(int));
for (pptr = &pmap->clientPixelsRed[MAXCLIENTS]; --pptr >= pmap->clientPixelsRed; )
*pptr = (Pixel *)NULL;
if (alloc == AllocAll)
{
if (class & DynamicClass)
pmap->flags |= AllAllocated;
for (pent = &pmap->red[size - 1]; pent >= pmap->red; pent--)
pent->refcnt = AllocPrivate;
pmap->freeRed = 0;
ppix = malloc(size * sizeof(Pixel));
if (!ppix)
{
free(pmap);
return BadAlloc;
}
pmap->clientPixelsRed[client] = ppix;
for(i = 0; i < size; i++)
ppix[i] = i;
pmap->numPixelsRed[client] = size;
}
if ((class | DynamicClass) == DirectColor)
{
pmap->freeGreen = NUMGREEN(pVisual);
pmap->green = (EntryPtr)((char *)pmap->numPixelsRed +
(MAXCLIENTS * sizeof(int)));
pmap->clientPixelsGreen = (Pixel **)((char *)pmap->green + sizebytes);
pmap->numPixelsGreen = (int *)((char *)pmap->clientPixelsGreen +
(MAXCLIENTS * sizeof(Pixel *)));
pmap->freeBlue = NUMBLUE(pVisual);
pmap->blue = (EntryPtr)((char *)pmap->numPixelsGreen +
(MAXCLIENTS * sizeof(int)));
pmap->clientPixelsBlue = (Pixel **)((char *)pmap->blue + sizebytes);
pmap->numPixelsBlue = (int *)((char *)pmap->clientPixelsBlue +
(MAXCLIENTS * sizeof(Pixel *)));
memset((char *) pmap->green, 0, (int)sizebytes);
memset((char *) pmap->blue, 0, (int)sizebytes);
memmove((char *) pmap->clientPixelsGreen,
(char *) pmap->clientPixelsRed,
MAXCLIENTS * sizeof(Pixel *));
memmove((char *) pmap->clientPixelsBlue,
(char *) pmap->clientPixelsRed,
MAXCLIENTS * sizeof(Pixel *));
memset((char *) pmap->numPixelsGreen, 0, MAXCLIENTS * sizeof(int));
memset((char *) pmap->numPixelsBlue, 0, MAXCLIENTS * sizeof(int));
/* If every cell is allocated, mark its refcnt */
if (alloc == AllocAll)
{
size = pmap->freeGreen;
for(pent = &pmap->green[size-1]; pent >= pmap->green; pent--)
pent->refcnt = AllocPrivate;
pmap->freeGreen = 0;
ppix = malloc(size * sizeof(Pixel));
if (!ppix)
{
free(pmap->clientPixelsRed[client]);
free(pmap);
return BadAlloc;
}
pmap->clientPixelsGreen[client] = ppix;
for(i = 0; i < size; i++)
ppix[i] = i;
pmap->numPixelsGreen[client] = size;
size = pmap->freeBlue;
for(pent = &pmap->blue[size-1]; pent >= pmap->blue; pent--)
pent->refcnt = AllocPrivate;
pmap->freeBlue = 0;
ppix = malloc(size * sizeof(Pixel));
if (!ppix)
{
free(pmap->clientPixelsGreen[client]);
free(pmap->clientPixelsRed[client]);
free(pmap);
return BadAlloc;
}
pmap->clientPixelsBlue[client] = ppix;
for(i = 0; i < size; i++)
ppix[i] = i;
pmap->numPixelsBlue[client] = size;
}
}
pmap->flags |= BeingCreated;
if (!AddResource(mid, RT_COLORMAP, (pointer)pmap))
return BadAlloc;
/*
* Security creation/labeling check
*/
i = XaceHook(XACE_RESOURCE_ACCESS, clients[client], mid, RT_COLORMAP,
pmap, RT_NONE, NULL, DixCreateAccess);
if (i != Success) {
FreeResource(mid, RT_NONE);
return i;
}
/* If the device wants a chance to initialize the colormap in any way,
* this is it. In specific, if this is a Static colormap, this is the
* time to fill in the colormap's values */
if (!(*pScreen->CreateColormap)(pmap))
{
FreeResource (mid, RT_NONE);
return BadAlloc;
}
pmap->flags &= ~BeingCreated;
*ppcmap = pmap;
return Success;
}
/**
*
* \param value must conform to DeleteType
*/
int
FreeColormap (pointer value, XID mid)
{
int i;
EntryPtr pent;
ColormapPtr pmap = (ColormapPtr)value;
if(CLIENT_ID(mid) != SERVER_ID)
{
(*pmap->pScreen->UninstallColormap) (pmap);
WalkTree(pmap->pScreen, (VisitWindowProcPtr)TellNoMap, (pointer) &mid);
}
/* This is the device's chance to undo anything it needs to, especially
* to free any storage it allocated */
(*pmap->pScreen->DestroyColormap)(pmap);
if(pmap->clientPixelsRed)
{
for(i = 0; i < MAXCLIENTS; i++)
free(pmap->clientPixelsRed[i]);
}
if ((pmap->class == PseudoColor) || (pmap->class == GrayScale))
{
for(pent = &pmap->red[pmap->pVisual->ColormapEntries - 1];
pent >= pmap->red;
pent--)
{
if(pent->fShared)
{
if (--pent->co.shco.red->refcnt == 0)
free(pent->co.shco.red);
if (--pent->co.shco.green->refcnt == 0)
free(pent->co.shco.green);
if (--pent->co.shco.blue->refcnt == 0)
free(pent->co.shco.blue);
}
}
}
if((pmap->class | DynamicClass) == DirectColor)
{
for(i = 0; i < MAXCLIENTS; i++)
{
free(pmap->clientPixelsGreen[i]);
free(pmap->clientPixelsBlue[i]);
}
}
if (pmap->flags & IsDefault) {
dixFreePrivates(pmap->devPrivates, PRIVATE_COLORMAP);
free(pmap);
} else
dixFreeObjectWithPrivates(pmap, PRIVATE_COLORMAP);
return Success;
}
/* Tell window that pmid has disappeared */
static int
TellNoMap (WindowPtr pwin, Colormap *pmid)
{
xEvent xE;
if (wColormap(pwin) == *pmid)
{
/* This should be call to DeliverEvent */
xE.u.u.type = ColormapNotify;
xE.u.colormap.window = pwin->drawable.id;
xE.u.colormap.colormap = None;
xE.u.colormap.new = TRUE;
xE.u.colormap.state = ColormapUninstalled;
#ifdef PANORAMIX
if(noPanoramiXExtension || !pwin->drawable.pScreen->myNum)
#endif
DeliverEvents(pwin, &xE, 1, (WindowPtr)NULL);
if (pwin->optional) {
pwin->optional->colormap = None;
CheckWindowOptionalNeed (pwin);
}
}
return WT_WALKCHILDREN;
}
/* Tell window that pmid got uninstalled */
int
TellLostMap (WindowPtr pwin, pointer value)
{
Colormap *pmid = (Colormap *)value;
xEvent xE;
#ifdef PANORAMIX
if(!noPanoramiXExtension && pwin->drawable.pScreen->myNum)
return WT_STOPWALKING;
#endif
if (wColormap(pwin) == *pmid)
{
/* This should be call to DeliverEvent */
xE.u.u.type = ColormapNotify;
xE.u.colormap.window = pwin->drawable.id;
xE.u.colormap.colormap = *pmid;
xE.u.colormap.new = FALSE;
xE.u.colormap.state = ColormapUninstalled;
DeliverEvents(pwin, &xE, 1, (WindowPtr)NULL);
}
return WT_WALKCHILDREN;
}
/* Tell window that pmid got installed */
int
TellGainedMap (WindowPtr pwin, pointer value)
{
Colormap *pmid = (Colormap *)value;
xEvent xE;
#ifdef PANORAMIX
if(!noPanoramiXExtension && pwin->drawable.pScreen->myNum)
return WT_STOPWALKING;
#endif
if (wColormap (pwin) == *pmid)
{
/* This should be call to DeliverEvent */
xE.u.u.type = ColormapNotify;
xE.u.colormap.window = pwin->drawable.id;
xE.u.colormap.colormap = *pmid;
xE.u.colormap.new = FALSE;
xE.u.colormap.state = ColormapInstalled;
DeliverEvents(pwin, &xE, 1, (WindowPtr)NULL);
}
return WT_WALKCHILDREN;
}
int
CopyColormapAndFree (Colormap mid, ColormapPtr pSrc, int client)
{
ColormapPtr pmap = (ColormapPtr) NULL;
int result, alloc, size;
Colormap midSrc;
ScreenPtr pScreen;
VisualPtr pVisual;
pScreen = pSrc->pScreen;
pVisual = pSrc->pVisual;
midSrc = pSrc->mid;
alloc = ((pSrc->flags & AllAllocated) && CLIENT_ID(midSrc) == client) ?
AllocAll : AllocNone;
size = pVisual->ColormapEntries;
/* If the create returns non-0, it failed */
result = CreateColormap (mid, pScreen, pVisual, &pmap, alloc, client);
if(result != Success)
return result;
if(alloc == AllocAll)
{
memmove((char *)pmap->red, (char *)pSrc->red, size * sizeof(Entry));
if((pmap->class | DynamicClass) == DirectColor)
{
memmove((char *)pmap->green, (char *)pSrc->green, size * sizeof(Entry));
memmove((char *)pmap->blue, (char *)pSrc->blue, size * sizeof(Entry));
}
pSrc->flags &= ~AllAllocated;
FreePixels(pSrc, client);
UpdateColors(pmap);
return Success;
}
CopyFree(REDMAP, client, pSrc, pmap);
if ((pmap->class | DynamicClass) == DirectColor)
{
CopyFree(GREENMAP, client, pSrc, pmap);
CopyFree(BLUEMAP, client, pSrc, pmap);
}
if (pmap->class & DynamicClass)
UpdateColors(pmap);
/* XXX should worry about removing any RT_CMAPENTRY resource */
return Success;
}
/* Helper routine for freeing large numbers of cells from a map */
static void
CopyFree (int channel, int client, ColormapPtr pmapSrc, ColormapPtr pmapDst)
{
int z, npix;
EntryPtr pentSrcFirst, pentDstFirst;
EntryPtr pentSrc, pentDst;
Pixel *ppix;
int nalloc;
switch(channel)
{
default: /* so compiler can see that everything gets initialized */
case REDMAP:
ppix = (pmapSrc->clientPixelsRed)[client];
npix = (pmapSrc->numPixelsRed)[client];
pentSrcFirst = pmapSrc->red;
pentDstFirst = pmapDst->red;
break;
case GREENMAP:
ppix = (pmapSrc->clientPixelsGreen)[client];
npix = (pmapSrc->numPixelsGreen)[client];
pentSrcFirst = pmapSrc->green;
pentDstFirst = pmapDst->green;
break;
case BLUEMAP:
ppix = (pmapSrc->clientPixelsBlue)[client];
npix = (pmapSrc->numPixelsBlue)[client];
pentSrcFirst = pmapSrc->blue;
pentDstFirst = pmapDst->blue;
break;
}
nalloc = 0;
if (pmapSrc->class & DynamicClass)
{
for(z = npix; --z >= 0; ppix++)
{
/* Copy entries */
pentSrc = pentSrcFirst + *ppix;
pentDst = pentDstFirst + *ppix;
if (pentDst->refcnt > 0)
{
pentDst->refcnt++;
}
else
{
*pentDst = *pentSrc;
nalloc++;
if (pentSrc->refcnt > 0)
pentDst->refcnt = 1;
else
pentSrc->fShared = FALSE;
}
FreeCell(pmapSrc, *ppix, channel);
}
}
/* Note that FreeCell has already fixed pmapSrc->free{Color} */
switch(channel)
{
case REDMAP:
pmapDst->freeRed -= nalloc;
(pmapDst->clientPixelsRed)[client] =
(pmapSrc->clientPixelsRed)[client];
(pmapSrc->clientPixelsRed)[client] = (Pixel *) NULL;
(pmapDst->numPixelsRed)[client] = (pmapSrc->numPixelsRed)[client];
(pmapSrc->numPixelsRed)[client] = 0;
break;
case GREENMAP:
pmapDst->freeGreen -= nalloc;
(pmapDst->clientPixelsGreen)[client] =
(pmapSrc->clientPixelsGreen)[client];
(pmapSrc->clientPixelsGreen)[client] = (Pixel *) NULL;
(pmapDst->numPixelsGreen)[client] = (pmapSrc->numPixelsGreen)[client];
(pmapSrc->numPixelsGreen)[client] = 0;
break;
case BLUEMAP:
pmapDst->freeBlue -= nalloc;
pmapDst->clientPixelsBlue[client] = pmapSrc->clientPixelsBlue[client];
pmapSrc->clientPixelsBlue[client] = (Pixel *) NULL;
pmapDst->numPixelsBlue[client] = pmapSrc->numPixelsBlue[client];
pmapSrc->numPixelsBlue[client] = 0;
break;
}
}
/* Free the ith entry in a color map. Must handle freeing of
* colors allocated through AllocColorPlanes */
static void
FreeCell (ColormapPtr pmap, Pixel i, int channel)
{
EntryPtr pent;
int *pCount;
switch (channel)
{
default: /* so compiler can see that everything gets initialized */
case PSEUDOMAP:
case REDMAP:
pent = (EntryPtr) &pmap->red[i];
pCount = &pmap->freeRed;
break;
case GREENMAP:
pent = (EntryPtr) &pmap->green[i];
pCount = &pmap->freeGreen;
break;
case BLUEMAP:
pent = (EntryPtr) &pmap->blue[i];
pCount = &pmap->freeBlue;
break;
}
/* If it's not privately allocated and it's not time to free it, just
* decrement the count */
if (pent->refcnt > 1)
pent->refcnt--;
else
{
/* If the color type is shared, find the sharedcolor. If decremented
* refcnt is 0, free the shared cell. */
if (pent->fShared)
{
if(--pent->co.shco.red->refcnt == 0)
free(pent->co.shco.red);
if(--pent->co.shco.green->refcnt == 0)
free(pent->co.shco.green);
if(--pent->co.shco.blue->refcnt == 0)
free(pent->co.shco.blue);
pent->fShared = FALSE;
}
pent->refcnt = 0;
*pCount += 1;
}
}
static void
UpdateColors (ColormapPtr pmap)
{
xColorItem *defs;
xColorItem *pdef;
EntryPtr pent;
VisualPtr pVisual;
int i, n, size;
pVisual = pmap->pVisual;
size = pVisual->ColormapEntries;
defs = malloc(size * sizeof(xColorItem));
if (!defs)
return;
n = 0;
pdef = defs;
if (pmap->class == DirectColor)
{
for (i = 0; i < size; i++)
{
if (!pmap->red[i].refcnt &&
!pmap->green[i].refcnt &&
!pmap->blue[i].refcnt)
continue;
pdef->pixel = ((Pixel)i << pVisual->offsetRed) |
((Pixel)i << pVisual->offsetGreen) |
((Pixel)i << pVisual->offsetBlue);
pdef->red = pmap->red[i].co.local.red;
pdef->green = pmap->green[i].co.local.green;
pdef->blue = pmap->blue[i].co.local.blue;
pdef->flags = DoRed|DoGreen|DoBlue;
pdef++;
n++;
}
}
else
{
for (i = 0, pent = pmap->red; i < size; i++, pent++)
{
if (!pent->refcnt)
continue;
pdef->pixel = i;
if(pent->fShared)
{
pdef->red = pent->co.shco.red->color;
pdef->green = pent->co.shco.green->color;
pdef->blue = pent->co.shco.blue->color;
}
else
{
pdef->red = pent->co.local.red;
pdef->green = pent->co.local.green;
pdef->blue = pent->co.local.blue;
}
pdef->flags = DoRed|DoGreen|DoBlue;
pdef++;
n++;
}
}
if (n)
(*pmap->pScreen->StoreColors)(pmap, n, defs);
free(defs);
}
/* Get a read-only color from a ColorMap (probably slow for large maps)
* Returns by changing the value in pred, pgreen, pblue and pPix
*/
int
AllocColor (ColormapPtr pmap,
unsigned short *pred, unsigned short *pgreen, unsigned short *pblue,
Pixel *pPix, int client)
{
Pixel pixR, pixG, pixB;
int entries;
xrgb rgb;
int class;
VisualPtr pVisual;
int npix;
Pixel *ppix;
pVisual = pmap->pVisual;
(*pmap->pScreen->ResolveColor) (pred, pgreen, pblue, pVisual);
rgb.red = *pred;
rgb.green = *pgreen;
rgb.blue = *pblue;
class = pmap->class;
entries = pVisual->ColormapEntries;
/* If the colormap is being created, then we want to be able to change
* the colormap, even if it's a static type. Otherwise, we'd never be
* able to initialize static colormaps
*/
if(pmap->flags & BeingCreated)
class |= DynamicClass;
/* If this is one of the static storage classes, and we're not initializing
* it, the best we can do is to find the closest color entry to the
* requested one and return that.
*/
switch (class) {
case StaticColor:
case StaticGray:
/* Look up all three components in the same pmap */
*pPix = pixR = FindBestPixel(pmap->red, entries, &rgb, PSEUDOMAP);
*pred = pmap->red[pixR].co.local.red;
*pgreen = pmap->red[pixR].co.local.green;
*pblue = pmap->red[pixR].co.local.blue;
npix = pmap->numPixelsRed[client];
ppix = (Pixel *) realloc(pmap->clientPixelsRed[client],
(npix + 1) * sizeof(Pixel));
if (!ppix)
return BadAlloc;
ppix[npix] = pixR;
pmap->clientPixelsRed[client] = ppix;
pmap->numPixelsRed[client]++;
break;
case TrueColor:
/* Look up each component in its own map, then OR them together */
pixR = FindBestPixel(pmap->red, NUMRED(pVisual), &rgb, REDMAP);
pixG = FindBestPixel(pmap->green, NUMGREEN(pVisual), &rgb, GREENMAP);
pixB = FindBestPixel(pmap->blue, NUMBLUE(pVisual), &rgb, BLUEMAP);
*pPix = (pixR << pVisual->offsetRed) |
(pixG << pVisual->offsetGreen) |
(pixB << pVisual->offsetBlue) |
ALPHAMASK(pVisual);
*pred = pmap->red[pixR].co.local.red;
*pgreen = pmap->green[pixG].co.local.green;
*pblue = pmap->blue[pixB].co.local.blue;
npix = pmap->numPixelsRed[client];
ppix = (Pixel *) realloc(pmap->clientPixelsRed[client],
(npix + 1) * sizeof(Pixel));
if (!ppix)
return BadAlloc;
ppix[npix] = pixR;
pmap->clientPixelsRed[client] = ppix;
npix = pmap->numPixelsGreen[client];
ppix = (Pixel *) realloc(pmap->clientPixelsGreen[client],
(npix + 1) * sizeof(Pixel));
if (!ppix)
return BadAlloc;
ppix[npix] = pixG;
pmap->clientPixelsGreen[client] = ppix;
npix = pmap->numPixelsBlue[client];
ppix = (Pixel *) realloc(pmap->clientPixelsBlue[client],
(npix + 1) * sizeof(Pixel));
if (!ppix)
return BadAlloc;
ppix[npix] = pixB;
pmap->clientPixelsBlue[client] = ppix;
pmap->numPixelsRed[client]++;
pmap->numPixelsGreen[client]++;
pmap->numPixelsBlue[client]++;
break;
case GrayScale:
case PseudoColor:
if (pmap->mid != pmap->pScreen->defColormap &&
pmap->pVisual->vid == pmap->pScreen->rootVisual)
{
ColormapPtr prootmap;
dixLookupResourceByType((pointer *)&prootmap, pmap->pScreen->defColormap,
RT_COLORMAP, clients[client], DixReadAccess);
if (pmap->class == prootmap->class)
FindColorInRootCmap (prootmap, prootmap->red, entries, &rgb,
pPix, PSEUDOMAP, AllComp);
}
if (FindColor(pmap, pmap->red, entries, &rgb, pPix, PSEUDOMAP,
client, AllComp) != Success)
return BadAlloc;
break;
case DirectColor:
if (pmap->mid != pmap->pScreen->defColormap &&
pmap->pVisual->vid == pmap->pScreen->rootVisual)
{
ColormapPtr prootmap;
dixLookupResourceByType((pointer *)&prootmap, pmap->pScreen->defColormap,
RT_COLORMAP, clients[client], DixReadAccess);
if (pmap->class == prootmap->class)
{
pixR = (*pPix & pVisual->redMask) >> pVisual->offsetRed;
FindColorInRootCmap (prootmap, prootmap->red, entries, &rgb,
&pixR, REDMAP, RedComp);
pixG = (*pPix & pVisual->greenMask) >> pVisual->offsetGreen;
FindColorInRootCmap (prootmap, prootmap->green, entries, &rgb,
&pixG, GREENMAP, GreenComp);
pixB = (*pPix & pVisual->blueMask) >> pVisual->offsetBlue;
FindColorInRootCmap (prootmap, prootmap->blue, entries, &rgb,
&pixB, BLUEMAP, BlueComp);
*pPix = pixR | pixG | pixB;
}
}
pixR = (*pPix & pVisual->redMask) >> pVisual->offsetRed;
if (FindColor(pmap, pmap->red, NUMRED(pVisual), &rgb, &pixR, REDMAP,
client, RedComp) != Success)
return BadAlloc;
pixG = (*pPix & pVisual->greenMask) >> pVisual->offsetGreen;
if (FindColor(pmap, pmap->green, NUMGREEN(pVisual), &rgb, &pixG,
GREENMAP, client, GreenComp) != Success)
{
(void)FreeCo(pmap, client, REDMAP, 1, &pixR, (Pixel)0);
return BadAlloc;
}
pixB = (*pPix & pVisual->blueMask) >> pVisual->offsetBlue;
if (FindColor(pmap, pmap->blue, NUMBLUE(pVisual), &rgb, &pixB, BLUEMAP,
client, BlueComp) != Success)
{
(void)FreeCo(pmap, client, GREENMAP, 1, &pixG, (Pixel)0);
(void)FreeCo(pmap, client, REDMAP, 1, &pixR, (Pixel)0);
return BadAlloc;
}
*pPix = pixR | pixG | pixB | ALPHAMASK(pVisual);
break;
}
/* if this is the client's first pixel in this colormap, tell the
* resource manager that the client has pixels in this colormap which
* should be freed when the client dies */
if ((pmap->numPixelsRed[client] == 1) &&
(CLIENT_ID(pmap->mid) != client) &&
!(pmap->flags & BeingCreated))
{
colorResource *pcr;
pcr = malloc(sizeof(colorResource));
if (!pcr)
{
(void)FreeColors(pmap, client, 1, pPix, (Pixel)0);
return BadAlloc;
}
pcr->mid = pmap->mid;
pcr->client = client;
if (!AddResource(FakeClientID(client), RT_CMAPENTRY, (pointer)pcr))
return BadAlloc;
}
return Success;
}
/*
* FakeAllocColor -- fake an AllocColor request by
* returning a free pixel if availible, otherwise returning
* the closest matching pixel. This is used by the mi
* software sprite code to recolor cursors. A nice side-effect
* is that this routine will never return failure.
*/
void
FakeAllocColor (ColormapPtr pmap, xColorItem *item)
{
Pixel pixR, pixG, pixB;
Pixel temp;
int entries;
xrgb rgb;
int class;
VisualPtr pVisual;