From b68f82a63d4ade1035fba6b1f7b949a9a5ca5dc8 Mon Sep 17 00:00:00 2001 From: Daniel Cook Date: Sat, 25 Feb 2023 17:17:44 -0800 Subject: [PATCH] Fully working on Windows WPD has to have it's own thread, so it can coinitialize. Had to get all camlib operations working on a new thread. Thankfully this is easy since mingw has pthread. Still need to get it to recognize only cameras (it'll recognize phones). --- .gitignore | 2 ++ Makefile | 31 +++++++++--------- gtk.c | 83 ++++++++++++++++++++++++++++++++++++++++--------- src/evproc.c | 1 - src/installer.c | 1 - 5 files changed, 85 insertions(+), 33 deletions(-) diff --git a/.gitignore b/.gitignore index fe0dc79..1d14c86 100644 --- a/.gitignore +++ b/.gitignore @@ -10,3 +10,5 @@ libusb*/ libusb*/* gtk/* *.res +*.zip +*.dll diff --git a/Makefile b/Makefile index de39379..7f404fe 100644 --- a/Makefile +++ b/Makefile @@ -5,9 +5,9 @@ # compiled because of "#ifdef WIN32" guards FILES=$(patsubst %.c,%.o,$(wildcard src/*.c)) -CL_OBJ=libusb.o operations.o packet.o enums.o data.o enum_dump.o util.o canon.o backend.o liveview.o bind.o -CL_OBJ2=$(addprefix camlib/src/,$(CL_OBJ)) -FILES+=$(CL_OBJ2) +CL_OBJ_=operations.o packet.o enums.o data.o enum_dump.o util.o canon.o liveview.o bind.o +UNIX_FILES=$(FILES) $(addprefix camlib/src/,$(CL_OBJ_) libusb.o backend.o) +WIN_FILES=$(FILES) $(addprefix camlib/src/,$(CL_OBJ_) winapi.o) RM=rm -rf @@ -25,8 +25,8 @@ clean-out: clean: clean-out $(RM) -r *.zip *.AppImage unix-gtk unix-cli win64* win32* SD_BACKUP camlib/src/*.o -unix-gtk: $(FILES) gtk.o - $(CC) gtk.o $(FILES) $(CFLAGS) $(LDFLAGS) -o unix-gtk +unix-gtk: $(UNIX_FILES) gtk.o + $(CC) gtk.o $(UNIX_FILES) $(CFLAGS) $(LDFLAGS) -o unix-gtk style: clang-format -style=file -i src/*.c src/*.h gtk.c @@ -38,15 +38,15 @@ style: # Download GTK libs, GTK_ZIP is sent from parent target # See https://github.com/petabyt/windows-gtk for more info on this gtk: - -wget -4 -nc https://github.com/petabyt/windows-gtk/raw/master/$(GTK_ZIP) -O ~/Downloads/$(GTK_ZIP) + -wget -4 -nc https://github.com/petabyt/windows-gtk/raw/master/$(GTK_ZIP) -O $(GTK_ZIP) mkdir gtk - unzip ~/Downloads/$(GTK_ZIP) -d gtk + unzip $(GTK_ZIP) -d gtk mv gtk/win32 . rm -rf gtk mv win32 gtk libusb: - -wget -4 -nc https://github.com/petabyt/libwpd/releases/download/0.1.1/libwpd_x64.dll -O ~/Downloads/libwpd_x64.dll + -wget -4 -nc https://github.com/petabyt/libwpd/releases/download/0.1.1/libwpd_x64.dll -O libwpd_x64.dll # Contains app info, asset stuff win.res: assets/win.rc @@ -57,14 +57,13 @@ win.res: assets/win.rc win-gtk: win64-gtk-mlinstall win64-gtk-mlinstall: MINGW=x86_64-w64-mingw32 win64-gtk-mlinstall: CC=$(MINGW)-gcc -win64-gtk-mlinstall: CFLAGS=-s -lws2_32 -lkernel32 -lurlmon -Ilibusb/include -Igtk/include +win64-gtk-mlinstall: CFLAGS=-s -lws2_32 -lkernel32 -lurlmon -Icamlib/src -Igtk/include win64-gtk-mlinstall: GTK_ZIP=win64-gtk-2021.zip -win64-gtk-mlinstall: win.res gtk libusb gtk.o $(FILES) ../libwinusb/liblibusb.a +win64-gtk-mlinstall: win.res gtk libusb gtk.o $(WIN_FILES) -mkdir win64-gtk-mlinstall - $(CC) win.res gtk.o $(FILES) gtk/lib/* ~/Downloads/libwpd_x64.dll \ - cp ~/Downloads/libwpd_x64.dll win32-gtk-mlinstall/libwpd.dll - $(CFLAGS) -o win64-gtk-mlinstall/mlinstall.exe - cd gtk/lib/; cp * ../../win64-gtk-mlinstall/ + $(CC) win.res gtk.o $(WIN_FILES) gtk/lib/* libwpd_x64.dll $(CFLAGS) -o win64-gtk-mlinstall/mlinstall.exe + cp libwpd_x64.dll win64-gtk-mlinstall/libwpd.dll + cd gtk/lib/ && cp * ../../win64-gtk-mlinstall/ cp assets/README.txt win64-gtk-mlinstall/ # 32 bit Windows XP, ReactOS @@ -75,9 +74,9 @@ win32-gtk-mlinstall: CFLAGS=-s -lws2_32 -lkernel32 -lurlmon -Ilibusb/include -Ig win32-gtk-mlinstall: GTK_ZIP=win32-gtk-2013.zip win32-gtk-mlinstall: win.res gtk libusb gtk.o $(FILES) -mkdir win32-gtk-mlinstall - $(CC) win.res gtk.o $(FILES) gtk/lib/* ~/Downloads/libwpd_x86.dll \ + $(CC) win.res gtk.o $(FILES) gtk/lib/* libwpd_x86.dll \ $(CFLAGS) -o win32-gtk-mlinstall/mlinstall.exe - cp ~/Downloads/libwpd_x86.dll win32-gtk-mlinstall/libwpd.dll + cp libwpd_x86.dll win32-gtk-mlinstall/libwpd.dll cp gtk/lib/* win32-gtk-mlinstall/ cp assets/README.txt win32-gtk-mlinstall/ diff --git a/gtk.c b/gtk.c index b471e01..cbd40f2 100644 --- a/gtk.c +++ b/gtk.c @@ -3,9 +3,9 @@ #include #include #include -#include #include #include +#include #include #include @@ -20,7 +20,7 @@ // Activated with CLI flag -d int dev_flag = 0; -char *deviceNotFound = "Couldn't find a PTP/USB device."; +char *deviceNotFound = "Couldn't find a PTP/USB device.\n"; char *driveNotFound = "Couldn't find card. Make sure\nthe EOS_DIGITAL card is mounted."; char *driveNotSupported = "Only ExFAT, FAT32, and FAT16\ncards are supported."; #ifdef WIN32 @@ -123,7 +123,7 @@ static void unscriptflag(GtkWidget *widget, gpointer data) } } -int ptpConnect() { +int ptp_connect_init() { int r = ptp_device_init(&ptp_runtime); if (r) { logprint(deviceNotFound); @@ -138,10 +138,9 @@ int ptpConnect() { return r; } -static void deviceinfo(GtkWidget *widget, gpointer data) -{ +void *deviceinfo_thread(void *arg) { logclear(); - if (ptpConnect()) return; + if (ptp_connect_init()) return (void *)1; struct PtpDeviceInfo di; ptp_get_device_info(&ptp_runtime, &di); @@ -160,25 +159,64 @@ static void deviceinfo(GtkWidget *widget, gpointer data) printf("Model ID is %d\n", model_get(di.model)); ptp_device_close(&ptp_runtime); + return (void *)0; +} + +static void deviceinfo(GtkWidget *widget, gpointer data) { + pthread_t thread; + + if (pthread_create(&thread, NULL, deviceinfo_thread, NULL)) { + return; + } + + if (pthread_join(thread, NULL)) { + return; + } +} + +void *eventproc_thread(void *arg) { + logclear(); + if (ptp_connect_init()) return (void*)1; + + uintptr_t r = (uintptr_t)evproc_run((char *)arg); + return (void *)r; } // Run a custom event proc from input static void eventproc(GtkWidget *widget, gpointer data) { logclear(); - if (ptpConnect()) return; + pthread_t thread; const gchar *entry = gtk_entry_get_text(GTK_ENTRY(widget)); - returnMessage(evproc_run((char *)entry)); + + if (pthread_create(&thread, NULL, eventproc_thread, (void *)entry)) { + return; + } + + int result; + if (pthread_join(thread, (void **)&result)) { + return; + } + + returnMessage(result); } static void enablebootdisk(GtkWidget *widget, gpointer data) { logclear(); + pthread_t thread; - if (ptpConnect()) return; + if (pthread_create(&thread, NULL, eventproc_thread, (void *)"EnableBootDisk")) { + return; + } - if (evproc_run("EnableBootDisk")) { + int result; + if (pthread_join(thread, (void **)&result)) { + return; + } + + if (result) { logprint("Couldn't enable boot disk.\n"); } else { logprint("Enabled boot disk\n"); @@ -188,10 +226,18 @@ static void enablebootdisk(GtkWidget *widget, gpointer data) static void disablebootdisk(GtkWidget *widget, gpointer data) { logclear(); + pthread_t thread; + + if (pthread_create(&thread, NULL, eventproc_thread, (void *)"DisableBootDisk")) { + return; + } - if (ptpConnect()) return; + int result; + if (pthread_join(thread, (void **)&result)) { + return; + } - if (evproc_run("DisableBootDisk")) { + if (result) { logprint("Couldn't disable boot disk.\n"); } else { logprint("Disabled boot disk\n"); @@ -209,11 +255,11 @@ static void showdrive(GtkWidget *widget, gpointer data) } } -static void oneclick(GtkWidget *widget, gpointer data) -{ +void *oneclick_thread(void *arg) { + int result = 0; logclear(); - if (ptpConnect()) return; + if (ptp_connect_init()) pthread_exit(&result); struct PtpDeviceInfo di; ptp_get_device_info(&ptp_runtime, &di); @@ -230,6 +276,12 @@ static void oneclick(GtkWidget *widget, gpointer data) "Come back in 5 years and check again."); break; } + + pthread_exit(&result); +} + +static void oneclick(GtkWidget *widget, gpointer data) +{ } static int downloadmodule(GtkWidget *widget, gpointer data) @@ -387,6 +439,7 @@ int main(int argc, char *argv[]) g_print("https://www.magiclantern.fm/forum/index.php?topic=26162\n"); ptp_generic_init(&ptp_runtime); + //ptp_backend_init(&ptp_runtime); window = gtk_window_new(GTK_WINDOW_TOPLEVEL); gtk_window_set_title(GTK_WINDOW(window), "MLinstall"); diff --git a/src/evproc.c b/src/evproc.c index e5e7c4b..b1367f1 100644 --- a/src/evproc.c +++ b/src/evproc.c @@ -1,7 +1,6 @@ #include #include #include -#include #include #include diff --git a/src/installer.c b/src/installer.c index 11d83a2..2404f72 100644 --- a/src/installer.c +++ b/src/installer.c @@ -1,7 +1,6 @@ #include #include #include -#include #include "evproc.h" #include "drive.h"