]>
Dogcows Code - chaz/tint2/blob - src/util/common.c
224c4ea6a3d7357738f0de9929679f3cea77dc89
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) from Omega distribution
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>
25 #include <sys/types.h>
34 #include "../server.h"
38 void copy_file(const char *pathSrc
, const char *pathDest
)
40 FILE *fileSrc
, *fileDest
;
44 fileSrc
= fopen(pathSrc
, "rb");
45 if (fileSrc
== NULL
) return;
47 fileDest
= fopen(pathDest
, "wb");
48 if (fileDest
== NULL
) return;
50 while ((nb
= fread(line
, 1, 100, fileSrc
)) > 0)
51 if ( nb
!= fwrite(line
, 1, nb
, fileDest
))
52 printf("Error while copying file %s to %s\n", pathSrc
, pathDest
);
59 int parse_line (const char *line
, char **key
, char **value
)
63 /* Skip useless lines */
64 if ((line
[0] == '#') || (line
[0] == '\n')) return 0;
65 if (!(a
= strchr (line
, '='))) return 0;
67 /* overwrite '=' with '\0' */
72 /* overwrite '\n' with '\0' if '\n' present */
73 if ((b
= strchr (a
, '\n'))) b
[0] = '\0';
83 void tint_exec(const char *command
)
89 // change for the fork the signal mask
91 // sigprocmask(SIG_SETMASK, &sigset, 0);
92 // sigprocmask(SIG_UNBLOCK, &sigset, 0);
93 execl("/bin/sh", "/bin/sh", "-c", command
, NULL
);
100 int hex_char_to_int (char c
)
104 if (c
>= '0' && c
<= '9') r
= c
- '0';
105 else if (c
>= 'a' && c
<= 'f') r
= c
- 'a' + 10;
106 else if (c
>= 'A' && c
<= 'F') r
= c
- 'A' + 10;
113 int hex_to_rgb (char *hex
, int *r
, int *g
, int *b
)
117 if (hex
== NULL
|| hex
[0] != '#') return (0);
121 *r
= hex_char_to_int (hex
[1]);
122 *g
= hex_char_to_int (hex
[2]);
123 *b
= hex_char_to_int (hex
[3]);
125 else if (len
== 6 + 1) {
126 *r
= hex_char_to_int (hex
[1]) * 16 + hex_char_to_int (hex
[2]);
127 *g
= hex_char_to_int (hex
[3]) * 16 + hex_char_to_int (hex
[4]);
128 *b
= hex_char_to_int (hex
[5]) * 16 + hex_char_to_int (hex
[6]);
130 else if (len
== 12 + 1) {
131 *r
= hex_char_to_int (hex
[1]) * 16 + hex_char_to_int (hex
[2]);
132 *g
= hex_char_to_int (hex
[5]) * 16 + hex_char_to_int (hex
[6]);
133 *b
= hex_char_to_int (hex
[9]) * 16 + hex_char_to_int (hex
[10]);
141 void get_color (char *hex
, double *rgb
)
144 hex_to_rgb (hex
, &r
, &g
, &b
);
146 rgb
[0] = (r
/ 255.0);
147 rgb
[1] = (g
/ 255.0);
148 rgb
[2] = (b
/ 255.0);
152 void extract_values (const char *value
, char **value1
, char **value2
, char **value3
)
156 if (*value1
) free (*value1
);
157 if (*value2
) free (*value2
);
158 if (*value3
) free (*value3
);
160 if ((b
= strchr (value
, ' '))) {
168 *value1
= strdup (value
);
172 if ((c
= strchr (b
, ' '))) {
180 *value2
= strdup (b
);
185 *value3
= strdup (c
);
191 void adjust_asb(DATA32
*data
, int w
, int h
, int alpha
, float satur
, float bright
)
194 unsigned int a
, r
, g
, b
, argb
;
197 float h2
, f
, p
, q
, t
;
198 float hue
, saturation
, brightness
;
199 float redc
, greenc
, bluec
;
201 for(y
= 0; y
< h
; y
++) {
202 for(id
= y
* w
, x
= 0; x
< w
; x
++, id
++) {
204 a
= (argb
>> 24) & 0xff;
205 // transparent => nothing to do.
206 if (a
== 0) continue;
207 r
= (argb
>> 16) & 0xff;
208 g
= (argb
>> 8) & 0xff;
211 // convert RGB to HSB
212 cmax
= (r
> g
) ? r
: g
;
213 if (b
> cmax
) cmax
= b
;
214 cmin
= (r
< g
) ? r
: g
;
215 if (b
< cmin
) cmin
= b
;
216 brightness
= ((float)cmax
) / 255.0f
;
218 saturation
= ((float)(cmax
- cmin
)) / ((float)cmax
);
224 redc
= ((float)(cmax
- r
)) / ((float)(cmax
- cmin
));
225 greenc
= ((float)(cmax
- g
)) / ((float)(cmax
- cmin
));
226 bluec
= ((float)(cmax
- b
)) / ((float)(cmax
- cmin
));
228 hue
= bluec
- greenc
;
230 hue
= 2.0f
+ redc
- bluec
;
232 hue
= 4.0f
+ greenc
- redc
;
240 if (saturation
< 0.0) saturation
= 0.0;
241 if (saturation
> 1.0) saturation
= 1.0;
242 brightness
+= bright
;
243 if (brightness
< 0.0) brightness
= 0.0;
244 if (brightness
> 1.0) brightness
= 1.0;
248 // convert HSB to RGB
249 if (saturation
== 0) {
250 r
= g
= b
= (int)(brightness
* 255.0f
+ 0.5f
);
252 h2
= (hue
- (int)hue
) * 6.0f
;
254 p
= brightness
* (1.0f
- saturation
);
255 q
= brightness
* (1.0f
- saturation
* f
);
256 t
= brightness
* (1.0f
- (saturation
* (1.0f
- f
)));
259 r
= (int)(brightness
* 255.0f
+ 0.5f
);
260 g
= (int)(t
* 255.0f
+ 0.5f
);
261 b
= (int)(p
* 255.0f
+ 0.5f
);
264 r
= (int)(q
* 255.0f
+ 0.5f
);
265 g
= (int)(brightness
* 255.0f
+ 0.5f
);
266 b
= (int)(p
* 255.0f
+ 0.5f
);
269 r
= (int)(p
* 255.0f
+ 0.5f
);
270 g
= (int)(brightness
* 255.0f
+ 0.5f
);
271 b
= (int)(t
* 255.0f
+ 0.5f
);
274 r
= (int)(p
* 255.0f
+ 0.5f
);
275 g
= (int)(q
* 255.0f
+ 0.5f
);
276 b
= (int)(brightness
* 255.0f
+ 0.5f
);
279 r
= (int)(t
* 255.0f
+ 0.5f
);
280 g
= (int)(p
* 255.0f
+ 0.5f
);
281 b
= (int)(brightness
* 255.0f
+ 0.5f
);
284 r
= (int)(brightness
* 255.0f
+ 0.5f
);
285 g
= (int)(p
* 255.0f
+ 0.5f
);
286 b
= (int)(q
* 255.0f
+ 0.5f
);
292 argb
= (argb
<< 8) + r
;
293 argb
= (argb
<< 8) + g
;
294 argb
= (argb
<< 8) + b
;
301 void createHeuristicMask(DATA32
* data
, int w
, int h
)
303 // first we need to find the mask color, therefore we check all 4 edge pixel and take the color which
304 // appears most often (we only need to check three edges, the 4th is implicitly clear)
305 unsigned int topLeft
= data
[0], topRight
= data
[w
-1], bottomLeft
= data
[w
*h
-w
], bottomRight
= data
[w
*h
-1];
306 int max
= (topLeft
== topRight
) + (topLeft
== bottomLeft
) + (topLeft
== bottomRight
);
308 if ( max
< (topRight
== topLeft
) + (topRight
== bottomLeft
) + (topRight
== bottomRight
) ) {
309 max
= (topRight
== topLeft
) + (topRight
== bottomLeft
) + (topRight
== bottomRight
);
312 if ( max
< (bottomLeft
== topRight
) + (bottomLeft
== topLeft
) + (bottomLeft
== bottomRight
) )
315 // now mask out every pixel which has the same color as the edge pixels
316 unsigned char* udata
= (unsigned char*)data
;
317 unsigned char b
= udata
[4*maskPos
];
318 unsigned char g
= udata
[4*maskPos
+1];
319 unsigned char r
= udata
[4*maskPos
+1];
321 for (i
=0; i
<h
*w
; ++i
) {
322 if ( b
-udata
[0] == 0 && g
-udata
[1] == 0 && r
-udata
[2] == 0 )
329 void render_image(Drawable d
, int x
, int y
, int w
, int h
)
331 // in real_transparency mode imlib_render_image_on_drawable does not the right thing, because
332 // the operation is IMLIB_OP_COPY, but we would need IMLIB_OP_OVER (which does not exist)
333 // Therefore we have to do it with the XRender extension (i.e. copy what imlib is doing internally)
334 // But first we need to render the image onto itself with PictOpIn to adjust the colors to the alpha channel
335 Pixmap pmap_tmp
= XCreatePixmap(server
.dsp
, server
.root_win
, w
, h
, 32);
336 imlib_context_set_drawable(pmap_tmp
);
337 imlib_context_set_blend(0);
338 imlib_render_image_on_drawable(0, 0);
339 Picture pict_image
= XRenderCreatePicture(server
.dsp
, pmap_tmp
, XRenderFindStandardFormat(server
.dsp
, PictStandardARGB32
), 0, 0);
340 Picture pict_drawable
= XRenderCreatePicture(server
.dsp
, d
, XRenderFindVisualFormat(server
.dsp
, server
.visual
), 0, 0);
341 XRenderComposite(server
.dsp
, PictOpIn
, pict_image
, None
, pict_image
, 0, 0, 0, 0, 0, 0, w
, h
);
342 XRenderComposite(server
.dsp
, PictOpOver
, pict_image
, None
, pict_drawable
, 0, 0, 0, 0, x
, y
, w
, h
);
343 imlib_context_set_blend(1);
344 XFreePixmap(server
.dsp
, pmap_tmp
);
345 XRenderFreePicture(server
.dsp
, pict_image
);
346 XRenderFreePicture(server
.dsp
, pict_drawable
);
351 * Scan given directory for files which are meet the given mask and sort them alphabetically
353 * @param path - directory path
354 * @param mask - filename mask
358 GList
*dir_scan_alpha(const char *path
, const char *mask
)
366 GDir
*dir
= g_dir_open(path
, 0, &err
);
369 fprintf(stderr
, "%s\n", err
->message
);
373 while ((n
= g_dir_read_name(dir
))) {
374 if (!fnmatch(mask
, n
, FNM_PATHNAME
)) {
375 fn
= g_build_filename(path
, n
, NULL
);
377 if (stat((char *)fn
, &st
) < 0)
380 // Only regular files
381 if (S_ISREG(st
.st_mode
))
382 list
= g_list_prepend(list
, (gpointer
)fn
);
386 list
= g_list_sort (list
, (GCompareFunc
) &strcmp
);
This page took 0.049726 seconds and 4 git commands to generate.