Skip to content

Instantly share code, notes, and snippets.

@dec05eba
Created June 23, 2025 09:25
Show Gist options
  • Select an option

  • Save dec05eba/bb5d4ff5aa46def0cc3b30adcbf9934e to your computer and use it in GitHub Desktop.

Select an option

Save dec05eba/bb5d4ff5aa46def0cc3b30adcbf9934e to your computer and use it in GitHub Desktop.
Randr monitor property example
/* Compile with: gcc main.c -o dpi $(pkg-config --cflags --libs x11 xrandr) */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <assert.h>
#include <X11/Xlib.h>
#include <X11/Xatom.h>
#include <X11/extensions/Xrandr.h>
int main(int argc, char **argv) {
if(argc != 3) {
fprintf(stderr, "usage: dpi <monitor> <dpi>\n");
return 1;
}
const char *target_monitor = argv[1];
const int dpi = atoi(argv[2]);
if(dpi <= 0) {
fprintf(stderr, "Error: expected dpi to be an integer value and be above 0\n");
return 1;
}
Display *dpy = XOpenDisplay(NULL);
assert(dpy);
const Atom xft_dpi_atom = XInternAtom(dpy, "Xft.dpi", False);
bool monitor_found = false;
int num_monitors = 0;
XRRMonitorInfo *monitors = XRRGetMonitors(dpy, DefaultRootWindow(dpy), True, &num_monitors);
for(int i = 0; i < num_monitors; ++i) {
char *monitor_name = XGetAtomName(dpy, monitors[i].name);
if(!monitor_name)
continue;
if(strcmp(monitor_name, target_monitor) == 0) {
monitor_found = true;
long values = dpi;
for(int j = 0; j < monitors[i].noutput; ++j) {
XRRConfigureOutputProperty(dpy, monitors[i].outputs[j], xft_dpi_atom, False, False, 0, NULL);
XRRChangeOutputProperty(dpy, monitors[i].outputs[j], xft_dpi_atom, XA_INTEGER, 32, PropModeReplace, (const unsigned char*)&values, 1);
}
}
XFree(monitor_name);
}
if(monitors)
XRRFreeMonitors(monitors);
if(!monitor_found)
fprintf(stderr, "Error: monitor %s is not a valid active monitor\n", target_monitor);
XFlush(dpy);
XCloseDisplay(dpy);
return 0;
}
@dec05eba
Copy link
Author

Run with: dpi <monitor> <dpi>, for example dpi DP-1 96. The DPI value will then be visible for the monitor in xrandr --properties and can be set with xrandr --output DP-1 --set Xft.dpi 192

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment