]> Dogcows Code - chaz/tint2/blob - src/util/common.c
b9f2b727e3adf0262a5b75f37865a86519905506
[chaz/tint2] / src / util / common.c
1 /**************************************************************************
2 *
3 * Tint2 : common windows function
4 *
5 * Copyright (C) 2007 Pål Staurland (staura@gmail.com)
6 * Modified (C) 2008 thierry lorthiois (lorthiois@bbsoft.fr) from Omega distribution
7 *
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.
11 *
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 **************************************************************************/
20
21 #include <X11/Xlib.h>
22 #include <X11/Xutil.h>
23 #include <X11/Xatom.h>
24 #include <X11/extensions/Xrender.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <unistd.h>
29
30 #include "common.h"
31 #include "../server.h"
32
33
34
35 void copy_file(const char *pathSrc, const char *pathDest)
36 {
37 FILE *fileSrc, *fileDest;
38 char line[100];
39 int nb;
40
41 fileSrc = fopen(pathSrc, "rb");
42 if (fileSrc == NULL) return;
43
44 fileDest = fopen(pathDest, "wb");
45 if (fileDest == NULL) return;
46
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);
50
51 fclose (fileDest);
52 fclose (fileSrc);
53 }
54
55
56 int parse_line (const char *line, char **key, char **value)
57 {
58 char *a, *b;
59
60 /* Skip useless lines */
61 if ((line[0] == '#') || (line[0] == '\n')) return 0;
62 if (!(a = strchr (line, '='))) return 0;
63
64 /* overwrite '=' with '\0' */
65 a[0] = '\0';
66 *key = strdup (line);
67 a++;
68
69 /* overwrite '\n' with '\0' if '\n' present */
70 if ((b = strchr (a, '\n'))) b[0] = '\0';
71
72 *value = strdup (a);
73
74 g_strstrip(*key);
75 g_strstrip(*value);
76 return 1;
77 }
78
79
80 void tint_exec(const char *command)
81 {
82 if (command) {
83 pid_t pid;
84 pid = fork();
85 if (pid == 0) {
86 // change for the fork the signal mask
87 // sigset_t sigset;
88 // sigprocmask(SIG_SETMASK, &sigset, 0);
89 // sigprocmask(SIG_UNBLOCK, &sigset, 0);
90 execl("/bin/sh", "/bin/sh", "-c", command, NULL);
91 _exit(0);
92 }
93 }
94 }
95
96
97 int hex_char_to_int (char c)
98 {
99 int r;
100
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;
104 else r = 0;
105
106 return r;
107 }
108
109
110 int hex_to_rgb (char *hex, int *r, int *g, int *b)
111 {
112 int len;
113
114 if (hex == NULL || hex[0] != '#') return (0);
115
116 len = strlen (hex);
117 if (len == 3 + 1) {
118 *r = hex_char_to_int (hex[1]);
119 *g = hex_char_to_int (hex[2]);
120 *b = hex_char_to_int (hex[3]);
121 }
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]);
126 }
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]);
131 }
132 else return 0;
133
134 return 1;
135 }
136
137
138 void get_color (char *hex, double *rgb)
139 {
140 int r, g, b;
141 hex_to_rgb (hex, &r, &g, &b);
142
143 rgb[0] = (r / 255.0);
144 rgb[1] = (g / 255.0);
145 rgb[2] = (b / 255.0);
146 }
147
148
149 void adjust_asb(DATA32 *data, int w, int h, int alpha, float satur, float bright)
150 {
151 unsigned int x, y;
152 unsigned int a, r, g, b, argb;
153 unsigned long id;
154 int cmax, cmin;
155 float h2, f, p, q, t;
156 float hue, saturation, brightness;
157 float redc, greenc, bluec;
158
159 for(y = 0; y < h; y++) {
160 for(id = y * w, x = 0; x < w; x++, id++) {
161 argb = data[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;
167 b = (argb) & 0xff;
168
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;
175 if (cmax != 0)
176 saturation = ((float)(cmax - cmin)) / ((float)cmax);
177 else
178 saturation = 0;
179 if (saturation == 0)
180 hue = 0;
181 else {
182 redc = ((float)(cmax - r)) / ((float)(cmax - cmin));
183 greenc = ((float)(cmax - g)) / ((float)(cmax - cmin));
184 bluec = ((float)(cmax - b)) / ((float)(cmax - cmin));
185 if (r == cmax)
186 hue = bluec - greenc;
187 else if (g == cmax)
188 hue = 2.0f + redc - bluec;
189 else
190 hue = 4.0f + greenc - redc;
191 hue = hue / 6.0f;
192 if (hue < 0)
193 hue = hue + 1.0f;
194 }
195
196 // adjust
197 saturation += satur;
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;
203 if (alpha != 100)
204 a = (a * alpha)/100;
205
206 // convert HSB to RGB
207 if (saturation == 0) {
208 r = g = b = (int)(brightness * 255.0f + 0.5f);
209 } else {
210 h2 = (hue - (int)hue) * 6.0f;
211 f = h2 - (int)(h2);
212 p = brightness * (1.0f - saturation);
213 q = brightness * (1.0f - saturation * f);
214 t = brightness * (1.0f - (saturation * (1.0f - f)));
215 switch ((int) h2) {
216 case 0:
217 r = (int)(brightness * 255.0f + 0.5f);
218 g = (int)(t * 255.0f + 0.5f);
219 b = (int)(p * 255.0f + 0.5f);
220 break;
221 case 1:
222 r = (int)(q * 255.0f + 0.5f);
223 g = (int)(brightness * 255.0f + 0.5f);
224 b = (int)(p * 255.0f + 0.5f);
225 break;
226 case 2:
227 r = (int)(p * 255.0f + 0.5f);
228 g = (int)(brightness * 255.0f + 0.5f);
229 b = (int)(t * 255.0f + 0.5f);
230 break;
231 case 3:
232 r = (int)(p * 255.0f + 0.5f);
233 g = (int)(q * 255.0f + 0.5f);
234 b = (int)(brightness * 255.0f + 0.5f);
235 break;
236 case 4:
237 r = (int)(t * 255.0f + 0.5f);
238 g = (int)(p * 255.0f + 0.5f);
239 b = (int)(brightness * 255.0f + 0.5f);
240 break;
241 case 5:
242 r = (int)(brightness * 255.0f + 0.5f);
243 g = (int)(p * 255.0f + 0.5f);
244 b = (int)(q * 255.0f + 0.5f);
245 break;
246 }
247 }
248
249 argb = a;
250 argb = (argb << 8) + r;
251 argb = (argb << 8) + g;
252 argb = (argb << 8) + b;
253 data[id] = argb;
254 }
255 }
256 }
257
258
259 void createHeuristicMask(DATA32* data, int w, int h)
260 {
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);
265 int maskPos = 0;
266 if ( max < (topRight == topLeft) + (topRight == bottomLeft) + (topRight == bottomRight) ) {
267 max = (topRight == topLeft) + (topRight == bottomLeft) + (topRight == bottomRight);
268 maskPos = w-1;
269 }
270 if ( max < (bottomLeft == topRight) + (bottomLeft == topLeft) + (bottomLeft == bottomRight) )
271 maskPos = w*h-w;
272
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];
278 int i;
279 for (i=0; i<h*w; ++i) {
280 if ( b-udata[0] == 0 && g-udata[1] == 0 && r-udata[2] == 0 )
281 udata[3] = 0;
282 udata += 4;
283 }
284 }
285
286
287 void render_image(Drawable d, int x, int y, int w, int h)
288 {
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);
305 }
This page took 0.051683 seconds and 3 git commands to generate.