1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 // This is a small program that tries to connect to the X server. It
6 // continually retries until it connects or 30 seconds pass. If it fails
7 // to connect to the X server or fails to find needed functiona, it returns
8 // an error code of -1.
10 // This is to help verify that a useful X server is available before we start
11 // start running tests on the build bots.
20 #include <X11/extensions/XInput2.h>
23 void Sleep(int duration_ms
) {
24 struct timespec sleep_time
, remaining
;
26 // Contains the portion of duration_ms >= 1 sec.
27 sleep_time
.tv_sec
= duration_ms
/ 1000;
28 duration_ms
-= sleep_time
.tv_sec
* 1000;
30 // Contains the portion of duration_ms < 1 sec.
31 sleep_time
.tv_nsec
= duration_ms
* 1000 * 1000; // nanoseconds.
33 while (nanosleep(&sleep_time
, &remaining
) == -1 && errno
== EINTR
)
34 sleep_time
= remaining
;
37 int main(int argc
, char* argv
[]) {
38 Display
* display
= NULL
;
39 if (argv
[1] && strcmp(argv
[1], "--noserver") == 0) {
40 display
= XOpenDisplay(NULL
);
42 fprintf(stderr
, "Found unexpected connectable display %s\n",
45 // Return success when we got an unexpected display so that the code
46 // without the --noserver is the same, but slow, rather than inverted.
50 int kNumTries
= 78; // 78*77/2 * 10 = 30s of waiting
52 for (tries
= 0; tries
< kNumTries
; ++tries
) {
53 display
= XOpenDisplay(NULL
);
60 fprintf(stderr
, "Failed to connect to %s\n", XDisplayName(NULL
));
64 fprintf(stderr
, "Connected after %d retries\n", tries
);
68 int opcode
, event
, err
;
69 if (!XQueryExtension(display
, "XInputExtension", &opcode
, &event
, &err
)) {
71 "Failed to get XInputExtension on %s.\n", XDisplayName(NULL
));
75 int major
= 2, minor
= 0;
76 if (XIQueryVersion(display
, &major
, &minor
) == BadRequest
) {
78 "Server does not have XInput2 on %s.\n", XDisplayName(NULL
));
82 // Ask for the list of devices. This can cause some Xvfb to crash.
84 XIDeviceInfo
* devices
= XIQueryDevice(display
, XIAllDevices
, &count
);
86 XIFreeDeviceInfo(devices
);
89 "XInput2 verified initially sane on %s.\n", XDisplayName(NULL
));