Created
June 23, 2025 09:25
-
-
Save dec05eba/bb5d4ff5aa46def0cc3b30adcbf9934e to your computer and use it in GitHub Desktop.
Randr monitor property example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* 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; | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Run with:
dpi <monitor> <dpi>, for exampledpi DP-1 96. The DPI value will then be visible for the monitor inxrandr --propertiesand can be set withxrandr --output DP-1 --set Xft.dpi 192