Skip to content

Commit

Permalink
Introduce ServoSurface
Browse files Browse the repository at this point in the history
  • Loading branch information
paulrouget committed Aug 1, 2018
1 parent 02983ef commit c36b165
Show file tree
Hide file tree
Showing 7 changed files with 506 additions and 191 deletions.
54 changes: 24 additions & 30 deletions ports/libsimpleservo/src/jniapi.rs
Expand Up @@ -36,21 +36,19 @@ where
}

#[no_mangle]
pub fn Java_com_mozilla_servoview_NativeServo_version(env: JNIEnv, _class: JClass) -> jstring {
pub fn Java_com_mozilla_servoview_JNIServo_version(env: JNIEnv, _class: JClass) -> jstring {
let v = api::servo_version();
let output = env.new_string(v).expect("Couldn't create java string");
output.into_inner()
}

#[no_mangle]
pub fn Java_com_mozilla_servoview_NativeServo_init(
pub fn Java_com_mozilla_servoview_JNIServo_init(
env: JNIEnv,
_: JClass,
activity: JObject,
args: JString,
url: JString,
wakeup_obj: JObject,
readfile_obj: JObject,
callbacks_obj: JObject,
width: jint,
height: jint,
Expand Down Expand Up @@ -80,9 +78,11 @@ pub fn Java_com_mozilla_servoview_NativeServo_init(
Some(env.get_string(url).expect("Couldn't get java string").into())
};

let wakeup = Box::new(WakeupCallback::new(wakeup_obj, &env));
let readfile = Box::new(ReadFileCallback::new(readfile_obj, &env));
let callbacks = Box::new(HostCallbacks::new(callbacks_obj, &env));
let callbacks_ref = env.new_global_ref(callbacks_obj).unwrap();

let wakeup = Box::new(WakeupCallback::new(callbacks_ref.clone(), &env));
let readfile = Box::new(ReadFileCallback::new(callbacks_ref.clone(), &env));
let callbacks = Box::new(HostCallbacks::new(callbacks_ref, &env));

gl_glue::egl::init().and_then(|gl| {
api::init(
Expand All @@ -100,7 +100,7 @@ pub fn Java_com_mozilla_servoview_NativeServo_init(
}

#[no_mangle]
pub fn Java_com_mozilla_servoview_NativeServo_setBatchMode(
pub fn Java_com_mozilla_servoview_JNIServo_setBatchMode(
env: JNIEnv,
_: JClass,
batch: jboolean,
Expand All @@ -110,7 +110,7 @@ pub fn Java_com_mozilla_servoview_NativeServo_setBatchMode(
}

#[no_mangle]
pub fn Java_com_mozilla_servoview_NativeServo_resize(
pub fn Java_com_mozilla_servoview_JNIServo_resize(
env: JNIEnv,
_: JClass,
width: jint,
Expand All @@ -121,38 +121,38 @@ pub fn Java_com_mozilla_servoview_NativeServo_resize(
}

#[no_mangle]
pub fn Java_com_mozilla_servoview_NativeServo_performUpdates(env: JNIEnv, _class: JClass) {
pub fn Java_com_mozilla_servoview_JNIServo_performUpdates(env: JNIEnv, _class: JClass) {
debug!("performUpdates");
call(env, |s| s.perform_updates());
}

#[no_mangle]
pub fn Java_com_mozilla_servoview_NativeServo_loadUri(env: JNIEnv, _class: JClass, url: JString) {
pub fn Java_com_mozilla_servoview_JNIServo_loadUri(env: JNIEnv, _class: JClass, url: JString) {
debug!("loadUri");
let url: String = env.get_string(url).unwrap().into();
call(env, |s| s.load_uri(&url));
}

#[no_mangle]
pub fn Java_com_mozilla_servoview_NativeServo_reload(env: JNIEnv, _class: JClass) {
pub fn Java_com_mozilla_servoview_JNIServo_reload(env: JNIEnv, _class: JClass) {
debug!("reload");
call(env, |s| s.reload());
}

#[no_mangle]
pub fn Java_com_mozilla_servoview_NativeServo_goBack(env: JNIEnv, _class: JClass) {
pub fn Java_com_mozilla_servoview_JNIServo_goBack(env: JNIEnv, _class: JClass) {
debug!("goBack");
call(env, |s| s.go_back());
}

#[no_mangle]
pub fn Java_com_mozilla_servoview_NativeServo_goForward(env: JNIEnv, _class: JClass) {
pub fn Java_com_mozilla_servoview_JNIServo_goForward(env: JNIEnv, _class: JClass) {
debug!("goForward");
call(env, |s| s.go_forward());
}

#[no_mangle]
pub fn Java_com_mozilla_servoview_NativeServo_scrollStart(
pub fn Java_com_mozilla_servoview_JNIServo_scrollStart(
env: JNIEnv,
_: JClass,
dx: jint,
Expand All @@ -165,7 +165,7 @@ pub fn Java_com_mozilla_servoview_NativeServo_scrollStart(
}

#[no_mangle]
pub fn Java_com_mozilla_servoview_NativeServo_scrollEnd(
pub fn Java_com_mozilla_servoview_JNIServo_scrollEnd(
env: JNIEnv,
_: JClass,
dx: jint,
Expand All @@ -179,7 +179,7 @@ pub fn Java_com_mozilla_servoview_NativeServo_scrollEnd(


#[no_mangle]
pub fn Java_com_mozilla_servoview_NativeServo_scroll(
pub fn Java_com_mozilla_servoview_JNIServo_scroll(
env: JNIEnv,
_: JClass,
dx: jint,
Expand All @@ -192,7 +192,7 @@ pub fn Java_com_mozilla_servoview_NativeServo_scroll(
}

#[no_mangle]
pub fn Java_com_mozilla_servoview_NativeServo_click(env: JNIEnv, _: JClass, x: jint, y: jint) {
pub fn Java_com_mozilla_servoview_JNIServo_click(env: JNIEnv, _: JClass, x: jint, y: jint) {
debug!("click");
call(env, |s| s.click(x as u32, y as u32));
}
Expand All @@ -203,12 +203,9 @@ pub struct WakeupCallback {
}

impl WakeupCallback {
pub fn new(jobject: JObject, env: &JNIEnv) -> WakeupCallback {
pub fn new(callback: GlobalRef, env: &JNIEnv) -> WakeupCallback {
let jvm = Arc::new(env.get_java_vm().unwrap());
WakeupCallback {
callback: env.new_global_ref(jobject).unwrap(),
jvm,
}
WakeupCallback { callback, jvm }
}
}

Expand All @@ -233,9 +230,9 @@ pub struct ReadFileCallback {
}

impl ReadFileCallback {
pub fn new(jobject: JObject, env: &JNIEnv) -> ReadFileCallback {
pub fn new(callback: GlobalRef, env: &JNIEnv) -> ReadFileCallback {
let jvm = env.get_java_vm().unwrap();
let callback = Mutex::new(env.new_global_ref(jobject).unwrap());
let callback = Mutex::new(callback);
ReadFileCallback { callback, jvm }
}
}
Expand All @@ -260,12 +257,9 @@ impl ReadFileTrait for ReadFileCallback {
}

impl HostCallbacks {
pub fn new(jobject: JObject, env: &JNIEnv) -> HostCallbacks {
pub fn new(callbacks: GlobalRef, env: &JNIEnv) -> HostCallbacks {
let jvm = env.get_java_vm().unwrap();
HostCallbacks {
callbacks: env.new_global_ref(jobject).unwrap(),
jvm,
}
HostCallbacks { callbacks, jvm }
}
}

Expand Down
Expand Up @@ -20,10 +20,11 @@
import android.widget.ProgressBar;

import com.mozilla.servoview.ServoView;
import com.mozilla.servoview.Servo;

import java.io.File;

public class MainActivity extends Activity implements ServoView.Client {
public class MainActivity extends Activity implements Servo.Client {

private static final String LOGTAG = "MainActivity";

Expand All @@ -49,10 +50,10 @@ protected void onCreate(Bundle savedInstanceState) {
mUrlField = findViewById(R.id.urlfield);
mProgressBar = findViewById(R.id.progressbar);

mServoView.setClient(this);
mBackButton.setEnabled(false);
mFwdButton.setEnabled(false);

mServoView.setClient(this);
mServoView.requestFocus();

if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
Expand All @@ -66,9 +67,7 @@ protected void onCreate(Bundle savedInstanceState) {
}

String args = getIntent().getStringExtra("servoargs");
if (args != null) {
mServoView.setServoArgs(args);
}
mServoView.setServoArgs(args);

setupUrlField();
}
Expand Down Expand Up @@ -105,18 +104,16 @@ private void loadUrlFromField() {
mServoView.loadUri(Uri.parse(uri));
}

// From activity_main.xml:
public void onReloadClicked(View v) {
mServoView.reload();
}

public void onBackClicked(View v) {
mServoView.goBack();
}

public void onForwardClicked(View v) {
mServoView.goForward();
}

public void onStopClicked(View v) {
mServoView.stop();
}
Expand Down
Expand Up @@ -10,48 +10,65 @@
/**
* Maps /ports/libsimpleservo API
*/
public class NativeServo {
@SuppressWarnings("JniMissingFunction")
public class JNIServo {
JNIServo() {
System.loadLibrary("c++_shared");
System.loadLibrary("simpleservo");
}

public native String version();

public native void init(Activity activity,
String args,
String url,
WakeupCallback wakeup,
ReadFileCallback readfile,
ServoCallbacks callbacks,
Callbacks callbacks,
int width, int height, boolean log);

public native void setBatchMode(boolean mode);

public native void performUpdates();

public native void resize(int width, int height);

public native void reload();

public native void stop();

public native void goBack();

public native void goForward();

public native void loadUri(String uri);

public native void scrollStart(int dx, int dy, int x, int y);

public native void scroll(int dx, int dy, int x, int y);
public native void scrollEnd(int dx, int dy, int x, int y);
public native void click(int x, int y);

NativeServo() {
System.loadLibrary("c++_shared");
System.loadLibrary("simpleservo");
}
public native void scrollEnd(int dx, int dy, int x, int y);

public interface ReadFileCallback {
byte[] readfile(String file);
}
public native void click(int x, int y);

public interface WakeupCallback {
public interface Callbacks {
void wakeup();
}

public interface ServoCallbacks {
void flush();

void makeCurrent();

void onAnimatingChanged(boolean animating);

void onLoadStarted();

void onLoadEnded();

void onTitleChanged(String title);

void onUrlChanged(String url);

void onHistoryChanged(boolean canGoBack, boolean canGoForward);
void onAnimatingChanged(boolean animating);

byte[] readfile(String file);
}
}

0 comments on commit c36b165

Please sign in to comment.