How do I get the screen resolution?

Adam D Ruppe destructionator at gmail.com
Thu Apr 28 11:42:28 UTC 2022


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).


More information about the Digitalmars-d-learn mailing list