Change to the linux kernel coding style
[wmaker-crm.git] / plugins / libwmfun / generic.c
Commit [+]AuthorDateLineData
7aadc1ef id2000-12-03 18:58:41 +00001/*
2 * libwmfun - WindowMaker texture function library
3 * Copyright (C) 1999 Tobias Gloth
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
18 * USA.
19 */
20
21/*
22 * $Id$
23 *
24 * $Log$
25 * Revision 1.1 2000/12/03 18:58:41 id
26 * initiate plugins branch
27 *
28 * Revision 1.1.1.1 1999/02/21 17:16:47 gloth
29 * initial revision
30 *
31 */
32
33#include <stdarg.h>
34#include <stdio.h>
35#include <stdlib.h>
36
37#include "generic.h"
38
39static Display *dpy = 0;
40static Colormap cmap;
41
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +020042void initWindowMaker(Display * d, Colormap c)
43{
7aadc1ef id2000-12-03 18:58:41 +000044 if (!d) {
45 return;
46 }
47 dpy = d;
48 cmap = c;
49}
50
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +020051void error(const char *format, ...)
52{
7aadc1ef id2000-12-03 18:58:41 +000053 va_list args;
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +020054 va_start(args, format);
55 vfprintf(stderr, format, args);
56 va_end(args);
7aadc1ef id2000-12-03 18:58:41 +000057}
58
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +020059int parse_color(const char *string, int *result)
60{
7aadc1ef id2000-12-03 18:58:41 +000061 XColor color;
62
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020063 if (!XParseColor(dpy, cmap, string, &color)) {
7aadc1ef id2000-12-03 18:58:41 +000064 return 0;
65 }
66 result[0] = color.red >> 8;
67 result[1] = color.green >> 8;
68 result[2] = color.blue >> 8;
69
70 return 1;
71}
72
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +020073int parse_xcolor(const char *string, XColor * xcolor)
74{
75 if (!XParseColor(dpy, cmap, string, xcolor)) {
76 return 0;
77 }
78 if (!XAllocColor(dpy, cmap, xcolor)) {
79 return 0;
7aadc1ef id2000-12-03 18:58:41 +000080 }
7aadc1ef id2000-12-03 18:58:41 +000081
82 return 1;
83}
84
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +020085void interpolate_color(int *t, const int *s0, const int *s1, int mix)
86{
7aadc1ef id2000-12-03 18:58:41 +000087 t[0] = (mix * s0[0] + (255 - mix) * s1[0]) >> 8;
88 t[1] = (mix * s0[1] + (255 - mix) * s1[1]) >> 8;
89 t[2] = (mix * s0[2] + (255 - mix) * s1[2]) >> 8;
90}
91
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +020092int random_int(int range)
93{
7aadc1ef id2000-12-03 18:58:41 +000094 return rand() / (RAND_MAX / range + 1);
95}
96
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +020097double random_double(double range)
98{
7aadc1ef id2000-12-03 18:58:41 +000099 return ((double)rand()) / (((double)RAND_MAX) / range);
100}
101
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200102int start_image(const char *name, int argc, int argc_min, int argc_max, int width, int height, RImage ** image)
103{
7aadc1ef id2000-12-03 18:58:41 +0000104
105 if (!dpy) {
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200106 error("%s: library not initialized\n", name);
7aadc1ef id2000-12-03 18:58:41 +0000107 return 0;
108 }
109
110 if ((argc < argc_min) || (argc >= argc_max)) {
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200111 error("%s: invalid number of parameters: \n", name, argc);
7aadc1ef id2000-12-03 18:58:41 +0000112 return 0;
113 }
114
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200115 *image = RCreateImage(width, height, 0);
7aadc1ef id2000-12-03 18:58:41 +0000116 if (!*image) {
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200117 error("%s: can't create image\n", name);
7aadc1ef id2000-12-03 18:58:41 +0000118 return 0;
119 }
120 return 1;
121}