[PATCH xrandr] xrandr: dump configuration in parsable format

Stefan Tomanek stefan.tomanek at wertarbyte.de
Mon Nov 8 14:29:39 PST 2010


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>
---
 xrandr.c   |   55 ++++++++++++++++++++++++++++++++++++++++---------------
 xrandr.man |    3 +++
 2 files changed, 43 insertions(+), 15 deletions(-)

diff --git a/xrandr.c b/xrandr.c
index 0adf375..c880cb3 100644
--- a/xrandr.c
+++ b/xrandr.c
@@ -140,6 +140,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*/
@@ -2160,6 +2161,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;
 
@@ -2251,6 +2253,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 ();
@@ -2918,11 +2924,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)
 	{
@@ -2935,28 +2943,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 (" (");
@@ -2980,7 +2998,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);
@@ -2989,7 +3007,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		||
@@ -3001,12 +3019,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)
@@ -3221,7 +3246,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,
diff --git a/xrandr.man b/xrandr.man
index 68991d7..e327709 100644
--- a/xrandr.man
+++ b/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"
-- 
1.7.2.3


More information about the xorg-devel mailing list