• Skip to content
  • Skip to link menu
Trinity API Reference
  • Trinity API Reference
  • tdecore
 

tdecore

  • tdecore
tdehardwaredevices.cpp
1 /* This file is part of the TDE libraries
2  Copyright (C) 2012 Timothy Pearson <kb9vqf@pearsoncomputing.net>
3 
4  This library is free software; you can redistribute it and/or
5  modify it under the terms of the GNU Library General Public
6  License version 2 as published by the Free Software Foundation.
7 
8  This library is distributed in the hope that it will be useful,
9  but WITHOUT ANY WARRANTY; without even the implied warranty of
10  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11  Library General Public License for more details.
12 
13  You should have received a copy of the GNU Library General Public License
14  along with this library; see the file COPYING.LIB. If not, write to
15  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
16  Boston, MA 02110-1301, USA.
17 */
18 
19 #include <tdehardwaredevices.h>
20 
21 #include <tqfile.h>
22 #include <tqdir.h>
23 #include <tqstringlist.h>
24 #include <tqsocketnotifier.h>
25 
26 #include <tdeglobal.h>
27 #include <tdelocale.h>
28 #include <tdeconfig.h>
29 #include <tdetempfile.h>
30 #include <ksimpledirwatch.h>
31 #include <kstandarddirs.h>
32 
33 #include <tdeapplication.h>
34 #include <dcopclient.h>
35 
36 #include <libudev.h>
37 
38 #include <fcntl.h>
39 #include <poll.h>
40 #include <sys/stat.h>
41 #include <sys/ioctl.h>
42 #include <linux/fs.h>
43 #include <linux/cdrom.h>
44 
45 // Backlight devices
46 #include <linux/fb.h>
47 
48 // Input devices
49 #include <linux/input.h>
50 
51 // Network devices
52 #include <sys/types.h>
53 #include <ifaddrs.h>
54 #include <netinet/in.h>
55 #include <arpa/inet.h>
56 #include <arpa/inet.h>
57 #include <sys/socket.h>
58 #include <netdb.h>
59 
60 // Network connection manager
61 #include "tdenetworkconnections.h"
62 
63 // Compile-time configuration
64 #include "config.h"
65 
66 #ifdef WITH_NETWORK_MANAGER_BACKEND
67  #include "networkbackends/network-manager/network-manager.h"
68 #endif // WITH_NETWORK_MANAGER_BACKEND
69 
70 // uPower and uDisks2 integration
71 #if defined(WITH_UPOWER) || defined(WITH_UDISKS) || defined(WITH_UDISKS2)
72  #include <tqdbusdata.h>
73  #include <tqdbusmessage.h>
74  #include <tqdbusproxy.h>
75  #include <tqdbusvariant.h>
76  #include <tqdbusconnection.h>
77 #endif // defined(WITH_UPOWER) || defined(WITH_UDISKS) || defined(WITH_UDISKS2)
78 #if defined(WITH_UDISKS) || defined(WITH_UDISKS2)
79  #include <tqdbuserror.h>
80  #include <tqdbusdatamap.h>
81  #include <tqdbusobjectpath.h>
82 #endif // defined(WITH_UDISKS) || defined(WITH_UDISKS2)
83 #if defined(WITH_UDISKS)
84  #include "tqdbusdatalist.h"
85 #endif // ddefined(WITH_UDISKS)
86 
87 // BEGIN BLOCK
88 // Copied from include/linux/genhd.h
89 #define GENHD_FL_REMOVABLE 1
90 #define GENHD_FL_MEDIA_CHANGE_NOTIFY 4
91 #define GENHD_FL_CD 8
92 #define GENHD_FL_UP 16
93 #define GENHD_FL_SUPPRESS_PARTITION_INFO 32
94 #define GENHD_FL_EXT_DEVT 64
95 #define GENHD_FL_NATIVE_CAPACITY 128
96 #define GENHD_FL_BLOCK_EVENTS_ON_EXCL_WRITE 256
97 // END BLOCK
98 
99 // NOTE TO DEVELOPERS
100 // This command will greatly help when attempting to find properties to distinguish one device from another
101 // udevadm info --query=all --path=/sys/....
102 
103 // This routine is courtsey of an answer on "Stack Overflow"
104 // It takes an LSB-first int and makes it an MSB-first int (or vice versa)
105 unsigned int reverse_bits(register unsigned int x)
106 {
107  x = (((x & 0xaaaaaaaa) >> 1) | ((x & 0x55555555) << 1));
108  x = (((x & 0xcccccccc) >> 2) | ((x & 0x33333333) << 2));
109  x = (((x & 0xf0f0f0f0) >> 4) | ((x & 0x0f0f0f0f) << 4));
110  x = (((x & 0xff00ff00) >> 8) | ((x & 0x00ff00ff) << 8));
111  return((x >> 16) | (x << 16));
112 }
113 
114 #define BIT_IS_SET(bits, n) (bits[n >> 3] & (1 << (n & 0x7)))
115 
116 TDESensorCluster::TDESensorCluster() {
117  label = TQString::null;
118  current = -1;
119  minimum = -1;
120  maximum = -1;
121  warning = -1;
122  critical = -1;
123 }
124 
125 TDEGenericDevice::TDEGenericDevice(TDEGenericDeviceType::TDEGenericDeviceType dt, TQString dn) : TQObject() {
126  m_deviceType = dt;
127  m_deviceName = dn;
128 
129  m_parentDevice = 0;
130  m_friendlyName = TQString::null;
131  m_blacklistedForUpdate = false;
132 }
133 
134 TDEGenericDevice::~TDEGenericDevice() {
135 }
136 
137 TDEGenericDeviceType::TDEGenericDeviceType TDEGenericDevice::type() {
138  return m_deviceType;
139 }
140 
141 TQString TDEGenericDevice::name() {
142  return m_deviceName;
143 }
144 
145 void TDEGenericDevice::internalSetName(TQString dn) {
146  m_deviceName = dn;
147 }
148 
149 TQString TDEGenericDevice::vendorName() {
150  return m_vendorName;
151 }
152 
153 void TDEGenericDevice::internalSetVendorName(TQString vn) {
154  m_vendorName = vn;
155 }
156 
157 TQString TDEGenericDevice::vendorModel() {
158  return m_vendorModel;
159 }
160 
161 void TDEGenericDevice::internalSetVendorModel(TQString vm) {
162  m_vendorModel = vm;
163 }
164 
165 TQString TDEGenericDevice::serialNumber() {
166  return m_serialNumber;
167 }
168 
169 void TDEGenericDevice::internalSetSerialNumber(TQString sn) {
170  m_serialNumber = sn;
171 }
172 
173 TQString TDEGenericDevice::systemPath() {
174  if (!m_systemPath.endsWith("/")) {
175  m_systemPath += "/";
176  }
177  return m_systemPath;
178 }
179 
180 void TDEGenericDevice::internalSetSystemPath(TQString sp) {
181  m_systemPath = sp;
182 }
183 
184 TQString TDEGenericDevice::deviceNode() {
185  return m_deviceNode;
186 }
187 
188 void TDEGenericDevice::internalSetDeviceNode(TQString sn) {
189  m_deviceNode = sn;
190 }
191 
192 TQString TDEGenericDevice::deviceBus() {
193  return m_deviceBus;
194 }
195 
196 void TDEGenericDevice::internalSetDeviceBus(TQString db) {
197  m_deviceBus = db;
198 }
199 
200 TQString TDEGenericDevice::uniqueID() {
201  m_uniqueID = m_systemPath+m_deviceNode;
202  return m_uniqueID;
203 }
204 
205 TQString TDEGenericDevice::vendorID() {
206  return m_vendorID;
207 }
208 
209 void TDEGenericDevice::internalSetVendorID(TQString id) {
210  m_vendorID = id;
211  m_vendorID.replace("0x", "");
212 }
213 
214 TQString TDEGenericDevice::modelID() {
215  return m_modelID;
216 }
217 
218 void TDEGenericDevice::internalSetModelID(TQString id) {
219  m_modelID = id;
220  m_modelID.replace("0x", "");
221 }
222 
223 TQString TDEGenericDevice::vendorEncoded() {
224  return m_vendorenc;
225 }
226 
227 void TDEGenericDevice::internalSetVendorEncoded(TQString id) {
228  m_vendorenc = id;
229 }
230 
231 TQString TDEGenericDevice::modelEncoded() {
232  return m_modelenc;
233 }
234 
235 void TDEGenericDevice::internalSetModelEncoded(TQString id) {
236  m_modelenc = id;
237 }
238 
239 TQString TDEGenericDevice::subVendorID() {
240  return m_subvendorID;
241 }
242 
243 void TDEGenericDevice::internalSetSubVendorID(TQString id) {
244  m_subvendorID = id;
245  m_subvendorID.replace("0x", "");
246 }
247 
248 TQString TDEGenericDevice::PCIClass() {
249  return m_pciClass;
250 }
251 
252 void TDEGenericDevice::internalSetPCIClass(TQString cl) {
253  m_pciClass = cl;
254  m_pciClass.replace("0x", "");
255 }
256 
257 TQString TDEGenericDevice::moduleAlias() {
258  return m_modAlias;
259 }
260 
261 void TDEGenericDevice::internalSetModuleAlias(TQString ma) {
262  m_modAlias = ma;
263 }
264 
265 TQString TDEGenericDevice::deviceDriver() {
266  return m_deviceDriver;
267 }
268 
269 void TDEGenericDevice::internalSetDeviceDriver(TQString dr) {
270  m_deviceDriver = dr;
271 }
272 
273 TQString TDEGenericDevice::subsystem() {
274  return m_subsystem;
275 }
276 
277 void TDEGenericDevice::internalSetSubsystem(TQString ss) {
278  m_subsystem = ss;
279 }
280 
281 TQString TDEGenericDevice::subModelID() {
282  return m_submodelID;
283 }
284 
285 void TDEGenericDevice::internalSetSubModelID(TQString id) {
286  m_submodelID = id;
287  m_submodelID.replace("0x", "");
288 }
289 
290 void TDEGenericDevice::internalSetParentDevice(TDEGenericDevice* pd) {
291  m_parentDevice = pd;
292 }
293 
294 TDEGenericDevice* TDEGenericDevice::parentDevice() {
295  return m_parentDevice;
296 }
297 
298 TQPixmap TDEGenericDevice::icon(TDEIcon::StdSizes size) {
299  return TDEGlobal::hardwareDevices()->getDeviceTypeIconFromType(type(), size);
300 }
301 
302 bool TDEGenericDevice::blacklistedForUpdate() {
303  return m_blacklistedForUpdate;
304 }
305 
306 void TDEGenericDevice::internalSetBlacklistedForUpdate(bool bl) {
307  m_blacklistedForUpdate = bl;
308 }
309 
310 TQString TDEGenericDevice::friendlyDeviceType() {
311  return TDEGlobal::hardwareDevices()->getFriendlyDeviceTypeStringFromType(type());
312 }
313 
314 TQString TDEGenericDevice::busID() {
315  TQString busid = m_systemPath;
316  busid = busid.remove(0, busid.findRev("/")+1);
317  busid = busid.remove(0, busid.find(":")+1);
318  return busid;
319 }
320 
321 TQString TDEGenericDevice::friendlyName() {
322  if (m_friendlyName.isNull()) {
323  if (type() == TDEGenericDeviceType::RootSystem) {
324  m_friendlyName = "Linux System";
325  }
326  else if (type() == TDEGenericDeviceType::Root) {
327  TQString friendlyDriverName = systemPath();
328  friendlyDriverName.truncate(friendlyDriverName.length()-1);
329  friendlyDriverName.remove(0, friendlyDriverName.findRev("/")+1);
330  m_friendlyName = friendlyDriverName;
331  }
332  else if (m_modAlias.lower().startsWith("pci")) {
333  m_friendlyName = TDEGlobal::hardwareDevices()->findPCIDeviceName(m_vendorID, m_modelID, m_subvendorID, m_submodelID);
334  }
335  else if (m_modAlias.lower().startsWith("usb")) {
336  m_friendlyName = TDEGlobal::hardwareDevices()->findUSBDeviceName(m_vendorID, m_modelID, m_subvendorID, m_submodelID);
337  }
338  else {
339  TQString acpigentype = systemPath();
340  acpigentype.truncate(acpigentype.length()-1);
341  acpigentype.remove(0, acpigentype.findRev("/")+1);
342  TQString pnpgentype = acpigentype;
343  pnpgentype.truncate(pnpgentype.find(":"));
344  if (pnpgentype.startsWith("PNP")) {
345  m_friendlyName = TDEGlobal::hardwareDevices()->findPNPDeviceName(pnpgentype);
346  }
347  else if (acpigentype.startsWith("device:")) {
348  acpigentype.remove(0, acpigentype.findRev(":")+1);
349  acpigentype.prepend("0x");
350  m_friendlyName = i18n("ACPI Node %1").arg(acpigentype.toUInt(0,0));
351  }
352  }
353  }
354 
355  if (m_friendlyName.isNull()) {
356  // Could not identify based on model/vendor codes
357  // Try to construct something from the model/vendor strings if they are available
358  if (!m_vendorName.isNull() && !m_vendorModel.isNull()) {
359  m_friendlyName = m_vendorName + " " + m_vendorModel;
360  }
361  }
362 
363  if (m_friendlyName.isNull()) {
364  // Could not identify based on model/vendor
365  // Guess by type
366  if (type() == TDEGenericDeviceType::CPU) {
367  m_friendlyName = name();
368  }
369  else if (type() == TDEGenericDeviceType::Event) {
370  // Use parent node name
371  if (m_parentDevice) {
372  return m_parentDevice->friendlyName();
373  }
374  else {
375  m_friendlyName = i18n("Generic Event Device");
376  }
377  }
378  else if (type() == TDEGenericDeviceType::Input) {
379  // Use parent node name
380  if (m_parentDevice) {
381  return m_parentDevice->friendlyName();
382  }
383  else {
384  m_friendlyName = i18n("Generic Input Device");
385  }
386  }
387  // Guess by driver
388  else if (!m_deviceDriver.isNull()) {
389  TQString friendlyDriverName = m_deviceDriver.lower();
390  friendlyDriverName[0] = friendlyDriverName[0].upper();
391  m_friendlyName = i18n("Generic %1 Device").arg(friendlyDriverName);
392  }
393  else if (m_systemPath.lower().startsWith("/sys/devices/virtual")) {
394  TQString friendlyDriverName = systemPath();
395  friendlyDriverName.truncate(friendlyDriverName.length()-1);
396  friendlyDriverName.remove(0, friendlyDriverName.findRev("/")+1);
397  if (!friendlyDriverName.isNull()) {
398  m_friendlyName = i18n("Virtual Device %1").arg(friendlyDriverName);
399  }
400  else {
401  m_friendlyName = i18n("Unknown Virtual Device");
402  }
403  }
404  else {
405  // I really have no idea what this peripheral is; say so!
406  m_friendlyName = i18n("Unknown Device") + " " + name();
407  }
408  }
409 
410  return m_friendlyName;
411 }
412 
413 TDEStorageDevice::TDEStorageDevice(TDEGenericDeviceType::TDEGenericDeviceType dt, TQString dn) : TDEGenericDevice(dt, dn), m_mediaInserted(true) {
414  m_diskType = TDEDiskDeviceType::Null;
415  m_diskStatus = TDEDiskDeviceStatus::Null;
416 }
417 
418 TDEStorageDevice::~TDEStorageDevice() {
419 }
420 
421 TDEDiskDeviceType::TDEDiskDeviceType TDEStorageDevice::diskType() {
422  return m_diskType;
423 }
424 
425 void TDEStorageDevice::internalSetDiskType(TDEDiskDeviceType::TDEDiskDeviceType dt) {
426  m_diskType = dt;
427 }
428 
429 bool TDEStorageDevice::isDiskOfType(TDEDiskDeviceType::TDEDiskDeviceType tf) {
430  return ((m_diskType&tf)!=TDEDiskDeviceType::Null);
431 }
432 
433 TDEDiskDeviceStatus::TDEDiskDeviceStatus TDEStorageDevice::diskStatus() {
434  return m_diskStatus;
435 }
436 
437 void TDEStorageDevice::internalSetDiskStatus(TDEDiskDeviceStatus::TDEDiskDeviceStatus st) {
438  m_diskStatus = st;
439 }
440 
441 bool TDEStorageDevice::checkDiskStatus(TDEDiskDeviceStatus::TDEDiskDeviceStatus sf) {
442  return ((m_diskStatus&sf)!=(TDEDiskDeviceStatus::TDEDiskDeviceStatus)0);
443 }
444 
445 bool TDEStorageDevice::lockDriveMedia(bool lock) {
446  int fd = open(deviceNode().ascii(), O_RDWR | O_NONBLOCK);
447  if (fd < 0) {
448  return false;
449  }
450  if (ioctl(fd, CDROM_LOCKDOOR, (lock)?1:0) != 0) {
451  close(fd);
452  return false;
453  }
454  else {
455  close(fd);
456  return true;
457  }
458 }
459 
460 bool ejectDriveUDisks(TDEStorageDevice* sdevice) {
461 #ifdef WITH_UDISKS
462  TQT_DBusConnection dbusConn = TQT_DBusConnection::addConnection(TQT_DBusConnection::SystemBus);
463  if (dbusConn.isConnected()) {
464  TQString blockDeviceString = sdevice->deviceNode();
465  blockDeviceString.replace("/dev/", "");
466  blockDeviceString = "/org/freedesktop/UDisks/devices/" + blockDeviceString;
467 
468  // Eject the drive!
469  TQT_DBusError error;
470  TQT_DBusProxy driveControl("org.freedesktop.UDisks", blockDeviceString, "org.freedesktop.UDisks.Device", dbusConn);
471  if (driveControl.canSend()) {
472  TQValueList<TQT_DBusData> params;
473  TQT_DBusDataList options;
474  params << TQT_DBusData::fromList(options);
475  TQT_DBusMessage reply = driveControl.sendWithReply("DriveEject", params, &error);
476  if (error.isValid()) {
477  // Error!
478  printf("[ERROR] %s\n", error.name().ascii()); fflush(stdout);
479  return FALSE;
480  }
481  else {
482  return TRUE;
483  }
484  }
485  else {
486  return FALSE;
487  }
488  }
489  else {
490  return FALSE;
491  }
492 #else // WITH_UDISKS
493  return FALSE;
494 #endif // WITH_UDISKS
495 }
496 
497 bool ejectDriveUDisks2(TDEStorageDevice* sdevice) {
498 #ifdef WITH_UDISKS2
499  TQT_DBusConnection dbusConn = TQT_DBusConnection::addConnection(TQT_DBusConnection::SystemBus);
500  if (dbusConn.isConnected()) {
501  TQString blockDeviceString = sdevice->deviceNode();
502  blockDeviceString.replace("/dev/", "");
503  blockDeviceString = "/org/freedesktop/UDisks2/block_devices/" + blockDeviceString;
504  TQT_DBusProxy hardwareControl("org.freedesktop.UDisks2", blockDeviceString, "org.freedesktop.DBus.Properties", dbusConn);
505  if (hardwareControl.canSend()) {
506  // get associated udisks2 drive path
507  TQT_DBusError error;
508  TQValueList<TQT_DBusData> params;
509  params << TQT_DBusData::fromString("org.freedesktop.UDisks2.Block") << TQT_DBusData::fromString("Drive");
510  TQT_DBusMessage reply = hardwareControl.sendWithReply("Get", params, &error);
511  if (error.isValid()) {
512  // Error!
513  printf("[ERROR] %s\n", error.name().ascii()); fflush(stdout);
514  return FALSE;
515  }
516  else {
517  if (reply.type() == TQT_DBusMessage::ReplyMessage && reply.count() == 1) {
518  TQT_DBusObjectPath driveObjectPath = reply[0].toVariant().value.toObjectPath();
519  if (!driveObjectPath.isValid()) {
520  return FALSE;
521  }
522 
523  error = TQT_DBusError();
524  TQT_DBusProxy driveInformation("org.freedesktop.UDisks2", driveObjectPath, "org.freedesktop.DBus.Properties", dbusConn);
525  // can eject?
526  TQValueList<TQT_DBusData> params;
527  params << TQT_DBusData::fromString("org.freedesktop.UDisks2.Drive") << TQT_DBusData::fromString("Ejectable");
528  TQT_DBusMessage reply = driveInformation.sendWithReply("Get", params, &error);
529  if (error.isValid()) {
530  // Error!
531  printf("[ERROR] %s\n", error.name().ascii()); fflush(stdout);
532  return FALSE;
533  }
534  if (reply.type() == TQT_DBusMessage::ReplyMessage && reply.count() == 1) {
535  bool ejectable = reply[0].toVariant().value.toBool();
536  if (!ejectable) {
537  return FALSE;
538  }
539 
540  // Eject the drive!
541  TQT_DBusProxy driveControl("org.freedesktop.UDisks2", driveObjectPath, "org.freedesktop.UDisks2.Drive", dbusConn);
542  TQValueList<TQT_DBusData> params;
543  TQT_DBusDataMap<TQString> options(TQT_DBusData::Variant);
544  params << TQT_DBusData::fromStringKeyMap(options);
545  TQT_DBusMessage reply = driveControl.sendWithReply("Eject", params, &error);
546  if (error.isValid()) {
547  // Error!
548  printf("[ERROR] %s\n", error.name().ascii()); fflush(stdout);
549  return FALSE;
550  }
551  else {
552  return TRUE;
553  }
554  }
555  else {
556  return FALSE;
557  }
558  }
559  else {
560  return FALSE;
561  }
562  }
563  }
564  else {
565  return FALSE;
566  }
567  }
568  else {
569  return FALSE;
570  }
571 #else // WITH_UDISKS2
572  return FALSE;
573 #endif // WITH_UDISKS2
574 }
575 
576 bool TDEStorageDevice::ejectDrive() {
577  if (ejectDriveUDisks2(this)) {
578  return TRUE;
579  }
580  else {
581 #ifdef WITH_UDISKS2
582  printf("[tdehwlib] Failed to eject drive '%s' via udisks2, falling back to alternate mechanism\n", deviceNode().ascii());
583 #endif // WITH_UDISKS2
584  if (ejectDriveUDisks(this)) {
585  return TRUE;
586  }
587  else {
588 #ifdef WITH_UDISKS
589  printf("[tdehwlib] Failed to eject drive '%s' via udisks, falling back to alternate mechanism\n", deviceNode().ascii());
590 #endif // WITH_UDISKS
591  TQString command = TQString("eject -v '%1' 2>&1").arg(deviceNode());
592 
593  FILE *exepipe = popen(command.ascii(), "r");
594  if (exepipe) {
595  TQString pmount_output;
596  char buffer[8092];
597  pmount_output = fgets(buffer, sizeof(buffer), exepipe);
598  int retcode = pclose(exepipe);
599  if (retcode == 0) {
600  return TRUE;
601  }
602  else {
603  printf("[tdehwlib] Failed to eject drive '%s' via 'eject' command\n", deviceNode().ascii());
604  return FALSE;
605  }
606  }
607  else {
608  printf("[tdehwlib] Failed to eject drive '%s' via 'eject' command\n", deviceNode().ascii());
609  return FALSE;
610  }
611  }
612  }
613 }
614 
615 bool TDEStorageDevice::ejectDriveMedia() {
616  int fd = open(deviceNode().ascii(), O_RDWR | O_NONBLOCK);
617  if (fd < 0) {
618  return false;
619  }
620  if (ioctl(fd, CDROMEJECT) != 0) {
621  close(fd);
622  return false;
623  }
624  else {
625  close(fd);
626  return true;
627  }
628 }
629 
630 TQString TDEStorageDevice::diskLabel() {
631  return m_diskName;
632 }
633 
634 void TDEStorageDevice::internalSetDiskLabel(TQString dn) {
635  m_diskName = dn;
636 }
637 
638 bool TDEStorageDevice::mediaInserted() {
639  return m_mediaInserted;
640 }
641 
642 void TDEStorageDevice::internalSetMediaInserted(bool inserted) {
643  m_mediaInserted = inserted;
644 }
645 
646 TQString TDEStorageDevice::fileSystemName() {
647  return m_fileSystemName;
648 }
649 
650 void TDEStorageDevice::internalSetFileSystemName(TQString fn) {
651  m_fileSystemName = fn;
652 }
653 
654 TQString TDEStorageDevice::fileSystemUsage() {
655  return m_fileSystemUsage;
656 }
657 
658 void TDEStorageDevice::internalSetFileSystemUsage(TQString fu) {
659  m_fileSystemUsage = fu;
660 }
661 
662 TQString TDEStorageDevice::diskUUID() {
663  return m_diskUUID;
664 }
665 
666 void TDEStorageDevice::internalSetDiskUUID(TQString id) {
667  m_diskUUID = id;
668 }
669 
670 TQStringList TDEStorageDevice::holdingDevices() {
671  return m_holdingDevices;
672 }
673 
674 void TDEStorageDevice::internalSetHoldingDevices(TQStringList hd) {
675  m_holdingDevices = hd;
676 }
677 
678 TQStringList TDEStorageDevice::slaveDevices() {
679  return m_slaveDevices;
680 }
681 
682 void TDEStorageDevice::internalSetSlaveDevices(TQStringList sd) {
683  m_slaveDevices = sd;
684 }
685 
686 TQString TDEStorageDevice::friendlyName() {
687  // Return the actual storage device name
688  TQString devicevendorid = vendorEncoded();
689  TQString devicemodelid = modelEncoded();
690 
691  devicevendorid.replace("\\x20", " ");
692  devicemodelid.replace("\\x20", " ");
693 
694  devicevendorid = devicevendorid.stripWhiteSpace();
695  devicemodelid = devicemodelid.stripWhiteSpace();
696  devicevendorid = devicevendorid.simplifyWhiteSpace();
697  devicemodelid = devicemodelid.simplifyWhiteSpace();
698 
699  TQString devicename = devicevendorid + " " + devicemodelid;
700 
701  devicename = devicename.stripWhiteSpace();
702  devicename = devicename.simplifyWhiteSpace();
703 
704  if (devicename != "") {
705  return devicename;
706  }
707 
708  if (isDiskOfType(TDEDiskDeviceType::Camera)) {
709  return TDEGenericDevice::friendlyName();
710  }
711 
712  if (isDiskOfType(TDEDiskDeviceType::Floppy)) {
713  return friendlyDeviceType();
714  }
715 
716  TQString label = diskLabel();
717  if (label.isNull()) {
718  if (deviceSize() > 0) {
719  if (checkDiskStatus(TDEDiskDeviceStatus::Hotpluggable)) {
720  label = i18n("%1 Removable Device").arg(deviceFriendlySize());
721  }
722  else {
723  label = i18n("%1 Fixed Storage Device").arg(deviceFriendlySize());
724  }
725  }
726  }
727 
728  if (!label.isNull()) {
729  return label;
730  }
731 
732  return friendlyDeviceType();
733 }
734 
735 TQString TDEStorageDevice::friendlyDeviceType() {
736  TQString ret = i18n("Hard Disk Drive");
737 
738  // Keep this in sync with TDEStorageDevice::icon(TDEIcon::StdSizes size) below
739  if (isDiskOfType(TDEDiskDeviceType::Floppy)) {
740  ret = i18n("Floppy Drive");
741  }
742  if (isDiskOfType(TDEDiskDeviceType::Optical)) {
743  ret = i18n("Optical Drive");
744  }
745  if (isDiskOfType(TDEDiskDeviceType::CDROM)) {
746  ret = i18n("CDROM Drive");
747  }
748  if (isDiskOfType(TDEDiskDeviceType::CDRW)) {
749  ret = i18n("CDRW Drive");
750  }
751  if (isDiskOfType(TDEDiskDeviceType::DVDROM)) {
752  ret = i18n("DVD Drive");
753  }
754  if (isDiskOfType(TDEDiskDeviceType::DVDRW)) {
755  ret = i18n("DVDRW Drive");
756  }
757  if (isDiskOfType(TDEDiskDeviceType::DVDRAM)) {
758  ret = i18n("DVDRAM Drive");
759  }
760  if (isDiskOfType(TDEDiskDeviceType::Zip)) {
761  ret = i18n("Zip Drive");
762  }
763  if (isDiskOfType(TDEDiskDeviceType::Tape)) {
764  ret = i18n("Tape Drive");
765  }
766  if (isDiskOfType(TDEDiskDeviceType::Camera)) {
767  ret = i18n("Digital Camera");
768  }
769 
770  if (isDiskOfType(TDEDiskDeviceType::HDD)) {
771  ret = i18n("Hard Disk Drive");
772  if (checkDiskStatus(TDEDiskDeviceStatus::Hotpluggable)) {
773  ret = i18n("Removable Storage");
774  }
775  if (isDiskOfType(TDEDiskDeviceType::CompactFlash)) {
776  ret = i18n("Compact Flash");
777  }
778  if (isDiskOfType(TDEDiskDeviceType::MemoryStick)) {
779  ret = i18n("Memory Stick");
780  }
781  if (isDiskOfType(TDEDiskDeviceType::SmartMedia)) {
782  ret = i18n("Smart Media");
783  }
784  if (isDiskOfType(TDEDiskDeviceType::SDMMC)) {
785  ret = i18n("Secure Digital");
786  }
787  }
788 
789  if (isDiskOfType(TDEDiskDeviceType::RAM)) {
790  ret = i18n("Random Access Memory");
791  }
792  if (isDiskOfType(TDEDiskDeviceType::Loop)) {
793  ret = i18n("Loop Device");
794  }
795 
796  return ret;
797 }
798 
799 TQPixmap TDEStorageDevice::icon(TDEIcon::StdSizes size) {
800  TQPixmap ret = DesktopIcon("hdd_unmount", size);
801 
802  if (isDiskOfType(TDEDiskDeviceType::Floppy)) {
803  ret = DesktopIcon("3floppy_unmount", size);
804  }
805  if (isDiskOfType(TDEDiskDeviceType::Optical)) {
806  ret = DesktopIcon("cdrom_unmount", size);
807  }
808  if (isDiskOfType(TDEDiskDeviceType::CDROM)) {
809  ret = DesktopIcon("cdrom_unmount", size);
810  }
811  if (isDiskOfType(TDEDiskDeviceType::CDRW)) {
812  ret = DesktopIcon("cdwriter_unmount", size);
813  }
814  if (isDiskOfType(TDEDiskDeviceType::DVDROM)) {
815  ret = DesktopIcon("dvd_unmount", size);
816  }
817  if (isDiskOfType(TDEDiskDeviceType::DVDRW)) {
818  ret = DesktopIcon("dvd_unmount", size);
819  }
820  if (isDiskOfType(TDEDiskDeviceType::DVDRAM)) {
821  ret = DesktopIcon("dvd_unmount", size);
822  }
823  if (isDiskOfType(TDEDiskDeviceType::Zip)) {
824  ret = DesktopIcon("zip_unmount", size);
825  }
826  if (isDiskOfType(TDEDiskDeviceType::Tape)) {
827  ret = DesktopIcon("tape_unmount", size);
828  }
829  if (isDiskOfType(TDEDiskDeviceType::Camera)) {
830  ret = DesktopIcon("camera_unmount");
831  }
832 
833  if (isDiskOfType(TDEDiskDeviceType::HDD)) {
834  ret = DesktopIcon("hdd_unmount", size);
835  if (checkDiskStatus(TDEDiskDeviceStatus::Hotpluggable)) {
836  ret = DesktopIcon("usbpendrive_unmount", size);
837  }
838  if (isDiskOfType(TDEDiskDeviceType::CompactFlash)) {
839  ret = DesktopIcon("compact_flash_unmount", size);
840  }
841  if (isDiskOfType(TDEDiskDeviceType::MemoryStick)) {
842  ret = DesktopIcon("memory_stick_unmount", size);
843  }
844  if (isDiskOfType(TDEDiskDeviceType::SmartMedia)) {
845  ret = DesktopIcon("smart_media_unmount", size);
846  }
847  if (isDiskOfType(TDEDiskDeviceType::SDMMC)) {
848  ret = DesktopIcon("sd_mmc_unmount", size);
849  }
850  }
851 
852  if (isDiskOfType(TDEDiskDeviceType::RAM)) {
853  ret = DesktopIcon("memory", size);
854  }
855  if (isDiskOfType(TDEDiskDeviceType::Loop)) {
856  ret = DesktopIcon("blockdevice", size);
857  }
858 
859  return ret;
860 }
861 
862 unsigned long long TDEStorageDevice::deviceSize() {
863  TQString bsnodename = systemPath();
864  bsnodename.append("/queue/physical_block_size");
865  TQFile bsfile( bsnodename );
866  TQString blocksize;
867  if ( bsfile.open( IO_ReadOnly ) ) {
868  TQTextStream stream( &bsfile );
869  blocksize = stream.readLine();
870  bsfile.close();
871  }
872  else {
873  // Drat, I can't get a guaranteed block size. Assume a block size of 512, as everything I have read indicates that /sys/block/<dev>/size is given in terms of a 512 byte block...
874  blocksize = "512";
875  }
876 
877  TQString dsnodename = systemPath();
878  dsnodename.append("/size");
879  TQFile dsfile( dsnodename );
880  TQString devicesize;
881  if ( dsfile.open( IO_ReadOnly ) ) {
882  TQTextStream stream( &dsfile );
883  devicesize = stream.readLine();
884  dsfile.close();
885  }
886 
887  return ((unsigned long long)blocksize.toULong()*(unsigned long long)devicesize.toULong());
888 }
889 
890 TQString TDEStorageDevice::deviceFriendlySize() {
891  return TDEHardwareDevices::bytesToFriendlySizeString(deviceSize());
892 }
893 
894 TQString TDEStorageDevice::mountPath() {
895  // See if this device node is mounted
896  // This requires parsing /proc/mounts, looking for deviceNode()
897 
898  // The Device Mapper throws a monkey wrench into this
899  // It likes to advertise mounts as /dev/mapper/<something>,
900  // where <something> is listed in <system path>/dm/name
901 
902  // First, ensure that all device information (mainly holders/slaves) is accurate
903  TDEGlobal::hardwareDevices()->rescanDeviceInformation(this);
904 
905  TQString dmnodename = systemPath();
906  dmnodename.append("/dm/name");
907  TQFile namefile( dmnodename );
908  TQString dmaltname;
909  if ( namefile.open( IO_ReadOnly ) ) {
910  TQTextStream stream( &namefile );
911  dmaltname = stream.readLine();
912  namefile.close();
913  }
914  if (!dmaltname.isNull()) {
915  dmaltname.prepend("/dev/mapper/");
916  }
917 
918  TQStringList lines;
919  TQFile file( "/proc/mounts" );
920  if ( file.open( IO_ReadOnly ) ) {
921  TQTextStream stream( &file );
922  TQString line;
923  while ( !stream.atEnd() ) {
924  line = stream.readLine();
925  TQStringList mountInfo = TQStringList::split(" ", line, true);
926  TQString testNode = *mountInfo.at(0);
927  // Check for match
928  if ((testNode == deviceNode()) || (testNode == dmaltname) || (testNode == ("/dev/disk/by-uuid/" + diskUUID()))) {
929  TQString ret = *mountInfo.at(1);
930  ret.replace("\\040", " ");
931  return ret;
932  }
933  lines += line;
934  }
935  file.close();
936  }
937 
938  // While this device is not directly mounted, it could concievably be mounted via the Device Mapper
939  // If so, try to retrieve the mount path...
940  TQStringList slaveDeviceList = holdingDevices();
941  for ( TQStringList::Iterator slavedevit = slaveDeviceList.begin(); slavedevit != slaveDeviceList.end(); ++slavedevit ) {
942  // Try to locate this device path in the TDE device tree
943  TDEHardwareDevices *hwdevices = TDEGlobal::hardwareDevices();
944  TDEGenericDevice *hwdevice = hwdevices->findBySystemPath(*slavedevit);
945  if ((hwdevice) && (hwdevice->type() == TDEGenericDeviceType::Disk)) {
946  TDEStorageDevice* sdevice = static_cast<TDEStorageDevice*>(hwdevice);
947  return sdevice->mountPath();
948  }
949  }
950 
951  return TQString::null;
952 }
953 
954 TQString TDEStorageDevice::mountDevice(TQString mediaName, TQString mountOptions, TQString* errRet, int* retcode) {
955  int internal_retcode;
956  if (!retcode) {
957  retcode = &internal_retcode;
958  }
959 
960  TQString ret = mountPath();
961 
962  if (!ret.isNull()) {
963  return ret;
964  }
965 
966  // Create dummy password file
967  KTempFile passwordFile(TQString::null, "tmp", 0600);
968  passwordFile.setAutoDelete(true);
969 
970  TQString passFileName = passwordFile.name();
971  TQString devNode = deviceNode();
972  passFileName.replace("'", "'\\''");
973  devNode.replace("'", "'\\''");
974  mediaName.replace("'", "'\\''");
975  TQString command = TQString("pmount -p '%1' %2 '%3' '%4' 2>&1").arg(passFileName).arg(mountOptions).arg(devNode).arg(mediaName);
976 
977  FILE *exepipe = popen(command.ascii(), "r");
978  if (exepipe) {
979  TQString pmount_output;
980  char buffer[8092];
981  pmount_output = fgets(buffer, sizeof(buffer), exepipe);
982  *retcode = pclose(exepipe);
983  if (errRet) {
984  *errRet = pmount_output;
985  }
986  }
987 
988  // Update internal mount data
989  TDEGlobal::hardwareDevices()->processModifiedMounts();
990 
991  ret = mountPath();
992 
993  return ret;
994 }
995 
996 TQString TDEStorageDevice::mountEncryptedDevice(TQString passphrase, TQString mediaName, TQString mountOptions, TQString* errRet, int* retcode) {
997  int internal_retcode;
998  if (!retcode) {
999  retcode = &internal_retcode;
1000  }
1001 
1002  TQString ret = mountPath();
1003 
1004  if (!ret.isNull()) {
1005  return ret;
1006  }
1007 
1008  // Create dummy password file
1009  KTempFile passwordFile(TQString::null, "tmp", 0600);
1010  passwordFile.setAutoDelete(true);
1011  TQFile* pwFile = passwordFile.file();
1012  if (!pwFile) {
1013  return TQString::null;
1014  }
1015 
1016  pwFile->writeBlock(passphrase.ascii(), passphrase.length());
1017  pwFile->flush();
1018 
1019  TQString passFileName = passwordFile.name();
1020  TQString devNode = deviceNode();
1021  passFileName.replace("'", "'\\''");
1022  devNode.replace("'", "'\\''");
1023  mediaName.replace("'", "'\\''");
1024  TQString command = TQString("pmount -p '%1' %2 '%3' '%4' 2>&1").arg(passFileName).arg(mountOptions).arg(devNode).arg(mediaName);
1025 
1026  FILE *exepipe = popen(command.ascii(), "r");
1027  if (exepipe) {
1028  TQString pmount_output;
1029  char buffer[8092];
1030  pmount_output = fgets(buffer, sizeof(buffer), exepipe);
1031  *retcode = pclose(exepipe);
1032  if (errRet) {
1033  *errRet = pmount_output;
1034  }
1035  }
1036 
1037  // Update internal mount data
1038  TDEGlobal::hardwareDevices()->processModifiedMounts();
1039 
1040  ret = mountPath();
1041 
1042  return ret;
1043 }
1044 
1045 bool TDEStorageDevice::unmountDevice(TQString* errRet, int* retcode) {
1046  int internal_retcode;
1047  if (!retcode) {
1048  retcode = &internal_retcode;
1049  }
1050 
1051  TQString mountpoint = mountPath();
1052 
1053  if (mountpoint.isNull()) {
1054  return true;
1055  }
1056 
1057  mountpoint.replace("'", "'\\''");
1058  TQString command = TQString("pumount '%1' 2>&1").arg(mountpoint);
1059  FILE *exepipe = popen(command.ascii(), "r");
1060  if (exepipe) {
1061  TQString pmount_output;
1062  char buffer[8092];
1063  pmount_output = fgets(buffer, sizeof(buffer), exepipe);
1064  *retcode = pclose(exepipe);
1065  if (*retcode == 0) {
1066  return true;
1067  }
1068  else {
1069  if (errRet) {
1070  *errRet = pmount_output;
1071  }
1072  }
1073  }
1074 
1075  // Update internal mount data
1076  TDEGlobal::hardwareDevices()->processModifiedMounts();
1077 
1078  return false;
1079 }
1080 
1081 TDECPUDevice::TDECPUDevice(TDEGenericDeviceType::TDEGenericDeviceType dt, TQString dn) : TDEGenericDevice(dt, dn) {
1082  m_frequency = -1;
1083  m_minfrequency = -1;
1084  m_maxfrequency = -1;
1085  m_corenumber = -1;
1086  m_transitionlatency = -1;
1087 }
1088 
1089 TDECPUDevice::~TDECPUDevice() {
1090 }
1091 
1092 double TDECPUDevice::frequency() {
1093  return m_frequency;
1094 }
1095 
1096 void TDECPUDevice::internalSetFrequency(double fr) {
1097  m_frequency = fr;
1098 }
1099 
1100 double TDECPUDevice::minFrequency() {
1101  return m_minfrequency;
1102 }
1103 
1104 void TDECPUDevice::internalSetMinFrequency(double fr) {
1105  m_minfrequency = fr;
1106 }
1107 
1108 double TDECPUDevice::maxFrequency() {
1109  return m_maxfrequency;
1110 }
1111 
1112 void TDECPUDevice::internalSetMaxFrequency(double fr) {
1113  m_maxfrequency = fr;
1114 }
1115 
1116 double TDECPUDevice::transitionLatency() {
1117  return m_transitionlatency;
1118 }
1119 
1120 void TDECPUDevice::internalSetTransitionLatency(double tl) {
1121  m_transitionlatency = tl;
1122 }
1123 
1124 TQString TDECPUDevice::governor() {
1125  return m_governor;
1126 }
1127 
1128 void TDECPUDevice::internalSetGovernor(TQString gr) {
1129  m_governor = gr;
1130 }
1131 
1132 TQString TDECPUDevice::scalingDriver() {
1133  return m_scalingdriver;
1134 }
1135 
1136 void TDECPUDevice::internalSetScalingDriver(TQString dr) {
1137  m_scalingdriver = dr;
1138 }
1139 
1140 TQStringList TDECPUDevice::dependentProcessors() {
1141  return m_tiedprocs;
1142 }
1143 
1144 void TDECPUDevice::internalSetDependentProcessors(TQStringList dp) {
1145  m_tiedprocs = dp;
1146 }
1147 
1148 TQStringList TDECPUDevice::availableFrequencies() {
1149  return m_frequencies;
1150 }
1151 
1152 void TDECPUDevice::internalSetAvailableFrequencies(TQStringList af) {
1153  m_frequencies = af;
1154 }
1155 
1156 TQStringList TDECPUDevice::availableGovernors() {
1157  return m_governers;
1158 }
1159 
1160 void TDECPUDevice::internalSetAvailableGovernors(TQStringList gp) {
1161  m_governers = gp;
1162 }
1163 
1164 void TDECPUDevice::internalSetCoreNumber(int cn) {
1165  m_corenumber = cn;
1166 }
1167 
1168 bool TDECPUDevice::canSetGovernor() {
1169  TQString governornode = systemPath() + "/cpufreq/scaling_governor";
1170  int rval = access (governornode.ascii(), W_OK);
1171  if (rval == 0) {
1172  return TRUE;
1173  }
1174  else {
1175 #ifdef WITH_UPOWER
1176  TQT_DBusConnection dbusConn = TQT_DBusConnection::addConnection(TQT_DBusConnection::SystemBus);
1177  if (dbusConn.isConnected()) {
1178  TQT_DBusProxy hardwareControl("org.trinitydesktop.hardwarecontrol", "/org/trinitydesktop/hardwarecontrol", "org.trinitydesktop.hardwarecontrol.CPUGovernor", dbusConn);
1179  if (hardwareControl.canSend()) {
1180  // can set CPU governor?
1181  TQValueList<TQT_DBusData> params;
1182  params << TQT_DBusData::fromInt32(coreNumber());
1183  TQT_DBusMessage reply = hardwareControl.sendWithReply("CanSetCPUGovernor", params);
1184  if (reply.type() == TQT_DBusMessage::ReplyMessage && reply.count() == 1) {
1185  return reply[0].toVariant().value.toBool();
1186  }
1187  else {
1188  return FALSE;
1189  }
1190  }
1191  else {
1192  return FALSE;
1193  }
1194  }
1195  else {
1196  return FALSE;
1197  }
1198 #else // WITH_UPOWER
1199  return FALSE;
1200 #endif// WITH_UPOWER
1201  }
1202 }
1203 
1204 void TDECPUDevice::setGovernor(TQString gv) {
1205  TQString governornode = systemPath() + "/cpufreq/scaling_governor";
1206  TQFile file( governornode );
1207  if ( file.open( IO_WriteOnly ) ) {
1208  TQTextStream stream( &file );
1209  stream << gv.lower();
1210  file.close();
1211  }
1212 #ifdef WITH_UPOWER
1213  else {
1214  TQT_DBusConnection dbusConn = TQT_DBusConnection::addConnection(TQT_DBusConnection::SystemBus);
1215  if (dbusConn.isConnected()) {
1216  TQT_DBusProxy hardwareControl("org.trinitydesktop.hardwarecontrol", "/org/trinitydesktop/hardwarecontrol", "org.trinitydesktop.hardwarecontrol.CPUGovernor", dbusConn);
1217  if (hardwareControl.canSend()) {
1218  // set CPU governor
1219  TQValueList<TQT_DBusData> params;
1220  params << TQT_DBusData::fromInt32(coreNumber()) << TQT_DBusData::fromString(gv.lower());
1221  hardwareControl.sendWithReply("SetCPUGovernor", params);
1222  }
1223  else {
1224  return;
1225  }
1226  }
1227  else {
1228  return;
1229  }
1230  }
1231 #endif // WITH_UPOWER
1232 
1233  // Force update of the device information object
1234  TDEGlobal::hardwareDevices()->processModifiedCPUs();
1235 }
1236 
1237 bool TDECPUDevice::canSetMaximumScalingFrequency() {
1238  TQString freqnode = systemPath() + "/cpufreq/scaling_max_freq";
1239  int rval = access (freqnode.ascii(), W_OK);
1240  if (rval == 0) {
1241  return TRUE;
1242  }
1243  else {
1244  return FALSE;
1245  }
1246 }
1247 
1248 void TDECPUDevice::setMaximumScalingFrequency(double fr) {
1249  TQString freqnode = systemPath() + "/cpufreq/scaling_max_freq";
1250  TQFile file( freqnode );
1251  if ( file.open( IO_WriteOnly ) ) {
1252  TQTextStream stream( &file );
1253  stream << TQString("%1").arg(fr*1000000.0, 0, 'f', 0);
1254  file.close();
1255  }
1256 
1257  // Force update of the device information object
1258  TDEGlobal::hardwareDevices()->processModifiedCPUs();
1259 }
1260 
1261 int TDECPUDevice::coreNumber() {
1262  return m_corenumber;
1263 }
1264 
1265 TDESensorDevice::TDESensorDevice(TDEGenericDeviceType::TDEGenericDeviceType dt, TQString dn) : TDEGenericDevice(dt, dn) {
1266 }
1267 
1268 TDESensorDevice::~TDESensorDevice() {
1269 }
1270 
1271 TDESensorClusterMap TDESensorDevice::values() {
1272  return m_sensorValues;
1273 }
1274 
1275 void TDESensorDevice::internalSetValues(TDESensorClusterMap cl) {
1276  m_sensorValues = cl;
1277 }
1278 
1279 TDERootSystemDevice::TDERootSystemDevice(TDEGenericDeviceType::TDEGenericDeviceType dt, TQString dn) : TDEGenericDevice(dt, dn) {
1280  m_hibernationSpace = -1;
1281 }
1282 
1283 TDERootSystemDevice::~TDERootSystemDevice() {
1284 }
1285 
1286 TDESystemFormFactor::TDESystemFormFactor TDERootSystemDevice::formFactor() {
1287  return m_formFactor;
1288 }
1289 
1290 void TDERootSystemDevice::internalSetFormFactor(TDESystemFormFactor::TDESystemFormFactor ff) {
1291  m_formFactor = ff;
1292 }
1293 
1294 TDESystemPowerStateList TDERootSystemDevice::powerStates() {
1295  return m_powerStates;
1296 }
1297 
1298 void TDERootSystemDevice::internalSetPowerStates(TDESystemPowerStateList ps) {
1299  m_powerStates = ps;
1300 }
1301 
1302 TDESystemHibernationMethodList TDERootSystemDevice::hibernationMethods() {
1303  return m_hibernationMethods;
1304 }
1305 
1306 void TDERootSystemDevice::internalSetHibernationMethods(TDESystemHibernationMethodList hm) {
1307  m_hibernationMethods = hm;
1308 }
1309 
1310 TDESystemHibernationMethod::TDESystemHibernationMethod TDERootSystemDevice::hibernationMethod() {
1311  return m_hibernationMethod;
1312 }
1313 
1314 void TDERootSystemDevice::internalSetHibernationMethod(TDESystemHibernationMethod::TDESystemHibernationMethod hm) {
1315  m_hibernationMethod = hm;
1316 }
1317 
1318 unsigned long TDERootSystemDevice::diskSpaceNeededForHibernation() {
1319  return m_hibernationSpace;
1320 }
1321 
1322 void TDERootSystemDevice::internalSetDiskSpaceNeededForHibernation(unsigned long sz) {
1323  m_hibernationSpace = sz;
1324 }
1325 
1326 bool TDERootSystemDevice::canSetHibernationMethod() {
1327  TQString hibernationnode = "/sys/power/disk";
1328  int rval = access (hibernationnode.ascii(), W_OK);
1329  if (rval == 0) {
1330  return TRUE;
1331  }
1332  else {
1333  return FALSE;
1334  }
1335 }
1336 
1337 bool TDERootSystemDevice::canStandby() {
1338  TQString statenode = "/sys/power/state";
1339  int rval = access (statenode.ascii(), W_OK);
1340  if (rval == 0) {
1341  if (powerStates().contains(TDESystemPowerState::Standby)) {
1342  return TRUE;
1343  }
1344  else {
1345  return FALSE;
1346  }
1347  }
1348  else {
1349  return FALSE;
1350  }
1351 }
1352 
1353 bool TDERootSystemDevice::canSuspend() {
1354  TQString statenode = "/sys/power/state";
1355  int rval = access (statenode.ascii(), W_OK);
1356  if (rval == 0) {
1357  if (powerStates().contains(TDESystemPowerState::Suspend)) {
1358  return TRUE;
1359  }
1360  else {
1361  return FALSE;
1362  }
1363  }
1364  else {
1365 #ifdef WITH_UPOWER
1366  TQT_DBusConnection dbusConn = TQT_DBusConnection::addConnection(TQT_DBusConnection::SystemBus);
1367  if (dbusConn.isConnected()) {
1368  TQT_DBusProxy upowerProperties("org.freedesktop.UPower", "/org/freedesktop/UPower", "org.freedesktop.DBus.Properties", dbusConn);
1369  if (upowerProperties.canSend()) {
1370  // can suspend?
1371  TQValueList<TQT_DBusData> params;
1372  params << TQT_DBusData::fromString(upowerProperties.interface()) << TQT_DBusData::fromString("CanSuspend");
1373  TQT_DBusMessage reply = upowerProperties.sendWithReply("Get", params);
1374  if (reply.type() == TQT_DBusMessage::ReplyMessage && reply.count() == 1) {
1375  return reply[0].toVariant().value.toBool();
1376  }
1377  else {
1378  return FALSE;
1379  }
1380  }
1381  else {
1382  return FALSE;
1383  }
1384  }
1385  else {
1386  return FALSE;
1387  }
1388 #else // WITH_UPOWER
1389  return FALSE;
1390 #endif// WITH_UPOWER
1391  }
1392 }
1393 
1394 bool TDERootSystemDevice::canHibernate() {
1395  TQString statenode = "/sys/power/state";
1396  int rval = access (statenode.ascii(), W_OK);
1397  if (rval == 0) {
1398  if (powerStates().contains(TDESystemPowerState::Hibernate)) {
1399  return TRUE;
1400  }
1401  else {
1402  return FALSE;
1403  }
1404  }
1405  else {
1406 #ifdef WITH_UPOWER
1407  TQT_DBusConnection dbusConn = TQT_DBusConnection::addConnection(TQT_DBusConnection::SystemBus);
1408  if (dbusConn.isConnected()) {
1409  TQT_DBusProxy upowerProperties("org.freedesktop.UPower", "/org/freedesktop/UPower", "org.freedesktop.DBus.Properties", dbusConn);
1410  if (upowerProperties.canSend()) {
1411  // can hibernate?
1412  TQValueList<TQT_DBusData> params;
1413  params << TQT_DBusData::fromString(upowerProperties.interface()) << TQT_DBusData::fromString("CanHibernate");
1414  TQT_DBusMessage reply = upowerProperties.sendWithReply("Get", params);
1415  if (reply.type() == TQT_DBusMessage::ReplyMessage && reply.count() == 1) {
1416  return reply[0].toVariant().value.toBool();
1417  }
1418  else {
1419  return FALSE;
1420  }
1421  }
1422  else {
1423  return FALSE;
1424  }
1425  }
1426  else {
1427  return FALSE;
1428  }
1429 #else // WITH_UPOWER
1430  return FALSE;
1431 #endif// WITH_UPOWER
1432  }
1433 }
1434 
1435 bool TDERootSystemDevice::canPowerOff() {
1436  TDEConfig *config = TDEGlobal::config();
1437  config->reparseConfiguration(); // config may have changed in the KControl module
1438 
1439  config->setGroup("General" );
1440  bool maysd = false;
1441 #ifdef WITH_CONSOLEKIT
1442  if (config->readBoolEntry( "offerShutdown", true )) {
1443  TQT_DBusConnection dbusConn = TQT_DBusConnection::addConnection(TQT_DBusConnection::SystemBus);
1444  if (dbusConn.isConnected()) {
1445  TQT_DBusProxy consoleKitManager("org.freedesktop.ConsoleKit", "/org/freedesktop/ConsoleKit/Manager", "org.freedesktop.ConsoleKit.Manager", dbusConn);
1446  if (consoleKitManager.canSend()) {
1447  // can power off?
1448  TQValueList<TQT_DBusData> params;
1449  TQT_DBusMessage reply = consoleKitManager.sendWithReply("CanStop", params);
1450  if (reply.type() == TQT_DBusMessage::ReplyMessage && reply.count() == 1) {
1451  maysd = reply[0].toBool();
1452  }
1453  else {
1454  maysd = false;
1455  }
1456  }
1457  else {
1458  maysd = false;
1459  }
1460  }
1461  else {
1462  maysd = false;
1463  }
1464  }
1465 #else // WITH_CONSOLEKIT
1466  // FIXME
1467  // Can we power down this system?
1468  // This should probably be checked via DCOP and therefore interface with KDM
1469  if (config->readBoolEntry( "offerShutdown", true )/* && DM().canShutdown()*/) { // FIXME
1470  maysd = true;
1471  }
1472 #endif // WITH_CONSOLEKIT
1473 
1474  return maysd;
1475 }
1476 
1477 bool TDERootSystemDevice::canReboot() {
1478  TDEConfig *config = TDEGlobal::config();
1479  config->reparseConfiguration(); // config may have changed in the KControl module
1480 
1481  config->setGroup("General" );
1482  bool mayrb = false;
1483 #ifdef WITH_CONSOLEKIT
1484  if (config->readBoolEntry( "offerShutdown", true )) {
1485  TQT_DBusConnection dbusConn = TQT_DBusConnection::addConnection(TQT_DBusConnection::SystemBus);
1486  if (dbusConn.isConnected()) {
1487  TQT_DBusProxy consoleKitManager("org.freedesktop.ConsoleKit", "/org/freedesktop/ConsoleKit/Manager", "org.freedesktop.ConsoleKit.Manager", dbusConn);
1488  if (consoleKitManager.canSend()) {
1489  // can reboot?
1490  TQValueList<TQT_DBusData> params;
1491  TQT_DBusMessage reply = consoleKitManager.sendWithReply("CanRestart", params);
1492  if (reply.type() == TQT_DBusMessage::ReplyMessage && reply.count() == 1) {
1493  mayrb = reply[0].toBool();
1494  }
1495  else {
1496  mayrb = false;
1497  }
1498  }
1499  else {
1500  mayrb = false;
1501  }
1502  }
1503  else {
1504  mayrb = false;
1505  }
1506  }
1507 #else // WITH_CONSOLEKIT
1508  // FIXME
1509  // Can we power down this system?
1510  // This should probably be checked via DCOP and therefore interface with KDM
1511  if (config->readBoolEntry( "offerShutdown", true )/* && DM().canShutdown()*/) { // FIXME
1512  mayrb = true;
1513  }
1514 #endif // WITH_CONSOLEKIT
1515 
1516  return mayrb;
1517 }
1518 
1519 void TDERootSystemDevice::setHibernationMethod(TDESystemHibernationMethod::TDESystemHibernationMethod hm) {
1520  TQString hibernationnode = "/sys/power/disk";
1521  TQFile file( hibernationnode );
1522  if ( file.open( IO_WriteOnly ) ) {
1523  TQString hibernationCommand;
1524  if (hm == TDESystemHibernationMethod::Platform) {
1525  hibernationCommand = "platform";
1526  }
1527  if (hm == TDESystemHibernationMethod::Shutdown) {
1528  hibernationCommand = "shutdown";
1529  }
1530  if (hm == TDESystemHibernationMethod::Reboot) {
1531  hibernationCommand = "reboot";
1532  }
1533  if (hm == TDESystemHibernationMethod::TestProc) {
1534  hibernationCommand = "testproc";
1535  }
1536  if (hm == TDESystemHibernationMethod::Test) {
1537  hibernationCommand = "test";
1538  }
1539  TQTextStream stream( &file );
1540  stream << hibernationCommand;
1541  file.close();
1542  }
1543 }
1544 
1545 bool TDERootSystemDevice::setPowerState(TDESystemPowerState::TDESystemPowerState ps) {
1546  if ((ps == TDESystemPowerState::Standby) || (ps == TDESystemPowerState::Suspend) || (ps == TDESystemPowerState::Hibernate)) {
1547  TQString statenode = "/sys/power/state";
1548  TQFile file( statenode );
1549  if ( file.open( IO_WriteOnly ) ) {
1550  TQString powerCommand;
1551  if (ps == TDESystemPowerState::Standby) {
1552  powerCommand = "standby";
1553  }
1554  if (ps == TDESystemPowerState::Suspend) {
1555  powerCommand = "mem";
1556  }
1557  if (ps == TDESystemPowerState::Hibernate) {
1558  powerCommand = "disk";
1559  }
1560  TQTextStream stream( &file );
1561  stream << powerCommand;
1562  file.close();
1563  return true;
1564  }
1565  else {
1566 #ifdef WITH_UPOWER
1567  TQT_DBusConnection dbusConn;
1568  dbusConn = TQT_DBusConnection::addConnection(TQT_DBusConnection::SystemBus);
1569  if ( dbusConn.isConnected() ) {
1570  if (ps == TDESystemPowerState::Suspend) {
1571  TQT_DBusMessage msg = TQT_DBusMessage::methodCall(
1572  "org.freedesktop.UPower",
1573  "/org/freedesktop/UPower",
1574  "org.freedesktop.UPower",
1575  "Suspend");
1576  dbusConn.sendWithReply(msg);
1577  return true;
1578  }
1579  else if (ps == TDESystemPowerState::Hibernate) {
1580  TQT_DBusMessage msg = TQT_DBusMessage::methodCall(
1581  "org.freedesktop.UPower",
1582  "/org/freedesktop/UPower",
1583  "org.freedesktop.UPower",
1584  "Hibernate");
1585  dbusConn.sendWithReply(msg);
1586  return true;
1587  }
1588  else {
1589  return false;
1590  }
1591  }
1592  else {
1593  return false;
1594  }
1595 #else // WITH_UPOWER
1596  return false;
1597 #endif // WITH_UPOWER
1598  }
1599  }
1600  else if (ps == TDESystemPowerState::PowerOff) {
1601 #ifdef WITH_CONSOLEKIT
1602  TDEConfig *config = TDEGlobal::config();
1603  config->reparseConfiguration(); // config may have changed in the KControl module
1604  config->setGroup("General" );
1605  if (config->readBoolEntry( "offerShutdown", true )) {
1606  TQT_DBusConnection dbusConn;
1607  dbusConn = TQT_DBusConnection::addConnection(TQT_DBusConnection::SystemBus);
1608  if ( dbusConn.isConnected() ) {
1609  TQT_DBusMessage msg = TQT_DBusMessage::methodCall(
1610  "org.freedesktop.ConsoleKit",
1611  "/org/freedesktop/ConsoleKit/Manager",
1612  "org.freedesktop.ConsoleKit.Manager",
1613  "Stop");
1614  dbusConn.sendWithReply(msg);
1615  return true;
1616  }
1617  else {
1618  return false;
1619  }
1620  }
1621  else {
1622  return false;
1623  }
1624 #else // WITH_CONSOLEKIT
1625  // Power down the system using a DCOP command
1626  // Values are explained at http://lists.kde.org/?l=kde-linux&m=115770988603387
1627  TQByteArray data;
1628  TQDataStream arg(data, IO_WriteOnly);
1629  arg << (int)0 << (int)2 << (int)2;
1630  if ( kapp->dcopClient()->send("ksmserver", "default", "logout(int,int,int)", data) ) {
1631  return true;
1632  }
1633  return false;
1634 #endif // WITH_CONSOLEKIT
1635  }
1636  else if (ps == TDESystemPowerState::Reboot) {
1637 #ifdef WITH_CONSOLEKIT
1638  TDEConfig *config = TDEGlobal::config();
1639  config->reparseConfiguration(); // config may have changed in the KControl module
1640  config->setGroup("General" );
1641  if (config->readBoolEntry( "offerShutdown", true )) {
1642  TQT_DBusConnection dbusConn;
1643  dbusConn = TQT_DBusConnection::addConnection(TQT_DBusConnection::SystemBus);
1644  if ( dbusConn.isConnected() ) {
1645  TQT_DBusMessage msg = TQT_DBusMessage::methodCall(
1646  "org.freedesktop.ConsoleKit",
1647  "/org/freedesktop/ConsoleKit/Manager",
1648  "org.freedesktop.ConsoleKit.Manager",
1649  "Restart");
1650  dbusConn.sendWithReply(msg);
1651  return true;
1652  }
1653  else {
1654  return false;
1655  }
1656  }
1657  else {
1658  return false;
1659  }
1660 #else // WITH_CONSOLEKIT
1661  // Power down the system using a DCOP command
1662  // Values are explained at http://lists.kde.org/?l=kde-linux&m=115770988603387
1663  TQByteArray data;
1664  TQDataStream arg(data, IO_WriteOnly);
1665  arg << (int)0 << (int)1 << (int)2;
1666  if ( kapp->dcopClient()->send("ksmserver", "default", "logout(int,int,int)", data) ) {
1667  return true;
1668  }
1669  return false;
1670 #endif // WITH_CONSOLEKIT
1671  }
1672  else if (ps == TDESystemPowerState::Active) {
1673  // Ummm...we're already active...
1674  return true;
1675  }
1676 
1677  return false;
1678 }
1679 
1680 TDEBatteryDevice::TDEBatteryDevice(TDEGenericDeviceType::TDEGenericDeviceType dt, TQString dn) : TDEGenericDevice(dt, dn) {
1681 }
1682 
1683 TDEBatteryDevice::~TDEBatteryDevice() {
1684 }
1685 
1686 double TDEBatteryDevice::voltage() {
1687  return m_currentVoltage;
1688 }
1689 
1690 void TDEBatteryDevice::internalSetVoltage(double vt) {
1691  m_currentVoltage = vt;
1692 }
1693 
1694 double TDEBatteryDevice::maximumVoltage() {
1695  return m_maximumVoltage;
1696 }
1697 
1698 void TDEBatteryDevice::internalSetMaximumVoltage(double vt) {
1699  m_maximumVoltage = vt;
1700 }
1701 
1702 double TDEBatteryDevice::minimumVoltage() {
1703  return m_minimumVoltage;
1704 }
1705 
1706 void TDEBatteryDevice::internalSetMinimumVoltage(double vt) {
1707  m_minimumVoltage = vt;
1708 }
1709 
1710 double TDEBatteryDevice::maximumDesignVoltage() {
1711  return m_maximumDesignVoltage;
1712 }
1713 
1714 void TDEBatteryDevice::internalSetMaximumDesignVoltage(double vt) {
1715  m_maximumDesignVoltage = vt;
1716 }
1717 
1718 double TDEBatteryDevice::energy() {
1719  return m_currentEnergy;
1720 }
1721 
1722 void TDEBatteryDevice::internalSetEnergy(double vt) {
1723  m_currentEnergy = vt;
1724 }
1725 
1726 double TDEBatteryDevice::alarmEnergy() {
1727  return m_alarmEnergy;
1728 }
1729 
1730 void TDEBatteryDevice::internalSetAlarmEnergy(double vt) {
1731  m_alarmEnergy = vt;
1732 }
1733 
1734 double TDEBatteryDevice::maximumEnergy() {
1735  return m_maximumEnergy;
1736 }
1737 
1738 void TDEBatteryDevice::internalSetMaximumEnergy(double vt) {
1739  m_maximumEnergy = vt;
1740 }
1741 
1742 double TDEBatteryDevice::maximumDesignEnergy() {
1743  return m_maximumDesignEnergy;
1744 }
1745 
1746 void TDEBatteryDevice::internalSetMaximumDesignEnergy(double vt) {
1747  m_maximumDesignEnergy = vt;
1748 }
1749 
1750 double TDEBatteryDevice::dischargeRate() {
1751  return m_dischargeRate;
1752 }
1753 
1754 void TDEBatteryDevice::internalSetDischargeRate(double vt) {
1755  m_dischargeRate = vt;
1756 }
1757 
1758 double TDEBatteryDevice::timeRemaining() {
1759  return m_timeRemaining;
1760 }
1761 
1762 void TDEBatteryDevice::internalSetTimeRemaining(double tr) {
1763  m_timeRemaining = tr;
1764 }
1765 
1766 TQString TDEBatteryDevice::technology() {
1767  return m_technology;
1768 }
1769 
1770 void TDEBatteryDevice::internalSetTechnology(TQString tc) {
1771  m_technology = tc;
1772 }
1773 
1774 TDEBatteryStatus::TDEBatteryStatus TDEBatteryDevice::status() {
1775  return m_status;
1776 }
1777 
1778 void TDEBatteryDevice::internalSetStatus(TQString tc) {
1779  tc = tc.lower();
1780 
1781  if (tc == "charging") {
1782  m_status = TDEBatteryStatus::Charging;
1783  }
1784  else if (tc == "discharging") {
1785  m_status = TDEBatteryStatus::Discharging;
1786  }
1787  else if (tc == "full") {
1788  m_status = TDEBatteryStatus::Full;
1789  }
1790  else {
1791  m_status = TDEBatteryStatus::Unknown;
1792  }
1793 }
1794 
1795 bool TDEBatteryDevice::installed() {
1796  return m_installed;
1797 }
1798 
1799 void TDEBatteryDevice::internalSetInstalled(bool tc) {
1800  m_installed = tc;
1801 }
1802 
1803 double TDEBatteryDevice::chargePercent() {
1804  return (m_currentEnergy/m_maximumEnergy)*100.0;
1805 }
1806 
1807 TDEMainsPowerDevice::TDEMainsPowerDevice(TDEGenericDeviceType::TDEGenericDeviceType dt, TQString dn) : TDEGenericDevice(dt, dn) {
1808 }
1809 
1810 TDEMainsPowerDevice::~TDEMainsPowerDevice() {
1811 }
1812 
1813 bool TDEMainsPowerDevice::online() {
1814  return m_online;
1815 }
1816 
1817 void TDEMainsPowerDevice::internalSetOnline(bool tc) {
1818  m_online = tc;
1819 }
1820 
1821 TDENetworkDevice::TDENetworkDevice(TDEGenericDeviceType::TDEGenericDeviceType dt, TQString dn) : TDEGenericDevice(dt, dn) {
1822  m_rxbytes = -1;
1823  m_txbytes = -1;
1824  m_rxpackets = -1;
1825  m_txpackets = -1;
1826  m_connectionManager = NULL;
1827 }
1828 
1829 TDENetworkDevice::~TDENetworkDevice() {
1830  if (m_connectionManager) {
1831  delete m_connectionManager;
1832  }
1833 }
1834 
1835 TQString TDENetworkDevice::macAddress() {
1836  return m_macAddress;
1837 }
1838 
1839 void TDENetworkDevice::internalSetMacAddress(TQString ma) {
1840  m_macAddress = ma;
1841 }
1842 
1843 TQString TDENetworkDevice::state() {
1844  return m_state;
1845 }
1846 
1847 void TDENetworkDevice::internalSetState(TQString st) {
1848  m_state = st;
1849 }
1850 
1851 bool TDENetworkDevice::carrierPresent() {
1852  return m_carrier;
1853 }
1854 
1855 void TDENetworkDevice::internalSetCarrierPresent(bool cp) {
1856  m_carrier = cp;
1857 }
1858 
1859 bool TDENetworkDevice::dormant() {
1860  return m_dormant;
1861 }
1862 
1863 void TDENetworkDevice::internalSetDormant(bool dm) {
1864  m_dormant = dm;
1865 }
1866 
1867 TQString TDENetworkDevice::ipV4Address() {
1868  return m_ipV4Address;
1869 }
1870 
1871 void TDENetworkDevice::internalSetIpV4Address(TQString ad) {
1872  m_ipV4Address = ad;
1873 }
1874 
1875 TQString TDENetworkDevice::ipV6Address() {
1876  return m_ipV6Address;
1877 }
1878 
1879 void TDENetworkDevice::internalSetIpV6Address(TQString ad) {
1880  m_ipV6Address = ad;
1881 }
1882 
1883 TQString TDENetworkDevice::ipV4Netmask() {
1884  return m_ipV4Netmask;
1885 }
1886 
1887 void TDENetworkDevice::internalSetIpV4Netmask(TQString nm) {
1888  m_ipV4Netmask = nm;
1889 }
1890 
1891 TQString TDENetworkDevice::ipV6Netmask() {
1892  return m_ipV6Netmask;
1893 }
1894 
1895 void TDENetworkDevice::internalSetIpV6Netmask(TQString nm) {
1896  m_ipV6Netmask = nm;
1897 }
1898 
1899 TQString TDENetworkDevice::ipV4Broadcast() {
1900  return m_ipV4Broadcast;
1901 }
1902 
1903 void TDENetworkDevice::internalSetIpV4Broadcast(TQString br) {
1904  m_ipV4Broadcast = br;
1905 }
1906 
1907 TQString TDENetworkDevice::ipV6Broadcast() {
1908  return m_ipV6Broadcast;
1909 }
1910 
1911 void TDENetworkDevice::internalSetIpV6Broadcast(TQString br) {
1912  m_ipV6Broadcast = br;
1913 }
1914 
1915 TQString TDENetworkDevice::ipV4Destination() {
1916  return m_ipV4Destination;
1917 }
1918 
1919 void TDENetworkDevice::internalSetIpV4Destination(TQString ds) {
1920  m_ipV4Destination = ds;
1921 }
1922 
1923 TQString TDENetworkDevice::ipV6Destination() {
1924  return m_ipV6Destination;
1925 }
1926 
1927 void TDENetworkDevice::internalSetIpV6Destination(TQString ds) {
1928  m_ipV6Destination = ds;
1929 }
1930 
1931 double TDENetworkDevice::rxBytes() {
1932  return m_rxbytes;
1933 }
1934 
1935 void TDENetworkDevice::internalSetRxBytes(double rx) {
1936  m_rxbytes = rx;
1937 }
1938 
1939 double TDENetworkDevice::txBytes() {
1940  return m_txbytes;
1941 }
1942 
1943 void TDENetworkDevice::internalSetTxBytes(double tx) {
1944  m_txbytes = tx;
1945 }
1946 
1947 double TDENetworkDevice::rxPackets() {
1948  return m_rxpackets;
1949 }
1950 
1951 void TDENetworkDevice::internalSetRxPackets(double rx) {
1952  m_rxpackets = rx;
1953 }
1954 
1955 double TDENetworkDevice::txPackets() {
1956  return m_txpackets;
1957 }
1958 
1959 void TDENetworkDevice::internalSetTxPackets(double tx) {
1960  m_txpackets = tx;
1961 }
1962 
1963 TDENetworkConnectionManager* TDENetworkDevice::connectionManager() {
1964 #ifdef WITH_NETWORK_MANAGER_BACKEND
1965  if (!m_connectionManager) {
1966  m_connectionManager = new TDENetworkConnectionManager_BackendNM(m_macAddress);
1967  }
1968 #endif // WITH_NETWORK_MANAGER_BACKEND
1969 
1970  return m_connectionManager;
1971 }
1972 
1973 void TDENetworkDevice::internalSetConnectionManager(TDENetworkConnectionManager* mgr) {
1974  m_connectionManager = mgr;
1975 }
1976 
1977 TDEBacklightDevice::TDEBacklightDevice(TDEGenericDeviceType::TDEGenericDeviceType dt, TQString dn) : TDEGenericDevice(dt, dn) {
1978 }
1979 
1980 TDEBacklightDevice::~TDEBacklightDevice() {
1981 }
1982 
1983 TDEDisplayPowerLevel::TDEDisplayPowerLevel TDEBacklightDevice::powerLevel() {
1984  return m_powerLevel;
1985 }
1986 
1987 void TDEBacklightDevice::internalSetPowerLevel(TDEDisplayPowerLevel::TDEDisplayPowerLevel pl) {
1988  m_powerLevel = pl;
1989 }
1990 
1991 void TDEBacklightDevice::internalSetMaximumRawBrightness(int br) {
1992  m_maximumBrightness = br;
1993 }
1994 
1995 void TDEBacklightDevice::internalSetCurrentRawBrightness(int br) {
1996  m_currentBrightness = br;
1997 }
1998 
1999 int TDEBacklightDevice::brightnessSteps() {
2000  return m_maximumBrightness + 1;
2001 }
2002 
2003 double TDEBacklightDevice::brightnessPercent() {
2004  return (((m_currentBrightness*1.0)/m_maximumBrightness)*100.0);
2005 }
2006 
2007 bool TDEBacklightDevice::canSetBrightness() {
2008  TQString brightnessnode = systemPath() + "/brightness";
2009  int rval = access (brightnessnode.ascii(), W_OK);
2010  if (rval == 0) {
2011  return TRUE;
2012  }
2013  else {
2014 #ifdef WITH_UPOWER
2015  TQT_DBusConnection dbusConn = TQT_DBusConnection::addConnection(TQT_DBusConnection::SystemBus);
2016  if (dbusConn.isConnected()) {
2017  TQT_DBusProxy hardwareControl("org.trinitydesktop.hardwarecontrol", "/org/trinitydesktop/hardwarecontrol", "org.trinitydesktop.hardwarecontrol.Brightness", dbusConn);
2018  if (hardwareControl.canSend()) {
2019  // can set brightness?
2020  TQValueList<TQT_DBusData> params;
2021  params << TQT_DBusData::fromString(brightnessnode);
2022  TQT_DBusMessage reply = hardwareControl.sendWithReply("CanSetBrightness", params);
2023  if (reply.type() == TQT_DBusMessage::ReplyMessage && reply.count() == 1) {
2024  return reply[0].toVariant().value.toBool();
2025  }
2026  else {
2027  return FALSE;
2028  }
2029  }
2030  else {
2031  return FALSE;
2032  }
2033  }
2034  else {
2035  return FALSE;
2036  }
2037 #else // WITH_UPOWER
2038  return FALSE;
2039 #endif// WITH_UPOWER
2040  }
2041 }
2042 
2043 int TDEBacklightDevice::rawBrightness() {
2044  return m_currentBrightness;
2045 }
2046 
2047 void TDEBacklightDevice::setRawBrightness(int br) {
2048  TQString brightnessnode = systemPath() + "/brightness";
2049  TQString brightnessCommand = TQString("%1").arg(br);
2050  TQFile file( brightnessnode );
2051  if ( file.open( IO_WriteOnly ) ) {
2052  TQTextStream stream( &file );
2053  stream << brightnessCommand;
2054  file.close();
2055  }
2056 #ifdef WITH_UPOWER
2057  else {
2058  TQT_DBusConnection dbusConn = TQT_DBusConnection::addConnection(TQT_DBusConnection::SystemBus);
2059  if (dbusConn.isConnected()) {
2060  TQT_DBusProxy hardwareControl("org.trinitydesktop.hardwarecontrol", "/org/trinitydesktop/hardwarecontrol", "org.trinitydesktop.hardwarecontrol.Brightness", dbusConn);
2061  if (hardwareControl.canSend()) {
2062  // set brightness
2063  TQValueList<TQT_DBusData> params;
2064  params << TQT_DBusData::fromString(brightnessnode) << TQT_DBusData::fromString(brightnessCommand);
2065  hardwareControl.sendWithReply("SetBrightness", params);
2066  }
2067  else {
2068  return;
2069  }
2070  }
2071  else {
2072  return;
2073  }
2074  }
2075 #endif // WITH_UPOWER
2076 }
2077 
2078 TDEMonitorDevice::TDEMonitorDevice(TDEGenericDeviceType::TDEGenericDeviceType dt, TQString dn) : TDEGenericDevice(dt, dn) {
2079 }
2080 
2081 TDEMonitorDevice::~TDEMonitorDevice() {
2082 }
2083 
2084 bool TDEMonitorDevice::connected() {
2085  return m_connected;
2086 }
2087 
2088 void TDEMonitorDevice::internalSetConnected(bool cn) {
2089  m_connected = cn;
2090 }
2091 
2092 bool TDEMonitorDevice::enabled() {
2093  return m_enabled;
2094 }
2095 
2096 void TDEMonitorDevice::internalSetEnabled(bool en) {
2097  m_enabled = en;
2098 }
2099 
2100 TQByteArray TDEMonitorDevice::edid() {
2101  return m_edid;
2102 }
2103 
2104 void TDEMonitorDevice::internalSetEdid(TQByteArray ed) {
2105  m_edid = ed;
2106 }
2107 
2108 TDEResolutionList TDEMonitorDevice::resolutions() {
2109  return m_resolutions;
2110 }
2111 
2112 void TDEMonitorDevice::internalSetResolutions(TDEResolutionList rs) {
2113  m_resolutions = rs;
2114 }
2115 
2116 TQString TDEMonitorDevice::portType() {
2117  return m_portType;
2118 }
2119 
2120 void TDEMonitorDevice::internalSetPortType(TQString pt) {
2121  m_portType = pt;
2122 }
2123 
2124 TDEDisplayPowerLevel::TDEDisplayPowerLevel TDEMonitorDevice::powerLevel() {
2125  return m_powerLevel;
2126 }
2127 
2128 void TDEMonitorDevice::internalSetPowerLevel(TDEDisplayPowerLevel::TDEDisplayPowerLevel pl) {
2129  m_powerLevel = pl;
2130 }
2131 
2132 TDEEventDevice::TDEEventDevice(TDEGenericDeviceType::TDEGenericDeviceType dt, TQString dn) : TDEGenericDevice(dt, dn) {
2133  m_fd = -1;
2134  m_fdMonitorActive = false;
2135 }
2136 
2137 TDEEventDevice::~TDEEventDevice() {
2138  if (m_fd >= 0) {
2139  close(m_fd);
2140  }
2141 }
2142 
2143 TDEEventDeviceType::TDEEventDeviceType TDEEventDevice::eventType() {
2144  return m_eventType;
2145 }
2146 
2147 void TDEEventDevice::internalSetEventType(TDEEventDeviceType::TDEEventDeviceType et) {
2148  m_eventType = et;
2149 }
2150 
2151 TDESwitchType::TDESwitchType TDEEventDevice::providedSwitches() {
2152  return m_providedSwitches;
2153 }
2154 
2155 void TDEEventDevice::internalSetProvidedSwitches(TDESwitchType::TDESwitchType sl) {
2156  m_providedSwitches = sl;
2157 }
2158 
2159 TDESwitchType::TDESwitchType TDEEventDevice::activeSwitches() {
2160  return m_switchActive;
2161 }
2162 
2163 void TDEEventDevice::internalSetActiveSwitches(TDESwitchType::TDESwitchType sl) {
2164  m_switchActive = sl;
2165 }
2166 
2167 // Keep this in sync with the TDESwitchType definition in the header
2168 TQStringList TDEEventDevice::friendlySwitchList(TDESwitchType::TDESwitchType switches) {
2169  TQStringList ret;
2170 
2171  if (switches & TDESwitchType::Lid) {
2172  ret.append(i18n("Lid Switch"));
2173  }
2174  if (switches & TDESwitchType::TabletMode) {
2175  ret.append(i18n("Tablet Mode"));
2176  }
2177  if (switches & TDESwitchType::HeadphoneInsert) {
2178  ret.append(i18n("Headphone Inserted"));
2179  }
2180  if (switches & TDESwitchType::RFKill) {
2181  ret.append(i18n("Radio Frequency Device Kill Switch"));
2182  }
2183  if (switches & TDESwitchType::Radio) {
2184  ret.append(i18n("Enable Radio"));
2185  }
2186  if (switches & TDESwitchType::MicrophoneInsert) {
2187  ret.append(i18n("Microphone Inserted"));
2188  }
2189  if (switches & TDESwitchType::Dock) {
2190  ret.append(i18n("Docked"));
2191  }
2192  if (switches & TDESwitchType::LineOutInsert) {
2193  ret.append(i18n("Line Out Inserted"));
2194  }
2195  if (switches & TDESwitchType::JackPhysicalInsert) {
2196  ret.append(i18n("Physical Jack Inserted"));
2197  }
2198  if (switches & TDESwitchType::VideoOutInsert) {
2199  ret.append(i18n("Video Out Inserted"));
2200  }
2201  if (switches & TDESwitchType::CameraLensCover) {
2202  ret.append(i18n("Camera Lens Cover"));
2203  }
2204  if (switches & TDESwitchType::KeypadSlide) {
2205  ret.append(i18n("Keypad Slide"));
2206  }
2207  if (switches & TDESwitchType::FrontProximity) {
2208  ret.append(i18n("Front Proximity"));
2209  }
2210  if (switches & TDESwitchType::RotateLock) {
2211  ret.append(i18n("Rotate Lock"));
2212  }
2213  if (switches & TDESwitchType::LineInInsert) {
2214  ret.append(i18n("Line In Inserted"));
2215  }
2216 
2217  return ret;
2218 }
2219 
2220 void TDEEventDevice::internalStartFdMonitoring(TDEHardwareDevices* hwmanager) {
2221  if (!m_fdMonitorActive) {
2222  // For security and performance reasons, only monitor known ACPI buttons
2223  if (eventType() != TDEEventDeviceType::Unknown) {
2224  if (m_fd >= 0) {
2225  m_eventNotifier = new TQSocketNotifier(m_fd, TQSocketNotifier::Read, this);
2226  connect( m_eventNotifier, TQT_SIGNAL(activated(int)), this, TQT_SLOT(eventReceived()) );
2227  }
2228  connect( this, TQT_SIGNAL(keyPressed(unsigned int, TDEEventDevice*)), hwmanager, TQT_SLOT(processEventDeviceKeyPressed(unsigned int, TDEEventDevice*)) );
2229  }
2230  m_fdMonitorActive = true;
2231  }
2232 }
2233 
2234 void TDEEventDevice::eventReceived() {
2235  struct input_event ev;
2236  int r;
2237  r = read(m_fd, &ev, sizeof(struct input_event));
2238  if (r > 0) {
2239  if (ev.type == EV_KEY) {
2240  emit keyPressed(ev.code, this);
2241  }
2242  }
2243 }
2244 
2245 TDEInputDevice::TDEInputDevice(TDEGenericDeviceType::TDEGenericDeviceType dt, TQString dn) : TDEGenericDevice(dt, dn) {
2246 }
2247 
2248 TDEInputDevice::~TDEInputDevice() {
2249 }
2250 
2251 TDEInputDeviceType::TDEInputDeviceType TDEInputDevice::inputType() {
2252  return m_inputType;
2253 }
2254 
2255 void TDEInputDevice::internalSetInputType(TDEInputDeviceType::TDEInputDeviceType it) {
2256  m_inputType = it;
2257 }
2258 
2259 TDEHardwareDevices::TDEHardwareDevices() {
2260  // Initialize members
2261  pci_id_map = 0;
2262  usb_id_map = 0;
2263  pnp_id_map = 0;
2264  dpy_id_map = 0;
2265 
2266  // Set up device list
2267  m_deviceList.setAutoDelete( TRUE ); // the list owns the objects
2268 
2269  // Initialize udev interface
2270  m_udevStruct = udev_new();
2271  if (!m_udevStruct) {
2272  printf("Unable to create udev interface\n");
2273  }
2274 
2275  if (m_udevStruct) {
2276  // Set up device add/remove monitoring
2277  m_udevMonitorStruct = udev_monitor_new_from_netlink(m_udevStruct, "udev");
2278  udev_monitor_filter_add_match_subsystem_devtype(m_udevMonitorStruct, NULL, NULL);
2279  udev_monitor_enable_receiving(m_udevMonitorStruct);
2280 
2281  int udevmonitorfd = udev_monitor_get_fd(m_udevMonitorStruct);
2282  if (udevmonitorfd >= 0) {
2283  m_devScanNotifier = new TQSocketNotifier(udevmonitorfd, TQSocketNotifier::Read, this);
2284  connect( m_devScanNotifier, TQT_SIGNAL(activated(int)), this, TQT_SLOT(processHotPluggedHardware()) );
2285  }
2286 
2287  // Read in the current mount table
2288  // Yes, a race condition exists between this and the mount monitor start below, but it shouldn't be a problem 99.99% of the time
2289  m_mountTable.clear();
2290  TQFile file( "/proc/mounts" );
2291  if ( file.open( IO_ReadOnly ) ) {
2292  TQTextStream stream( &file );
2293  while ( !stream.atEnd() ) {
2294  m_mountTable.append(stream.readLine());
2295  }
2296  file.close();
2297  }
2298 
2299  // Monitor for changed mounts
2300  m_procMountsFd = open("/proc/mounts", O_RDONLY, 0);
2301  if (m_procMountsFd >= 0) {
2302  m_mountScanNotifier = new TQSocketNotifier(m_procMountsFd, TQSocketNotifier::Exception, this);
2303  connect( m_mountScanNotifier, TQT_SIGNAL(activated(int)), this, TQT_SLOT(processModifiedMounts()) );
2304  }
2305 
2306  // Read in the current cpu information
2307  // Yes, a race condition exists between this and the cpu monitor start below, but it shouldn't be a problem 99.99% of the time
2308  m_cpuInfo.clear();
2309  TQFile cpufile( "/proc/cpuinfo" );
2310  if ( cpufile.open( IO_ReadOnly ) ) {
2311  TQTextStream stream( &cpufile );
2312  while ( !stream.atEnd() ) {
2313  m_cpuInfo.append(stream.readLine());
2314  }
2315  cpufile.close();
2316  }
2317 
2318 // [FIXME 0.01]
2319 // Apparently the Linux kernel just does not notify userspace applications of CPU frequency changes
2320 // This is STUPID, as it means I have to poll the CPU information structures with a 0.5 second or so timer just to keep the information up to date
2321 #if 0
2322  // Monitor for changed cpu information
2323  // Watched directories are set up during the initial CPU scan
2324  m_cpuWatch = new KSimpleDirWatch(this);
2325  connect( m_cpuWatch, TQT_SIGNAL(dirty(const TQString &)), this, TQT_SLOT(processModifiedCPUs()) );
2326 #else
2327  m_cpuWatchTimer = new TQTimer(this);
2328  connect( m_cpuWatchTimer, SIGNAL(timeout()), this, SLOT(processModifiedCPUs()) );
2329  TQDir nodezerocpufreq("/sys/devices/system/cpu/cpu0/cpufreq");
2330  if (nodezerocpufreq.exists()) {
2331  m_cpuWatchTimer->start( 500, FALSE ); // 0.5 second repeating timer
2332  }
2333 #endif
2334 
2335  // Some devices do not receive update signals from udev
2336  // These devices must be polled, and a good polling interval is 1 second
2337  m_deviceWatchTimer = new TQTimer(this);
2338  connect( m_deviceWatchTimer, SIGNAL(timeout()), this, SLOT(processStatelessDevices()) );
2339  m_deviceWatchTimer->start( 1000, FALSE ); // 1 second repeating timer
2340 
2341  // Update internal device information
2342  queryHardwareInformation();
2343  }
2344 }
2345 
2346 TDEHardwareDevices::~TDEHardwareDevices() {
2347  // Stop device scanning
2348  m_deviceWatchTimer->stop();
2349 
2350 // [FIXME 0.01]
2351 #if 0
2352  // Stop CPU scanning
2353  m_cpuWatch->stopScan();
2354 #else
2355  m_cpuWatchTimer->stop();
2356 #endif
2357 
2358  // Stop mount scanning
2359  close(m_procMountsFd);
2360 
2361  // Tear down udev interface
2362  udev_unref(m_udevStruct);
2363 
2364  // Delete members
2365  if (pci_id_map) {
2366  delete pci_id_map;
2367  }
2368  if (usb_id_map) {
2369  delete usb_id_map;
2370  }
2371  if (pnp_id_map) {
2372  delete pnp_id_map;
2373  }
2374  if (dpy_id_map) {
2375  delete dpy_id_map;
2376  }
2377 }
2378 
2379 void TDEHardwareDevices::rescanDeviceInformation(TDEGenericDevice* hwdevice) {
2380  struct udev_device *dev;
2381  dev = udev_device_new_from_syspath(m_udevStruct, hwdevice->systemPath().ascii());
2382  classifyUnknownDevice(dev, hwdevice, false);
2383  updateParentDeviceInformation(hwdevice); // Update parent/child tables for this device
2384  udev_device_unref(dev);
2385 }
2386 
2387 TDEGenericDevice* TDEHardwareDevices::findBySystemPath(TQString syspath) {
2388  if (!syspath.endsWith("/")) {
2389  syspath += "/";
2390  }
2391  TDEGenericDevice *hwdevice;
2392  // We can't use m_deviceList directly as m_deviceList can only have one iterator active against it at any given time
2393  TDEGenericHardwareList devList = listAllPhysicalDevices();
2394  for ( hwdevice = devList.first(); hwdevice; hwdevice = devList.next() ) {
2395  if (hwdevice->systemPath() == syspath) {
2396  return hwdevice;
2397  }
2398  }
2399 
2400  return 0;
2401 }
2402 
2403 TDEGenericDevice* TDEHardwareDevices::findByUniqueID(TQString uid) {
2404  TDEGenericDevice *hwdevice;
2405  // We can't use m_deviceList directly as m_deviceList can only have one iterator active against it at any given time
2406  TDEGenericHardwareList devList = listAllPhysicalDevices();
2407  for ( hwdevice = devList.first(); hwdevice; hwdevice = devList.next() ) {
2408  if (hwdevice->uniqueID() == uid) {
2409  return hwdevice;
2410  }
2411  }
2412 
2413  return 0;
2414 }
2415 
2416 TDEGenericDevice* TDEHardwareDevices::findByDeviceNode(TQString devnode) {
2417  TDEGenericDevice *hwdevice;
2418  for ( hwdevice = m_deviceList.first(); hwdevice; hwdevice = m_deviceList.next() ) {
2419  if (hwdevice->deviceNode() == devnode) {
2420  return hwdevice;
2421  }
2422  }
2423 
2424  return 0;
2425 }
2426 
2427 TDEStorageDevice* TDEHardwareDevices::findDiskByUID(TQString uid) {
2428  TDEGenericDevice *hwdevice;
2429  for ( hwdevice = m_deviceList.first(); hwdevice; hwdevice = m_deviceList.next() ) {
2430  if (hwdevice->type() == TDEGenericDeviceType::Disk) {
2431  TDEStorageDevice* sdevice = static_cast<TDEStorageDevice*>(hwdevice);
2432  if (sdevice->uniqueID() == uid) {
2433  return sdevice;
2434  }
2435  }
2436  }
2437 
2438  return 0;
2439 }
2440 
2441 void TDEHardwareDevices::processHotPluggedHardware() {
2442  udev_device* dev = udev_monitor_receive_device(m_udevMonitorStruct);
2443  if (dev) {
2444  TQString actionevent(udev_device_get_action(dev));
2445  if (actionevent == "add") {
2446  TDEGenericDevice* device = classifyUnknownDevice(dev);
2447 
2448  // Make sure this device is not a duplicate
2449  TDEGenericDevice *hwdevice;
2450  for (hwdevice = m_deviceList.first(); hwdevice; hwdevice = m_deviceList.next()) {
2451  if (hwdevice->systemPath() == device->systemPath()) {
2452  delete device;
2453  device = 0;
2454  break;
2455  }
2456  }
2457 
2458  if (device) {
2459  m_deviceList.append(device);
2460  updateParentDeviceInformation(device); // Update parent/child tables for this device
2461  emit hardwareAdded(device);
2462  emit hardwareEvent(TDEHardwareEvent::HardwareAdded, device->uniqueID());
2463  }
2464  }
2465  else if (actionevent == "remove") {
2466  // Delete device from hardware listing
2467  TQString systempath(udev_device_get_syspath(dev));
2468  systempath += "/";
2469  TDEGenericDevice *hwdevice;
2470  for (hwdevice = m_deviceList.first(); hwdevice; hwdevice = m_deviceList.next()) {
2471  if (hwdevice->systemPath() == systempath) {
2472  emit hardwareRemoved(hwdevice);
2473  emit hardwareEvent(TDEHardwareEvent::HardwareRemoved, hwdevice->uniqueID());
2474 
2475  // If the device is a storage device and has a slave, update it as well
2476  if (hwdevice->type() == TDEGenericDeviceType::Disk) {
2477  TDEStorageDevice* sdevice = static_cast<TDEStorageDevice*>(hwdevice);
2478  TQStringList slavedevices = sdevice->slaveDevices();
2479  m_deviceList.remove(hwdevice);
2480  for ( TQStringList::Iterator slaveit = slavedevices.begin(); slaveit != slavedevices.end(); ++slaveit ) {
2481  TDEGenericDevice* slavedevice = findBySystemPath(*slaveit);
2482  if (slavedevice) {
2483  rescanDeviceInformation(slavedevice);
2484  emit hardwareUpdated(slavedevice);
2485  emit hardwareEvent(TDEHardwareEvent::HardwareUpdated, slavedevice->uniqueID());
2486  }
2487  }
2488  }
2489  else {
2490  m_deviceList.remove(hwdevice);
2491  }
2492 
2493  break;
2494  }
2495  }
2496  }
2497  else if (actionevent == "change") {
2498  // Update device and emit change event
2499  TQString systempath(udev_device_get_syspath(dev));
2500  systempath += "/";
2501  TDEGenericDevice *hwdevice;
2502  for (hwdevice = m_deviceList.first(); hwdevice; hwdevice = m_deviceList.next()) {
2503  if (hwdevice->systemPath() == systempath) {
2504  if (!hwdevice->blacklistedForUpdate()) {
2505  classifyUnknownDevice(dev, hwdevice, false);
2506  updateParentDeviceInformation(hwdevice); // Update parent/child tables for this device
2507  emit hardwareUpdated(hwdevice);
2508  emit hardwareEvent(TDEHardwareEvent::HardwareUpdated, hwdevice->uniqueID());
2509  }
2510  }
2511  else if ((hwdevice->type() == TDEGenericDeviceType::Monitor)
2512  && (hwdevice->systemPath().contains(systempath))) {
2513  if (!hwdevice->blacklistedForUpdate()) {
2514  struct udev_device *slavedev;
2515  slavedev = udev_device_new_from_syspath(m_udevStruct, hwdevice->systemPath().ascii());
2516  classifyUnknownDevice(slavedev, hwdevice, false);
2517  udev_device_unref(slavedev);
2518  updateParentDeviceInformation(hwdevice); // Update parent/child tables for this device
2519  emit hardwareUpdated(hwdevice);
2520  emit hardwareEvent(TDEHardwareEvent::HardwareUpdated, hwdevice->uniqueID());
2521  }
2522  }
2523  }
2524  }
2525  }
2526 }
2527 
2528 void TDEHardwareDevices::processModifiedCPUs() {
2529  // Detect what changed between the old cpu information and the new information,
2530  // and emit appropriate events
2531 
2532  // Read new CPU information table
2533  m_cpuInfo.clear();
2534  TQFile cpufile( "/proc/cpuinfo" );
2535  if ( cpufile.open( IO_ReadOnly ) ) {
2536  TQTextStream stream( &cpufile );
2537  while ( !stream.atEnd() ) {
2538  m_cpuInfo.append(stream.readLine());
2539  }
2540  cpufile.close();
2541  }
2542 
2543  // Ensure "processor" is the first entry in each block and determine which cpuinfo type is in use
2544  bool cpuinfo_format_x86 = true;
2545  bool cpuinfo_format_arm = false;
2546 
2547  TQString curline1;
2548  TQString curline2;
2549  int blockNumber = 0;
2550  TQStringList::Iterator blockBegin = m_cpuInfo.begin();
2551  for (TQStringList::Iterator cpuit1 = m_cpuInfo.begin(); cpuit1 != m_cpuInfo.end(); ++cpuit1) {
2552  curline1 = *cpuit1;
2553  curline1 = curline1.stripWhiteSpace();
2554  if (!(*blockBegin).startsWith("processor")) {
2555  bool found = false;
2556  TQStringList::Iterator cpuit2;
2557  for (cpuit2 = blockBegin; cpuit2 != m_cpuInfo.end(); ++cpuit2) {
2558  curline2 = *cpuit2;
2559  curline2 = curline2.stripWhiteSpace();
2560  if (curline2.startsWith("processor")) {
2561  found = true;
2562  break;
2563  }
2564  else if (curline2 == "") {
2565  break;
2566  }
2567  }
2568  if (found) {
2569  m_cpuInfo.insert(blockBegin, (*cpuit2));
2570  }
2571  else {
2572  m_cpuInfo.insert(blockBegin, "processor : 0");
2573  }
2574  }
2575  if (curline1 == "") {
2576  blockNumber++;
2577  blockBegin = cpuit1;
2578  blockBegin++;
2579  }
2580  if (curline1.startsWith("Processor")) {
2581  cpuinfo_format_x86 = false;
2582  cpuinfo_format_arm = true;
2583  }
2584  }
2585 
2586  // Parse CPU information table
2587  TDECPUDevice *cdevice;
2588  cdevice = 0;
2589  bool modified = false;
2590  bool have_frequency = false;
2591 
2592  TQString curline;
2593  int processorNumber = 0;
2594  int processorCount = 0;
2595 
2596  if (cpuinfo_format_x86) {
2597  // ===================================================================================================================================
2598  // x86/x86_64
2599  // ===================================================================================================================================
2600  TQStringList::Iterator cpuit;
2601  for (cpuit = m_cpuInfo.begin(); cpuit != m_cpuInfo.end(); ++cpuit) {
2602  curline = *cpuit;
2603  if (curline.startsWith("processor")) {
2604  curline.remove(0, curline.find(":")+1);
2605  curline = curline.stripWhiteSpace();
2606  processorNumber = curline.toInt();
2607  if (!cdevice) cdevice = dynamic_cast<TDECPUDevice*>(findBySystemPath(TQString("/sys/devices/system/cpu/cpu%1").arg(processorNumber)));
2608  if (cdevice) {
2609  if (cdevice->coreNumber() != processorNumber) modified = true;
2610  cdevice->internalSetCoreNumber(processorNumber);
2611  }
2612  }
2613  if (curline.startsWith("model name")) {
2614  curline.remove(0, curline.find(":")+1);
2615  curline = curline.stripWhiteSpace();
2616  if (cdevice) {
2617  if (cdevice->name() != curline) modified = true;
2618  cdevice->internalSetName(curline);
2619  }
2620  }
2621  if (curline.startsWith("cpu MHz")) {
2622  curline.remove(0, curline.find(":")+1);
2623  curline = curline.stripWhiteSpace();
2624  if (cdevice) {
2625  if (cdevice->frequency() != curline.toDouble()) modified = true;
2626  cdevice->internalSetFrequency(curline.toDouble());
2627  have_frequency = true;
2628  }
2629  }
2630  if (curline.startsWith("vendor_id")) {
2631  curline.remove(0, curline.find(":")+1);
2632  curline = curline.stripWhiteSpace();
2633  if (cdevice) {
2634  if (cdevice->vendorName() != curline) modified = true;
2635  cdevice->internalSetVendorName(curline);
2636  if (cdevice->vendorEncoded() != curline) modified = true;
2637  cdevice->internalSetVendorEncoded(curline);
2638  }
2639  }
2640  curline = curline.stripWhiteSpace();
2641  if (curline == "") {
2642  cdevice = 0;
2643  }
2644  }
2645  }
2646  else if (cpuinfo_format_arm) {
2647  // ===================================================================================================================================
2648  // ARM
2649  // ===================================================================================================================================
2650  TQStringList::Iterator cpuit;
2651  TQString modelName;
2652  TQString vendorName;
2653  TQString serialNumber;
2654  for (cpuit = m_cpuInfo.begin(); cpuit != m_cpuInfo.end(); ++cpuit) {
2655  curline = *cpuit;
2656  if (curline.startsWith("Processor")) {
2657  curline.remove(0, curline.find(":")+1);
2658  curline = curline.stripWhiteSpace();
2659  modelName = curline;
2660  }
2661  if (curline.startsWith("Hardware")) {
2662  curline.remove(0, curline.find(":")+1);
2663  curline = curline.stripWhiteSpace();
2664  vendorName = curline;
2665  }
2666  if (curline.startsWith("Serial")) {
2667  curline.remove(0, curline.find(":")+1);
2668  curline = curline.stripWhiteSpace();
2669  serialNumber = curline;
2670  }
2671  }
2672  for (TQStringList::Iterator cpuit = m_cpuInfo.begin(); cpuit != m_cpuInfo.end(); ++cpuit) {
2673  curline = *cpuit;
2674  if (curline.startsWith("processor")) {
2675  curline.remove(0, curline.find(":")+1);
2676  curline = curline.stripWhiteSpace();
2677  processorNumber = curline.toInt();
2678  if (!cdevice) {
2679  cdevice = dynamic_cast<TDECPUDevice*>(findBySystemPath(TQString("/sys/devices/system/cpu/cpu%1").arg(processorNumber)));
2680  if (cdevice) {
2681  // Set up CPU information structures
2682  if (cdevice->coreNumber() != processorNumber) modified = true;
2683  cdevice->internalSetCoreNumber(processorNumber);
2684  if (cdevice->name() != modelName) modified = true;
2685  cdevice->internalSetName(modelName);
2686  if (cdevice->vendorName() != vendorName) modified = true;
2687  cdevice->internalSetVendorName(vendorName);
2688  if (cdevice->vendorEncoded() != vendorName) modified = true;
2689  cdevice->internalSetVendorEncoded(vendorName);
2690  if (cdevice->serialNumber() != serialNumber) modified = true;
2691  cdevice->internalSetSerialNumber(serialNumber);
2692  }
2693  }
2694  }
2695  curline = curline.stripWhiteSpace();
2696  if (curline == "") {
2697  cdevice = 0;
2698  }
2699  }
2700  }
2701 
2702  processorCount = processorNumber+1;
2703 
2704  // Read in other information from cpufreq, if available
2705  for (processorNumber=0; processorNumber<processorCount; processorNumber++) {
2706  cdevice = dynamic_cast<TDECPUDevice*>(findBySystemPath(TQString("/sys/devices/system/cpu/cpu%1").arg(processorNumber)));
2707  TQDir cpufreq_dir(TQString("/sys/devices/system/cpu/cpu%1/cpufreq").arg(processorNumber));
2708  TQString scalinggovernor;
2709  TQString scalingdriver;
2710  double minfrequency = -1;
2711  double maxfrequency = -1;
2712  double trlatency = -1;
2713  TQStringList affectedcpulist;
2714  TQStringList frequencylist;
2715  TQStringList governorlist;
2716  if (cpufreq_dir.exists()) {
2717  TQString nodename = cpufreq_dir.path();
2718  nodename.append("/scaling_governor");
2719  TQFile scalinggovernorfile(nodename);
2720  if (scalinggovernorfile.open(IO_ReadOnly)) {
2721  TQTextStream stream( &scalinggovernorfile );
2722  scalinggovernor = stream.readLine();
2723  scalinggovernorfile.close();
2724  }
2725  nodename = cpufreq_dir.path();
2726  nodename.append("/scaling_driver");
2727  TQFile scalingdriverfile(nodename);
2728  if (scalingdriverfile.open(IO_ReadOnly)) {
2729  TQTextStream stream( &scalingdriverfile );
2730  scalingdriver = stream.readLine();
2731  scalingdriverfile.close();
2732  }
2733  nodename = cpufreq_dir.path();
2734  nodename.append("/cpuinfo_min_freq");
2735  TQFile minfrequencyfile(nodename);
2736  if (minfrequencyfile.open(IO_ReadOnly)) {
2737  TQTextStream stream( &minfrequencyfile );
2738  minfrequency = stream.readLine().toDouble()/1000.0;
2739  minfrequencyfile.close();
2740  }
2741  nodename = cpufreq_dir.path();
2742  nodename.append("/cpuinfo_max_freq");
2743  TQFile maxfrequencyfile(nodename);
2744  if (maxfrequencyfile.open(IO_ReadOnly)) {
2745  TQTextStream stream( &maxfrequencyfile );
2746  maxfrequency = stream.readLine().toDouble()/1000.0;
2747  maxfrequencyfile.close();
2748  }
2749  nodename = cpufreq_dir.path();
2750  nodename.append("/cpuinfo_transition_latency");
2751  TQFile trlatencyfile(nodename);
2752  if (trlatencyfile.open(IO_ReadOnly)) {
2753  TQTextStream stream( &trlatencyfile );
2754  trlatency = stream.readLine().toDouble()/1000.0;
2755  trlatencyfile.close();
2756  }
2757  nodename = cpufreq_dir.path();
2758  nodename.append("/affected_cpus");
2759  TQFile tiedcpusfile(nodename);
2760  if (tiedcpusfile.open(IO_ReadOnly)) {
2761  TQTextStream stream( &tiedcpusfile );
2762  affectedcpulist = TQStringList::split(" ", stream.readLine());
2763  tiedcpusfile.close();
2764  }
2765  nodename = cpufreq_dir.path();
2766  nodename.append("/scaling_available_frequencies");
2767  TQFile availfreqsfile(nodename);
2768  if (availfreqsfile.open(IO_ReadOnly)) {
2769  TQTextStream stream( &availfreqsfile );
2770  frequencylist = TQStringList::split(" ", stream.readLine());
2771  availfreqsfile.close();
2772  }
2773  nodename = cpufreq_dir.path();
2774  nodename.append("/scaling_available_governors");
2775  TQFile availgvrnsfile(nodename);
2776  if (availgvrnsfile.open(IO_ReadOnly)) {
2777  TQTextStream stream( &availgvrnsfile );
2778  governorlist = TQStringList::split(" ", stream.readLine());
2779  availgvrnsfile.close();
2780  }
2781 
2782  if (!have_frequency) {
2783  nodename = cpufreq_dir.path();
2784  nodename.append("/cpuinfo_cur_freq");
2785  TQFile cpufreqfile(nodename);
2786  if (cpufreqfile.open(IO_ReadOnly)) {
2787  TQTextStream stream( &cpufreqfile );
2788  if (cdevice) cdevice->internalSetFrequency(stream.readLine().toDouble()/1000.0);
2789  cpufreqfile.close();
2790  have_frequency = true;
2791  }
2792  }
2793 
2794  bool frequencyFound;
2795  TQStringList::Iterator freqit;
2796  frequencyFound = false;
2797  for ( freqit = frequencylist.begin(); freqit != frequencylist.end(); ++freqit ) {
2798  double thisfrequency = (*freqit).toDouble()/1000.0;
2799  if (thisfrequency == minfrequency) {
2800  frequencyFound = true;
2801  }
2802  }
2803  if (!frequencyFound) {
2804  int minFrequencyInt = (minfrequency*1000.0);
2805  frequencylist.prepend(TQString("%1").arg(minFrequencyInt));
2806  }
2807  frequencyFound = false;
2808  for ( freqit = frequencylist.begin(); freqit != frequencylist.end(); ++freqit ) {
2809  double thisfrequency = (*freqit).toDouble()/1000.0;
2810  if (thisfrequency == maxfrequency) {
2811  frequencyFound = true;
2812  }
2813  }
2814  if (!frequencyFound) {
2815  int maxfrequencyInt = (maxfrequency*1000.0);
2816  frequencylist.append(TQString("%1").arg(maxfrequencyInt));
2817  }
2818  }
2819  else {
2820  if (have_frequency) {
2821  if (cdevice) {
2822  minfrequency = cdevice->frequency();
2823  maxfrequency = cdevice->frequency();
2824  }
2825  }
2826  }
2827 
2828  // Update CPU information structure
2829  if (cdevice) {
2830  if (cdevice->governor() != scalinggovernor) modified = true;
2831  cdevice->internalSetGovernor(scalinggovernor);
2832  if (cdevice->scalingDriver() != scalingdriver) modified = true;
2833  cdevice->internalSetScalingDriver(scalingdriver);
2834  if (cdevice->minFrequency() != minfrequency) modified = true;
2835  cdevice->internalSetMinFrequency(minfrequency);
2836  if (cdevice->maxFrequency() != maxfrequency) modified = true;
2837  cdevice->internalSetMaxFrequency(maxfrequency);
2838  if (cdevice->transitionLatency() != trlatency) modified = true;
2839  cdevice->internalSetTransitionLatency(trlatency);
2840  if (cdevice->dependentProcessors().join(" ") != affectedcpulist.join(" ")) modified = true;
2841  cdevice->internalSetDependentProcessors(affectedcpulist);
2842  if (cdevice->availableFrequencies().join(" ") != frequencylist.join(" ")) modified = true;
2843  cdevice->internalSetAvailableFrequencies(frequencylist);
2844  if (cdevice->availableGovernors().join(" ") != governorlist.join(" ")) modified = true;
2845  cdevice->internalSetAvailableGovernors(governorlist);
2846  }
2847  }
2848 
2849  if (modified) {
2850  for (processorNumber=0; processorNumber<processorCount; processorNumber++) {
2851  TDEGenericDevice* hwdevice = findBySystemPath(TQString("/sys/devices/system/cpu/cpu%1").arg(processorNumber));
2852  // Signal new information available
2853  emit hardwareUpdated(hwdevice);
2854  emit hardwareEvent(TDEHardwareEvent::HardwareUpdated, hwdevice->uniqueID());
2855  }
2856  }
2857 }
2858 
2859 void TDEHardwareDevices::processStatelessDevices() {
2860  // Some devices do not emit changed signals
2861  // So far, network cards and sensors need to be polled
2862  TDEGenericDevice *hwdevice;
2863 
2864  // We can't use m_deviceList directly as m_deviceList can only have one iterator active against it at any given time
2865  TDEGenericHardwareList devList = listAllPhysicalDevices();
2866  for ( hwdevice = devList.first(); hwdevice; hwdevice = devList.next() ) {
2867  if ((hwdevice->type() == TDEGenericDeviceType::RootSystem) || (hwdevice->type() == TDEGenericDeviceType::Network) || (hwdevice->type() == TDEGenericDeviceType::OtherSensor) || (hwdevice->type() == TDEGenericDeviceType::Event) || (hwdevice->type() == TDEGenericDeviceType::Battery) || (hwdevice->type() == TDEGenericDeviceType::PowerSupply)) {
2868  rescanDeviceInformation(hwdevice);
2869  emit hardwareUpdated(hwdevice);
2870  emit hardwareEvent(TDEHardwareEvent::HardwareUpdated, hwdevice->uniqueID());
2871  }
2872  }
2873 }
2874 
2875 void TDEHardwareDevices::processEventDeviceKeyPressed(unsigned int keycode, TDEEventDevice* edevice) {
2876  emit eventDeviceKeyPressed(keycode, edevice);
2877 }
2878 
2879 void TDEHardwareDevices::processModifiedMounts() {
2880  // Detect what changed between the old mount table and the new one,
2881  // and emit appropriate events
2882 
2883  TQStringList deletedEntries = m_mountTable;
2884 
2885  // Read in the new mount table
2886  m_mountTable.clear();
2887  TQFile file( "/proc/mounts" );
2888  if ( file.open( IO_ReadOnly ) ) {
2889  TQTextStream stream( &file );
2890  while ( !stream.atEnd() ) {
2891  m_mountTable.append(stream.readLine());
2892  }
2893  file.close();
2894  }
2895 
2896  TQStringList addedEntries = m_mountTable;
2897 
2898  // Remove all entries that are identical in both tables
2899  processModifiedMounts_removeagain:
2900  for ( TQStringList::Iterator delit = deletedEntries.begin(); delit != deletedEntries.end(); ++delit ) {
2901  for ( TQStringList::Iterator addit = addedEntries.begin(); addit != addedEntries.end(); ++addit ) {
2902  if ((*delit) == (*addit)) {
2903  deletedEntries.remove(delit);
2904  addedEntries.remove(addit);
2905  // Reset iterators to prevent bugs/crashes
2906  // FIXME
2907  // Is there any way to completely reset both loops without using goto?
2908  goto processModifiedMounts_removeagain;
2909  }
2910  }
2911  }
2912 
2913  TQStringList::Iterator it;
2914  for ( it = addedEntries.begin(); it != addedEntries.end(); ++it ) {
2915  TQStringList mountInfo = TQStringList::split(" ", (*it), true);
2916  // Try to find a device that matches the altered node
2917  TDEGenericDevice* hwdevice = findByDeviceNode(*mountInfo.at(0));
2918  if (hwdevice) {
2919  emit hardwareUpdated(hwdevice);
2920  emit hardwareEvent(TDEHardwareEvent::HardwareUpdated, hwdevice->uniqueID());
2921  // If the device is a storage device and has a slave, update it as well
2922  if (hwdevice->type() == TDEGenericDeviceType::Disk) {
2923  TDEStorageDevice* sdevice = static_cast<TDEStorageDevice*>(hwdevice);
2924  TQStringList slavedevices = sdevice->slaveDevices();
2925  for ( TQStringList::Iterator slaveit = slavedevices.begin(); slaveit != slavedevices.end(); ++slaveit ) {
2926  TDEGenericDevice* slavedevice = findBySystemPath(*slaveit);
2927  if (slavedevice) {
2928  emit hardwareUpdated(slavedevice);
2929  emit hardwareEvent(TDEHardwareEvent::HardwareUpdated, slavedevice->uniqueID());
2930  }
2931  }
2932  }
2933  }
2934  }
2935  for ( it = deletedEntries.begin(); it != deletedEntries.end(); ++it ) {
2936  TQStringList mountInfo = TQStringList::split(" ", (*it), true);
2937  // Try to find a device that matches the altered node
2938  TDEGenericDevice* hwdevice = findByDeviceNode(*mountInfo.at(0));
2939  if (hwdevice) {
2940  emit hardwareUpdated(hwdevice);
2941  emit hardwareEvent(TDEHardwareEvent::HardwareUpdated, hwdevice->uniqueID());
2942  // If the device is a storage device and has a slave, update it as well
2943  if (hwdevice->type() == TDEGenericDeviceType::Disk) {
2944  TDEStorageDevice* sdevice = static_cast<TDEStorageDevice*>(hwdevice);
2945  TQStringList slavedevices = sdevice->slaveDevices();
2946  for ( TQStringList::Iterator slaveit = slavedevices.begin(); slaveit != slavedevices.end(); ++slaveit ) {
2947  TDEGenericDevice* slavedevice = findBySystemPath(*slaveit);
2948  if (slavedevice) {
2949  emit hardwareUpdated(slavedevice);
2950  emit hardwareEvent(TDEHardwareEvent::HardwareUpdated, slavedevice->uniqueID());
2951  }
2952  }
2953  }
2954  }
2955  }
2956 
2957  emit mountTableModified();
2958  emit hardwareEvent(TDEHardwareEvent::MountTableModified, TQString());
2959 }
2960 
2961 TDEDiskDeviceType::TDEDiskDeviceType classifyDiskType(udev_device* dev, const TQString devicebus, const TQString disktypestring, const TQString systempath, const TQString devicevendor, const TQString devicemodel, const TQString filesystemtype, const TQString devicedriver) {
2962  // Classify a disk device type to the best of our ability
2963  TDEDiskDeviceType::TDEDiskDeviceType disktype = TDEDiskDeviceType::Null;
2964 
2965  if (devicebus.upper() == "USB") {
2966  disktype = disktype | TDEDiskDeviceType::USB;
2967  }
2968 
2969  if (disktypestring.upper() == "ZIP") {
2970  disktype = disktype | TDEDiskDeviceType::Zip;
2971  }
2972  if ((devicevendor.upper() == "IOMEGA") && (devicemodel.upper().contains("ZIP"))) {
2973  disktype = disktype | TDEDiskDeviceType::Zip;
2974  }
2975 
2976  if ((devicevendor.upper() == "APPLE") && (devicemodel.upper().contains("IPOD"))) {
2977  disktype = disktype | TDEDiskDeviceType::MediaDevice;
2978  }
2979  if ((devicevendor.upper() == "SANDISK") && (devicemodel.upper().contains("SANSA"))) {
2980  disktype = disktype | TDEDiskDeviceType::MediaDevice;
2981  }
2982 
2983  if (disktypestring.upper() == "TAPE") {
2984  disktype = disktype | TDEDiskDeviceType::Tape;
2985  }
2986 
2987  if ((disktypestring.upper() == "COMPACT_FLASH")
2988  || (TQString(udev_device_get_property_value(dev, "ID_DRIVE_FLASH_CF")) == "1")) {
2989  disktype = disktype | TDEDiskDeviceType::CompactFlash;
2990  }
2991 
2992  if ((disktypestring.upper() == "MEMORY_STICK")
2993  || (TQString(udev_device_get_property_value(dev, "ID_DRIVE_FLASH_MS")) == "1")) {
2994  disktype = disktype | TDEDiskDeviceType::MemoryStick;
2995  }
2996 
2997  if ((disktypestring.upper() == "SMART_MEDIA")
2998  || (TQString(udev_device_get_property_value(dev, "ID_DRIVE_FLASH_SM")) == "1")) {
2999  disktype = disktype | TDEDiskDeviceType::SmartMedia;
3000  }
3001 
3002  if ((disktypestring.upper() == "SD_MMC")
3003  || (TQString(udev_device_get_property_value(dev, "ID_DRIVE_FLASH_SD")) == "1")
3004  || (TQString(udev_device_get_property_value(dev, "ID_DRIVE_FLASH_SDHC")) == "1")
3005  || (TQString(udev_device_get_property_value(dev, "ID_DRIVE_FLASH_MMC")) == "1")) {
3006  disktype = disktype | TDEDiskDeviceType::SDMMC;
3007  }
3008 
3009  if ((disktypestring.upper() == "FLASHKEY")
3010  || (TQString(udev_device_get_property_value(dev, " ID_DRIVE_FLASH")) == "1")) {
3011  disktype = disktype | TDEDiskDeviceType::Flash;
3012  }
3013 
3014  if (disktypestring.upper() == "OPTICAL") {
3015  disktype = disktype | TDEDiskDeviceType::Optical;
3016  }
3017 
3018  if (disktypestring.upper() == "JAZ") {
3019  disktype = disktype | TDEDiskDeviceType::Jaz;
3020  }
3021 
3022  if (disktypestring.upper() == "DISK") {
3023  disktype = disktype | TDEDiskDeviceType::HDD;
3024  }
3025  if (disktypestring.isNull()) {
3026  // Fallback
3027  // If we can't recognize the disk type then set it as a simple HDD volume
3028  disktype = disktype | TDEDiskDeviceType::HDD;
3029  }
3030 
3031  // Certain combinations of media flags should never be set at the same time as they don't make sense
3032  // This block is needed as udev is more than happy to provide inconsistent data to us
3033  if ((disktype & TDEDiskDeviceType::Zip) || (disktype & TDEDiskDeviceType::Floppy) || (disktype & TDEDiskDeviceType::Jaz) || (disktype & TDEDiskDeviceType::Tape)) {
3034  disktype = disktype & ~TDEDiskDeviceType::HDD;
3035  }
3036 
3037  if (disktypestring.upper() == "CD") {
3038  disktype = disktype & ~TDEDiskDeviceType::HDD;
3039  disktype = disktype | TDEDiskDeviceType::Optical;
3040 
3041  if (TQString(udev_device_get_property_value(dev, "ID_CDROM_MEDIA")) == "1") {
3042  disktype = disktype | TDEDiskDeviceType::CDROM;
3043  }
3044  if (TQString(udev_device_get_property_value(dev, "ID_CDROM_MEDIA_CD_RW")) == "1") {
3045  disktype = disktype | TDEDiskDeviceType::CDRW;
3046  disktype = disktype & ~TDEDiskDeviceType::CDROM;
3047  }
3048  if (TQString(udev_device_get_property_value(dev, "ID_CDROM_MEDIA_DVD")) == "1") {
3049  disktype = disktype | TDEDiskDeviceType::DVDROM;
3050  disktype = disktype & ~TDEDiskDeviceType::CDROM;
3051  }
3052  if (TQString(udev_device_get_property_value(dev, "ID_CDROM_MEDIA_DVD_RAM")) == "1") {
3053  disktype = disktype | TDEDiskDeviceType::DVDRAM;
3054  disktype = disktype & ~TDEDiskDeviceType::DVDROM;
3055  }
3056  if ((TQString(udev_device_get_property_value(dev, "ID_CDROM_MEDIA_DVD_R")) == "1")
3057  || (TQString(udev_device_get_property_value(dev, "ID_CDROM_MEDIA_DVD_R_DL")) == "1")
3058  || (TQString(udev_device_get_property_value(dev, "ID_CDROM_MEDIA_DVD_PLUS_R")) == "1")
3059  || (TQString(udev_device_get_property_value(dev, "ID_CDROM_MEDIA_DVD_MINUS_R")) == "1")
3060  || (TQString(udev_device_get_property_value(dev, "ID_CDROM_MEDIA_DVD_PLUS_R_DL")) == "1")
3061  || (TQString(udev_device_get_property_value(dev, "ID_CDROM_MEDIA_DVD_MINUS_R_DL")) == "1")
3062  ) {
3063  disktype = disktype | TDEDiskDeviceType::DVDRW;
3064  disktype = disktype & ~TDEDiskDeviceType::DVDROM;
3065  }
3066  if ((TQString(udev_device_get_property_value(dev, "ID_CDROM_MEDIA_DVD_RW")) == "1")
3067  || (TQString(udev_device_get_property_value(dev, "ID_CDROM_MEDIA_DVD_RW_DL")) == "1")
3068  || (TQString(udev_device_get_property_value(dev, "ID_CDROM_MEDIA_DVD_PLUS_RW")) == "1")
3069  || (TQString(udev_device_get_property_value(dev, "ID_CDROM_MEDIA_DVD_MINUS_RW")) == "1")
3070  || (TQString(udev_device_get_property_value(dev, "ID_CDROM_MEDIA_DVD_PLUS_RW_DL")) == "1")
3071  || (TQString(udev_device_get_property_value(dev, "ID_CDROM_MEDIA_DVD_MINUS_RW_DL")) == "1")
3072  ) {
3073  disktype = disktype | TDEDiskDeviceType::DVDRW; // FIXME
3074  disktype = disktype & ~TDEDiskDeviceType::DVDROM;
3075  }
3076  if (TQString(udev_device_get_property_value(dev, "ID_CDROM_MEDIA_BD")) == "1") {
3077  disktype = disktype | TDEDiskDeviceType::BDROM;
3078  disktype = disktype & ~TDEDiskDeviceType::CDROM;
3079  }
3080  if ((TQString(udev_device_get_property_value(dev, "ID_CDROM_MEDIA_BD_R")) == "1")
3081  || (TQString(udev_device_get_property_value(dev, "ID_CDROM_MEDIA_BD_R_DL")) == "1")
3082  || (TQString(udev_device_get_property_value(dev, "ID_CDROM_MEDIA_BD_PLUS_R")) == "1")
3083  || (TQString(udev_device_get_property_value(dev, "ID_CDROM_MEDIA_BD_MINUS_R")) == "1")
3084  || (TQString(udev_device_get_property_value(dev, "ID_CDROM_MEDIA_DVD_PLUS_R_DL")) == "1")
3085  || (TQString(udev_device_get_property_value(dev, "ID_CDROM_MEDIA_DVD_MINUS_R_DL")) == "1")
3086  ) {
3087  disktype = disktype | TDEDiskDeviceType::BDRW; // FIXME
3088  disktype = disktype & ~TDEDiskDeviceType::BDROM;
3089  }
3090  if ((TQString(udev_device_get_property_value(dev, "ID_CDROM_MEDIA_BD_RW")) == "1")
3091  || (TQString(udev_device_get_property_value(dev, "ID_CDROM_MEDIA_BD_RW_DL")) == "1")
3092  || (TQString(udev_device_get_property_value(dev, "ID_CDROM_MEDIA_BD_PLUS_RW")) == "1")
3093  || (TQString(udev_device_get_property_value(dev, "ID_CDROM_MEDIA_BD_MINUS_RW")) == "1")
3094  || (TQString(udev_device_get_property_value(dev, "ID_CDROM_MEDIA_DVD_PLUS_RW_DL")) == "1")
3095  || (TQString(udev_device_get_property_value(dev, "ID_CDROM_MEDIA_DVD_MINUS_RW_DL")) == "1")
3096  ) {
3097  disktype = disktype | TDEDiskDeviceType::BDRW;
3098  disktype = disktype & ~TDEDiskDeviceType::BDROM;
3099  }
3100  if (!TQString(udev_device_get_property_value(dev, "ID_CDROM_MEDIA_TRACK_COUNT_AUDIO")).isNull()) {
3101  disktype = disktype | TDEDiskDeviceType::CDAudio;
3102  }
3103  if ((TQString(udev_device_get_property_value(dev, "ID_CDROM_MEDIA_VCD")) == "1") || (TQString(udev_device_get_property_value(dev, "ID_CDROM_MEDIA_SDVD")) == "1")) {
3104  disktype = disktype | TDEDiskDeviceType::CDVideo;
3105  }
3106  }
3107 
3108  // Detect RAM and Loop devices, since udev can't seem to...
3109  if (systempath.startsWith("/sys/devices/virtual/block/ram")) {
3110  disktype = disktype | TDEDiskDeviceType::RAM;
3111  }
3112  if (systempath.startsWith("/sys/devices/virtual/block/loop")) {
3113  disktype = disktype | TDEDiskDeviceType::Loop;
3114  }
3115 
3116  if (filesystemtype.upper() == "CRYPTO_LUKS") {
3117  disktype = disktype | TDEDiskDeviceType::LUKS;
3118  }
3119  else if (filesystemtype.upper() == "CRYPTO") {
3120  disktype = disktype | TDEDiskDeviceType::OtherCrypted;
3121  }
3122 
3123  return disktype;
3124 }
3125 
3126  // TDEStandardDirs::kde_default
3127 
3128 typedef TQMap<TQString, TQString> TDEConfigMap;
3129 
3130 TQString readUdevAttribute(udev_device* dev, TQString attr) {
3131  return TQString(udev_device_get_property_value(dev, attr.ascii()));
3132 }
3133 
3134 TDEGenericDeviceType::TDEGenericDeviceType readGenericDeviceTypeFromString(TQString query) {
3135  TDEGenericDeviceType::TDEGenericDeviceType ret = TDEGenericDeviceType::Other;
3136 
3137  // Keep this in sync with the TDEGenericDeviceType definition in the header
3138  if (query == "Root") {
3139  ret = TDEGenericDeviceType::Root;
3140  }
3141  else if (query == "RootSystem") {
3142  ret = TDEGenericDeviceType::RootSystem;
3143  }
3144  else if (query == "CPU") {
3145  ret = TDEGenericDeviceType::CPU;
3146  }
3147  else if (query == "GPU") {
3148  ret = TDEGenericDeviceType::GPU;
3149  }
3150  else if (query == "RAM") {
3151  ret = TDEGenericDeviceType::RAM;
3152  }
3153  else if (query == "Bus") {
3154  ret = TDEGenericDeviceType::Bus;
3155  }
3156  else if (query == "I2C") {
3157  ret = TDEGenericDeviceType::I2C;
3158  }
3159  else if (query == "MDIO") {
3160  ret = TDEGenericDeviceType::MDIO;
3161  }
3162  else if (query == "Mainboard") {
3163  ret = TDEGenericDeviceType::Mainboard;
3164  }
3165  else if (query == "Disk") {
3166  ret = TDEGenericDeviceType::Disk;
3167  }
3168  else if (query == "SCSI") {
3169  ret = TDEGenericDeviceType::SCSI;
3170  }
3171  else if (query == "StorageController") {
3172  ret = TDEGenericDeviceType::StorageController;
3173  }
3174  else if (query == "Mouse") {
3175  ret = TDEGenericDeviceType::Mouse;
3176  }
3177  else if (query == "Keyboard") {
3178  ret = TDEGenericDeviceType::Keyboard;
3179  }
3180  else if (query == "HID") {
3181  ret = TDEGenericDeviceType::HID;
3182  }
3183  else if (query == "Modem") {
3184  ret = TDEGenericDeviceType::Modem;
3185  }
3186  else if (query == "Monitor") {
3187  ret = TDEGenericDeviceType::Monitor;
3188  }
3189  else if (query == "Network") {
3190  ret = TDEGenericDeviceType::Network;
3191  }
3192  else if (query == "Printer") {
3193  ret = TDEGenericDeviceType::Printer;
3194  }
3195  else if (query == "Scanner") {
3196  ret = TDEGenericDeviceType::Scanner;
3197  }
3198  else if (query == "Sound") {
3199  ret = TDEGenericDeviceType::Sound;
3200  }
3201  else if (query == "VideoCapture") {
3202  ret = TDEGenericDeviceType::VideoCapture;
3203  }
3204  else if (query == "IEEE1394") {
3205  ret = TDEGenericDeviceType::IEEE1394;
3206  }
3207  else if (query == "PCMCIA") {
3208  ret = TDEGenericDeviceType::PCMCIA;
3209  }
3210  else if (query == "Camera") {
3211  ret = TDEGenericDeviceType::Camera;
3212  }
3213  else if (query == "Serial") {
3214  ret = TDEGenericDeviceType::Serial;
3215  }
3216  else if (query == "Parallel") {
3217  ret = TDEGenericDeviceType::Parallel;
3218  }
3219  else if (query == "TextIO") {
3220  ret = TDEGenericDeviceType::TextIO;
3221  }
3222  else if (query == "Peripheral") {
3223  ret = TDEGenericDeviceType::Peripheral;
3224  }
3225  else if (query == "Backlight") {
3226  ret = TDEGenericDeviceType::Backlight;
3227  }
3228  else if (query == "Battery") {
3229  ret = TDEGenericDeviceType::Battery;
3230  }
3231  else if (query == "Power") {
3232  ret = TDEGenericDeviceType::PowerSupply;
3233  }
3234  else if (query == "Dock") {
3235  ret = TDEGenericDeviceType::Dock;
3236  }
3237  else if (query == "ThermalSensor") {
3238  ret = TDEGenericDeviceType::ThermalSensor;
3239  }
3240  else if (query == "ThermalControl") {
3241  ret = TDEGenericDeviceType::ThermalControl;
3242  }
3243  else if (query == "Bluetooth") {
3244  ret = TDEGenericDeviceType::BlueTooth;
3245  }
3246  else if (query == "Bridge") {
3247  ret = TDEGenericDeviceType::Bridge;
3248  }
3249  else if (query == "Platform") {
3250  ret = TDEGenericDeviceType::Platform;
3251  }
3252  else if (query == "Cryptography") {
3253  ret = TDEGenericDeviceType::Cryptography;
3254  }
3255  else if (query == "Event") {
3256  ret = TDEGenericDeviceType::Event;
3257  }
3258  else if (query == "Input") {
3259  ret = TDEGenericDeviceType::Input;
3260  }
3261  else if (query == "PNP") {
3262  ret = TDEGenericDeviceType::PNP;
3263  }
3264  else if (query == "OtherACPI") {
3265  ret = TDEGenericDeviceType::OtherACPI;
3266  }
3267  else if (query == "OtherUSB") {
3268  ret = TDEGenericDeviceType::OtherUSB;
3269  }
3270  else if (query == "OtherMultimedia") {
3271  ret = TDEGenericDeviceType::OtherMultimedia;
3272  }
3273  else if (query == "OtherPeripheral") {
3274  ret = TDEGenericDeviceType::OtherPeripheral;
3275  }
3276  else if (query == "OtherSensor") {
3277  ret = TDEGenericDeviceType::OtherSensor;
3278  }
3279  else if (query == "OtherVirtual") {
3280  ret = TDEGenericDeviceType::OtherVirtual;
3281  }
3282  else {
3283  ret = TDEGenericDeviceType::Other;
3284  }
3285 
3286  return ret;
3287 }
3288 
3289 TDEDiskDeviceType::TDEDiskDeviceType readDiskDeviceSubtypeFromString(TQString query, TDEDiskDeviceType::TDEDiskDeviceType flagsIn=TDEDiskDeviceType::Null) {
3290  TDEDiskDeviceType::TDEDiskDeviceType ret = flagsIn;
3291 
3292  // Keep this in sync with the TDEDiskDeviceType definition in the header
3293  if (query == "MediaDevice") {
3294  ret = ret | TDEDiskDeviceType::MediaDevice;
3295  }
3296  if (query == "Floppy") {
3297  ret = ret | TDEDiskDeviceType::Floppy;
3298  }
3299  if (query == "CDROM") {
3300  ret = ret | TDEDiskDeviceType::CDROM;
3301  }
3302  if (query == "CDRW") {
3303  ret = ret | TDEDiskDeviceType::CDRW;
3304  }
3305  if (query == "DVDROM") {
3306  ret = ret | TDEDiskDeviceType::DVDROM;
3307  }
3308  if (query == "DVDRAM") {
3309  ret = ret | TDEDiskDeviceType::DVDRAM;
3310  }
3311  if (query == "DVDRW") {
3312  ret = ret | TDEDiskDeviceType::DVDRW;
3313  }
3314  if (query == "BDROM") {
3315  ret = ret | TDEDiskDeviceType::BDROM;
3316  }
3317  if (query == "BDRW") {
3318  ret = ret | TDEDiskDeviceType::BDRW;
3319  }
3320  if (query == "Zip") {
3321  ret = ret | TDEDiskDeviceType::Zip;
3322  }
3323  if (query == "Jaz") {
3324  ret = ret | TDEDiskDeviceType::Jaz;
3325  }
3326  if (query == "Camera") {
3327  ret = ret | TDEDiskDeviceType::Camera;
3328  }
3329  if (query == "LUKS") {
3330  ret = ret | TDEDiskDeviceType::LUKS;
3331  }
3332  if (query == "OtherCrypted") {
3333  ret = ret | TDEDiskDeviceType::OtherCrypted;
3334  }
3335  if (query == "CDAudio") {
3336  ret = ret | TDEDiskDeviceType::CDAudio;
3337  }
3338  if (query == "CDVideo") {
3339  ret = ret | TDEDiskDeviceType::CDVideo;
3340  }
3341  if (query == "DVDVideo") {
3342  ret = ret | TDEDiskDeviceType::DVDVideo;
3343  }
3344  if (query == "BDVideo") {
3345  ret = ret | TDEDiskDeviceType::BDVideo;
3346  }
3347  if (query == "Flash") {
3348  ret = ret | TDEDiskDeviceType::Flash;
3349  }
3350  if (query == "USB") {
3351  ret = ret | TDEDiskDeviceType::USB;
3352  }
3353  if (query == "Tape") {
3354  ret = ret | TDEDiskDeviceType::Tape;
3355  }
3356  if (query == "HDD") {
3357  ret = ret | TDEDiskDeviceType::HDD;
3358  }
3359  if (query == "Optical") {
3360  ret = ret | TDEDiskDeviceType::Optical;
3361  }
3362  if (query == "RAM") {
3363  ret = ret | TDEDiskDeviceType::RAM;
3364  }
3365  if (query == "Loop") {
3366  ret = ret | TDEDiskDeviceType::Loop;
3367  }
3368  if (query == "CompactFlash") {
3369  ret = ret | TDEDiskDeviceType::CompactFlash;
3370  }
3371  if (query == "MemoryStick") {
3372  ret = ret | TDEDiskDeviceType::MemoryStick;
3373  }
3374  if (query == "SmartMedia") {
3375  ret = ret | TDEDiskDeviceType::SmartMedia;
3376  }
3377  if (query == "SDMMC") {
3378  ret = ret | TDEDiskDeviceType::SDMMC;
3379  }
3380  if (query == "UnlockedCrypt") {
3381  ret = ret | TDEDiskDeviceType::UnlockedCrypt;
3382  }
3383 
3384  return ret;
3385 }
3386 
3387 TDEGenericDevice* createDeviceObjectForType(TDEGenericDeviceType::TDEGenericDeviceType type) {
3388  TDEGenericDevice* ret = 0;
3389 
3390  if (type == TDEGenericDeviceType::Disk) {
3391  ret = new TDEStorageDevice(type);
3392  }
3393  else {
3394  ret = new TDEGenericDevice(type);
3395  }
3396 
3397  return ret;
3398 }
3399 
3400 TDEGenericDevice* TDEHardwareDevices::classifyUnknownDeviceByExternalRules(udev_device* dev, TDEGenericDevice* existingdevice, bool classifySubDevices) {
3401  // This routine expects to see the hardware config files into <prefix>/share/apps/tdehwlib/deviceclasses/, suffixed with "hwclass"
3402  TDEGenericDevice* device = existingdevice;
3403  if (!device) device = new TDEGenericDevice(TDEGenericDeviceType::Other);
3404 
3405  // Handle subtype if needed/desired
3406  // To speed things up we rely on the prior scan results stored in m_externalSubtype
3407  if (classifySubDevices) {
3408  if (!device->m_externalRulesFile.isNull()) {
3409  if (device->type() == TDEGenericDeviceType::Disk) {
3410  // Disk class
3411  TDEStorageDevice* sdevice = static_cast<TDEStorageDevice*>(device);
3412  TQStringList subtype = device->m_externalSubtype;
3413  TDEDiskDeviceType::TDEDiskDeviceType desiredSubdeviceType = TDEDiskDeviceType::Null;
3414  if (subtype.count()>0) {
3415  for ( TQStringList::Iterator paramit = subtype.begin(); paramit != subtype.end(); ++paramit ) {
3416  desiredSubdeviceType = readDiskDeviceSubtypeFromString(*paramit, desiredSubdeviceType);
3417  }
3418  if (desiredSubdeviceType != sdevice->diskType()) {
3419  printf("[tdehardwaredevices] Rules file %s used to set device subtype for device at path %s\n", device->m_externalRulesFile.ascii(), device->systemPath().ascii()); fflush(stdout);
3420  sdevice->internalSetDiskType(desiredSubdeviceType);
3421  }
3422  }
3423  }
3424  }
3425  }
3426  else {
3427  TQStringList hardware_info_directories(TDEGlobal::dirs()->resourceDirs("data"));
3428  TQString hardware_info_directory_suffix("tdehwlib/deviceclasses/");
3429  TQString hardware_info_directory;
3430 
3431  // Scan the hardware_info_directory for configuration files
3432  // For each one, open it with TDEConfig() and apply its rules to classify the device
3433  // FIXME
3434  // Should this also scan up to <n> subdirectories for the files? That feature might end up being too expensive...
3435 
3436  device->m_externalRulesFile = TQString::null;
3437  for ( TQStringList::Iterator it = hardware_info_directories.begin(); it != hardware_info_directories.end(); ++it ) {
3438  hardware_info_directory = (*it);
3439  hardware_info_directory += hardware_info_directory_suffix;
3440 
3441  if (TDEGlobal::dirs()->exists(hardware_info_directory)) {
3442  TQDir d(hardware_info_directory);
3443  d.setFilter( TQDir::Files | TQDir::Hidden );
3444 
3445  const TQFileInfoList *list = d.entryInfoList();
3446  TQFileInfoListIterator it( *list );
3447  TQFileInfo *fi;
3448 
3449  while ((fi = it.current()) != 0) {
3450  if (fi->extension(false) == "hwclass") {
3451  bool match = true;
3452 
3453  // Open the rules file
3454  TDEConfig rulesFile(fi->absFilePath(), true, false);
3455  rulesFile.setGroup("Conditions");
3456  TDEConfigMap conditionmap = rulesFile.entryMap("Conditions");
3457  TDEConfigMap::Iterator cndit;
3458  for (cndit = conditionmap.begin(); cndit != conditionmap.end(); ++cndit) {
3459  TQStringList conditionList = TQStringList::split(',', cndit.data(), false);
3460  bool atleastonematch = false;
3461  for ( TQStringList::Iterator paramit = conditionList.begin(); paramit != conditionList.end(); ++paramit ) {
3462  if (cndit.key() == "VENDOR_ID") {
3463  if (device->vendorID() == (*paramit)) {
3464  atleastonematch = true;
3465  }
3466  }
3467  else if (cndit.key() == "MODEL_ID") {
3468  if (device->modelID() == (*paramit)) {
3469  atleastonematch = true;
3470  }
3471  }
3472  else if (cndit.key() == "DRIVER") {
3473  if (device->deviceDriver() == (*paramit)) {
3474  atleastonematch = true;
3475  }
3476  }
3477  else if (readUdevAttribute(dev, cndit.key()) == (*paramit)) {
3478  atleastonematch = true;
3479  }
3480  }
3481  if (!atleastonematch) {
3482  match = false;
3483  }
3484  }
3485 
3486  if (match) {
3487  rulesFile.setGroup("DeviceType");
3488  TQString gentype = rulesFile.readEntry("GENTYPE");
3489  TDEGenericDeviceType::TDEGenericDeviceType desiredDeviceType = device->type();
3490  if (!gentype.isNull()) {
3491  desiredDeviceType = readGenericDeviceTypeFromString(gentype);
3492  }
3493 
3494  // Handle main type
3495  if (desiredDeviceType != device->type()) {
3496  printf("[tdehardwaredevices] Rules file %s used to set device type for device at path %s\n", fi->absFilePath().ascii(), device->systemPath().ascii()); fflush(stdout);
3497  if (m_deviceList.contains(device)) {
3498  m_deviceList.remove(device);
3499  }
3500  else {
3501  delete device;
3502  }
3503  device = createDeviceObjectForType(desiredDeviceType);
3504  }
3505 
3506  // Parse subtype and store in m_externalSubtype for later
3507  // This speeds things up considerably due to the expense of the file scanning/parsing/matching operation
3508  device->m_externalSubtype = rulesFile.readListEntry("SUBTYPE", ',');
3509  device->m_externalRulesFile = fi->absFilePath();
3510 
3511  // Process blacklist entries
3512  rulesFile.setGroup("DeviceSettings");
3513  device->internalSetBlacklistedForUpdate(rulesFile.readBoolEntry("UPDATE_BLACKLISTED", device->blacklistedForUpdate()));
3514  }
3515  }
3516  ++it;
3517  }
3518  }
3519  }
3520  }
3521 
3522  return device;
3523 }
3524 
3525 TDEGenericDevice* TDEHardwareDevices::classifyUnknownDevice(udev_device* dev, TDEGenericDevice* existingdevice, bool force_full_classification) {
3526  // Classify device and create TDEHW device object
3527  TQString devicename;
3528  TQString devicetype;
3529  TQString devicedriver;
3530  TQString devicesubsystem;
3531  TQString devicenode;
3532  TQString systempath;
3533  TQString devicevendorid;
3534  TQString devicemodelid;
3535  TQString devicevendoridenc;
3536  TQString devicemodelidenc;
3537  TQString devicesubvendorid;
3538  TQString devicesubmodelid;
3539  TQString devicetypestring;
3540  TQString devicetypestring_alt;
3541  TQString devicepciclass;
3542  TDEGenericDevice* device = existingdevice;
3543  bool temp_udev_device = !dev;
3544  if (dev) {
3545  devicename = (udev_device_get_sysname(dev));
3546  devicetype = (udev_device_get_devtype(dev));
3547  devicedriver = (udev_device_get_driver(dev));
3548  devicesubsystem = (udev_device_get_subsystem(dev));
3549  devicenode = (udev_device_get_devnode(dev));
3550  systempath = (udev_device_get_syspath(dev));
3551  systempath += "/";
3552  devicevendorid = (udev_device_get_property_value(dev, "ID_VENDOR_ID"));
3553  devicemodelid = (udev_device_get_property_value(dev, "ID_MODEL_ID"));
3554  devicevendoridenc = (udev_device_get_property_value(dev, "ID_VENDOR_ENC"));
3555  devicemodelidenc = (udev_device_get_property_value(dev, "ID_MODEL_ENC"));
3556  devicesubvendorid = (udev_device_get_property_value(dev, "ID_SUBVENDOR_ID"));
3557  devicesubmodelid = (udev_device_get_property_value(dev, "ID_SUBMODEL_ID"));
3558  devicetypestring = (udev_device_get_property_value(dev, "ID_TYPE"));
3559  devicetypestring_alt = (udev_device_get_property_value(dev, "DEVTYPE"));
3560  devicepciclass = (udev_device_get_property_value(dev, "PCI_CLASS"));
3561  }
3562  else {
3563  if (device) {
3564  devicename = device->name();
3565  devicetype = device->m_udevtype;
3566  devicedriver = device->deviceDriver();
3567  devicesubsystem = device->subsystem();
3568  devicenode = device->deviceNode();
3569  systempath = device->systemPath();
3570  devicevendorid = device->vendorID();
3571  devicemodelid = device->modelID();
3572  devicevendoridenc = device->vendorEncoded();
3573  devicemodelidenc = device->modelEncoded();
3574  devicesubvendorid = device->subVendorID();
3575  devicesubmodelid = device->subModelID();
3576  devicetypestring = device->m_udevdevicetypestring;
3577  devicetypestring_alt = device->udevdevicetypestring_alt;
3578  devicepciclass = device->PCIClass();
3579  }
3580  TQString syspathudev = systempath;
3581  syspathudev.truncate(syspathudev.length()-1); // Remove trailing slash
3582  dev = udev_device_new_from_syspath(m_udevStruct, syspathudev.ascii());
3583  }
3584 
3585  // FIXME
3586  // Only a small subset of devices are classified right now
3587  // Figure out the remaining udev logic to classify the rest!
3588  // Helpful file: http://www.enlightenment.org/svn/e/trunk/PROTO/enna-explorer/src/bin/udev.c
3589 
3590  bool done = false;
3591  TQString current_path = systempath;
3592  TQString devicemodalias = TQString::null;
3593 
3594  while (done == false) {
3595  TQString malnodename = current_path;
3596  malnodename.append("/modalias");
3597  TQFile malfile(malnodename);
3598  if (malfile.open(IO_ReadOnly)) {
3599  TQTextStream stream( &malfile );
3600  devicemodalias = stream.readLine();
3601  malfile.close();
3602  }
3603  if (devicemodalias.startsWith("pci") || devicemodalias.startsWith("usb")) {
3604  done = true;
3605  }
3606  else {
3607  devicemodalias = TQString::null;
3608  current_path.truncate(current_path.findRev("/"));
3609  if (!current_path.startsWith("/sys/devices")) {
3610  // Abort!
3611  done = true;
3612  }
3613  }
3614  }
3615 
3616  // Many devices do not provide their vendor/model ID via udev
3617  // Worse, sometimes udev provides an invalid model ID!
3618  // Go after it manually if needed...
3619  if (devicevendorid.isNull() || devicemodelid.isNull() || devicemodelid.contains("/")) {
3620  if (devicemodalias != TQString::null) {
3621  // For added fun the device string lengths differ between pci and usb
3622  if (devicemodalias.startsWith("pci")) {
3623  int vloc = devicemodalias.find("v");
3624  int dloc = devicemodalias.find("d", vloc);
3625  int svloc = devicemodalias.find("sv");
3626  int sdloc = devicemodalias.find("sd", vloc);
3627 
3628  devicevendorid = devicemodalias.mid(vloc+1, 8).lower();
3629  devicemodelid = devicemodalias.mid(dloc+1, 8).lower();
3630  if (svloc != -1) {
3631  devicesubvendorid = devicemodalias.mid(svloc+1, 8).lower();
3632  devicesubmodelid = devicemodalias.mid(sdloc+1, 8).lower();
3633  }
3634  devicevendorid.remove(0,4);
3635  devicemodelid.remove(0,4);
3636  devicesubvendorid.remove(0,4);
3637  devicesubmodelid.remove(0,4);
3638  }
3639  if (devicemodalias.startsWith("usb")) {
3640  int vloc = devicemodalias.find("v");
3641  int dloc = devicemodalias.find("p", vloc);
3642  int svloc = devicemodalias.find("sv");
3643  int sdloc = devicemodalias.find("sp", vloc);
3644 
3645  devicevendorid = devicemodalias.mid(vloc+1, 4).lower();
3646  devicemodelid = devicemodalias.mid(dloc+1, 4).lower();
3647  if (svloc != -1) {
3648  devicesubvendorid = devicemodalias.mid(svloc+1, 4).lower();
3649  devicesubmodelid = devicemodalias.mid(sdloc+1, 4).lower();
3650  }
3651  }
3652  }
3653  }
3654 
3655  // Most of the time udev doesn't barf up a device driver either, so go after it manually...
3656  if (devicedriver.isNull()) {
3657  TQString driverSymlink = udev_device_get_syspath(dev);
3658  TQString driverSymlinkDir = driverSymlink;
3659  driverSymlink.append("/device/driver");
3660  driverSymlinkDir.append("/device/");
3661  TQFileInfo dirfi(driverSymlink);
3662  if (dirfi.isSymLink()) {
3663  char* collapsedPath = realpath((driverSymlinkDir + dirfi.readLink()).ascii(), NULL);
3664  devicedriver = TQString(collapsedPath);
3665  free(collapsedPath);
3666  devicedriver.remove(0, devicedriver.findRev("/")+1);
3667  }
3668  }
3669 
3670  // udev removes critical leading zeroes in the PCI device class, so go after it manually...
3671  TQString classnodename = systempath;
3672  classnodename.append("/class");
3673  TQFile classfile( classnodename );
3674  if ( classfile.open( IO_ReadOnly ) ) {
3675  TQTextStream stream( &classfile );
3676  devicepciclass = stream.readLine();
3677  devicepciclass.replace("0x", "");
3678  devicepciclass = devicepciclass.lower();
3679  classfile.close();
3680  }
3681 
3682  // Classify generic device type and create appropriate object
3683 
3684  // Pull out all event special devices and stuff them under Event
3685  TQString syspath_tail = systempath.lower();
3686  syspath_tail.truncate(syspath_tail.length()-1);
3687  syspath_tail.remove(0, syspath_tail.findRev("/")+1);
3688  if (syspath_tail.startsWith("event")) {
3689  if (!device) device = new TDEEventDevice(TDEGenericDeviceType::Event);
3690  }
3691  // Pull out all input special devices and stuff them under Input
3692  if (syspath_tail.startsWith("input")) {
3693  if (!device) device = new TDEInputDevice(TDEGenericDeviceType::Input);
3694  }
3695 
3696  // Check for keyboard
3697  // Linux doesn't actually ID the keyboard device itself as such, it instead IDs the input device that is underneath the actual keyboard itseld
3698  // Therefore we need to scan <syspath>/input/input* for the ID_INPUT_KEYBOARD attribute
3699  bool is_keyboard = false;
3700  TQString inputtopdirname = udev_device_get_syspath(dev);
3701  inputtopdirname.append("/input/");
3702  TQDir inputdir(inputtopdirname);
3703  inputdir.setFilter(TQDir::All);
3704  const TQFileInfoList *dirlist = inputdir.entryInfoList();
3705  if (dirlist) {
3706  TQFileInfoListIterator inputdirsit(*dirlist);
3707  TQFileInfo *dirfi;
3708  while ( (dirfi = inputdirsit.current()) != 0 ) {
3709  if ((dirfi->fileName() != ".") && (dirfi->fileName() != "..")) {
3710  struct udev_device *slavedev;
3711  slavedev = udev_device_new_from_syspath(m_udevStruct, (inputtopdirname + dirfi->fileName()).ascii());
3712  if (udev_device_get_property_value(slavedev, "ID_INPUT_KEYBOARD") != 0) {
3713  is_keyboard = true;
3714  }
3715  udev_device_unref(slavedev);
3716  }
3717  ++inputdirsit;
3718  }
3719  }
3720  if (is_keyboard) {
3721  if (!device) device = new TDEGenericDevice(TDEGenericDeviceType::Keyboard);
3722  }
3723 
3724  // Classify specific known devices
3725  if (((devicetype == "disk")
3726  || (devicetype == "partition")
3727  || (devicedriver == "floppy")
3728  || (devicesubsystem == "scsi_disk")
3729  || (devicesubsystem == "scsi_tape"))
3730  && ((devicenode != "")
3731  )) {
3732  if (!device) device = new TDEStorageDevice(TDEGenericDeviceType::Disk);
3733  }
3734  else if (devicetype == "host") {
3735  if (devicesubsystem == "bluetooth") {
3736  if (!device) device = new TDEGenericDevice(TDEGenericDeviceType::BlueTooth);
3737  }
3738  }
3739  else if (devicetype.isNull()) {
3740  if (devicesubsystem == "acpi") {
3741  // If the ACPI device exposes a system path ending in /PNPxxxx:yy, the device type can be precisely determined
3742  // See ftp://ftp.microsoft.com/developr/drg/plug-and-play/devids.txt for more information
3743  TQString pnpgentype = systempath;
3744  pnpgentype.remove(0, pnpgentype.findRev("/")+1);
3745  pnpgentype.truncate(pnpgentype.find(":"));
3746  if (pnpgentype.startsWith("PNP")) {
3747  // If a device has been classified as belonging to the ACPI subsystem usually there is a "real" device related to it elsewhere in the system
3748  // Furthermore, the "real" device elsewhere almost always has more functionality exposed via sysfs
3749  // Therefore all ACPI subsystem devices should be stuffed in the OtherACPI category and largely ignored
3750  if (!device) device = new TDEGenericDevice(TDEGenericDeviceType::OtherACPI);
3751  }
3752  else {
3753  if (!device) device = new TDEGenericDevice(TDEGenericDeviceType::OtherACPI);
3754  }
3755  }
3756  else if (devicesubsystem == "input") {
3757  // Figure out if this device is a mouse, keyboard, or something else
3758  // Check for mouse
3759  // udev doesn't reliably help here, so guess from the device name
3760  if (systempath.contains("/mouse")) {
3761  if (!device) device = new TDEGenericDevice(TDEGenericDeviceType::Mouse);
3762  }
3763  if (!device) {
3764  // Second mouse check
3765  // Look for ID_INPUT_MOUSE property presence
3766  if (udev_device_get_property_value(dev, "ID_INPUT_MOUSE") != 0) {
3767  if (!device) device = new TDEGenericDevice(TDEGenericDeviceType::Mouse);
3768  }
3769  }
3770  if (!device) {
3771  // Check for keyboard
3772  // Look for ID_INPUT_KEYBOARD property presence
3773  if (udev_device_get_property_value(dev, "ID_INPUT_KEYBOARD") != 0) {
3774  if (!device) device = new TDEGenericDevice(TDEGenericDeviceType::Keyboard);
3775  }
3776  }
3777  if (!device) {
3778  if (!device) device = new TDEGenericDevice(TDEGenericDeviceType::HID);
3779  }
3780  }
3781  else if (devicesubsystem == "tty") {
3782  if (devicenode.contains("/ttyS")) {
3783  if (!device) device = new TDEGenericDevice(TDEGenericDeviceType::Serial);
3784  }
3785  else {
3786  if (!device) device = new TDEGenericDevice(TDEGenericDeviceType::TextIO);
3787  }
3788  }
3789  else if (devicesubsystem == "usb-serial") {
3790  if (!device) device = new TDEGenericDevice(TDEGenericDeviceType::Serial);
3791  }
3792  else if ((devicesubsystem == "spi_master")
3793  || (devicesubsystem == "spidev")) {
3794  if (!device) device = new TDEGenericDevice(TDEGenericDeviceType::Serial);
3795  }
3796  else if (devicesubsystem == "spi") {
3797  if (!device) device = new TDEGenericDevice(TDEGenericDeviceType::Platform);
3798  }
3799  else if (devicesubsystem == "thermal") {
3800  // FIXME
3801  // Figure out a way to differentiate between ThermalControl (fans and coolers) and ThermalSensor types
3802  if (!device) device = new TDEGenericDevice(TDEGenericDeviceType::ThermalControl);
3803  }
3804  else if (devicesubsystem == "hwmon") {
3805  // FIXME
3806  // This might pick up thermal sensors
3807  if (!device) device = new TDESensorDevice(TDEGenericDeviceType::OtherSensor);
3808  }
3809  }
3810 
3811  // Try to at least generally classify unclassified devices
3812  if (device == 0) {
3813  if (devicesubsystem == "backlight") {
3814  if (!device) device = new TDEBacklightDevice(TDEGenericDeviceType::Backlight);
3815  }
3816  if (systempath.lower().startsWith("/sys/devices/virtual")) {
3817  if (!device) device = new TDEGenericDevice(TDEGenericDeviceType::OtherVirtual);
3818  }
3819  if (systempath.lower().startsWith("/sys/module/")
3820  || (systempath.lower().startsWith("/sys/kernel/"))) {
3821  if (!device) device = new TDEGenericDevice(TDEGenericDeviceType::Platform); // FIXME Should go into a new kernel module category when the tdelibs ABI can be broken again
3822  }
3823  if ((devicetypestring == "audio")
3824  || (devicesubsystem == "sound")
3825  || (devicesubsystem == "ac97")) {
3826  if (!device) device = new TDEGenericDevice(TDEGenericDeviceType::Sound);
3827  }
3828  if ((devicesubsystem == "video4linux")
3829  || (devicesubsystem == "dvb")) {
3830  if (!device) device = new TDEGenericDevice(TDEGenericDeviceType::VideoCapture);
3831  }
3832  if ((devicetypestring_alt == "scsi_target")
3833  || (devicesubsystem == "scsi_host")
3834  || (devicesubsystem == "scsi_disk")
3835  || (devicesubsystem == "scsi_device")
3836  || (devicesubsystem == "scsi_generic")
3837  || (devicesubsystem == "scsi")
3838  || (devicetypestring_alt == "sas_target")
3839  || (devicesubsystem == "sas_host")
3840  || (devicesubsystem == "sas_port")
3841  || (devicesubsystem == "sas_device")
3842  || (devicesubsystem == "sas_generic")
3843  || (devicesubsystem == "sas_phy")
3844  || (devicesubsystem == "sas_end_device")
3845  || (devicesubsystem == "spi_transport")
3846  || (devicesubsystem == "spi_host")
3847  || (devicesubsystem == "ata_port")
3848  || (devicesubsystem == "ata_link")
3849  || (devicesubsystem == "ata_disk")
3850  || (devicesubsystem == "ata_device")
3851  || (devicesubsystem == "ata")) {
3852  if (!device) device = new TDEGenericDevice(TDEGenericDeviceType::Platform);
3853  }
3854  if (devicesubsystem == "infiniband") {
3855  if (!device) device = new TDEGenericDevice(TDEGenericDeviceType::Peripheral);
3856  }
3857  if ((devicesubsystem == "infiniband_cm")
3858  || (devicesubsystem == "infiniband_mad")) {
3859  if (!device) device = new TDEGenericDevice(TDEGenericDeviceType::Platform);
3860  }
3861  if ((devicesubsystem == "enclosure")
3862  || (devicesubsystem == "clocksource")
3863  || (devicesubsystem == "amba")) {
3864  if (!device) device = new TDEGenericDevice(TDEGenericDeviceType::Platform);
3865  }
3866  if (devicesubsystem == "ipmi_si") {
3867  if (!device) device = new TDEGenericDevice(TDEGenericDeviceType::Mainboard);
3868  }
3869  if (devicesubsystem == "misc") {
3870  if (devicedriver.startsWith("tpm_")) {
3871  if (!device) device = new TDEGenericDevice(TDEGenericDeviceType::Cryptography);
3872  }
3873  }
3874  if (devicesubsystem == "leds") {
3875  if (!device) device = new TDEGenericDevice(TDEGenericDeviceType::OtherACPI);
3876  }
3877  if (devicesubsystem == "net") {
3878  if (!device) device = new TDENetworkDevice(TDEGenericDeviceType::Network);
3879  }
3880  if ((devicesubsystem == "i2c")
3881  || (devicesubsystem == "i2c-dev")) {
3882  if (!device) device = new TDEGenericDevice(TDEGenericDeviceType::I2C);
3883  }
3884  if (devicesubsystem == "mdio_bus") {
3885  if (!device) device = new TDEGenericDevice(TDEGenericDeviceType::MDIO);
3886  }
3887  if (devicesubsystem == "graphics") {
3888  if (devicenode.isNull()) { // GPUs do not have associated device nodes
3889  if (!device) device = new TDEGenericDevice(TDEGenericDeviceType::GPU);
3890  }
3891  else {
3892  if (!device) device = new TDEGenericDevice(TDEGenericDeviceType::Platform);
3893  }
3894  }
3895  if (devicesubsystem == "mmc_host") {
3896  if (!device) device = new TDEGenericDevice(TDEGenericDeviceType::StorageController);
3897  }
3898  if (devicesubsystem == "mmc") {
3899  if (!device) device = new TDEGenericDevice(TDEGenericDeviceType::Platform);
3900  }
3901  if ((devicesubsystem == "event_source")
3902  || (devicesubsystem == "rtc")) {
3903  if (!device) device = new TDEGenericDevice(TDEGenericDeviceType::Mainboard);
3904  }
3905  if (devicesubsystem == "bsg") {
3906  if (!device) device = new TDEGenericDevice(TDEGenericDeviceType::SCSI);
3907  }
3908  if (devicesubsystem == "firewire") {
3909  if (!device) device = new TDEGenericDevice(TDEGenericDeviceType::IEEE1394);
3910  }
3911  if (devicesubsystem == "drm") {
3912  if (devicenode.isNull()) { // Monitors do not have associated device nodes
3913  if (!device) device = new TDEMonitorDevice(TDEGenericDeviceType::Monitor);
3914  }
3915  else {
3916  if (!device) device = new TDEGenericDevice(TDEGenericDeviceType::Platform);
3917  }
3918  }
3919  if (devicesubsystem == "serio") {
3920  if (devicedriver.contains("atkbd")) {
3921  if (!device) device = new TDEGenericDevice(TDEGenericDeviceType::Keyboard);
3922  }
3923  else if (devicedriver.contains("mouse")) {
3924  if (!device) device = new TDEGenericDevice(TDEGenericDeviceType::Mouse);
3925  }
3926  else {
3927  if (!device) device = new TDEGenericDevice(TDEGenericDeviceType::Serial);
3928  }
3929  }
3930  if (devicesubsystem == "ppdev") {
3931  if (!device) device = new TDEGenericDevice(TDEGenericDeviceType::Parallel);
3932  }
3933  if (devicesubsystem == "printer") {
3934  if (!device) device = new TDEGenericDevice(TDEGenericDeviceType::Printer);
3935  }
3936  if (devicesubsystem == "bridge") {
3937  if (!device) device = new TDEGenericDevice(TDEGenericDeviceType::Bridge);
3938  }
3939  if ((devicesubsystem == "pci_bus")
3940  || (devicesubsystem == "pci_express")) {
3941  if (!device) device = new TDEGenericDevice(TDEGenericDeviceType::Bus);
3942  }
3943  if (devicesubsystem == "pcmcia_socket") {
3944  if (!device) device = new TDEGenericDevice(TDEGenericDeviceType::PCMCIA);
3945  }
3946  if (devicesubsystem == "platform") {
3947  if (!device) device = new TDEGenericDevice(TDEGenericDeviceType::Platform);
3948  }
3949  if (devicesubsystem == "ieee80211") {
3950  if (!device) device = new TDEGenericDevice(TDEGenericDeviceType::Platform);
3951  }
3952  if (devicesubsystem == "rfkill") {
3953  if (!device) device = new TDEGenericDevice(TDEGenericDeviceType::Platform);
3954  }
3955  if (devicesubsystem == "machinecheck") {
3956  if (!device) device = new TDEGenericDevice(TDEGenericDeviceType::Platform);
3957  }
3958  if (devicesubsystem == "pnp") {
3959  if (!device) device = new TDEGenericDevice(TDEGenericDeviceType::PNP);
3960  }
3961  if ((devicesubsystem == "hid")
3962  || (devicesubsystem == "hidraw")
3963  || (devicesubsystem == "usbhid")) {
3964  if (!device) device = new TDEGenericDevice(TDEGenericDeviceType::HID);
3965  }
3966  if (devicesubsystem == "power_supply") {
3967  TQString powersupplyname(udev_device_get_property_value(dev, "POWER_SUPPLY_NAME"));
3968  if (powersupplyname.upper().startsWith("AC")) {
3969  if (!device) device = new TDEMainsPowerDevice(TDEGenericDeviceType::PowerSupply);
3970  }
3971  else {
3972  if (!device) device = new TDEBatteryDevice(TDEGenericDeviceType::Battery);
3973  }
3974  }
3975 
3976  // Moderate accuracy classification, if PCI device class is available
3977  // See http://www.acm.uiuc.edu/sigops/roll_your_own/7.c.1.html for codes and meanings
3978  if (!devicepciclass.isNull()) {
3979  // Pre PCI 2.0
3980  if (devicepciclass.startsWith("0001")) {
3981  if (devicenode.isNull()) { // GPUs do not have associated device nodes
3982  if (!device) device = new TDEGenericDevice(TDEGenericDeviceType::GPU);
3983  }
3984  else {
3985  if (!device) device = new TDEGenericDevice(TDEGenericDeviceType::Platform);
3986  }
3987  }
3988  // Post PCI 2.0
3989  TQString devicepcisubclass = devicepciclass;
3990  devicepcisubclass = devicepcisubclass.remove(0,2);
3991  if (devicepciclass.startsWith("01")) {
3992  if (!device) device = new TDEGenericDevice(TDEGenericDeviceType::StorageController);
3993  }
3994  if (devicepciclass.startsWith("02")) {
3995  if (!device) device = new TDENetworkDevice(TDEGenericDeviceType::Network);
3996  }
3997  if (devicepciclass.startsWith("03")) {
3998  if (devicenode.isNull()) { // GPUs do not have associated device nodes
3999  if (!device) device = new TDEGenericDevice(TDEGenericDeviceType::GPU);
4000  }
4001  else {
4002  if (!device) device = new TDEGenericDevice(TDEGenericDeviceType::Platform);
4003  }
4004  }
4005  if (devicepciclass.startsWith("04")) {
4006  if (!device) device = new TDEGenericDevice(TDEGenericDeviceType::OtherMultimedia);
4007  }
4008  if (devicepciclass.startsWith("05")) {
4009  if (!device) device = new TDEGenericDevice(TDEGenericDeviceType::RAM);
4010  }
4011  if (devicepciclass.startsWith("06")) {
4012  if (!device) device = new TDEGenericDevice(TDEGenericDeviceType::Bridge);
4013  }
4014  if (devicepciclass.startsWith("07")) {
4015  if (devicepcisubclass.startsWith("03")) {
4016  if (!device) device = new TDEGenericDevice(TDEGenericDeviceType::Modem);
4017  }
4018  }
4019  if (devicepciclass.startsWith("0a")) {
4020  if (!device) device = new TDEGenericDevice(TDEGenericDeviceType::Dock);
4021  }
4022  if (devicepciclass.startsWith("0b")) {
4023  if (!device) device = new TDEGenericDevice(TDEGenericDeviceType::CPU);
4024  }
4025  if (devicepciclass.startsWith("0c")) {
4026  if (!device) device = new TDEGenericDevice(TDEGenericDeviceType::Serial);
4027  }
4028  }
4029 
4030  // Last ditch attempt at classification
4031  // Likely inaccurate and sweeping
4032  if ((devicesubsystem == "usb")
4033  || (devicesubsystem == "usb_device")
4034  || (devicesubsystem == "usbmon")) {
4035  // Get USB interface class for further classification
4036  int usbInterfaceClass = -1;
4037  {
4038  TQFile ifaceprotofile(current_path + "/bInterfaceClass");
4039  if (ifaceprotofile.open(IO_ReadOnly)) {
4040  TQTextStream stream( &ifaceprotofile );
4041  usbInterfaceClass = stream.readLine().toUInt();
4042  ifaceprotofile.close();
4043  }
4044  }
4045  // Get USB interface subclass for further classification
4046  int usbInterfaceSubClass = -1;
4047  {
4048  TQFile ifaceprotofile(current_path + "/bInterfaceSubClass");
4049  if (ifaceprotofile.open(IO_ReadOnly)) {
4050  TQTextStream stream( &ifaceprotofile );
4051  usbInterfaceSubClass = stream.readLine().toUInt();
4052  ifaceprotofile.close();
4053  }
4054  }
4055  // Get USB interface protocol for further classification
4056  int usbInterfaceProtocol = -1;
4057  {
4058  TQFile ifaceprotofile(current_path + "/bInterfaceProtocol");
4059  if (ifaceprotofile.open(IO_ReadOnly)) {
4060  TQTextStream stream( &ifaceprotofile );
4061  usbInterfaceProtocol = stream.readLine().toUInt();
4062  ifaceprotofile.close();
4063  }
4064  }
4065  if ((usbInterfaceClass == 6) && (usbInterfaceSubClass == 1) && (usbInterfaceProtocol == 1)) {
4066  // PictBridge
4067  if (!device) {
4068  device = new TDEStorageDevice(TDEGenericDeviceType::Disk);
4069  TDEStorageDevice* sdevice = static_cast<TDEStorageDevice*>(device);
4070  sdevice->internalSetDiskType(TDEDiskDeviceType::Camera);
4071  TQString parentsyspathudev = systempath;
4072  parentsyspathudev.truncate(parentsyspathudev.length()-1); // Remove trailing slash
4073  parentsyspathudev.truncate(parentsyspathudev.findRev("/"));
4074  struct udev_device *parentdev;
4075  parentdev = udev_device_new_from_syspath(m_udevStruct, parentsyspathudev.ascii());
4076  devicenode = (udev_device_get_devnode(parentdev));
4077  }
4078  }
4079  else {
4080  if (!device) device = new TDEGenericDevice(TDEGenericDeviceType::OtherUSB);
4081  }
4082  }
4083  if (devicesubsystem == "pci") {
4084  if (!device) device = new TDEGenericDevice(TDEGenericDeviceType::OtherPeripheral);
4085  }
4086  if (devicesubsystem == "cpu") {
4087  if (!device) device = new TDEGenericDevice(TDEGenericDeviceType::Platform);
4088  }
4089  }
4090 
4091  if (device == 0) {
4092  // Unhandled
4093  if (!device) device = new TDEGenericDevice(TDEGenericDeviceType::Other);
4094  printf("[FIXME] UNCLASSIFIED DEVICE name: %s type: %s subsystem: %s driver: %s [Node Path: %s] [Syspath: %s] [%s:%s]\n", devicename.ascii(), devicetype.ascii(), devicesubsystem.ascii(), devicedriver.ascii(), devicenode.ascii(), udev_device_get_syspath(dev), devicevendorid.ascii(), devicemodelid.ascii()); fflush(stdout);
4095  }
4096 
4097  // Root devices are special
4098  if ((device->type() == TDEGenericDeviceType::Root) || (device->type() == TDEGenericDeviceType::RootSystem)) {
4099  systempath = device->systemPath();
4100  }
4101 
4102  // Set preliminary basic device information
4103  device->internalSetName(devicename);
4104  device->internalSetDeviceNode(devicenode);
4105  device->internalSetSystemPath(systempath);
4106  device->internalSetVendorID(devicevendorid);
4107  device->internalSetModelID(devicemodelid);
4108  device->internalSetVendorEncoded(devicevendoridenc);
4109  device->internalSetModelEncoded(devicemodelidenc);
4110  device->internalSetSubVendorID(devicesubvendorid);
4111  device->internalSetSubModelID(devicesubmodelid);
4112  device->internalSetModuleAlias(devicemodalias);
4113  device->internalSetDeviceDriver(devicedriver);
4114  device->internalSetSubsystem(devicesubsystem);
4115  device->internalSetPCIClass(devicepciclass);
4116 
4117  updateBlacklists(device, dev);
4118 
4119  if (force_full_classification) {
4120  // Check external rules for possible device type overrides
4121  device = classifyUnknownDeviceByExternalRules(dev, device, false);
4122  }
4123 
4124  if (device->type() == TDEGenericDeviceType::Disk) {
4125  TDEStorageDevice* sdevice = static_cast<TDEStorageDevice*>(device);
4126  if (sdevice->diskType() & TDEDiskDeviceType::Camera) {
4127  // PictBridge cameras are special and should not be classified by standard rules
4128  sdevice->internalSetDiskStatus(TDEDiskDeviceStatus::Removable);
4129  sdevice->internalSetFileSystemName("pictbridge");
4130  }
4131  else {
4132  bool removable = false;
4133  bool hotpluggable = false;
4134 
4135  // We can get the removable flag, but we have no idea if the device has the ability to notify on media insertion/removal
4136  // If there is no such notification possible, then we should not set the removable flag
4137  // udev can be such an amazing pain at times
4138  // It exports a /capabilities node with no info on what the bits actually mean
4139  // This information is very poorly documented as a set of #defines in include/linux/genhd.h
4140  // We are specifically interested in GENHD_FL_REMOVABLE and GENHD_FL_MEDIA_CHANGE_NOTIFY
4141  // The "removable" flag should also really be renamed to "hotpluggable", as that is far more precise...
4142  TQString capabilitynodename = systempath;
4143  capabilitynodename.append("/capability");
4144  TQFile capabilityfile( capabilitynodename );
4145  unsigned int capabilities = 0;
4146  if ( capabilityfile.open( IO_ReadOnly ) ) {
4147  TQTextStream stream( &capabilityfile );
4148  TQString capabilitystring;
4149  capabilitystring = stream.readLine();
4150  capabilities = capabilitystring.toUInt();
4151  capabilityfile.close();
4152  }
4153  if (capabilities & GENHD_FL_REMOVABLE) {
4154  // FIXME
4155  // For added fun this is not always true; i.e. GENHD_FL_REMOVABLE can be set when the device cannot be hotplugged (floppy drives).
4156  hotpluggable = true;
4157  }
4158  if (capabilities & GENHD_FL_MEDIA_CHANGE_NOTIFY) {
4159  removable = true;
4160  }
4161 
4162  // See if any other devices are exclusively using this device, such as the Device Mapper
4163  TQStringList holdingDeviceNodes;
4164  TQString holdersnodename = udev_device_get_syspath(dev);
4165  holdersnodename.append("/holders/");
4166  TQDir holdersdir(holdersnodename);
4167  holdersdir.setFilter(TQDir::All);
4168  const TQFileInfoList *dirlist = holdersdir.entryInfoList();
4169  if (dirlist) {
4170  TQFileInfoListIterator holdersdirit(*dirlist);
4171  TQFileInfo *dirfi;
4172  while ( (dirfi = holdersdirit.current()) != 0 ) {
4173  if (dirfi->isSymLink()) {
4174  char* collapsedPath = realpath((holdersnodename + dirfi->readLink()).ascii(), NULL);
4175  holdingDeviceNodes.append(TQString(collapsedPath));
4176  free(collapsedPath);
4177  }
4178  ++holdersdirit;
4179  }
4180  }
4181 
4182  // See if any other physical devices underlie this device, for example when the Device Mapper is in use
4183  TQStringList slaveDeviceNodes;
4184  TQString slavesnodename = udev_device_get_syspath(dev);
4185  slavesnodename.append("/slaves/");
4186  TQDir slavedir(slavesnodename);
4187  slavedir.setFilter(TQDir::All);
4188  dirlist = slavedir.entryInfoList();
4189  if (dirlist) {
4190  TQFileInfoListIterator slavedirit(*dirlist);
4191  TQFileInfo *dirfi;
4192  while ( (dirfi = slavedirit.current()) != 0 ) {
4193  if (dirfi->isSymLink()) {
4194  char* collapsedPath = realpath((slavesnodename + dirfi->readLink()).ascii(), NULL);
4195  slaveDeviceNodes.append(TQString(collapsedPath));
4196  free(collapsedPath);
4197  }
4198  ++slavedirit;
4199  }
4200  }
4201 
4202  // Determine generic disk information
4203  TQString devicevendor(udev_device_get_property_value(dev, "ID_VENDOR"));
4204  TQString devicemodel(udev_device_get_property_value(dev, "ID_MODEL"));
4205  TQString devicebus(udev_device_get_property_value(dev, "ID_BUS"));
4206 
4207  // Get disk specific info
4208  TQString disklabel(udev_device_get_property_value(dev, "ID_FS_LABEL"));
4209  TQString diskuuid(udev_device_get_property_value(dev, "ID_FS_UUID"));
4210  TQString filesystemtype(udev_device_get_property_value(dev, "ID_FS_TYPE"));
4211  TQString filesystemusage(udev_device_get_property_value(dev, "ID_FS_USAGE"));
4212 
4213  device->internalSetVendorName(devicevendor);
4214  device->internalSetVendorModel(devicemodel);
4215  device->internalSetDeviceBus(devicebus);
4216 
4217  TDEDiskDeviceType::TDEDiskDeviceType disktype = sdevice->diskType();
4218  TDEDiskDeviceStatus::TDEDiskDeviceStatus diskstatus = TDEDiskDeviceStatus::Null;
4219 
4220  disktype = classifyDiskType(dev, devicebus, devicetypestring, systempath, devicevendor, devicemodel, filesystemtype, devicedriver);
4221  sdevice->internalSetDiskType(disktype);
4222  device = classifyUnknownDeviceByExternalRules(dev, device, true); // Check external rules for possible subtype overrides
4223  disktype = sdevice->diskType(); // The type can be overridden by an external rule
4224 
4225  if ((disktype & TDEDiskDeviceType::CDROM)
4226  || (disktype & TDEDiskDeviceType::CDRW)
4227  || (disktype & TDEDiskDeviceType::DVDROM)
4228  || (disktype & TDEDiskDeviceType::DVDRAM)
4229  || (disktype & TDEDiskDeviceType::DVDRW)
4230  || (disktype & TDEDiskDeviceType::BDROM)
4231  || (disktype & TDEDiskDeviceType::BDRW)
4232  || (disktype & TDEDiskDeviceType::CDAudio)
4233  || (disktype & TDEDiskDeviceType::CDVideo)
4234  || (disktype & TDEDiskDeviceType::DVDVideo)
4235  || (disktype & TDEDiskDeviceType::BDVideo)
4236  ) {
4237  // These drives are guaranteed to be optical
4238  disktype = disktype | TDEDiskDeviceType::Optical;
4239  }
4240 
4241  if (disktype & TDEDiskDeviceType::Floppy) {
4242  // Floppy drives don't work well under udev
4243  // I have to look for the block device name manually
4244  TQString floppyblknodename = systempath;
4245  floppyblknodename.append("/block");
4246  TQDir floppyblkdir(floppyblknodename);
4247  floppyblkdir.setFilter(TQDir::All);
4248  const TQFileInfoList *floppyblkdirlist = floppyblkdir.entryInfoList();
4249  if (floppyblkdirlist) {
4250  TQFileInfoListIterator floppyblkdirit(*floppyblkdirlist);
4251  TQFileInfo *dirfi;
4252  while ( (dirfi = floppyblkdirit.current()) != 0 ) {
4253  if ((dirfi->fileName() != ".") && (dirfi->fileName() != "..")) {
4254  // Does this routine work with more than one floppy drive in the system?
4255  devicenode = TQString("/dev/").append(dirfi->fileName());
4256  }
4257  ++floppyblkdirit;
4258  }
4259  }
4260 
4261  // Some interesting information can be gleaned from the CMOS type file
4262  // 0 : Defaults
4263  // 1 : 5 1/4 DD
4264  // 2 : 5 1/4 HD
4265  // 3 : 3 1/2 DD
4266  // 4 : 3 1/2 HD
4267  // 5 : 3 1/2 ED
4268  // 6 : 3 1/2 ED
4269  // 16 : unknown or not installed
4270  TQString floppycmsnodename = systempath;
4271  floppycmsnodename.append("/cmos");
4272  TQFile floppycmsfile( floppycmsnodename );
4273  TQString cmosstring;
4274  if ( floppycmsfile.open( IO_ReadOnly ) ) {
4275  TQTextStream stream( &floppycmsfile );
4276  cmosstring = stream.readLine();
4277  floppycmsfile.close();
4278  }
4279  // FIXME
4280  // Do something with the information in cmosstring
4281 
4282  if (devicenode.isNull()) {
4283  // This floppy drive cannot be mounted, so ignore it
4284  disktype = disktype & ~TDEDiskDeviceType::Floppy;
4285  }
4286  }
4287 
4288  if (devicetypestring.upper() == "CD") {
4289  if (TQString(udev_device_get_property_value(dev, "ID_CDROM_MEDIA_STATE")).upper() == "BLANK") {
4290  diskstatus = diskstatus | TDEDiskDeviceStatus::Blank;
4291  }
4292  sdevice->internalSetMediaInserted((TQString(udev_device_get_property_value(dev, "ID_CDROM_MEDIA")) != ""));
4293  }
4294 
4295  if (disktype & TDEDiskDeviceType::Zip) {
4296  // A Zip drive does not advertise its status via udev, but it can be guessed from the size parameter
4297  TQString zipnodename = systempath;
4298  zipnodename.append("/size");
4299  TQFile namefile( zipnodename );
4300  TQString zipsize;
4301  if ( namefile.open( IO_ReadOnly ) ) {
4302  TQTextStream stream( &namefile );
4303  zipsize = stream.readLine();
4304  namefile.close();
4305  }
4306  if (!zipsize.isNull()) {
4307  sdevice->internalSetMediaInserted((zipsize.toInt() != 0));
4308  }
4309  }
4310 
4311  if (removable) {
4312  diskstatus = diskstatus | TDEDiskDeviceStatus::Removable;
4313  }
4314  if (hotpluggable) {
4315  diskstatus = diskstatus | TDEDiskDeviceStatus::Hotpluggable;
4316  }
4317 
4318  if ((filesystemtype.upper() != "CRYPTO_LUKS") && (filesystemtype.upper() != "CRYPTO") && (filesystemtype.upper() != "SWAP") && (!filesystemtype.isNull())) {
4319  diskstatus = diskstatus | TDEDiskDeviceStatus::ContainsFilesystem;
4320  }
4321 
4322  // Set mountable flag if device is likely to be mountable
4323  diskstatus = diskstatus | TDEDiskDeviceStatus::Mountable;
4324  if ((devicetypestring.upper().isNull()) && (disktype & TDEDiskDeviceType::HDD)) {
4325  diskstatus = diskstatus & ~TDEDiskDeviceStatus::Mountable;
4326  }
4327  if (removable) {
4328  if (sdevice->mediaInserted()) {
4329  diskstatus = diskstatus | TDEDiskDeviceStatus::Inserted;
4330  }
4331  else {
4332  diskstatus = diskstatus & ~TDEDiskDeviceStatus::Mountable;
4333  }
4334  }
4335  // Swap partitions cannot be mounted
4336  if (filesystemtype.upper() == "SWAP") {
4337  diskstatus = diskstatus & ~TDEDiskDeviceStatus::Mountable;
4338  }
4339  // If certain disk types do not report the presence of a filesystem, they are likely not mountable
4340  if ((disktype & TDEDiskDeviceType::HDD) || (disktype & TDEDiskDeviceType::Optical)) {
4341  if (!(diskstatus & TDEDiskDeviceStatus::ContainsFilesystem)) {
4342  diskstatus = diskstatus & ~TDEDiskDeviceStatus::Mountable;
4343  }
4344  }
4345 
4346  if (holdingDeviceNodes.count() > 0) {
4347  diskstatus = diskstatus | TDEDiskDeviceStatus::UsedByDevice;
4348  }
4349 
4350  if (slaveDeviceNodes.count() > 0) {
4351  diskstatus = diskstatus | TDEDiskDeviceStatus::UsesDevice;
4352  }
4353 
4354  // See if any slaves were crypted
4355  for ( TQStringList::Iterator slaveit = slaveDeviceNodes.begin(); slaveit != slaveDeviceNodes.end(); ++slaveit ) {
4356  struct udev_device *slavedev;
4357  slavedev = udev_device_new_from_syspath(m_udevStruct, (*slaveit).ascii());
4358  TQString slavediskfstype(udev_device_get_property_value(slavedev, "ID_FS_TYPE"));
4359  if ((slavediskfstype.upper() == "CRYPTO_LUKS") || (slavediskfstype.upper() == "CRYPTO")) {
4360  disktype = disktype | TDEDiskDeviceType::UnlockedCrypt;
4361  // Set disk type based on parent device
4362  disktype = disktype | classifyDiskType(slavedev, TQString(udev_device_get_property_value(dev, "ID_BUS")), TQString(udev_device_get_property_value(dev, "ID_TYPE")), (*slaveit), TQString(udev_device_get_property_value(dev, "ID_VENDOR")), TQString(udev_device_get_property_value(dev, "ID_MODEL")), TQString(udev_device_get_property_value(dev, "ID_FS_TYPE")), TQString(udev_device_get_driver(dev)));
4363  }
4364  udev_device_unref(slavedev);
4365  }
4366 
4367  sdevice->internalSetDiskType(disktype);
4368  sdevice->internalSetDiskUUID(diskuuid);
4369  sdevice->internalSetDiskStatus(diskstatus);
4370  sdevice->internalSetFileSystemName(filesystemtype);
4371  sdevice->internalSetFileSystemUsage(filesystemusage);
4372  sdevice->internalSetSlaveDevices(slaveDeviceNodes);
4373  sdevice->internalSetHoldingDevices(holdingDeviceNodes);
4374 
4375  // Clean up disk label
4376  if ((sdevice->isDiskOfType(TDEDiskDeviceType::CDROM))
4377  || (sdevice->isDiskOfType(TDEDiskDeviceType::CDRW))
4378  || (sdevice->isDiskOfType(TDEDiskDeviceType::DVDROM))
4379  || (sdevice->isDiskOfType(TDEDiskDeviceType::DVDRW))
4380  || (sdevice->isDiskOfType(TDEDiskDeviceType::BDROM))
4381  || (sdevice->isDiskOfType(TDEDiskDeviceType::BDRW))
4382  || (sdevice->isDiskOfType(TDEDiskDeviceType::CDAudio))
4383  || (sdevice->isDiskOfType(TDEDiskDeviceType::CDVideo))
4384  || (sdevice->isDiskOfType(TDEDiskDeviceType::DVDVideo))
4385  || (sdevice->isDiskOfType(TDEDiskDeviceType::BDVideo))
4386  ) {
4387  if (disklabel == "" && sdevice->diskLabel().isNull()) {
4388  // Read the volume label in via volname, since udev couldn't be bothered to do this on its own
4389  FILE *exepipe = popen(((TQString("volname %1").arg(devicenode).ascii())), "r");
4390  if (exepipe) {
4391  char buffer[8092];
4392  disklabel = fgets(buffer, sizeof(buffer), exepipe);
4393  pclose(exepipe);
4394  }
4395  }
4396  }
4397 
4398  sdevice->internalSetDiskLabel(disklabel);
4399  }
4400  }
4401 
4402  if (device->type() == TDEGenericDeviceType::Network) {
4403  // Network devices don't have devices nodes per se, but we can at least return the Linux network name...
4404  TQString potentialdevicenode = systempath;
4405  if (potentialdevicenode.endsWith("/")) potentialdevicenode.truncate(potentialdevicenode.length()-1);
4406  potentialdevicenode.remove(0, potentialdevicenode.findRev("/")+1);
4407  TQString potentialparentnode = systempath;
4408  if (potentialparentnode.endsWith("/")) potentialparentnode.truncate(potentialparentnode.length()-1);
4409  potentialparentnode.remove(0, potentialparentnode.findRev("/", potentialparentnode.findRev("/")-1)+1);
4410  if (potentialparentnode.startsWith("net/")) {
4411  devicenode = potentialdevicenode;
4412  }
4413 
4414  if (devicenode.isNull()) {
4415  // Platform device, not a physical device
4416  // HACK
4417  // This only works because devices of type Platform only access the TDEGenericDevice class!
4418  device->m_deviceType = TDEGenericDeviceType::Platform;
4419  }
4420  else {
4421  // Gather network device information
4422  TDENetworkDevice* ndevice = dynamic_cast<TDENetworkDevice*>(device);
4423  TQString valuesnodename = systempath + "/";
4424  TQDir valuesdir(valuesnodename);
4425  valuesdir.setFilter(TQDir::All);
4426  TQString nodename;
4427  const TQFileInfoList *dirlist = valuesdir.entryInfoList();
4428  if (dirlist) {
4429  TQFileInfoListIterator valuesdirit(*dirlist);
4430  TQFileInfo *dirfi;
4431  while ( (dirfi = valuesdirit.current()) != 0 ) {
4432  nodename = dirfi->fileName();
4433  TQFile file( valuesnodename + nodename );
4434  if ( file.open( IO_ReadOnly ) ) {
4435  TQTextStream stream( &file );
4436  TQString line;
4437  line = stream.readLine();
4438  if (nodename == "address") {
4439  ndevice->internalSetMacAddress(line);
4440  }
4441  if (nodename == "carrier") {
4442  ndevice->internalSetCarrierPresent(line.toInt());
4443  }
4444  if (nodename == "dormant") {
4445  ndevice->internalSetDormant(line.toInt());
4446  }
4447  if (nodename == "operstate") {
4448  TQString friendlyState = line.lower();
4449  friendlyState[0] = friendlyState[0].upper();
4450  ndevice->internalSetState(friendlyState);
4451  }
4452  file.close();
4453  }
4454  ++valuesdirit;
4455  }
4456  }
4457  // Gather connection information such as IP addresses
4458  if (ndevice->state().upper() == "UP") {
4459  struct ifaddrs *ifaddr, *ifa;
4460  int family, s;
4461  char host[NI_MAXHOST];
4462 
4463  if (getifaddrs(&ifaddr) != -1) {
4464  for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) {
4465  if (ifa->ifa_addr == NULL) {
4466  continue;
4467  }
4468 
4469  family = ifa->ifa_addr->sa_family;
4470 
4471  if (TQString(ifa->ifa_name) == devicenode) {
4472  if ((family == AF_INET) || (family == AF_INET6)) {
4473  s = getnameinfo(ifa->ifa_addr, (family == AF_INET) ? sizeof(struct sockaddr_in) : sizeof(struct sockaddr_in6), host, NI_MAXHOST, NULL, 0, NI_NUMERICHOST);
4474  if (s == 0) {
4475  TQString address(host);
4476  if (family == AF_INET) {
4477  ndevice->internalSetIpV4Address(address);
4478  }
4479  if (family == AF_INET6) {
4480  address.truncate(address.findRev("%"));
4481  ndevice->internalSetIpV6Address(address);
4482  }
4483  }
4484  s = getnameinfo(ifa->ifa_netmask, (family == AF_INET) ? sizeof(struct sockaddr_in) : sizeof(struct sockaddr_in6), host, NI_MAXHOST, NULL, 0, NI_NUMERICHOST);
4485  if (s == 0) {
4486  TQString address(host);
4487  if (family == AF_INET) {
4488  ndevice->internalSetIpV4Netmask(address);
4489  }
4490  if (family == AF_INET6) {
4491  address.truncate(address.findRev("%"));
4492  ndevice->internalSetIpV6Netmask(address);
4493  }
4494  }
4495  s = getnameinfo(ifa->ifa_ifu.ifu_broadaddr, (family == AF_INET) ? sizeof(struct sockaddr_in) : sizeof(struct sockaddr_in6), host, NI_MAXHOST, NULL, 0, NI_NUMERICHOST);
4496  if (s == 0) {
4497  TQString address(host);
4498  if (family == AF_INET) {
4499  ndevice->internalSetIpV4Broadcast(address);
4500  }
4501  if (family == AF_INET6) {
4502  address.truncate(address.findRev("%"));
4503  ndevice->internalSetIpV6Broadcast(address);
4504  }
4505  }
4506  s = getnameinfo(ifa->ifa_ifu.ifu_dstaddr, (family == AF_INET) ? sizeof(struct sockaddr_in) : sizeof(struct sockaddr_in6), host, NI_MAXHOST, NULL, 0, NI_NUMERICHOST);
4507  if (s == 0) {
4508  TQString address(host);
4509  if (family == AF_INET) {
4510  ndevice->internalSetIpV4Destination(address);
4511  }
4512  if (family == AF_INET6) {
4513  address.truncate(address.findRev("%"));
4514  ndevice->internalSetIpV6Destination(address);
4515  }
4516  }
4517  }
4518  }
4519  }
4520  }
4521 
4522  freeifaddrs(ifaddr);
4523 
4524  // Gather statistics
4525  TQString valuesnodename = systempath + "/statistics/";
4526  TQDir valuesdir(valuesnodename);
4527  valuesdir.setFilter(TQDir::All);
4528  TQString nodename;
4529  const TQFileInfoList *dirlist = valuesdir.entryInfoList();
4530  if (dirlist) {
4531  TQFileInfoListIterator valuesdirit(*dirlist);
4532  TQFileInfo *dirfi;
4533  while ( (dirfi = valuesdirit.current()) != 0 ) {
4534  nodename = dirfi->fileName();
4535  TQFile file( valuesnodename + nodename );
4536  if ( file.open( IO_ReadOnly ) ) {
4537  TQTextStream stream( &file );
4538  TQString line;
4539  line = stream.readLine();
4540  if (nodename == "rx_bytes") {
4541  ndevice->internalSetRxBytes(line.toDouble());
4542  }
4543  if (nodename == "tx_bytes") {
4544  ndevice->internalSetTxBytes(line.toDouble());
4545  }
4546  if (nodename == "rx_packets") {
4547  ndevice->internalSetRxPackets(line.toDouble());
4548  }
4549  if (nodename == "tx_packets") {
4550  ndevice->internalSetTxPackets(line.toDouble());
4551  }
4552  file.close();
4553  }
4554  ++valuesdirit;
4555  }
4556  }
4557  }
4558  }
4559  }
4560 
4561  if ((device->type() == TDEGenericDeviceType::OtherSensor) || (device->type() == TDEGenericDeviceType::ThermalSensor)) {
4562  // Populate all sensor values
4563  TDESensorClusterMap sensors;
4564  TQString valuesnodename = systempath + "/";
4565  TQDir valuesdir(valuesnodename);
4566  valuesdir.setFilter(TQDir::All);
4567  TQString nodename;
4568  const TQFileInfoList *dirlist = valuesdir.entryInfoList();
4569  if (dirlist) {
4570  TQFileInfoListIterator valuesdirit(*dirlist);
4571  TQFileInfo *dirfi;
4572  while ( (dirfi = valuesdirit.current()) != 0 ) {
4573  nodename = dirfi->fileName();
4574  if (nodename.contains("_")) {
4575  TQFile file( valuesnodename + nodename );
4576  if ( file.open( IO_ReadOnly ) ) {
4577  TQTextStream stream( &file );
4578  TQString line;
4579  line = stream.readLine();
4580  TQStringList sensornodelist = TQStringList::split("_", nodename);
4581  TQString sensornodename = *(sensornodelist.at(0));
4582  TQString sensornodetype = *(sensornodelist.at(1));
4583  double lineValue = line.toDouble();
4584  if (!sensornodename.contains("fan")) {
4585  lineValue = lineValue / 1000.0;
4586  }
4587  if (sensornodetype == "label") {
4588  sensors[sensornodename].label = line;
4589  }
4590  if (sensornodetype == "input") {
4591  sensors[sensornodename].current = lineValue;
4592  }
4593  if (sensornodetype == "min") {
4594  sensors[sensornodename].minimum = lineValue;
4595  }
4596  if (sensornodetype == "max") {
4597  sensors[sensornodename].maximum = lineValue;
4598  }
4599  if (sensornodetype == "warn") {
4600  sensors[sensornodename].warning = lineValue;
4601  }
4602  if (sensornodetype == "crit") {
4603  sensors[sensornodename].critical = lineValue;
4604  }
4605  file.close();
4606  }
4607  }
4608  ++valuesdirit;
4609  }
4610  }
4611 
4612  TDESensorDevice* sdevice = dynamic_cast<TDESensorDevice*>(device);
4613  sdevice->internalSetValues(sensors);
4614  }
4615 
4616  if (device->type() == TDEGenericDeviceType::Battery) {
4617  // Populate all battery values
4618  TDEBatteryDevice* bdevice = dynamic_cast<TDEBatteryDevice*>(device);
4619  TQString valuesnodename = systempath + "/";
4620  TQDir valuesdir(valuesnodename);
4621  valuesdir.setFilter(TQDir::All);
4622  TQString nodename;
4623  const TQFileInfoList *dirlist = valuesdir.entryInfoList();
4624  if (dirlist) {
4625  TQFileInfoListIterator valuesdirit(*dirlist);
4626  TQFileInfo *dirfi;
4627  while ( (dirfi = valuesdirit.current()) != 0 ) {
4628  nodename = dirfi->fileName();
4629  TQFile file( valuesnodename + nodename );
4630  if ( file.open( IO_ReadOnly ) ) {
4631  TQTextStream stream( &file );
4632  TQString line;
4633  line = stream.readLine();
4634  if (nodename == "alarm") {
4635  bdevice->internalSetAlarmEnergy(line.toDouble()/1000000.0);
4636  }
4637  if (nodename == "energy_full") {
4638  bdevice->internalSetMaximumEnergy(line.toDouble()/1000000.0);
4639  }
4640  if (nodename == "energy_full_design") {
4641  bdevice->internalSetMaximumDesignEnergy(line.toDouble()/1000000.0);
4642  }
4643  if (nodename == "energy_now") {
4644  bdevice->internalSetEnergy(line.toDouble()/1000000.0);
4645  }
4646  if (nodename == "manufacturer") {
4647  bdevice->internalSetVendorName(line.stripWhiteSpace());
4648  }
4649  if (nodename == "model_name") {
4650  bdevice->internalSetVendorModel(line.stripWhiteSpace());
4651  }
4652  if (nodename == "power_now") {
4653  bdevice->internalSetDischargeRate(line.toDouble()/1000000.0);
4654  }
4655  if (nodename == "present") {
4656  bdevice->internalSetInstalled(line.toInt());
4657  }
4658  if (nodename == "serial_number") {
4659  bdevice->internalSetSerialNumber(line.stripWhiteSpace());
4660  }
4661  if (nodename == "status") {
4662  bdevice->internalSetStatus(line);
4663  }
4664  if (nodename == "technology") {
4665  bdevice->internalSetTechnology(line);
4666  }
4667  if (nodename == "voltage_min_design") {
4668  bdevice->internalSetMinimumVoltage(line.toDouble()/1000000.0);
4669  }
4670  if (nodename == "voltage_now") {
4671  bdevice->internalSetVoltage(line.toDouble()/1000000.0);
4672  }
4673  file.close();
4674  }
4675  ++valuesdirit;
4676  }
4677  }
4678 
4679  // Calculate time remaining
4680  // Discharge rate is in watt-hours
4681  // Energy is in watt-hours
4682  // Therefore, energy/rate = time in hours
4683  // Convert to seconds...
4684  bdevice->internalSetTimeRemaining((bdevice->energy()/bdevice->dischargeRate())*60*60);
4685  }
4686 
4687  if (device->type() == TDEGenericDeviceType::PowerSupply) {
4688  // Populate all power supply values
4689  TDEMainsPowerDevice* pdevice = dynamic_cast<TDEMainsPowerDevice*>(device);
4690  TQString valuesnodename = systempath + "/";
4691  TQDir valuesdir(valuesnodename);
4692  valuesdir.setFilter(TQDir::All);
4693  TQString nodename;
4694  const TQFileInfoList *dirlist = valuesdir.entryInfoList();
4695  if (dirlist) {
4696  TQFileInfoListIterator valuesdirit(*dirlist);
4697  TQFileInfo *dirfi;
4698  while ( (dirfi = valuesdirit.current()) != 0 ) {
4699  nodename = dirfi->fileName();
4700  TQFile file( valuesnodename + nodename );
4701  if ( file.open( IO_ReadOnly ) ) {
4702  TQTextStream stream( &file );
4703  TQString line;
4704  line = stream.readLine();
4705  if (nodename == "manufacturer") {
4706  pdevice->internalSetVendorName(line.stripWhiteSpace());
4707  }
4708  if (nodename == "model_name") {
4709  pdevice->internalSetVendorModel(line.stripWhiteSpace());
4710  }
4711  if (nodename == "online") {
4712  pdevice->internalSetOnline(line.toInt());
4713  }
4714  if (nodename == "serial_number") {
4715  pdevice->internalSetSerialNumber(line.stripWhiteSpace());
4716  }
4717  file.close();
4718  }
4719  ++valuesdirit;
4720  }
4721  }
4722  }
4723 
4724  if (device->type() == TDEGenericDeviceType::Backlight) {
4725  // Populate all backlight values
4726  TDEBacklightDevice* bdevice = dynamic_cast<TDEBacklightDevice*>(device);
4727  TQString valuesnodename = systempath + "/";
4728  TQDir valuesdir(valuesnodename);
4729  valuesdir.setFilter(TQDir::All);
4730  TQString nodename;
4731  const TQFileInfoList *dirlist = valuesdir.entryInfoList();
4732  if (dirlist) {
4733  TQFileInfoListIterator valuesdirit(*dirlist);
4734  TQFileInfo *dirfi;
4735  while ( (dirfi = valuesdirit.current()) != 0 ) {
4736  nodename = dirfi->fileName();
4737  TQFile file( valuesnodename + nodename );
4738  if ( file.open( IO_ReadOnly ) ) {
4739  TQTextStream stream( &file );
4740  TQString line;
4741  line = stream.readLine();
4742  if (nodename == "bl_power") {
4743  TDEDisplayPowerLevel::TDEDisplayPowerLevel pl = TDEDisplayPowerLevel::On;
4744  int rpl = line.toInt();
4745  if (rpl == FB_BLANK_UNBLANK) {
4746  pl = TDEDisplayPowerLevel::On;
4747  }
4748  else if (rpl == FB_BLANK_POWERDOWN) {
4749  pl = TDEDisplayPowerLevel::Off;
4750  }
4751  bdevice->internalSetPowerLevel(pl);
4752  }
4753  if (nodename == "max_brightness") {
4754  bdevice->internalSetMaximumRawBrightness(line.toInt());
4755  }
4756  if (nodename == "actual_brightness") {
4757  bdevice->internalSetCurrentRawBrightness(line.toInt());
4758  }
4759  file.close();
4760  }
4761  ++valuesdirit;
4762  }
4763  }
4764  }
4765 
4766  if (device->type() == TDEGenericDeviceType::Monitor) {
4767  TDEMonitorDevice* mdevice = dynamic_cast<TDEMonitorDevice*>(device);
4768  TQString valuesnodename = systempath + "/";
4769  TQDir valuesdir(valuesnodename);
4770  valuesdir.setFilter(TQDir::All);
4771  TQString nodename;
4772  const TQFileInfoList *dirlist = valuesdir.entryInfoList();
4773  if (dirlist) {
4774  TQFileInfoListIterator valuesdirit(*dirlist);
4775  TQFileInfo *dirfi;
4776  while ( (dirfi = valuesdirit.current()) != 0 ) {
4777  nodename = dirfi->fileName();
4778  TQFile file( valuesnodename + nodename );
4779  if ( file.open( IO_ReadOnly ) ) {
4780  TQTextStream stream( &file );
4781  TQString line;
4782  line = stream.readLine();
4783  if (nodename == "status") {
4784  mdevice->internalSetConnected(line.lower() == "connected");
4785  }
4786  if (nodename == "enabled") {
4787  mdevice->internalSetEnabled(line.lower() == "enabled");
4788  }
4789  if (nodename == "modes") {
4790  TQStringList resinfo;
4791  TQStringList resolutionsStringList = line.upper();
4792  while ((!stream.atEnd()) && (!line.isNull())) {
4793  line = stream.readLine();
4794  if (!line.isNull()) {
4795  resolutionsStringList.append(line.upper());
4796  }
4797  }
4798  TDEResolutionList resolutions;
4799  resolutions.clear();
4800  for (TQStringList::Iterator it = resolutionsStringList.begin(); it != resolutionsStringList.end(); ++it) {
4801  resinfo = TQStringList::split('X', *it, true);
4802  resolutions.append(TDEResolutionPair((*(resinfo.at(0))).toUInt(), (*(resinfo.at(1))).toUInt()));
4803  }
4804  mdevice->internalSetResolutions(resolutions);
4805  }
4806  if (nodename == "dpms") {
4807  TDEDisplayPowerLevel::TDEDisplayPowerLevel pl = TDEDisplayPowerLevel::On;
4808  if (line == "On") {
4809  pl = TDEDisplayPowerLevel::On;
4810  }
4811  else if (line == "Standby") {
4812  pl = TDEDisplayPowerLevel::Standby;
4813  }
4814  else if (line == "Suspend") {
4815  pl = TDEDisplayPowerLevel::Suspend;
4816  }
4817  else if (line == "Off") {
4818  pl = TDEDisplayPowerLevel::Off;
4819  }
4820  mdevice->internalSetPowerLevel(pl);
4821  }
4822  file.close();
4823  }
4824  ++valuesdirit;
4825  }
4826  }
4827 
4828  TQString genericPortName = mdevice->systemPath();
4829  genericPortName.remove(0, genericPortName.find("-")+1);
4830  genericPortName.truncate(genericPortName.findRev("-"));
4831  mdevice->internalSetPortType(genericPortName);
4832 
4833  if (mdevice->connected()) {
4834  TQPair<TQString,TQString> monitor_info = getEDIDMonitorName(device->systemPath());
4835  if (!monitor_info.first.isNull()) {
4836  mdevice->internalSetVendorName(monitor_info.first);
4837  mdevice->internalSetVendorModel(monitor_info.second);
4838  mdevice->m_friendlyName = monitor_info.first + " " + monitor_info.second;
4839  }
4840  else {
4841  mdevice->m_friendlyName = i18n("Generic %1 Device").arg(genericPortName);
4842  }
4843  mdevice->internalSetEdid(getEDID(mdevice->systemPath()));
4844  }
4845  else {
4846  mdevice->m_friendlyName = i18n("Disconnected %1 Port").arg(genericPortName);
4847  mdevice->internalSetEdid(TQByteArray());
4848  mdevice->internalSetResolutions(TDEResolutionList());
4849  }
4850 
4851  // FIXME
4852  // Much of the code in libtderandr should be integrated into/interfaced with this library
4853  }
4854 
4855  if (device->type() == TDEGenericDeviceType::RootSystem) {
4856  // Try to obtain as much generic information about this system as possible
4857  TDERootSystemDevice* rdevice = dynamic_cast<TDERootSystemDevice*>(device);
4858 
4859  // Guess at my form factor
4860  // dmidecode would tell me this, but is somewhat unreliable
4861  TDESystemFormFactor::TDESystemFormFactor formfactor = TDESystemFormFactor::Desktop;
4862  if (listByDeviceClass(TDEGenericDeviceType::Backlight).count() > 0) { // Is this really a good way to determine if a machine is a laptop?
4863  formfactor = TDESystemFormFactor::Laptop;
4864  }
4865  rdevice->internalSetFormFactor(formfactor);
4866 
4867  TQString valuesnodename = "/sys/power/";
4868  TQDir valuesdir(valuesnodename);
4869  valuesdir.setFilter(TQDir::All);
4870  TQString nodename;
4871  const TQFileInfoList *dirlist = valuesdir.entryInfoList();
4872  if (dirlist) {
4873  TQFileInfoListIterator valuesdirit(*dirlist);
4874  TQFileInfo *dirfi;
4875  while ( (dirfi = valuesdirit.current()) != 0 ) {
4876  nodename = dirfi->fileName();
4877  TQFile file( valuesnodename + nodename );
4878  if ( file.open( IO_ReadOnly ) ) {
4879  TQTextStream stream( &file );
4880  TQString line;
4881  line = stream.readLine();
4882  if (nodename == "state") {
4883  TDESystemPowerStateList powerstates;
4884  // Always assume that these two fully on/fully off states are available
4885  powerstates.append(TDESystemPowerState::Active);
4886  powerstates.append(TDESystemPowerState::PowerOff);
4887  if (line.contains("standby")) {
4888  powerstates.append(TDESystemPowerState::Standby);
4889  }
4890  if (line.contains("mem")) {
4891  powerstates.append(TDESystemPowerState::Suspend);
4892  }
4893  if (line.contains("disk")) {
4894  powerstates.append(TDESystemPowerState::Hibernate);
4895  }
4896  rdevice->internalSetPowerStates(powerstates);
4897  }
4898  if (nodename == "disk") {
4899  // Get list of available hibernation methods
4900  TDESystemHibernationMethodList hibernationmethods;
4901  if (line.contains("platform")) {
4902  hibernationmethods.append(TDESystemHibernationMethod::Platform);
4903  }
4904  if (line.contains("shutdown")) {
4905  hibernationmethods.append(TDESystemHibernationMethod::Shutdown);
4906  }
4907  if (line.contains("reboot")) {
4908  hibernationmethods.append(TDESystemHibernationMethod::Reboot);
4909  }
4910  if (line.contains("testproc")) {
4911  hibernationmethods.append(TDESystemHibernationMethod::TestProc);
4912  }
4913  if (line.contains("test")) {
4914  hibernationmethods.append(TDESystemHibernationMethod::Test);
4915  }
4916  rdevice->internalSetHibernationMethods(hibernationmethods);
4917 
4918  // Get current hibernation method
4919  line.truncate(line.findRev("]"));
4920  line.remove(0, line.findRev("[")+1);
4921  TDESystemHibernationMethod::TDESystemHibernationMethod hibernationmethod = TDESystemHibernationMethod::Unsupported;
4922  if (line.contains("platform")) {
4923  hibernationmethod = TDESystemHibernationMethod::Platform;
4924  }
4925  if (line.contains("shutdown")) {
4926  hibernationmethod = TDESystemHibernationMethod::Shutdown;
4927  }
4928  if (line.contains("reboot")) {
4929  hibernationmethod = TDESystemHibernationMethod::Reboot;
4930  }
4931  if (line.contains("testproc")) {
4932  hibernationmethod = TDESystemHibernationMethod::TestProc;
4933  }
4934  if (line.contains("test")) {
4935  hibernationmethod = TDESystemHibernationMethod::Test;
4936  }
4937  rdevice->internalSetHibernationMethod(hibernationmethod);
4938  }
4939  if (nodename == "image_size") {
4940  rdevice->internalSetDiskSpaceNeededForHibernation(line.toULong());
4941  }
4942  file.close();
4943  }
4944  ++valuesdirit;
4945  }
4946  }
4947  }
4948 
4949  // NOTE
4950  // Keep these two handlers (Event and Input) in sync!
4951 
4952  if (device->type() == TDEGenericDeviceType::Event) {
4953  // Try to obtain as much type information about this event device as possible
4954  TDEEventDevice* edevice = dynamic_cast<TDEEventDevice*>(device);
4955  if (edevice->systemPath().contains("PNP0C0D")) {
4956  edevice->internalSetEventType(TDEEventDeviceType::ACPILidSwitch);
4957  }
4958  else if (edevice->systemPath().contains("PNP0C0E") || edevice->systemPath().contains("/LNXSLPBN")) {
4959  edevice->internalSetEventType(TDEEventDeviceType::ACPISleepButton);
4960  }
4961  else if (edevice->systemPath().contains("PNP0C0C") || edevice->systemPath().contains("/LNXPWRBN")) {
4962  edevice->internalSetEventType(TDEEventDeviceType::ACPIPowerButton);
4963  }
4964  else {
4965  edevice->internalSetEventType(TDEEventDeviceType::Unknown);
4966  }
4967  }
4968 
4969  if (device->type() == TDEGenericDeviceType::Input) {
4970  // Try to obtain as much type information about this input device as possible
4971  TDEInputDevice* idevice = dynamic_cast<TDEInputDevice*>(device);
4972  if (idevice->systemPath().contains("PNP0C0D")) {
4973  idevice->internalSetInputType(TDEInputDeviceType::ACPILidSwitch);
4974  }
4975  else if (idevice->systemPath().contains("PNP0C0E") || idevice->systemPath().contains("/LNXSLPBN")) {
4976  idevice->internalSetInputType(TDEInputDeviceType::ACPISleepButton);
4977  }
4978  else if (idevice->systemPath().contains("PNP0C0C") || idevice->systemPath().contains("/LNXPWRBN")) {
4979  idevice->internalSetInputType(TDEInputDeviceType::ACPIPowerButton);
4980  }
4981  else {
4982  idevice->internalSetInputType(TDEInputDeviceType::Unknown);
4983  }
4984  }
4985 
4986  if (device->type() == TDEGenericDeviceType::Event) {
4987  // Try to obtain as much specific information about this event device as possible
4988  TDEEventDevice* edevice = dynamic_cast<TDEEventDevice*>(device);
4989  int r;
4990  char switches[SW_CNT];
4991 
4992  // Figure out which switch types are supported, if any
4993  TDESwitchType::TDESwitchType supportedSwitches = TDESwitchType::Null;
4994  if (edevice->m_fd < 0) {
4995  edevice->m_fd = open(edevice->deviceNode().ascii(), O_RDONLY);
4996  }
4997  r = ioctl(edevice->m_fd, EVIOCGBIT(EV_SW, sizeof(switches)), switches);
4998  if (r > 0) {
4999  if (BIT_IS_SET(switches, SW_LID)) {
5000  supportedSwitches = supportedSwitches | TDESwitchType::Lid;
5001  }
5002  if (BIT_IS_SET(switches, SW_TABLET_MODE)) {
5003  supportedSwitches = supportedSwitches | TDESwitchType::TabletMode;
5004  }
5005  if (BIT_IS_SET(switches, SW_RFKILL_ALL)) {
5006  supportedSwitches = supportedSwitches | TDESwitchType::RFKill;
5007  }
5008  if (BIT_IS_SET(switches, SW_RADIO)) {
5009  supportedSwitches = supportedSwitches | TDESwitchType::Radio;
5010  }
5011  if (BIT_IS_SET(switches, SW_MICROPHONE_INSERT)) {
5012  supportedSwitches = supportedSwitches | TDESwitchType::MicrophoneInsert;
5013  }
5014  if (BIT_IS_SET(switches, SW_DOCK)) {
5015  supportedSwitches = supportedSwitches | TDESwitchType::Dock;
5016  }
5017  if (BIT_IS_SET(switches, SW_LINEOUT_INSERT)) {
5018  supportedSwitches = supportedSwitches | TDESwitchType::LineOutInsert;
5019  }
5020  if (BIT_IS_SET(switches, SW_JACK_PHYSICAL_INSERT)) {
5021  supportedSwitches = supportedSwitches | TDESwitchType::JackPhysicalInsert;
5022  }
5023  if (BIT_IS_SET(switches, SW_VIDEOOUT_INSERT)) {
5024  supportedSwitches = supportedSwitches | TDESwitchType::VideoOutInsert;
5025  }
5026 #if 0 // Some old kernels don't provide these defines... [FIXME]
5027  if (BIT_IS_SET(switches, SW_CAMERA_LENS_COVER)) {
5028  supportedSwitches = supportedSwitches | TDESwitchType::CameraLensCover;
5029  }
5030  if (BIT_IS_SET(switches, SW_KEYPAD_SLIDE)) {
5031  supportedSwitches = supportedSwitches | TDESwitchType::KeypadSlide;
5032  }
5033  if (BIT_IS_SET(switches, SW_FRONT_PROXIMITY)) {
5034  supportedSwitches = supportedSwitches | TDESwitchType::FrontProximity;
5035  }
5036  if (BIT_IS_SET(switches, SW_ROTATE_LOCK)) {
5037  supportedSwitches = supportedSwitches | TDESwitchType::RotateLock;
5038  }
5039  if (BIT_IS_SET(switches, SW_LINEIN_INSERT)) {
5040  supportedSwitches = supportedSwitches | TDESwitchType::LineInInsert;
5041  }
5042 #endif
5043  }
5044  edevice->internalSetProvidedSwitches(supportedSwitches);
5045 
5046  // Figure out which switch types are active, if any
5047  TDESwitchType::TDESwitchType activeSwitches = TDESwitchType::Null;
5048  r = ioctl(edevice->m_fd, EVIOCGSW(sizeof(switches)), switches);
5049  if (r > 0) {
5050  if (BIT_IS_SET(switches, SW_LID)) {
5051  activeSwitches = activeSwitches | TDESwitchType::Lid;
5052  }
5053  if (BIT_IS_SET(switches, SW_TABLET_MODE)) {
5054  activeSwitches = activeSwitches | TDESwitchType::TabletMode;
5055  }
5056  if (BIT_IS_SET(switches, SW_RFKILL_ALL)) {
5057  activeSwitches = activeSwitches | TDESwitchType::RFKill;
5058  }
5059  if (BIT_IS_SET(switches, SW_RADIO)) {
5060  activeSwitches = activeSwitches | TDESwitchType::Radio;
5061  }
5062  if (BIT_IS_SET(switches, SW_MICROPHONE_INSERT)) {
5063  activeSwitches = activeSwitches | TDESwitchType::MicrophoneInsert;
5064  }
5065  if (BIT_IS_SET(switches, SW_DOCK)) {
5066  activeSwitches = activeSwitches | TDESwitchType::Dock;
5067  }
5068  if (BIT_IS_SET(switches, SW_LINEOUT_INSERT)) {
5069  activeSwitches = activeSwitches | TDESwitchType::LineOutInsert;
5070  }
5071  if (BIT_IS_SET(switches, SW_JACK_PHYSICAL_INSERT)) {
5072  activeSwitches = activeSwitches | TDESwitchType::JackPhysicalInsert;
5073  }
5074  if (BIT_IS_SET(switches, SW_VIDEOOUT_INSERT)) {
5075  activeSwitches = activeSwitches | TDESwitchType::VideoOutInsert;
5076  }
5077 #if 0 // Some old kernels don't provide these defines... [FIXME]
5078  if (BIT_IS_SET(switches, SW_CAMERA_LENS_COVER)) {
5079  activeSwitches = activeSwitches | TDESwitchType::CameraLensCover;
5080  }
5081  if (BIT_IS_SET(switches, SW_KEYPAD_SLIDE)) {
5082  activeSwitches = activeSwitches | TDESwitchType::KeypadSlide;
5083  }
5084  if (BIT_IS_SET(switches, SW_FRONT_PROXIMITY)) {
5085  activeSwitches = activeSwitches | TDESwitchType::FrontProximity;
5086  }
5087  if (BIT_IS_SET(switches, SW_ROTATE_LOCK)) {
5088  activeSwitches = activeSwitches | TDESwitchType::RotateLock;
5089  }
5090  if (BIT_IS_SET(switches, SW_LINEIN_INSERT)) {
5091  activeSwitches = activeSwitches | TDESwitchType::LineInInsert;
5092  }
5093 #endif
5094  }
5095  edevice->internalSetActiveSwitches(activeSwitches);
5096 
5097  edevice->internalStartFdMonitoring(this);
5098  }
5099 
5100  // Root devices are still special
5101  if ((device->type() == TDEGenericDeviceType::Root) || (device->type() == TDEGenericDeviceType::RootSystem)) {
5102  systempath = device->systemPath();
5103  }
5104 
5105  // Set basic device information again, as some information may have changed
5106  device->internalSetName(devicename);
5107  device->internalSetDeviceNode(devicenode);
5108  device->internalSetSystemPath(systempath);
5109  device->internalSetVendorID(devicevendorid);
5110  device->internalSetModelID(devicemodelid);
5111  device->internalSetVendorEncoded(devicevendoridenc);
5112  device->internalSetModelEncoded(devicemodelidenc);
5113  device->internalSetSubVendorID(devicesubvendorid);
5114  device->internalSetSubModelID(devicesubmodelid);
5115  device->internalSetDeviceDriver(devicedriver);
5116  device->internalSetSubsystem(devicesubsystem);
5117  device->internalSetPCIClass(devicepciclass);
5118 
5119  // Internal use only!
5120  device->m_udevtype = devicetype;
5121  device->m_udevdevicetypestring = devicetypestring;
5122  device->udevdevicetypestring_alt = devicetypestring_alt;
5123 
5124  if (temp_udev_device) {
5125  udev_device_unref(dev);
5126  }
5127 
5128  return device;
5129 }
5130 
5131 void TDEHardwareDevices::updateBlacklists(TDEGenericDevice* hwdevice, udev_device* dev) {
5132  // HACK
5133  // I am lucky enough to have a Flash drive that spams udev continually with device change events
5134  // I imagine I am not the only one, so here is a section in which specific devices can be blacklisted!
5135 
5136  // For "U3 System" fake CD
5137  if ((hwdevice->vendorID() == "08ec") && (hwdevice->modelID() == "0020") && (TQString(udev_device_get_property_value(dev, "ID_TYPE")) == "cd")) {
5138  hwdevice->internalSetBlacklistedForUpdate(true);
5139  }
5140 }
5141 
5142 bool TDEHardwareDevices::queryHardwareInformation() {
5143  if (!m_udevStruct) {
5144  return false;
5145  }
5146 
5147  // Prepare the device list for repopulation
5148  m_deviceList.clear();
5149  addCoreSystemDevices();
5150 
5151  struct udev_enumerate *enumerate;
5152  struct udev_list_entry *devices, *dev_list_entry;
5153  struct udev_device *dev;
5154 
5155  // Create a list of all devices
5156  enumerate = udev_enumerate_new(m_udevStruct);
5157  udev_enumerate_add_match_subsystem(enumerate, NULL);
5158  udev_enumerate_scan_devices(enumerate);
5159  devices = udev_enumerate_get_list_entry(enumerate);
5160  // Get detailed information on each detected device
5161  udev_list_entry_foreach(dev_list_entry, devices) {
5162  const char *path;
5163 
5164  // Get the filename of the /sys entry for the device and create a udev_device object (dev) representing it
5165  path = udev_list_entry_get_name(dev_list_entry);
5166  dev = udev_device_new_from_syspath(m_udevStruct, path);
5167 
5168  TDEGenericDevice* device = classifyUnknownDevice(dev);
5169 
5170  // Make sure this device is not a duplicate
5171  TDEGenericDevice *hwdevice;
5172  for (hwdevice = m_deviceList.first(); hwdevice; hwdevice = m_deviceList.next()) {
5173  if (hwdevice->systemPath() == device->systemPath()) {
5174  delete device;
5175  device = 0;
5176  break;
5177  }
5178  }
5179 
5180  if (device) {
5181  m_deviceList.append(device);
5182  }
5183 
5184  udev_device_unref(dev);
5185  }
5186 
5187  // Free the enumerator object
5188  udev_enumerate_unref(enumerate);
5189 
5190  // Update parent/child tables for all devices
5191  updateParentDeviceInformation();
5192 
5193  emit hardwareEvent(TDEHardwareEvent::HardwareListModified, TQString());
5194 
5195  return true;
5196 }
5197 
5198 void TDEHardwareDevices::updateParentDeviceInformation(TDEGenericDevice* hwdevice) {
5199  // Scan for the first path up the sysfs tree that is available in the main hardware table
5200  bool done = false;
5201  TQString current_path = hwdevice->systemPath();
5202  TDEGenericDevice* parentdevice = 0;
5203 
5204  if (current_path.endsWith("/")) {
5205  current_path.truncate(current_path.findRev("/"));
5206  }
5207  while (done == false) {
5208  current_path.truncate(current_path.findRev("/"));
5209  if (current_path.startsWith("/sys/devices")) {
5210  if (current_path.endsWith("/")) {
5211  current_path.truncate(current_path.findRev("/"));
5212  }
5213  parentdevice = findBySystemPath(current_path);
5214  if (parentdevice) {
5215  done = true;
5216  }
5217  }
5218  else {
5219  // Abort!
5220  done = true;
5221  }
5222  }
5223 
5224  hwdevice->internalSetParentDevice(parentdevice);
5225 }
5226 
5227 void TDEHardwareDevices::updateParentDeviceInformation() {
5228  TDEGenericDevice *hwdevice;
5229 
5230  // We can't use m_deviceList directly as m_deviceList can only have one iterator active against it at any given time
5231  TDEGenericHardwareList devList = listAllPhysicalDevices();
5232  for ( hwdevice = devList.first(); hwdevice; hwdevice = devList.next() ) {
5233  updateParentDeviceInformation(hwdevice);
5234  }
5235 }
5236 
5237 void TDEHardwareDevices::addCoreSystemDevices() {
5238  TDEGenericDevice *hwdevice;
5239 
5240  // Add the Main Root System Device, which provides all other devices
5241  hwdevice = new TDERootSystemDevice(TDEGenericDeviceType::RootSystem);
5242  hwdevice->internalSetSystemPath("/sys/devices");
5243  m_deviceList.append(hwdevice);
5244  rescanDeviceInformation(hwdevice);
5245 
5246  // Add core top-level devices in /sys/devices to the hardware listing
5247  TQStringList holdingDeviceNodes;
5248  TQString devicesnodename = "/sys/devices";
5249  TQDir devicesdir(devicesnodename);
5250  devicesdir.setFilter(TQDir::All);
5251  TQString nodename;
5252  const TQFileInfoList *dirlist = devicesdir.entryInfoList();
5253  if (dirlist) {
5254  TQFileInfoListIterator devicesdirit(*dirlist);
5255  TQFileInfo *dirfi;
5256  while ( (dirfi = devicesdirit.current()) != 0 ) {
5257  nodename = dirfi->fileName();
5258  if (nodename != "." && nodename != "..") {
5259  hwdevice = new TDEGenericDevice(TDEGenericDeviceType::Root);
5260  hwdevice->internalSetSystemPath(dirfi->absFilePath());
5261  m_deviceList.append(hwdevice);
5262  }
5263  ++devicesdirit;
5264  }
5265  }
5266 
5267  // Handle CPUs, which are currently handled terribly by udev
5268  // Parse /proc/cpuinfo to extract some information about the CPUs
5269  hwdevice = 0;
5270  TQDir d("/sys/devices/system/cpu/");
5271  d.setFilter( TQDir::Dirs );
5272  const TQFileInfoList *list = d.entryInfoList();
5273  TQFileInfoListIterator it( *list );
5274  TQFileInfo *fi;
5275  while ((fi = it.current()) != 0) {
5276  TQString directoryName = fi->fileName();
5277  if (directoryName.startsWith("cpu")) {
5278  directoryName = directoryName.remove(0,3);
5279  bool isInt;
5280  int processorNumber = directoryName.toUInt(&isInt, 10);
5281  if (isInt) {
5282  hwdevice = new TDECPUDevice(TDEGenericDeviceType::CPU);
5283  hwdevice->internalSetSystemPath(TQString("/sys/devices/system/cpu/cpu%1").arg(processorNumber));
5284  m_deviceList.append(hwdevice);
5285  }
5286  }
5287  ++it;
5288  }
5289 
5290  // Populate CPU information
5291  processModifiedCPUs();
5292 }
5293 
5294 TQString TDEHardwareDevices::findPCIDeviceName(TQString vendorid, TQString modelid, TQString subvendorid, TQString submodelid) {
5295  TQString vendorName = TQString::null;
5296  TQString modelName = TQString::null;
5297  TQString friendlyName = TQString::null;
5298 
5299  if (!pci_id_map) {
5300  pci_id_map = new TDEDeviceIDMap;
5301 
5302  TQString database_filename = "/usr/share/pci.ids";
5303  if (!TQFile::exists(database_filename)) {
5304  database_filename = "/usr/share/misc/pci.ids";
5305  }
5306  if (!TQFile::exists(database_filename)) {
5307  printf("[tdehardwaredevices] Unable to locate PCI information database pci.ids\n"); fflush(stdout);
5308  return i18n("Unknown PCI Device");
5309  }
5310 
5311  TQFile database(database_filename);
5312  if (database.open(IO_ReadOnly)) {
5313  TQTextStream stream(&database);
5314  TQString line;
5315  TQString vendorID;
5316  TQString modelID;
5317  TQString subvendorID;
5318  TQString submodelID;
5319  TQString deviceMapKey;
5320  TQStringList devinfo;
5321  while (!stream.atEnd()) {
5322  line = stream.readLine();
5323  if ((!line.upper().startsWith("\t")) && (!line.upper().startsWith("#"))) {
5324  line.replace("\t", "");
5325  devinfo = TQStringList::split(' ', line, false);
5326  vendorID = *(devinfo.at(0));
5327  vendorName = line;
5328  vendorName.remove(0, vendorName.find(" "));
5329  vendorName = vendorName.stripWhiteSpace();
5330  modelName = TQString::null;
5331  deviceMapKey = vendorID.lower() + ":::";
5332  }
5333  else {
5334  if ((line.upper().startsWith("\t")) && (!line.upper().startsWith("\t\t"))) {
5335  line.replace("\t", "");
5336  devinfo = TQStringList::split(' ', line, false);
5337  modelID = *(devinfo.at(0));
5338  modelName = line;
5339  modelName.remove(0, modelName.find(" "));
5340  modelName = modelName.stripWhiteSpace();
5341  deviceMapKey = vendorID.lower() + ":" + modelID.lower() + "::";
5342  }
5343  else {
5344  if (line.upper().startsWith("\t\t")) {
5345  line.replace("\t", "");
5346  devinfo = TQStringList::split(' ', line, false);
5347  subvendorID = *(devinfo.at(0));
5348  submodelID = *(devinfo.at(1));
5349  modelName = line;
5350  modelName.remove(0, modelName.find(" "));
5351  modelName = modelName.stripWhiteSpace();
5352  modelName.remove(0, modelName.find(" "));
5353  modelName = modelName.stripWhiteSpace();
5354  deviceMapKey = vendorID.lower() + ":" + modelID.lower() + ":" + subvendorID.lower() + ":" + submodelID.lower();
5355  }
5356  }
5357  }
5358  if (modelName.isNull()) {
5359  pci_id_map->insert(deviceMapKey, "***UNKNOWN DEVICE*** " + vendorName, true);
5360  }
5361  else {
5362  pci_id_map->insert(deviceMapKey, vendorName + " " + modelName, true);
5363  }
5364  }
5365  database.close();
5366  }
5367  else {
5368  printf("[tdehardwaredevices] Unable to open PCI information database %s\n", database_filename.ascii()); fflush(stdout);
5369  }
5370  }
5371 
5372  if (pci_id_map) {
5373  TQString deviceName;
5374  TQString deviceMapKey = vendorid.lower() + ":" + modelid.lower() + ":" + subvendorid.lower() + ":" + submodelid.lower();
5375 
5376  deviceName = (*pci_id_map)[deviceMapKey];
5377  if (deviceName.isNull() || deviceName.startsWith("***UNKNOWN DEVICE*** ")) {
5378  deviceMapKey = vendorid.lower() + ":" + modelid.lower() + ":" + subvendorid.lower() + ":";
5379  deviceName = (*pci_id_map)[deviceMapKey];
5380  if (deviceName.isNull() || deviceName.startsWith("***UNKNOWN DEVICE*** ")) {
5381  deviceMapKey = vendorid.lower() + ":" + modelid.lower() + "::";
5382  deviceName = (*pci_id_map)[deviceMapKey];
5383  }
5384  }
5385 
5386  if (deviceName.startsWith("***UNKNOWN DEVICE*** ")) {
5387  deviceName.replace("***UNKNOWN DEVICE*** ", "");
5388  deviceName.prepend(i18n("Unknown PCI Device") + " ");
5389  if (subvendorid.isNull()) {
5390  deviceName.append(TQString(" [%1:%2]").arg(vendorid.lower()).arg(modelid.lower()));
5391  }
5392  else {
5393  deviceName.append(TQString(" [%1:%2] [%3:%4]").arg(vendorid.lower()).arg(modelid.lower()).arg(subvendorid.lower()).arg(submodelid.lower()));
5394  }
5395  }
5396 
5397  return deviceName;
5398  }
5399  else {
5400  return i18n("Unknown PCI Device");
5401  }
5402 }
5403 
5404 TQString TDEHardwareDevices::findUSBDeviceName(TQString vendorid, TQString modelid, TQString subvendorid, TQString submodelid) {
5405  TQString vendorName = TQString::null;
5406  TQString modelName = TQString::null;
5407  TQString friendlyName = TQString::null;
5408 
5409  if (!usb_id_map) {
5410  usb_id_map = new TDEDeviceIDMap;
5411 
5412  TQString database_filename = "/usr/share/usb.ids";
5413  if (!TQFile::exists(database_filename)) {
5414  database_filename = "/usr/share/misc/usb.ids";
5415  }
5416  if (!TQFile::exists(database_filename)) {
5417  printf("[tdehardwaredevices] Unable to locate USB information database usb.ids\n"); fflush(stdout);
5418  return i18n("Unknown USB Device");
5419  }
5420 
5421  TQFile database(database_filename);
5422  if (database.open(IO_ReadOnly)) {
5423  TQTextStream stream(&database);
5424  TQString line;
5425  TQString vendorID;
5426  TQString modelID;
5427  TQString subvendorID;
5428  TQString submodelID;
5429  TQString deviceMapKey;
5430  TQStringList devinfo;
5431  while (!stream.atEnd()) {
5432  line = stream.readLine();
5433  if ((!line.upper().startsWith("\t")) && (!line.upper().startsWith("#"))) {
5434  line.replace("\t", "");
5435  devinfo = TQStringList::split(' ', line, false);
5436  vendorID = *(devinfo.at(0));
5437  vendorName = line;
5438  vendorName.remove(0, vendorName.find(" "));
5439  vendorName = vendorName.stripWhiteSpace();
5440  modelName = TQString::null;
5441  deviceMapKey = vendorID.lower() + ":::";
5442  }
5443  else {
5444  if ((line.upper().startsWith("\t")) && (!line.upper().startsWith("\t\t"))) {
5445  line.replace("\t", "");
5446  devinfo = TQStringList::split(' ', line, false);
5447  modelID = *(devinfo.at(0));
5448  modelName = line;
5449  modelName.remove(0, modelName.find(" "));
5450  modelName = modelName.stripWhiteSpace();
5451  deviceMapKey = vendorID.lower() + ":" + modelID.lower() + "::";
5452  }
5453  else {
5454  if (line.upper().startsWith("\t\t")) {
5455  line.replace("\t", "");
5456  devinfo = TQStringList::split(' ', line, false);
5457  subvendorID = *(devinfo.at(0));
5458  submodelID = *(devinfo.at(1));
5459  modelName = line;
5460  modelName.remove(0, modelName.find(" "));
5461  modelName = modelName.stripWhiteSpace();
5462  modelName.remove(0, modelName.find(" "));
5463  modelName = modelName.stripWhiteSpace();
5464  deviceMapKey = vendorID.lower() + ":" + modelID.lower() + ":" + subvendorID.lower() + ":" + submodelID.lower();
5465  }
5466  }
5467  }
5468  if (modelName.isNull()) {
5469  usb_id_map->insert(deviceMapKey, "***UNKNOWN DEVICE*** " + vendorName, true);
5470  }
5471  else {
5472  usb_id_map->insert(deviceMapKey, vendorName + " " + modelName, true);
5473  }
5474  }
5475  database.close();
5476  }
5477  else {
5478  printf("[tdehardwaredevices] Unable to open USB information database %s\n", database_filename.ascii()); fflush(stdout);
5479  }
5480  }
5481 
5482  if (usb_id_map) {
5483  TQString deviceName;
5484  TQString deviceMapKey = vendorid.lower() + ":" + modelid.lower() + ":" + subvendorid.lower() + ":" + submodelid.lower();
5485 
5486  deviceName = (*usb_id_map)[deviceMapKey];
5487  if (deviceName.isNull() || deviceName.startsWith("***UNKNOWN DEVICE*** ")) {
5488  deviceMapKey = vendorid.lower() + ":" + modelid.lower() + ":" + subvendorid.lower() + ":";
5489  deviceName = (*usb_id_map)[deviceMapKey];
5490  if (deviceName.isNull() || deviceName.startsWith("***UNKNOWN DEVICE*** ")) {
5491  deviceMapKey = vendorid.lower() + ":" + modelid.lower() + "::";
5492  deviceName = (*usb_id_map)[deviceMapKey];
5493  }
5494  }
5495 
5496  if (deviceName.startsWith("***UNKNOWN DEVICE*** ")) {
5497  deviceName.replace("***UNKNOWN DEVICE*** ", "");
5498  deviceName.prepend(i18n("Unknown USB Device") + " ");
5499  if (subvendorid.isNull()) {
5500  deviceName.append(TQString(" [%1:%2]").arg(vendorid.lower()).arg(modelid.lower()));
5501  }
5502  else {
5503  deviceName.append(TQString(" [%1:%2] [%3:%4]").arg(vendorid.lower()).arg(modelid.lower()).arg(subvendorid.lower()).arg(submodelid.lower()));
5504  }
5505  }
5506 
5507  return deviceName;
5508  }
5509  else {
5510  return i18n("Unknown USB Device");
5511  }
5512 }
5513 
5514 TQString TDEHardwareDevices::findPNPDeviceName(TQString pnpid) {
5515  TQString friendlyName = TQString::null;
5516 
5517  if (!pnp_id_map) {
5518  pnp_id_map = new TDEDeviceIDMap;
5519 
5520  TQStringList hardware_info_directories(TDEGlobal::dirs()->resourceDirs("data"));
5521  TQString hardware_info_directory_suffix("tdehwlib/pnpdev/");
5522  TQString hardware_info_directory;
5523  TQString database_filename;
5524 
5525  for ( TQStringList::Iterator it = hardware_info_directories.begin(); it != hardware_info_directories.end(); ++it ) {
5526  hardware_info_directory = (*it);
5527  hardware_info_directory += hardware_info_directory_suffix;
5528 
5529  if (TDEGlobal::dirs()->exists(hardware_info_directory)) {
5530  database_filename = hardware_info_directory + "pnp.ids";
5531  if (TQFile::exists(database_filename)) {
5532  break;
5533  }
5534  }
5535  }
5536 
5537  if (!TQFile::exists(database_filename)) {
5538  printf("[tdehardwaredevices] Unable to locate PNP information database pnp.ids\n"); fflush(stdout);
5539  return i18n("Unknown PNP Device");
5540  }
5541 
5542  TQFile database(database_filename);
5543  if (database.open(IO_ReadOnly)) {
5544  TQTextStream stream(&database);
5545  TQString line;
5546  TQString pnpID;
5547  TQString vendorName;
5548  TQString deviceMapKey;
5549  TQStringList devinfo;
5550  while (!stream.atEnd()) {
5551  line = stream.readLine();
5552  if ((!line.upper().startsWith("\t")) && (!line.upper().startsWith("#"))) {
5553  devinfo = TQStringList::split('\t', line, false);
5554  if (devinfo.count() > 1) {
5555  pnpID = *(devinfo.at(0));
5556  vendorName = *(devinfo.at(1));;
5557  vendorName = vendorName.stripWhiteSpace();
5558  deviceMapKey = pnpID.upper().stripWhiteSpace();
5559  if (!deviceMapKey.isNull()) {
5560  pnp_id_map->insert(deviceMapKey, vendorName, true);
5561  }
5562  }
5563  }
5564  }
5565  database.close();
5566  }
5567  else {
5568  printf("[tdehardwaredevices] Unable to open PNP information database %s\n", database_filename.ascii()); fflush(stdout);
5569  }
5570  }
5571 
5572  if (pnp_id_map) {
5573  TQString deviceName;
5574 
5575  deviceName = (*pnp_id_map)[pnpid];
5576 
5577  return deviceName;
5578  }
5579  else {
5580  return i18n("Unknown PNP Device");
5581  }
5582 }
5583 
5584 TQString TDEHardwareDevices::findMonitorManufacturerName(TQString dpyid) {
5585  TQString friendlyName = TQString::null;
5586 
5587  if (!dpy_id_map) {
5588  dpy_id_map = new TDEDeviceIDMap;
5589 
5590  TQStringList hardware_info_directories(TDEGlobal::dirs()->resourceDirs("data"));
5591  TQString hardware_info_directory_suffix("tdehwlib/pnpdev/");
5592  TQString hardware_info_directory;
5593  TQString database_filename;
5594 
5595  for ( TQStringList::Iterator it = hardware_info_directories.begin(); it != hardware_info_directories.end(); ++it ) {
5596  hardware_info_directory = (*it);
5597  hardware_info_directory += hardware_info_directory_suffix;
5598 
5599  if (TDEGlobal::dirs()->exists(hardware_info_directory)) {
5600  database_filename = hardware_info_directory + "dpy.ids";
5601  if (TQFile::exists(database_filename)) {
5602  break;
5603  }
5604  }
5605  }
5606 
5607  if (!TQFile::exists(database_filename)) {
5608  printf("[tdehardwaredevices] Unable to locate monitor information database dpy.ids\n"); fflush(stdout);
5609  return i18n("Unknown Monitor Device");
5610  }
5611 
5612  TQFile database(database_filename);
5613  if (database.open(IO_ReadOnly)) {
5614  TQTextStream stream(&database);
5615  TQString line;
5616  TQString dpyID;
5617  TQString vendorName;
5618  TQString deviceMapKey;
5619  TQStringList devinfo;
5620  while (!stream.atEnd()) {
5621  line = stream.readLine();
5622  if ((!line.upper().startsWith("\t")) && (!line.upper().startsWith("#"))) {
5623  devinfo = TQStringList::split('\t', line, false);
5624  if (devinfo.count() > 1) {
5625  dpyID = *(devinfo.at(0));
5626  vendorName = *(devinfo.at(1));;
5627  vendorName = vendorName.stripWhiteSpace();
5628  deviceMapKey = dpyID.upper().stripWhiteSpace();
5629  if (!deviceMapKey.isNull()) {
5630  dpy_id_map->insert(deviceMapKey, vendorName, true);
5631  }
5632  }
5633  }
5634  }
5635  database.close();
5636  }
5637  else {
5638  printf("[tdehardwaredevices] Unable to open monitor information database %s\n", database_filename.ascii()); fflush(stdout);
5639  }
5640  }
5641 
5642  if (dpy_id_map) {
5643  TQString deviceName;
5644 
5645  deviceName = (*dpy_id_map)[dpyid];
5646 
5647  return deviceName;
5648  }
5649  else {
5650  return i18n("Unknown Monitor Device");
5651  }
5652 }
5653 
5654 TQPair<TQString,TQString> TDEHardwareDevices::getEDIDMonitorName(TQString path) {
5655  TQPair<TQString,TQString> edid;
5656  TQByteArray binaryedid = getEDID(path);
5657  if (binaryedid.isNull()) {
5658  return TQPair<TQString,TQString>(TQString::null, TQString::null);
5659  }
5660 
5661  // Get the manufacturer ID
5662  unsigned char letter_1 = ((binaryedid[8]>>2) & 0x1F) + 0x40;
5663  unsigned char letter_2 = (((binaryedid[8] & 0x03) << 3) | ((binaryedid[9]>>5) & 0x07)) + 0x40;
5664  unsigned char letter_3 = (binaryedid[9] & 0x1F) + 0x40;
5665  TQChar qletter_1 = TQChar(letter_1);
5666  TQChar qletter_2 = TQChar(letter_2);
5667  TQChar qletter_3 = TQChar(letter_3);
5668  TQString manufacturer_id = TQString("%1%2%3").arg(qletter_1).arg(qletter_2).arg(qletter_3);
5669 
5670  // Get the model ID
5671  unsigned int raw_model_id = (((binaryedid[10] << 8) | binaryedid[11]) << 16) & 0xFFFF0000;
5672  // Reverse the bit order
5673  unsigned int model_id = reverse_bits(raw_model_id);
5674 
5675  // Try to get the model name
5676  bool has_friendly_name = false;
5677  unsigned char descriptor_block[18];
5678  int i;
5679  for (i=72;i<90;i++) {
5680  descriptor_block[i-72] = binaryedid[i] & 0xFF;
5681  }
5682  if ((descriptor_block[0] != 0) || (descriptor_block[1] != 0) || (descriptor_block[3] != 0xFC)) {
5683  for (i=90;i<108;i++) {
5684  descriptor_block[i-90] = binaryedid[i] & 0xFF;
5685  }
5686  if ((descriptor_block[0] != 0) || (descriptor_block[1] != 0) || (descriptor_block[3] != 0xFC)) {
5687  for (i=108;i<126;i++) {
5688  descriptor_block[i-108] = binaryedid[i] & 0xFF;
5689  }
5690  }
5691  }
5692 
5693  TQString monitor_name;
5694  if ((descriptor_block[0] == 0) && (descriptor_block[1] == 0) && (descriptor_block[3] == 0xFC)) {
5695  char* pos = strchr((char *)(descriptor_block+5), '\n');
5696  if (pos) {
5697  *pos = 0;
5698  has_friendly_name = true;
5699  monitor_name = TQString((char *)(descriptor_block+5));
5700  }
5701  else {
5702  has_friendly_name = false;
5703  }
5704  }
5705 
5706  // Look up manufacturer name
5707  TQString manufacturer_name = findMonitorManufacturerName(manufacturer_id);
5708  if (manufacturer_name.isNull()) {
5709  manufacturer_name = manufacturer_id;
5710  }
5711 
5712  if (has_friendly_name) {
5713  edid.first = TQString("%1").arg(manufacturer_name);
5714  edid.second = TQString("%2").arg(monitor_name);
5715  }
5716  else {
5717  edid.first = TQString("%1").arg(manufacturer_name);
5718  edid.second = TQString("0x%2").arg(model_id, 0, 16);
5719  }
5720 
5721  return edid;
5722 }
5723 
5724 TQByteArray TDEHardwareDevices::getEDID(TQString path) {
5725  TQFile file(TQString("%1/edid").arg(path));
5726  if (!file.open (IO_ReadOnly)) {
5727  return TQByteArray();
5728  }
5729  TQByteArray binaryedid = file.readAll();
5730  file.close();
5731  return binaryedid;
5732 }
5733 
5734 TQString TDEHardwareDevices::getFriendlyDeviceTypeStringFromType(TDEGenericDeviceType::TDEGenericDeviceType query) {
5735  TQString ret = "Unknown Device";
5736 
5737  // Keep this in sync with the TDEGenericDeviceType definition in the header
5738  if (query == TDEGenericDeviceType::Root) {
5739  ret = i18n("Root");
5740  }
5741  else if (query == TDEGenericDeviceType::RootSystem) {
5742  ret = i18n("System Root");
5743  }
5744  else if (query == TDEGenericDeviceType::CPU) {
5745  ret = i18n("CPU");
5746  }
5747  else if (query == TDEGenericDeviceType::GPU) {
5748  ret = i18n("Graphics Processor");
5749  }
5750  else if (query == TDEGenericDeviceType::RAM) {
5751  ret = i18n("RAM");
5752  }
5753  else if (query == TDEGenericDeviceType::Bus) {
5754  ret = i18n("Bus");
5755  }
5756  else if (query == TDEGenericDeviceType::I2C) {
5757  ret = i18n("I2C Bus");
5758  }
5759  else if (query == TDEGenericDeviceType::MDIO) {
5760  ret = i18n("MDIO Bus");
5761  }
5762  else if (query == TDEGenericDeviceType::Mainboard) {
5763  ret = i18n("Mainboard");
5764  }
5765  else if (query == TDEGenericDeviceType::Disk) {
5766  ret = i18n("Disk");
5767  }
5768  else if (query == TDEGenericDeviceType::SCSI) {
5769  ret = i18n("SCSI");
5770  }
5771  else if (query == TDEGenericDeviceType::StorageController) {
5772  ret = i18n("Storage Controller");
5773  }
5774  else if (query == TDEGenericDeviceType::Mouse) {
5775  ret = i18n("Mouse");
5776  }
5777  else if (query == TDEGenericDeviceType::Keyboard) {
5778  ret = i18n("Keyboard");
5779  }
5780  else if (query == TDEGenericDeviceType::HID) {
5781  ret = i18n("HID");
5782  }
5783  else if (query == TDEGenericDeviceType::Modem) {
5784  ret = i18n("Modem");
5785  }
5786  else if (query == TDEGenericDeviceType::Monitor) {
5787  ret = i18n("Monitor and Display");
5788  }
5789  else if (query == TDEGenericDeviceType::Network) {
5790  ret = i18n("Network");
5791  }
5792  else if (query == TDEGenericDeviceType::Printer) {
5793  ret = i18n("Printer");
5794  }
5795  else if (query == TDEGenericDeviceType::Scanner) {
5796  ret = i18n("Scanner");
5797  }
5798  else if (query == TDEGenericDeviceType::Sound) {
5799  ret = i18n("Sound");
5800  }
5801  else if (query == TDEGenericDeviceType::VideoCapture) {
5802  ret = i18n("Video Capture");
5803  }
5804  else if (query == TDEGenericDeviceType::IEEE1394) {
5805  ret = i18n("IEEE1394");
5806  }
5807  else if (query == TDEGenericDeviceType::PCMCIA) {
5808  ret = i18n("PCMCIA");
5809  }
5810  else if (query == TDEGenericDeviceType::Camera) {
5811  ret = i18n("Camera");
5812  }
5813  else if (query == TDEGenericDeviceType::TextIO) {
5814  ret = i18n("Text I/O");
5815  }
5816  else if (query == TDEGenericDeviceType::Serial) {
5817  ret = i18n("Serial Communications Controller");
5818  }
5819  else if (query == TDEGenericDeviceType::Parallel) {
5820  ret = i18n("Parallel Port");
5821  }
5822  else if (query == TDEGenericDeviceType::Peripheral) {
5823  ret = i18n("Peripheral");
5824  }
5825  else if (query == TDEGenericDeviceType::Backlight) {
5826  ret = i18n("Backlight");
5827  }
5828  else if (query == TDEGenericDeviceType::Battery) {
5829  ret = i18n("Battery");
5830  }
5831  else if (query == TDEGenericDeviceType::PowerSupply) {
5832  ret = i18n("Power Supply");
5833  }
5834  else if (query == TDEGenericDeviceType::Dock) {
5835  ret = i18n("Docking Station");
5836  }
5837  else if (query == TDEGenericDeviceType::ThermalSensor) {
5838  ret = i18n("Thermal Sensor");
5839  }
5840  else if (query == TDEGenericDeviceType::ThermalControl) {
5841  ret = i18n("Thermal Control");
5842  }
5843  else if (query == TDEGenericDeviceType::BlueTooth) {
5844  ret = i18n("Bluetooth");
5845  }
5846  else if (query == TDEGenericDeviceType::Bridge) {
5847  ret = i18n("Bridge");
5848  }
5849  else if (query == TDEGenericDeviceType::Platform) {
5850  ret = i18n("Platform");
5851  }
5852  else if (query == TDEGenericDeviceType::Cryptography) {
5853  ret = i18n("Cryptography");
5854  }
5855  else if (query == TDEGenericDeviceType::Event) {
5856  ret = i18n("Platform Event");
5857  }
5858  else if (query == TDEGenericDeviceType::Input) {
5859  ret = i18n("Platform Input");
5860  }
5861  else if (query == TDEGenericDeviceType::PNP) {
5862  ret = i18n("Plug and Play");
5863  }
5864  else if (query == TDEGenericDeviceType::OtherACPI) {
5865  ret = i18n("Other ACPI");
5866  }
5867  else if (query == TDEGenericDeviceType::OtherUSB) {
5868  ret = i18n("Other USB");
5869  }
5870  else if (query == TDEGenericDeviceType::OtherMultimedia) {
5871  ret = i18n("Other Multimedia");
5872  }
5873  else if (query == TDEGenericDeviceType::OtherPeripheral) {
5874  ret = i18n("Other Peripheral");
5875  }
5876  else if (query == TDEGenericDeviceType::OtherSensor) {
5877  ret = i18n("Other Sensor");
5878  }
5879  else if (query == TDEGenericDeviceType::OtherVirtual) {
5880  ret = i18n("Other Virtual");
5881  }
5882  else {
5883  ret = i18n("Unknown Device");
5884  }
5885 
5886  return ret;
5887 }
5888 
5889 TQPixmap TDEHardwareDevices::getDeviceTypeIconFromType(TDEGenericDeviceType::TDEGenericDeviceType query, TDEIcon::StdSizes size) {
5890  TQPixmap ret = DesktopIcon("misc", size);
5891 
5892 // // Keep this in sync with the TDEGenericDeviceType definition in the header
5893  if (query == TDEGenericDeviceType::Root) {
5894  ret = DesktopIcon("kcmdevices", size);
5895  }
5896  else if (query == TDEGenericDeviceType::RootSystem) {
5897  ret = DesktopIcon("kcmdevices", size);
5898  }
5899  else if (query == TDEGenericDeviceType::CPU) {
5900  ret = DesktopIcon("kcmprocessor", size);
5901  }
5902  else if (query == TDEGenericDeviceType::GPU) {
5903  ret = DesktopIcon("kcmpci", size);
5904  }
5905  else if (query == TDEGenericDeviceType::RAM) {
5906  ret = DesktopIcon("memory", size);
5907  }
5908  else if (query == TDEGenericDeviceType::Bus) {
5909  ret = DesktopIcon("kcmpci", size);
5910  }
5911  else if (query == TDEGenericDeviceType::I2C) {
5912  ret = DesktopIcon("input_devices_settings", size);
5913  }
5914  else if (query == TDEGenericDeviceType::MDIO) {
5915  ret = DesktopIcon("input_devices_settings", size);
5916  }
5917  else if (query == TDEGenericDeviceType::Mainboard) {
5918  ret = DesktopIcon("kcmpci", size); // FIXME
5919  }
5920  else if (query == TDEGenericDeviceType::Disk) {
5921  ret = DesktopIcon("hdd_unmount", size);
5922  }
5923  else if (query == TDEGenericDeviceType::SCSI) {
5924  ret = DesktopIcon("kcmscsi", size);
5925  }
5926  else if (query == TDEGenericDeviceType::StorageController) {
5927  ret = DesktopIcon("kcmpci", size);
5928  }
5929  else if (query == TDEGenericDeviceType::Mouse) {
5930  ret = DesktopIcon("mouse", size);
5931  }
5932  else if (query == TDEGenericDeviceType::Keyboard) {
5933  ret = DesktopIcon("keyboard", size);
5934  }
5935  else if (query == TDEGenericDeviceType::HID) {
5936  ret = DesktopIcon("kcmdevices", size); // FIXME
5937  }
5938  else if (query == TDEGenericDeviceType::Modem) {
5939  ret = DesktopIcon("kcmpci", size);
5940  }
5941  else if (query == TDEGenericDeviceType::Monitor) {
5942  ret = DesktopIcon("background", size);
5943  }
5944  else if (query == TDEGenericDeviceType::Network) {
5945  ret = DesktopIcon("kcmpci", size);
5946  }
5947  else if (query == TDEGenericDeviceType::Printer) {
5948  ret = DesktopIcon("printer1", size);
5949  }
5950  else if (query == TDEGenericDeviceType::Scanner) {
5951  ret = DesktopIcon("scanner", size);
5952  }
5953  else if (query == TDEGenericDeviceType::Sound) {
5954  ret = DesktopIcon("kcmsound", size);
5955  }
5956  else if (query == TDEGenericDeviceType::VideoCapture) {
5957  ret = DesktopIcon("tv", size); // FIXME
5958  }
5959  else if (query == TDEGenericDeviceType::IEEE1394) {
5960  ret = DesktopIcon("ieee1394", size);
5961  }
5962  else if (query == TDEGenericDeviceType::PCMCIA) {
5963  ret = DesktopIcon("kcmdevices", size); // FIXME
5964  }
5965  else if (query == TDEGenericDeviceType::Camera) {
5966  ret = DesktopIcon("camera", size);
5967  }
5968  else if (query == TDEGenericDeviceType::Serial) {
5969  ret = DesktopIcon("input_devices_settings", size);
5970  }
5971  else if (query == TDEGenericDeviceType::Parallel) {
5972  ret = DesktopIcon("input_devices_settings", size);
5973  }
5974  else if (query == TDEGenericDeviceType::TextIO) {
5975  ret = DesktopIcon("chardevice", size);
5976  }
5977  else if (query == TDEGenericDeviceType::Peripheral) {
5978  ret = DesktopIcon("kcmpci", size);
5979  }
5980  else if (query == TDEGenericDeviceType::Backlight) {
5981  ret = DesktopIcon("tdescreensaver", size); // FIXME
5982  }
5983  else if (query == TDEGenericDeviceType::Battery) {
5984  ret = DesktopIcon("energy", size);
5985  }
5986  else if (query == TDEGenericDeviceType::PowerSupply) {
5987  ret = DesktopIcon("energy", size);
5988  }
5989  else if (query == TDEGenericDeviceType::Dock) {
5990  ret = DesktopIcon("kcmdevices", size); // FIXME
5991  }
5992  else if (query == TDEGenericDeviceType::ThermalSensor) {
5993  ret = DesktopIcon("kcmdevices", size); // FIXME
5994  }
5995  else if (query == TDEGenericDeviceType::ThermalControl) {
5996  ret = DesktopIcon("kcmdevices", size); // FIXME
5997  }
5998  else if (query == TDEGenericDeviceType::BlueTooth) {
5999  ret = DesktopIcon("kcmpci", size); // FIXME
6000  }
6001  else if (query == TDEGenericDeviceType::Bridge) {
6002  ret = DesktopIcon("kcmpci", size);
6003  }
6004  else if (query == TDEGenericDeviceType::Platform) {
6005  ret = DesktopIcon("kcmsystem", size);
6006  }
6007  else if (query == TDEGenericDeviceType::Cryptography) {
6008  ret = DesktopIcon("password", size);
6009  }
6010  else if (query == TDEGenericDeviceType::Event) {
6011  ret = DesktopIcon("kcmsystem", size);
6012  }
6013  else if (query == TDEGenericDeviceType::Input) {
6014  ret = DesktopIcon("kcmsystem", size);
6015  }
6016  else if (query == TDEGenericDeviceType::PNP) {
6017  ret = DesktopIcon("kcmsystem", size);
6018  }
6019  else if (query == TDEGenericDeviceType::OtherACPI) {
6020  ret = DesktopIcon("kcmdevices", size); // FIXME
6021  }
6022  else if (query == TDEGenericDeviceType::OtherUSB) {
6023  ret = DesktopIcon("usb", size);
6024  }
6025  else if (query == TDEGenericDeviceType::OtherMultimedia) {
6026  ret = DesktopIcon("kcmsound", size);
6027  }
6028  else if (query == TDEGenericDeviceType::OtherPeripheral) {
6029  ret = DesktopIcon("kcmpci", size);
6030  }
6031  else if (query == TDEGenericDeviceType::OtherSensor) {
6032  ret = DesktopIcon("kcmdevices", size); // FIXME
6033  }
6034  else if (query == TDEGenericDeviceType::OtherVirtual) {
6035  ret = DesktopIcon("kcmsystem", size);
6036  }
6037  else {
6038  ret = DesktopIcon("hwinfo", size);
6039  }
6040 
6041  return ret;
6042 }
6043 
6044 TDERootSystemDevice* TDEHardwareDevices::rootSystemDevice() {
6045  TDEGenericDevice *hwdevice;
6046  for ( hwdevice = m_deviceList.first(); hwdevice; hwdevice = m_deviceList.next() ) {
6047  if (hwdevice->type() == TDEGenericDeviceType::RootSystem) {
6048  return dynamic_cast<TDERootSystemDevice*>(hwdevice);
6049  }
6050  }
6051 
6052  return 0;
6053 }
6054 
6055 TQString TDEHardwareDevices::bytesToFriendlySizeString(double bytes) {
6056  TQString prettystring;
6057 
6058  prettystring = TQString("%1B").arg(bytes);
6059 
6060  if (bytes > 1024) {
6061  bytes = bytes / 1024;
6062  prettystring = TQString("%1KB").arg(bytes, 0, 'f', 1);
6063  }
6064 
6065  if (bytes > 1024) {
6066  bytes = bytes / 1024;
6067  prettystring = TQString("%1MB").arg(bytes, 0, 'f', 1);
6068  }
6069 
6070  if (bytes > 1024) {
6071  bytes = bytes / 1024;
6072  prettystring = TQString("%1GB").arg(bytes, 0, 'f', 1);
6073  }
6074 
6075  if (bytes > 1024) {
6076  bytes = bytes / 1024;
6077  prettystring = TQString("%1TB").arg(bytes, 0, 'f', 1);
6078  }
6079 
6080  if (bytes > 1024) {
6081  bytes = bytes / 1024;
6082  prettystring = TQString("%1PB").arg(bytes, 0, 'f', 1);
6083  }
6084 
6085  if (bytes > 1024) {
6086  bytes = bytes / 1024;
6087  prettystring = TQString("%1EB").arg(bytes, 0, 'f', 1);
6088  }
6089 
6090  if (bytes > 1024) {
6091  bytes = bytes / 1024;
6092  prettystring = TQString("%1ZB").arg(bytes, 0, 'f', 1);
6093  }
6094 
6095  if (bytes > 1024) {
6096  bytes = bytes / 1024;
6097  prettystring = TQString("%1YB").arg(bytes, 0, 'f', 1);
6098  }
6099 
6100  return prettystring;
6101 }
6102 
6103 TDEGenericHardwareList TDEHardwareDevices::listByDeviceClass(TDEGenericDeviceType::TDEGenericDeviceType cl) {
6104  TDEGenericHardwareList ret;
6105  ret.setAutoDelete(false);
6106 
6107  TDEGenericDevice *hwdevice;
6108  for ( hwdevice = m_deviceList.first(); hwdevice; hwdevice = m_deviceList.next() ) {
6109  if (hwdevice->type() == cl) {
6110  ret.append(hwdevice);
6111  }
6112  }
6113 
6114  return ret;
6115 }
6116 
6117 TDEGenericHardwareList TDEHardwareDevices::listAllPhysicalDevices() {
6118  TDEGenericHardwareList ret = m_deviceList;
6119  ret.setAutoDelete(false);
6120 
6121  return ret;
6122 }
6123 
6124 #include "tdehardwaredevices.moc"

tdecore

Skip menu "tdecore"
  • Main Page
  • Modules
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

tdecore

Skip menu "tdecore"
  • arts
  • dcop
  • dnssd
  • interfaces
  •   kspeech
  •     interface
  •     library
  •   tdetexteditor
  • kate
  • kded
  • kdoctools
  • kimgio
  • kjs
  • knewstuff
  • libtdemid
  • libtdescreensaver
  • tdeabc
  • tdecmshell
  • tdecore
  • tdefx
  • tdehtml
  • tdeinit
  • tdeio
  •   bookmarks
  •   httpfilter
  •   kpasswdserver
  •   kssl
  •   tdefile
  •   tdeio
  •   tdeioexec
  • tdeioslave
  •   http
  • tdemdi
  •   tdemdi
  • tdeparts
  • tdeprint
  • tderandr
  • tderesources
  • tdespell2
  • tdesu
  • tdeui
  • tdeunittest
  • tdeutils
  • tdewallet
Generated for tdecore by doxygen 1.8.3.1
This website is maintained by Timothy Pearson.