Skip to content

Commit

Permalink
Cleanup code
Browse files Browse the repository at this point in the history
  • Loading branch information
v3rm0n committed Dec 13, 2022
1 parent 7ed6741 commit f1bdc58
Show file tree
Hide file tree
Showing 8 changed files with 144 additions and 65 deletions.
2 changes: 1 addition & 1 deletion app/jni/src/audio.c
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,6 @@ Java_io_maido_m8client_M8SDLActivity_setFileDescriptor(JNIEnv *env, jobject thiz
}

JNIEXPORT void JNICALL
Java_io_maido_m8client_M8SDLActivity_sendClickEvent(JNIEnv *env, jobject thiz, jchar event) {
Java_io_maido_m8client_M8TouchListener_sendClickEvent(JNIEnv *env, jobject thiz, jchar event) {
send_msg_controller(event);
}
60 changes: 60 additions & 0 deletions app/src/main/assets/config.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
; edit this file to change m8c defaults
; this file is re-written every time it is read,
; so do not expect comments or commented out values to survive!
; valid parameter changes will be written back and persisted though.

[graphics]
; set this to true to have m8c start fullscreen
fullscreen=true
; set this to false to run m8c in software rendering mode (may be useful for Raspberry Pi)
use_gpu=true
; the delay amount in ms in the main loop, decrease value for faster operation, increase value if too much cpu usage
idle_ms = 10
; show a spinning cube if device is not inserted
wait_for_device = true
; number of zero-byte attempts to disconnect if wait_for_device = false (128 = about 2 sec for default idle_ms)
wait_packets = 128

[keyboard]
; these need to be the decimal value of the SDL scancodes.
; a table exists here: https://github.com/libsdl-org/sdlwiki/blob/main/SDLScancodeLookup.mediawiki
key_up=82
key_left=80
key_down=81
key_right=79
key_select=225
key_select_alt=4
key_start=44
key_start_alt=22
key_opt=226
key_opt_alt=29
key_edit=224
key_edit_alt=27
key_delete=76
key_reset=21

[gamepad]
; these need to be the decimal value of the SDL Controller buttons.
; a table exists here: https://wiki.libsdl.org/SDL_GameControllerButton
gamepad_up=11
gamepad_left=13
gamepad_down=12
gamepad_right=14
gamepad_select=4
gamepad_start=6
gamepad_opt=1
gamepad_edit=0
gamepad_quit=8
gamepad_reset=7

gamepad_analog_threshold=32766 ;the threshold for analog sticks to trigger cursor movement (working values: 1-32766)
gamepad_analog_invert=false ;NOT IMPLEMENTED YET: invert up/down and left/right axis (true/false)

; these need to be the decimal value of the controller axis
; you can use -1 if you do not wish to map the function to an analog axis
gamepad_analog_axis_updown=1
gamepad_analog_axis_leftright=0
gamepad_analog_axis_start=5
gamepad_analog_axis_select=4
gamepad_analog_axis_opt=-1
gamepad_analog_axis_edit=-1
11 changes: 6 additions & 5 deletions app/src/main/java/io/maido/m8client/M8ClientActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public void onReceive(Context context, Intent intent) {

UsbManager usbManager = (UsbManager) getSystemService(Context.USB_SERVICE);
if (intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false)) {
if (device != null && M8Device.isM8(device)) {
if (device != null && M8Util.isM8(device)) {
connectToM8(usbManager, device);
} else {
Log.d(TAG, "Device was not M8");
Expand All @@ -51,7 +51,7 @@ public void onReceive(Context context, Intent intent) {

private void stopM8SDLActivity(Intent intent) {
UsbDevice device = intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
if (device != null && M8Device.isM8(device)) {
if (device != null && M8Util.isM8(device)) {
Log.i(TAG, "Device disconnected");
Intent finishActivity = new Intent(M8SDLActivity.FINISH);
sendBroadcast(finishActivity);
Expand All @@ -63,9 +63,10 @@ private void stopM8SDLActivity(Intent intent) {

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d(TAG, "onCreate");
registerReceiver(usbReceiver, new IntentFilter(UsbManager.ACTION_USB_DEVICE_DETACHED));
M8Device.copyGameControllerDB(this);
M8Util.copyGameControllerDB(this);
UsbManager usbManager = (UsbManager) getSystemService(Context.USB_SERVICE);
UsbDevice usbDevice = getIntent().getParcelableExtra(UsbManager.EXTRA_DEVICE);
// Activity was launched by attaching the USB device so permissions are implicitly granted
Expand All @@ -74,7 +75,7 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
connectToM8(usbManager, usbDevice);
}
searchForM8();
super.onCreate(savedInstanceState);
setContentView(R.layout.nodevice);
}

@Override
Expand Down Expand Up @@ -105,7 +106,7 @@ private void searchForM8() {
UsbManager usbManager = (UsbManager) getSystemService(Context.USB_SERVICE);
HashMap<String, UsbDevice> deviceList = usbManager.getDeviceList();
for (UsbDevice device : deviceList.values()) {
if (M8Device.isM8(device)) {
if (M8Util.isM8(device)) {
connectToM8WithPermission(usbManager, device);
break;
}
Expand Down
66 changes: 8 additions & 58 deletions app/src/main/java/io/maido/m8client/M8SDLActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,12 @@
import android.content.Intent;
import android.content.IntentFilter;
import android.content.res.Configuration;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.FrameLayout;

import org.libsdl.app.SDLActivity;

import java.util.HashSet;
import java.util.Set;

public class M8SDLActivity extends SDLActivity {
public static String FINISH = M8SDLActivity.class.getSimpleName() + ".FINISH";
public static String FILE_DESCRIPTOR = M8SDLActivity.class.getSimpleName() + ".FILE_DESCRIPTOR";
Expand Down Expand Up @@ -56,26 +51,12 @@ protected void onStart() {
super.onStart();
}

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}

native public void sendClickEvent(char event);

@Override
protected void onStop() {
unregisterReceiver(finishReceiver);
super.onStop();
}

@Override
protected String[] getLibraries() {
return new String[]{
"main"
};
}

@Override
public void setContentView(View view) {
FrameLayout mainLayout = new FrameLayout(this);
Expand All @@ -100,53 +81,22 @@ public void onConfigurationChanged(Configuration newConfig) {

private void addListeners() {
View up = findViewById(R.id.up);
up.setOnTouchListener(new ButtonTouchListener(M8Keys.UP));
up.setOnTouchListener(new M8TouchListener(M8Keys.UP));
View down = findViewById(R.id.down);
down.setOnTouchListener(new ButtonTouchListener(M8Keys.DOWN));
down.setOnTouchListener(new M8TouchListener(M8Keys.DOWN));
View left = findViewById(R.id.left);
left.setOnTouchListener(new ButtonTouchListener(M8Keys.LEFT));
left.setOnTouchListener(new M8TouchListener(M8Keys.LEFT));
View right = findViewById(R.id.right);
right.setOnTouchListener(new ButtonTouchListener(M8Keys.RIGHT));
right.setOnTouchListener(new M8TouchListener(M8Keys.RIGHT));

View play = findViewById(R.id.play);
play.setOnTouchListener(new ButtonTouchListener(M8Keys.PLAY));
play.setOnTouchListener(new M8TouchListener(M8Keys.PLAY));
View shift = findViewById(R.id.shift);
shift.setOnTouchListener(new ButtonTouchListener(M8Keys.SHIFT));
shift.setOnTouchListener(new M8TouchListener(M8Keys.SHIFT));
View option = findViewById(R.id.option);
option.setOnTouchListener(new ButtonTouchListener(M8Keys.OPTION));
option.setOnTouchListener(new M8TouchListener(M8Keys.OPTION));
View edit = findViewById(R.id.edit);
edit.setOnTouchListener(new ButtonTouchListener(M8Keys.EDIT));
}

private static final Set<M8Keys> modifiers = new HashSet<>();

class ButtonTouchListener implements View.OnTouchListener {

private final M8Keys key;

ButtonTouchListener(M8Keys key) {
this.key = key;
}

@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
int action = motionEvent.getActionMasked();
switch (action) {
case MotionEvent.ACTION_DOWN:
modifiers.add(key);
char code = key.getCode(modifiers);
Log.d(TAG, "Sending " + key + " as " + code);
sendClickEvent(code);
break;
case MotionEvent.ACTION_UP:
modifiers.remove(key);
Log.d(TAG, "Key up " + key);
sendClickEvent((char) 0);
view.performClick();
break;
}
return true;
}
edit.setOnTouchListener(new M8TouchListener(M8Keys.EDIT));
}

public native void setFileDescriptor(int fileDescriptor);
Expand Down
44 changes: 44 additions & 0 deletions app/src/main/java/io/maido/m8client/M8TouchListener.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package io.maido.m8client;

import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

import java.util.HashSet;
import java.util.Set;


class M8TouchListener implements View.OnTouchListener {

private static final String TAG = M8TouchListener.class.getSimpleName();

private static final Set<M8Keys> modifiers = new HashSet<>();

private final M8Keys key;

public M8TouchListener(M8Keys key) {
this.key = key;
}

@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
int action = motionEvent.getActionMasked();
switch (action) {
case MotionEvent.ACTION_DOWN:
modifiers.add(key);
char code = key.getCode(modifiers);
Log.d(TAG, "Sending " + key + " as " + code);
sendClickEvent(code);
break;
case MotionEvent.ACTION_UP:
modifiers.remove(key);
Log.d(TAG, "Key up " + key);
sendClickEvent((char) 0);
view.performClick();
break;
}
return true;
}

native public void sendClickEvent(char event);
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,14 @@
import java.io.InputStream;
import java.io.OutputStream;

public class M8Device {
public class M8Util {

public static boolean isM8(UsbDevice usbDevice) {
return usbDevice.getProductName().equals("M8");
}

public static void copyGameControllerDB(Context context) {
copyFile(context, "config.ini", "/data/data/io.maido.m8client/files/config.ini");
copyFile(context, "gamecontrollerdb.txt", "/data/data/io.maido.m8client/files/gamecontrollerdb.txt");
}

Expand Down
22 changes: 22 additions & 0 deletions app/src/main/res/layout/nodevice.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">

<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/connect_your_m8_device"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

</FrameLayout>
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@
<string name="edit">Edit</string>
<string name="play">Play</string>
<string name="shift">Shift</string>
<string name="connect_your_m8_device">Connect your M8 device!</string>
</resources>

0 comments on commit f1bdc58

Please sign in to comment.