From dfa5664e0b068fdc554caee2b8606aa135266949 Mon Sep 17 00:00:00 2001 From: routersys Date: Wed, 9 Sep 2026 17:02:32 +0900 Subject: [PATCH] =?UTF-8?q?=E7=8A=B6=E6=85=8B=E3=81=AE=E5=A4=89=E5=8C=96?= =?UTF-8?q?=E3=82=92=E9=80=9A=E7=9F=A5=E3=81=A7=E4=BC=9D=E3=81=88=E3=82=8B?= =?UTF-8?q?=E3=82=88=E3=81=86=E3=81=AB=E3=81=97=E3=81=A6=E5=91=A8=E6=9C=9F?= =?UTF-8?q?=E5=87=A6=E7=90=86=E3=82=92=E7=84=A1=E3=81=8F=E3=81=97=E3=81=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ProxyEdit.vcxproj | 2 ++ ProxyEdit.vcxproj.filters | 6 ++++ src/Notifier.cpp | 31 ++++++++++++++---- src/Notify.cpp | 59 +++++++++++++++++++++++++++++++++ src/Notify.h | 12 +++++++ src/ProxyBuilder.cpp | 41 ++++++++++++++++++----- src/ScanController.cpp | 69 +++++++++++++++++++++++++++------------ src/ScanController.h | 5 ++- src/StatusPanel.cpp | 45 +++++-------------------- 9 files changed, 195 insertions(+), 75 deletions(-) create mode 100644 src/Notify.cpp create mode 100644 src/Notify.h diff --git a/ProxyEdit.vcxproj b/ProxyEdit.vcxproj index 1360483..0e751bb 100644 --- a/ProxyEdit.vcxproj +++ b/ProxyEdit.vcxproj @@ -99,6 +99,7 @@ + @@ -118,6 +119,7 @@ + diff --git a/ProxyEdit.vcxproj.filters b/ProxyEdit.vcxproj.filters index 4292e39..caea12b 100644 --- a/ProxyEdit.vcxproj.filters +++ b/ProxyEdit.vcxproj.filters @@ -40,6 +40,9 @@ ソース ファイル + + ソース ファイル + ソース ファイル @@ -90,6 +93,9 @@ ヘッダー ファイル + + ヘッダー ファイル + ヘッダー ファイル diff --git a/src/Notifier.cpp b/src/Notifier.cpp index d6b9934..1ad555c 100644 --- a/src/Notifier.cpp +++ b/src/Notifier.cpp @@ -7,17 +7,19 @@ #include "HostContext.h" #include "Log.h" +#include "Notify.h" #include "ProxyBuilder.h" namespace pe { namespace { -const UINT_PTR kTimerId = 0x50450001; -const UINT kTimerInterval = 700; +const wchar_t* kClassName = L"ProxyEditNotifierSink"; +const UINT kStateMessage = WM_APP + 1; ITaskbarList3* g_taskbar = nullptr; HWND g_window = nullptr; +HWND g_sink = nullptr; bool g_showing = false; std::atomic g_finished_reported{true}; @@ -66,8 +68,13 @@ void Update() { } } -void CALLBACK OnTimer(HWND, UINT, UINT_PTR, DWORD) { - Update(); +LRESULT CALLBACK SinkProc(HWND window, UINT message, WPARAM first, LPARAM second) { + if (message == kStateMessage) { + AcknowledgeStateChange(window); + Update(); + return 0; + } + return DefWindowProcW(window, message, first, second); } } @@ -75,12 +82,24 @@ void CALLBACK OnTimer(HWND, UINT, UINT_PTR, DWORD) { void StartNotifier() { g_window = HostWindow(); if (!g_window) return; - SetTimer(g_window, kTimerId, kTimerInterval, OnTimer); + WNDCLASSEXW description{}; + description.cbSize = sizeof(description); + description.lpfnWndProc = SinkProc; + description.hInstance = ModuleInstance(); + description.lpszClassName = kClassName; + RegisterClassExW(&description); + g_sink = CreateWindowExW(0, kClassName, L"", 0, 0, 0, 0, 0, HWND_MESSAGE, nullptr, + ModuleInstance(), nullptr); + if (g_sink) AddStateListener(g_sink, kStateMessage); } void StopNotifier() { + if (g_sink) { + RemoveStateListener(g_sink); + DestroyWindow(g_sink); + g_sink = nullptr; + } if (g_window) { - KillTimer(g_window, kTimerId); if (g_taskbar && g_showing) g_taskbar->SetProgressState(g_window, TBPF_NOPROGRESS); g_window = nullptr; } diff --git a/src/Notify.cpp b/src/Notify.cpp new file mode 100644 index 0000000..ca849d8 --- /dev/null +++ b/src/Notify.cpp @@ -0,0 +1,59 @@ +#include "Notify.h" + +#include +#include +#include +#include + +namespace pe { + +namespace { + +struct Listener { + HWND window = nullptr; + UINT message = 0; + std::atomic pending{false}; +}; + +std::vector> g_listeners; +std::mutex g_lock; + +} + +void AddStateListener(HWND window, UINT message) { + if (!window) return; + std::lock_guard lock(g_lock); + for (const auto& listener : g_listeners) { + if (listener->window == window) return; + } + auto listener = std::make_unique(); + listener->window = window; + listener->message = message; + g_listeners.push_back(std::move(listener)); +} + +void RemoveStateListener(HWND window) { + std::lock_guard lock(g_lock); + for (size_t index = 0; index < g_listeners.size(); index++) { + if (g_listeners[index]->window != window) continue; + g_listeners.erase(g_listeners.begin() + (ptrdiff_t)index); + return; + } +} + +void AcknowledgeStateChange(HWND window) { + std::lock_guard lock(g_lock); + for (const auto& listener : g_listeners) { + if (listener->window == window) listener->pending.store(false); + } +} + +void PublishStateChange() { + std::lock_guard lock(g_lock); + for (const auto& listener : g_listeners) { + if (listener->pending.exchange(true)) continue; + if (!PostMessageW(listener->window, listener->message, 0, 0)) listener->pending.store(false); + } +} + +} diff --git a/src/Notify.h b/src/Notify.h new file mode 100644 index 0000000..c9ac9bd --- /dev/null +++ b/src/Notify.h @@ -0,0 +1,12 @@ +#pragma once + +#include + +namespace pe { + +void AddStateListener(HWND window, UINT message); +void RemoveStateListener(HWND window); +void AcknowledgeStateChange(HWND window); +void PublishStateChange(); + +} diff --git a/src/ProxyBuilder.cpp b/src/ProxyBuilder.cpp index 939a865..af27e8e 100644 --- a/src/ProxyBuilder.cpp +++ b/src/ProxyBuilder.cpp @@ -13,6 +13,7 @@ #include "JpegCodec.h" #include "Log.h" #include "MediaDecoder.h" +#include "Notify.h" #include "ProxyFormat.h" #include "ProxyStore.h" #include "Settings.h" @@ -48,6 +49,15 @@ std::mutex g_wake_lock; std::atomic g_stop{false}; std::atomic g_paused{false}; std::atomic g_active{0}; +std::atomic g_generation{0}; + +void WakeWorkers() { + { + std::lock_guard lock(g_wake_lock); + g_generation.fetch_add(1); + } + g_wake.notify_all(); +} std::wstring Normalized(const std::wstring& path) { std::wstring lowered = path; @@ -104,8 +114,11 @@ bool PickChunk(const JobPointer& job, int& chunk) { } void MarkChunk(const JobPointer& job, int chunk, unsigned char state) { - std::lock_guard lock(job->state_lock); - if (chunk >= 0 && chunk < (int)job->chunks.size()) job->chunks[(size_t)chunk] = state; + { + std::lock_guard lock(job->state_lock); + if (chunk >= 0 && chunk < (int)job->chunks.size()) job->chunks[(size_t)chunk] = state; + } + PublishStateChange(); } bool OpenDecoder(const JobPointer& job) { @@ -130,6 +143,7 @@ void FailJob(const JobPointer& job, const wchar_t* reason) { job->message = reason; } Warn(L"生成を中止しました: %s (%s)", reason, job->key.path.c_str()); + PublishStateChange(); } void ProcessChunk(const JobPointer& job, int chunk) { @@ -200,16 +214,19 @@ bool TakeWork(JobPointer& picked, int& chunk) { void Worker() { while (!g_stop.load()) { + const unsigned long long seen = g_generation.load(); if (g_paused.load()) { std::unique_lock lock(g_wake_lock); - g_wake.wait_for(lock, std::chrono::milliseconds(400)); + g_wake.wait(lock, [seen] { + return g_stop.load() || !g_paused.load() || g_generation.load() != seen; + }); continue; } JobPointer job; int chunk = -1; if (!TakeWork(job, chunk)) { std::unique_lock lock(g_wake_lock); - g_wake.wait_for(lock, std::chrono::milliseconds(500)); + g_wake.wait(lock, [seen] { return g_stop.load() || g_generation.load() != seen; }); continue; } g_active.fetch_add(1); @@ -221,6 +238,8 @@ void Worker() { } g_active.fetch_sub(1); job->work.unlock(); + WakeWorkers(); + PublishStateChange(); } } @@ -237,7 +256,7 @@ void StartBuilder() { void StopBuilder() { g_stop.store(true); - g_wake.notify_all(); + WakeWorkers(); for (std::thread& worker : g_workers) { if (worker.joinable()) worker.join(); } @@ -317,7 +336,8 @@ bool RegisterSource(const std::wstring& source, std::wstring& proxy_path) { } proxy_path = job->proxy; Say(L"プロキシを登録しました: %s -> %dx%d", source.c_str(), header.proxy_width, header.proxy_height); - g_wake.notify_all(); + WakeWorkers(); + PublishStateChange(); return true; } @@ -326,7 +346,7 @@ void FocusSource(const std::wstring& source, int frame) { auto found = g_jobs.find(Normalized(source)); if (found == g_jobs.end()) return; found->second->focus.store(frame); - g_wake.notify_all(); + WakeWorkers(); } void RequestWholeSource(const std::wstring& source) { @@ -334,7 +354,8 @@ void RequestWholeSource(const std::wstring& source) { auto found = g_jobs.find(Normalized(source)); if (found == g_jobs.end()) return; found->second->want_all.store(true); - g_wake.notify_all(); + WakeWorkers(); + PublishStateChange(); } void DiscardSource(const std::wstring& source) { @@ -350,11 +371,13 @@ void DiscardSource(const std::wstring& source) { job->writer.Close(); job->decoder.Close(); RemoveProxy(job->proxy); + PublishStateChange(); } void SetBuilderPaused(bool paused) { g_paused.store(paused); - g_wake.notify_all(); + WakeWorkers(); + PublishStateChange(); } bool BuilderPaused() { diff --git a/src/ScanController.cpp b/src/ScanController.cpp index b6c294e..abd294b 100644 --- a/src/ScanController.cpp +++ b/src/ScanController.cpp @@ -6,12 +6,12 @@ #include #include #include -#include #include #include #include "HostContext.h" #include "Log.h" +#include "Notify.h" #include "ProxyBuilder.h" #include "ProxyFormat.h" #include "ProxyStore.h" @@ -19,18 +19,34 @@ namespace pe { +ScanResult ApplyProxies(); +ScanResult RestoreOriginals(); + namespace { const wchar_t* kEffect = L"動画ファイル"; const wchar_t* kItem = L"ファイル"; std::atomic g_scanning{false}; -std::atomic g_last_scan{0}; -std::atomic g_requested{false}; +std::atomic g_pending{0}; std::atomic g_stop{false}; std::thread g_worker; std::mutex g_wake_lock; std::condition_variable g_wake; +enum RequestKind { + kRequestAutomatic = 1, + kRequestApply = 2, + kRequestRestore = 4, +}; + +void Post(int kind) { + { + std::lock_guard lock(g_wake_lock); + g_pending.fetch_or(kind); + } + g_wake.notify_all(); +} + struct Decision { std::wstring proxy; long long size = 0; @@ -128,34 +144,43 @@ bool Eligible(const std::wstring& path, const MEDIA_INFO& info) { } void ScanWorker() { - while (!g_stop.load()) { + while (true) { + int taken = 0; { std::unique_lock lock(g_wake_lock); - g_wake.wait_for(lock, std::chrono::milliseconds(400)); + g_wake.wait(lock, [] { return g_stop.load() || g_pending.load() != 0; }); + if (g_stop.load()) return; + taken = g_pending.exchange(0); } - if (g_stop.load()) return; - if (!g_requested.exchange(false)) continue; - if (!CurrentSettings().enabled) continue; - ScanResult result = ApplyProxies(); - g_last_scan.store(GetTickCount64()); - if (result.swapped > 0) { - Say(L"自動でプロキシへ差し替えました: %d 件", result.swapped); + if (taken & kRequestRestore) { + ScanResult result = RestoreOriginals(); + Say(L"元素材へ戻しました: %d 件", result.restored); + PublishStateChange(); + } + if (taken & kRequestApply) { + ScanResult result = ApplyProxies(); + Say(L"プロキシへ差し替えました: 対象 %d 件、差し替え %d 件", result.eligible, result.swapped); + PublishStateChange(); + } else if ((taken & kRequestAutomatic) && CurrentSettings().enabled) { + ScanResult result = ApplyProxies(); + if (result.swapped > 0) { + Say(L"自動でプロキシへ差し替えました: %d 件", result.swapped); + } + PublishStateChange(); } } } void OnHostEvent(void*) { - RequestAutomaticScan(); + Post(kRequestAutomatic); } void OnEditMenuApply(EDIT_SECTION*) { - ScanResult result = ApplyProxies(); - Say(L"プロキシへ差し替えました: 対象 %d 件、差し替え %d 件", result.eligible, result.swapped); + Post(kRequestApply); } void OnEditMenuRestore(EDIT_SECTION*) { - ScanResult result = RestoreOriginals(); - Say(L"元素材へ戻しました: %d 件", result.restored); + Post(kRequestRestore); } } @@ -268,10 +293,12 @@ ScanResult RestoreOriginals() { return result; } -void RequestAutomaticScan() { - if (GetTickCount64() - g_last_scan.load() < 1500) return; - g_requested.store(true); - g_wake.notify_all(); +void RequestApply() { + Post(kRequestApply); +} + +void RequestRestore() { + Post(kRequestRestore); } void StartScanController() { diff --git a/src/ScanController.h b/src/ScanController.h index a89b672..bae4119 100644 --- a/src/ScanController.h +++ b/src/ScanController.h @@ -18,11 +18,10 @@ struct ScanResult { }; void RegisterScanMenus(HOST_APP_TABLE* host); -ScanResult ApplyProxies(); -ScanResult RestoreOriginals(); +void RequestApply(); +void RequestRestore(); bool IsProxyPath(const std::wstring& path); std::wstring SourceOfProxy(const std::wstring& path); -void RequestAutomaticScan(); void StartScanController(); void StopScanController(); diff --git a/src/StatusPanel.cpp b/src/StatusPanel.cpp index 7e6f190..2d228c7 100644 --- a/src/StatusPanel.cpp +++ b/src/StatusPanel.cpp @@ -8,6 +8,7 @@ #include "HostContext.h" #include "Log.h" +#include "Notify.h" #include "ProxyBuilder.h" #include "ScanController.h" #include "Settings.h" @@ -17,7 +18,7 @@ namespace pe { namespace { const wchar_t* kClassName = L"ProxyEditStatusPanel"; -const UINT_PTR kTimerId = 0x50450002; +const UINT kStateMessage = WM_APP + 1; struct Button { RECT area{}; @@ -48,9 +49,6 @@ RECT g_thumb{}; bool g_tracking = false; bool g_dragging = false; int g_drag_offset = 0; -BuilderSummary g_shown; -bool g_shown_valid = false; -unsigned long long g_asked = 0; int Scaled(HWND window, int value) { UINT dpi = GetDpiForWindow(window); @@ -391,49 +389,24 @@ int HitButton(POINT point) { void RunAction(HWND window, int action) { if (action == kActionApply) { - ScanResult result = ApplyProxies(); - Say(L"プロキシへ差し替えました: 対象 %d 件、差し替え %d 件", result.eligible, result.swapped); + RequestApply(); } else if (action == kActionRestore) { - ScanResult result = RestoreOriginals(); - Say(L"元素材へ戻しました: %d 件", result.restored); + RequestRestore(); } else if (action == kActionPause) { SetBuilderPaused(!BuilderPaused()); } InvalidateRect(window, nullptr, FALSE); } -bool SummaryChanged(const BuilderSummary& summary) { - if (!g_shown_valid) return true; - return summary.jobs != g_shown.jobs || summary.queued_chunks != g_shown.queued_chunks || - summary.bytes != g_shown.bytes || summary.paused != g_shown.paused || - summary.working != g_shown.working || summary.overall != g_shown.overall; -} - LRESULT CALLBACK PanelProc(HWND window, UINT message, WPARAM first, LPARAM second) { switch (message) { case WM_CREATE: - SetTimer(window, kTimerId, 250, nullptr); + AddStateListener(window, kStateMessage); return 0; - case WM_TIMER: { - if (!IsWindowVisible(window)) return 0; - if (g_jobs.empty() && CurrentSettings().enabled) { - const unsigned long long now = GetTickCount64(); - if (now - g_asked >= 4000) { - EDIT_INFO info = EditInfo(); - if (info.width > 0 && (info.frame_max > 0 || info.layer_max > 0)) { - g_asked = now; - RequestAutomaticScan(); - } - } - } - BuilderSummary summary = BuilderState(); - if (SummaryChanged(summary)) { - g_shown = summary; - g_shown_valid = true; - InvalidateRect(window, nullptr, FALSE); - } + case kStateMessage: + AcknowledgeStateChange(window); + if (IsWindowVisible(window)) InvalidateRect(window, nullptr, FALSE); return 0; - } case WM_ERASEBKGND: return 1; case WM_PAINT: @@ -555,7 +528,7 @@ LRESULT CALLBACK PanelProc(HWND window, UINT message, WPARAM first, LPARAM secon return 0; } case WM_DESTROY: - KillTimer(window, kTimerId); + RemoveStateListener(window); return 0; default: break;