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