Skip to content

Commit

Permalink
Generating version.h in cmake
Browse files Browse the repository at this point in the history
  • Loading branch information
houmain committed Jan 23, 2021
1 parent 2660f06 commit 912b450
Show file tree
Hide file tree
Showing 11 changed files with 95 additions and 11 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
/_*
/src/_version.h
/CMakeLists.txt.user
12 changes: 12 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
cmake_minimum_required(VERSION 3.0.0)
project(keymapper LANGUAGES CXX)

set(GIT_COMMIT_LAST_TAG "unknown")
find_package(Git)
if(GIT_FOUND)
execute_process(COMMAND ${GIT_EXECUTABLE} describe --tags
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
OUTPUT_VARIABLE GIT_COMMIT_LAST_TAG
OUTPUT_STRIP_TRAILING_WHITESPACE)
endif()
configure_file("src/version.h.in" "${CMAKE_SOURCE_DIR}/src/_version.h")

set(CMAKE_CXX_STANDARD 17)

include_directories(src)
Expand Down Expand Up @@ -53,6 +63,8 @@ if(NOT WIN32)
src/linux/server/main.cpp
src/linux/server/uinput_keyboard.cpp
src/linux/server/uinput_keyboard.h
src/linux/server/Settings.cpp
src/linux/server/Settings.h
)
target_link_libraries(keymapperd usb-1.0 udev)

Expand Down
1 change: 0 additions & 1 deletion src/_version.h

This file was deleted.

4 changes: 4 additions & 0 deletions src/linux/client/Settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ bool interpret_commandline(Settings& settings, int argc, char* argv[]) {
return false;
settings.config_file_path = argv[i];
}
if (argument == "-v" || argument == "--verbose") {
settings.verbose = true;
}
else {
return false;
}
Expand Down Expand Up @@ -41,6 +44,7 @@ void print_help_message(const char* argv0) {
"Usage: %s [-options]\n"
" -c, --config <path> configuration file.\n"
" -u, --update reload configuration file when it changes.\n"
" -v, --verbose enable verbose output.\n"
"\n"
"All Rights Reserved.\n"
"This program comes with absolutely no warranty.\n"
Expand Down
1 change: 1 addition & 0 deletions src/linux/client/Settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
struct Settings {
std::string config_file_path;
bool auto_update_config;
bool verbose;
};

bool interpret_commandline(Settings& settings, int argc, char* argv[]);
Expand Down
43 changes: 43 additions & 0 deletions src/linux/server/Settings.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@

#include "Settings.h"
#include <cstdio>

bool interpret_commandline(Settings& settings, int argc, char* argv[]) {
for (auto i = 1; i < argc; i++) {
const auto argument = std::string(argv[i]);
if (argument == "-v" || argument == "--verbose") {
settings.verbose = true;
}
else {
return false;
}
}
return true;
}

void print_help_message(const char* argv0) {
auto program = std::string(argv0);
if (auto i = program.rfind('/'); i != std::string::npos)
program = program.substr(i + 1);
if (auto i = program.rfind('.'); i != std::string::npos)
program = program.substr(0, i);

const auto version =
#if __has_include("../../_version.h")
# include "../../_version.h"
" ";
#else
"";
#endif

std::printf(
"keymapperd %s(c) 2019-2021 by Albert Kalchmair\n"
"\n"
"Usage: %s [-options]\n"
" -v, --verbose enable verbose output.\n"
"\n"
"All Rights Reserved.\n"
"This program comes with absolutely no warranty.\n"
"See the GNU General Public License, version 3 for details.\n"
"\n", version, program.c_str());
}
10 changes: 10 additions & 0 deletions src/linux/server/Settings.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#pragma once

#include <string>

struct Settings {
bool verbose;
};

bool interpret_commandline(Settings& settings, int argc, char* argv[]);
void print_help_message(const char* arg0);
10 changes: 9 additions & 1 deletion src/linux/server/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include "ipc.h"
#include "GrabbedKeyboards.h"
#include "uinput_keyboard.h"
#include "Settings.h"
#include "runtime/Stage.h"
#include <linux/uinput.h>

Expand All @@ -10,7 +11,14 @@ namespace {
const auto uinput_keyboard_name = "Keymapper";
}

int main() {
int main(int argc, char* argv[]) {
auto settings = Settings{ };

if (!interpret_commandline(settings, argc, argv)) {
print_help_message(argv[0]);
return 1;
}

// wait for client connection loop
for (;;) {
const auto ipc_fd = initialize_ipc(ipc_fifo_filename);
Expand Down
1 change: 1 addition & 0 deletions src/version.h.in
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"${GIT_COMMIT_LAST_TAG}"
11 changes: 8 additions & 3 deletions src/win32/Settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,14 @@ bool interpret_commandline(Settings& settings, int argc, wchar_t* argv[]) {

void print_help_message(const wchar_t* argv0) {
const auto version = std::string(
#include "../_version.h"
);
print(("keymapper " + version + " (c) 2019-2021 by Albert Kalchmair\n"
#if __has_include("../_version.h")
# include "../_version.h"
" ");
#else
"");
#endif

print(("keymapper " + version + "(c) 2019-2021 by Albert Kalchmair\n"
"\n"
"Usage: keymapper [-options]\n"
" -c, --config <path> configuration file.\n"
Expand Down
12 changes: 6 additions & 6 deletions src/win32/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,19 +103,19 @@ void reuse_buffer(KeySequence&& buffer) {
}

int WINAPI wWinMain(HINSTANCE instance, HINSTANCE, LPWSTR, int) {
LimitSingleInstance single_instance("Global\\{658914E7-CCA6-4425-89FF-EF4A13B75F31}");
if (single_instance.is_another_instance_running())
return 1;

SetPriorityClass(GetCurrentProcess(), HIGH_PRIORITY_CLASS);

g_settings.config_file_path = config_filename;

if (!interpret_commandline(g_settings, __argc, __wargv)) {
print_help_message(__wargv[0]);
return 1;
}

LimitSingleInstance single_instance("Global\\{658914E7-CCA6-4425-89FF-EF4A13B75F31}");
if (single_instance.is_another_instance_running())
return 1;

SetPriorityClass(GetCurrentProcess(), HIGH_PRIORITY_CLASS);

// load initial configuration
g_config_file = ConfigFile(g_settings.config_file_path);
if (!g_config_file.update())
Expand Down

0 comments on commit 912b450

Please sign in to comment.