Commit 4bb193b4 authored by Tom Anderson's avatar Tom Anderson Committed by Commit Bot

Port xwmstartupcheck to XProto

This removes all usage of Xlib except for ConnectionNumber.

R=msisov

Bug: 1066670
Change-Id: Ic714254bfda1fc2087c089dbe471b92e3b4c4915
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2373186
Commit-Queue: Thomas Anderson <thomasanderson@chromium.org>
Reviewed-by: default avatarMaksim Sisov (GMT+3) <msisov@igalia.com>
Cr-Commit-Position: refs/heads/master@{#801410}
parent cd7ad821
......@@ -7,7 +7,10 @@ executable("xwmstartupcheck") {
sources = [ "xwmstartupcheck.cc" ]
deps = [ "//base" ]
deps = [
"//base",
"//ui/gfx/x",
]
configs += [ "//build/config/linux:x11" ]
}
......@@ -7,12 +7,15 @@
// BEFORE the Wm starts.
//
#include <time.h>
#include <cerrno>
#include <cstdio>
#include <time.h>
#include <X11/Xlib.h>
#include "base/command_line.h"
#include "ui/gfx/x/connection.h"
#include "ui/gfx/x/x11.h"
#include "ui/gfx/x/xproto.h"
void CalculateTimeout(const timespec& now,
const timespec& deadline,
......@@ -24,53 +27,37 @@ void CalculateTimeout(const timespec& now,
timeout->tv_sec = 0;
}
class XScopedDisplay {
public:
explicit XScopedDisplay(Display* display) : display_(display) {}
~XScopedDisplay() {
if (display_)
XCloseDisplay(display_);
}
Display* display() const { return display_; }
private:
Display* const display_;
};
int main(int argc, char* argv[]) {
// Connects to a display specified in the current process' env value DISPLAY.
XScopedDisplay scoped_display(XOpenDisplay(nullptr));
// No display found - fail early.
if (!scoped_display.display()) {
fprintf(stderr, "Couldn't connect to a display.\n");
return 1;
}
base::CommandLine::Init(argc, argv);
auto* xdisplay = scoped_display.display();
// Connects to a X server specified in the current process' env value DISPLAY.
x11::Connection connection;
auto root_window = DefaultRootWindow(xdisplay);
if (!root_window) {
fprintf(stderr, "Couldn't find root window.\n");
// No display found - fail early.
if (!connection.Ready()) {
fprintf(stderr, "Couldn't connect to the X11 server.\n");
return 1;
}
auto dummmy_window = XCreateSimpleWindow(
xdisplay, root_window, 0 /*x*/, 0 /*y*/, 1 /*width*/, 1 /*height*/,
0 /*border width*/, 0 /*border*/, 0 /*background*/);
if (!dummmy_window) {
auto dummy_window = connection.GenerateId<x11::Window>();
auto req = connection.CreateWindow({
.wid = dummy_window,
.parent = connection.default_root(),
.width = 1,
.height = 1,
// We are only interested in the ReparentNotify events that are sent
// whenever our dummy window is reparented because of a wm start.
.event_mask = x11::EventMask::StructureNotify,
});
if (req.Sync().error) {
fprintf(stderr, "Couldn't create a dummy window.");
return 1;
}
XMapWindow(xdisplay, dummmy_window);
// We are only interested in the ReparentNotify events that are sent whenever
// our dummy window is reparented because of a wm start.
XSelectInput(xdisplay, dummmy_window, StructureNotifyMask);
XFlush(xdisplay);
connection.MapWindow({dummy_window});
connection.Flush();
int display_fd = ConnectionNumber(xdisplay);
int display_fd = ConnectionNumber(connection.display());
// Set deadline as 30s.
struct timespec now, deadline;
......@@ -82,7 +69,6 @@ int main(int argc, char* argv[]) {
struct timeval tv;
CalculateTimeout(now, deadline, &tv);
XEvent ev;
do {
fd_set in_fds;
FD_ZERO(&in_fds);
......@@ -95,14 +81,14 @@ int main(int argc, char* argv[]) {
break;
}
} else if (ret > 0) {
while (XPending(xdisplay)) {
XNextEvent(xdisplay, &ev);
connection.ReadResponses();
for (const auto& event : connection.events()) {
// If we got ReparentNotify, a wm has started up and we can stop
// execution.
if (ev.type == ReparentNotify) {
if (event.As<x11::ReparentNotifyEvent>())
return 0;
}
}
connection.events().clear();
}
// Calculate next timeout. If it's less or equal to 0, give up.
clock_gettime(CLOCK_REALTIME, &now);
......@@ -114,7 +100,7 @@ int main(int argc, char* argv[]) {
#if defined(LEAK_SANITIZER)
// XOpenDisplay leaks memory if it takes more than one try to connect. This
// causes LSan bots to fail. We don't care about memory leaks in xdisplaycheck
// causes LSan bots to fail. We don't care about memory leaks in xwmstartupcheck
// anyway, so just disable LSan completely.
// This function isn't referenced from the executable itself. Make sure it isn't
// stripped by the linker.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment