2 # vim:ts=4:sw=4:expandtab
8 use Time::HiRes qw(sleep);
11 our @EXPORT = qw(start_xdummy);
13 # reads in a whole file
15 open(my $fh, '<', shift) or return '';
20 =head2 start_xdummy($parallel)
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.
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.
40 $parallel ||= $num_cores * 2;
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/;
48 say "Starting $parallel Xdummy instances, starting at :$displaynum...";
50 for my $idx (0 .. ($parallel-1)) {
52 die "Could not fork: $!" unless defined($pid);
54 # Child, close stdout/stderr, then start Xdummy.
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';
63 push(@childpids, $pid);
64 push(@displays, ":$displaynum");
68 # Wait until the X11 sockets actually appear. Pretty ugly solution, but as
69 # long as we can’t socket-activate X11…
74 my $path = "/tmp/.X11-unix/X" . substr($_, 1);
75 $sockets_ready = 0 unless -S $path;
78 } until $sockets_ready;
80 return \@displays, \@childpids;