-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathallow_arrow.cpp
More file actions
377 lines (326 loc) · 11.2 KB
/
Copy pathallow_arrow.cpp
File metadata and controls
377 lines (326 loc) · 11.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
/*
The MIT License (MIT)
Copyright (c) 2024-2025 sigma-axis
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include <cstdint>
#include <optional>
#include <bit>
#define NOMINMAX
#define WIN32_LEAN_AND_MEAN
#include <Windows.h>
#include <CommCtrl.h>
#pragma comment(lib, "comctl32")
using byte = uint8_t;
#include <aviutl/filter.hpp>
using namespace AviUtl;
////////////////////////////////
// ウィンドウフックのラッパー.
////////////////////////////////
class HookTarget
{
template<class TSelf>
static LRESULT CALLBACK subclassproc(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam, uintptr_t uid, auto)
{
if (message == WM_DESTROY) {
reinterpret_cast<TSelf*>(uid)->hwnd = nullptr;
::RemoveWindowSubclass(hwnd, subclassproc<TSelf>, uid);
}
else if (reinterpret_cast<TSelf*>(uid)->hwnd != nullptr) {
if (auto ret = TSelf::callback(message, wparam, lparam); ret.has_value())
return ret.value();
}
return ::DefSubclassProc(hwnd, message, wparam, lparam);
}
auto hook_uid() const { return reinterpret_cast<uintptr_t>(this); }
protected:
HWND hwnd = nullptr;
public:
// hooking window procedures.
template<class TSelf>
bool begin_hook(this const TSelf& self)
{
if (self.hwnd != nullptr)
return ::SetWindowSubclass(self.hwnd, subclassproc<TSelf>, self.hook_uid(), 0) != FALSE;
return false;
}
template<class TSelf>
bool end_hook(this const TSelf& self)
{
if (self.hwnd != nullptr)
return ::RemoveWindowSubclass(self.hwnd, subclassproc<TSelf>, self.hook_uid()) != FALSE;
return false;
}
void send_message(auto... args)
{
if (hwnd != nullptr && ::IsWindowVisible(hwnd) != FALSE) {
// 注意: 非表示の設定ダイアログに特定のメッセージを送ると例外が飛ぶ.
auto t = hwnd; hwnd = nullptr;
::SendMessageW(t, args...);
hwnd = t;
}
}
};
// 拡張編集ウィンドウ.
inline constinit class : public HookTarget {
static FilterPlugin* get_exeditfp(FilterPlugin* this_fp)
{
auto h_exedit = ::GetModuleHandleW(L"exedit.auf");
SysInfo si; this_fp->exfunc->get_sys_info(nullptr, &si);
for (int i = 0; i < si.filter_n; i++) {
auto that_fp = this_fp->exfunc->get_filterp(i);
if (that_fp->dll_hinst == h_exedit) return that_fp;
}
return nullptr;
}
public:
bool init(FilterPlugin* fp)
{
auto exeditfp = get_exeditfp(fp);
if (exeditfp != nullptr) hwnd = exeditfp->hwnd;
return hwnd != nullptr;
}
inline static std::optional<LRESULT> callback(UINT message, WPARAM wparam, LPARAM lparam);
} exedit_window;
// 設定ダイアログ.
inline constinit class : public HookTarget {
static auto get_settingdlg()
{
return ::FindWindowW(L"ExtendedFilterClass", nullptr);
}
public:
bool init()
{
hwnd = get_settingdlg();
return hwnd != nullptr;
}
inline static std::optional<LRESULT> callback(UINT message, WPARAM wparam, LPARAM lparam);
} setting_dlg;
// メインウィンドウ.
inline constinit struct {
HWND hwnd = nullptr;
auto send_message(auto... args)
{
return ::SendMessageW(hwnd, args...);
}
} main_window;
////////////////////////////////
// 設定項目.
////////////////////////////////
inline constinit struct Settings {
struct {
bool Left = false, Right = false, Up = false, Down = false, Delete = false;
constexpr bool is_effective() const { return Left || Right || Up || Down || Delete; }
} Timeline;
struct {
bool Left = false, Right = false, Up = false, Down = false;
constexpr bool is_effective() const { return std::bit_cast<uint32_t>(*this) != 0; }
} SettingDlg;
void load(const char* ini_file)
{
#define load_val(wnd, key) wnd.key = 0 != ::GetPrivateProfileIntA(#wnd, "hook" #key, 0, ini_file)
load_val(Timeline, Left);
load_val(Timeline, Right);
load_val(Timeline, Up);
load_val(Timeline, Down);
load_val(Timeline, Delete);
load_val(SettingDlg, Left);
load_val(SettingDlg, Right);
load_val(SettingDlg, Up);
load_val(SettingDlg, Down);
#undef load
}
} settings;
////////////////////////////////
// 設定ロードセーブ.
////////////////////////////////
// replacing a file extension when it's known.
template<class T, size_t len_max, size_t len_old, size_t len_new>
static void replace_tail(T(&dst)[len_max], size_t len, const T(&tail_old)[len_old], const T(&tail_new)[len_new])
{
if (len < len_old || len - len_old + len_new > len_max) return;
std::memcpy(dst + len - len_old, tail_new, len_new * sizeof(T));
}
inline void load_settings(HMODULE h_dll)
{
char path[MAX_PATH];
replace_tail(path, ::GetModuleFileNameA(h_dll, path, std::size(path)) + 1, "auf", "ini");
settings.load(path);
}
////////////////////////////////
// 各種コマンド実行の実装.
////////////////////////////////
struct Menu {
enum class ID : unsigned int {
ExEditLeft,
ExEditRight,
ExEditUp,
ExEditDown,
ExEditDelete,
SettingDlgLeft,
SettingDlgRight,
Invalid,
};
using enum ID;
static constexpr bool is_invalid(ID id) { return id >= Invalid; }
static constexpr bool is_timeline(ID id) { return id <= ExEditDelete; }
static constexpr bool is_settingdlg(ID id) { return id <= SettingDlgRight; }
inline constexpr static struct { ID id; const char* name; } Items[] = {
{ ExEditLeft, "タイムライン左入力" },
{ ExEditRight, "タイムライン右入力" },
{ ExEditUp, "タイムライン上入力" },
{ ExEditDown, "タイムライン下入力" },
{ ExEditDelete, "タイムラインDelete入力" },
{ SettingDlgLeft, "設定ダイアログ左入力" },
{ SettingDlgRight, "設定ダイアログ右入力" },
};
};
bool menu_timeline_handler(Menu::ID id)
{
WPARAM vk = 0;
switch (id) {
case Menu::ExEditLeft: vk = VK_LEFT; break;
case Menu::ExEditRight: vk = VK_RIGHT; break;
case Menu::ExEditUp: vk = VK_UP; break;
case Menu::ExEditDown: vk = VK_DOWN; break;
case Menu::ExEditDelete: vk = VK_DELETE; break;
}
if (vk != 0) exedit_window.send_message(WM_KEYDOWN, vk, 0);
return false;
}
bool menu_setting_dlg_handler(Menu::ID id)
{
WPARAM vk = 0;
switch (id) {
case Menu::SettingDlgLeft: vk = VK_LEFT; break;
case Menu::SettingDlgRight: vk = VK_RIGHT; break;
}
if (vk != 0) setting_dlg.send_message(WM_KEYDOWN, vk, 0);
return false;
}
////////////////////////////////
// フックのコールバック関数.
////////////////////////////////
inline std::optional<LRESULT> decltype(exedit_window)::callback(UINT message, WPARAM wparam, LPARAM lparam)
{
switch (message) {
case WM_KEYDOWN:
case WM_SYSKEYDOWN:
if ([=] {
switch (wparam) {
case VK_LEFT: return settings.Timeline.Left;
case VK_RIGHT: return settings.Timeline.Right;
case VK_UP: return settings.Timeline.Up;
case VK_DOWN: return settings.Timeline.Down;
case VK_DELETE: return settings.Timeline.Delete;
default: return false;
}
}()) return main_window.send_message(message, wparam, lparam);
break;
}
return std::nullopt;
}
inline std::optional<LRESULT> decltype(setting_dlg)::callback(UINT message, WPARAM wparam, LPARAM lparam)
{
switch (message) {
case WM_KEYDOWN:
case WM_SYSKEYDOWN:
if ([=] {
switch (wparam) {
case VK_LEFT: return settings.SettingDlg.Left;
case VK_RIGHT: return settings.SettingDlg.Right;
case VK_UP: return settings.SettingDlg.Up;
case VK_DOWN: return settings.SettingDlg.Down;
default: return false;
}
}()) return main_window.send_message(message, wparam, lparam);
break;
}
return std::nullopt;
}
////////////////////////////////
// AviUtlに渡す関数の定義.
////////////////////////////////
BOOL func_init(FilterPlugin* fp)
{
// 拡張編集の確認.
if (!exedit_window.init(fp)) {
::MessageBoxA(fp->hwnd, "拡張編集が見つかりませんでした.",
fp->name, MB_OK | MB_ICONEXCLAMATION);
return FALSE;
}
// メインウィンドウのハンドルを取得,保存.
main_window.hwnd = fp->hwnd_parent;
// message-only window を作成,登録.これで NoConfig でも AviUtl からメッセージを受け取れる.
fp->hwnd = ::CreateWindowExW(0, L"AviUtl", L"", 0, 0, 0, 0, 0,
HWND_MESSAGE, nullptr, fp->hinst_parent, nullptr);
// メニュー登録.
for (auto [id, name] : Menu::Items)
fp->exfunc->add_menu_item(fp, name, fp->hwnd, static_cast<int32_t>(id), 0, ExFunc::AddMenuItemFlag::None);
// 設定ロード.
load_settings(fp->dll_hinst);
return TRUE;
}
BOOL func_WndProc(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam, EditHandle* editp, FilterPlugin* fp)
{
using Message = FilterPlugin::WindowMessage;
switch (message) {
// 拡張編集タイムライン/設定ダイアログのフック/アンフック.
case Message::ChangeWindow:
setting_dlg.init();
if (settings.Timeline.is_effective()) exedit_window.begin_hook();
if (settings.SettingDlg.is_effective()) setting_dlg.begin_hook();
break;
case Message::Exit:
if (settings.Timeline.is_effective()) exedit_window.end_hook();
if (settings.SettingDlg.is_effective()) setting_dlg.end_hook();
// message-only window を削除.必要ないかもしれないけど.
fp->hwnd = nullptr; ::DestroyWindow(hwnd);
break;
case Message::Command:
// コマンドハンドラ.
if (auto id = static_cast<Menu::ID>(wparam); !Menu::is_invalid(id) &&
fp->exfunc->is_editing(editp) && !fp->exfunc->is_saving(editp)) {
return (
Menu::is_timeline(id) ? menu_timeline_handler(id) :
Menu::is_settingdlg(id) ? menu_setting_dlg_handler(id) :
false) ? TRUE : FALSE;
}
break;
}
return FALSE;
}
////////////////////////////////
// Entry point.
////////////////////////////////
BOOL WINAPI DllMain(HINSTANCE hinst, DWORD fdwReason, LPVOID lpvReserved)
{
switch (fdwReason) {
case DLL_PROCESS_ATTACH:
::DisableThreadLibraryCalls(hinst);
break;
}
return TRUE;
}
////////////////////////////////
// 看板.
////////////////////////////////
#define PLUGIN_NAME "Allow Arrow"
#define PLUGIN_VERSION "v1.20"
#define PLUGIN_AUTHOR "sigma-axis"
#define PLUGIN_INFO_FMT(name, ver, author) (name##" "##ver##" by "##author)
extern "C" __declspec(dllexport) FilterPluginDLL* __stdcall GetFilterTable(void)
{
// (フィルタとは名ばかりの)看板.
using Flag = FilterPlugin::Flag;
static constinit FilterPluginDLL filter {
.flag = Flag::AlwaysActive | Flag::ExInformation | Flag::NoConfig,
.name = PLUGIN_NAME,
.func_init = func_init,
.func_WndProc = func_WndProc,
.information = PLUGIN_INFO_FMT(PLUGIN_NAME, PLUGIN_VERSION, PLUGIN_AUTHOR),
};
return &filter;
}