-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.cpp
167 lines (137 loc) · 5.23 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#include <iostream>
#include <stdio.h>
#include <cstdlib>
#include <unistd.h>
#include "includes/MidiHelper.h"
#include "includes/EvDevHelper.h"
#include "includes/cxxopts.hpp"
#include "includes/UtilsHelper.h"
#include "includes/ConfigHelper.h"
#include "spdlog/sinks/stdout_color_sinks.h"
#include "spdlog/async.h"
using namespace std;
MidiHelper *rtmidi_helper;
EvDevHelper *evdev_helper;
ConfigHelper *config_helper;
void save_pid()
{
ofstream pid_file;
pid_file.open (config_helper->get_string_value("pid_file").c_str());
auto pid = getpid();
pid_file << pid;
pid_file.close();
}
bool delete_pid_file()
{
shared_ptr<spdlog::logger> logger = spdlog::get(config_helper->get_string_value("traktor_s4_logger_name"));
if(remove(config_helper->get_string_value("pid_file").c_str()) != 0){
logger->error( "[main::delete_pid_file] Error removing PID file");
return false;
}
logger->info( "[main::delete_pid_file] PID file removed");
return true;
}
void shutdown_application(int signum)
{
EvDevHelper::shutdown_buttons_leds(::config_helper);
delete_pid_file();
exit(signum);
}
void capture_signals()
{
signal(SIGTERM,shutdown_application);
signal(SIGKILL,shutdown_application);
signal(SIGSEGV,shutdown_application);
signal(SIGABRT,shutdown_application);
signal(SIGQUIT,shutdown_application);
signal(SIGTSTP,shutdown_application);
signal(SIGINT,shutdown_application);
}
static void show_main_configuration() {
shared_ptr<spdlog::logger> logger = spdlog::get(config_helper->get_string_value("traktor_s4_logger_name"));
logger->set_level(config_helper->log_level);
logger->info( "[main::init_application] Traktor Kontrol S4 Mk1 Driver for Linux started");
logger->info("[main:show_main_configuration] Current arguments:");
if (config_helper->log_level == spdlog::level::info) {
logger->info("[main:show_main_configuration] Log level: INFO");
} else {
logger->info("[main:show_main_configuration] Log level: DEBUG");
}
if (config_helper->log_mode == LOG_FILE) {
logger->info("[main:show_main_configuration] Log mode: LOG IN FILE");
}
else{
logger->info("[main:show_main_configuration] Log mode: LOG IN CONSOLE");
}
}
static void init_application() {
shared_ptr<spdlog::logger> logger = spdlog::get(config_helper->get_string_value("traktor_s4_logger_name"));
logger->info("[main::init_application] Starting helpers....");
evdev_helper = new EvDevHelper(config_helper);
rtmidi_helper = new MidiHelper(config_helper);
logger->info("[main::init_application] Get MIDI information....");
MidiHelper::show_midi_information(rtmidi_helper, config_helper);
logger->info("[main::init_application] Initializing EvDev device....");
evdev_helper->check_evdev_status();
logger->info("[main::init_application] Initializing leds....");
evdev_helper->initialize_buttons_leds(config_helper);
logger->info("[main::init_application] Reading events from Traktor Kontrol S4 Mk1....");
evdev_helper->read_events_from_device(rtmidi_helper->pMidiOut);
}
int main(int argc, char **argv)
{
capture_signals();
config_helper = new ConfigHelper();
cxxopts::Options options("TraktorKontrolS4DriverLinux", "Driver for Native Instruments Traktor Kontrol S4 Mk1");
options.add_options()
("help", "Shows this help")
("logLevel", "Enable Log level: debug | info (default)", cxxopts::value<std::string>())
("logMode", "Enable Log model: console | logfile (default)", cxxopts::value<std::string>())
("configFile", "Set PATH for config.json path (default: ../config.json)", cxxopts::value<std::string>());
try{
auto result = options.parse(argc, argv);
if (result.count("help"))
{
std::cout << options.help() << std::endl;
exit(EXIT_SUCCESS);
}
string config_file_uri = "../config.json";
if (result.count("configFile")){
config_file_uri = result["configFile"].as<std::string>();
}
if (!config_helper->init_config(config_file_uri)) {
cerr << "Error reading config file (config.json)" << endl;
exit (EXIT_FAILURE);
}
save_pid();
if (result.count("logLevel")){
if(result["logLevel"].as<std::string>() == "debug"){
config_helper->log_level = spdlog::level::debug;
}
else if(result["logLevel"].as<std::string>() == "info"){
config_helper->log_level = spdlog::level::info;
}
}
else{
config_helper->log_level = spdlog::level::info;
}
spdlog::drop_all();
if (result.count("logMode") && (result["logMode"].as<std::string>() == "console")){
config_helper->log_mode = LOG_CONSOLE;
auto console_logger = spdlog::stdout_color_mt(
config_helper->get_string_value("traktor_s4_logger_name"));
}
else{
config_helper->log_mode = LOG_FILE;
auto file_logger = spdlog::basic_logger_mt<spdlog::async_factory>(
config_helper->get_string_value("traktor_s4_logger_name"), config_helper->get_string_value("traktor_s4_log_file"));
}
}
catch(cxxopts::exceptions::no_such_option error){
std::cout << options.help() << std::endl;
exit(EXIT_SUCCESS);
}
show_main_configuration();
init_application();
shutdown_application(EXIT_SUCCESS);
}