1 /* context.c - X context management
3 * Raster graphics library
5 * Copyright (c) 1997, 1998, 1999 Alfredo K. Kojima
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Library General Public License for more details.
17 * You should have received a copy of the GNU Library General Public
18 * License along with this library; if not, write to the Free
19 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 #include <X11/Xutil.h>
26 #include <X11/Xatom.h>
40 extern void _wraster_change_filter(int type
);
43 static Bool
bestContext(Display
*dpy
, int screen_number
, RContext
*context
);
45 static RContextAttributes DEFAULT_CONTEXT_ATTRIBS
= {
46 RC_UseSharedMemory
|RC_RenderMode
|RC_ColorsPerChannel
, /* flags */
47 RDitheredRendering
, /* render_mode */
48 4, /* colors_per_channel */
53 True
, /* use_shared_memory */
62 * Colormap allocation for PseudoColor visuals:
65 * switch standardColormap:
67 * allocate colors according to colors_per_channel
70 * if there's a std colormap defined
74 * create a std colormap and set it
81 *----------------------------------------------------------------------
82 * allocateStandardPseudoColor
83 * Creates the internal colormap for PseudoColor, setting the
84 * color values according to the supplied standard colormap.
91 *----------------------------------------------------------------------
94 allocateStandardPseudoColor(RContext
*ctx
, XStandardColormap
*stdcmap
)
98 ctx
->ncolors
= stdcmap
->red_max
* stdcmap
->red_mult
99 + stdcmap
->green_max
* stdcmap
->green_mult
100 + stdcmap
->blue_max
* stdcmap
->blue_mult
+ 1;
102 if (ctx
->ncolors
<= 1) {
103 RErrorCode
= RERR_INTERNAL
;
104 puts("wraster: bad standard colormap");
109 ctx
->colors
= malloc(sizeof(XColor
)*ctx
->ncolors
);
111 RErrorCode
= RERR_NOMEMORY
;
117 #define calc(max,mult) (((i / stdcmap->mult) % \
118 (stdcmap->max + 1)) * 65535) / stdcmap->max
120 for (i
= 0; i
< ctx
->ncolors
; i
++) {
121 ctx
->colors
[i
].pixel
= i
+ stdcmap
->base_pixel
;
122 ctx
->colors
[i
].red
= calc(red_max
, red_mult
);
123 ctx
->colors
[i
].green
= calc(green_max
, green_mult
);
124 ctx
->colors
[i
].blue
= calc(blue_max
, blue_mult
);
134 setupStandardColormap(RContext
*ctx
, Atom property
)
136 if (!XmuLookupStandardColormap(ctx
->dpy
, ctx
->screen_number
,
137 ctx
->visual
->visualid
,
138 ctx
->depth
, property
,
140 RErrorCode
= RERR_STDCMAPFAIL
;
156 allocatePseudoColor(RContext
*ctx
)
159 XColor avcolors
[256];
161 int i
, ncolors
, r
, g
, b
;
163 int cpc
= ctx
->attribs
->colors_per_channel
;
165 ncolors
= cpc
* cpc
* cpc
;
167 if (ncolors
> (1<<ctx
->depth
)) {
168 /* reduce colormap size */
169 cpc
= ctx
->attribs
->colors_per_channel
= 1<<((int)ctx
->depth
/3);
170 ncolors
= cpc
* cpc
* cpc
;
173 assert(cpc
>= 2 && ncolors
<= (1<<ctx
->depth
));
175 colors
= malloc(sizeof(XColor
)*ncolors
);
177 RErrorCode
= RERR_NOMEMORY
;
182 if ((ctx
->attribs
->flags
& RC_GammaCorrection
) && ctx
->attribs
->rgamma
> 0
183 && ctx
->attribs
->ggamma
> 0 && ctx
->attribs
->bgamma
> 0) {
187 /* do gamma correction */
188 rg
= 1.0/ctx
->attribs
->rgamma
;
189 gg
= 1.0/ctx
->attribs
->ggamma
;
190 bg
= 1.0/ctx
->attribs
->bgamma
;
191 for (r
=0; r
<cpc
; r
++) {
192 for (g
=0; g
<cpc
; g
++) {
193 for (b
=0; b
<cpc
; b
++) {
194 colors
[i
].red
=(r
*0xffff) / (cpc
-1);
195 colors
[i
].green
=(g
*0xffff) / (cpc
-1);
196 colors
[i
].blue
=(b
*0xffff) / (cpc
-1);
197 colors
[i
].flags
= DoRed
|DoGreen
|DoBlue
;
199 tmp
= (double)colors
[i
].red
/ 65536.0;
200 colors
[i
].red
= (unsigned short)(65536.0*pow(tmp
, rg
));
202 tmp
= (double)colors
[i
].green
/ 65536.0;
203 colors
[i
].green
= (unsigned short)(65536.0*pow(tmp
, gg
));
205 tmp
= (double)colors
[i
].blue
/ 65536.0;
206 colors
[i
].blue
= (unsigned short)(65536.0*pow(tmp
, bg
));
214 for (r
=0; r
<cpc
; r
++) {
215 for (g
=0; g
<cpc
; g
++) {
216 for (b
=0; b
<cpc
; b
++) {
217 colors
[i
].red
=(r
*0xffff) / (cpc
-1);
218 colors
[i
].green
=(g
*0xffff) / (cpc
-1);
219 colors
[i
].blue
=(b
*0xffff) / (cpc
-1);
220 colors
[i
].flags
= DoRed
|DoGreen
|DoBlue
;
226 /* try to allocate the colors */
227 for (i
=0; i
<ncolors
; i
++) {
228 if (!XAllocColor(ctx
->dpy
, ctx
->cmap
, &(colors
[i
]))) {
229 colors
[i
].flags
= 0; /* failed */
231 colors
[i
].flags
= DoRed
|DoGreen
|DoBlue
;
234 /* try to allocate close values for the colors that couldn't
235 * be allocated before */
236 avncolors
= (1<<ctx
->depth
>256 ? 256 : 1<<ctx
->depth
);
237 for (i
=0; i
<avncolors
; i
++) avcolors
[i
].pixel
= i
;
239 XQueryColors(ctx
->dpy
, ctx
->cmap
, avcolors
, avncolors
);
241 for (i
=0; i
<ncolors
; i
++) {
242 if (colors
[i
].flags
==0) {
244 unsigned long cdiff
=0xffffffff, diff
;
245 unsigned long closest
=0;
250 /* find closest color */
251 for (j
=0; j
<avncolors
; j
++) {
252 r
= (colors
[i
].red
- avcolors
[i
].red
)>>8;
253 g
= (colors
[i
].green
- avcolors
[i
].green
)>>8;
254 b
= (colors
[i
].blue
- avcolors
[i
].blue
)>>8;
255 diff
= r
*r
+ g
*g
+ b
*b
;
261 /* allocate closest color found */
262 colors
[i
].red
= avcolors
[closest
].red
;
263 colors
[i
].green
= avcolors
[closest
].green
;
264 colors
[i
].blue
= avcolors
[closest
].blue
;
265 if (XAllocColor(ctx
->dpy
, ctx
->cmap
, &colors
[i
])) {
266 colors
[i
].flags
= DoRed
|DoGreen
|DoBlue
;
267 break; /* succeeded, don't need to retry */
270 printf("close color allocation failed. Retrying...\n");
276 ctx
->colors
= colors
;
277 ctx
->ncolors
= ncolors
;
284 allocateGrayScale(RContext
*ctx
)
287 XColor avcolors
[256];
289 int i
, ncolors
, r
, g
, b
;
291 int cpc
= ctx
->attribs
->colors_per_channel
;
293 ncolors
= cpc
* cpc
* cpc
;
295 if (ctx
->vclass
== StaticGray
) {
296 /* we might as well use all grays */
297 ncolors
= 1<<ctx
->depth
;
299 if ( ncolors
> (1<<ctx
->depth
) ) {
300 /* reduce colormap size */
301 cpc
= ctx
->attribs
->colors_per_channel
= 1<<((int)ctx
->depth
/3);
302 ncolors
= cpc
* cpc
* cpc
;
305 assert(cpc
>= 2 && ncolors
<= (1<<ctx
->depth
));
308 if (ncolors
>=256 && ctx
->vclass
==StaticGray
) {
309 /* don't need dithering for 256 levels of gray in StaticGray visual */
310 ctx
->attribs
->render_mode
= RBestMatchRendering
;
313 colors
= malloc(sizeof(XColor
)*ncolors
);
315 RErrorCode
= RERR_NOMEMORY
;
318 for (i
=0; i
<ncolors
; i
++) {
319 colors
[i
].red
=(i
*0xffff) / (ncolors
-1);
320 colors
[i
].green
=(i
*0xffff) / (ncolors
-1);
321 colors
[i
].blue
=(i
*0xffff) / (ncolors
-1);
322 colors
[i
].flags
= DoRed
|DoGreen
|DoBlue
;
324 /* try to allocate the colors */
325 for (i
=0; i
<ncolors
; i
++) {
327 printf("trying:%x,%x,%x\n",colors
[i
].red
,colors
[i
].green
,colors
[i
].blue
);
329 if (!XAllocColor(ctx
->dpy
, ctx
->cmap
, &(colors
[i
]))) {
330 colors
[i
].flags
= 0; /* failed */
332 printf("failed:%x,%x,%x\n",colors
[i
].red
,colors
[i
].green
,colors
[i
].blue
);
335 colors
[i
].flags
= DoRed
|DoGreen
|DoBlue
;
337 printf("success:%x,%x,%x\n",colors
[i
].red
,colors
[i
].green
,colors
[i
].blue
);
341 /* try to allocate close values for the colors that couldn't
342 * be allocated before */
343 avncolors
= (1<<ctx
->depth
>256 ? 256 : 1<<ctx
->depth
);
344 for (i
=0; i
<avncolors
; i
++) avcolors
[i
].pixel
= i
;
346 XQueryColors(ctx
->dpy
, ctx
->cmap
, avcolors
, avncolors
);
348 for (i
=0; i
<ncolors
; i
++) {
349 if (colors
[i
].flags
==0) {
351 unsigned long cdiff
=0xffffffff, diff
;
352 unsigned long closest
=0;
357 /* find closest color */
358 for (j
=0; j
<avncolors
; j
++) {
359 r
= (colors
[i
].red
- avcolors
[i
].red
)>>8;
360 g
= (colors
[i
].green
- avcolors
[i
].green
)>>8;
361 b
= (colors
[i
].blue
- avcolors
[i
].blue
)>>8;
362 diff
= r
*r
+ g
*g
+ b
*b
;
368 /* allocate closest color found */
370 printf("best match:%x,%x,%x => %x,%x,%x\n",colors
[i
].red
,colors
[i
].green
,colors
[i
].blue
,avcolors
[closest
].red
,avcolors
[closest
].green
,avcolors
[closest
].blue
);
372 colors
[i
].red
= avcolors
[closest
].red
;
373 colors
[i
].green
= avcolors
[closest
].green
;
374 colors
[i
].blue
= avcolors
[closest
].blue
;
375 if (XAllocColor(ctx
->dpy
, ctx
->cmap
, &colors
[i
])) {
376 colors
[i
].flags
= DoRed
|DoGreen
|DoBlue
;
377 break; /* succeeded, don't need to retry */
380 printf("close color allocation failed. Retrying...\n");
390 setupPseudoColorColormap(RContext
*context
)
394 if (context
->attribs
->standard_colormap_mode
== RCreateStdColormap
) {
395 property
= XInternAtom(context
->dpy
, "RGB_DEFAULT_MAP", False
);
397 if (!setupStandardColormap(context
, property
)) {
402 if (context
->attribs
->standard_colormap_mode
!= RIgnoreStdColormap
) {
403 XStandardColormap
*maps
;
407 property
= XInternAtom(context
->dpy
, "RGB_BEST_MAP", False
);
408 if (!XGetRGBColormaps(context
->dpy
,
409 DefaultRootWindow(context
->dpy
),
410 &maps
, &count
, property
)) {
415 property
= XInternAtom(context
->dpy
, "RGB_DEFAULT_MAP", False
);
416 if (!XGetRGBColormaps(context
->dpy
,
417 DefaultRootWindow(context
->dpy
),
418 &maps
, &count
, property
)) {
423 if (!XGetRGBColormaps(context
->dpy
,
424 DefaultRootWindow(context
->dpy
),
425 &maps
, &count
, property
)) {
433 for (i
= 0; i
< count
; i
++) {
434 if (maps
[i
].visualid
== context
->visual
->visualid
) {
441 puts("wrlib: no std cmap found");
445 && allocateStandardPseudoColor(context
, &maps
[theMap
])) {
447 context
->std_rgb_map
= XAllocStandardColormap();
449 *context
->std_rgb_map
= maps
[theMap
];
460 context
->attribs
->standard_colormap_mode
= RIgnoreStdColormap
;
462 /* RIgnoreStdColormap and fallback */
463 return allocatePseudoColor(context
);
470 mygetenv(char *var
, int scr
)
475 sprintf(varname
, "%s%i", var
, scr
);
485 gatherconfig(RContext
*context
, int screen_n
)
489 ptr
= mygetenv("WRASTER_GAMMA", screen_n
);
492 if (sscanf(ptr
, "%f/%f/%f", &g1
, &g2
, &g3
)!=3
493 || g1
<=0.0 || g2
<=0.0 || g3
<=0.0) {
494 printf("wrlib: invalid value(s) for gamma correction \"%s\"\n",
497 context
->attribs
->flags
|= RC_GammaCorrection
;
498 context
->attribs
->rgamma
= g1
;
499 context
->attribs
->ggamma
= g2
;
500 context
->attribs
->bgamma
= g3
;
503 ptr
= mygetenv("WRASTER_COLOR_RESOLUTION", screen_n
);
506 if (sscanf(ptr
, "%d", &i
)!=1 || i
<2 || i
>6) {
507 printf("wrlib: invalid value for color resolution \"%s\"\n",ptr
);
509 context
->attribs
->flags
|= RC_ColorsPerChannel
;
510 context
->attribs
->colors_per_channel
= i
;
517 getColormap(RContext
*context
, int screen_number
)
519 Colormap cmap
= None
;
520 XStandardColormap
*cmaps
;
523 if (XGetRGBColormaps(context
->dpy
,
524 RootWindow(context
->dpy
, screen_number
),
525 &cmaps
, &ncmaps
, XA_RGB_DEFAULT_MAP
)) {
526 for (i
=0; i
<ncmaps
; ++i
) {
527 if (cmaps
[i
].visualid
== context
->visual
->visualid
) {
529 cmap
= cmaps
[i
].colormap
;
538 cmap
= XCreateColormap(context
->dpy
,
539 RootWindow(context
->dpy
, screen_number
),
540 context
->visual
, AllocNone
);
542 color
.red
= color
.green
= color
.blue
= 0;
543 XAllocColor(context
->dpy
, cmap
, &color
);
544 context
->black
= color
.pixel
;
546 color
.red
= color
.green
= color
.blue
= 0xffff;
547 XAllocColor(context
->dpy
, cmap
, &color
);
548 context
->white
= color
.pixel
;
551 context
->cmap
= cmap
;
556 count_offset(unsigned long mask
)
561 while ((mask
& 1)==0) {
570 RCreateContext(Display
*dpy
, int screen_number
, RContextAttributes
*attribs
)
576 context
= malloc(sizeof(RContext
));
578 RErrorCode
= RERR_NOMEMORY
;
581 memset(context
, 0, sizeof(RContext
));
585 context
->screen_number
= screen_number
;
587 context
->attribs
= malloc(sizeof(RContextAttributes
));
588 if (!context
->attribs
) {
590 RErrorCode
= RERR_NOMEMORY
;
594 *context
->attribs
= DEFAULT_CONTEXT_ATTRIBS
;
596 *context
->attribs
= *attribs
;
598 if (!(context
->attribs
->flags
& RC_StandardColormap
)) {
599 context
->attribs
->standard_colormap_mode
= RUseStdColormap
;
602 /* get configuration from environment variables */
603 gatherconfig(context
, screen_number
);
605 _wraster_change_filter(context
->attribs
->scaling_filter
);
607 if ((context
->attribs
->flags
& RC_VisualID
)) {
608 XVisualInfo
*vinfo
, templ
;
611 templ
.screen
= screen_number
;
612 templ
.visualid
= context
->attribs
->visualid
;
613 vinfo
= XGetVisualInfo(context
->dpy
, VisualIDMask
|VisualScreenMask
,
615 if (!vinfo
|| nret
==0) {
617 RErrorCode
= RERR_BADVISUALID
;
621 if (vinfo
[0].visual
== DefaultVisual(dpy
, screen_number
)) {
622 context
->attribs
->flags
|= RC_DefaultVisual
;
624 XSetWindowAttributes attr
;
627 context
->visual
= vinfo
[0].visual
;
628 context
->depth
= vinfo
[0].depth
;
629 context
->vclass
= vinfo
[0].class;
630 getColormap(context
, screen_number
);
631 attr
.colormap
= context
->cmap
;
632 attr
.override_redirect
= True
;
633 attr
.border_pixel
= 0;
634 attr
.background_pixel
= 0;
635 mask
= CWBorderPixel
|CWColormap
|CWOverrideRedirect
|CWBackPixel
;
637 XCreateWindow(dpy
, RootWindow(dpy
, screen_number
), 1, 1,
638 1, 1, 0, context
->depth
, CopyFromParent
,
639 context
->visual
, mask
, &attr
);
640 /* XSetWindowColormap(dpy, context->drawable, attr.colormap);*/
646 if (!context
->visual
) {
647 if ((context
->attribs
->flags
& RC_DefaultVisual
)
648 || !bestContext(dpy
, screen_number
, context
)) {
649 context
->visual
= DefaultVisual(dpy
, screen_number
);
650 context
->depth
= DefaultDepth(dpy
, screen_number
);
651 context
->cmap
= DefaultColormap(dpy
, screen_number
);
652 context
->drawable
= RootWindow(dpy
, screen_number
);
653 context
->black
= BlackPixel(dpy
, screen_number
);
654 context
->white
= WhitePixel(dpy
, screen_number
);
655 context
->vclass
= context
->visual
->class;
659 gcv
.function
= GXcopy
;
660 gcv
.graphics_exposures
= False
;
661 context
->copy_gc
= XCreateGC(dpy
, context
->drawable
, GCFunction
662 |GCGraphicsExposures
, &gcv
);
664 if (context
->vclass
== PseudoColor
|| context
->vclass
== StaticColor
) {
666 if (!setupPseudoColorColormap(context
)) {
670 } else if (context
->vclass
== GrayScale
|| context
->vclass
== StaticGray
) {
671 context
->colors
= allocateGrayScale(context
);
672 if (!context
->colors
) {
675 } else if (context
->vclass
== TrueColor
) {
676 /* calc offsets to create a TrueColor pixel */
677 context
->red_offset
= count_offset(context
->visual
->red_mask
);
678 context
->green_offset
= count_offset(context
->visual
->green_mask
);
679 context
->blue_offset
= count_offset(context
->visual
->blue_mask
);
680 /* disable dithering on 24 bits visuals */
681 if (context
->depth
>= 24)
682 context
->attribs
->render_mode
= RBestMatchRendering
;
685 /* check avaiability of MIT-SHM */
687 if (!(context
->attribs
->flags
& RC_UseSharedMemory
)) {
688 context
->attribs
->flags
|= RC_UseSharedMemory
;
689 context
->attribs
->use_shared_memory
= True
;
692 if (context
->attribs
->use_shared_memory
) {
696 context
->flags
.use_shared_pixmap
= 0;
698 if (!XShmQueryVersion(context
->dpy
, &major
, &minor
, &sharedPixmaps
)) {
699 context
->attribs
->use_shared_memory
= False
;
701 if (XShmPixmapFormat(context
->dpy
)==ZPixmap
)
702 context
->flags
.use_shared_pixmap
= sharedPixmaps
;
712 bestContext(Display
*dpy
, int screen_number
, RContext
*context
)
714 XVisualInfo
*vinfo
=NULL
, rvinfo
;
715 int best
= -1, numvis
, i
;
717 XSetWindowAttributes attr
;
719 rvinfo
.class = TrueColor
;
720 rvinfo
.screen
= screen_number
;
721 flags
= VisualClassMask
| VisualScreenMask
;
723 vinfo
= XGetVisualInfo(dpy
, flags
, &rvinfo
, &numvis
);
724 if (vinfo
) { /* look for a TrueColor, 24-bit or more (pref 24) */
725 for (i
=numvis
-1, best
= -1; i
>=0; i
--) {
726 if (vinfo
[i
].depth
== 24) best
= i
;
727 else if (vinfo
[i
].depth
>24 && best
<0) best
= i
;
732 if (best
== -1) { /* look for a DirectColor, 24-bit or more (pref 24) */
733 rvinfo
.class = DirectColor
;
734 if (vinfo
) XFree((char *) vinfo
);
735 vinfo
= XGetVisualInfo(dpy
, flags
, &rvinfo
, &numvis
);
737 for (i
=0, best
= -1; i
<numvis
; i
++) {
738 if (vinfo
[i
].depth
== 24) best
= i
;
739 else if (vinfo
[i
].depth
>24 && best
<0) best
= i
;
745 context
->visual
= vinfo
[best
].visual
;
746 context
->depth
= vinfo
[best
].depth
;
747 context
->vclass
= vinfo
[best
].class;
748 getColormap(context
, screen_number
);
749 attr
.colormap
= context
->cmap
;
750 attr
.override_redirect
= True
;
751 attr
.border_pixel
= 0;
753 XCreateWindow(dpy
, RootWindow(dpy
, screen_number
),
754 1, 1, 1, 1, 0, context
->depth
,
755 CopyFromParent
, context
->visual
,
756 CWBorderPixel
|CWColormap
|CWOverrideRedirect
, &attr
);
757 /* XSetWindowColormap(dpy, context->drawable, context->cmap);*/
759 if (vinfo
) XFree((char *) vinfo
);