-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathstint.c
96 lines (81 loc) · 2.57 KB
/
stint.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/*
* stint - simple, suckless-style color grabber for X11
*
* Copyright (C) 2012 Devin J. Pohly
* This program 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 3 of the License, or
* (at your option) any later version.
*
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <signal.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/cursorfont.h>
int
main(int argc, char *argv[])
{
int rv = 0;
// Get display, root window, and crosshair cursor
Display *dpy = XOpenDisplay(NULL);
if (!dpy) {
fprintf(stderr, "could not open display\n");
return 1;
}
Window root = DefaultRootWindow(dpy);
Cursor cross = XCreateFontCursor(dpy, XC_crosshair);
// Grab pointer clicking and motion events
if (XGrabPointer(dpy, root, False, ButtonPressMask | Button1MotionMask | ButtonReleaseMask,
GrabModeAsync, GrabModeAsync, root, cross, CurrentTime)) {
fprintf(stderr, "could not grab pointer\n");
rv = 1;
goto out_free;
}
// Wait for button press
XEvent ev;
do {
XNextEvent(dpy, &ev);
} while (ev.type != ButtonPress);
// Cancel if a non-Button1 button was pressed
if (ev.xbutton.button != 1) {
// Wait for release...
int b = ev.xbutton.button;
do {
XNextEvent(dpy, &ev);
} while (ev.type != ButtonRelease || ev.xbutton.button != b);
// ... and cancel
rv = 2;
goto out_ungrab;
}
// Don't die immediately if we get a SIGPIPE
struct sigaction sa_ign = { .sa_handler = SIG_IGN };
sigaction(SIGPIPE, &sa_ign, NULL);
// Print colors until Button1 is released
while (ev.type != ButtonRelease || ev.xbutton.button != 1) {
XColor c;
// Grab a 1x1 screenshot located at (x, y) and find the color
c.pixel = XGetPixel(XGetImage(dpy, root, ev.xbutton.x_root, ev.xbutton.y_root,
1, 1, AllPlanes, ZPixmap), 0, 0);
XQueryColor(dpy, DefaultColormap(dpy, DefaultScreen(dpy)), &c);
// What color is it?
printf("%d %d %d\n", c.red >> 8, c.green >> 8, c.blue >> 8);
fflush(stdout);
XNextEvent(dpy, &ev);
}
out_ungrab:
// Release the pointer grab
XUngrabPointer(dpy, CurrentTime);
out_free:
// Clean up
XFreeCursor(dpy, cross);
XCloseDisplay(dpy);
return rv;
}