=?n?q?=5BPATCH=20xrandr-utils=204/4=5D=20Refactor=20name=5Ft=20into=20XRUName?=

Bryce Harrington bryce at canonical.com
Wed Feb 15 16:10:43 PST 2012


Signed-off-by: Bryce Harrington <bryce at canonical.com>
---
 examples/xrandr.c                    |  202 ++++++++++++----------------------
 include/X11/extensions/XrandrUtils.h |   38 +++++++
 man/XrandrUtils.man                  |   15 +++
 src/Makefile.am                      |    3 +-
 src/names.c                          |   80 +++++++++++++
 5 files changed, 206 insertions(+), 132 deletions(-)
 create mode 100644 src/names.c

diff --git a/examples/xrandr.c b/examples/xrandr.c
index 7ddddb4..7eb50bf 100644
--- a/examples/xrandr.c
+++ b/examples/xrandr.c
@@ -186,21 +186,6 @@ typedef enum _changes {
     changes_primary = (1 << 12),
 } changes_t;
 
-typedef enum _name_kind {
-    name_none = 0,
-    name_string = (1 << 0),
-    name_xid = (1 << 1),
-    name_index = (1 << 2),
-    name_preferred = (1 << 3),
-} name_kind_t;
-
-typedef struct {
-    name_kind_t	    kind;
-    char    	    *string;
-    XID	    	    xid;
-    int		    index;
-} name_t;
-
 typedef struct _crtc crtc_t;
 typedef struct _output	output_t;
 typedef struct _transform transform_t;
