8 my_handler(Display *display, XErrorEvent *error) {
11 XGetErrorText(display, error->error_code, buffer, sizeof(buffer));
12 i_push_error(error->error_code, buffer);
18 imss_x11(unsigned long display_ul, int window_id,
19 int left, int top, int right, int bottom, int direct) {
20 Display *display = (Display *)display_ul;
21 int own_display = 0; /* non-zero if we connect */
23 XWindowAttributes attr;
28 XErrorHandler old_handler;
35 /* we don't want the default noisy error handling */
36 old_handler = XSetErrorHandler(my_handler);
39 display = XOpenDisplay(NULL);
42 i_push_error(0, "No display supplied and cannot connect");
47 screen = DefaultScreen(display);
48 root_id = RootWindow(display, screen);
53 if (!XGetWindowAttributes(display, window_id, &attr)) {
54 i_push_error(0, "Cannot XGetWindowAttributes");
58 /* adjust negative/zero values to window size */
66 bottom += attr.height;
68 mm_log((3, "window @(%d,%d) %dx%d\n", attr.x, attr.y, attr.width, attr.height));
73 if (right > attr.width)
77 if (bottom > attr.height)
81 if (right <= left || bottom <= top) {
82 i_push_error(0, "image would be empty");
86 height = bottom - top;
88 /* try to get the pixels directly, this returns black images in
89 some ill-determined cases, I suspect compositing.
91 image = XGetImage(display, window_id, left, top, width, height,
95 int rootx = left, rooty = top;
96 Window child_id; /* ignored */
98 if (root_id != window_id) {
99 XWindowAttributes root_attr;
101 if (!XTranslateCoordinates(display, window_id, root_id, left, top,
102 &rootx, &rooty, &child_id)) {
103 i_push_error(0, "could not translate co-ordinates");
107 if (!XGetWindowAttributes(display, root_id, &root_attr)) {
108 i_push_error(0, "Cannot XGetWindowAttributes for root");
112 /* clip the window to the root, in case it's partly off the edge
119 if (rootx + width > root_attr.width) {
120 width = root_attr.width - rootx;
126 if (rooty + height > root_attr.height) {
127 height = root_attr.height - rooty;
130 if (width == 0 || height == 0) {
131 i_push_error(0, "window is completely clipped by the root window");
135 image = XGetImage(display, root_id, rootx, rooty,
136 width, height, -1, ZPixmap);
139 i_push_error(0, "Cannot XGetImage");
143 result = i_img_8_new(width, height, 3);
144 line = mymalloc(sizeof(i_color) * width);
145 colors = mymalloc(sizeof(XColor) * width);
146 for (y = 0; y < height; ++y) {
148 /* XQueryColors seems to be a round-trip, so do one big request
149 instead of one per pixel */
150 for (x = 0; x < width; ++x) {
151 colors[x].pixel = XGetPixel(image, x, y);
153 XQueryColors(display, attr.colormap, colors, width);
154 for (x = 0; x < width; ++x) {
155 cp->rgb.r = colors[x].red >> 8;
156 cp->rgb.g = colors[x].green >> 8;
157 cp->rgb.b = colors[x].blue >> 8;
160 i_plin(result, 0, width, y, line);
164 XDestroyImage(image);
166 XSetErrorHandler(old_handler);
168 XCloseDisplay(display);
170 i_tags_setn(&result->tags, "ss_window_width", attr.width);
171 i_tags_setn(&result->tags, "ss_window_height", attr.height);
172 i_tags_set(&result->tags, "ss_type", "X11", 3);
173 i_tags_setn(&result->tags, "ss_left", left);
174 i_tags_setn(&result->tags, "ss_top", top);
180 XDestroyImage(image);
182 XSetErrorHandler(old_handler);
184 XCloseDisplay(display);
189 imss_x11_open(char const *display_name) {
190 XErrorHandler old_handler;
194 old_handler = XSetErrorHandler(my_handler);
195 display = XOpenDisplay(display_name);
197 i_push_errorf(0, "Cannot connect to X server %s", XDisplayName(display_name));
199 XSetErrorHandler(old_handler);
201 return (unsigned long)display;
205 imss_x11_close(unsigned long display) {
206 XCloseDisplay((Display *)display);