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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
|
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <X11/cursorfont.h>
#include <X11/keysym.h>
#include <X11/X.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#ifdef XINERAMA
#include <X11/extensions/Xinerama.h>
#endif
#include "config.h"
#define MIN(x, y) (x < y ? x : y)
#define MAX(x, y) (x > y ? x : y)
enum mode {
MODE_SEL,
MODE_FLL,
MODE_SCR,
MODE_WIN
};
struct pointer_t {
int ret;
Bool sel;
Cursor cur;
int x0;
int y0;
int x1;
int y1;
int lx;
int ly;
};
void capfull(void);
void capscr(void);
void capsel(void);
void capwin(void);
unsigned char channel(unsigned long px, Mask m);
void die(const char* s);
void drawrect(void);
void mkppm(XImage* img);
void parseargs(int argc, char** argv);
void quit(Bool ex);
void run(void);
void setup(void);
Display* dpy = NULL;
Window root = None;
GC selgc = 0;
int scr = -1;
enum mode mode = MODE_SEL;
int selscr = -1;
struct pointer_t p = {0};
void capfull(void)
{
int w = DisplayWidth(dpy, scr);
int h = DisplayHeight(dpy, scr);
XImage* img = XGetImage(dpy, root, 0, 0, w, h, AllPlanes, ZPixmap);
if (!img)
die("XGetImage failed");
mkppm(img);
}
void capscr(void)
{
#ifdef XINERAMA
int n;
XineramaScreenInfo* si = XineramaQueryScreens(dpy, &n);
if (!si || selscr < 0 || selscr >= n) {
if (si)
XFree(si);
die("invalid screen");
}
int x = si[selscr].x_org;
int y = si[selscr].y_org;
int w = si[selscr].width;
int h = si[selscr].height;
XFree(si);
XImage* img = XGetImage(dpy, root, x, y, w, h, AllPlanes, ZPixmap);
if (!img)
die("XGetImage failed");
mkppm(img);
#else
die("xinerama support not compiled");
#endif
}
void capsel(void)
{
int rx = MIN(p.x0, p.x1);
int ry = MIN(p.y0, p.y1);
int rw = MAX(p.x0, p.x1) - rx;
int rh = MAX(p.y0, p.y1) - ry;
if (rw <= 0 || rh <= 0)
die("empty selection");
XImage* img = XGetImage(dpy, root, rx, ry, rw, rh, AllPlanes, ZPixmap);
if (!img)
die("XGetImage failed");
mkppm(img);
}
void capwin(void)
{
Window child;
Window rroot;
int rx;
int ry;
int wx;
int wy;
unsigned int rw;
unsigned int rh;
unsigned int rb;
unsigned int rd;
/* find window under pointer */
XQueryPointer(dpy, root, &rroot, &child, &rx, &ry, &wx, &wy, &rb);
if (child == None)
child = root;
XGetGeometry(dpy, child, &rroot, &rx, &ry, &rw, &rh, &rb, &rd);
XTranslateCoordinates(dpy, child, root, 0, 0, &rx, &ry, &rroot);
XImage* img = XGetImage(dpy, root, rx, ry, rw, rh, AllPlanes, ZPixmap);
if (!img)
die("XGetImage failed");
mkppm(img);
}
unsigned char channel(unsigned long px, Mask m)
{
if (m == 0)
return 0;
int shift = 0;
/* shift down until we find channel */
while ((m & 1UL) == 0) {
m >>= 1UL;
shift++;
}
/* bits in channel */
int bits = 0;
while (m & 1UL) {
m >>= 1UL;
bits++;
}
/* raw channel value */
unsigned long rv = (px >> shift) & ((1UL << bits) - 1UL);
/* scale v from [0, 2^bits-1]->[0,255] to meet standard */
if (bits == 8)
return rv;
return ((rv * 255UL) / ((1UL << bits) - 1UL));
}
void die(const char* s)
{
fprintf(stderr, "xnap: %s\n", s);
exit(EXIT_FAILURE);
}
void drawrect(void)
{
int rx = MIN(p.x0, p.x1);
int ry = MIN(p.y0, p.y1);
int rw = MAX(p.x0, p.x1) - rx;
int rh = MAX(p.y0, p.y1) - ry;
if (rw > 0 && rh > 0)
XDrawRectangle(dpy, root, selgc, rx, ry, rw, rh);
}
void mkppm(XImage* img)
{
FILE* out = stdout;
/* write ppm metadata header */
fprintf(out, "P6\n%d %d\n255\n", img->width, img->height);
for (int y = 0; y < img->height; y++) {
for (int x = 0; x < img->width; x++) {
unsigned long px = XGetPixel(img, x, y);
fputc(channel(px, img->red_mask), out);
fputc(channel(px, img->green_mask), out);
fputc(channel(px, img->blue_mask), out);
}
}
fflush(stdout);
}
void parseargs(int argc, char** argv)
{
for (int i = 1; i < argc; i++) {
if (!strcmp(argv[i], "-f"))
mode = MODE_FLL;
else if (!strcmp(argv[i], "-w"))
mode = MODE_WIN;
else if (!strcmp(argv[i], "-s") && i+1 < argc) {
mode = MODE_SCR;
selscr = atoi(argv[++i]);
}
else
die("usage: xnap [-f | -w | -s N]");
}
}
void quit(Bool ex)
{
XUngrabPointer(dpy, CurrentTime);
XFreeCursor(dpy, p.cur);
XFreeGC(dpy, selgc);
XCloseDisplay(dpy);
if (ex)
exit(EXIT_SUCCESS);
}
void run(void)
{
XEvent ev;
for (;;) {
XNextEvent(dpy, &ev);
unsigned int b = ev.xbutton.button;
if (ev.type == ButtonPress && b == Button1) {
if (mode == MODE_WIN) {
capwin();
quit(True);
}
/* start selecting for selection mode */
p.sel = True;
p.x0 = p.x1 = p.lx = ev.xbutton.x_root;
p.y0 = p.y1 = p.ly = ev.xbutton.y_root;
}
else if (ev.type == MotionNotify && p.sel) { /* dragging selection */
/* keep only latest motion ev */
while (XCheckTypedEvent(dpy, MotionNotify, &ev));
/* remove old rectangle */
drawrect();
p.x1 = ev.xmotion.x_root;
p.y1 = ev.xmotion.y_root;
/* make new rect */
drawrect();
p.lx = p.x1;
p.ly = p.y1;
}
else if (ev.type == ButtonRelease && p.sel && b == Button1) { /* release selection */
/* remove final rectangle */
drawrect();
XSync(dpy, False);
p.sel = False;
capsel();
quit(True);
}
else if (ev.type == ButtonPress && (b == Button2 || b == Button3)) { /* quit */
quit(True);
}
}
}
void setup(void)
{
/* X surface */
dpy = XOpenDisplay(NULL);
if (dpy == NULL)
die ("failed to open display");
scr = DefaultScreen(dpy);
root = RootWindow(dpy, scr);
/* pointer */
const unsigned int cur = (mode == MODE_WIN ? win_cursor : sel_cursor);
p.cur = XCreateFontCursor(dpy, cur);
p.ret = XGrabPointer(
dpy, root, False,
ButtonPressMask | ButtonReleaseMask | PointerMotionMask,
GrabModeAsync, GrabModeAsync, None, p.cur, CurrentTime
);
if (p.ret != GrabSuccess)
die("XGrabPointer failed");
/* selection rectangle */
XGCValues gcv;
gcv.function = GXxor;
gcv.foreground = WhitePixel(dpy, scr) ^ BlackPixel(dpy, scr);
gcv.line_style = LineSolid;
gcv.line_width = sel_line_width;
gcv.subwindow_mode = IncludeInferiors;
selgc = XCreateGC(dpy, root, GCFunction | GCForeground | GCLineWidth | GCLineStyle | GCSubwindowMode, &gcv);
}
int main(int argc, char** argv)
{
parseargs(argc, argv);
/* immediate capture modes */
if (mode == MODE_FLL || mode == MODE_SCR) {
dpy = XOpenDisplay(NULL);
if (dpy == NULL)
die("failed to open display");
scr = DefaultScreen(dpy);
root = RootWindow(dpy, scr);
if (mode == MODE_FLL)
capfull();
else
capscr();
XCloseDisplay(dpy);
return EXIT_SUCCESS;
}
setup();
run();
quit(True); /* unreachable */
return EXIT_SUCCESS;
}
|