Getting the screen DPI?

How can an application get the current screen DPI?

There is some talk of “HiDPI” around the place, but scanning the actual Haiku API headers doesn’t turn up much in relation to DPI. My apps already have a bunch of support for high DPI but they need to be told what the DPI actually is.

There seems to be a workable example in MonitorView::_UpdateDPI() but is that really the best option?

No, this is just for determining the monitor’s physical DPI, not UI “DPI” which may be different.

Haiku does not actually have anything like a “scale factor” which other OSes do, and pixels are always just pixels, never “device pixels” or “logical pixels” or anything else (well, unless you SetScale in a BView, but that’s just as with any other UI toolkit’s drawing API.) Instead, Haiku scales the user interface purely based on font size, and derives all metrics from that one way or another.

If you really need something more like a “scaling factor”, as non-native ported applications commonly do, you can either do max(1.0f, be_plain_font->Size() / 12.0f); or if you are at least a bit more flexible than that, you can ask BControlLook for the current spacing metrics and use those to decide how much you want to scale things by.


If you really want the physical DPI, then yes, _UpdateDPI is the way to get it.

Some things to be aware of:

  • It might not be available at all
  • It might be completely wrong, no sanitization of the data returned by the display is done
  • Even when it is accurate, there are edge cases where it won’t be the right thing. For example, a large screen viewed at a large distance (we have such things in meeting rooms at my workplace for example) may have a rather low DPI, but still needs the UI to be larger than the same screen viewed from very close

That’s why we recommend to base scaling on the font size setting instead. Later we will probably add some code to set the initial font/ui scale setting based on the DPI when a new display is connected, but only as a “best guess” and let the user override it.

1 Like

Doesn’t it make more sense anyway to query the resolution of the screen in order to display everything in the correct proportions instead of seeing what the graphics card can do but then not displaying it correctly?

But then it would make sense if you write software that compares the display of the possible to the actual image for system testing?

So I’ve done this, but it returns 1.0f for the moment. At least I can be somewhat confident that it won’t fall apart when the size changes. For the actual DPI I return to the client app I just multiple the scale by 96.0 as some sort of default.

If you want a higher number, increase your system font size in Appearance preferences appropriately: 18pt for “1.5x”, 24pt for “2x”, etc.

1 Like