Add missing libraries to examples' Makefiles
[dockapps.git] / wmtunlo / wmtunlo.c
blob349b0c6dba5d115ca51e193b6ce3abb79144d686
2 /*
3 * wmtunlo
5 * Copyright (C) 2001 pasp
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 #include <stdio.h>
23 #include <unistd.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <time.h>
27 #include <X11/X.h>
28 #include <X11/xpm.h>
29 #include <sys/types.h>
30 #include <sys/stat.h>
31 #include <fcntl.h>
32 #include <math.h>
34 #include <X11/Xlib.h>
35 #include <X11/xpm.h>
36 #include <X11/extensions/shape.h>
38 #include "master.h"
39 #include "mask.h"
41 #include "docklib.h"
43 void wmtunlo_read_prefs(void);
44 void wmtunlo_write_prefs(void);
46 void ButtonPressEvent(XButtonEvent *);
47 int GotFirstClick1, GotDoubleClick1;
48 int GotFirstClick2, GotDoubleClick2;
49 int GotFirstClick3, GotDoubleClick3;
50 int DblClkDelay;
51 int HasExecute;
52 char ClickCommand1[MAX_PATH];
53 char ClickCommand2[MAX_PATH];
54 char ClickCommand3[MAX_PATH];
56 int tex_block_w, tex_block_h, shade_switch, persp_switch;
57 int t_color_1, t_color_2;
58 float t_persp, t_shade_level;
59 float t_mov_counter, t_rot_counter;
60 float t_mov_step, t_rot_step;
61 float t_col_add_R, t_col_add_G, t_col_add_B;
62 float t_col_mul_R, t_col_mul_G, t_col_mul_B;
64 int main(int argc, char *argv[])
67 typedef struct {
68 Display *display;
69 int screen;
70 Visual *visual;
71 int depth;
72 Colormap cmap;
73 int format;
74 int bitmap_pad;
75 int Color[256];
76 unsigned char R[256];
77 unsigned char G[256];
78 unsigned char B[256];
79 } DisplayInfo;
81 XImage *xim;
82 XEvent event;
83 XColor xColor, xColors[256];
84 DisplayInfo Info;
85 int i, j, shade, k, l, p, q, result, col;
86 float xz, yz, px, py;
87 unsigned char *Image, *Texture, *Tunnel_map;
90 /* Read preferences */
91 wmtunlo_read_prefs();
93 /* Open window */
94 openXwindow(argc, argv, dock_master, dock_mask_bits, dock_mask_width, dock_mask_height);
96 /* Get Display parameters */
97 Info.display = display;
98 Info.screen = DefaultScreen(display);
99 Info.visual = DefaultVisual(display, Info.screen);
100 Info.depth = DefaultDepth(display, Info.screen);
101 Info.cmap = DefaultColormap(display, 0);
103 /* Initialize Color Table */
105 for (i=0; i<256; ++i) {
106 col = t_col_mul_R * (float)i + t_col_add_R;
107 if(col<0) col = 0;
108 if(col>255) col = 255;
109 Info.R[i] = col;
111 col = t_col_mul_G * (float)i + t_col_add_G;
112 if(col<0) col = 0;
113 if(col>255) col = 255;
114 Info.G[i] = col;
116 col = t_col_mul_B * (float)i + t_col_add_B;
117 if(col<0) col = 0;
118 if(col>255) col = 255;
119 Info.B[i] = col;
122 /* Create an XImage with null data. Then allocate space for data. */
123 Info.format = ZPixmap;
124 if (Info.depth == 8){
126 Info.bitmap_pad = 8;
128 /* Set a private colormap */
129 Info.cmap = XCreateColormap(Info.display, RootWindow(Info.display, Info.screen), Info.visual, AllocAll);
130 for (i=0; i<256; ++i){
131 Info.Color[i] = i;
132 xColors[i].pixel = i;
133 xColors[i].red = (unsigned short)Info.R[i] << 8;
134 xColors[i].green = (unsigned short)Info.G[i] << 8;
135 xColors[i].blue = (unsigned short)Info.B[i] << 8;
136 xColors[i].flags = DoRed | DoGreen | DoBlue;
138 XStoreColors(Info.display, Info.cmap, xColors, 256);
139 XSetWindowColormap(Info.display, win, Info.cmap);
141 } else if (Info.depth > 8) {
143 /* Allocate Colors */
144 for (i=0; i<256; ++i){
145 xColor.red = (unsigned short)Info.R[i] << 8;
146 xColor.green = (unsigned short)Info.G[i] << 8;
147 xColor.blue = (unsigned short)Info.B[i] << 8;
148 xColor.flags = DoRed | DoGreen | DoBlue;
149 XAllocColor(Info.display, Info.cmap, &xColor);
150 Info.Color[i] = xColor.pixel;
152 Info.bitmap_pad = 32;
154 } else {
155 fprintf(stderr, "Need at least 8-bit display!\n");
156 exit(-1);
159 xim = XCreateImage(Info.display, Info.visual, Info.depth, Info.format, 0, (char *)0, 54, 54, Info.bitmap_pad, 0);
160 xim->data = (char *)malloc(xim->bytes_per_line * 54 );
162 /* Allocate memory for image data */
163 Image = (unsigned char *)malloc(sizeof(unsigned char)*54*54);
165 /* Generate tunnel map */
166 Tunnel_map = (unsigned char *)malloc(sizeof(unsigned char)*54*54*4);
168 for (k=0, yz=-54/2; yz<54/2; yz++)
169 for (xz=-54/2; xz<54/2; xz++) {
171 py = sqrt(xz*xz + yz*yz);
172 shade= py * t_shade_level;
173 if(shade>255) shade=255;
174 if(persp_switch) py = t_persp/py;
175 px = atan2(yz,xz) * 128.0 / M_PI;
176 result = (int)py * 256 + (int)px;
178 Tunnel_map[k++]= result&0xff;
179 Tunnel_map[k++]= result>>8;
180 Tunnel_map[k++]= shade&0xff;
183 /* Generate texture */
184 Texture = (unsigned char *)malloc(sizeof(unsigned char)*256*256);
186 for(i=0,q=1;i<256;i+=tex_block_h, q*=-1)
187 for(j=0,p=q;j<256;j+=tex_block_w, p*=-1)
188 for(l=0;l<tex_block_h;l++)
189 for(k=0;k<tex_block_w;k++)
190 if((j+k < 256) && (i+l < 256)) {
191 if(p>0)
192 Texture[(i+l)*256+j+k] = t_color_1;
193 else
194 Texture[(i+l)*256+j+k] = t_color_2;
197 /* Animation. */
199 t_mov_counter = 0.0;
200 t_rot_counter = 0.0;
202 while(1) {
204 /* Process any pending X events. */
206 while(XPending(display)){
207 XNextEvent(display, &event);
208 switch(event.type){
209 case Expose:
210 RedrawWindow();
211 break;
212 case EnterNotify:
213 XSetInputFocus(display, iconwin, RevertToNone, CurrentTime);
214 if (Info.depth == 8) XInstallColormap(display, Info.cmap);
215 break;
216 case LeaveNotify:
217 XSetInputFocus(display, None, RevertToNone, CurrentTime);
218 if (Info.depth == 8) XUninstallColormap(display, Info.cmap);
219 break;
220 case ButtonPress:
221 ButtonPressEvent(&event.xbutton);
222 break;
223 case ButtonRelease:
224 break;
228 /* Draw tunnel. */
230 for(i=0;i<54;i++)
231 for(j=0;j<54;j++) {
233 if(shade_switch)
234 *(Image + 54*i + j) = (Tunnel_map[(54*i+j)*3 + 2] * Texture[ \
235 ((Tunnel_map[(54*i+j)*3 + 1] + (int)t_mov_counter) & 0xff)*256 + \
236 Tunnel_map[(54*i+j)*3 + 0] - (int)t_rot_counter]) >> 8;
237 else
238 *(Image + 54*i + j) = Texture[ \
239 ((Tunnel_map[(54*i+j)*3 + 1] + (int)t_mov_counter) & 0xff)*256 + \
240 Tunnel_map[(54*i+j)*3 + 0] - (int)t_rot_counter];
243 /* Move tunnel. */
245 t_mov_counter += t_mov_step;
246 if(t_mov_counter > 256.0) t_mov_counter = 0.0;
247 if(t_mov_counter < 0.0) t_mov_counter = 256.0;
248 t_rot_counter += t_rot_step;
249 if(t_rot_counter > 256.0) t_rot_counter = 0.0;
250 if(t_rot_counter < 0.0) t_rot_counter = 256.0;
252 /* Paste up image. */
253 for ( i=0; i<54; ++i )
254 for ( j=0; j<54; ++j ) {
255 XPutPixel(xim, i, j, Info.Color[*(Image + j*54 + i)]);
256 XFlush(display);
259 XPutImage(display, wmgen.pixmap, NormalGC, xim, 0, 0, 5, 5, 54, 54);
261 /* Make changes visible */
262 RedrawWindow();
264 /* Wait for next update */
265 usleep(10000L);
268 free(xim->data);
269 XDestroyImage(xim);
271 free(Image);
272 free(Tunnel_map);
273 free(Texture);
277 * This routine handles button presses.
279 * Double click on
280 * Mouse Button 1: Execute the command defined in the -e command-line option.
281 * Mouse Button 2: No action assigned.
282 * Mouse Button 3: No action assigned.
286 void ButtonPressEvent(XButtonEvent *xev){
288 DblClkDelay = 0;
289 if ((xev->button == Button1) && (xev->type == ButtonPress)){
290 if (GotFirstClick1) GotDoubleClick1 = 1;
291 else GotFirstClick1 = 1;
292 } else if ((xev->button == Button2) && (xev->type == ButtonPress)){
293 if (GotFirstClick2) GotDoubleClick2 = 1;
294 else GotFirstClick2 = 1;
295 } else if ((xev->button == Button3) && (xev->type == ButtonPress)){
296 if (GotFirstClick3) GotDoubleClick3 = 1;
297 else GotFirstClick3 = 1;
301 * We got a double click on Mouse Button1 (i.e. the left one)
303 if (GotDoubleClick1) {
304 GotFirstClick1 = 0;
305 GotDoubleClick1 = 0;
306 system(ClickCommand1);
310 * We got a double click on Mouse Button2 (i.e. the left one)
312 if (GotDoubleClick2) {
313 GotFirstClick2 = 0;
314 GotDoubleClick2 = 0;
315 system(ClickCommand2);
319 * We got a double click on Mouse Button3 (i.e. the left one)
321 if (GotDoubleClick3) {
322 GotFirstClick3 = 0;
323 GotDoubleClick3 = 0;
324 system(ClickCommand3);
327 return;
331 /*----------------------------------------------------------------------*/
332 /* Write preferences */
334 void wmtunlo_write_prefs(void)
336 if (p_prefs_openfile (p_getfilename_config (".clay", "wmtunlorc"), P_WRITE)) {
338 p_prefs_put_int("shade_switch", shade_switch);
339 p_prefs_put_int("persp_switch", persp_switch);
340 p_prefs_put_lf ();
341 p_prefs_put_float("t_shade_level", t_shade_level);
342 p_prefs_put_float("t_persp", t_persp);
343 p_prefs_put_float("t_mov_step", t_mov_step);
344 p_prefs_put_float("t_rot_step", t_rot_step);
345 p_prefs_put_lf ();
346 p_prefs_put_int("tex_block_w", tex_block_w);
347 p_prefs_put_int("tex_block_h", tex_block_h);
348 p_prefs_put_lf ();
349 p_prefs_put_float("t_col_add_R", t_col_add_R);
350 p_prefs_put_float("t_col_add_G", t_col_add_G);
351 p_prefs_put_float("t_col_add_B", t_col_add_B);
352 p_prefs_put_lf ();
353 p_prefs_put_float("t_col_mul_R", t_col_mul_R);
354 p_prefs_put_float("t_col_mul_G", t_col_mul_G);
355 p_prefs_put_float("t_col_mul_B", t_col_mul_B);
356 p_prefs_put_lf ();
357 p_prefs_put_int("t_color_1", t_color_1);
358 p_prefs_put_int("t_color_2", t_color_2);
359 p_prefs_put_lf ();
360 p_prefs_put_string("command1", ClickCommand1);
361 p_prefs_put_string("command2", ClickCommand2);
362 p_prefs_put_string("command3", ClickCommand3);
366 p_prefs_closefile ();
369 /*----------------------------------------------------------------------*/
370 /* Read preferences */
372 void wmtunlo_read_prefs(void)
374 if (p_prefs_openfile (p_getfilename_config(".clay", "wmtunlorc"), P_READ)) {
376 shade_switch = p_prefs_get_int("shade_switch");
377 persp_switch = p_prefs_get_int("persp_switch");
379 t_shade_level = p_prefs_get_float("t_shade_level");
380 t_persp = p_prefs_get_float("t_persp");
381 t_mov_step = p_prefs_get_float("t_mov_step");
382 t_rot_step = p_prefs_get_float("t_rot_step");
384 tex_block_w = p_prefs_get_int("tex_block_w");
385 tex_block_h = p_prefs_get_int("tex_block_h");
387 t_col_add_R = p_prefs_get_float("t_col_add_R");
388 t_col_add_G = p_prefs_get_float("t_col_add_G");
389 t_col_add_B = p_prefs_get_float("t_col_add_B");
391 t_col_mul_R = p_prefs_get_float("t_col_mul_R");
392 t_col_mul_G = p_prefs_get_float("t_col_mul_G");
393 t_col_mul_B = p_prefs_get_float("t_col_mul_B");
395 t_color_1 = p_prefs_get_int("t_color_1") % 255;
396 t_color_2 = p_prefs_get_int("t_color_2") % 255;
398 strcpy(ClickCommand1, p_prefs_get_string ("command1"));
399 strcpy(ClickCommand2, p_prefs_get_string ("command2"));
400 strcpy(ClickCommand3, p_prefs_get_string ("command3"));
402 p_prefs_closefile ();
404 } else {
406 shade_switch = 1;
407 persp_switch = 1;
409 t_shade_level = 5.2;
410 t_persp = 1000.0;
411 t_mov_step = 0.6;
412 t_rot_step = 0.3;
414 tex_block_w = 32;
415 tex_block_h = 32;
417 t_col_add_R = 24.0;
418 t_col_add_G = -12.0;
419 t_col_add_B = 55.0;
421 t_col_mul_R = 1.0;
422 t_col_mul_G = 1.0;
423 t_col_mul_B = 0.0;
425 t_color_1 = 20;
426 t_color_2 = 128;
428 strcpy(ClickCommand1, "xlock");
429 strcpy(ClickCommand2, "xlock -mode thornbird");
430 strcpy(ClickCommand3, "xlock -mode blank");
432 wmtunlo_write_prefs ();
437 /*----------------------------------------------------------------------*/