60 lines
1.6 KiB
C++
60 lines
1.6 KiB
C++
// g++ screena.cpp -o screena -lX11 -lXext -Ofast -mfpmath=both -march=native -m64 -funroll-loops -mavx2 `pkg-config opencv --cflags --libs` && ./screena
|
|
|
|
// This includes most headers!
|
|
|
|
#include "screen.h"
|
|
|
|
// https://stackoverflow.com/a/39781697/4399859
|
|
|
|
ScreenShot::ScreenShot() {
|
|
|
|
display = XOpenDisplay(nullptr);
|
|
root = DefaultRootWindow(display);
|
|
|
|
int count_screens = ScreenCount(display);
|
|
if(count_screens == 0) {
|
|
puts("no screen info!");
|
|
}
|
|
for (int i = 0; i < count_screens; ++i) {
|
|
Screen* screen = ScreenOfDisplay(display, i);
|
|
printf("\tScreen %d: %dX%d\n", i + 1, screen->width, screen->height);
|
|
}
|
|
|
|
XGetWindowAttributes(display, root, &window_attributes);
|
|
Screen* screen = window_attributes.screen;
|
|
width = screen->width;
|
|
height = screen->height;
|
|
x = 0;
|
|
y = 0;
|
|
|
|
ximg = XShmCreateImage(display, DefaultVisualOfScreen(screen), DefaultDepthOfScreen(screen), ZPixmap, NULL, &shminfo, width, height);
|
|
|
|
shminfo.shmid = shmget(IPC_PRIVATE, ximg->bytes_per_line * ximg->height, IPC_CREAT|0777);
|
|
shminfo.shmaddr = ximg->data = (char*)shmat(shminfo.shmid, 0, 0);
|
|
shminfo.readOnly = false;
|
|
if(shminfo.shmid < 0)
|
|
puts("Fatal shminfo error!");
|
|
Status s1 = XShmAttach(display, &shminfo);
|
|
printf("XShmAttach() %s\n", s1 ? "success!" : "failure!");
|
|
|
|
init = true;
|
|
}
|
|
|
|
void ScreenShot::operator() (cv::Mat& cv_img){
|
|
if(init)
|
|
init = false;
|
|
|
|
XShmGetImage(display, root, ximg, 0, 0, 0x00ffffff);
|
|
cv_img = cv::Mat(height, width, CV_8UC4, ximg->data);
|
|
}
|
|
|
|
ScreenShot::~ScreenShot(){
|
|
if(!init)
|
|
XDestroyImage(ximg);
|
|
|
|
XShmDetach(display, &shminfo);
|
|
shmdt(shminfo.shmaddr);
|
|
XCloseDisplay(display);
|
|
}
|
|
|