1 /* context.c - X context management
3 * Raster graphics library
5 * Copyright (c) 1997 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>
37 static Bool
bestContext(Display
*dpy
, int screen_number
, RContext
*context
);
39 static RContextAttributes DEFAULT_CONTEXT_ATTRIBS
= {
40 RC_UseSharedMemory
|RC_RenderMode
|RC_ColorsPerChannel
, /* flags */
41 RM_DITHER
, /* render_mode */
42 4, /* colors_per_channel */
47 True
/* use_shared_memory */
52 allocatePseudoColor(RContext
*ctx
)
57 int i
, ncolors
, r
, g
, b
;
59 int cpc
= ctx
->attribs
->colors_per_channel
;
61 ncolors
= cpc
* cpc
* cpc
;
63 if ( ncolors
> (1<<ctx
->depth
) ) {
64 /* reduce colormap size */
65 cpc
= ctx
->attribs
->colors_per_channel
= 1<<((int)ctx
->depth
/3);
66 ncolors
= cpc
* cpc
* cpc
;
69 assert(cpc
>= 2 && ncolors
<= (1<<ctx
->depth
));
71 colors
= malloc(sizeof(XColor
)*ncolors
);
73 RErrorCode
= RERR_NOMEMORY
;
78 if ((ctx
->attribs
->flags
& RC_GammaCorrection
) && ctx
->attribs
->rgamma
> 0
79 && ctx
->attribs
->ggamma
> 0 && ctx
->attribs
->bgamma
> 0) {
83 /* do gamma correction */
84 rg
= 1.0/ctx
->attribs
->rgamma
;
85 gg
= 1.0/ctx
->attribs
->ggamma
;
86 bg
= 1.0/ctx
->attribs
->bgamma
;
87 for (r
=0; r
<cpc
; r
++) {
88 for (g
=0; g
<cpc
; g
++) {
89 for (b
=0; b
<cpc
; b
++) {
90 colors
[i
].red
=(r
*0xffff) / (cpc
-1);
91 colors
[i
].green
=(g
*0xffff) / (cpc
-1);
92 colors
[i
].blue
=(b
*0xffff) / (cpc
-1);
93 colors
[i
].flags
= DoRed
|DoGreen
|DoBlue
;
95 tmp
= (double)colors
[i
].red
/ 65536.0;
96 colors
[i
].red
= (unsigned short)(65536.0*pow(tmp
, rg
));
98 tmp
= (double)colors
[i
].green
/ 65536.0;
99 colors
[i
].green
= (unsigned short)(65536.0*pow(tmp
, gg
));
101 tmp
= (double)colors
[i
].blue
/ 65536.0;
102 colors
[i
].blue
= (unsigned short)(65536.0*pow(tmp
, bg
));
110 for (r
=0; r
<cpc
; r
++) {
111 for (g
=0; g
<cpc
; g
++) {
112 for (b
=0; b
<cpc
; b
++) {
113 colors
[i
].red
=(r
*0xffff) / (cpc
-1);
114 colors
[i
].green
=(g
*0xffff) / (cpc
-1);
115 colors
[i
].blue
=(b
*0xffff) / (cpc
-1);
116 colors
[i
].flags
= DoRed
|DoGreen
|DoBlue
;
122 /* try to allocate the colors */
123 for (i
=0; i
<ncolors
; i
++) {
124 if (!XAllocColor(ctx
->dpy
, ctx
->cmap
, &(colors
[i
]))) {
125 colors
[i
].flags
= 0; /* failed */
127 colors
[i
].flags
= DoRed
|DoGreen
|DoBlue
;
130 /* try to allocate close values for the colors that couldn't
131 * be allocated before */
132 avncolors
= (1<<ctx
->depth
>256 ? 256 : 1<<ctx
->depth
);
133 for (i
=0; i
<avncolors
; i
++) avcolors
[i
].pixel
= i
;
135 XQueryColors(ctx
->dpy
, ctx
->cmap
, avcolors
, avncolors
);
137 for (i
=0; i
<ncolors
; i
++) {
138 if (colors
[i
].flags
==0) {
140 unsigned long cdiff
=0xffffffff, diff
;
141 unsigned long closest
=0;
146 /* find closest color */
147 for (j
=0; j
<avncolors
; j
++) {
148 r
= (colors
[i
].red
- avcolors
[i
].red
)>>8;
149 g
= (colors
[i
].green
- avcolors
[i
].green
)>>8;
150 b
= (colors
[i
].blue
- avcolors
[i
].blue
)>>8;
151 diff
= r
*r
+ g
*g
+ b
*b
;
157 /* allocate closest color found */
158 colors
[i
].red
= avcolors
[closest
].red
;
159 colors
[i
].green
= avcolors
[closest
].green
;
160 colors
[i
].blue
= avcolors
[closest
].blue
;
161 if (XAllocColor(ctx
->dpy
, ctx
->cmap
, &colors
[i
])) {
162 colors
[i
].flags
= DoRed
|DoGreen
|DoBlue
;
163 break; /* succeeded, don't need to retry */
166 printf("close color allocation failed. Retrying...\n");
176 allocateGrayScale(RContext
*ctx
)
179 XColor avcolors
[256];
181 int i
, ncolors
, r
, g
, b
;
183 int cpc
= ctx
->attribs
->colors_per_channel
;
185 ncolors
= cpc
* cpc
* cpc
;
187 if (ctx
->vclass
== StaticGray
) {
188 /* we might as well use all grays */
189 ncolors
= 1<<ctx
->depth
;
191 if ( ncolors
> (1<<ctx
->depth
) ) {
192 /* reduce colormap size */
193 cpc
= ctx
->attribs
->colors_per_channel
= 1<<((int)ctx
->depth
/3);
194 ncolors
= cpc
* cpc
* cpc
;
197 assert(cpc
>= 2 && ncolors
<= (1<<ctx
->depth
));
200 if (ncolors
>=256 && ctx
->vclass
==StaticGray
) {
201 /* don't need dithering for 256 levels of gray in StaticGray visual */
202 ctx
->attribs
->render_mode
= RM_MATCH
;
205 colors
= malloc(sizeof(XColor
)*ncolors
);
207 RErrorCode
= RERR_NOMEMORY
;
210 for (i
=0; i
<ncolors
; i
++) {
211 colors
[i
].red
=(i
*0xffff) / (ncolors
-1);
212 colors
[i
].green
=(i
*0xffff) / (ncolors
-1);
213 colors
[i
].blue
=(i
*0xffff) / (ncolors
-1);
214 colors
[i
].flags
= DoRed
|DoGreen
|DoBlue
;
216 /* try to allocate the colors */
217 for (i
=0; i
<ncolors
; i
++) {
219 printf("trying:%x,%x,%x\n",colors
[i
].red
,colors
[i
].green
,colors
[i
].blue
);
221 if (!XAllocColor(ctx
->dpy
, ctx
->cmap
, &(colors
[i
]))) {
222 colors
[i
].flags
= 0; /* failed */
224 printf("failed:%x,%x,%x\n",colors
[i
].red
,colors
[i
].green
,colors
[i
].blue
);
227 colors
[i
].flags
= DoRed
|DoGreen
|DoBlue
;
229 printf("success:%x,%x,%x\n",colors
[i
].red
,colors
[i
].green
,colors
[i
].blue
);
233 /* try to allocate close values for the colors that couldn't
234 * be allocated before */
235 avncolors
= (1<<ctx
->depth
>256 ? 256 : 1<<ctx
->depth
);
236 for (i
=0; i
<avncolors
; i
++) avcolors
[i
].pixel
= i
;
238 XQueryColors(ctx
->dpy
, ctx
->cmap
, avcolors
, avncolors
);
240 for (i
=0; i
<ncolors
; i
++) {
241 if (colors
[i
].flags
==0) {
243 unsigned long cdiff
=0xffffffff, diff
;
244 unsigned long closest
=0;
249 /* find closest color */
250 for (j
=0; j
<avncolors
; j
++) {
251 r
= (colors
[i
].red
- avcolors
[i
].red
)>>8;
252 g
= (colors
[i
].green
- avcolors
[i
].green
)>>8;
253 b
= (colors
[i
].blue
- avcolors
[i
].blue
)>>8;
254 diff
= r
*r
+ g
*g
+ b
*b
;
260 /* allocate closest color found */
262 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
);
264 colors
[i
].red
= avcolors
[closest
].red
;
265 colors
[i
].green
= avcolors
[closest
].green
;
266 colors
[i
].blue
= avcolors
[closest
].blue
;
267 if (XAllocColor(ctx
->dpy
, ctx
->cmap
, &colors
[i
])) {
268 colors
[i
].flags
= DoRed
|DoGreen
|DoBlue
;
269 break; /* succeeded, don't need to retry */
272 printf("close color allocation failed. Retrying...\n");
282 mygetenv(char *var
, int scr
)
287 sprintf(varname
, "%s%i", var
, scr
);
297 gatherconfig(RContext
*context
, int screen_n
)
301 ptr
= mygetenv("WRASTER_GAMMA", screen_n
);
304 if (sscanf(ptr
, "%f/%f/%f", &g1
, &g2
, &g3
)!=3
305 || g1
<=0.0 || g2
<=0.0 || g3
<=0.0) {
306 printf("wrlib: invalid value(s) for gamma correction \"%s\"\n",
309 context
->attribs
->flags
|= RC_GammaCorrection
;
310 context
->attribs
->rgamma
= g1
;
311 context
->attribs
->ggamma
= g2
;
312 context
->attribs
->bgamma
= g3
;
315 ptr
= mygetenv("WRASTER_COLOR_RESOLUTION", screen_n
);
318 if (sscanf(ptr
, "%d", &i
)!=1 || i
<2 || i
>6) {
319 printf("wrlib: invalid value for color resolution \"%s\"\n",ptr
);
321 context
->attribs
->flags
|= RC_ColorsPerChannel
;
322 context
->attribs
->colors_per_channel
= i
;
329 getColormap(RContext
*context
, int screen_number
)
331 Colormap cmap
= None
;
332 XStandardColormap
*cmaps
;
335 if (XGetRGBColormaps(context
->dpy
,
336 RootWindow(context
->dpy
, screen_number
),
337 &cmaps
, &ncmaps
, XA_RGB_DEFAULT_MAP
)) {
338 for (i
=0; i
<ncmaps
; ++i
) {
339 if (cmaps
[i
].visualid
== context
->visual
->visualid
) {
341 cmap
= cmaps
[i
].colormap
;
350 cmap
= XCreateColormap(context
->dpy
,
351 RootWindow(context
->dpy
, screen_number
),
352 context
->visual
, AllocNone
);
354 color
.red
= color
.green
= color
.blue
= 0;
355 XAllocColor(context
->dpy
, cmap
, &color
);
356 context
->black
= color
.pixel
;
358 color
.red
= color
.green
= color
.blue
= 0xffff;
359 XAllocColor(context
->dpy
, cmap
, &color
);
360 context
->white
= color
.pixel
;
363 context
->cmap
= cmap
;
368 count_offset(unsigned long mask
)
373 while ((mask
& 1)==0) {
382 RCreateContext(Display
*dpy
, int screen_number
, RContextAttributes
*attribs
)
388 context
= malloc(sizeof(RContext
));
390 RErrorCode
= RERR_NOMEMORY
;
393 memset(context
, 0, sizeof(RContext
));
397 context
->screen_number
= screen_number
;
399 context
->attribs
= malloc(sizeof(RContextAttributes
));
400 if (!context
->attribs
) {
402 RErrorCode
= RERR_NOMEMORY
;
406 *context
->attribs
= DEFAULT_CONTEXT_ATTRIBS
;
408 *context
->attribs
= *attribs
;
410 /* get configuration from environment variables */
411 gatherconfig(context
, screen_number
);
413 if ((context
->attribs
->flags
& RC_VisualID
)) {
414 XVisualInfo
*vinfo
, templ
;
417 templ
.screen
= screen_number
;
418 templ
.visualid
= context
->attribs
->visualid
;
419 vinfo
= XGetVisualInfo(context
->dpy
, VisualIDMask
|VisualScreenMask
,
421 if (!vinfo
|| nret
==0) {
423 RErrorCode
= RERR_BADVISUALID
;
427 if (vinfo
[0].visual
== DefaultVisual(dpy
, screen_number
)) {
428 context
->attribs
->flags
|= RC_DefaultVisual
;
430 XSetWindowAttributes attr
;
433 context
->visual
= vinfo
[0].visual
;
434 context
->depth
= vinfo
[0].depth
;
435 context
->vclass
= vinfo
[0].class;
436 getColormap(context
, screen_number
);
437 attr
.colormap
= context
->cmap
;
438 attr
.override_redirect
= True
;
439 attr
.border_pixel
= 0;
440 attr
.background_pixel
= 0;
441 mask
= CWBorderPixel
|CWColormap
|CWOverrideRedirect
|CWBackPixel
;
443 XCreateWindow(dpy
, RootWindow(dpy
, screen_number
), 1, 1,
444 1, 1, 0, context
->depth
, CopyFromParent
,
445 context
->visual
, mask
, &attr
);
446 /* XSetWindowColormap(dpy, context->drawable, attr.colormap);*/
452 if (!context
->visual
) {
453 if ((context
->attribs
->flags
& RC_DefaultVisual
)
454 || !bestContext(dpy
, screen_number
, context
)) {
455 context
->visual
= DefaultVisual(dpy
, screen_number
);
456 context
->depth
= DefaultDepth(dpy
, screen_number
);
457 context
->cmap
= DefaultColormap(dpy
, screen_number
);
458 context
->drawable
= RootWindow(dpy
, screen_number
);
459 context
->black
= BlackPixel(dpy
, screen_number
);
460 context
->white
= WhitePixel(dpy
, screen_number
);
461 context
->vclass
= context
->visual
->class;
465 gcv
.function
= GXcopy
;
466 gcv
.graphics_exposures
= False
;
467 context
->copy_gc
= XCreateGC(dpy
, context
->drawable
, GCFunction
468 |GCGraphicsExposures
, &gcv
);
470 if (context
->vclass
== PseudoColor
|| context
->vclass
== StaticColor
) {
471 context
->colors
= allocatePseudoColor(context
);
472 if (!context
->colors
) {
475 } else if (context
->vclass
== GrayScale
|| context
->vclass
== StaticGray
) {
476 context
->colors
= allocateGrayScale(context
);
477 if (!context
->colors
) {
480 } else if (context
->vclass
== TrueColor
) {
481 /* calc offsets to create a TrueColor pixel */
482 context
->red_offset
= count_offset(context
->visual
->red_mask
);
483 context
->green_offset
= count_offset(context
->visual
->green_mask
);
484 context
->blue_offset
= count_offset(context
->visual
->blue_mask
);
485 /* disable dithering on 24 bits visuals */
486 if (context
->depth
>= 24)
487 context
->attribs
->render_mode
= RM_MATCH
;
490 /* check avaiability of MIT-SHM */
492 if (!(context
->attribs
->flags
& RC_UseSharedMemory
)) {
493 context
->attribs
->flags
|= RC_UseSharedMemory
;
494 context
->attribs
->use_shared_memory
= True
;
497 if (context
->attribs
->use_shared_memory
) {
501 context
->flags
.use_shared_pixmap
= 0;
503 if (!XShmQueryVersion(context
->dpy
, &major
, &minor
, &sharedPixmaps
)) {
504 context
->attribs
->use_shared_memory
= False
;
506 if (XShmPixmapFormat(context
->dpy
)==ZPixmap
)
507 context
->flags
.use_shared_pixmap
= sharedPixmaps
;
517 bestContext(Display
*dpy
, int screen_number
, RContext
*context
)
519 XVisualInfo
*vinfo
=NULL
, rvinfo
;
520 int best
= -1, numvis
, i
;
522 XSetWindowAttributes attr
;
524 rvinfo
.class = TrueColor
;
525 rvinfo
.screen
= screen_number
;
526 flags
= VisualClassMask
| VisualScreenMask
;
528 vinfo
= XGetVisualInfo(dpy
, flags
, &rvinfo
, &numvis
);
529 if (vinfo
) { /* look for a TrueColor, 24-bit or more (pref 24) */
530 for (i
=numvis
-1, best
= -1; i
>=0; i
--) {
531 if (vinfo
[i
].depth
== 24) best
= i
;
532 else if (vinfo
[i
].depth
>24 && best
<0) best
= i
;
537 if (best
== -1) { /* look for a DirectColor, 24-bit or more (pref 24) */
538 rvinfo
.class = DirectColor
;
539 if (vinfo
) XFree((char *) vinfo
);
540 vinfo
= XGetVisualInfo(dpy
, flags
, &rvinfo
, &numvis
);
542 for (i
=0, best
= -1; i
<numvis
; i
++) {
543 if (vinfo
[i
].depth
== 24) best
= i
;
544 else if (vinfo
[i
].depth
>24 && best
<0) best
= i
;
550 context
->visual
= vinfo
[best
].visual
;
551 context
->depth
= vinfo
[best
].depth
;
552 context
->vclass
= vinfo
[best
].class;
553 getColormap(context
, screen_number
);
554 attr
.colormap
= context
->cmap
;
555 attr
.override_redirect
= True
;
556 attr
.border_pixel
= 0;
558 XCreateWindow(dpy
, RootWindow(dpy
, screen_number
),
559 1, 1, 1, 1, 0, context
->depth
,
560 CopyFromParent
, context
->visual
,
561 CWBorderPixel
|CWColormap
|CWOverrideRedirect
, &attr
);
562 /* XSetWindowColormap(dpy, context->drawable, context->cmap);*/
564 if (vinfo
) XFree((char *) vinfo
);