jawt 0.2.1

Cross-platform Rust bindings to Java AWT
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
// Copyright (c) 2025 Gobley Contributors.

//! Implements the [Awt] struct.

use std::ffi::c_void;
use std::fmt;
use std::ptr::NonNull;

use jni::objects::JObject;
use jni::sys::*;
use jni::JNIEnv;

#[cfg(feature = "java-9")]
use crate::rect::Rect;
use crate::sys::*;
use crate::version::AwtVersion;
use crate::DrawingSurface;

#[cfg(target_os = "windows")]
pub type AwtPlatformInfo = windows::Win32::Foundation::HWND;

#[cfg(target_os = "macos")]
pub type AwtPlatformInfo = NonNull<objc2_app_kit::NSView>;

#[cfg(all(
    target_family = "unix",
    not(target_vendor = "apple"),
    not(target_os = "android")
))]
pub type AwtPlatformInfo = x11_dl::xlib::Window;

type UnsafeAwtGetter = unsafe extern "C" fn(*mut jni::sys::JNIEnv, *mut JAWT) -> jboolean;

/// Structure for containing native AWT functions.
pub struct Awt(pub(crate) JAWT);

impl fmt::Debug for Awt {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("Awt")
            .field("version", &self.version())
            .field("GetDrawingSurface", &self.0.GetDrawingSurface)
            .field("FreeDrawingSurface", &self.0.FreeDrawingSurface)
            .field("Lock", &self.0.Lock)
            .field("Unlock", &self.0.Unlock)
            .field("GetComponent", &self.0.GetComponent)
            .field("CreateEmbeddedFrame", &self.0.CreateEmbeddedFrame)
            .field("SetBoudns", &self.0.SetBounds)
            .field(
                "SynthesizeWindowActivation",
                &self.0.SynthesizeWindowActivation,
            )
            .finish()
    }
}

impl Clone for Awt {
    fn clone(&self) -> Self {
        Self(JAWT {
            version: self.0.version,
            GetDrawingSurface: self.0.GetDrawingSurface,
            FreeDrawingSurface: self.0.FreeDrawingSurface,
            Lock: self.0.Lock,
            Unlock: self.0.Unlock,
            GetComponent: self.0.GetComponent,
            CreateEmbeddedFrame: self.0.CreateEmbeddedFrame,
            SetBounds: self.0.SetBounds,
            SynthesizeWindowActivation: self.0.SynthesizeWindowActivation,
        })
    }
}

// Safety: the underlying `JAWT` struct is just a collection of function pointers.
unsafe impl Send for Awt {}

// Safety: the underlying `JAWT` struct is just a collection of function pointers.
unsafe impl Sync for Awt {}

impl Awt {
    /// Construct a safe [Awt] instance from a raw [JAWT] instance.
    ///
    /// # Safety
    ///
    /// `inner` must be properly initialized using [JAWT_GetAWT].
    pub const unsafe fn from_inner(inner: JAWT) -> Self {
        Self(inner)
    }

    /// Returns a shared reference to the underlying [JAWT] instance.
    pub const fn as_ref(&self) -> &JAWT {
        &self.0
    }

    /// Returns a mutable reference to the underlying [JAWT] instance.
    ///
    /// # Safety
    ///
    /// The caller should ensure that the underlying instance keep containing valid field values.
    pub unsafe fn as_mut(&mut self) -> &mut JAWT {
        &mut self.0
    }

    /// Consumes [Awt] and returns the underlying [JAWT] instance.
    #[inline(always)]
    pub fn into_inner(self) -> JAWT {
        self.0
    }

    #[inline(always)]
    fn find_get_awt(env: &JNIEnv) -> Option<UnsafeAwtGetter> {
        use once_cell::sync::OnceCell;

        static GET_AWT: OnceCell<UnsafeAwtGetter> = OnceCell::new();

        let cached_get_awt = GET_AWT.get_or_try_init(|| {
            let finders: &[unsafe fn(&JNIEnv) -> Option<UnsafeAwtGetter>] = &[
                #[cfg(feature = "dynamic-get-awt")]
                Self::find_dynamic_get_awt,
                #[cfg(feature = "static-get-awt")]
                Self::find_static_get_awt,
            ];
            for finder in finders {
                if let Some(get_awt) = unsafe { finder(env) } {
                    return Ok(get_awt);
                }
            }
            Err(())
        });

        cached_get_awt.ok().copied()
    }

