Commit c6647bf9c9bce3b4b2457d1dcb91d8e6ca011c52

Sam Lantinga 2018-08-21T17:24:12

Added the iOS sensor implementation

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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
diff --git a/Xcode-iOS/SDL/SDL.xcodeproj/project.pbxproj b/Xcode-iOS/SDL/SDL.xcodeproj/project.pbxproj
index dd380b9..aa20507 100755
--- a/Xcode-iOS/SDL/SDL.xcodeproj/project.pbxproj
+++ b/Xcode-iOS/SDL/SDL.xcodeproj/project.pbxproj
@@ -195,6 +195,14 @@
 		AADC5A631FDA10C800960936 /* SDL_shaders_metal_ios.h in Headers */ = {isa = PBXBuildFile; fileRef = AADC5A611FDA10C800960936 /* SDL_shaders_metal_ios.h */; };
 		AADC5A641FDA10C800960936 /* SDL_render_metal.m in Sources */ = {isa = PBXBuildFile; fileRef = AADC5A621FDA10C800960936 /* SDL_render_metal.m */; };
 		AADC5A651FDA10CB00960936 /* SDL_render_metal.m in Sources */ = {isa = PBXBuildFile; fileRef = AADC5A621FDA10C800960936 /* SDL_render_metal.m */; };
+		F30D9C99212CD0360047DF2E /* SDL_sensor.h in Headers */ = {isa = PBXBuildFile; fileRef = F30D9C98212CD0360047DF2E /* SDL_sensor.h */; };
+		F30D9C9E212CD0990047DF2E /* SDL_sensor_c.h in Headers */ = {isa = PBXBuildFile; fileRef = F30D9C9B212CD0980047DF2E /* SDL_sensor_c.h */; };
+		F30D9C9F212CD0990047DF2E /* SDL_syssensor.h in Headers */ = {isa = PBXBuildFile; fileRef = F30D9C9C212CD0990047DF2E /* SDL_syssensor.h */; };
+		F30D9CA0212CD0990047DF2E /* SDL_sensor.c in Sources */ = {isa = PBXBuildFile; fileRef = F30D9C9D212CD0990047DF2E /* SDL_sensor.c */; };
+		F30D9CA1212CD0990047DF2E /* SDL_sensor.c in Sources */ = {isa = PBXBuildFile; fileRef = F30D9C9D212CD0990047DF2E /* SDL_sensor.c */; };
+		F30D9CA5212CD0BF0047DF2E /* SDL_coremotionsensor.m in Sources */ = {isa = PBXBuildFile; fileRef = F30D9CA3212CD0BF0047DF2E /* SDL_coremotionsensor.m */; };
+		F30D9CA6212CD0BF0047DF2E /* SDL_coremotionsensor.m in Sources */ = {isa = PBXBuildFile; fileRef = F30D9CA3212CD0BF0047DF2E /* SDL_coremotionsensor.m */; };
+		F30D9CA7212CD0BF0047DF2E /* SDL_coremotionsensor.h in Headers */ = {isa = PBXBuildFile; fileRef = F30D9CA4212CD0BF0047DF2E /* SDL_coremotionsensor.h */; };
 		F3BDD77620F51C3C004ECBF3 /* hid.mm in Sources */ = {isa = PBXBuildFile; fileRef = F3BDD77520F51C3C004ECBF3 /* hid.mm */; };
 		F3BDD79220F51CB8004ECBF3 /* SDL_hidapi_xbox360.c in Sources */ = {isa = PBXBuildFile; fileRef = F3BDD78B20F51CB8004ECBF3 /* SDL_hidapi_xbox360.c */; };
 		F3BDD79320F51CB8004ECBF3 /* SDL_hidapi_xbox360.c in Sources */ = {isa = PBXBuildFile; fileRef = F3BDD78B20F51CB8004ECBF3 /* SDL_hidapi_xbox360.c */; };
