Skip to content

Instantly share code, notes, and snippets.

@sylt
Created October 12, 2014 12:42
Show Gist options
  • Save sylt/4ff5909d9cdd2b15c0b9 to your computer and use it in GitHub Desktop.
Save sylt/4ff5909d9cdd2b15c0b9 to your computer and use it in GitHub Desktop.
Program for generating joystick configuration for Visual Boy Advance 1.8.0 on Linux
// Program to generate joystick configuration for Visual Boy Advance
// 1.8.0 on Linux. I know there existed another program before to do
// this, but I could not find it so I wrote this.
//
// It has worked for me, but it might not work for you. Compile with:
// gcc -o vba-joy vba-joy.c -Wall -Wextra $(pkg-config --libs sdl) -std=c99
//
// Usage: ./vba-joy [joystickIndex]
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <SDL/SDL.h>
static const char* ButtonNames[] = {
"Left", "Right", "Up", "Down", "A", "B", "L", "R", "Start", "Select", "Speed"
};
#define BUTTON_COUNT (sizeof(ButtonNames) / sizeof(ButtonNames[0]))
static int ButtonValues[BUTTON_COUNT];
int main(int argc, char* argv[])
{
int joystickIndex = 0;
if (argc == 2 && isdigit(*argv[1])) {
joystickIndex = atoi(argv[1]);
}
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK) < 0) {
fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
return 1;
}
atexit(SDL_Quit);
const int joystickCount = SDL_NumJoysticks();
if (joystickCount < 0) {
fprintf(stderr, "Got error when probing for joysticks: %s\n",
SDL_GetError());
return 1;
}
else if (joystickCount == 0) {
fprintf(stderr, "SDL did not find any joysticks\n");
return 1;
}
printf("Available joysticks (%d):\n", joystickCount);
for(int i = 0; i < joystickCount; i++) {
printf(" Joystick %d: %s\n", i, SDL_JoystickName(i));
}
if (joystickIndex >= joystickCount) {
fprintf(stderr, "Can't configure non-existing joystick %d\n",
joystickIndex);
return 1;
}
printf("\n");
SDL_JoystickEventState(SDL_ENABLE);
SDL_Joystick* joystick = SDL_JoystickOpen(joystickIndex);
if (joystick == NULL) {
fprintf(stderr, "Could not open joystick %d: %s\n",
joystickIndex, SDL_GetError());
return 1;
}
printf("Starting configuration of joystick %d:\n", joystickIndex);
for (size_t button = 0; button != BUTTON_COUNT; ++button) {
printf(" Press \"%s\" button.\n", ButtonNames[button]);
for (SDL_Event event; SDL_WaitEvent(&event);) {
int what = 0; // Same name as in Visual Boy Advance source code.
switch(event.type) {
case SDL_QUIT:
goto cleanup_and_exit;
case SDL_JOYBUTTONDOWN:
what = event.jbutton.button + 128;
break;
case SDL_JOYAXISMOTION:
if (event.jaxis.value == 0) {
continue; // Filter away release of axis
}
what = (event.jaxis.axis << 1) | (event.jaxis.value > 16384);
break;
default:
continue;
}
what = ((joystickIndex + 1) << 12) + what;
ButtonValues[button] = what;
break; // Next button
}
}
printf("\nResulting button configuration for VisualBoyAdvance.cfg: \n\n");
for (size_t button = 0; button != BUTTON_COUNT; ++button) {
printf("Joy%d_%s=%04x\n", joystickIndex,
ButtonNames[button], ButtonValues[button]);
}
cleanup_and_exit:
SDL_JoystickClose(joystick);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment