]>
Dogcows Code - chaz/openbox/blob - render/instance.c
195792498a3ccb9725437cb205fed073bd409512
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("color %d (%d,%d,%d) in hash table with %d "
37 "leftover references",
38 c
->id
, RrColorRed(c
), RrColorGreen(c
), RrColorBlue(c
),
44 static void f(gpointer key
, gpointer value
, gpointer n
)
47 if (c
->id
== *(gint
*)n
)
48 g_message("color %d has %d references", c
->id
, c
->refcount
);
51 void print_refs(gint id
)
53 g_hash_table_foreach(RrColorHash(definst
), f
, &id
);
57 RrInstance
* RrInstanceNew (Display
*display
, gint screen
)
59 definst
= g_new (RrInstance
, 1);
60 definst
->display
= display
;
61 definst
->screen
= screen
;
63 definst
->depth
= DefaultDepth(display
, screen
);
64 definst
->visual
= DefaultVisual(display
, screen
);
65 definst
->colormap
= DefaultColormap(display
, screen
);
67 definst
->pseudo_colors
= NULL
;
69 definst
->color_hash
= g_hash_table_new_full(g_int_hash
, g_int_equal
,
72 switch (definst
->visual
->class) {
74 RrTrueColorSetup(definst
);
80 RrPseudoColorSetup(definst
);
83 g_critical("Unsupported visual class");
85 return definst
= NULL
;
90 void RrTrueColorSetup (RrInstance
*inst
)
92 gulong red_mask
, green_mask
, blue_mask
;
93 XImage
*timage
= NULL
;
95 timage
= XCreateImage(inst
->display
, inst
->visual
, inst
->depth
,
96 ZPixmap
, 0, NULL
, 1, 1, 32, 0);
97 g_assert(timage
!= NULL
);
98 /* find the offsets for each color in the visual's masks */
99 inst
->red_mask
= red_mask
= timage
->red_mask
;
100 inst
->green_mask
= green_mask
= timage
->green_mask
;
101 inst
->blue_mask
= blue_mask
= timage
->blue_mask
;
103 inst
->red_offset
= 0;
104 inst
->green_offset
= 0;
105 inst
->blue_offset
= 0;
107 while (! (red_mask
& 1)) { inst
->red_offset
++; red_mask
>>= 1; }
108 while (! (green_mask
& 1)) { inst
->green_offset
++; green_mask
>>= 1; }
109 while (! (blue_mask
& 1)) { inst
->blue_offset
++; blue_mask
>>= 1; }
111 inst
->red_shift
= inst
->green_shift
= inst
->blue_shift
= 8;
112 while (red_mask
) { red_mask
>>= 1; inst
->red_shift
--; }
113 while (green_mask
) { green_mask
>>= 1; inst
->green_shift
--; }
114 while (blue_mask
) { blue_mask
>>= 1; inst
->blue_shift
--; }
118 #define RrPseudoNcolors(inst) (1 << (inst->pseudo_bpc * 3))
120 void RrPseudoColorSetup (RrInstance
*inst
)
123 gint tr
, tg
, tb
, n
, r
, g
, b
, i
, incolors
, ii
;
127 /* determine the number of colors and the bits-per-color */
128 inst
->pseudo_bpc
= 2; /* XXX THIS SHOULD BE A USER OPTION */
129 g_assert(inst
->pseudo_bpc
>= 1);
130 _ncolors
= RrPseudoNcolors(inst
);
132 if (_ncolors
> 1 << inst
->depth
) {
133 g_warning("PseudoRenderControl: Invalid colormap size. Resizing.\n");
134 inst
->pseudo_bpc
= 1 << (inst
->depth
/3) >> 3;
135 _ncolors
= 1 << (inst
->pseudo_bpc
* 3);
138 /* build a color cube */
139 inst
->pseudo_colors
= g_new(XColor
, _ncolors
);
140 cpc
= 1 << inst
->pseudo_bpc
; /* colors per channel */
142 for (n
= 0, r
= 0; r
< cpc
; r
++)
143 for (g
= 0; g
< cpc
; g
++)
144 for (b
= 0; b
< cpc
; b
++, n
++) {
145 tr
= (gint
)(((gfloat
)(r
)/(gfloat
)(cpc
-1)) * 0xFF);
146 tg
= (gint
)(((gfloat
)(g
)/(gfloat
)(cpc
-1)) * 0xFF);
147 tb
= (gint
)(((gfloat
)(b
)/(gfloat
)(cpc
-1)) * 0xFF);
148 inst
->pseudo_colors
[n
].red
= tr
| tr
<< 8;
149 inst
->pseudo_colors
[n
].green
= tg
| tg
<< 8;
150 inst
->pseudo_colors
[n
].blue
= tb
| tb
<< 8;
151 /* used to track allocation */
152 inst
->pseudo_colors
[n
].flags
= DoRed
|DoGreen
|DoBlue
;
155 /* allocate the colors */
156 for (i
= 0; i
< _ncolors
; i
++)
157 if (!XAllocColor(inst
->display
, inst
->colormap
,
158 &inst
->pseudo_colors
[i
]))
159 inst
->pseudo_colors
[i
].flags
= 0; /* mark it as unallocated */
161 /* try allocate any colors that failed allocation above */
163 /* get the allocated values from the X server
164 (only the first 256 XXX why!?)
166 incolors
= (((1 << inst
->depth
) > 256) ? 256 : (1 << inst
->depth
));
167 for (i
= 0; i
< incolors
; i
++)
168 icolors
[i
].pixel
= i
;
169 XQueryColors(inst
->display
, inst
->colormap
, icolors
, incolors
);
171 /* try match unallocated ones */
172 for (i
= 0; i
< _ncolors
; i
++) {
173 if (!inst
->pseudo_colors
[i
].flags
) { /* if it wasn't allocated... */
174 gulong closest
= 0xffffffff, close
= 0;
175 for (ii
= 0; ii
< incolors
; ii
++) {
176 /* find deviations */
177 r
= (inst
->pseudo_colors
[i
].red
- icolors
[ii
].red
) & 0xff;
178 g
= (inst
->pseudo_colors
[i
].green
- icolors
[ii
].green
) & 0xff;
179 b
= (inst
->pseudo_colors
[i
].blue
- icolors
[ii
].blue
) & 0xff;
180 /* find a weighted absolute deviation */
181 dev
= (r
* r
) + (g
* g
) + (b
* b
);
189 inst
->pseudo_colors
[i
].red
= icolors
[close
].red
;
190 inst
->pseudo_colors
[i
].green
= icolors
[close
].green
;
191 inst
->pseudo_colors
[i
].blue
= icolors
[close
].blue
;
192 inst
->pseudo_colors
[i
].pixel
= icolors
[close
].pixel
;
194 /* try alloc this closest color, it had better succeed! */
195 if (XAllocColor(inst
->display
, inst
->colormap
,
196 &inst
->pseudo_colors
[i
]))
197 /* mark as alloced */
198 inst
->pseudo_colors
[i
].flags
= DoRed
|DoGreen
|DoBlue
;
200 /* wtf has gone wrong, its already alloced for chissake! */
201 g_assert_not_reached();
206 void RrInstanceFree (RrInstance
*inst
)
209 if (inst
== definst
) definst
= NULL
;
210 g_free(inst
->pseudo_colors
);
211 g_hash_table_destroy(inst
->color_hash
);
215 Display
* RrDisplay (const RrInstance
*inst
)
217 return (inst
? inst
: definst
)->display
;
220 gint
RrScreen (const RrInstance
*inst
)
222 return (inst
? inst
: definst
)->screen
;
225 Window
RrRootWindow (const RrInstance
*inst
)
227 return RootWindow (RrDisplay (inst
), RrScreen (inst
));
230 Visual
*RrVisual (const RrInstance
*inst
)
232 return (inst
? inst
: definst
)->visual
;
235 gint
RrDepth (const RrInstance
*inst
)
237 return (inst
? inst
: definst
)->depth
;
240 Colormap
RrColormap (const RrInstance
*inst
)
242 return (inst
? inst
: definst
)->colormap
;
245 gint
RrRedOffset (const RrInstance
*inst
)
247 return (inst
? inst
: definst
)->red_offset
;
250 gint
RrGreenOffset (const RrInstance
*inst
)
252 return (inst
? inst
: definst
)->green_offset
;
255 gint
RrBlueOffset (const RrInstance
*inst
)
257 return (inst
? inst
: definst
)->blue_offset
;
260 gint
RrRedShift (const RrInstance
*inst
)
262 return (inst
? inst
: definst
)->red_shift
;
265 gint
RrGreenShift (const RrInstance
*inst
)
267 return (inst
? inst
: definst
)->green_shift
;
270 gint
RrBlueShift (const RrInstance
*inst
)
272 return (inst
? inst
: definst
)->blue_shift
;
275 gint
RrRedMask (const RrInstance
*inst
)
277 return (inst
? inst
: definst
)->red_mask
;
280 gint
RrGreenMask (const RrInstance
*inst
)
282 return (inst
? inst
: definst
)->green_mask
;
285 gint
RrBlueMask (const RrInstance
*inst
)
287 return (inst
? inst
: definst
)->blue_mask
;
290 guint
RrPseudoBPC (const RrInstance
*inst
)
292 return (inst
? inst
: definst
)->pseudo_bpc
;
295 XColor
*RrPseudoColors (const RrInstance
*inst
)
297 return (inst
? inst
: definst
)->pseudo_colors
;
300 GHashTable
* RrColorHash (const RrInstance
*inst
)
302 return (inst
? inst
: definst
)->color_hash
;
This page took 0.054342 seconds and 4 git commands to generate.