1 /* xscreensaver, Copyright (c) 1992, 1995, 1996, 1998, 2000
2 * Jamie Zawinski <jwz@jwz.org>
4 * Permission to use, copy, modify, distribute, and sell this software and its
5 * documentation for any purpose is hereby granted without fee, provided that
6 * the above copyright notice appear in all copies and that both that
7 * copyright notice and this permission notice appear in supporting
8 * documentation. No representations are made about the suitability of this
9 * software for any purpose. It is provided "as is" without express or
12 * This code derived from TI Explorer Lisp code by Joe Keane, Fritz Mueller,
17 #include "screenhack.h"
19 #define POINT_COUNT 16
22 #define ANGLE_SCALE 0.001
46 float hs_two_observer_z;
60 double hs_cos_xy, hs_sin_xy;
61 double hs_cos_xz, hs_sin_xz;
62 double hs_cos_yz, hs_sin_yz;
63 double hs_cos_xw, hs_sin_xw;
64 double hs_cos_yw, hs_sin_yw;
65 double hs_cos_zw, hs_sin_zw;
66 double hs_ref_ax, hs_ref_ay, hs_ref_az, hs_ref_aw;
67 double hs_ref_bx, hs_ref_by, hs_ref_bz, hs_ref_bw;
68 double hs_ref_cx, hs_ref_cy, hs_ref_cz, hs_ref_cw;
69 double hs_ref_dx, hs_ref_dy, hs_ref_dz, hs_ref_dw;
70 struct point_state hs_points[POINT_COUNT];
73 static const struct line_info line_table[LINE_COUNT];
75 static void init (struct hyper_state *hs);
76 static void hyper (struct hyper_state *hs);
77 static void check_events (struct hyper_state *hs);
78 static void set_sizes (struct hyper_state *hs, int width, int height);
80 static struct hyper_state hyper_state;
83 char *progclass = "Hypercube";
109 XrmOptionDescRec options [] =
111 { "-color0", ".color0", XrmoptionSepArg, 0 },
112 { "-color1", ".color1", XrmoptionSepArg, 0 },
113 { "-color2", ".color2", XrmoptionSepArg, 0 },
114 { "-color3", ".color3", XrmoptionSepArg, 0 },
115 { "-color4", ".color4", XrmoptionSepArg, 0 },
116 { "-color5", ".color5", XrmoptionSepArg, 0 },
117 { "-color6", ".color6", XrmoptionSepArg, 0 },
118 { "-color7", ".color7", XrmoptionSepArg, 0 },
120 { "-xw", ".xw", XrmoptionSepArg, 0 },
121 { "-xy", ".xy", XrmoptionSepArg, 0 },
122 { "-xz", ".xz", XrmoptionSepArg, 0 },
123 { "-yw", ".yw", XrmoptionSepArg, 0 },
124 { "-yz", ".yz", XrmoptionSepArg, 0 },
125 { "-zw", ".zw", XrmoptionSepArg, 0 },
127 { "-observer-z", ".observer-z", XrmoptionSepArg, 0 },
128 { "-delay", ".delay", XrmoptionSepArg, 0 },
134 screenhack (Display *d, Window w)
136 struct hyper_state *hs;
149 init (struct hyper_state *hs)
155 unsigned long bg_pixel;
159 dpy = hs->hs_display;
162 observer_z = get_float_resource ("observer-z", "Float");
163 if (observer_z < 1.125)
165 /* hs->hs_observer_z = observer_z; */
166 hs->hs_two_observer_z = 2.0 * observer_z;
170 XWindowAttributes wa;
174 root = get_boolean_resource("root", "Boolean");
175 XGetWindowAttributes (dpy, win, &wa);
176 XSelectInput(dpy, win, root ? ExposureMask :
177 wa.your_event_mask | ExposureMask | ButtonPressMask);
182 set_sizes (hs, width, height);
185 delay = get_integer_resource ("delay", "Integer");
186 hs->hs_delay = delay;
188 bg_pixel = get_pixel_resource ("background", "Background", dpy, cmap);
193 unsigned long fg_pixel;
196 gcv.function = GXcopy;
197 gcv.foreground = bg_pixel;
198 black_gc = XCreateGC (dpy, win, GCForeground|GCFunction, &gcv);
199 fg_pixel = get_pixel_resource ("foreground", "Foreground", dpy, cmap);
200 gcv.foreground = fg_pixel ^ bg_pixel;
201 white_gc = XCreateGC (dpy, win, GCForeground|GCFunction, &gcv);
202 hs->hs_color_gcs[0] = black_gc;
203 hs->hs_color_gcs[1] = white_gc;
209 gcv.function = GXxor;
210 for (col = 0; col < 8; col++)
213 unsigned long fg_pixel;
216 sprintf (buffer, "color%d", col);
217 fg_pixel = get_pixel_resource (buffer, "Foreground", dpy, cmap);
218 gcv.foreground = fg_pixel ^ bg_pixel;
219 color_gc = XCreateGC (dpy, win, GCForeground|GCFunction, &gcv);
220 hs->hs_color_gcs[col] = color_gc;
224 hs->hs_ref_ax = 1.0, hs->hs_ref_ay = 0.0, hs->hs_ref_az = 0.0, hs->hs_ref_aw = 0.0;
225 hs->hs_ref_bx = 0.0, hs->hs_ref_by = 1.0, hs->hs_ref_bz = 0.0, hs->hs_ref_bw = 0.0;
226 hs->hs_ref_cx = 0.0, hs->hs_ref_cy = 0.0, hs->hs_ref_cz = 1.0, hs->hs_ref_cw = 0.0;
227 hs->hs_ref_dx = 0.0, hs->hs_ref_dy = 0.0, hs->hs_ref_dz = 0.0, hs->hs_ref_dw = 1.0;
236 double cos_xy, sin_xy;
237 double cos_xz, sin_xz;
238 double cos_yz, sin_yz;
239 double cos_xw, sin_xw;
240 double cos_yw, sin_yw;
241 double cos_zw, sin_zw;
243 xy = get_float_resource ("xy", "Float") * ANGLE_SCALE;
244 xz = get_float_resource ("xz", "Float") * ANGLE_SCALE;
245 yz = get_float_resource ("yz", "Float") * ANGLE_SCALE;
246 xw = get_float_resource ("xw", "Float") * ANGLE_SCALE;
247 yw = get_float_resource ("yw", "Float") * ANGLE_SCALE;
248 zw = get_float_resource ("zw", "Float") * ANGLE_SCALE;
250 cos_xy = cos (xy), sin_xy = sin (xy);
251 hs->hs_cos_xy = cos_xy, hs->hs_sin_xy = sin_xy;
252 cos_xz = cos (xz), sin_xz = sin (xz);
253 hs->hs_cos_xz = cos_xz, hs->hs_sin_xz = sin_xz;
254 cos_yz = cos (yz), sin_yz = sin (yz);
255 hs->hs_cos_yz = cos_yz, hs->hs_sin_yz = sin_yz;
256 cos_xw = cos (xw), sin_xw = sin (xw);
257 hs->hs_cos_xw = cos_xw, hs->hs_sin_xw = sin_xw;
258 cos_yw = cos (yw), sin_yw = sin (yw);
259 hs->hs_cos_yw = cos_yw, hs->hs_sin_yw = sin_yw;
260 cos_zw = cos (zw), sin_zw = sin (zw);
261 hs->hs_cos_zw = cos_zw, hs->hs_sin_zw = sin_zw;
267 hyper (struct hyper_state *hs)
277 char moved[POINT_COUNT];
285 resize = hs->hs_resize;
286 if (icon || !(roted | resize))
294 double az, bz, cz, dz;
296 double ax, bx, cx, dx;
298 double ay, by, cy, dy;
300 struct point_state *ps;
311 #define compute(as,bs,cs,ds,i) \
312 az = hs->hs_ref_az; bz = hs->hs_ref_bz; cz = hs->hs_ref_cz; dz = hs->hs_ref_dz; \
313 ax = hs->hs_ref_ax; bx = hs->hs_ref_bx; cx = hs->hs_ref_cx; dx = hs->hs_ref_dx; \
314 ay = hs->hs_ref_ay; by = hs->hs_ref_by; cy = hs->hs_ref_cy; dy = hs->hs_ref_dy; \
315 sum_z = as az bs bz cs cz ds dz; \
316 observer_z = hs->hs_two_observer_z; \
317 unit_scale = hs->hs_unit_scale; \
318 sum_x = as ax bs bx cs cx ds dx; \
319 sum_y = as ay bs by cs cy ds dy; \
320 ps = &hs->hs_points[i]; \
321 mul = unit_scale / (observer_z - sum_z); \
322 offset_x = hs->hs_offset_x; \
323 offset_y = hs->hs_offset_y; \
326 xf = sum_x * mul + offset_x; \
327 yf = sum_y * mul + offset_y; \
328 new_x = (int)rint(xf); \
329 new_y = (int)rint(yf); \
334 mov = old_x != new_x || old_y != new_y; \
337 compute (-, -, -, -, 0);
338 compute (-, -, -, +, 1);
339 compute (-, -, +, -, 2);
340 compute (-, -, +, +, 3);
341 compute (-, +, -, -, 4);
342 compute (-, +, -, +, 5);
343 compute (-, +, +, -, 6);
344 compute (-, +, +, +, 7);
345 compute (+, -, -, -, 8);
346 compute (+, -, -, +, 9);
347 compute (+, -, +, -, 10);
348 compute (+, -, +, +, 11);
349 compute (+, +, -, -, 12);
350 compute (+, +, -, +, 13);
351 compute (+, +, +, -, 14);
352 compute (+, +, +, +, 15);
357 redraw = hs->hs_redraw;
358 if (icon || !(roted | redraw))
363 const struct line_info *lip;
369 lip = &line_table[0];
371 dpy = hs->hs_display;
379 struct point_state *sp;
380 struct point_state *sq;
396 if (!(redraw | mov_p | mov_q))
399 sp = &hs->hs_points[ip];
400 sq = &hs->hs_points[iq];
404 erase_gc = hs->hs_color_gcs[0];
405 draw_gc = hs->hs_color_gcs[1];
411 color_gc = hs->hs_color_gcs[col];
422 XDrawLine (dpy, win, erase_gc, p_x, p_y, q_x, q_y);
429 XDrawLine (dpy, win, draw_gc, p_x, p_y, q_x, q_y);
451 /* If you get error messages about the following forms, and you think you're
452 using an ANSI C conforming compiler, then you're mistaken. Possibly you're
453 mixing an ANSI compiler with a non-ANSI preprocessor, or vice versa.
454 Regardless, your system is broken; it's not a bug in this program.
456 #if defined(__STDC__) || defined(__ANSI_CPP__)
458 #define rotate(name,dim0,dim1) \
459 old_u = hs->hs_ref_##name##dim0; \
460 old_v = hs->hs_ref_##name##dim1; \
461 new_u = old_u * cos_a + old_v * sin_a; \
462 new_v = old_v * cos_a - old_u * sin_a; \
463 hs->hs_ref_##name##dim0 = new_u; \
464 hs->hs_ref_##name##dim1 = new_v;
466 #define rotates(dim0,dim1) \
467 if (hs->hs_sin_##dim0##dim1 != 0) { \
468 cos_a = hs->hs_cos_##dim0##dim1; \
469 sin_a = hs->hs_sin_##dim0##dim1; \
470 rotate(a,dim0,dim1); \
471 rotate(b,dim0,dim1); \
472 rotate(c,dim0,dim1); \
473 rotate(d,dim0,dim1); \
476 #else /* !__STDC__, courtesy of Andreas Luik <luik@isa.de> */
478 #define rotate(name,dim0,dim1) \
479 old_u = hs->hs_ref_/**/name/**/dim0; \
480 old_v = hs->hs_ref_/**/name/**/dim1; \
481 new_u = old_u * cos_a + old_v * sin_a; \
482 new_v = old_v * cos_a - old_u * sin_a; \
483 hs->hs_ref_/**/name/**/dim0 = new_u; \
484 hs->hs_ref_/**/name/**/dim1 = new_v;
486 #define rotates(dim0,dim1) \
487 if (hs->hs_sin_/**/dim0/**/dim1 != 0) { \
488 cos_a = hs->hs_cos_/**/dim0/**/dim1; \
489 sin_a = hs->hs_sin_/**/dim0/**/dim1; \
490 rotate(a,dim0,dim1); \
491 rotate(b,dim0,dim1); \
492 rotate(c,dim0,dim1); \
493 rotate(d,dim0,dim1); \
496 #endif /* !__STDC__ */
507 /* stop = hs->hs_stop; */
508 delay = hs->hs_delay;
509 if (stop && delay < 10000)
518 check_events (struct hyper_state *hs)
527 dpy = hs->hs_display;
528 count = XEventsQueued (dpy, QueuedAfterReading);
537 XNextEvent (dpy, &e);
546 case ConfigureNotify:
553 switch (e.xbutton.button)
556 hs->hs_stop = !hs->hs_stop;
569 screenhack_handle_event(dpy, &e);
574 resize = hs->hs_resize;
578 XWindowAttributes wa;
582 XGetWindowAttributes (dpy, win, &wa);
585 set_sizes (&hyper_state, width, height);
588 redraw = hs->hs_redraw;
590 XClearWindow (dpy, win);
595 set_sizes (struct hyper_state *hs, int width, int height)
604 observer_z = 0.5 * hs->hs_two_observer_z;
605 min_dim = width < height ? width : height;
606 var = sqrt(observer_z * observer_z - 1.0);
607 offset_x = 0.5 * (double)(width - 1);
608 offset_y = 0.5 * (double)(height - 1);
609 unit_scale = 0.4 * min_dim * var;
610 hs->hs_offset_x = (float)offset_x;
611 hs->hs_offset_y = (float)offset_y;
612 hs->hs_unit_scale = (float)unit_scale;
618 static const struct line_info line_table[LINE_COUNT] =