xqueryptr.c: Add new program to interrogate the pointer position. master
authorMark Wooding <mdw@distorted.org.uk>
Sat, 18 Mar 2023 12:02:05 +0000 (12:02 +0000)
committerMark Wooding <mdw@distorted.org.uk>
Sat, 18 Mar 2023 12:50:47 +0000 (12:50 +0000)
Makefile.am
xqueryptr.1 [new file with mode: 0644]
xqueryptr.c [new file with mode: 0644]

index dcba780..00f9b96 100644 (file)
@@ -63,6 +63,11 @@ bin_PROGRAMS         += xwarpptr
 dist_man_MANS          += xwarpptr.1
 xwarpptr_SOURCES        = xwarpptr.c
 
+## xqueryptr.
+bin_PROGRAMS           += xqueryptr
+dist_man_MANS          += xqueryptr.1
+xqueryptr_SOURCES       = xqueryptr.c
+
 ## xrepaint.
 bin_PROGRAMS           += xrepaint
 dist_man_MANS          += xrepaint.1
diff --git a/xqueryptr.1 b/xqueryptr.1
new file mode 100644 (file)
index 0000000..b3970be
--- /dev/null
@@ -0,0 +1,38 @@
+.\" -*-nroff-*-
+.TH xqueryptr 1 "18 March 2022" "Straylight/Edgeware" "xtoys"
+.SH NAME
+xqueryptr \- query pointer state
+.SH SYNOPSIS
+.B xqueryptr
+.RB [ \-d
+.IR display ]
+.SH DESCRIPTION
+The
+.B xqueryptr
+reports the current state of the pointing device as four integers:
+.I screen
+.I x
+.I y
+.IR mask .
+.PP
+The
+.I screen
+identifies which screen (in the technical X11 sense) the pointer is on
+in decimal, and is nowadays almost always 0;
+.I x
+and
+.I y
+give the pointer position relative to the top left corner of the screen
+in decimal; and
+.I mask
+gives the button and modifier state in hexadecimal.
+.SS Options
+.TP 5
+.BI "\-d, \-\-display " display
+Choose which display to connect to.
+.SH BUGS
+Hopefully none.
+.SH SEE ALSO
+.BR XQueryPointer (3x).
+.SH AUTHOR
+Mark Wooding (mdw@distorted.org.uk).
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 -------------------------------------------------*/