config.c revision 6747b715
1/*
2 * Copyright © 2006-2007 Daniel Stone
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 *
23 * Author: Daniel Stone <daniel@fooishbar.org>
24 */
25
26#ifdef HAVE_DIX_CONFIG_H
27#include <dix-config.h>
28#endif
29
30#include "os.h"
31#include "inputstr.h"
32#include "hotplug.h"
33#include "config-backends.h"
34
35void
36config_init(void)
37{
38#ifdef CONFIG_UDEV
39    if (!config_udev_init())
40        ErrorF("[config] failed to initialise udev\n");
41#elif defined(CONFIG_NEED_DBUS)
42    if (config_dbus_core_init()) {
43# ifdef CONFIG_DBUS_API
44       if (!config_dbus_init())
45	    ErrorF("[config] failed to initialise D-Bus API\n");
46# endif
47# ifdef CONFIG_HAL
48        if (!config_hal_init())
49            ErrorF("[config] failed to initialise HAL\n");
50# endif
51    }
52    else {
53	ErrorF("[config] failed to initialise D-Bus core\n");
54    }
55#endif
56}
57
58void
59config_fini(void)
60{
61#if defined(CONFIG_UDEV)
62    config_udev_fini();
63#elif defined(CONFIG_NEED_DBUS)
64# ifdef CONFIG_HAL
65    config_hal_fini();
66# endif
67# ifdef CONFIG_DBUS_API
68    config_dbus_fini();
69# endif
70    config_dbus_core_fini();
71#endif
72}
73
74static void
75remove_device(const char *backend, DeviceIntPtr dev)
76{
77    /* this only gets called for devices that have already been added */
78    LogMessage(X_INFO, "config/%s: removing device %s\n", backend, dev->name);
79
80    /* Call PIE here so we don't try to dereference a device that's
81     * already been removed. */
82    OsBlockSignals();
83    ProcessInputEvents();
84    DeleteInputDeviceRequest(dev);
85    OsReleaseSignals();
86}
87
88void
89remove_devices(const char *backend, const char *config_info)
90{
91    DeviceIntPtr dev, next;
92
93    for (dev = inputInfo.devices; dev; dev = next) {
94        next = dev->next;
95        if (dev->config_info && strcmp(dev->config_info, config_info) == 0)
96            remove_device(backend, dev);
97    }
98    for (dev = inputInfo.off_devices; dev; dev = next) {
99        next = dev->next;
100        if (dev->config_info && strcmp(dev->config_info, config_info) == 0)
101            remove_device(backend, dev);
102    }
103}
104
105BOOL
106device_is_duplicate(const char *config_info)
107{
108    DeviceIntPtr dev;
109
110    for (dev = inputInfo.devices; dev; dev = dev->next)
111    {
112        if (dev->config_info && (strcmp(dev->config_info, config_info) == 0))
113            return TRUE;
114    }
115
116    for (dev = inputInfo.off_devices; dev; dev = dev->next)
117    {
118        if (dev->config_info && (strcmp(dev->config_info, config_info) == 0))
119            return TRUE;
120    }
121
122    return FALSE;
123}
124
125void
126add_option(InputOption **options, const char *key, const char *value)
127{
128    if (!value || *value == '\0')
129        return;
130
131    for (; *options; options = &(*options)->next)
132        ;
133    *options = calloc(sizeof(**options), 1);
134    if (!*options) /* Yeesh. */
135        return;
136    (*options)->key = strdup(key);
137    (*options)->value = strdup(value);
138    (*options)->next = NULL;
139}
140