Skip to content

Commit

Permalink
Fully working on Windows
Browse files Browse the repository at this point in the history
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).
  • Loading branch information
petabyt committed Feb 26, 2023
1 parent d67d492 commit b68f82a
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 33 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,5 @@ libusb*/
libusb*/*
gtk/*
*.res
*.zip
*.dll
31 changes: 15 additions & 16 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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/

Expand Down
83 changes: 68 additions & 15 deletions gtk.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <usb.h>
#include <gtk/gtk.h>
#include <ctype.h>
#include <pthread.h>

#include <camlib.h>
#include <ptp.h>
Expand All @@ -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
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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");
Expand All @@ -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");
Expand All @@ -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);
Expand All @@ -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)
Expand Down Expand Up @@ -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");
Expand Down
1 change: 0 additions & 1 deletion src/evproc.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <usb.h>
#include <stdint.h>

#include <camlib.h>
Expand Down
1 change: 0 additions & 1 deletion src/installer.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <usb.h>

#include "evproc.h"
#include "drive.h"
Expand Down

0 comments on commit b68f82a

Please sign in to comment.