2 # vim:ts=4:sw=4:expandtab:ft=perl
3 # © 2010 Michael Stapelberg, see LICENSE for license information
9 use IPC::Run qw(start pump);
17 my ($workspaces, $outputs) = ([], {});
25 my $result = GetOptions(
26 'command=s' => \$command,
27 'input-on=s' => \$input_on,
28 'output-on=s' => \$output_on,
29 'show-all' => \$show_all,
30 'help' => sub { pod2usage(1); exit 0 },
34 say "i3-wsbar is only useful in combination with dzen2.";
35 say "Please specify -c (command)";
39 my @input_on = split(/,/, $input_on);
40 my @output_on = split(/,/, $output_on);
45 # Wait a short amount of time and try to connect to i3 again
49 $timer = AnyEvent->timer(
51 cb => sub { $i3->connect->cb(\&connected) }
57 # Connection attempt succeeded or failed
67 workspace => \&ws_change,
68 output => \&output_change,
69 _error => sub { reconnect() }
75 # Called when a ws changes
77 # Request the current workspaces and update the output afterwards
78 $i3->get_workspaces->cb(
81 $workspaces = $cv->recv;
86 # Called when the reply to the GET_OUTPUTS message arrives
87 # Compares old outputs with new outputs and starts/kills
88 # $command for each output (if specified)
90 my $reply = shift->recv;
91 my %old = %{$outputs};
92 my %new = map { ($_->{name}, $_) } grep { $_->{active} } @{$reply};
94 # If no command was given, we do not need to compare outputs
101 for my $name (keys %new) {
102 next if @output_on and !($name ~~ @output_on);
104 if (defined($old{$name})) {
105 # Check if the mode changed (by reversing the hashes so
106 # that we can check for equality using the smartmatch op)
107 my %oldrect = reverse %{$old{$name}->{rect}};
108 my %newrect = reverse %{$new{$name}->{rect}};
109 next if (%oldrect ~~ %newrect);
111 # On mode changes, we re-start the command
112 $outputs->{$name}->{cmd}->finish;
113 delete $outputs->{$name};
116 my $x = $new{$name}->{rect}->{x};
117 my $launch = $command;
118 $launch =~ s/([^%])%x/$1$x/g;
119 $launch =~ s/%%x/%x/g;
121 $new{$name}->{cmd_input} = '';
122 my @cmd = ('/bin/sh', '-c', $launch);
123 $new{$name}->{cmd} = start \@cmd, \$new{$name}->{cmd_input};
124 $outputs->{$name} = $new{$name};
128 for my $name (keys %old) {
129 next if defined($new{$name});
131 $outputs->{$name}->{cmd}->finish;
132 delete $outputs->{$name};
139 $i3->get_outputs->cb(\&got_outputs)
143 my $dzen_bg = "#111111";
146 for my $name (keys %{$outputs}) {
147 my $width = $outputs->{$name}->{rect}->{width};
150 for my $ws (@{$workspaces}) {
151 next if $ws->{output} ne $name and !$show_all;
153 my ($bg, $fg) = qw(333333 888888);
154 ($bg, $fg) = qw(4c7899 ffffff) if $ws->{visible};
155 ($bg, $fg) = qw(900000 ffffff) if $ws->{urgent};
157 my $cmd = q|i3-msg "| . $ws->{num} . q|"|;
158 my $name = $ws->{name};
160 # Begin the clickable area
161 $out .= qq|^ca(1,$cmd)|;
163 # Draw the rest of the bar in the background color, but
164 # don’t move the "cursor"
165 $out .= qq|^p(_LOCK_X)^fg(#$bg)^r(${width}x17)^p(_UNLOCK_X)|;
166 # Draw the name of the workspace without overwriting the
168 $out .= qq|^p(+3)^fg(#$fg)^ib(1)$name^ib(0)^p(+5)|;
169 # Draw the rest of the bar in the normal background color
170 # without moving the "cursor"
171 $out .= qq|^p(_LOCK_X)^fg($dzen_bg)^r(${width}x17)^p(_UNLOCK_X)|;
173 # End the clickable area
176 # Move to the next rect, reset Y coordinate
177 $out .= qq|^p(2)^pa(;2)|;
180 $out .= qq|^p(_LOCK_X)^fg($dzen_bg)^r(${width}x17)^p(_UNLOCK_X)^fg(white)|;
182 $out .= $last_line if (!@input_on or $name ~~ @input_on);
185 $outputs->{$name}->{cmd_input} = $out;
187 pump $outputs->{$name}->{cmd} while length $outputs->{$name}->{cmd_input};
189 warn "Could not write to dzen2";
195 $i3->connect->cb(\&connected);
197 $stdin = AnyEvent->io(
201 chomp (my $line = <STDIN>);
206 # let AnyEvent do the rest ("endless loop")
207 AnyEvent->condvar->recv
213 i3-wsbar - sample implementation of a standalone workspace bar
217 i3-wsbar -c <dzen2-commandline> [options]
223 =item B<--command> <command>
225 This command (at the moment only dzen2 is supported) will be started for each
226 output. C<%x> will be replaced with the X coordinate of the output.
229 --command "dzen2 -dock -x %x"
231 =item B<--input-on> <list-of-RandR-outputs>
233 Specifies on which outputs the contents of stdin should be appended to the
239 =item B<--output-on> <list-of-RandR-outputs>
241 Specifies for which outputs i3-wsbar should start C<command>.
245 If enabled, all workspaces are shown (not only those of the current output).
246 Handy to use with C<--output-on>.