File: pixmap.c

package info (click to toggle)
eterm 0.7-1
  • links: PTS
  • area: main
  • in suites: hamm
  • size: 1,024 kB
  • ctags: 1,205
  • sloc: ansic: 14,536; sh: 308; makefile: 294
file content (798 lines) | stat: -rw-r--r-- 18,444 bytes parent folder | download
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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#include <unistd.h>

#include "main.h"
#include "feature.h"
#include "debug.h"
#include "mem.h"
#include "options.h"

#ifdef USE_IMLIB
#  include <X11/Imlib.h>
#endif

#ifdef PIXMAP_SUPPORT
/* Specifying a single extension is irrelevant with Imlib. -vendu */
#  ifdef USE_IMLIB
#    define PIXMAP_EXT NULL
#  endif

extern XWindowAttributes attr;
extern char * rs_path;
extern const char * rs_pixmapScale;

/* the originally loaded pixmap and its scaling */
Pixmap set_bgPixmap (const char * /* file */);

static struct {
  short w, h, x, y;
  Pixmap pixmap;
} bgPixmap = { 0, 0, 50, 50, None };

#  ifdef IMLIB_SCALING
static struct {
  ImlibData *id;
  ImlibImage *im;
  int last_w,last_h;
} imlib = { NULL, NULL, 0, 0 };
#  endif

#  ifdef USE_IMLIB
#    ifdef IMLIB_SCALING

void
ReadPixmapViaImlib(Display *d, char *filename)
{
#      ifdef DEBUG_IMLIB
  fprintf(stderr, "ReadPixmapViaImlib(%s)\n", filename);
#      endif
  if (imlib.id == NULL) {
#      ifdef DEBUG_IMLIB
	fprintf(stderr, "ImlibInit()\n");
#      endif
	imlib.id = Imlib_init(d);
  }
#      ifdef DEBUG_IMLIB
  fprintf(stderr, "ImlibLoadImage(%s)\n", filename);
#      endif
  imlib.im = Imlib_load_image(imlib.id, filename);
}

#    else /* IMLIB_SCALING */
Pixmap
ReadFileToPixmapViaImlib(Display *d, char *filename, int *w, int *h)
{
  ImlibData  *id;
  ImlibImage *im;
  Pixmap      pmap;
  
#      ifdef DEBUG_IMLIB
  fprintf(stderr, "ReadFileToPixmapViaImlib(%s)\n", filename);
#      endif
  id = Imlib_init(d);
  im = Imlib_load_image(id,filename);
  
  if (!im) return 0;

  *w=im->rgb_width;
  *h=im->rgb_height;
  
  Imlib_render(id,im,*w,*h);
  pmap = Imlib_move_image(id,im);
  Imlib_destroy_image(id,im);
  
  return pmap;
}
#    endif /* IMLIB_SCALING */
#  endif /* USE_IMLIB */

/*
 * These GEOM strings indicate absolute size/position:
 * @ `WxH+X+Y'
 * @ `WxH+X'	-> Y = X
 * @ `WxH'	-> Y = X = 50
 * @ `W+X+Y'	-> H = W
 * @ `W+X'	-> H = W, Y = X
 * @ `W'	-> H = W, X = Y = 50
 * @ `0xH'	-> H *= H/100, X = Y = 50 (W unchanged)
 * @ `Wx0'	-> W *= W/100, X = Y = 50 (H unchanged)
 * @ `=+X+Y'	-> (H, W unchanged)
 * @ `=+X'	-> Y = X (H, W unchanged)
 *
 * These GEOM strings adjust position relative to current position:
 * @ `+X+Y'
 * @ `+X'	-> Y = X
 *
 * And this GEOM string is for querying current scale/position:
 * @ `?'
 */


  /*   '[', 2*4 + 2*3 digits + 3 delimiters, ']'. -vendu */
