From 8bd0027e71f3c61a80886fec6dfbd5abfc0389fb Mon Sep 17 00:00:00 2001 From: Cory McWilliams Date: Sun, 15 Oct 2023 16:55:25 +0000 Subject: [PATCH] Enough glue to load a web page from our web server in the iOS simulator. Next challenge is uv_spawn: permission denied. git-svn-id: https://www.unprompted.com/svn/projects/tildefriends/trunk@4519 ed5197a5-7fde-0310-b194-c3ffbd925b24 --- Makefile | 17 +++++++++++++++++ core/httpd.js | 2 +- src/ios.m | 17 ++++++++++++++++- src/ios/Info.plist | 8 ++++---- src/log.h | 3 +++ src/main.c | 38 +++++++++++++++++++++++++++++++++++--- 6 files changed, 76 insertions(+), 9 deletions(-) diff --git a/Makefile b/Makefile index 1304fec6..7e80122e 100644 --- a/Makefile +++ b/Makefile @@ -643,6 +643,23 @@ apkgo: out/TildeFriends-release.apk @adb shell am start com.unprompted.tildefriends/.MainActivity .PHONY: apkgo +# iOS Support +out/%.app/Info.plist: src/ios/Info.plist + mkdir -p $(dir $@) + cp -v $< $@ + +out/%.app/data.zip: $(RAW_FILES) + @zip -u $@ -q -9 -x '*.map' -r $(PACKAGE_DIRS) $(RAW_FILES) + +out/tildefriends-iossimdebug.app/tildefriends: out/iossimdebug/tildefriends out/tildefriends-iossimdebug.app/Info.plist out/tildefriends-iossimdebug.app/data.zip + mkdir -p $(dir $@) + cp out/iossimdebug/tildefriends $@ + +iossimdebuggo: out/tildefriends-iossimdebug.app/tildefriends + xcrun simctl install booted out/tildefriends-iossimdebug.app/ + xcrun simctl launch booted com.unprompted.tildefriends +.PHONEY: iossimdebuggo + apklog: @adb logcat *:S tildefriends .PHONY: apklog diff --git a/core/httpd.js b/core/httpd.js index a6a25065..8e1e42cc 100644 --- a/core/httpd.js +++ b/core/httpd.js @@ -445,7 +445,7 @@ function handleConnection(client) { if (result == -2) { /* More. */ } else { - badRequest(client, 'Bad request.'); + badRequest(client, `Bad request(parse=${result}, length=${inputBuffer.length - data.length}).`); return; } } else if (typeof result === 'object') { diff --git a/src/ios.m b/src/ios.m index 65d0442f..56bf6fd0 100644 --- a/src/ios.m +++ b/src/ios.m @@ -1,7 +1,14 @@ +#include "log.h" + #import #import #import +#include +#include + +void tf_run_thread_start(const char* zip_path); + @interface ViewController : UIViewController @property(strong, nonatomic) WKWebView* web_view; @end @@ -12,7 +19,7 @@ [super viewDidLoad]; WKWebViewConfiguration* configuration = [[WKWebViewConfiguration alloc] init]; self.web_view = [[WKWebView alloc] initWithFrame:self.view.frame configuration:configuration]; - [self.web_view loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"https://www.tildefriends.net/"]]]; + [self.web_view loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://localhost:12345/"]]]; [self.view addSubview:self.web_view]; } @end @@ -35,5 +42,13 @@ int main(int argc, char* argv[]) { + NSFileManager* file_manager = [NSFileManager defaultManager]; + NSString* library_directory = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) objectAtIndex:0]; + [file_manager changeCurrentDirectoryPath:library_directory]; + size_t path_length = strlen(argv[0]) - strlen(basename(argv[0])); + size_t length = path_length + strlen("data.zip"); + char* zip_path = alloca(length + 1); + snprintf(zip_path, length + 1, "%.*sdata.zip", (int)path_length, argv[0]); + tf_run_thread_start(zip_path); return UIApplicationMain(argc, argv, nil, @"AppDelegate"); } diff --git a/src/ios/Info.plist b/src/ios/Info.plist index 297e0cf3..4ce68737 100644 --- a/src/ios/Info.plist +++ b/src/ios/Info.plist @@ -3,21 +3,21 @@ CFBundleName - cory + tildefriends CFBundleSupportedPlatforms iPhoneOS CFBundleExecutable - cory + tildefriends CFBundleIdentifier - cory + com.unprompted.tildefriends CFBundleResourceSpecification ResourceRules.plist LSRequiresIPhoneOS CFBundleDisplayName - cory + Tilde Friends CFBundleVersion 1.0 CFBundleShortVersionString diff --git a/src/log.h b/src/log.h index 0c88ed65..2226d11f 100644 --- a/src/log.h +++ b/src/log.h @@ -3,6 +3,9 @@ #if defined(__ANDROID__) #include #define tf_printf(...) __android_log_print(ANDROID_LOG_INFO, "tildefriends", __VA_ARGS__) +#elif defined(__APPLE__) +#include +#define tf_printf(...) os_log(OS_LOG_DEFAULT, __VA_ARGS__) #else #include #define tf_printf printf diff --git a/src/main.c b/src/main.c index 3022a96c..c6c905b4 100644 --- a/src/main.c +++ b/src/main.c @@ -36,7 +36,8 @@ struct backtrace_state* g_backtrace_state; -#if !TARGET_OS_IPHONE +const char* k_db_path_default = "db.sqlite"; + #define XOPT_PARSE(name, flags, options, config_ptr, argc, argv, extrac_ptr, extrav_ptr, err_ptr, autohelp_file, autohelp_usage, autohelp_prefix, autohelp_suffix, autohelp_spacer) do { \ xoptContext *_xopt_ctx; \ *(err_ptr) = NULL; \ @@ -65,6 +66,7 @@ struct backtrace_state* g_backtrace_state; break; \ } while (false) +#if !TARGET_OS_IPHONE static int _tf_command_test(const char* file, int argc, char* argv[]); static int _tf_command_import(const char* file, int argc, char* argv[]); static int _tf_command_export(const char* file, int argc, char* argv[]); @@ -88,8 +90,6 @@ const command_t k_commands[] = { { "check", _tf_command_check, "Validate messages in the SSB database." }, }; -const char* k_db_path_default = "db.sqlite"; - void shedPrivileges() { #if !defined(_WIN32) @@ -309,6 +309,7 @@ xopt_help: } return 1; } +#endif typedef struct tf_run_args_t { const char* script; @@ -373,6 +374,7 @@ static void _tf_run_task_thread(void* data) info->result = _tf_run_task(&info->args, info->index); } +#if !TARGET_OS_IPHONE static int _tf_command_run(const char* file, int argc, char* argv[]) { xoptOption options[] = { @@ -654,3 +656,33 @@ done: return result; } #endif + +void tf_run_thread_start(const char* zip_path) +{ + uv_thread_t* thread = tf_malloc(sizeof(uv_thread_t)); + tf_run_thread_data_t* data = tf_malloc(sizeof(tf_run_thread_data_t)); + tf_run_args_t args = + { + .count = 1, + .script = "core/core.js", + .http_port = 12345, + .https_port = 12346, + .ssb_port = 8008, + .db_path = k_db_path_default, + .zip = zip_path, + }; + *data = (tf_run_thread_data_t) + { + .args = args, + }; + uv_thread_create(thread, _tf_run_task_thread, data); +#if 0 + uv_thread_join(&threads[i]); + if (data[i].result != 0) + { + result = data[i].result; + } + tf_free(data); + tf_free(threads); +#endif +}