Debian Codegraph @

src/systeminfo/linux/qudevwrapper.cpp

raw symbols SCIP
1 /****************************************************************************
2 **
3 ** Copyright (C) 2018 The Qt Company Ltd and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the QtSystems module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 3 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL3 included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 3 requirements
23 ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24 **
25 ** GNU General Public License Usage
26 ** Alternatively, this file may be used under the terms of the GNU
27 ** General Public License version 2.0 or (at your option) the GNU General
28 ** Public license version 3 or any later version approved by the KDE Free
29 ** Qt Foundation. The licenses are as published by the Free Software
30 ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31 ** included in the packaging of this file. Please review the following
32 ** information to ensure the GNU General Public License requirements will
33 ** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34 ** https://www.gnu.org/licenses/gpl-3.0.html.
35 **
36 ** $QT_END_LICENSE$
37 **
38 ****************************************************************************/
39
40 #include "qudevwrapper_p.h"
41
42 #include <QMetaMethod>
43 #include <QSocketNotifier>
44
45 #include <poll.h>
46 #include <sys/select.h>
47 #if !defined(QT_NO_UDEV)
48
49 QT_BEGIN_NAMESPACE
50
51 QUDevWrapper::QUDevWrapper(QObject *parent)
52 : QObject(parent)
53 , udev(0)
54 , udevMonitor(0)
55 , udevFd(-1)
56 , notifier(0)
57 , watcherEnabled(false)
58 , watchPowerSupply(false)
59 , watchDrives(false)
60 {
61 }
62
63 bool QUDevWrapper::addUDevWatcher(const QByteArray &subsystem)
64 {
65 if (!udev)
66 udev = udev_new();
67
68 if (udev && !udevMonitor)
69 udevMonitor = udev_monitor_new_from_netlink(udev, "udev");
70
71 if (udevMonitor) {
72 if (udev_monitor_filter_add_match_subsystem_devtype(udevMonitor, subsystem, NULL) >= 0) {
73 if (!watcherEnabled) {
74 if (udev_monitor_enable_receiving(udevMonitor) >= 0) {
75 udevFd = udev_monitor_get_fd(udevMonitor);
76 if (udevFd >= 0) {
77 notifier = new QSocketNotifier(udevFd, QSocketNotifier::Read, this);
78 if (connect(notifier, SIGNAL(activated(int)), this, SLOT(onUDevChanges()))) {
79 watcherEnabled = true;
80 return true;
81 }
82 }
83 }
84 } else {
85 if (udev_monitor_filter_update(udevMonitor) >= 0)
86 return true;
87 }
88 }
89 }
90 return false;
91 }
92
93 bool QUDevWrapper::removeAllUDevWatcher()
94 {
95 if (!udev || !udevMonitor)
96 return true;
97
98 if (udev_monitor_filter_remove(udevMonitor) >= 0) {
99 if (udev_monitor_filter_update(udevMonitor) >= 0)
100 return true;
101 }
102 return false;
103 }
104
105 void QUDevWrapper::connectNotify(const QMetaMethod &signal)
106 {
107 static const QMetaMethod driveChangedSignal = QMetaMethod::fromSignal(&QUDevWrapper::driveChanged);
108 static const QMetaMethod batteryDataChangedSignal = QMetaMethod::fromSignal(&QUDevWrapper::batteryDataChanged);
109 static const QMetaMethod chargerTypeChangedSignal = QMetaMethod::fromSignal(&QUDevWrapper::chargerTypeChanged);
110
111 if (!watchDrives && signal == driveChangedSignal) {
112 if (addUDevWatcher("block"))
113 watchDrives = true;
114 } else if (!watchPowerSupply && (signal == batteryDataChangedSignal
115 || signal == chargerTypeChangedSignal)) {
116 if (addUDevWatcher("power_supply"))
117 watchPowerSupply = true;
118 }
119 }
120 void QUDevWrapper::disconnectNotify(const QMetaMethod &signal)
121 {
122 static const QMetaMethod driveChangedSignal = QMetaMethod::fromSignal(&QUDevWrapper::driveChanged);
123 static const QMetaMethod batteryDataChangedSignal = QMetaMethod::fromSignal(&QUDevWrapper::batteryDataChanged);
124 static const QMetaMethod chargerTypeChangedSignal = QMetaMethod::fromSignal(&QUDevWrapper::chargerTypeChanged);
125
126 if (watchDrives && signal == driveChangedSignal) {
127 if (removeAllUDevWatcher()) {
128 watchDrives = false;
129 if (watchPowerSupply) {
130 if (!addUDevWatcher("power_supply"))
131 watchPowerSupply = false;
132 }
133 }
134 } else if (watchPowerSupply && (signal == batteryDataChangedSignal
135 || signal == chargerTypeChangedSignal)) {
136 if (removeAllUDevWatcher()) {
137 watchPowerSupply = false;
138 if (watchDrives) {
139 if (!addUDevWatcher("block"))
140 watchDrives = false;
141 }
142 }
143 }
144
145 if (!watchDrives && !watchPowerSupply) {
146 disconnect(notifier, SIGNAL(activated(int)), this, SLOT(onUDevChanges()));
147 udev_monitor_unref(udevMonitor);
148 udevMonitor = 0;
149 udevFd = -1;
150 watcherEnabled = false;
151 udev_unref(udev);
152 }
153 }
154
155 void QUDevWrapper::onUDevChanges()
156 {
157 int ret;
158 struct udev_device *device = 0;
159 QByteArray subsystem;
160 QByteArray action;
161 QByteArray sysname;
162 struct pollfd pollfds[1];
163
164 pollfds[0].fd = udevFd;
165 pollfds[0].events = POLLIN;
166 pollfds[0].revents = 0;
167 ret = poll(pollfds, 1, -1);
168
169 if ((pollfds[0].revents & POLLIN) && ret == 1) {
170 device = udev_monitor_receive_device(udevMonitor);
171 if (device) {
172 subsystem = udev_device_get_subsystem(device);
173 action = udev_device_get_action(device);
174 sysname = udev_device_get_sysname(device);
175
176 #if defined(QT_SIMULATOR)
177 if (qstrcmp(subsystem, "block") == 0 && qstrcmp(action, "change") == 0)
178 emit driveChanged();
179 #endif
180 if (qstrcmp(subsystem, "block") == 0
181 && ((qstrcmp(action, "add") == 0) || qstrcmp(action, "remove") == 0)) {
182 emit driveChanged();
183 } else if (qstrcmp(subsystem, "power_supply") == 0) {
184 int i = -1;
185 if (sysname.contains("AC")) {
186 bool enabled = false;
187 if (qstrcmp(udev_device_get_sysattr_value(device, "online"), "1") == 0)
188 enabled = true;
189 emit chargerTypeChanged("AC", enabled);
190 } else if (sysname.contains("USB")) {
191 bool enabled = false;
192 QByteArray charger(udev_device_get_sysattr_value(device, "type"));
193 if (qstrcmp(udev_device_get_sysattr_value(device, "present"), "1") == 0)
194 enabled = true;
195 emit chargerTypeChanged(charger, enabled);
196 } else if (sysname.contains("BAT")) {
197 bool ok;
198 i = sysname.right(1).toInt(&ok);
199 if (!ok)
200 i = -1;
201 }
202
203 if (i > -1) {
204 QByteArray status(udev_device_get_sysattr_value(device, "status"));
205 if (!status.isEmpty())
206 emit batteryDataChanged(i, "status", status);
207
208 QByteArray remainingCapacity(udev_device_get_sysattr_value(device, "charge_now"));
209 if (!remainingCapacity.isEmpty())
210 emit batteryDataChanged(i, "charge_now", remainingCapacity);
211
212 QByteArray remainingChargingTime(udev_device_get_sysattr_value(device, "time_to_full_avg"));
213 if (!remainingChargingTime.isEmpty())
214 emit batteryDataChanged(i, "time_to_full_avg", remainingChargingTime);
215
216 QByteArray voltage(udev_device_get_sysattr_value(device, "voltage_now"));
217 if (!voltage.isEmpty())
218 emit batteryDataChanged(i, "voltage_now", voltage);
219
220 QByteArray currentFlow(udev_device_get_sysattr_value(device, "current_now"));
221 if (!currentFlow.isEmpty())
222 emit batteryDataChanged(i, "current_now", currentFlow);
223
224 QByteArray batteryStatus(udev_device_get_sysattr_value(device, "capacity_level"));
225 if (!batteryStatus.isEmpty())
226 emit batteryDataChanged(i, "capacity_level", batteryStatus);
227 }
228 }
229 udev_device_unref(device);
230 }
231 }
232 }
233
234 QT_END_NAMESPACE
235
236 #endif // QT_NO_UDEV