Debian Codegraph @

test/checkkeysthreads.c

raw
1 /*
2 Copyright (C) 1997-2026 Sam Lantinga <[email protected]>
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 program: Loop, watching keystrokes
14 Note that you need to call SDL_PollEvent() or SDL_WaitEvent() to
15 pump the event loop and catch keystrokes.
16 */
17
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <string.h>
21
22 #ifdef __EMSCRIPTEN__
23 #include <emscripten/emscripten.h>
24 #endif
25
26 #include "SDL.h"
27
28 int done;
29
30 /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
31 static void
32 quit(int rc)
33 {
34 SDL_Quit();
35 exit(rc);
36 }
37
38 static void
39 print_string(char **text, size_t *maxlen, const char *fmt, ...)
40 {
41 int len;
42 va_list ap;
43
44 va_start(ap, fmt);
45 len = SDL_vsnprintf(*text, *maxlen, fmt, ap);
46 if (len > 0) {
47 *text += len;
48 if (((size_t)len) < *maxlen) {
49 *maxlen -= (size_t)len;
50 } else {
51 *maxlen = 0;
52 }
53 }
54 va_end(ap);
55 }
56
57 static void
58 print_modifiers(char **text, size_t *maxlen)
59 {
60 int mod;
61 print_string(text, maxlen, " modifiers:");
62 mod = SDL_GetModState();
63 if (!mod) {
64 print_string(text, maxlen, " (none)");
65 return;
66 }
67 if (mod & KMOD_LSHIFT) {
68 print_string(text, maxlen, " LSHIFT");
69 }
70 if (mod & KMOD_RSHIFT) {
71 print_string(text, maxlen, " RSHIFT");
72 }
73 if (mod & KMOD_LCTRL) {
74 print_string(text, maxlen, " LCTRL");
75 }
76 if (mod & KMOD_RCTRL) {
77 print_string(text, maxlen, " RCTRL");
78 }
79 if (mod & KMOD_LALT) {
80 print_string(text, maxlen, " LALT");
81 }
82 if (mod & KMOD_RALT) {
83 print_string(text, maxlen, " RALT");
84 }
85 if (mod & KMOD_LGUI) {
86 print_string(text, maxlen, " LGUI");
87 }
88 if (mod & KMOD_RGUI) {
89 print_string(text, maxlen, " RGUI");
90 }
91 if (mod & KMOD_NUM) {
92 print_string(text, maxlen, " NUM");
93 }
94 if (mod & KMOD_CAPS) {
95 print_string(text, maxlen, " CAPS");
96 }
97 if (mod & KMOD_MODE) {
98 print_string(text, maxlen, " MODE");
99 }
100 if (mod & KMOD_SCROLL) {
101 print_string(text, maxlen, " SCROLL");
102 }
103 }
104
105 static void
106 PrintModifierState(void)
107 {
108 char message[512];
109 char *spot;
110 size_t left;
111
112 spot = message;
113 left = sizeof(message);
114
115 print_modifiers(&spot, &left);
116 SDL_Log("Initial state:%s\n", message);
117 }
118
119 static void
120 PrintKey(SDL_Keysym *sym, SDL_bool pressed, SDL_bool repeat)
121 {
122 char message[512];
123 char *spot;
124 size_t left;
125
126 spot = message;
127 left = sizeof(message);
128
129 /* Print the keycode, name and state */
130 if (sym->sym) {
131 print_string(&spot, &left,
132 "Key %s: scancode %d = %s, keycode 0x%08X = %s ",
133 pressed ? "pressed " : "released",
134 sym->scancode,
135 SDL_GetScancodeName(sym->scancode),
136 sym->sym, SDL_GetKeyName(sym->sym));
137 } else {
138 print_string(&spot, &left,
139 "Unknown Key (scancode %d = %s) %s ",
140 sym->scancode,
141 SDL_GetScancodeName(sym->scancode),
142 pressed ? "pressed " : "released");
143 }
144 print_modifiers(&spot, &left);
145 if (repeat) {
146 print_string(&spot, &left, " (repeat)");
147 }
148 SDL_Log("%s\n", message);
149 }
150
151 static void
152 PrintText(const char *eventtype, const char *text)
153 {
154 const char *spot;
155 char expanded[1024];
156
157 expanded[0] = '\0';
158 for (spot = text; *spot; ++spot) {
159 size_t length = SDL_strlen(expanded);
160 (void)SDL_snprintf(expanded + length, sizeof(expanded) - length, "\\x%.2x", (unsigned char)*spot);
161 }
162 SDL_Log("%s Text (%s): \"%s%s\"\n", eventtype, expanded, *text == '"' ? "\\" : "", text);
163 }
164
165 void loop(void)
166 {
167 SDL_Event event;
168 /* Check for events */
169 /*SDL_WaitEvent(&event); emscripten does not like waiting*/
170
171 (void)fprintf(stderr, "starting loop\n");
172 (void)fflush(stderr);
173 // while (SDL_PollEvent(&event)) {
174 while (!done && SDL_WaitEvent(&event)) {
175 SDL_Log("Got event type: %" SDL_PRIu32 "\n", event.type);
176 switch (event.type) {
177 case SDL_KEYDOWN:
178 case SDL_KEYUP:
179 PrintKey(&event.key.keysym, (event.key.state == SDL_PRESSED) ? SDL_TRUE : SDL_FALSE, (event.key.repeat) ? SDL_TRUE : SDL_FALSE);
180 break;
181 case SDL_TEXTEDITING:
182 PrintText("EDIT", event.text.text);
183 break;
184 case SDL_TEXTINPUT:
185 PrintText("INPUT", event.text.text);
186 break;
187 case SDL_MOUSEBUTTONDOWN:
188 /* Left button quits the app, other buttons toggles text input */
189 (void)fprintf(stderr, "mouse button down button: %d (LEFT=%d)\n", event.button.button, SDL_BUTTON_LEFT);
190 (void)fflush(stderr);
191 if (event.button.button == SDL_BUTTON_LEFT) {
192 done = 1;
193 } else {
194 if (SDL_IsTextInputActive()) {
195 SDL_Log("Stopping text input\n");
196 SDL_StopTextInput();
197 } else {
198 SDL_Log("Starting text input\n");
199 SDL_StartTextInput();
200 }
201 }
202 break;
203 case SDL_QUIT:
204 done = 1;
205 break;
206 default:
207 break;
208 }
209 (void)fprintf(stderr, "waiting new event\n");
210 (void)fflush(stderr);
211 }
212 (void)fprintf(stderr, "exiting event loop\n");
213 (void)fflush(stderr);
214 #ifdef __EMSCRIPTEN__
215 if (done) {
216 emscripten_cancel_main_loop();
217 }
218 #endif
219 }
220
221 /* Very simple thread - counts 0 to 9 delaying 50ms between increments */
222 static int SDLCALL ping_thread(void *ptr)
223 {
224 int cnt;
225 SDL_Event sdlevent;
226 SDL_memset(&sdlevent, 0, sizeof(SDL_Event));
227 for (cnt = 0; cnt < 10; ++cnt) {
228 (void)fprintf(stderr, "sending event (%d/%d) from thread.\n", cnt + 1, 10);
229 (void)fflush(stderr);
230 sdlevent.type = SDL_KEYDOWN;
231 sdlevent.key.keysym.sym = SDLK_1;
232 SDL_PushEvent(&sdlevent);
233 SDL_Delay(1000 + rand() % 1000);
234 }
235 return cnt;
236 }
237
238 int main(int argc, char *argv[])
239 {
240 SDL_Window *window;
241 SDL_Renderer *renderer;
242 SDL_Thread *thread;
243
244 /* Enable standard application logging */
245 SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
246
247 /* Initialize SDL */
248 if (SDL_Init(SDL_INIT_VIDEO) < 0) {
249 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError());
250 return 1;
251 }
252
253 /* Set 640x480 video mode */
254 window = SDL_CreateWindow("CheckKeys Test",
255 SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
256 640, 480, 0);
257 if (!window) {
258 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create 640x480 window: %s\n",
259 SDL_GetError());
260 quit(2);
261 }
262
263 /* On wayland, no window will actually show until something has
264 actually been displayed.
265 */
266 renderer = SDL_CreateRenderer(window, -1, 0);
267 SDL_RenderPresent(renderer);
268
269 #ifdef __IPHONEOS__
270 /* Creating the context creates the view, which we need to show keyboard */
271 SDL_GL_CreateContext(window);
272 #endif
273
274 SDL_StartTextInput();
275
276 /* Print initial modifier state */
277 SDL_PumpEvents();
278 PrintModifierState();
279
280 /* Watch keystrokes */
281 done = 0;
282
283 thread = SDL_CreateThread(ping_thread, "PingThread", NULL);
284
285 #ifdef __EMSCRIPTEN__
286 emscripten_set_main_loop(loop, 0, 1);
287 #else
288 while (!done) {
289 loop();
290 }
291 #endif
292
293 SDL_WaitThread(thread, NULL);
294 SDL_Quit();
295 return 0;
296 }
297
298 /* vi: set ts=4 sw=4 expandtab: */