2021-03-12 20:58:20 +00:00
|
|
|
/*
|
2023-01-09 17:48:21 +00:00
|
|
|
Copyright (C) 1997-2023 Sam Lantinga <slouken@libsdl.org>
|
2021-03-12 20:58:20 +00:00
|
|
|
|
|
|
|
This software is provided 'as-is', without any express or implied
|
|
|
|
warranty. In no event will the authors be held liable for any damages
|
|
|
|
arising from the use of this software.
|
|
|
|
|
|
|
|
Permission is granted to anyone to use this software for any purpose,
|
|
|
|
including commercial applications, and to alter it and redistribute it
|
|
|
|
freely.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* Simple program: Loop, watching keystrokes
|
|
|
|
Note that you need to call SDL_PollEvent() or SDL_WaitEvent() to
|
|
|
|
pump the event loop and catch keystrokes.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#ifdef __EMSCRIPTEN__
|
|
|
|
#include <emscripten/emscripten.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "SDL.h"
|
|
|
|
|
|
|
|
int done;
|
|
|
|
|
|
|
|
/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
|
|
|
|
static void
|
|
|
|
quit(int rc)
|
|
|
|
{
|
|
|
|
SDL_Quit();
|
|
|
|
exit(rc);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
print_string(char **text, size_t *maxlen, const char *fmt, ...)
|
|
|
|
{
|
|
|
|
int len;
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
va_start(ap, fmt);
|
|
|
|
len = SDL_vsnprintf(*text, *maxlen, fmt, ap);
|
|
|
|
if (len > 0) {
|
|
|
|
*text += len;
|
2022-11-30 20:51:59 +00:00
|
|
|
if (((size_t)len) < *maxlen) {
|
|
|
|
*maxlen -= (size_t)len;
|
2021-03-12 20:58:20 +00:00
|
|
|
} else {
|
|
|
|
*maxlen = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
va_end(ap);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
print_modifiers(char **text, size_t *maxlen)
|
|
|
|
{
|
|
|
|
int mod;
|
|
|
|
print_string(text, maxlen, " modifiers:");
|
|
|
|
mod = SDL_GetModState();
|
|
|
|
if (!mod) {
|
|
|
|
print_string(text, maxlen, " (none)");
|
|
|
|
return;
|
|
|
|
}
|
2022-11-27 16:38:43 +00:00
|
|
|
if (mod & KMOD_LSHIFT) {
|
2021-03-12 20:58:20 +00:00
|
|
|
print_string(text, maxlen, " LSHIFT");
|
2022-11-27 16:38:43 +00:00
|
|
|
}
|
|
|
|
if (mod & KMOD_RSHIFT) {
|
2021-03-12 20:58:20 +00:00
|
|
|
print_string(text, maxlen, " RSHIFT");
|
2022-11-27 16:38:43 +00:00
|
|
|
}
|
|
|
|
if (mod & KMOD_LCTRL) {
|
2021-03-12 20:58:20 +00:00
|
|
|
print_string(text, maxlen, " LCTRL");
|
2022-11-27 16:38:43 +00:00
|
|
|
}
|
|
|
|
if (mod & KMOD_RCTRL) {
|
2021-03-12 20:58:20 +00:00
|
|
|
print_string(text, maxlen, " RCTRL");
|
2022-11-27 16:38:43 +00:00
|
|
|
}
|
|
|
|
if (mod & KMOD_LALT) {
|
2021-03-12 20:58:20 +00:00
|
|
|
print_string(text, maxlen, " LALT");
|
2022-11-27 16:38:43 +00:00
|
|
|
}
|
|
|
|
if (mod & KMOD_RALT) {
|
2021-03-12 20:58:20 +00:00
|
|
|
print_string(text, maxlen, " RALT");
|
2022-11-27 16:38:43 +00:00
|
|
|
}
|
|
|
|
if (mod & KMOD_LGUI) {
|
2021-03-12 20:58:20 +00:00
|
|
|
print_string(text, maxlen, " LGUI");
|
2022-11-27 16:38:43 +00:00
|
|
|
}
|
|
|
|
if (mod & KMOD_RGUI) {
|
2021-03-12 20:58:20 +00:00
|
|
|
print_string(text, maxlen, " RGUI");
|
2022-11-27 16:38:43 +00:00
|
|
|
}
|
|
|
|
if (mod & KMOD_NUM) {
|
2021-03-12 20:58:20 +00:00
|
|
|
print_string(text, maxlen, " NUM");
|
2022-11-27 16:38:43 +00:00
|
|
|
}
|
|
|
|
if (mod & KMOD_CAPS) {
|
2021-03-12 20:58:20 +00:00
|
|
|
print_string(text, maxlen, " CAPS");
|
2022-11-27 16:38:43 +00:00
|
|
|
}
|
|
|
|
if (mod & KMOD_MODE) {
|
2021-03-12 20:58:20 +00:00
|
|
|
print_string(text, maxlen, " MODE");
|
2022-11-27 16:38:43 +00:00
|
|
|
}
|
|
|
|
if (mod & KMOD_SCROLL) {
|
2021-08-11 00:50:17 +00:00
|
|
|
print_string(text, maxlen, " SCROLL");
|
2022-11-27 16:38:43 +00:00
|
|
|
}
|
2021-03-12 20:58:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
PrintModifierState()
|
|
|
|
{
|
|
|
|
char message[512];
|
|
|
|
char *spot;
|
|
|
|
size_t left;
|
|
|
|
|
|
|
|
spot = message;
|
|
|
|
left = sizeof(message);
|
|
|
|
|
|
|
|
print_modifiers(&spot, &left);
|
|
|
|
SDL_Log("Initial state:%s\n", message);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2022-11-30 20:51:59 +00:00
|
|
|
PrintKey(SDL_Keysym *sym, SDL_bool pressed, SDL_bool repeat)
|
2021-03-12 20:58:20 +00:00
|
|
|
{
|
|
|
|
char message[512];
|
|
|
|
char *spot;
|
|
|
|
size_t left;
|
|
|
|
|
|
|
|
spot = message;
|
|
|
|
left = sizeof(message);
|
|
|
|
|
|
|
|
/* Print the keycode, name and state */
|
|
|
|
if (sym->sym) {
|
|
|
|
print_string(&spot, &left,
|
2022-11-30 20:51:59 +00:00
|
|
|
"Key %s: scancode %d = %s, keycode 0x%08X = %s ",
|
|
|
|
pressed ? "pressed " : "released",
|
|
|
|
sym->scancode,
|
|
|
|
SDL_GetScancodeName(sym->scancode),
|
|
|
|
sym->sym, SDL_GetKeyName(sym->sym));
|
2021-03-12 20:58:20 +00:00
|
|
|
} else {
|
|
|
|
print_string(&spot, &left,
|
2022-11-30 20:51:59 +00:00
|
|
|
"Unknown Key (scancode %d = %s) %s ",
|
|
|
|
sym->scancode,
|
|
|
|
SDL_GetScancodeName(sym->scancode),
|
|
|
|
pressed ? "pressed " : "released");
|
2021-03-12 20:58:20 +00:00
|
|
|
}
|
|
|
|
print_modifiers(&spot, &left);
|
|
|
|
if (repeat) {
|
|
|
|
print_string(&spot, &left, " (repeat)");
|
|
|
|
}
|
|
|
|
SDL_Log("%s\n", message);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2021-11-19 22:02:02 +00:00
|
|
|
PrintText(const char *eventtype, const char *text)
|
2021-03-12 20:58:20 +00:00
|
|
|
{
|
2021-11-19 22:02:02 +00:00
|
|
|
const char *spot;
|
|
|
|
char expanded[1024];
|
2021-03-12 20:58:20 +00:00
|
|
|
|
|
|
|
expanded[0] = '\0';
|
2022-11-30 20:51:59 +00:00
|
|
|
for (spot = text; *spot; ++spot) {
|
2021-03-12 20:58:20 +00:00
|
|
|
size_t length = SDL_strlen(expanded);
|
2023-03-09 23:10:00 +00:00
|
|
|
(void)SDL_snprintf(expanded + length, sizeof(expanded) - length, "\\x%.2x", (unsigned char)*spot);
|
2021-03-12 20:58:20 +00:00
|
|
|
}
|
|
|
|
SDL_Log("%s Text (%s): \"%s%s\"\n", eventtype, expanded, *text == '"' ? "\\" : "", text);
|
|
|
|
}
|
|
|
|
|
2022-11-30 20:51:59 +00:00
|
|
|
void loop()
|
2021-03-12 20:58:20 +00:00
|
|
|
{
|
|
|
|
SDL_Event event;
|
|
|
|
/* Check for events */
|
|
|
|
/*SDL_WaitEvent(&event); emscripten does not like waiting*/
|
|
|
|
|
2022-12-01 21:07:03 +00:00
|
|
|
(void)fprintf(stderr, "starting loop\n");
|
|
|
|
(void)fflush(stderr);
|
2021-03-12 20:58:20 +00:00
|
|
|
// while (SDL_PollEvent(&event)) {
|
|
|
|
while (!done && SDL_WaitEvent(&event)) {
|
2022-12-01 21:07:03 +00:00
|
|
|
SDL_Log("Got event type: %" SDL_PRIu32 "\n", event.type);
|
2021-03-12 20:58:20 +00:00
|
|
|
switch (event.type) {
|
|
|
|
case SDL_KEYDOWN:
|
|
|
|
case SDL_KEYUP:
|
|
|
|
PrintKey(&event.key.keysym, (event.key.state == SDL_PRESSED) ? SDL_TRUE : SDL_FALSE, (event.key.repeat) ? SDL_TRUE : SDL_FALSE);
|
|
|
|
break;
|
|
|
|
case SDL_TEXTEDITING:
|
|
|
|
PrintText("EDIT", event.text.text);
|
|
|
|
break;
|
|
|
|
case SDL_TEXTINPUT:
|
|
|
|
PrintText("INPUT", event.text.text);
|
|
|
|
break;
|
|
|
|
case SDL_MOUSEBUTTONDOWN:
|
|
|
|
/* Left button quits the app, other buttons toggles text input */
|
2022-12-01 21:07:03 +00:00
|
|
|
(void)fprintf(stderr, "mouse button down button: %d (LEFT=%d)\n", event.button.button, SDL_BUTTON_LEFT);
|
|
|
|
(void)fflush(stderr);
|
2021-03-12 20:58:20 +00:00
|
|
|
if (event.button.button == SDL_BUTTON_LEFT) {
|
|
|
|
done = 1;
|
|
|
|
} else {
|
|
|
|
if (SDL_IsTextInputActive()) {
|
|
|
|
SDL_Log("Stopping text input\n");
|
|
|
|
SDL_StopTextInput();
|
|
|
|
} else {
|
|
|
|
SDL_Log("Starting text input\n");
|
|
|
|
SDL_StartTextInput();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case SDL_QUIT:
|
|
|
|
done = 1;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2022-12-01 21:07:03 +00:00
|
|
|
(void)fprintf(stderr, "waiting new event\n");
|
|
|
|
(void)fflush(stderr);
|
2021-03-12 20:58:20 +00:00
|
|
|
}
|
2022-12-01 21:07:03 +00:00
|
|
|
(void)fprintf(stderr, "exiting event loop\n");
|
|
|
|
(void)fflush(stderr);
|
2021-03-12 20:58:20 +00:00
|
|
|
#ifdef __EMSCRIPTEN__
|
|
|
|
if (done) {
|
|
|
|
emscripten_cancel_main_loop();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Very simple thread - counts 0 to 9 delaying 50ms between increments */
|
2021-06-04 21:05:56 +00:00
|
|
|
static int SDLCALL ping_thread(void *ptr)
|
2021-03-12 20:58:20 +00:00
|
|
|
{
|
|
|
|
int cnt;
|
|
|
|
SDL_Event sdlevent;
|
2022-11-30 20:51:59 +00:00
|
|
|
SDL_memset(&sdlevent, 0, sizeof(SDL_Event));
|
2021-03-12 20:58:20 +00:00
|
|
|
for (cnt = 0; cnt < 10; ++cnt) {
|
2022-12-01 21:07:03 +00:00
|
|
|
(void)fprintf(stderr, "sending event (%d/%d) from thread.\n", cnt + 1, 10);
|
|
|
|
(void)fflush(stderr);
|
2021-03-12 20:58:20 +00:00
|
|
|
sdlevent.type = SDL_KEYDOWN;
|
|
|
|
sdlevent.key.keysym.sym = SDLK_1;
|
|
|
|
SDL_PushEvent(&sdlevent);
|
|
|
|
SDL_Delay(1000 + rand() % 1000);
|
|
|
|
}
|
|
|
|
return cnt;
|
|
|
|
}
|
|
|
|
|
2022-11-30 20:51:59 +00:00
|
|
|
int main(int argc, char *argv[])
|
2021-03-12 20:58:20 +00:00
|
|
|
{
|
|
|
|
SDL_Window *window;
|
2021-10-27 01:27:34 +00:00
|
|
|
SDL_Renderer *renderer;
|
2021-06-04 21:05:56 +00:00
|
|
|
SDL_Thread *thread;
|
2021-03-12 20:58:20 +00:00
|
|
|
|
|
|
|
/* Enable standard application logging */
|
|
|
|
SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
|
|
|
|
|
|
|
|
/* Initialize SDL */
|
|
|
|
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
|
|
|
|
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError());
|
2022-11-27 16:38:43 +00:00
|
|
|
return 1;
|
2021-03-12 20:58:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Set 640x480 video mode */
|
|
|
|
window = SDL_CreateWindow("CheckKeys Test",
|
|
|
|
SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
|
|
|
|
640, 480, 0);
|
2022-11-27 16:38:43 +00:00
|
|
|
if (window == NULL) {
|
2021-03-12 20:58:20 +00:00
|
|
|
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create 640x480 window: %s\n",
|
2022-11-30 20:51:59 +00:00
|
|
|
SDL_GetError());
|
2021-03-12 20:58:20 +00:00
|
|
|
quit(2);
|
|
|
|
}
|
|
|
|
|
2021-10-27 01:27:34 +00:00
|
|
|
/* On wayland, no window will actually show until something has
|
|
|
|
actually been displayed.
|
|
|
|
*/
|
|
|
|
renderer = SDL_CreateRenderer(window, -1, 0);
|
|
|
|
SDL_RenderPresent(renderer);
|
|
|
|
|
2021-03-12 20:58:20 +00:00
|
|
|
#if __IPHONEOS__
|
|
|
|
/* Creating the context creates the view, which we need to show keyboard */
|
|
|
|
SDL_GL_CreateContext(window);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
SDL_StartTextInput();
|
|
|
|
|
|
|
|
/* Print initial modifier state */
|
|
|
|
SDL_PumpEvents();
|
|
|
|
PrintModifierState();
|
|
|
|
|
|
|
|
/* Watch keystrokes */
|
|
|
|
done = 0;
|
|
|
|
|
2022-12-01 21:07:03 +00:00
|
|
|
thread = SDL_CreateThread(ping_thread, "PingThread", NULL);
|
2021-03-12 20:58:20 +00:00
|
|
|
|
|
|
|
#ifdef __EMSCRIPTEN__
|
|
|
|
emscripten_set_main_loop(loop, 0, 1);
|
|
|
|
#else
|
|
|
|
while (!done) {
|
|
|
|
loop();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
SDL_WaitThread(thread, NULL);
|
|
|
|
SDL_Quit();
|
2022-11-27 16:38:43 +00:00
|
|
|
return 0;
|
2021-03-12 20:58:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* vi: set ts=4 sw=4 expandtab: */
|