]>
Dogcows Code - chaz/openbox/blob - render/instance.c
1 /* -*- indent-tabs-mode: nil; tab-width: 4; c-basic-offset: 4; -*-
3 instance.c for the Openbox window manager
4 Copyright (c) 2006 Mikael Magnusson
5 Copyright (c) 2003 Ben Jansens
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program 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
15 GNU General Public License for more details.
17 See the COPYING file for a copy of the GNU General Public License.
23 static RrInstance
*definst
= NULL
;
25 static void RrTrueColorSetup (RrInstance
*inst
);
26 static void RrPseudoColorSetup (RrInstance
*inst
);
37 g_error("color %d (%d,%d,%d) in hash table with %d "
38 "leftover references",
39 c
->id
, RrColorRed(c
), RrColorGreen(c
), RrColorBlue(c
),
45 static void f(gpointer key
, gpointer value
, gpointer n
)
48 if (c
->id
== *(gint
*)n
)
49 g_message("color %d has %d references", c
->id
, c
->refcount
);
52 void print_refs(gint id
)
54 g_hash_table_foreach(RrColorHash(definst
), f
, &id
);
58 RrInstance
* RrInstanceNew (Display
*display
, gint screen
)
60 definst
= g_new (RrInstance
, 1);
61 definst
->display
= display
;
62 definst
->screen
= screen
;
64 definst
->depth
= DefaultDepth(display
, screen
);
65 definst
->visual
= DefaultVisual(display
, screen
);
66 definst
->colormap
= DefaultColormap(display
, screen
);
68 definst
->pseudo_colors
= NULL
;
70 definst
->color_hash
= g_hash_table_new_full(g_int_hash
, g_int_equal
,
73 switch (definst
->visual
->class) {
75 RrTrueColorSetup(definst
);
81 RrPseudoColorSetup(definst
);
84 g_critical("Unsupported visual class");
86 return definst
= NULL
;
91 void RrTrueColorSetup (RrInstance
*inst
)
93 gulong red_mask
, green_mask
, blue_mask
;
94 XImage
*timage
= NULL
;
96 timage
= XCreateImage(inst
->display
, inst
->visual
, inst
->depth
,
97 ZPixmap
, 0, NULL
, 1, 1, 32, 0);
98 g_assert(timage
!= NULL
);
99 /* find the offsets for each color in the visual's masks */
100 inst
->red_mask
= red_mask
= timage
->red_mask
;
101 inst
->green_mask
= green_mask
= timage
->green_mask
;
102 inst
->blue_mask
= blue_mask
= timage
->blue_mask
;
104 inst
->red_offset
= 0;
105 inst
->green_offset
= 0;
106 inst
->blue_offset
= 0;
108 while (! (red_mask
& 1)) { inst
->red_offset
++; red_mask
>>= 1; }
109 while (! (green_mask
& 1)) { inst
->green_offset
++; green_mask
>>= 1; }
110 while (! (blue_mask
& 1)) { inst
->blue_offset
++; blue_mask
>>= 1; }
112 inst
->red_shift
= inst
->green_shift
= inst
->blue_shift
= 8;
113 while (red_mask
) { red_mask
>>= 1; inst
->red_shift
--; }
114 while (green_mask
) { green_mask
>>= 1; inst
->green_shift
--; }
115 while (blue_mask
) { blue_mask
>>= 1; inst
->blue_shift
--; }
119 #define RrPseudoNcolors(inst) (1 << (inst->pseudo_bpc * 3))
121 void RrPseudoColorSetup (RrInstance
*inst
)
124 gint tr
, tg
, tb
, n
, r
, g
, b
, i
, incolors
, ii
;
128 /* determine the number of colors and the bits-per-color */
129 inst
->pseudo_bpc
= 2; /* XXX THIS SHOULD BE A USER OPTION */
130 g_assert(inst
->pseudo_bpc
>= 1);
131 _ncolors
= RrPseudoNcolors(inst
);
133 if (_ncolors
> 1 << inst
->depth
) {
134 g_warning("PseudoRenderControl: Invalid colormap size. Resizing.\n");
135 inst
->pseudo_bpc
= 1 << (inst
->depth
/3) >> 3;
136 _ncolors
= 1 << (inst
->pseudo_bpc
* 3);
139 /* build a color cube */
140 inst
->pseudo_colors
= g_new(XColor
, _ncolors
);
141 cpc
= 1 << inst
->pseudo_bpc
; /* colors per channel */
143 for (n
= 0, r
= 0; r
< cpc
; r
++)
144 for (g
= 0; g
< cpc
; g
++)
145 for (b
= 0; b
< cpc
; b
++, n
++) {
146 tr
= (gint
)(((gfloat
)(r
)/(gfloat
)(cpc
-1)) * 0xFF);
147 tg
= (gint
)(((gfloat
)(g
)/(gfloat
)(cpc
-1)) * 0xFF);
148 tb
= (gint
)(((gfloat
)(b
)/(gfloat
)(cpc
-1)) * 0xFF);
149 inst
->pseudo_colors
[n
].red
= tr
| tr
<< 8;
150 inst
->pseudo_colors
[n
].green
= tg
| tg
<< 8;
151 inst
->pseudo_colors
[n
].blue
= tb
| tb
<< 8;
152 /* used to track allocation */
153 inst
->pseudo_colors
[n
].flags
= DoRed
|DoGreen
|DoBlue
;
156 /* allocate the colors */
157 for (i
= 0; i
< _ncolors
; i
++)
158 if (!XAllocColor(inst
->display
, inst
->colormap
,
159 &inst
->pseudo_colors
[i
]))
160 inst
->pseudo_colors
[i
].flags
= 0; /* mark it as unallocated */
162 /* try allocate any colors that failed allocation above */
164 /* get the allocated values from the X server
165 (only the first 256 XXX why!?)
167 incolors
= (((1 << inst
->depth
) > 256) ? 256 : (1 << inst
->depth
));
168 for (i
= 0; i
< incolors
; i
++)
169 icolors
[i
].pixel
= i
;
170 XQueryColors(inst
->display
, inst
->colormap
, icolors
, incolors
);
172 /* try match unallocated ones */
173 for (i
= 0; i
< _ncolors
; i
++) {
174 if (!inst
->pseudo_colors
[i
].flags
) { /* if it wasn't allocated... */
175 gulong closest
= 0xffffffff, close
= 0;
176 for (ii
= 0; ii
< incolors
; ii
++) {
177 /* find deviations */
178 r
= (inst
->pseudo_colors
[i
].red
- icolors
[ii
].red
) & 0xff;
179 g
= (inst
->pseudo_colors
[i
].green
- icolors
[ii
].green
) & 0xff;
180 b
= (inst
->pseudo_colors
[i
].blue
- icolors
[ii
].blue
) & 0xff;
181 /* find a weighted absolute deviation */
182 dev
= (r
* r
) + (g
* g
) + (b
* b
);
190 inst
->pseudo_colors
[i
].red
= icolors
[close
].red
;
191 inst
->pseudo_colors
[i
].green
= icolors
[close
].green
;
192 inst
->pseudo_colors
[i
].blue
= icolors
[close
].blue
;
193 inst
->pseudo_colors
[i
].pixel
= icolors
[close
].pixel
;
195 /* try alloc this closest color, it had better succeed! */
196 if (XAllocColor(inst
->display
, inst
->colormap
,
197 &inst
->pseudo_colors
[i
]))
198 /* mark as alloced */
199 inst
->pseudo_colors
[i
].flags
= DoRed
|DoGreen
|DoBlue
;
201 /* wtf has gone wrong, its already alloced for chissake! */
202 g_assert_not_reached();
207 void RrInstanceFree (RrInstance
*inst
)
210 if (inst
== definst
) definst
= NULL
;
211 g_free(inst
->pseudo_colors
);
212 g_hash_table_destroy(inst
->color_hash
);
216 Display
* RrDisplay (const RrInstance
*inst
)
218 return (inst
? inst
: definst
)->display
;
221 gint
RrScreen (const RrInstance
*inst
)
223 return (inst
? inst
: definst
)->screen
;
226 Window
RrRootWindow (const RrInstance
*inst
)
228 return RootWindow (RrDisplay (inst
), RrScreen (inst
));
231 Visual
*RrVisual (const RrInstance
*inst
)
233 return (inst
? inst
: definst
)->visual
;
236 gint
RrDepth (const RrInstance
*inst
)
238 return (inst
? inst
: definst
)->depth
;
241 Colormap
RrColormap (const RrInstance
*inst
)
243 return (inst
? inst
: definst
)->colormap
;
246 gint
RrRedOffset (const RrInstance
*inst
)
248 return (inst
? inst
: definst
)->red_offset
;
251 gint
RrGreenOffset (const RrInstance
*inst
)
253 return (inst
? inst
: definst
)->green_offset
;
256 gint
RrBlueOffset (const RrInstance
*inst
)
258 return (inst
? inst
: definst
)->blue_offset
;
261 gint
RrRedShift (const RrInstance
*inst
)
263 return (inst
? inst
: definst
)->red_shift
;
266 gint
RrGreenShift (const RrInstance
*inst
)
268 return (inst
? inst
: definst
)->green_shift
;
271 gint
RrBlueShift (const RrInstance
*inst
)
273 return (inst
? inst
: definst
)->blue_shift
;
276 gint
RrRedMask (const RrInstance
*inst
)
278 return (inst
? inst
: definst
)->red_mask
;
281 gint
RrGreenMask (const RrInstance
*inst
)
283 return (inst
? inst
: definst
)->green_mask
;
286 gint
RrBlueMask (const RrInstance
*inst
)
288 return (inst
? inst
: definst
)->blue_mask
;
291 guint
RrPseudoBPC (const RrInstance
*inst
)
293 return (inst
? inst
: definst
)->pseudo_bpc
;
296 XColor
*RrPseudoColors (const RrInstance
*inst
)
298 return (inst
? inst
: definst
)->pseudo_colors
;
301 GHashTable
* RrColorHash (const RrInstance
*inst
)
303 return (inst
? inst
: definst
)->color_hash
;
This page took 0.045995 seconds and 4 git commands to generate.