~ubuntu-branches/ubuntu/lucid/libxpm/lucid

« back to all changes in this revision

Viewing changes to sxpm/sxpm.c

  • Committer: Bazaar Package Importer
  • Author(s): Daniel Stone
  • Date: 2005-07-23 01:33:31 UTC
  • Revision ID: james.westby@ubuntu.com-20050723013331-3azqgwnbdc7sjosd
Tags: upstream-3.5.2
ImportĀ upstreamĀ versionĀ 3.5.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 1989-95 GROUPE BULL
 
3
 *
 
4
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 
5
 * of this software and associated documentation files (the "Software"), to
 
6
 * deal in the Software without restriction, including without limitation the
 
7
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
 
8
 * sell copies of the Software, and to permit persons to whom the Software is
 
9
 * furnished to do so, subject to the following conditions:
 
10
 *
 
11
 * The above copyright notice and this permission notice shall be included in
 
12
 * all copies or substantial portions of the Software.
 
13
 *
 
14
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 
15
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 
16
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
 
17
 * GROUPE BULL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
 
18
 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 
19
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 
20
 *
 
21
 * Except as contained in this notice, the name of GROUPE BULL shall not be
 
22
 * used in advertising or otherwise to promote the sale, use or other dealings
 
23
 * in this Software without prior written authorization from GROUPE BULL.
 
24
 */
 
25
/* $XFree86: xc/extras/Xpm/sxpm/sxpm.c,v 1.2 2001/08/01 00:44:34 tsi Exp $ */
 
26
 
 
27
/*****************************************************************************\
 
28
* sxpm.c:                                                                     *
 
29
*                                                                             *
 
30
*  Show XPM File program                                                      *
 
31
*                                                                             *
 
32
*  Developed by Arnaud Le Hors                                                *
 
33
\*****************************************************************************/
 
34
 
 
35
#include <stdio.h>
 
36
#include <stdlib.h>
 
37
#include <X11/StringDefs.h>
 
38
#include <X11/Intrinsic.h>
 
39
#include <X11/IntrinsicP.h>
 
40
#include <X11/Shell.h>
 
41
 
 
42
#ifdef VMS
 
43
#include <X11/shape.h>
 
44
#else
 
45
#include <X11/extensions/shape.h>
 
46
#endif
 
47
 
 
48
#include <X11/xpm.h>
 
49
 
 
50
/* XPM */
 
51
/* plaid pixmap */
 
52
static char *plaid[] = {
 
53
    /* width height ncolors chars_per_pixel */
 
54
    "22 22 4 2 XPMEXT",
 
55
    /* colors */
 
56
    "   c red   m white  s light_color",
 
57
    "Y  c green m black  s lines_in_mix",
 
58
    "+  c yellow        m white  s lines_in_dark",
 
59
    "x          m black  s dark_color",
 
60
    /* pixels */
 
61
    "x   x   x x x   x   x x x x x x + x x x x x ",
 
62
    "  x   x   x   x   x   x x x x x x x x x x x ",
 
63
    "x   x   x x x   x   x x x x x x + x x x x x ",
 
64
    "  x   x   x   x   x   x x x x x x x x x x x ",
 
65
    "x   x   x x x   x   x x x x x x + x x x x x ",
 
66
    "Y Y Y Y Y x Y Y Y Y Y + x + x + x + x + x + ",
 
67
    "x   x   x x x   x   x x x x x x + x x x x x ",
 
68
    "  x   x   x   x   x   x x x x x x x x x x x ",
 
69
    "x   x   x x x   x   x x x x x x + x x x x x ",
 
70
    "  x   x   x   x   x   x x x x x x x x x x x ",
 
71
    "x   x   x x x   x   x x x x x x + x x x x x ",
 
72
    "          x           x   x   x Y x   x   x ",
 
73
    "          x             x   x   Y   x   x   ",
 
74
    "          x           x   x   x Y x   x   x ",
 
75
    "          x             x   x   Y   x   x   ",
 
76
    "          x           x   x   x Y x   x   x ",
 
77
    "x x x x x x x x x x x x x x x x x x x x x x ",
 
78
    "          x           x   x   x Y x   x   x ",
 
79
    "          x             x   x   Y   x   x   ",
 
80
    "          x           x   x   x Y x   x   x ",
 
81
    "          x             x   x   Y   x   x   ",
 
82
    "          x           x   x   x Y x   x   x ",
 
83
    "bullshit",
 
84
    "XPMEXT ext1 data1",
 
85
    "XPMEXT ext2",
 
86
    "data2_1",
 
87
    "data2_2",
 
88
    "XPMEXT",
 
89
    "foo",
 
90
    "",
 
91
    "XPMEXT ext3",
 
92
    "data3",
 
93
    "XPMENDEXT"
 
94
};
 
