File: peperoni.cpp

package info (click to toggle)
qlcplus 4.14.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 58,644 kB
  • sloc: cpp: 182,867; javascript: 7,764; xml: 2,453; ansic: 2,120; sh: 1,716; python: 634; ruby: 606; makefile: 23
file content (326 lines) | stat: -rw-r--r-- 8,666 bytes parent folder | download
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
/*
  Q Light Controller
  peperoni.cpp

  Copyright (c)	Heikki Junnila

  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0.txt

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
*/

#include <QMessageBox>
#include <QString>
#include <QDebug>
#include <libusb.h>

#include "peperonidevice.h"
#include "peperoni.h"

/*****************************************************************************
 * Initialization
 *****************************************************************************/

Peperoni::~Peperoni()
{
}

void Peperoni::init()
{
    m_ctx = NULL;

    if (libusb_init(&m_ctx) != 0)
        qWarning() << "Unable to initialize libusb context!";

    rescanDevices();
}

QString Peperoni::name()
{
    return QString("Peperoni");
}

int Peperoni::capabilities() const
{
    return QLCIOPlugin::Output | QLCIOPlugin::Input;
}

/*****************************************************************************
 * Outputs
 *****************************************************************************/

bool Peperoni::openOutput(quint32 output, quint32 universe)
{
    Q_UNUSED(universe)
    if (m_devices.contains(output) == false)
        return false;

    if (m_devices[output] != NULL)
        return m_devices[output]->open(output, PeperoniDevice::OutputMode);
    return false;
}

void Peperoni::closeOutput(quint32 output, quint32 universe)
{
    Q_UNUSED(universe)
    if (m_devices.contains(output) && m_devices[output] != NULL)
            m_devices[output]->close(output, PeperoniDevice::OutputMode);
}

QStringList Peperoni::outputs()
{
    QStringList list;
    int i = 0;

    foreach (PeperoniDevice* dev, m_devices)
        list << dev->name(i++);

    return list;
}

QString Peperoni::pluginInfo()
{
    QString str;

    str += QString("<HTML>");
    str += QString("<HEAD>");
    str += QString("<TITLE>%1</TITLE>").arg(name());
    str += QString("</HEAD>");
    str += QString("<BODY>");

    str += QString("<H3>%1</H3>").arg(name());
    str += QString("<P>");
    str += tr("This plugin provides DMX input and output support for Peperoni DMX devices.");
    str += QString("</P>");

    return str;
}

QString Peperoni::outputInfo(quint32 output)
{
    QString str;

    if (m_devices.contains(output) == false)
        return str;

    if (m_devices[output] != NULL)
    {
        str += m_devices[output]->outputInfoText(output);
    }
    else
        qDebug() << "Peperoni invalid output!" << output << m_devices.size();

    str += QString("</BODY>");
    str += QString("</HTML>");

    return str;
}

void Peperoni::writeUniverse(quint32 universe, quint32 output, const QByteArray &data, bool dataChanged)
{
    Q_UNUSED(universe)
    Q_UNUSED(dataChanged)

    if (m_devices.contains(output) == false)
        return;

    if (m_devices[output] != NULL)
        m_devices[output]->outputDMX(output, data);
    else
        qDebug() << "Peperoni invalid output!" << output << m_devices.size();
}

/*************************************************************************
 * Inputs
 *************************************************************************/

bool Peperoni::openInput(quint32 input, quint32 universe)
{
    Q_UNUSED(universe)
    if (m_devices.contains(input) == false)
        return false;

    if (m_devices[input] != NULL)
    {
        connect(m_devices[input], SIGNAL(valueChanged(quint32, quint32,quint32,uchar)),
                this, SIGNAL(valueChanged(quint32, quint32,quint32,uchar)));
        return m_devices[input]->open(input, PeperoniDevice::InputMode);
    }
    return false;
}

void Peperoni::closeInput(quint32 input, quint32 universe)
{
    Q_UNUSED(universe)
    if (m_devices.contains(input) && m_devices[input] != NULL)
    {
        m_devices[input]->close(input, PeperoniDevice::InputMode);
        disconnect(m_devices[input], SIGNAL(valueChanged(quint32,quint32,quint32,uchar)),
                   this, SIGNAL(valueChanged(quint32,quint32,quint32,uchar)));
    }
}

QStringList Peperoni::inputs()
{
    QStringList list;
    int i = 0;

    foreach (PeperoniDevice* dev, m_devices)
        list << dev->name(i++);

    return list;
}

QString Peperoni::inputInfo(quint32 input)
{
    QString str;

    if (m_devices.contains(input) == false)
        return str;

    if (m_devices[input] != NULL)
    {
        str += m_devices[input]->baseInfoText(input);
        str += m_devices[input]->inputInfoText(input);
    }
    else
        qDebug() << "Peperoni invalid input!" << input << m_devices.size();

    str += QString("</BODY>");
    str += QString("</HTML>");

    return str;
}

/*****************************************************************************
 * Configuration
 *****************************************************************************/

void Peperoni::configure()
{
    int r = QMessageBox::question(NULL, name(),
                                  tr("Do you wish to re-scan your hardware?"),
                                  QMessageBox::Yes, QMessageBox::No);
    if (r == QMessageBox::Yes)
        rescanDevices();
}

bool Peperoni::canConfigure()
{
    return true;
}

/*****************************************************************************
 * Devices
 *****************************************************************************/

void Peperoni::rescanDevices()
{
    /* Treat all devices as dead first, until we find them again. Those
       that aren't found, get destroyed at the end of this function. */
    QHash <quint32, PeperoniDevice*> destroyList(m_devices);
    quint32 line = 0;
    int devCount = m_devices.count();

    libusb_device** devices = NULL;
    ssize_t count = libusb_get_device_list(m_ctx, &devices);
    for (ssize_t i = 0; i < count; i++)
    {
        libusb_device* dev = devices[i];
        Q_ASSERT(dev != NULL);

        libusb_device_descriptor desc;
        int r = libusb_get_device_descriptor(dev, &desc);
        if (r < 0)
        {
            qWarning() << "Unable to get device descriptor:" << r;
            continue;
        }

        if (device(dev) == true)
        {
            /* We already have this device and it's still
               there. Remove from the destroy list and
               continue iterating. */
            destroyList.remove(line);
            line++;
            continue;
        }
        else if (PeperoniDevice::isPeperoniDevice(&desc) == true)
        {
            /* This is a new device. Create and append. */
            PeperoniDevice* pepdev = new PeperoniDevice(this, dev, &desc, line);
            m_devices[line] = pepdev;
            if (PeperoniDevice::outputsNumber(&desc) == 2)
            {
                line++;
                /* reserve another line with the same device reference */
                m_devices[line] = pepdev;
            }
            line++;
        }
    }

    //qDebug() << "[Peperoni] Need to destroy" << destroyList.count() << "devices";
    QHashIterator<quint32, PeperoniDevice*> it(destroyList);
    while (it.hasNext())
    {
        it.next();
        PeperoniDevice *dev = m_devices.take(it.key());
        dev->closeAll();
        delete dev;
    }

    //qDebug() << "[Peperoni] devices found:" << m_devices.count();
    if (m_devices.count() != devCount)
        emit configurationChanged();
}

bool Peperoni::device(struct libusb_device* usbdev)
{
    foreach (PeperoniDevice* dev, m_devices)
    {
        if (dev->device() == usbdev)
            return true;
    }
    return false;
}


/*****************************************************************************
 * Hotplug
 *****************************************************************************/

void Peperoni::slotDeviceAdded(uint vid, uint pid)
{
    qDebug() << Q_FUNC_INFO << QString::number(vid, 16) << QString::number(pid, 16);

    if (!PeperoniDevice::isPeperoniDevice(vid, pid))
    {
        qDebug() << Q_FUNC_INFO << "not a Peperoni device";
        return;
    }

    rescanDevices();
}

void Peperoni::slotDeviceRemoved(uint vid, uint pid)
{
    qDebug() << Q_FUNC_INFO << QString::number(vid, 16) << QString::number(pid, 16);

    if (!PeperoniDevice::isPeperoniDevice(vid, pid))
    {
        qDebug() << Q_FUNC_INFO << "not a Peperoni device";
        return;
    }

    rescanDevices();
}