#define GEOM_LEN 19    /* #undef'd immediately after scale_pixmap(). - vendu */
int
scale_pixmap (const char * geom)
{
  /*   static char str [] = "[1000x1000+100+100]";   /* should be big enough */
  static char str[GEOM_LEN+1] = {'\0'};
  int w = 0, h = 0, x = 0, y = 0;
  int flags;
  int changed = 0;
  char * p, tmp;
  int n;
  
  if (geom == NULL) return 0;
  
  dprintf(("scale_pixmap(\"%s\")\n", geom));
  if (!strcmp (geom, "?"))
	{
	  sprintf (str,
			   "[%dx%d+%d+%d]",
			   bgPixmap.w,
			   bgPixmap.h,
			   bgPixmap.x,
			   bgPixmap.y);
	  xterm_seq (XTerm_title, str);
	  return 0;
	}
  
  {
	
	if ((p = strchr (geom, ';')) == NULL)
	  p = strchr (geom, '\0');
	n = (p - geom);
	/*	if (n >= sizeof(str) - 1) return 0;    NO! -vendu */
	if (n > GEOM_LEN - 1) return 0;
	strncpy (str, geom, n);
	str [n] = '\0';
  }
  
  flags = XParseGeometry (str, &x, &y, &w, &h);
  
  if (!flags) {
	flags |= WidthValue;    /* default is tile */
	w = 0; 
  }
  
  if (flags & WidthValue)
	{
	  if (!(flags & XValue)) { x = 50; }
	  if (!(flags & HeightValue)) h = w;
	  
	  if (w && !h)
		{
		  w = bgPixmap.w * ((float)w/100);
		  h = bgPixmap.h;
		}
	  else if (h && !w)
		{
		  w = bgPixmap.w;
		  h = bgPixmap.h * ((float)h/100);
		}
	  
	  /* FIXME: Should be fixed to use automagic limits. -vendu */
	  
	  if (w > 1000) w = 1000;
	  if (h > 1000) h = 1000;
	  
	  if (bgPixmap.w != w)
		{
		  bgPixmap.w = w;
		  changed++;
		}
	  if (bgPixmap.h != h)
		{
		  bgPixmap.h = h;
		  changed++;
		}
	}
  
  if (!(flags & YValue))
	{
	  if (flags & XNegative) flags |= YNegative;
	  y = x;
	}
  if (!(flags & WidthValue) && geom [0] != '=')
	{
	  x += bgPixmap.x;
	  y += bgPixmap.y;
	}
  else
	{
	  if (flags & XNegative) x += 100;
	  if (flags & YNegative) y += 100;
	}
  
  x = (x <= 0 ? 0 : (x >= 100 ? 100 : x));
  y = (y <= 0 ? 0 : (y >= 100 ? 100 : y));;
  if (bgPixmap.x != x)
	{
	  bgPixmap.x = x;
	  changed++;
	}
  if (bgPixmap.y != y)
	{
	  bgPixmap.y = y;
	  changed++;
	}
  return changed;
}
#undef GEOM_LEN

