-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgpudeviceinterface.cpp
218 lines (193 loc) · 6.03 KB
/
gpudeviceinterface.cpp
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/****************************************************************************}
{ gpudeviceinterface.cpp - interface to a single GPU device from a plugin }
{ }
{ Copyright (c) 2018 Alexey Parfenov <zxed@alkatrazstudio.net> }
{ }
{ This file is part of GPU Fan Meister. }
{ }
{ GPU Fan Meister is free software: you can redistribute it and/or modify it }
{ under the terms of the GNU General Public License as published by }
{ the Free Software Foundation, either version 3 of the License, }
{ or (at your option) any later version. }
{ }
{ GPU Fan Meister is distributed in the hope that it will be useful, }
{ but WITHOUT ANY WARRANTY; without even the implied warranty of }
{ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU }
{ General Public License for more details: https://gnu.org/licenses/gpl.html }
{****************************************************************************/
#include "gpudeviceinterface.h"
#include <QJSEngine>
GpuDeviceInterface::GpuDeviceInterface(GpuDevicePlugin::CPtr plugin, size_t deviceIndex) :
plugin_(plugin),
deviceIndex(deviceIndex)
{
clearLastError();
plugin_->raw()->initDevice(deviceIndex);
throwLastError();
}
GpuDeviceInterface::~GpuDeviceInterface()
{
clearLastError();
plugin_->raw()->deinitDevice(deviceIndex);
//throwLastError();
}
QString GpuDeviceInterface::id() const
{
clearLastError();
const char* result = plugin_->raw()->getId(deviceIndex);
throwLastError();
return result;
}
QString GpuDeviceInterface::name() const
{
clearLastError();
const char* result = plugin_->raw()->getName(deviceIndex);
throwLastError();
return result;
}
unsigned int GpuDeviceInterface::temperature() const
{
clearLastError();
unsigned int temp = plugin_->raw()->getTemperature(deviceIndex);
throwLastError();
return temp;
}
unsigned int GpuDeviceInterface::fanSpeed()
{
clearLastError();
unsigned int speed = plugin_->raw()->getFanSpeed(deviceIndex);
throwLastError();
return speed;
}
void GpuDeviceInterface::setFanSpeed(unsigned int value)
{
clearLastError();
plugin_->raw()->setFanSpeed(deviceIndex, value);
throwLastError();
}
bool GpuDeviceInterface::isFanManualMode() const
{
clearLastError();
bool result = plugin_->raw()->isFanManualMode(deviceIndex);
throwLastError();
return result;
}
void GpuDeviceInterface::setFanManualMode(bool value)
{
clearLastError();
plugin_->raw()->setFanManualMode(deviceIndex, value);
throwLastError();
}
unsigned int GpuDeviceInterface::gpuRate() const
{
if(!plugin_->canGetGpuRate())
return 0;
clearLastError();
unsigned int rate = plugin_->raw()->getGpuRate(deviceIndex);
throwLastError();
return rate;
}
unsigned int GpuDeviceInterface::memoryRate() const
{
if(!plugin_->canGetMemoryRate())
return 0;
clearLastError();
unsigned int rate = plugin_->raw()->getMemoryRate(deviceIndex);
throwLastError();
return rate;
}
unsigned long long GpuDeviceInterface::totalMemory() const
{
if(!plugin_->canGetTotalMemory())
return 0;
clearLastError();
unsigned long long mem = plugin_->raw()->getTotalMemory(deviceIndex);
throwLastError();
return mem;
}
unsigned long long GpuDeviceInterface::freeMemory() const
{
if(!plugin_->canGetFreeMemory())
return 0;
clearLastError();
unsigned long long mem = plugin_->raw()->getFreeMemory(deviceIndex);
throwLastError();
return mem;
}
QJSValue GpuDeviceInterface::customFunction(const QString &name, const QJSValue &val, QJSEngineEx *engine) const
{
clearLastError();
auto f = plugin_->customFunction(name);
if(!f)
{
engine->throwError(QStringLiteral("Custom function not found: ") + name);
return QJSValue();
}
bool ok;
QJSValue tmpVal = engine->jsonStringify(val, ok);
if(!ok)
return tmpVal;
QByteArray json = tmpVal.toString().toUtf8();
const char* result = f(deviceIndex, json.constData());
throwLastError();
if(!result)
return QJSValue();
return engine->jsonParse(result);
}
void GpuDeviceInterface::clearLastError() const
{
plugin_->raw()->clearLastError();
}
void GpuDeviceInterface::throwLastError() const
{
const GpuFanMeister_Error* err = plugin_->raw()->getLastError();
if(err->code)
throw Error(err, this);
}
GpuDeviceInterface::Error::Error(const GpuFanMeister_Error* err, const GpuDeviceInterface *parent) :
std::runtime_error(""),
text_(err->text),
code_(err->code),
devId(parent ? parent->id() : ""),
devName(parent ? parent->name() : "")
{
}
GpuDeviceInterface::Error::Error(const GpuFanMeister_Error &err, const GpuDeviceInterface *parent) :
std::runtime_error(""),
text_(err.text),
code_(err.code),
devId(parent ? parent->id() : ""),
devName(parent ? parent->name() : "")
{
}
const char *GpuDeviceInterface::Error::what() const noexcept
{
static QByteArray errChars;
if(errChars.isEmpty())
{
QString errText;
if(!devId.isEmpty() || !devName.isEmpty())
{
if(!devId.isEmpty())
{
errText.append(devId);
errText.append(" - ");
}
if(!devName.isEmpty())
{
errText.append(devName);
errText.append(" - ");
}
}
else
{
errText.append("No device - ");
}
errText.append("error: ");
errText.append(QString::number(code_));
errText.append(" - ");
errText.append(text_);
errChars = errText.toUtf8();
}
return errChars.constData();
}