From http://www.jwz.org/xscreensaver/xscreensaver-5.34.tar.gz
[xscreensaver] / android / XScreenSaverView.c
1 #include <stdio.h>
2 #include <string.h>
3 #include <zlib.h>
4 //#include <android/log.h>
5 #include "screenhackI.h"
6 #include "xlockmoreI.h"
7
8 #ifndef isupper
9 # define isupper(c)  ((c) >= 'A' && (c) <= 'Z')
10 #endif
11 #ifndef _tolower
12 # define _tolower(c)  ((c) - 'A' + 'a')
13 #endif
14
15 extern struct xscreensaver_function_table *xscreensaver_function_table;
16
17 extern const char *progname;
18 extern const char *progclass;
19 int mono_p = 0;
20
21 static char *hilbert_mode;
22 static char *sproingies_count;
23 static char *sproingies_wireframe;
24 static char *bouncingcow_count;
25 static char *bouncingcow_speed;
26 static char *superquadrics_spinspeed;
27 static char *stonerview_delay;
28 static char *stonerview_transparent;
29 static char *unknownpleasures_wireframe;
30 static char *unknownpleasures_speed;
31 static char *hypertorus_displayMode;
32 static char *hypertorus_appearance;
33 static char *hypertorus_colors;
34 static char *hypertorus_projection3d;
35 static char *hypertorus_projection4d;
36 static char *hypertorus_speedwx;
37 static char *hypertorus_speedwy;
38 static char *hypertorus_speedwz;
39 static char *hypertorus_speedxy;
40 static char *hypertorus_speedxz;
41 static char *hypertorus_speedyz;
42 static char *glhanoi_light;
43 static char *glhanoi_fog;
44 static char *glhanoi_trails;
45 static char *glhanoi_poles;
46 static char *glhanoi_speed;
47
48
49 Bool 
50 get_boolean_resource (Display *dpy, char *res_name, char *res_class)
51 {
52   char *tmp, buf [100];
53   char *s = get_string_resource (dpy, res_name, res_class);
54   char *os = s;
55   if (! s) return 0;
56   for (tmp = buf; *s; s++)
57     *tmp++ = isupper (*s) ? _tolower (*s) : *s;
58   *tmp = 0;
59   //free (os);
60
61   while (*buf &&
62          (buf[strlen(buf)-1] == ' ' ||
63           buf[strlen(buf)-1] == '\t'))
64     buf[strlen(buf)-1] = 0;
65
66   if (!strcmp (buf, "on") || !strcmp (buf, "true") || !strcmp (buf, "yes"))
67     return 1;
68   if (!strcmp (buf,"off") || !strcmp (buf, "false") || !strcmp (buf,"no"))
69     return 0;
70   fprintf (stderr, "%s: %s must be boolean, not %s.\n",
71            progname, res_name, buf);
72   return 0;
73 }
74
75 int 
76 get_integer_resource (Display *dpy, char *res_name, char *res_class)
77 {
78   int val;
79   char c, *s = get_string_resource (dpy, res_name, res_class);
80   char *ss = s;
81   if (!s) return 0;
82
83   while (*ss && *ss <= ' ') ss++;                       /* skip whitespace */
84
85   if (ss[0] == '0' && (ss[1] == 'x' || ss[1] == 'X'))   /* 0x: parse as hex */
86     {
87       if (1 == sscanf (ss+2, "%x %c", (unsigned int *) &val, &c))
88         {
89           //free (s);
90           return val;
91         }
92     }
93   else                                                  /* else parse as dec */
94     {
95       if (1 == sscanf (ss, "%d %c", &val, &c))
96         {
97           //free (s);
98           return val;
99         }
100     }
101
102   fprintf (stderr, "%s: %s must be an integer, not %s.\n",
103            progname, res_name, s);
104   //free (s);
105   return 0;
106 }
107
108 double
109 get_float_resource (Display *dpy, char *res_name, char *res_class)
110 {
111   double val;
112   char c, *s = get_string_resource (dpy, res_name, res_class);
113   if (! s) return 0.0;
114   if (1 == sscanf (s, " %lf %c", &val, &c))
115     {
116       //free (s);
117       return val;
118     }
119   fprintf (stderr, "%s: %s must be a float, not %s.\n",
120            progname, res_name, s);
121   //free (s);
122   return 0.0;
123 }
124
125
126 // TODO: fill in what is not here
127 char *get_string_resource(Display * dpy, char *name, char *class)
128 {
129     char *implement;
130     //__android_log_print (ANDROID_LOG_INFO, "xscreensaver", "s %s %s %s", progname, name, class); 
131
132     if (strcmp(progname, "hilbert") == 0) {
133         if (strcmp(name, "mode") == 0 && strcmp(class, "Mode") == 0) {
134             implement = malloc(8 * sizeof(char));
135             strcpy(implement, hilbert_mode);
136         } else if (strcmp(name, "ends") == 0 && strcmp(class, "Ends") == 0) {
137             implement = malloc(5 * sizeof(char));
138             strcpy(implement, "open");
139         } else if (strcmp(name, "speed") == 0 && strcmp(class, "Speed") == 0) {
140             return "1.0";
141         } else if (strcmp(name, "thickness") == 0
142                    && strcmp(class, "Thickness") == 0) {
143             return "0.25";
144         } else if (strcmp(name, "delay") == 0 && strcmp(class, "Usecs") == 0) {
145             return "30000";
146         } else if (strcmp(name, "maxDepth") == 0
147             && strcmp(class, "MaxDepth") == 0) {
148             // too deep is too much for less powerful Android phones
149             return "3";
150             //return 5;
151         } else if (strcmp(name, "spin") == 0 && strcmp(class, "Spin") == 0) {
152             return "True";
153         } else if (strcmp(name, "wireframe") == 0
154                    && strcmp(class, "Boolean") == 0) {
155             return "False";
156         } else if (strcmp(name, "wander") == 0
157                    && strcmp(class, "Wander") == 0) {
158             return "False";
159         } else {
160             return 0;
161         }
162     }
163     else if (strcmp(progname, "sproingies") == 0) {
164         if (strcmp(name, "count") == 0 && strcmp(class, "Int") == 0) {
165             return sproingies_count;
166         } else if (strcmp(name, "wireframe") == 0
167             && strcmp(class, "Boolean") == 0) {
168             return sproingies_wireframe;
169         } else {
170             return 0;
171         }
172     }
173     else if (strcmp(progname, "superquadrics") == 0) {
174         if (strcmp(name, "spinspeed") == 0
175             && strcmp(class, "Spinspeed") == 0) {
176             return superquadrics_spinspeed;
177         } else if (strcmp(name, "count") == 0 && strcmp(class, "Int") == 0) {
178             return "25";
179         } else if (strcmp(name, "cycles") == 0
180                    && strcmp(class, "Int") == 0) {
181             return "40";
182         } else if (strcmp(name, "delay") == 0
183                    && strcmp(class, "Usecs") == 0) {
184             return "40000";
185         } else if (strcmp(name, "wireframe") == 0
186             && strcmp(class, "Boolean") == 0) {
187             return "False";
188         } else {
189             return 0;
190         }
191     }
192     else if (strcmp(progname, "stonerview") == 0) {
193         if (strcmp(name, "use3d") == 0 && strcmp(class, "Boolean") == 0) {
194             return "True";
195         } else if (strcmp(name, "transparent") == 0
196                    && strcmp(class, "Transparent") == 0) {
197             return stonerview_transparent;
198         } else if (strcmp(name, "wireframe") == 0
199                    && strcmp(class, "Boolean") == 0) {
200             return "False";
201         } else if (strcmp(name, "doFPS") == 0
202                    && strcmp(class, "DoFPS") == 0) {
203             return "False";
204         } else {
205             return 0;
206         }
207     }
208     else if (strcmp(progname, "bouncingcow") == 0) {
209         if (strcmp(name, "count") == 0
210             && strcmp(class, "Int") == 0) {
211             return bouncingcow_count;
212         } else if (strcmp(name, "speed") == 0
213             && strcmp(class, "Speed") == 0) {
214             return bouncingcow_speed;
215         } else {
216             return 0;
217         }
218     }
219     else if (strcmp(progname, "unknownpleasures") == 0) {
220
221         if (strcmp(name, "wireframe") == 0) {
222           return unknownpleasures_wireframe;
223         } else if (strcmp(name, "speed") == 0) {
224           return unknownpleasures_speed;
225         } else if (strcmp(name, "count") == 0) {
226           return "80";
227         } else if (strcmp(name, "resolution") == 0) {
228           return "100";
229           //return "200";
230         } else if (strcmp(name, "ortho") == 0) {
231           return "True";
232           //return "False";
233         } else {
234           return 0;
235         }
236
237     }
238     else if (strcmp(progname, "hypertorus") == 0) {
239         if (strcmp(name, "displayMode") == 0) {
240             return hypertorus_displayMode;
241         } else if (strcmp(name, "appearance") == 0) {
242             return hypertorus_appearance;
243         } else if (strcmp(name, "colors") == 0) {
244             return hypertorus_colors;
245         } else if (strcmp(name, "projection3d") == 0) {
246             return hypertorus_projection3d;
247         } else if (strcmp(name, "projection4d") == 0) {
248             return hypertorus_projection4d;
249         } else if (strcmp(name, "speedwx") == 0) {
250             return hypertorus_speedwz;
251         } else if (strcmp(name, "speedwy") == 0) {
252             return hypertorus_speedwy;
253         } else if (strcmp(name, "speedwz") == 0) {
254             return hypertorus_speedwz;
255         } else if (strcmp(name, "speedxy") == 0) {
256             return hypertorus_speedxy;
257         } else if (strcmp(name, "speedxz") == 0) {
258             return hypertorus_speedxz;
259         } else if (strcmp(name, "speedyz") == 0) {
260             return hypertorus_speedyz;
261         } else {
262             return 0;
263         }
264     }
265     else if (strcmp(progname, "glhanoi") == 0) {
266         if (strcmp(name, "light") == 0) {
267             return glhanoi_light;
268         } else if (strcmp(name, "fog") == 0) {
269             return glhanoi_fog;
270         } else if (strcmp(name, "trails") == 0) {
271             return glhanoi_trails;
272         } else if (strcmp(name, "poles") == 0) {
273             return glhanoi_poles;
274         } else if (strcmp(name, "speed") == 0) {
275             return glhanoi_speed;
276         } else {
277             return 0;
278         }
279     }
280     else {
281         implement = 0;
282     }
283
284     return implement;
285 }
286
287
288 void setSuperquadricsSettings(char *hck, char *khck)
289 {
290     if (strcmp(khck, "superquadrics_spinspeed") == 0) {
291         superquadrics_spinspeed = malloc(4 * sizeof(char));
292         strcpy(superquadrics_spinspeed, hck);
293     }
294 }
295
296 void setHilbertSettings(char *hck, char *khck)
297 {
298     if (strcmp(khck, "hilbert_mode") == 0) {
299         if (!hilbert_mode) {
300             hilbert_mode = malloc(8 * sizeof(char));
301         }
302         if (strcmp(hck, "3D") == 0) {
303             strcpy(hilbert_mode, "3D");
304         }
305         else if (strcmp(hck, "2D") == 0) {
306             strcpy(hilbert_mode, "2D");
307         }
308     }
309 }
310
311 void setSproingiesSettings(char *hck, char *khck)
312 {
313     if (strcmp(khck, "sproingies_count") == 0) {
314         sproingies_count = malloc(3 * sizeof(char));
315         strcpy(sproingies_count, hck);
316     }
317     else if (strcmp(khck, "sproingies_wireframe") == 0) {
318         sproingies_wireframe = malloc(6 * sizeof(char));
319         strcpy(sproingies_wireframe, hck);
320     }
321 }
322
323 void setStonerviewSettings(char *hck, char *khck)
324 {
325     if (strcmp(khck, "stonerview_transparent") == 0) {
326         stonerview_transparent = malloc(6 * sizeof(char));
327         strcpy(stonerview_transparent, hck);
328     }
329 }
330
331 void setBouncingcowSettings(char *hck, char *khck)
332 {
333     if (strcmp(khck, "bouncingcow_count") == 0) {
334         bouncingcow_count = malloc(3 * sizeof(char));
335         strcpy(bouncingcow_count, hck);
336     }
337     else if (strcmp(khck, "bouncingcow_speed") == 0) {
338         bouncingcow_speed = malloc(4 * sizeof(char));
339         strcpy(bouncingcow_speed, hck);
340     }
341 }
342
343 void setUnknownpleasuresSettings(char *hck, char *khck)
344 {
345     if (strcmp(khck, "unknownpleasures_speed") == 0) {
346         unknownpleasures_speed = malloc(3 * sizeof(char));
347         strcpy(unknownpleasures_speed, hck);
348     }
349     else if (strcmp(khck, "unknownpleasures_wireframe") == 0) {
350         unknownpleasures_wireframe = malloc(6 * sizeof(char));
351         strcpy(unknownpleasures_wireframe, hck);
352     }
353 }
354
355 void setHypertorusSettings(char *hck, char *khck)
356 {
357     if (strcmp(khck, "hypertorus_displayMode") == 0) {
358         hypertorus_displayMode = malloc(13 * sizeof(char));
359         strcpy(hypertorus_displayMode, hck);
360     }
361     else if (strcmp(khck, "hypertorus_appearance") == 0) {
362         hypertorus_appearance = malloc(12 * sizeof(char));
363         strcpy(hypertorus_appearance, hck);
364     }
365     else if (strcmp(khck, "hypertorus_colors") == 0) {
366         hypertorus_colors = malloc(5 * sizeof(char));
367         strcpy(hypertorus_colors, hck);
368     }
369     else if (strcmp(khck, "hypertorus_projection3d") == 0) {
370         hypertorus_projection3d = malloc(17 * sizeof(char));
371         strcpy(hypertorus_projection3d, hck);
372     }
373     else if (strcmp(khck, "hypertorus_projection4d") == 0) {
374         hypertorus_projection4d = malloc(17 * sizeof(char));
375         strcpy(hypertorus_projection4d, hck);
376     }
377     else if (strcmp(khck, "hypertorus_speedwx") == 0) {
378         hypertorus_speedwx = malloc(5 * sizeof(char));
379         strcpy(hypertorus_speedwx, hck);
380     }
381     else if (strcmp(khck, "hypertorus_speedwy") == 0) {
382         hypertorus_speedwy = malloc(5 * sizeof(char));
383         strcpy(hypertorus_speedwy, hck);
384     }
385     else if (strcmp(khck, "hypertorus_speedwz") == 0) {
386         hypertorus_speedwz = malloc(5 * sizeof(char));
387         strcpy(hypertorus_speedwz, hck);
388     }
389     else if (strcmp(khck, "hypertorus_speedxy") == 0) {
390         hypertorus_speedxy = malloc(5 * sizeof(char));
391         strcpy(hypertorus_speedxy, hck);
392     }
393     else if (strcmp(khck, "hypertorus_speedxz") == 0) {
394         hypertorus_speedxz = malloc(5 * sizeof(char));
395         strcpy(hypertorus_speedxz, hck);
396     }
397     else if (strcmp(khck, "hypertorus_speedyz") == 0) {
398         hypertorus_speedyz = malloc(5 * sizeof(char));
399         strcpy(hypertorus_speedyz, hck);
400     }
401 }
402
403 void setGlhanoiSettings(char *hck, char *khck) {
404
405     if (strcmp(khck, "glhanoi_light") == 0) {
406         glhanoi_light = malloc(6 * sizeof(char));
407         strcpy(glhanoi_light , hck);
408     }
409     else if (strcmp(khck, "glhanoi_fog") == 0) {
410         glhanoi_fog = malloc(6 * sizeof(char));
411         strcpy(glhanoi_fog , hck);
412     }
413     else if (strcmp(khck, "glhanoi_trails") == 0) {
414         glhanoi_trails = malloc(3 * sizeof(char));
415         strcpy(glhanoi_trails , hck);
416     }
417     else if (strcmp(khck, "glhanoi_poles") == 0) {
418         glhanoi_poles = malloc(3 * sizeof(char));
419         strcpy(glhanoi_poles , hck);
420     }
421     else if (strcmp(khck, "glhanoi_speed") == 0) {
422         glhanoi_speed = malloc(3 * sizeof(char));
423         strcpy(glhanoi_speed , hck);
424     }
425 }