    #[inline(always)]
    #[cfg(feature = "dynamic-get-awt")]
    unsafe fn find_dynamic_get_awt(env: &JNIEnv) -> Option<UnsafeAwtGetter> {
        // Unsafe operations below: the safe JNI wrapper allocates a new `CString` every time a
        // `&str` is passed. We choose to use the unsafe counterpart to prevent it.

        let env = env.get_raw();

        let find_class = unsafe { (**env).FindClass }?;
        let get_static_method_id = unsafe { (**env).GetStaticMethodID }?;
        let call_static_object_method = unsafe { (**env).CallStaticObjectMethod }?;
        let new_string_utf = unsafe { (**env).NewStringUTF }?;

        // C-string literals become stable starting with Rust 1.77
        let system_class = unsafe { find_class(env, b"java/lang/System\0".as_ptr() as _) };
        if system_class.is_null() {
            return None;
        }

        let get_property_method = unsafe {
            get_static_method_id(
                env,
                system_class,
                b"getProperty\0".as_ptr() as _,
                b"(Ljava/lang/String;)Ljava/lang/String;\0".as_ptr() as _,
            )
        };
        if get_property_method.is_null() {
            return None;
        }

        let java_home_string = unsafe { new_string_utf(env, b"java.home\0".as_ptr() as _) };
        if java_home_string.is_null() {
            return None;
        }

        let property_string = unsafe {
            call_static_object_method(env, system_class, get_property_method, java_home_string)
        };
        if property_string.is_null() {
            return None;
        }

        Self::find_awt_from_java_home(env, property_string)
    }

    #[inline(always)]
    #[cfg(all(feature = "dynamic-get-awt", target_family = "windows"))]
    unsafe fn find_awt_from_java_home(
        env: *mut jni::sys::JNIEnv,
        java_home: jstring,
    ) -> Option<UnsafeAwtGetter> {
        use std::ptr;

        use libc::*;
        use windows::core::{PCSTR, PCWSTR};
        use windows::Win32::System::LibraryLoader::{GetProcAddress, LoadLibraryW};

        let get_string_chars = (**env).GetStringChars?;
        let release_string_chars = (**env).ReleaseStringChars?;
        let java_home_chars = get_string_chars(env, java_home, ptr::null_mut());
        let java_home_chars_len = wcslen(java_home_chars);

        let path_suffix = utf16_literal::utf16!("\\bin\\jawt.dll\0");

        let mut jawt_path = Vec::with_capacity(java_home_chars_len + path_suffix.len());

        ptr::copy(java_home_chars, jawt_path.as_mut_ptr(), java_home_chars_len);
        ptr::copy(
            path_suffix.as_ptr() as _,
            jawt_path.as_mut_ptr().add(java_home_chars_len),
            path_suffix.len(),
        );

        jawt_path.set_len(java_home_chars_len + path_suffix.len());

        release_string_chars(env, java_home, java_home_chars);

        let library = LoadLibraryW(PCWSTR(jawt_path.as_mut_ptr() as _)).ok()?;
        let symbol = GetProcAddress(library, PCSTR(b"JAWT_GetAWT\0".as_ptr() as _))?;

        Some(std::mem::transmute::<
            unsafe extern "system" fn() -> isize,
            UnsafeAwtGetter,
        >(symbol))
    }

