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

# i3status-wrapper -- wrapper for i3status(1), plus other monitoring
#
# Copyright (C) 2019, 2021-2023 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 // warn "couldn't fork monitoring loop") {
    my $caffeinated_id;

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

    sub register_caffeinated {
	$caffeinated_id = $_[0]->{id};
	$info{caffeinated_name} = $_[0]->{name};
	kill USR1 => $i3status;
    }

    sub clear_caffeinated {
	undef $caffeinated_id;
	undef $info{caffeinated_name};
	kill USR1 => $i3status;
    }

    # Determine the initial state -- the WM might just have been reloaded.

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

    my @trees = decode_json `$wmipc -t get_tree`;
    while (@trees) {
	for ((shift @trees)->{nodes}->@*) {
	    if (grep $_ eq "caffeinated", $_->{marks}->@*) {
		register_caffeinated($_);
		last;
	    } else {
		unshift @trees, $_;
	    }
	}
    }

    # Now loop forever reading events, assuming no exceptions.

    eval {
	while (my $event = decode_json <$events>) {
	    if ($event->{change} eq "mark") {
		if (grep $_ eq "caffeinated", $event->{container}{marks}->@*)
		{
		    register_caffeinated($event->{container});
		} elsif ($caffeinated_id
		    and $caffeinated_id == $event->{container}{id}) {
		    clear_caffeinated();
		}
	    } 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.
	    }
	}
    };

    # Give up if there's a decoding error.  We can't ignore the problem
    # because we don't want our ideas regarding how many workspaces there are,
    # and whether anything is caffeinated, to get out of sync.
    #
    # The user can use the WM's "reload" command to restart this loop.
    $@ and wsbuttons("yes"), clear_caffeinated();
}

$pipe->reader;
open STDIN, "<&=", $pipe->fileno or die "couldn't reopen 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) = (<> =~ /^,?(.*)/)) {
    # If there is a decoding error, just skip this line, to minimise status
    # bar freezes.  This should be fine here because this filtering loop is in
    # itself stateless.  It's only if the decoding error involves newlines in
    # the wrong places, or similar, that this skip could cause us to produce
    # invalid output.
    my $blocks = eval { decode_json $statusline } // next;

    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";
}

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