summaryrefslogtreecommitdiff
path: root/hw/xnest/Color.c
blob: 61b5608008859b26ebdafc12b925608fe32f54bc (plain)
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
/* $Xorg: Color.c,v 1.3 2000/08/17 19:53:27 cpqbld Exp $ */
/*

Copyright 1993 by Davor Matic

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.  Davor Matic makes no representations about
the suitability of this software for any purpose.  It is provided "as
is" without express or implied warranty.

*/
#include "X.h"
#include "Xproto.h"
#include "scrnintstr.h"
#include "window.h"
#include "windowstr.h"
#include "colormapst.h"
#include "resource.h"

#include "Xnest.h"


#include "Display.h"
#include "Screen.h"
#include "Color.h"
#include "Visual.h"
#include "XNWindow.h"
#include "Args.h"

static ColormapPtr InstalledMaps[MAXSCREENS];

Bool xnestCreateColormap(pCmap)
     ColormapPtr pCmap;
{
  VisualPtr pVisual;
  XColor *colors;
  int i, ncolors;
  Pixel red, green, blue;
  Pixel redInc, greenInc, blueInc;

  pVisual = pCmap->pVisual;
  ncolors = pVisual->ColormapEntries;

  pCmap->devPriv = (pointer)xalloc(sizeof(xnestPrivColormap));
  
  xnestColormapPriv(pCmap)->colormap = 
    XCreateColormap(xnestDisplay,
		    xnestDefaultWindows[pCmap->pScreen->myNum],
		    xnestVisual(pVisual),
		    (pVisual->class & DynamicClass) ? 
		    AllocAll : AllocNone);
 
  
  switch (pVisual->class) {
  case StaticGray: /* read only */
    colors = (XColor *)xalloc(ncolors * sizeof(XColor));
    for (i = 0; i < ncolors; i++)
      colors[i].pixel = i;
    XQueryColors(xnestDisplay, xnestColormap(pCmap), colors, ncolors);
    for (i = 0; i < ncolors; i++) {
      pCmap->red[i].co.local.red = colors[i].red;
      pCmap->red[i].co.local.green = colors[i].red;
      pCmap->red[i].co.local.blue = colors[i].red;
    }
    xfree(colors);
    break;

  case StaticColor: /* read only */
    colors = (XColor *)xalloc(ncolors * sizeof(XColor));
    for (i = 0; i < ncolors; i++)
      colors[i].pixel = i;
    XQueryColors(xnestDisplay, xnestColormap(pCmap), colors, ncolors);
    for (i = 0; i < ncolors; i++) {
      pCmap->red[i].co.local.red = colors[i].red;
      pCmap->red[i].co.local.green = colors[i].green;
      pCmap->red[i].co.local.blue = colors[i].blue;
    }
    xfree(colors);
    break;
    
  case TrueColor: /* read only */
    colors = (XColor *)xalloc(ncolors * sizeof(XColor));
    red = green = blue = 0L;
    redInc = lowbit(pVisual->redMask);
    greenInc = lowbit(pVisual->greenMask);
    blueInc = lowbit(pVisual->blueMask);
    for (i = 0; i < ncolors; i++) {
      colors[i].pixel = red | green | blue;
      red += redInc;
      if (red > pVisual->redMask) red = 0L;
      green += greenInc;
      if (green > pVisual->greenMask) green = 0L;
      blue += blueInc;
      if (blue > pVisual->blueMask) blue = 0L;
    }
    XQueryColors(xnestDisplay, xnestColormap(pCmap), colors, ncolors);
    for (i = 0; i < ncolors; i++) {
      pCmap->red[i].co.local.red = colors[i].red;
      pCmap->green[i].co.local.green = colors[i].green;
      pCmap->blue[i].co.local.blue = colors[i].blue;
    }
    xfree(colors);
    break;
    
  case GrayScale: /* read and write */
    break;

  case PseudoColor: /* read and write */
    break;

  case DirectColor: /* read and write */
    break;
  }
  
  return True;
}

void xnestDestroyColormap (pCmap)
     ColormapPtr pCmap;
{
  XFreeColormap(xnestDisplay, xnestColormap(pCmap));
  xfree(pCmap->devPriv);
}

#define SEARCH_PREDICATE \
  (xnestWindow(pWin) != None && wColormap(pWin) == icws->cmapIDs[i])

