OSVR Framework (Internal Development Docs)  0.6-1962-g59773924
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
JointClientKitC.cpp
Go to the documentation of this file.
1 
11 // Copyright 2015 Sensics, Inc.
12 //
13 // Licensed under the Apache License, Version 2.0 (the "License");
14 // you may not use this file except in compliance with the License.
15 // You may obtain a copy of the License at
16 //
17 // http://www.apache.org/licenses/LICENSE-2.0
18 //
19 // Unless required by applicable law or agreed to in writing, software
20 // distributed under the License is distributed on an "AS IS" BASIS,
21 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22 // See the License for the specific language governing permissions and
23 // limitations under the License.
24 
25 // Internal Includes
27 #include "JointClientContext.h"
28 #include <osvr/Server/Server.h>
29 #include <osvr/Util/MacroToolsC.h>
30 #include <osvr/Util/Verbosity.h>
31 
32 // Library/third-party includes
33 // - none
34 
35 // Standard includes
36 #include <functional>
37 #include <vector>
38 #include <memory>
39 #include <string>
40 
42 
43 using ServerOp = std::function<void(Server &)>;
44 
46  void apply(osvr::server::Server &s) {
47  for (auto &op : operations) {
48  op(s);
49  }
50  }
51 
52  template <typename F> void enqueue(F &&f) {
53  operations.emplace_back(std::forward<F>(f));
54  }
55  std::vector<ServerOp> operations;
56 
57  bool haveAutoload = false;
58 };
59 
62 }
63 
64 #define OSVR_CHECK_OPTS \
65  OSVR_UTIL_MULTILINE_BEGIN \
66  if (nullptr == opts) { \
67  OSVR_DEV_VERBOSE( \
68  "Can't modify a null JointClientKit options object!"); \
69  return OSVR_RETURN_FAILURE; \
70  } \
71  OSVR_UTIL_MULTILINE_END
72 
73 #define OSVR_OPTS_SINGLE_SHOT(MEMBER, DESC) \
74  OSVR_UTIL_MULTILINE_BEGIN \
75  if (opts->MEMBER) { \
76  OSVR_DEV_VERBOSE("Already queued " DESC \
77  " -- can't do it a second time!"); \
78  return OSVR_RETURN_FAILURE; \
79  } \
80  opts->MEMBER = true; \
81  OSVR_UTIL_MULTILINE_END
82 
83 #define OSVR_OPTS_CHECK_STRING(PARAM, DESC) \
84  OSVR_UTIL_MULTILINE_BEGIN \
85  if (nullptr == (PARAM) || '\0' == (PARAM)[0]) { \
86  OSVR_DEV_VERBOSE("Invalid string passed for " DESC \
87  " -- can't be null or empty!"); \
88  return OSVR_RETURN_FAILURE; \
89  } \
90  OSVR_UTIL_MULTILINE_END
91 
92 OSVR_ReturnCode
94  OSVR_CHECK_OPTS;
95  OSVR_OPTS_SINGLE_SHOT(haveAutoload, "autoload of plugins");
96  opts->enqueue([](Server &s) { s.loadAutoPlugins(); });
97  return OSVR_RETURN_SUCCESS;
98 }
99 
101  const char *pluginName) {
102  OSVR_CHECK_OPTS;
103  OSVR_OPTS_CHECK_STRING(pluginName, "plugin name");
104  auto name = std::string{pluginName};
105  opts->enqueue([name](Server &s) { s.loadPlugin(name); });
106  return OSVR_RETURN_SUCCESS;
107 }
108 
110  OSVR_JointClientOpts opts, const char *pluginName, const char *driverName,
111  const char *params) {
112  OSVR_CHECK_OPTS;
113  OSVR_OPTS_CHECK_STRING(pluginName, "plugin name");
114  OSVR_OPTS_CHECK_STRING(driverName, "driver name");
115  auto plugin = std::string{pluginName};
116  auto driver = std::string{driverName};
117  auto p = std::string{(params ? params : "")};
118  opts->enqueue([plugin, driver, p](Server &s) {
119  s.instantiateDriver(plugin, driver, p);
120  });
121  return OSVR_RETURN_SUCCESS;
122 }
123 
125  const char *path,
126  const char *source) {
127  OSVR_CHECK_OPTS;
128  OSVR_OPTS_CHECK_STRING(path, "path");
129  OSVR_OPTS_CHECK_STRING(source, "source");
130  auto p = std::string{path};
131  auto src = std::string{source};
132  opts->enqueue([p, src](Server &s) { s.addAlias(p, src); });
133  return OSVR_RETURN_SUCCESS;
134 }
135 
137  const char *aliases) {
138  OSVR_CHECK_OPTS;
139  OSVR_OPTS_CHECK_STRING(aliases, "aliases JSON");
140  auto json = std::string{aliases};
141  opts->enqueue([json](Server &s) { s.addAliases(json); });
142  return OSVR_RETURN_SUCCESS;
143 }
144 
146  const char *path,
147  const char *s) {
148  OSVR_CHECK_OPTS;
149  OSVR_OPTS_CHECK_STRING(path, "path");
150  OSVR_OPTS_CHECK_STRING(s, "string value");
151  auto p = std::string{path};
152  auto str = std::string{s};
153  opts->enqueue([p, str](Server &s) { s.addString(p, str); });
154  return OSVR_RETURN_SUCCESS;
155 }
156 
157 OSVR_ReturnCode
159  OSVR_CHECK_OPTS;
160  opts->enqueue([](Server &s) { s.triggerHardwareDetect(); });
161  return OSVR_RETURN_SUCCESS;
162 }
163 
164 OSVR_ClientContext osvrJointClientInit(const char applicationIdentifier[],
165  OSVR_JointClientOpts opts) {
166  try {
167  using OptionPtr = std::unique_ptr<OSVR_JointClientContextOptsObject>;
168  // Take ownership of options, if any
169  OptionPtr opt(opts);
170 
171  using JointContextPtr =
172  std::unique_ptr<osvr::client::JointClientContext>;
173  // Make the context.
174  auto ctx = JointContextPtr{
175  osvr::common::makeContext<osvr::client::JointClientContext>(
176  applicationIdentifier)};
177 
178  if (opt) {
179  opt->apply(ctx->getServer());
180  } else {
181  // Default behavior when no options are passed.
182  ctx->getServer().loadAutoPlugins();
183  ctx->getServer().triggerHardwareDetect();
184  }
185  // Transfer ownership to the client app.
186  return ctx.release();
187  } catch (std::exception const &e) {
188  OSVR_DEV_VERBOSE(
189  "Caught an exception in osvrJointClientInit: " << e.what());
190  } catch (...) {
191 
192  OSVR_DEV_VERBOSE(
193  "Caught an unrecognized exception type in osvrJointClientInit.");
194  }
195 
196  return nullptr;
197 }
OSVR_SERVER_EXPORT bool addAliases(Json::Value const &aliases, common::AliasPriority priority=common::ALIASPRIORITY_MANUAL)
Add alias entries to the tree from JSON.
Definition: Server.cpp:120
OSVR_SERVER_EXPORT void triggerHardwareDetect()
Run all hardware detect callbacks.
Definition: Server.cpp:101
OSVR_ReturnCode osvrJointClientOptionsLoadPlugin(OSVR_JointClientOpts opts, const char *pluginName)
Queues up the manual load of a plugin by name.
OSVR_ReturnCode osvrJointClientOptionsAddAlias(OSVR_JointClientOpts opts, const char *path, const char *source)
Queues up the manual addition of an alias to the path tree.
OSVR_SERVER_EXPORT bool addString(std::string const &path, std::string const &value)
Add a string entry to the tree.
Definition: Server.cpp:116
OSVR_ReturnCode osvrJointClientOptionsAddString(OSVR_JointClientOpts opts, const char *path, const char *s)
Queues up the manual addition of a string element to the path tree.
Class handling a run-loop with a registration context and connection.
Definition: Server.h:67
OSVR_SERVER_EXPORT void loadPlugin(std::string const &plugin)
Load plugin by name.
Definition: Server.cpp:85
OSVR_JointClientOpts osvrJointClientCreateOptions()
Creates an empty OSVR_JointClientOpts.
OSVR_ClientContext osvrJointClientInit(const char applicationIdentifier[], OSVR_JointClientOpts opts)
Initialize the library, starting up a "joint" context that also contains a server.
OSVR_SERVER_EXPORT bool addAlias(std::string const &path, std::string const &source, common::AliasPriority priority=common::ALIASPRIORITY_MANUAL)
Add an alias entry to the tree.
Definition: Server.cpp:111
Header declaring osvr::server::Server.
#define OSVR_RETURN_SUCCESS
The "success" value for an OSVR_ReturnCode.
Definition: ReturnCodesC.h:45
OSVR_ReturnCode osvrJointClientOptionsTriggerHardwareDetect(OSVR_JointClientOpts opts)
Queues up a trigger for hardware detection.
OSVR_ReturnCode osvrJointClientOptionsAutoloadPlugins(OSVR_JointClientOpts opts)
Queues up the autoloading of plugins. May only be called once per options object. ...
OSVR_SERVER_EXPORT void loadAutoPlugins()
Load all auto-loadable plugins.
Definition: Server.cpp:89
OSVR_ReturnCode osvrJointClientOptionsAddAliases(OSVR_JointClientOpts opts, const char *aliases)
Queues up the manual addition of aliases specified in JSON to the path tree.
Internal, configured header file for verbosity macros.
OSVR_ReturnCode osvrJointClientOptionsInstantiateDriver(OSVR_JointClientOpts opts, const char *pluginName, const char *driverName, const char *params)
Queues up the manual instantiation of a plugin/driver by name with optional parameters (JSON)...
Header containing basic macro tools.
OSVR_SERVER_EXPORT void instantiateDriver(std::string const &plugin, std::string const &driver, std::string const &params=std::string())
Instantiate the named driver with parameters.
Definition: Server.cpp:95