src/sensor/windows/SDL_windowssensor.c
changeset 13841 b89df0d683ad
parent 13839 52dbeb97b294
child 13842 a09f7d0800eb
equal deleted inserted replaced
13840:df7f6873371a 13841:b89df0d683ad
    34 #define COBJMACROS
    34 #define COBJMACROS
    35 #include <InitGuid.h>
    35 #include <InitGuid.h>
    36 #include <SensorsApi.h>
    36 #include <SensorsApi.h>
    37 #include <Sensors.h>
    37 #include <Sensors.h>
    38 
    38 
    39 #ifndef CLSID_SensorManager
    39 DEFINE_GUID(SDL_CLSID_SensorManager, 0x77A1C827, 0xFCD2, 0x4689, 0x89, 0x15, 0x9D, 0x61, 0x3C, 0xC5, 0xFA, 0x3E);
    40 DEFINE_GUID(CLSID_SensorManager, 0x77A1C827, 0xFCD2, 0x4689, 0x89, 0x15, 0x9D, 0x61, 0x3C, 0xC5, 0xFA, 0x3E);
    40 DEFINE_GUID(SDL_IID_SensorManager, 0xBD77DB67, 0x45A8, 0x42DC, 0x8D, 0x00, 0x6D, 0xCF, 0x15, 0xF8, 0x37, 0x7A);
    41 #endif
    41 DEFINE_GUID(SDL_IID_SensorManagerEvents, 0x9B3B0B86, 0x266A, 0x4AAD, 0xB2, 0x1F, 0xFD, 0xE5, 0x50, 0x10, 0x01, 0xB7);
    42 
    42 DEFINE_GUID(SDL_IID_SensorEvents, 0x5D8DCC91, 0x4641, 0x47E7, 0xB7, 0xC3, 0xB7, 0x4F, 0x48, 0xA6, 0xC3, 0x91);
    43 DEFINE_GUID(IID_SensorManager, 0xBD77DB67, 0x45A8, 0x42DC, 0x8D, 0x00, 0x6D, 0xCF, 0x15, 0xF8, 0x37, 0x7A);
       
    44 DEFINE_GUID(IID_SensorManagerEvents, 0x9B3B0B86, 0x266A, 0x4AAD, 0xB2, 0x1F, 0xFD, 0xE5, 0x50, 0x10, 0x01, 0xB7);
       
    45 DEFINE_GUID(IID_SensorEvents, 0x5D8DCC91, 0x4641, 0x47E7, 0xB7, 0xC3, 0xB7, 0x4F, 0x48, 0xA6, 0xC3, 0x91);
       
    46 
    43 
    47 /* These constants aren't available in Visual Studio 2015 or earlier Windows SDK  */
    44 /* These constants aren't available in Visual Studio 2015 or earlier Windows SDK  */
    48 #ifndef SENSOR_DATA_TYPE_ANGULAR_VELOCITY_X_DEGREES_PER_SECOND
    45 DEFINE_PROPERTYKEY(SDL_SENSOR_DATA_TYPE_ANGULAR_VELOCITY_X_DEGREES_PER_SECOND, 0X3F8A69A2, 0X7C5, 0X4E48, 0XA9, 0X65, 0XCD, 0X79, 0X7A, 0XAB, 0X56, 0XD5, 10); //[VT_R8]
    49 DEFINE_PROPERTYKEY(SENSOR_DATA_TYPE_ANGULAR_VELOCITY_X_DEGREES_PER_SECOND, 0X3F8A69A2, 0X7C5, 0X4E48, 0XA9, 0X65, 0XCD, 0X79, 0X7A, 0XAB, 0X56, 0XD5, 10); //[VT_R8]
    46 DEFINE_PROPERTYKEY(SDL_SENSOR_DATA_TYPE_ANGULAR_VELOCITY_Y_DEGREES_PER_SECOND, 0X3F8A69A2, 0X7C5, 0X4E48, 0XA9, 0X65, 0XCD, 0X79, 0X7A, 0XAB, 0X56, 0XD5, 11); //[VT_R8]
    50 DEFINE_PROPERTYKEY(SENSOR_DATA_TYPE_ANGULAR_VELOCITY_Y_DEGREES_PER_SECOND, 0X3F8A69A2, 0X7C5, 0X4E48, 0XA9, 0X65, 0XCD, 0X79, 0X7A, 0XAB, 0X56, 0XD5, 11); //[VT_R8]
    47 DEFINE_PROPERTYKEY(SDL_SENSOR_DATA_TYPE_ANGULAR_VELOCITY_Z_DEGREES_PER_SECOND, 0X3F8A69A2, 0X7C5, 0X4E48, 0XA9, 0X65, 0XCD, 0X79, 0X7A, 0XAB, 0X56, 0XD5, 12); //[VT_R8]
    51 DEFINE_PROPERTYKEY(SENSOR_DATA_TYPE_ANGULAR_VELOCITY_Z_DEGREES_PER_SECOND, 0X3F8A69A2, 0X7C5, 0X4E48, 0XA9, 0X65, 0XCD, 0X79, 0X7A, 0XAB, 0X56, 0XD5, 12); //[VT_R8]
       
    52 #endif
       
    53 
    48 
    54 typedef struct
    49 typedef struct
    55 {
    50 {
    56     SDL_SensorID id;
    51     SDL_SensorID id;
    57     ISensor *sensor;
    52     ISensor *sensor;
    75     if (!ppvObject) {
    70     if (!ppvObject) {
    76         return E_INVALIDARG;
    71         return E_INVALIDARG;
    77     }
    72     }
    78 
    73 
    79     *ppvObject = NULL;
    74     *ppvObject = NULL;
    80     if (WIN_IsEqualIID(riid, &IID_IUnknown) || WIN_IsEqualIID(riid, &IID_SensorManagerEvents)) {
    75     if (WIN_IsEqualIID(riid, &IID_IUnknown) || WIN_IsEqualIID(riid, &SDL_IID_SensorManagerEvents)) {
    81         *ppvObject = This;
    76         *ppvObject = This;
    82         return S_OK;
    77         return S_OK;
    83     }
    78     }
    84     return E_NOINTERFACE;
    79     return E_NOINTERFACE;
    85 }
    80 }
   115     if (!ppvObject) {
   110     if (!ppvObject) {
   116         return E_INVALIDARG;
   111         return E_INVALIDARG;
   117     }
   112     }
   118 
   113 
   119     *ppvObject = NULL;
   114     *ppvObject = NULL;
   120     if (WIN_IsEqualIID(riid, &IID_IUnknown) || WIN_IsEqualIID(riid, &IID_SensorEvents)) {
   115     if (WIN_IsEqualIID(riid, &IID_IUnknown) || WIN_IsEqualIID(riid, &SDL_IID_SensorEvents)) {
   121         *ppvObject = This;
   116         *ppvObject = This;
   122         return S_OK;
   117         return S_OK;
   123     }
   118     }
   124     return E_NOINTERFACE;
   119     return E_NOINTERFACE;
   125 }
   120 }
   164 #ifdef DEBUG_SENSORS
   159 #ifdef DEBUG_SENSORS
   165                 SDL_Log("Sensor %s data updated\n", SDL_sensors[i].name);
   160                 SDL_Log("Sensor %s data updated\n", SDL_sensors[i].name);
   166 #endif
   161 #endif
   167                 switch (SDL_sensors[i].type) {
   162                 switch (SDL_sensors[i].type) {
   168                 case SDL_SENSOR_ACCEL:
   163                 case SDL_SENSOR_ACCEL:
   169                     hrX = ISensorDataReport_GetSensorValue(pNewData, &SENSOR_DATA_TYPE_ACCELERATION_X_G, &valueX);
   164                     hrX = ISensorDataReport_GetSensorValue(pNewData, &SDL_SENSOR_DATA_TYPE_ACCELERATION_X_G, &valueX);
   170                     hrY = ISensorDataReport_GetSensorValue(pNewData, &SENSOR_DATA_TYPE_ACCELERATION_Y_G, &valueY);
   165                     hrY = ISensorDataReport_GetSensorValue(pNewData, &SDL_SENSOR_DATA_TYPE_ACCELERATION_Y_G, &valueY);
   171                     hrZ = ISensorDataReport_GetSensorValue(pNewData, &SENSOR_DATA_TYPE_ACCELERATION_Z_G, &valueZ);
   166                     hrZ = ISensorDataReport_GetSensorValue(pNewData, &SDL_SENSOR_DATA_TYPE_ACCELERATION_Z_G, &valueZ);
   172                     if (SUCCEEDED(hrX) && SUCCEEDED(hrY) && SUCCEEDED(hrZ) &&
   167                     if (SUCCEEDED(hrX) && SUCCEEDED(hrY) && SUCCEEDED(hrZ) &&
   173                         valueX.vt == VT_R8 && valueY.vt == VT_R8 && valueZ.vt == VT_R8) {
   168                         valueX.vt == VT_R8 && valueY.vt == VT_R8 && valueZ.vt == VT_R8) {
   174                         float values[3];
   169                         float values[3];
   175 
   170 
   176                         values[0] = (float)valueX.dblVal * SDL_STANDARD_GRAVITY;
   171                         values[0] = (float)valueX.dblVal * SDL_STANDARD_GRAVITY;
   178                         values[2] = (float)valueZ.dblVal * SDL_STANDARD_GRAVITY;
   173                         values[2] = (float)valueZ.dblVal * SDL_STANDARD_GRAVITY;
   179                         SDL_PrivateSensorUpdate(SDL_sensors[i].sensor_opened, values, 3);
   174                         SDL_PrivateSensorUpdate(SDL_sensors[i].sensor_opened, values, 3);
   180                     }
   175                     }
   181                     break;
   176                     break;
   182                 case SDL_SENSOR_GYRO:
   177                 case SDL_SENSOR_GYRO:
   183                     hrX = ISensorDataReport_GetSensorValue(pNewData, &SENSOR_DATA_TYPE_ANGULAR_VELOCITY_X_DEGREES_PER_SECOND, &valueX);
   178                     hrX = ISensorDataReport_GetSensorValue(pNewData, &SDL_SENSOR_DATA_TYPE_ANGULAR_VELOCITY_X_DEGREES_PER_SECOND, &valueX);
   184                     hrY = ISensorDataReport_GetSensorValue(pNewData, &SENSOR_DATA_TYPE_ANGULAR_VELOCITY_Y_DEGREES_PER_SECOND, &valueY);
   179                     hrY = ISensorDataReport_GetSensorValue(pNewData, &SDL_SENSOR_DATA_TYPE_ANGULAR_VELOCITY_Y_DEGREES_PER_SECOND, &valueY);
   185                     hrZ = ISensorDataReport_GetSensorValue(pNewData, &SENSOR_DATA_TYPE_ANGULAR_VELOCITY_Z_DEGREES_PER_SECOND, &valueZ);
   180                     hrZ = ISensorDataReport_GetSensorValue(pNewData, &SDL_SENSOR_DATA_TYPE_ANGULAR_VELOCITY_Z_DEGREES_PER_SECOND, &valueZ);
   186                     if (SUCCEEDED(hrX) && SUCCEEDED(hrY) && SUCCEEDED(hrZ) &&
   181                     if (SUCCEEDED(hrX) && SUCCEEDED(hrY) && SUCCEEDED(hrZ) &&
   187                         valueX.vt == VT_R8 && valueY.vt == VT_R8 && valueZ.vt == VT_R8) {
   182                         valueX.vt == VT_R8 && valueY.vt == VT_R8 && valueZ.vt == VT_R8) {
   188                         const float DEGREES_TO_RADIANS = (float)(M_PI / 180.0f);
   183                         const float DEGREES_TO_RADIANS = (float)(M_PI / 180.0f);
   189                         float values[3];
   184                         float values[3];
   190 
   185 
   352 
   347 
   353     if (WIN_CoInitialize() == S_OK) {
   348     if (WIN_CoInitialize() == S_OK) {
   354         SDL_windowscoinit = SDL_TRUE;
   349         SDL_windowscoinit = SDL_TRUE;
   355     }
   350     }
   356 
   351 
   357     hr = CoCreateInstance(&CLSID_SensorManager, NULL, CLSCTX_INPROC_SERVER, &IID_SensorManager, (LPVOID *) &SDL_sensor_manager);
   352     hr = CoCreateInstance(&SDL_CLSID_SensorManager, NULL, CLSCTX_INPROC_SERVER, &SDL_IID_SensorManager, (LPVOID *) &SDL_sensor_manager);
   358     if (FAILED(hr)) {
   353     if (FAILED(hr)) {
   359         return SDL_SetError("Couldn't create the sensor manager: 0x%.4x", hr);
   354         return SDL_SetError("Couldn't create the sensor manager: 0x%.4x", hr);
   360     }
   355     }
   361 
   356 
   362     hr = ISensorManager_SetEventSink(SDL_sensor_manager, &sensor_manager_events);
   357     hr = ISensorManager_SetEventSink(SDL_sensor_manager, &sensor_manager_events);