Repository navigation
Expand file tree
/
Copy pathagrab.cpp
More file actions
140 lines (109 loc) · 3.34 KB
/
Copy pathagrab.cpp
File metadata and controls
140 lines (109 loc) · 3.34 KB
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
#include <X11/Xlib.h>
#include <X11/Xatom.h>
#include <X11/cursorfont.h>
#include <cairo.h>
#include <cairo-xlib.h>
#include <unistd.h>
#include <cstdio>
unsigned int width, height;
void get_screen_size(Display *display, unsigned int &w, unsigned int &h) {
Screen *screen = XDefaultScreenOfDisplay(display);
w = WidthOfScreen(screen);
h = HeightOfScreen(screen);
}
cairo_surface_t *take_screenshot(Display *display) {
cairo_surface_t *root_s = cairo_xlib_surface_create(
display, DefaultRootWindow(display),
DefaultVisual(display, DefaultScreen(display)),
width, height
);
cairo_surface_t *screenshot = cairo_image_surface_create(CAIRO_FORMAT_RGB24, width, height);
cairo_t *cscr = cairo_create(screenshot);
cairo_set_source_surface(cscr, root_s, 0, 0);
cairo_paint(cscr);
cairo_destroy(cscr);
cairo_surface_destroy(root_s);
return screenshot;
}
Window create_overlay(Display *display) {
Window window = XCreateWindow(
display, DefaultRootWindow(display), 0, 0, 100, 100,
0, CopyFromParent, CopyFromParent, CopyFromParent, 0, 0
);
Atom atoms[2] = { XInternAtom(display, "_NET_WM_STATE_FULLSCREEN", False), None };
XChangeProperty(
display, window,
XInternAtom(display, "_NET_WM_STATE", False), XA_ATOM,
32, PropModeReplace, (unsigned char *) atoms, 1
);
return window;
}
cairo_surface_t* overlay_surface(Display *dsp, Drawable &da) {
int screen;
cairo_surface_t *sfc;
screen = DefaultScreen(dsp);
sfc = cairo_xlib_surface_create(dsp, da, DefaultVisual(dsp, screen), width, height);
cairo_xlib_surface_set_size(sfc, width, height);
return sfc;
}
int _abs(int x) {
return x < 0 ? -x : x;
}
int _min(int a, int b) {
return a < b ? a : b;
}
int main() {
Display *display = XOpenDisplay(0);
if (!display) return 1;
get_screen_size(display, width, height);
cairo_surface_t *screenshot = take_screenshot(display);
Window overlay = create_overlay(display);
XDefineCursor(display, overlay, XCreateFontCursor(display, XC_crosshair));
XSelectInput(display, overlay, ButtonPressMask | ButtonReleaseMask | ButtonMotionMask);
cairo_surface_t *surface = overlay_surface(display, overlay);
cairo_t *ctx = cairo_create(surface);
cairo_set_line_width(ctx, 1);
XMapWindow(display, overlay);
unsigned int orgx, orgy;
unsigned int x, y, w = 0, h = 0;
XEvent event;
bool loop = true;
double ov_r = 0.0, ov_g = 0.7, ov_b = 1.0, ov_a = 0.3;
while (loop) {
XNextEvent(display, &event);
switch (event.type) {
case MotionNotify:
cairo_push_group(ctx);
cairo_set_source_surface(ctx, screenshot, 0.0, 0.0);
cairo_rectangle(ctx, x, y, w, h);
cairo_fill(ctx);
w = event.xmotion.x - orgx;
h = event.xmotion.y - orgy;
x = _min(orgx, orgx + w);
y = _min(orgy, orgy + h);
w = _abs(w);
h = _abs(h);
cairo_rectangle(ctx, x, y, w, h);
cairo_set_source_rgba(ctx, ov_r, ov_g, ov_b, ov_a);
cairo_fill(ctx);
cairo_rectangle(ctx, (double) x + 0.5, (double) y + 0.5, w - 1, h - 1);
cairo_set_source_rgba(ctx, ov_r, ov_g, ov_b, 1);
cairo_stroke(ctx);
cairo_pop_group_to_source(ctx);
cairo_paint(ctx);
break;
case ButtonPress:
orgx = event.xbutton.x;
orgy = event.xbutton.y;
break;
case ButtonRelease:
loop = false;
break;
}
}
printf("-x %d -y %d -w %d -h %d\n", x, y, w, h);
cairo_destroy(ctx);
cairo_surface_destroy(surface);
XCloseDisplay(display);
return 0;
}