diff options
author | Peter Hutterer <peter.hutterer@who-t.net> | 2010-05-25 17:12:34 +1000 |
---|---|---|
committer | Keith Packard <keithp@keithp.com> | 2010-05-25 10:06:11 -0700 |
commit | 86303a338ad563d0b986a3c052104301c310c4ac (patch) | |
tree | 94527f3cb2379466dc061852f6fe8fd955a12244 /dix/inpututils.c | |
parent | bf78e11839f8278020b604672ff7c3d194232be9 (diff) |
dix: add helper functions to duplicate and free InputAttributes.
No special memory handling is used to give drivers the maximum flexibility
with the data. Drivers should be able to call realloc on the product string
if needed and perform similar operations.
Signed-off-by: Peter Hutterer <peter.hutterer@who-t.net>
Reviewed-by: Dan Nicholson <dbn.lists@gmail.com>
Signed-off-by: Keith Packard <keithp@keithp.com>
Diffstat (limited to 'dix/inpututils.c')
-rw-r--r-- | dix/inpututils.c | 79 |
1 files changed, 79 insertions, 0 deletions
diff --git a/dix/inpututils.c b/dix/inpututils.c index 8e7537250..df2ace0bb 100644 --- a/dix/inpututils.c +++ b/dix/inpututils.c @@ -331,3 +331,82 @@ int generate_modkeymap(ClientPtr client, DeviceIntPtr dev, return Success; } + +/** + * Duplicate the InputAttributes in the most obvious way. + * No special memory handling is used to give drivers the maximum + * flexibility with the data. Drivers should be able to call realloc on the + * product string if needed and perform similar operations. + */ +InputAttributes* +DuplicateInputAttributes(InputAttributes *attrs) +{ + InputAttributes *new_attr; + int ntags = 0; + char **tags, **new_tags; + + if (!attrs) + return NULL; + + if (!(new_attr = calloc(1, sizeof(InputAttributes)))) + goto unwind; + + if (attrs->product && !(new_attr->product = strdup(attrs->product))) + goto unwind; + if (attrs->vendor && !(new_attr->vendor = strdup(attrs->vendor))) + goto unwind; + if (attrs->device && !(new_attr->device = strdup(attrs->device))) + goto unwind; + + new_attr->flags = attrs->flags; + + if ((tags = attrs->tags)) + { + while(*tags++) + ntags++; + + new_attr->tags = calloc(ntags + 1, sizeof(char*)); + if (!new_attr->tags) + goto unwind; + + tags = attrs->tags; + new_tags = new_attr->tags; + + while(*tags) + { + *new_tags = strdup(*tags); + if (!*new_tags) + goto unwind; + + tags++; + new_tags++; + } + } + + return new_attr; + +unwind: + FreeInputAttributes(new_attr); + return NULL; +} + +void +FreeInputAttributes(InputAttributes *attrs) +{ + char **tags; + + if (!attrs) + return; + + free(attrs->product); + free(attrs->vendor); + free(attrs->device); + + if ((tags = attrs->tags)) + while(*tags) + free(*tags++); + + free(attrs->tags); + free(attrs); +} + |