xqueryptr.c: Add new program to interrogate the pointer position.
[xtoys] / xqueryptr.c
diff --git a/xqueryptr.c b/xqueryptr.c
new file mode 100644 (file)
index 0000000..4d165ee
--- /dev/null
@@ -0,0 +1,124 @@
+/* -*-c-*-
+ *
+ * Query the current X pointer state
+ *
+ * (c) 2023 Straylight/Edgeware
+ */
+
+/*----- Licensing notice --------------------------------------------------*
+ *
+ * This file is part of the Edgeware X tools collection.
+ *
+ * X tools is free software: you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by the
+ * Free Software Foundation; either version 2 of the License, or (at your
+ * option) any later version.
+ *
+ * X tools is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with X tools.  If not, write to the Free Software Foundation,
+ * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ */
+
+/*----- Header files ------------------------------------------------------*/
+
+#include <errno.h>
+#include <limits.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+#include <X11/Xlib.h>
+
+#include <mLib/alloc.h>
+#include <mLib/macros.h>
+#include <mLib/mdwopt.h>
+#include <mLib/quis.h>
+#include <mLib/report.h>
+
+/*----- Help and version information --------------------------------------*/
+
+static void version(void)
+  { pquis(stdout, "$ version " VERSION "\n"); }
+
+static void usage(FILE *fp)
+  { pquis(fp, "Usage: $ [-d DISPLAY] X Y\n"); }
+
+static void help(void)
+{
+  version(); putchar('\n');
+  usage(stdout);
+  fputs("\n\
+Report the current pointer state, as SCREEN X Y MASK.\n\
+\n\
+Command-line options:\n\
+\n\
+-h, --help                     Display this help.\n\
+-v, --version                  Display program's version number.\n\
+-u, --usage                    Display short usage summary.\n\
+\n\
+-d, --display=DISPLAY          Connect to X DISPLAY.\n",
+       stdout);
+}
+
+/*----- Main program ------------------------------------------------------*/
+
+int main(int argc, char *argv[])
+{
+  const char *display = 0;
+  Display *dpy = 0;
+  Window root, child;
+  int i, screen, x, y, winx, winy;
+  unsigned mask;
+
+  unsigned f = 0;
+#define f_bogus 1u
+
+  ego(argv[0]);
+
+  /* Parse arguments. */
+  for (;;) {
+    static struct option opt[] = {
+      { "help",                0,                      0,      'h' },
+      { "usage",       0,                      0,      'u' },
+      { "version",     0,                      0,      'v' },
+      { "display",     OPTF_ARGREQ,            0,      'd' },
+      {        0,              0,                      0,      0 }
+    };
+
+    i = mdwopt(argc, argv, "huvd:", opt, 0, 0, 0); if (i < 0) break;
+    switch (i) {
+      case 'h': help(); exit(0);
+      case 'u': usage(stdout); exit(0);
+      case 'v': version(); exit(0);
+      case 'd': display = optarg; break;
+      default: f |= f_bogus; break;
+    }
+  }
+  if ((f&f_bogus) || argc - optind != 0) {
+    usage(stderr);
+    exit(EXIT_FAILURE);
+  }
+
+  /* Open the display. */
+  if ((dpy = XOpenDisplay(display)) == 0)
+    die(EXIT_FAILURE, "couldn't open display");
+
+  /* Find and print where the pointer is. */
+  XQueryPointer(dpy, DefaultRootWindow(dpy), &root, &child,
+               &x, &y, &winx, &winy, &mask);
+  for (i = 0; i < ScreenCount(dpy); i++)
+    if (root == RootWindow(dpy, i)) { screen = i; goto found_root; }
+  die(2, "pointer is on unknown root window 0x%08lx", (unsigned long)root);
+found_root:
+  printf("%d %d %d 0x%08x\n", screen, x, y, mask);
+
+  /* Done. */
+  XCloseDisplay(dpy);
+  return (0);
+}
+
+/*----- That's all, folks -------------------------------------------------*/