2014-11-21 21:24:33 +01:00
|
|
|
// STL includes
|
|
|
|
#include <iostream>
|
|
|
|
#include <cstdint>
|
|
|
|
|
|
|
|
// X11 includes
|
|
|
|
#include <X11/Xutil.h>
|
|
|
|
|
|
|
|
// X11Grabber includes
|
|
|
|
#include <grabber/X11Grabber.h>
|
|
|
|
|
2014-12-14 14:26:59 +01:00
|
|
|
X11Grabber::X11Grabber(int cropLeft, int cropRight, int cropTop, int cropBottom, int horizontalPixelDecimation, int verticalPixelDecimation) :
|
|
|
|
_imageResampler(),
|
|
|
|
_cropLeft(cropLeft),
|
|
|
|
_cropRight(cropRight),
|
|
|
|
_cropTop(cropTop),
|
|
|
|
_cropBottom(cropBottom),
|
2014-11-21 21:24:33 +01:00
|
|
|
_x11Display(nullptr),
|
|
|
|
_screenWidth(0),
|
|
|
|
_screenHeight(0),
|
2016-01-21 16:40:46 +01:00
|
|
|
_croppedWidth(0),
|
|
|
|
_croppedHeight(0),
|
2014-11-21 21:24:33 +01:00
|
|
|
_image(0,0)
|
|
|
|
{
|
2014-12-14 14:26:59 +01:00
|
|
|
_imageResampler.setHorizontalPixelDecimation(horizontalPixelDecimation);
|
|
|
|
_imageResampler.setVerticalPixelDecimation(verticalPixelDecimation);
|
2016-01-21 16:40:46 +01:00
|
|
|
_imageResampler.setCropping(0, 0, 0, 0); // cropping is performed by XShmGetImage
|
2014-11-21 21:24:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
X11Grabber::~X11Grabber()
|
|
|
|
{
|
|
|
|
if (_x11Display != nullptr)
|
|
|
|
{
|
2016-01-21 16:40:46 +01:00
|
|
|
freeResources();
|
2014-11-21 21:24:33 +01:00
|
|
|
XCloseDisplay(_x11Display);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-21 16:40:46 +01:00
|
|
|
void X11Grabber::freeResources()
|
2014-11-21 21:24:33 +01:00
|
|
|
{
|
2016-01-21 16:40:46 +01:00
|
|
|
// Cleanup allocated resources of the X11 grab
|
|
|
|
XShmDetach(_x11Display, &_shminfo);
|
|
|
|
XDestroyImage(_xImage);
|
|
|
|
shmdt(_shminfo.shmaddr);
|
|
|
|
shmctl(_shminfo.shmid, IPC_RMID, 0);
|
|
|
|
}
|
2014-11-21 21:24:33 +01:00
|
|
|
|
2016-01-21 16:40:46 +01:00
|
|
|
void X11Grabber::setupResources()
|
|
|
|
{
|
|
|
|
_xImage = XShmCreateImage(_x11Display, _windowAttr.visual,
|
|
|
|
_windowAttr.depth, ZPixmap, NULL, &_shminfo,
|
|
|
|
_croppedWidth, _croppedHeight);
|
|
|
|
|
|
|
|
_shminfo.shmid = shmget(IPC_PRIVATE, _xImage->bytes_per_line * _xImage->height, IPC_CREAT|0777);
|
|
|
|
_shminfo.shmaddr = _xImage->data = (char*)shmat(_shminfo.shmid,0,0);
|
|
|
|
_shminfo.readOnly = False;
|
|
|
|
|
|
|
|
XShmAttach(_x11Display, &_shminfo);
|
2014-11-21 21:24:33 +01:00
|
|
|
}
|
|
|
|
|
2016-01-21 16:40:46 +01:00
|
|
|
bool X11Grabber::Setup()
|
2014-11-21 21:24:33 +01:00
|
|
|
{
|
2016-01-21 16:40:46 +01:00
|
|
|
_x11Display = XOpenDisplay(NULL);
|
2014-11-21 21:24:33 +01:00
|
|
|
if (_x11Display == nullptr)
|
|
|
|
{
|
2016-03-23 17:40:34 +01:00
|
|
|
std::cerr << "X11GRABBER ERROR: Unable to open display";
|
2016-01-21 16:40:46 +01:00
|
|
|
if (getenv("DISPLAY"))
|
|
|
|
std::cerr << " " << std::string(getenv("DISPLAY")) << std::endl;
|
|
|
|
else
|
|
|
|
std::cerr << ". DISPLAY environment variable not set" << std::endl;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
_window = DefaultRootWindow(_x11Display);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2014-11-21 21:24:33 +01:00
|
|
|
|
2016-01-21 16:40:46 +01:00
|
|
|
Image<ColorRgb> & X11Grabber::grab()
|
|
|
|
{
|
2016-01-21 16:51:20 +01:00
|
|
|
updateScreenDimensions();
|
|
|
|
|
2016-01-21 16:40:46 +01:00
|
|
|
XShmGetImage(_x11Display, _window, _xImage, _cropLeft, _cropTop, 0x00FFFFFF);
|
|
|
|
if (_xImage == nullptr)
|
2014-11-21 21:24:33 +01:00
|
|
|
{
|
2016-03-23 17:40:34 +01:00
|
|
|
std::cerr << "X11GRABBER ERROR: Grab failed" << std::endl;
|
2014-11-21 21:24:33 +01:00
|
|
|
return _image;
|
|
|
|
}
|
|
|
|
|
2016-01-21 16:40:46 +01:00
|
|
|
_imageResampler.processImage(reinterpret_cast<const uint8_t *>(_xImage->data), _xImage->width, _xImage->height, _xImage->bytes_per_line, PIXELFORMAT_BGR32, _image);
|
2014-11-21 21:24:33 +01:00
|
|
|
|
|
|
|
return _image;
|
|
|
|
}
|
|
|
|
|
|
|
|
int X11Grabber::updateScreenDimensions()
|
|
|
|
{
|
2016-01-21 16:40:46 +01:00
|
|
|
const Status status = XGetWindowAttributes(_x11Display, _window, &_windowAttr);
|
2014-11-21 21:24:33 +01:00
|
|
|
if (status == 0)
|
|
|
|
{
|
2016-03-23 17:40:34 +01:00
|
|
|
std::cerr << "X11GRABBER ERROR: Failed to obtain window attributes" << std::endl;
|
2014-11-21 21:24:33 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2016-01-21 16:40:46 +01:00
|
|
|
if (_screenWidth == unsigned(_windowAttr.width) && _screenHeight == unsigned(_windowAttr.height))
|
2014-11-21 21:24:33 +01:00
|
|
|
{
|
|
|
|
// No update required
|
|
|
|
return 0;
|
|
|
|
}
|
2016-01-21 16:40:46 +01:00
|
|
|
|
2016-03-23 17:40:34 +01:00
|
|
|
std::cout << "X11GRABBER INFO: Update of screen resolution: [" << _screenWidth << "x" << _screenHeight <<"] => ";
|
2016-01-21 16:40:46 +01:00
|
|
|
|
|
|
|
if (_screenWidth || _screenHeight)
|
|
|
|
freeResources();
|
|
|
|
|
|
|
|
_screenWidth = _windowAttr.width;
|
|
|
|
_screenHeight = _windowAttr.height;
|
|
|
|
|
2014-11-21 21:24:33 +01:00
|
|
|
std::cout << "[" << _screenWidth << "x" << _screenHeight <<"]" << std::endl;
|
2016-01-21 16:40:46 +01:00
|
|
|
|
|
|
|
if (_screenWidth > unsigned(_cropLeft + _cropRight))
|
|
|
|
_croppedWidth = _screenWidth - _cropLeft - _cropRight;
|
|
|
|
else
|
|
|
|
_croppedWidth = _screenWidth;
|
|
|
|
|
|
|
|
if (_screenHeight > unsigned(_cropTop + _cropBottom))
|
|
|
|
_croppedHeight = _screenHeight - _cropTop - _cropBottom;
|
|
|
|
else
|
|
|
|
_croppedHeight = _screenHeight;
|
|
|
|
|
|
|
|
setupResources();
|
2014-11-21 21:24:33 +01:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|