home *** CD-ROM | disk | FTP | other *** search
- /*
- xseticonwindow.c - set the icon window of an application from a
- bitmap or preexisting window.
- */
- static char ego[] = " Copyright (C) 1990 Robert Forsman <thoth@ufl.edu> \n";
- /*
- This program is free software; you can redistribute it and/or modify
- it under the terms of the GNU General Public License as published by
- the Free Software Foundation; either version 1, or (at your option)
- any later version.
-
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
-
- You should have received a copy of the GNU General Public License
- along with this program; if not, write to the Free Software
- Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
-
- dsimple.c and dsimple.h are part of the *FREE* MIT sample X
- windows server (R4) and are subject to their copyrights and license
- agreements (similar to public domain). The copyright from their
- source distribution (mit/include/copyright.h) is included for your
- reading convenience.
-
- */
-
- #include <stdio.h>
- #include <signal.h>
- #include <X11/Xlib.h>
- #include <X11/Xutil.h>
-
- #include "dsimple.h"
-
- extern int errno;
- extern char *sys_errlist[];
- Window window;
-
- usage()
- {
- fprintf (stderr,
- "usage: %s [-options ...]\n\n", program_name);
- fprintf (stderr,
- "where options include:\n");
- fprintf (stderr,
- " -help print this message\n");
- fprintf (stderr,
- " -display host:dpy X server to contact\n");
- fprintf (stderr,
- " -root use the root window\n");
- fprintf (stderr,
- " -id windowid use the window with the specified id\n");
- fprintf (stderr,
- " -name windowname use the window with the specifed name\n");
-
- fprintf (stderr,
- " -frame don't ignore window manager frames\n");
- fprintf (stderr,
- " -bitmap name of a bitmap file to use to build the icon\n");
- fprintf (stderr,
- " -foreground foreground color for bitmap icon\n");
- fprintf (stderr,
- " -background background color for bitmap icon\n");
- fprintf (stderr,
- " -bordercolor bordercolor for icon window\n");
- fprintf (stderr,
- " -borderwidth borderwidth for icon window\n");
- fprintf (stderr,
- " -iconwindow preexisting window to use as an icon\n");
- fprintf (stderr, "\n");
- exit (1);
- }
-
- int done;
-
- handlebreak() {
- done = 1;
- }
-
- long
- parse_color_or_die(string)
- char *string;
- {
- Colormap cmap;
- XColor xc;
-
- cmap = DefaultColormap(dpy, screen);
- if (!XParseColor(dpy, cmap, string, &xc))
- {
- fprintf(stderr, "color \"%s\" does not make sense\n", string);
- exit(1);
- }
- if (!XAllocColor(dpy, cmap, &xc))
- {
- fprintf(stderr, "Unable to allocate color \"%s\", colormap probably full", string);
- exit(1);
- }
- return xc.pixel;
- }
-
- long
- parse_int_or_die(string)
- char *string;
- {
- long l;
- int idx;
- if (1!=sscanf(string, "%li%n", &l, &idx) || string[idx]!=0)
- {
- fprintf(stderr, "Unable to parse integer \"%s\"\n", string);
- exit(1);
- }
- return l;
- }
-
- main(argc,argv)
- int argc;
- char **argv;
- {
- int i,j;
- int frame;
- Window window, iconwindow;
- XWMHints *backup, ours;
- char *bitmapname;
- Pixmap stipple;
- long foreground, background, bordercolor;
- int borderwidth;
-
- INIT_NAME;
- Setup_Display_And_Screen(&argc, argv);
- window = Select_Window_Args(&argc, argv);
-
- frame = 0;
- bitmapname = NULL;
- foreground = WhitePixel(dpy,screen);
- background = BlackPixel(dpy,screen);
- bordercolor = BlackPixel(dpy,screen);
- borderwidth = 1;
- iconwindow = None;
- for (i=1,j=1; i<argc; i++)
- {
- if (0==strcmp(argv[i],"-frame")) {
- frame = 1;
- continue;
- }
- if (0==strcmp(argv[i],"-bitmap")) {
- if (++i >= argc)
- usage();
- bitmapname = argv[i];
- continue;
- }
- if (0==strcmp(argv[i],"-foreground") || 0==strcmp(argv[i],"-fg")) {
- if (++i >= argc)
- usage();
- foreground = parse_color_or_die(argv[i]);
- continue;
- }
- if (0==strcmp(argv[i],"-background") || 0==strcmp(argv[i],"-bg")) {
- if (++i >= argc)
- usage();
- background = parse_color_or_die(argv[i]);
- continue;
- }
- if (0==strcmp(argv[i],"-bordercolor") || 0==strcmp(argv[i],"-bc")) {
- if (++i >= argc)
- usage();
- bordercolor = parse_color_or_die(argv[i]);
- continue;
- }
- if (0==strcmp(argv[i],"-borderwidth") || 0==strcmp(argv[i],"-bw")) {
- if (++i >= argc)
- usage();
- borderwidth = parse_int_or_die(argv[i]);
- continue;
- }
- if (0==strcmp(argv[i],"-iconwindow")) {
- if (++i >= argc)
- usage();
- iconwindow = parse_int_or_die(argv[i]);
- continue;
- }
- argv[j++] = argv[i];
- }
- if (j>1)
- usage();
- if (bitmapname==NULL && iconwindow==None)
- {
- 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");
- usage();
- }
-
- if (!window) {
- printf("\nxseticonwindow ==> Please select the window you would like\n");
- printf(" ==> to set the iconwindow of by clicking the\n");
- printf(" ==> mouse in that window.\n");
- window = Select_Window(dpy);
- if (window && !frame) {
- Window root;
- int dummyi;
- unsigned int dummy;
-
- if (XGetGeometry (dpy, window, &root, &dummyi, &dummyi,
- &dummy, &dummy, &dummy, &dummy) &&
- window != root)
- window = XmuClientWindow (dpy, window);
- }
- }
-
- if (iconwindow==None)
- {
- XGCValues xgcv;
- GC gc;
- Pixmap pmap;
- unsigned int width,height;
- int xhot,yhot;
- int rval;
-
- rval = XReadBitmapFile(dpy, DefaultRootWindow(dpy), bitmapname,
- &width, &height, &stipple, &xhot, &yhot);
- switch (rval)
- {
- case BitmapOpenFailed:
- fprintf(stderr,"Unable to open bitmap file %s (%s)\n", bitmapname,
- sys_errlist[errno]);
- exit(1); break;
- case BitmapFileInvalid:
- fprintf(stderr,"Bitmap file %s has bogus format\n", bitmapname);
- exit(1); break;
- case BitmapNoMemory:
- fprintf(stderr,"Bitmap file %s ran out of memory\n", bitmapname);
- exit(1); break;
- case BitmapSuccess:
- break;
- default:
- fprintf(stderr,"XReadBitmapFile returned strange code %d\n",rval);
- break;
- }
- iconwindow = XCreateSimpleWindow
- (dpy, DefaultRootWindow(dpy), 0, 0, width, height, borderwidth,
- bordercolor, background);
-
- xgcv.stipple = stipple;
- xgcv.foreground = foreground;
- xgcv.background = background;
- xgcv.fill_style = FillOpaqueStippled;
- gc = XCreateGC(dpy, iconwindow,
- GCStipple|GCForeground|GCBackground|GCFillStyle,
- &xgcv);
- pmap = XCreatePixmap(dpy, iconwindow, width, height,
- DefaultDepth(dpy,screen));
- XFillRectangle(dpy, pmap, gc, 0, 0, width, height);
- XSetWindowBackgroundPixmap(dpy, iconwindow, pmap);
- XFreePixmap(dpy, stipple);
- }
- XWithdrawWindow(dpy, window, screen);
- backup = XGetWMHints(dpy,window);
- if (backup==NULL)
- {
- fprintf(stderr, "Error getting window hints from window 0x%x\n",window);
- exit(1);
- }
- bcopy(backup, &ours, sizeof(ours));
- ours.flags |= IconWindowHint;
- ours.flags &= ~(long)IconPixmapHint;
- ours.icon_window = iconwindow;
- XSetWMHints(dpy, window, &ours);
- XMapWindow(dpy, window);
- XSelectInput(dpy, window, StructureNotifyMask);
-
- signal(SIGHUP,handlebreak);
- signal(SIGINT,handlebreak);
- signal(SIGQUIT,handlebreak);
- signal(SIGTERM,handlebreak);
-
- done = 0;
- while (!done)
- {
- static XEvent xev;
- XNextEvent(dpy,&xev);
- if (xev.type==DestroyNotify)
- exit(0);
- }
- XWithdrawWindow(dpy, window, screen);
- XSetWMHints(dpy, window, backup);
- XMapWindow(dpy, window);
- XFlush(dpy);
- sleep (3);
- /* I don't know why I need this other pair, but TWM freaks and unmanages
- the window if I don't */
- XWithdrawWindow(dpy, window, screen);
- XMapWindow(dpy, window);
-
- XCloseDisplay(dpy);
- }
-