Skip to content

Commit e9ec1e7

Browse files
alemartSiegeLord
authored andcommitted
Android: implement al_get_joystick_name()
1 parent 1c51be8 commit e9ec1e7

File tree

2 files changed

+27
-4
lines changed

2 files changed

+27
-4
lines changed

android/gradle_project/allegro/src/main/java/org/liballeg/android/AllegroActivity.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -575,6 +575,18 @@ public int indexOfJoystick(int id) {
575575
return joysticks.indexOf(id, 0);
576576
}
577577

578+
public String getJoystickName(int index) {
579+
if (index >= 0 && index < joysticks.size()) {
580+
int id = joysticks.get(index);
581+
InputDevice input = InputDevice.getDevice(id);
582+
583+
if (input != null)
584+
return input.getName();
585+
}
586+
587+
return "";
588+
}
589+
578590
public void setJoystickActive() {
579591
joystickActive = true;
580592
}

src/android/android_joystick.c

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,25 +9,37 @@ ALLEGRO_DEBUG_CHANNEL("android")
99
typedef struct ALLEGRO_JOYSTICK_ANDROID {
1010
ALLEGRO_JOYSTICK parent;
1111
ALLEGRO_JOYSTICK_STATE joystate;
12-
const char *name;
12+
char name[64];
1313
} ALLEGRO_JOYSTICK_ANDROID;
1414

1515
static _AL_VECTOR joysticks = _AL_VECTOR_INITIALIZER(ALLEGRO_JOYSTICK_ANDROID *);
1616
static bool initialized;
1717

18+
static char *android_get_joystick_name(JNIEnv *env, jobject activity, int index, char *buffer, size_t buffer_size)
19+
{
20+
jobject str_obj = _jni_callObjectMethodV(env, activity, "getJoystickName", "(I)Ljava/lang/String;", (jint)index);
21+
ALLEGRO_USTR *s = _jni_getString(env, str_obj);
22+
_al_sane_strncpy(buffer, al_cstr(s), buffer_size);
23+
al_ustr_free(s);
24+
25+
return buffer;
26+
}
27+
1828
static void android_init_joysticks(int num)
1929
{
2030
int i, j;
31+
JNIEnv *env = _al_android_get_jnienv();
32+
jobject activity = _al_android_activity_object();
2133

2234
for (i = 0; i < num; i++) {
2335
ALLEGRO_JOYSTICK_ANDROID *stick = al_calloc(1, sizeof(ALLEGRO_JOYSTICK_ANDROID));
2436
ALLEGRO_JOYSTICK_ANDROID **ptr;
2537
ALLEGRO_JOYSTICK *joy;
2638

2739
joy = (void *)stick;
28-
stick->name = "Android Joystick";
2940

3041
/* Fill in the joystick information fields. */
42+
android_get_joystick_name(env, activity, i, stick->name, sizeof(stick->name));
3143
joy->info.num_sticks = 2;
3244
joy->info.num_buttons = 11;
3345
joy->info.stick[0].name = "Stick 1";
@@ -57,11 +69,10 @@ static bool andjoy_init_joystick(void)
5769
ALLEGRO_JOYSTICK *joy;
5870
int num;
5971

60-
accel->name = "Accelerometer";
61-
6272
joy = (void *)accel;
6373

6474
/* Fill in the joystick information fields. */
75+
_al_sane_strncpy(accel->name, "Accelerometer", sizeof(accel->name));
6576
joy->info.num_sticks = 1;
6677
joy->info.num_buttons = 0;
6778
joy->info.stick[0].name = "Accelerometer";

0 commit comments

Comments
 (0)