-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathexclusive_callback.cpp
168 lines (142 loc) · 4.09 KB
/
exclusive_callback.cpp
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
#include "ai.h"
#include "exclusive_callback.h"
#include "debug.h"
#include "modules/Gui.h"
#include "modules/Screen.h"
#include "df/viewscreen.h"
#undef Key
#undef Char
ExclusiveCallback::ExclusiveCallback(const std::string & description, size_t wait_multiplier) :
out_proxy(),
pull(nullptr),
push([&](coroutine_t::pull_type & input) { init(input); }),
wait_multiplier(wait_multiplier),
wait_frames(0),
did_delay(true),
feed_keys(),
expectedScreen(),
expectedFocus(),
expectedParentFocus(),
description(description),
dfplex_blacklist(false)
{
if (wait_multiplier < 1)
{
wait_multiplier = 1;
}
}
ExclusiveCallback::~ExclusiveCallback()
{
}
void ExclusiveCallback::KeyNoDelay(df::interface_key key)
{
feed_keys.push_back(key);
}
void ExclusiveCallback::Key(df::interface_key key, const char *filename, int lineno)
{
checkScreen(filename, lineno);
KeyNoDelay(key);
Delay();
}
const static char safe_char[128] =
{
'C', 'u', 'e', 'a', 'a', 'a', 'a', 'c', 'e', 'e', 'e', 'i', 'i', 'i', 'A', 'A',
'E', 0, 0, 'o', 'o', 'o', 'u', 'u', 'y', 'O', 'U', 0, 0, 0, 0, 0,
'a', 'i', 'o', 'u', 'n', 'N', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
};
void ExclusiveCallback::Char(char c, const char *filename, int lineno)
{
if (c < 0)
{
c = safe_char[(uint8_t)c - 128];
}
Key(Screen::charToKey(c), filename, lineno);
}
void ExclusiveCallback::Delay(size_t frames)
{
for (size_t i = 0; i < frames; i++)
{
out_proxy.clear();
out_proxy.set(*(*pull)().get());
}
// Wait until we have an actual viewscreen.
while (Screen::isDismissed(Gui::getCurViewscreen(false)))
{
size_t real_wait_multiplier = wait_multiplier;
wait_multiplier = 1;
out_proxy.clear();
out_proxy.set(*(*pull)().get());
wait_multiplier = real_wait_multiplier;
}
did_delay = true;
}
void ExclusiveCallback::AssertDelayed()
{
DFAI_ASSERT(did_delay, "previous iteration of exclusive callback \"" << description << "\" did not call Delay.");
did_delay = false;
}
void ExclusiveCallback::checkScreen(const char *filename, int lineno)
{
if (!expectedScreen)
{
return;
}
bool first = true;
for (;;)
{
df::viewscreen *curview = Gui::getCurViewscreen(true);
bool isExpectedScreen = expectedScreen->is_instance(curview) &&
(expectedFocus.empty() || Gui::getFocusString(curview) == expectedFocus) &&
(expectedParentFocus.empty() || Gui::getFocusString(curview->parent) == expectedParentFocus);
if (first)
{
DFAI_ASSERT_LOC(isExpectedScreen,
"expected screen to be " << expectedScreen->getName() << ":" << expectedFocus << ":" << expectedParentFocus <<
", but it is " << virtual_identity::get(curview)->getName() << ":" << Gui::getFocusString(curview) << ":" << Gui::getFocusString(curview->parent),
filename, lineno);
}
if (isExpectedScreen)
{
return;
}
first = false;
Delay();
}
}
bool ExclusiveCallback::run(color_ostream & out, const std::function<void(std::vector<df::interface_key> &)> & send_keys)
{
if (wait_frames)
{
wait_frames--;
return false;
}
bool done = !push(&out);
if (!feed_keys.empty())
{
send_keys(feed_keys);
}
if (!done)
{
wait_frames = wait_multiplier - 1;
return false;
}
return true;
}
void ExclusiveCallback::init(coroutine_t::pull_type & input)
{
pull = &input;
out_proxy.set(*pull->get());
Run(out_proxy);
out_proxy.clear();
// Make sure we wait for the screen to go back to normal if our last calls before returning were to KeyNoWait.
if (!feed_keys.empty())
{
wait_multiplier = 1;
Delay();
}
}