| 1 | // Copyright (C) 2013 Laszlo Papp <lpapp@kde.org> |
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
| 3 | |
| 4 | #ifndef QTUDEV_P_H |
| 5 | #define QTUDEV_P_H |
| 6 | |
| 7 | // |
| 8 | // W A R N I N G |
| 9 | // ------------- |
| 10 | // |
| 11 | // This file is not part of the Qt API. It exists purely as an |
| 12 | // implementation detail. This header file may change from version to |
| 13 | // version without notice, or even be removed. |
| 14 | // |
| 15 | // We mean it. |
| 16 | // |
| 17 | |
| 18 | #ifdef LINK_LIBUDEV |
| 19 | extern "C" |
| 20 | { |
| 21 | #include <libudev.h> |
| 22 | } |
| 23 | #else |
| 24 | #include <QtCore/qlibrary.h> |
| 25 | #include <QtCore/qstring.h> |
| 26 | #include <QtCore/qdebug.h> |
| 27 | #include <QtCore/private/qglobal_p.h> |
| 28 | |
| 29 | #define GENERATE_SYMBOL_VARIABLE(returnType, symbolName, ...) \ |
| 30 | typedef returnType (*fp_##symbolName)(__VA_ARGS__); \ |
| 31 | static fp_##symbolName symbolName; |
| 32 | |
| 33 | #define RESOLVE_SYMBOL(symbolName) \ |
| 34 | symbolName = (fp_##symbolName)resolveSymbol(udevLibrary, #symbolName); \ |
| 35 | if (!symbolName) \ |
| 36 | return false; |
| 37 | |
| 38 | struct udev; |
| 39 | |
| 40 | #define udev_list_entry_foreach(list_entry, first_entry) \ |
| 41 | for (list_entry = first_entry; \ |
| 42 | list_entry != nullptr; \ |
| 43 | list_entry = udev_list_entry_get_next(list_entry)) |
| 44 | |
| 45 | struct udev_device; |
| 46 | struct udev_enumerate; |
| 47 | struct udev_list_entry; |
| 48 | |
| 49 | GENERATE_SYMBOL_VARIABLE(struct ::udev *, udev_new); |
| 50 | GENERATE_SYMBOL_VARIABLE(struct ::udev_enumerate *, udev_enumerate_new, struct ::udev *) |
| 51 | GENERATE_SYMBOL_VARIABLE(int, udev_enumerate_add_match_subsystem, struct udev_enumerate *, const char *) |
| 52 | GENERATE_SYMBOL_VARIABLE(int, udev_enumerate_scan_devices, struct udev_enumerate *) |
| 53 | GENERATE_SYMBOL_VARIABLE(struct udev_list_entry *, udev_enumerate_get_list_entry, struct udev_enumerate *) |
| 54 | GENERATE_SYMBOL_VARIABLE(struct udev_list_entry *, udev_list_entry_get_next, struct udev_list_entry *) |
| 55 | GENERATE_SYMBOL_VARIABLE(struct udev_device *, udev_device_new_from_syspath, struct udev *udev, const char *syspath) |
| 56 | GENERATE_SYMBOL_VARIABLE(const char *, udev_list_entry_get_name, struct udev_list_entry *) |
| 57 | GENERATE_SYMBOL_VARIABLE(const char *, udev_device_get_devnode, struct udev_device *) |
| 58 | GENERATE_SYMBOL_VARIABLE(const char *, udev_device_get_sysname, struct udev_device *) |
| 59 | GENERATE_SYMBOL_VARIABLE(const char *, udev_device_get_driver, struct udev_device *) |
| 60 | GENERATE_SYMBOL_VARIABLE(struct udev_device *, udev_device_get_parent, struct udev_device *) |
| 61 | GENERATE_SYMBOL_VARIABLE(const char *, udev_device_get_subsystem, struct udev_device *) |
| 62 | GENERATE_SYMBOL_VARIABLE(const char *, udev_device_get_property_value, struct udev_device *, const char *) |
| 63 | GENERATE_SYMBOL_VARIABLE(void, udev_device_unref, struct udev_device *) |
| 64 | GENERATE_SYMBOL_VARIABLE(void, udev_enumerate_unref, struct udev_enumerate *) |
| 65 | GENERATE_SYMBOL_VARIABLE(void, udev_unref, struct udev *) |
| 66 | |
| 67 | inline QFunctionPointer resolveSymbol(QLibrary *udevLibrary, const char *symbolName) |
| 68 | { |
| 69 | QFunctionPointer symbolFunctionPointer = udevLibrary->resolve(symbolName); |
| 70 | if (!symbolFunctionPointer) |
| 71 | qWarning("Failed to resolve the udev symbol: %s" , symbolName); |
| 72 | |
| 73 | return symbolFunctionPointer; |
| 74 | } |
| 75 | |
| 76 | inline bool resolveSymbols(QLibrary *udevLibrary) |
| 77 | { |
| 78 | if (!udevLibrary->isLoaded()) { |
| 79 | udevLibrary->setFileNameAndVersion(QStringLiteral("udev" ), 1); |
| 80 | if (!udevLibrary->load()) { |
| 81 | udevLibrary->setFileNameAndVersion(QStringLiteral("udev" ), 0); |
| 82 | if (!udevLibrary->load()) { |
| 83 | qWarning("Failed to load the library: %s, supported version(s): %i and %i" , qPrintable(udevLibrary->fileName()), 1, 0); |
| 84 | return false; |
| 85 | } |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | RESOLVE_SYMBOL(udev_new) |
| 90 | RESOLVE_SYMBOL(udev_enumerate_new) |
| 91 | RESOLVE_SYMBOL(udev_enumerate_add_match_subsystem) |
| 92 | RESOLVE_SYMBOL(udev_enumerate_scan_devices) |
| 93 | RESOLVE_SYMBOL(udev_enumerate_get_list_entry) |
| 94 | RESOLVE_SYMBOL(udev_list_entry_get_next) |
| 95 | RESOLVE_SYMBOL(udev_device_new_from_syspath) |
| 96 | RESOLVE_SYMBOL(udev_list_entry_get_name) |
| 97 | RESOLVE_SYMBOL(udev_device_get_devnode) |
| 98 | RESOLVE_SYMBOL(udev_device_get_sysname) |
| 99 | RESOLVE_SYMBOL(udev_device_get_driver) |
| 100 | RESOLVE_SYMBOL(udev_device_get_parent) |
| 101 | RESOLVE_SYMBOL(udev_device_get_subsystem) |
| 102 | RESOLVE_SYMBOL(udev_device_get_property_value) |
| 103 | RESOLVE_SYMBOL(udev_device_unref) |
| 104 | RESOLVE_SYMBOL(udev_enumerate_unref) |
| 105 | RESOLVE_SYMBOL(udev_unref) |
| 106 | |
| 107 | return true; |
| 108 | } |
| 109 | |
| 110 | #endif |
| 111 | |
| 112 | #endif |
| 113 | |