diff options
| author | Fuwn <[email protected]> | 2023-05-06 02:26:32 -0700 |
|---|---|---|
| committer | Fuwn <[email protected]> | 2023-05-06 02:26:32 -0700 |
| commit | a1a7da24b94ec83ad4e5678e5355fbb6f85a8ee8 (patch) | |
| tree | ac58b4ce9e433fade9acd456052f485709471605 | |
| parent | chore: bump to 1.1.4 (diff) | |
| download | soyuz-a1a7da24b94ec83ad4e5678e5355fbb6f85a8ee8.tar.xz soyuz-a1a7da24b94ec83ad4e5678e5355fbb6f85a8ee8.zip | |
docs: bump copyright headers
| -rw-r--r-- | include/soyuz/library.hh | 28 | ||||
| -rw-r--r-- | include/soyuz/resource.hh | 11 | ||||
| -rw-r--r-- | include/soyuz/soyuz.hh | 8 | ||||
| -rw-r--r-- | include/soyuz/tray.hh | 13 | ||||
| -rw-r--r-- | include/soyuz/windows.hh | 53 | ||||
| -rw-r--r-- | resource/resource.rc | 2 | ||||
| -rw-r--r-- | soyuz/library.cc | 150 | ||||
| -rw-r--r-- | soyuz/soyuz.cc | 99 | ||||
| -rw-r--r-- | soyuz/tray.cc | 162 |
9 files changed, 213 insertions, 313 deletions
diff --git a/include/soyuz/library.hh b/include/soyuz/library.hh index 5074d2a..c43fc22 100644 --- a/include/soyuz/library.hh +++ b/include/soyuz/library.hh @@ -1,31 +1,22 @@ -// Copyright (C) 2021-2022 Fuwn +// Copyright (C) 2021-2023 Fuwn // SPDX-License-Identifier: GPL-3.0-only -/** - * @file library.hh - * @author Fuwn - * @date 2021. August. 18. - */ - #ifndef SOYUZ_LIBRARY_HH #define SOYUZ_LIBRARY_HH #pragma once -#include <string> #include <Windows.h> +#include <string> #include <vector> + #define NT_SUCCESS(status) (status >= 0) #define STATUS_INFO_LENGTH_MISMATCH ((NTSTATUS)0xC0000004L) namespace soyuz { const std::string numbers_as_string[] = { - "zero", - "one", - "two", - "three", - "four", + "zero", "one", "two", "three", "four", }; enum log_level { @@ -37,14 +28,11 @@ enum log_level { }; struct log_t { - log_level level; + log_level level; std::string value; - log_t( - log_level level, - std::string value - ) : level(level), value(std::move(value)) - {} + log_t(log_level level, std::string value) + : level(level), value(std::move(value)) {} [[nodiscard]] auto to_colorref() const -> COLORREF; }; @@ -59,6 +47,6 @@ auto exit(int) -> void; // https://stackoverflow.com/a/10467633/14452787 auto current_date_time() -> std::string; -} +} // namespace soyuz #endif // SOYUZ_LIBRARY_HH diff --git a/include/soyuz/resource.hh b/include/soyuz/resource.hh index 4caebbf..1efc8a1 100644 --- a/include/soyuz/resource.hh +++ b/include/soyuz/resource.hh @@ -1,18 +1,13 @@ -// Copyright (C) 2021-2022 Fuwn +// Copyright (C) 2021-2023 Fuwn // SPDX-License-Identifier: GPL-3.0-only -/** - * @file resource.hh - * @author Fuwn - * @date 2021. August. 18. - */ - #ifndef SOYUZ_RESOURCE_HH #define SOYUZ_RESOURCE_HH #pragma once -#include <shellapi.h> #include <Windows.h> +#include <shellapi.h> + #define ICO1 101 #define ID_TRAY_APP_ICON 1001 diff --git a/include/soyuz/soyuz.hh b/include/soyuz/soyuz.hh index 42cc2a4..ad4b19f 100644 --- a/include/soyuz/soyuz.hh +++ b/include/soyuz/soyuz.hh @@ -1,12 +1,6 @@ -// Copyright (C) 2021-2022 Fuwn +// Copyright (C) 2021-2023 Fuwn // SPDX-License-Identifier: GPL-3.0-only -/** - * @file soyuz.hh - * @author Fuwn - * @date 2021. August. 18. - */ - #ifndef SOYUZ_SOYUZ_HH #define SOYUZ_SOYUZ_HH #pragma once diff --git a/include/soyuz/tray.hh b/include/soyuz/tray.hh index 178600d..09a3d56 100644 --- a/include/soyuz/tray.hh +++ b/include/soyuz/tray.hh @@ -1,19 +1,14 @@ -// Copyright (C) 2021-2022 Fuwn +// Copyright (C) 2021-2023 Fuwn // SPDX-License-Identifier: GPL-3.0-only -/** - * @file tray.hh - * @author Fuwn - * @date 2021. August. 18. - */ - #ifndef SOYUZ_TRAY_HH #define SOYUZ_TRAY_HH #pragma once +#include <Windows.h> #include <string> #include <vector> -#include <Windows.h> + #include <soyuz/library.hh> @@ -27,6 +22,6 @@ namespace soyuz { [[maybe_unused]] auto log(const std::string &) -> void; auto log(log_level, const std::string &) -> void; -} +} // namespace soyuz #endif // SOYUZ_TRAY_HH diff --git a/include/soyuz/windows.hh b/include/soyuz/windows.hh index 1acfaac..073e113 100644 --- a/include/soyuz/windows.hh +++ b/include/soyuz/windows.hh @@ -1,45 +1,34 @@ -// Copyright (C) 2021-2022 Fuwn +// Copyright (C) 2021-2023 Fuwn // SPDX-License-Identifier: GPL-3.0-only -/** - * @file windows.hh - * @author Fuwn - * @date 2021. August. 18. - */ - #ifndef SOYUZ_WINDOWS_HH #define SOYUZ_WINDOWS_HH #pragma once #include <Windows.h> -enum PROCESSINFOCLASS { - ProcessHandleInformation = 51 -}; +enum PROCESSINFOCLASS { ProcessHandleInformation = 51 }; typedef struct _PROCESS_HANDLE_TABLE_ENTRY_INFO { - HANDLE HandleValue; + HANDLE HandleValue; ULONG_PTR HandleCount; ULONG_PTR PointerCount; - ULONG GrantedAccess; - ULONG ObjectTypeIndex; - ULONG HandleAttributes; - ULONG Reserved; -} PROCESS_HANDLE_TABLE_ENTRY_INFO, * PPROCESS_HANDLE_TABLE_ENTRY_INFO; + ULONG GrantedAccess; + ULONG ObjectTypeIndex; + ULONG HandleAttributes; + ULONG Reserved; +} PROCESS_HANDLE_TABLE_ENTRY_INFO, *PPROCESS_HANDLE_TABLE_ENTRY_INFO; typedef struct _PROCESS_HANDLE_SNAPSHOT_INFORMATION { - ULONG_PTR NumberOfHandles; - ULONG_PTR Reserved; + ULONG_PTR NumberOfHandles; + ULONG_PTR Reserved; PROCESS_HANDLE_TABLE_ENTRY_INFO Handles[1]; -} PROCESS_HANDLE_SNAPSHOT_INFORMATION, * PPROCESS_HANDLE_SNAPSHOT_INFORMATION; +} PROCESS_HANDLE_SNAPSHOT_INFORMATION, *PPROCESS_HANDLE_SNAPSHOT_INFORMATION; extern "C" NTSTATUS NTAPI NtQueryInformationProcess( - _In_ HANDLE ProcessHandle, - _In_ PROCESSINFOCLASS ProcessInformationClass, - _Out_writes_bytes_(ProcessInformationLength) PVOID ProcessInformation, - _In_ ULONG ProcessInformationLength, - _Out_opt_ PULONG ReturnLength -); + _In_ HANDLE ProcessHandle, _In_ PROCESSINFOCLASS ProcessInformationClass, + _Out_writes_bytes_(ProcessInformationLength) PVOID ProcessInformation, + _In_ ULONG ProcessInformationLength, _Out_opt_ PULONG ReturnLength); typedef enum _OBJECT_INFORMATION_CLASS { ObjectNameInformation = 1 @@ -48,19 +37,17 @@ typedef enum _OBJECT_INFORMATION_CLASS { typedef struct _UNICODE_STRING { USHORT Length; USHORT MaximumLength; - PWSTR Buffer; + PWSTR Buffer; } UNICODE_STRING; typedef struct _OBJECT_NAME_INFORMATION { UNICODE_STRING Name; -} OBJECT_NAME_INFORMATION, * POBJECT_NAME_INFORMATION; +} OBJECT_NAME_INFORMATION, *POBJECT_NAME_INFORMATION; extern "C" NTSTATUS NTAPI NtQueryObject( - _In_opt_ HANDLE Handle, - _In_ OBJECT_INFORMATION_CLASS ObjectInformationClass, - _Out_writes_bytes_opt_(ObjectInformationLength) PVOID ObjectInformation, - _In_ ULONG ObjectInformationLength, - _Out_opt_ PULONG ReturnLength -); + _In_opt_ HANDLE Handle, + _In_ OBJECT_INFORMATION_CLASS ObjectInformationClass, + _Out_writes_bytes_opt_(ObjectInformationLength) PVOID ObjectInformation, + _In_ ULONG ObjectInformationLength, _Out_opt_ PULONG ReturnLength); #endif // SOYUZ_WINDOWS_HH diff --git a/resource/resource.rc b/resource/resource.rc index f44f6ca..9f80aa2 100644 --- a/resource/resource.rc +++ b/resource/resource.rc @@ -1,4 +1,4 @@ -// Copyright (C) 2021-2022 Fuwn +// Copyright (C) 2021-2023 Fuwn // SPDX-License-Identifier: GPL-3.0-only #include "soyuz/resource.hh" diff --git a/soyuz/library.cc b/soyuz/library.cc index a39f4d7..5d10f83 100644 --- a/soyuz/library.cc +++ b/soyuz/library.cc @@ -1,20 +1,15 @@ -// Copyright (C) 2021-2022 Fuwn +// Copyright (C) 2021-2023 Fuwn // SPDX-License-Identifier: GPL-3.0-only -/** - * @file library.cc - * @author Fuwn - * @date 2021. August. 18. - */ - #include <soyuz/library.hh> +#include <Windows.h> #include <cstdio> #include <fmt/format.h> #include <fmt/os.h> #include <memory> #include <string> -#include <Windows.h> + #include <soyuz/soyuz.hh> #include <soyuz/tray.hh> @@ -60,51 +55,41 @@ auto find_lunar() -> DWORD { } auto delete_handle(DWORD pid) -> int { - HANDLE lunar = OpenProcess( - PROCESS_QUERY_INFORMATION | PROCESS_DUP_HANDLE, - FALSE, - pid - ); + HANDLE lunar = + OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_DUP_HANDLE, FALSE, pid); ULONG size = 1 << 10; - std::unique_ptr<BYTE []> buffer; + std::unique_ptr<BYTE[]> buffer; if (!lunar) { - soyuz::log( - soyuz::log_level::LOG_LEVEL_WARN, - fmt::format( - "could not open handle to lunar client: {}", - GetLastError() - ) - ); + soyuz::log(soyuz::log_level::LOG_LEVEL_WARN, + fmt::format("could not open handle to lunar client: {}", + GetLastError())); return 1; } for (;;) { - buffer = std::make_unique<BYTE []>(size); + buffer = std::make_unique<BYTE[]>(size); - NTSTATUS status = NtQueryInformationProcess( - lunar, - ProcessHandleInformation, - buffer.get(), - size, - &size - ); + NTSTATUS status = NtQueryInformationProcess(lunar, ProcessHandleInformation, + buffer.get(), size, &size); - if (NT_SUCCESS(status)) { break; } - if (status == STATUS_INFO_LENGTH_MISMATCH) { size += 1 << 10; continue; } + if (NT_SUCCESS(status)) { + break; + } + if (status == STATUS_INFO_LENGTH_MISMATCH) { + size += 1 << 10; + continue; + } - soyuz::log( - soyuz::log_level::LOG_LEVEL_DEBUG, - "could not enumerate handle, skipping" - ); + soyuz::log(soyuz::log_level::LOG_LEVEL_DEBUG, + "could not enumerate handle, skipping"); return 1; } - auto *info = reinterpret_cast<PROCESS_HANDLE_SNAPSHOT_INFORMATION *>( - buffer.get() - ); + auto *info = + reinterpret_cast<PROCESS_HANDLE_SNAPSHOT_INFORMATION *>(buffer.get()); for (ULONG i = 0; i < info->NumberOfHandles; ++i) { HANDLE h = info->Handles[i].HandleValue; @@ -113,27 +98,19 @@ auto delete_handle(DWORD pid) -> int { WCHAR target_name[256]; DWORD session_id; - if (!DuplicateHandle( - lunar, - h, - GetCurrentProcess(), - &target, - 0, - FALSE, - DUPLICATE_SAME_ACCESS - )) { continue; } - - NTSTATUS status = NtQueryObject( - target, - ObjectNameInformation, - name_buffer, - sizeof(name_buffer), - nullptr - ); + if (!DuplicateHandle(lunar, h, GetCurrentProcess(), &target, 0, FALSE, + DUPLICATE_SAME_ACCESS)) { + continue; + } + + NTSTATUS status = NtQueryObject(target, ObjectNameInformation, name_buffer, + sizeof(name_buffer), nullptr); CloseHandle(target); - if (!NT_SUCCESS(status)) { continue; } + if (!NT_SUCCESS(status)) { + continue; + } ProcessIdToSessionId(pid, &session_id); swprintf_s(target_name, DISCORD_IPC_NAMED_PIPE_NAME); @@ -142,24 +119,13 @@ auto delete_handle(DWORD pid) -> int { auto *name = reinterpret_cast<UNICODE_STRING *>(name_buffer); if (name->Buffer && _wcsnicmp(name->Buffer, target_name, length) == 0) { - soyuz::log( - soyuz::log_level::LOG_LEVEL_INFO, - "found lunar client's discord ipc named pipe" - ); - DuplicateHandle( - lunar, - h, - GetCurrentProcess(), - &target, - 0, - FALSE, - DUPLICATE_CLOSE_SOURCE - ); + soyuz::log(soyuz::log_level::LOG_LEVEL_INFO, + "found lunar client's discord ipc named pipe"); + DuplicateHandle(lunar, h, GetCurrentProcess(), &target, 0, FALSE, + DUPLICATE_CLOSE_SOURCE); CloseHandle(target); - soyuz::log( - soyuz::log_level::LOG_LEVEL_INFO, - "closed lunar client's discord ipc named pipe" - ); + soyuz::log(soyuz::log_level::LOG_LEVEL_INFO, + "closed lunar client's discord ipc named pipe"); return 0; } @@ -173,12 +139,12 @@ auto write_log_file(const std::string &message) -> void { } auto init_log_file() -> void { -// if (!log_file.is_open()) { -// soyuz::log("could not open 'soyuz.log'"); -// soyuz::log("proceeding without logging to file"); -// -// return; -// } + // if (!log_file.is_open()) { + // soyuz::log("could not open 'soyuz.log'"); + // soyuz::log("proceeding without logging to file"); + // + // return; + // } soyuz::log(soyuz::log_level::LOG_LEVEL_DEBUG, "opened 'soyuz.log'"); } @@ -208,13 +174,25 @@ auto current_date_time() -> std::string { auto log_t::to_colorref() const -> COLORREF { switch (this->level) { - case LOG_LEVEL_TRACE: { return 0x00FF0000; } // blue - case LOG_LEVEL_DEBUG: { return 0x0000FF00; } // green - case LOG_LEVEL_INFO: { return 0x00000000; } // black - case LOG_LEVEL_WARN: { return 0x000080FF; } // orange - case LOG_LEVEL_ERROR: { return 0x000000FF; } // red - default: { return 0x00000000; } // black + case LOG_LEVEL_TRACE: { + return 0x00FF0000; + } // blue + case LOG_LEVEL_DEBUG: { + return 0x0000FF00; + } // green + case LOG_LEVEL_INFO: { + return 0x00000000; + } // black + case LOG_LEVEL_WARN: { + return 0x000080FF; + } // orange + case LOG_LEVEL_ERROR: { + return 0x000000FF; + } // red + default: { + return 0x00000000; + } // black } } -} +} // namespace soyuz diff --git a/soyuz/soyuz.cc b/soyuz/soyuz.cc index d8a9c8e..9c18ff7 100644 --- a/soyuz/soyuz.cc +++ b/soyuz/soyuz.cc @@ -1,17 +1,12 @@ -// Copyright (C) 2021-2022 Fuwn +// Copyright (C) 2021-2023 Fuwn // SPDX-License-Identifier: GPL-3.0-only -/** - * @file soyuz.cc - * @author Fuwn - * @date 2021. August. 18. - */ - #pragma comment(lib, "ntdll.lib") +#include <Windows.h> #include <fmt/format.h> #include <thread> -#include <Windows.h> + #include <soyuz/library.hh> #include <soyuz/resource.hh> @@ -45,22 +40,14 @@ int WINAPI WinMain(HINSTANCE instance, HINSTANCE, LPSTR, int show) { wincl.cbWndExtra = 0; wincl.hbrBackground = (HBRUSH)(CreateSolidBrush(RGB(255, 255, 255))); - if (!RegisterClassEx(&wincl)) { return 0; } - - window = CreateWindowEx( - 0, - class_name, - class_name, - WS_OVERLAPPEDWINDOW ^ WS_THICKFRAME, - CW_USEDEFAULT, - CW_USEDEFAULT, - 544, - 375, - HWND_DESKTOP, - nullptr, - instance, - nullptr - ); + if (!RegisterClassEx(&wincl)) { + return 0; + } + + window = CreateWindowEx(0, class_name, class_name, + WS_OVERLAPPEDWINDOW ^ WS_THICKFRAME, CW_USEDEFAULT, + CW_USEDEFAULT, 544, 375, HWND_DESKTOP, nullptr, + instance, nullptr); InitNotifyIconData(); ShowWindow(window, show); @@ -70,76 +57,55 @@ int WINAPI WinMain(HINSTANCE instance, HINSTANCE, LPSTR, int show) { * * https://medium.com/@vgasparyan1995/a-new-thread-in-c-20-jthread-ebd121ae8906 */ - std::jthread soyuz {[](const std::stop_token &stop) -> void { + std::jthread soyuz{[](const std::stop_token &stop) -> void { // Check if Lunar Client is open, if not; close Soyuz DWORD pid = soyuz::find_lunar(); if (pid == 0 || pid == 3435973836) { - soyuz::log( - soyuz::log_level::LOG_LEVEL_ERROR, - "could not locate lunar client" - ); - soyuz::log( - soyuz::log_level::LOG_LEVEL_WARN, - "this window will close in five seconds" - ); + soyuz::log(soyuz::log_level::LOG_LEVEL_ERROR, + "could not locate lunar client"); + soyuz::log(soyuz::log_level::LOG_LEVEL_WARN, + "this window will close in five seconds"); for (int i = 4; i > -1; --i) { std::this_thread::sleep_for(std::chrono::seconds(1)); - soyuz::log( - soyuz::log_level::LOG_LEVEL_WARN, - fmt::format( - "> {} second{}", - soyuz::numbers_as_string[i], - i != 1 ? "s" : "" - ) - ); + soyuz::log(soyuz::log_level::LOG_LEVEL_WARN, + fmt::format("> {} second{}", soyuz::numbers_as_string[i], + i != 1 ? "s" : "")); } soyuz::exit(1); } - soyuz::log( - soyuz::log_level::LOG_LEVEL_INFO, - fmt::format( - "located lunar client: pid {}", - pid - ) - ); // GetLastError() + soyuz::log(soyuz::log_level::LOG_LEVEL_INFO, + fmt::format("located lunar client: pid {}", + pid)); // GetLastError() soyuz::log(soyuz::log_level::LOG_LEVEL_INFO, "hooked lunar client"); - soyuz::log( - soyuz::log_level::LOG_LEVEL_INFO, - "you may now close this window" - ); + soyuz::log(soyuz::log_level::LOG_LEVEL_INFO, + "you may now close this window"); while (!stop.stop_requested()) { /** - * Check if Lunar Client is open before every `delete_handle` run, if not; timeout + * Check if Lunar Client is open before every `delete_handle` run, if not; + * timeout * * Thanks, @LorenzoHanssens (#1) */ pid = soyuz::find_lunar(); if (pid == 0 || pid == 3435973836) { - soyuz::log( - soyuz::log_level::LOG_LEVEL_WARN, - "could not locate lunar client, waiting 10 seconds" - ); + soyuz::log(soyuz::log_level::LOG_LEVEL_WARN, + "could not locate lunar client, waiting 10 seconds"); std::this_thread::sleep_for(std::chrono::seconds(10)); } // If Lunar Client **is** open, close it's Discord IPC Named Pipe if (soyuz::delete_handle(pid) == 1) { - soyuz::log( - soyuz::log_level::LOG_LEVEL_WARN, - "unable to close lunar client's discord ipc named pipe," - ); - soyuz::log( - soyuz::log_level::LOG_LEVEL_WARN, - "> waiting 10 seconds" - ); + soyuz::log(soyuz::log_level::LOG_LEVEL_WARN, + "unable to close lunar client's discord ipc named pipe,"); + soyuz::log(soyuz::log_level::LOG_LEVEL_WARN, "> waiting 10 seconds"); std::this_thread::sleep_for(std::chrono::seconds(10)); } @@ -152,7 +118,8 @@ int WINAPI WinMain(HINSTANCE instance, HINSTANCE, LPSTR, int show) { } soyuz::log(soyuz::log_level::LOG_LEVEL_INFO, "requesting exit"); - soyuz.request_stop(); soyuz.detach(); + soyuz.request_stop(); + soyuz.detach(); soyuz::log(soyuz::log_level::LOG_LEVEL_INFO, "exiting"); soyuz::close_log_file(); diff --git a/soyuz/tray.cc b/soyuz/tray.cc index 6d10279..f688868 100644 --- a/soyuz/tray.cc +++ b/soyuz/tray.cc @@ -1,12 +1,6 @@ -// Copyright (C) 2021-2022 Fuwn +// Copyright (C) 2021-2023 Fuwn // SPDX-License-Identifier: GPL-3.0-only -/** - * @file tray.cc - * @author Fuwn - * @date 2021. August. 18. - */ - #include <soyuz/tray.hh> #include <fmt/format.h> @@ -24,12 +18,8 @@ TCHAR tip[64] = TEXT(WINDOW_TRAY_NAME); char class_name[] = WINDOW_TRAY_NAME; std::vector<soyuz::log_t> logs; -auto CALLBACK WindowProcedure( - HWND hwnd, - UINT message, - WPARAM wParam, - LPARAM lParam -) -> LRESULT { +auto CALLBACK WindowProcedure(HWND hwnd, UINT message, WPARAM wParam, + LPARAM lParam) -> LRESULT { if (message == WM_TASKBAR && !IsWindowVisible(window)) { minimize(); @@ -37,89 +27,91 @@ auto CALLBACK WindowProcedure( } switch (message) { - case WM_ACTIVATE: { Shell_NotifyIcon(NIM_ADD, &data); } break; - - case WM_PAINT: { - PAINTSTRUCT ps; - RECT rect; - HDC hdc = BeginPaint(window, &ps); - int height = 5; - - GetClientRect(hwnd, &rect); - - for (soyuz::log_t &log : logs) { - SetTextColor(hdc, log.to_colorref()); - TextOut( - hdc, - 5, - height, - log.value.c_str(), - (int)strlen(log.value.c_str()) - ); - - height += 20; - } + case WM_ACTIVATE: { + Shell_NotifyIcon(NIM_ADD, &data); + } break; - EndPaint(window, &ps); - } break; + case WM_PAINT: { + PAINTSTRUCT ps; + RECT rect; + HDC hdc = BeginPaint(window, &ps); + int height = 5; - case WM_CREATE: { - ShowWindow(window, SW_HIDE); + GetClientRect(hwnd, &rect); - menu = CreatePopupMenu(); + for (soyuz::log_t &log : logs) { + SetTextColor(hdc, log.to_colorref()); + TextOut(hdc, 5, height, log.value.c_str(), + (int)strlen(log.value.c_str())); - AppendMenu(menu, MF_STRING, ID_TRAY_EXIT, TEXT("Exit Soyuz")); - } break; + height += 20; + } - case WM_SYSCOMMAND: - switch(wParam & 0xFFF0) { - case SC_MINIMIZE: - case SC_CLOSE: { minimize(); return 0; } // break; - } break; + EndPaint(window, &ps); + } break; - case WM_SYSICON: { - if (wParam == ID_TRAY_APP_ICON) { SetForegroundWindow(window); } + case WM_CREATE: { + ShowWindow(window, SW_HIDE); - if (lParam == WM_LBUTTONUP) { - restore(); - } else if (lParam == WM_RBUTTONDOWN) { - POINT curPoint; + menu = CreatePopupMenu(); - GetCursorPos(&curPoint); - SetForegroundWindow(window); + AppendMenu(menu, MF_STRING, ID_TRAY_EXIT, TEXT("Exit Soyuz")); + } break; - UINT clicked = TrackPopupMenu( - menu, - TPM_RETURNCMD | TPM_NONOTIFY, - curPoint.x, - curPoint.y, - 0, - hwnd, - nullptr - ); + case WM_SYSCOMMAND: + switch (wParam & 0xFFF0) { + case SC_MINIMIZE: + case SC_CLOSE: { + minimize(); + return 0; + } // break; + } + break; - SendMessage(hwnd, WM_NULL, 0, 0); + case WM_SYSICON: { + if (wParam == ID_TRAY_APP_ICON) { + SetForegroundWindow(window); + } - if (clicked == ID_TRAY_EXIT) { - Shell_NotifyIcon(NIM_DELETE, &data); - PostQuitMessage(0); - } - } - } break; + if (lParam == WM_LBUTTONUP) { + restore(); + } else if (lParam == WM_RBUTTONDOWN) { + POINT curPoint; + + GetCursorPos(&curPoint); + SetForegroundWindow(window); - case WM_NCHITTEST: { - LRESULT uHitTest = DefWindowProc(hwnd, WM_NCHITTEST, wParam, lParam); + UINT clicked = TrackPopupMenu(menu, TPM_RETURNCMD | TPM_NONOTIFY, + curPoint.x, curPoint.y, 0, hwnd, nullptr); - if (uHitTest == HTCLIENT) { - return HTCAPTION; - } else { - return uHitTest; + SendMessage(hwnd, WM_NULL, 0, 0); + + if (clicked == ID_TRAY_EXIT) { + Shell_NotifyIcon(NIM_DELETE, &data); + PostQuitMessage(0); } } + } break; + + case WM_NCHITTEST: { + LRESULT uHitTest = DefWindowProc(hwnd, WM_NCHITTEST, wParam, lParam); - case WM_CLOSE: { minimize(); return 0; } // break; - case WM_DESTROY: { PostQuitMessage(0); } break; - default: {} break; + if (uHitTest == HTCLIENT) { + return HTCAPTION; + } else { + return uHitTest; + } + } + + case WM_CLOSE: { + minimize(); + return 0; + } // break; + case WM_DESTROY: { + PostQuitMessage(0); + } break; + default: { + } break; } return DefWindowProc(hwnd, message, wParam, lParam); @@ -144,7 +136,9 @@ void InitNotifyIconData() { namespace soyuz { [[maybe_unused]] auto log(const std::string &message) -> void { - if (logs.size() == 16) { logs.erase(logs.begin()); } + if (logs.size() == 16) { + logs.erase(logs.begin()); + } std::string to_log = fmt::format("[{}] {}", current_date_time(), message); @@ -154,7 +148,9 @@ namespace soyuz { } auto log(log_level level, const std::string &message) -> void { - if (logs.size() == 16) { logs.erase(logs.begin()); } + if (logs.size() == 16) { + logs.erase(logs.begin()); + } std::string to_log = fmt::format("[{}] {}", current_date_time(), message); @@ -163,4 +159,4 @@ auto log(log_level level, const std::string &message) -> void { RedrawWindow(window, nullptr, nullptr, RDW_INVALIDATE | RDW_UPDATENOW); } -} +} // namespace soyuz |