-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathControlDispatcher.j
More file actions
365 lines (319 loc) · 14 KB
/
ControlDispatcher.j
File metadata and controls
365 lines (319 loc) · 14 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
/*
This file is part of FrACT10, a vision test battery.
© 2024 Michael Bach, bach@uni-freiburg.de, <https://michaelbach.de>
ControlDispatcher.j
Dispatcher for HTML communication messages to control FrACT
*/
@import <Foundation/Foundation.j>
@import "Globals.j"
@implementation ControlDispatcher: CPObject {
CPString m1, m2, m3;
float m3AsNumber;
BOOL _sendHTMLMessageOnRunDone;
CPString _origin;
}
/**
set up listener to dispatch control messages to FrACT10 when embedded as iframe
*/
+ (void) init { //console.info("ControlDispatcher>init")
_sendHTMLMessageOnRunDone = NO;
[[CPNotificationCenter defaultCenter] addObserver: self selector: @selector(dispatchNotification:) name: "dispatchNotification" object: nil];
window.addEventListener("message", (e) => { //console.info("In addEventListener>message: ", e);
if (e.origin !== "http://localhost:4000") { //only from local host (for unittesting)
if (e.source !== window.parent) return; //or from embedding window
if (e.origin !== window.location.origin) return; //same
}
_origin = e.origin;
const data = e.data;
if (!data || (typeof data !== "object") || Array.isArray(data)) {
[self _logProblem: {error: "bad payload", reason: "data is not object"}];
return;
}
const m1 = data.m1, m2Raw = data.m2, m3Raw = data.m3;
if (typeof m1 !== "string") {
[self _logProblem: {error: "bad payload", reason: "m1 not string"}];
return;
}
if ((m2Raw === undefined) || (m3Raw === undefined) || (m2Raw === null) || (m3Raw === null)) {
[self _logProblem: {error: "bad payload", reason: "m fields missing"}];
return;
}
const m2 = String(m2Raw);
const m3 = String(m3Raw);
if ((m1.length > 32) || (m2.length > 256) || (m3.length > 2048)) {
[self _logProblem: {error: "bad payload", reason: "payload too large"}];
return;
}
const userInfo = {m1: m1, m2: m2, m3: m3};
[[CPNotificationCenter defaultCenter] postNotificationName: "dispatchNotification" object: nil userInfo: userInfo];
});
}
+ (void) dispatchNotification: (CPNotification) notification {
//let's vet the message content somewhat
const message = [notification userInfo];
if (!message || (typeof message !== "object") || Array.isArray(message)) return;
if ((typeof message.m1 !== "string") || (typeof message.m2 !== "string") || (typeof message.m3 !== "string")) return;
if ((message.m1.length > 32) || (message.m2.length > 256) || (message.m3.length > 2048)) return;
m1 = message.m1; m2 = message.m2; m3 = message.m3;
const m2AsNumber = Number(m2);
const m3Lower = m3.toLowerCase ? m3.toLowerCase() : m3;
if (m3Lower === "true") {
m3AsNumber = 1;
} else if (m3Lower === "false") {
m3AsNumber = 0;
} else {
m3AsNumber = Number(m3);
}
const messageHandlers = {
"getVersion": () => {
[self post2parentM1:"getVersion" m2:gVersionStringOfFract m3:gVersionDateOfFrACT success:YES];
_sendHTMLMessageOnRunDone = NO;
},
"getSetting": () => [self manageGetSetting],
"setSetting": () => [self manageSetSetting],
"getValue": () => [self manageGetValue],
"setValue": () => [self manageSetValue],
"run": () => [self manageRun],
"getTestDetails": () => {
[self post2parentM1:"getTestDetails" m2:gTestDetails m3:"" success:YES];
_sendHTMLMessageOnRunDone = NO;
},
"sendChar": () => {
[self sendChar:m2]; [self post2parentM1:m1 m2:m2 m3:m3 success:YES];
},
"respondWithChar": () => {
const keyEvent = [CPEvent keyEventWithType:CPKeyDown location:CGPointMakeZero() modifierFlags:0 timestamp:0 windowNumber:0 context:nil characters:m2 charactersIgnoringModifiers:m2 isARepeat:NO keyCode:0];
[gAppController.currentFractController performSelector:@selector(keyDown:) withObject:keyEvent];
[self post2parentM1:m1 m2:m2 m3:m3 success:YES];
},
"unittest": () => [self manageUnittests],
"reload": () => window.location.reload(NO),
"setFullScreen": () => {
[Misc fullScreenOn:m2]; [self post2parentM1:m1 m2:m2 m3:m3 success:YES];
},
"settingsPane": () => {
if (isNaN(m2AsNumber) || (m2AsNumber > 6)) {
[self _logProblemM123];
return;
}
if (m2AsNumber < 0) {
[gAppController buttonSettingsClose_action:nil];
[self post2parentM1:m1 m2:m2 m3:m3 success:YES];
return;
}
[gAppController setSettingsPaneTabViewSelectedIndex:m2AsNumber];
[gAppController buttonSettings_action:nil];
[Misc udpateGUI];
[self post2parentM1:m1 m2:m2 m3:m3 success:YES];
},
"redraw": () => {
[Misc udpateGUI]; [self post2parentM1:m1 m2:m2 m3:m3 success:YES];
},
"setHomeState": () => {
[self sendChar:String.fromCharCode(13)];
[self sendChar:String.fromCharCode(10)];
if ([Misc isInRun]) {
[gAppController.currentFractController runEnd];
} else {
if (gLatestAlert) {
let alertWindow = [gLatestAlert window];
if (alertWindow) {
[CPApp stopModal];
[alertWindow orderOut:self];
gLatestAlert = null;
}
}
}
[[gAppController window] makeKeyWindow];
[self post2parentM1:m1 m2:m2 m3:m3 success:YES];
}
};
/* deprecated names
messageHandlers["Version"] = messageHandlers["getVersion"];
messageHandlers["Settings"] = messageHandlers["setSetting"];
messageHandlers["Run"] = messageHandlers["run"];
messageHandlers["Unittest"] = messageHandlers["unittest"];
messageHandlers["setHomeStatus"] = messageHandlers["setHomeState"];*/
const handler = messageHandlers[m1];
if (handler) {
handler();
} else {
[self _logProblem: m1 + ", " + m2 + ", " + m3];
}
}
//works except for <esc> in BaLM switch
+ (void) sendChar: (CPString) s { //console.info("ControlDispatcher>sendChar", s)
const keyEvent = [CPEvent keyEventWithType:CPKeyDown location:CGPointMakeZero() modifierFlags:0 timestamp:0 windowNumber:0 context:nil characters:s charactersIgnoringModifiers:s isARepeat:NO keyCode:s.charCodeAt(0)];
const frontWindow = [[CPApp orderedWindows] objectAtIndex:0];
[frontWindow sendEvent: keyEvent];
}
/**
called from AppController
*/
+ (void) runDoneSuccessful: (BOOL) success { //console.info("ControlDispatcher>runDoneSuccessful")
if (!_sendHTMLMessageOnRunDone) return;
_sendHTMLMessageOnRunDone = NO;
[self post2parentM1: m1 m2: m2 m3: m3 success: success];
}
+ (void) manageGetSetting {
if (m2 === "allKeys") {
const allKeys = [[[CPUserDefaults standardUserDefaults]._domains objectForKey: CPApplicationDomain] allKeys];
[self post2parentM1: m1 m2: m2 m3: allKeys success: YES];
return;
}
const sttng = [[CPUserDefaults standardUserDefaults] objectForKey: m2];
[self post2parentM1: m1 m2: m2 m3: sttng success: (sttng !== null)];
}
+ (void) manageSetSetting {
if ((m2 === "preset") || (m2 === "Preset")) {
[self _notify:"notificationApplyPresetNamed" object:m3];
return;
}
const meta = gSettingsNamesAndTypesMap.get(m2);
switch(meta ? meta.type : null) {
case "bool": case "int": case "float":
[self setNumberSettingNamed:m2]; break;
case "color":
[self setColorSettingNamed:m2]; break;
case "str":
[self setStringSettingNamed:m2]; break;
default:
[self _logProblemM123];
}
}
+ (void) manageGetValue {
m3 = null; const _inRun = [Misc isInRun];
if (m2 === "isInRun") {
const s = [Misc testNameGivenTestID: gCurrentTestID];
[self post2parentM1: m1 m2: _inRun m3: s success: YES];
return;
}
if (!_inRun) { //if no test is running, all options further down are moot
[self _logProblemM123]; return;
}
switch(m2) {
case "currentAlternative":
m3 = [gAppController.currentFractController.alternativesGenerator currentAlternative]
[self post2parentM1: m1 m2: m2 m3: m3 success: (m3 !== null)];
break;
case "currentTrial":
m3 = gAppController.currentFractController.iTrial;
[self post2parentM1: m1 m2: m2 m3: m3 success: (m3 !== null)];
break;
case "currentValue":
m3 = [TrialHistoryManager value];
[self post2parentM1: m1 m2: m2 m3: m3 success: (m3 !== null)];
break;
default:
[self _logProblemM123];
}
}
+ (void) manageSetValue {
switch(m2) {
case "resultString":
[gAppController setResultStringFieldTo: m3];
[Misc udpateGUI];
[self post2parentM1:m1 m2:m2 m3:m3 success:YES];
break;
default:
[self _logProblemM123];
}
}
+ (void) manageRun {
_sendHTMLMessageOnRunDone = YES; //need to switch off again if parsing below fails
switch(m2) {
case "testNumber": case "TestNumber":
if ((m3AsNumber >= 1) && (m3AsNumber <= 10)) {
[self _notify: "notificationRunFractControllerTest" object: m3AsNumber]; return;
}
case "acuity": case "Acuity": { //need brackets so scope of variables stays local
const testKey = {"Letters": kTestAcuityLetters, "Landolt": kTestAcuityLandolt, "LandoltRing": kTestAcuityLandolt, "LandoltC": kTestAcuityLandolt, "TumblingE": kTestAcuityE, "TAO": kTestAcuityTAO, "Vernier": kTestAcuityVernier, "Line": kTestAcuityLineByLine,
"BalmLight": kTestBalmLight, "BalmLocation": kTestBalmLocation, "BalmMotion": kTestBalmMotion}[m3];
if (testKey !== undefined) {
[self _notify: "notificationRunFractControllerTest" object: testKey]; return;
}}
case "contrast": case "Contrast": {
const testKey = {"Letters": kTestContrastLetters, "Landolt": kTestContrastLandolt, "LandoltRing": kTestContrastLandolt, "LandoltC": kTestContrastLandolt, "TumblingE": kTestContrastE, "Grating": kTestContrastG}[m3];
if (testKey !== undefined) {
[self _notify: "notificationRunFractControllerTest" object: testKey]; return;
}}
}
[self _logProblemM123];
}
+ (void) manageUnittests { //console.log("\nControlDispatcher>unittest")
switch(m2) {
case "allAutomatic":
[self post2parentM1: m1 m2: m2 m3: m3 success: [Misc allUnittests]];
break;
case "rewardImages": case "RewardImages": //ignore m3
[gAppController.rewardsController unittest];
break;
case "throwError": case "Error":
throw new Error("Runtime error on purpose for testing.");
break;
default:
[self _logProblemM123];
}
}
+ (void) setNumberSettingNamed: (CPString) sName { //console.info("setNumberSettingNamed: ", sName);
if (isNaN(m3AsNumber)) {
[self _logProblemM123]; return;
}
const sNameCapitalised = sName.charAt(0).toUpperCase() + sName.slice(1);
const setter = CPSelectorFromString("set" + sNameCapitalised + ":");
[Settings performSelector: setter withObject: m3AsNumber];
[Settings allNotCheckButSet: NO]; //check whether we were in range
let m3Now = [Settings performSelector: CPSelectorFromString(sName)];
if (typeof(m3Now) === "boolean") {
m3Now = Number(m3Now);
}
[Misc udpateGUI];
[self post2parentM1: m1 m2: m2 m3: m3Now success: m3AsNumber === m3Now];
}
+ (void) setStringSettingNamed: (CPString) sName {
const sNameCapitalised = sName.charAt(0).toUpperCase() + sName.slice(1);
const setter = CPSelectorFromString("set" + sNameCapitalised + ":");
[Settings performSelector: setter withObject: m3];
let m3Now = [Settings performSelector: CPSelectorFromString(sName)];
[Misc udpateGUI];
[self post2parentM1: m1 m2: m2 m3: m3Now success: m3 === m3Now];
}
+ (void) setColorSettingNamed: (CPString) sName {
if (["acuityForeColor", "acuityBackColor"].includes(m2)) {
[Settings setIsAcuityColor: YES];
}
if (["gratingForeColor", "gratingBackColor"].includes(m2)) {
[Settings setIsGratingColor: YES];
}
const sNameCapitalised = sName.charAt(0).toUpperCase() + sName.slice(1);
const setter = CPSelectorFromString("set" + sNameCapitalised + ":");
[Settings performSelector: setter withObject: m3];
[[CPNotificationCenter defaultCenter] postNotificationName: "settingsDidChange" object: nil]; //make sure colors are updated
let m3Now = [Settings performSelector: CPSelectorFromString(sName)]; //read back
m3Now = [m3Now hexString];
[Misc udpateGUI];
[self post2parentM1: m1 m2: m2 m3: m3Now success: m3 === m3Now];
}
+ (void) _notify: (CPString) aNotificationName object: (id) anObject {
[[gAppController window] orderFront: self]; //otherwise we would crash here
[[CPNotificationCenter defaultCenter] postNotificationName: aNotificationName object: anObject];
}
+ (void) post2parentM1: (CPString) m1 m2: (CPString) m2 m3: (CPString) m3 success: (BOOL) success { //console.info("post2parentM1");
try {
window.parent.postMessage({success: success, m1, m2, m3}, _origin);
}
catch(e) { //avoid the global error catcher, `_origin` might be undefined
}
}
+ (void) _logProblemM123 {
const data = {success: NO, m1, m2, m3};
console.log("FrACT10 received unexpected message.data: ", data);
window.parent.postMessage(data, _origin);
_sendHTMLMessageOnRunDone = NO;
}
+ (void) _logProblem: (id) data {
console.log("FrACT10 received unexpected message.data: ", data);
window.parent.postMessage({success: NO, data: data}, _origin);
_sendHTMLMessageOnRunDone = NO;
}
@end