Fix arrow keys in switchpanel.
[wmaker-crm.git] / wrlib / context.c
blob3fdaafe444c19fa684ed63efc186591dcb29f7aa
1 /* context.c - X context management
3 * Raster graphics library
5 * Copyright (c) 1997-2003 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., 51 Franklin St, Fifth Floor, Boston,
20 * MA 02110-1301, USA.
23 #include <config.h>
25 #include <X11/Xlib.h>
26 #include <X11/Xutil.h>
27 #include <X11/Xatom.h>
28 #include <X11/Xmu/StdCmap.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <assert.h>
35 #include <math.h>
37 #include "wraster.h"
39 extern void _wraster_change_filter(int type);
41 static Bool bestContext(Display * dpy, int screen_number, RContext * context);
43 static RContextAttributes DEFAULT_CONTEXT_ATTRIBS = {
44 RC_UseSharedMemory | RC_RenderMode | RC_ColorsPerChannel, /* flags */
45 RDitheredRendering, /* render_mode */
46 4, /* colors_per_channel */
51 True, /* use_shared_memory */
52 RMitchellFilter,
53 RUseStdColormap
58 * Colormap allocation for PseudoColor visuals:
61 * switch standardColormap:
62 * none:
63 * allocate colors according to colors_per_channel
65 * best/default:
66 * if there's a std colormap defined then use it
68 * else
69 * create a std colormap and set it
73 *----------------------------------------------------------------------
74 * allocateStandardPseudoColor
75 * Creates the internal colormap for PseudoColor, setting the
76 * color values according to the supplied standard colormap.
78 * Returns: -
80 * Side effects: -
82 * Notes: -
83 *----------------------------------------------------------------------
85 static Bool allocateStandardPseudoColor(RContext * ctx, XStandardColormap * stdcmap)
87 int i;
89 ctx->ncolors = stdcmap->red_max * stdcmap->red_mult
90 + stdcmap->green_max * stdcmap->green_mult + stdcmap->blue_max * stdcmap->blue_mult + 1;
92 if (ctx->ncolors <= 1) {
93 RErrorCode = RERR_INTERNAL;
94 puts("wraster: bad standard colormap");
96 return False;
99 ctx->colors = malloc(sizeof(XColor) * ctx->ncolors);
100 if (!ctx->colors) {
101 RErrorCode = RERR_NOMEMORY;
103 return False;
106 ctx->pixels = malloc(sizeof(unsigned long) * ctx->ncolors);
107 if (!ctx->pixels) {
109 free(ctx->colors);
110 ctx->colors = NULL;
112 RErrorCode = RERR_NOMEMORY;
114 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);
126 ctx->pixels[i] = ctx->colors[i].pixel;
129 #undef calc
131 return True;
134 static Bool setupStandardColormap(RContext * ctx, Atom property)
136 if (!XmuLookupStandardColormap(ctx->dpy, ctx->screen_number,
137 ctx->visual->visualid, ctx->depth, property, True, True)) {
138 RErrorCode = RERR_STDCMAPFAIL;
140 return False;
142 return True;
145 static Bool allocatePseudoColor(RContext * ctx)
147 XColor *colors;
148 XColor avcolors[256];
149 int avncolors;
150 int i, ncolors, r, g, b;
151 int retries;
152 int cpc = ctx->attribs->colors_per_channel;
154 ncolors = cpc * cpc * cpc;
156 if (ncolors > (1 << ctx->depth)) {
157 /* reduce colormap size */
158 cpc = ctx->attribs->colors_per_channel = 1 << ((int)ctx->depth / 3);
159 ncolors = cpc * cpc * cpc;
162 assert(cpc >= 2 && ncolors <= (1 << ctx->depth));
164 colors = malloc(sizeof(XColor) * ncolors);
165 if (!colors) {
166 RErrorCode = RERR_NOMEMORY;
167 return False;
170 ctx->pixels = malloc(sizeof(unsigned long) * ncolors);
171 if (!ctx->pixels) {
172 free(colors);
173 RErrorCode = RERR_NOMEMORY;
174 return False;
177 i = 0;
179 if ((ctx->attribs->flags & RC_GammaCorrection) && ctx->attribs->rgamma > 0
180 && ctx->attribs->ggamma > 0 && ctx->attribs->bgamma > 0) {
181 double rg, gg, bg;
182 double tmp;
184 /* do gamma correction */
185 rg = 1.0 / ctx->attribs->rgamma;
186 gg = 1.0 / ctx->attribs->ggamma;
187 bg = 1.0 / ctx->attribs->bgamma;
188 for (r = 0; r < cpc; r++) {
189 for (g = 0; g < cpc; g++) {
190 for (b = 0; b < cpc; b++) {
191 colors[i].red = (r * 0xffff) / (cpc - 1);
192 colors[i].green = (g * 0xffff) / (cpc - 1);
193 colors[i].blue = (b * 0xffff) / (cpc - 1);
194 colors[i].flags = DoRed | DoGreen | DoBlue;
196 tmp = (double)colors[i].red / 65536.0;
197 colors[i].red = (unsigned short)(65536.0 * pow(tmp, rg));
199 tmp = (double)colors[i].green / 65536.0;
200 colors[i].green = (unsigned short)(65536.0 * pow(tmp, gg));
202 tmp = (double)colors[i].blue / 65536.0;
203 colors[i].blue = (unsigned short)(65536.0 * pow(tmp, bg));
205 i++;
210 } else {
211 for (r = 0; r < cpc; r++) {
212 for (g = 0; g < cpc; g++) {
213 for (b = 0; b < cpc; b++) {
214 colors[i].red = (r * 0xffff) / (cpc - 1);
215 colors[i].green = (g * 0xffff) / (cpc - 1);
216 colors[i].blue = (b * 0xffff) / (cpc - 1);
217 colors[i].flags = DoRed | DoGreen | DoBlue;
218 i++;
223 /* try to allocate the colors */
224 for (i = 0; i < ncolors; i++) {
225 if (!XAllocColor(ctx->dpy, ctx->cmap, &(colors[i]))) {
226 colors[i].flags = 0; /* failed */
227 } else {
228 colors[i].flags = DoRed | DoGreen | DoBlue;
231 /* try to allocate close values for the colors that couldn't
232 * be allocated before */
233 avncolors = (1 << ctx->depth > 256 ? 256 : 1 << ctx->depth);
234 for (i = 0; i < avncolors; i++)
235 avcolors[i].pixel = i;
237 XQueryColors(ctx->dpy, ctx->cmap, avcolors, avncolors);
239 for (i = 0; i < ncolors; i++) {
240 if (colors[i].flags == 0) {
241 int j;
242 unsigned long cdiff = 0xffffffff, diff;
243 unsigned long closest = 0;
245 retries = 2;
247 while (retries--) {
248 /* find closest color */
249 for (j = 0; j < avncolors; j++) {
250 r = (colors[i].red - avcolors[i].red) >> 8;
251 g = (colors[i].green - avcolors[i].green) >> 8;
252 b = (colors[i].blue - avcolors[i].blue) >> 8;
253 diff = r * r + g * g + b * b;
254 if (diff < cdiff) {
255 cdiff = diff;
256 closest = j;
259 /* allocate closest color found */
260 colors[i].red = avcolors[closest].red;
261 colors[i].green = avcolors[closest].green;
262 colors[i].blue = avcolors[closest].blue;
263 if (XAllocColor(ctx->dpy, ctx->cmap, &colors[i])) {
264 colors[i].flags = DoRed | DoGreen | DoBlue;
265 break; /* succeeded, don't need to retry */
267 #ifdef WRLIB_DEBUG
268 fputs("close color allocation failed. Retrying...\n", stderr);
269 #endif
274 ctx->colors = colors;
275 ctx->ncolors = ncolors;
277 /* fill the pixels shortcut array */
278 for (i = 0; i < ncolors; i++) {
279 ctx->pixels[i] = ctx->colors[i].pixel;
282 return True;
285 static XColor *allocateGrayScale(RContext * ctx)
287 XColor *colors;
288 XColor avcolors[256];
289 int avncolors;
290 int i, ncolors, r, g, b;
291 int retries;
292 int cpc = ctx->attribs->colors_per_channel;
294 ncolors = cpc * cpc * cpc;
296 if (ctx->vclass == StaticGray) {
297 /* we might as well use all grays */
298 ncolors = 1 << ctx->depth;
299 } else {
300 if (ncolors > (1 << ctx->depth)) {
301 /* reduce colormap size */
302 cpc = ctx->attribs->colors_per_channel = 1 << ((int)ctx->depth / 3);
303 ncolors = cpc * cpc * cpc;
306 assert(cpc >= 2 && ncolors <= (1 << ctx->depth));
309 if (ncolors >= 256 && ctx->vclass == StaticGray) {
310 /* don't need dithering for 256 levels of gray in StaticGray visual */
311 ctx->attribs->render_mode = RBestMatchRendering;
314 colors = malloc(sizeof(XColor) * ncolors);
315 if (!colors) {
316 RErrorCode = RERR_NOMEMORY;
317 return False;
319 for (i = 0; i < ncolors; i++) {
320 colors[i].red = (i * 0xffff) / (ncolors - 1);
321 colors[i].green = (i * 0xffff) / (ncolors - 1);
322 colors[i].blue = (i * 0xffff) / (ncolors - 1);
323 colors[i].flags = DoRed | DoGreen | DoBlue;
325 /* try to allocate the colors */
326 for (i = 0; i < ncolors; i++) {
327 #ifdef WRLIB_DEBUG
328 fprintf(stderr, "trying:%x,%x,%x\n", colors[i].red, colors[i].green, colors[i].blue);
329 #endif
330 if (!XAllocColor(ctx->dpy, ctx->cmap, &(colors[i]))) {
331 colors[i].flags = 0; /* failed */
332 #ifdef WRLIB_DEBUG
333 fprintf(stderr, "failed:%x,%x,%x\n", colors[i].red, colors[i].green, colors[i].blue);
334 #endif
335 } else {
336 colors[i].flags = DoRed | DoGreen | DoBlue;
337 #ifdef WRLIB_DEBUG
338 fprintf(stderr, "success:%x,%x,%x\n", colors[i].red, colors[i].green, colors[i].blue);
339 #endif
342 /* try to allocate close values for the colors that couldn't
343 * be allocated before */
344 avncolors = (1 << ctx->depth > 256 ? 256 : 1 << ctx->depth);
345 for (i = 0; i < avncolors; i++)
346 avcolors[i].pixel = i;
348 XQueryColors(ctx->dpy, ctx->cmap, avcolors, avncolors);
350 for (i = 0; i < ncolors; i++) {
351 if (colors[i].flags == 0) {
352 int j;
353 unsigned long cdiff = 0xffffffff, diff;
354 unsigned long closest = 0;
356 retries = 2;
358 while (retries--) {
359 /* find closest color */
360 for (j = 0; j < avncolors; j++) {
361 r = (colors[i].red - avcolors[i].red) >> 8;
362 g = (colors[i].green - avcolors[i].green) >> 8;
363 b = (colors[i].blue - avcolors[i].blue) >> 8;
364 diff = r * r + g * g + b * b;
365 if (diff < cdiff) {
366 cdiff = diff;
367 closest = j;
370 /* allocate closest color found */
371 #ifdef WRLIB_DEBUG
372 fprintf(stderr, "best match:%x,%x,%x => %x,%x,%x\n",
373 colors[i].red, colors[i].green, colors[i].blue,
374 avcolors[closest].red, avcolors[closest].green, avcolors[closest].blue);
375 #endif
376 colors[i].red = avcolors[closest].red;
377 colors[i].green = avcolors[closest].green;
378 colors[i].blue = avcolors[closest].blue;
379 if (XAllocColor(ctx->dpy, ctx->cmap, &colors[i])) {
380 colors[i].flags = DoRed | DoGreen | DoBlue;
381 break; /* succeeded, don't need to retry */
383 #ifdef WRLIB_DEBUG
384 fputs("close color allocation failed. Retrying...\n", stderr);
385 #endif
389 return colors;
392 static Bool setupPseudoColorColormap(RContext * context)
394 Atom property = 0;
396 if (context->attribs->standard_colormap_mode == RCreateStdColormap) {
397 property = XInternAtom(context->dpy, "RGB_DEFAULT_MAP", False);
399 if (!setupStandardColormap(context, property)) {
400 return False;
404 if (context->attribs->standard_colormap_mode != RIgnoreStdColormap) {
405 XStandardColormap *maps;
406 int count, i;
408 if (!property) {
409 property = XInternAtom(context->dpy, "RGB_BEST_MAP", False);
410 if (!XGetRGBColormaps(context->dpy,
411 DefaultRootWindow(context->dpy), &maps, &count, property)) {
412 maps = NULL;
415 if (!maps) {
416 property = XInternAtom(context->dpy, "RGB_DEFAULT_MAP", False);
417 if (!XGetRGBColormaps(context->dpy,
418 DefaultRootWindow(context->dpy), &maps, &count, property)) {
419 maps = NULL;
422 } else {
423 if (!XGetRGBColormaps(context->dpy,
424 DefaultRootWindow(context->dpy), &maps, &count, property)) {
425 maps = NULL;
429 if (maps) {
430 int theMap = -1;
432 for (i = 0; i < count; i++) {
433 if (maps[i].visualid == context->visual->visualid) {
434 theMap = i;
435 break;
439 if (theMap < 0) {
440 puts("wrlib: no std cmap found");
443 if (theMap >= 0 && allocateStandardPseudoColor(context, &maps[theMap])) {
445 context->std_rgb_map = XAllocStandardColormap();
447 *context->std_rgb_map = maps[theMap];
449 context->cmap = context->std_rgb_map->colormap;
451 XFree(maps);
453 return True;
456 XFree(maps);
460 context->attribs->standard_colormap_mode = RIgnoreStdColormap;
462 /* RIgnoreStdColormap and fallback */
463 return allocatePseudoColor(context);
466 static char *mygetenv(const char *var, int scr)
468 char *p;
469 char varname[64];
471 snprintf(varname, sizeof(varname), "%s%i", var, scr);
472 p = getenv(varname);
473 if (!p) {
474 p = getenv(var);
476 return p;
479 static void gatherconfig(RContext * context, int screen_n)
481 char *ptr;
483 ptr = mygetenv("WRASTER_GAMMA", screen_n);
484 if (ptr) {
485 float g1, g2, g3;
486 if (sscanf(ptr, "%f/%f/%f", &g1, &g2, &g3) != 3 || g1 <= 0.0 || g2 <= 0.0 || g3 <= 0.0) {
487 printf("wrlib: invalid value(s) for gamma correction \"%s\"\n", ptr);
488 } else {
489 context->attribs->flags |= RC_GammaCorrection;
490 context->attribs->rgamma = g1;
491 context->attribs->ggamma = g2;
492 context->attribs->bgamma = g3;
495 ptr = mygetenv("WRASTER_COLOR_RESOLUTION", screen_n);
496 if (ptr) {
497 int i;
498 if (sscanf(ptr, "%d", &i) != 1 || i < 2 || i > 6) {
499 printf("wrlib: invalid value for color resolution \"%s\"\n", ptr);
500 } else {
501 context->attribs->flags |= RC_ColorsPerChannel;
502 context->attribs->colors_per_channel = i;
507 static void getColormap(RContext * context, int screen_number)
509 Colormap cmap = None;
510 XStandardColormap *cmaps;
511 int ncmaps, i;
513 if (XGetRGBColormaps(context->dpy,
514 RootWindow(context->dpy, screen_number), &cmaps, &ncmaps, XA_RGB_DEFAULT_MAP)) {
515 for (i = 0; i < ncmaps; ++i) {
516 if (cmaps[i].visualid == context->visual->visualid) {
517 cmap = cmaps[i].colormap;
518 break;
521 XFree(cmaps);
523 if (cmap == None) {
524 XColor color;
526 cmap = XCreateColormap(context->dpy,
527 RootWindow(context->dpy, screen_number), context->visual, AllocNone);
529 color.red = color.green = color.blue = 0;
530 XAllocColor(context->dpy, cmap, &color);
531 context->black = color.pixel;
533 color.red = color.green = color.blue = 0xffff;
534 XAllocColor(context->dpy, cmap, &color);
535 context->white = color.pixel;
538 context->cmap = cmap;
541 static int count_offset(unsigned long mask)
543 int i;
545 i = 0;
546 while ((mask & 1) == 0) {
547 i++;
548 mask = mask >> 1;
550 return i;
553 RContext *RCreateContext(Display * dpy, int screen_number, const RContextAttributes * attribs)
555 RContext *context;
556 XGCValues gcv;
558 context = malloc(sizeof(RContext));
559 if (!context) {
560 RErrorCode = RERR_NOMEMORY;
561 return NULL;
563 memset(context, 0, sizeof(RContext));
565 context->dpy = dpy;
567 context->screen_number = screen_number;
569 context->attribs = malloc(sizeof(RContextAttributes));
570 if (!context->attribs) {
571 free(context);
572 RErrorCode = RERR_NOMEMORY;
573 return NULL;
575 if (!attribs)
576 *context->attribs = DEFAULT_CONTEXT_ATTRIBS;
577 else
578 *context->attribs = *attribs;
580 if (!(context->attribs->flags & RC_StandardColormap)) {
581 context->attribs->standard_colormap_mode = RUseStdColormap;
584 if (!(context->attribs->flags & RC_ScalingFilter)) {
585 context->attribs->flags |= RC_ScalingFilter;
586 context->attribs->scaling_filter = RMitchellFilter;
589 /* get configuration from environment variables */
590 gatherconfig(context, screen_number);
591 _wraster_change_filter(context->attribs->scaling_filter);
592 if ((context->attribs->flags & RC_VisualID)) {
593 XVisualInfo *vinfo, templ;
594 int nret;
596 templ.screen = screen_number;
597 templ.visualid = context->attribs->visualid;
598 vinfo = XGetVisualInfo(context->dpy, VisualIDMask | VisualScreenMask, &templ, &nret);
599 if (!vinfo || nret == 0) {
600 free(context);
601 RErrorCode = RERR_BADVISUALID;
602 return NULL;
605 if (vinfo[0].visual == DefaultVisual(dpy, screen_number)) {
606 context->attribs->flags |= RC_DefaultVisual;
607 } else {
608 XSetWindowAttributes attr;
609 unsigned long mask;
611 context->visual = vinfo[0].visual;
612 context->depth = vinfo[0].depth;
613 context->vclass = vinfo[0].class;
614 getColormap(context, screen_number);
615 attr.colormap = context->cmap;
616 attr.override_redirect = True;
617 attr.border_pixel = 0;
618 attr.background_pixel = 0;
619 mask = CWBorderPixel | CWColormap | CWOverrideRedirect | CWBackPixel;
620 context->drawable =
621 XCreateWindow(dpy, RootWindow(dpy, screen_number), 1, 1,
622 1, 1, 0, context->depth, CopyFromParent, context->visual, mask, &attr);
624 XFree(vinfo);
627 /* use default */
628 if (!context->visual) {
629 if ((context->attribs->flags & RC_DefaultVisual)
630 || !bestContext(dpy, screen_number, context)) {
631 context->visual = DefaultVisual(dpy, screen_number);
632 context->depth = DefaultDepth(dpy, screen_number);
633 context->cmap = DefaultColormap(dpy, screen_number);
634 context->drawable = RootWindow(dpy, screen_number);
635 context->black = BlackPixel(dpy, screen_number);
636 context->white = WhitePixel(dpy, screen_number);
637 context->vclass = context->visual->class;
641 gcv.function = GXcopy;
642 gcv.graphics_exposures = False;
643 context->copy_gc = XCreateGC(dpy, context->drawable, GCFunction | GCGraphicsExposures, &gcv);
645 if (context->vclass == PseudoColor || context->vclass == StaticColor) {
646 if (!setupPseudoColorColormap(context)) {
647 free(context);
648 return NULL;
650 } else if (context->vclass == GrayScale || context->vclass == StaticGray) {
651 context->colors = allocateGrayScale(context);
652 if (!context->colors) {
653 free(context);
654 return NULL;
656 } else if (context->vclass == TrueColor) {
657 /* calc offsets to create a TrueColor pixel */
658 context->red_offset = count_offset(context->visual->red_mask);
659 context->green_offset = count_offset(context->visual->green_mask);
660 context->blue_offset = count_offset(context->visual->blue_mask);
661 /* disable dithering on 24 bits visuals */
662 if (context->depth >= 24)
663 context->attribs->render_mode = RBestMatchRendering;
666 /* check avaiability of MIT-SHM */
667 #ifdef XSHM
668 if (!(context->attribs->flags & RC_UseSharedMemory)) {
669 context->attribs->flags |= RC_UseSharedMemory;
670 context->attribs->use_shared_memory = True;
673 if (context->attribs->use_shared_memory) {
674 int major, minor;
675 Bool sharedPixmaps;
677 context->flags.use_shared_pixmap = 0;
679 if (!XShmQueryVersion(context->dpy, &major, &minor, &sharedPixmaps)) {
680 context->attribs->use_shared_memory = False;
681 } else {
682 if (XShmPixmapFormat(context->dpy) == ZPixmap)
683 context->flags.use_shared_pixmap = sharedPixmaps;
686 #endif
688 return context;
691 static Bool bestContext(Display * dpy, int screen_number, RContext * context)
693 XVisualInfo *vinfo = NULL, rvinfo;
694 int best = -1, numvis, i;
695 long flags;
696 XSetWindowAttributes attr;
698 rvinfo.class = TrueColor;
699 rvinfo.screen = screen_number;
700 flags = VisualClassMask | VisualScreenMask;
702 vinfo = XGetVisualInfo(dpy, flags, &rvinfo, &numvis);
703 if (vinfo) { /* look for a TrueColor, 24-bit or more (pref 24) */
704 for (i = numvis - 1, best = -1; i >= 0; i--) {
705 if (vinfo[i].depth == 24)
706 best = i;
707 else if (vinfo[i].depth > 24 && best < 0)
708 best = i;
711 if (best > -1) {
712 context->visual = vinfo[best].visual;
713 context->depth = vinfo[best].depth;
714 context->vclass = vinfo[best].class;
715 getColormap(context, screen_number);
716 attr.colormap = context->cmap;
717 attr.override_redirect = True;
718 attr.border_pixel = 0;
719 context->drawable =
720 XCreateWindow(dpy, RootWindow(dpy, screen_number),
721 1, 1, 1, 1, 0, context->depth,
722 CopyFromParent, context->visual,
723 CWBorderPixel | CWColormap | CWOverrideRedirect, &attr);
725 if (vinfo)
726 XFree((char *)vinfo);
728 if (best < 0)
729 return False;
730 else
731 return True;