Files
yellownotes/info_over_me.cpp
2025-11-27 09:46:00 +01:00

120 lines
2.5 KiB
C++

#include "info_over_me.h"
#include "utils/whereami.h"
#include <iostream>
#if defined(__linux) || defined(TARGET_OS_OSX)
#include <unistd.h>
#endif
#ifdef _WIN32
#include <windows.h>
#endif
extern "C" {
#include "gtk-imports.h"
}
#include <list>
#include <filesystem>
std::string InfoOverMe::containingFolder()
{
int len = wai_getExecutablePath(NULL, 0, NULL);
char *path = static_cast<char *>(malloc(len + 1));
wai_getExecutablePath(path, len, NULL);
path[len] = '\0';
std::filesystem::path p(path);
if (std::filesystem::is_regular_file(p)) {
std::filesystem::path pp = p.parent_path();
return pp.string();
} else {
return p.string();
}
}
std::string InfoOverMe::myHostname()
{
char buf[10240];
#if defined(__linux) || defined(TARGET_OS_OSX)
int r = gethostname(buf, 10240);
#endif
#ifdef _WIN32
DWORD size = 10240;
bool r = GetComputerNameA(buf, &size);
buf[size] = '\0';
if (!r) { sprintf(buf, "unknown"); }
#endif
std::string name(buf);
return name;
}
std::string InfoOverMe::myOsHostname()
{
std::string os;
#ifdef __linux
os = "linux";
#else
#ifdef _WIN32
os = "windows";
#else
#ifdef TARGET_OS_OSX
os = "osx";
#else
os = "unknown";
#endif
#endif
#endif
return os + "_" + myHostname();
}
std::string InfoOverMe::myId()
{
std::list<GdkRectangle> l;
GdkDisplay *d = gdk_display_get_default();
int n = gdk_display_get_n_monitors(d);
int i;
for(i = 0; i < n; i++) {
GdkMonitor *m = gdk_display_get_monitor(d, i);
GdkRectangle r;
gdk_monitor_get_geometry(m, &r);
l.push_back(r);
//std::cout << r.width << ", " << r.height << ", x = " << r.x << ", y = " << r.y << std::endl;
}
auto compare = [](const GdkRectangle &a, const GdkRectangle &b) {
if (a.x < b.x) {
return true;
} else if (a.x > b. x) {
return false;
} else if (a.y < b.y) {
return true;
} else {
return false;
}
};
l.sort(compare);
std::list<GdkRectangle>::iterator it;
std::string monitor_id;
std::string sep = "";
for(it = l.begin(); it != l.end(); it++) {
char buf[200];
const GdkRectangle &r = *it;
sprintf(buf, "%d-%d-%d-%d", r.x, r.y, r.width, r.height);
std::string geom(buf);
monitor_id += sep;
monitor_id += geom;
sep = "-";
}
monitor_id = myOsHostname() + "/" + monitor_id;
return monitor_id;
}
InfoOverMe::InfoOverMe() {}