1 /* -*- indent-tabs-mode: nil; tab-width: 4; c-basic-offset: 4; -*-
3 instance.c for the Openbox window manager
4 Copyright (c) 2003 Ben Jansens
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 See the COPYING file for a copy of the GNU General Public License.
22 static RrInstance *definst = NULL;
24 static void RrTrueColorSetup (RrInstance *inst);
25 static void RrPseudoColorSetup (RrInstance *inst);
36 g_error("removing color from hash table with references");
40 RrInstance* RrInstanceNew (Display *display, gint screen)
42 definst = g_new (RrInstance, 1);
43 definst->display = display;
44 definst->screen = screen;
46 definst->depth = DefaultDepth(display, screen);
47 definst->visual = DefaultVisual(display, screen);
48 definst->colormap = DefaultColormap(display, screen);
50 definst->pseudo_colors = NULL;
52 definst->color_hash = g_hash_table_new_full(g_int_hash, g_int_equal,
55 switch (definst->visual->class) {
57 RrTrueColorSetup(definst);
63 RrPseudoColorSetup(definst);
66 g_critical("Unsupported visual class");
68 return definst = NULL;
73 void RrTrueColorSetup (RrInstance *inst)
75 unsigned long red_mask, green_mask, blue_mask;
76 XImage *timage = NULL;
78 timage = XCreateImage(inst->display, inst->visual, inst->depth,
79 ZPixmap, 0, NULL, 1, 1, 32, 0);
80 g_assert(timage != NULL);
81 /* find the offsets for each color in the visual's masks */
82 inst->red_mask = red_mask = timage->red_mask;
83 inst->green_mask = green_mask = timage->green_mask;
84 inst->blue_mask = blue_mask = timage->blue_mask;
87 inst->green_offset = 0;
88 inst->blue_offset = 0;
90 while (! (red_mask & 1)) { inst->red_offset++; red_mask >>= 1; }
91 while (! (green_mask & 1)) { inst->green_offset++; green_mask >>= 1; }
92 while (! (blue_mask & 1)) { inst->blue_offset++; blue_mask >>= 1; }
94 inst->red_shift = inst->green_shift = inst->blue_shift = 8;
95 while (red_mask) { red_mask >>= 1; inst->red_shift--; }
96 while (green_mask) { green_mask >>= 1; inst->green_shift--; }
97 while (blue_mask) { blue_mask >>= 1; inst->blue_shift--; }
101 #define RrPseudoNcolors(isnt) (1 << (inst->pseudo_bpc * 3))
103 void RrPseudoColorSetup (RrInstance *inst)
106 int tr, tg, tb, n, r, g, b, i, incolors, ii;
110 /* determine the number of colors and the bits-per-color */
111 inst->pseudo_bpc = 2; /* XXX THIS SHOULD BE A USER OPTION */
112 g_assert(inst->pseudo_bpc >= 1);
113 _ncolors = RrPseudoNcolors(inst);
115 if (_ncolors > 1 << inst->depth) {
116 g_warning("PseudoRenderControl: Invalid colormap size. Resizing.\n");
117 inst->pseudo_bpc = 1 << (inst->depth/3) >> 3;
118 _ncolors = 1 << (inst->pseudo_bpc * 3);
121 /* build a color cube */
122 inst->pseudo_colors = g_new(XColor, _ncolors);
123 cpc = 1 << inst->pseudo_bpc; /* colors per channel */
125 for (n = 0, r = 0; r < cpc; r++)
126 for (g = 0; g < cpc; g++)
127 for (b = 0; b < cpc; b++, n++) {
128 tr = (int)(((float)(r)/(float)(cpc-1)) * 0xFF);
129 tg = (int)(((float)(g)/(float)(cpc-1)) * 0xFF);
130 tb = (int)(((float)(b)/(float)(cpc-1)) * 0xFF);
131 inst->pseudo_colors[n].red = tr | tr << 8;
132 inst->pseudo_colors[n].green = tg | tg << 8;
133 inst->pseudo_colors[n].blue = tb | tb << 8;
134 /* used to track allocation */
135 inst->pseudo_colors[n].flags = DoRed|DoGreen|DoBlue;
138 /* allocate the colors */
139 for (i = 0; i < _ncolors; i++)
140 if (!XAllocColor(inst->display, inst->colormap,
141 &inst->pseudo_colors[i]))
142 inst->pseudo_colors[i].flags = 0; /* mark it as unallocated */
144 /* try allocate any colors that failed allocation above */
146 /* get the allocated values from the X server
147 (only the first 256 XXX why!?)
149 incolors = (((1 << inst->depth) > 256) ? 256 : (1 << inst->depth));
150 for (i = 0; i < incolors; i++)
151 icolors[i].pixel = i;
152 XQueryColors(inst->display, inst->colormap, icolors, incolors);
154 /* try match unallocated ones */
155 for (i = 0; i < _ncolors; i++) {
156 if (!inst->pseudo_colors[i].flags) { /* if it wasn't allocated... */
157 unsigned long closest = 0xffffffff, close = 0;
158 for (ii = 0; ii < incolors; ii++) {
159 /* find deviations */
160 r = (inst->pseudo_colors[i].red - icolors[ii].red) & 0xff;
161 g = (inst->pseudo_colors[i].green - icolors[ii].green) & 0xff;
162 b = (inst->pseudo_colors[i].blue - icolors[ii].blue) & 0xff;
163 /* find a weighted absolute deviation */
164 dev = (r * r) + (g * g) + (b * b);
172 inst->pseudo_colors[i].red = icolors[close].red;
173 inst->pseudo_colors[i].green = icolors[close].green;
174 inst->pseudo_colors[i].blue = icolors[close].blue;
175 inst->pseudo_colors[i].pixel = icolors[close].pixel;
177 /* try alloc this closest color, it had better succeed! */
178 if (XAllocColor(inst->display, inst->colormap,
179 &inst->pseudo_colors[i]))
180 /* mark as alloced */
181 inst->pseudo_colors[i].flags = DoRed|DoGreen|DoBlue;
183 /* wtf has gone wrong, its already alloced for chissake! */
184 g_assert_not_reached();
189 void RrInstanceFree (RrInstance *inst)
192 if (inst == definst) definst = NULL;
193 g_free(inst->pseudo_colors);
194 g_hash_table_destroy(inst->color_hash);
198 Display* RrDisplay (const RrInstance *inst)
200 return (inst ? inst : definst)->display;
203 gint RrScreen (const RrInstance *inst)
205 return (inst ? inst : definst)->screen;
208 Window RrRootWindow (const RrInstance *inst)
210 return RootWindow (RrDisplay (inst), RrScreen (inst));
213 Visual *RrVisual (const RrInstance *inst)
215 return (inst ? inst : definst)->visual;
218 gint RrDepth (const RrInstance *inst)
220 return (inst ? inst : definst)->depth;
223 Colormap RrColormap (const RrInstance *inst)
225 return (inst ? inst : definst)->colormap;
228 gint RrRedOffset (const RrInstance *inst)
230 return (inst ? inst : definst)->red_offset;
233 gint RrGreenOffset (const RrInstance *inst)
235 return (inst ? inst : definst)->green_offset;
238 gint RrBlueOffset (const RrInstance *inst)
240 return (inst ? inst : definst)->blue_offset;
243 gint RrRedShift (const RrInstance *inst)
245 return (inst ? inst : definst)->red_shift;
248 gint RrGreenShift (const RrInstance *inst)
250 return (inst ? inst : definst)->green_shift;
253 gint RrBlueShift (const RrInstance *inst)
255 return (inst ? inst : definst)->blue_shift;
258 gint RrRedMask (const RrInstance *inst)
260 return (inst ? inst : definst)->red_mask;
263 gint RrGreenMask (const RrInstance *inst)
265 return (inst ? inst : definst)->green_mask;
268 gint RrBlueMask (const RrInstance *inst)
270 return (inst ? inst : definst)->blue_mask;
273 guint RrPseudoBPC (const RrInstance *inst)
275 return (inst ? inst : definst)->pseudo_bpc;
278 XColor *RrPseudoColors (const RrInstance *inst)
280 return (inst ? inst : definst)->pseudo_colors;
283 GHashTable* RrColorHash (const RrInstance *inst)
285 return (inst ? inst : definst)->color_hash;