summaryrefslogtreecommitdiff
path: root/contrib/i3-wsbar
blob: a485c1500740d0d921006408dbac99f68fde0514 (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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
#!/usr/bin/env perl
# vim:ts=4:sw=4:expandtab:ft=perl
# © 2010 Michael Stapelberg, see LICENSE for license information

use strict;
use warnings;
use Getopt::Long;
use Pod::Usage;
use IPC::Run qw(start pump);
use Try::Tiny;
use AnyEvent::I3;
use AnyEvent;
use v5.10;

my $stdin;
my $socket_path = undef;
my ($workspaces, $outputs) = ([], {});
my $last_line = "";
my $w = AnyEvent->timer(
    after => 2,
    cb => sub {
        say "Connection to i3 timed out. Verify socket path ($socket_path)";
        exit 1;
    }
);

my $command = "";
my $input_on = "";
my $output_on = "";
my $show_all = 0;

my $result = GetOptions(
    'command=s' => \$command,
    'socket=s' => \$socket_path,
    'input-on=s' => \$input_on,
    'output-on=s' => \$output_on,
    'show-all' => \$show_all,
    'help' => sub { pod2usage(1); exit 0 },
);

if ($command eq '') {
    say "i3-wsbar is only useful in combination with dzen2.";
    say "Please specify -c (command)";
    exit 1;
}

my $i3 = i3($socket_path);

my @input_on = split(/,/, $input_on);
my @output_on = split(/,/, $output_on);

# Disable buffering
$| = 1;

# Wait a short amount of time and try to connect to i3 again
sub reconnect {
    my $timer;
    if (!defined($w)) {
        $w = AnyEvent->timer(
            after => 2,
            cb => sub {
                say "Connection to i3 timed out. Verify socket path ($socket_path)";
                exit 1;
            }
        );
    }

    my $c = sub {
        $timer = AnyEvent->timer(
            after => 0.01,
            cb => sub { $i3->connect->cb(\&connected) }
        );
    };
    $c->();
}

# Connection attempt succeeded or failed
sub connected {
    my ($cv) = @_;

    if (!$cv->recv) {
        reconnect();
        return;
    }

    $w = undef;

    $i3->subscribe({
        workspace => \&ws_change,
        output => \&output_change,
        _error => sub { reconnect() }
    });
    ws_change();
    output_change();
}

# Called when a ws changes
sub ws_change {
    # Request the current workspaces and update the output afterwards
    $i3->get_workspaces->cb(
        sub {
            my ($cv) = @_;
            $workspaces = $cv->recv;
            update_output();
        });
}

# Called when the reply to the GET_OUTPUTS message arrives
# Compares old outputs with new outputs and starts/kills
# $command for each output (if specified)
sub got_outputs {
    my $reply = shift->recv;
    my %old = %{$outputs};
    my %new = map { ($_->{name}, $_) } grep { $_->{active} } @{$reply};

    # If no command was given, we do not need to compare outputs
    if ($command eq '') {
        update_output();
        return;
    }

    # Handle new outputs
    for my $name (keys %new) {
        next if @output_on and !($name ~~ @output_on);

        if (defined($old{$name})) {
            # Check if the mode changed (by reversing the hashes so
            # that we can check for equality using the smartmatch op)
            my %oldrect = reverse %{$old{$name}->{rect}};
            my %newrect = reverse %{$new{$name}->{rect}};
            next if (%oldrect ~~ %newrect);

            # On mode changes, we re-start the command
            $outputs->{$name}->{cmd}->finish;
            delete $outputs->{$name};
        }

        my $x = $new{$name}->{rect}->{x};
        my $w = $new{$name}->{rect}->{width};
        my $launch = $command;
        $launch =~ s/([^%])%x/$1$x/g;
        $launch =~ s/([^%])%w/$1$w/g;
        $launch =~ s/%%x/%x/g;
        $launch =~ s/%%w/%w/g;

        $new{$name}->{cmd_input} = '';
        my @cmd = ('/bin/sh', '-c', $launch);
        $new{$name}->{cmd} = start \@cmd, \$new{$name}->{cmd_input};
        $outputs->{$name} = $new{$name};
    }

    # Handle old outputs
    for my $name (keys %old) {
        next if defined($new{$name});

        $outputs->{$name}->{cmd}->finish;
        delete $outputs->{$name};
    }

    update_output();
}

sub output_change {
    $i3->get_outputs->cb(\&got_outputs)
}

sub update_output {
    my $dzen_bg = "#111111";
    my $out;
    my $previous_output;

    for my $name (keys %{$outputs}) {
        my $width = $outputs->{$name}->{rect}->{width};

        $previous_output = undef;
        $out = qq|^pa(;2)|;
        for my $ws (@{$workspaces}) {
            next if $ws->{output} ne $name and !$show_all;

            # Display a separator if we are on a different output now
            if (defined($previous_output) and
                ($ws->{output} ne $previous_output)) {
                $out .= qq|^fg(#900000)^ib(1)\|^ib(0)^p(+4)|;
            }
            $previous_output = $ws->{output};

            my ($bg, $fg) = qw(333333 888888);
            ($bg, $fg) = qw(4c7899 ffffff) if $ws->{visible};
            ($bg, $fg) = qw(900000 ffffff) if $ws->{urgent};

            my $cmd = q|i3-msg "workspace | . $ws->{name} . q|"|;
            my $name = $ws->{name};

            # Begin the clickable area
            $out .= qq|^ca(1,$cmd)|;

            # Draw the rest of the bar in the background color, but
            # don’t move the "cursor"
            $out .= qq|^p(_LOCK_X)^fg(#$bg)^r(${width}x17)^p(_UNLOCK_X)|;
            # Draw the name of the workspace without overwriting the
            # background color
            $out .= qq|^p(+3)^fg(#$fg)^ib(1)$name^ib(0)^p(+5)|;
            # Draw the rest of the bar in the normal background color
            # without moving the "cursor"
            $out .= qq|^p(_LOCK_X)^fg($dzen_bg)^r(${width}x17)^p(_UNLOCK_X)|;

            # End the clickable area
            $out .= qq|^ca()|;

            # Move to the next rect, reset Y coordinate
            $out .= qq|^p(2)^pa(;2)|;
        }

        $out .= qq|^p(_LOCK_X)^fg($dzen_bg)^r(${width}x17)^p(_UNLOCK_X)^fg()|;
        $out .= qq|^p(+5)|;
        $out .= $last_line if (!@input_on or $name ~~ @input_on);
        $out .= "\n";

        $outputs->{$name}->{cmd_input} = $out;
        try {
            pump $outputs->{$name}->{cmd} while length $outputs->{$name}->{cmd_input};
        } catch {
            warn "Could not write to dzen2";
            exit 1;
        }
    }
}

$i3->connect->cb(\&connected);

$stdin = AnyEvent->io(
    fh => \*STDIN,
    poll => 'r',
    cb => sub {
        my $line = <STDIN>;
        if (!defined($line)) {
            undef $stdin;
            return;
        }
        chomp($line);
        $last_line = $line;
        update_output();
    });

# let AnyEvent do the rest ("endless loop")
AnyEvent->condvar->recv

__END__

=head1 NAME

i3-wsbar - sample implementation of a standalone workspace bar

=head1 SYNOPSIS

i3-wsbar -c <dzen2-commandline> [options]

=head1 OPTIONS

=over 4

=item B<--command> <command>

This command (at the moment only dzen2 is supported) will be started for each
output. C<%x> will be replaced with the X coordinate of the output, C<%w> will
be replaced with the width of the output.

Example:
    --command "dzen2 -dock -x %x -w %w"

=item B<--input-on> <list-of-RandR-outputs>

Specifies on which outputs the contents of stdin should be appended to the
workspace bar.

Example:
    --input-on "LVDS1"

=item B<--output-on> <list-of-RandR-outputs>

Specifies for which outputs i3-wsbar should start C<command>.

=item B<--show-all>

If enabled, all workspaces are shown (not only those of the current output).
Handy to use with C<--output-on>.

=back

=cut