void
resize_pixmap (void)
{
  XGCValues gcvalue;
  GC gc;
  unsigned int width = TermWin_TotalWidth ();
  unsigned int height = TermWin_TotalHeight ();
  float p, incr;
  Pixmap tmp;
  int xsize, ysize;
#if 0
  Window W_dummy;
  int dummy;
  unsigned int u_dummy;
#endif
  
  /* This *might* be useful later... -vendu */
#if 0
  /* Ignore Xroot, x, y, border and depth. */
  XGetGeometry(Xdisplay, TermWin.vt, &W_dummy, &dummy, &dummy, &width, &height, &u_dummy, &u_dummy);
#endif
  
  if (!imlib.im) return;

#  ifdef IMLIB_SCALING
#    ifdef DEBUG_IMLIB
  fprintf(stderr, "resize_pixmap() block 1\n");
#    endif
	xsize = imlib.im->rgb_width;
	ysize = imlib.im->rgb_height;
#  else
  xsize = attr.width;
  ysize = attr.height;
#  endif
  
  if (TermWin.pixmap != None) {
#  ifdef DEBUG_IMLIB
	fprintf(stderr, "Imlib_free_pixmap(TermWin.pixmap)\n");
#  endif
	if (imlib.id) Imlib_free_pixmap(imlib.id, TermWin.pixmap);
	TermWin.pixmap = None;
  }
  
#  ifdef DEBUG_PIXMAP
  fprintf(stderr, "XCreatePixmap(TermWin.pixmap)\n");
#  endif
  TermWin.pixmap = XCreatePixmap (Xdisplay, TermWin.vt,
								  width, height, Xdepth);
  
  gcvalue.foreground = PixColors [bgColor];
  gc = XCreateGC (Xdisplay, TermWin.vt, GCForeground, &gcvalue);
  
#  ifdef IMLIB_SCALING
#    ifdef DEBUG_IMLIB
  fprintf(stderr, "resize_pixmap() block 2\n");
#    endif
  if (imlib.im)
#  else
	if (bgPixmap.pixmap)
#  endif
	  {
		int w = bgPixmap.w;
		int h = bgPixmap.h;
		int x = bgPixmap.x;
		int y = bgPixmap.y;
		
		/*
		 * don't zoom pixmap too much nor expand really small pixmaps
		 */
		
#  ifdef DEBUG_IMLIB
		fprintf(stderr, "resize_pixmap() block 3\n");
#  endif
		if (w > 1000 || h > 1000) {
		  w = 1;
		} else if (width > (10 * xsize) || height > (10 * ysize))
		  w = 0;		/* tile */
		
		if (w)
		  {
#  if defined(USE_IMLIB) && !defined(IMLIB_SCALING)
#    ifdef DEBUG_PIXMAP
			fprintf(stderr, "tmp = XCreatePixmap()\n");
#    endif
			tmp = XCreatePixmap (Xdisplay, TermWin.vt,
								 width, ysize,
								 Xdepth);
			
			XFillRectangle (Xdisplay, tmp, gc, 0, 0, width, ysize);
#  endif
			/*
			 * horizontal scaling
			 */
#  ifdef DEBUG_IMLIB
			fprintf(stderr, "resize_pixmap() block 4\n");
#  endif
			incr = (float)xsize;
			
			p = 0;
			
			if (w == 1)
			  {
				/* display image directly - no scaling at all */
				incr = width;
#  ifdef DEBUG_IMLIB
				fprintf(stderr, "resize_pixmap() block 5\n");
#  endif
				if (xsize <= width)
				  {
#  ifdef DEBUG_IMLIB
					fprintf(stderr, "resize_pixmap() block 6\n");
#  endif
					w = xsize;
					x = (width - w) / 2;
					w += x;
				  }
				else
				  {
					x = 0;
					w = width;
				  }
			  }
			else if (w < 10)
			  {
				incr *= w;	/* fit W images across screen */
				x = 0;
				w = width;
			  }
			else
			  {
				incr *= 100.0 / w;
				if (w < 100)	/* contract */
				  {
					w = (w * width) / 100;
					if (x >= 0)	/* position */
					  {
						float pos;
						pos = (float)x / 100 * width - (w/2);
						
						x = (width - w);
						if (pos <= 0) x = 0; else if (pos < x) x = pos;
					  }
					else
					  {
						x = (width - w) / 2;
					  }
					w += x;
				  }
				else if (w >= 100)		/* expand */
				  {
					if (x > 0)	/* position */
					  {
						float pos;
#  ifdef DEBUG_IMLIB
						fprintf(stderr, "resize_pixmap() block 7\n");
#  endif
						pos = (float)x / 100 * xsize - (incr/2);
						p = xsize - (incr);
						if (pos <= 0) p = 0; else if (pos < p) p = pos;
					  }
					x = 0;
					w = width;
				  }
			  }
			incr /= width;
			
#  ifndef IMLIB_SCALING
			for (/*nil*/; x < w; x++, p += incr)
			  {
#    ifdef USE_IMLIB
				if (p >= xsize) p = 0;
				XCopyArea (Xdisplay, bgPixmap.pixmap, tmp, gc,
						   (int)p, 0, 1, ysize, x, 0);
#    endif
			  }
#  endif /* !IMLIB_SCALING */
			
			/*
			 * vertical scaling
			 */
#  ifdef DEBUG_IMLIB
			fprintf(stderr, "resize_pixmap() block 8\n");
#  endif
			incr = (float)ysize;
			p = 0;
			
			if (h == 1)
			  {
				/* display image directly - no scaling at all */
				incr = height;
#  ifdef DEBUG_IMLIB
				fprintf(stderr, "resize_pixmap() block 9\n");
#  endif
				if (ysize <= height)
				  {
#  ifdef DEBUG_IMLIB
					fprintf(stderr, "resize_pixmap() block 10\n");
#  endif
					h = ysize;
					y = (height - h) / 2;
					h += y;
				  }
				else
				  {
					y = 0;
					h = height;
				  }
			  }
			else if (h < 10)
			  {
				incr *= h;	/* fit H images across screen */
				y = 0;
				h = height;
			  }
			else
			  {
				incr *= 100.0 / h;
				if (h < 100)		/* contract */
				  {
					h = (h * height) / 100;
					if (y >= 0)	/* position */
					  {
						float pos;
						pos = (float)y / 100 * height - (h/2);
						
						y = (height - h);
						if (pos < 0.0f) y = 0; else if (pos < y) y = pos;
					  }
					else
					  {
						y = (height - h) / 2;
					  }
					h += y;
				  }
				else if (h >= 100)		/* expand */
				  {
					if (y > 0)		/* position */
					  {
						float pos;
#  ifdef DEBUG_IMLIB
						fprintf(stderr, "resize_pixmap() block 11\n");
#  endif
						pos = (float)y / 100 * ysize - (incr/2);
						p = ysize - (incr);
						if (pos < 0) p = 0; else if (pos < p) p = pos;
					  }
					y = 0;
					h = height;
				  }
			  }
			incr /= height;
			
#  ifdef IMLIB_SCALING
#    ifdef DEBUG_IMLIB
			fprintf(stderr, "resize_pixmap() block 12\n");
#    endif
			imlib.last_w = w; imlib.last_h = h;
#    ifdef DEBUG_IMLIB
			fprintf(stderr, "Imlib_render(%dx%d)\n", w, h);
#    endif
			Imlib_render(imlib.id, imlib.im, w, h);
#    ifdef DEBUG_PIXMAP
			fprintf(stderr, "bgPixmap.pixmap = Imlib_move_image()\n");
#    endif
			bgPixmap.pixmap = Imlib_move_image(imlib.id, imlib.im);
#    ifdef DEBUG_IMLIB
			fprintf(stderr, "Imlib_destroy_image()\n");
#    endif
			Imlib_destroy_image(imlib.id, imlib.im);
#    ifdef DEBUG_PIXMAP
			fprintf(stderr, "TermWin.pixmap = bgPixmap.pixmap\n");
#    endif
			TermWin.pixmap = bgPixmap.pixmap;
#  else
			for (/*nil*/; y < h; y++, p += incr)
			  {
#    ifdef USE_IMLIB
				if (p >= ysize) p = 0;
#    endif
				XCopyArea (Xdisplay, tmp, TermWin.pixmap, gc,
						   0, (int)p, width, 1, 0, y);
			  }
#    ifdef DEBUG_IMLIB
			fprintf(stderr, "Imlib_free_pixmap(tmp)\n");
#    endif
			Imlib_free_pixmap(imlib.id, tmp);
#  endif /* IMLIB_SCALING */
		  }
		
		else /* if (w), light years above. -vendu*/
		  {
			/* tiled */
			
#  ifdef IMLIB_SCALING
#    ifdef DEBUG_IMLIB
			fprintf(stderr, "resize_pixmap() block 13\n");
			fprintf(stderr, "Imlib_render(%dx%d)\n", xsize, ysize);
#    endif
			Imlib_render(imlib.id,imlib.im,imlib.im->rgb_width,imlib.im->rgb_height);
#    ifdef DEBUG_IMLIB
			fprintf(stderr, "bgPixmap.pixmap = Imlib_move_image()\n");
#    endif
			bgPixmap.pixmap=Imlib_move_image(imlib.id,imlib.im);
#    ifdef DEBUG_IMLIB
			fprintf(stderr, "Imlib_destroy_image()\n");
#    endif
			Imlib_destroy_image(imlib.id, imlib.im);
#  endif
			
#  ifdef DEBUG_IMLIB
			fprintf(stderr, "resize_pixmap() block 14\n");
#  endif
			for (y = 0; y < height; y += ysize)
			  {
				unsigned int h = (height - y);
#  ifdef USE_IMLIB
#    ifdef DEBUG_IMLIB
				fprintf(stderr, "resize_pixmap() block 15\n");
#    endif
				if (h > ysize) h = ysize;
				for (x = 0; x < width; x += xsize)
#  endif
				  {
					unsigned int w = (width - x);
#  ifdef DEBUG_IMLIB
					fprintf(stderr, "resize_pixmap() block 16\n");
#  endif
					if (w > xsize) w = xsize;
					XCopyArea (Xdisplay,
							   bgPixmap.pixmap,
							   TermWin.pixmap,
							   gc,
							   0, 0, w, h, x, y);
				  }
			  }
		  }
	  }
	else
	  XFillRectangle (Xdisplay, TermWin.pixmap, gc,
					  0, 0, width, height);
  
#  ifdef PIXMAP_BUFFERING
  if (TermWin.buf_pixmap) XFreePixmap (Xdisplay, TermWin.buf_pixmap);
  TermWin.buf_pixmap = XCreatePixmap (Xdisplay, TermWin.vt,
									  width, height, Xdepth);
  XCopyArea (Xdisplay,
			 TermWin.pixmap,
			 TermWin.buf_pixmap,
			 gc,
			 0, 0,
			 width, height,
			 0, 0);
  XSetWindowBackgroundPixmap (Xdisplay, TermWin.vt, TermWin.buf_pixmap);
#  else	/* PIXMAP_BUFFERING */
  XSetWindowBackgroundPixmap (Xdisplay, TermWin.vt, TermWin.pixmap);
#  endif	/* PIXMAP_BUFFERING */
  XSync (Xdisplay, 0);
  XFreeGC (Xdisplay, gc);
  
  XClearWindow (Xdisplay, TermWin.vt);
  XFlush (Xdisplay);
  
  XSync (Xdisplay, 0);
  
}
#else	/* PIXMAP_SUPPORT */
#  define scale_pixmap(str)	((void)0)
#  define resize_pixmap()	((void)0)
#endif	/* PIXMAP_SUPPORT */

