2020-06-24 02:49:24 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2019-2020 PixlOne
|
|
|
|
*
|
|
|
|
* 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/>.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2019-07-17 05:53:51 +00:00
|
|
|
#include <algorithm>
|
|
|
|
#include <cstdio>
|
|
|
|
#include <cstdlib>
|
2020-04-13 02:25:39 +00:00
|
|
|
#include <mutex>
|
2019-07-17 05:53:51 +00:00
|
|
|
|
2020-06-24 22:10:25 +00:00
|
|
|
#include "util/log.h"
|
2020-06-20 02:01:14 +00:00
|
|
|
#include "DeviceManager.h"
|
2020-04-13 02:25:39 +00:00
|
|
|
#include "logid.h"
|
2020-07-05 06:55:46 +00:00
|
|
|
#include "InputDevice.h"
|
2020-07-10 07:16:51 +00:00
|
|
|
#include "util/workqueue.h"
|
2019-07-17 05:53:51 +00:00
|
|
|
|
2020-07-05 06:55:46 +00:00
|
|
|
#define LOGID_VIRTUAL_INPUT_NAME "LogiOps Virtual Input"
|
2019-08-09 18:25:40 +00:00
|
|
|
#define DEFAULT_CONFIG_FILE "/etc/logid.cfg"
|
2019-07-17 05:53:51 +00:00
|
|
|
|
2020-04-11 00:53:13 +00:00
|
|
|
#ifndef LOGIOPS_VERSION
|
|
|
|
#define LOGIOPS_VERSION "null"
|
|
|
|
#warning Version is undefined!
|
|
|
|
#endif
|
|
|
|
|
2019-10-05 01:56:17 +00:00
|
|
|
using namespace logid;
|
|
|
|
|
2020-07-14 04:12:40 +00:00
|
|
|
struct CmdlineOptions
|
|
|
|
{
|
|
|
|
std::string config_file = DEFAULT_CONFIG_FILE;
|
|
|
|
};
|
2020-04-12 23:44:12 +00:00
|
|
|
|
2020-06-24 22:10:25 +00:00
|
|
|
LogLevel logid::global_loglevel = INFO;
|
2020-07-02 04:38:40 +00:00
|
|
|
std::shared_ptr<Configuration> logid::global_config;
|
2020-06-25 02:27:19 +00:00
|
|
|
std::unique_ptr<DeviceManager> logid::device_manager;
|
2020-07-05 06:55:46 +00:00
|
|
|
std::unique_ptr<InputDevice> logid::virtual_input;
|
2020-07-12 06:51:56 +00:00
|
|
|
std::shared_ptr<workqueue> logid::global_workqueue;
|
2019-07-17 05:53:51 +00:00
|
|
|
|
2020-04-13 02:25:39 +00:00
|
|
|
bool logid::kill_logid = false;
|
2020-06-25 02:27:19 +00:00
|
|
|
std::mutex logid::device_manager_reload;
|
2020-04-13 02:25:39 +00:00
|
|
|
|
2019-08-09 02:13:50 +00:00
|
|
|
enum class Option
|
|
|
|
{
|
|
|
|
None,
|
|
|
|
Verbose,
|
|
|
|
Config,
|
2020-04-11 00:53:13 +00:00
|
|
|
Help,
|
|
|
|
Version
|
2019-08-09 02:13:50 +00:00
|
|
|
};
|
|
|
|
|
2020-06-16 23:53:38 +00:00
|
|
|
/*
|
2020-04-13 02:25:39 +00:00
|
|
|
void logid::reload()
|
|
|
|
{
|
|
|
|
log_printf(INFO, "Reloading logid...");
|
|
|
|
finder_reloading.lock();
|
|
|
|
finder->stop();
|
|
|
|
Configuration* old_config = global_config;
|
|
|
|
global_config = new Configuration(config_file.c_str());
|
|
|
|
delete(old_config);
|
|
|
|
delete(finder);
|
2020-06-16 23:53:38 +00:00
|
|
|
finder = new DeviceMonitor();
|
2020-04-13 02:25:39 +00:00
|
|
|
finder_reloading.unlock();
|
|
|
|
}
|
2020-06-16 23:53:38 +00:00
|
|
|
*/
|
2020-04-13 02:25:39 +00:00
|
|
|
|
2020-07-14 04:12:40 +00:00
|
|
|
void readCliOptions(const int argc, char** argv, CmdlineOptions& options)
|
2019-07-17 05:53:51 +00:00
|
|
|
{
|
2020-06-24 02:35:37 +00:00
|
|
|
for(int i = 1; i < argc; i++) {
|
2019-08-09 02:13:50 +00:00
|
|
|
Option option = Option::None;
|
2020-06-24 02:35:37 +00:00
|
|
|
if(argv[i][0] == '-') {
|
|
|
|
// This argument is an option
|
|
|
|
switch(argv[i][1]) {
|
|
|
|
case '-': {
|
|
|
|
// Full option name
|
|
|
|
std::string op_str = argv[i];
|
|
|
|
if (op_str == "--verbose") option = Option::Verbose;
|
|
|
|
if (op_str == "--config") option = Option::Config;
|
|
|
|
if (op_str == "--help") option = Option::Help;
|
|
|
|
if (op_str == "--version") option = Option::Version;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'v': // Verbosity
|
|
|
|
option = Option::Verbose;
|
|
|
|
break;
|
|
|
|
case 'V': //Version
|
2020-04-11 00:53:13 +00:00
|
|
|
option = Option::Version;
|
|
|
|
break;
|
2020-06-24 02:35:37 +00:00
|
|
|
case 'c': // Config file path
|
|
|
|
option = Option::Config;
|
|
|
|
break;
|
|
|
|
case 'h': // Help
|
|
|
|
option = Option::Help;
|
|
|
|
break;
|
|
|
|
default:
|
2020-06-24 22:10:25 +00:00
|
|
|
logPrintf(WARN, "%s is not a valid option, ignoring.",
|
2020-06-24 02:35:37 +00:00
|
|
|
argv[i]);
|
2019-08-09 02:13:50 +00:00
|
|
|
}
|
2020-06-24 02:35:37 +00:00
|
|
|
|
|
|
|
switch(option) {
|
|
|
|
case Option::Verbose: {
|
|
|
|
if (++i >= argc) {
|
2020-06-24 22:10:25 +00:00
|
|
|
global_loglevel = DEBUG; // Assume debug verbosity
|
2019-08-09 02:13:50 +00:00
|
|
|
break;
|
|
|
|
}
|
2020-06-24 02:35:37 +00:00
|
|
|
std::string loglevel = argv[i];
|
|
|
|
try {
|
2020-06-24 22:10:25 +00:00
|
|
|
global_loglevel = toLogLevel(argv[i]);
|
2020-06-24 02:35:37 +00:00
|
|
|
} catch (std::invalid_argument &e) {
|
|
|
|
if (argv[i][0] == '-') {
|
2020-06-24 22:10:25 +00:00
|
|
|
global_loglevel = DEBUG; // Assume debug verbosity
|
2020-06-24 02:35:37 +00:00
|
|
|
i--; // Go back to last argument to continue loop.
|
|
|
|
} else {
|
2020-06-24 22:10:25 +00:00
|
|
|
logPrintf(WARN, e.what());
|
2020-06-24 02:35:37 +00:00
|
|
|
printf("Valid verbosity levels are: Debug, Info, "
|
|
|
|
"Warn/Warning, or Error.\n");
|
2020-04-12 23:44:12 +00:00
|
|
|
exit(EXIT_FAILURE);
|
2019-08-09 02:13:50 +00:00
|
|
|
}
|
2020-06-24 02:35:37 +00:00
|
|
|
}
|
2019-08-09 02:13:50 +00:00
|
|
|
break;
|
2020-06-24 02:35:37 +00:00
|
|
|
}
|
|
|
|
case Option::Config: {
|
|
|
|
if (++i >= argc) {
|
2020-06-24 22:10:25 +00:00
|
|
|
logPrintf(ERROR, "Config file is not specified.");
|
2020-06-24 02:35:37 +00:00
|
|
|
exit(EXIT_FAILURE);
|
2019-08-09 02:13:50 +00:00
|
|
|
}
|
2020-07-14 04:12:40 +00:00
|
|
|
options.config_file = argv[i];
|
2020-06-24 02:35:37 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Option::Help:
|
|
|
|
printf(R"(logid version %s
|
2020-04-11 00:53:13 +00:00
|
|
|
Usage: %s [options]
|
2019-08-09 02:13:50 +00:00
|
|
|
Possible options are:
|
|
|
|
-v,--verbose [level] Set log level to debug/info/warn/error (leave blank for debug)
|
2020-04-11 00:53:13 +00:00
|
|
|
-V,--version Print version number
|
2019-08-09 02:13:50 +00:00
|
|
|
-c,--config [file path] Change config file from default at %s
|
|
|
|
-h,--help Print this message.
|
2020-04-11 00:53:13 +00:00
|
|
|
)", LOGIOPS_VERSION, argv[0], DEFAULT_CONFIG_FILE);
|
2020-06-24 02:35:37 +00:00
|
|
|
exit(EXIT_SUCCESS);
|
|
|
|
case Option::Version:
|
|
|
|
printf("%s\n", LOGIOPS_VERSION);
|
|
|
|
exit(EXIT_SUCCESS);
|
|
|
|
case Option::None:
|
|
|
|
break;
|
2019-08-09 02:13:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-04-12 23:44:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char** argv)
|
|
|
|
{
|
2020-07-14 04:12:40 +00:00
|
|
|
CmdlineOptions options{};
|
|
|
|
readCliOptions(argc, argv, options);
|
2019-08-09 02:13:50 +00:00
|
|
|
|
2019-07-17 05:53:51 +00:00
|
|
|
// Read config
|
2020-07-02 04:38:40 +00:00
|
|
|
try {
|
2020-07-14 04:12:40 +00:00
|
|
|
global_config = std::make_shared<Configuration>(options.config_file);
|
2020-07-02 04:38:40 +00:00
|
|
|
}
|
|
|
|
catch (std::exception &e) {
|
|
|
|
global_config = std::make_shared<Configuration>();
|
|
|
|
}
|
2020-07-14 04:12:40 +00:00
|
|
|
global_workqueue = std::make_shared<workqueue>(
|
|
|
|
global_config->workerCount());
|
2020-07-10 07:16:51 +00:00
|
|
|
|
2020-07-05 06:55:46 +00:00
|
|
|
//Create a virtual input device
|
|
|
|
try {
|
|
|
|
virtual_input = std::make_unique<InputDevice>(LOGID_VIRTUAL_INPUT_NAME);
|
|
|
|
} catch(std::system_error& e) {
|
|
|
|
logPrintf(ERROR, "Could not create input device: %s", e.what());
|
2019-07-17 05:53:51 +00:00
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
2019-07-28 06:08:18 +00:00
|
|
|
// Scan devices, create listeners, handlers, etc.
|
2020-06-25 02:27:19 +00:00
|
|
|
device_manager = std::make_unique<DeviceManager>();
|
2020-04-13 02:25:39 +00:00
|
|
|
|
2020-06-24 02:35:37 +00:00
|
|
|
while(!kill_logid) {
|
2020-06-25 02:27:19 +00:00
|
|
|
device_manager_reload.lock();
|
|
|
|
device_manager_reload.unlock();
|
|
|
|
device_manager->run();
|
2020-04-13 02:25:39 +00:00
|
|
|
}
|
2019-07-17 05:53:51 +00:00
|
|
|
|
|
|
|
return EXIT_SUCCESS;
|
2020-04-13 02:25:39 +00:00
|
|
|
}
|