mirror of
https://github.com/hyperion-project/hyperion.ng.git
synced 2023-10-10 13:36:59 +02:00
Implemented the X11-grabber (based on v4l code).
Former-commit-id: 17ad66ff7e2a1e3fae2d853e5360f791934b6563
This commit is contained in:
parent
2d7d18b34d
commit
8e446bf883
@ -10,19 +10,30 @@ find_package(Protobuf REQUIRED)
|
|||||||
# find Qt4
|
# find Qt4
|
||||||
find_package(Qt4 REQUIRED QtCore QtGui QtNetwork)
|
find_package(Qt4 REQUIRED QtCore QtGui QtNetwork)
|
||||||
|
|
||||||
|
# Find X11
|
||||||
|
find_package(X11 REQUIRED)
|
||||||
|
|
||||||
include_directories(
|
include_directories(
|
||||||
${CMAKE_CURRENT_BINARY_DIR}
|
${CMAKE_CURRENT_BINARY_DIR}
|
||||||
${PROTOBUF_INCLUDE_DIRS}
|
${PROTOBUF_INCLUDE_DIRS}
|
||||||
${QT_INCLUDES}
|
${QT_INCLUDES}
|
||||||
|
${X11_INCLUDES}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
set(Hyperion_X11_QT_HEADERS
|
||||||
|
ProtoWrapper.h
|
||||||
|
X11Wrapper.h)
|
||||||
|
|
||||||
set(Hyperion_X11_HEADERS
|
set(Hyperion_X11_HEADERS
|
||||||
X11Grabber.h
|
X11Grabber.h
|
||||||
../hyperion-v4l2/ProtoConnection.h
|
../hyperion-v4l2/ProtoConnection.h
|
||||||
)
|
)
|
||||||
|
|
||||||
set(Hyperion_X11_SOURCES
|
set(Hyperion_X11_SOURCES
|
||||||
|
hyperion-x11.cpp
|
||||||
|
ProtoWrapper.cpp
|
||||||
X11Grabber.cpp
|
X11Grabber.cpp
|
||||||
|
X11Wrapper.cpp
|
||||||
../hyperion-v4l2/ProtoConnection.cpp
|
../hyperion-v4l2/ProtoConnection.cpp
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -30,15 +41,16 @@ set(Hyperion_X11_PROTOS
|
|||||||
${CMAKE_CURRENT_SOURCE_DIR}/../../libsrc/protoserver/message.proto
|
${CMAKE_CURRENT_SOURCE_DIR}/../../libsrc/protoserver/message.proto
|
||||||
)
|
)
|
||||||
|
|
||||||
protobuf_generate_cpp(Hyperion_X11_PROTO_SRCS Hyperion_X11_PROTO_HDRS
|
QT4_WRAP_CPP(Hyperion_X11_HEADERS_MOC ${Hyperion_X11_QT_HEADERS})
|
||||||
${Hyperion_X11_PROTOS}
|
|
||||||
)
|
protobuf_generate_cpp(Hyperion_X11_PROTO_SRCS Hyperion_X11_PROTO_HDRS ${Hyperion_X11_PROTOS})
|
||||||
|
|
||||||
add_executable(hyperion-x11
|
add_executable(hyperion-x11
|
||||||
${Hyperion_X11_HEADERS}
|
${Hyperion_X11_HEADERS}
|
||||||
${Hyperion_X11_SOURCES}
|
${Hyperion_X11_SOURCES}
|
||||||
${Hyperion_X11_PROTO_SRCS}
|
${Hyperion_X11_PROTO_SRCS}
|
||||||
${Hyperion_X11_PROTO_HDRS}
|
${Hyperion_X11_PROTO_HDRS}
|
||||||
|
${Hyperion_X11_HEADERS_MOC}
|
||||||
)
|
)
|
||||||
|
|
||||||
target_link_libraries(hyperion-x11
|
target_link_libraries(hyperion-x11
|
||||||
@ -46,6 +58,7 @@ target_link_libraries(hyperion-x11
|
|||||||
blackborder
|
blackborder
|
||||||
hyperion-utils
|
hyperion-utils
|
||||||
${PROTOBUF_LIBRARIES}
|
${PROTOBUF_LIBRARIES}
|
||||||
|
${X11_LIBRARIES}
|
||||||
pthread
|
pthread
|
||||||
)
|
)
|
||||||
|
|
||||||
|
17
src/hyperion-x11/ProtoWrapper.cpp
Normal file
17
src/hyperion-x11/ProtoWrapper.cpp
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
|
||||||
|
//
|
||||||
|
#include "ProtoWrapper.h"
|
||||||
|
|
||||||
|
ProtoWrapper::ProtoWrapper(const std::string & protoAddress, const bool skipReply) :
|
||||||
|
_priority(200),
|
||||||
|
_duration_ms(2000),
|
||||||
|
_connection(protoAddress)
|
||||||
|
{
|
||||||
|
_connection.setSkipReply(skipReply);
|
||||||
|
}
|
||||||
|
|
||||||
|
void ProtoWrapper::process(const Image<ColorRgb> & image)
|
||||||
|
{
|
||||||
|
std::cout << "Attempt to send screenshot" << std::endl;
|
||||||
|
_connection.setImage(image, _priority, _duration_ms);
|
||||||
|
}
|
23
src/hyperion-x11/ProtoWrapper.h
Normal file
23
src/hyperion-x11/ProtoWrapper.h
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
|
||||||
|
// QT includes
|
||||||
|
#include <QObject>
|
||||||
|
|
||||||
|
|
||||||
|
#include "../hyperion-v4l2/ProtoConnection.h"
|
||||||
|
|
||||||
|
class ProtoWrapper : public QObject
|
||||||
|
{
|
||||||
|
Q_OBJECT
|
||||||
|
public:
|
||||||
|
ProtoWrapper(const std::string & protoAddress, const bool skipReply);
|
||||||
|
|
||||||
|
public slots:
|
||||||
|
void process(const Image<ColorRgb> & image);
|
||||||
|
|
||||||
|
private:
|
||||||
|
|
||||||
|
int _priority;
|
||||||
|
int _duration_ms;
|
||||||
|
|
||||||
|
ProtoConnection _connection;
|
||||||
|
};
|
@ -1,14 +1,115 @@
|
|||||||
|
// STL includes
|
||||||
|
#include <iostream>
|
||||||
|
#include <cstdint>
|
||||||
|
|
||||||
#include <X11/Xlib.h>
|
// X11 includes
|
||||||
|
#include <X11/Xutil.h>
|
||||||
|
|
||||||
int main(int argc, char ** argv)
|
// X11Grabber includes
|
||||||
|
#include "X11Grabber.h"
|
||||||
|
|
||||||
|
X11Grabber::X11Grabber(const unsigned cropHorizontal, const unsigned cropVertical, const unsigned pixelDecimation) :
|
||||||
|
_pixelDecimation(pixelDecimation),
|
||||||
|
_cropWidth(cropHorizontal),
|
||||||
|
_cropHeight(cropVertical),
|
||||||
|
_x11Display(nullptr),
|
||||||
|
_screenWidth(0),
|
||||||
|
_screenHeight(0),
|
||||||
|
_image(0,0)
|
||||||
{
|
{
|
||||||
Display * dspl;
|
// empty
|
||||||
Drawable drwbl;
|
}
|
||||||
int x = 0;
|
|
||||||
int y = 0;
|
X11Grabber::~X11Grabber()
|
||||||
int width = 0;
|
{
|
||||||
int height = 0;
|
if (_x11Display != nullptr)
|
||||||
|
{
|
||||||
XImage * xImage = XGetImage(dspl, drwbl, x, y, width, height, AllPlanes, ZPixmap);
|
XCloseDisplay(_x11Display);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int X11Grabber::open()
|
||||||
|
{
|
||||||
|
const char * display_name = nullptr;
|
||||||
|
_x11Display = XOpenDisplay(display_name);
|
||||||
|
|
||||||
|
if (_x11Display == nullptr)
|
||||||
|
{
|
||||||
|
std::cerr << "Failed to open the default X11-display" << std::endl;
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
Image<ColorRgb> & X11Grabber::grab()
|
||||||
|
{
|
||||||
|
if (_x11Display == nullptr)
|
||||||
|
{
|
||||||
|
open();
|
||||||
|
}
|
||||||
|
|
||||||
|
updateScreenDimensions();
|
||||||
|
|
||||||
|
const int croppedWidth = _screenWidth - 2*_cropWidth;
|
||||||
|
const int croppedHeight = _screenHeight - 2*_cropHeight;
|
||||||
|
|
||||||
|
// Capture the current screen
|
||||||
|
XImage * xImage = XGetImage(_x11Display, DefaultRootWindow(_x11Display), _cropWidth, _cropHeight, croppedWidth, croppedHeight, AllPlanes, ZPixmap);
|
||||||
|
if (xImage == nullptr)
|
||||||
|
{
|
||||||
|
std::cerr << "Grab failed" << std::endl;
|
||||||
|
return _image;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Copy the capture XImage to the local image (and apply required decimation)
|
||||||
|
ColorRgb * outputPtr = _image.memptr();
|
||||||
|
for (int iY=(_pixelDecimation/2); iY<croppedHeight; iY+=_pixelDecimation)
|
||||||
|
{
|
||||||
|
for (int iX=(_pixelDecimation/2); iX<croppedWidth; iX+=_pixelDecimation)
|
||||||
|
{
|
||||||
|
// Extract the pixel from the X11-image
|
||||||
|
const uint32_t pixel = uint32_t(XGetPixel(xImage, iX, iY));
|
||||||
|
|
||||||
|
// Assign the color value
|
||||||
|
outputPtr->red = uint8_t((pixel >> 16) & 0xff);
|
||||||
|
outputPtr->green = uint8_t((pixel >> 8) & 0xff);
|
||||||
|
outputPtr->blue = uint8_t((pixel >> 0) & 0xff);
|
||||||
|
|
||||||
|
// Move to the next output pixel
|
||||||
|
++outputPtr;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Cleanup allocated resources of the X11 grab
|
||||||
|
XDestroyImage(xImage);
|
||||||
|
|
||||||
|
return _image;
|
||||||
|
}
|
||||||
|
|
||||||
|
int X11Grabber::updateScreenDimensions()
|
||||||
|
{
|
||||||
|
XWindowAttributes window_attributes_return;
|
||||||
|
const Status status = XGetWindowAttributes(_x11Display, DefaultRootWindow(_x11Display), &window_attributes_return);
|
||||||
|
if (status == 0)
|
||||||
|
{
|
||||||
|
std::cerr << "Failed to obtain window attributes" << std::endl;
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (_screenWidth == unsigned(window_attributes_return.width) && _screenHeight == unsigned(window_attributes_return.height))
|
||||||
|
{
|
||||||
|
// No update required
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
std::cout << "Update of screen resolution: [" << _screenWidth << "x" << _screenHeight <<"] => ";
|
||||||
|
_screenWidth = window_attributes_return.width;
|
||||||
|
_screenHeight = window_attributes_return.height;
|
||||||
|
std::cout << "[" << _screenWidth << "x" << _screenHeight <<"]" << std::endl;
|
||||||
|
|
||||||
|
// Update the size of the buffer used to transfer the screenshot
|
||||||
|
int width = (_screenWidth - 2 * _cropWidth + _pixelDecimation/2) / _pixelDecimation;
|
||||||
|
int height = (_screenHeight - 2 * _cropHeight + _pixelDecimation/2) / _pixelDecimation;
|
||||||
|
_image.resize(width, height);
|
||||||
|
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,37 @@
|
|||||||
|
|
||||||
|
// Hyperion-utils includes
|
||||||
|
#include <utils/Image.h>
|
||||||
|
#include <utils/ColorRgb.h>
|
||||||
|
|
||||||
|
// X11 includes
|
||||||
|
#include <X11/Xlib.h>
|
||||||
|
|
||||||
|
class X11Grabber
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
|
||||||
|
X11Grabber(const unsigned cropHorizontal, const unsigned cropVertical, const unsigned pixelDecimation);
|
||||||
|
|
||||||
|
virtual ~X11Grabber();
|
||||||
|
|
||||||
|
int open();
|
||||||
|
|
||||||
|
Image<ColorRgb> & grab();
|
||||||
|
|
||||||
|
private:
|
||||||
|
|
||||||
|
const unsigned _pixelDecimation;
|
||||||
|
|
||||||
|
const unsigned _cropWidth;
|
||||||
|
const unsigned _cropHeight;
|
||||||
|
|
||||||
|
/// Reference to the X11 display (nullptr if not opened)
|
||||||
|
Display * _x11Display;
|
||||||
|
|
||||||
|
unsigned _screenWidth;
|
||||||
|
unsigned _screenHeight;
|
||||||
|
|
||||||
|
Image<ColorRgb> _image;
|
||||||
|
|
||||||
|
int updateScreenDimensions();
|
||||||
|
};
|
33
src/hyperion-x11/X11Wrapper.cpp
Normal file
33
src/hyperion-x11/X11Wrapper.cpp
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
|
||||||
|
// Hyperion-X11 includes
|
||||||
|
#include "X11Wrapper.h"
|
||||||
|
|
||||||
|
X11Wrapper::X11Wrapper(const unsigned cropHorizontal, const unsigned cropVertical, const unsigned pixelDecimation) :
|
||||||
|
_timer(this),
|
||||||
|
_grabber(cropHorizontal, cropVertical, pixelDecimation)
|
||||||
|
{
|
||||||
|
// Connect capturing to the timeout signal of the timer
|
||||||
|
connect(&_timer, SIGNAL(timeout()), this, SLOT(capture()));
|
||||||
|
}
|
||||||
|
|
||||||
|
const Image<ColorRgb> & X11Wrapper::getScreenshot()
|
||||||
|
{
|
||||||
|
const Image<ColorRgb> & screenshot = _grabber.grab();
|
||||||
|
return screenshot;
|
||||||
|
}
|
||||||
|
|
||||||
|
void X11Wrapper::start()
|
||||||
|
{
|
||||||
|
_timer.start(200);
|
||||||
|
}
|
||||||
|
|
||||||
|
void X11Wrapper::stop()
|
||||||
|
{
|
||||||
|
_timer.stop();
|
||||||
|
}
|
||||||
|
|
||||||
|
void X11Wrapper::capture()
|
||||||
|
{
|
||||||
|
const Image<ColorRgb> & screenshot = _grabber.grab();
|
||||||
|
emit sig_screenshot(screenshot);
|
||||||
|
}
|
39
src/hyperion-x11/X11Wrapper.h
Normal file
39
src/hyperion-x11/X11Wrapper.h
Normal file
@ -0,0 +1,39 @@
|
|||||||
|
|
||||||
|
// QT includes
|
||||||
|
#include <QTimer>
|
||||||
|
|
||||||
|
// Hyperion-X11 includes
|
||||||
|
#include "X11Grabber.h"
|
||||||
|
|
||||||
|
class X11Wrapper : public QObject
|
||||||
|
{
|
||||||
|
Q_OBJECT
|
||||||
|
public:
|
||||||
|
X11Wrapper(const unsigned cropHorizontal, const unsigned cropVertical, const unsigned pixelDecimation);
|
||||||
|
|
||||||
|
const Image<ColorRgb> & getScreenshot();
|
||||||
|
|
||||||
|
///
|
||||||
|
/// Starts the timed capturing of screenshots
|
||||||
|
///
|
||||||
|
void start();
|
||||||
|
|
||||||
|
void stop();
|
||||||
|
|
||||||
|
signals:
|
||||||
|
void sig_screenshot(const Image<ColorRgb> & screenshot);
|
||||||
|
|
||||||
|
private slots:
|
||||||
|
///
|
||||||
|
/// Performs a single screenshot capture and publishes the capture screenshot on the screenshot
|
||||||
|
/// signal.
|
||||||
|
///
|
||||||
|
void capture();
|
||||||
|
|
||||||
|
private:
|
||||||
|
/// The QT timer to generate capture-publish events
|
||||||
|
QTimer _timer;
|
||||||
|
|
||||||
|
/// The grabber for creating screenshots
|
||||||
|
X11Grabber _grabber;
|
||||||
|
};
|
90
src/hyperion-x11/hyperion-x11.cpp
Normal file
90
src/hyperion-x11/hyperion-x11.cpp
Normal file
@ -0,0 +1,90 @@
|
|||||||
|
|
||||||
|
// QT includes
|
||||||
|
#include <QCoreApplication>
|
||||||
|
#include <QImage>
|
||||||
|
|
||||||
|
// getoptPlusPLus includes
|
||||||
|
#include <getoptPlusPlus/getoptpp.h>
|
||||||
|
|
||||||
|
#include "ProtoWrapper.h"
|
||||||
|
#include "X11Wrapper.h"
|
||||||
|
|
||||||
|
using namespace vlofgren;
|
||||||
|
|
||||||
|
// save the image as screenshot
|
||||||
|
void saveScreenshot(const char * filename, const Image<ColorRgb> & image)
|
||||||
|
{
|
||||||
|
// store as PNG
|
||||||
|
QImage pngImage((const uint8_t *) image.memptr(), image.width(), image.height(), 3*image.width(), QImage::Format_RGB888);
|
||||||
|
pngImage.save(filename);
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(int argc, char ** argv)
|
||||||
|
{
|
||||||
|
QCoreApplication app(argc, argv);
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
// create the option parser and initialize all parameters
|
||||||
|
OptionsParser optionParser("X11 capture application for Hyperion");
|
||||||
|
ParameterSet & parameters = optionParser.getParameters();
|
||||||
|
|
||||||
|
IntParameter & argCropWidth = parameters.add<IntParameter> (0x0, "crop-width", "Number of pixels to crop from the left and right sides in the picture before decimation [default=0]");
|
||||||
|
IntParameter & argCropHeight = parameters.add<IntParameter> (0x0, "crop-height", "Number of pixels to crop from the top and the bottom in the picture before decimation [default=0]");
|
||||||
|
IntParameter & argSizeDecimation = parameters.add<IntParameter> ('s', "size-decimator", "Decimation factor for the output size [default=1]");
|
||||||
|
SwitchParameter<> & argScreenshot = parameters.add<SwitchParameter<>> (0x0, "screenshot", "Take a single screenshot, save it to file and quit");
|
||||||
|
StringParameter & argAddress = parameters.add<StringParameter> ('a', "address", "Set the address of the hyperion server [default: 127.0.0.1:19445]");
|
||||||
|
IntParameter & argPriority = parameters.add<IntParameter> ('p', "priority", "Use the provided priority channel (the lower the number, the higher the priority) [default: 800]");
|
||||||
|
SwitchParameter<> & argSkipReply = parameters.add<SwitchParameter<>> (0x0, "skip-reply", "Do not receive and check reply messages from Hyperion");
|
||||||
|
SwitchParameter<> & argHelp = parameters.add<SwitchParameter<>> ('h', "help", "Show this help message and exit");
|
||||||
|
|
||||||
|
// set defaults
|
||||||
|
argCropWidth.setDefault(0);
|
||||||
|
argCropHeight.setDefault(0);
|
||||||
|
argSizeDecimation.setDefault(1);
|
||||||
|
argAddress.setDefault("127.0.0.1:19445");
|
||||||
|
argPriority.setDefault(800);
|
||||||
|
|
||||||
|
// parse all options
|
||||||
|
optionParser.parse(argc, const_cast<const char **>(argv));
|
||||||
|
|
||||||
|
// check if we need to display the usage. exit if we do.
|
||||||
|
if (argHelp.isSet())
|
||||||
|
{
|
||||||
|
optionParser.usage();
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create the X11 grabbing stuff
|
||||||
|
X11Wrapper x11Wrapper(argCropWidth.getValue(), argCropHeight.getValue(), argSizeDecimation.getValue());
|
||||||
|
|
||||||
|
if (argScreenshot.isSet())
|
||||||
|
{
|
||||||
|
// Capture a single screenshot and finish
|
||||||
|
const Image<ColorRgb> & screenshot = x11Wrapper.getScreenshot();
|
||||||
|
saveScreenshot("screenshot.png", screenshot);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// Create the Proto-connection with hyperiond
|
||||||
|
ProtoWrapper protoWrapper("127.0.0.1:19445", argSkipReply.isSet());
|
||||||
|
|
||||||
|
// Connect the screen capturing to the proto processing
|
||||||
|
QObject::connect(&x11Wrapper, SIGNAL(sig_screenshot(const Image<ColorRgb> &)), &protoWrapper, SLOT(process(Image<ColorRgb>)));
|
||||||
|
|
||||||
|
// Start the capturing
|
||||||
|
x11Wrapper.start();
|
||||||
|
|
||||||
|
// Start the application
|
||||||
|
app.exec();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (const std::runtime_error & e)
|
||||||
|
{
|
||||||
|
// An error occured. Display error and quit
|
||||||
|
std::cerr << e.what() << std::endl;
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user