#if defined(PIXMAP_SUPPORT) || (MENUBAR_MAX)
/*
 * search for FILE in the current working directory, and within the
 * colon-delimited PATHLIST, adding the file extension EXT if required.
 *
 * FILE is either @ or zero terminated
 */
const char *
search_path(const char *pathlist, const char *file, const char *ext) {
  
  /* FIXME: the 256 below should be changed to some #define in <dirent.h> */
  static char name [256];
  char * p;
  const char * path; /* Was char * before. -vendu */
  int maxpath, len;
  struct stat fst;
  
#  ifdef DEBUG_OPTIONS
  fprintf(stderr, "search_path(\"%s\", \"%s\", \"%s\") called.\n",
		  pathlist, file, ext);
  fprintf(stderr, "search_path():  Checking for file \"%s\"\n", file);
#  endif
  if (!access(file, R_OK)) {
    if (stat(file, &fst)) {
      dprintf(("Unable to stat %s -- %s\n", file, strerror(errno)));
    } else {
      dprintf(("Stat returned mode 0x%08o, S_ISDIR() == %d\n", fst.st_mode, S_ISDIR(fst.st_mode)));
    }
    if (!S_ISDIR(fst.st_mode)) return file;
  }
  
  /* Changed to use '@' as the delimiter. -vendu */
  if ((p = strchr(file, '@')) == NULL) p = strchr(file, '\0');
  len = (p - file);
  
  /* check about adding a trailing extension */
  if (ext != NULL) {
	
    char *dot;
	
    dot  = strrchr(p, '.');
    path = strrchr(p, '/');
    if (dot != NULL || (path != NULL && dot <= path)) ext = NULL;
  }
  
  /* leave room for an extra '/' and trailing '\0' */
  maxpath = sizeof(name) - (len + (ext ? strlen(ext) : 0) + 2);
  if (maxpath <= 0) return NULL;
  
  /* check if we can find it now */
  strncpy(name, file, len);
  name[len] = '\0';
  
#  ifdef DEBUG_OPTIONS
  fprintf(stderr, "search_path():  Checking for file \"%s\"\n", name);
#  endif
  if (!access(name, R_OK)) {
    stat(name, &fst);
    if (!S_ISDIR(fst.st_mode)) return name;
  }
  if (ext) {
    strcat(name, ext);
#  ifdef DEBUG_OPTIONS
    fprintf(stderr, "search_path():  Checking for file \"%s\"\n", name);
#  endif
    if (!access(name, R_OK)) {
      stat(name, &fst);
      if (!S_ISDIR(fst.st_mode)) return name;
    }
  }
  
  for (path = pathlist; path != NULL && *path != '\0'; path = p) {
	
    int n;
	
    /* colon delimited */
    if ((p = strchr(path, ':')) == NULL) p = strchr(path, '\0');
	
    n = (p - path);
    if (*p != '\0') p++;
	
    if (n > 0 && n <= maxpath) {
	  
      strncpy(name, path, n);
      if (name[n-1] != '/') name[n++] = '/';
      name[n] = '\0';
      strncat(name, file, len);
	  
#  ifdef DEBUG_OPTIONS
      fprintf(stderr, "search_path():  Checking for file \"%s\"\n", name);
#  endif
      if (!access(name, R_OK)) {
		stat(name, &fst);
		if (!S_ISDIR(fst.st_mode)) return name;
      }
      if (ext) {
		strcat(name, ext);
#  ifdef DEBUG_OPTIONS
		fprintf(stderr, "search_path():  Checking for file \"%s\"\n", name);
#  endif
		if (!access(name, R_OK)) {
		  stat(name, &fst);
		  if (!S_ISDIR(fst.st_mode)) return name;
		}
      }
    }
  }
  return NULL;
}
#endif	/* PIXMAP_SUPPORT || (MENUBAR_MAX) */

