Skip to content

Commit

Permalink
Get X-H1 working
Browse files Browse the repository at this point in the history
- some wrong things in imager viewer setup
- Test suite works!
- Multithreading issues, race condition. Need to overhaul and remove all Java synchronized stuff, and rely only on camlib recursive mutex.
  • Loading branch information
petabyt committed Feb 2, 2024
1 parent 13108a8 commit 1f5efc5
Show file tree
Hide file tree
Showing 20 changed files with 162 additions and 203 deletions.
23 changes: 1 addition & 22 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -37,25 +37,4 @@ dependencies {
implementation 'com.google.android.material:material:1.5.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.3'
implementation 'androidx.recyclerview:recyclerview:1.1.0'
}

/*
// https://github.com/Dushistov/flapigen-rs/blob/master/android-example/app/build.gradle
def rustBasePath = "../rust/"
def archTriplets = [
'armeabi-v7a': 'armv7-linux-androideabi',
'arm64-v8a': 'aarch64-linux-android',
'x86': 'i686-linux-android',
'x86_64': 'x86_64-linux-android',
]
tasks.create(name: "rust", description: "Build all rust releases")
archTriplets.each { arch, target ->
tasks.create(name: "cargo-build-${arch}", type: Exec, description: "Running cargo for for ${arch}") {
workingDir rustBasePath
commandLine 'cargo', 'build', "--target=${target}", '--release'
}
rust.dependsOn "cargo-build-${arch}"
}
tasks.withType(JavaCompile) { compileTask -> compileTask.dependsOn "rust" }
*/
}
3 changes: 3 additions & 0 deletions app/src/main/assets/script.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
x = ui.NewWindow("Hello", 100, 100, false)
x:SetChild(ui.NewLabel("Hello from Lua"))
x:Show()
13 changes: 9 additions & 4 deletions app/src/main/java/camlib/WiFiComm.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
public class WiFiComm {
public static final String TAG = "camlib";

public boolean killSwitch = true;
//public boolean killSwitch = true;

static Network wifiDevice = null;

Expand Down Expand Up @@ -76,8 +76,8 @@ public void onUnavailable() {
connectivityManager.requestNetwork(requestBuilder.build(), networkCallback);
}

public static Socket connectWiFiSocket(ConnectivityManager connectivityManager, String ip, int port) throws Exception {
NetworkInfo wifiInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
public static Network getWiFiNetwork(ConnectivityManager cm) throws Exception {
NetworkInfo wifiInfo = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
if (!wifiInfo.isAvailable()) {
throw new Exception("WiFi is not available.");
} else if (!wifiInfo.isConnected()) {
Expand All @@ -88,6 +88,11 @@ public static Socket connectWiFiSocket(ConnectivityManager connectivityManager,
throw new Exception("Not connected to WiFi.");
}

return tryConnectToSocket(wifiDevice, ip, port);
return wifiDevice;
}

public static Socket connectWiFiSocket(ConnectivityManager cm, String ip, int port) throws Exception {
Network dev = getWiFiNetwork(cm);
return tryConnectToSocket(dev, ip, port);
}
}
2 changes: 1 addition & 1 deletion app/src/main/java/dev/danielc/fujiapp/ImageAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public void onBindViewHolder(ImageViewHolder holder, int position) {
@Override
public void run() {
int id = object_ids[adapterPosition];
byte[] jpegByteArray = Backend.cPtpGetThumb(id);
byte[] jpegByteArray = Backend._cPtpGetThumb(id);
if (jpegByteArray == null) {
Backend.reportError(Backend.PTP_IO_ERR, "Failed to get image thumbnail, stopping connection");
return;
Expand Down
10 changes: 5 additions & 5 deletions app/src/main/java/dev/danielc/fujiapp/LibU.java
Original file line number Diff line number Diff line change
Expand Up @@ -104,13 +104,13 @@ public static void storeJSONSettings(Activity ctx, String key, String value) thr
prefs.edit().putString(ctx.getPackageName() + "." + key, value).apply();
}

public static String readFileFromAssets(Context ctx, String file) throws Exception {
public static byte[] readFileFromAssets(Context ctx, String file) throws Exception {
try {
InputStream inputStream = ctx.getAssets().open(file);
Scanner scanner = new Scanner(inputStream).useDelimiter("\\A");
String fileContent = scanner.hasNext() ? scanner.next() : "";
scanner.close();
return fileContent;
byte buffer[] = new byte[inputStream.available()];
inputStream.read(buffer);
inputStream.close();
return buffer;
} catch (IOException e) {
e.printStackTrace();
throw e;
Expand Down
3 changes: 3 additions & 0 deletions app/src/main/java/dev/danielc/fujiapp/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ protected void onCreate(Bundle savedInstanceState) {
handler = new Handler(Looper.getMainLooper());

LibUI.buttonBackgroundResource = R.drawable.grey_button;
LibUI.popupDrawableResource = R.drawable.border;

Backend.init();
Backend.updateLog();
Expand Down Expand Up @@ -81,6 +82,8 @@ public void onClick(View v) {

// Idea: Show WiFi status on screen?
WiFiComm.startNetworkListeners(m);

//Backend.cFujiScriptsScreen(MainActivity.this);
}

public void connectClick(View v) {
Expand Down
39 changes: 10 additions & 29 deletions app/src/main/java/libui/LibUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,13 @@

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Color;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.util.DisplayMetrics;
import android.util.Log;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.LayoutInflater;
Expand All @@ -20,14 +17,10 @@
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.Space;
import android.widget.TextView;
import android.widget.Toast;

Expand All @@ -45,6 +38,7 @@
import com.google.android.material.tabs.TabLayout;

import java.util.ArrayList;
import android.graphics.drawable.ColorDrawable;

public class LibUI {
public static Context ctx = null;
Expand Down Expand Up @@ -386,23 +380,6 @@ public void setChild(View v) {
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT));

Button back = new Button(ctx);
back.setText("Close");
if (buttonBackgroundResource != 0) {
back.setBackground(ContextCompat.getDrawable(ctx, buttonBackgroundResource));
}

back.setTextSize(14f);

back.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
userSleep();
dismiss();
}
});

bar.addView(back);
TextView tv = new TextView(ctx);
tv.setText(title);
tv.setLayoutParams(new ViewGroup.LayoutParams(
Expand All @@ -422,9 +399,9 @@ public void onClick(View v) {
ViewGroup.LayoutParams.MATCH_PARENT));

TypedValue typedValue = new TypedValue();
if (ctx.getTheme().resolveAttribute(android.R.attr.windowBackground, typedValue, true)) {
rel.setBackgroundColor(typedValue.data);
}
// if (ctx.getTheme().resolveAttribute(android.R.attr.windowBackground, typedValue, true)) {
// rel.setBackgroundColor(typedValue.data);
// }

rel.addView(bar);

Expand Down Expand Up @@ -452,10 +429,14 @@ public void onClick(View v) {

this.popupWindow = new PopupWindow(
(int)(width / 1.2),
(int)(height / 1.2)
(int)(height / 1.9)
);

this.popupWindow.setOutsideTouchable(false);
// if (popupDrawableResource != 0) {
// this.popupWindow.setBackgroundDrawable(new ColorDrawable(Color.WHITE));
// }

this.popupWindow.setOutsideTouchable(true);
}
}

Expand Down
14 changes: 5 additions & 9 deletions lib/Android.mk
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,22 @@ LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)

CAMLIB_CORE := $(addprefix camlib/src/,transport.c bind.c data.c enum_dump.c enums.c canon.c liveview.c no_usb.c operations.c packet.c lib.c ml.c conv.c generic.c)
CAMLIB_CORE += camlib/lua/lua-cjson/strbuf.c camlib/lua/lua-cjson/lua_cjson.c camlib/lua/lua.c camlib/lua/runtime.c

FUDGE_CORE := main.c jni.c lib.c fuji.c tester.c models.c net.c viewer.c
FUDGE_CORE := main.c jni.c fuji.c tester.c models.c net.c viewer.c
FUDGE_CORE += scripts.c camlib.c

LUA_CORE := $(addprefix lua/,lbaselib.c lauxlib.c lapi.c lcode.c lctype.c ldebug.c ldo.c ldump.c lfunc.c lgc.c llex.c lmem.c lobject.c lopcodes.c lparser.c lstate.c lstring.c ltable.c ltm.c lundump.c lvm.c lzio.c ltests.c)

LOCAL_MODULE := fujiapp
LOCAL_CFLAGS := -Wall -D ANDROID
LOCAL_SRC_FILES := $(FUDGE_CORE) $(CAMLIB_CORE) $(LUA_CORE)
LOCAL_C_INCLUDES += $(LOCAL_PATH) $(LOCAL_PATH)/camlib/src
LOCAL_C_INCLUDES += $(LOCAL_PATH) $(LOCAL_PATH)/camlib/src $(LOCAL_PATH)/camlib/lua $(LOCAL_PATH)/lua
LOCAL_LDLIBS += -llog

LIBUIFW := libuifw
#LIBUIFW := ../../libuifw

LOCAL_SRC_FILES += $(LIBUIFW)/libui.c
LOCAL_SRC_FILES += $(LIBUIFW)/libui.c $(LIBUIFW)/lib.c $(LIBUIFW)/lua.c
LOCAL_C_INCLUDES += $(LOCAL_PATH)/$(LIBUIFW)

#LOCAL_SHARED_LIBRARIES += rust

include $(BUILD_SHARED_LIBRARY)

#include $(LOCAL_PATH)/Rust.mk
include $(BUILD_SHARED_LIBRARY)
2 changes: 1 addition & 1 deletion lib/camlib
2 changes: 2 additions & 0 deletions lib/camlib.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ PTP_FUNC(jbyteArray, cPtpGetThumb)(JNIEnv *env, jobject thiz, jint handle) {
set_jni_env(env);
struct PtpRuntime *r = ptp_get();

__android_log_write(ANDROID_LOG_ERROR, "camlib", "Trying to get thumbnail");

int rc = ptp_get_thumbnail(r, (int)handle);
if (rc == PTP_CHECK_CODE) {
__android_log_write(ANDROID_LOG_ERROR, "camlib", "Thumbnail get failed");
Expand Down
18 changes: 12 additions & 6 deletions lib/fuji.c
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,8 @@ int fuji_config_init_mode(struct PtpRuntime *r) {
fuji_known.remote_image_view_version = ptp_parse_prop_value(r);
ptp_verbose_log("RemoteGetObjectVersion: 0x%X", fuji_known.remote_image_view_version);

// TODO: set PTP_PC_FUJI_RemoteGetObjectVersion

rc = ptp_get_prop_value(r, PTP_PC_FUJI_ImageGetVersion);
if (rc) return rc;
fuji_known.image_get_version = ptp_parse_prop_value(r);
Expand Down Expand Up @@ -176,6 +178,10 @@ int fuji_config_init_mode(struct PtpRuntime *r) {

ptp_verbose_log("Setting mode to %d", mode);

// On newer cams, setting function mode causes cam to have a dialog (Yes/No accept connection)
// We have to wait for a response in this case
r->wait_for_response = 255;

rc = ptp_set_prop_value16(r, PTP_PC_FUJI_FunctionMode, mode);
if (rc) return rc;

Expand Down Expand Up @@ -258,21 +264,21 @@ int fuji_config_image_viewer(struct PtpRuntime *r) {
rc = fuji_get_events(r);
if (rc) return rc;

//ptp_verbose_log("PTP_PC_FUJI_RemoteGetObjectVersion: %d\n", fuji_known.remote_image_view_version);

rc = ptp_set_prop_value(r, PTP_PC_FUJI_RemoteGetObjectVersion, fuji_known.remote_image_view_version);
rc = ptp_get_prop_value(r, PTP_PC_FUJI_RemoteGetObjectVersion);
fuji_known.remote_image_view_version = ptp_parse_prop_value(r);
if (rc) return rc;

// SD card slot?
// Check SD card slot, not really useful for now
rc = ptp_get_prop_value(r, PTP_PC_FUJI_StorageID);
if (rc) return rc;
ptp_verbose_log("Storage ID: %d\n", ptp_parse_prop_value(r));

// Now we finally enter the remote image viewer
rc = ptp_set_prop_value16(r, PTP_PC_FUJI_FunctionMode, FUJI_MODE_REMOTE_IMG_VIEW);
if (rc) return rc;

// Set the prop again! For no reason! beause fuji devs say so
rc = ptp_set_prop_value(r, PTP_PC_FUJI_RemoteGetObjectVersion, fuji_known.remote_image_view_version);
// Set the prop higher - X-S10 and X-H1 want 4
rc = ptp_set_prop_value(r, PTP_PC_FUJI_RemoteGetObjectVersion, 4);
if (rc) return rc;

// The props we set should show up here
Expand Down
1 change: 1 addition & 0 deletions lib/jni.c
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ JNI_FUNC(void, cNotifyScreenStart)(JNIEnv *env, jobject thiz, jstring string) {
}
#endif

// Return array of valid objects on main storage device
JNI_FUNC(jintArray, cGetObjectHandles)(JNIEnv *env, jobject thiz) {
backend.env = env;

Expand Down
64 changes: 49 additions & 15 deletions lib/lib.c
Original file line number Diff line number Diff line change
@@ -1,36 +1,70 @@
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <string.h>
#include <jni.h>
#include <android/log.h>

#error "no"
#define LIBU(ret, name) JNIEXPORT ret JNICALL Java_libui_LibU_##name

int libu_write_file(JNIEnv *env, char *path, void *data, size_t length) {
jclass class = (*env)->FindClass(env, "libui/LibU");
jclass class = (*env)->FindClass(env, "libui/LibU");

jmethodID method = (*env)->GetStaticMethodID(env, class, "writeFile", "(Ljava/lang/String;[B)V");
jmethodID method = (*env)->GetStaticMethodID(env, class, "writeFile", "(Ljava/lang/String;[B)V");

jbyteArray jdata = (*env)->NewByteArray(env, length);
(*env)->SetByteArrayRegion(
env, jdata,
0, length, (const jbyte *)(data)
);
(*env)->SetByteArrayRegion(
env, jdata,
0, length, (const jbyte *)(data)
);

jstring jpath = (*env)->NewStringUTF(env, path);
jstring jpath = (*env)->NewStringUTF(env, path);

(*env)->CallStaticVoidMethod(env, class, method,
jpath, jdata
);
jpath, jdata
);

(*env)->DeleteLocalRef(env, jdata);
(*env)->DeleteLocalRef(env, jpath);

return 0;
}

void *libu_get_assets_file(JNIEnv *env, jobject ctx, char *filename, int *length) {
jclass class = (*env)->FindClass(env, "libui/LibU");

jmethodID method = (*env)->GetStaticMethodID(env, class, "readFileFromAssets", "(Landroid/content/Context;Ljava/lang/String;)[B");

jstring jfile = (*env)->NewStringUTF(env, filename);

(*env)->DeleteLocalRef(env, jdata);
(*env)->DeleteLocalRef(env, jpath);
jbyteArray array = (*env)->CallStaticObjectMethod(env, class, method,
ctx, jfile
);

jbyte *bytes = (*env)->GetByteArrayElements(env, array, 0);

(*length) = (*env)->GetArrayLength(env, array);

void *new = malloc(*length);
memcpy(new, bytes, *length);

(*env)->DeleteLocalRef(env, jfile);
(*env)->ReleaseByteArrayElements(env, array, bytes, 0);

return new;
}

return 0;
void *libu_get_txt_file(JNIEnv *env, jobject ctx, char *filename) {
int length = 0;
char *bytes = libu_get_assets_file(env, ctx, filename, &length);
length += 1;
bytes = realloc(bytes, length);
bytes[length] = '\0';
return bytes;
}

// Added in POSIX 2008, not C standard
char *stpcpy(char *dst, const char *src) {
const size_t len = strlen(src);
return (char *)memcpy (dst, src, len + 1) + len;
const size_t len = strlen(src);
return (char *)memcpy (dst, src, len + 1) + len;
}
2 changes: 1 addition & 1 deletion lib/libuifw
Loading

0 comments on commit 1f5efc5

Please sign in to comment.