Skip to content
Snippets Groups Projects
Commit ede48885 authored by simon.fanetti's avatar simon.fanetti
Browse files

fixed xrandr with lib instead of parsing output

parent a1464dff
No related branches found
No related tags found
No related merge requests found
......@@ -65,7 +65,10 @@ FORMS += \
INCLUDEPATH += ../../inc
LIBS += -L"../../build" -lsandbox -lrealsense2 -lyaml-cpp
LIBS += -L"../../build" -lsandbox
LIBS += -lrealsense2
LIBS += -lyaml-cpp
LIBS += -lXrandr -lX11
LIBS += $(shell pkg-config --libs --cflags opencv)
#QMAKE_CXXFLAGS += -fsanitize=address -fsanitize=leak -fsanitize=undefined
#include "monitorgui.h"
#include "ui_monitorgui.h"
#include <X11/Xlib.h>
#include <X11/extensions/Xrandr.h>
MonitorGui::MonitorGui(SandboxSetup *_setup, QWidget *parent) :
SubApp("Monitors", "Cancel resolution", parent),
......@@ -11,7 +13,7 @@ MonitorGui::MonitorGui(SandboxSetup *_setup, QWidget *parent) :
std::string path = ".monitors.tmp";
monitors = std::map<std::string, std::vector<std::string>>();
initMonitorMapWithFile(path);
initMonitorMap();
QList<QScreen*> screens = QApplication::screens();
for(int i=0; i<screens.size(); i++){
......@@ -80,17 +82,40 @@ void MonitorGui::on_cbxOutputs_currentIndexChanged(int index)
showMonitorsScreenshot(sc);
}
void MonitorGui::initMonitorMap(){
void MonitorGui::initMonitorMapWithFile(std::string path){
Display *dp = XOpenDisplay(NULL);
if(dp){
XRRScreenResources *screen = XRRGetScreenResources (dp, DefaultRootWindow(dp));
// Save in file the monitors and their resolutions
int err = system( ("xrandr | sed -n '1!p' > " + path).c_str() );
if(err){
std::cout << "Couldn't get screens and their resolutions" << std::endl;
exit(err);
// 4 display ports
for(int i=0; i<screen->ncrtc; i++){
XRRCrtcInfo *info = XRRGetCrtcInfo (dp, screen, screen->crtcs[i]);
// 2 display port ON
for(int j=0; j<info->noutput; j++){
XRROutputInfo *output = XRRGetOutputInfo(dp, screen, info->outputs[j]);
if(output->nmode > 0){
std::string key = std::string(output->name);
monitors[key] = std::vector<std::string>();
for(int k=0; k<output->nmode; k++){
XRRModeInfo resolution = screen->modes[k];
std::string res = std::string(std::to_string(resolution.width) + "x" + std::to_string(resolution.height));
if(std::find(monitors[key].begin(), monitors[key].end(), res) == monitors[key].end())
monitors[key].push_back(res);
}
}
XRRFreeOutputInfo(output);
}
XRRFreeCrtcInfo(info);
}
XRRFreeScreenResources(screen);
XCloseDisplay(dp);
}
loadResolutionsFromFile(path);
std::remove(path.c_str());
}
// load resolutions into GUI
......@@ -119,31 +144,6 @@ std::vector<std::string> MonitorGui::splitResolution(std::string s){
return res;
}
bool MonitorGui::isResolution(std::string s){
return splitResolution(s).size() == 2;
}
void MonitorGui::loadResolutionsFromFile(std::string path){
// Get monitors and their resolutions into a Map
std::ifstream infile(path);
std::string line;
std::string key;
while (std::getline(infile, line)){
std::istringstream iss(line);
std::string l;
iss >> l;
if(!isResolution(l)){
key = l;
monitors[key] = std::vector<std::string>();
}else{
monitors[key].push_back(l);
}
}
}
void MonitorGui::showMonitorsScreenshot(QScreen *screen){
QPixmap px = screen->grabWindow(0);
ui->lblScreenshot->setPixmap(px);
......
......@@ -50,10 +50,8 @@ private:
QScreen* getMonitor();
void loadResolutionsOf(QScreen* screen);
std::vector<std::string> splitResolution(std::string s);
bool isResolution(std::string s);
void loadResolutionsFromFile(std::string path);
void initMonitorMapWithFile(std::string path);
void showMonitorsScreenshot(QScreen *screen);
void initMonitorMap();
};
#endif // MONITORGUI_H
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment