/
scx11.c
154 lines (133 loc) · 3.7 KB
/
scx11.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
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
#include "imext.h"
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include "imss.h"
static
int
my_handler(Display *display, XErrorEvent *error) {
char buffer[500];
XGetErrorText(display, error->error_code, buffer, sizeof(buffer));
i_push_error(error->error_code, buffer);
return 0;
}
i_img *
imss_x11(unsigned long display_ul, int window_id,
int left, int top, int right, int bottom) {
Display *display = (Display *)display_ul;
int own_display = 0; /* non-zero if we connect */
XImage *image;
XWindowAttributes attr;
i_img *result;
i_color *line, *cp;
int x, y;
XColor *colors;
XErrorHandler old_handler;
int width, height;
i_clear_error();
/* we don't want the default noisy error handling */
old_handler = XSetErrorHandler(my_handler);
if (!display) {
display = XOpenDisplay(NULL);
++own_display;
if (!display) {
XSetErrorHandler(old_handler);
i_push_error(0, "No display supplied and cannot connect");
return NULL;
}
}
if (!window_id) {
int screen = DefaultScreen(display);
window_id = RootWindow(display, screen);
}
if (!XGetWindowAttributes(display, window_id, &attr)) {
XSetErrorHandler(old_handler);
if (own_display)
XCloseDisplay(display);
i_push_error(0, "Cannot XGetWindowAttributes");
return NULL;
}
/* adjust negative/zero values to window size */
if (left < 0)
left += attr.width;
if (top < 0)
top += attr.height;
if (right <= 0)
right += attr.width;
if (bottom <= 0)
bottom += attr.height;
/* clamp */
if (left < 0)
left = 0;
if (right > attr.width)
right = attr.width;
if (top < 0)
top = 0;
if (bottom > attr.height)
bottom = attr.height;
/* validate */
if (right <= left || bottom <= top) {
XSetErrorHandler(old_handler);
if (own_display)
XCloseDisplay(display);
i_push_error(0, "image would be empty");
return NULL;
}
width = right - left;
height = bottom - top;
image = XGetImage(display, window_id, left, top, width, height,
-1, ZPixmap);
if (!image) {
XSetErrorHandler(old_handler);
if (own_display)
XCloseDisplay(display);
i_push_error(0, "Cannot XGetImage");
return NULL;
}
result = i_img_8_new(width, height, 3);
line = mymalloc(sizeof(i_color) * width);
colors = mymalloc(sizeof(XColor) * width);
for (y = 0; y < height; ++y) {
cp = line;
/* XQueryColors seems to be a round-trip, so do one big request
instead of one per pixel */
for (x = 0; x < width; ++x) {
colors[x].pixel = XGetPixel(image, x, y);
}
XQueryColors(display, attr.colormap, colors, width);
for (x = 0; x < width; ++x) {
cp->rgb.r = colors[x].red >> 8;
cp->rgb.g = colors[x].green >> 8;
cp->rgb.b = colors[x].blue >> 8;
++cp;
}
i_plin(result, 0, width, y, line);
}
myfree(line);
myfree(colors);
XDestroyImage(image);
XSetErrorHandler(old_handler);
if (own_display)
XCloseDisplay(display);
i_tags_setn(&result->tags, "ss_window_width", attr.width);
i_tags_setn(&result->tags, "ss_window_height", attr.height);
i_tags_set(&result->tags, "ss_type", "X11", 3);
i_tags_setn(&result->tags, "ss_left", left);
i_tags_setn(&result->tags, "ss_top", top);
return result;
}
unsigned long
imss_x11_open(char const *display_name) {
XErrorHandler old_handler;
Display *display;
i_clear_error();
old_handler = XSetErrorHandler(my_handler);
display = XOpenDisplay(display_name);
if (!display)
i_push_errorf(0, "Cannot connect to X server %s", XDisplayName(display_name));
XSetErrorHandler(old_handler);
return (unsigned long)display;
}
void
imss_x11_close(unsigned long display) {
XCloseDisplay((Display *)display);
}