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);
16 my ($workspaces, $outputs) = ([], {});
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 },
33 say "i3-wsbar is only useful in combination with dzen2.";
34 say "Please specify -c (command)";
38 my @input_on = split(/,/, $input_on);
39 my @output_on = split(/,/, $output_on);
44 # Wait a short amount of time and try to connect to i3 again
48 $timer = AnyEvent->timer(
50 cb => sub { $i3->connect->cb(\&connected) }
56 # Connection attempt succeeded or failed
66 workspace => \&ws_change,
67 output => \&output_change,
68 _error => sub { reconnect() }
74 # Called when a ws changes
76 # Request the current workspaces and update the output afterwards
77 $i3->get_workspaces->cb(
80 $workspaces = $cv->recv;
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)
89 my $reply = shift->recv;
90 my %old = %{$outputs};
91 my %new = map { ($_->{name}, $_) } grep { $_->{active} } @{$reply};
93 # If no command was given, we do not need to compare outputs
100 for my $name (keys %new) {
101 next if @output_on and !($name ~~ @output_on);
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);
110 # On mode changes, we re-start the command
111 $outputs->{$name}->{cmd}->finish;
112 delete $outputs->{$name};
115 my $x = $new{$name}->{rect}->{x};
116 my $launch = $command;
117 $launch =~ s/([^%])%x/$1$x/g;
118 $launch =~ s/%%x/%x/g;
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};
127 for my $name (keys %old) {
128 next if defined($new{$name});
130 $outputs->{$name}->{cmd}->finish;
131 delete $outputs->{$name};
138 $i3->get_outputs->cb(\&got_outputs)
142 my $dzen_bg = "#111111";
145 for my $name (keys %{$outputs}) {
146 my $width = $outputs->{$name}->{rect}->{width};
149 for my $ws (@{$workspaces}) {
150 next if $ws->{output} ne $name and !$show_all;
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};
156 my $cmd = q|i3-msg "| . $ws->{num} . q|"|;
157 my $name = $ws->{name};
159 # Begin the clickable area
160 $out .= qq|^ca(1,$cmd)|;
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
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)|;
172 # End the clickable area
175 # Move to the next rect, reset Y coordinate
176 $out .= qq|^p(2)^pa(;2)|;
179 $out .= qq|^p(_LOCK_X)^fg($dzen_bg)^r(${width}x17)^p(_UNLOCK_X)^fg(white)|;
181 $out .= $last_line if (!@input_on or $name ~~ @input_on);
184 $outputs->{$name}->{cmd_input} = $out;
185 pump $outputs->{$name}->{cmd} while length $outputs->{$name}->{cmd_input};
189 $i3->connect->cb(\&connected);
191 $stdin = AnyEvent->io(
195 chomp (my $line = <STDIN>);
200 # let AnyEvent do the rest ("endless loop")
201 AnyEvent->condvar->recv
207 i3-wsbar - sample implementation of a standalone workspace bar
211 i3-wsbar -c <dzen2-commandline> [options]
217 =item B<--command> <command>
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.
223 --command "dzen2 -dock -x %x"
225 =item B<--input-on> <list-of-RandR-outputs>
227 Specifies on which outputs the contents of stdin should be appended to the
233 =item B<--output-on> <list-of-RandR-outputs>
235 Specifies for which outputs i3-wsbar should start C<command>.
239 If enabled, all workspaces are shown (not only those of the current output).
240 Handy to use with C<--output-on>.