File: fbd-feedback-vibra-pattern.c

package info (click to toggle)
feedbackd 0.8.9-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,076 kB
  • sloc: ansic: 7,956; xml: 97; makefile: 31; sh: 27; python: 8
file content (358 lines) | stat: -rw-r--r-- 10,509 bytes parent folder | download | duplicates (2)
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
/*
 * Copyright (C) 2024 The Phosh Developers
 *
 * SPDX-License-Identifier: GPL-3.0+

 * Author: Guido Günther <agx@sigxcpu.org>
 */

#define G_LOG_DOMAIN "fbd-feedback-vibra-pattern"

#include "fbd-enums.h"
#include "fbd-feedback-vibra-pattern.h"
#include "fbd-feedback-vibra-priv.h"
#include "fbd-feedback-manager.h"

#include <json-glib/json-glib.h>

/**
 * FbdFeedbackVibraPattern:
 *
 * Describes a pattern feedback via a haptic motor
 *
 * The #FbdVibraVibraPattern describes the properties of a haptic feedback
 * event. It knows nothing about the hardware itself but calls
 * #FbdDevVibra for that.
 */

enum {
  PROP_0,
  PROP_MAGNITUDES,
  PROP_DURATIONS,
  PROP_LAST_PROP,
};
static GParamSpec *props[PROP_LAST_PROP];

typedef struct _FbdFeedbackVibraPattern {
  FbdFeedbackVibra parent;

  GArray          *magnitudes;
  GArray          *durations;
  int              pos;

  guint            timer_id;
} FbdFeedbackVibraPattern;

static void json_serializable_iface_init (JsonSerializableIface *iface);

G_DEFINE_TYPE_WITH_CODE (FbdFeedbackVibraPattern, fbd_feedback_vibra_pattern,
                         FBD_TYPE_FEEDBACK_VIBRA,
                         G_IMPLEMENT_INTERFACE (JSON_TYPE_SERIALIZABLE,
                                                json_serializable_iface_init));


static gboolean
fbd_feedback_vibra_pattern_serializable_deserialize_property (JsonSerializable *serializable,
                                                              const gchar *property_name,
                                                              GValue *value,
                                                              GParamSpec *pspec,
                                                              JsonNode *property_node)
{
  if (g_strcmp0 (property_name, "magnitudes") == 0) {
    if (JSON_NODE_TYPE (property_node) == JSON_NODE_ARRAY) {
      JsonArray *array = json_node_get_array (property_node);
      guint array_len = json_array_get_length (array);
      g_autoptr (GArray) magnitudes = g_array_sized_new (FALSE, FALSE, sizeof (double), array_len);

      for (guint i = 0; i < array_len; i++) {
        JsonNode *element_node = json_array_get_element (array, i);

        if (JSON_NODE_HOLDS_VALUE (element_node)) {
          double magnitude;

          magnitude = json_node_get_double (element_node);
          magnitude = MAX (0.0, MIN (magnitude, 1.0));
          g_array_append_val (magnitudes, magnitude);
        } else {
          return FALSE;
        }
      }
      g_value_set_boxed (value, magnitudes);
      return TRUE;
    }
  } else if (g_strcmp0 (property_name, "durations") == 0) {
    if (JSON_NODE_TYPE (property_node) == JSON_NODE_ARRAY) {
      JsonArray *array = json_node_get_array (property_node);
      guint array_len = json_array_get_length (array);
      g_autoptr (GArray) durations = g_array_sized_new (FALSE, FALSE, sizeof (guint), array_len);

      for (guint i = 0; i < array_len; i++) {
        JsonNode *element_node = json_array_get_element (array, i);

        if (JSON_NODE_HOLDS_VALUE (element_node)) {
          guint duration;

          duration = json_node_get_int (element_node);
          g_array_append_val (durations, duration);
        } else {
          return FALSE;
        }
      }
      g_value_set_boxed (value, durations);
      return TRUE;
    }
  }

  return FALSE;
}