@@ -521,6 +529,12 @@
 		AADA5B8E16CCAB7C00107CF7 /* SDL_bits.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SDL_bits.h; sourceTree = "<group>"; };
 		AADC5A611FDA10C800960936 /* SDL_shaders_metal_ios.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SDL_shaders_metal_ios.h; sourceTree = "<group>"; };
 		AADC5A621FDA10C800960936 /* SDL_render_metal.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SDL_render_metal.m; sourceTree = "<group>"; };
+		F30D9C98212CD0360047DF2E /* SDL_sensor.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SDL_sensor.h; sourceTree = "<group>"; };
+		F30D9C9B212CD0980047DF2E /* SDL_sensor_c.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SDL_sensor_c.h; sourceTree = "<group>"; };
+		F30D9C9C212CD0990047DF2E /* SDL_syssensor.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SDL_syssensor.h; sourceTree = "<group>"; };
+		F30D9C9D212CD0990047DF2E /* SDL_sensor.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = SDL_sensor.c; sourceTree = "<group>"; };
+		F30D9CA3212CD0BF0047DF2E /* SDL_coremotionsensor.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SDL_coremotionsensor.m; sourceTree = "<group>"; };
+		F30D9CA4212CD0BF0047DF2E /* SDL_coremotionsensor.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SDL_coremotionsensor.h; sourceTree = "<group>"; };
 		F3BDD77520F51C3C004ECBF3 /* hid.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = hid.mm; sourceTree = "<group>"; };
 		F3BDD78B20F51CB8004ECBF3 /* SDL_hidapi_xbox360.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = SDL_hidapi_xbox360.c; sourceTree = "<group>"; };
 		F3BDD78C20F51CB8004ECBF3 /* SDL_hidapi_switch.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = SDL_hidapi_switch.c; sourceTree = "<group>"; };
@@ -814,6 +828,26 @@
 			path = metal;
 			sourceTree = "<group>";
 		};
+		F30D9C9A212CD0590047DF2E /* sensor */ = {
+			isa = PBXGroup;
+			children = (
+				F30D9CA2212CD09E0047DF2E /* coremotion */,
+				F30D9C9B212CD0980047DF2E /* SDL_sensor_c.h */,
+				F30D9C9D212CD0990047DF2E /* SDL_sensor.c */,
+				F30D9C9C212CD0990047DF2E /* SDL_syssensor.h */,
+			);
+			path = sensor;
+			sourceTree = "<group>";
+		};
+		F30D9CA2212CD09E0047DF2E /* coremotion */ = {
+			isa = PBXGroup;
+			children = (
+				F30D9CA4212CD0BF0047DF2E /* SDL_coremotionsensor.h */,
+				F30D9CA3212CD0BF0047DF2E /* SDL_coremotionsensor.m */,
+			);
+			path = coremotion;
+			sourceTree = "<group>";
+		};
 		F35CEA6E20F51B7F003ECE98 /* hidapi */ = {
 			isa = PBXGroup;
 			children = (
@@ -976,6 +1010,7 @@
 				AA7558891595D55500BBD41B /* SDL_revision.h */,
 				AA75588A1595D55500BBD41B /* SDL_rwops.h */,
 				AA75588B1595D55500BBD41B /* SDL_scancode.h */,
+				F30D9C98212CD0360047DF2E /* SDL_sensor.h */,
 				AA75588C1595D55500BBD41B /* SDL_shape.h */,
 				AA75588D1595D55500BBD41B /* SDL_stdinc.h */,
 				AA75588E1595D55500BBD41B /* SDL_surface.h */,
@@ -1009,6 +1044,7 @@
 				FD8BD8150E27E25900B52CD5 /* loadso */,
 				56ED04DE118A8E9A00A56AA6 /* power */,
 				041B2CE312FA0F680087D585 /* render */,
+				F30D9C9A212CD0590047DF2E /* sensor */,
 				FD3F4A6F0DEA620800C5B771 /* stdlib */,
 				FD99B9E00DD52EDC00FB1D6B /* thread */,
 				FD99BA1E0DD52EDC00FB1D6B /* timer */,
@@ -1263,6 +1299,7 @@
 				AA75589C1595D55500BBD41B /* SDL_audio.h in Headers */,
 				55FFA91A2122302B00D7CBED /* SDL_syspower.h in Headers */,
 				AA75589D1595D55500BBD41B /* SDL_blendmode.h in Headers */,
+				F30D9C9E212CD0990047DF2E /* SDL_sensor_c.h in Headers */,
 				AA75589E1595D55500BBD41B /* SDL_clipboard.h in Headers */,
 				AA75589F1595D55500BBD41B /* SDL_config_iphoneos.h in Headers */,
 				AA7558A01595D55500BBD41B /* SDL_config.h in Headers */,
@@ -1277,6 +1314,7 @@
 				AA7558A71595D55500BBD41B /* SDL_haptic.h in Headers */,
 				AA7558A81595D55500BBD41B /* SDL_hints.h in Headers */,
 				566726461DF72CF5001DD3DB /* SDL_dataqueue.h in Headers */,
+				F30D9C9F212CD0990047DF2E /* SDL_syssensor.h in Headers */,
 				AA7558AA1595D55500BBD41B /* SDL_joystick.h in Headers */,
 				AA13B34B1FB8B27800D9FEE6 /* SDL_shape_internals.h in Headers */,
 				AA7558AB1595D55500BBD41B /* SDL_keyboard.h in Headers */,
@@ -1284,6 +1322,7 @@
 				AA7558AC1595D55500BBD41B /* SDL_keycode.h in Headers */,
 				AA7558AD1595D55500BBD41B /* SDL_loadso.h in Headers */,
 				AA7558AE1595D55500BBD41B /* SDL_log.h in Headers */,
+				F30D9CA7212CD0BF0047DF2E /* SDL_coremotionsensor.h in Headers */,
 				AA7558AF1595D55500BBD41B /* SDL_main.h in Headers */,
 				AA7558B01595D55500BBD41B /* SDL_mouse.h in Headers */,
 				AA7558B11595D55500BBD41B /* SDL_mutex.h in Headers */,
@@ -1306,6 +1345,7 @@
 				FAD4F7021BA3C4E8008346CE /* SDL_sysjoystick_c.h in Headers */,
 				AA7558C11595D55500BBD41B /* SDL_surface.h in Headers */,
 				AA7558C21595D55500BBD41B /* SDL_system.h in Headers */,
+				F30D9C99212CD0360047DF2E /* SDL_sensor.h in Headers */,
 				AA7558C31595D55500BBD41B /* SDL_syswm.h in Headers */,
 				AA7558C41595D55500BBD41B /* SDL_thread.h in Headers */,
 				AA7558C51595D55500BBD41B /* SDL_timer.h in Headers */,
@@ -1495,9 +1535,11 @@
 				AADC5A651FDA10CB00960936 /* SDL_render_metal.m in Sources */,
 				FAB598561BB5C31600BE72C5 /* SDL_sysloadso.c in Sources */,
 				FAB598571BB5C31600BE72C5 /* SDL_power.c in Sources */,
+				F30D9CA1212CD0990047DF2E /* SDL_sensor.c in Sources */,
 				FAB598581BB5C31600BE72C5 /* SDL_syspower.m in Sources */,
 				56F9D5601DF73BA400C15B5D /* SDL_dataqueue.c in Sources */,
 				FAB598591BB5C31600BE72C5 /* SDL_render_gles.c in Sources */,
+				F30D9CA6212CD0BF0047DF2E /* SDL_coremotionsensor.m in Sources */,
 				FAB5985A1BB5C31600BE72C5 /* SDL_render_gles2.c in Sources */,
 				FAB5985B1BB5C31600BE72C5 /* SDL_shaders_gles2.c in Sources */,
 				FAB5985D1BB5C31600BE72C5 /* SDL_blendfillrect.c in Sources */,
@@ -1570,6 +1612,7 @@
 			files = (
 				FD6526810DE8FCDD002AD96B /* SDL_systimer.c in Sources */,
 				FD6526800DE8FCDD002AD96B /* SDL_timer.c in Sources */,
+				F30D9CA5212CD0BF0047DF2E /* SDL_coremotionsensor.m in Sources */,
 				FD3F4A7B0DEA620800C5B771 /* SDL_string.c in Sources */,
 				FD6526660DE8FCDD002AD96B /* SDL_dummyaudio.c in Sources */,
 				FD6526670DE8FCDD002AD96B /* SDL_audio.c in Sources */,
@@ -1667,6 +1710,7 @@
 				0402A85912FE70C600CECEE3 /* SDL_shaders_gles2.c in Sources */,
 				04BAC09D1300C1290055DE28 /* SDL_log.c in Sources */,
 				56EA86FB13E9EC2B002E47EB /* SDL_coreaudio.m in Sources */,
+				F30D9CA0212CD0990047DF2E /* SDL_sensor.c in Sources */,
 				F3BDD79420F51CB8004ECBF3 /* SDL_hidapi_switch.c in Sources */,
 				93CB792613FC5F5300BD3E05 /* SDL_uikitviewcontroller.m in Sources */,
 				AA628ADB159369E3005138DD /* SDL_rotate.c in Sources */,
diff --git a/include/SDL_config_iphoneos.h b/include/SDL_config_iphoneos.h
index c103b29..2ea4c92 100644
--- a/include/SDL_config_iphoneos.h
+++ b/include/SDL_config_iphoneos.h
@@ -139,8 +139,8 @@
 #define SDL_JOYSTICK_MFI 1
 #define SDL_JOYSTICK_HIDAPI 1
 
-/* Enable the dummy sensor driver */
-#define SDL_SENSOR_DUMMY  1
+/* Enable the CoreMotion sensor driver */
+#define SDL_SENSOR_COREMOTION   1
 
 /* Enable Unix style SO loading */
 #define SDL_LOADSO_DLOPEN 1
diff --git a/src/sensor/SDL_sensor.c b/src/sensor/SDL_sensor.c
index 59b2457..46f94a4 100644
--- a/src/sensor/SDL_sensor.c
+++ b/src/sensor/SDL_sensor.c
@@ -36,6 +36,9 @@ static SDL_SensorDriver *SDL_sensor_drivers[] = {
 #ifdef SDL_SENSOR_ANDROID
     &SDL_ANDROID_SensorDriver,
 #endif
+#ifdef SDL_SENSOR_COREMOTION
+    &SDL_COREMOTION_SensorDriver,
+#endif
 #if defined(SDL_SENSOR_DUMMY) || defined(SDL_SENSOR_DISABLED)
     &SDL_DUMMY_SensorDriver
 #endif
diff --git a/src/sensor/SDL_syssensor.h b/src/sensor/SDL_syssensor.h
index ab80f8c..f10aee3 100644
--- a/src/sensor/SDL_syssensor.h
+++ b/src/sensor/SDL_syssensor.h
@@ -93,6 +93,7 @@ typedef struct _SDL_SensorDriver
 
 /* The available sensor drivers */
 extern SDL_SensorDriver SDL_ANDROID_SensorDriver;
+extern SDL_SensorDriver SDL_COREMOTION_SensorDriver;
 extern SDL_SensorDriver SDL_DUMMY_SensorDriver;
 
 /* vi: set ts=4 sw=4 expandtab: */
diff --git a/src/sensor/android/SDL_androidsensor.c b/src/sensor/android/SDL_androidsensor.c
index f4344e0..117c18d 100644
--- a/src/sensor/android/SDL_androidsensor.c
+++ b/src/sensor/android/SDL_androidsensor.c
@@ -156,7 +156,7 @@ SDL_ANDROID_SensorOpen(SDL_Sensor *sensor, int device_index)
 }
     
 static void
-SDL_ANDROID_SensorUpdate(SDL_Sensor* sensor)
+SDL_ANDROID_SensorUpdate(SDL_Sensor *sensor)
 {
     int events;
     ASensorEvent event;
@@ -171,7 +171,7 @@ SDL_ANDROID_SensorUpdate(SDL_Sensor* sensor)
 }
 
 static void
-SDL_ANDROID_SensorClose(SDL_Sensor * sensor)
+SDL_ANDROID_SensorClose(SDL_Sensor *sensor)
 {
     if (sensor->hwdata) {
         ASensorEventQueue_disableSensor(sensor->hwdata->eventqueue, sensor->hwdata->asensor);
diff --git a/src/sensor/coremotion/SDL_coremotionsensor.h b/src/sensor/coremotion/SDL_coremotionsensor.h
new file mode 100644
index 0000000..2312e84
--- /dev/null
+++ b/src/sensor/coremotion/SDL_coremotionsensor.h
@@ -0,0 +1,30 @@
+/*
+  Simple DirectMedia Layer
+  Copyright (C) 1997-2018 Sam Lantinga <slouken@libsdl.org>
+
+  This software is provided 'as-is', without any express or implied
+  warranty.  In no event will the authors be held liable for any damages
+  arising from the use of this software.
+
+  Permission is granted to anyone to use this software for any purpose,
+  including commercial applications, and to alter it and redistribute it
+  freely, subject to the following restrictions:
+
+  1. The origin of this software must not be misrepresented; you must not
+     claim that you wrote the original software. If you use this software
+     in a product, an acknowledgment in the product documentation would be
+     appreciated but is not required.
+  2. Altered source versions must be plainly marked as such, and must not be
+     misrepresented as being the original software.
+  3. This notice may not be removed or altered from any source distribution.
+*/
+#include "SDL_config.h"
+
+/* The private structure used to keep track of a sensor */
+struct sensor_hwdata
+{
+    float data[3];
+};
+
+
+/* vi: set ts=4 sw=4 expandtab: */
diff --git a/src/sensor/coremotion/SDL_coremotionsensor.m b/src/sensor/coremotion/SDL_coremotionsensor.m
new file mode 100644
index 0000000..b384dba
--- /dev/null
+++ b/src/sensor/coremotion/SDL_coremotionsensor.m
@@ -0,0 +1,236 @@
+/*
+  Simple DirectMedia Layer
+  Copyright (C) 1997-2018 Sam Lantinga <slouken@libsdl.org>
+
+  This software is provided 'as-is', without any express or implied
+  warranty.  In no event will the authors be held liable for any damages
+  arising from the use of this software.
+
+  Permission is granted to anyone to use this software for any purpose,
+  including commercial applications, and to alter it and redistribute it
+  freely, subject to the following restrictions:
+
+  1. The origin of this software must not be misrepresented; you must not
+     claim that you wrote the original software. If you use this software
+     in a product, an acknowledgment in the product documentation would be
+     appreciated but is not required.
+  2. Altered source versions must be plainly marked as such, and must not be
+     misrepresented as being the original software.
+  3. This notice may not be removed or altered from any source distribution.
+*/
+
+#include "SDL_config.h"
+
+#ifdef SDL_SENSOR_COREMOTION
+
+/* This is the system specific header for the SDL sensor API */
+#include <CoreMotion/CoreMotion.h>
+
+#include "SDL_error.h"
+#include "SDL_sensor.h"
+#include "SDL_coremotionsensor.h"
+#include "../SDL_syssensor.h"
+#include "../SDL_sensor_c.h"
+
+#define STANDARD_GRAVITY    9.80665f
+
+typedef struct
+{
+    SDL_SensorType type;
+    SDL_SensorID instance_id;
+} SDL_CoreMotionSensor;
+
+static CMMotionManager *SDL_motion_manager;
+static SDL_CoreMotionSensor *SDL_sensors;
+static int SDL_sensors_count;
+
+static int
+SDL_COREMOTION_SensorInit(void)
+{
+    int i, sensors_count = 0;
+
+    if (!SDL_motion_manager) {
+        SDL_motion_manager = [[CMMotionManager alloc] init];
+    }
+
+    if (SDL_motion_manager.accelerometerAvailable) {
+        ++sensors_count;
+    }
+    if (SDL_motion_manager.gyroAvailable) {
+        ++sensors_count;
+    }
+
+    if (sensors_count > 0) {
+        SDL_sensors = (SDL_CoreMotionSensor *)SDL_calloc(sensors_count, sizeof(*SDL_sensors));
+        if (!SDL_sensors) {
+            return SDL_OutOfMemory();
+        }
+
+        i = 0;
+        if (SDL_motion_manager.accelerometerAvailable) {
+            SDL_sensors[i].type = SDL_SENSOR_ACCEL;
+            SDL_sensors[i].instance_id = SDL_GetNextSensorInstanceID();
+            ++i;
+        }
+        if (SDL_motion_manager.gyroAvailable) {
+            SDL_sensors[i].type = SDL_SENSOR_GYRO;
+            SDL_sensors[i].instance_id = SDL_GetNextSensorInstanceID();
+            ++i;
+        }
+        SDL_sensors_count = sensors_count;
+    }
+    return 0;
+}
+
+static int
+SDL_COREMOTION_SensorGetCount(void)
+{
+    return SDL_sensors_count;
+}
+
+static void
+SDL_COREMOTION_SensorDetect(void)
+{
+}
+
+static const char *
+SDL_COREMOTION_SensorGetDeviceName(int device_index)
+{
+    switch (SDL_sensors[device_index].type) {
+    case SDL_SENSOR_ACCEL:
+        return "Accelerometer";
+    case SDL_SENSOR_GYRO:
+        return "Gyro";
+    default:
+        return "Unknown";
+    }
+}
+
+static SDL_SensorType
+SDL_COREMOTION_SensorGetDeviceType(int device_index)
+{
+    return SDL_sensors[device_index].type;
+}
+
+static int
+SDL_COREMOTION_SensorGetDeviceNonPortableType(int device_index)
+{
+    return SDL_sensors[device_index].type;
+}
+
+static SDL_SensorID
+SDL_COREMOTION_SensorGetDeviceInstanceID(int device_index)
+{
+    return SDL_sensors[device_index].instance_id;
+}
+
+static int
+SDL_COREMOTION_SensorOpen(SDL_Sensor *sensor, int device_index)
+{
+    struct sensor_hwdata *hwdata;
+
+    hwdata = (struct sensor_hwdata *)SDL_calloc(1, sizeof(*hwdata));
+    if (hwdata == NULL) {
+        return SDL_OutOfMemory();
+    }
+    sensor->hwdata = hwdata;
+
+    switch (sensor->type)
+    {
+    case SDL_SENSOR_ACCEL:
+        [SDL_motion_manager startAccelerometerUpdates];
+        break;
+    case SDL_SENSOR_GYRO:
+        [SDL_motion_manager startGyroUpdates];
+        break;
+    default:
+        break;
+    }
+    return 0;
+}
+    
+static void
+SDL_COREMOTION_SensorUpdate(SDL_Sensor *sensor)
+{
+    switch (sensor->type)
+    {
+    case SDL_SENSOR_ACCEL:
+        {
+            CMAccelerometerData *accelerometerData = SDL_motion_manager.accelerometerData;
+            if (accelerometerData) {
+                CMAcceleration acceleration = accelerometerData.acceleration;
+                float data[3];
+                data[0] = acceleration.x * STANDARD_GRAVITY;
+                data[1] = acceleration.y * STANDARD_GRAVITY;
+                data[2] = acceleration.z * STANDARD_GRAVITY;
+                if (SDL_memcmp(data, sensor->hwdata->data, sizeof(data)) != 0) {
+                    SDL_PrivateSensorUpdate(sensor, data, SDL_arraysize(data));
+                    SDL_memcpy(sensor->hwdata->data, data, sizeof(data));
+                }
+            }
+        }
+        break;
+    case SDL_SENSOR_GYRO:
+        {
+            CMGyroData *gyroData = SDL_motion_manager.gyroData;
+            if (gyroData) {
+                CMRotationRate rotationRate = gyroData.rotationRate;
+                float data[3];
+                data[0] = rotationRate.x;
+                data[1] = rotationRate.y;
+                data[2] = rotationRate.z;
+                if (SDL_memcmp(data, sensor->hwdata->data, sizeof(data)) != 0) {
+                    SDL_PrivateSensorUpdate(sensor, data, SDL_arraysize(data));
+                    SDL_memcpy(sensor->hwdata->data, data, sizeof(data));
+                }
+            }
+        }
+        break;
+    default:
+        break;
+    }
+}
+
+static void
+SDL_COREMOTION_SensorClose(SDL_Sensor *sensor)
+{
+    if (sensor->hwdata) {
+        switch (sensor->type)
+        {
+        case SDL_SENSOR_ACCEL:
+            [SDL_motion_manager stopAccelerometerUpdates];
+            break;
+        case SDL_SENSOR_GYRO:
+            [SDL_motion_manager stopGyroUpdates];
+            break;
+        default:
+            break;
+        }
+        SDL_free(sensor->hwdata);
+        sensor->hwdata = NULL;
+    }
+}
+
+static void
+SDL_COREMOTION_SensorQuit(void)
+{
+}
+
+SDL_SensorDriver SDL_COREMOTION_SensorDriver =
+{
+    SDL_COREMOTION_SensorInit,
+    SDL_COREMOTION_SensorGetCount,
+    SDL_COREMOTION_SensorDetect,
+    SDL_COREMOTION_SensorGetDeviceName,
+    SDL_COREMOTION_SensorGetDeviceType,
+    SDL_COREMOTION_SensorGetDeviceNonPortableType,
+    SDL_COREMOTION_SensorGetDeviceInstanceID,
+    SDL_COREMOTION_SensorOpen,
+    SDL_COREMOTION_SensorUpdate,
+    SDL_COREMOTION_SensorClose,
+    SDL_COREMOTION_SensorQuit,
+};
+
+#endif /* SDL_SENSOR_COREMOTION */
+
+/* vi: set ts=4 sw=4 expandtab: */
diff --git a/src/sensor/dummy/SDL_dummysensor.c b/src/sensor/dummy/SDL_dummysensor.c
index 0148c09..cf04045 100644
--- a/src/sensor/dummy/SDL_dummysensor.c
+++ b/src/sensor/dummy/SDL_dummysensor.c
@@ -76,12 +76,12 @@ SDL_DUMMY_SensorOpen(SDL_Sensor *sensor, int device_index)
 }
     
 static void
-SDL_DUMMY_SensorUpdate(SDL_Sensor* sensor)
+SDL_DUMMY_SensorUpdate(SDL_Sensor *sensor)
 {
 }
 
 static void
-SDL_DUMMY_SensorClose(SDL_Sensor * sensor)
+SDL_DUMMY_SensorClose(SDL_Sensor *sensor)
 {
 }