Skip to content

Commit

Permalink
v0.2
Browse files Browse the repository at this point in the history
  • Loading branch information
dixonary committed Dec 11, 2017
1 parent 5197558 commit 1e1cb0b
Show file tree
Hide file tree
Showing 18 changed files with 432 additions and 85 deletions.
25 changes: 21 additions & 4 deletions reHackable-launcher.pro → draft.pro
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ QT += quick
CONFIG += c++11
LIBS += -lqsgepaper

TARGET = reHackable-launcher
TARGET = draft

# The following define makes your compiler emit warnings if you use
# any feature of Qt which as been marked deprecated (the exact warnings
Expand All @@ -16,10 +16,14 @@ DEFINES += QT_DEPRECATED_WARNINGS
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0

HEADERS += \
mainview.h
mainview.h \
options.h \
handler.h

SOURCES += main.cpp \
mainview.cpp
mainview.cpp \
options.cpp \
handler.cpp

DEPLOYMENT_PATH = /usr/share/$$TARGET
DEFINES += DEPLOYMENT_PATH=\\\"$$DEPLOYMENT_PATH\\\"
Expand All @@ -30,17 +34,30 @@ js.path == $$DEPLOYMENT_PATH/js
INSTALLS += js

qml.files = qml/Main.qml
qml.files+= qml/MenuItem.qml
qml.path == $$DEPLOYMENT_PATH/qml
INSTALLS += qml


# Installs /etc/draft and /lib/systemd/system/draft.service.
configFiles.files = extra-files/draft
configFiles.path = /etc/
INSTALLS += configFiles

service.files = extra-files/draft.service
service.path=/lib/systemd/system/
INSTALLS += service

# Additional import path used to resolve QML modules in Qt Creator's code model
QML_IMPORT_PATH =




target.path = /usr/bin
INSTALLS += target

DISTFILES += \
js/Main.js
js/Main.js \
qml/MenuItem.qml

13 changes: 13 additions & 0 deletions extra-files/draft.service
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[Unit]
Description=draft launcher
#StartLimitIntervalSec=600
#StartLimitBurst=4
After=home.mount

[Service]
ExecStart=/usr/bin/draft

[Install]
WantedBy=multi-user.target


1 change: 1 addition & 0 deletions extra-files/draft/.terminate
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
:
5 changes: 5 additions & 0 deletions extra-files/draft/01-xochitl
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
name=xochitl
desc=The standard environment for reMarkable.
call=/usr/bin/xochitl
term=killall xochitl
imgFile=xochitl
5 changes: 5 additions & 0 deletions extra-files/draft/02-fingerterm
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
name=fingerterm
desc=Touchscreen accessible terminal.
call=/usr/bin/fingerterm
term=:
imgFile=fingerterm
5 changes: 5 additions & 0 deletions extra-files/draft/99-shutdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
name=shutdown
desc=Switch off the reMarkable.
call=shutdown now
term=:
imgFile=power
Binary file added extra-files/draft/icons/fingerterm.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added extra-files/draft/icons/power.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added extra-files/draft/icons/xochitl.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
69 changes: 69 additions & 0 deletions handler.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#include "handler.h"
#include "mainview.h"
#include <QtQuick>
#include <QString>
#include <iostream>
#include <fstream>
#include <time.h>
#include <unistd.h>
#include <stdlib.h>

time_t lastReturn;
int qSize = 1404;

Handler::Handler(std::string lT, std::string term, QGuiApplication* app, MainView* mainView, QObject* obj)
: link(lT), term(term), ef(obj), app(app), mainView(mainView){

time(&lastReturn);

}

bool Handler::eventFilter(QObject* obj, QEvent* event)
{
if (event->type() == QEvent::MouseButtonPress) {
obj->parent()->setProperty("color", QVariant("#22000000"));
return true;
}

else if (event->type() == QEvent::MouseButtonRelease) {
obj->parent()->setProperty("color", QVariant("white"));
time_t tim;
time(&tim);
// 1 second cooldown before opening again!
if(difftime(tim, lastReturn) > 1) {
handleEvent();
}
return true;
}
else
return false;
}

void Handler::handleEvent() {

std::cout << "Setting termfile /etc/draft/.terminate" << std::endl;
std::ofstream termfile;
termfile.open("/etc/draft/.terminate");
termfile << term << std::endl;

std::cout << "Running command " << link << "..." << std::endl;
system((link).c_str());

// Don't exit any more - just head back to the launcher!
std::cout << "Running again" << std::endl;

// Yes, this is an exceptionally hacky way of repainting the screen
qSize = 1-(qSize-1403)+1403;

usleep(500000);

mainView->rootObject()->setProperty("width",QVariant(qSize));

// Cooldown
time(&lastReturn);

}

Handler::~Handler() {
ef->removeEventFilter(this);
}
27 changes: 27 additions & 0 deletions handler.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#pragma once

#include <QObject>
#include <QQuickItem>
#include <QDebug>
#include <QtGui>
#include "mainview.h"
#include <time.h>

extern time_t lastReturn;

class Handler : public QObject
{
Q_OBJECT
public:
Handler(std::string lT, std::string term, QGuiApplication* app, MainView* mainView, QObject* obj);
~Handler();
bool eventFilter(QObject* obj, QEvent* event);
void handleEvent();

private:
std::string link;
std::string term;
QObject* ef;
QGuiApplication* app;
MainView* mainView;
};
11 changes: 8 additions & 3 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
//#include <QtGui>
//#include <QtPlugin>
#include "mainview.h"
#include "options.h"
#include "handler.h"