static void
set_magnitudes (FbdFeedbackVibraPattern *self, GArray *magnitudes)
{
  g_clear_pointer (&self->magnitudes, g_array_unref);
  self->magnitudes = g_array_ref (magnitudes);
}


static void
set_durations (FbdFeedbackVibraPattern *self, GArray *durations)
{
  guint total_duration = 0;

  g_clear_pointer (&self->durations, g_array_unref);
  self->durations = g_array_ref (durations);

  for (int i = 0; i < self->durations->len; i++) {
    guint *duration = &g_array_index (self->durations, guint, i);
    total_duration += *duration;
  }

  fbd_feedback_vibra_set_duration (FBD_FEEDBACK_VIBRA (self), total_duration);
}


static void
fbd_feedback_vibra_pattern_set_property (GObject      *object,
                                         guint         property_id,
                                         const GValue *value,
                                         GParamSpec   *pspec)
{
  FbdFeedbackVibraPattern *self = FBD_FEEDBACK_VIBRA_PATTERN (object);

  switch (property_id) {
  case PROP_MAGNITUDES:
    set_magnitudes (self, g_value_get_boxed (value));
    break;
  case PROP_DURATIONS:
    set_durations (self, g_value_get_boxed (value));
    break;
  default:
    G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
    break;
  }
}


static void
fbd_feedback_vibra_pattern_get_property (GObject    *object,
                                         guint       property_id,
                                         GValue     *value,
                                         GParamSpec *pspec)
{
  FbdFeedbackVibraPattern *self = FBD_FEEDBACK_VIBRA_PATTERN (object);

  switch (property_id) {
  case PROP_MAGNITUDES:
    g_value_set_boxed (value, self->magnitudes);
    break;
  case PROP_DURATIONS:
    g_value_set_boxed (value, self->durations);
    break;
  default:
    G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
    break;
  }
}


static void on_timer_expired (gpointer data);


static void
do_pattern_step (FbdFeedbackVibraPattern *self)
{
  FbdFeedbackManager *manager = fbd_feedback_manager_get_default ();
  FbdDevVibra *dev = fbd_feedback_manager_get_dev_vibra (manager);
  double magnitude;
  guint duration;

  magnitude = g_array_index (self->magnitudes, double, self->pos);
  duration = g_array_index (self->durations, guint, self->pos);

  g_debug ("step: pos: %d/%d, magn: %f, timeout %u",
           self->pos,
           self->durations->len,
           magnitude,
           duration);

  /* Remove any pending effect */
  fbd_dev_vibra_remove_effect (dev);

  if (magnitude != 0.0) {
    double max_strength = fbd_feedback_vibra_get_max_strength (FBD_FEEDBACK_VIBRA (self));

    magnitude = MIN (magnitude, max_strength);
    fbd_dev_vibra_rumble (dev, magnitude, duration, TRUE);
  }

  self->timer_id = g_timeout_add_once (duration,
                                       on_timer_expired,
                                       self);
  g_source_set_name_by_id (self->timer_id, "feedback-vibra-pattern-timer");
}


static void
on_timer_expired (gpointer data)
{
  FbdFeedbackVibraPattern *self = data;

  g_return_if_fail (FBD_IS_FEEDBACK_VIBRA_PATTERN (self));
  g_return_if_fail (self->pos < self->durations->len);

  self->pos++;

  if (self->pos == self->durations->len) {
    self->pos = 0;
    self->timer_id = 0;
    return;
  }

  do_pattern_step (self);
}


static void
fbd_feedback_vibra_pattern_end_vibra (FbdFeedbackVibra *vibra)
{
  FbdFeedbackVibraPattern *self = FBD_FEEDBACK_VIBRA_PATTERN (vibra);
  FbdFeedbackManager *manager = fbd_feedback_manager_get_default ();
  FbdDevVibra *dev = fbd_feedback_manager_get_dev_vibra (manager);

  self->pos = 0;
  g_clear_handle_id (&self->timer_id, g_source_remove);

  if (dev)
    fbd_dev_vibra_stop (dev);
}


