]> git.sur5r.net Git - i3/i3/blob - i3-wsbar
Bugfix: i3-wsbar: properly catch errors when writing to child process
[i3/i3] / i3-wsbar
1 #!/usr/bin/env perl
2 # vim:ts=4:sw=4:expandtab:ft=perl
3 # © 2010 Michael Stapelberg, see LICENSE for license information
4
5 use strict;
6 use warnings;
7 use Getopt::Long;
8 use Pod::Usage;
9 use IPC::Run qw(start pump);
10 use Try::Tiny;
11 use AnyEvent::I3;
12 use AnyEvent;
13 use v5.10;
14
15 my $stdin;
16 my $socket_path = undef;
17 my ($workspaces, $outputs) = ([], {});
18 my $last_line = "";
19 my $w = AnyEvent->timer(
20     after => 2,
21     cb => sub {
22         say "Connection to i3 timed out. Verify socket path ($socket_path)";
23         exit 1;
24     }
25 );
26
27 my $command = "";
28 my $input_on = "";
29 my $output_on = "";
30 my $show_all = 0;
31
32 my $result = GetOptions(
33     'command=s' => \$command,
34     'socket=s' => \$socket_path,
35     'input-on=s' => \$input_on,
36     'output-on=s' => \$output_on,
37     'show-all' => \$show_all,
38     'help' => sub { pod2usage(1); exit 0 },
39 );
40
41 if ($command eq '') {
42     say "i3-wsbar is only useful in combination with dzen2.";
43     say "Please specify -c (command)";
44     exit 1;
45 }
46
47 my $i3 = i3($socket_path);
48
49 my @input_on = split(/,/, $input_on);
50 my @output_on = split(/,/, $output_on);
51
52 # Disable buffering
53 $| = 1;
54
55 # Wait a short amount of time and try to connect to i3 again
56 sub reconnect {
57     my $timer;
58     if (!defined($w)) {
59         $w = AnyEvent->timer(
60             after => 2,
61             cb => sub {
62                 say "Connection to i3 timed out. Verify socket path ($socket_path)";
63                 exit 1;
64             }
65         );
66     }
67
68     my $c = sub {
69         $timer = AnyEvent->timer(
70             after => 0.01,
71             cb => sub { $i3->connect->cb(\&connected) }
72         );
73     };
74     $c->();
75 }
76
77 # Connection attempt succeeded or failed
78 sub connected {
79     my ($cv) = @_;
80
81     if (!$cv->recv) {
82         reconnect();
83         return;
84     }
85
86     $w = undef;
87
88     $i3->subscribe({
89         workspace => \&ws_change,
90         output => \&output_change,
91         _error => sub { reconnect() }
92     });
93     ws_change();
94     output_change();
95 }
96
97 # Called when a ws changes
98 sub ws_change {
99     # Request the current workspaces and update the output afterwards
100     $i3->get_workspaces->cb(
101         sub {
102             my ($cv) = @_;
103             $workspaces = $cv->recv;
104             update_output();
105         });
106 }
107
108 # Called when the reply to the GET_OUTPUTS message arrives
109 # Compares old outputs with new outputs and starts/kills
110 # $command for each output (if specified)
111 sub got_outputs {
112     my $reply = shift->recv;
113     my %old = %{$outputs};
114     my %new = map { ($_->{name}, $_) } grep { $_->{active} } @{$reply};
115
116     # If no command was given, we do not need to compare outputs
117     if ($command eq '') {
118         update_output();
119         return;
120     }
121
122     # Handle new outputs
123     for my $name (keys %new) {
124         next if @output_on and !($name ~~ @output_on);
125
126         if (defined($old{$name})) {
127             # Check if the mode changed (by reversing the hashes so
128             # that we can check for equality using the smartmatch op)
129             my %oldrect = reverse %{$old{$name}->{rect}};
130             my %newrect = reverse %{$new{$name}->{rect}};
131             next if (%oldrect ~~ %newrect);
132
133             # On mode changes, we re-start the command
134             $outputs->{$name}->{cmd}->finish;
135             delete $outputs->{$name};
136         }
137
138         my $x = $new{$name}->{rect}->{x};
139         my $launch = $command;
140         $launch =~ s/([^%])%x/$1$x/g;
141         $launch =~ s/%%x/%x/g;
142
143         $new{$name}->{cmd_input} = '';
144         my @cmd = ('/bin/sh', '-c', $launch);
145         $new{$name}->{cmd} = start \@cmd, \$new{$name}->{cmd_input};
146         $outputs->{$name} = $new{$name};
147     }
148
149     # Handle old outputs
150     for my $name (keys %old) {
151         next if defined($new{$name});
152
153         $outputs->{$name}->{cmd}->finish;
154         delete $outputs->{$name};
155     }
156
157     update_output();
158 }
159
160 sub output_change {
161     $i3->get_outputs->cb(\&got_outputs)
162 }
163
164 sub update_output {
165     my $dzen_bg = "#111111";
166     my $out;
167
168     for my $name (keys %{$outputs}) {
169         my $width = $outputs->{$name}->{rect}->{width};
170
171         $out = qq|^pa(;2)|;
172         for my $ws (@{$workspaces}) {
173             next if $ws->{output} ne $name and !$show_all;
174
175             my ($bg, $fg) = qw(333333 888888);
176             ($bg, $fg) = qw(4c7899 ffffff) if $ws->{visible};
177             ($bg, $fg) = qw(900000 ffffff) if $ws->{urgent};
178
179             my $cmd = q|i3-msg "| . $ws->{num} . q|"|;
180             my $name = $ws->{name};
181
182             # Begin the clickable area
183             $out .= qq|^ca(1,$cmd)|;
184
185             # Draw the rest of the bar in the background color, but
186             # don’t move the "cursor"
187             $out .= qq|^p(_LOCK_X)^fg(#$bg)^r(${width}x17)^p(_UNLOCK_X)|;
188             # Draw the name of the workspace without overwriting the
189             # background color
190             $out .= qq|^p(+3)^fg(#$fg)^ib(1)$name^ib(0)^p(+5)|;
191             # Draw the rest of the bar in the normal background color
192             # without moving the "cursor"
193             $out .= qq|^p(_LOCK_X)^fg($dzen_bg)^r(${width}x17)^p(_UNLOCK_X)|;
194
195             # End the clickable area
196             $out .= qq|^ca()|;
197
198             # Move to the next rect, reset Y coordinate
199             $out .= qq|^p(2)^pa(;2)|;
200         }
201
202         $out .= qq|^p(_LOCK_X)^fg($dzen_bg)^r(${width}x17)^p(_UNLOCK_X)^fg(white)|;
203         $out .= qq|^p(+5)|;
204         $out .= $last_line if (!@input_on or $name ~~ @input_on);
205         $out .= "\n";
206
207         $outputs->{$name}->{cmd_input} = $out;
208         try {
209             pump $outputs->{$name}->{cmd} while length $outputs->{$name}->{cmd_input};
210         } catch {
211             warn "Could not write to dzen2";
212             exit 1;
213         }
214     }
215 }
216
217 $i3->connect->cb(\&connected);
218
219 $stdin = AnyEvent->io(
220     fh => \*STDIN,
221     poll => 'r',
222     cb => sub {
223         chomp (my $line = <STDIN>);
224         $last_line = $line;
225         update_output();
226     });
227
228 # let AnyEvent do the rest ("endless loop")
229 AnyEvent->condvar->recv
230
231 __END__
232
233 =head1 NAME
234
235 i3-wsbar - sample implementation of a standalone workspace bar
236
237 =head1 SYNOPSIS
238
239 i3-wsbar -c <dzen2-commandline> [options]
240
241 =head1 OPTIONS
242
243 =over 4
244
245 =item B<--command> <command>
246
247 This command (at the moment only dzen2 is supported) will be started for each
248 output. C<%x> will be replaced with the X coordinate of the output.
249
250 Example:
251     --command "dzen2 -dock -x %x"
252
253 =item B<--input-on> <list-of-RandR-outputs>
254
255 Specifies on which outputs the contents of stdin should be appended to the
256 workspace bar.
257
258 Example:
259     --input-on "LVDS1"
260
261 =item B<--output-on> <list-of-RandR-outputs>
262
263 Specifies for which outputs i3-wsbar should start C<command>.
264
265 =item B<--show-all>
266
267 If enabled, all workspaces are shown (not only those of the current output).
268 Handy to use with C<--output-on>.
269
270 =back
271
272 =cut