Debian Codegraph @

3rdparty/osgXR/src/Action.cpp

raw symbols SCIP
1 // SPDX-License-Identifier: LGPL-2.1-only
2 // Copyright (C) 2021 James Hogan <[email protected]>
3
4 #include "Action.h"
5 #include "ActionSet.h"
6
7 #include "OpenXR/Action.h"
8 #include "OpenXR/Session.h"
9 #include "OpenXR/Space.h"
10
11 #include <map>
12
13 using namespace osgXR;
14
15 // Internal API
16
17 Action::Private::Private(ActionSet *actionSet) :
18 _actionSet(actionSet),
19 _updated(true)
20 {
21 ActionSet::Private::get(_actionSet)->registerAction(this);
22 }
23
24 Action::Private::~Private()
25 {
26 ActionSet::Private::get(_actionSet)->unregisterAction(this);
27 }
28
29 void Action::Private::setName(const std::string &name)
30 {
31 _updated = true;
32 _name = name;
33 }
34
35 const std::string &Action::Private::getName() const
36 {
37 return _name;
38 }
39
40 void Action::Private::setLocalizedName(const std::string &localizedName)
41 {
42 _updated = true;
43 _localizedName = localizedName;
44 }
45
46 const std::string &Action::Private::getLocalizedName() const
47 {
48 return _localizedName;
49 }
50
51 void Action::Private::addSubaction(std::shared_ptr<Subaction::Private> subaction)
52 {
53 _updated = true;
54 _subactions.insert(subaction);
55 }
56
57 void Action::Private::cleanupInstance()
58 {
59 _updated = true;
60 _action = nullptr;
61 }
62
63 void Action::Private::getBoundSources(std::vector<std::string> &sourcePaths) const
64 {
65 OpenXR::Session *session = ActionSet::Private::get(_actionSet)->getSession();
66 if (_action.valid() && session)
67 {
68 std::vector<XrPath> paths;
69 if (session->getActionBoundSources(_action, paths))
70 {
71 // Convert XrPath's into std::string's
72 OpenXR::Instance *instance = session->getInstance();
73 sourcePaths.resize(paths.size());
74 for (unsigned int i = 0; i < paths.size(); ++i)
75 sourcePaths[i] = OpenXR::Path(instance, paths[i]).toString();
76
77 // Success!
78 return;
79 }
80 }
81
82 // Failure, clear output
83 sourcePaths.resize(0);
84 }
85
86 void Action::Private::getBoundSourcesLocalizedNames(XrInputSourceLocalizedNameFlags whichComponents,
87 std::vector<std::string> &names) const
88 {
89 OpenXR::Session *session = ActionSet::Private::get(_actionSet)->getSession();
90 if (_action.valid() && session)
91 {
92 std::vector<XrPath> paths;
93 if (session->getActionBoundSources(_action, paths))
94 {
95 // Convert XrPath's into localized names
96 names.resize(paths.size());
97 for (unsigned int i = 0; i < paths.size(); ++i)
98 names[i] = session->getInputSourceLocalizedName(paths[i],
99 whichComponents);
100
101 // Success!
102 return;
103 }
104 }
105
106 // Failure, clear output
107 names.resize(0);
108 }
109
110 namespace osgXR {
111
112 template <typename T>
113 class ActionPrivateCommon : public Action::Private
114 {
115 public:
116
117 typedef typename T::State State;
118
119 ActionPrivateCommon(ActionSet *actionSet) :
120 Private(actionSet)
121 {
122 }
123
124 void cleanupSession() override
125 {
126 _states.clear();
127 }
128
129 OpenXR::Action *setup(OpenXR::Instance *instance) override
130 {
131 OpenXR::ActionSet *actionSet = ActionSet::Private::get(_actionSet)->setup(instance);
132 if (!actionSet)
133 {
134 // Can't continue without an action set
135 _action = nullptr;
136 _updated = true;
137 }
138 else if (_updated || actionSet != _action->getActionSet())
139 {
140 _action = new T(actionSet, _name, _localizedName);
141 for (auto &subaction: _subactions)
142 _action->addSubaction(subaction->setup(instance));
143 _updated = false;
144 }
145 return _action;
146 }
147
148 State *getState(Subaction::Private *subaction = nullptr)
149 {
150 auto it = _states.find(subaction);
151 if (it != _states.end())
152 return (*it).second.get();
153
154 OpenXR::Session *session = ActionSet::Private::get(_actionSet)->getSession();
155 if (session)
156 {
157 OpenXR::Path subactionPath;
158 if (subaction)
159 subactionPath = subaction->setup(session->getInstance());
160 OpenXR::Action *action = setup(session->getInstance());
161 if (action)
162 {
163 osg::ref_ptr<State> ret = static_cast<T*>(_action.get())->createState(session,
164 subactionPath);
165 _states[subaction] = ret;
166 return ret.get();
167 }
168 }
169 return nullptr;
170 }
171
172 protected:
173
174 std::map<Subaction::Private *, osg::ref_ptr<State>> _states;
175 };
176
177 template <typename T>
178 class ActionPrivateSimple : public ActionPrivateCommon<T>
179 {
180 public:
181
182 typedef typename T::State State;
183
184 ActionPrivateSimple(ActionSet *actionSet) :
185 ActionPrivateCommon<T>(actionSet)
186 {
187 }
188
189 auto getValue(Subaction::Private *subaction)
190 {
191 State *state = this->getState(subaction);
192 if (state && state->update() && state->isActive())
193 return state->getCurrentState();
194 else
195 return T::State::Info::defaultValue();
196 }
197 };
198
199 typedef ActionPrivateSimple<OpenXR::ActionBoolean> ActionPrivateBoolean;
200 typedef ActionPrivateSimple<OpenXR::ActionFloat> ActionPrivateFloat;
201 typedef ActionPrivateSimple<OpenXR::ActionVector2f> ActionPrivateVector2f;
202
203 class ActionPrivatePose : public ActionPrivateCommon<OpenXR::ActionPose>
204 {
205 public:
206
207 typedef OpenXR::ActionPose::State State;
208
209 ActionPrivatePose(ActionSet *actionSet) :
210 ActionPrivateCommon(actionSet)
211 {
212 }
213
214 OpenXR::Space *getSpace(Subaction::Private *subaction)
215 {
216 State *state = getState(subaction);
217 if (state && state->update() && state->isActive())
218 return state->getSpace();
219 else
220 return nullptr;
221 }
222
223 bool locate(Subaction::Private *subaction,
224 ActionPose::Location &location)
225 {
226 OpenXR::Space *space = getSpace(subaction);
227 OpenXR::Session *session = ActionSet::Private::get(_actionSet)->getSession();
228 if (session && space)
229 {
230 OpenXR::Space::Location loc;
231 bool ret = space->locate(session->getLocalSpace(), session->getLastDisplayTime(),
232 loc);
233 location = ActionPose::Location((ActionPose::Location::Flags)loc.getFlags(),
234 loc.getOrientation(),
235 loc.getPosition());
236 return ret;
237 }
238 else
239 {
240 location = ActionPose::Location();
241 return false;
242 }
243 }
244 };
245
246 class ActionPrivateVibration : public ActionPrivateCommon<OpenXR::ActionVibration>
247 {
248 public:
249
250 typedef OpenXR::ActionVibration::State State;
251
252 ActionPrivateVibration(ActionSet *actionSet) :
253 ActionPrivateCommon(actionSet)
254 {
255 }
256
257 bool applyHapticFeedback(Subaction::Private *subaction,
258 int64_t duration_ns, float frequency,
259 float amplitude)
260 {
261 State *state = getState(subaction);
262 if (!state)
263 return false;
264 return state->applyHapticFeedback(duration_ns, frequency,
265 amplitude);
266 }
267
268 bool stopHapticFeedback(Subaction::Private *subaction)
269 {
270 State *state = getState(subaction);
271 if (!state)
272 return false;
273 return state->stopHapticFeedback();
274 }
275 };
276
277 }
278
279 // Public API
280
281 Action::Action(Private *priv) :
282 _private(priv)
283 {
284 }
285
286 Action::~Action()
287 {
288 }
289
290 void Action::addSubaction(Subaction *subaction)
291 {
292 _private->addSubaction(Subaction::Private::get(subaction));
293 }
294
295 void Action::setName(const std::string &name,
296 const std::string &localizedName)
297 {
298 _private->setName(name);
299 _private->setLocalizedName(localizedName);
300 }
301
302 void Action::setName(const std::string &name)
303 {
304 _private->setName(name);
305 }
306
307 const std::string &Action::getName() const
308 {
309 return _private->getName();
310 }
311
312 void Action::setLocalizedName(const std::string &localizedName)
313 {
314 _private->setLocalizedName(localizedName);
315 }
316
317 const std::string &Action::getLocalizedName() const
318 {
319 return _private->getLocalizedName();
320 }
321
322 void Action::getBoundSources(std::vector<std::string> &sourcePaths) const
323 {
324 _private->getBoundSources(sourcePaths);
325 }
326
327 void Action::getBoundSourcesLocalizedNames(uint32_t whichComponents,
328 std::vector<std::string> &names) const
329 {
330 _private->getBoundSourcesLocalizedNames(whichComponents, names);
331 }
332
333 // ActionBoolean
334
335 ActionBoolean::ActionBoolean(ActionSet *actionSet) :
336 Action(new ActionPrivateBoolean(actionSet))
337 {
338 }
339
340 ActionBoolean::ActionBoolean(ActionSet *actionSet,
341 const std::string &name) :
342 Action(new ActionPrivateBoolean(actionSet))
343 {
344 setName(name, name);
345 }
346
347 ActionBoolean::ActionBoolean(ActionSet *actionSet,
348 const std::string &name,
349 const std::string &localizedName) :
350 Action(new ActionPrivateBoolean(actionSet))
351 {
352 setName(name, localizedName);
353 }
354
355 bool ActionBoolean::getValue(Subaction *subaction)
356 {
357 auto privSubaction = Subaction::Private::get(subaction);
358 return static_cast<ActionPrivateBoolean *>(Private::get(this))->getValue(privSubaction.get());
359 }
360
361 // ActionFloat
362
363 ActionFloat::ActionFloat(ActionSet *actionSet) :
364 Action(new ActionPrivateFloat(actionSet))
365 {
366 }
367
368 ActionFloat::ActionFloat(ActionSet *actionSet,
369 const std::string &name) :
370 Action(new ActionPrivateFloat(actionSet))
371 {
372 setName(name, name);
373 }
374
375 ActionFloat::ActionFloat(ActionSet *actionSet,
376 const std::string &name,
377 const std::string &localizedName) :
378 Action(new ActionPrivateFloat(actionSet))
379 {
380 setName(name, localizedName);
381 }
382
383 float ActionFloat::getValue(Subaction *subaction)
384 {
385 auto privSubaction = Subaction::Private::get(subaction);
386 return static_cast<ActionPrivateFloat *>(Private::get(this))->getValue(privSubaction.get());
387 }
388
389 // ActionVector2f
390
391 ActionVector2f::ActionVector2f(ActionSet *actionSet) :
392 Action(new ActionPrivateVector2f(actionSet))
393 {
394 }
395
396 ActionVector2f::ActionVector2f(ActionSet *actionSet,
397 const std::string &name) :
398 Action(new ActionPrivateVector2f(actionSet))
399 {
400 setName(name, name);
401 }
402
403 ActionVector2f::ActionVector2f(ActionSet *actionSet,
404 const std::string &name,
405 const std::string &localizedName) :
406 Action(new ActionPrivateVector2f(actionSet))
407 {
408 setName(name, localizedName);
409 }
410
411 osg::Vec2f ActionVector2f::getValue(Subaction *subaction)
412 {
413 auto privSubaction = Subaction::Private::get(subaction);
414 return static_cast<ActionPrivateVector2f *>(Private::get(this))->getValue(privSubaction.get());
415 }
416
417 // ActionPose
418
419 ActionPose::ActionPose(ActionSet *actionSet) :
420 Action(new ActionPrivatePose(actionSet))
421 {
422 }
423
424 ActionPose::ActionPose(ActionSet *actionSet,
425 const std::string &name) :
426 Action(new ActionPrivatePose(actionSet))
427 {
428 setName(name, name);
429 }
430
431 ActionPose::ActionPose(ActionSet *actionSet,
432 const std::string &name,
433 const std::string &localizedName) :
434 Action(new ActionPrivatePose(actionSet))
435 {
436 setName(name, localizedName);
437 }
438
439 ActionPose::Location ActionPose::getValue(Subaction *subaction)
440 {
441 Location location;
442 auto privSubaction = Subaction::Private::get(subaction);
443 static_cast<ActionPrivatePose *>(Private::get(this))->locate(privSubaction.get(),
444 location);
445 return location;
446 }
447
448 ActionPose::Location::Location() :
449 _flags((Flags)0)
450 {
451 }
452
453 ActionPose::Location::Location(Flags flags,
454 const osg::Quat &orientation,
455 const osg::Vec3f &position) :
456 _flags(flags),
457 _orientation(orientation),
458 _position(position)
459 {
460 }
461
462 // ActionVibration
463
464 ActionVibration::ActionVibration(ActionSet *actionSet) :
465 Action(new ActionPrivateVibration(actionSet))
466 {
467 }
468
469 ActionVibration::ActionVibration(ActionSet *actionSet,
470 const std::string &name) :
471 Action(new ActionPrivateVibration(actionSet))
472 {
473 setName(name, name);
474 }
475
476 ActionVibration::ActionVibration(ActionSet *actionSet,
477 const std::string &name,
478 const std::string &localizedName) :
479 Action(new ActionPrivateVibration(actionSet))
480 {
481 setName(name, localizedName);
482 }
483
484 bool ActionVibration::applyHapticFeedback(int64_t duration_ns, float frequency,
485 float amplitude)
486 {
487 auto priv = static_cast<ActionPrivateVibration *>(Private::get(this));
488 return priv->applyHapticFeedback(nullptr, duration_ns, frequency,
489 amplitude);
490 }
491
492 bool ActionVibration::applyHapticFeedback(Subaction *subaction,
493 int64_t duration_ns, float frequency,
494 float amplitude)
495 {
496 auto privSubaction = Subaction::Private::get(subaction);
497 auto priv = static_cast<ActionPrivateVibration *>(Private::get(this));
498 return priv->applyHapticFeedback(privSubaction.get(), duration_ns, frequency,
499 amplitude);
500 }
501
502 bool ActionVibration::stopHapticFeedback(Subaction *subaction)
503 {
504 auto privSubaction = Subaction::Private::get(subaction);
505 auto priv = static_cast<ActionPrivateVibration *>(Private::get(this));
506 return priv->stopHapticFeedback(privSubaction.get());
507 }