Q_IMPORT_PLUGIN(QsgEpaperPlugin)

Expand All @@ -10,17 +12,20 @@ int main(int argc, char *argv[])
qputenv("QMLSCENE_DEVICE", "epaper");
qputenv("QT_QPA_PLATFORM", "epaper:enable_fonts");
qputenv("QT_QPA_EVDEV_TOUCHSCREEN_PARAMETERS", "rotate=180");
// qputenv("QT_QPA_EVDEV_TOUCHSCREEN_PARAMETERS", "/dev/input/event1");
// qputenv("QT_QPA_EGLFS_NO_LIBINPUT", "1");

system("/usr/bin/button-capture &");

QGuiApplication app(argc, argv);
MainView view;

srand(time(NULL));

view.rootContext()->setContextProperty("screenGeometry", app.primaryScreen()->geometry());
view.engine()->addImportPath(QStringLiteral(DEPLOYMENT_PATH));
view.setSource(QDir(DEPLOYMENT_PATH).filePath("qml/Main.qml"));
view.show();

qDebug() << "view shown";
Options options(&view, &app);

return app.exec();
}
5 changes: 1 addition & 4 deletions mainview.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#ifndef MAINVIEW_H
#define MAINVIEW_H
#pragma once

#include <QObject>
#include <QQuickView>
Expand All @@ -17,5 +16,3 @@ public slots:
void tabletEvent(QTabletEvent* te);
void touchEvent(QTouchEvent* te);
};

#endif // MAINVIEW_H
120 changes: 120 additions & 0 deletions options.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
#include "options.h"
#include "handler.h"
#include <dirent.h>
#include <string>
#include <vector>
#include <iostream>
#include <QtQuick>
#include <algorithm>

const std::string configDir = "/etc/draft";

// Create options and add them to the screen.
Options::Options(MainView* mainView, QGuiApplication* app) :
mainView(mainView),
optionsView(mainView->rootObject()->findChild<QQuickItem*>("optionsArea")),
app(app)
{

std::vector<std::string> filenames;

// If the config directory doesn't exist,
// then print an error and stop.
if(!Options::read_directory(configDir, filenames)) {
Options::error("Failed to read directory - it does not exist.");
return;
}

std::sort(filenames.begin(), filenames.end());

for(std::string f : filenames) {

std::cout << "parsing file " << f << std::endl;
QFile file((configDir + "/" + f).c_str());
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
Options::error("Couldn't find the file " + f + ".");
break;
}

QTextStream in(&file);

OptionItem opt;

while (!in.atEnd()) {
std::string line = in.readLine().toStdString();
if(line.length() > 0) {
size_t sep = line.find("=");
if(sep != line.npos) {
std::string lhs = line.substr(0,sep);
std::string rhs = line.substr(sep+1);

if (lhs == "name") opt.name = rhs;
else if(lhs == "desc") opt.desc = rhs;
else if(lhs == "imgFile") opt.imgFile = rhs;
else if(lhs == "call") opt.call = rhs;
else if(lhs == "term") opt.term = rhs;
else std::cout << "ignoring unknown parameter \"" << line
<< "\" in file \"" << f << "\"" << std::endl;
}
}
else {
std::cout << "ignoring malformed line \"" << line
<< "\" in file \"" << f << "\"" << std::endl;
}
}

if(opt.call == "" || opt.term == "") continue;
createOption(opt, optionList.size());
optionList.push_back(opt);

}

}

void Options::createOption(OptionItem &option, size_t index) {

QQuickView* opt = new QQuickView();
opt->setSource(QDir(DEPLOYMENT_PATH).filePath("qml/MenuItem.qml"));
opt->show();

QQuickItem* root = opt->rootObject();
root->setProperty("itemNumber", QVariant(index));
root->setParentItem(optionsView);

root->setProperty("t_name",QVariant(option.name.c_str()));
root->setProperty("t_desc",QVariant(option.desc.c_str()));
root->setProperty("t_imgFile",QVariant(("file://"+configDir+"/icons/"+option.imgFile+".png").c_str()));

QObject* mouseArea = root->children().at(0);
Handler* handler = new Handler(option.call, option.term, app, mainView, mouseArea);
root->children().at(0)->installEventFilter(handler);

option.object = opt;
option.handler = handler;
}

void Options::error(std::string text) {
std::cout << "!! Error: " << text << std::endl;
}


// Stolen shamelessly from Martin Broadhurst
bool Options::read_directory(const std::string name,
std::vector<std::string>& filenames)
{
DIR* dirp = opendir(name.c_str());
if(dirp == nullptr) {
return false;
}

struct dirent * dp;
while ((dp = readdir(dirp)) != NULL) {
std::string dn = dp->d_name;

if(dn == "." || dn == ".."|| dn == "icons") continue;

filenames.push_back(dn);
}
closedir(dirp);
return true;
}
32 changes: 32 additions & 0 deletions options.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#pragma once

#include <QObject>
#include <QQuickView>
#include <QDebug>
#include <QGuiApplication>
#include "mainview.h"

struct OptionItem {
std::string name;
std::string desc;
std::string call;
std::string term;
std::string imgFile;
QObject* object;
QObject* handler;
};

class Options
{
public:
Options(MainView* mainView, QGuiApplication* app);
bool read_directory(const std::string name, std::vector<std::string>& files);
private:
void error(std::string text);
void createOption(OptionItem &option, size_t index);

MainView* mainView;
QQuickItem* optionsView;
std::vector<OptionItem> optionList;
QGuiApplication* app;
};
Loading

0 comments on commit 1e1cb0b

Please sign in to comment.