summaryrefslogtreecommitdiff
path: root/scripts/desktop/i3status-wrapper
blob: 3ac4a5c6b7a52c71f270d1fc5512456ff4a02fa9 (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
#!/usr/bin/perl

# i3status-wrapper -- wrapper for i3status(1), plus other monitoring
#
# Copyright (C) 2019, 2021-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.032;
use strict;
use warnings;
use lib "$ENV{HOME}/src/dotfiles/perl5";

use JSON;
use Local::Desktop;
use IO::Pipe;
use IPC::Shareable;
use Sys::Hostname;

$| = 1;

my $pipe = IO::Pipe->new;

my $i3status = fork // die "couldn't fork()!";
unless ($i3status) {
    $pipe->writer;
    open STDOUT, ">&=", $pipe->fileno
      or die "couldn't open child's STDOUT";
    exec "i3status";
}

tie my %info, "IPC::Shareable", undef, { destroy => 1 };

unless (fork // die "couldn't fork()!") {
    $pipe->reader;
    open STDIN, "<&=", $pipe->fileno
      or die "couldn't reopen child's STDIN!";

    # Following based on Michael Stapelberg's sample i3status-wrapper script.

    my $hostname = hostname;
    my $username = $ENV{LOGNAME} || $ENV{USER} || getpwuid($<);

    # Skip the first line which contains the version header.
    print scalar <>;

    # The second line contains the start of the infinite array.
    print scalar <>;

    # Read lines forever, ignore a comma at the beginning if it exists.
    while (my ($statusline) = (<> =~ /^,?(.*)/)) {
        my @blocks = @{ decode_json $statusline };

        unshift @blocks,
          {
            name      => "caffeinated",
            full_text => "Caffeinated: " . $info{caffeinated_name} }
          if $info{caffeinated_name};

        unshift @blocks,
          { name => "hostinfo", full_text => $username . "@" . $hostname };

        print encode_json(\@blocks) . ",\n";
    }
}

my $caffeinated_id;

open my $events, "-|",
  $wmipc, "-t", "subscribe", "-m", '[ "window", "workspace" ]';

my $workspaces = @{ decode_json `$wmipc -t get_workspaces` };
$workspaces > 1 or wsbuttons("no");

while (my $event = decode_json <$events>) {
    if ($event->{change} eq "mark") {
        if (grep $_ eq "caffeinated", $event->{container}{marks}->@*) {
            $caffeinated_id = $event->{container}{id};
            $info{caffeinated_name} = $event->{container}{name};
            kill USR1 => $i3status;
        } elsif ($caffeinated_id
            and $caffeinated_id == $event->{container}{id}) {
            undef $caffeinated_id, undef $info{caffeinated_name};
            kill USR1 => $i3status;
        }
    } elsif ($event->{change} eq "init") {
	++$workspaces == 2 and wsbuttons("yes");
    } elsif ($event->{change} eq "empty") {
	--$workspaces == 1 and wsbuttons("no");

	# For simplicity, only fresh-workspace script calls compact_workspaces
	# atm.  For greater consistency we could call it here, too, without
	# supplying a leave_gap argument.

	# compact_workspaces;
    }
}

sub wsbuttons {
    return unless $ENV{XDG_CURRENT_DESKTOP} eq "sway";
    wmipc "bar bar-0 workspace_buttons $_[0]";
}