The i3 FAQ has migrated to https://www.reddit.com/r/i3wm/. All content here is read-only.
Ask Your Question
0

How do i subscribe to i3-events using bash easily?

asked 2015-04-02 01:13:07 +0000

gurkensalat gravatar image

updated 2015-04-26 17:00:07 +0000

How do i subscribe to i3-events using bash easily?

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
1

answered 2015-04-02 01:15:16 +0000

gurkensalat gravatar image

updated 2015-05-25 15:24:12 +0000

You can use the following perl script "i3subscribe" to subscribe to i3-events:

#!/usr/bin/env perl

BEGIN { $| = 1 } # flush \n

use strict;
use warnings;
use Data::Dumper;
use AnyEvent::I3;
use v5.10;

my $i3 = i3();
$i3->connect->recv or die "Error connecting to i3";

sub subscribe {
    my $ev = $_[0];
    my $dump = $_[1];
    if($i3->subscribe({
        $ev => sub {
            my ($msg) = @_;
            say "$ev:$msg->{'change'}";
            if($dump) {
                print Dumper($msg);
            }
        }
    })->recv->{success}) {
        say "Successfully subscribed to $ev-event";
    }
}

my $nextArg = shift;
if(!$nextArg) {
    say "Subscribe to i3-events";
    say "Usage:   $0 workspace|output|mode|window|barconfig_update|binding [dump]";
    say "Example: $0 workspace dump window binding dump";
    exit 1;
}
while($nextArg) {
    my $arg = $nextArg;
    $nextArg = shift;
    my $dump = 0;
    if($nextArg and $nextArg eq "dump") {
        $dump = 1;
        $nextArg = shift;
    }
    subscribe("$arg", $dump);
}
AE::cv->recv;

Example:

i3subscribe window workspace | while read -r event; do
...
done

Example use case: Different keyboard layout for every window

edit flag offensive delete link more

Comments

The link seems to be dead. Do you have some source repository for it (github, bitbucket, etc.)? If it is not to long you could also post it here entirely.

Adaephon gravatar imageAdaephon ( 2015-04-24 06:04:09 +0000 )edit

updated my post, thank you

gurkensalat gravatar imagegurkensalat ( 2015-04-26 16:54:25 +0000 )edit

Question Tools

Stats

Asked: 2015-04-02 01:13:07 +0000

Seen: 443 times

Last updated: May 25