Skip to content

Commit

Permalink
Functional desktop app connecting/disconnecting
Browse files Browse the repository at this point in the history
  • Loading branch information
petabyt committed Nov 17, 2024
1 parent 2353c58 commit e1dc428
Show file tree
Hide file tree
Showing 22 changed files with 911 additions and 1,016 deletions.
16 changes: 9 additions & 7 deletions desktop/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ include cross.mk
FUDGE = ../lib

CAMLIB_CORE := transport.o operations.o packet.o enums.o data.o enum_dump.o lib.o canon.o liveview.o bind.o ml.o conv.o generic.o canon_adv.o
OBJ := main.o ui.o jank.o wifi.o backend.o
FUDGE_CORE := fuji.o tester.o data.o net.o discovery.o exif.o uilua.o fuji_usb.o object.o
OBJ := main.o ui.o jank.o os.o backend.o
FUDGE_CORE := fuji.o tester.o data.o net.o discovery.o exif.o uilua.o fuji_usb.o object.o fuji_lua.o lua_runtime.o

ifeq ($(TARGET),l)
CAMLIB_CORE += libusb.o
Expand All @@ -16,7 +16,7 @@ OBJ += win.o
endif

OBJ += $(addprefix camlib-,$(CAMLIB_CORE))
OBJ += $(addprefix camlua-,lua.o runtime.o)
OBJ += $(addprefix camlua-,lua.o)
OBJ += $(addprefix camluajson-,lua_cjson.o strbuf.o)
OBJ += $(addprefix fudge-,$(FUDGE_CORE))
OBJ := $(call convert_target,$(OBJ))
Expand All @@ -28,18 +28,20 @@ CFLAGS := -I$(FUDGE) -I$(CAMLIB_DIR)/src -I$(CAMLIB_DIR)/src/lua -g
# -fsanitize=address -static-libasan

ifeq ($(TARGET),w) # -----------------------
WIN_LIBS += $(LIBWPD_A) $(LIBLUA_A) $(LIBUI_DLL)
WIN_LIBS += $(LIBWPD_A) $(LIBLUA_A) $(LIBUI_A)
WIN_LIBS += $(WIN_LINK_ESSENTIALS) -lstdc++ -lgcc -lpthread

fudge.exe: $(OBJ) win.res
$(CC) $(OBJ) $(CFLAGS) $(WIN_LIBS) $(LDFLAGS) win.res -o fudge.exe
endif # --------------------

ifeq ($(TARGET),l) # ----------------------
CFLAGS += $(shell pkg-config --libs --cflags lua-5.3 libusb-1.0)
LDFLAGS += -lm -lui
CFLAGS += $(shell pkg-config --cflags lua-5.3 libusb-1.0)
LDFLAGS += -lm -lui $(shell pkg-config --libs lua-5.3)
LDFLAGS += $(shell pkg-config --libs libusb-1.0)
#LDFLAGS += -lusb-vcam
fudge.out: $(OBJ)
$(CC) $(OBJ) $(CFLAGS) $(LDFLAGS) -o fudge.out
$(CC) $(OBJ) $(LDFLAGS) -o fudge.out
endif # ----------------------------

# Manual targets to compile in this dir
Expand Down
32 changes: 32 additions & 0 deletions desktop/assets/libui.manifest
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
<assemblyIdentity
version="1.0.0.0"
processorArchitecture="*"
name="CompanyName.ProductName.YourApplication"
type="win32"
/>
<description>Your application description here.</description>
<!-- we DO need comctl6 in the static case -->
<dependency>
<dependentAssembly>
<assemblyIdentity
type="win32"
name="Microsoft.Windows.Common-Controls"
version="6.0.0.0"
processorArchitecture="*"
publicKeyToken="6595b64144ccf1df"
language="*"
/>
</dependentAssembly>
</dependency>
<compatibility xmlns="urn:schemas-microsoft-com:compatibility.v1">
<application>
<!--The ID below indicates application support for Windows Vista -->
<supportedOS Id="{e2011457-1546-43c5-a5fe-008deee3d3f0}"/>
<!--The ID below indicates application support for Windows 7 -->
<supportedOS Id="{35138b9a-5d96-4fbd-8e2d-a2440225f93a}"/>
</application>
</compatibility>
</assembly>

148 changes: 144 additions & 4 deletions desktop/backend.c
Original file line number Diff line number Diff line change
@@ -1,11 +1,151 @@
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <camlib.h>
#include <fuji.h>
#include <app.h>
#include <fujiptp.h>
#include <lua.h>
#include <camlua.h>
#include <pthread.h>
#include "desktop.h"

