From 1a8c17fb50798d1a64e636f0f33ea91edbc04fac Mon Sep 17 00:00:00 2001 From: Reinder Feenstra Date: Thu, 26 May 2022 09:59:09 +0200 Subject: [PATCH] trayicon: added allow client server restart/shutdown options to popupmenu --- server/src/os/windows/trayicon.cpp | 66 ++++++++++++++++++++++++++++++ server/src/os/windows/trayicon.hpp | 15 +++++++ 2 files changed, 81 insertions(+) diff --git a/server/src/os/windows/trayicon.cpp b/server/src/os/windows/trayicon.cpp index 3c70a413..c2ec836b 100644 --- a/server/src/os/windows/trayicon.cpp +++ b/server/src/os/windows/trayicon.cpp @@ -27,6 +27,7 @@ #include "consolewindow.hpp" #include "../../core/eventloop.hpp" #include "../../traintastic/traintastic.hpp" +#include "../../utils/setthreadname.hpp" namespace Windows { @@ -50,6 +51,8 @@ void TrayIcon::remove() void TrayIcon::run() { + setThreadName("trayicon"); + const LPCSTR windowClassName = "TraintasticServerTrayIcon"; // register window class: @@ -73,6 +76,9 @@ void TrayIcon::run() s_menu = CreatePopupMenu(); menuAddItem(MenuItem::ShowHideConsole, "Show/hide console", hasConsoleWindow()); menuAddSeperator(); + menuAddItem(MenuItem::AllowClientServerRestart, "Allow client to restart server"); + menuAddItem(MenuItem::AllowClientServerShutdown, "Allow client to shutdown server"); + menuAddSeperator(); menuAddItem(MenuItem::Restart, "Restart"); menuAddItem(MenuItem::Shutdown, "Shutdown"); @@ -97,6 +103,9 @@ void TrayIcon::run() Shell_NotifyIcon(NIM_ADD, ¬ifyIconData); + // Get settings: + EventLoop::call(getSettings); + // message loop: while(true) { @@ -166,8 +175,47 @@ LRESULT CALLBACK TrayIcon::windowProc(_In_ HWND hWnd, _In_ UINT uMsg, _In_ WPARA case MenuItem::ShowHideConsole: setConsoleWindowVisible(!isConsoleWindowVisible()); return 0; + + case MenuItem::AllowClientServerRestart: + { + bool value; + { + std::lock_guard lock{s_settings.mutex}; + value = !s_settings.allowClientServerRestart; + } + EventLoop::call( + [value]() + { + Traintastic::instance->settings->allowClientServerRestart = value; + getSettings(); + }); + break; + } + case MenuItem::AllowClientServerShutdown: + { + bool value; + { + std::lock_guard lock{s_settings.mutex}; + value = !s_settings.allowClientServerShutdown; + } + EventLoop::call( + [value]() + { + Traintastic::instance->settings->allowClientServerShutdown = value; + getSettings(); + }); + break; + } } break; + + case WM_TRAINTASTIC_SETTINGS: + { + std::lock_guard lock{s_settings.mutex}; + menuSetItemChecked(MenuItem::AllowClientServerRestart, s_settings.allowClientServerRestart); + menuSetItemChecked(MenuItem::AllowClientServerShutdown, s_settings.allowClientServerShutdown); + break; + } } return DefWindowProc(hWnd, uMsg, wParam, lParam); } @@ -197,4 +245,22 @@ void TrayIcon::menuAddSeperator() InsertMenuItem(s_menu, GetMenuItemCount(s_menu), TRUE, &item); } +void TrayIcon::menuSetItemChecked(MenuItem id, bool checked) +{ + assert(s_menu); + CheckMenuItem(s_menu, static_cast(id), checked ? MF_CHECKED : MF_UNCHECKED); +} + +void TrayIcon::getSettings() +{ + assert(isEventLoopThread()); + const auto& settings = *Traintastic::instance->settings.value(); + { + std::lock_guard lock{s_settings.mutex}; + s_settings.allowClientServerRestart = settings.allowClientServerRestart; + s_settings.allowClientServerShutdown = settings.allowClientServerShutdown; + } + PostMessage(s_window, WM_TRAINTASTIC_SETTINGS, 0, 0); +} + } diff --git a/server/src/os/windows/trayicon.hpp b/server/src/os/windows/trayicon.hpp index f5154699..bfdcb958 100644 --- a/server/src/os/windows/trayicon.hpp +++ b/server/src/os/windows/trayicon.hpp @@ -25,6 +25,7 @@ #include #include +#include #include #define _WINSOCKAPI_ // prevent windows.h including winsock.h #include @@ -43,19 +44,33 @@ class TrayIcon Shutdown = 1, Restart = 2, ShowHideConsole = 3, + AllowClientServerRestart = 4, + AllowClientServerShutdown = 5, + }; + + struct TraintasticSettings + { + std::mutex mutex; + bool allowClientServerRestart; + bool allowClientServerShutdown; }; static constexpr UINT WM_NOTIFYICON_CALLBACK = WM_USER + 1; + static constexpr UINT WM_TRAINTASTIC_SETTINGS = WM_USER + 2; static std::unique_ptr s_thread; static HWND s_window; static HMENU s_menu; + inline static TraintasticSettings s_settings = {{}, false, false}; static void run(); static LRESULT CALLBACK windowProc(_In_ HWND hWnd, _In_ UINT uMsg, _In_ WPARAM wParam, _In_ LPARAM lParam); static void menuAddItem(MenuItem id, const LPSTR text, bool enabled = true); static void menuAddSeperator(); + static void menuSetItemChecked(MenuItem id, bool checked); + + static void getSettings(); public: static void add();