Blame


1 fd465540 2004-12-27 devnull #include <u.h>
2 fd465540 2004-12-27 devnull #include <libc.h>
3 fd465540 2004-12-27 devnull #include <draw.h>
4 fd465540 2004-12-27 devnull #include <thread.h>
5 fd465540 2004-12-27 devnull #include <keyboard.h>
6 fd465540 2004-12-27 devnull #include <mouse.h>
7 fd465540 2004-12-27 devnull
8 fd465540 2004-12-27 devnull enum
9 fd465540 2004-12-27 devnull {
10 cbeb0b26 2006-04-01 devnull STACK = 8192
11 fd465540 2004-12-27 devnull };
12 fd465540 2004-12-27 devnull
13 fd465540 2004-12-27 devnull int nbit, npix;
14 fd465540 2004-12-27 devnull Image *pixel;
15 fd465540 2004-12-27 devnull Rectangle crect[256];
16 fd465540 2004-12-27 devnull Image *color[256];
17 fd465540 2004-12-27 devnull char *fmt;
18 fd465540 2004-12-27 devnull int ramp;
19 fd465540 2004-12-27 devnull
20 fd465540 2004-12-27 devnull Mousectl *mousectl;
21 fd465540 2004-12-27 devnull Keyboardctl *keyboardctl;
22 fd465540 2004-12-27 devnull
23 fd465540 2004-12-27 devnull void keyboardthread(void*);
24 fd465540 2004-12-27 devnull void mousethread(void*);
25 fd465540 2004-12-27 devnull void resizethread(void*);
26 fd465540 2004-12-27 devnull
27 fd465540 2004-12-27 devnull ulong
28 fd465540 2004-12-27 devnull grey(int i)
29 fd465540 2004-12-27 devnull {
30 fd465540 2004-12-27 devnull if(i < 0)
31 fd465540 2004-12-27 devnull return grey(0);
32 fd465540 2004-12-27 devnull if(i > 255)
33 fd465540 2004-12-27 devnull return grey(255);
34 fd465540 2004-12-27 devnull return (i<<16)+(i<<8)+i;
35 fd465540 2004-12-27 devnull }
36 fd465540 2004-12-27 devnull
37 fd465540 2004-12-27 devnull int
38 fd465540 2004-12-27 devnull dither[16] = {
39 fd465540 2004-12-27 devnull 0, 8, 2, 10,
40 fd465540 2004-12-27 devnull 12, 4, 14, 6,
41 fd465540 2004-12-27 devnull 3, 11, 1, 9,
42 fd465540 2004-12-27 devnull 15, 7, 13, 5
43 fd465540 2004-12-27 devnull };
44 fd465540 2004-12-27 devnull
45 a4a77934 2006-06-25 devnull extern int chattydrawclient;
46 a4a77934 2006-06-25 devnull
47 fd465540 2004-12-27 devnull void
48 fd465540 2004-12-27 devnull threadmain(int argc, char *argv[])
49 fd465540 2004-12-27 devnull {
50 fd465540 2004-12-27 devnull int i, j, k, l;
51 fd465540 2004-12-27 devnull Image *dark;
52 fd465540 2004-12-27 devnull
53 fd465540 2004-12-27 devnull ramp = 0;
54 fd465540 2004-12-27 devnull
55 fd465540 2004-12-27 devnull fmt = "index %3d r %3lud g %3lud b %3lud 0x%.8luX ";
56 fd465540 2004-12-27 devnull ARGBEGIN{
57 fd465540 2004-12-27 devnull default:
58 fd465540 2004-12-27 devnull goto Usage;
59 a4a77934 2006-06-25 devnull case 'D':
60 a4a77934 2006-06-25 devnull chattydrawclient = 1;
61 a4a77934 2006-06-25 devnull break;
62 fd465540 2004-12-27 devnull case 'x':
63 fd465540 2004-12-27 devnull fmt = "index %2luX r %3luX g %3luX b %3luX 0x%.8luX ";
64 fd465540 2004-12-27 devnull break;
65 fd465540 2004-12-27 devnull case 'r':
66 fd465540 2004-12-27 devnull ramp = 1;
67 fd465540 2004-12-27 devnull break;
68 fd465540 2004-12-27 devnull }ARGEND
69 fd465540 2004-12-27 devnull
70 fd465540 2004-12-27 devnull if(argc){
71 fd465540 2004-12-27 devnull Usage:
72 fd465540 2004-12-27 devnull fprint(2, "Usage: %s [-rx]\n", argv0);
73 5ad21e86 2005-01-15 devnull threadexitsall("usage");
74 fd465540 2004-12-27 devnull }
75 fd465540 2004-12-27 devnull
76 d7925b13 2005-01-14 devnull if(initdraw(0, nil, "colors") < 0)
77 fd465540 2004-12-27 devnull sysfatal("initdraw failed: %r");
78 fd465540 2004-12-27 devnull
79 fd465540 2004-12-27 devnull mousectl = initmouse(nil, display->image);
80 fd465540 2004-12-27 devnull if(mousectl == nil)
81 fd465540 2004-12-27 devnull sysfatal("initmouse: %r");
82 fd465540 2004-12-27 devnull
83 fd465540 2004-12-27 devnull keyboardctl = initkeyboard(nil);
84 fd465540 2004-12-27 devnull if(keyboardctl == nil)
85 fd465540 2004-12-27 devnull sysfatal("initkeyboard: %r");
86 fd465540 2004-12-27 devnull
87 fd465540 2004-12-27 devnull for(i=0; i<256; i++){
88 fd465540 2004-12-27 devnull if(ramp){
89 fd465540 2004-12-27 devnull if(screen->chan == CMAP8){
90 fd465540 2004-12-27 devnull /* dither the fine grey */
91 fd465540 2004-12-27 devnull j = i-(i%17);
92 fd465540 2004-12-27 devnull dark = allocimage(display, Rect(0,0,1,1), screen->chan, 1, (grey(j)<<8)+0xFF);
93 fd465540 2004-12-27 devnull color[i] = allocimage(display, Rect(0,0,4,4), screen->chan, 1, (grey(j+17)<<8)+0xFF);
94 fd465540 2004-12-27 devnull for(j=0; j<16; j++){
95 fd465540 2004-12-27 devnull k = j%4;
96 fd465540 2004-12-27 devnull l = j/4;
97 fd465540 2004-12-27 devnull if(dither[j] > (i%17))
98 fd465540 2004-12-27 devnull draw(color[i], Rect(k, l, k+1, l+1), dark, nil, ZP);
99 fd465540 2004-12-27 devnull }
100 fd465540 2004-12-27 devnull freeimage(dark);
101 fd465540 2004-12-27 devnull }else
102 fd465540 2004-12-27 devnull color[i] = allocimage(display, Rect(0,0,1,1), screen->chan, 1, (grey(i)<<8)+0xFF);
103 fd465540 2004-12-27 devnull }else
104 fd465540 2004-12-27 devnull color[i] = allocimage(display, Rect(0,0,1,1), screen->chan, 1, (cmap2rgb(i)<<8)+0xFF);
105 fd465540 2004-12-27 devnull if(color[i] == nil)
106 fd465540 2004-12-27 devnull sysfatal("can't allocate image: %r");
107 fd465540 2004-12-27 devnull }
108 fd465540 2004-12-27 devnull
109 fd465540 2004-12-27 devnull threadcreate(mousethread, nil, STACK);
110 fd465540 2004-12-27 devnull threadcreate(keyboardthread, nil, STACK);
111 fd465540 2004-12-27 devnull threadcreate(resizethread, nil, STACK);
112 fd465540 2004-12-27 devnull }
113 fd465540 2004-12-27 devnull
114 fd465540 2004-12-27 devnull void
115 fd465540 2004-12-27 devnull keyboardthread(void *v)
116 fd465540 2004-12-27 devnull {
117 fd465540 2004-12-27 devnull Rune r;
118 fd465540 2004-12-27 devnull
119 fd465540 2004-12-27 devnull USED(v);
120 fd465540 2004-12-27 devnull
121 fd465540 2004-12-27 devnull while(recv(keyboardctl->c, &r) >= 0)
122 fd465540 2004-12-27 devnull ;
123 fd465540 2004-12-27 devnull }
124 fd465540 2004-12-27 devnull
125 fd465540 2004-12-27 devnull char *buttons[] =
126 fd465540 2004-12-27 devnull {
127 fd465540 2004-12-27 devnull "exit",
128 fd465540 2004-12-27 devnull 0
129 fd465540 2004-12-27 devnull };
130 fd465540 2004-12-27 devnull
131 fd465540 2004-12-27 devnull Menu menu =
132 fd465540 2004-12-27 devnull {
133 fd465540 2004-12-27 devnull buttons
134 fd465540 2004-12-27 devnull };
135 fd465540 2004-12-27 devnull
136 fd465540 2004-12-27 devnull void
137 fd465540 2004-12-27 devnull mousethread(void *v)
138 fd465540 2004-12-27 devnull {
139 fd465540 2004-12-27 devnull Point p;
140 fd465540 2004-12-27 devnull Mouse m;
141 fd465540 2004-12-27 devnull int i, n, prev;
142 fd465540 2004-12-27 devnull char buf[100];
143 fd465540 2004-12-27 devnull ulong rgb;
144 fd465540 2004-12-27 devnull
145 fd465540 2004-12-27 devnull prev = -1;
146 fd465540 2004-12-27 devnull while(readmouse(mousectl) >= 0){
147 fd465540 2004-12-27 devnull m = mousectl->m;
148 fd465540 2004-12-27 devnull switch(m.buttons){
149 fd465540 2004-12-27 devnull case 1:
150 fd465540 2004-12-27 devnull while(m.buttons){
151 fd465540 2004-12-27 devnull if(screen->depth > 8)
152 fd465540 2004-12-27 devnull n = 256;
153 fd465540 2004-12-27 devnull else
154 fd465540 2004-12-27 devnull n = 1<<screen->depth;
155 fd465540 2004-12-27 devnull for(i=0; i!=n; i++)
156 fd465540 2004-12-27 devnull if(i!=prev && ptinrect(m.xy, crect[i])){
157 fd465540 2004-12-27 devnull if(ramp)
158 fd465540 2004-12-27 devnull rgb = grey(i);
159 fd465540 2004-12-27 devnull else
160 fd465540 2004-12-27 devnull rgb = cmap2rgb(i);
161 fd465540 2004-12-27 devnull sprint(buf, fmt,
162 fd465540 2004-12-27 devnull i,
163 fd465540 2004-12-27 devnull (rgb>>16)&0xFF,
164 fd465540 2004-12-27 devnull (rgb>>8)&0xFF,
165 fd465540 2004-12-27 devnull rgb&0xFF,
166 fd465540 2004-12-27 devnull (rgb<<8) | 0xFF);
167 fd465540 2004-12-27 devnull p = addpt(screen->r.min, Pt(2,2));
168 fd465540 2004-12-27 devnull draw(screen, Rpt(p, addpt(p, stringsize(font, buf))), display->white, nil, p);
169 fd465540 2004-12-27 devnull string(screen, p, display->black, ZP, font, buf);
170 fd465540 2004-12-27 devnull prev=i;
171 fd465540 2004-12-27 devnull break;
172 fd465540 2004-12-27 devnull }
173 fd465540 2004-12-27 devnull readmouse(mousectl);
174 fd465540 2004-12-27 devnull m = mousectl->m;
175 fd465540 2004-12-27 devnull }
176 fd465540 2004-12-27 devnull break;
177 fd465540 2004-12-27 devnull
178 fd465540 2004-12-27 devnull case 4:
179 fd465540 2004-12-27 devnull switch(menuhit(3, mousectl, &menu, nil)){
180 fd465540 2004-12-27 devnull case 0:
181 5ad21e86 2005-01-15 devnull threadexitsall(0);
182 fd465540 2004-12-27 devnull }
183 fd465540 2004-12-27 devnull }
184 fd465540 2004-12-27 devnull }
185 fd465540 2004-12-27 devnull }
186 fd465540 2004-12-27 devnull
187 fd465540 2004-12-27 devnull void
188 fd465540 2004-12-27 devnull eresized(int new)
189 fd465540 2004-12-27 devnull {
190 fd465540 2004-12-27 devnull int x, y, i, n, nx, ny;
191 fd465540 2004-12-27 devnull Rectangle r, b;
192 fd465540 2004-12-27 devnull
193 fd465540 2004-12-27 devnull if(new && getwindow(display, Refnone) < 0){
194 fd465540 2004-12-27 devnull fprint(2, "colors: can't reattach to window: %r\n");
195 5ad21e86 2005-01-15 devnull threadexitsall("resized");
196 fd465540 2004-12-27 devnull }
197 fd465540 2004-12-27 devnull if(screen->depth > 8){
198 fd465540 2004-12-27 devnull n = 256;
199 fd465540 2004-12-27 devnull nx = 16;
200 fd465540 2004-12-27 devnull }else{
201 fd465540 2004-12-27 devnull n = 1<<screen->depth;
202 fd465540 2004-12-27 devnull nx = 1<<(screen->depth/2);
203 fd465540 2004-12-27 devnull }
204 fd465540 2004-12-27 devnull
205 fd465540 2004-12-27 devnull ny = n/nx;
206 fd465540 2004-12-27 devnull draw(screen, screen->r, display->white, nil, ZP);
207 fd465540 2004-12-27 devnull r = insetrect(screen->r, 5);
208 fd465540 2004-12-27 devnull r.min.y+=20;
209 fd465540 2004-12-27 devnull b.max.y=r.min.y;
210 fd465540 2004-12-27 devnull for(i=n-1, y=0; y!=ny; y++){
211 fd465540 2004-12-27 devnull b.min.y=b.max.y;
212 fd465540 2004-12-27 devnull b.max.y=r.min.y+(r.max.y-r.min.y)*(y+1)/ny;
213 fd465540 2004-12-27 devnull b.max.x=r.min.x;
214 fd465540 2004-12-27 devnull for(x=0; x!=nx; x++, --i){
215 fd465540 2004-12-27 devnull b.min.x=b.max.x;
216 fd465540 2004-12-27 devnull b.max.x=r.min.x+(r.max.x-r.min.x)*(x+1)/nx;
217 fd465540 2004-12-27 devnull crect[i]=insetrect(b, 1);
218 fd465540 2004-12-27 devnull draw(screen, crect[i], color[i], nil, ZP);
219 fd465540 2004-12-27 devnull }
220 fd465540 2004-12-27 devnull }
221 fd465540 2004-12-27 devnull flushimage(display, 1);
222 fd465540 2004-12-27 devnull }
223 fd465540 2004-12-27 devnull
224 fd465540 2004-12-27 devnull void
225 fd465540 2004-12-27 devnull resizethread(void *v)
226 fd465540 2004-12-27 devnull {
227 fd465540 2004-12-27 devnull ulong x;
228 fd465540 2004-12-27 devnull
229 fd465540 2004-12-27 devnull USED(v);
230 fd465540 2004-12-27 devnull
231 fd465540 2004-12-27 devnull eresized(0);
232 fd465540 2004-12-27 devnull while(recv(mousectl->resizec, &x) >= 0)
233 fd465540 2004-12-27 devnull eresized(1);
234 fd465540 2004-12-27 devnull }
235 fd465540 2004-12-27 devnull