Alex Rivera | Logout

How to programmatically get the resolution of a window and that of the system in Linux?

Asked 2009-07-20T11:37:21.043
11

I'm trying to get the resolution of the screen as well as the resolution of a specific window (in which a program is running) on Linux system. I don't need to modify the resolution, I only need the current values. As far as I know, we can call some system functions to do so on Windows, how can we do that on Linux, preferably using C/C++ language? Thanks in advance.

update: Actually, I don't need to do a GUI, although I know Qt and GTK+ can do it, I'm reluctant to include an external library for just getting the resolution.

Edit
Report

1 Answer

9

To get screen resolution, you can use XRandR extension, like in xrandr sources:

  SizeID        current_size;
  XRRScreenSize *sizes;
  dpy = XOpenDisplay (display_name);
  // ...
  root = RootWindow (dpy, screen);
  sc = XRRGetScreenInfo (dpy, root);
  current_size = XRRConfigCurrentConfiguration (sc, &current_rotation);
  sizes = XRRConfigSizes(sc, &nsize);
  for (i = 0; i < nsize; i++) {
    printf ("%c%-2d %5d x %-5d  (%4dmm x%4dmm )",
         i == current_size ? '*' : ' ',
         i, sizes[i].width, sizes[i].height,
         sizes[i].mwidth, sizes[i].mheight);
     // ...
  }

You can see the output typing "xrandr" in your xterm.

Or, better, use the xdpyinfo method:

Display *dpy;
// dpy = ...
int scr = /* ... */
printf ("  dimensions:    %dx%d pixels (%dx%d millimeters)\n",
        DisplayWidth (dpy, scr), DisplayHeight (dpy, scr),
        DisplayWidthMM(dpy, scr), DisplayHeightMM (dpy, scr));
answered 2009-07-20T13:36:05.773

Your Answer