Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add from_java() constructors to InputQueue, KeyEvent and MotionEvent #456

Merged
merged 2 commits into from
Jan 27, 2024

Conversation

MarijnS95
Copy link
Member

These were added in API 31 (KeyEvent/MotionEvent) and API 33 (InputEvent), but not yet mapped in the NDK crate.

Copy link
Contributor

@rib rib left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, these look good to me.

As commented, I'd be tempted to avoid exposing jni_sys types in the public API but I think either way is probably OK.

@@ -6,6 +6,9 @@ use std::io::Result;
use std::os::raw::c_int;
use std::ptr::{self, NonNull};

#[cfg(feature = "api-level-33")]
use jni_sys::{jobject, JNIEnv};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd be slightly tempted to just use c_void pointers and avoid exposing any jni crate types in the public API but maybe there are other apis already exposing jni types?

The pointers are for unsafe methods anyway so the reduced type safety could be acceptable.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, let's think about it a bit and do that in a followup impact: breaking API/ABI-breaking change change 👍

@MarijnS95
Copy link
Member Author

Force-pushed to fix some wrong intradoc links :)

ndk/src/event.rs Outdated
/// # Safety
///
/// This function should be called with a healthy JVM pointer and with a non-null
/// [`android.view.MotionEvent`], which must be kept alive on the Java/Kotlin side.
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rib I copied the which must be kept alive on the Java/Kotlin side. portion from other from_java() functions, but I don't think it holds true for these APIs.

  1. As the copied doc-comment above suggests: ... that is a copy of the specified Java ...;
  2. And AInputEvent_release() which must be called states: The underlying Java object remains valid and does not change its state..

It seems the two objects are independent and have their own lifetime after this call.

If you agree I'll remove this text.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, I'm not sure tbh - from when I last tried to get my head around the implementation details for input event handling I recall some pretty funky lifetime tracking for events that went via an InputQueue since there's some special callbacks in java for events after they are handled by native code from what I vaguely recall and I don't really know how the bits all fit together if using these without really digging into the implementation.

My guess would be that you do actually need to ensure the event is kept alive in the jvm if they only hold a weak reference in the native event and guess that the release isn't just about freeing the struct but also tracking when the event is finished with on the jvm side so that it can progress to the next stage of being handled in the jvm.

I'd er on the side of keeping that requirement in the docs unless we really look into the implementation details more closely.

Copy link
Member Author

@MarijnS95 MarijnS95 Jan 27, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and guess that the release isn't just about freeing the struct but also tracking when the event is finished with on the jvm side so that it can progress to the next stage of being handled in the jvm.

To confirm, the terribly named _release (why not _destroy... release sounds like refcounting) function only calls delete and doesn't perform any "event finished" handling later. That, after all, happens elsewhere via AInputQueue_finishEvent() which has a reference to the input queue and exists specifically for explicitness of this purpose.

https://cs.android.com/android/platform/superproject/main/+/main:frameworks/base/native/android/input.cpp;l=57-59;drc=f5c06a171c6eb51c6c914540b36a8211036fe735


Looks like I already removed the wording in the last force-push, let's merge this.

@MarijnS95 MarijnS95 requested a review from rib January 18, 2024 22:07
@MarijnS95 MarijnS95 merged commit 12eb049 into master Jan 27, 2024
38 checks passed
@MarijnS95 MarijnS95 deleted the event-from-java branch January 27, 2024 15:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants