Debian Codegraph @

pTk/mTk/generic/tkVisual.c

raw symbols SCIP
1 /*
2 * tkVisual.c --
3 *
4 * This file contains library procedures for allocating and
5 * freeing visuals and colormaps. This code is based on a
6 * prototype implementation by Paul Mackerras.
7 *
8 * Copyright (c) 1994 The Regents of the University of California.
9 * Copyright (c) 1994-1997 Sun Microsystems, Inc.
10 *
11 * See the file "license.terms" for information on usage and redistribution
12 * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
13 *
14 * RCS: @(#) $Id: tkVisual.c,v 1.4 2002/08/05 04:30:40 dgp Exp $
15 */
16
17 #include "tkInt.h"
18 #include "tkPort.h"
19
20 /*
21 * The table below maps from symbolic names for visual classes
22 * to the associated X class symbols.
23 */
24
25 typedef struct VisualDictionary {
26 char *name; /* Textual name of class. */
27 int minLength; /* Minimum # characters that must be
28 * specified for an unambiguous match. */
29 int class; /* X symbol for class. */
30 } VisualDictionary;
31 static VisualDictionary visualNames[] = {
32 {"best", 1, 0},
33 {"directcolor", 2, DirectColor},
34 {"grayscale", 1, GrayScale},
35 {"greyscale", 1, GrayScale},
36 {"pseudocolor", 1, PseudoColor},
37 {"staticcolor", 7, StaticColor},
38 {"staticgray", 7, StaticGray},
39 {"staticgrey", 7, StaticGray},
40 {"truecolor", 1, TrueColor},
41 {NULL, 0, 0},
42 };
43
44 /*
45 * One of the following structures exists for each distinct non-default
46 * colormap allocated for a display by Tk_GetColormap.
47 */
48
49 struct TkColormap {
50 Colormap colormap; /* X's identifier for the colormap. */
51 Visual *visual; /* Visual for which colormap was
52 * allocated. */
53 int refCount; /* How many uses of the colormap are still
54 * outstanding (calls to Tk_GetColormap
55 * minus calls to Tk_FreeColormap). */
56 int shareable; /* 0 means this colormap was allocated by
57 * a call to Tk_GetColormap with "new",
58 * implying that the window wants it all
59 * for itself. 1 means that the colormap
60 * was allocated as a default for a particular
61 * visual, so it can be shared. */
62 struct TkColormap *nextPtr; /* Next in list of colormaps for this display,
63 * or NULL for end of list. */
64 };
65
66 /*
67 *----------------------------------------------------------------------
68 *
69 * Tk_GetVisual --
70 *
71 * Given a string identifying a particular kind of visual, this
72 * procedure returns a visual and depth that matches the specification.
73 *
74 * Results:
75 * The return value is normally a pointer to a visual. If an
76 * error occurred in looking up the visual, NULL is returned and
77 * an error message is left in the interp's result. The depth of the
78 * visual is returned to *depthPtr under normal returns. If
79 * colormapPtr is non-NULL, then this procedure also finds a
80 * suitable colormap for use with the visual in tkwin, and it
81 * returns that colormap in *colormapPtr unless an error occurs.
82 *
83 * Side effects:
84 * A new colormap may be allocated.
85 *
86 *----------------------------------------------------------------------
87 */
88
89 Visual *
90 Tk_GetVisual(interp, tkwin, arg, depthPtr, colormapPtr)
91 Tcl_Interp *interp; /* Interpreter to use for error
92 * reporting. */
93 Tk_Window tkwin; /* Window in which visual will be
94 * used. */
95 Tcl_Obj *arg; /* String describing visual. See
96 * manual entry for details. */
97 int *depthPtr; /* The depth of the returned visual
98 * is stored here. */
99 Colormap *colormapPtr; /* If non-NULL, then a suitable
100 * colormap for visual is placed here.
101 * This colormap must eventually be
102 * freed by calling Tk_FreeColormap. */
103 {
104 Tk_Window tkwin2;
105 XVisualInfo template, *visInfoList, *bestPtr;
106 long mask;
107 Visual *visual;
108 int length, c, numVisuals, prio, bestPrio, i;
109 CONST char *p;
110 VisualDictionary *dictPtr;
111 TkColormap *cmapPtr;
112 TkDisplay *dispPtr = ((TkWindow *) tkwin)->dispPtr;
113 char *string = Tcl_GetString(arg);
114
115 /*
116 * Parse string and set up a template for use in searching for
117 * an appropriate visual.
118 */
119
120 c = string[0];
121 if (c == '.') {
122 /*
123 * The string must be a window name. If the window is on the
124 * same screen as tkwin, then just use its visual. Otherwise
125 * use the information about the visual as a template for the
126 * search.
127 */
128
129 tkwin2 = Tk_NameToWindow(interp, string, tkwin);
130 if (tkwin2 == NULL) {
131 return NULL;
132 }
133 visual = Tk_Visual(tkwin2);
134 if (Tk_Screen(tkwin) == Tk_Screen(tkwin2)) {
135 *depthPtr = Tk_Depth(tkwin2);
136 if (colormapPtr != NULL) {
137 /*
138 * Use the colormap from the other window too (but be sure
139 * to increment its reference count if it's one of the ones
140 * allocated here).
141 */
142
143 *colormapPtr = Tk_Colormap(tkwin2);
144 for (cmapPtr = dispPtr->cmapPtr; cmapPtr != NULL;
145 cmapPtr = cmapPtr->nextPtr) {
146 if (cmapPtr->colormap == *colormapPtr) {
147 cmapPtr->refCount += 1;
148 break;
149 }
150 }
151 }
152 return visual;
153 }
154 template.depth = Tk_Depth(tkwin2);
155 template.class = visual->class;
156 template.red_mask = visual->red_mask;
157 template.green_mask = visual->green_mask;
158 template.blue_mask = visual->blue_mask;
159 template.colormap_size = visual->map_entries;
160 template.bits_per_rgb = visual->bits_per_rgb;
161 mask = VisualDepthMask|VisualClassMask|VisualRedMaskMask
162 |VisualGreenMaskMask|VisualBlueMaskMask|VisualColormapSizeMask
163 |VisualBitsPerRGBMask;
164 } else if ((c == 0) || ((c == 'd') && (string[1] != 0)
165 && (strncmp(string, "default", strlen(string)) == 0))) {
166 /*
167 * Use the default visual for the window's screen.
168 */
169
170 if (colormapPtr != NULL) {
171 *colormapPtr = DefaultColormapOfScreen(Tk_Screen(tkwin));
172 }
173 *depthPtr = DefaultDepthOfScreen(Tk_Screen(tkwin));
174 return DefaultVisualOfScreen(Tk_Screen(tkwin));
175 } else if (isdigit(UCHAR(c))) {
176 int visualId;
177
178 /*
179 * This is a visual ID.
180 */
181
182 if (Tcl_GetIntFromObj(interp, arg, &visualId) == TCL_ERROR) {
183 Tcl_ResetResult(interp);
184 Tcl_AppendResult(interp, "bad X identifier for visual: ",
185 string, "\"", (char *) NULL);
186 return NULL;
187 }
188 template.visualid = visualId;
189 mask = VisualIDMask;
190 } else {
191 /*
192 * Parse the string into a class name (or "best") optionally
193 * followed by whitespace and a depth.
194 */
195
196 for (p = string; *p != 0; p++) {
197 if (isspace(UCHAR(*p)) || isdigit(UCHAR(*p))) {
198 break;
199 }
200 }
201 length = p - string;
202 template.class = -1;
203 for (dictPtr = visualNames; dictPtr->name != NULL; dictPtr++) {
204 if ((dictPtr->name[0] == c) && (length >= dictPtr->minLength)
205 && (strncmp(string, dictPtr->name,
206 (size_t) length) == 0)) {
207 template.class = dictPtr->class;
208 break;
209 }
210 }
211 if (template.class == -1) {
212 Tcl_AppendResult(interp, "unknown or ambiguous visual name \"",
213 string, "\": class must be ", (char *) NULL);
214 for (dictPtr = visualNames; dictPtr->name != NULL; dictPtr++) {
215 Tcl_AppendResult(interp, dictPtr->name, ", ", (char *) NULL);
216 }
217 Tcl_AppendResult(interp, "or default", (char *) NULL);
218 return NULL;
219 }
220 while (isspace(UCHAR(*p))) {
221 p++;
222 }
223 if (*p == 0) {
224 template.depth = 10000;
225 } else {
226 Tcl_Obj * temp = NULL;
227 LangSetString(&temp,p);
228 if (Tcl_GetIntFromObj(interp, temp, &template.depth) != TCL_OK) {
229 LangFreeArg(temp,TCL_STATIC);
230 return NULL;
231 }
232 LangFreeArg(temp,TCL_STATIC);
233 }
234 if (c == 'b') {
235 mask = 0;
236 } else {
237 mask = VisualClassMask;
238 }
239 }
240
241 /*
242 * Find all visuals that match the template we've just created,
243 * and return an error if there are none that match.
244 */
245
246 template.screen = Tk_ScreenNumber(tkwin);
247 mask |= VisualScreenMask;
248 visInfoList = XGetVisualInfo(Tk_Display(tkwin), mask, &template,
249 &numVisuals);
250 if (visInfoList == NULL) {
251 Tcl_SetResult(interp, "couldn't find an appropriate visual",
252 TCL_STATIC);
253 return NULL;
254 }
255
256 /*
257 * Search through the visuals that were returned to find the best
258 * one. The choice is based on the following criteria, in decreasing
259 * order of importance:
260 *
261 * 1. Depth: choose a visual with exactly the desired depth,
262 * else one with more bits than requested but as few bits
263 * as possible, else one with fewer bits but as many as
264 * possible.
265 * 2. Class: some visual classes are more desirable than others;
266 * pick the visual with the most desirable class.
267 * 3. Default: the default visual for the screen gets preference
268 * over other visuals, all else being equal.
269 */
270
271 bestPrio = 0;
272 bestPtr = NULL;
273 for (i = 0; i < numVisuals; i++) {
274 switch (visInfoList[i].class) {
275 case DirectColor: prio = 5; break;
276 case GrayScale: prio = 1; break;
277 case PseudoColor: prio = 7; break;
278 case StaticColor: prio = 3; break;
279 case StaticGray: prio = 1; break;
280 case TrueColor: prio = 5; break;
281 default: prio = 0; break;
282 }
283 if (visInfoList[i].visual
284 == DefaultVisualOfScreen(Tk_Screen(tkwin))) {
285 prio++;
286 }
287 if (bestPtr == NULL) {
288 goto newBest;
289 }
290 if (visInfoList[i].depth < bestPtr->depth) {
291 if (visInfoList[i].depth >= template.depth) {
292 goto newBest;
293 }
294 } else if (visInfoList[i].depth > bestPtr->depth) {
295 if (bestPtr->depth < template.depth) {
296 goto newBest;
297 }
298 } else {
299 if (prio > bestPrio) {
300 goto newBest;
301 }
302 }
303 continue;
304
305 newBest:
306 bestPtr = &visInfoList[i];
307 bestPrio = prio;
308 }
309 *depthPtr = bestPtr->depth;
310 visual = bestPtr->visual;
311 XFree((char *) visInfoList);
312
313 /*
314 * If we need to find a colormap for this visual, do it now.
315 * If the visual is the default visual for the screen, then
316 * use the default colormap. Otherwise search for an existing
317 * colormap that's shareable. If all else fails, create a new
318 * colormap.
319 */
320
321 if (colormapPtr != NULL) {
322 if (visual == DefaultVisualOfScreen(Tk_Screen(tkwin))) {
323 *colormapPtr = DefaultColormapOfScreen(Tk_Screen(tkwin));
324 } else {
325 for (cmapPtr = dispPtr->cmapPtr; cmapPtr != NULL;
326 cmapPtr = cmapPtr->nextPtr) {
327 if (cmapPtr->shareable && (cmapPtr->visual == visual)) {
328 *colormapPtr = cmapPtr->colormap;
329 cmapPtr->refCount += 1;
330 goto done;
331 }
332 }
333 cmapPtr = (TkColormap *) ckalloc(sizeof(TkColormap));
334 cmapPtr->colormap = XCreateColormap(Tk_Display(tkwin),
335 RootWindowOfScreen(Tk_Screen(tkwin)), visual,
336 AllocNone);
337 cmapPtr->visual = visual;
338 cmapPtr->refCount = 1;
339 cmapPtr->shareable = 1;
340 cmapPtr->nextPtr = dispPtr->cmapPtr;
341 dispPtr->cmapPtr = cmapPtr;
342 *colormapPtr = cmapPtr->colormap;
343 }
344 }
345
346 done:
347 return visual;
348 }
349
350 /*
351 *----------------------------------------------------------------------
352 *
353 * Tk_GetColormap --
354 *
355 * Given a string identifying a colormap, this procedure finds
356 * an appropriate colormap.
357 *
358 * Results:
359 * The return value is normally the X resource identifier for the
360 * colormap. If an error occurs, None is returned and an error
361 * message is placed in the interp's result.
362 *
363 * Side effects:
364 * A reference count is incremented for the colormap, so
365 * Tk_FreeColormap must eventually be called exactly once for
366 * each call to Tk_GetColormap.
367 *
368 *----------------------------------------------------------------------
369 */
370
371 Colormap
372 Tk_GetColormap(interp, tkwin, string)
373 Tcl_Interp *interp; /* Interpreter to use for error
374 * reporting. */
375 Tk_Window tkwin; /* Window where colormap will be
376 * used. */
377 CONST char *string; /* String that identifies colormap:
378 * either "new" or the name of
379 * another window. */
380 {
381 Colormap colormap;
382 TkColormap *cmapPtr;
383 TkDisplay *dispPtr = ((TkWindow *) tkwin)->dispPtr;
384 Tk_Window other;
385
386 /*
387 * Allocate a new colormap, if that's what is wanted.
388 */
389
390 if (strcmp(string, "new") == 0) {
391 cmapPtr = (TkColormap *) ckalloc(sizeof(TkColormap));
392 cmapPtr->colormap = XCreateColormap(Tk_Display(tkwin),
393 RootWindowOfScreen(Tk_Screen(tkwin)), Tk_Visual(tkwin),
394 AllocNone);
395 cmapPtr->visual = Tk_Visual(tkwin);
396 cmapPtr->refCount = 1;
397 cmapPtr->shareable = 0;
398 cmapPtr->nextPtr = dispPtr->cmapPtr;
399 dispPtr->cmapPtr = cmapPtr;
400 return cmapPtr->colormap;
401 }
402
403 /*
404 * Use a colormap from an existing window. It must have the same
405 * visual as tkwin (which means, among other things, that the
406 * other window must be on the same screen).
407 */
408
409 other = Tk_NameToWindow(interp, string, tkwin);
410 if (other == NULL) {
411 return None;
412 }
413 if (Tk_Screen(other) != Tk_Screen(tkwin)) {
414 Tcl_AppendResult(interp, "can't use colormap for ", string,
415 ": not on same screen", (char *) NULL);
416 return None;
417 }
418 if (Tk_Visual(other) != Tk_Visual(tkwin)) {
419 Tcl_AppendResult(interp, "can't use colormap for ", string,
420 ": incompatible visuals", (char *) NULL);
421 return None;
422 }
423 colormap = Tk_Colormap(other);
424
425 /*
426 * If the colormap was a special one allocated by code in this file,
427 * increment its reference count.
428 */
429
430 for (cmapPtr = dispPtr->cmapPtr; cmapPtr != NULL;
431 cmapPtr = cmapPtr->nextPtr) {
432 if (cmapPtr->colormap == colormap) {
433 cmapPtr->refCount += 1;
434 }
435 }
436 return colormap;
437 }
438
439 /*
440 *----------------------------------------------------------------------
441 *
442 * Tk_FreeColormap --
443 *
444 * This procedure is called to release a colormap that was
445 * previously allocated by Tk_GetColormap.
446 *
447 * Results:
448 * None.
449 *
450 * Side effects:
451 * The colormap's reference count is decremented. If this was the
452 * last reference to the colormap, then the colormap is freed.
453 *
454 *----------------------------------------------------------------------
455 */
456
457 void
458 Tk_FreeColormap(display, colormap)
459 Display *display; /* Display for which colormap was
460 * allocated. */
461 Colormap colormap; /* Colormap that is no longer needed.
462 * Must have been returned by previous
463 * call to Tk_GetColormap, or
464 * preserved by a previous call to
465 * Tk_PreserveColormap. */
466 {
467 TkDisplay *dispPtr;
468 TkColormap *cmapPtr, *prevPtr;
469
470 /*
471 * Find Tk's information about the display, then see if this
472 * colormap is a non-default one (if it's a default one, there
473 * won't be an entry for it in the display's list).
474 */
475
476 dispPtr = TkGetDisplay(display);
477 if (dispPtr == NULL) {
478 panic("unknown display passed to Tk_FreeColormap");
479 }
480 for (prevPtr = NULL, cmapPtr = dispPtr->cmapPtr; cmapPtr != NULL;
481 prevPtr = cmapPtr, cmapPtr = cmapPtr->nextPtr) {
482 if (cmapPtr->colormap == colormap) {
483 cmapPtr->refCount -= 1;
484 if (cmapPtr->refCount == 0) {
485 XFreeColormap(display, colormap);
486 if (prevPtr == NULL) {
487 dispPtr->cmapPtr = cmapPtr->nextPtr;
488 } else {
489 prevPtr->nextPtr = cmapPtr->nextPtr;
490 }
491 ckfree((char *) cmapPtr);
492 }
493 return;
494 }
495 }
496 }
497
498 /*
499 *----------------------------------------------------------------------
500 *
501 * Tk_PreserveColormap --
502 *
503 * This procedure is called to indicate to Tk that the specified
504 * colormap is being referenced from another location and should
505 * not be freed until all extra references are eliminated. The
506 * colormap must have been returned by Tk_GetColormap.
507 *
508 * Results:
509 * None.
510 *
511 * Side effects:
512 * The colormap's reference count is incremented, so
513 * Tk_FreeColormap must eventually be called exactly once for
514 * each call to Tk_PreserveColormap.
515 *
516 *----------------------------------------------------------------------
517 */
518
519 void
520 Tk_PreserveColormap(display, colormap)
521 Display *display; /* Display for which colormap was
522 * allocated. */
523 Colormap colormap; /* Colormap that should be
524 * preserved. */
525 {
526 TkDisplay *dispPtr;
527 TkColormap *cmapPtr;
528
529 /*
530 * Find Tk's information about the display, then see if this
531 * colormap is a non-default one (if it's a default one, there
532 * won't be an entry for it in the display's list).
533 */
534
535 dispPtr = TkGetDisplay(display);
536 if (dispPtr == NULL) {
537 panic("unknown display passed to Tk_PreserveColormap");
538 }
539 for (cmapPtr = dispPtr->cmapPtr; cmapPtr != NULL;
540 cmapPtr = cmapPtr->nextPtr) {
541 if (cmapPtr->colormap == colormap) {
542 cmapPtr->refCount += 1;
543 return;
544 }
545 }
546 }
547