aboutsummaryrefslogtreecommitdiff
path: root/extra/js1.c
blob: 703332b2d75de281456936fb3e898cb75295b73c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
// File: js1.c
// Startdate: 2022-06-29
// Purpose: Test my C skills, while trying to test SDL2 game controller stuff
// References:
// http://www.libsdl.org/release/SDL-1.2.15/docs/html/guideinput.html
// https://wiki.libsdl.org/SDL_GameControllerMapping
// https://stackoverflow.com/q/29589982
// https://generalarcade.com/gamepadtool/
// https://wiki.winehq.org/Useful_Registry_Keys
// https://wiki.archlinux.org/title/Gamepad
// https://stackoverflow.com/questions/29589982/does-sdl-on-linux-support-more-than-one-gamepad-joystick
// Dependencies:
//    libsdl2-dev
// Documentation:
// try SDL_GAMECONTROLLERCONFIG env var.

#include "SDL2/SDL.h"
SDL_Joystick *joy;
SDL_GameController *ctrl;
char *guid[33];
char *mapping;
const char *newmapping = "03000000bd12000015d0000010010000,Tomee SNES USB Controller,platform:Linux,a:b2,b:b1,x:b3,y:b0,back:b8,start:b9,leftshoulder:b4,rightshoulder:b5,dpup:-a1,dpdown:+a1,dpleft:-a0,dpright:+a0,";

int main () {
   if (SDL_Init( SDL_INIT_VIDEO | SDL_INIT_JOYSTICK ) < 0)
   {
      fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
      exit(1);
   }
   SDL_InitSubSystem(SDL_INIT_JOYSTICK);
   SDL_InitSubSystem(SDL_INIT_GAMECONTROLLER);
   printf("%i joysticks were found.\n\n", SDL_NumJoysticks() );
   printf("The names of the joysticks are:\n");

   for( int i=0; i < SDL_NumJoysticks(); i++ ) 
   {
      joy = SDL_JoystickOpen(i);
		if (joy) {
			printf("Opened joystick #%d \"%s\"\n", i, SDL_JoystickNameForIndex(i));
			printf("Axes count: %d\n", SDL_JoystickNumAxes(joy));
			printf("Button count: %d\n", SDL_JoystickNumButtons(joy));
			printf("Ball count: %d\n", SDL_JoystickNumBalls(joy));
			SDL_JoystickGetGUIDString(SDL_JoystickGetGUID(joy),*guid,sizeof(guid));
			printf("GUID string: \"%s\"\n",guid);
			if (SDL_IsGameController(i)) {
				ctrl = SDL_GameControllerOpen(i);
				mapping = SDL_GameControllerMapping(ctrl);
				printf("Mapping: \"%s\"\n", mapping);
				SDL_free(mapping);
				printf("Trying new mapping.\n");
				int r = SDL_GameControllerAddMapping(newmapping);
				printf("Result: %d.\n", r);
			}
		}
   }
   return 0;
}
bgstack15