Support passing in arguments from embedding. Read arguments for Magic Leap from SERVO_ARGS env var.

This commit is contained in:
Josh Matthews 2019-03-20 16:41:20 -04:00
parent d0e9acf1eb
commit a207574b4c
10 changed files with 46 additions and 18 deletions

2
Cargo.lock generated
View file

@ -4179,7 +4179,6 @@ dependencies = [
"libloading 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)",
"libservo 0.0.1",
"log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)",
"serde_json 1.0.13 (registry+https://github.com/rust-lang/crates.io-index)",
"winapi 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)",
]
@ -4203,6 +4202,7 @@ dependencies = [
"jni 0.10.2 (registry+https://github.com/rust-lang/crates.io-index)",
"libc 0.2.44 (registry+https://github.com/rust-lang/crates.io-index)",
"log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)",
"serde_json 1.0.13 (registry+https://github.com/rust-lang/crates.io-index)",
"simpleservo 0.0.1",
]

View file

@ -69,7 +69,7 @@ pub enum MLKeyType {
}
#[repr(transparent)]
#[derive(Copy, Clone)]
#[derive(Clone, Copy)]
pub struct MLLogger(extern "C" fn(MLLogLevel, *const c_char));
#[repr(transparent)]
@ -108,6 +108,7 @@ pub unsafe extern "C" fn init_servo(
url_update: MLURLUpdate,
keyboard: MLKeyboard,
url: *const c_char,
args: *const c_char,
width: u32,
height: u32,
hidpi: f32,
@ -127,8 +128,20 @@ pub unsafe extern "C" fn init_servo(
width as i32,
height as i32,
);
let args = if args.is_null() {
vec![]
} else {
CStr::from_ptr(args)
.to_str()
.unwrap_or("")
.split(' ')
.map(|s| s.to_owned())
.collect()
};
info!("got args: {:?}", args);
let opts = InitOptions {
args: None,
args,
url: Some(url.to_string()),
density: hidpi,
enable_subpixel_text_antialiasing: false,

View file

@ -9,7 +9,6 @@ publish = false
[dependencies]
libservo = { path = "../../../components/servo" }
log = "0.4"
serde_json = "1.0"
[target.'cfg(not(target_os = "macos"))'.dependencies]
libc = "0.2"

View file

@ -41,7 +41,7 @@ thread_local! {
pub use servo::embedder_traits::EventLoopWaker;
pub struct InitOptions {
pub args: Option<String>,
pub args: Vec<String>,
pub url: Option<String>,
pub coordinates: Coordinates,
pub density: f32,
@ -135,16 +135,15 @@ pub fn servo_version() -> String {
/// Initialize Servo. At that point, we need a valid GL context.
/// In the future, this will be done in multiple steps.
pub fn init(
init_opts: InitOptions,
mut init_opts: InitOptions,
gl: Rc<dyn gl::Gl>,
waker: Box<dyn EventLoopWaker>,
callbacks: Box<dyn HostTrait>,
) -> Result<(), &'static str> {
resources::set(Box::new(ResourceReaderInstance::new()));
if let Some(args) = init_opts.args {
let mut args: Vec<String> = serde_json::from_str(&args)
.map_err(|_| "Invalid arguments. Servo arguments must be formatted as a JSON array")?;
let mut args = mem::replace(&mut init_opts.args, vec![]);
if !args.is_empty() {
// opts::from_cmdline_args expects the first argument to be the binary name.
args.insert(0, "servo".to_string());

View file

@ -76,7 +76,12 @@ fn init(
crate::env_logger::init();
let args = unsafe { CStr::from_ptr(opts.args) };
let args = args.to_str().map(|s| s.to_string()).ok();
let args = args
.to_str()
.unwrap_or("")
.split(' ')
.map(|s| s.to_owned())
.collect();
let url = unsafe { CStr::from_ptr(opts.url) };
let url = url.to_str().map(|s| s.to_string()).ok();

View file

@ -17,9 +17,10 @@ bench = false
android_injected_glue = "0.2"
android_logger = "0.7"
jni = "0.10.2"
log = "0.4"
simpleservo = { path = "../api" }
libc = "0.2"
log = "0.4"
serde_json = "1.0"
simpleservo = { path = "../api" }
[build-dependencies]
cc = "1.0"

View file

@ -683,8 +683,15 @@ fn get_options(env: &JNIEnv, opts: JObject) -> Result<(InitOptions, bool, Option
.l()
.map_err(|_| "coordinates is not an object")?;
let coordinates = jni_coords_to_rust_coords(&env, coordinates)?;
let args = match args {
Some(args) => serde_json::from_str(&args)
.map_err(|_| "Invalid arguments. Servo arguments must be formatted as a JSON array")?,
None => None,
};
let opts = InitOptions {
args,
args: args.unwrap_or(vec![]),
url,
coordinates,
density,

View file

@ -29,7 +29,7 @@ public:
/**
* Constructs the Landscape Application.
*/
Servo2D(const char* uri);
Servo2D(const char* uri, const char* args);
/**
* Destroys the Landscape Application.
@ -145,4 +145,5 @@ private:
bool controller_trigger_down_ = false; // Is the controller trigger currently down?
ServoInstance* servo_ = nullptr; // the servo instance we're embedding
const char* uri_ = nullptr;
const char* args_ = nullptr;
};

View file

@ -68,7 +68,7 @@ void keyboard(Servo2D* app, bool visible) {
// The functions Servo provides for hooking up to the ML.
extern "C" ServoInstance* init_servo(EGLContext, EGLSurface, EGLDisplay,
Servo2D*, MLLogger, MLHistoryUpdate, MLURLUpdate, MLKeyboard,
const char* url, int width, int height, float hidpi);
const char* url, const char* args, int width, int height, float hidpi);
extern "C" void heartbeat_servo(ServoInstance*);
extern "C" void keyboard_servo(ServoInstance*, char32_t code, lumin::ui::KeyType keyType);
extern "C" void trigger_servo(ServoInstance*, float x, float y, bool down);
@ -78,8 +78,9 @@ extern "C" void navigate_servo(ServoInstance*, const char* text);
extern "C" void discard_servo(ServoInstance*);
// Create a Servo2D instance
Servo2D::Servo2D(const char* uri)
Servo2D::Servo2D(const char* uri, const char* args)
: uri_(uri ? uri : HOME_PAGE)
, args_(args)
{
ML_LOG(Debug, "Servo2D Constructor.");
}
@ -173,7 +174,7 @@ int Servo2D::init() {
EGLDisplay dpy = eglGetDisplay(EGL_DEFAULT_DISPLAY);
// Hook into servo
servo_ = init_servo(ctx, surf, dpy, this, logger, history, url, keyboard, uri_, VIEWPORT_W, VIEWPORT_H, HIDPI);
servo_ = init_servo(ctx, surf, dpy, this, logger, history, url, keyboard, uri_, args_, VIEWPORT_W, VIEWPORT_H, HIDPI);
if (!servo_) {
ML_LOG(Error, "Servo2D Failed to init servo instance");
abort();

View file

@ -26,7 +26,9 @@ int main(int argc, char **argv)
}
}
Servo2D myApp(uri);
const char* args = getenv("SERVO_ARGS");
Servo2D myApp(uri, args);
int rv = myApp.run();
MLLifecycleFreeInitArgList(&list);