-
Notifications
You must be signed in to change notification settings - Fork 5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature request: glfwGetCurrentMonitor #1699
Comments
Do you mean glfwGetPrimaryMonitor? |
That function returns the first monitor in the monitor list, which is not the same as the monitor which the user is currently using. |
Do you mean windowed mode windows, full screen windows or both? |
Once the "current" monitor is fetched, it should be possible to open either a fullscreen or a windowed mode window on that monitor. For my particular use case I'm interested in opening a windowed mode window on the current monitor. This API would be a sufficient addition to accomplish that using other monitor geometry querying methods that are already available. |
Do I understand correctly, you want to get the monitor a window is on, or "most" on (In the case of it being in between)? Cause the way I see it, the "monitor" a window is on could be hard to figure out or define. |
@DerekCresswell both macOS and Windows have APIs to perform this function, as mentioned in the top post. |
This would be a useful addition. I propose |
@elmindreda thanks! IMO |
Here are a couple functions I have been using to get these behaviors using the existing API. The first gets the monitor the cursor is currently on (get focused monitor?). This requires the window to be created first, but if the window is resized/repositioned immediately after creation it is not even visible in the wrong location before it is moved. The second gets the monitor the window is most on by area (get closest monitor). Note, these are actually translated versions because my original versions further functionalize some of this stuff, so may have some typo bugs. bool glfw_get_mouse_monitor(
GLFWmonitor** monitor,
GLFWwindow* window
) {
bool success = false;
double cursor_position[2] = {0};
glfwGetCursorPos(window, &cursor_position[0], &cursor_position[1]);
int window_position[2] = {0};
glfwGetWindowPos(window, &window_position[0], &window_position[1]);
int monitors_size = 0;
GLFWmonitor** monitors = glfwGetMonitors(&monitors_size);
// convert cursor position from window coordinates to screen coordinates
cursor_position[0] += window_position[0];
cursor_position[1] += window_position[1];
for (int i = 0; ((!success) && (i < monitors_size)); ++i)
{
int monitor_position[2] = {0};
glfwGetMonitorPos(monitors[i], &monitor_position[0], &monitor_position[1]);
const GLFWvidmode* monitor_video_mode = glfwGetVideoMode(monitors[i]);
if (
(cursor_position[0] < monitor_position[0]) ||
(cursor_position[0] > (monitor_position[0] + monitor_video_mode->width)) ||
(cursor_position[1] < monitor_position[1]) ||
(cursor_position[1] > (monitor_position[1] + monitor_video_mode->height))
) {
*monitor = monitors[i];
success = true;
}
}
// true: monitor contains the monitor the mouse is on
// false: monitor is unmodified
return success;
}
bool glfw_get_window_monitor(
GLFWmonitor** monitor,
GLFWwindow* window
) {
bool success = false;
int window_rectangle[4] = {0};
glfwGetWindowPos(window, &window_rectangle[0], &window_rectangle[1]);
glfwGetWindowSize(window, &window_rectangle[2], &window_rectangle[3]);
int monitors_size = 0;
GLFWmonitor** monitors = glfwGetMonitors(&monitors_size);
GLFWmonitor* closest_monitor = NULL;
int max_overlap_area = 0;
for (int i = 0; i < monitors_size; ++i)
{
int monitor_position[2] = {0};
glfwGetMonitorPos(monitors[i], &monitor_position[0], &monitor_position[1]);
const GLFWvidmode* monitor_video_mode = glfwGetVideoMode(monitors[i]);
if (
!(
((window_rectangle[0] + window_rectangle[2]) < monitor_rectangle[0]) ||
(window_rectangle[0] > (monitor_rectangle[0] + monitor_rectangle[2])) ||
((window_rectangle[1] + window_rectangle[3]) < monitor_rectangle[1]) ||
(window_rectangle[1] > (monitor_rectangle[1] + monitor_rectangle[3]))
)
) {
int intersection_rectangle[4] = {0};
// x, width
if (window_rectangle[0] < monitor_rectangle[0])
{
intersection_rectangle[0] = monitor_rectangle[0];
if ((window_rectangle[0] + window_rectangle[2]) < (monitor_rectangle[0] + monitor_rectangle[2]))
{
intersection_rectangle[2] = (window_rectangle[0] + window_rectangle[2]) - intersection_rectangle[0];
}
else
{
intersection_rectangle[2] = monitor_rectangle[2];
}
}
else
{
intersection_rectangle[0] = window_rectangle[0];
if ((monitor_rectangle[0] + monitor_rectangle[2]) < (window_rectangle[0] + window_rectangle[2]))
{
intersection_rectangle[2] = (monitor_rectangle[0] + monitor_rectangle[2]) - intersection_rectangle[0];
}
else
{
intersection_rectangle[2] = window_rectangle[2];
}
}
// y, height
if (window_rectangle[1] < monitor_rectangle[1])
{
intersection_rectangle[1] = monitor_rectangle[1];
if ((window_rectangle[1] + window_rectangle[3]) < (monitor_rectangle[1] + monitor_rectangle[3]))
{
intersection_rectangle[3] = (window_rectangle[1] + window_rectangle[3]) - intersection_rectangle[1];
}
else
{
intersection_rectangle[3] = monitor_rectangle[3];
}
}
else
{
intersection_rectangle[1] = window_rectangle[1];
if ((monitor_rectangle[1] + monitor_rectangle[3]) < (window_rectangle[1] + window_rectangle[3]))
{
intersection_rectangle[3] = (monitor_rectangle[1] + monitor_rectangle[3]) - intersection_rectangle[1];
}
else
{
intersection_rectangle[3] = window_rectangle[3];
}
}
int overlap_area = intersection_rectangle[3] * intersection_rectangle[4];
if (overlap_area > max_overlap_area)
{
closest_monitor = monitors[i];
max_overlap_area = overlap_area;
}
}
}
if (closest_monitor)
{
*monitor = closest_monitor;
success = true;
}
// true: monitor contains the monitor the window is most on
// false: monitor is unmodified
return success;
} |
@slawrence2302 this is a neat workaround, thanks for posting it! FWIW, the proposal I'm making is that |
@nornagon, I thought that might be what you meant, and for the record I would also find that feature useful. Things like start menus (or equivalents) sometimes only being visible on the primary monitor make the cursor solution less than perfect, and I definitely agree that the easiest behavior for the user to understand is the behavior they're already used to. |
This feature would be very useful in cases when you need to create a scaled rendering context using the Is it against the ideas of GLFW to implement functions which are not fully cross-platform? (I guess it's not an issue since we have native access functions.) Because I think it would be easy to implement |
@slawrence2302
|
macOS and Windows both have some concept of which monitor is "current" when multiple displays are attached.
macOS:
[NSScreen main]
returns the monitor which contains the window which currently has keyboard focus.Windows:
MonitorFromWindow(GetForegroundWindow())
does the same. (MonitorFromWindow, GetForegroundWindow)It would be great to be able to get this information through glfw to support the use case of opening a window on the "correct" display.
The text was updated successfully, but these errors were encountered: