From 05284a03f9002b03a66ae355b34790ec02b726f0 Mon Sep 17 00:00:00 2001 From: Peter Hutterer Date: Thu, 4 Aug 2011 14:45:46 +1000 Subject: input: make InputOption opaque, provide interface functions. InputOptions is not switched to use struct list for a future patch to unify it with the XF86OptionRec. Signed-off-by: Peter Hutterer Reviewed-by: Dan Nicholson --- config/config-backends.h | 2 +- config/config.c | 22 -------------------- config/dbus.c | 52 +++++++++++++----------------------------------- config/hal.c | 36 +++++++++++++++------------------ config/udev.c | 35 ++++++++++++++------------------ 5 files changed, 46 insertions(+), 101 deletions(-) (limited to 'config') diff --git a/config/config-backends.h b/config/config-backends.h index 0d36d7208..35ab8a044 100644 --- a/config/config-backends.h +++ b/config/config-backends.h @@ -27,10 +27,10 @@ #include #endif #include "input.h" +#include "list.h" void remove_devices(const char *backend, const char *config_info); BOOL device_is_duplicate(const char *config_info); -InputOption* add_option(InputOption **options, const char *key, const char *value); #ifdef CONFIG_UDEV int config_udev_init(void); diff --git a/config/config.c b/config/config.c index af8f4f9b2..9c28785c6 100644 --- a/config/config.c +++ b/config/config.c @@ -122,25 +122,3 @@ device_is_duplicate(const char *config_info) return FALSE; } -/** - * Allocate a new option and append to the list. - * - * @return A pointer to the newly allocated InputOption struct. - */ -InputOption* -add_option(InputOption **options, const char *key, const char *value) -{ - if (!value || *value == '\0') - return NULL; - - for (; *options; options = &(*options)->next) - ; - *options = calloc(sizeof(**options), 1); - if (!*options) /* Yeesh. */ - return NULL; - (*options)->key = strdup(key); - (*options)->value = strdup(value); - (*options)->next = NULL; - - return *options; -} diff --git a/config/dbus.c b/config/dbus.c index 41eca998a..f0fc5686e 100644 --- a/config/dbus.c +++ b/config/dbus.c @@ -68,8 +68,7 @@ static int add_device(DBusMessage *message, DBusMessage *reply, DBusError *error) { DBusMessageIter iter, reply_iter, subiter; - InputOption *tmpo = NULL, *options = NULL; - char *tmp = NULL; + InputOption *input_options = NULL; int ret, err; DeviceIntPtr dev = NULL; @@ -80,7 +79,8 @@ add_device(DBusMessage *message, DBusMessage *reply, DBusError *error) MALFORMED_MESSAGE(); } - if (!add_option(&options, "_source", "client/dbus")) { + input_options = input_option_new(input_options, "_source", "client/dbus"); + if (!input_options) { ErrorF("[config/dbus] couldn't allocate first key/value pair\n"); ret = BadAlloc; goto unwind; @@ -88,36 +88,22 @@ add_device(DBusMessage *message, DBusMessage *reply, DBusError *error) /* signature should be [ss][ss]... */ while (dbus_message_iter_get_arg_type(&iter) == DBUS_TYPE_ARRAY) { - tmpo = calloc(sizeof(*tmpo), 1); - if (!tmpo) { - ErrorF("[config/dbus] couldn't allocate option\n"); - ret = BadAlloc; - goto unwind; - } - tmpo->next = options; - options = tmpo; - + char *key, *value; dbus_message_iter_recurse(&iter, &subiter); if (dbus_message_iter_get_arg_type(&subiter) != DBUS_TYPE_STRING) MALFORMED_MESSAGE(); - dbus_message_iter_get_basic(&subiter, &tmp); - if (!tmp) + dbus_message_iter_get_basic(&subiter, &key); + if (!key) MALFORMED_MESSAGE(); /* The _ prefix refers to internal settings, and may not be given by * the client. */ - if (tmp[0] == '_') { + if (key[0] == '_') { ErrorF("[config/dbus] attempted subterfuge: option name %s given\n", - tmp); + key); MALFORMED_MESSAGE(); } - options->key = strdup(tmp); - if (!options->key) { - ErrorF("[config/dbus] couldn't duplicate key!\n"); - ret = BadAlloc; - goto unwind; - } if (!dbus_message_iter_has_next(&subiter)) MALFORMED_MESSAGE(); @@ -125,20 +111,16 @@ add_device(DBusMessage *message, DBusMessage *reply, DBusError *error) if (dbus_message_iter_get_arg_type(&subiter) != DBUS_TYPE_STRING) MALFORMED_MESSAGE(); - dbus_message_iter_get_basic(&subiter, &tmp); - if (!tmp) + dbus_message_iter_get_basic(&subiter, &value); + if (!value) MALFORMED_MESSAGE(); - options->value = strdup(tmp); - if (!options->value) { - ErrorF("[config/dbus] couldn't duplicate option!\n"); - ret = BadAlloc; - goto unwind; - } + + input_options = input_option_new(input_options, key, value); dbus_message_iter_next(&iter); } - ret = NewInputDeviceRequest(options, NULL, &dev); + ret = NewInputDeviceRequest(input_options, NULL, &dev); if (ret != Success) { DebugF("[config/dbus] NewInputDeviceRequest failed\n"); goto unwind; @@ -172,13 +154,7 @@ unwind: dbus_message_iter_append_basic(&reply_iter, DBUS_TYPE_INT32, &err); } - while (options) { - tmpo = options; - options = options->next; - free(tmpo->key); - free(tmpo->value); - free(tmpo); - } + input_option_free_list(&input_options); return ret; } diff --git a/config/hal.c b/config/hal.c index 0b2d7d00c..aa234ebb4 100644 --- a/config/hal.c +++ b/config/hal.c @@ -128,7 +128,7 @@ device_added(LibHalContext *hal_ctx, const char *udi) { char *path = NULL, *driver = NULL, *name = NULL, *config_info = NULL; char *hal_tags, *parent; - InputOption *options = NULL, *tmpo = NULL; + InputOption *input_options = NULL; InputAttributes attrs = {0}; DeviceIntPtr dev = NULL; DBusError error; @@ -205,18 +205,19 @@ device_added(LibHalContext *hal_ctx, const char *udi) free(parent); } - if (!add_option(&options, "_source", "server/hal")) { + input_options = input_option_new(NULL, "_source", "server/hal"); + if (!input_options) { LogMessage(X_ERROR, "config/hal: couldn't allocate first key/value pair\n"); goto unwind; } /* most drivers use device.. not path. evdev uses both however, but the * path version isn't documented apparently. support both for now. */ - add_option(&options, "path", path); - add_option(&options, "device", path); + input_options = input_option_new(input_options, "path", path); + input_options = input_option_new(input_options, "device", path); - add_option(&options, "driver", driver); - add_option(&options, "name", name); + input_options = input_option_new(input_options, "driver", driver); + input_options = input_option_new(input_options, "name", name); if (asprintf (&config_info, "hal:%s", udi) == -1) { config_info = NULL; @@ -290,7 +291,7 @@ device_added(LibHalContext *hal_ctx, const char *udi) } else { /* all others */ - add_option(&options, psi_key + sizeof(LIBHAL_PROP_KEY)-1, tmp_val); + input_options = input_option_new(input_options, psi_key + sizeof(LIBHAL_PROP_KEY)-1, tmp_val); free(tmp_val); } } else @@ -358,20 +359,20 @@ device_added(LibHalContext *hal_ctx, const char *udi) /* Now add xkb options */ if (xkb_opts.layout) - add_option(&options, "xkb_layout", xkb_opts.layout); + input_options = input_option_new(input_options, "xkb_layout", xkb_opts.layout); if (xkb_opts.rules) - add_option(&options, "xkb_rules", xkb_opts.rules); + input_options = input_option_new(input_options, "xkb_rules", xkb_opts.rules); if (xkb_opts.variant) - add_option(&options, "xkb_variant", xkb_opts.variant); + input_options = input_option_new(input_options, "xkb_variant", xkb_opts.variant); if (xkb_opts.model) - add_option(&options, "xkb_model", xkb_opts.model); + input_options = input_option_new(input_options, "xkb_model", xkb_opts.model); if (xkb_opts.options) - add_option(&options, "xkb_options", xkb_opts.options); - add_option(&options, "config_info", config_info); + input_options = input_option_new(input_options, "xkb_options", xkb_opts.options); + input_options = input_option_new(input_options, "config_info", config_info); /* this isn't an error, but how else do you output something that the user can see? */ LogMessage(X_INFO, "config/hal: Adding input device %s\n", name); - if ((rc = NewInputDeviceRequest(options, &attrs, &dev)) != Success) { + if ((rc = NewInputDeviceRequest(input_options, &attrs, &dev)) != Success) { LogMessage(X_ERROR, "config/hal: NewInputDeviceRequest failed (%d)\n", rc); dev = NULL; goto unwind; @@ -384,12 +385,7 @@ unwind: free(driver); free(name); free(config_info); - while ((tmpo = options)) { - options = tmpo->next; - free(tmpo->key); /* NULL if dev != NULL */ - free(tmpo->value); /* NULL if dev != NULL */ - free(tmpo); - } + input_option_free_list(&input_options); free(attrs.product); free(attrs.vendor); diff --git a/config/udev.c b/config/udev.c index 1f431c1d3..1ba0d500d 100644 --- a/config/udev.c +++ b/config/udev.c @@ -60,7 +60,7 @@ device_added(struct udev_device *udev_device) const char *syspath; const char *tags_prop; const char *key, *value, *tmp; - InputOption *options = NULL, *tmpo; + InputOption *input_options; InputAttributes attrs = {}; DeviceIntPtr dev = NULL; struct udev_list_entry *set, *entry; @@ -93,8 +93,9 @@ device_added(struct udev_device *udev_device) return; } - if (!add_option(&options, "_source", "server/udev")) - goto unwind; + input_options = input_option_new(NULL, "_source", "server/udev"); + if (!input_options) + return; parent = udev_device_get_parent(udev_device); if (parent) { @@ -127,10 +128,9 @@ device_added(struct udev_device *udev_device) name = "(unnamed)"; else attrs.product = strdup(name); - add_option(&options, "name", name); - - add_option(&options, "path", path); - add_option(&options, "device", path); + input_options = input_option_new(input_options, "name", name); + input_options = input_option_new(input_options, "path", path); + input_options = input_option_new(input_options, "device", path); if (path) attrs.device = strdup(path); @@ -160,15 +160,15 @@ device_added(struct udev_device *udev_device) LOG_PROPERTY(path, key, value); tmp = key + sizeof(UDEV_XKB_PROP_KEY) - 1; if (!strcasecmp(tmp, "rules")) - add_option(&options, "xkb_rules", value); + input_options = input_option_new(input_options, "xkb_rules", value); else if (!strcasecmp(tmp, "layout")) - add_option(&options, "xkb_layout", value); + input_options = input_option_new(input_options, "xkb_layout", value); else if (!strcasecmp(tmp, "variant")) - add_option(&options, "xkb_variant", value); + input_options = input_option_new(input_options, "xkb_variant", value); else if (!strcasecmp(tmp, "model")) - add_option(&options, "xkb_model", value); + input_options = input_option_new(input_options, "xkb_model", value); else if (!strcasecmp(tmp, "options")) - add_option(&options, "xkb_options", value); + input_options = input_option_new(input_options, "xkb_options", value); } else if (!strcmp(key, "ID_VENDOR")) { LOG_PROPERTY(path, key, value); attrs.vendor = strdup(value); @@ -193,22 +193,17 @@ device_added(struct udev_device *udev_device) } } - add_option(&options, "config_info", config_info); + input_options = input_option_new(input_options, "config_info", config_info); LogMessage(X_INFO, "config/udev: Adding input device %s (%s)\n", name, path); - rc = NewInputDeviceRequest(options, &attrs, &dev); + rc = NewInputDeviceRequest(input_options, &attrs, &dev); if (rc != Success) goto unwind; unwind: free(config_info); - while ((tmpo = options)) { - options = tmpo->next; - free(tmpo->key); /* NULL if dev != NULL */ - free(tmpo->value); /* NULL if dev != NULL */ - free(tmpo); - } + input_option_free_list(&input_options); free(attrs.usb_id); free(attrs.pnp_id); -- cgit v1.2.3