Added the iOS sensor implementation
authorSam Lantinga <slouken@libsdl.org>
Tue, 21 Aug 2018 17:24:12 -0700
changeset 1213520df200b97ae
parent 12134 770797b794da
child 12136 ec2cee693e4b
Added the iOS sensor implementation
Xcode-iOS/SDL/SDL.xcodeproj/project.pbxproj
include/SDL_config_iphoneos.h
src/sensor/SDL_sensor.c
src/sensor/SDL_syssensor.h
src/sensor/android/SDL_androidsensor.c
src/sensor/coremotion/SDL_coremotionsensor.h
src/sensor/coremotion/SDL_coremotionsensor.m
src/sensor/dummy/SDL_dummysensor.c
     1.1 --- a/Xcode-iOS/SDL/SDL.xcodeproj/project.pbxproj	Tue Aug 21 16:47:44 2018 -0700
     1.2 +++ b/Xcode-iOS/SDL/SDL.xcodeproj/project.pbxproj	Tue Aug 21 17:24:12 2018 -0700
     1.3 @@ -195,6 +195,14 @@
     1.4  		AADC5A631FDA10C800960936 /* SDL_shaders_metal_ios.h in Headers */ = {isa = PBXBuildFile; fileRef = AADC5A611FDA10C800960936 /* SDL_shaders_metal_ios.h */; };
     1.5  		AADC5A641FDA10C800960936 /* SDL_render_metal.m in Sources */ = {isa = PBXBuildFile; fileRef = AADC5A621FDA10C800960936 /* SDL_render_metal.m */; };
     1.6  		AADC5A651FDA10CB00960936 /* SDL_render_metal.m in Sources */ = {isa = PBXBuildFile; fileRef = AADC5A621FDA10C800960936 /* SDL_render_metal.m */; };
     1.7 +		F30D9C99212CD0360047DF2E /* SDL_sensor.h in Headers */ = {isa = PBXBuildFile; fileRef = F30D9C98212CD0360047DF2E /* SDL_sensor.h */; };
     1.8 +		F30D9C9E212CD0990047DF2E /* SDL_sensor_c.h in Headers */ = {isa = PBXBuildFile; fileRef = F30D9C9B212CD0980047DF2E /* SDL_sensor_c.h */; };
     1.9 +		F30D9C9F212CD0990047DF2E /* SDL_syssensor.h in Headers */ = {isa = PBXBuildFile; fileRef = F30D9C9C212CD0990047DF2E /* SDL_syssensor.h */; };
    1.10 +		F30D9CA0212CD0990047DF2E /* SDL_sensor.c in Sources */ = {isa = PBXBuildFile; fileRef = F30D9C9D212CD0990047DF2E /* SDL_sensor.c */; };
    1.11 +		F30D9CA1212CD0990047DF2E /* SDL_sensor.c in Sources */ = {isa = PBXBuildFile; fileRef = F30D9C9D212CD0990047DF2E /* SDL_sensor.c */; };
    1.12 +		F30D9CA5212CD0BF0047DF2E /* SDL_coremotionsensor.m in Sources */ = {isa = PBXBuildFile; fileRef = F30D9CA3212CD0BF0047DF2E /* SDL_coremotionsensor.m */; };
    1.13 +		F30D9CA6212CD0BF0047DF2E /* SDL_coremotionsensor.m in Sources */ = {isa = PBXBuildFile; fileRef = F30D9CA3212CD0BF0047DF2E /* SDL_coremotionsensor.m */; };
    1.14 +		F30D9CA7212CD0BF0047DF2E /* SDL_coremotionsensor.h in Headers */ = {isa = PBXBuildFile; fileRef = F30D9CA4212CD0BF0047DF2E /* SDL_coremotionsensor.h */; };
    1.15  		F3BDD77620F51C3C004ECBF3 /* hid.mm in Sources */ = {isa = PBXBuildFile; fileRef = F3BDD77520F51C3C004ECBF3 /* hid.mm */; };
    1.16  		F3BDD79220F51CB8004ECBF3 /* SDL_hidapi_xbox360.c in Sources */ = {isa = PBXBuildFile; fileRef = F3BDD78B20F51CB8004ECBF3 /* SDL_hidapi_xbox360.c */; };
    1.17  		F3BDD79320F51CB8004ECBF3 /* SDL_hidapi_xbox360.c in Sources */ = {isa = PBXBuildFile; fileRef = F3BDD78B20F51CB8004ECBF3 /* SDL_hidapi_xbox360.c */; };
    1.18 @@ -521,6 +529,12 @@
    1.19  		AADA5B8E16CCAB7C00107CF7 /* SDL_bits.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SDL_bits.h; sourceTree = "<group>"; };
    1.20  		AADC5A611FDA10C800960936 /* SDL_shaders_metal_ios.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SDL_shaders_metal_ios.h; sourceTree = "<group>"; };
    1.21  		AADC5A621FDA10C800960936 /* SDL_render_metal.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SDL_render_metal.m; sourceTree = "<group>"; };
    1.22 +		F30D9C98212CD0360047DF2E /* SDL_sensor.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SDL_sensor.h; sourceTree = "<group>"; };
    1.23 +		F30D9C9B212CD0980047DF2E /* SDL_sensor_c.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SDL_sensor_c.h; sourceTree = "<group>"; };
    1.24 +		F30D9C9C212CD0990047DF2E /* SDL_syssensor.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SDL_syssensor.h; sourceTree = "<group>"; };
    1.25 +		F30D9C9D212CD0990047DF2E /* SDL_sensor.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = SDL_sensor.c; sourceTree = "<group>"; };
    1.26 +		F30D9CA3212CD0BF0047DF2E /* SDL_coremotionsensor.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SDL_coremotionsensor.m; sourceTree = "<group>"; };
    1.27 +		F30D9CA4212CD0BF0047DF2E /* SDL_coremotionsensor.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SDL_coremotionsensor.h; sourceTree = "<group>"; };
    1.28  		F3BDD77520F51C3C004ECBF3 /* hid.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = hid.mm; sourceTree = "<group>"; };
    1.29  		F3BDD78B20F51CB8004ECBF3 /* SDL_hidapi_xbox360.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = SDL_hidapi_xbox360.c; sourceTree = "<group>"; };
    1.30  		F3BDD78C20F51CB8004ECBF3 /* SDL_hidapi_switch.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = SDL_hidapi_switch.c; sourceTree = "<group>"; };
    1.31 @@ -814,6 +828,26 @@
    1.32  			path = metal;
    1.33  			sourceTree = "<group>";
    1.34  		};
    1.35 +		F30D9C9A212CD0590047DF2E /* sensor */ = {
    1.36 +			isa = PBXGroup;
    1.37 +			children = (
    1.38 +				F30D9CA2212CD09E0047DF2E /* coremotion */,
    1.39 +				F30D9C9B212CD0980047DF2E /* SDL_sensor_c.h */,
    1.40 +				F30D9C9D212CD0990047DF2E /* SDL_sensor.c */,
    1.41 +				F30D9C9C212CD0990047DF2E /* SDL_syssensor.h */,
    1.42 +			);
    1.43 +			path = sensor;
    1.44 +			sourceTree = "<group>";
    1.45 +		};
    1.46 +		F30D9CA2212CD09E0047DF2E /* coremotion */ = {
    1.47 +			isa = PBXGroup;
    1.48 +			children = (
    1.49 +				F30D9CA4212CD0BF0047DF2E /* SDL_coremotionsensor.h */,
    1.50 +				F30D9CA3212CD0BF0047DF2E /* SDL_coremotionsensor.m */,
    1.51 +			);
    1.52 +			path = coremotion;
    1.53 +			sourceTree = "<group>";
    1.54 +		};
    1.55  		F35CEA6E20F51B7F003ECE98 /* hidapi */ = {
    1.56  			isa = PBXGroup;
    1.57  			children = (
    1.58 @@ -976,6 +1010,7 @@
    1.59  				AA7558891595D55500BBD41B /* SDL_revision.h */,
    1.60  				AA75588A1595D55500BBD41B /* SDL_rwops.h */,
    1.61  				AA75588B1595D55500BBD41B /* SDL_scancode.h */,
    1.62 +				F30D9C98212CD0360047DF2E /* SDL_sensor.h */,
    1.63  				AA75588C1595D55500BBD41B /* SDL_shape.h */,
    1.64  				AA75588D1595D55500BBD41B /* SDL_stdinc.h */,
    1.65  				AA75588E1595D55500BBD41B /* SDL_surface.h */,
    1.66 @@ -1009,6 +1044,7 @@
    1.67  				FD8BD8150E27E25900B52CD5 /* loadso */,
    1.68  				56ED04DE118A8E9A00A56AA6 /* power */,
    1.69  				041B2CE312FA0F680087D585 /* render */,
    1.70 +				F30D9C9A212CD0590047DF2E /* sensor */,
    1.71  				FD3F4A6F0DEA620800C5B771 /* stdlib */,
    1.72  				FD99B9E00DD52EDC00FB1D6B /* thread */,
    1.73  				FD99BA1E0DD52EDC00FB1D6B /* timer */,
    1.74 @@ -1263,6 +1299,7 @@
    1.75  				AA75589C1595D55500BBD41B /* SDL_audio.h in Headers */,
    1.76  				55FFA91A2122302B00D7CBED /* SDL_syspower.h in Headers */,
    1.77  				AA75589D1595D55500BBD41B /* SDL_blendmode.h in Headers */,
    1.78 +				F30D9C9E212CD0990047DF2E /* SDL_sensor_c.h in Headers */,
    1.79  				AA75589E1595D55500BBD41B /* SDL_clipboard.h in Headers */,
    1.80  				AA75589F1595D55500BBD41B /* SDL_config_iphoneos.h in Headers */,
    1.81  				AA7558A01595D55500BBD41B /* SDL_config.h in Headers */,
    1.82 @@ -1277,6 +1314,7 @@
    1.83  				AA7558A71595D55500BBD41B /* SDL_haptic.h in Headers */,
    1.84  				AA7558A81595D55500BBD41B /* SDL_hints.h in Headers */,
    1.85  				566726461DF72CF5001DD3DB /* SDL_dataqueue.h in Headers */,
    1.86 +				F30D9C9F212CD0990047DF2E /* SDL_syssensor.h in Headers */,
    1.87  				AA7558AA1595D55500BBD41B /* SDL_joystick.h in Headers */,
    1.88  				AA13B34B1FB8B27800D9FEE6 /* SDL_shape_internals.h in Headers */,
    1.89  				AA7558AB1595D55500BBD41B /* SDL_keyboard.h in Headers */,
    1.90 @@ -1284,6 +1322,7 @@
    1.91  				AA7558AC1595D55500BBD41B /* SDL_keycode.h in Headers */,
    1.92  				AA7558AD1595D55500BBD41B /* SDL_loadso.h in Headers */,
    1.93  				AA7558AE1595D55500BBD41B /* SDL_log.h in Headers */,
    1.94 +				F30D9CA7212CD0BF0047DF2E /* SDL_coremotionsensor.h in Headers */,
    1.95  				AA7558AF1595D55500BBD41B /* SDL_main.h in Headers */,
    1.96  				AA7558B01595D55500BBD41B /* SDL_mouse.h in Headers */,
    1.97  				AA7558B11595D55500BBD41B /* SDL_mutex.h in Headers */,
    1.98 @@ -1306,6 +1345,7 @@
    1.99  				FAD4F7021BA3C4E8008346CE /* SDL_sysjoystick_c.h in Headers */,
   1.100  				AA7558C11595D55500BBD41B /* SDL_surface.h in Headers */,
   1.101  				AA7558C21595D55500BBD41B /* SDL_system.h in Headers */,
   1.102 +				F30D9C99212CD0360047DF2E /* SDL_sensor.h in Headers */,
   1.103  				AA7558C31595D55500BBD41B /* SDL_syswm.h in Headers */,
   1.104  				AA7558C41595D55500BBD41B /* SDL_thread.h in Headers */,
   1.105  				AA7558C51595D55500BBD41B /* SDL_timer.h in Headers */,
   1.106 @@ -1495,9 +1535,11 @@
   1.107  				AADC5A651FDA10CB00960936 /* SDL_render_metal.m in Sources */,
   1.108  				FAB598561BB5C31600BE72C5 /* SDL_sysloadso.c in Sources */,
   1.109  				FAB598571BB5C31600BE72C5 /* SDL_power.c in Sources */,
   1.110 +				F30D9CA1212CD0990047DF2E /* SDL_sensor.c in Sources */,
   1.111  				FAB598581BB5C31600BE72C5 /* SDL_syspower.m in Sources */,
   1.112  				56F9D5601DF73BA400C15B5D /* SDL_dataqueue.c in Sources */,
   1.113  				FAB598591BB5C31600BE72C5 /* SDL_render_gles.c in Sources */,
   1.114 +				F30D9CA6212CD0BF0047DF2E /* SDL_coremotionsensor.m in Sources */,
   1.115  				FAB5985A1BB5C31600BE72C5 /* SDL_render_gles2.c in Sources */,
   1.116  				FAB5985B1BB5C31600BE72C5 /* SDL_shaders_gles2.c in Sources */,
   1.117  				FAB5985D1BB5C31600BE72C5 /* SDL_blendfillrect.c in Sources */,
   1.118 @@ -1570,6 +1612,7 @@
   1.119  			files = (
   1.120  				FD6526810DE8FCDD002AD96B /* SDL_systimer.c in Sources */,
   1.121  				FD6526800DE8FCDD002AD96B /* SDL_timer.c in Sources */,
   1.122 +				F30D9CA5212CD0BF0047DF2E /* SDL_coremotionsensor.m in Sources */,
   1.123  				FD3F4A7B0DEA620800C5B771 /* SDL_string.c in Sources */,
   1.124  				FD6526660DE8FCDD002AD96B /* SDL_dummyaudio.c in Sources */,
   1.125  				FD6526670DE8FCDD002AD96B /* SDL_audio.c in Sources */,
   1.126 @@ -1667,6 +1710,7 @@
   1.127  				0402A85912FE70C600CECEE3 /* SDL_shaders_gles2.c in Sources */,
   1.128  				04BAC09D1300C1290055DE28 /* SDL_log.c in Sources */,
   1.129  				56EA86FB13E9EC2B002E47EB /* SDL_coreaudio.m in Sources */,
   1.130 +				F30D9CA0212CD0990047DF2E /* SDL_sensor.c in Sources */,
   1.131  				F3BDD79420F51CB8004ECBF3 /* SDL_hidapi_switch.c in Sources */,
   1.132  				93CB792613FC5F5300BD3E05 /* SDL_uikitviewcontroller.m in Sources */,
   1.133  				AA628ADB159369E3005138DD /* SDL_rotate.c in Sources */,
     2.1 --- a/include/SDL_config_iphoneos.h	Tue Aug 21 16:47:44 2018 -0700
     2.2 +++ b/include/SDL_config_iphoneos.h	Tue Aug 21 17:24:12 2018 -0700
     2.3 @@ -139,8 +139,8 @@
     2.4  #define SDL_JOYSTICK_MFI 1
     2.5  #define SDL_JOYSTICK_HIDAPI 1
     2.6  
     2.7 -/* Enable the dummy sensor driver */
     2.8 -#define SDL_SENSOR_DUMMY  1
     2.9 +/* Enable the CoreMotion sensor driver */
    2.10 +#define SDL_SENSOR_COREMOTION   1
    2.11  
    2.12  /* Enable Unix style SO loading */
    2.13  #define SDL_LOADSO_DLOPEN 1
     3.1 --- a/src/sensor/SDL_sensor.c	Tue Aug 21 16:47:44 2018 -0700
     3.2 +++ b/src/sensor/SDL_sensor.c	Tue Aug 21 17:24:12 2018 -0700
     3.3 @@ -36,6 +36,9 @@
     3.4  #ifdef SDL_SENSOR_ANDROID
     3.5      &SDL_ANDROID_SensorDriver,
     3.6  #endif
     3.7 +#ifdef SDL_SENSOR_COREMOTION
     3.8 +    &SDL_COREMOTION_SensorDriver,
     3.9 +#endif
    3.10  #if defined(SDL_SENSOR_DUMMY) || defined(SDL_SENSOR_DISABLED)
    3.11      &SDL_DUMMY_SensorDriver
    3.12  #endif
     4.1 --- a/src/sensor/SDL_syssensor.h	Tue Aug 21 16:47:44 2018 -0700
     4.2 +++ b/src/sensor/SDL_syssensor.h	Tue Aug 21 17:24:12 2018 -0700
     4.3 @@ -93,6 +93,7 @@
     4.4  
     4.5  /* The available sensor drivers */
     4.6  extern SDL_SensorDriver SDL_ANDROID_SensorDriver;
     4.7 +extern SDL_SensorDriver SDL_COREMOTION_SensorDriver;
     4.8  extern SDL_SensorDriver SDL_DUMMY_SensorDriver;
     4.9  
    4.10  /* vi: set ts=4 sw=4 expandtab: */
     5.1 --- a/src/sensor/android/SDL_androidsensor.c	Tue Aug 21 16:47:44 2018 -0700
     5.2 +++ b/src/sensor/android/SDL_androidsensor.c	Tue Aug 21 17:24:12 2018 -0700
     5.3 @@ -156,7 +156,7 @@
     5.4  }
     5.5      
     5.6  static void
     5.7 -SDL_ANDROID_SensorUpdate(SDL_Sensor* sensor)
     5.8 +SDL_ANDROID_SensorUpdate(SDL_Sensor *sensor)
     5.9  {
    5.10      int events;
    5.11      ASensorEvent event;
    5.12 @@ -171,7 +171,7 @@
    5.13  }
    5.14  
    5.15  static void
    5.16 -SDL_ANDROID_SensorClose(SDL_Sensor * sensor)
    5.17 +SDL_ANDROID_SensorClose(SDL_Sensor *sensor)
    5.18  {
    5.19      if (sensor->hwdata) {
    5.20          ASensorEventQueue_disableSensor(sensor->hwdata->eventqueue, sensor->hwdata->asensor);
     6.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     6.2 +++ b/src/sensor/coremotion/SDL_coremotionsensor.h	Tue Aug 21 17:24:12 2018 -0700
     6.3 @@ -0,0 +1,30 @@
     6.4 +/*
     6.5 +  Simple DirectMedia Layer
     6.6 +  Copyright (C) 1997-2018 Sam Lantinga <slouken@libsdl.org>
     6.7 +
     6.8 +  This software is provided 'as-is', without any express or implied
     6.9 +  warranty.  In no event will the authors be held liable for any damages
    6.10 +  arising from the use of this software.
    6.11 +
    6.12 +  Permission is granted to anyone to use this software for any purpose,
    6.13 +  including commercial applications, and to alter it and redistribute it
    6.14 +  freely, subject to the following restrictions:
    6.15 +
    6.16 +  1. The origin of this software must not be misrepresented; you must not
    6.17 +     claim that you wrote the original software. If you use this software
    6.18 +     in a product, an acknowledgment in the product documentation would be
    6.19 +     appreciated but is not required.
    6.20 +  2. Altered source versions must be plainly marked as such, and must not be
    6.21 +     misrepresented as being the original software.
    6.22 +  3. This notice may not be removed or altered from any source distribution.
    6.23 +*/
    6.24 +#include "SDL_config.h"
    6.25 +
    6.26 +/* The private structure used to keep track of a sensor */
    6.27 +struct sensor_hwdata
    6.28 +{
    6.29 +    float data[3];
    6.30 +};
    6.31 +
    6.32 +
    6.33 +/* vi: set ts=4 sw=4 expandtab: */
     7.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     7.2 +++ b/src/sensor/coremotion/SDL_coremotionsensor.m	Tue Aug 21 17:24:12 2018 -0700
     7.3 @@ -0,0 +1,236 @@
     7.4 +/*
     7.5 +  Simple DirectMedia Layer
     7.6 +  Copyright (C) 1997-2018 Sam Lantinga <slouken@libsdl.org>
     7.7 +
     7.8 +  This software is provided 'as-is', without any express or implied
     7.9 +  warranty.  In no event will the authors be held liable for any damages
    7.10 +  arising from the use of this software.
    7.11 +
    7.12 +  Permission is granted to anyone to use this software for any purpose,
    7.13 +  including commercial applications, and to alter it and redistribute it
    7.14 +  freely, subject to the following restrictions:
    7.15 +
    7.16 +  1. The origin of this software must not be misrepresented; you must not
    7.17 +     claim that you wrote the original software. If you use this software
    7.18 +     in a product, an acknowledgment in the product documentation would be
    7.19 +     appreciated but is not required.
    7.20 +  2. Altered source versions must be plainly marked as such, and must not be
    7.21 +     misrepresented as being the original software.
    7.22 +  3. This notice may not be removed or altered from any source distribution.
    7.23 +*/
    7.24 +
    7.25 +#include "SDL_config.h"
    7.26 +
    7.27 +#ifdef SDL_SENSOR_COREMOTION
    7.28 +
    7.29 +/* This is the system specific header for the SDL sensor API */
    7.30 +#include <CoreMotion/CoreMotion.h>
    7.31 +
    7.32 +#include "SDL_error.h"
    7.33 +#include "SDL_sensor.h"
    7.34 +#include "SDL_coremotionsensor.h"
    7.35 +#include "../SDL_syssensor.h"
    7.36 +#include "../SDL_sensor_c.h"
    7.37 +
    7.38 +#define STANDARD_GRAVITY    9.80665f
    7.39 +
    7.40 +typedef struct
    7.41 +{
    7.42 +    SDL_SensorType type;
    7.43 +    SDL_SensorID instance_id;
    7.44 +} SDL_CoreMotionSensor;
    7.45 +
    7.46 +static CMMotionManager *SDL_motion_manager;
    7.47 +static SDL_CoreMotionSensor *SDL_sensors;
    7.48 +static int SDL_sensors_count;
    7.49 +
    7.50 +static int
    7.51 +SDL_COREMOTION_SensorInit(void)
    7.52 +{
    7.53 +    int i, sensors_count = 0;
    7.54 +
    7.55 +    if (!SDL_motion_manager) {
    7.56 +        SDL_motion_manager = [[CMMotionManager alloc] init];
    7.57 +    }
    7.58 +
    7.59 +    if (SDL_motion_manager.accelerometerAvailable) {
    7.60 +        ++sensors_count;
    7.61 +    }
    7.62 +    if (SDL_motion_manager.gyroAvailable) {
    7.63 +        ++sensors_count;
    7.64 +    }
    7.65 +
    7.66 +    if (sensors_count > 0) {
    7.67 +        SDL_sensors = (SDL_CoreMotionSensor *)SDL_calloc(sensors_count, sizeof(*SDL_sensors));
    7.68 +        if (!SDL_sensors) {
    7.69 +            return SDL_OutOfMemory();
    7.70 +        }
    7.71 +
    7.72 +        i = 0;
    7.73 +        if (SDL_motion_manager.accelerometerAvailable) {
    7.74 +            SDL_sensors[i].type = SDL_SENSOR_ACCEL;
    7.75 +            SDL_sensors[i].instance_id = SDL_GetNextSensorInstanceID();
    7.76 +            ++i;
    7.77 +        }
    7.78 +        if (SDL_motion_manager.gyroAvailable) {
    7.79 +            SDL_sensors[i].type = SDL_SENSOR_GYRO;
    7.80 +            SDL_sensors[i].instance_id = SDL_GetNextSensorInstanceID();
    7.81 +            ++i;
    7.82 +        }
    7.83 +        SDL_sensors_count = sensors_count;
    7.84 +    }
    7.85 +    return 0;
    7.86 +}
    7.87 +
    7.88 +static int
    7.89 +SDL_COREMOTION_SensorGetCount(void)
    7.90 +{
    7.91 +    return SDL_sensors_count;
    7.92 +}
    7.93 +
    7.94 +static void
    7.95 +SDL_COREMOTION_SensorDetect(void)
    7.96 +{
    7.97 +}
    7.98 +
    7.99 +static const char *
   7.100 +SDL_COREMOTION_SensorGetDeviceName(int device_index)
   7.101 +{
   7.102 +    switch (SDL_sensors[device_index].type) {
   7.103 +    case SDL_SENSOR_ACCEL:
   7.104 +        return "Accelerometer";
   7.105 +    case SDL_SENSOR_GYRO:
   7.106 +        return "Gyro";
   7.107 +    default:
   7.108 +        return "Unknown";
   7.109 +    }
   7.110 +}
   7.111 +
   7.112 +static SDL_SensorType
   7.113 +SDL_COREMOTION_SensorGetDeviceType(int device_index)
   7.114 +{
   7.115 +    return SDL_sensors[device_index].type;
   7.116 +}
   7.117 +
   7.118 +static int
   7.119 +SDL_COREMOTION_SensorGetDeviceNonPortableType(int device_index)
   7.120 +{
   7.121 +    return SDL_sensors[device_index].type;
   7.122 +}
   7.123 +
   7.124 +static SDL_SensorID
   7.125 +SDL_COREMOTION_SensorGetDeviceInstanceID(int device_index)
   7.126 +{
   7.127 +    return SDL_sensors[device_index].instance_id;
   7.128 +}
   7.129 +
   7.130 +static int
   7.131 +SDL_COREMOTION_SensorOpen(SDL_Sensor *sensor, int device_index)
   7.132 +{
   7.133 +    struct sensor_hwdata *hwdata;
   7.134 +
   7.135 +    hwdata = (struct sensor_hwdata *)SDL_calloc(1, sizeof(*hwdata));
   7.136 +    if (hwdata == NULL) {
   7.137 +        return SDL_OutOfMemory();
   7.138 +    }
   7.139 +    sensor->hwdata = hwdata;
   7.140 +
   7.141 +    switch (sensor->type)
   7.142 +    {
   7.143 +    case SDL_SENSOR_ACCEL:
   7.144 +        [SDL_motion_manager startAccelerometerUpdates];
   7.145 +        break;
   7.146 +    case SDL_SENSOR_GYRO:
   7.147 +        [SDL_motion_manager startGyroUpdates];
   7.148 +        break;
   7.149 +    default:
   7.150 +        break;
   7.151 +    }
   7.152 +    return 0;
   7.153 +}
   7.154 +    
   7.155 +static void
   7.156 +SDL_COREMOTION_SensorUpdate(SDL_Sensor *sensor)
   7.157 +{
   7.158 +    switch (sensor->type)
   7.159 +    {
   7.160 +    case SDL_SENSOR_ACCEL:
   7.161 +        {
   7.162 +            CMAccelerometerData *accelerometerData = SDL_motion_manager.accelerometerData;
   7.163 +            if (accelerometerData) {
   7.164 +                CMAcceleration acceleration = accelerometerData.acceleration;
   7.165 +                float data[3];
   7.166 +                data[0] = acceleration.x * STANDARD_GRAVITY;
   7.167 +                data[1] = acceleration.y * STANDARD_GRAVITY;
   7.168 +                data[2] = acceleration.z * STANDARD_GRAVITY;
   7.169 +                if (SDL_memcmp(data, sensor->hwdata->data, sizeof(data)) != 0) {
   7.170 +                    SDL_PrivateSensorUpdate(sensor, data, SDL_arraysize(data));
   7.171 +                    SDL_memcpy(sensor->hwdata->data, data, sizeof(data));
   7.172 +                }
   7.173 +            }
   7.174 +        }
   7.175 +        break;
   7.176 +    case SDL_SENSOR_GYRO:
   7.177 +        {
   7.178 +            CMGyroData *gyroData = SDL_motion_manager.gyroData;
   7.179 +            if (gyroData) {
   7.180 +                CMRotationRate rotationRate = gyroData.rotationRate;
   7.181 +                float data[3];
   7.182 +                data[0] = rotationRate.x;
   7.183 +                data[1] = rotationRate.y;
   7.184 +                data[2] = rotationRate.z;
   7.185 +                if (SDL_memcmp(data, sensor->hwdata->data, sizeof(data)) != 0) {
   7.186 +                    SDL_PrivateSensorUpdate(sensor, data, SDL_arraysize(data));
   7.187 +                    SDL_memcpy(sensor->hwdata->data, data, sizeof(data));
   7.188 +                }
   7.189 +            }
   7.190 +        }
   7.191 +        break;
   7.192 +    default:
   7.193 +        break;
   7.194 +    }
   7.195 +}
   7.196 +
   7.197 +static void
   7.198 +SDL_COREMOTION_SensorClose(SDL_Sensor *sensor)
   7.199 +{
   7.200 +    if (sensor->hwdata) {
   7.201 +        switch (sensor->type)
   7.202 +        {
   7.203 +        case SDL_SENSOR_ACCEL:
   7.204 +            [SDL_motion_manager stopAccelerometerUpdates];
   7.205 +            break;
   7.206 +        case SDL_SENSOR_GYRO:
   7.207 +            [SDL_motion_manager stopGyroUpdates];
   7.208 +            break;
   7.209 +        default:
   7.210 +            break;
   7.211 +        }
   7.212 +        SDL_free(sensor->hwdata);
   7.213 +        sensor->hwdata = NULL;
   7.214 +    }
   7.215 +}
   7.216 +
   7.217 +static void
   7.218 +SDL_COREMOTION_SensorQuit(void)
   7.219 +{
   7.220 +}
   7.221 +
   7.222 +SDL_SensorDriver SDL_COREMOTION_SensorDriver =
   7.223 +{
   7.224 +    SDL_COREMOTION_SensorInit,
   7.225 +    SDL_COREMOTION_SensorGetCount,
   7.226 +    SDL_COREMOTION_SensorDetect,
   7.227 +    SDL_COREMOTION_SensorGetDeviceName,
   7.228 +    SDL_COREMOTION_SensorGetDeviceType,
   7.229 +    SDL_COREMOTION_SensorGetDeviceNonPortableType,
   7.230 +    SDL_COREMOTION_SensorGetDeviceInstanceID,
   7.231 +    SDL_COREMOTION_SensorOpen,
   7.232 +    SDL_COREMOTION_SensorUpdate,
   7.233 +    SDL_COREMOTION_SensorClose,
   7.234 +    SDL_COREMOTION_SensorQuit,
   7.235 +};
   7.236 +
   7.237 +#endif /* SDL_SENSOR_COREMOTION */
   7.238 +
   7.239 +/* vi: set ts=4 sw=4 expandtab: */
     8.1 --- a/src/sensor/dummy/SDL_dummysensor.c	Tue Aug 21 16:47:44 2018 -0700
     8.2 +++ b/src/sensor/dummy/SDL_dummysensor.c	Tue Aug 21 17:24:12 2018 -0700
     8.3 @@ -76,12 +76,12 @@
     8.4  }
     8.5      
     8.6  static void
     8.7 -SDL_DUMMY_SensorUpdate(SDL_Sensor* sensor)
     8.8 +SDL_DUMMY_SensorUpdate(SDL_Sensor *sensor)
     8.9  {
    8.10  }
    8.11  
    8.12  static void
    8.13 -SDL_DUMMY_SensorClose(SDL_Sensor * sensor)
    8.14 +SDL_DUMMY_SensorClose(SDL_Sensor *sensor)
    8.15  {
    8.16  }
    8.17