forked from Ybalrid/OpenXR-API-Layer-Template
/
layer_events.h
408 lines (378 loc) · 10.6 KB
/
layer_events.h
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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
#ifndef LAYER_EVENTS_H
#define LAYER_EVENTS_H
#include "layer_utl.h"
#include "thread_utl.h"
#define TARGET_APP (1U << 0)
#define TARGET_BUS (1U << 1)
#define TARGET_CLI (1U << 2)
#define TARGET_GUI (1U << 3)
enum EventType : unsigned char
{
EVENT_APP_REGISTER,
EVENT_APP_VAR,
EVENT_APP_ACTION,
EVENT_APP_ACTION_SET,
EVENT_APP_SOURCE,
EVENT_APP_PROFILE,
EVENT_APP_ACTION_MAP,
EVENT_APP_CUSTOM_ACTION,
EVENT_APP_BINDING,
EVENT_APP_RPN,
EVENT_APP_SESSION,
EVENT_DIAGMSG,
EVENT_COMMAND,
EVENT_CLIENT_REGISTER
};
enum CommandType{
EVENT_POLL_NULL,
EVENT_POLL_TRIGGER_INTERACTION_PROFILE_CHANGED,
EVENT_POLL_RELOAD_CONFIG,
EVENT_POLL_SET_EXTERNAL_SOURCE,
EVENT_POLL_MAP_DIRECT_SOURCE,
EVENT_POLL_MAP_AXIS,
EVENT_POLL_MAP_ACTION,
EVENT_POLL_RESET_ACTION,
EVENT_POLL_SET_PROFILE,
EVENT_POLL_DUMP_APP_BINDINGS,
EVENT_POLL_DUMP_LAYER_BINDINGS,
EVENT_POLL_DUMP_SOURCES,
EVENT_POLL_DUMP_VARIABLES,
EVENT_POLL_DUMP_ACTION_MAPS,
EVENT_POLL_DUMP_EXPRESSIONS,
EVENT_POLL_DUMP_CUSTOM_ACTIONS,
EVENT_POLL_DUMP_SESSION
};
constexpr static struct CommandDef
{
SubStr name;
SubStr sign;
SubStr description;
} gCommands[] =
{
{"", "", ""},
{"triggerInteractionProfileChanged", "", "\nSimulate Interaction Profile switch.\nMay trigger app to restart session/bindings\nMay be useful to reload bindings from config"},
{"reloadConfig", "", "\nReload configuration from file.\nResets all runtime binding changes, but keeps variables and sources"},
{"setExternalSource","sif", "<source name> <axis> <value>\nSets value of source marked with action_external\nUseful for external scripting"},
{"mapDirectSource","sis", "<action name> <controller index> <source mapping>[.<source_controller>]\nMap action to same-type source\nThe most cahce-friendly way"},
{"mapAxis","siis", "<action name> <controller index> <axis index> <mapping expression>\nMap action axis to any source or calculated expression"},
{"mapAction","sis", "<action name> <controller index> <actionmap name>\nMap action to actionmap section in config"},
{"resetAction","s", "<action name>\nRe-apply action definition from config"},
{"setProfile","s", "<profile name>\nApply maps and custom actions based on config profile section"},
{"dumpAppBindings","", "\nDump action sets created by App and it's active bindings"},
{"dumpLayerBindings","", "\nDump action sets created by Layer and it's active bindings"},
{"dumpSources","", "\nDump active layer sources and it's values"},
{"dumpVariables","", "\nDump variables and values"},
{"dumpActionMaps","", "\nDump active bound actionmaps and current values"},
{"dumpExpressions","","\nDump Expressions internal RPN representation"},
{"dumpCustomActions","", "\nDump custom action trigger time and period"},
{"dumpSession","", "\nDump session handle. Session state is only sent when it's changed"},
};
#define MAX_CMDLEN 256
struct CommandHeader
{
CommandType ctype;
union{
unsigned int i;
float f;
unsigned short offsets[2];
} args[4];
unsigned short datasize;
};
struct Command : CommandHeader
{
constexpr static EventType type = EVENT_COMMAND;
char data[MAX_CMDLEN - sizeof(CommandHeader)];
Command() = default;
void _AddStr(const SubStr &s, int arg)
{
int len = s.Len();
if(len > sizeof(data) - datasize)
len = sizeof(data) - datasize;
if(len)
memcpy(&data[datasize], s.begin, len);
args[arg].offsets[0] = datasize;
datasize += len;
args[arg].offsets[1] = datasize;
}
void _ParseArg(int j, const SubStr &s0, char c)
{
if(c == 's')
_AddStr(s0, j);
if(c == 'i')
args[j].i = atoi(s0.begin);
if(c == 'f')
args[j].f = atof(s0.begin);
}
Command(const SubStr &s)
{
datasize = 0;
ctype = EVENT_POLL_NULL;
SubStr s0, si, sn; // todo: encode signature to string/array instead
if(!s.Split2(s0, si, ' '))
s0 = s, si = {nullptr, nullptr};
for(int i = 1; i < sizeof(gCommands) / sizeof(gCommands[0]);i++)
{
if(!s0.Equals(gCommands[i].name))
continue;
for(int j = 0; j < gCommands[i].sign.Len();j++)
{
if(!si.begin)
return;
if( ( j == gCommands[i].sign.Len() - 1 ) || !si.Split2( s0, sn, ' ' ))
s0 = si, sn = {nullptr, nullptr};
_ParseArg(j, s0, gCommands[i].sign.begin[j]);
si = sn;
}
ctype = (CommandType)i;
return;
}
}
Command(const SubStr &cmd, const SubStr &arg0, const SubStr &arg1, const SubStr &arg2, const SubStr &arg3)
{
for(int i = 1; i < sizeof(gCommands) / sizeof(gCommands[0]);i++)
{
if(!cmd.Equals(gCommands[i].name))
continue;
int l = gCommands[i].sign.Len();
if(!arg0.begin && l > 0)
return;
if(l > 0)
_ParseArg(0, arg0, gCommands[i].sign.begin[0] );
if(!arg1.begin && l > 1)
return;
if(l > 1)
_ParseArg(1, arg1, gCommands[i].sign.begin[1] );
if(!arg2.begin && l > 2)
return;
if(l > 2)
_ParseArg(2, arg2, gCommands[i].sign.begin[2] );
if(!arg3.begin && l > 3)
return;
if(l > 3)
_ParseArg(3, arg3, gCommands[i].sign.begin[3] );
ctype = (CommandType)i;
return;
}
}
Command & operator = (const Command &other)
{
ctype = other.ctype;
memcpy(args, other.args, sizeof(other.args));
datasize = other.datasize;
memcpy(data, other.data, datasize);
return *this;
}
SubStr Str(int index) const
{
return {&data[args[index].offsets[0]], &data[args[index].offsets[1]]};
}
};
#define Field(type,name) \
constexpr static const char fld_name_##name[] = #name; \
Field_<type, fld_name_##name, sizeof(fld_name_##name) - 1> name
template <typename T, const auto &NAME, size_t nlen>
struct Field_
{
constexpr static const char *name = NAME;
T val;
operator T() const
{
return val;
}
//Field_(const Field_ &other) = delete;
//Field_& operator=(const Field_ &) = delete;
Field_() = default;
Field_(const T &def):val(def){}
template <typename DumperType>
Field_(Dumper<DumperType> &l){
char s[32] = {0};
if constexpr(sizeof(T) == 8 && ((T)0.1 == (T)0.0))
SBPrint(s, "%lld", (unsigned long long)l.GetData(this).val);
else
SBPrint(s, "%f", (double)l.GetData(this).val);
l.d.Dump(SubStr(NAME,nlen), SubStrB(s));
}
};
#define StringField(name, len) \
constexpr static const char fld_name_##name[] = #name; \
StringField_<fld_name_##name, sizeof(fld_name_##name) - 1, len> name
template <const auto &NAME, size_t nlen, size_t len>
struct StringField_
{
constexpr static const char *name = NAME;
char val[len];
//StringField_(const StringField_ &other) = delete;
//StringField_& operator=(const StringField_ &) = delete;
StringField_() = default;
StringField_(const SubStr &def):val(){ def.CopyTo(val);}
template <typename DumperType>
StringField_(Dumper<DumperType> &l){
l.d.Dump(SubStr(NAME,nlen), SubStrB(l.GetData(this).val));
}
};
struct AppReg
{
constexpr static EventType type = EVENT_APP_REGISTER;
StringField(name,32);
Field(unsigned int, version);
StringField(engine,32);
};
struct AppSession
{
constexpr static EventType type = EVENT_APP_SESSION;
Field(unsigned long long, handle);
Field(unsigned int, state);
};
struct AppVar
{
constexpr static EventType type = EVENT_APP_VAR;
StringField(name,32);
Field(float,value);
};
struct AppAction
{
constexpr static EventType type = EVENT_APP_ACTION;
Field(unsigned long long,handle);
Field(unsigned long long,session);
Field(int, actionType);
StringField(setName,32);
StringField(actName,32);
StringField(description,32);
// todo: subactions info
};
struct AppActionSet
{
constexpr static EventType type = EVENT_APP_ACTION_SET;
Field(unsigned long long,handle);
Field(unsigned long long,session);
StringField(setName,32);
StringField(description,32);
};
struct AppBinding
{
constexpr static EventType type = EVENT_APP_BINDING;
Field(int, index);
StringField(actName, 32);
StringField(setName,32);
Field(unsigned long long,session);
StringField(path,64);
StringField(description,64);
};
struct AppSource
{
constexpr static EventType type = EVENT_APP_SOURCE;
Field(unsigned long long,session);
StringField(name,32);
Field(int, index);
Field(float,x);
Field(float,y);
Field(unsigned long long, lastChangeTime);
Field(bool, changedSinceLastSync);
Field(bool, isActive);
Field(int, stype);
};
struct AppRPN
{
constexpr static EventType type = EVENT_APP_RPN;
Field(unsigned long long,session);
StringField(source,64);
StringField(rpn,64);
};
struct AppActionMap
{
constexpr static EventType type = EVENT_APP_ACTION_MAP;
Field(unsigned long long,session);
Field(unsigned long long,handle);
StringField(actName,32);
Field(unsigned char,hand);
Field(int,mapIndex);
Field(unsigned char,handIndex);
Field(int,actionIndex1);
Field(short,funcIndex1);
Field(unsigned char,axisIndex1);
Field(unsigned char,handIndex1);
Field(int,actionIndex2);
Field(short,funcIndex2);
Field(unsigned char,axisIndex2);
Field(unsigned char,handIndex2);
Field(float, x);
Field(float, y);
Field(bool, override);
Field(bool, hasAxisMapping);
};
struct AppCustomAction
{
constexpr static EventType type = EVENT_APP_CUSTOM_ACTION;
Field(unsigned long long,session);
Field(int, index);
Field(int, cmdIndex);
Field(bool, hasCondition);
Field(bool, hasVariables);
Field(float, triggerPeriod);
Field(float, lastTrigger);
};
struct DiagMsg
{
constexpr static EventType type = EVENT_DIAGMSG;
Field(unsigned int,level);
Field(unsigned int,code);
StringField(message, 64);
};
struct ClientReg
{
constexpr static EventType type = EVENT_CLIENT_REGISTER;
Field(bool,gui);
};
struct EventHeader
{
unsigned char target;
EventType type;
char displayName[14];
pid_t targetPid, sourcePid;
};
struct EventPacket
{
EventHeader head;
union EventData
{
Command cmd;
AppReg appReg;
AppVar appVar;
AppSession appSession;
AppAction appAction;
AppActionSet appActionSet;
AppBinding appBinding;
AppSource appSource;
AppRPN appRPN;
AppActionMap appActionMap;
AppCustomAction appCustomAction;
DiagMsg diagMsg;
ClientReg clReg;
} data;
};
// todo: polymorphic storage/array/growarray/queue?
template <typename Handler, typename T, typename... Ts>
static void HandlePacketImpl(const Handler &h, const EventPacket &p)
{
if(p.head.type == T::type)
h.Handle(*(T*)&p.data);
else
HandlePacketImpl<Handler, Ts...>(h, p);
}
template <typename Handler>static void HandlePacketImpl(const Handler &h, const EventPacket &p){}
template <typename Handler>
static void HandlePacket(const Handler &h, const EventPacket &p)
{
HandlePacketImpl<Handler, AppReg, AppVar, AppSession, AppAction, AppActionSet, AppBinding, AppSource, AppRPN, AppActionMap, AppCustomAction, DiagMsg, ClientReg>(h, p);
}
static unsigned long long GetTimeU64()
{
static unsigned long long startTime = 0;
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
if(!startTime)
startTime = ts.tv_sec;
ts.tv_sec -= startTime;
return ts.tv_sec*1e9 + ts.tv_nsec;
}
#endif // LAYER_EVENTS_H