95
 
 
96
#define win XtWindow(topw)
 
97
#define dpy XtDisplay(topw)
 
98
#define root XRootWindowOfScreen(XtScreen(topw))
 
99
#define xrdb XtDatabase(dpy)
 
100
static Colormap colormap;
 
101
 
 
102
void Usage();
 
103
void ErrorMessage();
 
104
void Punt();
 
105
void VersionInfo();
 
106
 
 
107
#ifdef __STDC__
 
108
void kinput(Widget widget, char *tag, XEvent *xe, Boolean *b);
 
109
#else
 
110
void kinput();
 
111
#endif
 
112
 
 
113
#define IWIDTH      50
 
114
#define IHEIGHT     50
 
115
 
 
116
typedef struct _XpmIcon {
 
117
    Pixmap pixmap;
 
118
    Pixmap mask;
 
119
    XpmAttributes attributes;
 
120
}        XpmIcon;
 
121
 
 
122
static char **command;
 
123
static Widget topw;
 
124
static XpmIcon view, icon;
 
125
static XrmOptionDescRec options[] = {
 
126
    {"-hints", ".hints", XrmoptionNoArg, (XtPointer) "True"},
 
127
    {"-icon", ".icon", XrmoptionSepArg, (XtPointer) NULL},
 
128
};
 
129
 
 
130
int
 
131
main(argc, argv)
 
132
    int argc;
 
133
    char **argv;
 
