Debian Codegraph @

src/gxr-manifest.c

raw symbols SCIP
1 /*
2 * gxr
3 * Copyright 2019 Collabora Ltd.
4 * Author: Christoph Haag <[email protected]>
5 * SPDX-License-Identifier: MIT
6 */
7
8 #include "gxr-manifest.h"
9
10 #include <json-glib/json-glib.h>
11
12 /*
13 * actions are saved in a hashmap
14 * (gchar)action name ->
15 * (struct){
16 * (GxrBindingType)type,
17 * (GxrBindingMode)mode
18 * (GList (GxrBindingInputPath))input_path,
19 * }
20 *
21 * example:
22 * "/actions/wm/in/grab_window" ->
23 * {
24 * type: BINDING_TYPE_BOOLEAN,
25 * mode: "button"
26 * [
27 * {
28 * component: "click",
29 * input path: [/user/hand/left/input/trigger, /user/hand/right/input/trigger],
30 * }
31 * ]
32 * }
33 */
34
35 struct _GxrManifest
36 {
37 GObject parent;
38
39 gchar *interaction_profile;
40 int num_inputs;
41 GHashTable *actions;
42 };
43
44 G_DEFINE_TYPE (GxrManifest, gxr_manifest, G_TYPE_OBJECT)
45
46 static void
47 gxr_manifest_finalize (GObject *gobject);
48
49 static void
50 gxr_manifest_class_init (GxrManifestClass *klass)
51 {
52 GObjectClass *object_class = G_OBJECT_CLASS (klass);
53
54 object_class->finalize = gxr_manifest_finalize;
55 }
56
57 static void
58 _free_input_path (gpointer data)
59 {
60 GxrBindingPath *path = data;
61 g_free (path->path);
62 g_free (path);
63 }
64
65 static void
66 _free_binding (gpointer data)
67 {
68 GxrBinding *binding = data;
69 g_list_free_full (binding->input_paths, _free_input_path);
70 g_free (binding);
71 }
72
73 static void
74 gxr_manifest_init (GxrManifest *self)
75 {
76 self->actions = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, _free_binding);
77 self->interaction_profile = NULL;
78 self->num_inputs = 0;
79 }
80
81 static GxrBindingType
82 _get_binding_type (const gchar *type_string)
83 {
84 if (g_str_equal (type_string, "boolean"))
85 return GXR_BINDING_TYPE_BOOLEAN;
86 if (g_str_equal (type_string, "vector1"))
87 return GXR_BINDING_TYPE_FLOAT;
88 if (g_str_equal (type_string, "vector2"))
89 return GXR_BINDING_TYPE_VEC2;
90 if (g_str_equal (type_string, "pose"))
91 return GXR_BINDING_TYPE_POSE;
92 if (g_str_equal (type_string, "vibration"))
93 return GXR_BINDING_TYPE_HAPTIC;
94
95 g_print ("Binding type %s is not known\n", type_string);
96 return GXR_BINDING_TYPE_UNKNOWN;
97 }
98
99 static GxrBindingMode
100 _get_binding_mode (const gchar *mode_string)
101 {
102 if (mode_string == NULL)
103 return GXR_BINDING_MODE_NONE;
104 if (g_str_equal (mode_string, "button"))
105 return GXR_BINDING_MODE_BUTTON;
106 if (g_str_equal (mode_string, "trackpad"))
107 return GXR_BINDING_MODE_TRACKPAD;
108 if (g_str_equal (mode_string, "joystick"))
109 return GXR_BINDING_MODE_ANALOG_STICK;
110
111 g_print ("Binding mode %s is not known\n", mode_string);
112 return GXR_BINDING_MODE_UNKNOWN;
113 }
114
115 static GxrBindingComponent
116 _get_binding_component (const gchar *component_string)
117 {
118 if (g_str_equal (component_string, "click"))
119 return GXR_BINDING_COMPONENT_CLICK;
120 if (g_str_equal (component_string, "pull"))
121 return GXR_BINDING_COMPONENT_PULL;
122 if (g_str_equal (component_string, "position"))
123 return GXR_BINDING_COMPONENT_POSITION;
124 if (g_str_equal (component_string, "touch"))
125 return GXR_BINDING_COMPONENT_TOUCH;
126
127 g_print ("Binding component %s is not known\n", component_string);
128 return GXR_BINDING_COMPONENT_UNKNOWN;
129 }
130
131 static gboolean
132 _parse_actions (GxrManifest *self, GInputStream *stream)
133 {
134 GError *error = NULL;
135
136 JsonParser *parser = json_parser_new ();
137 json_parser_load_from_stream (parser, stream, NULL, &error);
138 if (error)
139 {
140 g_print ("Unable to parse actions: %s\n", error->message);
141 g_error_free (error);
142 g_object_unref (parser);
143 return FALSE;
144 }
145
146 JsonNode *jnroot = json_parser_get_root (parser);
147
148 JsonObject *joroot;
149 joroot = json_node_get_object (jnroot);
150
151 JsonArray *joactions = json_object_get_array_member (joroot, "actions");
152
153 guint len = json_array_get_length (joactions);
154 g_debug ("parsing %d actions\n", len);
155
156 for (guint i = 0; i < len; i++)
157 {
158 JsonObject *joaction = json_array_get_object_element (joactions, i);
159 const gchar *name = json_object_get_string_member (joaction, "name");
160 const gchar *binding_type = json_object_get_string_member (joaction, "type");
161
162 g_debug ("Parsed action %s: %s\n", name, binding_type);
163
164 GxrBinding *binding = g_malloc (sizeof (GxrBinding));
165 binding->input_paths = NULL;
166 binding->mode = GXR_BINDING_MODE_UNKNOWN;
167 binding->type = _get_binding_type (binding_type);
168
169 g_hash_table_insert (self->actions, g_strdup (name), binding);
170 }
171 g_object_unref (parser);
172
173 return TRUE;
174 }
175
176 static gboolean
177 _parse_bindings (GxrManifest *self, GInputStream *stream)
178 {
179 GError *error = NULL;
180
181 JsonParser *parser = json_parser_new ();
182 json_parser_load_from_stream (parser, stream, NULL, &error);
183 if (error)
184 {
185 g_print ("Unable to parse bindings: %s\n", error->message);
186 g_error_free (error);
187 g_object_unref (parser);
188 return FALSE;
189 }
190
191 JsonNode *jnroot = json_parser_get_root (parser);
192
193 JsonObject *joroot;
194 joroot = json_node_get_object (jnroot);
195
196 if (json_object_has_member (joroot, "interaction_profile"))
197 self->interaction_profile = g_strdup (json_object_get_string_member (joroot, "interaction_profile"));
198
199
200 JsonObject *jobinding = json_object_get_object_member (joroot, "bindings");
201
202 GList *binding_list = json_object_get_members (jobinding);
203 for (GList *l = binding_list; l != NULL; l = l->next)
204 {
205 const gchar *actionset = l->data;
206 g_debug ("Parsing Action Set %s\n", actionset);
207
208 JsonObject *joactionset = json_object_get_object_member (jobinding, actionset);
209
210 if (json_object_has_member (joactionset, "sources"))
211 {
212 JsonArray *jasources = json_object_get_array_member (joactionset, "sources");
213 guint sources_len = json_array_get_length (jasources);
214 for (guint i = 0; i < sources_len; i++)
215 {
216 JsonObject *josource = json_array_get_object_element (jasources, i);
217
218 const gchar *path = json_object_get_string_member (josource, "path");
219 const gchar *mode = NULL;
220 if (json_object_has_member (josource, "mode"))
221 json_object_get_string_member (josource, "mode");
222
223 g_debug ("Parsed path %s with mode %s\n", path, mode);
224
225 JsonObject *joinputs = json_object_get_object_member (josource, "inputs");
226
227 GList *input_list = json_object_get_members (joinputs);
228 for (GList *m = input_list; m != NULL; m = m->next)
229 {
230 gchar *component = m->data;
231 g_debug ("Parsing component %s\n", component);
232
233 JsonObject *joinput = json_object_get_object_member (joinputs, component);
234
235 const gchar *output = json_object_get_string_member (joinput, "output");
236
237 g_debug ("%s: Parsed output %s for component %s\n", path, output, component);
238
239 GxrBinding *binding = g_hash_table_lookup (self->actions, output);
240 if (!binding)
241 {
242 g_print ("Binding: Failed to find action %s in paresed actions\n", output);
243 continue;
244 }
245
246 binding->mode = _get_binding_mode (mode);
247 GxrBindingPath *input_path = g_malloc (sizeof (GxrBindingPath));
248 input_path->component = _get_binding_component (component);
249 input_path->path = g_strdup (path);
250 g_debug ("Parsed input path %s\n", input_path->path);
251 binding->input_paths = g_list_append (binding->input_paths, input_path);
252
253 self->num_inputs++;
254 }
255 g_list_free (input_list);
256 }
257 }
258
259 if (json_object_has_member (joactionset, "haptics"))
260 {
261 JsonArray *jahaptics = json_object_get_array_member (joactionset, "haptics");
262 guint haptics_len = json_array_get_length (jahaptics);
263 for (guint i = 0; i < haptics_len; i++)
264 {
265 JsonObject *johaptics = json_array_get_object_element (jahaptics, i);
266
267 const gchar *path = json_object_get_string_member (johaptics, "path");
268 const gchar *output = json_object_get_string_member (johaptics, "output");
269
270 g_debug ("%s: Parsed haptics %s \n", path, output);
271
272 GxrBinding *binding = g_hash_table_lookup (self->actions, output);
273 if (!binding)
274 {
275 g_print ("Binding: Failed to find action %s in parsed actions\n", output);
276 continue;
277 }
278
279 binding->mode = GXR_BINDING_MODE_NONE;
280 GxrBindingPath *output_path = g_malloc (sizeof (GxrBindingPath));
281 output_path->component = GXR_BINDING_COMPONENT_NONE;
282 output_path->path = g_strdup (path);
283 g_debug ("Parsed input path %s\n", output_path->path);
284 binding->input_paths = g_list_append (binding->input_paths, output_path);
285
286 self->num_inputs++;
287 }
288 }
289
290 if (json_object_has_member (joactionset, "pose"))
291 {
292 JsonArray *japose = json_object_get_array_member (joactionset, "pose");
293 guint pose_len = json_array_get_length (japose);
294 for (guint i = 0; i < pose_len; i++)
295 {
296 JsonObject *jopose = json_array_get_object_element (japose, i);
297
298 const gchar *path = json_object_get_string_member (jopose, "path");
299 const gchar *output = json_object_get_string_member (jopose, "output");
300
301 g_debug ("%s: Parsed output pose %s \n", path, output);
302
303 GxrBinding *binding = g_hash_table_lookup (self->actions, output);
304 if (!binding)
305 {
306 g_print ("Binding: Failed to find action %s in parsed actions\n", output);
307 continue;
308 }
309
310 binding->mode = GXR_BINDING_MODE_NONE;
311 GxrBindingPath *input_path = g_malloc (sizeof (GxrBindingPath));
312 input_path->component = GXR_BINDING_COMPONENT_NONE;
313 input_path->path = g_strdup (path);
314 g_debug ("Parsed input path %s\n", input_path->path);
315 binding->input_paths = g_list_append (binding->input_paths, input_path);
316
317 self->num_inputs++;
318 }
319 }
320 }
321
322 g_list_free (binding_list);
323
324 g_object_unref (parser);
325
326 return TRUE;
327 }
328
329 gboolean
330 gxr_manifest_load (GxrManifest *self,
331 GInputStream *action_stream,
332 GInputStream *binding_stream)
333 {
334 if (!_parse_actions (self, action_stream))
335 return FALSE;
336
337 if (!_parse_bindings (self, binding_stream))
338 return FALSE;
339
340 return TRUE;
341 }
342
343 GxrManifest *
344 gxr_manifest_new (void)
345 {
346 return (GxrManifest *) g_object_new (GXR_TYPE_MANIFEST, 0);
347 }
348
349 gchar *
350 gxr_manifest_get_interaction_profile (GxrManifest *self)
351 {
352 return self->interaction_profile;
353 }
354
355 /* TODO: might want to make a better API */
356 GHashTable *
357 gxr_manifest_get_hash_table (GxrManifest *self)
358 {
359 return self->actions;
360 }
361
362 int
363 gxr_manifest_get_num_inputs (GxrManifest *self)
364 {
365 return self->num_inputs;
366 }
367
368 static void
369 gxr_manifest_finalize (GObject *gobject)
370 {
371 GxrManifest *self = GXR_MANIFEST (gobject);
372 g_hash_table_unref (self->actions);
373 if (self->interaction_profile)
374 g_free (self->interaction_profile);
375 (void) self;
376 }