From 79798dc40cc25379507355b67d7f88802668a75b Mon Sep 17 00:00:00 2001 From: Michael Stapelberg Date: Mon, 9 Jul 2012 15:49:16 +0200 Subject: [PATCH] use i3 --get-socketpath by default for determining the socket path This was introduced in i3 v4.1 (released 2011-11-11, so should be widespread enough by now). --- lib/AnyEvent/I3.pm | 40 ++++++++++++++++++++++++++++++++++------ 1 file changed, 34 insertions(+), 6 deletions(-) diff --git a/lib/AnyEvent/I3.pm b/lib/AnyEvent/I3.pm index 398040cb..33347638 100644 --- a/lib/AnyEvent/I3.pm +++ b/lib/AnyEvent/I3.pm @@ -29,7 +29,7 @@ then subscribe to events or send messages and receive their replies. use AnyEvent::I3 qw(:all); - my $i3 = i3("~/.i3/ipc.sock"); + my $i3 = i3(); $i3->connect->recv or die "Error connecting"; say "Connected to i3"; @@ -48,8 +48,12 @@ then subscribe to events or send messages and receive their replies. =head2 $i3 = i3([ $path ]); -Creates a new C object and returns it. C is the path of -the UNIX socket to connect to. +Creates a new C object and returns it. + +C is an optional path of the UNIX socket to connect to. It is strongly +advised to NOT specify this unless you're absolutely sure you need it. +C will automatically figure it out by querying the running i3 +instance on the current DISPLAY which is almost always what you want. =head1 SUBROUTINES/METHODS @@ -91,13 +95,37 @@ sub i3 { =head2 $i3 = AnyEvent::I3->new([ $path ]) -Creates a new C object and returns it. C is the path of -the UNIX socket to connect to. +Creates a new C object and returns it. + +C is an optional path of the UNIX socket to connect to. It is strongly +advised to NOT specify this unless you're absolutely sure you need it. +C will automatically figure it out by querying the running i3 +instance on the current DISPLAY which is almost always what you want. =cut sub new { my ($class, $path) = @_; + if (!$path) { + # This effectively circumvents taint mode checking for $ENV{PATH}. We + # do this because users might specify PATH explicitly to call i3 in a + # custom location (think ~/.bin/). + my $paths = $ENV{PATH}; + if ($paths =~ /^(.*)$/) { + $ENV{PATH} = $1; + } + chomp($path = qx(i3 --get-socketpath)); + # Circumventing taint mode again: the socket can be anywhere on the + # system and that’s okay. + if ($path =~ /^([^\0]+)$/) { + $path = $1; + } else { + warn "Asking i3 for the socket path failed. Is DISPLAY set and is i3 in your PATH?"; + } + } + + # This is the old default path (v3.*). This fallback line can be removed in + # a year from now. -- Michael, 2012-07-09 $path ||= '~/.i3/ipc.sock'; # Check if we need to resolve ~ @@ -292,7 +320,7 @@ sub _ensure_connection { return if defined($self->{ipchdl}); - $self->connect->recv or die "Unable to connect to i3" + $self->connect->recv or die "Unable to connect to i3 (socket path " . $self->{path} . ")"; } =head2 get_workspaces -- 2.39.5