1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <X11/Xlib.h>
4 #include <X11/Xutil.h>
5 
6 #include "xchadance.h"
7 
8 #define NCOLOR 16
9 
10 Display* display;
11 int screen;
12 Window rootwin;
13 Window mywin;
14 GC mygc;
15 int depth;
16 Colormap cmap;
17 unsigned long white;
18 Pixmap chachapix[NPAT];
19 XColor mycolor[NCOLOR];
20 unsigned int waitcount;
21 
22 
23 
24 
main(int argc,char ** argv)25 void main(int argc, char** argv)
26 {
27     if (parseargs(argc, argv)!=0) return;
28     if (openwindow()!=0) return;
29     if (makepixmap()!=0) return;
30     mainloop();
31     closewindow();
32 }
33 
parseargs(int argc,char ** argv)34 int parseargs(int argc, char** argv)
35 {
36     int i, result = 0;
37 
38     waitcount = 75*1000;
39     for (i=1; i<argc; i++) {
40 	if (strcmp(argv[i], "-wait")==0) {
41 	    i++;
42 	    if ((waitcount = atoi(argv[i])*1000)<=0) {
43 		fprintf( stderr, "illeagal waitcount.\n");
44 		result = -1;
45 		break;
46 	    }
47 	}
48 	else {
49 	    result = 1;
50 	    break;
51 	}
52     }
53     if (result != 0) {
54 	fprintf( stderr, "xchadance [-wait <waitcount(ms)>]\n");
55     }
56     return result;
57 }
58 
59 
openwindow()60 int openwindow()
61 {
62     XSizeHints	sizehints;
63     int i;
64 
65     if ((display = XOpenDisplay(NULL))==NULL) {
66 	fputs( "Error: cannot connect X server\n", stderr );
67 	return -1;
68     }
69     screen = DefaultScreen(display);
70     rootwin = RootWindow(display, screen);
71     depth = DefaultDepth(display, screen);
72     cmap = DefaultColormap(display, screen);
73     white = WhitePixel(display, screen);
74     mywin = XCreateSimpleWindow(display, rootwin, 0, 0, W_CHACHA, H_CHACHA,
75 			0, white, white);
76     mygc = XCreateGC(display, mywin, 0, 0);
77     for (i=0; i<NCOLOR; i++) {
78 	mycolor[i].red = 65535/15*palet[i].r;
79 	mycolor[i].green = 65535/15*palet[i].g;
80 	mycolor[i].blue = 65535/15*palet[i].b;
81 	if (XAllocColor(display, cmap, &mycolor[i])==0) {
82 	    fputs( "Error: cannot allocate color\n", stderr );
83 	    return -2;
84 	}
85     }
86     XSelectInput(display, mywin, ExposureMask);
87     sizehints.flags = PMinSize | PMaxSize;
88     sizehints.min_width = sizehints.max_width = W_CHACHA;
89     sizehints.min_height = sizehints.max_height = H_CHACHA;
90     XSetStandardProperties(display, mywin, "ChaDance", "ChaDance", None,
91 			   NULL, 0, &sizehints);
92 
93     XMapWindow(display, mywin);
94     return 0;
95 }
96 
mainloop()97 int mainloop()
98 {
99     int anime_id = 0;
100 
101     for (;;) {
102       if (XPending(display)) {
103 	XEvent event;
104 	XNextEvent(display, &event);
105       }
106 	XCopyArea(display, chachapix[animetbl[anime_id]], mywin, mygc,
107 		  0, 0, W_CHACHA, H_CHACHA, 0, 0);
108 	XFlush(display);
109 	if (++anime_id >= NANIMETBL) {
110 	    anime_id = 0;
111 	}
112 	usleep(waitcount);
113 #if 0
114     XEvent event;
115 	XNextEvent(display, &event);
116 	switch (event.type) {
117 	  case Expose:
118 	    XCopyArea(display, pixmap, mywin, mygc,
119 		      0, 0, W_CHACHA, H_CHACHA, 0, 0);
120 	    break;
121 	  default:
122 	    break;
123 	}
124 #endif
125     }
126     return 0;
127 }
128 
closewindow()129 int closewindow()
130 {
131     XDestroyWindow(display, mywin);
132     XCloseDisplay(display);
133     return 0;
134 }
135 
makepixmap()136 int makepixmap()
137 {
138     int x, y, no;
139 
140     for (no=0; no<NPAT; no++) {
141 	unsigned char *p = chacha[no];
142 
143 	chachapix[no] = XCreatePixmap(display, mywin, W_CHACHA, H_CHACHA, depth);
144 	for (y=0; y<H_CHACHA; y++) {
145 	    for (x=0; x<W_CHACHA; p++) {
146 		int color1, color2;
147 
148 		color1 = (*p)>>4;
149 		color2 = (*p) & 0x000f;
150 		XSetForeground(display, mygc, mycolor[color1].pixel);
151 		XDrawPoint(display, chachapix[no], mygc, x, y);
152 		x++;
153 		if (color1!=color2) {
154 		    XSetForeground(display, mygc, mycolor[color2].pixel);
155 		}
156 		XDrawPoint(display, chachapix[no], mygc, x, y);
157 		x++;
158 	    }
159 	}
160     }
161     return 0;
162 }
163