summaryrefslogtreecommitdiffstats
path: root/features/support/helpers/display_helper.rb
blob: 354935f0354bb5e0088a9e66c07db609de5a4c30 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
class Display

  def initialize(domain, x_display)
    @domain = domain
    @x_display = x_display
  end

  def start
    start_virtviewer(@domain)
    # We wait for the display to be active to not lose actions
    # (e.g. key presses via sikuli) that come immediately after
    # starting (or restoring) a vm
    try_for(20, { :delay => 0.1, :msg => "virt-viewer failed to start"}) {
      active?
    }
  end

  def stop
    stop_virtviewer
  end

  def restart
    stop_virtviewer
    start_virtviewer(@domain)
  end

  def start_virtviewer(domain)
    # virt-viewer forks, so we cannot (easily) get the child pid
    # and use it in active? and stop_virtviewer below...
    IO.popen(["virt-viewer", "-d",
                             "-f",
                             "-r",
                             "-c", "qemu:///system",
                             ["--display=", @x_display].join(''),
                             domain,
                             "&"].join(' '))
  end

  def active?
    p = IO.popen("xprop -display #{@x_display} " +
                 "-name '#{@domain} (1) - Virt Viewer' 2>/dev/null")
    Process.wait(p.pid)
    p.close
    $? == 0
  end

  def stop_virtviewer
    system("killall virt-viewer")
  end
end