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 class XScopedDisplay
{
39 XScopedDisplay() : display_(NULL
) {}
41 if (display_
) XCloseDisplay(display_
);
44 void set(Display
* display
) { display_
= display
; }
45 Display
* display() { return display_
; }
51 int main(int argc
, char* argv
[]) {
52 XScopedDisplay scoped_display
;
53 if (argv
[1] && strcmp(argv
[1], "--noserver") == 0) {
54 scoped_display
.set(XOpenDisplay(NULL
));
55 if (scoped_display
.display()) {
56 fprintf(stderr
, "Found unexpected connectable display %s\n",
59 // Return success when we got an unexpected display so that the code
60 // without the --noserver is the same, but slow, rather than inverted.
61 return !scoped_display
.display();
64 int kNumTries
= 78; // 78*77/2 * 10 = 30s of waiting
66 for (tries
= 0; tries
< kNumTries
; ++tries
) {
67 scoped_display
.set(XOpenDisplay(NULL
));
68 if (scoped_display
.display())
73 if (!scoped_display
.display()) {
74 fprintf(stderr
, "Failed to connect to %s\n", XDisplayName(NULL
));
78 fprintf(stderr
, "Connected after %d retries\n", tries
);
82 int opcode
, event
, err
;
83 if (!XQueryExtension(scoped_display
.display(), "XInputExtension", &opcode
,
86 "Failed to get XInputExtension on %s.\n", XDisplayName(NULL
));
90 int major
= 2, minor
= 0;
91 if (XIQueryVersion(scoped_display
.display(), &major
, &minor
) == BadRequest
) {
93 "Server does not have XInput2 on %s.\n", XDisplayName(NULL
));
97 // Ask for the list of devices. This can cause some Xvfb to crash.
99 XIDeviceInfo
* devices
=
100 XIQueryDevice(scoped_display
.display(), XIAllDevices
, &count
);
102 XIFreeDeviceInfo(devices
);
105 "XInput2 verified initially sane on %s.\n", XDisplayName(NULL
));
110 #if defined(LEAK_SANITIZER)
111 // XOpenDisplay leaks memory if it takes more than one try to connect. This
112 // causes LSan bots to fail. We don't care about memory leaks in xdisplaycheck
113 // anyway, so just disable LSan completely.
114 // This function isn't referenced from the executable itself. Make sure it isn't
115 // stripped by the linker.
116 __attribute__((used
))
117 __attribute__((visibility("default")))
118 extern "C" int __lsan_is_turned_off() { return 1; }