#ifdef PIXMAP_SUPPORT
/* I think it might be cool to make Eterm load the pixmaps in background.
 * You'd be able to start typing commands without waiting for the bg
 * pixmap processing to end. Thoughts right now: fork(), pthreads. -vendu
 */

Pixmap
set_bgPixmap (const char *file) {
  
  const char *f; /* Was char * before. -vendu */
  
  /* FIXME: assert() looks like a bad thing. Calls abort(). IMHO, stupid. */
  assert(file != NULL);
  
#  ifdef DEBUG_IMLIB
  fprintf(stderr, "[debug] set_bgPixmap(%s) called.\n", file);
#  endif
  
  /* Turn on scaling */
  if ((Options & Opt_pixmapScale) || (rs_pixmapScale)) {
    bgPixmap.h = 100; bgPixmap.w = 100;
  }
  
  XSetWindowBackground(Xdisplay, TermWin.vt, PixColors[bgColor]);
  
  if (*file != '\0') {
    XGetWindowAttributes(Xdisplay, TermWin.vt, &attr);
	
    /* search environment variables here too */
#  ifdef USE_IMLIB
    if ((f = search_path(rs_path, file, PIXMAP_EXT)) == NULL)
#  endif
#  ifdef PATH_ENV
#    ifdef USE_IMLIB
      if ((f = search_path(getenv (PATH_ENV), file, PIXMAP_EXT)) == NULL)
#    endif
#  endif /* PATH_ENV */
#  ifdef USE_IMLIB
		f = search_path(getenv("PATH"), file, PIXMAP_EXT);
#  endif
	
#  ifdef USE_IMLIB
#    ifdef IMLIB_SCALING
    if (f != NULL) {
      if (imlib.im != NULL) {
#      ifdef DEBUG_IMLIB
		fprintf(stderr, "Imlib_destroy_image()\n");
#      endif
		Imlib_destroy_image(imlib.id, imlib.im);
	  }
#      ifdef DEBUG_IMLIB
	  fprintf(stderr, "ReadPixmapViaImlib(%s)\n", (char*)f);
#      endif
      ReadPixmapViaImlib(Xdisplay, (char *)f);
    }
    if (imlib.im == NULL) {
#    else
#      ifdef DEBUG_IMLIB
	  fprintf(stderr, "ReadFileToPixmapViaImlib(%s)\n", (char*)f);
#      endif
	  if (f == NULL || !(bgPixmap.pixmap = 
						 ReadFileToPixmapViaImlib(Xdisplay, (char *)f,
												  &attr.width,
												  &attr.height))) {
#    endif
#  endif
		char * p;
		
		if ((p = strchr(file, ';')) == NULL) p = strchr(file, '\0');
		print_error("couldn't load image file \"%.*s\"", (p - file), file);
		resize_pixmap();
	  } else if (bgPixmap.pixmap != None) resize_pixmap();
	} else {
#  ifdef DEBUG_PIXMAP
	  fprintf(stderr, "Nuking pixmap pointers");
	  bgPixmap.pixmap = None;
#  endif
	  rs_pixmaps[pixmap_bg] = "";
#  ifdef USE_IMLIB
	  imlib.im = 0;
#  endif
	}
	
	/* FIXME: The call to XClearWindow could probably be removed. -vendu */
	XClearWindow(Xdisplay, TermWin.vt);
	scr_touch();
	XFlush(Xdisplay);
	return bgPixmap.pixmap;
  }
#  ifdef USE_IMLIB
#    undef PIXMAP_EXT
#  endif
#endif	/* PIXMAP_SUPPORT */