www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - How do I get the screen resolution?

reply Alexander Zhirov <azhirov1991 gmail.com> writes:
Are there any methods to get the screen resolution?
On C/C++ from under X11, it is not possible to do this on the 
command line via SSH, since the display is not defined. And is it 
possible to do this somehow by means of D, pulling out the system 
resolution of the installed display?
Apr 28 2022
next sibling parent reply Mike Parker <aldacron gmail.com> writes:
On Thursday, 28 April 2022 at 11:22:15 UTC, Alexander Zhirov 
wrote:
 Are there any methods to get the screen resolution?
 On C/C++ from under X11, it is not possible to do this on the 
 command line via SSH, since the display is not defined. And is 
 it possible to do this somehow by means of D, pulling out the 
 system resolution of the installed display?
Like C++, the D standard library doesn't have anything for this. It's platform specific. There are several open source libraries out there that include that functionality, so you could just look at one of them (GLFW, SDL, SFML, etc.) and do what they do for the platforms you care about. They all have to go through the same system APIs. For example, on Windows you can use two calls to `GetSystemMetrics` (one for the width, one for the height). (https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-getsystemmetrics)
Apr 28 2022
parent Mike Parker <aldacron gmail.com> writes:
On Thursday, 28 April 2022 at 11:35:46 UTC, Mike Parker wrote:

 go through the same system APIs. For example, on Windows you 
 can use two calls to `GetSystemMetrics` (one for the width, one 
 for the height). 
 (https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-getsystemmetrics)
Been a while since I've done any Win32 stuff directly. In this age of multiple monitors, it's probably better to use `EnumDisplaySettings' on Windows. https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-enumdisplaysettingsa But the best option is like I said above: see how other libraries do it.
Apr 28 2022
prev sibling next sibling parent Dennis <dkorpel gmail.com> writes:
On Thursday, 28 April 2022 at 11:22:15 UTC, Alexander Zhirov 
wrote:
 Are there any methods to get the screen resolution?
Example with GLFW: https://github.com/dkorpel/glfw-d/blob/7a1eec60d427617c098d0e54a26cba796956a976/examples/empty-window/app.d#L118 Note that there can be multiple monitors, but you can use `glfwGetPrimaryMonitor()` to find the main one.
Apr 28 2022
prev sibling next sibling parent Adam D Ruppe <destructionator gmail.com> writes:
On Thursday, 28 April 2022 at 11:22:15 UTC, Alexander Zhirov 
wrote:
 Are there any methods to get the screen resolution?
Call DisplayWidth/DisplayHeight on the X connection... my simpledisplay.d provides bindings (though not a high level helper function since both X and Windows calls are trivial)
 On C/C++ from under X11, it is not possible to do this on the 
 command line via SSH, since the display is not defined.
Just defined the display, that's easy. import std.process; enviornment["DISPLAY"] = ":0"; that can work with any lib, then connect. OR, with my lib, it is all included: --- import arsd.simpledisplay; XDisplayConnection.setDisplayName(":0"); // set the local name regardless of what is set on ssh auto connection = XDisplayConnection.get(); // connection import std.stdio; writeln(DisplayWidth(connection, 0), " x ", DisplayHeight(connection, 0)); // the 0 is the screen number --- Though note with a multiple monitor system, this will give the *full* size of the virtual screen, not individual screens. simpledisplay has methods for this..... but I marked it `private` and it only loads the data when a window is created... you COULD hack past this anyway but prolly not worth the hassle. Maybe I will make it public officially in the next release. But if you are desperate for the info now, here's how you can bypass private and get at it anyway: --- // use reflection to bypass `private` alias MonitorInfo = __traits(getMember, arsd.simpledisplay.SimpleWindow, "MonitorInfo"); // make a temporary window just to get the info auto window = new SimpleWindow(1, 1, null, OpenGlOptions.no, Resizability.fixedSize, WindowTypes.eventOnly); // this is the function that actually loads the monitor info window.actualDpi(); // make sure none waiting in a buffer flushGui(); // and now get the data out foreach(monitor; MonitorInfo.info) { writeln(monitor.position.size); } window.close(); --- But since you're bypassing private and poking internals, if/when I do decide to make this public instead of private, I'll probably break this approach (when it is public though you can just simply loop over the info).
Apr 28 2022
prev sibling parent reply Christopher Katko <ckatko gmail.com> writes:
On Thursday, 28 April 2022 at 11:22:15 UTC, Alexander Zhirov 
wrote:
 Are there any methods to get the screen resolution?
 On C/C++ from under X11, it is not possible to do this on the 
 command line via SSH, since the display is not defined. And is 
 it possible to do this somehow by means of D, pulling out the 
 system resolution of the installed display?
Are you sure about that? https://askubuntu.com/questions/1077581/from-a-remote-ssh-session-how-can-i-get-the-screen-resolution-of-the-physically
Apr 28 2022
parent Alexander Zhirov <azhirov1991 gmail.com> writes:
On Thursday, 28 April 2022 at 22:51:02 UTC, Christopher Katko 
wrote:

 Are you sure about that?
Well, if we're talking about programming, then most likely I need to work with something like this :) https://en.wikipedia.org/wiki/Display_Data_Channel And how to do it - I can't find.
Apr 28 2022