134
{
 
135
    int ErrorStatus;
 
136
    unsigned int verbose = 0;           /* performs verbose output */
 
137
    unsigned int stdinf = 1;            /* read from stdin */
 
138
    unsigned int stdoutf = 0;           /* write to stdout */
 
139
    unsigned int nod = 0;               /* no display */
 
140
    unsigned int nom = 0;               /* no mask display */
 
141
    unsigned int incResize = 0;
 
142
    unsigned int resize = 0;
 
143
    unsigned int w_rtn;
 
144
    unsigned int h_rtn;
 
145
    char *input = NULL;
 
146
    char *output = NULL;
 
147
    char *iconFile = NULL;
 
148
    unsigned int numsymbols = 0;
 
149
    XpmColorSymbol symbols[10];
 
150
    char *stype;
 
151
    XrmValue val;
 
152
    unsigned long valuemask = 0;
 
153
    int n;
 
154
    Arg args[4];
 
155
 
 
156
#ifdef Debug
 
157
    char **data;
 
158
    char *buffer;
 
159
#endif
 
160
 
 
161
    topw = XtInitialize(argv[0], "Sxpm",
 
162
                        options, XtNumber(options), &argc, argv);
 
163
 
 
164
    if (!topw) {
 
165
        fprintf(stderr, "Sxpm Error... [ Undefined DISPLAY ]\n");
 
166
        exit(1);
 
167
    }
 
168
    colormap = XDefaultColormapOfScreen(XtScreen(topw));
 
169
 
 
170
    /*
 
171
     * geometry management
 
172
     */
 
173
 
 
174
    if (XrmGetResource(xrdb, NULL, "sxpm.geometry", &stype, &val)
 
175
        || XrmGetResource(xrdb, NULL, "Sxpm.geometry", &stype, &val)) {
 
176
 
 
177
        int flags;
 
178
        int x_rtn;
 
179
        int y_rtn;
 
180
        char *geo = NULL;
 
181
 
 
182
        geo = (char *) val.addr;
 
183
        flags = XParseGeometry(geo, &x_rtn, &y_rtn,
 
184
                               (unsigned int *) &w_rtn,
 
185
                               (unsigned int *) &h_rtn);
 
186
 
 
187
        if (!((WidthValue & flags) && (HeightValue & flags)))
 
188
            resize = 1;
 
189
 
 
190
    } else
 
191
        resize = 1;
 
192
 
 
193
    n = 0;
 
194
    if (resize) {
 
195
        w_rtn = 0;
 
196
        h_rtn = 0;
 
197
        XtSetArg(args[n], XtNwidth, 1);
 
198
        n++;
 
199
        XtSetArg(args[n], XtNheight, 1);
 
200
        n++;
 
201
    }
 
202
    XtSetArg(args[n], XtNmappedWhenManaged, False);
 
203
    n++;
 
204
    XtSetArg(args[n], XtNinput, True);
 
205
    n++;
 
206
    XtSetValues(topw, args, n);
 
207
 
 
208
    if ((XrmGetResource(xrdb, "sxpm.hints", "", &stype, &val)
 
209
         || XrmGetResource(xrdb, "Sxpm.hints", "", &stype, &val))
 
210
        && !strcmp((char *) val.addr, "True")) {
 
211
        /* gotcha */
 
212
        incResize = 1;
 
213
        resize = 1;
 
214
    }
 
215
 
 
216
    /*
 
217
     * icon management
 
218
     */
 
219
 
 
220
    if (XrmGetResource(xrdb, "sxpm.icon", "", &stype, &val)
 
221
        || XrmGetResource(xrdb, "Sxpm.icon", "", &stype, &val)) {
 
222
        iconFile = (char *) val.addr;
 
223
    }
 
224
    if (iconFile) {
 
225
 
 
226
        XColor color, junk;
 
227
        Pixel bpix;
 
228
        Window iconW;
 
229
 
 
230
        if (XAllocNamedColor(dpy, colormap, "black", &color, &junk))
 
231
            bpix = color.pixel;
 
232
        else
 
233
            bpix = XBlackPixelOfScreen(XtScreen(topw));
 
234
 
 
235
        iconW = XCreateSimpleWindow(dpy, root, 0, 0,
 
236
                                    IWIDTH, IHEIGHT, 1, bpix, bpix);
 
237
 
 
238
        icon.attributes.valuemask = XpmReturnAllocPixels;
 
239
        ErrorStatus = XpmReadFileToPixmap(dpy, root, iconFile, &icon.pixmap,
 
240
                                          &icon.mask, &icon.attributes);
 
241
        ErrorMessage(ErrorStatus, "Icon");
 
242
 
 
243
        XSetWindowBackgroundPixmap(dpy, iconW, icon.pixmap);
 
244
 
 
245
        n = 0;
 
246
        XtSetArg(args[n], XtNbackground, bpix);
 
247
        n++;
 
248
        XtSetArg(args[n], XtNiconWindow, iconW);
 
249
        n++;
 
250
        XtSetValues(topw, args, n);
 
251
    }
 
252
 
 
253
    /*
 
254
     * arguments parsing
 
255
     */
 
256
 
 
257
    command = argv;
 
258
    for (n = 1; n < argc; n++) {
 
259
        if (strcmp(argv[n], "-plaid") == 0) {
 
260
            stdinf = 0;
 
261
            continue;
 
262
        }
 
263
        if (argv[n][0] != '-') {
 
264
            stdinf = 0;
 
265
            input = argv[n];
 
266
            continue;
 
267
        }
 
268
        if ((strlen(argv[n]) == 1) && (argv[n][0] == '-'))
 
269
            /* stdin */
 
270
            continue;
 
271
        if (strcmp(argv[n], "-o") == 0) {
 
272
            if (n < argc - 1) {
 
273
                if ((strlen(argv[n + 1]) == 1) && (argv[n + 1][0] == '-'))
 
274
                    stdoutf = 1;
 
275
                else
 
276
                    output = argv[n + 1];
 
277
                n++;
 
278
                continue;
 
279
            } else
 
280
                Usage();
 
281
        }
 
282
        if (strcmp(argv[n], "-nod") == 0) {
 
283
            nod = 1;
 
284
            continue;
 
285
        }
 
286
        if (strcmp(argv[n], "-nom") == 0) {
 
287
            nom = 1;
 
288
            continue;
 
289
        }
 
290
        if (strcmp(argv[n], "-sc") == 0) {
 
291
            if (n < argc - 2) {
 
292
                valuemask |= XpmColorSymbols;
 
293
                symbols[numsymbols].name = argv[++n];
 
294
                symbols[numsymbols++].value = argv[++n];
 
295
                continue;
 
296
            } else
 
297
                Usage();
 
298
        }
 
299
        if (strcmp(argv[n], "-sp") == 0) {
 
300
            if (n < argc - 2) {
 
301
                valuemask |= XpmColorSymbols;
 
302
                symbols[numsymbols].name = argv[++n];
 
303
                symbols[numsymbols].value = NULL;
 
304
                symbols[numsymbols++].pixel = atol(argv[++n]);
 
305
                continue;
 
306
            }
 
307
        }
 
308
        if (strcmp(argv[n], "-cp") == 0) {
 
309
            if (n < argc - 2) {
 
310
                valuemask |= XpmColorSymbols;
 
311
                symbols[numsymbols].name = NULL;
 
312
                symbols[numsymbols].value = argv[++n];
 
313
                symbols[numsymbols++].pixel = atol(argv[++n]);
 
314
                continue;
 
315
            }
 
316
        }
 
317
        if (strcmp(argv[n], "-mono") == 0) {
 
318
            valuemask |= XpmColorKey;
 
319
            view.attributes.color_key = XPM_MONO;
 
320
            continue;
 
321
        }
 
322
        if (strcmp(argv[n], "-gray4") == 0 || strcmp(argv[n], "-grey4") == 0) {
 
323
            valuemask |= XpmColorKey;
 
324
            view.attributes.color_key = XPM_GRAY4;
 
325
            continue;
 
326
        }
 
327
        if (strcmp(argv[n], "-gray") == 0 || strcmp(argv[n], "-grey") == 0) {
 
328
            valuemask |= XpmColorKey;
 
329
            view.attributes.color_key = XPM_GRAY;
 
330
            continue;
 
331
        }
 
332
        if (strcmp(argv[n], "-color") == 0) {
 
333
            valuemask |= XpmColorKey;
 
334
            view.attributes.color_key = XPM_COLOR;
 
335
            continue;
 
336
        }
 
337
        if (strncmp(argv[n], "-closecolors", 6) == 0) {
 
338
            valuemask |= XpmCloseness;
 
339
            view.attributes.closeness = 40000;
 
340
            continue;
 
341
        }
 
342
        if (strcmp(argv[n], "-rgb") == 0) {
 
343
            if (n < argc - 1) {
 
344
                valuemask |= XpmRgbFilename;
 
345
                view.attributes.rgb_fname = argv[++n];
 
346
                continue;
 
347
            } else
 
348
                Usage();
 
349
 
 
350
        }
 
351
        if (strncmp(argv[n], "-version", 4) == 0) {
 
352
            VersionInfo();
 
353
            exit(0);
 
354
        }
 
355
        if (strcmp(argv[n], "-v") == 0) {
 
356
            verbose = 1;
 
357
            continue;
 
358
        }
 
359
        if (strcmp(argv[n], "-pcmap") == 0) {
 
360
            valuemask |= XpmColormap;
 
361
            continue;
 
362
        }
 
363
        Usage();
 
364
    }
 
365
 
 
366
    XtRealizeWidget(topw);
 
367
    if (valuemask & XpmColormap) {
 
368
        colormap = XCreateColormap(dpy, win,
 
369
                                   DefaultVisual(dpy, DefaultScreen(dpy)),
 
370
                                   AllocNone);
 
371
        view.attributes.colormap = colormap;
 
372
        XSetWindowColormap(dpy, win, colormap);
 
373
    }
 
374
    view.attributes.colorsymbols = symbols;
 
375
    view.attributes.numsymbols = numsymbols;
 
376
    view.attributes.valuemask = valuemask;
 
377
 
 
378
#ifdef Debug
 
379
    /* this is just to test the XpmCreateDataFromPixmap function */
 
380
 
 
381
    view.attributes.valuemask |= XpmReturnAllocPixels;
 
382
    view.attributes.valuemask |= XpmReturnExtensions;
 
383
    ErrorStatus = XpmCreatePixmapFromData(dpy, win, plaid,
 
384
                                          &view.pixmap, &view.mask,
 
385
                                          &view.attributes);
 
386
    ErrorMessage(ErrorStatus, "Plaid");
 
387
 
 
388
    ErrorStatus = XpmCreateDataFromPixmap(dpy, &data, view.pixmap, view.mask,
 
389
                                          &view.attributes);
 
390
    ErrorMessage(ErrorStatus, "Data");
 
391
    if (verbose && view.attributes.nextensions) {
 
392
        unsigned int i, j;
 
393
 
 
394
        for (i = 0; i < view.attributes.nextensions; i++) {
 
395
            fprintf(stderr, "Xpm extension : %s\n",
 
396
                    view.attributes.extensions[i].name);
 
397
            for (j = 0; j < view.attributes.extensions[i].nlines; j++)
 
398
                fprintf(stderr, "\t\t%s\n",
 
399
                        view.attributes.extensions[i].lines[j]);
 
400
        }
 
401
    }
 
402
    XFreePixmap(dpy, view.pixmap);
 
403
    if (view.mask)
 
404
        XFreePixmap(dpy, view.mask);
 
405
 
 
406
    XFreeColors(dpy, colormap,
 
407
                view.attributes.alloc_pixels,
 
408
                view.attributes.nalloc_pixels, 0);
 
409
 
 
410
    XpmFreeAttributes(&view.attributes);
 
411
    view.attributes.valuemask = valuemask;
 
412
#endif
 
413
 
 
414
    if (input || stdinf) {
 
415
        view.attributes.valuemask |= XpmReturnInfos;
 
416
        view.attributes.valuemask |= XpmReturnAllocPixels;
 
417
        view.attributes.valuemask |= XpmReturnExtensions;
 
418
 
 
419
#ifdef Debug
 
420
        XpmFree(data);
 
421
 
 
422
        /*
 
423
         * this is just to test the XpmCreatePixmapFromBuffer and
 
424
         * XpmCreateBufferFromPixmap functions
 
425
         */
 
426
        ErrorStatus = XpmReadFileToBuffer(input, &buffer);
 
427
        ErrorMessage(ErrorStatus, "CreateBufferFromFile");
 
428
 
 
429
        ErrorStatus = XpmCreatePixmapFromBuffer(dpy, win, buffer,
 
430
                                                &view.pixmap, &view.mask,
 
431
                                                &view.attributes);
 
432
        ErrorMessage(ErrorStatus, "CreatePixmapFromBuffer");
 
433
        XpmFree(buffer);
 
434
        ErrorStatus = XpmCreateBufferFromPixmap(dpy, &buffer,
 
435
                                                view.pixmap, view.mask,
 
436
                                                &view.attributes);
 
437
        ErrorMessage(ErrorStatus, "CreateBufferFromPixmap");
 
438
        ErrorStatus = XpmWriteFileFromBuffer("buffer_output", buffer);
 
439
        ErrorMessage(ErrorStatus, "WriteFileFromBuffer");
 
440
        XpmFree(buffer);
 
441
        if (view.pixmap) {
 
442
            XFreePixmap(dpy, view.pixmap);
 
443
            if (view.mask)
 
444
                XFreePixmap(dpy, view.mask);
 
445
 
 
446
            XFreeColors(dpy, colormap, view.attributes.alloc_pixels,
 
447
                        view.attributes.nalloc_pixels, 0);
 
448
 
 
449
            XpmFreeAttributes(&view.attributes);
 
450
        }
 
451
        ErrorStatus = XpmReadFileToData(input, &data);
 
452
        ErrorMessage(ErrorStatus, "ReadFileToData");
 
453
        ErrorStatus = XpmCreatePixmapFromData(dpy, win, data,
 
454
                                              &view.pixmap, &view.mask,
 
455
                                              &view.attributes);
 
456
        ErrorMessage(ErrorStatus, "CreatePixmapFromData");
 
457
        ErrorStatus = XpmWriteFileFromData("sxpmout.xpm", data);
 
458
        ErrorMessage(ErrorStatus, "WriteFileFromData");
 
459
        XpmFree(data);
 
460
        XpmFreeAttributes(&view.attributes);
 
461
#endif
 
462
        ErrorStatus = XpmReadFileToPixmap(dpy, win, input,
 
463
                                          &view.pixmap, &view.mask,
 
464
                                          &view.attributes);
 
465
        ErrorMessage(ErrorStatus, "Read");
 
466
        if (verbose && view.attributes.nextensions) {
 
467
            unsigned int i, j;
 
468
 
 
469
            for (i = 0; i < view.attributes.nextensions; i++) {
 
470
                fprintf(stderr, "Xpm extension : %s\n",
 
471
                        view.attributes.extensions[i].name);
 
472
                for (j = 0; j < view.attributes.extensions[i].nlines; j++)
 
473
                    fprintf(stderr, "\t\t%s\n",
 
474
                            view.attributes.extensions[i].lines[j]);
 
475
            }
 
476
        }
 
477
    } else {
 
478
#ifdef Debug
 
479
        ErrorStatus = XpmCreatePixmapFromData(dpy, win, data,
 
480
                                              &view.pixmap, &view.mask,
 
481
                                              &view.attributes);
 
482
        XpmFree(data);
 
483
#else
 
484
        ErrorStatus = XpmCreatePixmapFromData(dpy, win, plaid,
 
485
                                              &view.pixmap, &view.mask,
 
486
                                              &view.attributes);
 
487
#endif
 
488
        ErrorMessage(ErrorStatus, "Plaid");
 
489
    }
 
490
    if (output || stdoutf) {
 
491
        ErrorStatus = XpmWriteFileFromPixmap(dpy, output, view.pixmap,
 
492
                                             view.mask, &view.attributes);
 
493
        ErrorMessage(ErrorStatus, "Write");
 
494
    }
 
495
    if (!nod) {
 
496
 
 
497
        /*
 
498
         * manage display if requested
 
499
         */
 
500
 
 
501
        XSizeHints size_hints;
 
502
        char *xString = NULL;
 
503
 
 
504
        if (w_rtn && h_rtn
 
505
            && ((w_rtn < view.attributes.width)
 
506
                || h_rtn < view.attributes.height)) {
 
507
            resize = 1;
 
508
        }
 
509
        if (resize) {
 
510
            XtResizeWidget(topw,
 
511
                           view.attributes.width, view.attributes.height, 1);
 
512
        }
 
513
        if (incResize) {
 
514
            size_hints.flags = USSize | PMinSize | PResizeInc;
 
515
            size_hints.height = view.attributes.height;
 
516
            size_hints.width = view.attributes.width;
 
517
            size_hints.height_inc = view.attributes.height;
 
518
            size_hints.width_inc = view.attributes.width;
 
519
        } else
 
520
            size_hints.flags = PMinSize;
 
521
 
 
522
        size_hints.min_height = view.attributes.height;
 
523
        size_hints.min_width = view.attributes.width;
 
524
        XSetWMNormalHints(dpy, win, &size_hints);
 
525
 
 
526
        if (input) {
 
527
            xString = (char *) XtMalloc((sizeof(char) * strlen(input)) + 20);
 
528
            sprintf(xString, "Sxpm: %s", input);
 
529
            XStoreName(dpy, win, xString);
 
530
            XSetIconName(dpy, win, xString);
 
531
        } else if (stdinf) {
 
532
            XStoreName(dpy, win, "Sxpm: stdin");
 
533
            XSetIconName(dpy, win, "Sxpm: stdin");
 
534
        } else {
 
535
            XStoreName(dpy, win, "Sxpm");
 
536
            XSetIconName(dpy, win, "Sxpm");
 
537
        }
 
538
 
 
539
        XtAddEventHandler(topw, KeyPressMask, False,
 
540
                          (XtEventHandler) kinput, NULL);
 
541
        XSetWindowBackgroundPixmap(dpy, win, view.pixmap);
 
542
 
 
543
        if (view.mask && !nom)
 
544
            XShapeCombineMask(dpy, win, ShapeBounding, 0, 0,
 
545
                              view.mask, ShapeSet);
 
546
 
 
547
        XClearWindow(dpy, win);
 
548
        XtMapWidget(topw);
 
549
        if (xString)
 
550
            XtFree(xString);
 
551
        XtMainLoop();
 
552
    }
 
553
    Punt(0);
 
554
 
 
555
    /* Muffle gcc */
 
556
    return 0;
 
557
}
 
