From 0531e9ccc6750687fb104764c4cc1fa5807c9552 Mon Sep 17 00:00:00 2001 From: houmain Date: Thu, 9 Mar 2023 19:09:37 +0100 Subject: [PATCH] Allow to disabled notifications using --no-tray --- keymapper.conf | 0 src/client/Settings.cpp | 6 +----- src/client/linux/main.cpp | 14 ++++++++------ src/client/windows/main.cpp | 20 ++++++++++---------- src/common/output.cpp | 5 +++-- src/common/output.h | 1 + src/server/linux/main.cpp | 4 ---- src/server/windows/main.cpp | 11 ++++++----- 8 files changed, 29 insertions(+), 32 deletions(-) mode change 100755 => 100644 keymapper.conf diff --git a/keymapper.conf b/keymapper.conf old mode 100755 new mode 100644 diff --git a/src/client/Settings.cpp b/src/client/Settings.cpp index 20b671c7..95a9d6a8 100644 --- a/src/client/Settings.cpp +++ b/src/client/Settings.cpp @@ -34,11 +34,9 @@ bool interpret_commandline(Settings& settings, int argc, char* argv[]) { else if (argument == T("--check")) { settings.check_config = true; } -#if defined(_WIN32) else if (argument == T("--no-tray")) { settings.no_tray_icon = true; } -#endif else { return false; } @@ -54,9 +52,7 @@ void print_help_message() { " -c, --config configuration file.\n" " -u, --update reload configuration file when it changes.\n" " -v, --verbose enable verbose output.\n" -#if defined(_WIN32) - " --no-tray do not show tray icon.\n" -#endif + " --no-tray do not show tray icon/notifications.\n" " --check check the config for errors.\n" " -h, --help print this help.\n" "\n" diff --git a/src/client/linux/main.cpp b/src/client/linux/main.cpp index aac386bd..1d5165f6 100644 --- a/src/client/linux/main.cpp +++ b/src/client/linux/main.cpp @@ -144,13 +144,13 @@ namespace { } return std::filesystem::absolute(filename, error); } -} // namespace -void show_notification(const char* message) { - auto ss = std::stringstream(); - ss << "notify-send -a keymapper keymapper \"" << message << "\""; - std::system(ss.str().c_str()); -} + void show_notification(const char* message) { + auto ss = std::stringstream(); + ss << "notify-send -a keymapper keymapper \"" << message << "\""; + std::system(ss.str().c_str()); + } +} // namespace int main(int argc, char* argv[]) { if (!interpret_commandline(g_settings, argc, argv)) { @@ -158,6 +158,8 @@ int main(int argc, char* argv[]) { return 1; } g_verbose_output = g_settings.verbose; + if (!g_settings.no_tray_icon) + g_show_notification = &show_notification; g_settings.config_file_path = resolve_config_file_path(std::move(g_settings.config_file_path)); diff --git a/src/client/windows/main.cpp b/src/client/windows/main.cpp index b8e211ae..ac101c14 100644 --- a/src/client/windows/main.cpp +++ b/src/client/windows/main.cpp @@ -338,24 +338,24 @@ namespace { } return std::filesystem::absolute(filename, error); } + + void show_notification(const char* message_) { + auto& icon = g_tray_icon; + const auto message = utf8_to_wide(message_); + icon.uFlags = NIF_INFO; + lstrcpyW(icon.szInfo, message.c_str()); + Shell_NotifyIconW(NIM_MODIFY, &icon); + } } // namespace -void show_notification(const char* message_) { - if (g_settings.no_tray_icon) - return; - auto& icon = g_tray_icon; - const auto message = utf8_to_wide(message_); - icon.uFlags = NIF_INFO; - lstrcpyW(icon.szInfo, message.c_str()); - Shell_NotifyIconW(NIM_MODIFY, &icon); -} - int WINAPI wWinMain(HINSTANCE instance, HINSTANCE, LPWSTR, int) { if (!interpret_commandline(g_settings, __argc, __wargv)) { print_help_message(); return 1; } g_verbose_output = g_settings.verbose; + if (!g_settings.no_tray_icon) + g_show_notification = &show_notification; g_settings.config_file_path = resolve_config_file_path(g_settings.config_file_path); diff --git a/src/common/output.cpp b/src/common/output.cpp index 57c65dde..a3424c94 100644 --- a/src/common/output.cpp +++ b/src/common/output.cpp @@ -6,6 +6,7 @@ #include bool g_verbose_output = false; +void(*g_show_notification)(const char* message); const auto about_header_str = std::string( #if __has_include("_version.h") @@ -54,8 +55,8 @@ namespace { std::fputc('\n', stdout); std::fflush(stdout); - if (notify) - show_notification(buffer.data()); + if (notify && g_show_notification) + g_show_notification(buffer.data()); } } // namespace diff --git a/src/common/output.h b/src/common/output.h index 97750f5a..59f3b516 100644 --- a/src/common/output.h +++ b/src/common/output.h @@ -5,6 +5,7 @@ extern const char* about_footer; extern bool g_verbose_output; extern bool g_output_color; +extern void(*g_show_notification)(const char* message); void message(const char* format, ...); void error(const char* format, ...); diff --git a/src/server/linux/main.cpp b/src/server/linux/main.cpp index cbb20f8d..66ba8ac7 100644 --- a/src/server/linux/main.cpp +++ b/src/server/linux/main.cpp @@ -258,10 +258,6 @@ namespace { } } // namespace -void show_notification(const char* message) { - // don't show notifications -} - int main(int argc, char* argv[]) { auto settings = Settings{ }; diff --git a/src/server/windows/main.cpp b/src/server/windows/main.cpp index 2632e754..f81359f1 100644 --- a/src/server/windows/main.cpp +++ b/src/server/windows/main.cpp @@ -486,12 +486,12 @@ namespace { } return DefWindowProcW(window, message, wparam, lparam); } -} // namespace -void show_notification(const char* message) { - MessageBoxA(nullptr, message, "Keymapper", - MB_ICONWARNING | MB_TOPMOST); -} + void show_notification(const char* message) { + MessageBoxA(nullptr, message, "Keymapper", + MB_ICONWARNING | MB_TOPMOST); + } +} // namespace int WINAPI wWinMain(HINSTANCE instance, HINSTANCE, LPWSTR, int) { auto settings = Settings{ }; @@ -499,6 +499,7 @@ int WINAPI wWinMain(HINSTANCE instance, HINSTANCE, LPWSTR, int) { print_help_message(); return 1; } + g_show_notification = &show_notification; const auto single_instance = LimitSingleInstance( "Global\\{E28F6E4E-A892-47ED-A6C2-DAC6AB8CCBFC}");