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 <unistd.h>
31#include "os.h"
32#include "inputstr.h"
33#include "hotplug.h"
34#include "config-backends.h"
35#include "systemd-logind.h"
36
37void
38config_pre_init(void)
39{
40#ifdef CONFIG_UDEV
41    if (!config_udev_pre_init())
42        ErrorF("[config] failed to pre-init udev\n");
43#endif
44}
45
46void
47config_init(void)
48{
49#ifdef CONFIG_UDEV
50    if (!config_udev_init())
51        ErrorF("[config] failed to initialise udev\n");
52#elif defined(CONFIG_HAL)
53    if (!config_hal_init())
54        ErrorF("[config] failed to initialise HAL\n");
55#elif defined(CONFIG_WSCONS)
56    if (!config_wscons_init())
57        ErrorF("[config] failed to initialise wscons\n");
58#endif
59}
60
61void
62config_fini(void)
63{
64#if defined(CONFIG_UDEV)
65    config_udev_fini();
66#elif defined(CONFIG_HAL)
67    config_hal_fini();
68#elif defined(CONFIG_WSCONS)
69    config_wscons_fini();
70#endif
71}
72
73void
74config_odev_probe(config_odev_probe_proc_ptr probe_callback)
75{
76#if defined(CONFIG_UDEV_KMS)
77    config_udev_odev_probe(probe_callback);
78#endif
79}
80
81static void
82remove_device(const char *backend, DeviceIntPtr dev)
83{
84    /* this only gets called for devices that have already been added */
85    LogMessage(X_INFO, "config/%s: removing device %s\n", backend, dev->name);
86
87    /* Call PIE here so we don't try to dereference a device that's
88     * already been removed. */
89    input_lock();
90    ProcessInputEvents();
91    DeleteInputDeviceRequest(dev);
92    input_unlock();
93}
94
95void
96remove_devices(const char *backend, const char *config_info)
97{
98    DeviceIntPtr dev, next;
99
100    for (dev = inputInfo.devices; dev; dev = next) {
101        next = dev->next;
102        if (dev->config_info && strcmp(dev->config_info, config_info) == 0)
103            remove_device(backend, dev);
104    }
105    for (dev = inputInfo.off_devices; dev; dev = next) {
106        next = dev->next;
107        if (dev->config_info && strcmp(dev->config_info, config_info) == 0)
108            remove_device(backend, dev);
109    }
110
111    RemoveInputDeviceTraces(config_info);
112}
113
114BOOL
115device_is_duplicate(const char *config_info)
116{
117    DeviceIntPtr dev;
118
119    for (dev = inputInfo.devices; dev; dev = dev->next) {
120        if (dev->config_info && (strcmp(dev->config_info, config_info) == 0))
121            return TRUE;
122    }
123
124    for (dev = inputInfo.off_devices; dev; dev = dev->next) {
125        if (dev->config_info && (strcmp(dev->config_info, config_info) == 0))
126            return TRUE;
127    }
128
129    return FALSE;
130}
131
132struct OdevAttributes *
133config_odev_allocate_attributes(void)
134{
135    struct OdevAttributes *attribs =
136        xnfcalloc(1, sizeof (struct OdevAttributes));
137    attribs->fd = -1;
138    return attribs;
139}
140
141void
142config_odev_free_attributes(struct OdevAttributes *attribs)
143{
144    if (attribs->fd != -1)
145        systemd_logind_release_fd(attribs->major, attribs->minor, attribs->fd);
146    free(attribs->path);
147    free(attribs->syspath);
148    free(attribs->busid);
149    free(attribs->driver);
150    free(attribs);
151}
152