config.c revision 6747b715
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
3005b261ecSmrg#include "os.h"
316747b715Smrg#include "inputstr.h"
3205b261ecSmrg#include "hotplug.h"
3305b261ecSmrg#include "config-backends.h"
3405b261ecSmrg
3505b261ecSmrgvoid
366747b715Smrgconfig_init(void)
3705b261ecSmrg{
386747b715Smrg#ifdef CONFIG_UDEV
396747b715Smrg    if (!config_udev_init())
406747b715Smrg        ErrorF("[config] failed to initialise udev\n");
416747b715Smrg#elif defined(CONFIG_NEED_DBUS)
4205b261ecSmrg    if (config_dbus_core_init()) {
4305b261ecSmrg# ifdef CONFIG_DBUS_API
4405b261ecSmrg       if (!config_dbus_init())
4505b261ecSmrg	    ErrorF("[config] failed to initialise D-Bus API\n");
4605b261ecSmrg# endif
4705b261ecSmrg# ifdef CONFIG_HAL
4805b261ecSmrg        if (!config_hal_init())
4905b261ecSmrg            ErrorF("[config] failed to initialise HAL\n");
5005b261ecSmrg# endif
5105b261ecSmrg    }
5205b261ecSmrg    else {
5305b261ecSmrg	ErrorF("[config] failed to initialise D-Bus core\n");
5405b261ecSmrg    }
5505b261ecSmrg#endif
5605b261ecSmrg}
5705b261ecSmrg
5805b261ecSmrgvoid
596747b715Smrgconfig_fini(void)
6005b261ecSmrg{
616747b715Smrg#if defined(CONFIG_UDEV)
626747b715Smrg    config_udev_fini();
636747b715Smrg#elif defined(CONFIG_NEED_DBUS)
6405b261ecSmrg# ifdef CONFIG_HAL
6505b261ecSmrg    config_hal_fini();
6605b261ecSmrg# endif
6705b261ecSmrg# ifdef CONFIG_DBUS_API
6805b261ecSmrg    config_dbus_fini();
6905b261ecSmrg# endif
7005b261ecSmrg    config_dbus_core_fini();
7105b261ecSmrg#endif
7205b261ecSmrg}
736747b715Smrg
746747b715Smrgstatic void
756747b715Smrgremove_device(const char *backend, DeviceIntPtr dev)
766747b715Smrg{
776747b715Smrg    /* this only gets called for devices that have already been added */
786747b715Smrg    LogMessage(X_INFO, "config/%s: removing device %s\n", backend, dev->name);
796747b715Smrg
806747b715Smrg    /* Call PIE here so we don't try to dereference a device that's
816747b715Smrg     * already been removed. */
826747b715Smrg    OsBlockSignals();
836747b715Smrg    ProcessInputEvents();
846747b715Smrg    DeleteInputDeviceRequest(dev);
856747b715Smrg    OsReleaseSignals();
866747b715Smrg}
876747b715Smrg
886747b715Smrgvoid
896747b715Smrgremove_devices(const char *backend, const char *config_info)
906747b715Smrg{
916747b715Smrg    DeviceIntPtr dev, next;
926747b715Smrg
936747b715Smrg    for (dev = inputInfo.devices; dev; dev = next) {
946747b715Smrg        next = dev->next;
956747b715Smrg        if (dev->config_info && strcmp(dev->config_info, config_info) == 0)
966747b715Smrg            remove_device(backend, dev);
976747b715Smrg    }
986747b715Smrg    for (dev = inputInfo.off_devices; dev; dev = next) {
996747b715Smrg        next = dev->next;
1006747b715Smrg        if (dev->config_info && strcmp(dev->config_info, config_info) == 0)
1016747b715Smrg            remove_device(backend, dev);
1026747b715Smrg    }
1036747b715Smrg}
1046747b715Smrg
1056747b715SmrgBOOL
1066747b715Smrgdevice_is_duplicate(const char *config_info)
1076747b715Smrg{
1086747b715Smrg    DeviceIntPtr dev;
1096747b715Smrg
1106747b715Smrg    for (dev = inputInfo.devices; dev; dev = dev->next)
1116747b715Smrg    {
1126747b715Smrg        if (dev->config_info && (strcmp(dev->config_info, config_info) == 0))
1136747b715Smrg            return TRUE;
1146747b715Smrg    }
1156747b715Smrg
1166747b715Smrg    for (dev = inputInfo.off_devices; dev; dev = dev->next)
1176747b715Smrg    {
1186747b715Smrg        if (dev->config_info && (strcmp(dev->config_info, config_info) == 0))
1196747b715Smrg            return TRUE;
1206747b715Smrg    }
1216747b715Smrg
1226747b715Smrg    return FALSE;
1236747b715Smrg}
1246747b715Smrg
1256747b715Smrgvoid
1266747b715Smrgadd_option(InputOption **options, const char *key, const char *value)
1276747b715Smrg{
1286747b715Smrg    if (!value || *value == '\0')
1296747b715Smrg        return;
1306747b715Smrg
1316747b715Smrg    for (; *options; options = &(*options)->next)
1326747b715Smrg        ;
1336747b715Smrg    *options = calloc(sizeof(**options), 1);
1346747b715Smrg    if (!*options) /* Yeesh. */
1356747b715Smrg        return;
1366747b715Smrg    (*options)->key = strdup(key);
1376747b715Smrg    (*options)->value = strdup(value);
1386747b715Smrg    (*options)->next = NULL;
1396747b715Smrg}
140