File: xpmicon.c

package info (click to toggle)
WMRack 1.0b3-1
  • links: PTS
  • area: main
  • in suites: slink
  • size: 424 kB
  • ctags: 260
  • sloc: ansic: 3,297; sh: 156; makefile: 109
file content (338 lines) | stat: -rw-r--r-- 7,488 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
/*
 * $Id: xpmicon.c,v 1.7 1997/10/18 14:39:20 ograf Exp $
 *
 * part of WMRack
 *
 * handles the whole pixmap stuff (styles)
 *
 * Copyright (c) 1997 by Oliver Graf <ograf@fga.de>
 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <X11/Xlib.h>
#include <X11/xpm.h>
#include <X11/extensions/shape.h>

#include "library.h"
#include "xpmicon.h"

/* include the default xpms */
#include "XPM/standart.style"

/*
 * first some requires variables
 */
XpmAttributes rackDefaultAttr;
XpmIcon rackXpm[]={{"cdnodisc",cdnodisc},
		   {"cdstopped",cdstopped},
		   {"cdplaying",cdplaying},
		   {"cdpaused",cdpaused},
		   {"mixer",mixer},
		   {"cdled",cdled},
		   {"mixled",mixled},
		   {"alphaled",alphaled}};

int curRack=RACK_NODISC;

XpmColorSymbol LedColorSymbols[4]={{"led_color_back", "#000000000000", 0},
				   {"led_color_high", "#0000FFFF0000", 0},
				   {"led_color_med",  "#00009CE60000", 0},
				   {"led_color_low",  "#000063180000", 0}};

char *ledAlphabet="ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 ";

/*
 * some internal functions
 */
/****************************************************************************/
Pixel xpm_getDimColor(Display *disp, Drawable draw, char *ColorName, float dim)
{
  XColor Color;
  XWindowAttributes Attributes;

  XGetWindowAttributes(disp,draw,&Attributes);
  Color.pixel = 0;

  if (!XParseColor (disp, Attributes.colormap, ColorName, &Color))
    fprintf(stderr,"xpm_getDimColor: can't parse %s\n", ColorName);
  else
    {
      Color.red/=dim;
      Color.green/=dim;
      Color.blue/=dim;
      Color.flags=DoRed|DoGreen|DoBlue;
      if (!XAllocColor (disp, Attributes.colormap, &Color))
	fprintf(stderr,"xpm_getDimColor: can't allocate %s\n", ColorName);
    }

  return Color.pixel;
}

int parseXpm(Display *disp, Drawable draw, char *buffer)
{
  int i, len;
  char *d1, *d2;

  /* check header */
  if (strncmp(buffer,"/* XPM */\nstatic char *",23)!=0)
    {
      fprintf(stderr,"parseXpm: invalid start of xpm\n");
      fprintf(stderr,buffer);
      return 1;
    }
  /* skip spaces */
  for (d1=buffer+23; *d1==32; d1++);
  /* find end */
  d2=strchr(d1,'[');
  if (d2==NULL)
    {
      fprintf(stderr,"parseXpm: can't parse xpm-name\n");
      return 1;
    }
  for (; *(d2-1)==32; d2--);
  len=d2-d1;
#ifdef DEBUG
  {
    char name[200];
    strncpy(name,d1,len);
    name[len]=0;
    fprintf(stderr,"parseXpm: found XPM %s\n",name);
  }
#endif
  /* find the right place */
  for (i=0; i<RACK_MAX; i++)
    {
      if (strncmp(rackXpm[i].name,d1,len)==0)
	{
	  if (rackXpm[i].loaded)
	    {
	      fprintf(stderr,"parseXpm: skipping double XPM for %s\n",d1);
	      break;
	    }
	  else
	    {
	      rackXpm[i].attributes=rackDefaultAttr;
	      if (XpmCreatePixmapFromBuffer(disp, draw, buffer,
					    &rackXpm[i].pixmap,
					    &rackXpm[i].mask,
					    &rackXpm[i].attributes)!=XpmSuccess)
		{
		  fprintf(stderr,"parseXpm: can't create pixmap from buffer\n");
		  return 1;
		}
	      rackXpm[i].loaded=1;
	      break;
	    }
	}
    }
  return 0;
}

/*
 * now the interface functions
 */

/*
 * xpm_setDefaultAttr(color)
 *
 * creates the attributes for xpm pixmap creation and sets
 * the led symbol colors to the specified color and 2 shades of it.
 */
int xpm_setDefaultAttr(Display *disp, Drawable draw, char *color, char *back)
{
  rackDefaultAttr.valuemask=XpmReturnPixels | XpmReturnExtensions | XpmColorSymbols;
  rackDefaultAttr.numsymbols=4;
  if (color!=NULL)
    {
      LedColorSymbols[1].value=NULL;
      LedColorSymbols[1].pixel=xpm_getDimColor(disp,draw,color,1);
      LedColorSymbols[2].value=NULL;
      LedColorSymbols[2].pixel=xpm_getDimColor(disp,draw,color,1.65);
      LedColorSymbols[3].value=NULL;
      LedColorSymbols[3].pixel=xpm_getDimColor(disp,draw,color,2.6);
    }
  if (back!=NULL)
    {
      LedColorSymbols[0].value=NULL;
      LedColorSymbols[0].pixel=xpm_getDimColor(disp,draw,back,1);
    }
  rackDefaultAttr.colorsymbols=LedColorSymbols;
  return 0;
}

