+# Reads the prefs we use from ~/.xscreensaver
+#
+sub get_x11_prefs() {
+ my $got_any_p = 0;
+
+ if (open (my $in, '<', $config_file)) {
+ print STDERR "$progname: reading $config_file\n" if ($verbose > 1);
+ local $/ = undef; # read entire file
+ my $body = <$in>;
+ close $in;
+ $got_any_p = get_x11_prefs_1 ($body);
+
+ } elsif ($verbose > 1) {
+ print STDERR "$progname: $config_file: $!\n";
+ }
+
+ if (! $got_any_p && defined ($ENV{DISPLAY})) {
+ # We weren't able to read settings from the .xscreensaver file.
+ # Fall back to any settings in the X resource database
+ # (/usr/X11R6/lib/X11/app-defaults/XScreenSaver)
+ #
+ print STDERR "$progname: reading X resources\n" if ($verbose > 1);
+ my $body = `appres XScreenSaver xscreensaver -1`;
+ $got_any_p = get_x11_prefs_1 ($body);
+ }
+}
+
+
+sub get_x11_prefs_1($) {
+ my ($body) = @_;
+
+ my $got_any_p = 0;
+ $body =~ s@\\\n@@gs;
+ $body =~ s@^[ \t]*#[^\n]*$@@gm;
+
+ if ($body =~ m/^[.*]*imageDirectory:[ \t]*([^\s]+)\s*$/im) {
+ $image_directory = $1;
+ $got_any_p = 1;
+ }
+ return $got_any_p;
+}
+
+
+sub get_cocoa_prefs($) {
+ my ($id) = @_;
+ print STDERR "$progname: reading Cocoa prefs: \"$id\"\n" if ($verbose > 1);
+ my $v = get_cocoa_pref_1 ($id, "imageDirectory");
+ $v = '~/Pictures' unless defined ($v); # Match default in XScreenSaverView
+ $image_directory = $v if defined ($v);
+}
+
+
+sub get_cocoa_pref_1($$) {
+ my ($id, $key) = @_;
+ # make sure there's nothing stupid/malicious in either string.
+ $id =~ s/[^-a-z\d. ]/_/gsi;
+ $key =~ s/[^-a-z\d. ]/_/gsi;
+ my $cmd = "defaults -currentHost read \"$id\" \"$key\"";
+
+ print STDERR "$progname: executing $cmd\n"
+ if ($verbose > 3);
+
+ my $val = `$cmd 2>/dev/null`;
+ $val =~ s/^\s+//s;
+ $val =~ s/\s+$//s;
+
+ print STDERR "$progname: Cocoa: $id $key = \"$val\"\n"
+ if ($verbose > 2);
+
+ $val = undef if ($val =~ m/^$/s);
+
+ return $val;
+}
+
+