-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhypjsch_cli.cpp
119 lines (102 loc) · 4.2 KB
/
hypjsch_cli.cpp
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/*
* ____ HYPSEUS COPYRIGHT NOTICE ____
*
* Copyright (C) 2022 DirtBagXon
*
* This file is part of HYPSEUS SINGE, a laserdisc arcade game emulator
*
* HYPSEUS SINGE is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* HYPSEUS SINGE is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* g++ hypjsch_cli.cpp -lSDL2 -o hypjsch_cli
*
*/
#include <SDL2/SDL.h>
#include <cinttypes>
#include "keycodes.h"
int main(int argc, char *argv[]) {
if (SDL_Init(SDL_INIT_JOYSTICK) != 0) {
SDL_Log("Unable to initialize SDL: %s", SDL_GetError());
exit(1);
}
SDL_Joystick *joystick;
const char ega[28] = "KEY_[AXIS] = SDLK_[DIR] 0 0";
const char egb[25] = "KEY_[ACT] = SDLK_[KEY] 0";
const char e[45] = "(Third column provides template config only)";
int n = 0x0a, t = 0x09, len = 0x31;
char j[50] = {0};
int index = 0;
if (SDL_NumJoysticks() == 0) {
fprintf(stderr, "No joysticks found%c", n);
SDL_Quit();
exit(1);
} else {
fprintf(stdout, "%c%d joystick(s) found - ", n, SDL_NumJoysticks());
fprintf(stdout, "%s%c%c", e, n, n);
}
SDL_Event event;
while (1) {
SDL_WaitEvent(&event);
if (event.type == SDL_QUIT) {
break;
}
for (int i=0; i < SDL_NumJoysticks(); i++) {
if (SDL_NumJoysticks() == 0) break;
joystick = SDL_JoystickOpen(i);
if (joystick == NULL) break;
index = event.jaxis.which * 0x64;
strncpy (j, SDL_JoystickNameForIndex(i), len);
j[len] = '\0';
if (event.type == SDL_JOYAXISMOTION && event.jaxis.which == i) {
if (abs(event.jaxis.value) > JITTER) {
if (event.jaxis.axis > 0x63) { // 3 digit input codes
index = index * 0x64;
fprintf (stdout, "%s:%c", j, t);
fprintf (stdout, "Axis: %04i%c- ", event.jaxis.axis + 1 + index, t);
if (event.jaxis.value < 0) fprintf (stdout, "%s -%04i%c", ega, event.jaxis.axis + 1 + index, n);
else fprintf (stdout, "%s +%04i%c", ega, event.jaxis.axis + 1 + index, n);
}
else
{
fprintf (stdout, "%s:%c", j, t);
fprintf (stdout, "Axis: %03i%c- ", event.jaxis.axis + 1 + index, t);
if (event.jaxis.value < 0) fprintf (stdout, "%s -%03i%c", ega, event.jaxis.axis + 1 + index, n);
else fprintf (stdout, "%s +%03i%c", ega, event.jaxis.axis + 1 + index, n);
}
}
} else if (event.type == SDL_JOYHATMOTION && event.jhat.hat == i) {
fprintf (stdout, "%s:%c", j, t);
fprintf (stdout, "HAT - Set KEY_UP Button to: %03d%c", event.jhat.which * 100, n);
} else if (event.type == SDL_JOYBUTTONDOWN && event.jbutton.which == i) {
if (event.jbutton.button > 0x63) { // 3 digit input codes
index = index * 0x64;
fprintf (stdout, "%s:%c", j, t);
fprintf (stdout, "Button: %04i%c- ", event.jbutton.button + 1 + index, t);
fprintf (stdout, "%s %04i%c", egb, event.jbutton.button + 1 + index, n);
}
else
{
fprintf (stdout, "%s:%c", j, t);
fprintf (stdout, "Button: %03i%c- ", event.jbutton.button + 1 + index, t);
fprintf (stdout, "%s %03i%c", egb, event.jbutton.button + 1 + index, n);
}
}
}
}
fprintf(stdout, "%c%s", n, e);
fprintf(stdout, " - %s%c%c", VERSION, n, n);
SDL_JoystickClose(joystick);
SDL_Quit();
return 0;
}