[PATCH] include: add version_compare helper function
Peter Hutterer
peter.hutterer at who-t.net
Tue May 10 19:20:50 PDT 2011
Compare two version numbers in the major.minor form.
Switch the few users of manual version switching over to the new function.
Signed-off-by: Peter Hutterer <peter.hutterer at who-t.net>
Reviewed-by: Jamey Sharp <jamey at minilop.net>
---
Xi/xiqueryversion.c | 7 +----
include/misc.h | 18 ++++++++++++++
randr/rrdispatch.c | 8 +++---
test/Makefile.am | 3 +-
test/misc.c | 62 +++++++++++++++++++++++++++++++++++++++++++++++++++
xfixes/xfixes.c | 12 +++++-----
6 files changed, 94 insertions(+), 16 deletions(-)
create mode 100644 test/misc.c
diff --git a/Xi/xiqueryversion.c b/Xi/xiqueryversion.c
index ae63297..f647f98 100644
--- a/Xi/xiqueryversion.c
+++ b/Xi/xiqueryversion.c
@@ -58,7 +58,6 @@ ProcXIQueryVersion(ClientPtr client)
xXIQueryVersionReply rep;
XIClientPtr pXIClient;
int major, minor;
- unsigned int sversion, cversion;
REQUEST(xXIQueryVersionReq);
REQUEST_SIZE_MATCH(xXIQueryVersionReq);
@@ -72,10 +71,8 @@ ProcXIQueryVersion(ClientPtr client)
pXIClient = dixLookupPrivate(&client->devPrivates, XIClientPrivateKey);
- sversion = XIVersion.major_version * 1000 + XIVersion.minor_version;
- cversion = stuff->major_version * 1000 + stuff->minor_version;
-
- if (sversion > cversion)
+ if (version_compare(XIVersion.major_version, XIVersion.minor_version,
+ stuff->major_version, stuff->minor_version) > 0)
{
major = stuff->major_version;
minor = stuff->minor_version;
diff --git a/include/misc.h b/include/misc.h
index 803f5ba..bdcc8cc 100644
--- a/include/misc.h
+++ b/include/misc.h
@@ -223,6 +223,24 @@ pad_to_int32(const int bytes) {
extern char**
xstrtokenize(const char *str, const char* separators);
+/**
+ * Compare the two version numbers comprising of major.minor.
+ *
+ * @return A value less than 0 if a is less than b, 0 if a is equal to b,
+ * or a value greater than 0
+ */
+static inline int
+version_compare(uint16_t a_major, uint16_t a_minor,
+ uint16_t b_major, uint16_t b_minor)
+{
+ int a, b;
+
+ a = a_major << 16 | a_minor;
+ b = b_major << 16 | b_minor;
+
+ return (a - b);
+}
+
/* some macros to help swap requests, replies, and events */
#define LengthRestB(stuff) \
diff --git a/randr/rrdispatch.c b/randr/rrdispatch.c
index 2135504..d1c99c2 100644
--- a/randr/rrdispatch.c
+++ b/randr/rrdispatch.c
@@ -28,8 +28,8 @@ RRClientKnowsRates (ClientPtr pClient)
{
rrClientPriv(pClient);
- return (pRRClient->major_version > 1 ||
- (pRRClient->major_version == 1 && pRRClient->minor_version >= 1));
+ return version_compare(pRRClient->major_version, pRRClient->minor_version,
+ 1, 1) >= 0;
}
static int
@@ -47,8 +47,8 @@ ProcRRQueryVersion (ClientPtr client)
rep.length = 0;
rep.sequenceNumber = client->sequence;
- if ((stuff->majorVersion * 1000 + stuff->minorVersion) <
- (SERVER_RANDR_MAJOR_VERSION * 1000 + SERVER_RANDR_MINOR_VERSION))
+ if (version_compare(stuff->majorVersion, stuff->minorVersion,
+ SERVER_RANDR_MAJOR_VERSION, SERVER_RANDR_MINOR_VERSION) < 0)
{
rep.majorVersion = stuff->majorVersion;
rep.minorVersion = stuff->minorVersion;
diff --git a/test/Makefile.am b/test/Makefile.am
index fe9bc1f..b7ee070 100644
--- a/test/Makefile.am
+++ b/test/Makefile.am
@@ -1,7 +1,7 @@
if ENABLE_UNIT_TESTS
if HAVE_LD_WRAP
SUBDIRS= . xi2
-noinst_PROGRAMS = xkb input xtest list
+noinst_PROGRAMS = xkb input xtest list misc
check_LTLIBRARIES = libxservertest.la
TESTS=$(noinst_PROGRAMS)
@@ -18,6 +18,7 @@ xkb_LDADD=$(TEST_LDADD)
input_LDADD=$(TEST_LDADD)
xtest_LDADD=$(TEST_LDADD)
list_LDADD=$(TEST_LDADD)
+misc_LDADD=$(TEST_LDADD)
libxservertest_la_LIBADD = \
$(XSERVER_LIBS) \
diff --git a/test/misc.c b/test/misc.c
new file mode 100644
index 0000000..3d3b1a1
--- /dev/null
+++ b/test/misc.c
@@ -0,0 +1,62 @@
+/**
+ * Copyright © 2011 Red Hat, Inc.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the next
+ * paragraph) shall be included in all copies or substantial portions of the
+ * Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ */
+
+#ifdef HAVE_DIX_CONFIG_H
+#include <dix-config.h>
+#endif
+
+#include <stdint.h>
+#include "misc.h"
+
+static void dix_version_compare(void)
+{
+ int rc;
+
+ rc = version_compare(0, 0, 1, 0);
+ assert(rc < 0);
+ rc = version_compare(1, 0, 0, 0);
+ assert(rc > 0);
+ rc = version_compare(0, 0, 0, 0);
+ assert(rc == 0);
+ rc = version_compare(1, 0, 1, 0);
+ assert(rc == 0);
+ rc = version_compare(1, 0, 0, 9);
+ assert(rc > 0);
+ rc = version_compare(0, 9, 1, 0);
+ assert(rc < 0);
+ rc = version_compare(1, 0, 1, 9);
+ assert(rc < 0);
+ rc = version_compare(1, 9, 1, 0);
+ assert(rc > 0);
+ rc = version_compare(2, 0, 1, 9);
+ assert(rc > 0);
+ rc = version_compare(1, 9, 2, 0);
+ assert(rc < 0);
+}
+
+int main(int argc, char** argv)
+{
+ dix_version_compare();
+
+ return 0;
+}
diff --git a/xfixes/xfixes.c b/xfixes/xfixes.c
index e8c7bf1..54f0df3 100644
--- a/xfixes/xfixes.c
+++ b/xfixes/xfixes.c
@@ -72,17 +72,17 @@ ProcXFixesQueryVersion(ClientPtr client)
rep.type = X_Reply;
rep.length = 0;
rep.sequenceNumber = client->sequence;
- if (stuff->majorVersion < SERVER_XFIXES_MAJOR_VERSION) {
+
+ if (version_compare(stuff->majorVersion, stuff->minorVersion,
+ SERVER_XFIXES_MAJOR_VERSION, SERVER_XFIXES_MAJOR_VERSION) < 0)
+ {
rep.majorVersion = stuff->majorVersion;
rep.minorVersion = stuff->minorVersion;
} else {
rep.majorVersion = SERVER_XFIXES_MAJOR_VERSION;
- if (stuff->majorVersion == SERVER_XFIXES_MAJOR_VERSION &&
- stuff->minorVersion < SERVER_XFIXES_MINOR_VERSION)
- rep.minorVersion = stuff->minorVersion;
- else
- rep.minorVersion = SERVER_XFIXES_MINOR_VERSION;
+ rep.minorVersion = SERVER_XFIXES_MINOR_VERSION;
}
+
pXFixesClient->major_version = rep.majorVersion;
pXFixesClient->minor_version = rep.minorVersion;
if (client->swapped) {
--
1.7.4.4
More information about the xorg-devel
mailing list