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) = ([], {});
18 my $w = AnyEvent->timer(
21 say "Connection to i3 timed out. Verify socket path ($socket_path)";
31 my $result = GetOptions(
32 'command=s' => \$command,
33 'socket=s' => \$socket_path,
34 'input-on=s' => \$input_on,
35 'output-on=s' => \$output_on,
36 'show-all' => \$show_all,
37 'help' => sub { pod2usage(1); exit 0 },
41 say "i3-wsbar is only useful in combination with dzen2.";
42 say "Please specify -c (command)";
46 my $i3 = i3($socket_path);
48 my @input_on = split(/,/, $input_on);
49 my @output_on = split(/,/, $output_on);
54 # Wait a short amount of time and try to connect to i3 again
61 say "Connection to i3 timed out. Verify socket path ($socket_path)";
68 $timer = AnyEvent->timer(
70 cb => sub { $i3->connect->cb(\&connected) }
76 # Connection attempt succeeded or failed
88 workspace => \&ws_change,
89 output => \&output_change,
90 _error => sub { reconnect() }
96 # Called when a ws changes
98 # Request the current workspaces and update the output afterwards
99 $i3->get_workspaces->cb(
102 $workspaces = $cv->recv;
107 # Called when the reply to the GET_OUTPUTS message arrives
108 # Compares old outputs with new outputs and starts/kills
109 # $command for each output (if specified)
111 my $reply = shift->recv;
112 my %old = %{$outputs};
113 my %new = map { ($_->{name}, $_) } grep { $_->{active} } @{$reply};
115 # If no command was given, we do not need to compare outputs
116 if ($command eq '') {
122 for my $name (keys %new) {
123 next if @output_on and !($name ~~ @output_on);
125 if (defined($old{$name})) {
126 # Check if the mode changed (by reversing the hashes so
127 # that we can check for equality using the smartmatch op)
128 my %oldrect = reverse %{$old{$name}->{rect}};
129 my %newrect = reverse %{$new{$name}->{rect}};
130 next if (%oldrect ~~ %newrect);
132 # On mode changes, we re-start the command
133 $outputs->{$name}->{cmd}->finish;
134 delete $outputs->{$name};
137 my $x = $new{$name}->{rect}->{x};
138 my $launch = $command;
139 $launch =~ s/([^%])%x/$1$x/g;
140 $launch =~ s/%%x/%x/g;
142 $new{$name}->{cmd_input} = '';
143 my @cmd = ('/bin/sh', '-c', $launch);
144 $new{$name}->{cmd} = start \@cmd, \$new{$name}->{cmd_input};
145 $outputs->{$name} = $new{$name};
149 for my $name (keys %old) {
150 next if defined($new{$name});
152 $outputs->{$name}->{cmd}->finish;
153 delete $outputs->{$name};
160 $i3->get_outputs->cb(\&got_outputs)
164 my $dzen_bg = "#111111";
167 for my $name (keys %{$outputs}) {
168 my $width = $outputs->{$name}->{rect}->{width};
171 for my $ws (@{$workspaces}) {
172 next if $ws->{output} ne $name and !$show_all;
174 my ($bg, $fg) = qw(333333 888888);
175 ($bg, $fg) = qw(4c7899 ffffff) if $ws->{visible};
176 ($bg, $fg) = qw(900000 ffffff) if $ws->{urgent};
178 my $cmd = q|i3-msg "| . $ws->{num} . q|"|;
179 my $name = $ws->{name};
181 # Begin the clickable area
182 $out .= qq|^ca(1,$cmd)|;
184 # Draw the rest of the bar in the background color, but
185 # don’t move the "cursor"
186 $out .= qq|^p(_LOCK_X)^fg(#$bg)^r(${width}x17)^p(_UNLOCK_X)|;
187 # Draw the name of the workspace without overwriting the
189 $out .= qq|^p(+3)^fg(#$fg)^ib(1)$name^ib(0)^p(+5)|;
190 # Draw the rest of the bar in the normal background color
191 # without moving the "cursor"
192 $out .= qq|^p(_LOCK_X)^fg($dzen_bg)^r(${width}x17)^p(_UNLOCK_X)|;
194 # End the clickable area
197 # Move to the next rect, reset Y coordinate
198 $out .= qq|^p(2)^pa(;2)|;
201 $out .= qq|^p(_LOCK_X)^fg($dzen_bg)^r(${width}x17)^p(_UNLOCK_X)^fg(white)|;
203 $out .= $last_line if (!@input_on or $name ~~ @input_on);
206 $outputs->{$name}->{cmd_input} = $out;
207 pump $outputs->{$name}->{cmd} while length $outputs->{$name}->{cmd_input};
211 $i3->connect->cb(\&connected);
213 $stdin = AnyEvent->io(
217 chomp (my $line = <STDIN>);
222 # let AnyEvent do the rest ("endless loop")
223 AnyEvent->condvar->recv
229 i3-wsbar - sample implementation of a standalone workspace bar
233 i3-wsbar -c <dzen2-commandline> [options]
239 =item B<--command> <command>
241 This command (at the moment only dzen2 is supported) will be started for each
242 output. C<%x> will be replaced with the X coordinate of the output.
245 --command "dzen2 -dock -x %x"
247 =item B<--input-on> <list-of-RandR-outputs>
249 Specifies on which outputs the contents of stdin should be appended to the
255 =item B<--output-on> <list-of-RandR-outputs>
257 Specifies for which outputs i3-wsbar should start C<command>.
261 If enabled, all workspaces are shown (not only those of the current output).
262 Handy to use with C<--output-on>.