Skip to content

Commit

Permalink
Initial insomniac lib WIP
Browse files Browse the repository at this point in the history
- project compiles, but not creating an executable
  • Loading branch information
Robert Scheinpflug committed Sep 13, 2015
1 parent 6c71d9c commit b8c3f00
Show file tree
Hide file tree
Showing 14 changed files with 1,106 additions and 34 deletions.
44 changes: 15 additions & 29 deletions harbour-bodyweight-timer.pro
Original file line number Diff line number Diff line change
Expand Up @@ -10,41 +10,24 @@
# - translation filenames have to be changed

# The name of your application
TARGET = harbour-bodyweight-timer

CONFIG += sailfishapp
CONFIG += c++11

TARGET = harbour-bodyweight-timer

QT += network dbus

SOURCES += src/harbour-bodyweight-timer.cpp \
SOURCES = \
src/harbour-bodyweight-timer.cpp \
src/applibrary.cpp

OTHER_FILES += qml/harbour-bodyweight-timer.qml \
qml/cover/CoverPage.qml \
rpm/harbour-bodyweight-timer.changes.in \
rpm/harbour-bodyweight-timer.spec \
rpm/harbour-bodyweight-timer.yaml \
translations/*.ts \
harbour-bodyweight-timer.desktop \
qml/components/TimePickerMinutesSeconds.qml \
qml/js/database.js \
qml/pages/sound/tripple_boxing-bell.wav \
qml/pages/sound/single_boxing-bell.wav \
qml/pages/About.qml \
qml/pages/CircleInterval.qml \
qml/pages/Home.qml \
qml/pages/IntervalSet.qml \
qml/pages/Ladder.qml \
qml/pages/SuperSet.qml \
qml/pages/Tabata.qml \
qml/pages/TimerPickerDialogMinutesSeconds.qml \
qml/js/global_functions.js \
harbour-bodyweight-timer.png \
qml/cover/cover.png \
qml/pages/ExerciseSettings.qml \
qml/pages/AppSettings.qml \
qml/pages/sound/double_boxing-bell.wav

OTHER_FILES += qml/*.qml \
qml/cover/*.qml \
qml/components/*.qml \
qml/pages/*.qml \
js/*.js \
rpm/*.spec \
rpm/harbour-bodyweight-timer.yaml

# to disable building translations every time, comment out the
# following CONFIG line
Expand All @@ -59,3 +42,6 @@ TRANSLATIONS += translations/harbour-bodyweight-timer-de.ts
HEADERS += \
src/applibrary.h

# on adding the following lines, project can not produce an executeable
TEMPLATE = subdirs
SUBDIRS = src/insomniac
1 change: 1 addition & 0 deletions qml/harbour-bodyweight-timer.qml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@

import QtQuick 2.0
import Sailfish.Silica 1.0
import harbour.kitchentimer.insomniac 1.0
import "pages"

ApplicationWindow
Expand Down
9 changes: 9 additions & 0 deletions rpm/harbour-bodyweight-timer.spec
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,17 @@ desktop-file-install --delete-original \
%files
%defattr(-,root,root,-)
%{_bindir}
%{_datadir}
%{_datadir}/%{name}
%{_datadir}/%{name}/lib/harbour/bodyweight-timer/insomniac
%{_datadir}/%{name}/lib/harbour/bodyweight-timer
%{_datadir}/%{name}/lib/harbour
%{_datadir}/%{name}/translations
%{_datadir}/%{name}/qml
%{_datadir}/%{name}/sounds
%{_datadir}/applications/%{name}.desktop
%{_datadir}/icons/hicolor/86x86/apps/%{name}.png
%{_bindir}/%{name}
%{_bindir}
# >> files
# << files
18 changes: 13 additions & 5 deletions rpm/harbour-bodyweight-timer.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,19 @@ Requires:

# All installed files
Files:
- '%{_bindir}'
- '%{_datadir}/%{name}'
- '%{_datadir}/applications/%{name}.desktop'
- '%{_datadir}/icons/hicolor/86x86/apps/%{name}.png'
- '%{_bindir}'
- '%{_datadir}'
- '%{_datadir}/%{name}'
- '%{_datadir}/%{name}/lib/harbour/bodyweight-timer/insomniac'
- '%{_datadir}/%{name}/lib/harbour/bodyweight-timer'
- '%{_datadir}/%{name}/lib/harbour'
- '%{_datadir}/%{name}/translations'
- '%{_datadir}/%{name}/qml'
- '%{_datadir}/%{name}/sounds'
- '%{_datadir}/applications/%{name}.desktop'
- '%{_datadir}/icons/hicolor/86x86/apps/%{name}.png'
- '%{_bindir}/%{name}'
- '%{_bindir}'

# For more information about yaml and what's supported in Sailfish OS
# build system, please see https://wiki.merproject.org/wiki/Spectacle

188 changes: 188 additions & 0 deletions src/insomniac/insomniac.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,188 @@
#include "insomniac.h"
#include <QDebug>

#include <errno.h>
#include <stdio.h>

Insomniac::Insomniac(QQuickItem *parent):
QQuickItem(parent)
, m_interval(0)
, m_timerWindow(120)
, m_running(false)
, m_repeat(true)
, m_iphbdHandler(0)
, m_notifier(0)
{
m_iphbdHandler = iphb_open(0);

if (!m_iphbdHandler) {
m_lastError = Insomniac::InternalError;
qDebug() << "iphb_open error" << m_iphbdHandler<< errno <<strerror(errno);
return;
}

int sockfd = iphb_get_fd(m_iphbdHandler);
if (!(sockfd > -1)) {
m_lastError = Insomniac::InternalError;
qDebug() << "socket failure"<<strerror(errno);
return;
}

m_notifier = new QSocketNotifier(sockfd, QSocketNotifier::Read);
if (!QObject::connect(m_notifier, SIGNAL(activated(int)), this, SLOT(heartbeatReceived(int)))) {
delete m_notifier, m_notifier = 0;
m_lastError = Insomniac::TimerFailed;
qDebug() << "timer failure";
return;
}
m_notifier->setEnabled(false);
}

Insomniac::~Insomniac()
{
if(m_iphbdHandler) {
(void)iphb_close(m_iphbdHandler);
}

if(m_notifier) {
delete m_notifier;
}
}

void Insomniac::wokeUp()
{
if (!m_running)
return;

if (!(m_iphbdHandler && m_notifier)) {
m_lastError = Insomniac::InternalError;
emit error(m_lastError);
return;
}

m_notifier->setEnabled(false);

(void)iphb_I_woke_up(m_iphbdHandler);

m_running = false;
emit runningChanged();
m_lastError = Insomniac::NoError;

start();
}

int Insomniac::interval() const
{
return m_interval;
}

void Insomniac::setInterval(int seconds)
{
m_interval = seconds;
}

void Insomniac::setRepeat(bool repeat)
{
m_repeat = repeat;
}

bool Insomniac::repeat() const
{
return m_repeat;
}

int Insomniac::timerWindow() const
{
return m_timerWindow;
}

/**
Sets the timer's timeout window in seconds.
The timeout window is a window of time set around the interval in which the timer will timeout.
It is wise to have timeout window quite big so all users of this service get synced.
For example if your preferred wait is 120 seconds and you can wait anywhere within 10 seconds,
use interval of 120 and timerWindow of 10. This means the timer will timeout anywhere from
115 seconds to 125 seconds.
*/
void Insomniac::setTimerWindow(int seconds)
{
m_timerWindow = seconds;
}

