Skip to content

@oranenj /toggle-primary.c
Created

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Toggle primary display using XRandr
/*
Copyright 2012, Jarkko Oranen
Redistribution, modification and use in any form is permitted
Use at your own risk. I will take no responsibility.
*/
// gcc -std=c99 -lXrandr toggle_primary.c -o toggle-primary
#include <X11/Xlib.h>
#include <X11/extensions/Xrandr.h>
Display* dpy = NULL;
Window root = 0;
int main(int argc, char** argv) {
dpy = XOpenDisplay(NULL);
root = DefaultRootWindow(dpy);
XRRScreenResources* res = XRRGetScreenResources(dpy, root);
int num_connected = 0;
RROutput connected[res->noutput];
for (int i = 0; i < res->noutput; i++) {
XRROutputInfo* oinfo = XRRGetOutputInfo(dpy, res, res->outputs[i]);
if (oinfo->connection == RR_Connected) {
connected[num_connected] = res->outputs[i];
num_connected++;
}
XRRFreeOutputInfo(oinfo);
}
if (num_connected <= 1) {
goto exit;
}
for (int i = 0; i < num_connected; i++) {
if (XRRGetOutputPrimary(dpy, root) == connected[i]) {
XRRSetOutputPrimary(dpy, root, connected[(i+1) % num_connected]);
break;
}
}
exit:
XSync(dpy, 1);
XCloseDisplay(dpy);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.