    #[inline(always)]
    #[cfg(all(feature = "dynamic-get-awt", target_family = "unix"))]
    unsafe fn find_awt_from_java_home(
        env: *mut jni::sys::JNIEnv,
        java_home: jstring,
    ) -> Option<UnsafeAwtGetter> {
        use std::ptr;

        use libc::*;

        let get_string_utf_chars = (**env).GetStringUTFChars?;
        let release_string_utf_chars = (**env).ReleaseStringUTFChars?;
        let java_home_chars = get_string_utf_chars(env, java_home, ptr::null_mut());
        let java_home_chars_len = strlen(java_home_chars);

        #[cfg(target_os = "macos")]
        let path_suffix = b"/lib/libjawt.dylib\0";
        #[cfg(not(target_os = "macos"))]
        let path_suffix = b"/lib/libjawt.so\0";

        let mut jawt_path = Vec::with_capacity(java_home_chars_len + path_suffix.len());

        ptr::copy(java_home_chars, jawt_path.as_mut_ptr(), java_home_chars_len);
        ptr::copy(
            path_suffix.as_ptr() as _,
            jawt_path.as_mut_ptr().add(java_home_chars_len),
            path_suffix.len(),
        );

        jawt_path.set_len(java_home_chars_len + path_suffix.len());

        release_string_utf_chars(env, java_home, java_home_chars);

        let handle = dlopen(jawt_path.as_ptr() as _, RTLD_LAZY | RTLD_LOCAL);
        if handle.is_null() {
            dlerror();
            return None;
        }

        let symbol = dlsym(handle, b"JAWT_GetAWT\0".as_ptr() as _);
        if symbol.is_null() {
            dlerror();
            return None;
        }

        Some(std::mem::transmute::<*mut c_void, UnsafeAwtGetter>(symbol))
    }

    #[inline(always)]
    #[cfg(feature = "static-get-awt")]
    fn find_static_get_awt(env: &JNIEnv) -> Option<UnsafeAwtGetter> {
        let _ = env;
        Some(JAWT_GetAWT)
    }

    fn from_version_raw(env: &JNIEnv, version: jint) -> Option<Self> {
        let get_awt = Self::find_get_awt(env)?;
        let mut inner = JAWT {
            version,
            GetDrawingSurface: None,
            FreeDrawingSurface: None,
            Lock: None,
            Unlock: None,
            GetComponent: None,
            CreateEmbeddedFrame: None,
            SetBounds: None,
            SynthesizeWindowActivation: None,
        };
        if unsafe { get_awt(env.get_raw(), &mut inner) } == JNI_FALSE {
            return None;
        }
        Some(Self(inner))
    }

    /// Get the AWT native structure. This function returns [None] if an error
    /// occurs.
    pub fn from_version(env: &JNIEnv, version: AwtVersion) -> Option<Self> {
        Self::from_version_raw(env, version.0)
    }

    #[cfg(target_os = "macos")]
    /// Get the AWT native structure with the [JAWT_MACOSX_USE_CALAYER] flag being set. When you
    /// create an [Awt] instance with a JAWT version less than 1.7, you must call this function or
    /// you will get a [None]. This is to maintain compatibility with applications that used the
    /// interface with Java 6 which had multiple rendering models. This function is not necessary
    /// when JAWT version 1.7 or greater is used as the one using [CALayer] is the only supported
    /// rendering mode.
    ///
    /// [CALayer]: objc2_quartz_core::CALayer
    pub fn from_version_with_ca_layer(env: &JNIEnv, version: AwtVersion) -> Option<Self> {
        Self::from_version_raw(env, version.0 | JAWT_MACOSX_USE_CALAYER)
    }

    /// Version of this structure.
    pub fn version(&self) -> AwtVersion {
        AwtVersion(self.0.version as _)
    }

    /// Return a [DrawingSurface] from a target Java object. This value may be cached. Returns
    /// [None] if an error has occurred. Target must be a [java.awt.Component] (should be a Canvas
    /// or Window for native rendering).
    ///
    /// [java.awt.Component]: https://docs.oracle.com/javase/8/docs/api/java/awt/Component.html
    pub fn drawing_surface(&self, env: &JNIEnv, target: JObject) -> Option<DrawingSurface> {
        let get_drawing_surface = crate::utils::unwrap_fn!(self.0, JAWT.GetDrawingSurface);
        let free_drawing_surface = crate::utils::unwrap_fn!(self.0, JAWT.FreeDrawingSurface);
        let drawing_surface =
            NonNull::new(unsafe { get_drawing_surface(env.get_raw(), target.into_raw()) })?;
        Some(DrawingSurface {
            inner: drawing_surface,
            free: free_drawing_surface,
        })
    }

