From 22f8381c08ec2c39dbafb9d9851c2b55d94be4a8 Mon Sep 17 00:00:00 2001 From: Timothy Pearson Date: Thu, 29 Mar 2012 17:11:06 -0500 Subject: Add initial tdehardwaredevices skeleton to tdecore Do not use the added classes yet, as the API and ABI are still under heavy construction! --- tdecore/CMakeLists.txt | 6 +- tdecore/kglobal.cpp | 8 ++ tdecore/kglobal.h | 7 ++ tdecore/kinstance.cpp | 18 +++++ tdecore/kinstance.h | 9 +++ tdecore/tdehardwaredevices.cpp | 171 +++++++++++++++++++++++++++++++++++++++++ tdecore/tdehardwaredevices.h | 168 ++++++++++++++++++++++++++++++++++++++++ 7 files changed, 384 insertions(+), 3 deletions(-) create mode 100644 tdecore/tdehardwaredevices.cpp create mode 100644 tdecore/tdehardwaredevices.h (limited to 'tdecore') diff --git a/tdecore/CMakeLists.txt b/tdecore/CMakeLists.txt index d5dbf8f96..1011eace7 100644 --- a/tdecore/CMakeLists.txt +++ b/tdecore/CMakeLists.txt @@ -66,7 +66,7 @@ install( FILES kcalendarsystem.h kcalendarsystemfactory.h kmacroexpander.h kmanagerselection.h kmountpoint.h kuser.h klockfile.h kidna.h ktempdir.h kshell.h fixx11h.h kxerrorhandler.h - tdelibs_export.h kde_file.h ktimezones.h + tdelibs_export.h kde_file.h ktimezones.h tdehardwaredevices.h ${CMAKE_CURRENT_BINARY_DIR}/kdemacros.h DESTINATION ${INCLUDE_INSTALL_DIR} ) @@ -122,14 +122,14 @@ set( ${target}_SRCS ktempdir.cpp kshell.cpp kmountpoint.cpp kcalendarsystemjalali.cpp kprotocolinfo_tdecore.cpp kprotocolinfofactory.cpp kxerrorhandler.cpp kuser.cpp kconfigskeleton.cpp kconfigdialogmanager.cpp klockfile.cpp - kqiodevicegzip_p.cpp ktimezones.cpp + kqiodevicegzip_p.cpp ktimezones.cpp tdehardwaredevices.cpp ) tde_add_library( ${target} SHARED AUTOMOC SOURCES ${${target}_SRCS} VERSION 4.2.0 EMBED tdecorenetwork-static - LINK ltdlc-static ${KDESVGICONS} DCOP-shared tdefx-shared ${ZLIB_LIBRARIES} ${LIBIDN_LIBRARIES} ${XCOMPOSITE_LIBRARIES} ICE SM + LINK ltdlc-static ${KDESVGICONS} DCOP-shared tdefx-shared ${ZLIB_LIBRARIES} ${LIBIDN_LIBRARIES} ${XCOMPOSITE_LIBRARIES} ICE SM udev DEPENDENCIES dcopidl dcopidl2cpp DESTINATION ${LIB_INSTALL_DIR} ) diff --git a/tdecore/kglobal.cpp b/tdecore/kglobal.cpp index 6a4702853..efb9d803f 100644 --- a/tdecore/kglobal.cpp +++ b/tdecore/kglobal.cpp @@ -34,6 +34,7 @@ #include #include #include +#include #include #include #include "kstaticdeleter.h" @@ -79,6 +80,13 @@ KIconLoader *KGlobal::iconLoader() return _instance->iconLoader(); } +TDEHardwareDevices *KGlobal::hardwareDevices() +{ + MYASSERT(_instance); + + return _instance->hardwareDevices(); +} + KInstance *KGlobal::instance() { MYASSERT(_instance); diff --git a/tdecore/kglobal.h b/tdecore/kglobal.h index cd433d530..a6a60c96c 100644 --- a/tdecore/kglobal.h +++ b/tdecore/kglobal.h @@ -25,6 +25,7 @@ class KCharsets; class KConfig; class KSharedConfig; class KIconLoader; +class TDEHardwareDevices; class KLocale; class KStandardDirs; class KStaticDeleterBase; @@ -77,6 +78,12 @@ public: */ static KIconLoader *iconLoader(); + /** + * Returns a hardwaredevices object. + * @return the global hardwaredevices object + */ + static TDEHardwareDevices *hardwareDevices(); + /** * Returns the global locale object. * @return the global locale object diff --git a/tdecore/kinstance.cpp b/tdecore/kinstance.cpp index 3e96e0365..fe0a5152c 100644 --- a/tdecore/kinstance.cpp +++ b/tdecore/kinstance.cpp @@ -24,6 +24,7 @@ #include "klocale.h" #include "kcharsets.h" #include "kiconloader.h" +#include "tdehardwaredevices.h" #include "kaboutdata.h" #include "kstandarddirs.h" #include "kdebug.h" @@ -70,6 +71,7 @@ KInstance::KInstance( const TQCString& name) : _dirs (0L), _config (0L), _iconLoader (0L), + _hardwaredevices (0L), _name( name ), _aboutData( new KAboutData( name, "", 0 ) ) { DEBUG_ADD @@ -88,6 +90,7 @@ KInstance::KInstance( const KAboutData * aboutData ) : _dirs (0L), _config (0L), _iconLoader (0L), + _hardwaredevices (0L), _name( aboutData->appName() ), _aboutData( aboutData ) { DEBUG_ADD @@ -107,6 +110,7 @@ KInstance::KInstance( KInstance* src ) : _dirs ( src->_dirs ), _config ( src->_config ), _iconLoader ( src->_iconLoader ), + _hardwaredevices ( src->_hardwaredevices ), _name( src->_name ), _aboutData( src->_aboutData ) { DEBUG_ADD @@ -125,6 +129,7 @@ KInstance::KInstance( KInstance* src ) src->_dirs = 0L; src->_config = 0L; src->_iconLoader = 0L; + src->_hardwaredevices = 0L; src->_aboutData = 0L; delete src; } @@ -143,6 +148,9 @@ KInstance::~KInstance() delete _iconLoader; _iconLoader = 0; + delete _hardwaredevices; + _hardwaredevices = 0; + // delete _config; // Do not delete, stored in d->sharedConfig _config = 0; delete _dirs; @@ -247,6 +255,16 @@ KIconLoader *KInstance::iconLoader() const return _iconLoader; } +TDEHardwareDevices *KInstance::hardwareDevices() const +{ + DEBUG_CHECK_ALIVE + if( _hardwaredevices == 0 ) { + _hardwaredevices = new TDEHardwareDevices( ); + } + + return _hardwaredevices; +} + void KInstance::newIconLoader() const { DEBUG_CHECK_ALIVE diff --git a/tdecore/kinstance.h b/tdecore/kinstance.h index 22da3bb3c..75cc4b46a 100644 --- a/tdecore/kinstance.h +++ b/tdecore/kinstance.h @@ -27,6 +27,7 @@ class TQFont; class KInstancePrivate; class KMimeSourceFactory; class KSharedConfig; +class TDEHardwareDevices; #include #include "tdelibs_export.h" @@ -100,6 +101,12 @@ class TDECORE_EXPORT KInstance */ KIconLoader *iconLoader() const; + /** + * Returns a hardwaredevices object. + * @return the hardwaredevices object. + */ + TDEHardwareDevices *hardwareDevices() const; + /** * Re-allocate the global iconloader. */ @@ -146,6 +153,8 @@ private: mutable KConfig *_config; mutable KIconLoader *_iconLoader; + mutable TDEHardwareDevices *_hardwaredevices; + TQCString _name; const KAboutData *_aboutData; diff --git a/tdecore/tdehardwaredevices.cpp b/tdecore/tdehardwaredevices.cpp new file mode 100644 index 000000000..a101cb017 --- /dev/null +++ b/tdecore/tdehardwaredevices.cpp @@ -0,0 +1,171 @@ +/* This file is part of the TDE libraries + Copyright (C) 2012 Timothy Pearson + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License version 2 as published by the Free Software Foundation. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public License + along with this library; see the file COPYING.LIB. If not, write to + the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + Boston, MA 02110-1301, USA. +*/ + +#include + +#include + +TDEGenericDevice::TDEGenericDevice(TDEGenericDeviceType::TDEGenericDeviceType dt, TQString dn) { + m_deviceType = dt; + m_deviceName = dn; +} + +TDEGenericDevice::~TDEGenericDevice() { +} + +TDEGenericDeviceType::TDEGenericDeviceType TDEGenericDevice::type() { + return m_deviceType; +} + +TQString &TDEGenericDevice::name() { + return m_deviceName; +} + +void TDEGenericDevice::setName(TQString dn) { + m_deviceName = dn; +} + +TQString &TDEGenericDevice::systemPath() { + return m_systemPath; +} + +void TDEGenericDevice::setSystemPath(TQString sp) { + m_systemPath = sp; +} + +TQString &TDEGenericDevice::deviceNode() { + return m_deviceNode; +} + +void TDEGenericDevice::setDeviceNode(TQString sn) { + m_deviceNode = sn; +} + +TDEHardwareDevices::TDEHardwareDevices() { + // Set up device list + m_deviceList.setAutoDelete( TRUE ); // the list owns the objects + + // Initialize udev interface + m_udevStruct = udev_new(); + if (!m_udevStruct) { + printf("Unable to create udev interface\n\r"); + } + + // Update internal device information + queryHardwareInformation(); +} + +TDEHardwareDevices::~TDEHardwareDevices() { + // Tear down udev interface + udev_unref(m_udevStruct); +} + +bool TDEHardwareDevices::queryHardwareInformation() { + if (!m_udevStruct) { + return false; + } + + // Prepare the device list for repopulation + m_deviceList.clear(); + + struct udev_enumerate *enumerate; + struct udev_list_entry *devices, *dev_list_entry; + struct udev_device *dev; + + // Create a list of all devices + enumerate = udev_enumerate_new(m_udevStruct); + udev_enumerate_add_match_subsystem(enumerate, NULL); + udev_enumerate_scan_devices(enumerate); + devices = udev_enumerate_get_list_entry(enumerate); + // Get detailed information on each detected device + udev_list_entry_foreach(dev_list_entry, devices) { + const char *path; + + // Get the filename of the /sys entry for the device and create a udev_device object (dev) representing it + path = udev_list_entry_get_name(dev_list_entry); + dev = udev_device_new_from_syspath(m_udevStruct, path); + + // Classify device and create TDEW device object + TQString devicename(udev_device_get_sysname(dev)); + TQString devicetype(udev_device_get_devtype(dev)); + TQString devicedriver(udev_device_get_driver(dev)); + TQString devicesubsystem(udev_device_get_subsystem(dev)); + TQString devicenode(udev_device_get_devnode(dev)); + TQString systempath(udev_device_get_syspath(dev)); + bool removable = false; + TDEGenericDevice* device = 0; + + if (devicetype == "disk") { + // Determine if disk is removable + TQString removablenodename = udev_device_get_syspath(dev); + removablenodename.append("/removable"); + FILE *fp = fopen(removablenodename.ascii(),"r"); + if (fp) { + if (fgetc(fp) == '1') { + removable = true; + } + fclose(fp); + } + if (removable) { + device = new TDEGenericDevice(TDEGenericDeviceType::RemovableDisk); + } + else { + device = new TDEGenericDevice(TDEGenericDeviceType::FixedDisk); + } + } + else if (devicetype.isNull()) { + if (devicesubsystem == "acpi") { + device = new TDEGenericDevice(TDEGenericDeviceType::OtherACPI); + } + else if (devicesubsystem == "input") { + device = new TDEGenericDevice(TDEGenericDeviceType::HID); + } + else if (devicesubsystem == "tty") { + device = new TDEGenericDevice(TDEGenericDeviceType::TextIO); + } + else if (devicesubsystem == "thermal") { + // FIXME + // Figure out a way to differentiate between ThermalControl (fans and coolers) and ThermalSensor types + device = new TDEGenericDevice(TDEGenericDeviceType::ThermalControl); + } + else if (devicesubsystem == "hwmon") { + // FIXME + // This might pick up thermal sensors + device = new TDEGenericDevice(TDEGenericDeviceType::OtherSensor); + } + } + if (device == 0) { + // Unhandled + device = new TDEGenericDevice(TDEGenericDeviceType::Other); + printf("[FIXME] UNCLASSIFIED DEVICE name: %s type: %s subsystem: %s driver: %s [Node Path: %s] [Syspath: %s]\n\r", devicename.ascii(), devicetype.ascii(), devicesubsystem.ascii(), devicedriver.ascii(), devicenode.ascii(), udev_device_get_syspath(dev)); fflush(stdout); + } + device->setName(devicename); + device->setDeviceNode(devicenode); + device->setSystemPath(systempath); + m_deviceList.append(device); + } + + // Free the enumerator object + udev_enumerate_unref(enumerate); + + return true; +} + +TDEGenericHardwareList &TDEHardwareDevices::listAllPhysicalDevices() { + return m_deviceList; +} \ No newline at end of file diff --git a/tdecore/tdehardwaredevices.h b/tdecore/tdehardwaredevices.h new file mode 100644 index 000000000..d80b86432 --- /dev/null +++ b/tdecore/tdehardwaredevices.h @@ -0,0 +1,168 @@ +/* This file is part of the TDE libraries + Copyright (C) 2012 Timothy Pearson + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License version 2 as published by the Free Software Foundation. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public License + along with this library; see the file COPYING.LIB. If not, write to + the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + Boston, MA 02110-1301, USA. +*/ +#ifndef _TDEHARDWAREDEVICES_H +#define _TDEHARDWAREDEVICES_H + +// TDE includes +#include +#include +#include "tdelibs_export.h" + +// udev includes +#include +#include +#include +#include +#include + +/** + * Hardware Device Access and Monitoring Library + * + * @author Timothy Pearson + */ + +namespace TDEGenericDeviceType { +enum TDEGenericDeviceType { + CPU, + GPU, + RAM, + Mainboard, + FixedDisk, + RemovableDisk, + StorageController, + HID, + Network, + Printer, + Scanner, + TextIO, + Peripheral, + Battery, + Power, + ThermalSensor, + ThermalControl, + OtherACPI, + OtherUSB, + OtherPeripheral, + OtherSensor, + Other +}; +}; + +class TDECORE_EXPORT TDEGenericDevice +{ + public: + /** + * Constructor. + * @param Device type + */ + TDEGenericDevice(TDEGenericDeviceType::TDEGenericDeviceType dt, TQString dn=TQString::null); + + /** + * Destructor. + */ + ~TDEGenericDevice(); + + /** + * @return a TDEGenericDeviceType::TDEGenericDeviceType specifying the device type + */ + TDEGenericDeviceType::TDEGenericDeviceType type(); + + /** + * @return a TQString with the device name, if any + */ + TQString &name(); + + /** + * @param a TQString with the device name, if any + */ + void setName(TQString dn); + + /** + * @return a TQString with the system path, if any + * + * This method is non-portable, so be careful! + */ + TQString &systemPath(); + + /** + * @param a TQString with the system path, if any + * + * This method is non-portable, so be careful! + */ + void setSystemPath(TQString sp); + + /** + * @return a TQString with the system device node, if any + * + * This method is non-portable, so be careful! + */ + TQString &deviceNode(); + + /** + * @param a TQString with the system device node, if any + * + * This method is non-portable, so be careful! + */ + void setDeviceNode(TQString sn); + + private: + TDEGenericDeviceType::TDEGenericDeviceType m_deviceType; + TQString m_deviceName; + TQString m_systemPath; + TQString m_deviceNode; +}; + +typedef TQPtrList TDEGenericHardwareList; + +class TDECORE_EXPORT TDEHardwareDevices +{ + public: + /** + * Constructor. + */ + TDEHardwareDevices(); + + /** + * Destructor. + */ + ~TDEHardwareDevices(); + + /** + * Query all hardware capabilities on all devices + * This does not normally need to be called by an application, as + * device detection is handled internally and automatically + * + * A call to this method immediately invalidates any TDEGenericHardwareList + * structures returned by listAllPhysicalDevices() + * + * @return TRUE if successful + */ + bool queryHardwareInformation(); + + /** + * List all hardware capabilities on all devices + * @return TQPtrList containing all known hardware devices + */ + TQPtrList &listAllPhysicalDevices(); + + private: + struct udev *m_udevStruct; + TDEGenericHardwareList m_deviceList; +}; + +#endif \ No newline at end of file -- cgit v1.2.3