static int xnestCountInstalledColormapWindows(pWin, ptr)
     WindowPtr pWin;
     pointer ptr;
{
  xnestInstalledColormapWindows *icws = (xnestInstalledColormapWindows *)ptr;
  int i;
  
  for (i = 0; i < icws->numCmapIDs; i++)
    if (SEARCH_PREDICATE) {
      icws->numWindows++;
      return WT_DONTWALKCHILDREN;
    }
  
  return WT_WALKCHILDREN;
}

static int xnestGetInstalledColormapWindows(pWin, ptr)
     WindowPtr pWin;
     pointer ptr;
{
  xnestInstalledColormapWindows *icws = (xnestInstalledColormapWindows *)ptr;
  int i;
  
  for (i = 0; i < icws->numCmapIDs; i++)
    if (SEARCH_PREDICATE) {
      icws->windows[icws->index++] = xnestWindow(pWin);
      return WT_DONTWALKCHILDREN;
    }
  
  return WT_WALKCHILDREN;
}

static Window *xnestOldInstalledColormapWindows = NULL;
static int xnestNumOldInstalledColormapWindows = 0;

static Bool xnestSameInstalledColormapWindows(windows, numWindows)
     Window *windows;
     int numWindows;
{
  if (xnestNumOldInstalledColormapWindows != numWindows) 
    return False;

  if (xnestOldInstalledColormapWindows == windows) 
    return True;

  if (xnestOldInstalledColormapWindows == NULL || windows == NULL) 
    return False;

  if (memcmp(xnestOldInstalledColormapWindows, windows, 
	   numWindows * sizeof(Window))) 
    return False;

  return True;
}

void xnestSetInstalledColormapWindows(pScreen)
     ScreenPtr pScreen;
{
  xnestInstalledColormapWindows icws;
  int numWindows;
  
  icws.cmapIDs = (Colormap *)xalloc(pScreen->maxInstalledCmaps *
				    sizeof(Colormap));
  icws.numCmapIDs = xnestListInstalledColormaps(pScreen, icws.cmapIDs);
  icws.numWindows = 0;
  WalkTree(pScreen, xnestCountInstalledColormapWindows, (pointer)&icws);
  if (icws.numWindows) {
    icws.windows = (Window *)xalloc((icws.numWindows + 1) * sizeof(Window));
    icws.index = 0;
    WalkTree(pScreen, xnestGetInstalledColormapWindows, (pointer)&icws);
    icws.windows[icws.numWindows] = xnestDefaultWindows[pScreen->myNum];
    numWindows = icws.numWindows + 1;
  }
  else {
    icws.windows = NULL;
    numWindows = 0;
  }
  
  xfree(icws.cmapIDs);
  
  if (!xnestSameInstalledColormapWindows(icws.windows, icws.numWindows)) {
    if (xnestOldInstalledColormapWindows)
      xfree(xnestOldInstalledColormapWindows);

#ifdef _XSERVER64
    {
      int i;
      Window64 *windows = (Window64 *)xalloc(numWindows * sizeof(Window64));

      for(i = 0; i < numWindows; ++i)
	  windows[i] = icws.windows[i];
      XSetWMColormapWindows(xnestDisplay, xnestDefaultWindows[pScreen->myNum],
			    windows, numWindows);
      xfree(windows);
    }
#else
    XSetWMColormapWindows(xnestDisplay, xnestDefaultWindows[pScreen->myNum],
			  icws.windows, numWindows);
#endif

    xnestOldInstalledColormapWindows = icws.windows;
    xnestNumOldInstalledColormapWindows = icws.numWindows;

#ifdef DUMB_WINDOW_MANAGERS
    /* 
      This code is for dumb window managers.
      This will only work with default local visual colormaps.
      */
    if (icws.numWindows)
      {
	WindowPtr pWin;
	Visual *visual;
	ColormapPtr pCmap;
	
	pWin = xnestWindowPtr(icws.windows[0]);
	visual = xnestVisualFromID(pScreen, wVisual(pWin));
	
	if (visual == xnestDefaultVisual(pScreen))
	  pCmap = (ColormapPtr)LookupIDByType(wColormap(pWin), 
					      RT_COLORMAP);
	else
	  pCmap = (ColormapPtr)LookupIDByType(pScreen->defColormap, 
					      RT_COLORMAP);
	
	XSetWindowColormap(xnestDisplay, 
			   xnestDefaultWindows[pScreen->myNum],
			   xnestColormap(pCmap));
      }
#endif /* DUMB_WINDOW_MANAGERS */
  }
  else
    if (icws.windows) xfree(icws.windows);
}