558
 
 
559
void
 
560
Usage()
 
561
{
 
562
    fprintf(stderr, "\nUsage:  %s [options...]\n", command[0]);
 
563
    fprintf(stderr, "Where options are:\n\
 
564
\n\
 
565
[-d host:display]            Display to connect to.\n\
 
566
[-g geom]                    Geometry of window.\n\
 
567
[-hints]                     Set ResizeInc for window.\n\
 
568
[-icon filename]             Set pixmap for iconWindow.\n\
 
569
[-plaid]                     Read the included plaid pixmap.\n\
 
570
[filename]                   Read from file 'filename', and from standard\n\
 
571
                             input if 'filename' is '-'.\n\
 
572
[-o filename]                Write to file 'filename', and to standard\n\
 
573
                             output if 'filename' is '-'.\n\
 
574
[-pcmap]                     Use a private colormap.\n\
 
575
[-closecolors]               Try to use `close' colors.\n\
 
576
[-nod]                       Don't display in window.\n\
 
577
[-nom]                       Don't use clip mask if any.\n\
 
578
[-mono]                      Use the colors specified for a monochrome visual.\n\
 
579
[-grey4]                     Use the colors specified for a 4 greyscale visual.\n\
 
580
[-grey]                      Use the colors specified for a greyscale visual.\n\
 
581
[-color]                     Use the colors specified for a color visual.\n\
 
582
[-sc symbol color]           Override color defaults.\n\
 
583
[-sp symbol pixel]           Override color defaults.\n\
 
584
[-cp color pixel]            Override color defaults.\n\
 
585
[-rgb filename]              Search color names in the rgb text file 'filename'.\n\
 
586
[-v]                         Verbose - print out extensions.\n\
 
587
[-version]                   Print out program's version number\n\
 
588
                             and library's version number if different.\n\
 
589
if no input is specified sxpm reads from standard input.\n\
 
590
\n");
 
591
    exit(0);
 
592
}
 
593
 
 
594
 
 
595
void
 
596
ErrorMessage(ErrorStatus, tag)
 
597
    int ErrorStatus;
 
598
    char *tag;
 
599
{
 
600
    char *error = NULL;
 
601
    char *warning = NULL;
 
602
 
 
603
    switch (ErrorStatus) {
 
604
    case XpmSuccess:
 
605
        return;
 
606
    case XpmColorError:
 
607
        warning = "Could not parse or alloc requested color";
 
608
        break;
 
609
    case XpmOpenFailed:
 
610
        error = "Cannot open file";
 
611
        break;
 
612
    case XpmFileInvalid:
 
613
        error = "Invalid XPM file";
 
614
        break;
 
615
    case XpmNoMemory:
 
616
        error = "Not enough memory";
 
617
        break;
 
618
    case XpmColorFailed:
 
619
        error = "Failed to parse or alloc some color";
 
620
        break;
 
621
    }
 
622
 
 
623
    if (warning)
 
624
        fprintf(stderr, "%s Xpm Warning: %s.\n", tag, warning);
 
625
 
 
626
    if (error) {
 
627
        fprintf(stderr, "%s Xpm Error: %s.\n", tag, error);
 
628
        Punt(1);
 
629
    }
 
630
}
 
631
 
 
632
void
 
633
Punt(i)
 
634
    int i;
 
635
{
 
636
    if (icon.pixmap) {
 
637
        XFreePixmap(dpy, icon.pixmap);
 
638
        if (icon.mask)
 
639
            XFreePixmap(dpy, icon.mask);
 
640
 
 
641
        XFreeColors(dpy, colormap,
 
642
                    icon.attributes.alloc_pixels,
 
643
                    icon.attributes.nalloc_pixels, 0);
 
644
 
 
645
        XpmFreeAttributes(&icon.attributes);
 
646
    }
 
647
    if (view.pixmap) {
 
648
        XFreePixmap(dpy, view.pixmap);
 
649
        if (view.mask)
 
650
            XFreePixmap(dpy, view.mask);
 
651
 
 
652
        XFreeColors(dpy, colormap,
 
653
                    view.attributes.alloc_pixels,
 
654
                    view.attributes.nalloc_pixels, 0);
 
655
 
 
656
        XpmFreeAttributes(&view.attributes);
 
657
    }
 
658
    exit(i);
 
659
}
 
660
 
 
661
void
 
662
kinput(widget, tag, xe, b)
 
663
    Widget widget;
 
664
    char *tag;
 
665
    XEvent *xe;
 
666
    Boolean *b;
 
667
{
 
668
    char c = '\0';
 
669
 
 
670
    XLookupString(&(xe->xkey), &c, 1, NULL, NULL);
 
671
    if (c == 'q' || c == 'Q')
 
672
        Punt(0);
 
673
}
 
674
 
 
675
/*
 
676
 * small function to extract various version numbers from the given global
 
677
 * number (following the rule described in xpm.h).
 
678
 */
 
679
void
 
680
GetNumbers(num, format_return, libmajor_return, libminor_return)
 
681
    int num;
 
682
    int *format_return;
 
683
    int *libmajor_return;
 
684
    char *libminor_return;
 
685
{
 
686
    *format_return = num / 10000;
 
687
    *libmajor_return = (num % 10000) / 100;
 
688
    *libminor_return = 'a' + (num % 10000) % 100 - 1;
 
689
}
 
690
 
 
691
void
 
692
VersionInfo()
 
693
{
 
694
    int format, libmajor;
 
695
    char libminor;
 
696
 
 
697
    GetNumbers(XpmIncludeVersion, &format, &libmajor, &libminor);
 
698
    fprintf(stderr, "sxpm version: %d.%d%c\n",
 
699
            format, libmajor, libminor);
 
700
    /*
 
701
     * if we are linked to an XPM library different from the one we've been
 
702
     * compiled with, print its own number too.
 
703
     */
 
704
    if (XpmIncludeVersion != XpmLibraryVersion()) {
 
705
        GetNumbers(XpmLibraryVersion(), &format, &libmajor, &libminor);
 
706
        fprintf(stderr, "using the XPM library version: %d.%d%c\n",
 
707
                format, libmajor, libminor);
 
708
    }
 
709
}