    #[cfg(feature = "java-1-4")]
    /// Since [1.4](AwtVersion::VERSION_1_4)
    ///
    /// Locks the entire AWT for synchronization purposes.
    ///
    /// # Safety
    ///
    /// After invoking this function, [Awt::unlock] should be called.
    pub unsafe fn lock(&self, env: &JNIEnv) {
        unsafe { crate::utils::unwrap_fn!(self.0, JAWT.Lock)(env.get_raw()) };
    }

    #[cfg(feature = "java-1-4")]
    /// Since [1.4](AwtVersion::VERSION_1_4)
    ///
    /// Unlocks the entire AWT for synchronization purposes.
    ///
    /// # Safety
    ///
    /// [Awt::lock] should be called before invoking this function.
    pub unsafe fn unlock(&self, env: &JNIEnv) {
        unsafe { crate::utils::unwrap_fn!(self.0, JAWT.Unlock)(env.get_raw()) };
    }
}

impl Awt {
    #[cfg(any(feature = "java-1-4", feature = "java-9"))]
    #[cfg(target_os = "windows")]
    fn lower_platform_info(platform_info: AwtPlatformInfo) -> *mut c_void {
        platform_info.0
    }

    #[cfg(any(feature = "java-1-4", feature = "java-9"))]
    #[cfg(target_os = "macos")]
    fn lower_platform_info(platform_info: AwtPlatformInfo) -> *mut c_void {
        platform_info.as_ptr() as *mut c_void
    }

    #[cfg(any(feature = "java-1-4", feature = "java-9"))]
    #[cfg(all(
        target_family = "unix",
        not(target_vendor = "apple"),
        not(target_os = "android")
    ))]
    fn lower_platform_info(platform_info: AwtPlatformInfo) -> *mut c_void {
        platform_info as *mut c_void
    }
}

impl Awt {
    #[cfg(feature = "java-1-4")]
    /// Since [1.4](AwtVersion::VERSION_1_4)
    ///
    /// Returns a reference to [java.awt.Component] from a native platform handle. On Windows, this
    /// corresponds to an HWND, on Linux, this is a Drawable. For other platforms, see the
    /// [appropriate machine-dependent header file] for a description. The reference returned by
    /// this function is a local reference that is only valid in this environment. This function
    /// returns a [JObject::null()] reference if no component could be found with matching platform
    /// information.
    ///
    /// [java.awt.Component]: https://docs.oracle.com/javase/8/docs/api/java/awt/Component.html
    /// [appropriate machine-dependent header file]: https://github.com/openjdk/jdk/blob/jdk-17%2B35/src/java.desktop/windows/native/include/jawt_md.h
    ///
    /// # Safety
    ///
    /// The caller should ensure that `platform_info` is a valid platform object.
    pub unsafe fn component_of<'env>(
        &self,
        env: &JNIEnv<'env>,
        platform_info: AwtPlatformInfo,
    ) -> JObject<'env> {
        JObject::from_raw(crate::utils::unwrap_fn!(self.0, JAWT.GetComponent)(
            env.get_raw(),
            Self::lower_platform_info(platform_info),
        ))
    }
}

#[cfg(feature = "java-9")]
pub struct AwtEmbeddedFrame<'a>(pub(crate) JObject<'a>);

#[cfg(feature = "java-9")]
impl<'a> AwtEmbeddedFrame<'a> {
    /// Construct a safe [AwtEmbeddedFrame] from a raw [JObject] instance.
    ///
    /// # Safety
    ///
    /// `inner` must be created using [JAWT::CreateEmbeddedFrame].
    pub unsafe fn from_inner(inner: JObject<'a>) -> Option<Self> {
        if inner.is_null() {
            return None;
        }
        Some(Self(inner))
    }

    /// Consumes [AwtEmbeddedFrame] and returns the underlying [JObject] instance.
    pub const fn into_inner(self) -> JObject<'a> {
        self.0
    }
}

