Skip to content

Instantly share code, notes, and snippets.

@gnif
Last active January 1, 2020 20:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gnif/7b63fc6f390e40a391f580236ce60693 to your computer and use it in GitHub Desktop.
Save gnif/7b63fc6f390e40a391f580236ce60693 to your computer and use it in GitHub Desktop.
Example X11 application that uses XGetFrame to capture an application window.
/*
Example X11 application that uses XGetFrame to capture an application window.
Copyright 2020 Geoffrey McRae <geoff@hostfission.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#include <stdbool.h>
#include <stdio.h>
#include <unistd.h>
#include <X11/X.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/cursorfont.h>
unsigned char x11_error;
bool select_window(Display *display, Window * window)
{
bool ret = false;
Cursor cursor = XCreateFontCursor(display, XC_crosshair);
int root = DefaultRootWindow(display);
XGrabPointer(
display,
root,
0,
ButtonMotionMask | ButtonPressMask | ButtonReleaseMask,
GrabModeAsync,
GrabModeAsync,
root,
cursor,
CurrentTime
);
XEvent e;
for(;;)
{
XNextEvent(display, &e);
if (e.type == ButtonPress)
{
*window = e.xbutton.subwindow;
ret = true;
break;
}
}
XUngrabPointer(display, CurrentTime);
XFreeCursor(display, cursor);
return ret;
};
int X11ErrorHandler(Display* display, XErrorEvent* error)
{
x11_error = error->error_code;
return 0;
}
int main(int argc, char * argv[])
{
Display *d;
int s;
Window w;
XEvent e;
XImage *image;
unsigned int frameCount = 0;
d = XOpenDisplay(NULL);
XSetErrorHandler(X11ErrorHandler);
s = DefaultScreen(d);
fprintf(stdout, "Please select a window...\n");
if (!select_window(d, &w))
fprintf(stderr, "Failed to select a window\n");
XWindowAttributes attributes;
XGetWindowAttributes(d, w, &attributes);
XSelectInput(d, w, StructureNotifyMask);
for(;;)
{
while(XPending(d))
{
XNextEvent(d, &e);
if (e.type == ConfigureNotify)
{
attributes.width = e.xconfigure.width;
attributes.height = e.xconfigure.height;
}
}
for(;;)
{
image = XGetImage(
d,
w,
0, 0,
attributes.width,
attributes.height,
AllPlanes,
ZPixmap
);
if (image)
break;
// if we failed with an invalid attribute error update the image size and retry
if (x11_error == 8)
{
XGetWindowAttributes(d, w, &attributes);
continue;
}
fprintf(stderr, "X11 Error %u\n", x11_error);
break;
}
if (!image)
break;
fprintf(stdout, "Got frame %u\n", ++frameCount);
XDestroyImage(image);
}
XCloseDisplay(d);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment