Warning, file /plasma-bigscreen/plasma-remotecontrollers/src/wiimote/wiimotecontroller.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 /*
0002  *   SPDX-FileCopyrightText: 2022 Bart Ribbers <bribbers@disroot.org>
0003  *
0004  *   SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0005  */
0006 
0007 #include "wiimotecontroller.h"
0008 #include "../controllermanager.h"
0009 #include "../notificationsmanager.h"
0010 
0011 #include <QDebug>
0012 
0013 WiimoteController::WiimoteController()
0014 {
0015     m_detectionThread = new QThread();
0016     m_eventsThread = new QThread();
0017     WiimoteEventWatcher *eventWatcher = new WiimoteEventWatcher();
0018     
0019     QObject::connect(m_detectionThread, &QThread::started, this, &WiimoteController::deviceDetection);
0020     QObject::connect(m_eventsThread, &QThread::started, eventWatcher, &WiimoteEventWatcher::watchEvents);
0021     
0022     this->moveToThread(m_detectionThread);
0023     eventWatcher->moveToThread(m_eventsThread);
0024 
0025     m_detectionThread->start();
0026     m_eventsThread->start();
0027 }
0028 
0029 void WiimoteController::deviceDetection()
0030 {
0031     // Keep trying to find a Wiiremote indefinitely
0032     struct xwii_monitor *mon;
0033     char *ent;
0034 
0035     while(true) {
0036         // First detect any new controllers
0037         mon = xwii_monitor_new(false, false);
0038         if (!mon) {
0039             qCritical() << "Cannot create monitor";
0040             return;
0041         }
0042 
0043         while ((ent = xwii_monitor_poll(mon))) {
0044             QString uniqueIdentifier = ent;
0045             if (ControllerManager::instance().isConnected(uniqueIdentifier))
0046                 continue;
0047 
0048             struct xwii_iface *iface;
0049             int ret = xwii_iface_new(&iface, ent);
0050 
0051             if (ret) {
0052                 qCritical() << "wiimote: ERROR: Failed to create a new Wiimote device, error:" << ret;
0053                 continue;
0054             }
0055 
0056             Wiimote *wiimote = new Wiimote(iface, ent);
0057             if (wiimote->getDevType() == WIIMOTE_DEVTYPE_UNKNOWN) {
0058                 qDebug() << "wiimote: DEBUG: Unable to determine device type, skipping";
0059                 continue;
0060             }
0061 
0062             ControllerManager::instance().newDevice(wiimote);
0063             free(ent);
0064         }
0065 
0066         xwii_monitor_unref(mon);
0067 
0068         usleep(LOOPTIME); // Don't hug the CPU
0069     }
0070 }
0071 
0072 WiimoteController::~WiimoteController()
0073 {
0074     m_detectionThread->quit();
0075     m_eventsThread->quit();
0076     m_detectionThread->wait();
0077     m_eventsThread->wait();
0078 }
0079 
0080 void WiimoteEventWatcher::watchEvents()
0081 {
0082     while (true) {
0083         QVector<Device*> devices = ControllerManager::instance().getDevicesByType(DeviceWiimote);
0084         for (int i = 0; i < devices.size(); i++)
0085             devices.at(i)->watchEvents();
0086         
0087         usleep(LOOPTIME_WII); // Don't hug the CPU
0088     }
0089 }