static void
fbd_feedback_vibra_pattern_start_vibra (FbdFeedbackVibra *vibra)
{
  FbdFeedbackVibraPattern *self = FBD_FEEDBACK_VIBRA_PATTERN (vibra);
  FbdFeedbackManager *manager = fbd_feedback_manager_get_default ();
  FbdDevVibra *dev = fbd_feedback_manager_get_dev_vibra (manager);

  g_return_if_fail (FBD_IS_DEV_VIBRA (dev));
  g_return_if_fail (self->magnitudes);
  g_return_if_fail (self->durations);
  g_return_if_fail (self->durations->len == self->magnitudes->len);

  if (self->pos)
    fbd_feedback_vibra_pattern_end_vibra (FBD_FEEDBACK_VIBRA (self));

  g_debug ("Pattern Vibra: %u elements", self->durations->len);

  if (self->durations->len == 0)
    return;

  do_pattern_step (self);
}


static gboolean
fbd_feedback_vibra_pattern_is_available (FbdFeedbackBase *base)
{
  FbdFeedbackManager *manager = fbd_feedback_manager_get_default ();
  FbdDevVibra *dev = fbd_feedback_manager_get_dev_vibra (manager);

  return FBD_IS_DEV_VIBRA (dev);
}


static void
fbd_feedback_vibra_pattern_finalize (GObject *object)
{
  FbdFeedbackVibraPattern *self = FBD_FEEDBACK_VIBRA_PATTERN (object);

  fbd_feedback_vibra_pattern_end_vibra (FBD_FEEDBACK_VIBRA (self));
  g_clear_pointer (&self->magnitudes, g_array_unref);
  g_clear_pointer (&self->durations, g_array_unref);

  G_OBJECT_CLASS (fbd_feedback_vibra_pattern_parent_class)->finalize (object);
}

static void
json_serializable_iface_init (JsonSerializableIface *iface)
{
  iface->deserialize_property = fbd_feedback_vibra_pattern_serializable_deserialize_property;
}

static void
fbd_feedback_vibra_pattern_class_init (FbdFeedbackVibraPatternClass *klass)
{
  GObjectClass *object_class = G_OBJECT_CLASS (klass);
  FbdFeedbackBaseClass *base_class = FBD_FEEDBACK_BASE_CLASS (klass);
  FbdFeedbackVibraClass *vibra_class = FBD_FEEDBACK_VIBRA_CLASS (klass);

  object_class->finalize = fbd_feedback_vibra_pattern_finalize;
  object_class->set_property = fbd_feedback_vibra_pattern_set_property;
  object_class->get_property = fbd_feedback_vibra_pattern_get_property;

  base_class->is_available = fbd_feedback_vibra_pattern_is_available;

  vibra_class->start_vibra = fbd_feedback_vibra_pattern_start_vibra;
  vibra_class->end_vibra = fbd_feedback_vibra_pattern_end_vibra;

  /**
   * FbdFeedbackVibraPattern:magnitudes
   *
   * The magnitudes as array of doubles. The values specify
   * the relative magnitude of a rumble. A value of 0 indicates
   * a pause.
   */
  props[PROP_MAGNITUDES] =
    g_param_spec_boxed ("magnitudes", "", "",
                        G_TYPE_ARRAY,
                        G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
  /**
   * FbdFeedbackVibraPattern:durations
   *
   * The durations as array of unsigned integers. The values specify
   * an the length of each rumble.
   */
  props[PROP_DURATIONS] =
    g_param_spec_boxed ("durations", "", "",
                        G_TYPE_ARRAY,
                        G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);

  g_object_class_install_properties (object_class, PROP_LAST_PROP, props);
}


static void
fbd_feedback_vibra_pattern_init (FbdFeedbackVibraPattern *self)
{
}


FbdFeedbackVibraPattern *
fbd_feedback_vibra_pattern_new (GArray *magnitudes, GArray *durations)
{
  return g_object_new (FBD_TYPE_FEEDBACK_VIBRA_PATTERN,
                       "magnitudes", magnitudes,
                       "durations", durations,
                       NULL);
}