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);
27 RrInstance* RrInstanceNew (Display *display, gint screen)
29 definst = g_new (RrInstance, 1);
30 definst->display = display;
31 definst->screen = screen;
33 definst->depth = DefaultDepth(display, screen);
34 definst->visual = DefaultVisual(display, screen);
35 definst->colormap = DefaultColormap(display, screen);
37 definst->pseudo_colors = NULL;
39 definst->color_hash = g_hash_table_new(g_int_hash, g_int_equal);
41 switch (definst->visual->class) {
43 RrTrueColorSetup(definst);
49 RrPseudoColorSetup(definst);
52 g_critical("Unsupported visual class");
54 return definst = NULL;
59 void RrTrueColorSetup (RrInstance *inst)
61 unsigned long red_mask, green_mask, blue_mask;
62 XImage *timage = NULL;
64 timage = XCreateImage(inst->display, inst->visual, inst->depth,
65 ZPixmap, 0, NULL, 1, 1, 32, 0);
66 g_assert(timage != NULL);
67 /* find the offsets for each color in the visual's masks */
68 inst->red_mask = red_mask = timage->red_mask;
69 inst->green_mask = green_mask = timage->green_mask;
70 inst->blue_mask = blue_mask = timage->blue_mask;
73 inst->green_offset = 0;
74 inst->blue_offset = 0;
76 while (! (red_mask & 1)) { inst->red_offset++; red_mask >>= 1; }
77 while (! (green_mask & 1)) { inst->green_offset++; green_mask >>= 1; }
78 while (! (blue_mask & 1)) { inst->blue_offset++; blue_mask >>= 1; }
80 inst->red_shift = inst->green_shift = inst->blue_shift = 8;
81 while (red_mask) { red_mask >>= 1; inst->red_shift--; }
82 while (green_mask) { green_mask >>= 1; inst->green_shift--; }
83 while (blue_mask) { blue_mask >>= 1; inst->blue_shift--; }
87 #define RrPseudoNcolors(isnt) (1 << (inst->pseudo_bpc * 3))
89 void RrPseudoColorSetup (RrInstance *inst)
92 int tr, tg, tb, n, r, g, b, i, incolors, ii;
96 /* determine the number of colors and the bits-per-color */
97 inst->pseudo_bpc = 2; /* XXX THIS SHOULD BE A USER OPTION */
98 g_assert(inst->pseudo_bpc >= 1);
99 _ncolors = RrPseudoNcolors(inst);
101 if (_ncolors > 1 << inst->depth) {
102 g_warning("PseudoRenderControl: Invalid colormap size. Resizing.\n");
103 inst->pseudo_bpc = 1 << (inst->depth/3) >> 3;
104 _ncolors = 1 << (inst->pseudo_bpc * 3);
107 /* build a color cube */
108 inst->pseudo_colors = g_new(XColor, _ncolors);
109 cpc = 1 << inst->pseudo_bpc; /* colors per channel */
111 for (n = 0, r = 0; r < cpc; r++)
112 for (g = 0; g < cpc; g++)
113 for (b = 0; b < cpc; b++, n++) {
114 tr = (int)(((float)(r)/(float)(cpc-1)) * 0xFF);
115 tg = (int)(((float)(g)/(float)(cpc-1)) * 0xFF);
116 tb = (int)(((float)(b)/(float)(cpc-1)) * 0xFF);
117 inst->pseudo_colors[n].red = tr | tr << 8;
118 inst->pseudo_colors[n].green = tg | tg << 8;
119 inst->pseudo_colors[n].blue = tb | tb << 8;
120 /* used to track allocation */
121 inst->pseudo_colors[n].flags = DoRed|DoGreen|DoBlue;
124 /* allocate the colors */
125 for (i = 0; i < _ncolors; i++)
126 if (!XAllocColor(inst->display, inst->colormap,
127 &inst->pseudo_colors[i]))
128 inst->pseudo_colors[i].flags = 0; /* mark it as unallocated */
130 /* try allocate any colors that failed allocation above */
132 /* get the allocated values from the X server
133 (only the first 256 XXX why!?)
135 incolors = (((1 << inst->depth) > 256) ? 256 : (1 << inst->depth));
136 for (i = 0; i < incolors; i++)
137 icolors[i].pixel = i;
138 XQueryColors(inst->display, inst->colormap, icolors, incolors);
140 /* try match unallocated ones */
141 for (i = 0; i < _ncolors; i++) {
142 if (!inst->pseudo_colors[i].flags) { /* if it wasn't allocated... */
143 unsigned long closest = 0xffffffff, close = 0;
144 for (ii = 0; ii < incolors; ii++) {
145 /* find deviations */
146 r = (inst->pseudo_colors[i].red - icolors[ii].red) & 0xff;
147 g = (inst->pseudo_colors[i].green - icolors[ii].green) & 0xff;
148 b = (inst->pseudo_colors[i].blue - icolors[ii].blue) & 0xff;
149 /* find a weighted absolute deviation */
150 dev = (r * r) + (g * g) + (b * b);
158 inst->pseudo_colors[i].red = icolors[close].red;
159 inst->pseudo_colors[i].green = icolors[close].green;
160 inst->pseudo_colors[i].blue = icolors[close].blue;
161 inst->pseudo_colors[i].pixel = icolors[close].pixel;
163 /* try alloc this closest color, it had better succeed! */
164 if (XAllocColor(inst->display, inst->colormap,
165 &inst->pseudo_colors[i]))
166 /* mark as alloced */
167 inst->pseudo_colors[i].flags = DoRed|DoGreen|DoBlue;
169 /* wtf has gone wrong, its already alloced for chissake! */
170 g_assert_not_reached();
175 void RrInstanceFree (RrInstance *inst)
178 if (inst == definst) definst = NULL;
179 g_free(inst->pseudo_colors);
180 g_hash_table_destroy(inst->color_hash);
184 Display* RrDisplay (const RrInstance *inst)
186 return (inst ? inst : definst)->display;
189 gint RrScreen (const RrInstance *inst)
191 return (inst ? inst : definst)->screen;
194 Window RrRootWindow (const RrInstance *inst)
196 return RootWindow (RrDisplay (inst), RrScreen (inst));
199 Visual *RrVisual (const RrInstance *inst)
201 return (inst ? inst : definst)->visual;
204 gint RrDepth (const RrInstance *inst)
206 return (inst ? inst : definst)->depth;
209 Colormap RrColormap (const RrInstance *inst)
211 return (inst ? inst : definst)->colormap;
214 gint RrRedOffset (const RrInstance *inst)
216 return (inst ? inst : definst)->red_offset;
219 gint RrGreenOffset (const RrInstance *inst)
221 return (inst ? inst : definst)->green_offset;
224 gint RrBlueOffset (const RrInstance *inst)
226 return (inst ? inst : definst)->blue_offset;
229 gint RrRedShift (const RrInstance *inst)
231 return (inst ? inst : definst)->red_shift;
234 gint RrGreenShift (const RrInstance *inst)
236 return (inst ? inst : definst)->green_shift;
239 gint RrBlueShift (const RrInstance *inst)
241 return (inst ? inst : definst)->blue_shift;
244 gint RrRedMask (const RrInstance *inst)
246 return (inst ? inst : definst)->red_mask;
249 gint RrGreenMask (const RrInstance *inst)
251 return (inst ? inst : definst)->green_mask;
254 gint RrBlueMask (const RrInstance *inst)
256 return (inst ? inst : definst)->blue_mask;
259 guint RrPseudoBPC (const RrInstance *inst)
261 return (inst ? inst : definst)->pseudo_bpc;
264 XColor *RrPseudoColors (const RrInstance *inst)
266 return (inst ? inst : definst)->pseudo_colors;
269 GHashTable* RrColorHash (const RrInstance *inst)
271 return (inst ? inst : definst)->color_hash;