Cleanup monitor management in Rendering Manager

This commit is contained in:
Bruno Herbelin
2022-12-26 15:45:42 +01:00
parent 55967ad27c
commit 189e7b8bc9
2 changed files with 83 additions and 95 deletions

View File

@@ -183,36 +183,89 @@ static void WindowCloseCallback( GLFWwindow* w )
void Rendering::MonitorConnect(GLFWmonitor* monitor, int event)
{
// Fill list of monitors of rendering manager
// reset list of monitors
Rendering::manager().monitors_.clear();
// list monitors with GLFW
int count_monitors = 0;
GLFWmonitor** monitors = glfwGetMonitors(&count_monitors);
// list monitors
if (count_monitors > 0) {
Rendering::manager().monitors_.clear();
int i = 0;
for (; i < count_monitors; i++) {
// Fill list of monitors of rendering manager
for (int i = 0; i < count_monitors; i++) {
// fill monitor structure
RenderingMonitor mo;
int x = 0, y = 0;
glfwGetMonitorPos(monitors[i], &x, &y);
const GLFWvidmode *vm = glfwGetVideoMode(monitors[i]);
mo.dimension = glm::ivec2(vm->width, vm->height);
glfwGetMonitorPos(monitors[i], &mo.position.x, &mo.position.y);
mo.name = glfwGetMonitorName(monitors[i]);
std::string n = glfwGetMonitorName(monitors[i]);
// add
Rendering::manager().monitors_.push_back(mo);
}
Rendering::manager().monitors_[n] = glm::ivec4(x, y, vm->width, vm->height);
}
// TODO : find appropriate
// inform Displays View that monitors changed
Mixer::manager().view(View::DISPLAYS)->recenter();
#ifndef NDEBUG
if (event == GLFW_CONNECTED)
g_printerr("Monitor %s connected\n", glfwGetMonitorName(monitor));
else if (event == GLFW_DISCONNECTED)
g_printerr("Monitor %s disconnected\n", glfwGetMonitorName(monitor));
for (auto m = Rendering::manager().monitors_.begin(); m != Rendering::manager().monitors_.end(); m++) {
g_printerr("Monitor %s [%d, %d](%d x %d)\n", m->name.c_str(), m->position.x, m->position.y, m->dimension.x, m->dimension.y);
}
#endif
}
GLFWmonitor *Rendering::monitorAt(int x, int y)
{
// default to primary monitor
GLFWmonitor *mo = glfwGetPrimaryMonitor();
// list all monitors
int count_monitors = 0;
GLFWmonitor** monitors = glfwGetMonitors(&count_monitors);
// if there is more than one monitor
if (count_monitors > 1) {
// try every monitor
for (int i = 0; i < count_monitors; i++) {
int workarea_x, workarea_y;
glfwGetMonitorPos(monitors[i], &workarea_x, &workarea_y);
const GLFWvidmode *vm = glfwGetVideoMode(monitors[i]);
if ( x >= workarea_x && x <= workarea_x + vm->width &&
y >= workarea_y && y <= workarea_y + vm->height) {
// found the monitor containing this point !
mo = monitors[i];
break;
}
}
}
return mo;
}
GLFWmonitor *Rendering::monitorNamed(const std::string &name)
{
// default to primary monitor
GLFWmonitor *mo = glfwGetPrimaryMonitor();
// list all monitors
int count_monitors = 0;
GLFWmonitor** monitors = glfwGetMonitors(&count_monitors);
// if there is more than one monitor
if (count_monitors > 1) {
// try every monitor
for (int i = 0; i < count_monitors; i++) {
if ( name.compare( glfwGetMonitorName(monitors[i]) ) == 0 ){
// found the monitor with this name
mo = monitors[i];
break;
}
}
}
return mo;
}
void Rendering::close()
{
glfwSetWindowShouldClose(main_.window(), GLFW_TRUE);
@@ -363,12 +416,6 @@ bool Rendering::isActive()
return !glfwWindowShouldClose(main_.window());
}
void Rendering::pushFrontDrawCallback(RenderingCallback function)
{
draw_callbacks_.push_front(function);
}
void Rendering::pushBackDrawCallback(RenderingCallback function)
{
draw_callbacks_.push_back(function);
@@ -662,67 +709,13 @@ bool RenderingWindow::isFullscreen ()
return Settings::application.windows[index_].fullscreen;
}
GLFWmonitor *RenderingWindow::monitorAt(int x, int y)
{
// default to primary monitor
GLFWmonitor *mo = glfwGetPrimaryMonitor();
// list all monitors
int count_monitors = 0;
GLFWmonitor** monitors = glfwGetMonitors(&count_monitors);
// if there is more than one monitor
if (count_monitors > 1) {
// pick at the coordinates given or at pos of window
// try every monitor
int i = 0;
for (; i < count_monitors; i++) {
int workarea_x, workarea_y;
glfwGetMonitorPos(monitors[i], &workarea_x, &workarea_y);
const GLFWvidmode *vm = glfwGetVideoMode(monitors[i]);
if ( x >= workarea_x && x <= workarea_x + vm->width &&
y >= workarea_y && y <= workarea_y + vm->height)
break;
}
// found the monitor containing this point !
if ( i < count_monitors)
mo = monitors[i];
}
return mo;
}
GLFWmonitor *RenderingWindow::monitorNamed(const std::string &name)
{
// default to primary monitor
GLFWmonitor *mo = glfwGetPrimaryMonitor();
// list all monitors
int count_monitors = 0;
GLFWmonitor** monitors = glfwGetMonitors(&count_monitors);
// if there is more than one monitor
if (count_monitors > 1) {
// pick at the coordinates given or at pos of window
// try every monitor
int i = 0;
for (; i < count_monitors; i++) {
if ( std::string( glfwGetMonitorName(monitors[i])) == name )
break;
}
// found the monitor with this name
if ( i < count_monitors)
mo = monitors[i];
}
return mo;
}
GLFWmonitor *RenderingWindow::monitor()
{
// pick at the coordinates given or at pos of window
int x = 1, y = 1;
if (window_ != nullptr)
glfwGetWindowPos(window_, &x, &y);
return monitorAt(x, y);
return Rendering::manager().monitorAt(x, y);
}
void RenderingWindow::setFullscreen_(GLFWmonitor *mo)
@@ -922,7 +915,7 @@ void RenderingWindow::show()
glfwShowWindow(window_);
if ( Settings::application.windows[index_].fullscreen ) {
GLFWmonitor *mo = monitorNamed(Settings::application.windows[index_].monitor);
GLFWmonitor *mo = Rendering::manager().monitorNamed(Settings::application.windows[index_].monitor);
setFullscreen_(mo);
}

View File

@@ -16,14 +16,6 @@ typedef struct GLFWmonitor GLFWmonitor;
typedef struct GLFWwindow GLFWwindow;
class FrameBuffer;
struct RenderingMonitor
{
RenderingMonitor() {}
glm::ivec2 dimension;
glm::ivec2 position;
std::string name;
};
struct RenderingAttrib
{
RenderingAttrib() {}
@@ -89,10 +81,6 @@ public:
// get monitor in which the window is
GLFWmonitor *monitor();
// get which monitor contains this point
static GLFWmonitor *monitorAt(int x, int y);
// get which monitor has this name
static GLFWmonitor *monitorNamed(const std::string &name);
glm::vec2 previous_size;
};
@@ -130,7 +118,6 @@ public:
// add function to call during Draw
typedef void (* RenderingCallback)(void);
void pushFrontDrawCallback(RenderingCallback function);
void pushBackDrawCallback(RenderingCallback function);
// push and pop rendering attributes
@@ -155,6 +142,14 @@ public:
// project from scene coordinate to window
glm::vec2 project(glm::vec3 scene_coordinate, glm::mat4 modelview = glm::mat4(1.f), bool to_framebuffer = true);
// get hold on the monitors
void monitorsUpdate();
inline std::map<std::string, glm::ivec4> monitors() { return monitors_; }
// get which monitor contains this point
GLFWmonitor *monitorAt(int x, int y);
// get which monitor has this name
GLFWmonitor *monitorNamed(const std::string &name);
// memory management
static glm::ivec2 getGPUMemoryInformation();
static bool shouldHaveEnoughMemory(glm::vec3 resolution, int flags);
@@ -179,7 +174,7 @@ private:
RenderingWindow output_;
// monitors
std::list<RenderingMonitor> monitors_;
std::map<std::string, glm::ivec4> monitors_;
static void MonitorConnect(GLFWmonitor* monitor, int event);
// file drop callback