Skip to content

Instantly share code, notes, and snippets.

@larsbergstrom
Created January 20, 2014 02:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save larsbergstrom/8513764 to your computer and use it in GitHub Desktop.
Save larsbergstrom/8513764 to your computer and use it in GitHub Desktop.
Additional diffs for Servo Android support
diff --git a/jni/main.cpp b/jni/main.cpp
index 34831f8..a881911 100644
--- a/jni/main.cpp
+++ b/jni/main.cpp
@@ -17,8 +17,10 @@
#include <jni.h>
#include <errno.h>
+#include <pthread.h>
#include <string.h>
+#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
@@ -75,7 +77,7 @@ static void init_servo()
{
LOGI("initializing native application for Servo");
- setenv("RUST_LOG", "servo", 1);
+ // setenv("RUST_LOG", "servo,gfx,std,script,util,net,glut", 1);
LOGI("load servo library");
void* libservo = android_dlopen("/data/data/com.example.ServoAndroid/lib/libservo.so");
@@ -85,7 +87,7 @@ static void init_servo()
}
LOGI("load rust-glut library");
- void* libglut = android_dlopen("/data/data/com.example.ServoAndroid/lib/libglut-102129e09d96658-0.1.so");
+ void* libglut = android_dlopen("/data/data/com.example.ServoAndroid/lib/libglut-f186cebf-0.1.so");
if (libglut == NULL) {
LOGW("failed to load rust-glut lib: %s", dlerror());
return;
@@ -117,27 +119,73 @@ static void init_servo()
*(void**)(&main) = dlsym(libservo, "android_start");
if (main) {
LOGI("go into android_start()");
- static char* argv[] = {"servo", "/mnt/sdcard/html/demo.html"};
+ printf("going from printf!\n");
+ static char* argv[] = {"servo", "http://en.wikipedia.org/wiki/Sekhemre_Khutawy_Sobekhotep"};
+ // /sdcard/about-mozilla.html
(*main)(2, argv);
return;
}
LOGW("could not find android_start() in the libServo shared library");
}
+extern "C" void *stderr_thread(void *) {
+ int pipes[2];
+ pipe(pipes);
+ dup2(pipes[1], STDERR_FILENO);
+ FILE *inputFile = fdopen(pipes[0], "r");
+ char readBuffer[1024];
+ while (1) {
+ fgets(readBuffer, sizeof(readBuffer), inputFile);
+ __android_log_write(2, "stderr", readBuffer);
+ }
+ return NULL;
+}
+
+extern "C" void *stdout_thread(void *) {
+ int pipes[2];
+ pipe(pipes);
+ dup2(pipes[1], STDOUT_FILENO);
+ FILE *inputFile = fdopen(pipes[0], "r");
+ char readBuffer[1024];
+ while (1) {
+ fgets(readBuffer, sizeof(readBuffer), inputFile);
+ __android_log_write(2, "stdout", readBuffer);
+ }
+ return NULL;
+}
+
+pthread_t stderr_tid = -1;
+pthread_t stdout_tid = -1;
+
+static void init_std_threads() {
+ pthread_create(&stderr_tid, NULL, stderr_thread, NULL);
+ pthread_create(&stdout_tid, NULL, stdout_thread, NULL);
+}
+
+static void shutdown_std_threads() {
+ // fgets is a safe cancellation point for pthread_cancel.
+ // TODO: missing on Android?
+ // pthread_cancel(stderr_tid);
+ // pthread_cancel(stdout_tid);
+}
+
+
const int W = 2560;
const int H = 1600;
static int init_display() {
LOGI("initialize GLUT window");
glutInitWindowSize(W, H);
return 0;
}
int main(int argc, char* argv[])
{
init_display();
+ init_std_threads();
init_servo();
+ shutdown_std_threads();
return 0;
}
diff --git a/src/libstd/rt/crate_map.rs b/src/libstd/rt/crate_map.rs
index d9b40cf..4d6a144 100644
--- a/src/libstd/rt/crate_map.rs
+++ b/src/libstd/rt/crate_map.rs
@@ -14,6 +14,9 @@ use option::{Some, None, Option};
use vec::ImmutableVector;
use rt::rtio::EventLoop;
+#[cfg(target_os = "android")]
+use path::Path;
+
// Need to tell the linker on OS X to not barf on undefined symbols
// and instead look them up at runtime, which we need to resolve
// the crate_map properly.
@@ -33,7 +36,7 @@ pub struct CrateMap<'a> {
event_loop_factory: Option<fn() -> ~EventLoop>,
}
-#[cfg(not(windows))]
+#[cfg(unix, not(target_os = "android"))]
pub fn get_crate_map() -> Option<&'static CrateMap<'static>> {
extern {
#[crate_map]
@@ -48,6 +51,7 @@ pub fn get_crate_map() -> Option<&'static CrateMap<'static>> {
}
}
+#[cfg(unix, target_os = "android")]
#[cfg(windows)]
pub fn get_crate_map() -> Option<&'static CrateMap<'static>> {
use cast::transmute;
@@ -55,12 +59,22 @@ pub fn get_crate_map() -> Option<&'static CrateMap<'static>> {
use unstable::dynamic_lib::dl;
let sym = unsafe {
- let module = dl::open_internal();
+ let module = if cfg!(target_os = "android") {
+ println!("Loading libservo.so");
+ dl::open_external(&Path::new("/data/data/com.example.ServoAndroid/lib/libservo.so"))
+ } else {
+ dl::open_internal()
+ };
let rust_crate_map_toplevel = if cfg!(target_arch = "x86") {
"__rust_crate_map_toplevel"
} else {
"_rust_crate_map_toplevel"
};
+ if module.is_null() {
+ println!("Failed to load libservo.so");
+ }
+
+ println!("Loading crate map symbol {:?}", rust_crate_map_toplevel);
let sym = rust_crate_map_toplevel.with_c_str(|buf| {
dl::symbol(module, buf)
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment