initial commit

This commit is contained in:
Blue 2018-08-05 00:46:25 +03:00 committed by Юрий Губич
commit 4b60ece582
327 changed files with 28286 additions and 0 deletions

22
lib/utils/CMakeLists.txt Normal file
View file

@ -0,0 +1,22 @@
cmake_minimum_required(VERSION 2.8.12)
project(utils)
find_package(Qt5Core REQUIRED)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_AUTOMOC ON)
set(HEADERS
defines.h
exception.h
signalcatcher.h
)
set(SOURCES
exception.cpp
signalcatcher.cpp
)
add_library(utils ${HEADERS} ${SOURCES})
target_link_libraries(utils Qt5::Core)

9
lib/utils/defines.h Normal file
View file

@ -0,0 +1,9 @@
#ifndef DEFINES_UTILS_H
#define DEFINES_UTILS_H
#define handler(HANDLER) \
void _h_##HANDLER(const W::Event& ev) {h_##HANDLER(ev);}\
virtual void h_##HANDLER(const W::Event& ev);\
#endif

14
lib/utils/exception.cpp Normal file
View file

@ -0,0 +1,14 @@
#include "exception.h"
Utils::Exception::Exception()
{
}
Utils::Exception::~Exception()
{
}
const char* Utils::Exception::what() const noexcept( true )
{
return getMessage().c_str();
}

22
lib/utils/exception.h Normal file
View file

@ -0,0 +1,22 @@
#ifndef EXCEPTION_H
#define EXCEPTION_H
#include <stdexcept>
#include <string>
namespace Utils
{
class Exception:
public std::exception
{
public:
Exception();
virtual ~Exception();
virtual std::string getMessage() const = 0;
const char* what() const noexcept( true );
};
}
#endif // EXCEPTION_H

View file

@ -0,0 +1,59 @@
#include "signalcatcher.h"
#include <signal.h>
#include <sys/socket.h>
#include <unistd.h>
int W::SignalCatcher::sigintFd[2] = {0,0};
W::SignalCatcher::SignalCatcher(QCoreApplication *p_app, QObject *parent):
QObject(parent),
app(p_app)
{
if (::socketpair(AF_UNIX, SOCK_STREAM, 0, sigintFd))
{
qFatal("Couldn't create INT socketpair");
}
if (setup_unix_signal_handlers() != 0)
{
qFatal("Couldn't install unix handlers");
}
snInt = new QSocketNotifier(sigintFd[1], QSocketNotifier::Read, this);
connect(snInt, SIGNAL(activated(int)), this, SLOT(handleSigInt()));
}
W::SignalCatcher::~SignalCatcher()
{}
void W::SignalCatcher::handleSigInt()
{
snInt->setEnabled(false);
char tmp;
::read(sigintFd[1], &tmp, sizeof(tmp));
app->quit();
snInt->setEnabled(true);
}
void W::SignalCatcher::intSignalHandler(int unused)
{
char a = 1;
::write(sigintFd[0], &a, sizeof(a));
}
int W::SignalCatcher::setup_unix_signal_handlers()
{
struct sigaction s_int;
s_int.sa_handler = SignalCatcher::intSignalHandler;
sigemptyset(&s_int.sa_mask);
s_int.sa_flags = 0;
s_int.sa_flags |= SA_RESTART;
if (sigaction(SIGINT, &s_int, 0) > 0)
return 1;
return 0;
}

33
lib/utils/signalcatcher.h Normal file
View file

@ -0,0 +1,33 @@
#ifndef SIGNALCATCHER_H
#define SIGNALCATCHER_H
#include <QtCore/QCoreApplication>
#include <QtCore/QObject>
#include <QtCore/QSocketNotifier>
namespace W
{
class SignalCatcher: public QObject
{
Q_OBJECT
public:
SignalCatcher(QCoreApplication *p_app, QObject *parent = 0);
~SignalCatcher();
static void intSignalHandler(int unused);
public slots:
void handleSigInt();
private:
QCoreApplication *app;
static int sigintFd[2];
QSocketNotifier *snInt;
static int setup_unix_signal_handlers();
};
}
#endif // SIGNALCATCHER_H