summaryrefslogtreecommitdiff
path: root/src/modules/ecordova/tizen/ecordova_devicemotion.c
blob: 99e4afa4618ebcb91ef96de9f5f7f9dc3f72cad6 (plain)
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
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif

#include "ecordova_devicemotion_private.h"

#define MY_CLASS ECORDOVA_DEVICEMOTION_CLASS
#define MY_CLASS_NAME "Ecordova_DeviceMotion"

static void _on_sensor_event(sensor_h, sensor_event_s *, void *);
static void _hash_data_free(sensor_listener_h);
static sensor_listener_h _create_listener(Ecordova_DeviceMotion_Data *);

static Eo_Base *
_ecordova_devicemotion_eo_base_constructor(Eo *obj,
                                           Ecordova_DeviceMotion_Data *pd)
{
   DBG("(%p)", obj);

   pd->obj = obj;
   pd->sensor = NULL;
   pd->listeners = eina_hash_int32_new(EINA_FREE_CB(_hash_data_free));

   return eo_do_super_ret(obj, MY_CLASS, obj, eo_constructor());
}

static void
_ecordova_devicemotion_eo_base_destructor(Eo *obj,
                                          Ecordova_DeviceMotion_Data *pd)
{
   DBG("(%p)", obj);

   eina_hash_free(pd->listeners);

   eo_do_super(obj, MY_CLASS, eo_destructor());
}

static void
_ecordova_devicemotion_current_acceleration_get(Eo *obj,
                                                Ecordova_DeviceMotion_Data *pd)
{
   sensor_listener_h listener = _create_listener(pd);
   if (!listener)
     {
        eo_do(obj, eo_event_callback_call(ECORDOVA_DEVICEMOTION_EVENT_ERROR,
                                          NULL));
        return;
     }

   int ret = sensor_listener_start(listener);
   EINA_SAFETY_ON_FALSE_GOTO(SENSOR_ERROR_NONE == ret, on_error);

   sensor_event_s event;
   ret = sensor_listener_read_data(listener, &event);
   EINA_SAFETY_ON_FALSE_GOTO(SENSOR_ERROR_NONE == ret, on_error);

   Ecordova_DeviceMotion_Acceleration acceleration = {
     .x = event.values[0],
     .y = event.values[1],
     .z = event.values[2],
     .timestamp = event.timestamp
   };
   eo_do(obj, eo_event_callback_call(ECORDOVA_DEVICEMOTION_EVENT_CURRENT_SUCCESS,
                                     &acceleration));

on_error:
   ret = sensor_destroy_listener(listener);
   EINA_SAFETY_ON_FALSE_RETURN(SENSOR_ERROR_NONE == ret);
}

static Ecordova_DeviceMotion_WatchID
_ecordova_devicemotion_acceleration_watch(Eo *obj,
                                          Ecordova_DeviceMotion_Data *pd,
                                          const Ecordova_DeviceMotion_AccelerometerOptions *options)
{
   sensor_listener_h listener = _create_listener(pd);
   if (!listener)
     {
        eo_do(obj, eo_event_callback_call(ECORDOVA_DEVICEMOTION_EVENT_ERROR,
                                          NULL));
        return 0;
     }

   const Ecordova_DeviceMotion_AccelerometerOptions default_options = {
     .frequency = 10000
   };
   if (!options)
     options = &default_options;

   int ret = sensor_listener_set_event_cb(listener,
                                          options->frequency,
                                          _on_sensor_event,
                                          pd->obj);
   EINA_SAFETY_ON_FALSE_GOTO(SENSOR_ERROR_NONE == ret, on_error);

   ret = sensor_listener_start(listener);
   EINA_SAFETY_ON_FALSE_GOTO(SENSOR_ERROR_NONE == ret, on_error);

   static Ecordova_DeviceMotion_WatchID id = 0;
   ++id;
   eina_hash_add(pd->listeners, &id, listener);
   return id;

on_error:
   ret = sensor_destroy_listener(listener);
   EINA_SAFETY_ON_FALSE_RETURN_VAL(SENSOR_ERROR_NONE == ret, 0);
   return 0;
}

static void
_ecordova_devicemotion_watch_clear(Eo *obj,
                                   Ecordova_DeviceMotion_Data *pd,
                                   Ecordova_DeviceMotion_WatchID watch_id)
{
   sensor_listener_h listener = eina_hash_find(pd->listeners, &watch_id);
   if (!listener)
     {
        eo_do(obj, eo_event_callback_call(ECORDOVA_DEVICEMOTION_EVENT_ERROR,
                                          NULL));
        return;
     }

   Eina_Bool ret = eina_hash_del(pd->listeners, &watch_id, NULL);
   EINA_SAFETY_ON_FALSE_RETURN(ret);
}

static sensor_listener_h
_create_listener(Ecordova_DeviceMotion_Data *pd)
{
   int ret = SENSOR_ERROR_NONE;

   if (!pd->sensor)
     {
        ret = sensor_get_default_sensor(SENSOR_ACCELEROMETER, &pd->sensor);
        EINA_SAFETY_ON_FALSE_RETURN_VAL(SENSOR_ERROR_NONE == ret, NULL);
     }

   sensor_listener_h listener = NULL;
   ret = sensor_create_listener(pd->sensor, &listener);
   EINA_SAFETY_ON_FALSE_RETURN_VAL(SENSOR_ERROR_NONE == ret, NULL);

   return listener;
}

static void
_on_sensor_event(sensor_h sensor EINA_UNUSED,
                 sensor_event_s *event,
                 void *user_data)
{
   Eo *obj = user_data;

   Ecordova_DeviceMotion_Acceleration acceleration = {
     .x = event->values[0],
     .y = event->values[1],
     .z = event->values[2],
     .timestamp = event->timestamp
   };
   eo_do(obj, eo_event_callback_call(ECORDOVA_DEVICEMOTION_EVENT_WATCH_SUCCESS,
                                     &acceleration));
}

static void
_hash_data_free(sensor_listener_h listener)
{
   int ret = sensor_destroy_listener(listener);
   EINA_SAFETY_ON_FALSE_RETURN(SENSOR_ERROR_NONE == ret);
}

#include "ecordova_devicemotion.eo.c"