- Added a global var WErrorCode similar to errno or RErrorCode.
[wmaker-crm.git] / wrlib / context.c
blob2772176eb8c848e4804238ffcd8b5e5f5d9a8b5b
1 /* context.c - X context management
2 *
3 * Raster graphics library
4 *
5 * Copyright (c) 1997, 1998, 1999 Alfredo K. Kojima
6 *
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.
22 #include <config.h>
24 #include <X11/Xlib.h>
25 #include <X11/Xutil.h>
26 #include <X11/Xatom.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <assert.h>
33 #include <math.h>
35 #include "StdCmap.h"
37 #include "wraster.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 */
49 0,
53 True, /* use_shared_memory */
54 RMitchellFilter,
55 RUseStdColormap
62 * Colormap allocation for PseudoColor visuals:
65 * switch standardColormap:
66 * none:
67 * allocate colors according to colors_per_channel
69 * best/default:
70 * if there's a std colormap defined
71 then use it
73 * else
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.
86 * Returns: -
88 * Side effects: -
90 * Notes: -
91 *----------------------------------------------------------------------
93 static Bool
94 allocateStandardPseudoColor(RContext *ctx, XStandardColormap *stdcmap)
96 int i;
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");
106 return False;
109 ctx->colors = malloc(sizeof(XColor)*ctx->ncolors);
110 if (!ctx->colors) {
111 RErrorCode = RERR_NOMEMORY;
113 return False;
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);
127 #undef calc
129 return True;
133 static Bool
134 setupStandardColormap(RContext *ctx, Atom property)
136 if (!XmuLookupStandardColormap(ctx->dpy, ctx->screen_number,
137 ctx->visual->visualid,
138 ctx->depth, property,
139 True, True)) {
140 RErrorCode = RERR_STDCMAPFAIL;
142 return False;
144 return True;
155 static Bool
156 allocatePseudoColor(RContext *ctx)
158 XColor *colors;
159 XColor avcolors[256];
160 int avncolors;
161 int i, ncolors, r, g, b;
162 int retries;
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);
176 if (!colors) {
177 RErrorCode = RERR_NOMEMORY;
178 return False;
180 i=0;
182 if ((ctx->attribs->flags & RC_GammaCorrection) && ctx->attribs->rgamma > 0
183 && ctx->attribs->ggamma > 0 && ctx->attribs->bgamma > 0) {
184 double rg, gg, bg;
185 double tmp;
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));
208 i++;
213 } else {
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;
221 i++;
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 */
230 } else {
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) {
243 int j;
244 unsigned long cdiff=0xffffffff, diff;
245 unsigned long closest=0;
247 retries = 2;
249 while (retries--) {
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;
256 if (diff<cdiff) {
257 cdiff = diff;
258 closest = j;
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 */
269 #ifdef DEBUG
270 printf("close color allocation failed. Retrying...\n");
271 #endif
276 ctx->colors = colors;
277 ctx->ncolors = ncolors;
279 return True;
283 static XColor*
284 allocateGrayScale(RContext *ctx)
286 XColor *colors;
287 XColor avcolors[256];
288 int avncolors;
289 int i, ncolors, r, g, b;
290 int retries;
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;
298 } else {
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);
314 if (!colors) {
315 RErrorCode = RERR_NOMEMORY;
316 return False;
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++) {
326 #ifdef DEBUG
327 printf("trying:%x,%x,%x\n",colors[i].red,colors[i].green,colors[i].blue);
328 #endif
329 if (!XAllocColor(ctx->dpy, ctx->cmap, &(colors[i]))) {
330 colors[i].flags = 0; /* failed */
331 #ifdef DEBUG
332 printf("failed:%x,%x,%x\n",colors[i].red,colors[i].green,colors[i].blue);
333 #endif
334 } else {
335 colors[i].flags = DoRed|DoGreen|DoBlue;
336 #ifdef DEBUG
337 printf("success:%x,%x,%x\n",colors[i].red,colors[i].green,colors[i].blue);
338 #endif
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) {
350 int j;
351 unsigned long cdiff=0xffffffff, diff;
352 unsigned long closest=0;
354 retries = 2;
356 while (retries--) {
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;
363 if (diff<cdiff) {
364 cdiff = diff;
365 closest = j;
368 /* allocate closest color found */
369 #ifdef DEBUG
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);
371 #endif
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 */
379 #ifdef DEBUG
380 printf("close color allocation failed. Retrying...\n");
381 #endif
385 return colors;
389 static Bool
390 setupPseudoColorColormap(RContext *context)
392 Atom property = 0;
394 if (context->attribs->standard_colormap_mode == RCreateStdColormap) {
395 property = XInternAtom(context->dpy, "RGB_DEFAULT_MAP", False);
397 if (!setupStandardColormap(context, property)) {
398 return False;
402 if (context->attribs->standard_colormap_mode != RIgnoreStdColormap) {
403 XStandardColormap *maps;
404 int count, i;
406 if (!property) {
407 property = XInternAtom(context->dpy, "RGB_BEST_MAP", False);
408 if (!XGetRGBColormaps(context->dpy,
409 DefaultRootWindow(context->dpy),
410 &maps, &count, property)) {
411 maps = NULL;
414 if (!maps) {
415 property = XInternAtom(context->dpy, "RGB_DEFAULT_MAP", False);
416 if (!XGetRGBColormaps(context->dpy,
417 DefaultRootWindow(context->dpy),
418 &maps, &count, property)) {
419 maps = NULL;
422 } else {
423 if (!XGetRGBColormaps(context->dpy,
424 DefaultRootWindow(context->dpy),
425 &maps, &count, property)) {
426 maps = NULL;
430 if (maps) {
431 int theMap = -1;
433 for (i = 0; i < count; i++) {
434 if (maps[i].visualid == context->visual->visualid) {
435 theMap = i;
436 break;
440 if (theMap < 0) {
441 puts("wrlib: no std cmap found");
444 if (theMap >= 0
445 && allocateStandardPseudoColor(context, &maps[theMap])) {
447 context->std_rgb_map = XAllocStandardColormap();
449 *context->std_rgb_map = maps[theMap];
451 XFree(maps);
453 return True;
456 XFree(maps);
460 context->attribs->standard_colormap_mode = RIgnoreStdColormap;
462 /* RIgnoreStdColormap and fallback */
463 return allocatePseudoColor(context);
469 static char*
470 mygetenv(char *var, int scr)
472 char *p;
473 char varname[64];
475 sprintf(varname, "%s%i", var, scr);
476 p = getenv(varname);
477 if (!p) {
478 p = getenv(var);
480 return p;
484 static void
485 gatherconfig(RContext *context, int screen_n)
487 char *ptr;
489 ptr = mygetenv("WRASTER_GAMMA", screen_n);
490 if (ptr) {
491 float g1,g2,g3;
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",
495 ptr);
496 } else {
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);
504 if (ptr) {
505 int i;
506 if (sscanf(ptr, "%d", &i)!=1 || i<2 || i>6) {
507 printf("wrlib: invalid value for color resolution \"%s\"\n",ptr);
508 } else {
509 context->attribs->flags |= RC_ColorsPerChannel;
510 context->attribs->colors_per_channel = i;
516 static void
517 getColormap(RContext *context, int screen_number)
519 Colormap cmap = None;
520 XStandardColormap *cmaps;
521 int ncmaps, i;
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) {
528 puts("ACHOU");
529 cmap = cmaps[i].colormap;
530 break;
533 XFree(cmaps);
535 if (cmap == None) {
536 XColor color;
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;
555 static int
556 count_offset(unsigned long mask)
558 int i;
560 i=0;
561 while ((mask & 1)==0) {
562 i++;
563 mask = mask >> 1;
565 return i;
569 RContext*
570 RCreateContext(Display *dpy, int screen_number, RContextAttributes *attribs)
572 RContext *context;
573 XGCValues gcv;
576 context = malloc(sizeof(RContext));
577 if (!context) {
578 RErrorCode = RERR_NOMEMORY;
579 return NULL;
581 memset(context, 0, sizeof(RContext));
583 context->dpy = dpy;
585 context->screen_number = screen_number;
587 context->attribs = malloc(sizeof(RContextAttributes));
588 if (!context->attribs) {
589 free(context);
590 RErrorCode = RERR_NOMEMORY;
591 return NULL;
593 if (!attribs)
594 *context->attribs = DEFAULT_CONTEXT_ATTRIBS;
595 else
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;
609 int nret;
611 templ.screen = screen_number;
612 templ.visualid = context->attribs->visualid;
613 vinfo = XGetVisualInfo(context->dpy, VisualIDMask|VisualScreenMask,
614 &templ, &nret);
615 if (!vinfo || nret==0) {
616 free(context);
617 RErrorCode = RERR_BADVISUALID;
618 return NULL;
621 if (vinfo[0].visual == DefaultVisual(dpy, screen_number)) {
622 context->attribs->flags |= RC_DefaultVisual;
623 } else {
624 XSetWindowAttributes attr;
625 unsigned long mask;
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;
636 context->drawable =
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);*/
642 XFree(vinfo);
645 /* use default */
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)) {
667 return NULL;
670 } else if (context->vclass == GrayScale || context->vclass == StaticGray) {
671 context->colors = allocateGrayScale(context);
672 if (!context->colors) {
673 return NULL;
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 */
686 #ifdef XSHM
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) {
693 int major, minor;
694 Bool sharedPixmaps;
696 context->flags.use_shared_pixmap = 0;
698 if (!XShmQueryVersion(context->dpy, &major, &minor, &sharedPixmaps)) {
699 context->attribs->use_shared_memory = False;
700 } else {
701 if (XShmPixmapFormat(context->dpy)==ZPixmap)
702 context->flags.use_shared_pixmap = sharedPixmaps;
705 #endif
707 return context;
711 static Bool
712 bestContext(Display *dpy, int screen_number, RContext *context)
714 XVisualInfo *vinfo=NULL, rvinfo;
715 int best = -1, numvis, i;
716 long flags;
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;
731 #if 0
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);
736 if (vinfo) {
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;
743 #endif
744 if (best > -1) {
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;
752 context->drawable =
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);
761 if (best < 0)
762 return False;
763 else
764 return True;