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