Created
September 6, 2012 16:51
-
-
Save oranenj/3658446 to your computer and use it in GitHub Desktop.
Toggle primary display using XRandr
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
/* | |
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