In a desktop application, I need to detect monitor resolution from time to time. The code is like:

Display *d;
Screen *s;
d = XOpenDisplay(":0");
if (NULL == d) {
    g_print("XOpenDisplay failed\n");
s = DefaultScreenOfDisplay(d);
if (NULL == s) {
    g_print("DefaultScreenOfDisplay failed\n");

g_print("Display size: %dx%d\n", s->width, s->height);

But another requirement needs me to turn off lightdm service and X server too, which this piece of code depends on.

Is there other method to detect resolution without Xlib support? Thanks

  • Have you looked at xrandr? – ctrl-alt-delor Jul 2 '18 at 11:27
  • With lightdm off, xrandr shows "Can't open display". So I figure it depends on X11 too? – user297907 Jul 2 '18 at 11:32
  • Oh you are turning X11 off as well. Can you make this clear in the question. (you don't need a display manager to run X11, so we just assumed that you were still running X11). – ctrl-alt-delor Jul 2 '18 at 11:40
  • Actually I'm doing this to avoid a confliction between my app and something in lightdm service. I'm not sure if it's the display manager or X11 itself. So I want to turn them all off. – user297907 Jul 2 '18 at 11:50

Finding the (native/maximum) monitor resolution is possible even without an X server by querying the EDID information directly. See How to get EDID for a single monitor?. Experience may vary depending on graphics card and drivers.

| improve this answer | |

You could try the command xrandr

| improve this answer | |
  • Or libxrandr. – ctrl-alt-delor Jul 2 '18 at 11:28
  • Answer should provide a complete explanation. your answer would be better as a comment. – Kiwy Jul 2 '18 at 12:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.