void app_get_file_path(char buffer[256], const char *filename) {
snprintf(buffer, 256, "%s", filename);
static struct PtpRuntime *ptp = NULL;

// TODO: System to manage a list of cameras connected
struct PtpRuntime *ptp_get() {
return ptp;
}

struct PtpRuntime *luaptp_get_runtime(lua_State *L) {
return ptp_get();
}

int luaopen_libuilua(lua_State *L);
int cam_lua_setup(lua_State *L) {
luaL_requiref(L, "ui", luaopen_libuilua, 1);
return 0;
}

void app_get_tether_file_path(char buffer[256]) {
snprintf(buffer, 256, "TETHER.JPG");
int fudge_usb_connect(struct PtpRuntime *r) {
static int attempts = 0;
app_log_clear();
int rc = fujiusb_try_connect(r);
if (rc) {
const char *msg = "No Fujifilm device found";
if (attempts) {
app_print("%s (%d)", msg, attempts);
} else {
app_print("%s", msg);
}
attempts++;
return rc;
}
attempts = 0;

rc = ptp_open_session(r);
if (rc != PTP_CHECK_CODE && rc) return rc;

ptp = r;

return 0;
}

void fudge_disconnect_all(void) {
struct PtpRuntime *r = ptp_get();
if (r == NULL) {
// 0 cameras connected
return;
}
printf("Disconnecting\n");
ptp_report_error(r, "Intentional", 0);
ptp = NULL;
}

int fudge_main_app_thread(struct PtpRuntime *r) {
struct PtpDeviceInfo di;
int rc = ptp_get_device_info(r, &di);
if (rc) return rc;

app_print("Connected to %s %s", di.manufacturer, di.model);
app_send_cam_name(di.model);

struct PtpArray *arr;
rc = ptp_fuji_get_object_handles(r, &arr);
app_print("%d files on card", arr->length);

while (r->io_kill_switch == 0) {
if (ptpusb_get_status(r)) {
app_print("USB cable disconnected.");
break;
}
usleep(100000);
}

return 0;
}

void *fudge_backup_settings(void *arg) {
return NULL;
}

void *fudge_usb_connect_thread(void *arg) {
struct PtpRuntime *r = ptp_new(PTP_USB);
int rc = fudge_usb_connect(r);
if (rc) goto exit;

app_update_connected_status(1);
rc = fudge_main_app_thread(r);
app_update_connected_status(0);

ptp = NULL;

exit:;
ptp_close(r);
pthread_exit(NULL);
return NULL;
}

int fudge_run_lua(struct PtpRuntime *r, const char *text) {
cam_run_lua_script_async(text);
}

static char *read_file(const char *filename) {
FILE* file = fopen(filename, "rb");
if (!file) return NULL;

fseek(file, 0, SEEK_END);
long file_size = ftell(file);
fseek(file, 0, SEEK_SET);

char* buffer = (char*)malloc(file_size + 1);
if (!buffer) return NULL;

fread(buffer, 1, file_size, file);
buffer[file_size] = '\0';

fclose(file);
return buffer;
}

int fuji_connect_run_script(const char *filename) {
const char *text = read_file(filename);
if (text == NULL) {
printf("%s not found\n", filename);
return -1;
}

struct PtpRuntime *r = ptp_new(PTP_USB);
int rc = fudge_usb_connect(r);
if (rc) return rc;

rc = cam_run_lua_script_async(text);
if (rc) {
printf("%s\n", cam_lua_get_error());
printf("Lua err %d\n", rc);
}
return rc;
// leak r
}
17 changes: 11 additions & 6 deletions desktop/ci.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,34 +32,39 @@ static int discovery_test() {
return 0;
}

void nothing(int x) {}

pid_t child_pid = -1;

int fudge_test_all_cameras_(const char *name) {
signal(SIGUSR1, nothing);
char thispid[16];
sprintf(thispid, "%d", getpid());
const char *ip_addr = "0.0.0.0";

child_pid = fork();
if (child_pid == -1) {
perror("fork failed");
exit(1);
}

if (child_pid == 0) {
int rc = execlp("/usr/bin/vcam", "vcam", name, "--ip", "0.0.0.0", NULL);
int rc = execlp("/usr/bin/vcam", "vcam", name, "--ip", ip_addr, "--sig", thispid, NULL);
printf("Return value: %d\n", rc);
exit(0);
}

printf("Crappy wait for vcam\n");
usleep(10000);
printf("Done waiting on vcam\n");
printf("Waiting for vcam...\n");
pause();

int rc = 0;
char *ip_addr = "0.0.0.0";

struct PtpRuntime *r = ptp_new(PTP_IP_USB);
fuji_reset_ptp(r);
fuji_get(r)->transport = FUJI_FEATURE_WIRELESS_COMM;
if (ptpip_connect(r, ip_addr, FUJI_CMD_IP_PORT, 0)) {
printf("Error connecting to %s:%d\n", ip_addr, FUJI_CMD_IP_PORT);
return 0;
return -1;
}

rc = fuji_test_setup(r);
Expand Down
8 changes: 4 additions & 4 deletions desktop/cross.mk
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ MINGW := x86_64-w64-mingw32
CC := $(MINGW)-gcc
CPP := $(MINGW)-c++

LIBWPD_A := /usr/x86_64-w64-mingw32/lib/libwpd.a
LIBLUA_A := /usr/x86_64-w64-mingw32/lib/liblua.a
LIBUI_DLL := /usr/x86_64-w64-mingw32/lib/libui_win64.dll
LIBUI_A := /usr/x86_64-w64-mingw32/lib/libui.a
LIBWPD_A := /usr/$(ARCH)-w64-mingw32/lib/libwpd.a
LIBLUA_A := /usr/$(ARCH)-w64-mingw32/lib/liblua.a
LIBUI_DLL := /usr/$(ARCH)-w64-mingw32/lib/libui_win64.dll
LIBUI_A := /usr/$(ARCH)-w64-mingw32/lib/libui.a

WIN_LINK_ESSENTIALS += -luser32 -lkernel32 -lgdi32 -lcomctl32 -luxtheme -lmsimg32 -lcomdlg32 -ld2d1 -ldwrite -lole32 -loleaut32 -loleacc -lssp -lurlmon -luuid -lws2_32

Expand Down
13 changes: 12 additions & 1 deletion desktop/desktop.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,20 @@ void network_init(void);

int fudge_main_ui(void);

int fuji_test_discovery(struct PtpRuntime *r);
void app_log_clear(void);
void app_update_connected_status(int connected);
void fudge_disconnect_all(void);

void *fudge_usb_connect_thread(void *arg);

void *fudge_backup_settings(void *arg);

int fuji_connect_run_script(const char *filename);

// Tests
int fuji_test_discovery(struct PtpRuntime *r);
int fuji_test_filesystem(struct PtpRuntime *r);
int fuji_test_setup(struct PtpRuntime *r);


#endif
79 changes: 79 additions & 0 deletions desktop/file_table.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
#include <stdlib.h>
#include <ui.h>
#include <object.h>
#include <object.h>
#include <app.h>

struct MyTableModelHandler {
uiTableModelHandler mh;
struct PtpRuntime *r;
struct PtpObjectCache *oc;
};

static inline struct MyTableModelHandler *my_handler(uiTableModelHandler *mh) {
return (struct MyTableModelHandler *)mh;
}

static int modelNumColumns(uiTableModelHandler *mh, uiTableModel *m) {
return 3;
}

static uiTableValueType modelColumnType(uiTableModelHandler *mh, uiTableModel *m, int column)
{
switch (column) {
case 0:
return uiTableValueTypeString;
case 1:
return uiTableValueTypeString;
default:
return uiTableValueTypeString;
}
}

static int modelNumRows(uiTableModelHandler *mh, uiTableModel *m) {
return ptp_object_service_length(
my_handler(mh)->r,
my_handler(mh)->oc
);
}

static uiTableValue *modelCellValue(uiTableModelHandler *mh, uiTableModel *m, int row, int column)
{
struct MyTableModelHandler *h = my_handler(mh);

if (column == 0) {
struct PtpObjectInfo *oi = ptp_object_service_get_index(h->r, h->oc, row);
return uiNewTableValueString(oi->filename);
}

return uiNewTableValueString("TODO");
}

static void modelSetCellValue(uiTableModelHandler *mh, uiTableModel *m, int row, int column, const uiTableValue *val) {
// This list is not editable
}

uiControl *create_files_tab(struct PtpRuntime *r, struct PtpObjectCache *oc) {
struct MyTableModelHandler *mh = (struct MyTableModelHandler *)malloc(sizeof(struct MyTableModelHandler));
mh->mh.NumColumns = modelNumColumns;
mh->mh.ColumnType = modelColumnType;
mh->mh.NumRows = modelNumRows;
mh->mh.CellValue = modelCellValue;
mh->mh.SetCellValue = modelSetCellValue;
mh->r = r;
mh->oc = oc;

uiTableModel *m = uiNewTableModel((uiTableModelHandler *)mh);

uiTableParams p = {
.Model = m,
.RowBackgroundColorModelColumn = 3,
};

uiTable *t = uiNewTable(&p);

uiTableAppendTextColumn(t, "Filename", 0, uiTableModelColumnNeverEditable, NULL);
uiTableAppendTextColumn(t, "Last Modified", 1, uiTableModelColumnNeverEditable, NULL);

return uiControl(t);
}
Loading

0 comments on commit e1dc428

Please sign in to comment.