Commit e9c94ac0b39375f20ff1f71e4797a9de0534c3d2

Sam Lantinga 2020-04-08T10:27:30

Fixed Windows accelerometer data units

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
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
diff --git a/src/sensor/windows/SDL_windowssensor.c b/src/sensor/windows/SDL_windowssensor.c
index 6b9fac4..53b592e 100644
--- a/src/sensor/windows/SDL_windowssensor.c
+++ b/src/sensor/windows/SDL_windowssensor.c
@@ -47,12 +47,12 @@ DEFINE_PROPERTYKEY(SENSOR_DATA_TYPE_ANGULAR_VELOCITY_Z_DEGREES_PER_SECOND, 0X3F8
 
 typedef struct
 {
-	SDL_SensorID id;
-	ISensor *sensor;
-	SENSOR_ID sensor_id;
-	char *name;
-	SDL_SensorType type;
-	SDL_Sensor *sensor_opened;
+    SDL_SensorID id;
+    ISensor *sensor;
+    SENSOR_ID sensor_id;
+    char *name;
+    SDL_SensorType type;
+    SDL_Sensor *sensor_opened;
 
 } SDL_Windows_Sensor;
 
@@ -66,320 +66,322 @@ static int DisconnectSensor(ISensor *sensor);
 
 static HRESULT STDMETHODCALLTYPE ISensorManagerEventsVtbl_QueryInterface(ISensorManagerEvents * This, REFIID riid, void **ppvObject)
 {
-	if (!ppvObject) {
-		return E_INVALIDARG;
-	}
-
-	*ppvObject = NULL;
-	if (WIN_IsEqualIID(riid, &IID_IUnknown) || WIN_IsEqualIID(riid, &IID_SensorManagerEvents)) {
-		*ppvObject = This;
-		return S_OK;
-	}
-	return E_NOINTERFACE;
+    if (!ppvObject) {
+        return E_INVALIDARG;
+    }
+
+    *ppvObject = NULL;
+    if (WIN_IsEqualIID(riid, &IID_IUnknown) || WIN_IsEqualIID(riid, &IID_SensorManagerEvents)) {
+        *ppvObject = This;
+        return S_OK;
+    }
+    return E_NOINTERFACE;
 }
 
 static ULONG STDMETHODCALLTYPE ISensorManagerEventsVtbl_AddRef(ISensorManagerEvents * This)
 {
-	return 1;
+    return 1;
 }
 
 static ULONG STDMETHODCALLTYPE ISensorManagerEventsVtbl_Release(ISensorManagerEvents * This)
 {
-	return 1;
+    return 1;
 }
 
 static HRESULT STDMETHODCALLTYPE ISensorManagerEventsVtbl_OnSensorEnter(ISensorManagerEvents * This, ISensor *pSensor, SensorState state)
 {
-	ConnectSensor(pSensor);
-	return S_OK;
+    ConnectSensor(pSensor);
+    return S_OK;
 }
 
 static ISensorManagerEventsVtbl sensor_manager_events_vtbl = {
-	ISensorManagerEventsVtbl_QueryInterface,
-	ISensorManagerEventsVtbl_AddRef,
-	ISensorManagerEventsVtbl_Release,
-	ISensorManagerEventsVtbl_OnSensorEnter
+    ISensorManagerEventsVtbl_QueryInterface,
+    ISensorManagerEventsVtbl_AddRef,
+    ISensorManagerEventsVtbl_Release,
+    ISensorManagerEventsVtbl_OnSensorEnter
 };
 static ISensorManagerEvents sensor_manager_events = {
-	&sensor_manager_events_vtbl
+    &sensor_manager_events_vtbl
 };
 
 static HRESULT STDMETHODCALLTYPE ISensorEventsVtbl_QueryInterface(ISensorEvents * This, REFIID riid, void **ppvObject)
 {
-	if (!ppvObject) {
-		return E_INVALIDARG;
-	}
-
-	*ppvObject = NULL;
-	if (WIN_IsEqualIID(riid, &IID_IUnknown) || WIN_IsEqualIID(riid, &IID_SensorEvents)) {
-		*ppvObject = This;
-		return S_OK;
-	}
-	return E_NOINTERFACE;
+    if (!ppvObject) {
+        return E_INVALIDARG;
+    }
+
+    *ppvObject = NULL;
+    if (WIN_IsEqualIID(riid, &IID_IUnknown) || WIN_IsEqualIID(riid, &IID_SensorEvents)) {
+        *ppvObject = This;
+        return S_OK;
+    }
+    return E_NOINTERFACE;
 }
 
 static ULONG STDMETHODCALLTYPE ISensorEventsVtbl_AddRef(ISensorEvents * This)
 {
-	return 1;
+    return 1;
 }
 
 static ULONG STDMETHODCALLTYPE ISensorEventsVtbl_Release(ISensorEvents * This)
 {
-	return 1;
+    return 1;
 }
 
 static HRESULT STDMETHODCALLTYPE ISensorEventsVtbl_OnStateChanged(ISensorEvents * This, ISensor *pSensor, SensorState state)
 {
 #ifdef DEBUG_SENSORS
-	int i;
-
-	SDL_LockSensors();
-	for (i = 0; i < SDL_num_sensors; ++i) {
-		if (pSensor == SDL_sensors[i].sensor) {
-			SDL_Log("Sensor %s state changed to %d\n", SDL_sensors[i].name, state);
-		}
-	}
-	SDL_UnlockSensors();
+    int i;
+
+    SDL_LockSensors();
+    for (i = 0; i < SDL_num_sensors; ++i) {
+        if (pSensor == SDL_sensors[i].sensor) {
+            SDL_Log("Sensor %s state changed to %d\n", SDL_sensors[i].name, state);
+        }
+    }
+    SDL_UnlockSensors();
 #endif
-	return S_OK;
+    return S_OK;
 }
 
 static HRESULT STDMETHODCALLTYPE ISensorEventsVtbl_OnDataUpdated(ISensorEvents * This, ISensor *pSensor, ISensorDataReport *pNewData)
 {
-	int i;
+    int i;
 
-	SDL_LockSensors();
-	for (i = 0; i < SDL_num_sensors; ++i) {
-		if (pSensor == SDL_sensors[i].sensor) {
-			if (SDL_sensors[i].sensor_opened) {
-				HRESULT hrX, hrY, hrZ;
-				PROPVARIANT valueX, valueY, valueZ;
+    SDL_LockSensors();
+    for (i = 0; i < SDL_num_sensors; ++i) {
+        if (pSensor == SDL_sensors[i].sensor) {
+            if (SDL_sensors[i].sensor_opened) {
+                HRESULT hrX, hrY, hrZ;
+                PROPVARIANT valueX, valueY, valueZ;
 
 #ifdef DEBUG_SENSORS
-				SDL_Log("Sensor %s data updated\n", SDL_sensors[i].name);
+                SDL_Log("Sensor %s data updated\n", SDL_sensors[i].name);
 #endif
-				switch (SDL_sensors[i].type) {
-				case SDL_SENSOR_ACCEL:
-					hrX = ISensorDataReport_GetSensorValue(pNewData, &SENSOR_DATA_TYPE_ACCELERATION_X_G, &valueX);
-					hrY = ISensorDataReport_GetSensorValue(pNewData, &SENSOR_DATA_TYPE_ACCELERATION_Y_G, &valueY);
-					hrZ = ISensorDataReport_GetSensorValue(pNewData, &SENSOR_DATA_TYPE_ACCELERATION_Z_G, &valueZ);
-					if (SUCCEEDED(hrX) && SUCCEEDED(hrY) && SUCCEEDED(hrZ) &&
-						valueX.vt == VT_R8 && valueY.vt == VT_R8 && valueZ.vt == VT_R8) {
-						float values[3];
-
-						values[0] = (float)valueX.dblVal;
-						values[1] = (float)valueY.dblVal;
-						values[2] = (float)valueZ.dblVal;
-						SDL_PrivateSensorUpdate(SDL_sensors[i].sensor_opened, values, 3);
-					}
-					break;
-				case SDL_SENSOR_GYRO:
-					hrX = ISensorDataReport_GetSensorValue(pNewData, &SENSOR_DATA_TYPE_ANGULAR_VELOCITY_X_DEGREES_PER_SECOND, &valueX);
-					hrY = ISensorDataReport_GetSensorValue(pNewData, &SENSOR_DATA_TYPE_ANGULAR_VELOCITY_Y_DEGREES_PER_SECOND, &valueY);
-					hrZ = ISensorDataReport_GetSensorValue(pNewData, &SENSOR_DATA_TYPE_ANGULAR_VELOCITY_Z_DEGREES_PER_SECOND, &valueZ);
-					if (SUCCEEDED(hrX) && SUCCEEDED(hrY) && SUCCEEDED(hrZ) &&
-						valueX.vt == VT_R8 && valueY.vt == VT_R8 && valueZ.vt == VT_R8) {
-						float values[3];
-
-						values[0] = (float)(valueX.dblVal * (M_PI / 180.0));
-						values[1] = (float)(valueY.dblVal * (M_PI / 180.0));
-						values[2] = (float)(valueZ.dblVal * (M_PI / 180.0));
-						SDL_PrivateSensorUpdate(SDL_sensors[i].sensor_opened, values, 3);
-					}
-					break;
-				default:
-					/* FIXME: Need to know how to interpret the data for this sensor */
-					break;
-				}
-			}
-			break;
-		}
-	}
-	SDL_UnlockSensors();
-
-	return S_OK;
+                switch (SDL_sensors[i].type) {
+                case SDL_SENSOR_ACCEL:
+                    hrX = ISensorDataReport_GetSensorValue(pNewData, &SENSOR_DATA_TYPE_ACCELERATION_X_G, &valueX);
+                    hrY = ISensorDataReport_GetSensorValue(pNewData, &SENSOR_DATA_TYPE_ACCELERATION_Y_G, &valueY);
+                    hrZ = ISensorDataReport_GetSensorValue(pNewData, &SENSOR_DATA_TYPE_ACCELERATION_Z_G, &valueZ);
+                    if (SUCCEEDED(hrX) && SUCCEEDED(hrY) && SUCCEEDED(hrZ) &&
+                        valueX.vt == VT_R8 && valueY.vt == VT_R8 && valueZ.vt == VT_R8) {
+                        float values[3];
+
+                        values[0] = (float)valueX.dblVal * SDL_STANDARD_GRAVITY;
+                        values[1] = (float)valueY.dblVal * SDL_STANDARD_GRAVITY;
+                        values[2] = (float)valueZ.dblVal * SDL_STANDARD_GRAVITY;
+                        SDL_PrivateSensorUpdate(SDL_sensors[i].sensor_opened, values, 3);
+                    }
+                    break;
+                case SDL_SENSOR_GYRO:
+                    hrX = ISensorDataReport_GetSensorValue(pNewData, &SENSOR_DATA_TYPE_ANGULAR_VELOCITY_X_DEGREES_PER_SECOND, &valueX);
+                    hrY = ISensorDataReport_GetSensorValue(pNewData, &SENSOR_DATA_TYPE_ANGULAR_VELOCITY_Y_DEGREES_PER_SECOND, &valueY);
+                    hrZ = ISensorDataReport_GetSensorValue(pNewData, &SENSOR_DATA_TYPE_ANGULAR_VELOCITY_Z_DEGREES_PER_SECOND, &valueZ);
+                    if (SUCCEEDED(hrX) && SUCCEEDED(hrY) && SUCCEEDED(hrZ) &&
+                        valueX.vt == VT_R8 && valueY.vt == VT_R8 && valueZ.vt == VT_R8) {
+                        const float DEGREES_TO_RADIANS = (float)(M_PI / 180.0f);
+                        float values[3];
+
+                        values[0] = (float)valueX.dblVal * DEGREES_TO_RADIANS;
+                        values[1] = (float)valueY.dblVal * DEGREES_TO_RADIANS;
+                        values[2] = (float)valueZ.dblVal * DEGREES_TO_RADIANS;
+                        SDL_PrivateSensorUpdate(SDL_sensors[i].sensor_opened, values, 3);
+                    }
+                    break;
+                default:
+                    /* FIXME: Need to know how to interpret the data for this sensor */
+                    break;
+                }
+            }
+            break;
+        }
+    }
+    SDL_UnlockSensors();
+
+    return S_OK;
 }
 
 static HRESULT STDMETHODCALLTYPE ISensorEventsVtbl_OnEvent(ISensorEvents * This, ISensor *pSensor, REFGUID eventID, IPortableDeviceValues *pEventData)
 {
 #ifdef DEBUG_SENSORS
-	int i;
-
-	SDL_LockSensors();
-	for (i = 0; i < SDL_num_sensors; ++i) {
-		if (pSensor == SDL_sensors[i].sensor) {
-			SDL_Log("Sensor %s event occurred\n", SDL_sensors[i].name);
-		}
-	}
-	SDL_UnlockSensors();
+    int i;
+
+    SDL_LockSensors();
+    for (i = 0; i < SDL_num_sensors; ++i) {
+        if (pSensor == SDL_sensors[i].sensor) {
+            SDL_Log("Sensor %s event occurred\n", SDL_sensors[i].name);
+        }
+    }
+    SDL_UnlockSensors();
 #endif
-	return S_OK;
+    return S_OK;
 }
 
 static HRESULT STDMETHODCALLTYPE ISensorEventsVtbl_OnLeave(ISensorEvents * This, REFSENSOR_ID ID)
 {
-	int i;
+    int i;
 
-	SDL_LockSensors();
-	for (i = 0; i < SDL_num_sensors; ++i) {
-		if (WIN_IsEqualIID(ID, &SDL_sensors[i].sensor_id)) {
+    SDL_LockSensors();
+    for (i = 0; i < SDL_num_sensors; ++i) {
+        if (WIN_IsEqualIID(ID, &SDL_sensors[i].sensor_id)) {
 #ifdef DEBUG_SENSORS
-			SDL_Log("Sensor %s disconnected\n", SDL_sensors[i].name);
+            SDL_Log("Sensor %s disconnected\n", SDL_sensors[i].name);
 #endif
-			DisconnectSensor(SDL_sensors[i].sensor);
-		}
-	}
-	SDL_UnlockSensors();
+            DisconnectSensor(SDL_sensors[i].sensor);
+        }
+    }
+    SDL_UnlockSensors();
 
-	return S_OK;
+    return S_OK;
 }
 
 static ISensorEventsVtbl sensor_events_vtbl = {
-	ISensorEventsVtbl_QueryInterface,
-	ISensorEventsVtbl_AddRef,
-	ISensorEventsVtbl_Release,
-	ISensorEventsVtbl_OnStateChanged,
-	ISensorEventsVtbl_OnDataUpdated,
-	ISensorEventsVtbl_OnEvent,
-	ISensorEventsVtbl_OnLeave
+    ISensorEventsVtbl_QueryInterface,
+    ISensorEventsVtbl_AddRef,
+    ISensorEventsVtbl_Release,
+    ISensorEventsVtbl_OnStateChanged,
+    ISensorEventsVtbl_OnDataUpdated,
+    ISensorEventsVtbl_OnEvent,
+    ISensorEventsVtbl_OnLeave
 };
 static ISensorEvents sensor_events = {
-	&sensor_events_vtbl
+    &sensor_events_vtbl
 };
 
 static int ConnectSensor(ISensor *sensor)
 {
-	SDL_Windows_Sensor *new_sensor, *new_sensors;
-	HRESULT hr;
-	SENSOR_ID sensor_id;
-	SENSOR_TYPE_ID type_id;
-	SDL_SensorType type;
-	BSTR bstr_name = NULL;
-	char *name;
-
-	hr = ISensor_GetID(sensor, &sensor_id);
-	if (FAILED(hr)) {
-		return SDL_SetError("Couldn't get sensor ID: 0x%.4x", hr);
-	}
-
-	hr = ISensor_GetType(sensor, &type_id);
-	if (FAILED(hr)) {
-		return SDL_SetError("Couldn't get sensor type: 0x%.4x", hr);
-	}
-
-	if (WIN_IsEqualIID(&type_id, &SENSOR_TYPE_ACCELEROMETER_3D)) {
-		type = SDL_SENSOR_ACCEL;
-	} else if (WIN_IsEqualIID(&type_id, &SENSOR_TYPE_GYROMETER_3D)) {
-		type = SDL_SENSOR_GYRO;
-	} else {
-		return SDL_SetError("Unknown sensor type");
-	}
-
-	hr = ISensor_GetFriendlyName(sensor, &bstr_name);
-	if (SUCCEEDED(hr) && bstr_name) {
-		name = WIN_StringToUTF8(bstr_name);
-	} else {
-		name = SDL_strdup("Unknown Sensor");
-	}
+    SDL_Windows_Sensor *new_sensor, *new_sensors;
+    HRESULT hr;
+    SENSOR_ID sensor_id;
+    SENSOR_TYPE_ID type_id;
+    SDL_SensorType type;
+    BSTR bstr_name = NULL;
+    char *name;
+
+    hr = ISensor_GetID(sensor, &sensor_id);
+    if (FAILED(hr)) {
+        return SDL_SetError("Couldn't get sensor ID: 0x%.4x", hr);
+    }
+
+    hr = ISensor_GetType(sensor, &type_id);
+    if (FAILED(hr)) {
+        return SDL_SetError("Couldn't get sensor type: 0x%.4x", hr);
+    }
+
+    if (WIN_IsEqualIID(&type_id, &SENSOR_TYPE_ACCELEROMETER_3D)) {
+        type = SDL_SENSOR_ACCEL;
+    } else if (WIN_IsEqualIID(&type_id, &SENSOR_TYPE_GYROMETER_3D)) {
+        type = SDL_SENSOR_GYRO;
+    } else {
+        return SDL_SetError("Unknown sensor type");
+    }
+
+    hr = ISensor_GetFriendlyName(sensor, &bstr_name);
+    if (SUCCEEDED(hr) && bstr_name) {
+        name = WIN_StringToUTF8(bstr_name);
+    } else {
+        name = SDL_strdup("Unknown Sensor");
+    }
     if (bstr_name != NULL) {
         SysFreeString(bstr_name);
     }
-	if (!name) {
-		return SDL_OutOfMemory();
-	}
+    if (!name) {
+        return SDL_OutOfMemory();
+    }
 
-	SDL_LockSensors();
-	new_sensors = (SDL_Windows_Sensor *)SDL_realloc(SDL_sensors, (SDL_num_sensors + 1) * sizeof(SDL_Windows_Sensor));
-	if (new_sensors == NULL) {
-		SDL_UnlockSensors();
-		return SDL_OutOfMemory();
-	}
+    SDL_LockSensors();
+    new_sensors = (SDL_Windows_Sensor *)SDL_realloc(SDL_sensors, (SDL_num_sensors + 1) * sizeof(SDL_Windows_Sensor));
+    if (new_sensors == NULL) {
+        SDL_UnlockSensors();
+        return SDL_OutOfMemory();
+    }
 
-	ISensor_AddRef(sensor);
-	ISensor_SetEventSink(sensor, &sensor_events);
+    ISensor_AddRef(sensor);
+    ISensor_SetEventSink(sensor, &sensor_events);
 
-	SDL_sensors = new_sensors;
-	new_sensor = &SDL_sensors[SDL_num_sensors];
-	++SDL_num_sensors;
+    SDL_sensors = new_sensors;
+    new_sensor = &SDL_sensors[SDL_num_sensors];
+    ++SDL_num_sensors;
 
-	new_sensor->id = SDL_GetNextSensorInstanceID();
-	new_sensor->sensor = sensor;
-	new_sensor->type = type;
-	new_sensor->name = name;
+    SDL_zerop(new_sensor);
+    new_sensor->id = SDL_GetNextSensorInstanceID();
+    new_sensor->sensor = sensor;
+    new_sensor->type = type;
+    new_sensor->name = name;
 
-	SDL_UnlockSensors();
+    SDL_UnlockSensors();
 
-	return 0;
+    return 0;
 }
 
 static int DisconnectSensor(ISensor *sensor)
 {
-	SDL_Windows_Sensor *old_sensor;
-	int i;
-
-	SDL_LockSensors();
-	for (i = 0; i < SDL_num_sensors; ++i) {
-		old_sensor = &SDL_sensors[i];
-		if (sensor == old_sensor->sensor) {
-			ISensor_SetEventSink(sensor, NULL);
-			ISensor_Release(sensor);
-			SDL_free(old_sensor->name);
-			--SDL_num_sensors;
-			if (i < SDL_num_sensors) {
-				SDL_memmove(&SDL_sensors[i], &SDL_sensors[i + 1], (SDL_num_sensors - i) * sizeof(SDL_sensors[i]));
-			}
-			break;
-		}
-	}
-	SDL_UnlockSensors();
-
-	return 0;
+    SDL_Windows_Sensor *old_sensor;
+    int i;
+
+    SDL_LockSensors();
+    for (i = 0; i < SDL_num_sensors; ++i) {
+        old_sensor = &SDL_sensors[i];
+        if (sensor == old_sensor->sensor) {
+            ISensor_SetEventSink(sensor, NULL);
+            ISensor_Release(sensor);
+            SDL_free(old_sensor->name);
+            --SDL_num_sensors;
+            if (i < SDL_num_sensors) {
+                SDL_memmove(&SDL_sensors[i], &SDL_sensors[i + 1], (SDL_num_sensors - i) * sizeof(SDL_sensors[i]));
+            }
+            break;
+        }
+    }
+    SDL_UnlockSensors();
+
+    return 0;
 }
 
 static int
 SDL_WINDOWS_SensorInit(void)
 {
-	HRESULT hr;
-	ISensorCollection *sensor_collection = NULL;
-
-	if (WIN_CoInitialize() == S_OK) {
-		SDL_windowscoinit = SDL_TRUE;
-	}
-
-	hr = CoCreateInstance(&CLSID_SensorManager, NULL, CLSCTX_INPROC_SERVER, &IID_SensorManager, &SDL_sensor_manager);
-	if (FAILED(hr)) {
-		return SDL_SetError("Couldn't create the sensor manager: 0x%.4x", hr);
-	}
-
-	hr = ISensorManager_SetEventSink(SDL_sensor_manager, &sensor_manager_events);
-	if (FAILED(hr)) {
-		ISensorManager_Release(SDL_sensor_manager);
-		return SDL_SetError("Couldn't set the sensor manager event sink: 0x%.4x", hr);
-	}
-
-	hr = ISensorManager_GetSensorsByCategory(SDL_sensor_manager, &SENSOR_CATEGORY_ALL, &sensor_collection);
-	if (SUCCEEDED(hr)) {
-		ULONG i, count;
-
-		hr = ISensorCollection_GetCount(sensor_collection, &count);
-		if (SUCCEEDED(hr)) {
-			for (i = 0; i < count; ++i) {
-				ISensor *sensor;
-
-				hr = ISensorCollection_GetAt(sensor_collection, i, &sensor);
-				if (SUCCEEDED(hr)) {
-					SensorState state;
-
-					hr = ISensor_GetState(sensor, &state);
-					if (SUCCEEDED(hr)) {
-						ISensorManagerEventsVtbl_OnSensorEnter(&sensor_manager_events, sensor, state);
-					}
-					ISensorManager_Release(sensor);
-				}
-			}
-		}
-		ISensorCollection_Release(sensor_collection);
-	}
+    HRESULT hr;
+    ISensorCollection *sensor_collection = NULL;
+
+    if (WIN_CoInitialize() == S_OK) {
+        SDL_windowscoinit = SDL_TRUE;
+    }
+
+    hr = CoCreateInstance(&CLSID_SensorManager, NULL, CLSCTX_INPROC_SERVER, &IID_SensorManager, &SDL_sensor_manager);
+    if (FAILED(hr)) {
+        return SDL_SetError("Couldn't create the sensor manager: 0x%.4x", hr);
+    }
+
+    hr = ISensorManager_SetEventSink(SDL_sensor_manager, &sensor_manager_events);
+    if (FAILED(hr)) {
+        ISensorManager_Release(SDL_sensor_manager);
+        return SDL_SetError("Couldn't set the sensor manager event sink: 0x%.4x", hr);
+    }
+
+    hr = ISensorManager_GetSensorsByCategory(SDL_sensor_manager, &SENSOR_CATEGORY_ALL, &sensor_collection);
+    if (SUCCEEDED(hr)) {
+        ULONG i, count;
+
+        hr = ISensorCollection_GetCount(sensor_collection, &count);
+        if (SUCCEEDED(hr)) {
+            for (i = 0; i < count; ++i) {
+                ISensor *sensor;
+
+                hr = ISensorCollection_GetAt(sensor_collection, i, &sensor);
+                if (SUCCEEDED(hr)) {
+                    SensorState state;
+
+                    hr = ISensor_GetState(sensor, &state);
+                    if (SUCCEEDED(hr)) {
+                        ISensorManagerEventsVtbl_OnSensorEnter(&sensor_manager_events, sensor, state);
+                    }
+                    ISensorManager_Release(sensor);
+                }
+            }
+        }
+        ISensorCollection_Release(sensor_collection);
+    }
     return 0;
 }
 
@@ -403,7 +405,7 @@ SDL_WINDOWS_SensorGetDeviceName(int device_index)
 static SDL_SensorType
 SDL_WINDOWS_SensorGetDeviceType(int device_index)
 {
-	return SDL_sensors[device_index].type;
+    return SDL_sensors[device_index].type;
 }
 
 static int
@@ -421,7 +423,7 @@ SDL_WINDOWS_SensorGetDeviceInstanceID(int device_index)
 static int
 SDL_WINDOWS_SensorOpen(SDL_Sensor *sensor, int device_index)
 {
-	SDL_sensors[device_index].sensor_opened = sensor;
+    SDL_sensors[device_index].sensor_opened = sensor;
     return 0;
 }
 
@@ -433,32 +435,32 @@ SDL_WINDOWS_SensorUpdate(SDL_Sensor *sensor)
 static void
 SDL_WINDOWS_SensorClose(SDL_Sensor *sensor)
 {
-	int i;
-
-	for (i = 0; i < SDL_num_sensors; ++i) {
-		if (sensor == SDL_sensors[i].sensor_opened) {
-			SDL_sensors[i].sensor_opened = NULL;
-			break;
-		}
-	}
+    int i;
+
+    for (i = 0; i < SDL_num_sensors; ++i) {
+        if (sensor == SDL_sensors[i].sensor_opened) {
+            SDL_sensors[i].sensor_opened = NULL;
+            break;
+        }
+    }
 }
 
 static void
 SDL_WINDOWS_SensorQuit(void)
 {
-	while (SDL_num_sensors > 0) {
-		DisconnectSensor(SDL_sensors[0].sensor);
-	}
-
-	if (SDL_sensor_manager) {
-		ISensorManager_SetEventSink(SDL_sensor_manager, NULL);
-		ISensorManager_Release(SDL_sensor_manager);
-		SDL_sensor_manager = NULL;
-	}
-
-	if (SDL_windowscoinit) {
-		WIN_CoUninitialize();
-	}
+    while (SDL_num_sensors > 0) {
+        DisconnectSensor(SDL_sensors[0].sensor);
+    }
+
+    if (SDL_sensor_manager) {
+        ISensorManager_SetEventSink(SDL_sensor_manager, NULL);
+        ISensorManager_Release(SDL_sensor_manager);
+        SDL_sensor_manager = NULL;
+    }
+
+    if (SDL_windowscoinit) {
+        WIN_CoUninitialize();
+    }
 }
 
 SDL_SensorDriver SDL_WINDOWS_SensorDriver =