File: pix.c

package info (click to toggle)
xbanner 1.31-13
  • links: PTS
  • area: main
  • in suites: hamm
  • size: 1,108 kB
  • ctags: 832
  • sloc: ansic: 5,084; sh: 263; csh: 168; makefile: 150
file content (176 lines) | stat: -rw-r--r-- 4,974 bytes parent folder | download | duplicates (8)
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
/* the pixmap paste thingy */

#include "xb_config.h"

#include <stdio.h>

#include <X11/Xlib.h>

#ifdef HAS_XPM
#include <X11/xpm.h>
#endif

#include "xbanner.h"

#define MAX_PATH_LEN 1024

/* defined outside the #ifdef to avoid ANSI's forbidden empty source files */
int	px = PX_DEFVAL,				/* X location of the image */
	py = PY_DEFVAL,				/* Y location of the image */
	ph,pw;					/* pixmap width/height     */
char	PIXFILE[MAX_PATH_LEN]=PIXNAME_DEFVAL;	/* filename...		   */
char	BGPIXFNAME[MAX_PATH_LEN]=BGPIXFN_DEFVAL;	/* filename for default bg */

Bool	dopix=DOPIX_DEFVAL;			/* do we put one on or not?*/

#ifdef HAS_XPM

void DoPasteOnePixmap(char *fname,int px,int py)
{
  int 		stat;		/* status returned from XpmReadFileToPixmap */
  Pixmap	pix,shape;	/* the pixmap and shapemask		    */
  XGCValues	gcv;		/* needed to set a clipping region	    */
  XpmAttributes xpm_attr;	/* needed to ask for size and set closeness */

  xpm_attr.valuemask = XpmCloseness|XpmSize; /* ask for size and set closeness */
  xpm_attr.closeness = CLOSENESS_DEFVAL;     /* wanted closeness value */

  /* do it! and check the results */
  stat=XpmReadFileToPixmap(disp,rootpix,fname,&pix,&shape,&xpm_attr);
  switch(stat)
  {
    case XpmFileInvalid:
      error(disp,mgc,"XpmFileInvalid - Invalid XPM file",False);
      return;
      break;
    case XpmOpenFailed:
      error(disp,mgc,"XpmOpenFailed - Can't open pixmap file",False);
      return;
      break;
    case XpmNoMemory:
      error(disp,mgc,"XpmNoMemory - Memory allocation error trying to load pixmap",False);
      return;
      break;
    case XpmColorError:
      error(disp,mgc,"XpmColorError - Color allocation error",False);
      return;
      break;
    case XpmColorFailed:
      error(disp,mgc,"XpmColorFailed - Could not allocate correct colors for pixmap",False);
      return;
      break;
    default:
      break;
  }
  
  /* for ease of use */
  ph = xpm_attr.height;
  pw = xpm_attr.width;

  /* did the image have a "None" color? */
  if(shape!=0)		/* I am not sure about this, but it seems to work */
  {
    gcv.clip_mask=shape;	/* set clip mask */
    gcv.clip_x_origin=px;	/* and clip origin */
    gcv.clip_y_origin=py;
    XChangeGC(disp,mgc,GCClipMask|GCClipXOrigin|GCClipYOrigin,&gcv);
  }

  /* paste it on screen - straight to the root window */
  XCopyArea(disp,pix,root,mgc,0,0,pw,ph,px,py);

  /* free stuff - not colors, though */
  if(shape!=0)		/* I am not sure about this, but it seems to work */
  {
    gcv.clip_mask=0;		/* reset clip mask */
    gcv.clip_x_origin=0;	/* and clip origin */
    gcv.clip_y_origin=0;
    XChangeGC(disp,mgc,GCClipMask|GCClipXOrigin|GCClipYOrigin,&gcv);
    XFreePixmap(disp,shape);
  }
  XFreePixmap(disp,pix);
  XpmFreeAttributes(&xpm_attr);
}

void DoPastePixmap(void)
{
  FILE *fin;
  char path[MAX_PATH_LEN];
  char line[MAX_PATH_LEN+10]; /* two 4-digit numbers and space following name */
  int pix_x,pix_y;

  /* do we really have to do this??? */
  if(dopix==False)
    return;		/* guess not */

  if(PIXFILE[0]!='@')
  {
    DoPasteOnePixmap(PIXFILE,px,py);
    return;
  }

  fin = fopen(PIXFILE+1,"r");
  if(fin==NULL || ferror(fin))
  {
    error(disp,DefaultGC(disp,DefaultScreen(disp)),"Can't read PIXlist",False);
    return;
  }
  while((fgets(line,MAX_PATH_LEN+10,fin))!=NULL)
  {
    sscanf(line,"%s %d %d",path,&pix_x,&pix_y);
    DoPasteOnePixmap(path,pix_x,pix_y);
  }
  fclose(fin);
}

void DoTilePixmap(Display *disp, Window w)
{
  int 		stat;		/* status returned from XpmReadFileToPixmap */
  Pixmap	pix,shape;	/* the pixmap and shapemask		    */
  XpmAttributes xpm_attr;	/* needed to ask for size and set closeness */

  xpm_attr.valuemask = XpmCloseness;		/* set closeness */
  xpm_attr.closeness = CLOSENESS_DEFVAL;	/* wanted closeness value */

  /* do it! and check the results */
  stat=XpmReadFileToPixmap(disp,w,BGPIXFNAME,&pix,&shape,&xpm_attr);
  switch(stat)
  {
    case XpmFileInvalid:
      error(disp,mgc,"XpmFileInvalid - Invalid XPM file",False);
      return;
      break;
    case XpmOpenFailed:
      error(disp,mgc,"XpmOpenFailed - Can't open pixmap file",False);
      return;
      break;
    case XpmNoMemory:
      error(disp,mgc,"XpmNoMemory - Memory allocation error trying to load pixmap",False);
      return;
      break;
    case XpmColorError:
      error(disp,mgc,"XpmColorError - Color allocation error",False);
      return;
      break;
    case XpmColorFailed:
      error(disp,mgc,"XpmColorFailed - Could not allocate correct colors for pixmap",False);
      return;
      break;
    default:
      break;
  }

  /* set the tile */
  XSetWindowBackgroundPixmap(disp,root,pix);

  /* now fill a rectangle the size of the entire screen */
  XClearWindow(disp,w);

  /* free stuff - not colors, though */
  if(shape!=0)		/* I am not sure about this, but it seems to work */
    XFreePixmap(disp,shape);
  XFreePixmap(disp,pix);
  XpmFreeAttributes(&xpm_attr);
}

#endif /* ifdef HAS_XPM */