]>
Dogcows Code - chaz/tint2/blob - src/util/common.c
1 /**************************************************************************
3 * Tint2 : common windows function
5 * Copyright (C) 2007 Pål Staurland (staura@gmail.com)
6 * Modified (C) 2008 thierry lorthiois (lorthiois@bbsoft.fr)
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License version 2
10 * as published by the Free Software Foundation.
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.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 **************************************************************************/
22 #include <X11/Xutil.h>
23 #include <X11/Xatom.h>
24 #include <X11/extensions/Xrender.h>
31 #include "../server.h"
35 void copy_file(const char *pathSrc
, const char *pathDest
)
37 FILE *fileSrc
, *fileDest
;
41 fileSrc
= fopen(pathSrc
, "rb");
42 if (fileSrc
== NULL
) return;
44 fileDest
= fopen(pathDest
, "wb");
45 if (fileDest
== NULL
) return;
47 while ((nb
= fread(line
, 1, 100, fileSrc
)) > 0)
48 if ( nb
!= fwrite(line
, 1, nb
, fileDest
))
49 printf("Error while copying file %s to %s\n", pathSrc
, pathDest
);
56 int parse_line (const char *line
, char **key
, char **value
)
60 /* Skip useless lines */
61 if ((line
[0] == '#') || (line
[0] == '\n')) return 0;
62 if (!(a
= strchr (line
, '='))) return 0;
64 /* overwrite '=' with '\0' */
69 /* overwrite '\n' with '\0' if '\n' present */
70 if ((b
= strchr (a
, '\n'))) b
[0] = '\0';
80 void tint_exec(const char *command
)
86 // change for the fork the signal mask
88 // sigprocmask(SIG_SETMASK, &sigset, 0);
89 // sigprocmask(SIG_UNBLOCK, &sigset, 0);
90 execl("/bin/sh", "/bin/sh", "-c", command
, NULL
);
97 int hex_char_to_int (char c
)
101 if (c
>= '0' && c
<= '9') r
= c
- '0';
102 else if (c
>= 'a' && c
<= 'f') r
= c
- 'a' + 10;
103 else if (c
>= 'A' && c
<= 'F') r
= c
- 'A' + 10;
110 int hex_to_rgb (char *hex
, int *r
, int *g
, int *b
)
114 if (hex
== NULL
|| hex
[0] != '#') return (0);
118 *r
= hex_char_to_int (hex
[1]);
119 *g
= hex_char_to_int (hex
[2]);
120 *b
= hex_char_to_int (hex
[3]);
122 else if (len
== 6 + 1) {
123 *r
= hex_char_to_int (hex
[1]) * 16 + hex_char_to_int (hex
[2]);
124 *g
= hex_char_to_int (hex
[3]) * 16 + hex_char_to_int (hex
[4]);
125 *b
= hex_char_to_int (hex
[5]) * 16 + hex_char_to_int (hex
[6]);
127 else if (len
== 12 + 1) {
128 *r
= hex_char_to_int (hex
[1]) * 16 + hex_char_to_int (hex
[2]);
129 *g
= hex_char_to_int (hex
[5]) * 16 + hex_char_to_int (hex
[6]);
130 *b
= hex_char_to_int (hex
[9]) * 16 + hex_char_to_int (hex
[10]);
138 void get_color (char *hex
, double *rgb
)
141 hex_to_rgb (hex
, &r
, &g
, &b
);
143 rgb
[0] = (r
/ 255.0);
144 rgb
[1] = (g
/ 255.0);
145 rgb
[2] = (b
/ 255.0);
149 void adjust_asb(DATA32
*data
, int w
, int h
, int alpha
, float satur
, float bright
)
152 unsigned int a
, r
, g
, b
, argb
;
155 float h2
, f
, p
, q
, t
;
156 float hue
, saturation
, brightness
;
157 float redc
, greenc
, bluec
;
159 for(y
= 0; y
< h
; y
++) {
160 for(id
= y
* w
, x
= 0; x
< w
; x
++, id
++) {
162 a
= (argb
>> 24) & 0xff;
163 // transparent => nothing to do.
164 if (a
== 0) continue;
165 r
= (argb
>> 16) & 0xff;
166 g
= (argb
>> 8) & 0xff;
169 // convert RGB to HSB
170 cmax
= (r
> g
) ? r
: g
;
171 if (b
> cmax
) cmax
= b
;
172 cmin
= (r
< g
) ? r
: g
;
173 if (b
< cmin
) cmin
= b
;
174 brightness
= ((float)cmax
) / 255.0f
;
176 saturation
= ((float)(cmax
- cmin
)) / ((float)cmax
);
182 redc
= ((float)(cmax
- r
)) / ((float)(cmax
- cmin
));
183 greenc
= ((float)(cmax
- g
)) / ((float)(cmax
- cmin
));
184 bluec
= ((float)(cmax
- b
)) / ((float)(cmax
- cmin
));
186 hue
= bluec
- greenc
;
188 hue
= 2.0f
+ redc
- bluec
;
190 hue
= 4.0f
+ greenc
- redc
;
198 if (saturation
< 0.0) saturation
= 0.0;
199 if (saturation
> 1.0) saturation
= 1.0;
200 brightness
+= bright
;
201 if (brightness
< 0.0) brightness
= 0.0;
202 if (brightness
> 1.0) brightness
= 1.0;
206 // convert HSB to RGB
207 if (saturation
== 0) {
208 r
= g
= b
= (int)(brightness
* 255.0f
+ 0.5f
);
210 h2
= (hue
- (int)hue
) * 6.0f
;
212 p
= brightness
* (1.0f
- saturation
);
213 q
= brightness
* (1.0f
- saturation
* f
);
214 t
= brightness
* (1.0f
- (saturation
* (1.0f
- f
)));
217 r
= (int)(brightness
* 255.0f
+ 0.5f
);
218 g
= (int)(t
* 255.0f
+ 0.5f
);
219 b
= (int)(p
* 255.0f
+ 0.5f
);
222 r
= (int)(q
* 255.0f
+ 0.5f
);
223 g
= (int)(brightness
* 255.0f
+ 0.5f
);
224 b
= (int)(p
* 255.0f
+ 0.5f
);
227 r
= (int)(p
* 255.0f
+ 0.5f
);
228 g
= (int)(brightness
* 255.0f
+ 0.5f
);
229 b
= (int)(t
* 255.0f
+ 0.5f
);
232 r
= (int)(p
* 255.0f
+ 0.5f
);
233 g
= (int)(q
* 255.0f
+ 0.5f
);
234 b
= (int)(brightness
* 255.0f
+ 0.5f
);
237 r
= (int)(t
* 255.0f
+ 0.5f
);
238 g
= (int)(p
* 255.0f
+ 0.5f
);
239 b
= (int)(brightness
* 255.0f
+ 0.5f
);
242 r
= (int)(brightness
* 255.0f
+ 0.5f
);
243 g
= (int)(p
* 255.0f
+ 0.5f
);
244 b
= (int)(q
* 255.0f
+ 0.5f
);
250 argb
= (argb
<< 8) + r
;
251 argb
= (argb
<< 8) + g
;
252 argb
= (argb
<< 8) + b
;
259 void createHeuristicMask(DATA32
* data
, int w
, int h
)
261 // first we need to find the mask color, therefore we check all 4 edge pixel and take the color which
262 // appears most often (we only need to check three edges, the 4th is implicitly clear)
263 unsigned int topLeft
= data
[0], topRight
= data
[w
-1], bottomLeft
= data
[w
*h
-w
], bottomRight
= data
[w
*h
-1];
264 int max
= (topLeft
== topRight
) + (topLeft
== bottomLeft
) + (topLeft
== bottomRight
);
266 if ( max
< (topRight
== topLeft
) + (topRight
== bottomLeft
) + (topRight
== bottomRight
) ) {
267 max
= (topRight
== topLeft
) + (topRight
== bottomLeft
) + (topRight
== bottomRight
);
270 if ( max
< (bottomLeft
== topRight
) + (bottomLeft
== topLeft
) + (bottomLeft
== bottomRight
) )
273 // now mask out every pixel which has the same color as the edge pixels
274 unsigned char* udata
= (unsigned char*)data
;
275 unsigned char b
= udata
[4*maskPos
];
276 unsigned char g
= udata
[4*maskPos
+1];
277 unsigned char r
= udata
[4*maskPos
+1];
279 for (i
=0; i
<h
*w
; ++i
) {
280 if ( b
-udata
[0] == 0 && g
-udata
[1] == 0 && r
-udata
[2] == 0 )
287 void render_image(Drawable d
, int x
, int y
, int w
, int h
)
289 // in real_transparency mode imlib_render_image_on_drawable does not the right thing, because
290 // the operation is IMLIB_OP_COPY, but we would need IMLIB_OP_OVER (which does not exist)
291 // Therefore we have to do it with the XRender extension (i.e. copy what imlib is doing internally)
292 // But first we need to render the image onto itself with PictOpIn to adjust the colors to the alpha channel
293 Pixmap pmap_tmp
= XCreatePixmap(server
.dsp
, server
.root_win
, w
, h
, 32);
294 imlib_context_set_drawable(pmap_tmp
);
295 imlib_context_set_blend(0);
296 imlib_render_image_on_drawable(0, 0);
297 Picture pict_image
= XRenderCreatePicture(server
.dsp
, pmap_tmp
, XRenderFindStandardFormat(server
.dsp
, PictStandardARGB32
), 0, 0);
298 Picture pict_drawable
= XRenderCreatePicture(server
.dsp
, d
, XRenderFindVisualFormat(server
.dsp
, server
.visual
), 0, 0);
299 XRenderComposite(server
.dsp
, PictOpIn
, pict_image
, None
, pict_image
, 0, 0, 0, 0, 0, 0, w
, h
);
300 XRenderComposite(server
.dsp
, PictOpOver
, pict_image
, None
, pict_drawable
, 0, 0, 0, 0, x
, y
, w
, h
);
301 imlib_context_set_blend(1);
302 XFreePixmap(server
.dsp
, pmap_tmp
);
303 XRenderFreePicture(server
.dsp
, pict_image
);
304 XRenderFreePicture(server
.dsp
, pict_drawable
);
This page took 0.050848 seconds and 4 git commands to generate.