#include #include #include #include #include #include "vroot.h" #include "geisya.xpm" #include "star5.xpm" #define NCOLORS 6 main () { Display *dpy; Window root; XWindowAttributes wa; GC g; GC gc; XImage *img; XImage *timg; XImage *clp; Pixmap pix; int x, y; Font f; XFontStruct *fs; XGCValues v; char *colors[NCOLORS] = {"red", "green", "blue", "cyan", "orange", "purple"}; XColor xcolors[NCOLORS]; XColor xc, sc; int c; int count; int txtwidth; /* open the display (connect to the X server) */ dpy = XOpenDisplay (getenv ("DISPLAY")); /* get the root window */ root = DefaultRootWindow (dpy); /* get attributes of the root window */ XGetWindowAttributes(dpy, root, &wa); /* create a GC for drawing in the window */ g = XCreateGC (dpy, root, 0, NULL); /* load a font */ f = XLoadFont(dpy, "-*-helvetica-bold-r-*-*-34-*-*-*-*-*-*-*"); XSetFont(dpy, g, f); /* get font metrics */ XGetGCValues(dpy, g, GCFont, &v); fs = XQueryFont(dpy, v.font); /* allocate colors */ for(c = 0; c < NCOLORS; c++) { XAllocNamedColor(dpy, DefaultColormapOfScreen(DefaultScreenOfDisplay(dpy)), colors[c], &sc, &xc); xcolors[c] = sc; } /* set foreground color */ XSetForeground(dpy, g, WhitePixelOfScreen(DefaultScreenOfDisplay(dpy)) ); /* read the image */ /* if (XpmReadFileToImage(dpy, "/home/mieki256/bin/geisya.xpm", &img, NULL, NULL)) { printf("Error reading file\n"); exit(1); } */ /* create the image */ if (XpmCreateImageFromData(dpy, geisya, &img, NULL, NULL)) { printf("Error reading image\n"); exit(1); } /* read the transparent image */ if (XpmCreateImageFromData(dpy, star5_xpm, &timg, &clp, NULL)) { printf("Error reading file\n"); exit(1); } /* copy the transparent image into the pixmap */ pix = XCreatePixmap(dpy, root, clp->width, clp->height, clp->depth); gc = XCreateGC(dpy, pix, 0, NULL); XPutImage(dpy, pix, gc, clp, 0, 0, 0, 0, clp->width, clp->height); count = 0; /* main loop. draw something */ while (1) { int x, y, w, h; switch (count % 6) { case 0: /* draw a square */ /* set a random foreground color */ XSetForeground(dpy, g, xcolors[random() % NCOLORS].pixel); /* random x and y position */ w = 50 + (random() % 200); h = 40 + (random() % 200); x = random() % (wa.width - w); y = random() % (wa.height - h); XFillRectangle(dpy, root, g, x, y, w, h); break; case 1: /* draw a circle */ XSetForeground(dpy, g, xcolors[random() % NCOLORS].pixel); w = 10 + (random() % 200); h = 10 + (random() % 200); x = random() % (wa.width - w); y = random() % (wa.height - h); XFillArc(dpy, root, g, x, y, w, h, 0, 360 * 64); break; case 2: /* draw a string */ XSetForeground(dpy, g, xcolors[random() % NCOLORS].pixel); x = random() % wa.width; y = random() % wa.height; XDrawString(dpy, root, g, x, y, "Draw String", strlen("Draw String") ); break; case 3: /* draw a string and boxes */ XSetForeground(dpy, g, xcolors[random() % NCOLORS].pixel); x = random() % wa.width; y = random() % wa.height; /* get text width and height */ txtwidth = XTextWidth(fs, "Ooops!", strlen("Ooops!") ); /* draw the frames (boxes) */ XDrawRectangle(dpy, root, g, x, y - fs->ascent, txtwidth, fs->ascent); XDrawRectangle(dpy, root, g, x, y, txtwidth, fs->descent); /* draw a string */ XDrawString(dpy, root, g, x, y, "Ooops!", strlen("Ooops!") ); break; case 4: /* draw image */ /* copy the image (which is ir->weight large and ir->height high, to a random position of the screen */ x = random() % (wa.width - img->width); y = random() % (wa.height - img->height); XPutImage(dpy, root, g, img, 0, 0, x, y, img->width, img->height ); break; case 5: /* draw transparent image */ x = random() % (wa.width - timg->width); y = random() % (wa.height - timg->height); /* set clip origin and copy */ XSetClipMask(dpy, g, pix); XSetClipOrigin(dpy, g, x, y); XPutImage(dpy, root, g, timg, 0, 0, x, y, timg->width, timg->height ); /* rest mask */ XSetClipMask(dpy, g, None); break; default: break; } /* once in a while, clear all */ count++; if( count > 60 * 5 ) { XClearWindow(dpy, root); count = 0; } /* flush changes and sleep */ XFlush(dpy); usleep(1000000 / 60); } XCloseDisplay (dpy); }