Skip to content

Commit

Permalink
include: add version_compare helper function
Browse files Browse the repository at this point in the history
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@who-t.net>
Reviewed-by: Jamey Sharp <jamey@minilop.net>
  • Loading branch information
whot committed May 12, 2011
1 parent c4f9c3a commit ffd4874
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 16 deletions.
7 changes: 2 additions & 5 deletions Xi/xiqueryversion.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ ProcXIQueryVersion(ClientPtr client)
xXIQueryVersionReply rep;
XIClientPtr pXIClient;
int major, minor;
unsigned int sversion, cversion;

REQUEST(xXIQueryVersionReq);
REQUEST_SIZE_MATCH(xXIQueryVersionReq);
Expand All @@ -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;
Expand Down
18 changes: 18 additions & 0 deletions include/misc.h
Original file line number Diff line number Diff line change
Expand Up @@ -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) \
Expand Down
8 changes: 4 additions & 4 deletions randr/rrdispatch.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
Expand Down
3 changes: 2 additions & 1 deletion test/Makefile.am
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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) \
Expand Down
62 changes: 62 additions & 0 deletions test/misc.c
Original file line number Diff line number Diff line change
@@ -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;
}
12 changes: 6 additions & 6 deletions xfixes/xfixes.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit ffd4874

Please sign in to comment.