/*
 * xpm_loadSet(display, drawable, filename)
 *
 * loads a style file
 */
int xpm_loadSet(Display *disp, Drawable draw, char *filename)
{
  FILE *f;
  char *buffer=NULL, line[4096], path[4096];
  int bufpos=0, bufalloc=0, i;

  buffer=lib_findfile(filename,1);
  if (buffer==NULL)
    {
      fprintf(stderr,"xpm_loadSet: can't find file\n");
      return 1;
    }
  strcpy(path,buffer);
  buffer=NULL;
#ifdef DEBUG
  fprintf(stderr,"xpm_loadSet: loading %s\n",path);
#endif

  f=fopen(path,"r");
  if (f==NULL)
    {
      perror("xpm_loadSet");
      return 1;
    }

  for (i=0; i<RACK_MAX; i++)
    rackXpm[i].loaded=0;

  while (fgets(line,4095,f)!=NULL)
    {
      if (strcmp(line,"/* XPM */\n")==0)
	{
	  if (buffer!=NULL)
	    {
	      if (parseXpm(disp,draw,buffer))
		{
		  free(buffer);
		  fclose(f);
		  return 1;
		}
	      free(buffer);
	      bufpos=bufalloc=0;
	    }
	  buffer=(char *)malloc(4096);
	  if (buffer==NULL)
	    {
	      fprintf(stderr,"xpm_loadSet: can' alloc enough mem\n");
	      fclose(f);
	      return 1;
	    }
	  bufalloc=4096;
	  strcpy(buffer+bufpos,line);
	  bufpos=strlen(buffer);
	  continue;
	}
      if (buffer!=NULL)
	{
	  if (bufpos+strlen(line)>=bufalloc)
	    {
	      char *newbuf;
	      newbuf=(char *)realloc(buffer,bufalloc+4096);
	      if (newbuf==NULL)
		{
		  fprintf(stderr,"xpm_loadSet: can' alloc enough mem\n");
		  fclose(f);
		  return 1;
		}
	      buffer=newbuf;
	      bufalloc+=4096;
	    }
	  strcpy(buffer+bufpos,line);
	  bufpos+=strlen(line);
	}
    }
  fclose(f);

  if (buffer!=NULL)
    {
      if (parseXpm(disp,draw,buffer))
	{
	  free(buffer);
	  return 1;
	}
      free(buffer);
    }

  /* check if all xpms are loaded */
  for (i=0; i<RACK_MAX; i++)
    if (!rackXpm[i].loaded)
      {
	fprintf(stderr,"xpm_loadSet: xpm for %s is missing, using default\n",
		rackXpm[i].name);
	if (xpm_setDefaultSet(disp,draw,i))
	  {
	    fprintf(stderr,"xpm_loadSet: can't set default pixmap\n");
	    return 1;
	  }
      }

  return 0;
}

/*
 * int xpm_getDefaultsSet(display, drawable, num)
 *
 * sets the specified pixmap buffer to the compiletime default
 * sets all pixmap buffers, if num is RACK_MAX
 *
 * returns 0 on success
 */
int xpm_setDefaultSet(Display *disp, Drawable draw, int num)
{
  int i, ret=0, x;

  if (num==RACK_MAX)
    for (i=0; i<RACK_MAX; i++)
      {
	if (rackXpm[i].standart==NULL)
	  continue;
	rackXpm[i].attributes=rackDefaultAttr;
	x=XpmCreatePixmapFromData(disp, draw, rackXpm[i].standart,
				  &rackXpm[i].pixmap,
				  &rackXpm[i].mask,
				  &rackXpm[i].attributes)!=XpmSuccess;
	ret|=x;
#ifdef DEBUG
	if (x)
	  fprintf(stderr,"xpm_setDefaultSet: failure to load %d (XpmErrNo %d)\n",i,x);
#endif
      }
  else
    {
      if (rackXpm[num].standart==NULL)
	return 0;
      rackXpm[num].attributes=rackDefaultAttr;
      ret|=XpmCreatePixmapFromData(disp, draw, rackXpm[num].standart,
				   &rackXpm[num].pixmap,
				   &rackXpm[num].mask,
				   &rackXpm[num].attributes)!=XpmSuccess;
#ifdef DEBUG
      if (ret)
	fprintf(stderr,"xpm_setDefaultSet: failure to load %d (XpmErrNo %d)\n",i,x);
#endif
    }
  return ret;
}

/*
 * xpm_freeSet(display)
 *
 * frees all pixmap data
 */
void xpm_freeSet(Display *disp)
{
  int i;

#ifdef DEBUG
  fprintf(stderr,"xpm_freeSet: freeing pixmaps\n");
#endif
  for (i=0; i<RACK_MAX; i++)
    {
      XFreePixmap(disp,rackXpm[i].pixmap);
      XFreePixmap(disp,rackXpm[i].mask);
    }
#ifdef DEBUG
  fprintf(stderr,"xpm_freeSet: done.\n");
#endif
}