Skip to content

Instantly share code, notes, and snippets.

@shiyuugohirao
Created March 8, 2024 10:00
Show Gist options
  • Save shiyuugohirao/a1ca1a15684a8618f15514f9baca36d3 to your computer and use it in GitHub Desktop.
Save shiyuugohirao/a1ca1a15684a8618f15514f9baca36d3 to your computer and use it in GitHub Desktop.
get Moitor information for openframeworks
#pragma once
#include "ofLog.h"
#include "ofVectorMath.h"
#include "GLFW/glfw3.h"
namespace ofxMonitorUtils {
struct MonitorInfo {
int index;
std::string name;
bool isPrimary;
int w, h, x, y, physW, physH, refreshRate;
MonitorInfo(){};
MonitorInfo(int i, std::string name, int w, int h, int x, int y,
bool isPrimary, int physW, int physH, int rate)
: index(i), name(name), w(w), h(h), x(x), y(y), isPrimary(isPrimary), physW(physW), physH(physH), refreshRate(rate){};
glm::vec2 getPosition() { return glm::vec2(x, y); }
glm::vec2 getResolution() { return glm::vec2(w, h); }
};
inline std::vector<MonitorInfo> getMonitorInformations(bool log = false) {
std::vector<MonitorInfo> monitorInfo;
glfwInit();
auto primary = glfwGetPrimaryMonitor();
int count;
const auto monitors = glfwGetMonitors(&count);
for (int i = 0; i < count; i++) {
auto monitor = monitors[i];
bool isPrimary = (primary == monitor);
int w, h, x, y, physW, physH, refreshRate;
glfwGetMonitorPhysicalSize(monitor, &physW, &physH);
glfwGetMonitorPos(monitor, &x, &y);
auto mode = glfwGetVideoMode(monitor);
w = mode->width;
h = mode->height;
refreshRate = mode->refreshRate;
std::string name = glfwGetMonitorName(monitor);
monitorInfo.push_back(MonitorInfo(i, name, w, h, x, y, isPrimary, physW, physH, refreshRate));
if (log) {
ofLogNotice(__FUNCTION__) << "\n "
<< i << ": " << name << (isPrimary ? "(Primary)" : "") << "\n"
<< " Resolution : " << w << "x" << h << "pix from Pos: " << x << ", " << y << "pix\n"
<< " PhysicalSize : " << physW << "x" << physH << "mm / RefreshRate:" << refreshRate << "Hz";
}
}
return monitorInfo;
}
inline MonitorInfo getPrimaryMonitorInfo(bool log = false) {
int monitorIndex = 0;
auto monitors = getMonitorInformations(log);
auto it = std::find_if(monitors.begin(), monitors.end(), [](const auto& mi) { return mi.isPrimary == true; });
if (it != monitors.end())
monitorIndex = it->index;
else
ofLogWarning(__FUNCTION__, "not found Primary Monitor, set monitorIndex to 0 ...");
return monitors[monitorIndex];
}
inline MonitorInfo getSubMonitorInfo(bool log = false) {
int monitorIndex = 0;
auto monitors = getMonitorInformations(log);
auto it = std::find_if(monitors.begin(), monitors.end(), [](const auto& mi) { return mi.isPrimary == false; });
if (it != monitors.end())
monitorIndex = it->index;
else
ofLogWarning(__FUNCTION__, "not found Sub Monitor, set monitorIndex to 0 ...");
return monitors[monitorIndex];
}
} // namespace ofxMonitorUtils
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment