Skip to content

Commit

Permalink
Fix keyboard detect and some crashes
Browse files Browse the repository at this point in the history
  • Loading branch information
Eeems committed Feb 2, 2024
1 parent 031eef3 commit 78e8f9a
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 41 deletions.
12 changes: 0 additions & 12 deletions applications/launcher/controller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -296,18 +296,6 @@ inline void updateUI(QObject* ui, const char* name, const QVariant& value){
ui->setProperty(name, value);
}
}
std::string Controller::exec(const char* cmd) {
std::array<char, 128> buffer;
std::string result;
std::unique_ptr<FILE, decltype(&pclose)> pipe(popen(cmd, "r"), pclose);
if (!pipe) {
throw std::runtime_error("popen() failed!");
}
while (fgets(buffer.data(), buffer.size(), pipe.get()) != nullptr) {
result += buffer.data();
}
return result;
}
void Controller::updateUIElements(){}
void Controller::setAutomaticSleep(bool state){
m_automaticSleep = state;
Expand Down
1 change: 0 additions & 1 deletion applications/launcher/controller.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ class Controller : public QObject
Q_PROPERTY(int maxTouchWidth READ maxTouchWidth)
Q_PROPERTY(int maxTouchHeight READ maxTouchHeight)
public:
static std::string exec(const char* cmd);
QObject* stateController;
QObject* root = nullptr;
explicit Controller(QObject* parent = 0)
Expand Down
54 changes: 33 additions & 21 deletions applications/system-service/wlan.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,12 @@ bool Wlan::pingIP(std::string ip, const char* port) {
}

bool Wlan::isConnected(){
auto ip = exec("/sbin/ip r | /bin/grep " + iface() + " | /bin/grep default | /usr/bin/awk '{print $3}'");
return ip != "" && (pingIP(ip, "53") || pingIP(ip, "80"));
try{
auto ip = exec("/sbin/ip r | /bin/grep " + iface() + " | /bin/grep default | /usr/bin/awk '{print $3}'");
return ip != "" && (pingIP(ip, "53") || pingIP(ip, "80"));
}catch(const std::runtime_error&){
return false;
}
}

int Wlan::link(){
Expand All @@ -83,15 +87,18 @@ int Wlan::link(){
return result;
}
O_WARNING("SignalPoll error: " << res.error());
auto out = exec("/bin/grep " + iface() + " /proc/net/wireless | /usr/bin/awk '{print $3}'");
if(QString(out.c_str()).isEmpty()){
return 0;
}
try {
return std::stoi(out);
}
catch (const std::invalid_argument& e) {
O_WARNING("link failed: " << out.c_str());
try{
auto out = exec("/bin/grep " + iface() + " /proc/net/wireless | /usr/bin/awk '{print $3}'");
if(QString(out.c_str()).isEmpty()){
return 0;
}
try{
return std::stoi(out);
}catch(const std::invalid_argument& e){
O_WARNING("link failed: " << out.c_str());
return 0;
}
}catch(const std::runtime_error&){
return 0;
}
return -100;
Expand All @@ -109,17 +116,22 @@ signed int Wlan::rssi(){
return result;
}
O_WARNING("SignalPoll error: " << res.error());
auto out = exec("/bin/grep " + iface() + " /proc/net/wireless | /usr/bin/awk '{print $4}'");
if(QString(out.c_str()).isEmpty()){
return 0;
}
try {
return std::stoi(out);
}
catch (const std::invalid_argument& e) {
O_WARNING("signal failed: " << out.c_str());
try{
auto out = exec("/bin/grep " + iface() + " /proc/net/wireless | /usr/bin/awk '{print $4}'");
if(QString(out.c_str()).isEmpty()){
return 0;
}
try {
return std::stoi(out);
}
catch (const std::invalid_argument& e) {
O_WARNING("signal failed: " << out.c_str());
return 0;
}
}catch(const std::runtime_error&){
return 0;
}

return -100;
}

Expand Down Expand Up @@ -158,7 +170,7 @@ std::string Wlan::exec(QString cmd) {
std::array<char, 128> buffer;
std::string result;
std::unique_ptr<FILE, decltype(&pclose)> pipe(popen(cmd.toStdString().c_str(), "r"), pclose);
if (!pipe) {
if(!pipe){
throw std::runtime_error("popen() failed!");
}
while (fgets(buffer.data(), buffer.size(), pipe.get()) != nullptr) {
Expand Down
20 changes: 14 additions & 6 deletions shared/liboxide/devicesettings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

#include "debug.h"
#include "liboxide.h"
#include "signalhandler.h"

#include <private/qdevicediscovery_p.h>

#define BITS_PER_LONG (sizeof(long) * 8)
#define NBITS(x) ((((x)-1)/BITS_PER_LONG)+1)
Expand Down Expand Up @@ -77,7 +80,6 @@ namespace Oxide {
}else{
O_DEBUG(("Buttons input device: " + buttonsPath).c_str());
}
watcher = new QFileSystemWatcher(QStringList() << "/dev/input", qApp);
}
DeviceSettings::~DeviceSettings(){}
bool DeviceSettings::checkBitSet(int fd, int type, int i) {
Expand Down Expand Up @@ -251,11 +253,17 @@ namespace Oxide {
}

void DeviceSettings::onInputDevicesChanged(std::function<void()> callback){
watcher->connect(watcher, &QFileSystemWatcher::directoryChanged, qApp, [callback](const QString& path){
qDebug() << path;
if(path == "/dev/input"){
callback();
}
static auto deviceDiscovery = QDeviceDiscovery::create(QDeviceDiscovery::Device_InputMask, qApp);
QObject::connect(deviceDiscovery, &QDeviceDiscovery::deviceDetected, qApp, [callback](const QString& device){
Q_UNUSED(device);
callback();
});
QObject::connect(deviceDiscovery, &QDeviceDiscovery::deviceRemoved, qApp, [callback](const QString& device){
Q_UNUSED(device);
callback();
});
QObject::connect(signalHandler, &SignalHandler::sigCont, qApp, [callback]{
callback();
});
}

Expand Down
1 change: 1 addition & 0 deletions shared/liboxide/liboxide.pro
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ QT += quick
QT += dbus
QT += core_private
QT += gui-private
QT += input_support-private

TEMPLATE = lib
DEFINES += LIBOXIDE_LIBRARY
Expand Down
25 changes: 24 additions & 1 deletion shared/liboxide/signalhandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,14 @@

static int sigUsr1Fd[2];
static int sigUsr2Fd[2];
static int sigContFd[2];

namespace Oxide {
int SignalHandler::setup_unix_signal_handlers(){
if(!signalHandler){
new SignalHandler(qApp);
}
struct sigaction usr1, usr2;
struct sigaction usr1, usr2, cont;

usr1.sa_handler = SignalHandler::usr1SignalHandler;
sigemptyset(&usr1.sa_mask);
Expand All @@ -33,6 +34,14 @@ namespace Oxide {
return 2;
}

cont.sa_handler = SignalHandler::contSignalHandler;
sigemptyset(&cont.sa_mask);
cont.sa_flags = 0;
cont.sa_flags |= SA_RESTART;
if(sigaction(SIGCONT, &cont, 0)){
return 3;
}

return 0;
}
SignalHandler* SignalHandler::singleton(SignalHandler* self){
Expand All @@ -55,6 +64,8 @@ namespace Oxide {
connect(snUsr1, &QSocketNotifier::activated, this, &SignalHandler::handleSigUsr1, Qt::QueuedConnection);
snUsr2 = new QSocketNotifier(sigUsr2Fd[1], QSocketNotifier::Read, this);
connect(snUsr2, &QSocketNotifier::activated, this, &SignalHandler::handleSigUsr2, Qt::QueuedConnection);
snCont = new QSocketNotifier(sigContFd[1], QSocketNotifier::Read, this);
connect(snCont, &QSocketNotifier::activated, this, &SignalHandler::handleSigCont, Qt::QueuedConnection);
}
SignalHandler::~SignalHandler(){}
void SignalHandler::usr1SignalHandler(int unused){
Expand All @@ -67,6 +78,11 @@ namespace Oxide {
char a = 1;
::write(sigUsr2Fd[0], &a, sizeof(a));
}
void SignalHandler::contSignalHandler(int unused){
Q_UNUSED(unused)
char a = 1;
::write(sigContFd[0], &a, sizeof(a));
}
void SignalHandler::handleSigUsr1(){
snUsr1->setEnabled(false);
char tmp;
Expand All @@ -81,6 +97,13 @@ namespace Oxide {
emit sigUsr2();
snUsr2->setEnabled(true);
}
void SignalHandler::handleSigCont(){
snCont->setEnabled(false);
char tmp;
::read(sigContFd[1], &tmp, sizeof(tmp));

Check notice on line 103 in shared/liboxide/signalhandler.cpp

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

shared/liboxide/signalhandler.cpp#L103

Check buffer boundaries if used in a loop including recursive loops (CWE-120, CWE-20).
emit sigCont();
snCont->setEnabled(true);
}
}

#include "moc_signalhandler.cpp"
8 changes: 8 additions & 0 deletions shared/liboxide/signalhandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ namespace Oxide {
* \brief Setup the unix signal handlers to listen to SIGUSR1 and SIGUSR2
* \retval 1 Failed to setup SIGUSR1
* \retval 2 Failed to setup SIGUSR2
* \retval 3 Failed to setup SIGUSR2
* \retval 0 Successfully setup both signal handlers
*
* This method will automatically create and register the singleton with a parent of qApp.
Expand All @@ -48,10 +49,12 @@ namespace Oxide {
~SignalHandler();
static void usr1SignalHandler(int unused);
static void usr2SignalHandler(int unused);
static void contSignalHandler(int unused);

public slots:
void handleSigUsr1();
void handleSigUsr2();
void handleSigCont();

signals:
/*!
Expand All @@ -62,10 +65,15 @@ namespace Oxide {
* \brief The process has recieved a SIGUSR2
*/
void sigUsr2();
/*
* \brief The process has recieved a SIGCONT
*/
void sigCont();

private:
QSocketNotifier* snUsr1;
QSocketNotifier* snUsr2;
QSocketNotifier* snCont;
};
}
/*! @} */

0 comments on commit 78e8f9a

Please sign in to comment.