xqueryptr.c: Add new program to interrogate the pointer position.
[xtoys] / xqueryptr.c
1 /* -*-c-*-
2 *
3 * Query the current X pointer state
4 *
5 * (c) 2023 Straylight/Edgeware
6 */
7
8 /*----- Licensing notice --------------------------------------------------*
9 *
10 * This file is part of the Edgeware X tools collection.
11 *
12 * X tools is free software: you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by the
14 * Free Software Foundation; either version 2 of the License, or (at your
15 * option) any later version.
16 *
17 * X tools is distributed in the hope that it will be useful, but WITHOUT
18 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
19 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
20 * for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with X tools. If not, write to the Free Software Foundation,
24 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
25 */
26
27 /*----- Header files ------------------------------------------------------*/
28
29 #include <errno.h>
30 #include <limits.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33
34 #include <X11/Xlib.h>
35
36 #include <mLib/alloc.h>
37 #include <mLib/macros.h>
38 #include <mLib/mdwopt.h>
39 #include <mLib/quis.h>
40 #include <mLib/report.h>
41
42 /*----- Help and version information --------------------------------------*/
43
44 static void version(void)
45 { pquis(stdout, "$ version " VERSION "\n"); }
46
47 static void usage(FILE *fp)
48 { pquis(fp, "Usage: $ [-d DISPLAY] X Y\n"); }
49
50 static void help(void)
51 {
52 version(); putchar('\n');
53 usage(stdout);
54 fputs("\n\
55 Report the current pointer state, as SCREEN X Y MASK.\n\
56 \n\
57 Command-line options:\n\
58 \n\
59 -h, --help Display this help.\n\
60 -v, --version Display program's version number.\n\
61 -u, --usage Display short usage summary.\n\
62 \n\
63 -d, --display=DISPLAY Connect to X DISPLAY.\n",
64 stdout);
65 }
66
67 /*----- Main program ------------------------------------------------------*/
68
69 int main(int argc, char *argv[])
70 {
71 const char *display = 0;
72 Display *dpy = 0;
73 Window root, child;
74 int i, screen, x, y, winx, winy;
75 unsigned mask;
76
77 unsigned f = 0;
78 #define f_bogus 1u
79
80 ego(argv[0]);
81
82 /* Parse arguments. */
83 for (;;) {
84 static struct option opt[] = {
85 { "help", 0, 0, 'h' },
86 { "usage", 0, 0, 'u' },
87 { "version", 0, 0, 'v' },
88 { "display", OPTF_ARGREQ, 0, 'd' },
89 { 0, 0, 0, 0 }
90 };
91
92 i = mdwopt(argc, argv, "huvd:", opt, 0, 0, 0); if (i < 0) break;
93 switch (i) {
94 case 'h': help(); exit(0);
95 case 'u': usage(stdout); exit(0);
96 case 'v': version(); exit(0);
97 case 'd': display = optarg; break;
98 default: f |= f_bogus; break;
99 }
100 }
101 if ((f&f_bogus) || argc - optind != 0) {
102 usage(stderr);
103 exit(EXIT_FAILURE);
104 }
105
106 /* Open the display. */
107 if ((dpy = XOpenDisplay(display)) == 0)
108 die(EXIT_FAILURE, "couldn't open display");
109
110 /* Find and print where the pointer is. */
111 XQueryPointer(dpy, DefaultRootWindow(dpy), &root, &child,
112 &x, &y, &winx, &winy, &mask);
113 for (i = 0; i < ScreenCount(dpy); i++)
114 if (root == RootWindow(dpy, i)) { screen = i; goto found_root; }
115 die(2, "pointer is on unknown root window 0x%08lx", (unsigned long)root);
116 found_root:
117 printf("%d %d %d 0x%08x\n", screen, x, y, mask);
118
119 /* Done. */
120 XCloseDisplay(dpy);
121 return (0);
122 }
123
124 /*----- That's all, folks -------------------------------------------------*/