summaryrefslogtreecommitdiff
path: root/src/contrib/SDL-3.2.20/test/testsensor.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/contrib/SDL-3.2.20/test/testsensor.c')
-rw-r--r--src/contrib/SDL-3.2.20/test/testsensor.c140
1 files changed, 140 insertions, 0 deletions
diff --git a/src/contrib/SDL-3.2.20/test/testsensor.c b/src/contrib/SDL-3.2.20/test/testsensor.c
new file mode 100644
index 0000000..42d1fa6
--- /dev/null
+++ b/src/contrib/SDL-3.2.20/test/testsensor.c
@@ -0,0 +1,140 @@
1/*
2 Copyright (C) 1997-2025 Sam Lantinga <slouken@libsdl.org>
3
4 This software is provided 'as-is', without any express or implied
5 warranty. In no event will the authors be held liable for any damages
6 arising from the use of this software.
7
8 Permission is granted to anyone to use this software for any purpose,
9 including commercial applications, and to alter it and redistribute it
10 freely.
11*/
12
13/* Simple test of the SDL sensor code */
14
15#include <SDL3/SDL.h>
16#include <SDL3/SDL_main.h>
17#include <SDL3/SDL_test.h>
18
19static const char *GetSensorTypeString(SDL_SensorType type)
20{
21 static char unknown_type[64];
22
23 switch (type) {
24 case SDL_SENSOR_INVALID:
25 return "SDL_SENSOR_INVALID";
26 case SDL_SENSOR_UNKNOWN:
27 return "SDL_SENSOR_UNKNOWN";
28 case SDL_SENSOR_ACCEL:
29 return "SDL_SENSOR_ACCEL";
30 case SDL_SENSOR_GYRO:
31 return "SDL_SENSOR_GYRO";
32 default:
33 (void)SDL_snprintf(unknown_type, sizeof(unknown_type), "UNKNOWN (%d)", type);
34 return unknown_type;
35 }
36}
37
38static void HandleSensorEvent(SDL_SensorEvent *event)
39{
40 SDL_Sensor *sensor = SDL_GetSensorFromID(event->which);
41 if (!sensor) {
42 SDL_Log("Couldn't get sensor for sensor event");
43 return;
44 }
45
46 switch (SDL_GetSensorType(sensor)) {
47 case SDL_SENSOR_ACCEL:
48 SDL_Log("Accelerometer update: %.2f, %.2f, %.2f", event->data[0], event->data[1], event->data[2]);
49 break;
50 case SDL_SENSOR_GYRO:
51 SDL_Log("Gyro update: %.2f, %.2f, %.2f", event->data[0], event->data[1], event->data[2]);
52 break;
53 default:
54 SDL_Log("Sensor update for sensor type %s", GetSensorTypeString(SDL_GetSensorType(sensor)));
55 break;
56 }
57}
58
59int main(int argc, char **argv)
60{
61 SDL_SensorID *sensors;
62 int i, num_sensors, num_opened;
63 SDLTest_CommonState *state;
64
65 /* Initialize test framework */
66 state = SDLTest_CommonCreateState(argv, 0);
67 if (!state) {
68 return 1;
69 }
70
71 if (!SDLTest_CommonDefaultArgs(state, argc, argv)) {
72 SDL_Quit();
73 SDLTest_CommonDestroyState(state);
74 return 1;
75 }
76
77 /* Load the SDL library */
78 if (!SDL_Init(SDL_INIT_SENSOR)) {
79 SDL_Log("Couldn't initialize SDL: %s", SDL_GetError());
80 SDL_Quit();
81 SDLTest_CommonDestroyState(state);
82 return 1;
83 }
84
85 sensors = SDL_GetSensors(&num_sensors);
86 num_opened = 0;
87
88 SDL_Log("There are %d sensors available", num_sensors);
89 if (sensors) {
90 for (i = 0; i < num_sensors; ++i) {
91 SDL_Log("Sensor %" SDL_PRIu32 ": %s, type %s, platform type %d",
92 sensors[i],
93 SDL_GetSensorNameForID(sensors[i]),
94 GetSensorTypeString(SDL_GetSensorTypeForID(sensors[i])),
95 SDL_GetSensorNonPortableTypeForID(sensors[i]));
96
97 if (SDL_GetSensorTypeForID(sensors[i]) != SDL_SENSOR_UNKNOWN) {
98 SDL_Sensor *sensor = SDL_OpenSensor(sensors[i]);
99 if (!sensor) {
100 SDL_Log("Couldn't open sensor %" SDL_PRIu32 ": %s", sensors[i], SDL_GetError());
101 } else {
102 ++num_opened;
103 }
104 }
105 }
106 SDL_free(sensors);
107 }
108 SDL_Log("Opened %d sensors", num_opened);
109
110 if (num_opened > 0) {
111 bool done = false;
112 SDL_Event event;
113
114 SDL_CreateWindow("Sensor Test", 0, 0, SDL_WINDOW_FULLSCREEN);
115 while (!done) {
116 /* Update to get the current event state */
117 SDL_PumpEvents();
118
119 /* Process all currently pending events */
120 while (SDL_PeepEvents(&event, 1, SDL_GETEVENT, SDL_EVENT_FIRST, SDL_EVENT_LAST) == 1) {
121 switch (event.type) {
122 case SDL_EVENT_SENSOR_UPDATE:
123 HandleSensorEvent(&event.sensor);
124 break;
125 case SDL_EVENT_MOUSE_BUTTON_UP:
126 case SDL_EVENT_KEY_UP:
127 case SDL_EVENT_QUIT:
128 done = true;
129 break;
130 default:
131 break;
132 }
133 }
134 }
135 }
136
137 SDL_Quit();
138 SDLTest_CommonDestroyState(state);
139 return 0;
140}