-
-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathportscanner.cpp
More file actions
203 lines (197 loc) · 6.52 KB
/
Copy pathportscanner.cpp
File metadata and controls
203 lines (197 loc) · 6.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
#include "portscanner.h"
#include <hidapi.h>
#include <QBitmap>
#include <QCoreApplication>
#include <QDebug>
#include <QDir>
#include <QDirIterator>
#include <QFile>
#include <QIcon>
#include <QProcess>
#include <QThread>
#include <algorithm>
#include "devices/dfu_arduino.h"
#include "devices/null_device.h"
#define PID_8U2 0x2FF7
#define VID_8U2 0x03eb
#define PID_16U2 0x2FEF
#define VID_16U2 0x03eb
PortScanner::PortScanner(Programmer* programmer, QObject* parent) : QObject(parent), m_hasSelected(false), m_selected(nullptr), programmer(programmer) {
hid_init();
m_emptyDevice = new NullDevice();
m_model.push_back(m_emptyDevice);
if (settings.contains("configMode")) {
m_graphical = settings.value("configMode").toBool();
} else {
m_graphical = true;
}
setSelected(nullptr);
}
void PortScanner::add(Device* device) {
if (std::find_if(m_model.begin(), m_model.end(), [device](QObject* f) { return *static_cast<Device*>(f) == *device; }) != m_model.end()) {
return;
}
if (device->open()) {
if (!m_hasSelected && m_selected) {
m_hasSelected = true;
m_selected = device;
selectedChanged();
hasSelectedChanged();
}
connect(device, &Device::descriptionChanged, this, &PortScanner::update);
m_model.append(device);
update();
}
}
void PortScanner::remove(Device* device) {
auto found = std::find_if(m_model.begin(), m_model.end(), [device](QObject* f) { return *static_cast<Device*>(f) == *device; });
if (found == m_model.end()) {
return;
}
auto foundEle = ((Device*)*found);
if (m_selected == foundEle) {
m_hasSelected = false;
selectedChanged();
hasSelectedChanged();
}
foundEle->close();
m_model.removeAll(foundEle);
update();
}
void PortScanner::add(UsbDevice_t device) {
if (ArdwiinoLookup::isArdwiino(device)) {
struct hid_device_info *devs, *cur_dev;
devs = hid_enumerate(device.vid, device.pid);
cur_dev = devs;
while (cur_dev) {
if (QString::fromWCharArray(cur_dev->serial_number) == device.serial || QString::fromUtf8(cur_dev->path).toLower() == device.hidPath.toLower()) {
add(new Ardwiino(cur_dev, device));
}
cur_dev = cur_dev->next;
}
hid_free_enumeration(devs);
} else if (device.vid == VID_8U2 && device.pid == PID_8U2) {
add(new DfuArduino("at90usb82", device));
} else if (device.vid == VID_16U2 && device.pid == PID_16U2) {
add(new DfuArduino("atmega16u2", device));
}
}
void PortScanner::remove(UsbDevice_t device) {
if (ArdwiinoLookup::isArdwiino(device)) {
remove(new Ardwiino(device));
} else if (device.vid == VID_8U2 && device.pid == PID_8U2) {
remove(new DfuArduino("at90usb82", device));
} else if (device.vid == VID_16U2 && device.pid == PID_16U2) {
remove(new DfuArduino("atmega16u2", device));
}
}
void PortScanner::serialDeviceDetected(const QSerialPortInfo& serialPortInfo) {
if (ArdwiinoLookup::isArdwiino(serialPortInfo)) {
add(new OutdatedArdwiino(serialPortInfo));
} else {
auto board = ArdwiinoLookup::detectBoard(serialPortInfo);
if (board.name != "") {
add(new Arduino(serialPortInfo));
}
}
}
void PortScanner::serialDeviceUnplugged(const QSerialPortInfo& serialPortInfo) {
if (ArdwiinoLookup::isArdwiino(serialPortInfo)) {
remove(new OutdatedArdwiino(serialPortInfo));
} else {
auto board = ArdwiinoLookup::detectBoard(serialPortInfo);
if (board.name != "") {
remove(new Arduino(serialPortInfo));
}
}
}
void PortScanner::update() {
if (m_model.length() == 0) {
m_model.push_back(m_emptyDevice);
} else if (m_model.first() == m_emptyDevice && m_model.length() > 1) {
m_model.removeAll(m_emptyDevice);
}
//For whatever reason, just updating the model is not enough to make the QML combobox update correctly. However, removing all items then adding them again does work.
auto m = m_model;
m_model.clear();
m_model.push_back(m_emptyDevice);
emit modelChanged();
m_model.clear();
m_model << m;
emit modelChanged();
}
void PortScanner::setSelected(Device* port) {
m_selected = port;
if (port != nullptr) {
m_hasSelected = true;
selectedChanged();
hasSelectedChanged();
return;
}
selectedChanged();
}
// TODO: the stuff below really should be moved to a different file, it does not belong here.
void PortScanner::fixLinux() {
QFile f("/sys/bus/usb/drivers/xpad/new_id");
f.open(QFile::ReadOnly);
QString s = QString(f.readAll());
f.close();
if (!s.contains("1209 2882")) {
QProcess p;
p.start("pkexec", {"tee", "-a", "/sys/bus/usb/drivers/xpad/new_id"});
p.waitForStarted();
p.write("1209 2882 ff");
p.closeWriteChannel();
p.waitForFinished();
}
}
void PortScanner::clearImages() {
images.clear();
}
void PortScanner::toggleGraphics() {
m_graphical = !m_graphical;
graphicalChanged();
settings.setValue("configMode", m_graphical);
}
QVector<uchar> data;
int width;
int height;
QString PortScanner::findElement(QString base, int w, int h, int mouseX, int mouseY) {
if (images.isEmpty()) {
width = w;
height = h;
data.clear();
data.resize(width * height);
auto imageList = QDir(":/" + base + "/components").entryList();
imageList.sort();
QVector<QRgb> colorMap;
colorMap.push_back(qRgba(0, 0, 0, 0));
colorMap.push_back(qRgba(255, 255, 255, 255));
for (auto image : imageList) {
auto i = QIcon(":/" + base + "/components/" + image).pixmap(QSize(width, height)).toImage();
i = i.convertToFormat(QImage::Format_Indexed8, colorMap, Qt::AutoColor);
auto s2 = i.height() * i.width();
auto s3 = data.data();
auto s = i.bits();
while (s2--) {
if (*s != 0) {
*s3 = *s;
}
s3++;
s++;
}
images.push_back(image);
colorMap.push_front(qRgba(0, 0, 0, 0));
}
}
double x = (double)mouseX / w;
double y = (double)mouseY / h;
if (x >= 1 || y >= 1 || x < 0 || y < 0) return "";
x *= width;
y *= height;
auto c = data[(int)y * width + (int)x];
if (c != 0) {
return "/" + base + "/components/" + images[c - 1];
}
return "";
}