Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/rotate #7

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
170 changes: 98 additions & 72 deletions src/config/config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,100 +16,126 @@
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/


#include <zconf.h>
#include "config.h"
#include "../util.h"
#include <zconf.h>

/**
* Check if config file exists at current path
*/
bool gebaar::config::Config::config_file_exists()
{
auto true_path = std::filesystem::path(config_file_path);
return std::filesystem::exists(true_path);
bool gebaar::config::Config::config_file_exists() {
auto true_path = std::filesystem::path(config_file_path);
return std::filesystem::exists(true_path);
}

/**
* Load Configuration from TOML file
*/
void gebaar::config::Config::load_config()
{
if (find_config_file()) {
if (config_file_exists()) {
try {
config = cpptoml::parse_file(std::filesystem::path(config_file_path));
} catch (const cpptoml::parse_exception& e) {
std::cerr << e.what() << std::endl;
exit(EXIT_FAILURE);
}

/* Swipe Settings */
swipe_three_commands[1] = *config->get_qualified_as<std::string>("swipe.commands.three.left_up");
swipe_three_commands[2] = *config->get_qualified_as<std::string>("swipe.commands.three.up");
swipe_three_commands[3] = *config->get_qualified_as<std::string>("swipe.commands.three.right_up");
swipe_three_commands[4] = *config->get_qualified_as<std::string>("swipe.commands.three.left");
swipe_three_commands[6] = *config->get_qualified_as<std::string>("swipe.commands.three.right");
swipe_three_commands[7] = *config->get_qualified_as<std::string>("swipe.commands.three.left_down");
swipe_three_commands[8] = *config->get_qualified_as<std::string>("swipe.commands.three.down");
swipe_three_commands[9] = *config->get_qualified_as<std::string>("swipe.commands.three.right_down");

swipe_four_commands[1] = *config->get_qualified_as<std::string>("swipe.commands.four.left_up");
swipe_four_commands[2] = *config->get_qualified_as<std::string>("swipe.commands.four.up");
swipe_four_commands[3] = *config->get_qualified_as<std::string>("swipe.commands.four.right_up");
swipe_four_commands[4] = *config->get_qualified_as<std::string>("swipe.commands.four.left");
swipe_four_commands[6] = *config->get_qualified_as<std::string>("swipe.commands.four.right");
swipe_four_commands[7] = *config->get_qualified_as<std::string>("swipe.commands.four.left_down");
swipe_four_commands[8] = *config->get_qualified_as<std::string>("swipe.commands.four.down");
swipe_four_commands[9] = *config->get_qualified_as<std::string>("swipe.commands.four.right_down");

settings.swipe_threshold = config->get_qualified_as<double>("swipe.settings.threshold").value_or(0.5);
settings.swipe_one_shot = config->get_qualified_as<bool>("swipe.settings.one_shot").value_or(true);
settings.swipe_trigger_on_release = config->get_qualified_as<bool>("swipe.settings.trigger_on_release").value_or(true);

/* Pinch settings */
pinch_commands[PINCH_IN] = *config->get_qualified_as<std::string>("pinch.commands.two.out");
pinch_commands[PINCH_OUT] = *config->get_qualified_as<std::string>("pinch.commands.two.in");

settings.pinch_threshold = config->get_qualified_as<double>("pinch.settings.threshold").value_or(0.25);
settings.pinch_one_shot = config->get_qualified_as<bool>("pinch.settings.one_shot").value_or(false);


loaded = true;
}
void gebaar::config::Config::load_config() {
if (find_config_file()) {
if (config_file_exists()) {
try {
config = cpptoml::parse_file(std::filesystem::path(config_file_path));
} catch (const cpptoml::parse_exception &e) {
std::cerr << e.what() << std::endl;
exit(EXIT_FAILURE);
}

/* Swipe Settings */
swipe_three_commands[1] = *config->get_qualified_as<std::string>(
"swipe.commands.three.left_up");
swipe_three_commands[2] =
*config->get_qualified_as<std::string>("swipe.commands.three.up");
swipe_three_commands[3] = *config->get_qualified_as<std::string>(
"swipe.commands.three.right_up");
swipe_three_commands[4] =
*config->get_qualified_as<std::string>("swipe.commands.three.left");
swipe_three_commands[6] =
*config->get_qualified_as<std::string>("swipe.commands.three.right");
swipe_three_commands[7] = *config->get_qualified_as<std::string>(
"swipe.commands.three.left_down");
swipe_three_commands[8] =
*config->get_qualified_as<std::string>("swipe.commands.three.down");
swipe_three_commands[9] = *config->get_qualified_as<std::string>(
"swipe.commands.three.right_down");

swipe_four_commands[1] =
*config->get_qualified_as<std::string>("swipe.commands.four.left_up");
swipe_four_commands[2] =
*config->get_qualified_as<std::string>("swipe.commands.four.up");
swipe_four_commands[3] = *config->get_qualified_as<std::string>(
"swipe.commands.four.right_up");
swipe_four_commands[4] =
*config->get_qualified_as<std::string>("swipe.commands.four.left");
swipe_four_commands[6] =
*config->get_qualified_as<std::string>("swipe.commands.four.right");
swipe_four_commands[7] = *config->get_qualified_as<std::string>(
"swipe.commands.four.left_down");
swipe_four_commands[8] =
*config->get_qualified_as<std::string>("swipe.commands.four.down");
swipe_four_commands[9] = *config->get_qualified_as<std::string>(
"swipe.commands.four.right_down");

settings.swipe_threshold =
config->get_qualified_as<double>("swipe.settings.threshold")
.value_or(0.5);
settings.swipe_one_shot =
config->get_qualified_as<bool>("swipe.settings.one_shot")
.value_or(true);
settings.swipe_trigger_on_release =
config->get_qualified_as<bool>("swipe.settings.trigger_on_release")
.value_or(true);

/* Pinch settings */
pinch_commands[PINCH_IN] =
*config->get_qualified_as<std::string>("pinch.commands.two.out");
pinch_commands[PINCH_OUT] =
*config->get_qualified_as<std::string>("pinch.commands.two.in");
pinch_commands[ROTATE_RIGHT] =
*config->get_qualified_as<std::string>("pinch.commands.rotate.right");
pinch_commands[ROTATE_LEFT] =
*config->get_qualified_as<std::string>("pinch.commands.rotate.left");

settings.pinch_threshold =
config->get_qualified_as<double>("pinch.settings.threshold")
.value_or(0.25);
settings.pinch_one_shot =
config->get_qualified_as<bool>("pinch.settings.one_shot")
.value_or(false);

loaded = true;
}

}
}

/**
* Find the configuration file according to XDG spec
* @return bool
*/
bool gebaar::config::Config::find_config_file()
{
std::string temp_path = gebaar::util::stringFromCharArray(getenv("XDG_CONFIG_HOME"));
bool gebaar::config::Config::find_config_file() {
std::string temp_path =
gebaar::util::stringFromCharArray(getenv("XDG_CONFIG_HOME"));
if (temp_path.empty()) {
// first get the path to HOME
temp_path = gebaar::util::stringFromCharArray(getenv("HOME"));
if (temp_path.empty()) {
// first get the path to HOME
temp_path = gebaar::util::stringFromCharArray(getenv("HOME"));
if (temp_path.empty()) {
temp_path = getpwuid(getuid())->pw_dir;
}
// then append .config
if (!temp_path.empty()) {
temp_path.append("/.config");
}
temp_path = getpwuid(getuid())->pw_dir;
}
// then append .config
if (!temp_path.empty()) {
config_file_path = temp_path;
config_file_path.append("/gebaar/gebaard.toml");
return true;
temp_path.append("/.config");
}
return false;
}
if (!temp_path.empty()) {
config_file_path = temp_path;
config_file_path.append("/gebaar/gebaard.toml");
return true;
}
return false;
}

gebaar::config::Config::Config()
{
if (!loaded) {
load_config();
}
gebaar::config::Config::Config() {
if (!loaded) {
load_config();
}
}
54 changes: 25 additions & 29 deletions src/config/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,44 +21,40 @@

#include <cpptoml.h>
#include <filesystem>
#include <pwd.h>
#include <iostream>
#include <pwd.h>

namespace gebaar::config {
class Config {
public:
Config();

bool loaded = false;

void load_config();


struct settings {
bool pinch_one_shot;
double pinch_threshold;
class Config {
public:
Config();

bool swipe_one_shot;
double swipe_threshold;
bool swipe_trigger_on_release;
} settings;
bool loaded = false;

enum pinch {PINCH_IN, PINCH_OUT};
std::string swipe_three_commands[10];
std::string swipe_four_commands[10];
std::string pinch_commands[10];
void load_config();

private:
struct settings {
bool pinch_one_shot;
double pinch_threshold;

bool config_file_exists();
bool swipe_one_shot;
double swipe_threshold;
bool swipe_trigger_on_release;
} settings;

bool find_config_file();
enum pinch { PINCH_IN, PINCH_OUT, ROTATE_RIGHT, ROTATE_LEFT };
std::string swipe_three_commands[10];
std::string swipe_four_commands[10];
std::string pinch_commands[10];

private:
bool config_file_exists();

std::string config_file_path;
std::shared_ptr<cpptoml::table> config;
bool find_config_file();

std::string config_file_path;
std::shared_ptr<cpptoml::table> config;
};
} // namespace gebaar::config

};
}
#endif //GEBAAR_CONFIG_H
#endif // GEBAAR_CONFIG_H
78 changes: 41 additions & 37 deletions src/daemonizer.cpp
Original file line number Diff line number Diff line change
@@ -1,63 +1,67 @@
/*
gebaar
Copyright (C) 2019 coffee2code

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/


#include "daemonizer.h"

/**
* Forking logic for classic style daemon functionality
*
* @return bool that denotes fork success
*/
bool gebaar::daemonizer::Daemonizer::daemonize()
{
pid_t pid = 0;
pid = fork();
if (pid<0) {
exit(EXIT_FAILURE);
}
if (pid>0) {
exit(EXIT_SUCCESS);
}
if (setsid()<0) {
// Boo.
}
signal(SIGCHLD, SIG_IGN);
signal(SIGTRAP, SIG_IGN);
pid = fork();
if (pid<0) {
exit(EXIT_FAILURE);
}
if (pid>0) {
exit(EXIT_SUCCESS);
}
umask(0);
if ((chdir("/"))<0) {
return false;
}
close(STDOUT_FILENO);
close(STDIN_FILENO);
close(STDERR_FILENO);
if (getpid()!=getsid(getpid())) {
//
}
return true;
bool gebaar::daemonizer::Daemonizer::daemonize() {
pid_t pid = 0;
pid = fork();
if (pid < 0) {
exit(EXIT_FAILURE);
}

if (pid > 0) {
exit(EXIT_SUCCESS);
}

if (setsid() < 0) {
// Boo.
}

signal(SIGCHLD, SIG_IGN);
signal(SIGTRAP, SIG_IGN);

pid = fork();
if (pid < 0) {
exit(EXIT_FAILURE);
}

if (pid > 0) {
exit(EXIT_SUCCESS);
}
umask(0);
if ((chdir("/")) < 0) {
return false;
}

close(STDOUT_FILENO);
close(STDIN_FILENO);
close(STDERR_FILENO);

if (getpid() != getsid(getpid())) {
//
}
return true;
}

gebaar::daemonizer::Daemonizer::Daemonizer() = default;
Expand Down
Loading