summaryrefslogtreecommitdiff
path: root/dix
diff options
context:
space:
mode:
authorPeter Hutterer <peter.hutterer@who-t.net>2011-08-04 14:45:46 +1000
committerPeter Hutterer <peter.hutterer@who-t.net>2011-08-22 15:56:53 +1000
commit05284a03f9002b03a66ae355b34790ec02b726f0 (patch)
treefba09523738e384c129d455f9af9703dafbf98a2 /dix
parentfcafe825751bef99f4c0b36250ca6f15f127502f (diff)
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 <peter.hutterer@who-t.net> Reviewed-by: Dan Nicholson <dbn.lists@gmail.com>
Diffstat (limited to 'dix')
-rw-r--r--dix/inpututils.c141
1 files changed, 141 insertions, 0 deletions
diff --git a/dix/inpututils.c b/dix/inpututils.c
index 96320767d..7aeb1e516 100644
--- a/dix/inpututils.c
+++ b/dix/inpututils.c
@@ -598,3 +598,144 @@ void init_device_event(DeviceEvent *event, DeviceIntPtr dev, Time ms)
event->deviceid = dev->id;
event->sourceid = dev->id;
}
+
+/**
+ * Delete the element with the key from the list, freeing all memory
+ * associated with the element..
+ */
+static void
+input_option_free(InputOption *o)
+{
+ free(o->key);
+ free(o->value);
+ free(o);
+}
+
+/*
+ * Create a new InputOption with the key/value pair provided.
+ * If a list is provided, the new options is added to the list and the list
+ * is returned.
+ *
+ * If a new option is added to a list that already contains that option, the
+ * previous option is overwritten.
+ *
+ * @param list The list to add to.
+ * @param key Option key, will be copied.
+ * @param value Option value, will be copied.
+ *
+ * @return If list is not NULL, the list with the new option added. If list
+ * is NULL, a new option list with one element. On failure, NULL is
+ * returned.
+ */
+InputOption*
+input_option_new(InputOption* list, const char *key, const char *value)
+{
+ InputOption *opt = NULL;
+
+ if (!key)
+ return NULL;
+
+ if (list)
+ {
+ nt_list_for_each_entry(opt, list, next)
+ {
+ if (strcmp(input_option_get_key(opt), key) == 0)
+ {
+ input_option_set_value(opt, value);
+ return list;
+ }
+ }
+ }
+
+ opt = calloc(1, sizeof(InputOption));
+ if (!opt)
+ return NULL;
+
+ nt_list_init(opt, next);
+ input_option_set_key(opt, key);
+ input_option_set_value(opt, value);
+
+ if (list)
+ {
+ nt_list_append(opt, list, InputOption, next);
+ return list;
+ } else
+ return opt;
+}
+
+InputOption*
+input_option_free_element(InputOption *list, const char *key)
+{
+ InputOption *element;
+
+ nt_list_for_each_entry(element, list, next) {
+ if (strcmp(input_option_get_key(element), key) == 0) {
+ nt_list_del(element, list, InputOption, next);
+ input_option_free(element);
+ break;
+ }
+ }
+ return list;
+}
+
+/**
+ * Free the list pointed at by opt.
+ */
+void
+input_option_free_list(InputOption **opt)
+{
+ InputOption *element, *tmp;
+
+ nt_list_for_each_entry_safe(element, tmp, *opt, next) {
+ nt_list_del(element, *opt, InputOption, next);
+ input_option_free(element);
+ }
+ *opt = NULL;
+}
+
+
+/**
+ * Find the InputOption with the given option name.
+ *
+ * @return The InputOption or NULL if not present.
+ */
+InputOption*
+input_option_find(InputOption *list, const char *key)
+{
+ InputOption *element;
+
+ nt_list_for_each_entry(element, list, next) {
+ if (strcmp(input_option_get_key(element), key) == 0)
+ return element;
+ }
+
+ return NULL;
+}
+
+const char*
+input_option_get_key(const InputOption *opt)
+{
+ return opt->key;
+}
+
+const char*
+input_option_get_value(const InputOption *opt)
+{
+ return opt->value;
+}
+
+void
+input_option_set_key(InputOption *opt, const char *key)
+{
+ free(opt->key);
+ if (key)
+ opt->key = strdup(key);
+}
+
+void
+input_option_set_value(InputOption *opt, const char *value)
+{
+ free(opt->value);
+ if (value)
+ opt->value = strdup(value);
+}