Skip to content

Instantly share code, notes, and snippets.

@pablotron
Created April 9, 2019 09:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pablotron/f5614e1f566b5eba0586c57a706488dd to your computer and use it in GitHub Desktop.
Save pablotron/f5614e1f566b5eba0586c57a706488dd to your computer and use it in GitHub Desktop.
SDL Trackpad Click Test
CFLAGS=-W -Wall -O2 -pedantic $(shell sdl2-config --cflags)
LIBS=-lm $(shell sdl2-config --libs)
OBJS=sdl-click.o
APP=sdl-click
.PHONY=all clean
all: $(APP)
$(APP): $(OBJS)
$(CC) $(CFLAGS) -o $(APP) $(OBJS) $(LIBS)
%.o: %.c
$(CC) $(CFLAGS) -c $<
clean:
$(RM) -f $(APP) $(OBJS)
#include <stdbool.h> // bool
#include <stdlib.h> // EXIT_{FAILURE,SUCCESS}
#include <string.h> // strerror
#include <errno.h> // errno
#include <SDL.h>
#define UNUSED(a) ((void) (a))
#define die(...) do { \
SDL_LogCritical(SDL_LOG_CATEGORY_APPLICATION, __VA_ARGS__); \
exit(EXIT_FAILURE); \
} while (0)
static void
log_mouse_event(
const SDL_Event * const ev
) {
// populate which_buf
char which_buf[512];
if (ev->button.which == SDL_TOUCH_MOUSEID) {
strncpy(which_buf, "SDL_TOUCH_MOUSEID", sizeof(which_buf));
} else {
snprintf(which_buf, sizeof(which_buf), "%u", ev->button.which);
}
fprintf(
stderr,
"{ "
"type = %s, "
"timestamp = %u, "
"windowID = %u, "
"which = %s, "
"button = %u, "
"state = %u, "
"clicks = %u, "
"x = %d, "
"y = %d "
"}\n",
(ev->button.type == SDL_MOUSEBUTTONDOWN) ? "SDL_MOUSEBUTTONDOWN" : "SDL_MOUSEBUTTONUP",
ev->button.timestamp,
ev->button.windowID,
which_buf,
ev->button.button,
ev->button.state,
ev->button.clicks,
ev->button.x,
ev->button.y
);
}
static void
log_touch_event(const SDL_Event * const ev) {
fprintf(
stderr,
"{ "
"type = %s, "
"timestamp = %u, "
"touchId = %ld, "
"fingerId = %ld, "
"x = %f, "
"y = %f, "
"dx = %f, "
"dy = %f, "
"pressure = %f "
"}\n",
(ev->tfinger.type == SDL_FINGERDOWN) ? "SDL_FINGERDOWN" : "SDL_FINGERUP",
ev->tfinger.timestamp,
ev->tfinger.touchId,
ev->tfinger.fingerId,
ev->tfinger.x,
ev->tfinger.y,
ev->tfinger.dx,
ev->tfinger.dy,
ev->tfinger.pressure
);
}
static void
draw(SDL_Renderer * const renderer) {
// set color
if (SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255)) {
die("SDL_SetRenderDrawColor(): %s", SDL_GetError());
}
// clear background
SDL_RenderClear(renderer);
}
int main(int argc, char *argv[]) {
UNUSED(argc);
UNUSED(argv);
// init sdl
if (SDL_Init(SDL_INIT_VIDEO)) {
die("SDL_Init(): %s", SDL_GetError());
}
// register SDL exit handler
if (atexit(SDL_Quit)) {
die("atexit(SDL_Init): %s", strerror(errno));
exit(EXIT_FAILURE);
}
// create window
SDL_Window *win = SDL_CreateWindow("SDL Click Test", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 800, 600, SDL_WINDOW_RESIZABLE
);
// check for error
if (!win) {
die("SDL_CreateWindow(): %s", SDL_GetError());
}
// create renderer
SDL_Renderer *renderer = SDL_CreateRenderer(win, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
if (!renderer) {
die("SDL_CreateRenderer(): %s", SDL_GetError());
}
// main loop
bool done = false;
while (!done) {
SDL_Event ev;
// read events
while (SDL_PollEvent(&ev)) {
switch (ev.type) {
case SDL_QUIT:
// exit
done = true;
break;
case SDL_KEYUP:
switch (ev.key.keysym.sym) {
case SDLK_ESCAPE:
case SDLK_q:
// exit
done = true;
break;
default:
// ignore event
break;
}
break;
case SDL_MOUSEBUTTONDOWN:
case SDL_MOUSEBUTTONUP:
log_mouse_event(&ev);
break;
case SDL_FINGERDOWN:
case SDL_FINGERUP:
log_touch_event(&ev);
break;
}
}
// draw
draw(renderer);
}
// fini renderer, window
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(win);
// exit with success
return EXIT_SUCCESS;
}
sdl-click test results
actions:
* three left taps (touchpad)
* three right taps (touchpad)
* three left clicks (hardware button)
* three right clicks (hardware button)
# sdl version
> sdl2-config --version
2.0.9
# results of command
> ./sdl-click
// left tap
{ type = SDL_MOUSEBUTTONDOWN, timestamp = 17299, windowID = 2, which = 0, button = 1, state = 1, clicks = 1, x = 466, y = 163 }
{ type = SDL_MOUSEBUTTONUP, timestamp = 17477, windowID = 2, which = 0, button = 1, state = 0, clicks = 1, x = 466, y = 163 }
// left tap
{ type = SDL_MOUSEBUTTONDOWN, timestamp = 22686, windowID = 2, which = 0, button = 1, state = 1, clicks = 1, x = 484, y = 219 }
{ type = SDL_MOUSEBUTTONUP, timestamp = 22863, windowID = 2, which = 0, button = 1, state = 0, clicks = 1, x = 484, y = 219 }
// left tap
{ type = SDL_MOUSEBUTTONDOWN, timestamp = 27562, windowID = 2, which = 0, button = 1, state = 1, clicks = 1, x = 337, y = 207 }
{ type = SDL_MOUSEBUTTONUP, timestamp = 27740, windowID = 2, which = 0, button = 1, state = 0, clicks = 1, x = 337, y = 207 }
// right tap (two finger tap)
{ type = SDL_MOUSEBUTTONDOWN, timestamp = 34497, windowID = 2, which = 0, button = 3, state = 1, clicks = 1, x = 430, y = 295 }
{ type = SDL_MOUSEBUTTONUP, timestamp = 34497, windowID = 2, which = 0, button = 3, state = 0, clicks = 1, x = 430, y = 295 }
// right tap (two finger tap)
{ type = SDL_MOUSEBUTTONDOWN, timestamp = 38453, windowID = 2, which = 0, button = 3, state = 1, clicks = 1, x = 451, y = 299 }
{ type = SDL_MOUSEBUTTONUP, timestamp = 38453, windowID = 2, which = 0, button = 3, state = 0, clicks = 1, x = 451, y = 299 }
// right tap (two finger tap)
{ type = SDL_MOUSEBUTTONDOWN, timestamp = 41964, windowID = 2, which = 0, button = 3, state = 1, clicks = 1, x = 391, y = 306 }
{ type = SDL_MOUSEBUTTONUP, timestamp = 41965, windowID = 2, which = 0, button = 3, state = 0, clicks = 1, x = 391, y = 306 }
// left click (hardware button)
{ type = SDL_MOUSEBUTTONDOWN, timestamp = 49335, windowID = 2, which = 0, button = 1, state = 1, clicks = 1, x = 399, y = 289 }
{ type = SDL_MOUSEBUTTONUP, timestamp = 49414, windowID = 2, which = 0, button = 1, state = 0, clicks = 1, x = 399, y = 289 }
// left click (hardware button)
{ type = SDL_MOUSEBUTTONDOWN, timestamp = 53522, windowID = 2, which = 0, button = 1, state = 1, clicks = 1, x = 491, y = 286 }
{ type = SDL_MOUSEBUTTONUP, timestamp = 53613, windowID = 2, which = 0, button = 1, state = 0, clicks = 1, x = 491, y = 286 }
// left click (hardware button)
{ type = SDL_MOUSEBUTTONDOWN, timestamp = 56849, windowID = 2, which = 0, button = 1, state = 1, clicks = 1, x = 532, y = 317 }
{ type = SDL_MOUSEBUTTONUP, timestamp = 56955, windowID = 2, which = 0, button = 1, state = 0, clicks = 1, x = 532, y = 317 }
// right click (hardware button)
{ type = SDL_MOUSEBUTTONDOWN, timestamp = 61523, windowID = 2, which = 0, button = 3, state = 1, clicks = 1, x = 471, y = 297 }
{ type = SDL_MOUSEBUTTONUP, timestamp = 61661, windowID = 2, which = 0, button = 3, state = 0, clicks = 1, x = 471, y = 297 }
// right click (hardware button)
{ type = SDL_MOUSEBUTTONDOWN, timestamp = 65756, windowID = 2, which = 0, button = 3, state = 1, clicks = 1, x = 609, y = 320 }
{ type = SDL_MOUSEBUTTONUP, timestamp = 65896, windowID = 2, which = 0, button = 3, state = 0, clicks = 1, x = 609, y = 320 }
// right click (hardware button)
{ type = SDL_MOUSEBUTTONDOWN, timestamp = 69142, windowID = 2, which = 0, button = 3, state = 1, clicks = 1, x = 613, y = 274 }
{ type = SDL_MOUSEBUTTONUP, timestamp = 69247, windowID = 2, which = 0, button = 3, state = 0, clicks = 1, x = 613, y = 274 }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment