1 /*--------------------------------*-C-*---------------------------------*
2 * File: pixmap.c
3 *----------------------------------------------------------------------*
4 *
5 * All portions of code are copyright by their respective author/s.
6 * Copyright (c) 1997 Carsten Haitzler <raster@zip.com.au>
7 * Copyright (c) 1997,1998 Oezguer Kesim <kesim@math.fu-berlin.de>
8 * Copyright (c) 1998-2001 Geoff Wing <gcw@pobox.com>
9 * Copyright (c) 2004 Jingmin Zhou <jimmyzhou@users.sourceforge.net>
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 *----------------------------------------------------------------------*/
25 /*
26 ** $Id: pixmap.c,v 1.24 2004/11/20 21:33:15 cvs Exp $
27 */
28
29 #include "../config.h"
30 #include "rxvt.h"
31
32
33 #ifdef DEBUG_VERBOSE
34 #define DEBUG_LEVEL 1
35 #else
36 #define DEBUG_LEVEL 0
37 #endif
38
39 #if DEBUG_LEVEL
40 #define DBG_MSG(d,x) if(d <= DEBUG_LEVEL) fprintf x
41 #else
42 #define DBG_MSG(d,x)
43 #endif
44
45
46 #ifdef BACKGROUND_IMAGE
47
48 /*--------------------------------------------------------------------*
49 * BEGIN `INTERNAL' ROUTINE PROTOTYPES *
50 *--------------------------------------------------------------------*/
51 void rxvt_pixmap_incr (unsigned int*, unsigned int*, float*, float*, unsigned int, unsigned int);
52 /*--------------------------------------------------------------------*
53 * END `INTERNAL' ROUTINE PROTOTYPES *
54 *--------------------------------------------------------------------*/
55
56
57
58 /*
59 * These GEOM strings indicate absolute size/position:
60 * @ `WxH+X+Y'
61 * @ `WxH+X' -> Y = X
62 * @ `WxH' -> Y = X = 50
63 * @ `W+X+Y' -> H = W
64 * @ `W+X' -> H = W, Y = X
65 * @ `W' -> H = W, X = Y = 50
66 * @ `0xH' -> H *= H/100, X = Y = 50 (W unchanged)
67 * @ `Wx0' -> W *= W/100, X = Y = 50 (H unchanged)
68 * @ `=+X+Y' -> (H, W unchanged)
69 * @ `=+X' -> Y = X (H, W unchanged)
70 *
71 * These GEOM strings adjust position relative to current position:
72 * @ `+X+Y'
73 * @ `+X' -> Y = X
74 *
75 * And this GEOM string is for querying current scale/position:
76 * @ `?'
77 */
78 /* EXTPROTO */
79 int
rxvt_scale_pixmap(rxvt_t * r,int page,const char * geom)80 rxvt_scale_pixmap(rxvt_t *r, int page, const char *geom)
81 {
82 int flags, changed = 0;
83 int x = 0, y = 0;
84 unsigned int w = 0, h = 0;
85 unsigned int n;
86 char *p, *str;
87 bgPixmap_t *bgpixmap = &(PVTS(r, page)->bg);
88
89 #define MAXLEN_GEOM sizeof("[1000x1000+1000+1000]")
90
91 if (geom == NULL)
92 return 0;
93 str = rxvt_malloc(MAXLEN_GEOM + 1);
94 if (!STRCMP(geom, "?")) {
95 sprintf(str, "[%dx%d+%d+%d]", /* can't presume snprintf() ! */
96 min(bgpixmap->w, 9999), min(bgpixmap->h, 9999),
97 min(bgpixmap->x, 9999), min(bgpixmap->y, 9999));
98 rxvt_xterm_seq(r, page, XTerm_title, str, CHAR_ST);
99 free(str);
100 return 0;
101 }
102
103 if ((p = STRCHR(geom, ';')) == NULL)
104 p = STRCHR(geom, '\0');
105 n = (p - geom);
106 if (n <= MAXLEN_GEOM) {
107 STRNCPY(str, geom, n);
108 str[n] = '\0';
109
110 flags = XParseGeometry(str, &x, &y, &w, &h);
111 if (!flags) {
112 flags |= WidthValue;
113 w = 0;
114 } /* default is tile */
115 if (flags & WidthValue) {
116 if (!(flags & XValue))
117 x = 50;
118 if (!(flags & HeightValue))
119 h = w;
120 if (w && !h) {
121 w = (bgpixmap->w * w) / 100;
122 h = bgpixmap->h;
123 } else if (h && !w) {
124 w = bgpixmap->w;
125 h = (bgpixmap->h * h) / 100;
126 }
127 if (w > 1000)
128 w = 1000;
129 if (h > 1000)
130 h = 1000;
131 if (bgpixmap->w != (short)w) {
132 bgpixmap->w = (short)w;
133 changed++;
134 }
135 if (bgpixmap->h != (short)h) {
136 bgpixmap->h = (short)h;
137 changed++;
138 }
139 }
140 if (!(flags & YValue)) {
141 if (flags & XNegative)
142 flags |= YNegative;
143 y = x;
144 }
145
146 if (!(flags & WidthValue) && geom[0] != '=') {
147 x += bgpixmap->x;
148 y += bgpixmap->y;
149 } else {
150 if (flags & XNegative)
151 x += 100;
152 if (flags & YNegative)
153 y += 100;
154 }
155 MIN_IT(x, 100);
156 MIN_IT(y, 100);
157 MAX_IT(x, 0);
158 MAX_IT(y, 0);
159 if (bgpixmap->x != x) {
160 bgpixmap->x = x;
161 changed++;
162 }
163 if (bgpixmap->y != y) {
164 bgpixmap->y = y;
165 changed++;
166 }
167 }
168 free(str);
169 return changed;
170 }
171
172
173 /* EXTPROTO */
174 void
rxvt_resize_pixmap(rxvt_t * r,int page)175 rxvt_resize_pixmap(rxvt_t *r, int page)
176 {
177 XGCValues gcvalue;
178 GC gc;
179 unsigned int width = VT_WIDTH(r);
180 unsigned int height = VT_HEIGHT(r);
181
182 if (PVTS(r, page)->pixmap != None)
183 XFreePixmap(r->Xdisplay, PVTS(r, page)->pixmap);
184
185 if (PVTS(r, page)->bg.pixmap == None) {
186 /* So be it: I'm not using pixmaps */
187 PVTS(r, page)->bg.pixmap = None;
188 # ifdef TRANSPARENT
189 if (!(r->Options & Opt_transparent) ||
190 r->h->am_transparent == 0)
191 # endif
192 XSetWindowBackground(r->Xdisplay, PVTS(r, page)->vt,
193 r->PixColors[Color_bg]);
194 return;
195 }
196
197 gcvalue.foreground = r->PixColors[Color_bg];
198 gc = XCreateGC(r->Xdisplay, PVTS(r, page)->vt, GCForeground, &gcvalue);
199
200 if ((None != gc) &&
201 (None != PVTS(r, page)->bg.pixmap)) {
202 /* we have a specified pixmap */
203 unsigned int w = PVTS(r, page)->bg.w;
204 unsigned int h = PVTS(r, page)->bg.h;
205 unsigned int x = PVTS(r, page)->bg.x;
206 unsigned int y = PVTS(r, page)->bg.y;
207 unsigned int xpmh = PVTS(r, page)->xpm_attr.height;
208 unsigned int xpmw = PVTS(r, page)->xpm_attr.width;
209 unsigned int pixmapw, pixmaph;
210 #ifdef TINTING_SUPPORT
211 GC sgc; /* dummy gc for fade/shade */
212 #endif
213
214 /*
215 * don't zoom pixmap too much nor expand really small pixmaps
216 */
217 if (w > 1000 || h > 1000)
218 w = 1;
219 else if (width > (10 * xpmw)
220 || height > (10 * xpmh))
221 w = 0; /* tile */
222
223 if (w == 0) {
224 /* basic X tiling - let the X server do it */
225 PVTS(r, page)->pixmap = XCreatePixmap(r->Xdisplay,
226 PVTS(r, page)->vt, xpmw, xpmh, (unsigned int)XDEPTH);
227 XCopyArea(r->Xdisplay, PVTS(r, page)->bg.pixmap,
228 PVTS(r, page)->pixmap, gc, 0, 0, xpmw, xpmh, 0, 0);
229
230 pixmapw = xpmw;
231 pixmaph = xpmh;
232 }
233 else {
234 float incr, p;
235 Pixmap tmp;
236
237 PVTS(r, page)->pixmap = XCreatePixmap(r->Xdisplay,
238 PVTS(r, page)->vt, width, height, (unsigned int)XDEPTH);
239
240 /* horizontal scaling */
241 rxvt_pixmap_incr(&w, &x, &incr, &p, width, xpmw);
242
243 tmp = XCreatePixmap(r->Xdisplay, PVTS(r, page)->vt,
244 width, xpmh, (unsigned int)XDEPTH);
245 XFillRectangle(r->Xdisplay, tmp, gc, 0, 0, width,
246 xpmh);
247
248 for ( /*nil */ ; x < w; x++, p += incr) {
249 if (p >= xpmw)
250 p = 0;
251 /* copy one column from the original pixmap to the
252 ** tmp pixmap */
253 XCopyArea(r->Xdisplay, PVTS(r, page)->bg.pixmap, tmp,
254 gc, (int)p, 0, 1, xpmh, (int)x, 0);
255 }
256
257 /* vertical scaling */
258 rxvt_pixmap_incr(&h, &y, &incr, &p, height, xpmh);
259
260 if (y > 0)
261 XFillRectangle(r->Xdisplay, PVTS(r, page)->pixmap, gc,
262 0, 0, width, y);
263 if (h < height)
264 XFillRectangle(r->Xdisplay, PVTS(r, page)->pixmap, gc,
265 0, (int)h, width, height - h + 1);
266 for ( /*nil */ ; y < h; y++, p += incr) {
267 if (p >= xpmh)
268 p = 0;
269 /* copy one row from the tmp pixmap to the main
270 ** pixmap */
271 XCopyArea(r->Xdisplay, tmp, PVTS(r, page)->pixmap, gc,
272 0, (int)p, width, 1, 0, (int)y);
273 }
274 XFreePixmap(r->Xdisplay, tmp);
275
276 pixmapw = width;
277 pixmaph = height;
278 }
279
280 #ifdef TINTING_SUPPORT
281 /* Alloc dummy GC */
282 sgc = XCreateGC (r->Xdisplay, PVTS(r, page)->vt, 0UL, &gcvalue);
283 if (None != sgc) {
284 /* Background fade support */
285 if (r->h->rs[Rs_backgroundFade]) {
286 rxvt_shade_pixmap (r, PVTS(r, page)->pixmap, sgc,
287 0, 0, pixmapw, pixmaph, r->TermWin.bgfade,
288 r->PixColors[Color_White]);
289 }
290
291 /* Tinting support */
292 if (ISSET_PIXCOLOR (r->h, Color_tint) &&
293 r->h->rs[Rs_shade]) {
294 rxvt_shade_pixmap (r, PVTS(r, page)->pixmap, sgc,
295 0, 0, pixmapw, pixmaph, r->TermWin.shade,
296 r->PixColors[Color_tint]);
297 }
298 /* Free dummy GC */
299 XFreeGC (r->Xdisplay, sgc);
300 } /* None != sgc */
301 #endif /* TINTING_SUPPORT */
302
303 /* Free gc */
304 XFreeGC(r->Xdisplay, gc);
305 }
306
307 XSetWindowBackgroundPixmap(r->Xdisplay, PVTS(r, page)->vt,
308 PVTS(r, page)->pixmap);
309 # ifdef TRANSPARENT
310 r->h->am_transparent = 0;
311 # endif
312
313 XClearWindow(r->Xdisplay, PVTS(r, page)->vt);
314
315 XSync(r->Xdisplay, False);
316 }
317
318 /*
319 * Calculate tiling sizes and increments
320 * At start, p == 0, incr == xpmwidthheight
321 */
322 /* INTPROTO */
323 void
rxvt_pixmap_incr(unsigned int * wh,unsigned int * xy,float * incr,float * p,unsigned int widthheight,unsigned int xpmwidthheight)324 rxvt_pixmap_incr(unsigned int *wh, unsigned int *xy, float *incr, float *p, unsigned int widthheight, unsigned int xpmwidthheight)
325 {
326 unsigned int cwh, cxy;
327 float cincr, cp;
328
329 cp = 0;
330 cincr = (float)xpmwidthheight;
331 cxy = *xy;
332 cwh = *wh;
333 if (cwh == 1) { /* display one image, no horizontal/vertical scaling */
334 cincr = (float)widthheight;
335 if (xpmwidthheight <= widthheight) {
336 cwh = xpmwidthheight;
337 cxy = (cxy * (widthheight - cwh)) / 100; /* beware! order */
338 cwh += cxy;
339 } else {
340 cxy = 0;
341 cwh = widthheight;
342 }
343 } else if (cwh < 10) { /* fit WH images across/down screen */
344 cincr *= cwh;
345 cxy = 0;
346 cwh = widthheight;
347 } else {
348 cincr *= 100.0 / cwh;
349 if (cwh < 100) { /* contract */
350 float pos;
351
352 cwh = (cwh * widthheight) / 100;
353 pos = (float)cxy / 100 * widthheight - (cwh / 2);
354
355 cxy = (widthheight - cwh);
356 if (pos <= 0)
357 cxy = 0;
358 else if (pos < cxy)
359 cxy = pos;
360 cwh += cxy;
361 } else { /* expand */
362 if (cxy > 0) { /* position */
363 float pos;
364
365 pos = (float)cxy / 100 * xpmwidthheight - (cincr / 2);
366 cp = xpmwidthheight - cincr;
367 if (pos <= 0)
368 cp = 0;
369 else if (pos < cp)
370 cp = pos;
371 }
372 cxy = 0;
373 cwh = widthheight;
374 }
375 }
376 cincr /= widthheight;
377 *wh = cwh;
378 *xy = cxy;
379 *incr = cincr;
380 *p = cp;
381 }
382
383
384 /* EXTPROTO */
385 Pixmap
rxvt_load_bg_pixmap(rxvt_t * r,int page,const char * file)386 rxvt_load_bg_pixmap(rxvt_t *r, int page, const char *file)
387 {
388 Pixmap pixmap;
389 long w = 0, h = 0;
390
391 assert(file != NULL);
392
393 pixmap = rxvt_load_pixmap (r, file, &w, &h);
394 if (PVTS(r, page)->bg.pixmap != None)
395 XFreePixmap (r->Xdisplay, PVTS(r, page)->bg.pixmap);
396 PVTS(r, page)->bg.pixmap = pixmap;
397
398 if (None == pixmap) {
399 XSetWindowBackground(r->Xdisplay, PVTS(r, page)->vt, r->PixColors[Color_bg]);
400 return None;
401 }
402
403 PVTS(r, page)->xpm_attr.closeness = 30000;
404 PVTS(r, page)->xpm_attr.colormap = XCMAP;
405 PVTS(r, page)->xpm_attr.visual = XVISUAL;
406 PVTS(r, page)->xpm_attr.depth = XDEPTH;
407 PVTS(r, page)->xpm_attr.valuemask = (XpmCloseness | XpmColormap |
408 XpmVisual | XpmDepth | XpmSize | XpmReturnPixels);
409 PVTS(r, page)->xpm_attr.width = w;
410 PVTS(r, page)->xpm_attr.height = h;
411 rxvt_resize_pixmap(r, page);
412 return (pixmap);
413 }
414
415
416 /* EXTPROTO */
417 Pixmap
rxvt_load_pixmap(rxvt_t * r,const char * file,long * pwidth,long * pheight)418 rxvt_load_pixmap(rxvt_t *r, const char *file, long* pwidth, long* pheight)
419 {
420 char* f;
421 int flen;
422 #if defined(USE_JPEG) || defined(USE_PNG)
423 long w = 0, h = 0;
424 #endif
425 XpmAttributes xpm_attr;
426 Pixmap pixmap = None;
427
428
429 assert(file != NULL);
430 if ((char) 0 == *file) { /* No file to load */
431 return None;
432 }
433
434 xpm_attr.closeness = 30000;
435 xpm_attr.colormap = XCMAP;
436 xpm_attr.visual = XVISUAL;
437 xpm_attr.depth = XDEPTH;
438 xpm_attr.valuemask = (XpmCloseness | XpmColormap |
439 XpmVisual | XpmDepth | XpmSize | XpmReturnPixels);
440
441
442 /* search environment variables here too */
443 if (NULL == (f = (char*) rxvt_File_find (file, ".xpm", r->h->rs[Rs_path]))
444 #ifdef USE_JPEG
445 && NULL == (f = (char*) rxvt_File_find (file, ".jpg", r->h->rs[Rs_path]))
446 && NULL == (f = (char*) rxvt_File_find (file, ".jpeg", r->h->rs[Rs_path]))
447 #endif
448 #ifdef USE_PNG
449 && NULL == (f = (char*) rxvt_File_find (file, ".png", r->h->rs[Rs_path]))
450 #endif
451 ) {
452 char *p;
453 /* semi-colon delimited */
454 if (NULL == (p = STRCHR(file, ';')))
455 p = STRCHR(file, '\0');
456 rxvt_print_error("couldn't load image file \"%.*s\"", (p - file), file);
457 return None;
458 }
459
460 flen = STRLEN (f);
461 #ifdef USE_JPEG
462 if ((flen >= 4 && !STRNCASECMP (f+flen-4, ".jpg", 4)) ||
463 (flen >= 5 && !STRNCASECMP (f+flen-5, ".jpeg",5))) {
464 GC gc = DefaultGC (r->Xdisplay, XSCREEN);
465 if (!JpegReadFileToPixmap (r->Xdisplay, XROOT, gc, f,
466 &pixmap, &w, &h)) {
467 *pwidth = w;
468 *pheight = h;
469 }
470 }
471 else
472 #endif
473 #ifdef USE_PNG
474 if (flen >= 4 && !STRNCASECMP (f+flen-4, ".png", 4)) {
475 GC gc = DefaultGC (r->Xdisplay, XSCREEN);
476 if (!PngReadFileToPixmap (r->Xdisplay, XROOT, gc, f,
477 &pixmap, &w, &h)) {
478 *pwidth = w;
479 *pheight = h;
480 }
481 }
482 else
483 #endif
484 #ifdef HAVE_LIBXPM
485 if (!XpmReadFileToPixmap(r->Xdisplay, XROOT, f,
486 &pixmap, NULL, &xpm_attr)) {
487 *pwidth = xpm_attr.width;
488 *pheight = xpm_attr.height;
489 }
490 #endif
491 {
492 /* empty to suppress compile error */
493 }
494
495 free(f);
496 if (None == pixmap) {
497 char *p;
498 /* semi-colon delimited */
499 if ((p = STRCHR(file, ';')) == NULL)
500 p = STRCHR(file, '\0');
501 rxvt_print_error("couldn't load image file \"%.*s\"", (p - file), file);
502 }
503
504 return (pixmap);
505 }
506
507 #endif /* BACKGROUND_IMAGE */
508
509 /*----------------------- end-of-file (C source) -----------------------*/
510