/* * * wmGrabImage-1.00 (C) 1999 Mike Henderson (mghenderson@lanl.gov) * * - Monitors an image on the WWW in a WindowMaker DockApp * * * * * 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 2, 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 (see the file COPYING); if not, write to the * Free Software Foundation, Inc., 59 Temple Place - Suite 330, * Boston, MA 02111-1307, USA * * * Platforms tested on: Linux, Solaris 2.6, IRIX 6.5 * * * ToDo: Add inicator to show if image is uptodate or not * Get rid of GrabImage and put all that stuff in wmGrabImage.c * Center image in icon - this info should be in the returned Attributes struct. * etc... * * I am still not sure we are handling color resources properly... Seems to work OK on my 24-bit displays... * * * * * Changes: * Version 0.72 - May 27, 2001 * Fixed resource leak (XFreeColors) * * Version 0.71 - May 24, 2001 * Added support for "zooming" the icon display * on a region * * Version 0.70 - March 28, 1999. * Added support for local images (e.g. file:///home/mgh/junk.gif.) * * Version 0.66 - February 23, 1999. * Added help line for -delay option. And added a man page. * * Version 0.65 - February 11, 1999. * Now starts netscape if not already running... * * Version 0.64 - February 9, 1999. * Added command-line option for "Time between updates" * * Version 0.63 - February 9, 1999. * Fixed (potential) memory leak in the ShapeMask Pixmap * (gets returned only if color None is used in xpm file) * Added XFreeColors call. * * Version 0.62 - February 4, 1999. * buf fixes. Added -c option to center vertically. * * * Version 0.61 - February 2, 1999. * Added Attributes support for Xpm Creation. * Also, added a kludge to GrabImage to stream edit * any "Transparent" colors that may get generated. (Change them * to black.) * * Version 0.6 - February 2, 1999. * Some bug fixes. Added Double click support. * Now, DoubleClick's do the following: * * Button1: display original size image via xv. * * Button2: send another URL (specified with the -http * command line option) to netscape. * * Button3: Force Update. * * * Version 0.5 - initial (very) beta release February 1, 1999. * * */ /* * Includes */ #include #include #include #include #include #include #include #include #include #include #include "../wmgeneral/wmgeneral.h" #include "wmGrabImage_master.xpm" #include "wmGrabImage_mask.xbm" /* * Delay between refreshes (in microseconds) */ #define DELAY 10000L #define WMGRABIMAGE_VERSION "0.72" void ParseCMDLine(int argc, char *argv[]); void pressEvent(XButtonEvent *xev); void print_usage(); char ImageURL[1024], XpmFileName[256], ImageFileName[1024]; char HttpURL[1024]; char *ConvertGeometry= NULL; int UpToDate = 0; int ForceUpdate = 1; int ForceUpdate2 = 1; int GotFirstClick1, GotDoubleClick1; int GotFirstClick2, GotDoubleClick2; int GotFirstClick3, GotDoubleClick3; int DblClkDelay; int CenterImage = 0; long int UpdateDELAY = 600; /* * main */ int main(int argc, char *argv[]) { struct tm *gTime, *gmt; struct stat fi; XEvent event; Pixmap NewPixmap, NewShapeMask; XpmAttributes Attributes; Colormap cmap; int n, s, m, dt1, dt2, dt3, len; int Year, Month, Day; int Hours, Mins, Secs; int i, j, Width, Height, yoff, fd, Flag; long CurrentLocalTime; double UT, hour24(), jd(), CurrentJD, OldFileUT, FileUT; char command[1040], ImageName[256]; int havePixmap= 0; /* * Parse any command line arguments. */ ParseCMDLine(argc, argv); /* * Figure out what the name of the image xpm file should be... */ len = strlen(ImageURL); for (j = 0, i=0; i 100){ m = 0; ++dt1; ++dt2; ++dt3; } else { /* * Increment counter */ ++m; } /* * Double Click Delays * Keep track of click events. If Delay too long, set GotFirstClick's to False. */ if (DblClkDelay > 15) { DblClkDelay = 0; GotFirstClick1 = 0; GotDoubleClick1 = 0; GotFirstClick2 = 0; GotDoubleClick2 = 0; GotFirstClick3 = 0; GotDoubleClick3 = 0; } else { ++DblClkDelay; } /* * Process any pending X events. */ while(XPending(display)){ XNextEvent(display, &event); switch(event.type){ case Expose: RedrawWindow(); break; case ButtonPress: pressEvent(&event.xbutton); break; case ButtonRelease: break; } } /* * Draw window. */ if (ForceUpdate||Flag){ /* * Compute Current Julian Date */ CurrentLocalTime = time(CurrentTime); gTime = gmtime(&CurrentLocalTime); Year = gTime->tm_year+1900; Month = gTime->tm_mon+1; Day = gTime->tm_mday; Hours = gTime->tm_hour; Mins = gTime->tm_min; Secs = gTime->tm_sec; UT = (double)Hours + (double)Mins/60.0 + (double)Secs/3600.0; CurrentJD = jd(Year, Month, Day, UT); /* * Clear window. */ copyXPMArea(5, 69, 54, 54, 5, 5); if (havePixmap) { /* * free up the colors, if we alloc'd some before */ if (Attributes.nalloc_pixels > 0) XFreeColors(display, cmap, Attributes.alloc_pixels, Attributes.nalloc_pixels, 0); /* * Free last pixmap -- we dont need it anymore... * A ShapeMask is returned if the Pixmap had the color None used. * We could probably change Transparent to None to make use of this, but for now, * lets just ignore it... */ if ( NewShapeMask != 0 ) XFreePixmap(display, NewShapeMask); XFreePixmap(display, NewPixmap); XpmFreeAttributes(&Attributes); havePixmap= 0; } /* * Grab new pixmap. Accept a reasonable color match. */ Attributes.valuemask = XpmExactColors | XpmCloseness | XpmReturnAllocPixels; Attributes.exactColors = 0; Attributes.closeness = 40000; if (XpmReadFileToPixmap(display, Root, XpmFileName, &NewPixmap, &NewShapeMask, &Attributes) >= 0){ Height = Attributes.height; Width = Attributes.width; yoff = (CenterImage) ? (54 - Height)/2 : 0; XCopyArea(display, NewPixmap, wmgen.pixmap, NormalGC, 0, 0, Width, Height, 5, 5+yoff); Flag = 0; ForceUpdate = 0; havePixmap= 1; } /* * Make changes visible */ RedrawWindow(); } /* * Check xpm file status */ if (dt2 > 1){ dt2 = 0; if ( (fd = open(XpmFileName, O_RDONLY)) >= 0 ) { fstat(fd, &fi); close(fd); gmt = gmtime(&fi.st_mtime); OldFileUT = FileUT; FileUT = (double)gmt->tm_hour + (double)gmt->tm_min/60.0 + (double)gmt->tm_sec/3600.0; if (FileUT != OldFileUT) ForceUpdate = 1; } } /* * Check every 5 min if the values are not up to date... */ if (ForceUpdate2||(dt3 > UpdateDELAY)){ dt3 = 0; /* * Execute Perl script to grab the Latest METAR Report */ if (ConvertGeometry != NULL) sprintf(command, "GrabImage %s %s &", ImageURL, ConvertGeometry); else sprintf(command, "GrabImage %s &", ImageURL); system(command); ForceUpdate = 1; ForceUpdate2 = 0; } /* * Wait for next update */ usleep(DELAY); } } /* * ParseCMDLine() */ void ParseCMDLine(int argc, char *argv[]) { int i; ImageURL[0] = '\0'; for (i = 1; i < argc; i++) { if (!strcmp(argv[i], "-display")){ ++i; } else if ((!strcmp(argv[i], "-url"))||(!strcmp(argv[i], "-u"))){ strcpy(ImageURL, argv[++i]); } else if (!strcmp(argv[i], "-delay")){ UpdateDELAY = atol(argv[++i]); if (UpdateDELAY < 10){ printf("Try a longer delay\n"); exit(-1); } } else if (!strcmp(argv[i], "-http")){ strcpy(HttpURL, argv[++i]); } else if (!strcmp(argv[i], "-geom")){ ConvertGeometry= argv[++i]; } else if (!strcmp(argv[i], "-c")){ CenterImage = 1; } else { print_usage(); exit(1); } } if (ImageURL[0] == '\0'){ fprintf(stderr, "You must specify an Image URL\n"); print_usage(); exit(-1); } } void print_usage(){ printf("\nwmGrabImage version: %s\n", WMGRABIMAGE_VERSION); printf("\nusage: wmGrabImage -u -http -display ] [-h]\n"); printf("\t\t[-geom \n\n"); printf("\t-display \tUse alternate X display.\n"); printf("\t-h\t\t\tDisplay help screen.\n"); printf("\t-url \t\tThe URL of image to display.\n\n"); printf("\t-delay