问题
All my searches lead to answers about how to get the geometry of the active screen in X11 using C, but that screen may contain many monitors, and there is normally only one screen unless there are many users simultaneously using the same computer.
Those answers about getting the screen geometry are out of date because they don't take into account multiple monitors.
So, how can I get the geometry (x, y, width, height) of the monitor that has the mouse pointer or active window on it? I would want the geometry relative to the screen.
回答1:
You need to use the RandR extension API. Get the source code of the xrandr
utility and steal what you need from there. You need XRRGetMonitors
function and related data structures. You can also use xrandr
to get the information you need on the command line.
I don't know if there's ever such thing as active monitor, because an active window can span several monitors, and a pointer can be in another monitor still. Anyway, the XRRMonitorInfo
structure contains all the information about your monitor geometry, including its size (in pixels and mm) and its position on the logical screen, so you can extract a monitor description from a pair of screen coordinates.
See also this answer.
来源:https://stackoverflow.com/questions/43005105/in-c-how-to-get-the-geometry-x-y-width-height-of-the-active-monitor-within