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);
15 my $socket_path = undef;
16 my ($workspaces, $outputs) = ([], {});
24 my $result = GetOptions(
25 'command=s' => \$command,
26 'socket=s' => \$socket_path,
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 $i3 = i3($socket_path);
41 my @input_on = split(/,/, $input_on);
42 my @output_on = split(/,/, $output_on);
47 # Wait a short amount of time and try to connect to i3 again
51 $timer = AnyEvent->timer(
53 cb => sub { $i3->connect->cb(\&connected) }
59 # Connection attempt succeeded or failed
69 workspace => \&ws_change,
70 output => \&output_change,
71 _error => sub { reconnect() }
77 # Called when a ws changes
79 # Request the current workspaces and update the output afterwards
80 $i3->get_workspaces->cb(
83 $workspaces = $cv->recv;
88 # Called when the reply to the GET_OUTPUTS message arrives
89 # Compares old outputs with new outputs and starts/kills
90 # $command for each output (if specified)
92 my $reply = shift->recv;
93 my %old = %{$outputs};
94 my %new = map { ($_->{name}, $_) } grep { $_->{active} } @{$reply};
96 # If no command was given, we do not need to compare outputs
103 for my $name (keys %new) {
104 next if @output_on and !($name ~~ @output_on);
106 if (defined($old{$name})) {
107 # Check if the mode changed (by reversing the hashes so
108 # that we can check for equality using the smartmatch op)
109 my %oldrect = reverse %{$old{$name}->{rect}};
110 my %newrect = reverse %{$new{$name}->{rect}};
111 next if (%oldrect ~~ %newrect);
113 # On mode changes, we re-start the command
114 $outputs->{$name}->{cmd}->finish;
115 delete $outputs->{$name};
118 my $x = $new{$name}->{rect}->{x};
119 my $launch = $command;
120 $launch =~ s/([^%])%x/$1$x/g;
121 $launch =~ s/%%x/%x/g;
123 $new{$name}->{cmd_input} = '';
124 my @cmd = ('/bin/sh', '-c', $launch);
125 $new{$name}->{cmd} = start \@cmd, \$new{$name}->{cmd_input};
126 $outputs->{$name} = $new{$name};
130 for my $name (keys %old) {
131 next if defined($new{$name});
133 $outputs->{$name}->{cmd}->finish;
134 delete $outputs->{$name};
141 $i3->get_outputs->cb(\&got_outputs)
145 my $dzen_bg = "#111111";
148 for my $name (keys %{$outputs}) {
149 my $width = $outputs->{$name}->{rect}->{width};
152 for my $ws (@{$workspaces}) {
153 next if $ws->{output} ne $name and !$show_all;
155 my ($bg, $fg) = qw(333333 888888);
156 ($bg, $fg) = qw(4c7899 ffffff) if $ws->{visible};
157 ($bg, $fg) = qw(900000 ffffff) if $ws->{urgent};
159 my $cmd = q|i3-msg "| . $ws->{num} . q|"|;
160 my $name = $ws->{name};
162 # Begin the clickable area
163 $out .= qq|^ca(1,$cmd)|;
165 # Draw the rest of the bar in the background color, but
166 # don’t move the "cursor"
167 $out .= qq|^p(_LOCK_X)^fg(#$bg)^r(${width}x17)^p(_UNLOCK_X)|;
168 # Draw the name of the workspace without overwriting the
170 $out .= qq|^p(+3)^fg(#$fg)^ib(1)$name^ib(0)^p(+5)|;
171 # Draw the rest of the bar in the normal background color
172 # without moving the "cursor"
173 $out .= qq|^p(_LOCK_X)^fg($dzen_bg)^r(${width}x17)^p(_UNLOCK_X)|;
175 # End the clickable area
178 # Move to the next rect, reset Y coordinate
179 $out .= qq|^p(2)^pa(;2)|;
182 $out .= qq|^p(_LOCK_X)^fg($dzen_bg)^r(${width}x17)^p(_UNLOCK_X)^fg(white)|;
184 $out .= $last_line if (!@input_on or $name ~~ @input_on);
187 $outputs->{$name}->{cmd_input} = $out;
188 pump $outputs->{$name}->{cmd} while length $outputs->{$name}->{cmd_input};
192 $i3->connect->cb(\&connected);
194 $stdin = AnyEvent->io(
198 chomp (my $line = <STDIN>);
203 # let AnyEvent do the rest ("endless loop")
204 AnyEvent->condvar->recv
210 i3-wsbar - sample implementation of a standalone workspace bar
214 i3-wsbar -c <dzen2-commandline> [options]
220 =item B<--command> <command>
222 This command (at the moment only dzen2 is supported) will be started for each
223 output. C<%x> will be replaced with the X coordinate of the output.
226 --command "dzen2 -dock -x %x"
228 =item B<--input-on> <list-of-RandR-outputs>
230 Specifies on which outputs the contents of stdin should be appended to the
236 =item B<--output-on> <list-of-RandR-outputs>
238 Specifies for which outputs i3-wsbar should start C<command>.
242 If enabled, all workspaces are shown (not only those of the current output).
243 Handy to use with C<--output-on>.