]> git.sur5r.net Git - i3/i3/blob - i3-wsbar
Merge i3bar into next
[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 $w = $new{$name}->{rect}->{width};
140         my $launch = $command;
141         $launch =~ s/([^%])%x/$1$x/g;
142         $launch =~ s/([^%])%w/$1$w/g;
143         $launch =~ s/%%x/%x/g;
144         $launch =~ s/%%w/%w/g;
145
146         $new{$name}->{cmd_input} = '';
147         my @cmd = ('/bin/sh', '-c', $launch);
148         $new{$name}->{cmd} = start \@cmd, \$new{$name}->{cmd_input};
149         $outputs->{$name} = $new{$name};
150     }
151
152     # Handle old outputs
153     for my $name (keys %old) {
154         next if defined($new{$name});
155
156         $outputs->{$name}->{cmd}->finish;
157         delete $outputs->{$name};
158     }
159
160     update_output();
161 }
162
163 sub output_change {
164     $i3->get_outputs->cb(\&got_outputs)
165 }
166
167 sub update_output {
168     my $dzen_bg = "#111111";
169     my $out;
170
171     for my $name (keys %{$outputs}) {
172         my $width = $outputs->{$name}->{rect}->{width};
173
174         $out = qq|^pa(;2)|;
175         for my $ws (@{$workspaces}) {
176             next if $ws->{output} ne $name and !$show_all;
177
178             my ($bg, $fg) = qw(333333 888888);
179             ($bg, $fg) = qw(4c7899 ffffff) if $ws->{visible};
180             ($bg, $fg) = qw(900000 ffffff) if $ws->{urgent};
181
182             my $cmd = q|i3-msg "| . $ws->{num} . q|"|;
183             my $name = $ws->{name};
184
185             # Begin the clickable area
186             $out .= qq|^ca(1,$cmd)|;
187
188             # Draw the rest of the bar in the background color, but
189             # don’t move the "cursor"
190             $out .= qq|^p(_LOCK_X)^fg(#$bg)^r(${width}x17)^p(_UNLOCK_X)|;
191             # Draw the name of the workspace without overwriting the
192             # background color
193             $out .= qq|^p(+3)^fg(#$fg)^ib(1)$name^ib(0)^p(+5)|;
194             # Draw the rest of the bar in the normal background color
195             # without moving the "cursor"
196             $out .= qq|^p(_LOCK_X)^fg($dzen_bg)^r(${width}x17)^p(_UNLOCK_X)|;
197
198             # End the clickable area
199             $out .= qq|^ca()|;
200
201             # Move to the next rect, reset Y coordinate
202             $out .= qq|^p(2)^pa(;2)|;
203         }
204
205         $out .= qq|^p(_LOCK_X)^fg($dzen_bg)^r(${width}x17)^p(_UNLOCK_X)^fg()|;
206         $out .= qq|^p(+5)|;
207         $out .= $last_line if (!@input_on or $name ~~ @input_on);
208         $out .= "\n";
209
210         $outputs->{$name}->{cmd_input} = $out;
211         try {
212             pump $outputs->{$name}->{cmd} while length $outputs->{$name}->{cmd_input};
213         } catch {
214             warn "Could not write to dzen2";
215             exit 1;
216         }
217     }
218 }
219
220 $i3->connect->cb(\&connected);
221
222 $stdin = AnyEvent->io(
223     fh => \*STDIN,
224     poll => 'r',
225     cb => sub {
226         chomp (my $line = <STDIN>);
227         $last_line = $line;
228         update_output();
229     });
230
231 # let AnyEvent do the rest ("endless loop")
232 AnyEvent->condvar->recv
233
234 __END__
235
236 =head1 NAME
237
238 i3-wsbar - sample implementation of a standalone workspace bar
239
240 =head1 SYNOPSIS
241
242 i3-wsbar -c <dzen2-commandline> [options]
243
244 =head1 OPTIONS
245
246 =over 4
247
248 =item B<--command> <command>
249
250 This command (at the moment only dzen2 is supported) will be started for each
251 output. C<%x> will be replaced with the X coordinate of the output, C<%w> will
252 be replaced with the width of the output.
253
254 Example:
255     --command "dzen2 -dock -x %x -w %w"
256
257 =item B<--input-on> <list-of-RandR-outputs>
258
259 Specifies on which outputs the contents of stdin should be appended to the
260 workspace bar.
261
262 Example:
263     --input-on "LVDS1"
264
265 =item B<--output-on> <list-of-RandR-outputs>
266
267 Specifies for which outputs i3-wsbar should start C<command>.
268
269 =item B<--show-all>
270
271 If enabled, all workspaces are shown (not only those of the current output).
272 Handy to use with C<--output-on>.
273
274 =back
275
276 =cut