Skip to content

Commit

Permalink
JBR-5961 Wayland: can't switch between projects using menu
Browse files Browse the repository at this point in the history
support Window.toFront in Wayland toolkit
  • Loading branch information
JB-Dmitry authored and jbrbot committed Nov 8, 2024
1 parent 72722da commit b420439
Show file tree
Hide file tree
Showing 8 changed files with 600 additions and 6 deletions.
5 changes: 5 additions & 0 deletions src/java.desktop/unix/classes/sun/awt/wl/WLComponentPeer.java
Original file line number Diff line number Diff line change
Expand Up @@ -830,6 +830,10 @@ final void requestUnsetFullScreen() {
performLocked(() -> nativeRequestUnsetFullScreen(nativePtr));
}

final void activate() {
performLocked(() -> nativeActivate(nativePtr));
}

private static native void initIDs();

protected native long nativeCreateFrame();
Expand Down Expand Up @@ -864,6 +868,7 @@ protected native void nativeCreateWLPopup(long ptr, long parentPtr,
private static native void nativeSetCursor(long pData);
private static native long nativeGetPredefinedCursor(String name);
private native void nativeShowWindowMenu(long ptr, int x, int y);
private native void nativeActivate(long ptr);

static long getParentNativePtr(Component target) {
Component parent = target.getParent();
Expand Down
5 changes: 0 additions & 5 deletions src/java.desktop/unix/classes/sun/awt/wl/WLFramePeer.java
Original file line number Diff line number Diff line change
Expand Up @@ -127,11 +127,6 @@ public void emulateActivation(boolean activate) {
throw new UnsupportedOperationException();
}

@Override
public void toFront() {
//throw new UnsupportedOperationException();
}

@Override
public void toBack() {
throw new UnsupportedOperationException();
Expand Down
2 changes: 1 addition & 1 deletion src/java.desktop/unix/classes/sun/awt/wl/WLWindowPeer.java
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public void endValidate() {

@Override
public void toFront() {
// TODO
activate();
}

@Override
Expand Down
76 changes: 76 additions & 0 deletions src/java.desktop/unix/native/libawt_wlawt/WLComponentPeer.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,53 @@ static jmethodID notifyConfiguredMID;
static jmethodID notifyEnteredOutputMID;
static jmethodID notifyLeftOutputMID;

struct activation_token_list_item {
struct xdg_activation_token_v1 *token;
struct activation_token_list_item *next_item;
};

static struct activation_token_list_item *add_token(struct activation_token_list_item *list,
struct xdg_activation_token_v1 *token_to_add) {
struct activation_token_list_item *new_item =
(struct activation_token_list_item *) calloc(1, sizeof(struct activation_token_list_item));
new_item->token = token_to_add;
new_item->next_item = list;
return new_item;
}

static struct activation_token_list_item *delete_last_token(struct activation_token_list_item *list) {
assert(list);
xdg_activation_token_v1_destroy(list->token);
struct activation_token_list_item *next_item = list->next_item;
free(list);
return next_item;
}

static struct activation_token_list_item *delete_token(struct activation_token_list_item *list,
struct xdg_activation_token_v1 *token_to_delete) {
if (list == NULL) {
return NULL;
} else if (list->token == token_to_delete) {
return delete_last_token(list);
} else {
list->next_item = delete_token(list->next_item, token_to_delete);
return list;
}
}

static void delete_all_tokens(struct activation_token_list_item *list) {
while (list) {
list = delete_last_token(list);
}
}

struct WLFrame {
jobject nativeFramePeer; // weak reference
struct wl_surface *wl_surface;
struct xdg_surface *xdg_surface;
struct WLFrame *parent;
struct xdg_positioner *xdg_positioner;
struct activation_token_list_item *activation_token_list;
jboolean toplevel;
union {
struct xdg_toplevel *xdg_toplevel;
Expand Down Expand Up @@ -224,6 +265,22 @@ static const struct xdg_popup_listener xdg_popup_listener = {
.popup_done = xdg_popup_done,
};

static void
xdg_activation_token_v1_done(void *data,
struct xdg_activation_token_v1 *xdg_activation_token_v1,
const char *token) {
struct WLFrame *frame = (struct WLFrame *) data;
assert(wlFrame);
struct wl_surface *surface = frame->wl_surface;
assert(surface);
xdg_activation_v1_activate(xdg_activation_v1, token, surface);
frame->activation_token_list = delete_token(frame->activation_token_list, xdg_activation_token_v1);
}

static const struct xdg_activation_token_v1_listener xdg_activation_token_v1_listener = {
.done = xdg_activation_token_v1_done,
};

JNIEXPORT void JNICALL
Java_sun_awt_wl_WLComponentPeer_initIDs
(JNIEnv *env, jclass clazz)
Expand Down Expand Up @@ -440,6 +497,8 @@ DoHide(struct WLFrame *frame)
}
xdg_surface_destroy(frame->xdg_surface);
wl_surface_destroy(frame->wl_surface);
delete_all_tokens(frame->activation_token_list);
frame->activation_token_list = NULL;
frame->wl_surface = NULL;
frame->xdg_surface = NULL;
frame->xdg_toplevel = NULL;
Expand Down Expand Up @@ -525,3 +584,20 @@ JNIEXPORT void JNICALL Java_sun_awt_wl_WLComponentPeer_nativeShowWindowMenu
xdg_toplevel_show_window_menu(frame->xdg_toplevel, wl_seat, last_mouse_pressed_serial, x, y);
}
}

JNIEXPORT void JNICALL
Java_sun_awt_wl_WLComponentPeer_nativeActivate
(JNIEnv *env, jobject obj, jlong ptr)
{
struct WLFrame *frame = jlong_to_ptr(ptr);
if (frame->wl_surface && xdg_activation_v1 && wl_seat) {
struct xdg_activation_token_v1 *token = xdg_activation_v1_get_activation_token(xdg_activation_v1);
xdg_activation_token_v1_add_listener(token, &xdg_activation_token_v1_listener, frame);
xdg_activation_token_v1_set_serial(token, last_input_or_focus_serial, wl_seat);
if (wl_surface_in_focus) {
xdg_activation_token_v1_set_surface(token, wl_surface_in_focus);
}
xdg_activation_token_v1_commit(token);
frame->activation_token_list = add_token(frame->activation_token_list, token);
}
}
16 changes: 16 additions & 0 deletions src/java.desktop/unix/native/libawt_wlawt/WLToolkit.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,19 @@ struct wl_display *wl_display = NULL;
struct wl_shm *wl_shm = NULL;
struct wl_compositor *wl_compositor = NULL;
struct xdg_wm_base *xdg_wm_base = NULL;
struct xdg_activation_v1 *xdg_activation_v1 = NULL;
struct wl_seat *wl_seat = NULL;

struct wl_keyboard *wl_keyboard;
struct wl_pointer *wl_pointer;

struct wl_cursor_theme *wl_cursor_theme = NULL;

struct wl_surface *wl_surface_in_focus = NULL;

uint32_t last_mouse_pressed_serial = 0;
uint32_t last_pointer_enter_serial = 0;
uint32_t last_input_or_focus_serial = 0;

static uint32_t num_of_outstanding_sync = 0;

Expand Down Expand Up @@ -312,6 +316,7 @@ wl_pointer_button(void *data, struct wl_pointer *wl_pointer, uint32_t serial,
pointer_event.button = button,
pointer_event.state = state;
if (state) {
last_input_or_focus_serial = serial;
last_mouse_pressed_serial = serial;
}
}
Expand Down Expand Up @@ -453,6 +458,9 @@ static void
wl_keyboard_enter(void *data, struct wl_keyboard *wl_keyboard,
uint32_t serial, struct wl_surface *surface, struct wl_array *keys)
{
wl_surface_in_focus = surface;
last_input_or_focus_serial = serial;

JNIEnv* env = getEnv();
(*env)->CallStaticVoidMethod(env,
tkClass,
Expand Down Expand Up @@ -482,6 +490,8 @@ static void
wl_keyboard_key(void *data, struct wl_keyboard *wl_keyboard,
uint32_t serial, uint32_t time, uint32_t evdev_key, uint32_t state)
{
last_input_or_focus_serial = serial;

const uint32_t scancode = evdev_key + 8;
const uint32_t keychar32 = xkb_ifs.xkb_state_key_get_utf32(xkb_state, scancode);
const xkb_keysym_t keysym = xkb_ifs.xkb_state_key_get_one_sym(xkb_state, scancode);
Expand All @@ -504,6 +514,8 @@ static void
wl_keyboard_leave(void *data, struct wl_keyboard *wl_keyboard,
uint32_t serial, struct wl_surface *surface)
{
wl_surface_in_focus = NULL;

JNIEnv* env = getEnv();
(*env)->CallStaticVoidMethod(env,
tkClass,
Expand All @@ -519,6 +531,8 @@ wl_keyboard_modifiers(void *data, struct wl_keyboard *wl_keyboard,
uint32_t mods_latched, uint32_t mods_locked,
uint32_t group)
{
last_input_or_focus_serial = serial;

xkb_ifs.xkb_state_update_mask(xkb_state,
mods_depressed,
mods_latched,
Expand Down Expand Up @@ -669,6 +683,8 @@ registry_global(void *data, struct wl_registry *wl_registry,
} else if (strcmp(interface, wl_output_interface.name) == 0) {
WLOutputRegister(wl_registry, name);
process_new_listener_before_end_of_init();
} else if (strcmp(interface, xdg_activation_v1_interface.name) == 0) {
xdg_activation_v1 = wl_registry_bind(wl_registry, name, &xdg_activation_v1_interface, 1);
}
#ifdef WAKEFIELD_ROBOT
else if (strcmp(interface, wakefield_interface.name) == 0) {
Expand Down
5 changes: 5 additions & 0 deletions src/java.desktop/unix/native/libawt_wlawt/WLToolkit.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include <wayland-client.h>
#include <wayland-cursor.h>
#include "xdg-shell-client-protocol.h"
#include "xdg-activation-v1-client-protocol.h"

#define CHECK_NULL_THROW_OOME_RETURN(env, x, msg, z)\
do { \
Expand All @@ -48,10 +49,14 @@ extern struct wl_display *wl_display;
extern struct wl_pointer *wl_pointer;
extern struct wl_compositor *wl_compositor;
extern struct xdg_wm_base *xdg_wm_base;
extern struct xdg_activation_v1 *xdg_activation_v1;
extern struct wl_cursor_theme *wl_cursor_theme;

extern struct wl_surface *wl_surface_in_focus;

extern uint32_t last_mouse_pressed_serial;
extern uint32_t last_pointer_enter_serial;
extern uint32_t last_input_or_focus_serial;

JNIEnv *getEnv();

Expand Down
Loading

0 comments on commit b420439

Please sign in to comment.