Skip to content

Commit

Permalink
Phew, finally finished the port to camlib
Browse files Browse the repository at this point in the history
Only have tested it on Linux. Will get Windows working eventually.
  • Loading branch information
petabyt committed Feb 25, 2023
1 parent 7cb617f commit d67d492
Show file tree
Hide file tree
Showing 15 changed files with 102 additions and 7,469 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "camlib"]
path = camlib
url = https://github.com/petabyt/camlib
25 changes: 13 additions & 12 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,26 @@
# Note: Platform specific files will not be
# 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)

RM=rm -rf

all: unix-gtk

# flags for unix-gtk
unix-gtk: LDFLAGS=-lusb $(shell pkg-config --libs gtk+-3.0)
unix-gtk: CFLAGS=$(shell pkg-config --cflags gtk+-3.0)
unix-gtk: CFLAGS=$(shell pkg-config --cflags gtk+-3.0) -Icamlib/src -DVERBOSE

# Clean incompatible stuff, use between comiling
clean-out:
$(RM) -r src/*.o mlinstall unix-gtk unix-cli win-gtk win-cli *.o *.out *.exe *.res gtk libusb

# Clean everything
clean: clean-out
$(RM) -r *.zip *.AppImage unix-gtk unix-cli win64* win32* SD_BACKUP
$(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
Expand All @@ -40,9 +46,7 @@ gtk:
mv win32 gtk

libusb:
-wget -4 -nc https://cfhcable.dl.sourceforge.net/project/libusb-win32/libusb-win32-releases/1.2.2.0/libusb-win32-bin-1.2.2.0.zip -O ~/Downloads/libusb.zip
unzip ~/Downloads/libusb.zip
mv libusb-win32-bin-1.2.2.0 libusb
-wget -4 -nc https://github.com/petabyt/libwpd/releases/download/0.1.1/libwpd_x64.dll -O ~/Downloads/libwpd_x64.dll

# Contains app info, asset stuff
win.res: assets/win.rc
Expand All @@ -57,7 +61,8 @@ win64-gtk-mlinstall: CFLAGS=-s -lws2_32 -lkernel32 -lurlmon -Ilibusb/include -Ig
win64-gtk-mlinstall: GTK_ZIP=win64-gtk-2021.zip
win64-gtk-mlinstall: win.res gtk libusb gtk.o $(FILES) ../libwinusb/liblibusb.a
-mkdir win64-gtk-mlinstall
$(CC) win.res gtk.o $(FILES) gtk/lib/* ../libwinusb/liblibusb.a /usr/x86_64-w64-mingw32/lib/libsetupapi.a /usr/x86_64-w64-mingw32/lib/libwinusb.a \
$(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/
cp assets/README.txt win64-gtk-mlinstall/
Expand All @@ -70,9 +75,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/* libusb/bin/x86/libusb0_x86.dll \
$(CC) win.res gtk.o $(FILES) gtk/lib/* ~/Downloads/libwpd_x86.dll \
$(CFLAGS) -o win32-gtk-mlinstall/mlinstall.exe
cp libusb/bin/x86/libusb0_x86.dll win32-gtk-mlinstall/libusb0.dll
cp ~/Downloads/libwpd_x86.dll win32-gtk-mlinstall/libwpd.dll
cp gtk/lib/* win32-gtk-mlinstall/
cp assets/README.txt win32-gtk-mlinstall/

Expand All @@ -82,10 +87,6 @@ win32-gtk-mlinstall: win.res gtk libusb gtk.o $(FILES)

# Release targets:
win64-gtk-mlinstall.zip: win64-gtk-mlinstall
wget -4 -nc https://github.com/mcuee/libusb-win32/releases/download/snapshot_1.2.7.3/libusb-win32-bin-1.2.7.3.zip
unzip libusb-win32-bin-1.2.7.3.zip -d win64-gtk-mlinstall
mv win64-gtk-mlinstall/libusb-win32-bin-1.2.7.3/bin/amd64/ win64-gtk-mlinstall/install-libusb
$(RM) win64-gtk-mlinstall/libusb-win32-bin-1.2.7.3
zip -r win64-gtk-mlinstall.zip win64-gtk-mlinstall

win32-gtk-mlinstall.zip: win32-gtk-mlinstall
Expand Down
1 change: 1 addition & 0 deletions camlib
Submodule camlib added at ec63eb
102 changes: 51 additions & 51 deletions gtk.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@
#include <gtk/gtk.h>
#include <ctype.h>

#include "src/config.h"
#include "src/ptp.h"
#include "src/ptpcam.h"
#include <camlib.h>
#include <ptp.h>

#include "src/drive.h"
#include "src/model.h"
Expand All @@ -21,6 +20,7 @@
// Activated with CLI flag -d
int dev_flag = 0;

char *deviceNotFound = "Couldn't find a PTP/USB device.";
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 All @@ -45,30 +45,25 @@ void logclear()
gtk_label_set_text(GTK_LABEL(logw), logbuf);
}

// Log a return message after doing a usb thing
struct PtpRuntime ptp_runtime;

// Detect PTP return codes
int returnMessage(unsigned int code)
{
switch (code) {
case 0:
logprint("No PTP/USB device found. \n");
#ifdef WIN32
logprint(
"You're on Windows, you'll\nneed to install libusb.\nSee the MANUAL link in the README.\n");
#endif
return 1;
}

char buf[128];
snprintf(buf, sizeof(buf), "Response Code: %x\n", code);
logprint(buf);

switch (code) {
case 0x2001:
case PTP_RC_OK:
logprint("Return Code OK.\n");
return 0;
case 0x201d:
case PTP_RC_InvalidParameter:
logprint("Return Code INVALID_PARAMETER.\n");
return 1;
case PTP_RC_OperationNotSupported:
logprint("Operation not supported. Your camera is probably unsupported.");
return 1;
case 1:
logprint("Parser error. See console.");
return 1;
Expand Down Expand Up @@ -128,53 +123,62 @@ static void unscriptflag(GtkWidget *widget, gpointer data)
}
}

int ptpConnect() {
int r = ptp_device_init(&ptp_runtime);
if (r) {
logprint(deviceNotFound);
return r;
}

r = ptp_open_session(&ptp_runtime);
if (r) {
return r;
}

return r;
}

static void deviceinfo(GtkWidget *widget, gpointer data)
{
logclear();
if (ptpConnect()) return;

int busn = 0;
int devn = 0;
short force = 0;
PTPParams params;
PTP_USB ptp_usb;
struct usb_device *dev;

if (open_camera(busn, devn, force, &ptp_usb, &params, &dev) < 0) {
returnMessage(0);
return;
}
struct PtpDeviceInfo di;
ptp_get_device_info(&ptp_runtime, &di);

PTPDeviceInfo info;
ptp_getdeviceinfo(&params, &info);

char buffer[256];
char buffer[512];
snprintf(buffer, sizeof(buffer),
"Manufacturer: %s\n"
"Model: %s\n"
"DeviceVersion: %s\n"
"SerialNumber: %s\n",
info.Manufacturer, info.Model, info.DeviceVersion, info.SerialNumber);
di.manufacturer, di.model, di.device_version, di.serial_number);

logprint(buffer);

// Test model detector
printf("Model ID is %d\n", model_get(info.Model));
printf("Model ID is %d\n", model_get(di.model));

close_camera(&ptp_usb, &params, dev);
ptp_device_close(&ptp_runtime);
}

// Run a custom event proc from input
static void eventproc(GtkWidget *widget, gpointer data)
{
logclear();
if (ptpConnect()) return;

const gchar *entry = gtk_entry_get_text(GTK_ENTRY(widget));
returnMessage(evproc_run((char *)entry));
}

static void enablebootdisk(GtkWidget *widget, gpointer data)
{
logclear();
if (returnMessage(evproc_run("EnableBootDisk"))) {

if (ptpConnect()) return;

if (evproc_run("EnableBootDisk")) {
logprint("Couldn't enable boot disk.\n");
} else {
logprint("Enabled boot disk\n");
Expand All @@ -184,7 +188,10 @@ static void enablebootdisk(GtkWidget *widget, gpointer data)
static void disablebootdisk(GtkWidget *widget, gpointer data)
{
logclear();
if (returnMessage(evproc_run("DisableBootDisk"))) {

if (ptpConnect()) return;

if (evproc_run("DisableBootDisk")) {
logprint("Couldn't disable boot disk.\n");
} else {
logprint("Disabled boot disk\n");
Expand All @@ -206,24 +213,14 @@ static void oneclick(GtkWidget *widget, gpointer data)
{
logclear();

int busn = 0;
int devn = 0;
short force = 0;
PTPParams params;
PTP_USB ptp_usb;
struct usb_device *dev;
if (ptpConnect()) return;

if (open_camera(busn, devn, force, &ptp_usb, &params, &dev) < 0) {
returnMessage(0);
return;
}

PTPDeviceInfo info;
ptp_getdeviceinfo(&params, &info);
struct PtpDeviceInfo di;
ptp_get_device_info(&ptp_runtime, &di);

close_camera(&ptp_usb, &params, dev);
//close_camera(&ptp_usb, &params, dev);

switch (installer_start(info.Model, info.DeviceVersion)) {
switch (installer_start(di.model, di.device_version)) {
case NO_AVAILABLE_FIRMWARE:
logprint("Your camera model has a working build,\n"
"but not for your firmware version.");
Expand Down Expand Up @@ -389,6 +386,8 @@ int main(int argc, char *argv[])
g_print("https://github.com/petabyt/mlinstall\n");
g_print("https://www.magiclantern.fm/forum/index.php?topic=26162\n");

ptp_generic_init(&ptp_runtime);

window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_title(GTK_WINDOW(window), "MLinstall");
gtk_window_set_default_size(GTK_WINDOW(window), 375, 500);
Expand Down Expand Up @@ -534,3 +533,4 @@ int main(int argc, char *argv[])

return 0;
}
// What kind of idiot would write a UI in C. This is seriously stupid.
33 changes: 15 additions & 18 deletions src/evproc.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@
#include <usb.h>
#include <stdint.h>

#include "config.h"
#include "ptp.h"
#include "ptpcam.h"
#include <camlib.h>

extern struct PtpRuntime ptp_runtime;

int ptp_canon_activate_command(struct PtpRuntime *r);
int ptp_canon_exec_evproc(struct PtpRuntime *r, void *data, int length);

// TODO:
// Parse hex into int
Expand Down Expand Up @@ -242,22 +245,16 @@ int evproc_run(char string[])
memcpy(data + curr, &footer, sizeof(struct EvProcFooter));
curr += sizeof(struct EvProcFooter);

int busn = 0;
int devn = 0;
short force = 0;
PTPParams params;
PTP_USB ptp_usb;
struct usb_device *dev;

if (open_camera(busn, devn, force, &ptp_usb, &params, &dev) < 0) {
return 0;
// Command is disabled on some cams, run it nonetheless
int ret = ptp_canon_activate_command(&ptp_runtime);
if (ret) {
printf("Error activating command %d\n", ret);
ptp_device_close(&ptp_runtime);
return 1;
}

// Command is disabled on some cams
ptp_activate_command(&params);

unsigned int r = ptp_run_command(&params, data, curr, 0, 0);
ret = ptp_canon_exec_evproc(&ptp_runtime, data, curr);

close_camera(&ptp_usb, &params, dev);
return r;
ptp_device_close(&ptp_runtime);
return ret;
}
Loading

0 comments on commit d67d492

Please sign in to comment.