]> git.sur5r.net Git - i3/i3/blob - testcases/lib/i3test/Util.pm
i3test::XTEST: don’t “use i3test” to avoid clobbering state
[i3/i3] / testcases / lib / i3test / Util.pm
1 package i3test::Util;
2 # vim:ts=4:sw=4:expandtab
3
4 use strict;
5 use warnings;
6 use v5.10;
7
8 use X11::XCB qw(GET_PROPERTY_TYPE_ANY);
9 use X11::XCB::Connection;
10
11 use Exporter qw(import);
12 our @EXPORT = qw(
13     slurp
14     get_socket_path
15 );
16
17 =encoding utf-8
18
19 =head1 NAME
20
21 i3test::Util - General utility functions
22
23 =cut
24
25 =head1 EXPORT
26
27 =cut
28
29 =head2 slurp($fn)
30
31 Reads the entire file specified in the arguments and returns the content.
32
33 =cut
34 sub slurp {
35     my ($file) = @_;
36     my $content = do {
37         local $/ = undef;
38         open my $fh, "<", $file or die "could not open $file: $!";
39         <$fh>;
40     };
41
42     return $content;
43 }
44
45 =head2 get_socket_path([X11::XCB::Connection])
46
47 Gets the socket path from the C<I3_SOCKET_PATH> atom stored on the X11 root
48 window.
49
50 =cut
51 sub get_socket_path {
52     my ($x) = @_;
53     $x //= X11::XCB::Connection->new();
54     my $atom = $x->atom(name => 'I3_SOCKET_PATH');
55     my $cookie = $x->get_property(0, $x->get_root_window(), $atom->id, GET_PROPERTY_TYPE_ANY, 0, 256);
56     my $reply = $x->get_property_reply($cookie->{sequence});
57     my $socketpath = $reply->{value};
58     if ($socketpath eq "/tmp/nested-$ENV{DISPLAY}") {
59         $socketpath .= '-activation';
60     }
61     return $socketpath;
62 }
63
64 =head1 AUTHOR
65
66 Michael Stapelberg <michael@i3wm.org>
67
68 =cut
69
70 1