void xnestSetScreenSaverColormapWindow(pScreen)
     ScreenPtr pScreen;
{
  if (xnestOldInstalledColormapWindows)
    xfree(xnestOldInstalledColormapWindows);
  
#ifdef _XSERVER64
  {
    Window64 window;

    window = xnestScreenSaverWindows[pScreen->myNum];
    XSetWMColormapWindows(xnestDisplay, xnestDefaultWindows[pScreen->myNum],
			  &window, 1);
    xnestScreenSaverWindows[pScreen->myNum] = window;
  }
#else
  XSetWMColormapWindows(xnestDisplay, xnestDefaultWindows[pScreen->myNum],
			&xnestScreenSaverWindows[pScreen->myNum], 1);
#endif /* _XSERVER64 */
  
  xnestOldInstalledColormapWindows = NULL;
  xnestNumOldInstalledColormapWindows = 0;

  xnestDirectUninstallColormaps(pScreen);
}

void xnestDirectInstallColormaps(pScreen)
     ScreenPtr pScreen;
{
  int i, n;
  Colormap pCmapIDs[MAXCMAPS];
  
  if (!xnestDoDirectColormaps) return;

  n = (*pScreen->ListInstalledColormaps)(pScreen, pCmapIDs);

  for (i = 0; i < n; i++) {
    ColormapPtr pCmap;
    
    pCmap = (ColormapPtr)LookupIDByType(pCmapIDs[i], RT_COLORMAP);
    if (pCmap)
      XInstallColormap(xnestDisplay, xnestColormap(pCmap));
  }
}

void xnestDirectUninstallColormaps(pScreen)
     ScreenPtr pScreen;
{
  int i, n;
  Colormap pCmapIDs[MAXCMAPS];

  if (!xnestDoDirectColormaps) return;

  n = (*pScreen->ListInstalledColormaps)(pScreen, pCmapIDs);

  for (i = 0; i < n; i++) {
    ColormapPtr pCmap;
    
    pCmap = (ColormapPtr)LookupIDByType(pCmapIDs[i], RT_COLORMAP);
    if (pCmap)
      XUninstallColormap(xnestDisplay, xnestColormap(pCmap));
  }
}

void xnestInstallColormap(pCmap)
     ColormapPtr pCmap;
{
  int index;
  ColormapPtr pOldCmap;
  
  index = pCmap->pScreen->myNum;
  pOldCmap = InstalledMaps[index];
  
  if(pCmap != pOldCmap)
    {
      xnestDirectUninstallColormaps(pCmap->pScreen);

      /* Uninstall pInstalledMap. Notify all interested parties. */
      if(pOldCmap != (ColormapPtr)None)
	WalkTree(pCmap->pScreen, TellLostMap, (pointer)&pOldCmap->mid);
      
      InstalledMaps[index] = pCmap;
      WalkTree(pCmap->pScreen, TellGainedMap, (pointer)&pCmap->mid);
      
      xnestSetInstalledColormapWindows(pCmap->pScreen);
      xnestDirectInstallColormaps(pCmap->pScreen);
    }
}

void xnestUninstallColormap(pCmap)
     ColormapPtr pCmap;
{
  int index;
  ColormapPtr pCurCmap;
  
  index = pCmap->pScreen->myNum;
  pCurCmap = InstalledMaps[index];
  
  if(pCmap == pCurCmap)
    {
      if (pCmap->mid != pCmap->pScreen->defColormap)
        {
	  pCurCmap = (ColormapPtr)LookupIDByType(pCmap->pScreen->defColormap,
						 RT_COLORMAP);
	  (*pCmap->pScreen->InstallColormap)(pCurCmap);
        }
    }
}

static Bool xnestInstalledDefaultColormap = False;

int xnestListInstalledColormaps(pScreen, pCmapIDs)
     ScreenPtr pScreen;
     Colormap *pCmapIDs;
{
  if (xnestInstalledDefaultColormap) {
    *pCmapIDs = InstalledMaps[pScreen->myNum]->mid;
    return 1;
  }
  else
    return 0;
}