@@ -215,7 +200,7 @@ struct _transform {
 };
 
 struct _crtc {
-    name_t	    crtc;
+    XRUName	    crtc;
     Bool	    changing;
     XRRCrtcInfo	    *crtc_info;
 
@@ -242,18 +227,18 @@ struct _output {
     
     output_prop_t   *props;
 
-    name_t	    output;
+    XRUName	    output;
     XRROutputInfo   *output_info;
     
-    name_t	    crtc;
+    XRUName	    crtc;
     crtc_t	    *crtc_info;
     crtc_t	    *current_crtc_info;
     
-    name_t	    mode;
+    XRUName	    mode;
     double	    refresh;
     XRRModeInfo	    *mode_info;
     
-    name_t	    addmode;
+    XRUName	    addmode;
 
     XRURelation	    relation;
     char	    *relative_to;
@@ -289,8 +274,8 @@ struct _umode {
     
     umode_action_t  action;
     XRRModeInfo	    mode;
-    name_t	    output;
-    name_t	    name;
+    XRUName	    output;
+    XRUName	    name;
 };
 
 static char *connection[3] = {
@@ -326,6 +311,23 @@ static int	minWidth, maxWidth, minHeight, maxHeight;
 static Bool    	has_1_2 = False;
 static Bool    	has_1_3 = False;
 
+static void
+set_name (XRUName *name, char *string, XRUNameKind valid)
+{
+    unsigned int xid; /* don't make it XID (which is unsigned long):
+                         scanf() takes unsigned int */
+    int index;
+
+    if ((valid & name_xid) && sscanf (string, "0x%x", &xid) == 1)
+        XRUSetNameXid (name, xid);
+    else if ((valid & name_index) && sscanf (string, "%d", &index) == 1)
+        XRUSetNameIndex (name, index);
+    else if (valid & name_string)
+        XRUSetNameString (name, string);
+    else
+	usage ();
+}
+
 static int
 mode_height (XRRModeInfo *mode_info, Rotation rotation)
 {
@@ -459,68 +461,6 @@ mode_hsync (XRRModeInfo *mode_info)
 }
 
 static void
-init_name (name_t *name)
-{
-    name->kind = name_none;
-}
-
-static void
-set_name_string (name_t *name, char *string)
-{
-    name->kind |= name_string;
-    name->string = string;
-}
-
-static void
-set_name_xid (name_t *name, XID xid)
-{
-    name->kind |= name_xid;
-    name->xid = xid;
-}
-
-static void
-set_name_index (name_t *name, int index)
-{
-    name->kind |= name_index;
-    name->index = index;
-}
-
-static void
-set_name_preferred (name_t *name)
-{
-    name->kind |= name_preferred;
-}
-
-static void
-set_name_all (name_t *name, name_t *old)
-{
-    if (old->kind & name_xid)
-	name->xid = old->xid;
-    if (old->kind & name_string)
-	name->string = old->string;
-    if (old->kind & name_index)
-	name->index = old->index;
-    name->kind |= old->kind;
-}
-
-static void
-set_name (name_t *name, char *string, name_kind_t valid)
-{
-    unsigned int xid; /* don't make it XID (which is unsigned long):
-			 scanf() takes unsigned int */
-    int index;
-
-    if ((valid & name_xid) && sscanf (string, "0x%x", &xid) == 1)
-	set_name_xid (name, xid);
-    else if ((valid & name_index) && sscanf (string, "%d", &index) == 1)
-	set_name_index (name, index);
-    else if (valid & name_string)
-	set_name_string (name, string);
-    else
-	usage ();
-}
-
-static void
 init_transform (transform_t *transform)
 {
     int x;
@@ -583,14 +523,14 @@ add_output (void)
 }
 
 static output_t *
-find_output (name_t *name)
+find_output (XRUName *name)
 {
     output_t *output;
 
     for (output = outputs; output; output = output->next)
     {
-	name_kind_t common = name->kind & output->output.kind;
-	
+	XRUNameKind common = name->kind & output->output.kind;
+
 	if ((common & name_xid) && name->xid == output->output.xid)
 	    break;
 	if ((common & name_string) && !strcmp (name->string, output->output.string))
@@ -604,33 +544,33 @@ find_output (name_t *name)
 static output_t *
 find_output_by_xid (RROutput output)
 {
-    name_t  output_name;
+    XRUName  output_name;
 
-    init_name (&output_name);
-    set_name_xid (&output_name, output);
+    XRUInitName (&output_name);
+    XRUSetNameXid (&output_name, output);
     return find_output (&output_name);
 }
 
 static output_t *
 find_output_by_name (char *name)
 {
-    name_t  output_name;
+    XRUName  output_name;
 
-    init_name (&output_name);
-    set_name_string (&output_name, name);
+    XRUInitName (&output_name);
+    XRUSetNameString (&output_name, name);
     return find_output (&output_name);
 }
 
 static crtc_t *
-find_crtc (name_t *name)
+find_crtc (XRUName *name)
 {
     int	    c;
     crtc_t  *crtc = NULL;
 
     for (c = 0; c < num_crtcs; c++)
     {
-	name_kind_t common;
-	
+	XRUNameKind common;
+
 	crtc = &crtcs[c];
 	common = name->kind & crtc->crtc.kind;
 	
@@ -648,15 +588,15 @@ find_crtc (name_t *name)
 static crtc_t *
 find_crtc_by_xid (RRCrtc crtc)
 {
-    name_t  crtc_name;
+    XRUName  crtc_name;
 
-    init_name (&crtc_name);
-    set_name_xid (&crtc_name, crtc);
+    XRUInitName (&crtc_name);
+    XRUSetNameXid (&crtc_name, crtc);
     return find_crtc (&crtc_name);
 }
 
 static XRRModeInfo *
-find_mode (name_t *name, double refresh)
+find_mode (XRUName *name, double refresh)
 {
     int		m;
     XRRModeInfo	*best = NULL;
@@ -691,10 +631,10 @@ find_mode (name_t *name, double refresh)
 static XRRModeInfo *
 find_mode_by_xid (RRMode mode)
 {
-    name_t  mode_name;
+    XRUName  mode_name;
 
-    init_name (&mode_name);
-    set_name_xid (&mode_name, mode);
+    XRUInitName (&mode_name);
+    XRUSetNameXid (&mode_name, mode);
     return find_mode (&mode_name, 0);
 }
 
@@ -702,16 +642,16 @@ find_mode_by_xid (RRMode mode)
 static XRRModeInfo *
 find_mode_by_name (char *name)
 {
-    name_t  mode_name;
-    init_name (&mode_name);
-    set_name_string (&mode_name, name);
+    XRUName  mode_name;
+    XRUInitName (&mode_name);
+    XRUSetNameString (&mode_name, name);
     return find_mode (&mode_name, 0);
 }
 #endif
 
 static
 XRRModeInfo *
-find_mode_for_output (output_t *output, name_t *name)
+find_mode_for_output (output_t *output, XRUName *name)
 {
     XRROutputInfo   *output_info = output->output_info;
     int		    m;
@@ -986,14 +926,14 @@ set_output_info (output_t *output, RROutput xid, XRROutputInfo *output_info)
     
     /* set output name and info */
     if (!(output->output.kind & name_xid))
-	set_name_xid (&output->output, xid);
+	XRUSetNameXid (&output->output, xid);
     if (!(output->output.kind & name_string))
-	set_name_string (&output->output, output_info->name);
+	XRUSetNameString (&output->output, output_info->name);
     output->output_info = output_info;
     
     /* set crtc name and info */
     if (!(output->changes & changes_crtc))
-	set_name_xid (&output->crtc, output_info->crtc);
+	XRUSetNameXid (&output->crtc, output_info->crtc);
     
     if (output->crtc.kind == name_xid && output->crtc.xid == None)
 	output->crtc_info = NULL;
@@ -1020,11 +960,11 @@ set_output_info (output_t *output, RROutput xid, XRROutputInfo *output_info)
 	if (output_info->crtc)
 	    crtc = find_crtc_by_xid(output_info->crtc);
 	if (crtc && crtc->crtc_info)
-	    set_name_xid (&output->mode, crtc->crtc_info->mode);
+	    XRUSetNameXid (&output->mode, crtc->crtc_info->mode);
 	else if (output->crtc_info)
-	    set_name_xid (&output->mode, output->crtc_info->crtc_info->mode);
+	    XRUSetNameXid (&output->mode, output->crtc_info->crtc_info->mode);
 	else
-	    set_name_xid (&output->mode, None);
+	    XRUSetNameXid (&output->mode, None);
 	if (output->mode.xid)
 	{
 	    output->mode_info = find_mode_by_xid (output->mode.xid);
@@ -1154,8 +1094,8 @@ get_crtcs (void)
 	    }
 	}
 
-	set_name_xid (&crtcs[c].crtc, res->crtcs[c]);
-	set_name_index (&crtcs[c].crtc, c);
+	XRUSetNameXid (&crtcs[c].crtc, res->crtcs[c]);
+	XRUSetNameIndex (&crtcs[c].crtc, c);
 	if (!crtc_info) fatal ("could not get crtc 0x%x information\n", res->crtcs[c]);
 	crtcs[c].crtc_info = crtc_info;
 	crtcs[c].panning_info = panning_info;
@@ -1579,16 +1519,16 @@ get_outputs (void)
     {
 	XRROutputInfo	*output_info = XRRGetOutputInfo (dpy, res, res->outputs[o]);
 	output_t	*output;
-	name_t		output_name;
+	XRUName		output_name;
 	if (!output_info) fatal ("could not get output 0x%x information\n", res->outputs[o]);
-	set_name_xid (&output_name, res->outputs[o]);
-	set_name_index (&output_name, o);
-	set_name_string (&output_name, output_info->name);
+	XRUSetNameXid (&output_name, res->outputs[o]);
+	XRUSetNameIndex (&output_name, o);
+	XRUSetNameString (&output_name, output_info->name);
 	output = find_output (&output_name);
 	if (!output)
 	{
 	    output = add_output ();
-	    set_name_all (&output->output, &output_name);
+	    XRUSetNameAll (&output->output, &output_name);
 	    /*
 	     * When global --automatic mode is set, turn on connected but off
 	     * outputs, turn off disconnected but on outputs
@@ -1625,15 +1565,15 @@ get_outputs (void)
 	    case RR_UnknownConnection:
 		if ((!(output->changes & changes_mode)))
 		{
-		    set_name_preferred (&output->mode);
+		    XRUSetNamePreferred (&output->mode);
 		    output->changes |= changes_mode;
 		}
 		break;
 	    case RR_Disconnected:
 		if ((!(output->changes & changes_mode)))
 		{
-		    set_name_xid (&output->mode, None);
-		    set_name_xid (&output->crtc, None);
+		    XRUSetNameXid (&output->mode, None);
+		    XRUSetNameXid (&output->crtc, None);
 		    output->changes |= changes_mode;
 		    output->changes |= changes_crtc;
 		}
@@ -1782,14 +1722,14 @@ set_positions (void)
 	for (output = outputs; output; output = output->next)
 	{
 	    output_t    *relation;
-	    name_t	relation_name;
+	    XRUName	relation_name;
 
 	    if (!(output->changes & changes_relation)) continue;
 	    
 	    if (output->mode_info == NULL) continue;
 
-	    init_name (&relation_name);
-	    set_name_string (&relation_name, output->relative_to);
+	    XRUInitName (&relation_name);
+	    XRUSetNameString (&relation_name, output->relative_to);
 	    relation = find_output (&relation_name);
 	    if (!relation) fatal ("cannot find output \"%s\"\n", output->relative_to);
 	    
@@ -2230,9 +2170,9 @@ main (int argc, char **argv)
 	    output = find_output_by_name (argv[i]);
 	    if (!output) {
 		output = add_output ();
-		set_name (&output->output, argv[i], name_string|name_xid);
+	        set_name (&output->output, argv[i], name_string|name_xid);
 	    }
-	    
+
 	    setit_1_2 = True;
 	    action_requested = True;
 	    continue;
@@ -2253,7 +2193,7 @@ main (int argc, char **argv)
 	}
 	if (!strcmp ("--preferred", argv[i])) {
 	    if (!output) usage();
-	    set_name_preferred (&output->mode);
+	    XRUSetNamePreferred (&output->mode);
 	    output->changes |= changes_mode;
 	    continue;
 	}
@@ -2452,8 +2392,8 @@ main (int argc, char **argv)
 	}
 	if (!strcmp ("--off", argv[i])) {
 	    if (!output) usage();
-	    set_name_xid (&output->mode, None);
-	    set_name_xid (&output->crtc, None);
+	    XRUSetNameXid (&output->mode, None);
+	    XRUSetNameXid (&output->crtc, None);
 	    output->changes |= changes_mode;
 	    continue;
 	}
diff --git a/include/X11/extensions/XrandrUtils.h b/include/X11/extensions/XrandrUtils.h
index 7b33769..5330358 100644
--- a/include/X11/extensions/XrandrUtils.h
+++ b/include/X11/extensions/XrandrUtils.h
@@ -28,6 +28,7 @@
 #ifndef _XRANDR_UTILS_H_
 #define _XRANDR_UTILS_H_
 
+#include <X11/Xdefs.h>
 #include <X11/extensions/randr.h>
 
 typedef enum {
@@ -46,6 +47,23 @@ typedef struct {
   int     x, y;
 } XRUPoint;
 
+typedef enum {
+  name_none = 0,
+  name_string = (1 << 0),
+  name_xid = (1 << 1),
+  name_index = (1 << 2),
+  name_preferred = (1 << 3),
+} XRUNameKind;
+
+typedef struct {
+  XRUNameKind     kind;
+  char            *string;
+  XID             xid;
+  int             index;
+} XRUName;
+
+
+/* Geometry */
 const char *
 XRURotationName(Rotation rotation);
 
@@ -64,4 +82,24 @@ XRUGetReflectionIndex(char * reflection_name);
 Rotation
 XRUGetReflection(int dirind);
 
+
+/* Names */
+void
+XRUInitName (XRUName *name);
+
+void
+XRUSetNameString (XRUName *name, char *string);
+
+void
+XRUSetNameXid (XRUName *name, XID xid);
+
+void
+XRUSetNameIndex (XRUName *name, int index);
+
+void
+XRUSetNamePreferred (XRUName *name);
+
+void
+XRUSetNameAll (XRUName *name, XRUName *old);
+
 #endif /* _XRANDR_UTILS_H_ */
diff --git a/man/XrandrUtils.man b/man/XrandrUtils.man
index 7acf0e9..60144f8 100644
--- a/man/XrandrUtils.man
+++ b/man/XrandrUtils.man
@@ -34,6 +34,21 @@ typedef struct {
   int     x, y;
 } XRUPoint;
 
+typedef enum {
+  name_none = 0,
+  name_string = (1 << 0),
+  name_xid = (1 << 1),
+  name_index = (1 << 2),
+  name_preferred = (1 << 3),
+} XRUNameKind;
+
+typedef struct {
+  XRUNameKind     kind;
+  char            *string;
+  XID             xid;
+  int             index;
+} XRUName;
+
 /**
  * Return string name for the given rotation,
  * or "invalid rotation" on error.
diff --git a/src/Makefile.am b/src/Makefile.am
index cbc5600..c7f1503 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -1,7 +1,8 @@
 lib_LTLIBRARIES = libXrandrUtils.la
 
 libXrandrUtils_la_SOURCES = 	\
-	XrandrUtils.c
+	XrandrUtils.c	\
+	names.c
 
 libXrandrUtils_la_LIBADD = @RANDR_UTILS_LIBS@
 
diff --git a/src/names.c b/src/names.c
new file mode 100644
index 0000000..b8f7ecb
--- /dev/null
+++ b/src/names.c
@@ -0,0 +1,80 @@
+/*
+ * Copyright © 2001 Keith Packard, member of The XFree86 Project, Inc.
+ * Copyright © 2002 Hewlett Packard Company, Inc.
+ * Copyright © 2006 Intel Corporation
+ *
+ * Permission to use, copy, modify, distribute, and sell this software and its
+ * documentation for any purpose is hereby granted without fee, provided that
+ * the above copyright notice appear in all copies and that both that copyright
+ * notice and this permission notice appear in supporting documentation, and
+ * that the name of the copyright holders not be used in advertising or
+ * publicity pertaining to distribution of the software without specific,
+ * written prior permission.  The copyright holders make no representations
+ * about the suitability of this software for any purpose.  It is provided "as
+ * is" without express or implied warranty.
+ *
+ * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
+ * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
+ * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
+ * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
+ * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
+ * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
+ * OF THIS SOFTWARE.
+ *
+ * Thanks to Jim Gettys who wrote most of the client side code,
+ * and part of the server code for randr.
+ */
+
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#include <string.h>
+#include <X11/extensions/randr.h>
+
+#include "XrandrUtils.h"
+
+void
+XRUInitName (XRUName *name)
+{
+    name->kind = name_none;
+}
+
+void
+XRUSetNameString (XRUName *name, char *string)
+{
+    name->kind |= name_string;
+    name->string = string;
+}
+
+void
+XRUSetNameXid (XRUName *name, XID xid)
+{
+    name->kind |= name_xid;
+    name->xid = xid;
+}
+
+void
+XRUSetNameIndex (XRUName *name, int index)
+{
+    name->kind |= name_index;
+    name->index = index;
+}
+
+void
+XRUSetNamePreferred (XRUName *name)
+{
+    name->kind |= name_preferred;
+}
+
+void
+XRUSetNameAll (XRUName *name, XRUName *old)
+{
+    if (old->kind & name_xid)
+        name->xid = old->xid;
+    if (old->kind & name_string)
+        name->string = old->string;
+    if (old->kind & name_index)
+        name->index = old->index;
+    name->kind |= old->kind;
+}
-- 
1.7.9



More information about the xorg-devel mailing list