Skip to content

Instantly share code, notes, and snippets.

@draftcode
Created September 11, 2013 03:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save draftcode/6518960 to your computer and use it in GitHub Desktop.
Save draftcode/6518960 to your computer and use it in GitHub Desktop.
xrrtest: xrrtest.cpp
${CXX} -lX11 -lXinerama -lXrandr -o xrrtest xrrtest.cpp
#include <cstdlib>
#include <cstdio>
#include <X11/Xlib.h>
#include <X11/extensions/Xrandr.h>
bool get_width_height(XRRScreenResources *res, RRMode id,
unsigned int *width, unsigned int *height) {
for (int i = 0; i < res->nmode; i++) {
if (res->modes[i].id == id) {
*width = res->modes[i].width;
*height = res->modes[i].height;
return true;
}
}
return false;
}
int main(void) {
Display *dpy = XOpenDisplay(NULL);
if (!dpy) abort();
XRRScreenResources *res = XRRGetScreenResources(dpy, RootWindow(dpy, 0));
if (!res) abort();
for (int output_number = 0; output_number < res->noutput; output_number++) {
XRROutputInfo *output_info = XRRGetOutputInfo(dpy, res, res->outputs[output_number]);
if (!output_info) abort();
if (output_info->connection == RR_Connected) {
printf("%s\n", output_info->name);
for (int mode_number = 0; mode_number < output_info->nmode; mode_number++) {
unsigned int width, height;
if (get_width_height(res, output_info->modes[mode_number],
&width, &height))
{
printf("\t%dx%d\n", width, height);
}
}
}
XRRFreeOutputInfo(output_info);
}
XRRFreeScreenResources(res);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment