ipc: implement GET_WORKSPACES message type

This is the foundation to use dzen2 or similar as a complete
replacement for the internal workspaces bar.

A testcase is included, more documentation about the IPC interface
will follow.
This commit is contained in:
Michael Stapelberg
2010-03-11 15:58:39 +01:00
parent 952914c3c5
commit 9a9ba1b859
10 changed files with 192 additions and 10 deletions

View File

@ -1,4 +1,4 @@
test:
PERL_DL_NONLAZY=1 /usr/bin/perl "-MExtUtils::Command::MM" "-e" "test_harness(1)" t/*.t
PERL_DL_NONLAZY=1 /usr/bin/perl "-MExtUtils::Command::MM" "-e" "test_harness(1)" t/15*.t
all: test

View File

@ -0,0 +1,56 @@
#!perl
# vim:ts=4:sw=4:expandtab
use Test::More tests => 8;
use Test::Exception;
use Data::Dumper;
use JSON::XS;
use List::MoreUtils qw(all);
use FindBin;
use lib "$FindBin::Bin/lib";
use i3test;
BEGIN {
use_ok('IO::Socket::UNIX') or BAIL_OUT('Cannot load IO::Socket::UNIX');
use_ok('X11::XCB::Connection') or BAIL_OUT('Cannot load X11::XCB::Connection');
}
my $sock = IO::Socket::UNIX->new(Peer => '/tmp/i3-ipc.sock');
isa_ok($sock, 'IO::Socket::UNIX');
####################
# Request workspaces
####################
# message type 1 is GET_WORKSPACES
my $message = "i3-ipc" . pack("LL", 0, 1);
$sock->write($message);
#######################################
# Test the reply format for correctness
#######################################
# The following lines duplicate functionality from recv_ipc_command
# to have it included in the test-suite.
my $buffer;
$sock->read($buffer, length($message));
is(substr($buffer, 0, length("i3-ipc")), "i3-ipc", "ipc message received");
my ($len, $type) = unpack("LL", substr($buffer, 6));
is($type, 1, "correct reply type");
# read the payload
$sock->read($buffer, $len);
my $workspaces;
#########################
# Actually test the reply
#########################
lives_ok { $workspaces = decode_json($buffer) } 'JSON could be decoded';
ok(@{$workspaces} > 0, "More than zero workspaces found");
my $name_exists = all { defined($_->{name}) } @{$workspaces};
ok($name_exists, "All workspaces have a name");
diag( "Testing i3, Perl $], $^X" );

View File

@ -40,4 +40,22 @@ sub format_ipc_command {
return $message;
}
sub recv_ipc_command {
my ($sock, $expected) = @_;
my $buffer;
# header is 14 bytes ("i3-ipc" + 32 bit + 32 bit)
$sock->read($buffer, 14);
return undef unless substr($buffer, 0, length("i3-ipc")) eq "i3-ipc";
my ($len, $type) = unpack("LL", substr($buffer, 6));
return undef unless $type == $expected;
# read the payload
$sock->read($buffer, $len);
decode_json($buffer)
}
1