2
* Copyright (C) 1989-95 GROUPE BULL
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:
11
* The above copyright notice and this permission notice shall be included in
12
* all copies or substantial portions of the Software.
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.
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.
25
/* $XFree86: xc/extras/Xpm/sxpm/sxpm.c,v 1.2 2001/08/01 00:44:34 tsi Exp $ */
27
/*****************************************************************************\
30
* Show XPM File program *
32
* Developed by Arnaud Le Hors *
33
\*****************************************************************************/
37
#include <X11/StringDefs.h>
38
#include <X11/Intrinsic.h>
39
#include <X11/IntrinsicP.h>
40
#include <X11/Shell.h>
43
#include <X11/shape.h>
45
#include <X11/extensions/shape.h>
52
static char *plaid[] = {
53
/* width height ncolors chars_per_pixel */
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",
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 ",
77
"x x x x x x x x x x x x x x x x x x x x x x ",
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;
108
void kinput(Widget widget, char *tag, XEvent *xe, Boolean *b);
116
typedef struct _XpmIcon {
119
XpmAttributes attributes;
122
static char **command;
124
static XpmIcon view, icon;
125
static XrmOptionDescRec options[] = {
126
{"-hints", ".hints", XrmoptionNoArg, (XtPointer) "True"},
127
{"-icon", ".icon", XrmoptionSepArg, (XtPointer) NULL},
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;
147
char *iconFile = NULL;
148
unsigned int numsymbols = 0;
149
XpmColorSymbol symbols[10];
152
unsigned long valuemask = 0;
161
topw = XtInitialize(argv[0], "Sxpm",
162
options, XtNumber(options), &argc, argv);
165
fprintf(stderr, "Sxpm Error... [ Undefined DISPLAY ]\n");
168
colormap = XDefaultColormapOfScreen(XtScreen(topw));
171
* geometry management
174
if (XrmGetResource(xrdb, NULL, "sxpm.geometry", &stype, &val)
175
|| XrmGetResource(xrdb, NULL, "Sxpm.geometry", &stype, &val)) {
182
geo = (char *) val.addr;
183
flags = XParseGeometry(geo, &x_rtn, &y_rtn,
184
(unsigned int *) &w_rtn,
185
(unsigned int *) &h_rtn);
187
if (!((WidthValue & flags) && (HeightValue & flags)))
197
XtSetArg(args[n], XtNwidth, 1);
199
XtSetArg(args[n], XtNheight, 1);
202
XtSetArg(args[n], XtNmappedWhenManaged, False);
204
XtSetArg(args[n], XtNinput, True);
206
XtSetValues(topw, args, n);
208
if ((XrmGetResource(xrdb, "sxpm.hints", "", &stype, &val)
209
|| XrmGetResource(xrdb, "Sxpm.hints", "", &stype, &val))
210
&& !strcmp((char *) val.addr, "True")) {
220
if (XrmGetResource(xrdb, "sxpm.icon", "", &stype, &val)
221
|| XrmGetResource(xrdb, "Sxpm.icon", "", &stype, &val)) {
222
iconFile = (char *) val.addr;
230
if (XAllocNamedColor(dpy, colormap, "black", &color, &junk))
233
bpix = XBlackPixelOfScreen(XtScreen(topw));
235
iconW = XCreateSimpleWindow(dpy, root, 0, 0,
236
IWIDTH, IHEIGHT, 1, bpix, bpix);
238
icon.attributes.valuemask = XpmReturnAllocPixels;
239
ErrorStatus = XpmReadFileToPixmap(dpy, root, iconFile, &icon.pixmap,
240
&icon.mask, &icon.attributes);
241
ErrorMessage(ErrorStatus, "Icon");
243
XSetWindowBackgroundPixmap(dpy, iconW, icon.pixmap);
246
XtSetArg(args[n], XtNbackground, bpix);
248
XtSetArg(args[n], XtNiconWindow, iconW);
250
XtSetValues(topw, args, n);
258
for (n = 1; n < argc; n++) {
259
if (strcmp(argv[n], "-plaid") == 0) {
263
if (argv[n][0] != '-') {
268
if ((strlen(argv[n]) == 1) && (argv[n][0] == '-'))
271
if (strcmp(argv[n], "-o") == 0) {
273
if ((strlen(argv[n + 1]) == 1) && (argv[n + 1][0] == '-'))
276
output = argv[n + 1];
282
if (strcmp(argv[n], "-nod") == 0) {
286
if (strcmp(argv[n], "-nom") == 0) {
290
if (strcmp(argv[n], "-sc") == 0) {
292
valuemask |= XpmColorSymbols;
293
symbols[numsymbols].name = argv[++n];
294
symbols[numsymbols++].value = argv[++n];
299
if (strcmp(argv[n], "-sp") == 0) {
301
valuemask |= XpmColorSymbols;
302
symbols[numsymbols].name = argv[++n];
303
symbols[numsymbols].value = NULL;
304
symbols[numsymbols++].pixel = atol(argv[++n]);
308
if (strcmp(argv[n], "-cp") == 0) {
310
valuemask |= XpmColorSymbols;
311
symbols[numsymbols].name = NULL;
312
symbols[numsymbols].value = argv[++n];
313
symbols[numsymbols++].pixel = atol(argv[++n]);
317
if (strcmp(argv[n], "-mono") == 0) {
318
valuemask |= XpmColorKey;
319
view.attributes.color_key = XPM_MONO;
322
if (strcmp(argv[n], "-gray4") == 0 || strcmp(argv[n], "-grey4") == 0) {
323
valuemask |= XpmColorKey;
324
view.attributes.color_key = XPM_GRAY4;
327
if (strcmp(argv[n], "-gray") == 0 || strcmp(argv[n], "-grey") == 0) {
328
valuemask |= XpmColorKey;
329
view.attributes.color_key = XPM_GRAY;
332
if (strcmp(argv[n], "-color") == 0) {
333
valuemask |= XpmColorKey;
334
view.attributes.color_key = XPM_COLOR;
337
if (strncmp(argv[n], "-closecolors", 6) == 0) {
338
valuemask |= XpmCloseness;
339
view.attributes.closeness = 40000;
342
if (strcmp(argv[n], "-rgb") == 0) {
344
valuemask |= XpmRgbFilename;
345
view.attributes.rgb_fname = argv[++n];
351
if (strncmp(argv[n], "-version", 4) == 0) {
355
if (strcmp(argv[n], "-v") == 0) {
359
if (strcmp(argv[n], "-pcmap") == 0) {
360
valuemask |= XpmColormap;
366
XtRealizeWidget(topw);
367
if (valuemask & XpmColormap) {
368
colormap = XCreateColormap(dpy, win,
369
DefaultVisual(dpy, DefaultScreen(dpy)),
371
view.attributes.colormap = colormap;
372
XSetWindowColormap(dpy, win, colormap);
374
view.attributes.colorsymbols = symbols;
375
view.attributes.numsymbols = numsymbols;
376
view.attributes.valuemask = valuemask;
379
/* this is just to test the XpmCreateDataFromPixmap function */
381
view.attributes.valuemask |= XpmReturnAllocPixels;
382
view.attributes.valuemask |= XpmReturnExtensions;
383
ErrorStatus = XpmCreatePixmapFromData(dpy, win, plaid,
384
&view.pixmap, &view.mask,
386
ErrorMessage(ErrorStatus, "Plaid");
388
ErrorStatus = XpmCreateDataFromPixmap(dpy, &data, view.pixmap, view.mask,
390
ErrorMessage(ErrorStatus, "Data");
391
if (verbose && view.attributes.nextensions) {
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]);
402
XFreePixmap(dpy, view.pixmap);
404
XFreePixmap(dpy, view.mask);
406
XFreeColors(dpy, colormap,
407
view.attributes.alloc_pixels,
408
view.attributes.nalloc_pixels, 0);
410
XpmFreeAttributes(&view.attributes);
411
view.attributes.valuemask = valuemask;
414
if (input || stdinf) {
415
view.attributes.valuemask |= XpmReturnInfos;
416
view.attributes.valuemask |= XpmReturnAllocPixels;
417
view.attributes.valuemask |= XpmReturnExtensions;
423
* this is just to test the XpmCreatePixmapFromBuffer and
424
* XpmCreateBufferFromPixmap functions
426
ErrorStatus = XpmReadFileToBuffer(input, &buffer);
427
ErrorMessage(ErrorStatus, "CreateBufferFromFile");
429
ErrorStatus = XpmCreatePixmapFromBuffer(dpy, win, buffer,
430
&view.pixmap, &view.mask,
432
ErrorMessage(ErrorStatus, "CreatePixmapFromBuffer");
434
ErrorStatus = XpmCreateBufferFromPixmap(dpy, &buffer,
435
view.pixmap, view.mask,
437
ErrorMessage(ErrorStatus, "CreateBufferFromPixmap");
438
ErrorStatus = XpmWriteFileFromBuffer("buffer_output", buffer);
439
ErrorMessage(ErrorStatus, "WriteFileFromBuffer");
442
XFreePixmap(dpy, view.pixmap);
444
XFreePixmap(dpy, view.mask);
446
XFreeColors(dpy, colormap, view.attributes.alloc_pixels,
447
view.attributes.nalloc_pixels, 0);
449
XpmFreeAttributes(&view.attributes);
451
ErrorStatus = XpmReadFileToData(input, &data);
452
ErrorMessage(ErrorStatus, "ReadFileToData");
453
ErrorStatus = XpmCreatePixmapFromData(dpy, win, data,
454
&view.pixmap, &view.mask,
456
ErrorMessage(ErrorStatus, "CreatePixmapFromData");
457
ErrorStatus = XpmWriteFileFromData("sxpmout.xpm", data);
458
ErrorMessage(ErrorStatus, "WriteFileFromData");
460
XpmFreeAttributes(&view.attributes);
462
ErrorStatus = XpmReadFileToPixmap(dpy, win, input,
463
&view.pixmap, &view.mask,
465
ErrorMessage(ErrorStatus, "Read");
466
if (verbose && view.attributes.nextensions) {
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]);
479
ErrorStatus = XpmCreatePixmapFromData(dpy, win, data,
480
&view.pixmap, &view.mask,
484
ErrorStatus = XpmCreatePixmapFromData(dpy, win, plaid,
485
&view.pixmap, &view.mask,
488
ErrorMessage(ErrorStatus, "Plaid");
490
if (output || stdoutf) {
491
ErrorStatus = XpmWriteFileFromPixmap(dpy, output, view.pixmap,
492
view.mask, &view.attributes);
493
ErrorMessage(ErrorStatus, "Write");
498
* manage display if requested
501
XSizeHints size_hints;
502
char *xString = NULL;
505
&& ((w_rtn < view.attributes.width)
506
|| h_rtn < view.attributes.height)) {
511
view.attributes.width, view.attributes.height, 1);
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;
520
size_hints.flags = PMinSize;
522
size_hints.min_height = view.attributes.height;
523
size_hints.min_width = view.attributes.width;
524
XSetWMNormalHints(dpy, win, &size_hints);
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);
532
XStoreName(dpy, win, "Sxpm: stdin");
533
XSetIconName(dpy, win, "Sxpm: stdin");
535
XStoreName(dpy, win, "Sxpm");
536
XSetIconName(dpy, win, "Sxpm");
539
XtAddEventHandler(topw, KeyPressMask, False,
540
(XtEventHandler) kinput, NULL);
541
XSetWindowBackgroundPixmap(dpy, win, view.pixmap);
543
if (view.mask && !nom)
544
XShapeCombineMask(dpy, win, ShapeBounding, 0, 0,
545
view.mask, ShapeSet);
547
XClearWindow(dpy, win);
562
fprintf(stderr, "\nUsage: %s [options...]\n", command[0]);
563
fprintf(stderr, "Where options are:\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\
596
ErrorMessage(ErrorStatus, tag)
601
char *warning = NULL;
603
switch (ErrorStatus) {
607
warning = "Could not parse or alloc requested color";
610
error = "Cannot open file";
613
error = "Invalid XPM file";
616
error = "Not enough memory";
619
error = "Failed to parse or alloc some color";
624
fprintf(stderr, "%s Xpm Warning: %s.\n", tag, warning);
627
fprintf(stderr, "%s Xpm Error: %s.\n", tag, error);
637
XFreePixmap(dpy, icon.pixmap);
639
XFreePixmap(dpy, icon.mask);
641
XFreeColors(dpy, colormap,
642
icon.attributes.alloc_pixels,
643
icon.attributes.nalloc_pixels, 0);
645
XpmFreeAttributes(&icon.attributes);
648
XFreePixmap(dpy, view.pixmap);
650
XFreePixmap(dpy, view.mask);
652
XFreeColors(dpy, colormap,
653
view.attributes.alloc_pixels,
654
view.attributes.nalloc_pixels, 0);
656
XpmFreeAttributes(&view.attributes);
662
kinput(widget, tag, xe, b)
670
XLookupString(&(xe->xkey), &c, 1, NULL, NULL);
671
if (c == 'q' || c == 'Q')
676
* small function to extract various version numbers from the given global
677
* number (following the rule described in xpm.h).
680
GetNumbers(num, format_return, libmajor_return, libminor_return)
683
int *libmajor_return;
684
char *libminor_return;
686
*format_return = num / 10000;
687
*libmajor_return = (num % 10000) / 100;
688
*libminor_return = 'a' + (num % 10000) % 100 - 1;
694
int format, libmajor;
697
GetNumbers(XpmIncludeVersion, &format, &libmajor, &libminor);
698
fprintf(stderr, "sxpm version: %d.%d%c\n",
699
format, libmajor, libminor);
701
* if we are linked to an XPM library different from the one we've been
702
* compiled with, print its own number too.
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);