home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / x / volume8 / xiconw / part01 / xseticonwindow.c < prev   
Encoding:
C/C++ Source or Header  |  1990-08-28  |  7.9 KB  |  295 lines

  1. /*
  2.   xseticonwindow.c - set the icon window of an application from a
  3.         bitmap or preexisting window.
  4.  */
  5. static char ego[] =  " Copyright (C) 1990 Robert Forsman <thoth@ufl.edu> \n";
  6. /*
  7.     This program is free software; you can redistribute it and/or modify
  8.     it under the terms of the GNU General Public License as published by
  9.     the Free Software Foundation; either version 1, or (at your option)
  10.     any later version.
  11.  
  12.     This program is distributed in the hope that it will be useful,
  13.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15.     GNU General Public License for more details.
  16.  
  17.     You should have received a copy of the GNU General Public License
  18.     along with this program; if not, write to the Free Software
  19.     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20.  
  21.     dsimple.c and dsimple.h are part of the *FREE* MIT sample X
  22.     windows server (R4) and are subject to their copyrights and license
  23.     agreements (similar to public domain).  The copyright from their
  24.     source distribution (mit/include/copyright.h) is included for your
  25.     reading convenience.
  26.  
  27.  */
  28.  
  29. #include <stdio.h>
  30. #include <signal.h>
  31. #include <X11/Xlib.h>
  32. #include <X11/Xutil.h>
  33.  
  34. #include "dsimple.h"
  35.  
  36. extern int errno;
  37. extern char *sys_errlist[];
  38. Window window;
  39.  
  40. usage()
  41. {
  42.     fprintf (stderr,
  43.     "usage:  %s [-options ...]\n\n", program_name);
  44.     fprintf (stderr,
  45.     "where options include:\n");
  46.     fprintf (stderr,
  47.     "    -help                print this message\n");
  48.     fprintf (stderr,
  49.     "    -display host:dpy    X server to contact\n");
  50.     fprintf (stderr,
  51.     "    -root                use the root window\n");
  52.     fprintf (stderr,
  53.     "    -id windowid         use the window with the specified id\n");
  54.     fprintf (stderr,
  55.     "    -name windowname     use the window with the specifed name\n");
  56.  
  57.     fprintf (stderr,
  58.     "    -frame               don't ignore window manager frames\n");
  59.     fprintf (stderr,
  60.     "    -bitmap              name of a bitmap file to use to build the icon\n");
  61.     fprintf (stderr,
  62.     "    -foreground          foreground color for bitmap icon\n");
  63.     fprintf (stderr,
  64.     "    -background          background color for bitmap icon\n");
  65.     fprintf (stderr,
  66.     "    -bordercolor         bordercolor for icon window\n");
  67.     fprintf (stderr,
  68.     "    -borderwidth         borderwidth for icon window\n");
  69.     fprintf (stderr,
  70.     "    -iconwindow          preexisting window to use as an icon\n");
  71.     fprintf (stderr, "\n");
  72.     exit (1);
  73. }
  74.  
  75. int done;
  76.  
  77. handlebreak() {
  78.   done = 1;
  79. }
  80.  
  81. long
  82. parse_color_or_die(string)
  83.      char *string;
  84. {
  85.   Colormap    cmap;
  86.   XColor    xc;
  87.  
  88.   cmap = DefaultColormap(dpy, screen);
  89.   if (!XParseColor(dpy, cmap, string, &xc))
  90.     {
  91.       fprintf(stderr, "color \"%s\" does not make sense\n", string);
  92.       exit(1);
  93.     }
  94.   if (!XAllocColor(dpy, cmap, &xc))
  95.     {
  96.       fprintf(stderr, "Unable to allocate color \"%s\", colormap probably full", string);
  97.       exit(1);
  98.     }
  99.   return xc.pixel;
  100. }
  101.  
  102. long
  103. parse_int_or_die(string)
  104.      char *string;
  105. {
  106.   long    l;
  107.   int    idx;
  108.   if (1!=sscanf(string, "%li%n", &l, &idx) || string[idx]!=0)
  109.     {
  110.       fprintf(stderr, "Unable to parse integer \"%s\"\n", string);
  111.       exit(1);
  112.     }
  113.   return l;
  114. }
  115.  
  116. main(argc,argv)
  117.      int argc;
  118.      char **argv;
  119. {
  120.   int    i,j;
  121.   int    frame;
  122.   Window    window, iconwindow;
  123.   XWMHints    *backup, ours;
  124.   char        *bitmapname;
  125.   Pixmap    stipple;
  126.   long    foreground, background, bordercolor;
  127.   int    borderwidth;
  128.  
  129.   INIT_NAME;
  130.   Setup_Display_And_Screen(&argc, argv);
  131.   window = Select_Window_Args(&argc, argv);
  132.  
  133.   frame = 0;
  134.   bitmapname = NULL;
  135.   foreground = WhitePixel(dpy,screen);
  136.   background = BlackPixel(dpy,screen);
  137.   bordercolor = BlackPixel(dpy,screen);
  138.   borderwidth = 1;
  139.   iconwindow = None;
  140.   for (i=1,j=1; i<argc; i++)
  141.     {
  142.       if (0==strcmp(argv[i],"-frame")) {
  143.     frame = 1;
  144.     continue;
  145.       }
  146.       if (0==strcmp(argv[i],"-bitmap")) {
  147.     if (++i >= argc)
  148.       usage();
  149.     bitmapname = argv[i];
  150.     continue;
  151.       }
  152.       if (0==strcmp(argv[i],"-foreground") || 0==strcmp(argv[i],"-fg")) {
  153.     if (++i >= argc)
  154.       usage();
  155.     foreground = parse_color_or_die(argv[i]);
  156.     continue;
  157.       }
  158.       if (0==strcmp(argv[i],"-background") || 0==strcmp(argv[i],"-bg")) {
  159.     if (++i >= argc)
  160.       usage();
  161.     background = parse_color_or_die(argv[i]);
  162.     continue;
  163.       }
  164.       if (0==strcmp(argv[i],"-bordercolor") || 0==strcmp(argv[i],"-bc")) {
  165.     if (++i >= argc)
  166.       usage();
  167.     bordercolor = parse_color_or_die(argv[i]);
  168.     continue;
  169.       }
  170.       if (0==strcmp(argv[i],"-borderwidth") || 0==strcmp(argv[i],"-bw")) {
  171.     if (++i >= argc)
  172.       usage();
  173.     borderwidth = parse_int_or_die(argv[i]);
  174.     continue;
  175.       }
  176.       if (0==strcmp(argv[i],"-iconwindow")) {
  177.     if (++i >= argc)
  178.       usage();
  179.     iconwindow = parse_int_or_die(argv[i]);
  180.     continue;
  181.       }
  182.       argv[j++] = argv[i];
  183.     }
  184.   if (j>1)
  185.     usage();
  186.   if (bitmapname==NULL && iconwindow==None)
  187.     {
  188.       fprintf(stderr,"I can not construct an icon window without a bitmap file name\n and you have not given me an icon window of your own.\n");
  189.       usage();
  190.     }
  191.  
  192.   if (!window) {
  193.     printf("\nxseticonwindow ==> Please select the window you would like\n");
  194.     printf("               ==> to set the iconwindow of by clicking the\n");
  195.     printf("               ==> mouse in that window.\n");
  196.     window = Select_Window(dpy);
  197.     if (window && !frame) {
  198.       Window root;
  199.       int dummyi;
  200.       unsigned int dummy;
  201.       
  202.       if (XGetGeometry (dpy, window, &root, &dummyi, &dummyi,
  203.             &dummy, &dummy, &dummy, &dummy) &&
  204.       window != root)
  205.     window = XmuClientWindow (dpy, window);
  206.     }
  207.   }
  208.  
  209.   if (iconwindow==None)
  210.     {
  211.       XGCValues xgcv;
  212.       GC    gc;
  213.       Pixmap    pmap;
  214.       unsigned int    width,height;
  215.       int    xhot,yhot;
  216.       int    rval;
  217.  
  218.       rval = XReadBitmapFile(dpy, DefaultRootWindow(dpy), bitmapname,
  219.                  &width, &height, &stipple, &xhot, &yhot);
  220.       switch (rval)
  221.     {
  222.     case BitmapOpenFailed:
  223.       fprintf(stderr,"Unable to open bitmap file %s (%s)\n", bitmapname,
  224.           sys_errlist[errno]);
  225.       exit(1); break;
  226.     case BitmapFileInvalid:
  227.       fprintf(stderr,"Bitmap file %s has bogus format\n", bitmapname);
  228.       exit(1); break;
  229.     case BitmapNoMemory:
  230.       fprintf(stderr,"Bitmap file %s ran out of memory\n", bitmapname);
  231.       exit(1); break;
  232.     case BitmapSuccess:
  233.       break;
  234.     default:
  235.       fprintf(stderr,"XReadBitmapFile returned strange code %d\n",rval);
  236.       break;
  237.     }
  238.       iconwindow = XCreateSimpleWindow
  239.     (dpy, DefaultRootWindow(dpy), 0, 0, width, height, borderwidth,
  240.      bordercolor, background);
  241.  
  242.       xgcv.stipple = stipple;
  243.       xgcv.foreground = foreground;
  244.       xgcv.background = background;
  245.       xgcv.fill_style = FillOpaqueStippled;
  246.       gc = XCreateGC(dpy, iconwindow,
  247.              GCStipple|GCForeground|GCBackground|GCFillStyle,
  248.              &xgcv);
  249.       pmap = XCreatePixmap(dpy, iconwindow, width, height,
  250.                DefaultDepth(dpy,screen));
  251.       XFillRectangle(dpy, pmap, gc, 0, 0, width, height);
  252.       XSetWindowBackgroundPixmap(dpy, iconwindow, pmap);
  253.       XFreePixmap(dpy, stipple);
  254.     }
  255.   XWithdrawWindow(dpy, window, screen);
  256.   backup = XGetWMHints(dpy,window);
  257.   if (backup==NULL)
  258.     {
  259.       fprintf(stderr, "Error getting window hints from window 0x%x\n",window);
  260.       exit(1);
  261.     }
  262.   bcopy(backup, &ours, sizeof(ours));
  263.   ours.flags |= IconWindowHint;
  264.   ours.flags &= ~(long)IconPixmapHint;
  265.   ours.icon_window = iconwindow;
  266.   XSetWMHints(dpy, window, &ours);
  267.   XMapWindow(dpy, window);
  268.   XSelectInput(dpy, window, StructureNotifyMask);
  269.  
  270.   signal(SIGHUP,handlebreak);
  271.   signal(SIGINT,handlebreak);
  272.   signal(SIGQUIT,handlebreak);
  273.   signal(SIGTERM,handlebreak);
  274.  
  275.   done = 0;
  276.   while (!done)
  277.     {
  278.       static XEvent    xev;
  279.       XNextEvent(dpy,&xev);
  280.       if (xev.type==DestroyNotify)
  281.     exit(0);
  282.     }
  283.   XWithdrawWindow(dpy, window, screen);
  284.   XSetWMHints(dpy, window, backup);
  285.   XMapWindow(dpy, window);
  286.   XFlush(dpy);
  287.   sleep (3);
  288.   /* I don't know why I need this other pair, but TWM freaks and unmanages
  289.      the window if I don't */
  290.   XWithdrawWindow(dpy, window, screen);
  291.   XMapWindow(dpy, window);
  292.  
  293.   XCloseDisplay(dpy);
  294. }
  295.