runitx Claude Opus 5.5 commited on
Commit
c7de05d
·
unverified ·
1 Parent(s): b36e93e

Extract environment helpers into Shutter::App::Environment

Browse files

Move fct_control_wm_settings, fct_init_unsaved_files and
fct_init_debug_output into the new module as control_wm_settings,
init_unsaved_files and init_debug_output. bin/shutter keeps one-line
wrappers that pass $sc, a callback to fct_get_key_by_filename, and
SHUTTER_VERSION/SHUTTER_REV explicitly. Adds t/Shutter/App/18_environment.t.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

bin/shutter CHANGED
@@ -9566,121 +9566,13 @@ modes are currently experimental, your desktop environment might fall back to an
9566
  return TRUE;
9567
  }
9568
 
9569
- sub fct_control_wm_settings {
9570
- my $mode = shift;
9571
- my $restore_value = shift;
9572
 
9573
- #compiz via dbus
9574
- my $bus = undef;
9575
- my $compiz = undef;
9576
- my $fpl = undef;
9577
 
9578
- #disable focus_prevention
9579
- my $curr_value = -1;
9580
 
9581
- #disable focus prevention when using compiz
9582
- eval {
9583
- $bus = Net::DBus->find;
9584
-
9585
- #Get a handle to the compiz service
9586
- $compiz = $bus->get_service("org.freedesktop.compiz");
9587
-
9588
- #Get the relevant object
9589
- $fpl = $compiz->get_object("/org/freedesktop/compiz/core/screen0/focus_prevention_level", "org.freedesktop.compiz");
9590
- };
9591
- if ($@) {
9592
- warn "INFO: DBus connection to org.freedesktop.compiz failed --> skipping compiz related tasks\n\n";
9593
- warn $@ . "\n\n";
9594
- return $curr_value;
9595
- }
9596
-
9597
- if (defined $fpl && $fpl) {
9598
- eval {
9599
- if ($mode eq 'start') {
9600
-
9601
- #save and return current value
9602
- if (defined $fpl && $fpl) {
9603
- $curr_value = $fpl->get;
9604
- }
9605
- if (defined $fpl && $fpl && $fpl->get != 0) {
9606
- $fpl->set(0);
9607
- }
9608
-
9609
- #re-enable focus prevention -> restore value
9610
- } elsif ($mode eq 'stop') {
9611
- if (defined $fpl && $fpl && defined $restore_value) {
9612
- $fpl->set($restore_value);
9613
- } elsif (defined $fpl && $fpl) {
9614
- $fpl->set(1);
9615
- }
9616
- }
9617
- };
9618
- if ($@) {
9619
- warn "ERROR: Unable to set/get focus_level_prevention --> skipping compiz related tasks\n\n";
9620
- warn $@ . "\n\n";
9621
- }
9622
- }
9623
-
9624
- return $curr_value;
9625
- }
9626
-
9627
- sub fct_init_unsaved_files {
9628
-
9629
- #delete all files in this folder
9630
- #except the ones that are in the current session
9631
- my @unsaved_files = bsd_glob(Shutter::App::Directories::get_cache_dir() . "/*");
9632
- foreach my $unsaved_file (@unsaved_files) {
9633
- utf8::decode $unsaved_file;
9634
- print $unsaved_file, " checking \n" if $sc->get_debug;
9635
- unless (fct_get_key_by_filename($unsaved_file)) {
9636
- print $unsaved_file, " deleted \n" if $sc->get_debug;
9637
- unlink $unsaved_file;
9638
- }
9639
- }
9640
- }
9641
-
9642
- sub fct_init_debug_output {
9643
-
9644
- print "\nINFO: gathering system information...";
9645
- print "\n";
9646
- print "\n";
9647
- print "Shutter ";
9648
- print SHUTTER_VERSION;
9649
- print ' ';
9650
- print SHUTTER_REV;
9651
- print "\n";
9652
-
9653
- #kernel info
9654
- if (File::Which::which('uname')) {
9655
- print `uname -a`, "\n";
9656
- }
9657
-
9658
- eval {
9659
- open my $fh, '<', '/etc/os-release' or die;
9660
- my %map = map {
9661
- chomp;
9662
- my ($key, $value) = split /=/, $_, 2;
9663
- $value =~ s/^(['"])(.*)\1$/$2/;
9664
- ($key, $value)
9665
- } <$fh>;
9666
- local $, = ' ';
9667
- say grep { $_ } map { $map{$_} } qw/NAME VERSION_ID BUILD_ID/;
9668
- };
9669
- say "Cannot open /etc/os-release" if $@;
9670
-
9671
- printf "Glib %s \n", $Glib::VERSION;
9672
- printf "Gtk3 %s \n", $Gtk3::VERSION;
9673
- print "\n";
9674
-
9675
- # The version info stuff appeared in 1.040.
9676
- print "Glib built for " . join(".", Glib->GET_VERSION_INFO) . ", running with " . join(".", Glib::major_version(), Glib::minor_version(), Glib::micro_version()) . "\n"
9677
- if $Glib::VERSION >= 1.040;
9678
- print "Gtk3 built for " . join(".", Gtk3->GET_VERSION_INFO) . ", running with " . join(".", Gtk3::major_version(), Gtk3::minor_version(), Gtk3::micro_version()) . "\n"
9679
- if $Gtk3::VERSION >= 1.040;
9680
- print "\n";
9681
-
9682
- return TRUE;
9683
- }
9684
 
9685
  #--------------------------------------
9686
 
 
9566
  return TRUE;
9567
  }
9568
 
9569
+ require Shutter::App::Environment;
 
 
9570
 
9571
+ sub fct_control_wm_settings { return Shutter::App::Environment::control_wm_settings(@_) }
 
 
 
9572
 
9573
+ sub fct_init_unsaved_files { return Shutter::App::Environment::init_unsaved_files($sc, sub { fct_get_key_by_filename(@_) }, @_) }
 
9574
 
9575
+ sub fct_init_debug_output { return Shutter::App::Environment::init_debug_output(SHUTTER_VERSION, SHUTTER_REV, @_) }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9576
 
9577
  #--------------------------------------
9578
 
share/shutter/resources/modules/Shutter/App/Environment.pm ADDED
@@ -0,0 +1,171 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ###################################################
2
+ #
3
+ # Copyright (C) 2008-2013 Mario Kemper <mario.kemper@gmail.com>
4
+ #
5
+ # This file is part of Shutter.
6
+ #
7
+ # Shutter 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 3 of the License, or
10
+ # (at your option) any later version.
11
+ #
12
+ # Shutter 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.
16
+ #
17
+ # You should have received a copy of the GNU General Public License
18
+ # along with Shutter; if not, write to the Free Software
19
+ # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20
+ #
21
+
22
+ package Shutter::App::Environment;
23
+
24
+ #modules
25
+ #--------------------------------------
26
+ use 5.010;
27
+ use utf8;
28
+ use strict;
29
+ use warnings;
30
+
31
+ use Gtk3;
32
+ use Net::DBus;
33
+ use File::Glob qw/ bsd_glob /;
34
+ use File::Which;
35
+
36
+ #Glib
37
+ use Glib qw/TRUE FALSE/;
38
+
39
+ require Shutter::App::Directories;
40
+
41
+ use Exporter qw(import);
42
+ our @EXPORT_OK = qw(control_wm_settings init_unsaved_files init_debug_output);
43
+
44
+ #--------------------------------------
45
+
46
+ # Disable (mode 'start') or restore (mode 'stop') compiz focus prevention via DBus.
47
+ # Returns the previous focus_prevention_level, or -1 if compiz is not reachable.
48
+ sub control_wm_settings {
49
+ my $mode = shift;
50
+ my $restore_value = shift;
51
+
52
+ #compiz via dbus
53
+ my $bus = undef;
54
+ my $compiz = undef;
55
+ my $fpl = undef;
56
+
57
+ #disable focus_prevention
58
+ my $curr_value = -1;
59
+
60
+ #disable focus prevention when using compiz
61
+ eval {
62
+ $bus = Net::DBus->find;
63
+
64
+ #Get a handle to the compiz service
65
+ $compiz = $bus->get_service("org.freedesktop.compiz");
66
+
67
+ #Get the relevant object
68
+ $fpl = $compiz->get_object("/org/freedesktop/compiz/core/screen0/focus_prevention_level", "org.freedesktop.compiz");
69
+ };
70
+ if ($@) {
71
+ warn "INFO: DBus connection to org.freedesktop.compiz failed --> skipping compiz related tasks\n\n";
72
+ warn $@ . "\n\n";
73
+ return $curr_value;
74
+ }
75
+
76
+ if (defined $fpl && $fpl) {
77
+ eval {
78
+ if ($mode eq 'start') {
79
+
80
+ #save and return current value
81
+ if (defined $fpl && $fpl) {
82
+ $curr_value = $fpl->get;
83
+ }
84
+ if (defined $fpl && $fpl && $fpl->get != 0) {
85
+ $fpl->set(0);
86
+ }
87
+
88
+ #re-enable focus prevention -> restore value
89
+ } elsif ($mode eq 'stop') {
90
+ if (defined $fpl && $fpl && defined $restore_value) {
91
+ $fpl->set($restore_value);
92
+ } elsif (defined $fpl && $fpl) {
93
+ $fpl->set(1);
94
+ }
95
+ }
96
+ };
97
+ if ($@) {
98
+ warn "ERROR: Unable to set/get focus_level_prevention --> skipping compiz related tasks\n\n";
99
+ warn $@ . "\n\n";
100
+ }
101
+ }
102
+
103
+ return $curr_value;
104
+ }
105
+
106
+ # Delete files in the 'unsaved' cache dir that are not part of the current session.
107
+ # $sc: Shutter::App::Common; $get_key_by_filename: coderef returning the session key for a file.
108
+ sub init_unsaved_files {
109
+ my ($sc, $get_key_by_filename) = @_;
110
+
111
+ #delete all files in this folder
112
+ #except the ones that are in the current session
113
+ my @unsaved_files = bsd_glob(Shutter::App::Directories::get_cache_dir() . "/*");
114
+ foreach my $unsaved_file (@unsaved_files) {
115
+ utf8::decode $unsaved_file;
116
+ print $unsaved_file, " checking \n" if $sc->get_debug;
117
+ unless ($get_key_by_filename->($unsaved_file)) {
118
+ print $unsaved_file, " deleted \n" if $sc->get_debug;
119
+ unlink $unsaved_file;
120
+ }
121
+ }
122
+ return;
123
+ }
124
+
125
+ # Print system/library version information (used with --debug).
126
+ # $version, $rev: SHUTTER_VERSION and SHUTTER_REV from bin/shutter.
127
+ sub init_debug_output {
128
+ my ($version, $rev) = @_;
129
+
130
+ print "\nINFO: gathering system information...";
131
+ print "\n";
132
+ print "\n";
133
+ print "Shutter ";
134
+ print $version;
135
+ print ' ';
136
+ print $rev;
137
+ print "\n";
138
+
139
+ #kernel info
140
+ if (File::Which::which('uname')) {
141
+ print `uname -a`, "\n";
142
+ }
143
+
144
+ eval {
145
+ open my $fh, '<', '/etc/os-release' or die; ## no critic (InputOutput::RequireBriefOpen)
146
+ my %map = map {
147
+ chomp;
148
+ my ($key, $value) = split /=/, $_, 2;
149
+ $value =~ s/^(['"])(.*)\1$/$2/;
150
+ ($key, $value)
151
+ } <$fh>;
152
+ local $, = ' ';
153
+ say grep { $_ } map { $map{$_} } qw/NAME VERSION_ID BUILD_ID/;
154
+ };
155
+ say "Cannot open /etc/os-release" if $@;
156
+
157
+ printf "Glib %s \n", $Glib::VERSION;
158
+ printf "Gtk3 %s \n", $Gtk3::VERSION;
159
+ print "\n";
160
+
161
+ # The version info stuff appeared in 1.040.
162
+ print "Glib built for " . join(".", Glib->GET_VERSION_INFO) . ", running with " . join(".", Glib::major_version(), Glib::minor_version(), Glib::micro_version()) . "\n"
163
+ if $Glib::VERSION >= 1.040;
164
+ print "Gtk3 built for " . join(".", Gtk3->GET_VERSION_INFO) . ", running with " . join(".", Gtk3::major_version(), Gtk3::minor_version(), Gtk3::micro_version()) . "\n"
165
+ if $Gtk3::VERSION >= 1.040;
166
+ print "\n";
167
+
168
+ return TRUE;
169
+ }
170
+
171
+ 1;
t/Shutter/App/18_environment.t ADDED
@@ -0,0 +1,107 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/perl
2
+
3
+ use 5.010;
4
+ use strict;
5
+ use warnings;
6
+
7
+ use Test::More;
8
+ use Test::MockModule;
9
+ use Capture::Tiny qw/ capture_stdout /;
10
+ use File::Temp qw/ tempdir /;
11
+ use Glib qw/ TRUE FALSE /;
12
+
13
+ BEGIN { use_ok("Shutter::App::Environment") }
14
+ can_ok("Shutter::App::Environment", qw/ control_wm_settings init_unsaved_files init_debug_output /);
15
+
16
+ # minimal stand-ins (no package statements, so the file stays a plain script)
17
+ sub Local::FakeCommon::new { my ($class, %args) = @_; return bless {%args}, $class }
18
+ sub Local::FakeCommon::get_debug { return $_[0]->{debug} }
19
+
20
+ my $fpl_level = 2;
21
+ my @fpl_sets;
22
+ sub Local::FakeFpl::get { return $fpl_level }
23
+ sub Local::FakeFpl::set { $fpl_level = $_[1]; push @fpl_sets, $_[1]; return }
24
+ sub Local::FakeService::get_object { return bless {}, 'Local::FakeFpl' }
25
+ sub Local::FakeBus::get_service { return bless {}, 'Local::FakeService' }
26
+
27
+ subtest "init_unsaved_files keeps session files and deletes the rest" => sub {
28
+ my $cache_dir = tempdir(CLEANUP => 1);
29
+ my $mock = Test::MockModule->new("Shutter::App::Directories");
30
+ $mock->mock("get_cache_dir", sub { return $cache_dir });
31
+
32
+ my @names = qw/ keep.png drop.png also_drop.jpg /;
33
+ for my $name (@names) {
34
+ open my $fh, '>', "$cache_dir/$name" or die "cannot create $name: $!";
35
+ close $fh;
36
+ }
37
+
38
+ my @asked;
39
+ my $lookup = sub {
40
+ my $file = shift;
41
+ push @asked, $file;
42
+ return $file =~ m{/keep\.png$}x ? 'session-key-1' : undef;
43
+ };
44
+
45
+ Shutter::App::Environment::init_unsaved_files(Local::FakeCommon->new(debug => FALSE), $lookup);
46
+
47
+ ok(-e "$cache_dir/keep.png", "file in session is kept");
48
+ ok(!-e "$cache_dir/drop.png", "file not in session is deleted");
49
+ ok(!-e "$cache_dir/also_drop.jpg", "other file not in session is deleted");
50
+ is_deeply([sort @asked], [sort map { "$cache_dir/$_" } @names], "lookup called once per cached file with full path");
51
+ };
52
+
53
+ subtest "init_unsaved_files prints progress in debug mode" => sub {
54
+ my $cache_dir = tempdir(CLEANUP => 1);
55
+ my $mock = Test::MockModule->new("Shutter::App::Directories");
56
+ $mock->mock("get_cache_dir", sub { return $cache_dir });
57
+
58
+ open my $fh, '>', "$cache_dir/x.png" or die $!;
59
+ close $fh;
60
+
61
+ my $out = capture_stdout {
62
+ Shutter::App::Environment::init_unsaved_files(Local::FakeCommon->new(debug => TRUE), sub { return });
63
+ };
64
+ like($out, qr{x\.png\ checking}x, "debug: checking message");
65
+ like($out, qr{x\.png\ deleted}x, "debug: deleted message");
66
+ ok(!-e "$cache_dir/x.png", "file deleted");
67
+ };
68
+
69
+ subtest "init_debug_output prints version information" => sub {
70
+ my $ret;
71
+ my $out = capture_stdout {
72
+ $ret = Shutter::App::Environment::init_debug_output('9.9.9', 'Rev.TEST');
73
+ };
74
+ ok($ret, "returns TRUE");
75
+ like($out, qr/Shutter\ 9\.9\.9\ Rev\.TEST/x, "prints given version and revision");
76
+ like($out, qr/^Glib\ \S+/xm, "prints Glib version");
77
+ like($out, qr/^Gtk3\ \S+/xm, "prints Gtk3 version");
78
+ };
79
+
80
+ subtest "control_wm_settings returns -1 when compiz is unavailable" => sub {
81
+ my $mock = Test::MockModule->new("Net::DBus", no_auto => 1);
82
+ $mock->mock("find", sub { die "no bus\n" });
83
+
84
+ my @warnings;
85
+ local $SIG{__WARN__} = sub { push @warnings, @_ };
86
+ is(Shutter::App::Environment::control_wm_settings('start'), -1, "start returns -1");
87
+ is(Shutter::App::Environment::control_wm_settings('stop', 1), -1, "stop returns -1");
88
+ like($warnings[0], qr/DBus\ connection\ to\ org\.freedesktop\.compiz\ failed/x, "warns about skipping compiz");
89
+ };
90
+
91
+ subtest "control_wm_settings saves, disables and restores focus prevention" => sub {
92
+ my $mock = Test::MockModule->new("Net::DBus", no_auto => 1);
93
+ $mock->mock("find", sub { return bless {}, 'Local::FakeBus' });
94
+
95
+ my $saved = Shutter::App::Environment::control_wm_settings('start');
96
+ is($saved, 2, "start returns previous level");
97
+ is($fpl_level, 0, "start disables focus prevention");
98
+
99
+ Shutter::App::Environment::control_wm_settings('stop', $saved);
100
+ is($fpl_level, 2, "stop restores saved level");
101
+
102
+ Shutter::App::Environment::control_wm_settings('stop');
103
+ is($fpl_level, 1, "stop without value sets level 1");
104
+ is_deeply(\@fpl_sets, [0, 2, 1], "set called as expected");
105
+ };
106
+
107
+ done_testing();