-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathchaosEngine.cpp
221 lines (184 loc) · 5.49 KB
/
chaosEngine.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
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
#include "chaos.h"
#include <iostream>
#include <unistd.h>
#include <cmath>
#include <algorithm>
#include <json/json.h>
using namespace Chaos;
Engine::Engine(Controller* dualshock)
: dualshock(dualshock), timePerModifier(30.0), pause(true)
{
dualshock->addInjector(this);
time.initialize();
chaosInterface.addObserver(this);
//commandListener.start();
//pthread_mutex_init(&chaosMutex, NULL);
}
void Engine::newCommand(const std::string& command) {
std::cout << "Chaos::Engine::newCommand() received: " << command << std::endl;
Json::Value root;
Json::CharReaderBuilder builder;
Json::CharReader* reader = builder.newCharReader();
std::string errs;
bool parsingSuccessful = reader->parse(command.c_str(), command.c_str() + command.length(), &root, &errs);
if (!parsingSuccessful) {
std::cerr << " - Unable to parse the above message!" << std::endl;
}
if (root.isMember("winner")) {
//printf("No winner reported\n");
//return;
Modifier* mod = Modifier::build(root["winner"].asString());
if (mod != NULL) {
//std::cout << "Adding Modifier: " << typeid(*mod).name() << std::endl;
mod->setDualshock(dualshock);
mod->setChaosEngine(this);
lock();
//pthread_mutex_lock(&chaosMutex);
modifiers.push_back(mod);
modifiersThatNeedToStart.push(mod);
// if( !pause ){
// mod->begin();
// }
unlock();
//pthread_mutex_unlock(&chaosMutex);
} else {
std::cerr << "ERROR: Unable to build Modifier for key: " << command << std::endl;
}
}
if (root.isMember("timePerModifier")) {
int newModifierTime = root["timePerModifier"].asFloat();
printf("New Modifier Time: %d\n", newModifierTime);
setTimePerModifier(newModifierTime);
}
}
void Engine::doAction() {
usleep(500); // 200Hz
// Update timers/states of modifiers
if(pause) {
//std::cout << "Paused" << std::endl;
pausedPrior = true;
return;
}
lock();
//pthread_mutex_lock(&chaosMutex);
while(!modifiersThatNeedToStart.empty()) {
modifiersThatNeedToStart.front()->begin();
modifiersThatNeedToStart.pop();
}
unlock();
//pthread_mutex_unlock(&chaosMutex);
lock();
//pthread_mutex_lock(&chaosMutex);
for (std::list<Modifier*>::iterator it = modifiers.begin(); it != modifiers.end(); it++) {
(*it)->_update(pausedPrior);
}
pausedPrior = false;
unlock();
//pthread_mutex_unlock(&chaosMutex);
// Check front element. If timer ran out, remove it.
if (modifiers.size() > 0) {
Modifier* front = modifiers.front();
if ((front->lifespan() >= 0 && front->lifetime() > front->lifespan()) ||
(front->lifespan() < 0 && front->lifetime() > timePerModifier)) {
// std::cout << "Killing Modifier: " << typeid(*front).name() << std::endl;
lock();
//pthread_mutex_lock(&chaosMutex);
front->finish(); // Do any cleanup, if necessary
modifiers.pop_front();
delete front;
unlock();
//pthread_mutex_unlock(&chaosMutex);
}
}
}
bool Engine::sniffify(const DeviceEvent* input, DeviceEvent* output) {
*output = *input;
bool valid = true;
lock();
//pthread_mutex_lock(&chaosMutex);
//int lockCount = 0;
//bool gotALock = false;
// while (lockCount < 1000) { // 1/100th of a second
// if(pthread_mutex_trylock(&chaosMutex)) {
// usleep(10);
// //return false;
// } else {
// gotALock = true;
// break;
// }
// }
//
// if (!gotALock) {
// fprintf(stderr, "Could not get a lock! May be missing events\n");
// return false;
// }
// Tweak the event based on modifiers
if (input->type == TYPE_BUTTON &&
(input->id == BUTTON_OPTIONS || input->id == BUTTON_PS) ) {
if(input->value == 1 && pause == false) { // on rising edge
pause = true;
chaosInterface.sendMessage("{\"pause\":1}");
pausePrimer = false;
std::cout << "Paused" << std::endl;
}
// else if(input->value == 1 && pause == true) { // on rising edge
// pausePrimer = true;
// } else if(input->value == 0 && pausePrimer == true) { // falling edge
// pause = false;
// }
}
if (input->type == TYPE_BUTTON && input->id == BUTTON_SHARE) {
if(input->value == 1 && pause == true) { // on rising edge
pausePrimer = true;
} else if(input->value == 0 && pausePrimer == true) { // falling edge
pause = false;
chaosInterface.sendMessage("{\"pause\":0}");
std::cout << "Resumed" << std::endl;
}
output->value = 0;
}
unlock();
//pthread_mutex_unlock(&chaosMutex);
if (!pause) {
lock();
//pthread_mutex_lock(&chaosMutex);
for (std::list<Modifier*>::iterator it = modifiers.begin(); it != modifiers.end(); it++) {
valid &= (*it)->tweak(output);
if (!valid) {
break;
}
}
unlock();
//pthread_mutex_unlock(&chaosMutex);
}
return valid;
}
void Engine::fakePipelinedEvent(DeviceEvent* fakeEvent, Modifier* modifierThatSentTheFakeEvent) {
bool valid = true;
if (!pause) {
//lock();
// Since we are faking a pipelined event, find the location of the modifier
// and feed the fake event through the rest of the modifier, in order:
std::list<Modifier*>::iterator it = std::find(modifiers.begin(), modifiers.end(), modifierThatSentTheFakeEvent);
for ( it++; it != modifiers.end(); it++) { // iterate from the next element till the end
valid &= (*it)->tweak(fakeEvent);
if (!valid) {
break;
}
}
//unlock();
}
if (valid) {
dualshock->applyEvent(fakeEvent);
}
}
void Engine::setInterfaceReply(const std::string& reply) {
// commandListener.setReply(reply);
chaosInterface.sendMessage(reply);
}
void Engine::setTimePerModifier(double time) {
timePerModifier = time;
}
bool Engine::isPaused() {
return pause;
}