summaryrefslogtreecommitdiff
path: root/src/modules/ecordova/tizen/ecordova_deviceorientation.c
blob: 8ba7f51acd76c8842a54c8e58343ca3b940d5cd7 (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
171
172
173
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif

#include "ecordova_deviceorientation_private.h"

#define MY_CLASS ECORDOVA_DEVICEORIENTATION_CLASS
#define MY_CLASS_NAME "Ecordova_DeviceOrientation"

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_DeviceOrientation_Data *);

static Eo_Base *
_ecordova_deviceorientation_eo_base_constructor(Eo *obj,
                                                Ecordova_DeviceOrientation_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_deviceorientation_eo_base_destructor(Eo *obj,
                                               Ecordova_DeviceOrientation_Data *pd)
{
   DBG("(%p)", obj);

   eina_hash_free(pd->listeners);

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

static void
_ecordova_deviceorientation_current_heading_get(Eo *obj,
                                                Ecordova_DeviceOrientation_Data *pd)
{
   sensor_listener_h listener = _create_listener(pd);
   if (!listener)
     {
        Ecordova_DeviceOrientation_Error error = ECORDOVA_DEVICEORIENTATION_ERROR_COMPASS_INTERNAL_ERR;
        eo_do(obj, eo_event_callback_call(ECORDOVA_DEVICEORIENTATION_EVENT_ERROR,
                                          &error));
        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);

   // TODO: check the correct values
   Ecordova_DeviceOrientation_Heading orientation = {
     .magnetic_heading = event.values[0],
     .true_heading = event.values[0],
     .heading_accuracy = 0,
     .timestamp = event.timestamp
   };
   eo_do(obj, eo_event_callback_call(ECORDOVA_DEVICEORIENTATION_EVENT_CURRENT_SUCCESS,
                                     &orientation));

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

static Ecordova_DeviceOrientation_WatchID
_ecordova_deviceorientation_heading_watch(Eo *obj,
                                          Ecordova_DeviceOrientation_Data *pd,
                                          const Ecordova_DeviceOrientation_OrientationOptions *options)
{
   sensor_listener_h listener = _create_listener(pd);
   if (!listener)
     {
        Ecordova_DeviceOrientation_Error error = ECORDOVA_DEVICEORIENTATION_ERROR_COMPASS_INTERNAL_ERR;
        eo_do(obj, eo_event_callback_call(ECORDOVA_DEVICEORIENTATION_EVENT_ERROR,
                                          &error));
        return 0;
     }

   const Ecordova_DeviceOrientation_OrientationOptions default_options = {
     .frequency = 100
   };
   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_DeviceOrientation_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_deviceorientation_watch_clear(Eo *obj,
                                        Ecordova_DeviceOrientation_Data *pd,
                                        Ecordova_DeviceOrientation_WatchID watch_id)
{
   sensor_listener_h listener = eina_hash_find(pd->listeners, &watch_id);
   if (!listener)
     {
        eo_do(obj, eo_event_callback_call(ECORDOVA_DEVICEORIENTATION_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_DeviceOrientation_Data *pd)
{
   int ret = SENSOR_ERROR_NONE;

   if (!pd->sensor)
     {
        ret = sensor_get_default_sensor(SENSOR_ORIENTATION, &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;

   // TODO: check the correct values
   Ecordova_DeviceOrientation_Heading orientation = {
     .magnetic_heading = event->values[0],
     .true_heading = event->values[0],
     .heading_accuracy = 0,
     .timestamp = event->timestamp
   };
   eo_do(obj, eo_event_callback_call(ECORDOVA_DEVICEORIENTATION_EVENT_WATCH_SUCCESS,
                                     &orientation));
}

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_deviceorientation.eo.c"