[RFC][PATCH 4/5] XResource extension v1.2 implementation of XResQueryClientIds
Erkki Seppälä
erkki.seppala at vincit.fi
Fri Dec 3 02:44:54 PST 2010
From: Erkki Seppala <erkki.seppala at vincit.fi>
Reviewed-by: Rami Ylimäki <rami.ylimaki at vincit.fi>
This is a short test for the new request. It gives a short example how
the new request could be used.
---
res-clientid-test.c | 140 +++++++++++++++++++++++++++++++++++++++++++++++++++
1 files changed, 140 insertions(+), 0 deletions(-)
create mode 100644 res-clientid-test.c
diff --git a/res-clientid-test.c b/res-clientid-test.c
new file mode 100644
index 0000000..4516d6d
--- /dev/null
+++ b/res-clientid-test.c
@@ -0,0 +1,140 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <X11/Xlib.h>
+#include <X11/extensions/XRes.h>
+#include <unistd.h>
+
+int main(int argc, char **argv)
+{
+ Display *display;
+
+ if (argc != 2 || !(display = XOpenDisplay(argv[1]))) {
+ printf("Cannot open display or invalid number of arguments provided\n");
+ printf("Usage: %s :0\n", argv[0]);
+ return 1;
+ }
+
+ {
+ int major, minor;
+ if (!XResQueryVersion(display, &major, &minor)) {
+ printf("XResQueryVersion call failed\n");
+ return 1;
+ }
+
+ if (major != 1 || minor < 2) {
+ printf("Invalid X-Resource extension version %d.%d, major 1 atleast minor 2 expected\n", major, minor);
+
+ return 1;
+ }
+ }
+
+ {
+ int event, error;
+
+ if (!XResQueryExtension(display, &event, &error)) {
+ printf("X-Resource extension not present\n");
+ return 1;
+ }
+ }
+
+ {
+ pid_t our_pid = getpid();
+ XID our_xid = 0;
+ long num_ids;
+ XResClientIdValue *client_ids;
+ XResClientIdSpec client_specs[3];
+ client_specs[0].client = None;
+ client_specs[0].mask = 0;
+ Status status = XResQueryClientIds(display, 1, client_specs, &num_ids, &client_ids);
+ if (status == Success) {
+ int c;
+ for (c = 0; c < num_ids; ++c) {
+ XResClientIdValue *value = client_ids + c;
+ printf("client %0x ", (int) value->spec.client);
+ switch (XResGetClientIdType(value)) {
+ case XRES_CLIENT_ID_XID: {
+ printf("xid");
+ } break;
+ case XRES_CLIENT_ID_PID: {
+ pid_t pid = XResGetClientPid(value);
+ printf("pid=%d", (int) pid);
+ if (pid == our_pid) {
+ our_xid = value->spec.client;
+ }
+ } break;
+ default:
+ printf("unknown");
+ }
+ printf("\n");
+ }
+ XResClientIdsDestroy(num_ids, client_ids);
+
+ if (!our_xid) {
+ printf("failed to find XID of this application");
+ return 1;
+ } else {
+ printf("XID of this client was found: 0x%0x\n", (int) our_xid);
+ }
+
+ } else {
+ printf("failed to query client ids\n");
+ return 1;
+ }
+
+ client_specs[0].client = our_xid;
+ client_specs[0].mask = XRES_CLIENT_ID_PID_MASK;
+ client_specs[1].client = our_xid;
+ client_specs[1].mask = XRES_CLIENT_ID_PID_MASK;
+ status = XResQueryClientIds(display, 2, client_specs, &num_ids, &client_ids);
+ if (status != Success) {
+ printf("failed to query for ourself\n");
+ return 1;
+ }
+ if (num_ids != 1) {
+ printf("failed to return exactly one result (got: %ld)\n", num_ids);
+ return 1;
+ }
+ if (client_ids[0].spec.client != our_xid) {
+ printf("failed to return our id (not exactly against the specification, though)\n");
+ return 1;
+ }
+ if (XResGetClientPid(&client_ids[0]) != our_pid) {
+ printf("failed to return our pid\n");
+ return 1;
+ }
+
+ XResClientIdsDestroy(num_ids, client_ids);
+
+ client_specs[2].client = None;
+ client_specs[1].mask = XRES_CLIENT_ID_PID_MASK;
+
+ status = XResQueryClientIds(display, 3, client_specs, &num_ids, &client_ids);
+ if (status != Success) {
+ printf("failed to query for ourself and Any\n");
+ return 1;
+ }
+
+ {
+ int numSelf = 0; // how many times are we mentioned in the list?
+ int c;
+ for (c = 0; c < num_ids; ++c) {
+ XResClientIdValue *value = client_ids + c;
+ if (value->spec.client == our_xid) {
+ ++numSelf;
+ }
+ }
+
+ if (numSelf != 1) {
+ printf("Our xid was not exactly once in the result set, but %d times\n", numSelf);
+ return 1;
+ }
+ }
+
+ XResClientIdsDestroy(num_ids, client_ids);
+
+ printf("all tests ok\n");
+ }
+
+ XCloseDisplay(display);
+ return 0;
+}
--
1.7.0.4
More information about the xorg-devel
mailing list