105b261ecSmrg/*
205b261ecSmrg * Copyright © 2006-2007 Daniel Stone
305b261ecSmrg *
405b261ecSmrg * Permission is hereby granted, free of charge, to any person obtaining a
505b261ecSmrg * copy of this software and associated documentation files (the "Software"),
605b261ecSmrg * to deal in the Software without restriction, including without limitation
705b261ecSmrg * the rights to use, copy, modify, merge, publish, distribute, sublicense,
805b261ecSmrg * and/or sell copies of the Software, and to permit persons to whom the
905b261ecSmrg * Software is furnished to do so, subject to the following conditions:
1005b261ecSmrg *
1105b261ecSmrg * The above copyright notice and this permission notice (including the next
1205b261ecSmrg * paragraph) shall be included in all copies or substantial portions of the
1305b261ecSmrg * Software.
1405b261ecSmrg *
1505b261ecSmrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1605b261ecSmrg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1705b261ecSmrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
1805b261ecSmrg * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
1905b261ecSmrg * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
2005b261ecSmrg * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
2105b261ecSmrg * DEALINGS IN THE SOFTWARE.
2205b261ecSmrg *
2305b261ecSmrg * Author: Daniel Stone <daniel@fooishbar.org>
2405b261ecSmrg */
2505b261ecSmrg
2605b261ecSmrg#ifdef HAVE_DIX_CONFIG_H
2705b261ecSmrg#include <dix-config.h>
2805b261ecSmrg#endif
2905b261ecSmrg
3035c4bbdfSmrg#include <unistd.h>
3105b261ecSmrg#include "os.h"
326747b715Smrg#include "inputstr.h"
3305b261ecSmrg#include "hotplug.h"
3405b261ecSmrg#include "config-backends.h"
3535c4bbdfSmrg#include "systemd-logind.h"
3635c4bbdfSmrg
3735c4bbdfSmrgvoid
3835c4bbdfSmrgconfig_pre_init(void)
3935c4bbdfSmrg{
4035c4bbdfSmrg#ifdef CONFIG_UDEV
4135c4bbdfSmrg    if (!config_udev_pre_init())
4235c4bbdfSmrg        ErrorF("[config] failed to pre-init udev\n");
4335c4bbdfSmrg#endif
4435c4bbdfSmrg}
4505b261ecSmrg
4605b261ecSmrgvoid
476747b715Smrgconfig_init(void)
4805b261ecSmrg{
496747b715Smrg#ifdef CONFIG_UDEV
506747b715Smrg    if (!config_udev_init())
516747b715Smrg        ErrorF("[config] failed to initialise udev\n");
5235c4bbdfSmrg#elif defined(CONFIG_HAL)
5335c4bbdfSmrg    if (!config_hal_init())
5435c4bbdfSmrg        ErrorF("[config] failed to initialise HAL\n");
5535c4bbdfSmrg#elif defined(CONFIG_WSCONS)
5635c4bbdfSmrg    if (!config_wscons_init())
5735c4bbdfSmrg        ErrorF("[config] failed to initialise wscons\n");
5805b261ecSmrg#endif
5905b261ecSmrg}
6005b261ecSmrg
6105b261ecSmrgvoid
626747b715Smrgconfig_fini(void)
6305b261ecSmrg{
646747b715Smrg#if defined(CONFIG_UDEV)
656747b715Smrg    config_udev_fini();
6635c4bbdfSmrg#elif defined(CONFIG_HAL)
6705b261ecSmrg    config_hal_fini();
6835c4bbdfSmrg#elif defined(CONFIG_WSCONS)
6935c4bbdfSmrg    config_wscons_fini();
7035c4bbdfSmrg#endif
7135c4bbdfSmrg}
7235c4bbdfSmrg
7335c4bbdfSmrgvoid
7435c4bbdfSmrgconfig_odev_probe(config_odev_probe_proc_ptr probe_callback)
7535c4bbdfSmrg{
7635c4bbdfSmrg#if defined(CONFIG_UDEV_KMS)
7735c4bbdfSmrg    config_udev_odev_probe(probe_callback);
7805b261ecSmrg#endif
7905b261ecSmrg}
806747b715Smrg
816747b715Smrgstatic void
826747b715Smrgremove_device(const char *backend, DeviceIntPtr dev)
836747b715Smrg{
846747b715Smrg    /* this only gets called for devices that have already been added */
856747b715Smrg    LogMessage(X_INFO, "config/%s: removing device %s\n", backend, dev->name);
866747b715Smrg
876747b715Smrg    /* Call PIE here so we don't try to dereference a device that's
886747b715Smrg     * already been removed. */
891b5d61b8Smrg    input_lock();
906747b715Smrg    ProcessInputEvents();
916747b715Smrg    DeleteInputDeviceRequest(dev);
921b5d61b8Smrg    input_unlock();
936747b715Smrg}
946747b715Smrg
956747b715Smrgvoid
966747b715Smrgremove_devices(const char *backend, const char *config_info)
976747b715Smrg{
986747b715Smrg    DeviceIntPtr dev, next;
996747b715Smrg
1006747b715Smrg    for (dev = inputInfo.devices; dev; dev = next) {
1016747b715Smrg        next = dev->next;
1026747b715Smrg        if (dev->config_info && strcmp(dev->config_info, config_info) == 0)
1036747b715Smrg            remove_device(backend, dev);
1046747b715Smrg    }
1056747b715Smrg    for (dev = inputInfo.off_devices; dev; dev = next) {
1066747b715Smrg        next = dev->next;
1076747b715Smrg        if (dev->config_info && strcmp(dev->config_info, config_info) == 0)
1086747b715Smrg            remove_device(backend, dev);
1096747b715Smrg    }
1101b5d61b8Smrg
1111b5d61b8Smrg    RemoveInputDeviceTraces(config_info);
1126747b715Smrg}
1136747b715Smrg
1146747b715SmrgBOOL
1156747b715Smrgdevice_is_duplicate(const char *config_info)
1166747b715Smrg{
1176747b715Smrg    DeviceIntPtr dev;
1186747b715Smrg
11935c4bbdfSmrg    for (dev = inputInfo.devices; dev; dev = dev->next) {
1206747b715Smrg        if (dev->config_info && (strcmp(dev->config_info, config_info) == 0))
1216747b715Smrg            return TRUE;
1226747b715Smrg    }
1236747b715Smrg
12435c4bbdfSmrg    for (dev = inputInfo.off_devices; dev; dev = dev->next) {
1256747b715Smrg        if (dev->config_info && (strcmp(dev->config_info, config_info) == 0))
1266747b715Smrg            return TRUE;
1276747b715Smrg    }
1286747b715Smrg
1296747b715Smrg    return FALSE;
1306747b715Smrg}
1316747b715Smrg
13235c4bbdfSmrgstruct OdevAttributes *
13335c4bbdfSmrgconfig_odev_allocate_attributes(void)
13435c4bbdfSmrg{
13535c4bbdfSmrg    struct OdevAttributes *attribs =
13635c4bbdfSmrg        xnfcalloc(1, sizeof (struct OdevAttributes));
13735c4bbdfSmrg    attribs->fd = -1;
13835c4bbdfSmrg    return attribs;
13935c4bbdfSmrg}
14035c4bbdfSmrg
1416747b715Smrgvoid
14235c4bbdfSmrgconfig_odev_free_attributes(struct OdevAttributes *attribs)
1436747b715Smrg{
14435c4bbdfSmrg    if (attribs->fd != -1)
14535c4bbdfSmrg        systemd_logind_release_fd(attribs->major, attribs->minor, attribs->fd);
14635c4bbdfSmrg    free(attribs->path);
14735c4bbdfSmrg    free(attribs->syspath);
14835c4bbdfSmrg    free(attribs->busid);
14935c4bbdfSmrg    free(attribs->driver);
15035c4bbdfSmrg    free(attribs);
1516747b715Smrg}
152