home *** CD-ROM | disk | FTP | other *** search
/ ftp.cs.arizona.edu / ftp.cs.arizona.edu.tar / ftp.cs.arizona.edu / icon / historic / v941.tgz / icon.v941src.tar / icon.v941src / src / xpm / sxpm.c < prev    next >
C/C++ Source or Header  |  2000-07-29  |  15KB  |  581 lines

  1. /* Copyright 1990-92 GROUPE BULL -- See license conditions in file COPYRIGHT */
  2. /*****************************************************************************\
  3. * sxpm.c:                                                                     *
  4. *                                                                             *
  5. *  Show XPM File program                                                      *
  6. *                                                                             *
  7. *  Developed by Arnaud Le Hors                                                *
  8. \*****************************************************************************/
  9.  
  10. #ifdef VMS
  11. #include "decw$include:Xlib.h"
  12. #include "decw$include:Intrinsic.h"
  13. #include "decw$include:Shell.h"
  14. #include "decw$include:shape.h"
  15. #else
  16. #include <X11/StringDefs.h>
  17. #include <X11/Intrinsic.h>
  18. #include <X11/Shell.h>
  19. #include <X11/extensions/shape.h>
  20. #endif
  21.  
  22. #include "xpm.h"
  23.  
  24. #ifdef Debug
  25. /* memory leak control tool */
  26. #include <mnemosyne.h>
  27. #endif
  28.  
  29. /* XPM */
  30. /* plaid pixmap */
  31. static char *plaid[] =
  32. {
  33. /* width height ncolors chars_per_pixel */
  34.  "22 22 4 2 XPMEXT",
  35. /* colors */
  36.  "   c red     m white  s light_color",
  37.  "Y  c green    m black  s lines_in_mix",
  38.  "+  c yellow    m white  s lines_in_dark",
  39.  "x         m black  s dark_color",
  40. /* pixels */
  41.  "x   x   x x x   x   x x x x x x + x x x x x ",
  42.  "  x   x   x   x   x   x x x x x x x x x x x ",
  43.  "x   x   x x x   x   x x x x x x + x x x x x ",
  44.  "  x   x   x   x   x   x x x x x x x x x x x ",
  45.  "x   x   x x x   x   x x x x x x + x x x x x ",
  46.  "Y Y Y Y Y x Y Y Y Y Y + x + x + x + x + x + ",
  47.  "x   x   x x x   x   x x x x x x + x x x x x ",
  48.  "  x   x   x   x   x   x x x x x x x x x x x ",
  49.  "x   x   x x x   x   x x x x x x + x x x x x ",
  50.  "  x   x   x   x   x   x x x x x x x x x x x ",
  51.  "x   x   x x x   x   x x x x x x + x x x x x ",
  52.  "          x           x   x   x Y x   x   x ",
  53.  "          x             x   x   Y   x   x   ",
  54.  "          x           x   x   x Y x   x   x ",
  55.  "          x             x   x   Y   x   x   ",
  56.  "          x           x   x   x Y x   x   x ",
  57.  "x x x x x x x x x x x x x x x x x x x x x x ",
  58.  "          x           x   x   x Y x   x   x ",
  59.  "          x             x   x   Y   x   x   ",
  60.  "          x           x   x   x Y x   x   x ",
  61.  "          x             x   x   Y   x   x   ",
  62.  "          x           x   x   x Y x   x   x ",
  63. "bullshit",
  64. "XPMEXT ext1 data1",
  65. "XPMEXT ext2",
  66. "data2_1",
  67. "data2_2",
  68. "XPMEXT",
  69. "foo",
  70. "",
  71. "XPMEXT ext3",
  72. "data3",
  73. "XPMENDEXT"
  74. };
  75.  
  76. #define win XtWindow(topw)
  77. #define dpy XtDisplay(topw)
  78. #define screen XtScreen(topw)
  79. #define root XRootWindowOfScreen(screen)
  80. #define xrdb XtDatabase(dpy)
  81. static Colormap colormap;
  82.  
  83. void Usage();
  84. void ErrorMessage();
  85. void Punt();
  86. void kinput();
  87.  
  88. #define IWIDTH      50
  89. #define IHEIGHT     50
  90.  
  91. typedef struct _XpmIcon {
  92.     Pixmap pixmap;
  93.     Pixmap mask;
  94.     XpmAttributes attributes;
  95. } XpmIcon;
  96.  
  97. static char **command;
  98. static Widget topw;
  99. static XpmIcon view, icon;
  100. static XrmOptionDescRec options[] = {
  101.     {"-hints", ".hints", XrmoptionNoArg, (XtPointer) "True"},
  102.     {"-icon", ".icon", XrmoptionSepArg, (XtPointer) NULL},
  103. };
  104.  
  105. main(argc, argv)
  106.     int argc;
  107.     char **argv;
  108. {
  109.     int ErrorStatus;
  110.     unsigned int verbose = 0;
  111.     unsigned int stdinf = 1;
  112.     unsigned int stdoutf = 0;
  113.     unsigned int nod = 0;
  114.     unsigned int incResize = 0;
  115.     unsigned int resize = 0;
  116.     unsigned int w_rtn;
  117.     unsigned int h_rtn;
  118.     char *input = NULL;
  119.     char *output = NULL;
  120.     char *iconFile = NULL;
  121.     unsigned int numsymbols = 0;
  122.     XpmColorSymbol symbols[10];
  123.     char *stype;
  124.     XrmValue val;
  125.     unsigned long valuemask = 0;
  126.     int n;
  127.     Arg args[3];
  128.  
  129. #ifdef Debug2
  130.     char **data;
  131.  
  132. #endif
  133.  
  134.     topw = XtInitialize(argv[0], "Sxpm",
  135.             options, XtNumber(options), &argc, argv);
  136.  
  137.     if (!topw) {
  138.     fprintf(stderr, "Sxpm Error... [ Undefined DISPLAY ]\n");
  139.     exit(1);
  140.     }
  141.  
  142.     colormap = XDefaultColormapOfScreen(screen);
  143.  
  144.     /*
  145.      * geometry management 
  146.      */
  147.  
  148.     if (XrmGetResource(xrdb, NULL, "sxpm.geometry", &stype, &val)
  149.     || XrmGetResource(xrdb, NULL, "Sxpm.geometry", &stype, &val)) {
  150.  
  151.     int flags;
  152.     int x_rtn;
  153.     int y_rtn;
  154.     char *geo = NULL;
  155.  
  156.     geo = (char *) val.addr;
  157.     flags = XParseGeometry(geo, &x_rtn, &y_rtn,
  158.                    (unsigned int *) &w_rtn,
  159.                    (unsigned int *) &h_rtn);
  160.     if (!((WidthValue & flags) && (HeightValue & flags)))
  161.         resize = 1;
  162.     } else
  163.     resize = 1;
  164.  
  165.     n = 0;
  166.     if (resize) {
  167.     w_rtn = 0;
  168.     h_rtn = 0;
  169.     XtSetArg(args[n], XtNwidth, 1);
  170.     n++;
  171.     XtSetArg(args[n], XtNheight, 1);
  172.     n++;
  173.     }
  174.     XtSetArg(args[n], XtNmappedWhenManaged, False);
  175.     n++;
  176.     XtSetValues(topw, args, n);
  177.  
  178.     if ((XrmGetResource(xrdb, "sxpm.hints", "", &stype, &val)
  179.      || XrmGetResource(xrdb, "Sxpm.hints", "", &stype, &val))
  180.     && !strcmp((char *) val.addr, "True")) {
  181.     /* gotcha */
  182.     incResize = 1;
  183.     resize = 1;
  184.     }
  185.  
  186.     /*
  187.      * icon management 
  188.      */
  189.  
  190.     if (XrmGetResource(xrdb, "sxpm.icon", "", &stype, &val) ||
  191.     XrmGetResource(xrdb, "Sxpm.icon", "", &stype, &val)) {
  192.     iconFile = (char *) val.addr;
  193.     }
  194.     if (iconFile) {
  195.  
  196.     XColor color, junk;
  197.     Pixel bpix;
  198.     Window iconW;
  199.  
  200.     if (XAllocNamedColor(dpy, colormap, "black", &color, &junk))
  201.         bpix = color.pixel;
  202.     else
  203.         bpix = XBlackPixelOfScreen(screen);
  204.  
  205.     iconW = XCreateSimpleWindow(dpy, root, 0, 0,
  206.                     IWIDTH, IHEIGHT, 1, bpix, bpix);
  207.  
  208.     icon.attributes.valuemask = XpmReturnPixels;
  209.     ErrorStatus = XpmReadFileToPixmap(dpy, root, iconFile, &icon.pixmap,
  210.                       &icon.mask, &icon.attributes);
  211.     ErrorMessage(ErrorStatus, "Icon");
  212.  
  213.     XSetWindowBackgroundPixmap(dpy, iconW, icon.pixmap);
  214.  
  215.     n = 0;
  216.     XtSetArg(args[n], XtNbackground, bpix);
  217.     n++;
  218.     XtSetArg(args[n], XtNiconWindow, iconW);
  219.     n++;
  220.     XtSetValues(topw, args, n);
  221.     }
  222.  
  223.     /*
  224.      * arguments parsing 
  225.      */
  226.  
  227.     command = argv;
  228.     for (n = 1; n < argc; n++) {
  229.     if (strncmp(argv[n], "-plaid", 3) == 0) {
  230.         stdinf = 0;
  231.         continue;
  232.     }
  233.     if (argv[n][0] != '-') {
  234.         stdinf = 0;
  235.         input = argv[n];
  236.         continue;
  237.     }
  238.     if ((strlen(argv[n]) == 1) && (argv[n][0] == '-'))
  239.         /* stdin */
  240.         continue;
  241.     if (strncmp(argv[n], "-o", 2) == 0) {
  242.         if (n < argc - 1) {
  243.         if ((strlen(argv[n + 1]) == 1) && (argv[n + 1][0] == '-'))
  244.             stdoutf = 1;
  245.         else
  246.             output = argv[n + 1];
  247.         n++;
  248.         continue;
  249.         } else
  250.         Usage();
  251.     }
  252.         if (strncmp(argv[n], "-nod", 2) == 0) {
  253.             nod = 1;
  254.             continue;
  255.         }
  256.     if (strncmp(argv[n], "-s", 2) == 0) {
  257.         if (n < argc - 2) {
  258.         valuemask |= XpmColorSymbols;
  259.         symbols[numsymbols].name = argv[++n];
  260.         symbols[numsymbols++].value = argv[++n];
  261.         continue;
  262.         } else
  263.         Usage();
  264.     }
  265.     if (strncmp(argv[n], "-p", 2) == 0) {
  266.         if (n < argc - 2) {
  267.         valuemask |= XpmColorSymbols;
  268.         symbols[numsymbols].name = argv[++n];
  269.         symbols[numsymbols].value = NULL;
  270.         symbols[numsymbols++].pixel = atol(argv[++n]);
  271.         continue;
  272.         }
  273.     }
  274.     if (strcmp(argv[n], "-closecolors") == 0) {
  275.         valuemask |= XpmCloseness;
  276.         view.attributes.closeness = 40000;
  277.         continue;
  278.     }
  279.     if (strncmp(argv[n], "-rgb", 3) == 0) {
  280.         if (n < argc - 1) {
  281.         valuemask |= XpmRgbFilename;
  282.         view.attributes.rgb_fname = argv[++n];
  283.         continue;
  284.         } else
  285.         Usage();
  286.  
  287.     }
  288.         if (strncmp(argv[n], "-v", 2) == 0) {
  289.             verbose = 1;
  290.             continue;
  291.         }
  292.         if (strncmp(argv[n], "-c", 2) == 0) {
  293.         valuemask |= XpmColormap;
  294.             continue;
  295.         }
  296.     Usage();
  297.     }
  298.  
  299.     XtRealizeWidget(topw);
  300.     if (valuemask & XpmColormap) {
  301.     colormap = XCreateColormap(dpy, win,
  302.                    DefaultVisual(dpy, DefaultScreen(dpy)),
  303.                    AllocNone);
  304.     view.attributes.colormap = colormap;
  305.     XSetWindowColormap(dpy, win, colormap);
  306.     }
  307.     view.attributes.colorsymbols = symbols;
  308.     view.attributes.numsymbols = numsymbols;
  309.     view.attributes.valuemask = valuemask;
  310.  
  311. #ifdef Debug2
  312.     /* this is just to test the XpmCreateDataFromPixmap function */
  313.  
  314.     view.attributes.valuemask |= XpmReturnPixels;
  315.     view.attributes.valuemask |= XpmReturnExtensions;
  316.     ErrorStatus = XpmCreatePixmapFromData(dpy, win, plaid,
  317.                       &view.pixmap, &view.mask,
  318.                       &view.attributes);
  319.     ErrorMessage(ErrorStatus, "Plaid");
  320.  
  321.     ErrorStatus = XpmCreateDataFromPixmap(dpy, &data, view.pixmap, view.mask,
  322.                       &view.attributes);
  323.     ErrorMessage(ErrorStatus, "Data");
  324.     if (verbose && view.attributes.nextensions) {
  325.     unsigned int i, j;
  326.     for (i = 0; i < view.attributes.nextensions; i++) {
  327.         fprintf(stderr, "Xpm extension : %s\n",
  328.             view.attributes.extensions[i].name);
  329.         for (j = 0; j < view.attributes.extensions[i].nlines; j++)
  330.         fprintf(stderr, "\t\t%s\n",
  331.             view.attributes.extensions[i].lines[j]);
  332.     }
  333.     }
  334.  
  335.     XFreePixmap(dpy, view.pixmap);
  336.     if (view.mask)
  337.     XFreePixmap(dpy, view.mask);
  338.  
  339.     XFreeColors(dpy, colormap,
  340.         view.attributes.pixels, view.attributes.npixels, 0);
  341.  
  342.     XpmFreeAttributes(&view.attributes);
  343.     view.attributes.valuemask = valuemask;
  344. #endif
  345.  
  346.     if (input || stdinf) {
  347.     view.attributes.valuemask |= XpmReturnInfos;
  348.     view.attributes.valuemask |= XpmReturnPixels;
  349.     view.attributes.valuemask |= XpmReturnExtensions;
  350.  
  351. #ifdef Debug2
  352.     free(data);
  353.  
  354.     ErrorStatus = XpmReadFileToData(input, &data);
  355.     ErrorMessage(ErrorStatus, "ReadFileToData");
  356.     ErrorStatus = XpmCreatePixmapFromData(dpy, win, data,
  357.                           &view.pixmap, &view.mask,
  358.                           &view.attributes);
  359.     ErrorMessage(ErrorStatus, "CreatePixmapFromData");
  360.     ErrorStatus = XpmWriteFileFromData("sxpmout.xpm", data);
  361.     ErrorMessage(ErrorStatus, "WriteFileFromData");
  362.     free(data);
  363. #endif
  364.     
  365.     ErrorStatus = XpmReadFileToPixmap(dpy, win, input,
  366.                       &view.pixmap, &view.mask,
  367.                       &view.attributes);
  368.     ErrorMessage(ErrorStatus, "Read");
  369.     if (verbose && view.attributes.nextensions) {
  370.         unsigned int i, j;
  371.         for (i = 0; i < view.attributes.nextensions; i++) {
  372.         fprintf(stderr, "Xpm extension : %s\n",
  373.                view.attributes.extensions[i].name);
  374.         for (j = 0; j < view.attributes.extensions[i].nlines; j++)
  375.             fprintf(stderr, "\t\t%s\n",
  376.                 view.attributes.extensions[i].lines[j]);
  377.         }
  378.     }
  379.     } else {
  380. #ifdef Debug2
  381.     ErrorStatus = XpmCreatePixmapFromData(dpy, win, data,
  382.                           &view.pixmap, &view.mask,
  383.                           &view.attributes);
  384.     free(data);
  385. #else
  386.     ErrorStatus = XpmCreatePixmapFromData(dpy, win, plaid,
  387.                           &view.pixmap, &view.mask,
  388.                           &view.attributes);
  389. #endif
  390.     ErrorMessage(ErrorStatus, "Plaid");
  391.     }
  392.     if (output || stdoutf) {
  393.     ErrorStatus = XpmWriteFileFromPixmap(dpy, output, view.pixmap,
  394.                          view.mask, &view.attributes);
  395.     ErrorMessage(ErrorStatus, "Write");
  396.     }
  397.     if (!nod) {
  398.  
  399.     /*
  400.      * manage display if requested 
  401.      */
  402.  
  403.     XSizeHints size_hints;
  404.     char *xString = NULL;
  405.  
  406.     if (w_rtn && h_rtn
  407.         && ((w_rtn < view.attributes.width)
  408.         || h_rtn < view.attributes.height)) {
  409.         resize = 1;
  410.     }
  411.     if (resize) {
  412.         XtResizeWidget(topw,
  413.              view.attributes.width, view.attributes.height, 1);
  414.     }
  415.     if (incResize) {
  416.         size_hints.flags = USSize | PMinSize | PResizeInc;
  417.         size_hints.height = view.attributes.height;
  418.         size_hints.width = view.attributes.width;
  419.         size_hints.height_inc = view.attributes.height;
  420.         size_hints.width_inc = view.attributes.width;
  421.     } else
  422.         size_hints.flags = PMinSize;
  423.  
  424.     size_hints.min_height = view.attributes.height;
  425.     size_hints.min_width = view.attributes.width;
  426.     XSetWMNormalHints(dpy, win, &size_hints);
  427.  
  428.     if (input) {
  429.         xString = (char *) XtMalloc((sizeof(char) * strlen(input)) + 20);
  430.         sprintf(xString, "Sxpm: %s\0", input);
  431.         XStoreName(dpy, XtWindow(topw), xString);
  432.         XSetIconName(dpy, XtWindow(topw), xString);
  433.     } else if (stdinf) {
  434.         XStoreName(dpy, XtWindow(topw), "Sxpm: stdin");
  435.         XSetIconName(dpy, XtWindow(topw), "Sxpm: stdin");
  436.     } else {
  437.         XStoreName(dpy, XtWindow(topw), "Sxpm");
  438.         XSetIconName(dpy, XtWindow(topw), "Sxpm");
  439.     }
  440.  
  441.     XtAddEventHandler(topw, KeyPressMask, False,
  442.               (XtEventHandler) kinput, NULL);
  443.     XSetWindowBackgroundPixmap(dpy, win, view.pixmap);
  444.  
  445.     if (view.mask)
  446.         XShapeCombineMask(dpy, win, ShapeBounding, 0, 0,
  447.                   view.mask, ShapeSet);
  448.  
  449.     XClearWindow(dpy, win);
  450.     XMapWindow(dpy, win);
  451.     if (xString)
  452.         XtFree(xString);
  453.     XtMainLoop();
  454.     }
  455.     Punt(0);
  456. }
  457.  
  458. void
  459. Usage()
  460. {
  461.     fprintf(stderr, "\nUsage:  %s [options...]\n", command[0]);
  462.     fprintf(stderr, "%s\n", "Where options are:");
  463.     fprintf(stderr, "\n%s\n",
  464.         "[-d host:display]            Display to connect to.");
  465.     fprintf(stderr, "%s\n",
  466.         "[-g geom]                    Geometry of window.");
  467.     fprintf(stderr, "%s\n",
  468.         "[-hints]                     Set ResizeInc for window.");
  469.     fprintf(stderr, "%s\n",
  470.         "[-icon filename]             Set pixmap for iconWindow.");
  471.     fprintf(stderr, "%s\n",
  472.         "[-s symbol_name color_name]  Overwrite color defaults.");
  473.     fprintf(stderr, "%s\n",
  474.         "[-p symbol_name pixel_value] Overwrite color defaults.");
  475.     fprintf(stderr, "%s\n",
  476.         "[-closecolors]               Try to use `close' colors.");
  477.     fprintf(stderr, "%s\n",
  478.         "[-plaid]                     Read the included plaid pixmap.");
  479.     fprintf(stderr, "%s\n",
  480.         "[filename]                   Read from file 'filename', and from \
  481. standard");
  482.     fprintf(stderr, "%s\n",
  483.         "                             input if 'filename' is '-'.");
  484.     fprintf(stderr, "%s\n",
  485.         "[-o filename]                Write to file 'filename', and to \
  486. standard");
  487.     fprintf(stderr, "%s\n",
  488.         "                             output if 'filename' is '-'.");
  489.     fprintf(stderr, "%s\n",
  490.         "[-nod]                       Don't display in window.");
  491.     fprintf(stderr, "%s\n",
  492.         "[-rgb filename]              Search color names in the \
  493. rgb text file 'filename'.");
  494.     fprintf(stderr, "%s\n",
  495.         "[-c]                         Use a private colormap.");
  496.     fprintf(stderr, "%s\n\n",
  497.         "[-v]                         Verbose - print out extensions.");
  498.     fprintf(stderr, "%s\n\n",
  499.         "if no input is specified sxpm reads from standard input.");
  500.     exit(0);
  501. }
  502.  
  503.  
  504. void
  505. ErrorMessage(ErrorStatus, tag)
  506.     int ErrorStatus;
  507.     char *tag;
  508. {
  509.     char *error = NULL;
  510.     char *warning = NULL;
  511.  
  512.     switch (ErrorStatus) {
  513.     case XpmSuccess:
  514.     return;
  515.     case XpmColorError:
  516.     warning = "Could not parse or alloc requested color";
  517.     break;
  518.     case XpmOpenFailed:
  519.     error = "Cannot open file";
  520.     break;
  521.     case XpmFileInvalid:
  522.     error = "Invalid XPM file";
  523.     break;
  524.     case XpmNoMemory:
  525.     error = "Not enough memory";
  526.     break;
  527.     case XpmColorFailed:
  528.     error = "Failed to parse or alloc some color";
  529.     break;
  530.     }
  531.  
  532.     if (warning)
  533.     printf("%s Xpm Warning: %s.\n", tag, warning);
  534.  
  535.     if (error) {
  536.     printf("%s Xpm Error: %s.\n", tag, error);
  537.     Punt(1);
  538.     }
  539. }
  540.  
  541. void
  542. Punt(i)
  543.     int i;
  544. {
  545.     if (icon.pixmap) {
  546.     XFreePixmap(dpy, icon.pixmap);
  547.     if (icon.mask)
  548.         XFreePixmap(dpy, icon.mask);
  549.  
  550.     XFreeColors(dpy, colormap,
  551.             icon.attributes.pixels, icon.attributes.npixels, 0);
  552.  
  553.     XpmFreeAttributes(&icon.attributes);
  554.     }
  555.     if (view.pixmap) {
  556.     XFreePixmap(dpy, view.pixmap);
  557.     if (view.mask)
  558.         XFreePixmap(dpy, view.mask);
  559.  
  560.     XFreeColors(dpy, colormap,
  561.             view.attributes.pixels, view.attributes.npixels, 0);
  562.  
  563.     XpmFreeAttributes(&view.attributes);
  564.     }
  565.     exit(i);
  566. }
  567.  
  568. void
  569. kinput(widget, tag, xe, b)
  570.     Widget widget;
  571.     char *tag;
  572.     XEvent *xe;
  573.     Boolean *b;
  574. {
  575.     char c = '\0';
  576.  
  577.     XLookupString(&(xe->xkey), &c, 1, NULL, NULL);
  578.     if (c == 'q' || c == 'Q')
  579.     Punt(0);
  580. }
  581.