41 lines
1.4 KiB
C
41 lines
1.4 KiB
C
// Copyright (c) 2026 Storm Dragon
|
|
// Authors: Storm Dragon, Angus Kola
|
|
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
#include "commands_native.h"
|
|
#include <sys/types.h>
|
|
#include <X11/extensions/XRes.h>
|
|
|
|
int cthulhu_xres_client_pids_supported(Display *display) {
|
|
int event, error, major = 0, minor = 0;
|
|
cthulhu_x11_begin_read(display);
|
|
int supported = XResQueryExtension(display, &event, &error) &&
|
|
XResQueryVersion(display, &major, &minor) &&
|
|
(major > 1 || (major == 1 && minor >= 2));
|
|
if (cthulhu_x11_end_read(display)) return 0;
|
|
return supported;
|
|
}
|
|
|
|
// _NET_WM_PID is written by the application in its own PID namespace. XRes
|
|
// identifies the local connection from the server side instead. Unsupported
|
|
// servers, remote clients and disappeared clients retain the property fallback.
|
|
int cthulhu_xres_client_pid(Display *display, Window window) {
|
|
XResClientIdSpec spec = {.client = window, .mask = XRES_CLIENT_ID_PID_MASK};
|
|
XResClientIdValue *ids = NULL;
|
|
long count = 0;
|
|
int pid = 0;
|
|
cthulhu_x11_begin_read(display);
|
|
if (XResQueryClientIds(display, 1, &spec, &count, &ids) == Success) {
|
|
for (long i = 0; i < count; i++) {
|
|
pid_t candidate = XResGetClientPid(&ids[i]);
|
|
if (candidate > 0) {
|
|
pid = candidate;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
if (ids) XResClientIdsDestroy(count, ids);
|
|
if (cthulhu_x11_end_read(display)) return 0;
|
|
return pid;
|
|
}
|