void Insomniac::start(int interval, int timeWindow)
{
m_interval = interval;
m_timerWindow = timeWindow;

start();
}

void Insomniac::start()
{
if (m_running) {
return;
}

if (!(m_iphbdHandler && m_notifier)) {
m_lastError = Insomniac::InternalError;
emit error(m_lastError);
return;
}

int mustWait = 0;
time_t unixTime = iphb_wait(m_iphbdHandler, m_interval - (m_timerWindow * .5)
, m_interval + (m_timerWindow * .5) , mustWait);

if (unixTime == (time_t)-1) {
m_lastError = Insomniac::TimerFailed;
emit error(m_lastError);
return;
}

m_notifier->setEnabled(true);
m_running = true;
emit runningChanged();
m_lastError = Insomniac::NoError;
}

void Insomniac::stop()
{
if (!m_running) {
return;
}

if (!(m_iphbdHandler && m_notifier)) {
m_lastError = Insomniac::InternalError;
emit error(m_lastError);
return;
}

m_notifier->setEnabled(false);

(void)iphb_discard_wakeups(m_iphbdHandler);

m_running = false;
emit runningChanged();
m_lastError = Insomniac::NoError;
}

void Insomniac::heartbeatReceived(int sock) {
Q_UNUSED(sock);

stop();
emit timeout();

if (m_repeat) {
start();
}
}

bool Insomniac::running() const
{
return m_running;
}

71 changes: 71 additions & 0 deletions src/insomniac/insomniac.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#ifndef INSOMNIAC_H
#define INSOMNIAC_H

#include <QQuickItem>
#include <QSocketNotifier>

extern "C" {
#include "libiphb/libiphb.h"
}

class Insomniac : public QQuickItem
{
Q_OBJECT
Q_DISABLE_COPY(Insomniac)

public:
Insomniac(QQuickItem *parent = 0);
~Insomniac();

Q_PROPERTY(int timerWindow READ timerWindow WRITE setTimerWindow)
Q_PROPERTY(bool repeat READ repeat WRITE setRepeat)
Q_PROPERTY(bool running READ running NOTIFY runningChanged)
Q_PROPERTY(int interval READ interval WRITE setInterval)

enum InsomniacError {
NoError = 0,
InvalidArgument,
TimerFailed,
InternalError
};

Insomniac::InsomniacError m_lastError;
InsomniacError lastError() const;

void wokeUp();

int interval() const;
void setInterval(int seconds);

int timerWindow() const;
void setTimerWindow(int seconds);

bool repeat() const;
void setRepeat(bool repeat);

bool running() const;

Q_SIGNALS:
void runningChanged();
void timeout();
void error(Insomniac::InsomniacError error);

private:
int m_interval;
int m_timerWindow;
bool m_running;
bool m_repeat;
iphb_t m_iphbdHandler;
QSocketNotifier *m_notifier;

public Q_SLOTS:
Q_INVOKABLE void start(int interval, int timerWindow);
Q_INVOKABLE void start();
Q_INVOKABLE void stop();

private Q_SLOTS:
void heartbeatReceived(int sock);
};

#endif // INSOMNIAC_H

Loading

0 comments on commit b8c3f00

Please sign in to comment.