Skip to content

Instantly share code, notes, and snippets.

@DanielGibson
Last active April 22, 2024 18:13
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DanielGibson/12041b57a540cbbfbaf9 to your computer and use it in GitHub Desktop.
Save DanielGibson/12041b57a540cbbfbaf9 to your computer and use it in GitHub Desktop.
test SDL2 input (mouse and keyboard events)
/*
* SDL2 mousebutton test
*
* build with:
* $ gcc $(sdl2-config --cflags) -o sdl2test sdl2test.c $(sdl2-config --libs)
*/
#include <stdio.h>
#include <SDL.h>
#include <errno.h>
void eventloop(FILE* outfile) {
while(1) {
SDL_Event ev;
if(SDL_PollEvent(&ev) == 0) {
//printf(".\n");
SDL_Delay(1);
continue;
}
switch(ev.type) {
case SDL_QUIT:
fprintf(outfile, "Received SDL_QUIT - bye!\n");
return;
case SDL_MOUSEBUTTONUP:
fprintf(outfile, "SDL_MOUSEBUTTONUP, button %d clicks %d\n", ev.button.button, (int)ev.button.clicks);
break;
case SDL_MOUSEBUTTONDOWN:
fprintf(outfile, "SDL_MOUSEBUTTONDOWN, button %d clicks %d\n", ev.button.button, (int)ev.button.clicks);
break;
case SDL_KEYUP:
case SDL_KEYDOWN:
if(ev.type == SDL_KEYUP)
fprintf(outfile, "SDL_KEYUP: ");
else
fprintf(outfile, "SDL_KEYDOWN: ");
fprintf(outfile, "Keycode: %s (%d) Scancode: %s (%d)\n",
SDL_GetKeyName(ev.key.keysym.sym), ev.key.keysym.sym,
SDL_GetScancodeName(ev.key.keysym.scancode),
ev.key.keysym.scancode);
if(ev.key.keysym.sym == SDLK_q) {
fprintf(outfile, "You pressed Q - bye!\n");
return;
}
break;
case SDL_MOUSEWHEEL:
fprintf(outfile, "MouseWheel: x: %d, y: %d\n", ev.wheel.x, ev.wheel.y);
break;
case SDL_TEXTINPUT:
fprintf(outfile, "SDL_TEXTINPUT: %s\n", ev.text.text ? ev.text.text : "<NULL>");
break;
case SDL_JOYBUTTONDOWN:
fprintf(outfile, "SDL_JOYBUTTONDOWN dev %d button %d state %d\n",
(int)ev.jbutton.which, (int)ev.jbutton.button, (int)ev.jbutton.state);
break;
case SDL_JOYBUTTONUP:
fprintf(outfile, "SDL_JOYBUTTONUP dev %d button %d state %d\n",
(int)ev.jbutton.which, (int)ev.jbutton.button, (int)ev.jbutton.state);
break;
case SDL_MOUSEMOTION:
case SDL_FINGERDOWN:
case SDL_FINGERUP:
case SDL_FINGERMOTION:
break; // ignore
case SDL_WINDOWEVENT:
break;
default:
// fprintf(outfile, "SDL_Event of type: 0x%x received\n", ev.type);
break;
}
}
}
int main(int argc, char** argv) {
//SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK);
SDL_Init(SDL_INIT_EVERYTHING);
//SDL_StartTextInput();
SDL_Joystick* gGameController = NULL;
SDL_Window* win = SDL_CreateWindow("Hello World!", 100, 100, 640, 480, SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE);
//SDL_SetRelativeMouseMode(1);
//SDL_SetWindowGrab(win, 1);
SDL_SetWindowTitle(win, "SDL input test");
// fill the window with black, so it shows something deterministic
// (and works with wayland)
{
SDL_Surface *s = SDL_GetWindowSurface(win);
Uint32 black = SDL_MapRGB(s->format, 0, 0, 0);
SDL_FillRect(s, NULL, black);
SDL_UpdateWindowSurface(win);
}
if( SDL_NumJoysticks() < 1 )
{
printf( "Warning: No joysticks connected!\n" );
}
else
{
//Load joystick
gGameController = SDL_JoystickOpen( 0 );
if( gGameController == NULL )
{
printf( "Warning: Unable to open game controller! SDL Error: %s\n", SDL_GetError() );
}
}
#ifdef _WIN32
// output debug prints to file on windows
const char* filename = "sdl2_test_out.txt";
#else
// output debug prints to stdout on other OS
const char* filename = "-";
#endif
FILE* output = NULL;
if(argc > 1) {
filename = argv[1];
}
if(strcmp(filename, "-") == 0)
output = stdout;
else
output = fopen(filename, "w");
if(!output) {
fprintf(stderr, "Couldn't create debug output file!\n");
exit(1);
}
eventloop(output);
if(strcmp(filename, "-") != 0)
fclose(output);
SDL_JoystickClose( gGameController );
gGameController = NULL;
SDL_DestroyWindow(win);
SDL_Quit();
return 0;
}
@mirabilos
Copy link

Fixed compile command (yours doesn’t work):

gcc -O2 $(sdl2-config --cflags) -o sdl2test sdl2test.c $(sdl2-config --libs)

Order matters.

Other than that, it should be noted that the window looks like it crashed, but things work well. Thanks, this helped me!

@DanielGibson
Copy link
Author

DanielGibson commented Sep 30, 2019

Yeah, I'm not drawing anything so the window can contain garbage.
I changed it according to your suggestion.

Danke, mira!
- caedes

@mirabilos
Copy link

mirabilos commented Sep 30, 2019 via email

@ahornby
Copy link

ahornby commented Jan 15, 2024

to get the window to display on linux wayland I needed to add a renderer.

    SDL_Window* win = SDL_CreateWindow("Hello World!", 
		SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 640, 480, SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE);
	
    SDL_Renderer *ren = SDL_CreateRenderer(win, -1, 0);

    SDL_SetRenderDrawColor(ren, 0, 0, 0, 255);
    SDL_RenderFillRect(ren, NULL);
    SDL_RenderPresent(ren);

@DanielGibson
Copy link
Author

to get the window to display on linux wayland I needed to add a renderer.

@ahornby: weird, that shouldn't be necessary? Though maybe I should draw something into the window.
Does it also work to add:

	{
		SDL_Surface *s = SDL_GetWindowSurface(win);
		Uint32 black = SDL_MapRGB(s->format, 0, 0, 0);
		SDL_FillRect(s, NULL, black);
		SDL_UpdateWindowSurface(win);
	}

to main(), after SDL_SetWindowTitle(win, "SDL input test"); ?

@ahornby
Copy link

ahornby commented Jan 15, 2024

@DanielGibson yes, that also works. Must need something displayed for the window. BTW the window title the snippet is asdf

@DanielGibson
Copy link
Author

DanielGibson commented Jan 15, 2024

ok, updated the gist - thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment