17#include <QFutureWatcher>
18#include <QtConcurrentRun>
21#include <sys/socket.h>
28static QString getHostName()
31 char hostnamebuf[HOST_NAME_MAX];
33 char hostnamebuf[256];
35 if (gethostname(hostnamebuf,
sizeof hostnamebuf) >= 0) {
36 hostnamebuf[
sizeof(hostnamebuf) - 1] = 0;
37 return QString::fromLocal8Bit(hostnamebuf);
46 , m_ownResolved(false)
47 , m_hostName(hostName)
48 , m_addressHints(std::make_unique<addrinfo>())
50 , m_ownAddress(nullptr)
64 if (m_watcher && m_watcher->isRunning()) {
66 m_watcher->waitForFinished();
68 if (m_ownAddressWatcher && m_ownAddressWatcher->isRunning()) {
69 m_ownAddressWatcher->cancel();
70 m_ownAddressWatcher->waitForFinished();
73 freeaddrinfo(m_address);
76 freeaddrinfo(m_ownAddress);
87 m_addressHints->ai_family = PF_UNSPEC;
88 m_addressHints->ai_socktype = SOCK_STREAM;
89 m_addressHints->ai_flags |= AI_CANONNAME;
91 m_watcher->setFuture(QtConcurrent::run([
this]() {
92 return getaddrinfo(m_hostName.toLocal8Bit().constData(),
nullptr, m_addressHints.get(), &m_address);
94 m_ownAddressWatcher->setFuture(QtConcurrent::run([
this] {
97 return getaddrinfo(getHostName().toLocal8Bit().constData(),
nullptr, m_addressHints.get(), &m_ownAddress);
101void GetAddrInfo::slotResolved()
103 if (resolved(m_watcher.get())) {
109void GetAddrInfo::slotOwnAddressResolved()
111 if (resolved(m_ownAddressWatcher.get())) {
112 m_ownResolved =
true;
119 if (!watcher->isFinished()) {
122 if (watcher->result() != 0) {
123 qCDebug(KWIN_CORE) <<
"getaddrinfo failed with error:" << gai_strerror(watcher->result());
131void GetAddrInfo::compare()
133 if (!m_resolved || !m_ownResolved) {
136 addrinfo *address = m_address;
138 if (address->ai_canonname && m_hostName == QByteArray(address->ai_canonname).toLower()) {
139 addrinfo *ownAddress = m_ownAddress;
140 bool localFound =
false;
142 if (ownAddress->ai_canonname && QByteArray(ownAddress->ai_canonname).toLower() == m_hostName) {
146 ownAddress = ownAddress->ai_next;
153 address = address->ai_next;
175 QString name = NETWinInfo(
connection(), window,
rootWindow(), NET::Properties(), NET::WM2ClientMachine).clientMachine();
176 if (name.isEmpty() && clientLeader && clientLeader != window) {
177 name = NETWinInfo(
connection(), clientLeader,
rootWindow(), NET::Properties(), NET::WM2ClientMachine).clientMachine();
179 if (name.isEmpty()) {
190void ClientMachine::checkForLocalhost()
196 QString host = getHostName();
198 if (!host.isEmpty()) {
199 host = host.toLower();
200 const QString lowerHostName(m_hostName.toLower());
201 if (host == lowerHostName) {
205 if (
int index = host.indexOf(
'.'); index != -1) {
206 if (QStringView(host).left(index) == lowerHostName) {
214 GetAddrInfo *info =
new GetAddrInfo(lowerHostName,
this);
216 connect(info, &GetAddrInfo::destroyed,
this, &ClientMachine::resolveFinished);
222void ClientMachine::setLocal()
228void ClientMachine::resolveFinished()
235#include "moc_client_machine.cpp"
~ClientMachine() override
void resolve(xcb_window_t window, xcb_window_t clientLeader)
ClientMachine(QObject *parent=nullptr)
static QString localhost()
GetAddrInfo(const QString &hostName, QObject *parent=nullptr)
KWIN_EXPORT xcb_window_t rootWindow()
KWIN_EXPORT xcb_connection_t * connection()