/* * Copyright (c) 2011 Benjamin Franzke * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */ #include #include #include #include #include "libudev.h" #include #include "wfdregistry.h" static struct wfd_registry *wfd_registry = NULL; static void wfd_destroy(void) { int i; if (wfd_registry->devices) { for (i = 0; i < wfd_registry->device_count; ++i) close(wfd_registry->devices[i].fd); free(wfd_registry->devices); } if (wfd_registry->udev) { udev_unref(wfd_registry->udev); } if (wfd_registry != NULL) free(wfd_registry); } static int wfd_init(void) { assert(wfd_registry == NULL); wfd_registry = calloc(1, sizeof *wfd_registry); if (wfd_registry == NULL) return -ENOMEM; wfd_registry->udev = udev_new(); if (wfd_registry->udev == NULL) { free(wfd_registry); return -ENOMEM; } atexit(wfd_destroy); return 0; } struct wfd_registry * wfd_get_registry(void) { if (wfd_registry == NULL) wfd_init(); return wfd_registry; }