summaryrefslogtreecommitdiff
path: root/perl5/Local/Desktop.pm
blob: f769d827160a127b3bbdc48129afc0d63467942a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
package Local::Desktop;

# graphical desktop management functions
#
# Copyright (C) 2020-2022 Sean Whitton
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or (at
# your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

use 5.028;
use strict;
use warnings;
use lib "$ENV{HOME}/src/dotfiles/perl5";

use Carp;
use JSON;
use File::Find;
use File::LibMagic;
use File::Spec::Functions "rel2abs";
use Exporter "import";
use File::Copy;
use List::Util "first", "any";
use Local::Desktop::WMIPC;

our @EXPORT = qw(
  @all_workspaces
  fresh_workspace
  compact_workspaces
  select_wallpaper_files
  ensure_resize_for_current_outputs
  resize_for_current_outputs
  pick_random_wallpapers );

my $output_re = qr/ ([0-9]+)x([0-9]+)\+([0-9]+)\+([0-9]+) /;

our @all_workspaces = (
    "1",     "2",      "3",      "4",     "5",     "6",
    "7",     "8",      "9",      "10",    "11:F1", "12:F2",
    "13:F3", "14:F4",  "15:F5",  "16:F6", "17:F7", "18:F8",
    "19:F9", "20:F10", "21:F11", "22:F12"
);

=head fresh_workspace($send)

Switch to the next free workspace, if any.  Return the name of that workspace,
or undef if no workspace was available.  If $send, send the current window to
the fresh workspace instead of switching focus there.

=cut

sub fresh_workspace {
    my $next_free_workspace = compact_workspaces(leave_gap => 1);

    if ($next_free_workspace) {
	my @cmds;
	my %opts = @_;

	push @cmds, "move container to workspace $next_free_workspace"
	  if $opts{send};

	# When !$opts{go} we should execute neither of these commands, but we
	# must work around <https://github.com/swaywm/sway/issues/6081>.
	#
	# In the case that !$opts{go}, can use 'C-i S-;' to move any other
	# wanted containers over, before finally going there with 'C-i ;'.
	push @cmds, "workspace $next_free_workspace";
	push @cmds, "workspace back_and_forth" unless $opts{go};

	Local::Desktop::WMIPC->new->cmd(@cmds);
    }
    $next_free_workspace
}

=head compact_workspaces(%opts)

Rename workspaces so as to remove gaps in the sequence of workspaces.

If C<$opts{leave_gap}>, ensure there is a gap of one workspace after the
currently focused workspace and return the name of the gap workspace, or just
return undef if there is no space for a gap.

=cut

sub compact_workspaces {
    my %opts       = @_;
    my $wmipc      = Local::Desktop::WMIPC->new;
    my @workspaces;
    foreach my $ws ($wmipc->get_workspaces->@*) {
	any { $_ eq $ws->{name} } @all_workspaces and push @workspaces, $ws
    }
    @workspaces < @all_workspaces or return;
    my ($current_workspace, $gap_workspace);
    if ($opts{leave_gap}) {
        $_->{focused} and $current_workspace = $_->{name}, last
          for @workspaces
    }

    my ($i, @cmds);
    while (my $next = shift @workspaces) {
        my $workspace = $all_workspaces[$i++];
        $opts{leave_gap}
          and $next->{name} eq $current_workspace
          and $gap_workspace = $all_workspaces[$i++];
        next if $next->{name} eq $workspace;
        my $pair = [$next->{name}, $workspace];
        _wsnum($next->{name}) > _wsnum($workspace)
          ? push @cmds, $pair
          : unshift @cmds, $pair
    }

    $wmipc->cmd(map "rename workspace $_->[0] to $_->[1]", @cmds);

    $opts{leave_gap} and $gap_workspace
}

=head select_wallpaper_files(@files)

Select the first entry of @files as the wallpaper for the first output, the
second entry of @files as the wallpaper for the second output, etc.

This function works by creating copies of those wallpapers in ~/local.

=cut

sub select_wallpaper_files {
    my $i;
    unlink <"$ENV{HOME}/local/wallpaper??.*">;
    for (@_) {
        -r or croak "$_ could not be read!";
        copy $_,
          sprintf "$ENV{HOME}/local/wallpaper%02d." . (/\.([^.]+)\z/)[0], $i++;
    }
}

=head resize_for_current_outputs()

Based on the output of xrandr(1), create ~/local/wallpaper.png from
~/local/wallpaperNN.*, such that executing `feh --bg-scale --no-xinerama
~/local/wallpaper.png` will put wallpaper00.* on the first output,
wallpaper01.* on the second, etc.  There should not be more than one file
matching ~/local/wallpaperNN.* for each NN.

Returns the value of $? right after executing convert(1).

=cut

sub resize_for_current_outputs {
    # note that swaybg and swaylock have per-output wallpapers built in, so
    # this function is only for X11

    chomp(my @xrandr = `xrandr`);
    my ($canvas_w, $canvas_h) = _get_screen_size(@xrandr);
    # sort the displays from left to right and then from top to bottom
    my @displays = sort {
        my (undef, undef, $a_x, $a_y) = $a =~ $output_re;
        my (undef, undef, $b_x, $b_y) = $b =~ $output_re;
        $a_y <=> $b_y or $a_x <=> $b_x
    } grep / connected /, @xrandr;

    my @wallpapers = sort {
        ($a =~ /wallpaper([0-9]+)\./)[0] <=> ($b =~ /wallpaper([0-9]+)\./)[0]
    } <"$ENV{HOME}/local/wallpaper??.*">;

    my @args = ("-page", "${canvas_w}x${canvas_h}", qw(-background none));
    for (@displays) {
        /$output_re/;
        push @args, '(', (shift @wallpapers || "canvas:#FFFFF6"),
          "-resize", "${1}x${2}^",
          qw(-gravity center -extent), "${1}x${2}",
          "-repage", "${1}x${2}+${3}+${4}", ')';
    }
    push @args, qw(-layers merge), "$ENV{HOME}/local/wallpaper.png";

    system "convert", @args;
    return $?;
}

=head ensure_resize_for_current_outputs()

Call C<resize_for_current_outputs()> if it looks to be needed.

=cut

sub ensure_resize_for_current_outputs {
    return unless <"$ENV{HOME}/local/wallpaper??.*">;
    resize_for_current_outputs(), return
      unless -e "$ENV{HOME}/local/wallpaper.png";
    my ($screen_w, $screen_h) = _get_screen_size(`xrandr`);
    my $magic = File::LibMagic->new;
    my ($img_w, $img_h)
      = $magic->info_from_filename("$ENV{HOME}/local/wallpaper.png")
      ->{description} =~ / ([0-9]+) x ([0-9]+),/;
    resize_for_current_outputs()
      if $img_w
      and $img_h
      and ($screen_w != $img_w or $screen_h != $img_h);
}

=head pick_random_wallpapers($n, @dirs)

Pick C<$n> random wallpapers from files in any of the directories listed in
C<@dirs>.

=cut

sub pick_random_wallpapers {
    my $n = shift;
    my $magic = File::LibMagic->new(follow_symlinks => 1);
    my (@images, @picks);
    find sub {
        push @images, $File::Find::name
          if $magic->info_from_filename($_)->{description} =~ /^\w+ image/
    }, @_;
    push @picks, splice @images, int(rand @images), 1 for 1 .. $n;
    return @picks;
}

sub _get_screen_size {
    chomp(@_);
    my ($canvas) = grep /^Screen 0:/, @_;
    $canvas =~ /current ([0-9]+) x ([0-9]+)/;
}

sub _wsnum { (split /:/, $_[0])[0] }

1;