impl Awt {
    #[cfg(feature = "java-9")]
    /// Since [9](AwtVersion::VERSION_9)
    ///
    /// Creates a [java.awt.Frame] placed in a native container. Container is referenced by the
    /// native platform handle. For example on Windows this corresponds to an `HWND`. For other
    /// platforms, see the [appropriate machine-dependent header file] for a description. The
    /// reference returned by this function is a local reference that is only valid in this
    /// environment. This function returns a [JObject::null()] reference if no frame could be
    /// created with matching platform information.
    ///
    /// [java.awt.Frame]: https://docs.oracle.com/en/java/javase/11/docs/api/java.desktop/java/awt/Frame.html
    /// [appropriate machine-dependent header file]: https://github.com/openjdk/jdk/blob/jdk-17%2B35/src/java.desktop/windows/native/include/jawt_md.h
    ///
    /// # Safety
    ///
    /// The caller should ensure that `platform_info` is a valid platform object and alive until the
    /// returned [AwtEmbeddedFrame] drops.
    pub unsafe fn new_embedded_frame<'env>(
        &self,
        env: &JNIEnv<'env>,
        platform_info: AwtPlatformInfo,
    ) -> Option<AwtEmbeddedFrame<'env>> {
        AwtEmbeddedFrame::from_inner(JObject::from_raw(crate::utils::unwrap_fn!(
            self.0,
            JAWT.CreateEmbeddedFrame
        )(
            env.get_raw(),
            Self::lower_platform_info(platform_info),
        )))
    }

    #[cfg(feature = "java-9")]
    /// Since [9](AwtVersion::VERSION_9)
    ///
    /// Moves and resizes the embedded frame. The new location of the top-left corner is specified
    /// by x and y parameters relative to the native parent component. The new size is specified by
    /// width and height.
    ///
    /// [java.awt.Component.setLocation()] and [java.awt.Component.setBounds()] for EmbeddedFrame
    /// really don't move it within the native parent. These methods always locate the embedded
    /// frame at (0, 0) for backward compatibility. To allow moving embedded frames this method was
    /// introduced, and it works just the same way as `setLocation()` and `setBounds()` for usual,
    /// non-embedded components.
    ///
    /// Using usual `get/setLocation()` and `get/setBounds()` together with this new
    /// method is not recommended.
    ///
    /// [java.awt.Frame]: https://docs.oracle.com/en/java/javase/11/docs/api/java.desktop/java/awt/Frame.html
    /// [java.awt.Component.setLocation()]: https://docs.oracle.com/en/java/javase/11/docs/api/java.desktop/java/awt/Component.html#setLocation(int,int)
    /// [java.awt.Component.setBounds()]: https://docs.oracle.com/en/java/javase/11/docs/api/java.desktop/java/awt/Component.html#setBounds(int,int,int,int)
    pub fn set_bounds(&self, env: &JNIEnv, embedded_frame: AwtEmbeddedFrame, new_location: Rect) {
        unsafe {
            crate::utils::unwrap_fn!(self.0, JAWT.SetBounds)(
                env.get_raw(),
                embedded_frame.into_inner().into_raw(),
                new_location.x,
                new_location.y,
                new_location.width,
                new_location.height,
            );
        }
    }

    #[cfg(feature = "java-9")]
    /// Since [9](AwtVersion::VERSION_9)
    ///
    /// Synthesize a native message to activate or deactivate an EmbeddedFrame window depending on
    /// the value of parameter `do_activate`, if `true` activates the window; otherwise, deactivates
    /// the window.
    pub fn synthesize_window_activation(
        &self,
        env: &JNIEnv,
        embedded_frame: AwtEmbeddedFrame,
        activate: bool,
    ) {
        unsafe {
            crate::utils::unwrap_fn!(self.0, JAWT.SynthesizeWindowActivation)(
                env.get_raw(),
                embedded_frame.into_inner().into_raw(),
                activate as jboolean,
            );
        }
    }
}

impl AsRef<JAWT> for Awt {
    fn as_ref(&self) -> &JAWT {
        &self.0
    }
}