void xnestStoreColors(pCmap, nColors, pColors)
     ColormapPtr pCmap;
     int nColors;
     xColorItem *pColors;
{
  if (pCmap->pVisual->class & DynamicClass)
#ifdef _XSERVER64
  {
    int i;
    XColor *pColors64 = (XColor *)xalloc(nColors * sizeof(XColor) );

    for(i = 0; i < nColors; ++i)
    {
      pColors64[i].pixel = pColors[i].pixel;
      pColors64[i].red = pColors[i].red;
      pColors64[i].green = pColors[i].green;
      pColors64[i].blue = pColors[i].blue;
      pColors64[i].flags = pColors[i].flags;
    }
    XStoreColors(xnestDisplay, xnestColormap(pCmap), pColors64, nColors);
    xfree(pColors64);
  }
#else
    XStoreColors(xnestDisplay, xnestColormap(pCmap),
		 (XColor *)pColors, nColors);
#endif
}

void xnestResolveColor(pRed, pGreen, pBlue, pVisual)
     unsigned short *pRed;
     unsigned short *pGreen;
     unsigned short *pBlue;
     VisualPtr pVisual;
{
  int shift;
  unsigned int lim;
  
  shift = 16 - pVisual->bitsPerRGBValue;
  lim = (1 << pVisual->bitsPerRGBValue) - 1;

  if ((pVisual->class == PseudoColor) || (pVisual->class == DirectColor))
    {
      /* rescale to rgb bits */
      *pRed = ((*pRed >> shift) * 65535) / lim;
      *pGreen = ((*pGreen >> shift) * 65535) / lim;
      *pBlue = ((*pBlue >> shift) * 65535) / lim;
    }
  else if (pVisual->class == GrayScale)
    {
      /* rescale to gray then rgb bits */
      *pRed = (30L * *pRed + 59L * *pGreen + 11L * *pBlue) / 100;
      *pBlue = *pGreen = *pRed = ((*pRed >> shift) * 65535) / lim;
    }
  else if (pVisual->class == StaticGray)
    {
      unsigned int limg;
 
      limg = pVisual->ColormapEntries - 1;
      /* rescale to gray then [0..limg] then [0..65535] then rgb bits */
      *pRed = (30L * *pRed + 59L * *pGreen + 11L * *pBlue) / 100;
      *pRed = ((((*pRed * (limg + 1))) >> 16) * 65535) / limg;
      *pBlue = *pGreen = *pRed = ((*pRed >> shift) * 65535) / lim;
    }
  else
    {
      unsigned limr, limg, limb;
      
      limr = pVisual->redMask >> pVisual->offsetRed;
      limg = pVisual->greenMask >> pVisual->offsetGreen;
      limb = pVisual->blueMask >> pVisual->offsetBlue;
      /* rescale to [0..limN] then [0..65535] then rgb bits */
      *pRed = ((((((*pRed * (limr + 1)) >> 16) *
		  65535) / limr) >> shift) * 65535) / lim;
      *pGreen = ((((((*pGreen * (limg + 1)) >> 16) *
		    65535) / limg) >> shift) * 65535) / lim;
      *pBlue = ((((((*pBlue * (limb + 1)) >> 16) *
		   65535) / limb) >> shift) * 65535) / lim;
    }
}

Bool xnestCreateDefaultColormap(pScreen)
    ScreenPtr   pScreen;
{
  VisualPtr   pVisual;
  ColormapPtr pCmap;
  unsigned short zero = 0, ones = 0xFFFF;  
  Pixel wp, bp;

  for (pVisual = pScreen->visuals;
       pVisual->vid != pScreen->rootVisual;
       pVisual++);

  if (CreateColormap(pScreen->defColormap, pScreen, pVisual, &pCmap,
		     (pVisual->class & DynamicClass) ? AllocNone : AllocAll, 0)
      != Success)
    return False;

  wp = pScreen->whitePixel;
  bp = pScreen->blackPixel;
  if ((AllocColor(pCmap, &ones, &ones, &ones, &wp, 0) !=
       Success) ||
      (AllocColor(pCmap, &zero, &zero, &zero, &bp, 0) !=
       Success))
    return FALSE;
  pScreen->whitePixel = wp;
  pScreen->blackPixel = bp;
  (*pScreen->InstallColormap)(pCmap);

  xnestInstalledDefaultColormap = True;

  return True;
}