2 # vim:ts=4:sw=4:expandtab
7 use Time::HiRes qw(sleep);
10 our @EXPORT = qw(start_xdummy);
12 # reads in a whole file
14 open(my $fh, '<', shift) or return '';
19 =head2 start_xdummy($parallel)
21 Starts C<$parallel> (or number of cores * 2 if undef) Xdummy processes (see
22 the file ./Xdummy) and returns two arrayrefs: a list of X11 display numbers to
23 the Xdummy processes and a list of PIDs of the processes.
32 # Yeah, I know it’s non-standard, but Perl’s POSIX module doesn’t have
33 # _SC_NPROCESSORS_CONF.
34 my $cpuinfo = slurp('/proc/cpuinfo');
35 my $num_cores = scalar grep { /model name/ } split("\n", $cpuinfo);
36 # If /proc/cpuinfo does not exist, we fall back to 2 cores.
39 $parallel ||= $num_cores * 2;
41 # First get the last used display number, then increment it by one.
42 # Effectively falls back to 1 if no X server is running.
43 my ($displaynum) = reverse ('0', sort </tmp/.X11-unix/X*>);
44 $displaynum =~ s/.*(\d)$/$1/;
47 say "Starting $parallel Xdummy instances, starting at :$displaynum...";
49 for my $idx (0 .. ($parallel-1)) {
51 die "Could not fork: $!" unless defined($pid);
53 # Child, close stdout/stderr, then start Xdummy.
56 # We use -config /dev/null to prevent Xdummy from using the system
57 # Xorg configuration. The tests should be independant from the
58 # actual system X configuration.
59 exec './Xdummy', ":$displaynum", '-config', '/dev/null';
62 push(@childpids, $pid);
63 push(@displays, ":$displaynum");
67 # Wait until the X11 sockets actually appear. Pretty ugly solution, but as
68 # long as we can’t socket-activate X11…
73 my $path = "/tmp/.X11-unix/X" . substr($_, 1);
74 $sockets_ready = 0 unless -S $path;
77 } until $sockets_ready;
79 return \@displays, \@childpids;