[PATCH xrandr] xrandr: dump configuration in parsable format

Stefan Tomanek stefan.tomanek at wertarbyte.de
Wed Mar 9 03:37:05 PST 2011


This patch adds the command line switch "--dump" to the xrandr program.
When specified, xrandr outputs the current display configuration in an
easily parsable format similar to the command line options accepted by
xrandr itself. That way, an existing configuration can easily be saved
and restored later.

Signed-off-by: Stefan Tomanek <stefan.tomanek at wertarbyte.de>
---
 man/xrandr.man |    3 +++
 xrandr.c       |   55 ++++++++++++++++++++++++++++++++++++++++---------------
 2 files changed, 43 insertions(+), 15 deletions(-)

diff --git a/man/xrandr.man b/man/xrandr.man
index ba36ae3..41f4332 100644
--- a/man/xrandr.man
+++ b/man/xrandr.man
@@ -26,6 +26,7 @@ xrandr \- primitive command line interface to RandR extension
 .B "xrandr"
 [\-help]  [\-display \fIdisplay\fP]
 [\-q] [\-v]
+[\-\-dump]
 [\-\-verbose]
 [\-\-dryrun]
 [\-\-screen \fIsnum\fP]
@@ -110,6 +111,8 @@ reported while executing the configuration changes.
 .IP "\-q, \-\-query"
 When this option is present, or when no configuration changes are requested,
 xrandr will display the current state of the system.
+.IP "\-\-dump"
+Dump the current display configuration in an easily parsable format.
 .IP "\-\-dryrun"
 Performs all the actions specified except that no changes are made.
 .IP "\-\-nograb"
diff --git a/xrandr.c b/xrandr.c
index 558fa45..df288e6 100644
--- a/xrandr.c
+++ b/xrandr.c
@@ -141,6 +141,7 @@ usage(void)
     fprintf(stderr, "  --rmmode <name>\n");
     fprintf(stderr, "  --addmode <output> <name>\n");
     fprintf(stderr, "  --delmode <output> <name>\n");
+    fprintf(stderr, "  --dump\n");
 
     exit(1);
     /*NOTREACHED*/
@@ -2161,6 +2162,7 @@ main (int argc, char **argv)
     Bool	modeit = False;
     Bool	propit = False;
     Bool	query_1 = False;
+    Bool	dump = False;
     int		major, minor;
     Bool	current = False;
 
@@ -2252,6 +2254,10 @@ main (int argc, char **argv)
 	    query = True;
 	    continue;
 	}
+	if (!strcmp ("--dump", argv[i])) {
+	    dump = True;
+	    continue;
+	}
 	if (!strcmp ("-o", argv[i]) || !strcmp ("--orientation", argv[i])) {
 	    char *endptr;
 	    if (++i>=argc) usage ();
@@ -2919,11 +2925,13 @@ main (int argc, char **argv)
 	get_screen (current);
 	get_crtcs ();
 	get_outputs ();
-
-        printf ("Screen %d: minimum %d x %d, current %d x %d, maximum %d x %d\n",
+	if (!dump)
+	{
+	    printf ("Screen %d: minimum %d x %d, current %d x %d, maximum %d x %d\n",
 		screen, minWidth, minHeight,
 		DisplayWidth (dpy, screen), DisplayHeight(dpy, screen),
 		maxWidth, maxHeight);
+	}
 
 	for (output = outputs; output; output = output->next)
 	{
@@ -2936,28 +2944,38 @@ main (int argc, char **argv)
 	    Bool	    *mode_shown;
 	    Rotation	    rotations = output_rotations (output);
 
-	    printf ("%s %s", output_info->name, connection[output_info->connection]);
+	    printf ( dump ? "output %s\n" : "%s %s", output_info->name, connection[output_info->connection]);
+	    if (dump && output_is_primary(output))
+	    {
+		printf( "primary\n" );
+	    }
+
 	    if (mode)
 	    {
 		if (crtc_info) {
-		    printf (" %dx%d+%d+%d",
+		    printf ( dump ? "mode %dx%d\npos %dx%d\n" : " %dx%d+%d+%d",
 			    crtc_info->width, crtc_info->height,
 			    crtc_info->x, crtc_info->y);
 		} else {
-		    printf (" %dx%d+%d+%d",
-			    mode->width, mode->height, output->x, output->y);
+		    printf ( dump ? "mode %dx%d\npos %dx%d\n" : " %dx%d+%d+%d",
+			mode->width, mode->height, output->x, output->y);
 		}
-		if (verbose)
+		if (verbose && !dump)
 		    printf (" (0x%x)", (int)mode->id);
-		if (output->rotation != RR_Rotate_0 || verbose)
+		if (output->rotation != RR_Rotate_0 || verbose || dump)
 		{
-		    printf (" %s", 
+		    printf ( dump ? "rotate %s\n" : " %s",
 			    rotation_name (output->rotation));
 		    if (output->rotation & (RR_Reflect_X|RR_Reflect_Y))
-			printf (" %s", reflection_name (output->rotation));
+			printf ( dump ? "reflect %s\n" : " %s", reflection_name (output->rotation));
+		}
+	    } else {
+		if (dump)
+		{
+		    printf( "off\n" );
 		}
 	    }
-	    if (rotations != RR_Rotate_0 || verbose)
+	    if (!dump && (rotations != RR_Rotate_0 || verbose))
 	    {
 		Bool    first = True;
 		printf (" (");
@@ -2981,7 +2999,7 @@ main (int argc, char **argv)
 		printf (")");
 	    }
 
-	    if (mode)
+	    if (!dump && mode)
 	    {
 		printf (" %dmm x %dmm",
 			(int)output_info->mm_width, (int)output_info->mm_height);
@@ -2990,7 +3008,7 @@ main (int argc, char **argv)
 	    if (crtc && crtc->panning_info && crtc->panning_info->width > 0)
 	    {
 		XRRPanning *pan = crtc->panning_info;
-		printf (" panning %dx%d+%d+%d",
+		printf ( dump ? "panning %dx%d+%d+%d" : " panning %dx%d+%d+%d",
 			pan->width, pan->height, pan->left, pan->top);
 		if ((pan->track_width    != 0 &&
 		     (pan->track_left    != pan->left		||
@@ -3002,12 +3020,19 @@ main (int argc, char **argv)
 		      pan->track_height  != pan->height		||
 		      pan->border_top    != 0			||
 		      pan->border_bottom != 0)))
-		    printf (" tracking %dx%d+%d+%d border %d/%d/%d/%d",
+		    printf ( dump ? "/%dx%d+%d+%d/%d/%d/%d/%d\n" : "tracking %dx%d+%d+%d border %d/%d/%d/%d",
 			    pan->track_width,  pan->track_height,
 			    pan->track_left,   pan->track_top,
 			    pan->border_left,  pan->border_top,
 			    pan->border_right, pan->border_bottom);
 	    }
+
+	    /* If we are dumping our configuration, we end the loop here */
+	    if (dump)
+	    {
+		continue;
+	    }
+
 	    printf ("\n");
 
 	    if (verbose)
@@ -3222,7 +3247,7 @@ main (int argc, char **argv)
 	{
 	    XRRModeInfo	*mode = &res->modes[m];
 
-	    if (!(mode->modeFlags & ModeShown))
+	    if (!(mode->modeFlags & ModeShown) && !dump)
 	    {
 		printf ("  %s (0x%x) %6.1fMHz\n",
 			mode->name, (int)mode->id,
-- 
1.7.2.3


More information about the xorg-devel mailing list