2 # Copyright © 2008-2016 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 parses the .c and .xml files and makes sure they are in sync: that
13 # options are spelled the same, and that all the numbers are in sync.
15 # It also converts the hacks/config/ XML files into the Android XML files.
17 # Created: 1-Aug-2008.
23 my $progname = $0; $progname =~ s@.*/@@g;
24 my ($version) = ('$Revision: 1.22 $' =~ m/\s(\d[.\d]+)\s/s);
30 my $text_default_opts = '';
31 foreach (qw(text-mode text-literal text-file text-url text-program)) {
32 my $s = $_; $s =~ s/-(.)/\U$1/g; $s =~ s/url/URL/si;
33 $text_default_opts .= "{\"-$_\", \".$s\", XrmoptionSepArg, 0},\n";
35 my $image_default_opts = '';
36 foreach (qw(choose-random-images grab-desktop-images)) {
37 my $s = $_; $s =~ s/-(.)/\U$1/g;
38 $image_default_opts .= "{\"-$_\", \".$s\", XrmoptionSepArg, 0},\n";
40 my $xlockmore_default_opts = '';
41 foreach (qw(count cycles delay ncolors size font)) {
42 $xlockmore_default_opts .= "{\"-$_\", \".$_\", XrmoptionSepArg, 0},\n";
44 $xlockmore_default_opts .=
45 "{\"-wireframe\", \".wireframe\", XrmoptionNoArg, \"true\"},\n" .
46 "{\"-3d\", \".use3d\", XrmoptionNoArg, \"true\"},\n" .
47 "{\"-no-3d\", \".use3d\", XrmoptionNoArg, \"false\"},\n";
49 my $thread_default_opts =
50 "{\"-threads\", \".useThreads\", XrmoptionNoArg, \"True\"},\n" .
51 "{\"-no-threads\", \".useThreads\", XrmoptionNoArg, \"False\"},\n";
53 my $analogtv_default_opts = '';
54 foreach (qw(color tint brightness contrast)) {
55 $analogtv_default_opts .= "{\"-tv-$_\", \".TV$_\", XrmoptionSepArg, 0},\n";
58 $analogtv_default_opts .= $thread_default_opts;
63 # - A table of the default resource values.
64 # - A table of "-switch" => "resource: value", or "-switch" => "resource: %"
68 my $file = lc($saver) . ".c";
71 $file = 'apple2-main.c' if ($file eq 'apple2.c');
72 $file = 'sproingiewrap.c' if ($file eq 'sproingies.c');
73 $file = 'b_lockglue.c' if ($file eq 'bubble3d.c');
74 $file = 'polyhedra-gl.c' if ($file eq 'polyhedra.c');
75 $file = 'companion.c' if ($file eq 'companioncube.c');
76 $file = 'rd-bomb.c' if ($file eq 'rdbomb.c');
79 $file = "glx/$ofile" unless (-f $file);
80 $file = "../hacks/$ofile" unless (-f $file);
81 $file = "../hacks/glx/$ofile" unless (-f $file);
83 open (my $in, '<', $file) || error ("$ofile: $!");
84 while (<$in>) { $body .= $_; }
89 my $thread_p = ($body =~ m/THREAD_DEFAULTS/);
90 my $analogtv_p = ($body =~ m/ANALOGTV_DEFAULTS/);
91 my $text_p = ($body =~ m/"textclient\.h"/);
92 my $grab_p = ($body =~ m/load_image_async/);
94 $body =~ s@/\*.*?\*/@@gs;
95 $body =~ s@^#\s*(if|ifdef|ifndef|elif|else|endif).*$@@gm;
96 $body =~ s/(THREAD|ANALOGTV)_(DEFAULTS|OPTIONS)(_XLOCK)?//gs;
97 $body =~ s/__extension__//gs;
99 print STDERR "$progname: $file: defaults:\n" if ($verbose > 2);
101 if ($body =~ m/_defaults\s*\[\]\s*=\s*{(.*?)}\s*;/s) {
102 foreach (split (/,\s*\n/, $1)) {
106 my ($key, $val) = m@^\"([^:\s]+)\s*:\s*(.*?)\s*\"$@;
107 print STDERR "$progname: $file: unparsable: $_\n" unless $key;
109 $res_to_val{$key} = $val;
110 print STDERR "$progname: $file: $key = $val\n" if ($verbose > 2);
112 } elsif ($body =~ m/\#\s*define\s*DEFAULTS\s*\\?\s*(.*?)\n[\n#]/s) {
115 $str =~ s/\"\s*\\\n\s*\"//gs;
116 $str =~ m/^\s*\"(.*?)\"\s*\\?\s*$/ ||
117 error ("$file: unparsable defaults: $str");
119 $str =~ s/\s*\\n\s*/\n/gs;
120 foreach (split (/\n/, $str)) {
121 my ($key, $val) = m@^([^:\s]+)\s*:\s*(.*?)\s*$@;
122 print STDERR "$progname: $file: unparsable: $_\n" unless $key;
124 $val =~ s/"\s*"\s*$//s;
125 $res_to_val{$key} = $val;
126 print STDERR "$progname: $file: $key = $val\n" if ($verbose > 2);
129 while ($body =~ s/^#\s*define\s+(DEF_([A-Z\d_]+))\s+\"([^\"]+)\"//m) {
130 my ($key1, $key2, $val) = ($1, lc($2), $3);
131 $key2 =~ s/_(.)/\U$1/gs; # "foo_bar" -> "fooBar"
132 $key2 =~ s/Rpm/RPM/; # kludge
133 $res_to_val{$key2} = $val;
134 print STDERR "$progname: $file: $key1 ($key2) = $val\n"
139 error ("$file: no defaults");
142 $body =~ m/XSCREENSAVER_MODULE(_2)?\s*\(\s*\"([^\"]+)\"/ ||
143 error ("$file: no module name");
144 $res_to_val{progclass} = $2;
145 $res_to_val{doFPS} = 'false';
146 $res_to_val{textMode} = 'date';
147 $res_to_val{textLiteral} = '';
148 $res_to_val{textURL} =
149 'https://en.wikipedia.org/w/index.php?title=Special:NewPages&feed=rss';
150 $res_to_val{grabDesktopImages} = 'true';
151 $res_to_val{chooseRandomImages} = 'true';
153 print STDERR "$progname: $file: progclass = $2\n" if ($verbose > 2);
155 print STDERR "$progname: $file: switches to resources:\n"
158 $switch_to_res{'-fps'} = 'doFPS: true';
159 $switch_to_res{'-fg'} = 'foreground: %';
160 $switch_to_res{'-bg'} = 'background: %';
161 $switch_to_res{'-no-grab-desktop-images'} = 'grabDesktopImages: false';
162 $switch_to_res{'-no-choose-random-images'} = 'chooseRandomImages: false';
164 my ($ign, $opts) = ($body =~ m/(_options|\bopts)\s*\[\]\s*=\s*{(.*?)}\s*;/s);
165 if ($xlockmore_p || $thread_p || $analogtv_p || $opts) {
166 $opts = '' unless $opts;
167 $opts .= ",\n$text_default_opts" if ($text_p);
168 $opts .= ",\n$image_default_opts" if ($grab_p);
169 $opts .= ",\n$xlockmore_default_opts" if ($xlockmore_p);
170 $opts .= ",\n$thread_default_opts" if ($thread_p);
171 $opts .= ",\n$analogtv_default_opts" if ($analogtv_p);
173 foreach (split (/,\s*\n/, $opts)) {
177 next if m/^\{\s*0\s*,/s;
178 my ($switch, $res, $type, $v0, $v1, $v2) =
179 m@^ \s* { \s * \"([^\"]+)\" \s* ,
180 \s * \"([^\"]+)\" \s* ,
182 \s * (\"([^\"]*)\"|([a-zA-Z\d_]+)) \s* }@xi;
183 print STDERR "$progname: $file: unparsable: $_\n" unless $switch;
184 my $val = defined($v1) ? $v1 : $v2;
185 $val = '%' if ($type eq 'XrmoptionSepArg');
187 $res =~ s/^[a-z\d]+\.//si;
188 $switch =~ s/^\+/-no-/s;
191 if (defined ($switch_to_res{$switch})) {
192 print STDERR "$progname: $file: DUP! $switch = \"$val\"\n"
195 $switch_to_res{$switch} = $val;
196 print STDERR "$progname: $file: $switch = \"$val\"\n"
201 error ("$file: no options");
204 return (\%res_to_val, \%switch_to_res);
210 # "resource = default value"
211 # or "resource != non-default value"
213 # Also a hash of the simplified XML contents.
216 my ($saver, $switch_to_res, $src_opts) = @_;
218 my $saver_title = undef;
220 my $file = "config/" . lc($saver) . ".xml";
222 $file = "../hacks/$ofile" unless (-f $file);
224 open (my $in, '<', $file) || error ("$ofile: $!");
225 while (<$in>) { $body .= $_; }
231 $body =~ s@<xscreensaver-text\s*/?>@
232 <select id="textMode">
233 <option id="date" _label="Display the date and time"/>
234 <option id="text" _label="Display static text"
235 arg-set="-text-mode literal"/>
236 <option id="url" _label="Display the contents of a URL"
237 arg-set="-text-mode url"/>
239 <string id="textLiteral" _label="Text to display" arg="-text-literal %"/>
240 <string id="textURL" _label="URL to display" arg="-text-url %"/>
243 $body =~ s@<xscreensaver-image\s*/?>@
244 <boolean id="grabDesktopImages" _label="Grab screenshots"
245 arg-unset="-no-grab-desktop-images"/>
246 <boolean id="chooseRandomImages" _label="Use photo library"
247 arg-unset="-no-choose-random-images"/>
250 $body =~ s/<!--.*?-->/ /gsi;
252 $body =~ s@(<(_description)>.*?</\2>)@{ $_ = $1; s/\n/\002/gs; $_; }@gsexi;
255 $body =~ s/</\001</gs;
256 $body =~ s/\001(<option)/$1/gs;
262 print STDERR "$progname: $file: options:\n" if ($verbose > 2);
263 foreach (split (m/\001/, $body)) {
265 my ($type, $args) = m@^<([?/]?[-_a-z]+)\b\s*(.*)$@si;
266 error ("$progname: $file: unparsable: $_") unless $type;
267 next if ($type =~ m@^/@);
269 my $ctrl = { type => $type };
271 if ($type =~ m/^( [hv]group |
280 } elsif ($type eq '_description') {
281 $args =~ s/\002/\n/gs;
283 $args =~ s/^\n*|\s*$//gs;
284 $ctrl->{text} = $args;
286 } elsif ($type eq 'screensaver') {
287 ($saver_title) = ($args =~ m/\b_label\s*=\s*\"([^\"]+)\"/s);
288 ($gl_p) = ($args =~ m/\bgl="?yes/s);
289 my $s = $saver_title;
291 my $val = "progclass = $s";
293 print STDERR "$progname: $file: name: $saver_title\n"
297 } elsif ($type eq 'video') {
298 error ("$file: multiple videos") if $video;
299 ($video) = ($args =~ m/\bhref="(.*?)"/);
300 error ("$file: unparsable video") unless $video;
301 error ("$file: unparsable video URL")
302 unless ($video =~ m@^https?://www\.youtube\.com/watch\?v=[^?&]+$@s);
305 } elsif ($type eq 'select') {
306 $args =~ s/</\001</gs;
307 my @opts = split (/\001/, $args);
310 my $this_res = undef;
313 error ("$file: unparsable option: $_") unless (m/^<option\s/);
317 $opt =~ s@^<option\s+@@s;
318 $opt =~ s@[?/]>\s*$@@s;
319 while ($opt =~ s/^\s*([^\s]+)\s*=\s*"(.*?)"\s*(.*)/$3/s) {
320 my ($k, $v) = ($1, $2);
324 error ("unparsable XML option line: $_ [$opt]") if ($opt);
327 my ($set) = $item{'arg-set'};
329 my ($set2, $val) = ($set =~ m/^(.*?) (.*)$/s);
330 $set = $set2 if ($set2);
331 my ($res) = $switch_to_res->{$set};
332 error ("$file: no resource for select switch \"$set\"") unless $res;
334 my ($res2, $val2) = ($res =~ m/^(.*?): (.*)$/s);
335 error ("$file: unparsable select resource: $res") unless $res2;
337 $val = $val2 unless ($val2 eq '%');
340 error ("$file: mismatched resources: $res vs $this_res")
341 if (defined($this_res) && $this_res ne $res);
344 $val = "$res != $val";
346 print STDERR "$progname: $file: select: $val\n" if ($verbose > 2);
349 error ("$file: multiple default options: $set") if ($unset_p);
353 $ctrl->{resource} = $this_res;
354 $ctrl->{default} = $src_opts->{$this_res};
355 $ctrl->{menu} = \@menu;
360 $rest =~ s@[/?]*>\s*$@@s;
361 while ($rest =~ s/^\s*([^\s]+)\s*=\s*"(.*?)"\s*(.*)/$3/s) {
362 my ($k, $v) = ($1, $2);
365 error ("unparsable XML line: $args [$rest]") if ($rest);
367 if ($type eq 'number') {
368 my ($arg) = $ctrl->{arg};
369 my ($val) = $ctrl->{default};
370 $val = '' unless defined($val);
373 $switch =~ s/\s+.*$//;
374 my ($res) = $switch_to_res->{$switch};
375 error ("$file: no resource for $type switch \"$arg\"") unless $res;
378 error ("$file: unparsable value: $res") if ($res =~ m/:/);
379 $ctrl->{resource} = $res;
381 $val = "$res = $val";
383 print STDERR "$progname: $file: number: $val\n" if ($verbose > 2);
385 } elsif ($type eq 'boolean') {
386 my ($set) = $ctrl->{'arg-set'};
387 my ($unset) = $ctrl->{'arg-unset'};
388 my ($arg) = $set || $unset || error ("$file: unparsable: $args");
389 my ($res) = $switch_to_res->{$arg};
390 error ("$file: no resource for boolean switch \"$arg\"") unless $res;
392 my ($res2, $val) = ($res =~ m/^(.*?): (.*)$/s);
393 error ("$file: unparsable boolean resource: $res") unless $res2;
396 $ctrl->{resource} = $res;
397 $ctrl->{convert} = 'invert' if ($val =~ m/false/i);
398 $ctrl->{default} = ($ctrl->{convert} ? 'true' : 'false');
400 # $val = ($set ? "$res != $val" : "$res = $val");
401 $val = "$res != $val";
403 print STDERR "$progname: $file: boolean: $val\n" if ($verbose > 2);
405 } elsif ($type eq 'string') {
406 my ($arg) = $ctrl->{arg};
409 $switch =~ s/\s+.*$//;
410 my ($res) = $switch_to_res->{$switch};
411 error ("$file: no resource for $type switch \"$arg\"") unless $res;
414 error ("$file: unparsable value: $res") if ($res =~ m/:/);
415 $ctrl->{resource} = $res;
416 $ctrl->{default} = $src_opts->{$res};
417 my $val = "$res = %";
419 print STDERR "$progname: $file: string: $val\n" if ($verbose > 2);
422 error ("$file: unknown type \"$type\" for no arg");
426 push @widgets, $ctrl if $ctrl;
429 # error ("$file: no video") unless $video;
430 print STDERR "\n$file: WARNING: no video\n\n" unless $video;
432 if ($video && $video_dups{$video} &&
433 $video_dups{$video} ne $saver_title) {
434 print STDERR "\n$file: WARNING: $saver_title: dup video with " .
435 $video_dups{$video} . "\n";
437 $video_dups{$video} = $saver_title if ($video);
439 return ($saver_title, $gl_p, \@result, \@widgets);
443 sub check_config($) {
447 return 0 if ($saver =~ m/(-helper)$/);
449 my ($src_opts, $switchmap) = parse_src ($saver);
450 my ($saver_title, $gl_p, $xml_opts, $widgets) =
451 parse_xml ($saver, $switchmap, $src_opts);
454 foreach my $claim (@$xml_opts) {
455 my ($res, $compare, $xval) = ($claim =~ m/^(.*) (=|!=) (.*)$/s);
456 error ("$saver: unparsable xml claim: $claim") unless $compare;
458 my $sval = $src_opts->{$res};
459 if ($res =~ m/^TV|^text-mode/) {
460 print STDERR "$progname: $saver: OK: skipping \"$res\"\n"
462 } elsif (!defined($sval)) {
463 print STDERR "$progname: $saver: $res: not in source\n";
464 } elsif ($claim !~ m/ = %$/s &&
468 print STDERR "$progname: $saver: " .
469 "src has \"$res = $sval\", xml has \"$claim\"\n";
471 } elsif ($verbose > 1) {
472 print STDERR "$progname: $saver: OK: \"$res = $sval\" vs \"$claim\"\n";
476 # Now make sure the progclass in the source and XML also matches
477 # the XCode target name.
479 my $obd = "../OSX/build/Debug";
481 my $progclass = $src_opts->{progclass};
482 $progclass = 'DNAlogo' if ($progclass eq 'DNALogo');
483 my $f = (glob("$obd/$progclass.saver*"))[0];
484 if (!$f && $progclass ne 'Flurry') {
485 print STDERR "$progname: $progclass.saver does not exist\n";
490 print STDERR "$progname: $saver: OK\n"
491 if ($verbose == 1 && $failures == 0);
497 # Returns true if the two files differ (by running "cmp")
500 my ($file1, $file2) = @_;
502 my @cmd = ("cmp", "-s", "$file1", "$file2");
503 print STDERR "$progname: executing \"" . join(" ", @cmd) . "\"\n"
507 my $exit_value = $? >> 8;
508 my $signal_num = $? & 127;
509 my $dumped_core = $? & 128;
511 error ("$cmd[0]: core dumped!") if ($dumped_core);
512 error ("$cmd[0]: signal $signal_num!") if ($signal_num);
518 my ($file1, $file2) = @_;
523 "--unidirectional-new-file", "$file1", "$file2");
524 print STDERR "$progname: executing \"" . join(" ", @cmd) . "\"\n"
528 my $exit_value = $? >> 8;
529 my $signal_num = $? & 127;
530 my $dumped_core = $? & 128;
532 error ("$cmd[0]: core dumped!") if ($dumped_core);
533 error ("$cmd[0]: signal $signal_num!") if ($signal_num);
538 # If the two files differ:
543 sub rename_or_delete($$;$) {
544 my ($file, $file_tmp, $suffix_msg) = @_;
546 my $changed_p = cmp_files ($file, $file_tmp);
548 if ($changed_p && $debug_p) {
549 print STDOUT "\n" . ('#' x 79) . "\n";
550 diff_files ("$file", "$file_tmp");
556 if (!rename ("$file_tmp", "$file")) {
558 error ("mv $file_tmp $file: $!");
560 print STDERR "$progname: wrote $file" .
561 ($suffix_msg ? " $suffix_msg" : "") . "\n";
564 unlink "$file_tmp" || error ("rm $file_tmp: $!\n");
565 print STDERR "$file unchanged" .
566 ($suffix_msg ? " $suffix_msg" : "") . "\n"
568 print STDERR "$progname: rm $file_tmp\n" if ($verbose > 2);
573 # Write the given body to the file, but don't alter the file's
574 # date if the new content is the same as the existing content.
576 sub write_file_if_changed($$;$) {
577 my ($outfile, $body, $suffix_msg) = @_;
579 my $file_tmp = "$outfile.tmp";
580 open (my $out, '>', $file_tmp) || error ("$file_tmp: $!");
581 (print $out $body) || error ("$file_tmp: $!");
582 close $out || error ("$file_tmp: $!");
583 rename_or_delete ($outfile, $file_tmp, $suffix_msg);
587 # Read the template file and splice in the @KEYWORDS@ in the hash.
589 sub read_template($$) {
590 my ($file, $subs) = @_;
592 open (my $in, '<', $file) || error ("$file: $!");
593 while (<$in>) { $body .= $_; }
596 $body =~ s@/\*.*?\*/@@gs; # omit comments
597 $body =~ s@//.*$@@gm;
599 foreach my $key (keys %$subs) {
600 my $val = $subs->{$key};
601 $body =~ s/@\Q$key\E@/$val/gs;
604 if ($body =~ m/(@[-_A-Z\d]+@)/s) {
605 error ("$file: unmatched: $1 [$body]");
608 $body =~ s/[ \t]+$//gm;
609 $body =~ s/(\n\n)\n+/$1/gs;
614 # This is duplicated in OSX/update-info-plist.pl
616 sub munge_blurb($$$$) {
617 my ($filename, $name, $vers, $desc) = @_;
619 $desc =~ s/^([ \t]*\n)+//s;
622 # in case it's done already...
623 $desc =~ s@<!--.*?-->@@gs;
624 $desc =~ s/^.* version \d[^\n]*\n//s;
625 $desc =~ s/^From the XScreenSaver.*\n//m;
626 $desc =~ s@^https://www\.jwz\.org/xscreensaver.*\n@@m;
628 s/\nCopyright [^ \r\n\t]+ (\d{4})(-\d{4})? (.*)\.$/\nWritten $3; $1./s;
631 error ("$filename: description contains markup: $1")
632 if ($desc =~ m/([<>&][^<>&\s]*)/s);
633 error ("$filename: description contains ctl chars: $1")
634 if ($desc =~ m/([\000-\010\013-\037])/s);
636 error ("$filename: can't extract authors")
637 unless ($desc =~ m@^(.*)\nWritten by[ \t]+(.+)$@s);
643 if ($authors =~ m@^(.*?)\s*[,;]\s+(\d\d\d\d)([-\s,;]+\d\d\d\d)*[.]?$@s) {
648 error ("$filename: can't extract year") unless $year;
649 my $cyear = 1900 + ((localtime())[5]);
650 $year = "$cyear" unless $year;
651 if ($year && ! ($year =~ m/$cyear/)) {
652 $year = "$year-$cyear";
655 $authors =~ s/[.,;\s]+$//s;
657 # List me as a co-author on all of them, since I'm the one who
658 # did the OSX port, packaged it up, and built the executables.
660 my $curator = "Jamie Zawinski";
661 if (! ($authors =~ m/$curator/si)) {
662 if ($authors =~ m@^(.*?),? and (.*)$@s) {
663 $authors = "$1, $2, and $curator";
665 $authors .= " and $curator";
669 my $desc1 = ("$name, version $vers.\n\n" . # savername.xml
672 "From the XScreenSaver collection: " .
673 "https://www.jwz.org/xscreensaver/\n" .
674 "Copyright \302\251 $year by $authors.\n");
676 my $desc2 = ("$name $vers,\n" . # Info.plist
677 "\302\251 $year $authors.\n" .
678 #"From the XScreenSaver collection:\n" .
679 #"https://www.jwz.org/xscreensaver/\n" .
684 # unwrap lines, but only when it's obviously ok: leave blank lines,
685 # and don't unwrap if that would compress leading whitespace on a line.
687 $desc2 =~ s/^(From |https?:)/\n$1/gm;
688 1 while ($desc2 =~ s/([^\s])[ \t]*\n([^\s])/$1 $2/gs);
689 $desc2 =~ s/\n\n(From |https?:)/\n$1/gs;
691 return ($desc1, $desc2);
695 sub build_android(@) {
698 my $package = "org.jwz.xscreensaver";
699 my $project_dir = "project/xscreensaver";
700 my $xml_dir = "$project_dir/res/xml";
701 my $values_dir = "$project_dir/res/values";
702 my $java_dir = "$project_dir/src/org/jwz/xscreensaver/gen";
705 my $xml_header = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";
708 my $daydream_java = '';
709 my $settings_java = '';
717 my $file = "../utils/version.h";
719 open (my $in, '<', $file) || error ("$file: $!");
720 while (<$in>) { $body .= $_; }
722 ($vers) = ($body =~ m@ (\d+\.\d+) @s);
723 error ("$file: no version number") unless $vers;
727 foreach my $saver (@savers) {
728 next if ($saver =~ m/(-helper)$/);
729 $saver = 'rdbomb' if ($saver eq 'rd-bomb');
731 my ($src_opts, $switchmap) = parse_src ($saver);
732 my ($saver_title, $gl_p, $xml_opts, $widgets) =
733 parse_xml ($saver, $switchmap, $src_opts);
735 my $saver_class = "${saver_title}";
736 $saver_class =~ s/\s+//gs;
737 $saver_class =~ s/^([a-z])/\U$1/gs; # upcase first letter
739 $saver_title =~ s/(.[a-z])([A-Z\d])/$1 $2/gs; # Spaces in InterCaps
740 $saver_title =~ s/^(GL|RD)[- ]?(.)/$1 \U$2/gs; # Space after "GL"
741 $saver_title =~ s/^Apple ?2$/Apple ][/gs; # "Apple ]["
742 $saver_title =~ s/(m)oe(bius)/$1ö$2/gsi; # ö
743 $saver_title =~ s/(moir)e/$1é/gsi; # é
744 $saver_title =~ s/^([a-z])/\U$1/s; # "M6502" for sorting
748 my $localize0 = sub($$) {
749 my ($key, $string) = @_;
750 $string =~ s@([\\\"\'])@\\$1@gs; # backslashify
751 $string =~ s@\n@\\n@gs; # quote newlines
752 $key =~ s@[^a-z\d_]+@_@gsi; # illegal characters
754 my $old = $string_dups{$key};
755 error ("dup string: $key: \"$old\" != \"$string\"")
756 if (defined($old) && $old ne $string);
757 $string_dups{$key} = $string;
759 my $fmt = ($string =~ m/%/ ? ' formatted="false"' : '');
760 $strings .= "<string name=\"${key}\"$fmt>$string</string>\n"
761 unless defined($old);
762 return "\@string/$key";
765 $localize0->('app_name', 'XScreenSaver');
767 $settings .= ("<Preference\n" .
768 " android:key=\"${saver}_reset\"\n" .
769 " android:title=\"" .
770 $localize0->('reset_to_defaults', 'Reset to defaults') .
774 my $daydream_desc = '';
775 foreach my $widget (@$widgets) {
776 my $type = $widget->{type};
777 my $rsrc = $widget->{resource};
778 my $label = $widget->{_label};
779 my $def = $widget->{default};
780 my $invert_p = (($widget->{convert} || '') eq 'invert');
782 my $key = "${saver}_$rsrc" if $rsrc;
784 #### The menus don't actually have titles on X11 or Cocoa...
785 $label = $widget->{resource} unless $label;
787 my $localize = sub($;$) {
788 my ($string, $suf) = @_;
789 $suf = 'title' unless $suf;
790 return $localize0->("${saver}_${rsrc}_${suf}", $string);
793 if ($type eq 'slider' || $type eq 'spinbutton') {
795 my $low = $widget->{low};
796 my $high = $widget->{high};
797 my $float_p = $low =~ m/[.]/;
798 my $low_label = $widget->{'_low-label'};
799 my $high_label = $widget->{'_high-label'};
801 $low_label = $low unless defined($low_label);
802 $high_label = $high unless defined($high_label);
804 ($low, $high) = ($high, $low)
805 if (($widget->{convert} || '') eq 'invert');
808 ("<$package.SliderPreference\n" .
809 " android:layout=\"\@layout/slider_preference\"\n" .
810 " android:key=\"${key}\"\n" .
811 " android:title=\"" . $localize->($label) . "\"\n" .
812 " android:defaultValue=\"$def\"\n" .
814 " high=\"$high\"\n" .
815 " lowLabel=\"" . $localize->($low_label, 'low_label') . "\"\n" .
816 " highLabel=\"" . $localize->($high_label, 'high_label') . "\"\n" .
817 " integral=\"" .($float_p ? 'false' : 'true'). "\" />\n");
819 } elsif ($type eq 'boolean') {
821 my $def = ($invert_p ? 'true' : 'false');
823 ("<CheckBoxPreference\n" .
824 " android:key=\"${key}\"\n" .
825 " android:title=\"" . $localize->($label) . "\"\n" .
826 " android:defaultValue=\"$def\" />\n");
828 } elsif ($type eq 'select') {
830 $label =~ s/^(.)/\U$1/s; # upcase first letter of menu title
831 $label =~ s/[-_]/ /gs;
832 $label =~ s/([a-z])([A-Z])/$1 $2/gs;
833 $def = '' unless defined ($def);
835 ("<ListPreference\n" .
836 " android:key=\"${key}\"\n" .
837 " android:title=\"" . $localize->($label, 'menu') . "\"\n" .
838 " android:entries=\"\@array/${key}_entries\"\n" .
839 " android:defaultValue=\"$def\"\n" .
840 " android:entryValues=\"\@array/${key}_values\" />\n");
843 foreach my $item (@{$widget->{menu}}) {
844 my $val = $item->{value};
845 if (! defined($val)) {
846 $val = $src_opts->{$widget->{resource}};
847 error ("$saver: no default resource in option menu " .
849 unless defined($val);
851 $val =~ s@([\\\"\'])@\\$1@gs; # backslashify
852 $a1 .= " <item>$val</item>\n";
856 foreach my $item (@{$widget->{menu}}) {
857 my $val = $item->{value};
858 $val = $src_opts->{$widget->{resource}} unless defined($val);
859 $a2 .= (" <item>" . $localize->($item->{_label}, $val) .
863 my $fmt1 = ($a1 =~ m/%/ ? ' formatted="false"' : '');
864 my $fmt2 = ($a2 =~ m/%/ ? ' formatted="false"' : '');
865 $arrays .= ("<string-array name=\"${key}_values\"$fmt1>\n" .
867 "</string-array>\n" .
868 "<string-array name=\"${key}_entries\"$fmt2>\n" .
870 "</string-array>\n");
872 } elsif ($type eq 'string') {
874 $def =~ s/&/&/gs;
876 ("<EditTextPreference\n" .
877 " android:key=\"${key}\"\n" .
878 " android:title=\"" . $localize->($label) . "\"\n" .
879 " android:defaultValue=\"$def\" />\n");
881 } elsif ($type eq 'file') {
883 } elsif ($type eq '_description') {
885 $type = 'description';
887 my $desc = $widget->{text};
888 (undef, $desc) = munge_blurb ($saver, $saver_title, $vers, $desc);
890 # Lose the Wikipedia URLs.
891 $desc =~ s@https?:.*?\b(wikipedia|mathworld)\b[^\s]+[ \t]*\n?@@gm;
892 $desc =~ s/(\n\n)\n+/$1/s;
893 $desc =~ s/\s*$/\n\n\n/s;
895 $daydream_desc = $desc;
897 my ($year) = ($daydream_desc =~ m/\b((19|20)\d\d)\b/s);
898 error ("$saver: no year") unless $year;
899 $daydream_desc =~ s/^.*?\n\n//gs;
900 $daydream_desc =~ s/\n.*$//gs;
901 $daydream_desc = "$year: $daydream_desc";
902 $daydream_desc =~ s/^(.{72}).+$/$1.../s;
906 " android:icon=\"\@drawable/thumbnail\"\n" .
907 " android:key=\"${saver}_${type}\"\n" .
908 # " android:selectable=\"false\"\n" .
909 " android:persistent=\"false\"\n" .
910 " android:layout=\"\@layout/preference_blurb\"\n" .
911 " android:summary=\"" . $localize->($desc) . "\">\n" .
912 " <intent android:action=\"android.intent.action.VIEW\"\n" .
913 " android:data=\"https://www.jwz.org/xscreensaver/\" />\n" .
917 error ("unhandled type: $type");
921 my $heading = "XScreenSaver: $saver_title";
923 $settings =~ s/^/ /gm;
924 $settings = ($xml_header .
925 "<PreferenceScreen xmlns:android=\"" .
926 "http://schemas.android.com/apk/res/android\"\n" .
927 " android:title=\"" .
928 $localize0->("${saver}_settings_title", $heading) . "\">\n" .
930 "</PreferenceScreen>\n");
932 my $saver_underscore = $saver;
933 $saver_underscore =~ s/-/_/g;
934 $write_files{"$xml_dir/${saver_underscore}_settings.xml"} = $settings;
936 $manifest .= ("<service android:label=\"" .
937 $localize0->("${saver_underscore}_saver_title",
940 " android:summary=\"" .
941 $localize0->("${saver_underscore}_saver_desc",
942 $daydream_desc) . "\"\n" .
943 " android:name=\".gen.Daydream\$$saver_class\"\n" .
944 " android:permission=\"android.permission" .
945 ".BIND_DREAM_SERVICE\"\n" .
946 " android:exported=\"true\"\n" .
947 " android:icon=\"\@drawable/${saver_underscore}\">\n" .
948 " <intent-filter>\n" .
949 " <action android:name=\"android.service.dreams" .
950 ".DreamService\" />\n" .
951 " <category android:name=\"android.intent.category" .
953 " </intent-filter>\n" .
954 " <meta-data android:name=\"android.service.dream\"\n" .
955 " android:resource=\"\@xml/${saver}_dream\" />\n" .
957 "<activity android:name=\"" .
958 "$package.gen.Settings\$$saver_class\" />\n"
961 my $dream = ("<dream xmlns:android=\"" .
962 "http://schemas.android.com/apk/res/android\"\n" .
963 " android:settingsActivity=\"" .
964 "$package.gen.Settings\$$saver_class\" />\n");
965 $write_files{"$xml_dir/${saver_underscore}_dream.xml"} = $dream;
968 (" public static class $saver_class extends XScreenSaverDaydream {\n" .
969 " public $saver_class() {\n" .
970 " super(jwxyz.API_" . ($gl_p ? 'GL' : 'XLIB') . ");\n" .
976 (" public static class $saver_class extends XScreenSaverSettings\n" .
977 " implements SharedPreferences.OnSharedPreferenceChangeListener {\n" .
983 $arrays = ($xml_header .
984 "<resources xmlns:xliff=\"" .
985 "urn:oasis:names:tc:xliff:document:1.2\">\n" .
989 $strings =~ s/^/ /gm;
990 $strings = ($xml_header .
995 $manifest .= "<activity android:name=\"$package.XScreenSaverSettings\" />\n";
997 $manifest .= ("<activity android:name=\"" .
998 "org.jwz.xscreensaver.XScreenSaverActivity\"\n" .
999 " android:theme=\"\@android:style/Theme.Holo\"\n" .
1000 " android:label=\"\@string/app_name\">\n" .
1001 " <intent-filter>\n" .
1002 " <action android:name=\"android.intent.action" .
1004 " <category android:name=\"android.intent.category" .
1005 ".LAUNCHER\" />\n" .
1006 " </intent-filter>\n" .
1007 " <intent-filter>\n" .
1008 " <action android:name=\"android.intent.action" .
1010 " <category android:name=\"android.intent.category" .
1012 " <category android:name=\"android.intent.category" .
1013 ".BROWSABLE\" />\n" .
1014 " </intent-filter>\n" .
1017 # Android wants this to be an int
1019 $versb =~ s/^(\d+)\.(\d+).*$/{ $1 * 10000 + $2 * 100 }/sex;
1020 $versb++ if ($versb == 53500); # Herp derp
1022 $manifest =~ s/^/ /gm;
1023 $manifest = ($xml_header .
1024 "<manifest xmlns:android=\"" .
1025 "http://schemas.android.com/apk/res/android\"\n" .
1026 " package=\"$package\"\n" .
1027 " android:versionCode=\"$versb\"\n" .
1028 " android:versionName=\"$vers\">\n" .
1030 " <uses-sdk android:minSdkVersion=\"14\"" .
1031 " android:targetSdkVersion=\"19\" />\n" .
1033 " <uses-feature android:glEsVersion=\"0x00010001\"\n" .
1034 " android:required=\"true\" />\n" .
1036 " <uses-permission android:name=\"" .
1037 "android.permission.INTERNET\" />\n" .
1038 " <uses-permission android:name=\"" .
1039 "android.permission.READ_EXTERNAL_STORAGE\" />\n" .
1041 " <application android:icon=\"\@drawable/thumbnail\"\n" .
1042 " android:label=\"\@string/app_name\"\n" .
1043 " android:name=\".XScreenSaverApp\">\n" .
1045 " </application>\n" .
1048 $daydream_java = ("package org.jwz.xscreensaver.gen;\n" .
1050 "import org.jwz.xscreensaver.XScreenSaverDaydream;\n" .
1051 "import org.jwz.xscreensaver.jwxyz;\n" .
1053 "public class Daydream {\n" .
1057 $settings_java = ("package org.jwz.xscreensaver.gen;\n" .
1059 "import android.content.SharedPreferences;\n" .
1060 "import org.jwz.xscreensaver.XScreenSaverSettings;\n" .
1062 "public class Settings {\n" .
1066 $write_files{"$project_dir/AndroidManifest.xml"} = $manifest;
1067 $write_files{"$values_dir/settings.xml"} = $arrays;
1068 $write_files{"$values_dir/strings.xml"} = $strings;
1069 $write_files{"$java_dir/Daydream.java"} = $daydream_java;
1070 $write_files{"$java_dir/Settings.java"} = $settings_java;
1073 foreach my $saver (sort @savers) {
1074 push @s2, $saver unless ($saver =~ m/(-helper)$/);
1078 foreach (@s2) { s/^(.*)$/${1}_xscreensaver_function_table/s; }
1079 foreach (@s3) { s/^(.*)$/{"$1", &${1}_xscreensaver_function_table}/s; }
1081 my $fntable_h = ("extern struct xscreensaver_function_table\n" .
1082 " " . join(",\n ", @s2) . ";\n" .
1084 "static const struct function_table_entry" .
1085 " function_table[] = {\n" .
1086 " " . join(",\n ", @s3) . "\n" .
1088 $write_files{"$gen_dir/function-table.h"} = $fntable_h;
1091 $write_files{"$values_dir/attrs.xml"} =
1092 # This file doesn't actually have any substitutions in it, so it could
1093 # just be static, somewhere...
1094 # SliderPreference.java refers to this via "R.styleable.SliderPreference".
1095 ("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" .
1097 " <declare-styleable name=\"SliderPreference\">\n" .
1098 " <attr name=\"android:summary\" />\n" .
1099 " </declare-styleable>\n" .
1103 foreach my $file (sort keys %write_files) {
1104 my ($dir) = ($file =~ m@^(.*)/[^/]*$@s);
1105 system ("mkdir", "-p", $dir) if (! -d $dir && !$debug_p);
1106 my $body = $write_files{$file};
1107 $body = "// Generated by $progname\n$body"
1108 if ($file =~ m/\.(java|[chm])$/s);
1109 write_file_if_changed ($file, $body);
1112 # Unlink any .xml files from a previous run that shouldn't be there:
1113 # if a hack is removed from $ANDROID_HACKS in android/Makefile but
1114 # the old XML files remain behind, the build blows up.
1116 foreach my $dd ($xml_dir, $gen_dir, $java_dir) {
1117 opendir (my $dirp, $dd) || error ("$dd: $!");
1118 my @files = readdir ($dirp);
1120 foreach my $f (sort @files) {
1121 next if ($f eq '.' || $f eq '..');
1123 next if (defined ($write_files{$f}));
1124 if ($f =~ m/_(settings|dream)\.xml$/s ||
1125 $f =~ m/(Settings|Daydream)\.java$/s) {
1126 print STDERR "$progname: rm $f\n";
1127 unlink ($f) unless ($debug_p);
1129 print STDERR "$progname: warning: unrecognised file: $f\n";
1138 print STDERR "$progname: $err\n";
1143 print STDERR "usage: $progname [--verbose] [--debug]" .
1144 " [--build-android] files ...\n";
1151 while ($#ARGV >= 0) {
1153 if (m/^--?verbose$/) { $verbose++; }
1154 elsif (m/^-v+$/) { $verbose += length($_)-1; }
1155 elsif (m/^--?debug$/s) { $debug_p++; }
1156 elsif (m/^--?build-android$/s) { $android_p++; }
1157 elsif (m/^-./) { usage; }
1158 else { push @files, $_; }
1162 usage unless ($#files >= 0);
1164 foreach my $file (@files) {
1165 $failures += check_config ($file);
1168 build_android (@files) if ($android_p);