/* * gru.c - grugru * Copyright (C) 1993, 1994 by ?????/candy */ static char rcsid[] = "$Id: gru.c,v 2.1 1995/02/22 09:37:46 candy Exp candy $"; #include #include #include #include #include #include #include #include #include #define NPALET 15 int pause_time; static int dump; char *myname; static long cur_wx, cur_wy; static Widget cur_graph; static Pixmap pattern[NPALET]; static GC gc_fg; static GC gc_bg; static void graph_get_graph_size(Widget w, long *xp, long *yp) { Display *d = XtDisplay(w); Window win = XtWindow(w); Window root; int x, y; unsigned int width, height, border, depth; XGetGeometry(d, win, &root, &x, &y, &width, &height, &border, &depth); *xp = width; *yp = height; }/* graph_get_graph_size */ static Pixmap create_off_screen(Widget w) { Display *d = XtDisplay(w); Window win = XtWindow(w); Pixmap pix; unsigned int width, height; { Window root; int x, y; unsigned int border, depth; XGetGeometry(d, win, &root, &x, &y, &width, &height, &border, &depth); if (dump) depth = 1; pix = XCreatePixmap(d, win, width, height, depth); } { GC gc = XCreateGC(d, pix, 0, NULL); unsigned long bg; XtVaGetValues(w, XtNbackground, &bg, NULL); XSetForeground(d, gc, bg); XFillRectangle(d, pix, gc, 0, 0, width, height); XFreeGC(d, gc); } return pix; }/* create_off_screen */ static void pix_init(Widget w) { Display *d = XtDisplay(w); int i; graph_get_graph_size(w, &cur_wx, &cur_wy); for (i = 0; i < NPALET; i++) { pattern[i] = create_off_screen(w); }/* for */ gc_fg = XCreateGC(d, pattern[0], 0, NULL); gc_bg = XCreateGC(d, pattern[0], 0, NULL); { unsigned long fg, bg; XtVaGetValues(w, XtNborderColor, &fg, XtNbackground, &bg, NULL); if (fg == bg) { int scno = XScreenNumberOfScreen(XtScreen(w)); if (fg == BlackPixel(d, scno)) fg = WhitePixel(d, scno); else fg = BlackPixel(d, scno); } XSetForeground(d, gc_fg, fg); XSetForeground(d, gc_bg, bg); } }/* pix_init */ static void pix_clear(Display *d, Pixmap pix) { XFillRectangle(d, pix, gc_bg, 0, 0, cur_wx, cur_wy); }/* pix_clear */ static void pix_circle(Display *d, Pixmap p, int x0, int y0, int r, int pal) { XDrawArc(d, p, (pal ? gc_fg : gc_bg), x0 - r, y0 - r, 2 * r, 2 * r, 0, 360 * 64); }/* pix_circle */ static void make_pattern(Display *d, Pixmap pix, int p0) { int i = 0; long r, rmax; long ox, oy; ox = cur_wx / 2; oy = cur_wy / 2; pix_clear(d, pix); rmax = sqrt(ox * ox + oy * oy); #define zcircle(x0,y0,r,p) pix_circle(d, pix, (x0)+ox,oy-(y0),(r),(p)) i = p0 % NPALET; for (r = 0; r < rmax; r++) { zcircle(0, 0, r, (i < NPALET / 3)); if (r % 100 == 0) i += NPALET / 2; if (((r / 100) % 2) == 0) { i = (i + 1) % NPALET; } else { i = (i + NPALET - 1) % NPALET; } }/* for */ }/* make_pattern */ static void make_patterns(Display *d) { int i; for (i = 0; i < NPALET; i++) { make_pattern(d, pattern[i], i); if (dump) { char name[64]; sprintf(name, "gru%02d.xbm", i); XWriteBitmapFile(d, name, pattern[i], cur_wx, cur_wy, 0, 0); } }/* for */ }/* make_patterns */ static void do_gru(Widget w) { static int cur_frame; Display *d = XtDisplay(w); Window win = XtWindow(w); int x0 = 0, y0 = 0, x1 = cur_wx, y1 = cur_wx; XCopyArea(d, pattern[cur_frame], win, gc_fg, x0, y0, x1, y1, x0, y0); cur_frame = (cur_frame + 1) % NPALET; }/* do_gru */ static void quit_command_proc(Widget w, XtPointer p1, XtPointer p2) { exit(1); }/* quit_command_proc */ static void timeout_proc(XtPointer p1, XtIntervalId* id) { static int done; XtAppContext app_con = p1; if (!done) { make_patterns(XtDisplay(cur_graph)); done++; if (dump) exit(0); } do_gru(cur_graph); XFlush(XtDisplay(cur_graph)); XtAppAddTimeOut(app_con, pause_time, timeout_proc, app_con); }/* timeout_proc */ int main(int argc, char *argv[]) { static String fallback_resources[] = { "*graph*showGrip: off", "*graph*resizeToPreferred: yes", "*graph.background: black", "*graph.fromVert: quit", "*graph.width: 640", "*graph.height: 480", NULL }; /* * option */ struct appr { int length; int count; int wait; int maxspeed; int random; int cspeed; int dump; }; static struct appr app_resources; #define DEFINT(member,name,val) {#member, name, XtRInt, sizeof(int), XtOffset(struct appr *, member), XtRImmediate, (XtPointer)(val)} #define DEFBOOL(name, class, default) {#name, class, XtRBoolean, sizeof(Boolean), XtOffsetOf(struct appr, name), XtRImmediate, (XtPointer)(default)} static XtResource resources[] = { DEFINT(wait, "Wait", 17), DEFBOOL(dump, "Dump", False), }; static XrmOptionDescRec options[] = { {"-wait", ".Wait", XrmoptionSepArg, NULL}, {"-dump", ".Dump", XrmoptionNoArg, "True"}, }; static char usage_msg[] = "gru version 0.8087\n" "usage: %s " "[-dump]" "[Xtoolkit options]" "\n"; XtAppContext app_con; int ex = 1; Widget toplevel = XtVaAppInitialize(&app_con, "Gru", options, XtNumber(options), &argc, argv, fallback_resources, NULL); if (argc > 1) { fprintf(stderr, usage_msg, argv[0]); } else { Widget graph, form = XtVaCreateManagedWidget("paned", panedWidgetClass, toplevel, NULL); XtVaGetApplicationResources(toplevel, (caddr_t)&app_resources, resources, XtNumber(resources), NULL); XawDialogAddButton(form, "quit", quit_command_proc, NULL); pause_time = app_resources.wait; dump = app_resources.dump; graph = XtVaCreateManagedWidget("graph", widgetClass, form, NULL); cur_graph = graph; XtRealizeWidget(toplevel); pix_init(graph); XtAppAddTimeOut(app_con, pause_time, timeout_proc, app_con); XtAppMainLoop(app_con); } return ex; }/* main */