]> git.sur5r.net Git - i3/i3/blob - testcases/lib/StartXDummy.pm
Merge branch 'complete-run' into next
[i3/i3] / testcases / lib / StartXDummy.pm
1 package StartXDummy;
2 # vim:ts=4:sw=4:expandtab
3
4 use strict;
5 use warnings;
6 use POSIX ();
7 use Exporter 'import';
8 use Time::HiRes qw(sleep);
9 use v5.10;
10
11 our @EXPORT = qw(start_xdummy);
12
13 # reads in a whole file
14 sub slurp {
15     open(my $fh, '<', shift) or return '';
16     local $/;
17     <$fh>;
18 }
19
20 =head2 start_xdummy($parallel)
21
22 Starts C<$parallel> (or number of cores * 2 if undef) Xdummy processes (see
23 the file ./Xdummy) and returns two arrayrefs: a list of X11 display numbers to
24 the Xdummy processes and a list of PIDs of the processes.
25
26 =cut
27 sub start_xdummy {
28     my ($parallel) = @_;
29
30     my @displays = ();
31     my @childpids = ();
32
33     # Yeah, I know it’s non-standard, but Perl’s POSIX module doesn’t have
34     # _SC_NPROCESSORS_CONF.
35     my $cpuinfo = slurp('/proc/cpuinfo');
36     my $num_cores = scalar grep { /model name/ } split("\n", $cpuinfo);
37     # If /proc/cpuinfo does not exist, we fall back to 2 cores.
38     $num_cores ||= 2;
39
40     $parallel ||= $num_cores * 2;
41
42     # First get the last used display number, then increment it by one.
43     # Effectively falls back to 1 if no X server is running.
44     my ($displaynum) = reverse ('0', sort </tmp/.X11-unix/X*>);
45     $displaynum =~ s/.*(\d)$/$1/;
46     $displaynum++;
47
48     say "Starting $parallel Xdummy instances, starting at :$displaynum...";
49
50     for my $idx (0 .. ($parallel-1)) {
51         my $pid = fork();
52         die "Could not fork: $!" unless defined($pid);
53         if ($pid == 0) {
54             # Child, close stdout/stderr, then start Xdummy.
55             POSIX::close(0);
56             POSIX::close(2);
57             # We use -config /dev/null to prevent Xdummy from using the system
58             # Xorg configuration. The tests should be independant from the
59             # actual system X configuration.
60             exec './Xdummy', ":$displaynum", '-config', '/dev/null';
61             exit 1;
62         }
63         push(@childpids, $pid);
64         push(@displays, ":$displaynum");
65         $displaynum++;
66     }
67
68     # Wait until the X11 sockets actually appear. Pretty ugly solution, but as
69     # long as we can’t socket-activate X11…
70     my $sockets_ready;
71     do {
72         $sockets_ready = 1;
73         for (@displays) {
74             my $path = "/tmp/.X11-unix/X" . substr($_, 1);
75             $sockets_ready = 0 unless -S $path;
76         }
77         sleep 0.1;
78     } until $sockets_ready;
79
80     return \@displays, \@childpids;
81 }
82
83 1