Skip to content

Commit

Permalink
Get UDev working and move everything to QtConcurrent::run
Browse files Browse the repository at this point in the history
  • Loading branch information
Eeems committed Jul 1, 2023
1 parent 4bf0d47 commit 722464f
Show file tree
Hide file tree
Showing 7 changed files with 148 additions and 74 deletions.
2 changes: 1 addition & 1 deletion applications/system-service/buttonhandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ void ButtonHandler::run(){
emit keyUp(map[ie.code].keyCode);
}
}else{
yieldCurrentThread();
QThread::yieldCurrentThread();
}
}
}
Expand Down
16 changes: 14 additions & 2 deletions applications/system-service/buttonhandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <QDebug>
#include <QElapsedTimer>
#include <QTimer>
#include <QtConcurrent/QtConcurrentRun>

#include <string>
#include <iostream>
Expand All @@ -24,6 +25,7 @@
#include <cstdlib>
#include <liboxide.h>
#include <liboxide/event_device.h>
#include <liboxide/udev.h>

using namespace std;
using namespace Oxide;
Expand All @@ -42,19 +44,28 @@ struct PressRecord {
static event_device buttons(deviceSettings.getButtonsDevicePath(), O_RDWR);


class ButtonHandler : public QThread {
class ButtonHandler : public QObject {
Q_OBJECT

public:
static ButtonHandler* init();

ButtonHandler() : QThread(), filebuf(buttons.fd, ios::in), stream(&filebuf), pressed(), timer(this), m_enabled(true) {
ButtonHandler() : QObject(), filebuf(buttons.fd, ios::in), stream(&filebuf), pressed(), timer(this), m_enabled(true), watcher(this) {
flood = build_flood();
timer.setInterval(100);
timer.setSingleShot(false);
connect(&timer, &QTimer::timeout, this, &ButtonHandler::timeout);
timer.start();
}
void start(){
if(watcher.isRunning()){
return;
}
Oxide::UDev::ensureMaxThreads();
watcher.setFuture(QtConcurrent::run([this]{
run();
}));
}
void setEnabled(bool enabled){
m_enabled = enabled;
}
Expand Down Expand Up @@ -151,6 +162,7 @@ private slots:
QTimer timer;
const QSet<Qt::Key> validKeys { Qt::Key_Left, Qt::Key_Home, Qt::Key_Right, Qt::Key_PowerOff };
bool m_enabled;
QFutureWatcher<void> watcher;
input_event* flood;
input_event* build_flood(){
auto n = 512 * 8;
Expand Down
21 changes: 17 additions & 4 deletions applications/system-service/digitizerhandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#include <QThread>
#include <QException>
#include <QtConcurrent/QtConcurrentRun>

#include <ext/stdio_filebuf.h>
#include <sstream>
Expand All @@ -13,14 +14,15 @@
#include <vector>
#include <liboxide.h>
#include <liboxide/event_device.h>
#include <liboxide/udev.h>

using namespace std;
using namespace Oxide;

#define touchHandler DigitizerHandler::singleton_touchScreen()
#define wacomHandler DigitizerHandler::singleton_wacom()

class DigitizerHandler : public QThread {
class DigitizerHandler : public QObject {
Q_OBJECT

public:
Expand Down Expand Up @@ -59,11 +61,12 @@ class DigitizerHandler : public QThread {
static int is_uint(string input);

DigitizerHandler(event_device& device)
: QThread(),
: QObject(),
filebuf(device.fd, ios::in),
stream(&filebuf),
m_enabled(true),
device(device) {
device(device),
watcher(this) {
flood = build_flood();
}
~DigitizerHandler(){
Expand All @@ -75,6 +78,15 @@ class DigitizerHandler : public QThread {
}
close(device.fd);
}
void start(){
if(watcher.isRunning()){
return;
}
Oxide::UDev::ensureMaxThreads();
watcher.setFuture(QtConcurrent::run([this]{
run();
}));
}
void setEnabled(bool enabled){
m_enabled = enabled;
}
Expand Down Expand Up @@ -146,7 +158,7 @@ class DigitizerHandler : public QThread {
qDebug() << "Reading From : " << device.device.c_str() << " (" << name << ")";
qDebug() << "Listening for events...";
while(handle_events()){
yieldCurrentThread();
QThread::yieldCurrentThread();
}
}
bool handle_events(){
Expand Down Expand Up @@ -200,6 +212,7 @@ class DigitizerHandler : public QThread {
bool read(input_event* ie){
return (bool)stream.read((char*)ie, static_cast<streamsize>(sizeof(struct input_event)));
}
QFutureWatcher<void> watcher;
};

#endif // DIGITIZERHANDLER_H
59 changes: 31 additions & 28 deletions applications/system-service/fifohandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@

#include <QDebug>
#include <QThread>
#include <QTimer>
#include <QtConcurrent/QtConcurrentRun>

#include <fstream>
#include <liboxide.h>
#include <liboxide/udev.h>

class FifoHandler : public QObject {
Q_OBJECT
Expand All @@ -15,33 +16,22 @@ class FifoHandler : public QObject {
FifoHandler(QString name, QString path, QObject* host)
: QObject(),
host(host),
_thread(this),
timer(this),
watcher(this),
_name(name),
path(path),
in(),
out() {
connect(host, &QObject::destroyed, this, &QObject::deleteLater);
connect(&_thread, &QThread::started, [this]{
emit started();
in.open(this->path.toStdString().c_str(), std::ifstream::in);
if(!in.good()){
O_WARNING("Unable to open fifi (in)" << ::strerror(errno));
}
timer.start(10);
});
connect(&_thread, &QThread::finished, this, [this]{
timer.stop();
connect(&watcher, &QFutureWatcher<void>::finished, [this]{
running = false;
emit finished();
});
connect(&timer, &QTimer::timeout, this, &FifoHandler::run);
QThread::create([this]{
QtConcurrent::run([this]{
out.open(this->path.toStdString().c_str(), std::ifstream::out);
if(!out.good()){
O_WARNING("Unable to open fifi (out)" << ::strerror(errno));
}
})->start();
moveToThread(&_thread);
});
}
~FifoHandler(){
if(in.is_open()){
Expand All @@ -50,10 +40,26 @@ class FifoHandler : public QObject {
if(out.is_open()){
out.close();
}
quit();
}
void start() { _thread.start(); }
void quit(){ _thread.quit();}
void start(){
if(running){
return;
}
running = true;
emit started();
Oxide::UDev::ensureMaxThreads();
watcher.setFuture(QtConcurrent::run([this]{
in.open(path.toStdString().c_str(), std::ifstream::in);
if(!in.good()){
O_WARNING("Unable to open fifi (in)" << ::strerror(errno));
}
run();
}));
}
void quit(){
running = false;
watcher.waitForFinished();
}
void write(const void* data, size_t size){
if(out.is_open()){
out.write((char*)data, size);
Expand All @@ -69,11 +75,10 @@ class FifoHandler : public QObject {
protected:
void run() {
if(!in.is_open()){
quit();
return;
}
std::string data;
while(getline_async(in, data)){
while(running && getline_async(in, data)){
QString line(data.c_str());
line = line.trimmed();
emit dataRecieved(this, line);
Expand All @@ -82,17 +87,16 @@ class FifoHandler : public QObject {
if(in.eof()){
in.clear();
}
thread()->yieldCurrentThread();
}

private:
QObject* host;
QThread _thread;
QTimer timer;
QFutureWatcher<void> watcher;
QString _name;
QString path;
std::ifstream in;
std::ofstream out;
bool running = false;
bool getline_async(std::istream& is, std::string& str, char delim = '\n') {
static std::string lineSoFar;
char inChar;
Expand All @@ -112,9 +116,8 @@ class FifoHandler : public QObject {
lineSoFar.append(1, inChar);
}
}
} while (charsRead != 0 && !lineRead);

return lineRead;
} while (running && charsRead != 0 && !lineRead);
return lineRead && running;
}
};

Expand Down
37 changes: 22 additions & 15 deletions applications/system-service/powerapi.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,21 +47,21 @@ class PowerAPI : public APIBase {
Oxide::Sentry::sentry_span(t, "update", "Update current state", [this]{
update();
});
if(deviceSettings.getDeviceType() == Oxide::DeviceSettings::RM1){
udev = new UDev(this);
udev->addMonitor("power_supply", NULL);
connect(udev, &UDev::event, this, QOverload<>::of(&PowerAPI::update));
udev->start();
}else{
Oxide::Sentry::sentry_span(t, "timer", "Setup timer", [this]{
Oxide::Sentry::sentry_span(t, "monitor", "Setup monitor", [this]{
if(deviceSettings.getDeviceType() == Oxide::DeviceSettings::RM1){
udev = new UDev(this);
udev->addMonitor("power_supply", NULL);
connect(udev, &UDev::event, this, QOverload<>::of(&PowerAPI::update));
udev->start();
}else{
timer = new QTimer(this);
timer->setSingleShot(false);
timer->setInterval(3 * 1000); // 3 seconds
timer->moveToThread(qApp->thread());
connect(timer, &QTimer::timeout, this, QOverload<>::of(&PowerAPI::update));
timer->start();
});
}
}
});
});
}
~PowerAPI(){
Expand All @@ -70,17 +70,24 @@ class PowerAPI : public APIBase {
timer->stop();
delete timer;
}
if(udev != nullptr){
qDebug() << "Killing UDev monitor";
udev->stop();
delete udev;
}
}

void setEnabled(bool enabled) override {
if(deviceSettings.getDeviceType() == Oxide::DeviceSettings::RM1){
// TODO
}else{
if(enabled){
timer->start();
if(enabled){
if(deviceSettings.getDeviceType() == Oxide::DeviceSettings::RM1){
udev->start();
}else{
timer->stop();
timer->start();
}
}else if(deviceSettings.getDeviceType() == Oxide::DeviceSettings::RM1){
udev->stop();
}else{
timer->stop();
}
}

Expand Down
Loading

0 comments on commit 722464f

Please sign in to comment.