Skip to content

Commit

Permalink
Half-baked mouse cursors
Browse files Browse the repository at this point in the history
  • Loading branch information
TheDrawingCoder-Gamer committed Dec 13, 2023
1 parent 78cd01f commit 460744e
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 45 deletions.
2 changes: 1 addition & 1 deletion examples/dashboard/java/PanelMouseCursors.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public void accept(Event e) {
keepCursor = false;
} else if (!keepCursor) {
if (window._lastCursor != MouseCursor.ARROW)
window.requestFrame();
window.requestFrame();
window.setMouseCursor(MouseCursor.ARROW);
}
lastInside = inside;
Expand Down
2 changes: 1 addition & 1 deletion shared/cc/MouseCursor.hh
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,4 @@ namespace jwm {
}
}

}
}
38 changes: 7 additions & 31 deletions wayland/cc/WindowManagerWayland.cc
Original file line number Diff line number Diff line change
Expand Up @@ -76,34 +76,7 @@ WindowManagerWayland::WindowManagerWayland():


wl_display_roundtrip(display);
{
wl_cursor_theme* cursor_theme = wl_cursor_theme_load(nullptr, 24, shm);
// TODO: what about if missing : (
auto loadCursor = [&](const char* name) {
wl_cursor* cursor = wl_cursor_theme_get_cursor(cursor_theme, name);
wl_cursor_image* cursorImage = cursor->images[0];
return cursorImage;
};

_cursors[static_cast<int>(jwm::MouseCursor::ARROW )] = loadCursor("default");
_cursors[static_cast<int>(jwm::MouseCursor::CROSSHAIR )] = loadCursor("crosshair");
_cursors[static_cast<int>(jwm::MouseCursor::HELP )] = loadCursor("help");
_cursors[static_cast<int>(jwm::MouseCursor::POINTING_HAND )] = loadCursor("pointer");
_cursors[static_cast<int>(jwm::MouseCursor::IBEAM )] = loadCursor("text");
_cursors[static_cast<int>(jwm::MouseCursor::NOT_ALLOWED )] = loadCursor("not-allowed");
_cursors[static_cast<int>(jwm::MouseCursor::WAIT )] = loadCursor("watch");
_cursors[static_cast<int>(jwm::MouseCursor::RESIZE_NS )] = loadCursor("ns-resize");
_cursors[static_cast<int>(jwm::MouseCursor::RESIZE_WE )] = loadCursor("ew-resize");
_cursors[static_cast<int>(jwm::MouseCursor::RESIZE_NESW )] = loadCursor("nesw-resize");
_cursors[static_cast<int>(jwm::MouseCursor::RESIZE_NWSE )] = loadCursor("nwse-resize");

cursorSurface = wl_compositor_create_surface(compositor);

wl_surface_attach(cursorSurface,
wl_cursor_image_get_buffer(_cursors[static_cast<int>(jwm::MouseCursor::ARROW)]), 0, 0);
wl_surface_commit(cursorSurface);
}

cursorSurface = wl_compositor_create_surface(compositor);

}

Expand Down Expand Up @@ -241,17 +214,20 @@ WindowWayland* WindowManagerWayland::getWindowForNative(wl_surface* surface) {
void WindowManagerWayland::pointerHandleEnter(void* data, wl_pointer* pointer, uint32_t serial,
wl_surface *surface, wl_fixed_t surface_x, wl_fixed_t surface_y) {
WindowManagerWayland* self = (WindowManagerWayland*)data;
wl_cursor_image* image = self->_cursors[static_cast<int>(jwm::MouseCursor::ARROW)];
wl_pointer_set_cursor(pointer, serial, self->cursorSurface, image->hotspot_x, image->hotspot_y);
if (self->getWindowForNative(surface))
self->mouseSerial = serial;
if (self->getWindowForNative(surface)) {
WindowWayland* window = self->getWindowForNative(surface);
window->setCursor(jwm::MouseCursor::ARROW);
self->focusedSurface = surface;
}
}
void WindowManagerWayland::pointerHandleLeave(void* data, wl_pointer* pointer, uint32_t serial,
wl_surface *surface) {
WindowManagerWayland* self = (WindowManagerWayland*)data;
self->focusedSurface = nullptr;
// ???
self->mouseMask = 0;
self->mouseSerial = -1;
}
void WindowManagerWayland::pointerHandleMotion(void* data, wl_pointer* pointer,
uint32_t time, wl_fixed_t surface_x, wl_fixed_t surface_y) {
Expand Down
1 change: 1 addition & 0 deletions wayland/cc/WindowManagerWayland.hh
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ namespace jwm {
wl_keyboard* keyboard = nullptr;
wl_data_device* dataDevice = nullptr;
wl_data_source* currentSource = nullptr;
uint32_t mouseSerial = -1;
uint32_t keyboardSerial = -1;
libdecor* decorCtx = nullptr;
xkb_context* _xkbContext = nullptr;
Expand Down
69 changes: 57 additions & 12 deletions wayland/cc/WindowWayland.cc
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ WindowWayland::WindowWayland(JNIEnv* env, WindowManagerWayland& windowManager):
_title("")

{
_makeCursors();
}

WindowWayland::~WindowWayland() {
Expand Down Expand Up @@ -148,6 +149,49 @@ bool WindowWayland::resize(int width, int height) {
return true;
}


void WindowWayland::_makeCursors() {

if (theme)
wl_cursor_theme_destroy(theme);
theme = wl_cursor_theme_load(nullptr, 24 * _scale, _windowManager.shm);

}
// cursed
wl_cursor* WindowWayland::_getCursorFor(jwm::MouseCursor cursor) {
switch (cursor) {
case jwm::MouseCursor::ARROW:
// works
return wl_cursor_theme_get_cursor(theme, "default");
case jwm::MouseCursor::CROSSHAIR:
// works
return wl_cursor_theme_get_cursor(theme, "crosshair");
case jwm::MouseCursor::HELP:
// sometimes works?
return wl_cursor_theme_get_cursor(theme, "help");
case jwm::MouseCursor::POINTING_HAND:
// SHOULD work
return wl_cursor_theme_get_cursor(theme, "pointer");
case jwm::MouseCursor::IBEAM:
// doesn't work at all
return wl_cursor_theme_get_cursor(theme, "text");
case jwm::MouseCursor::NOT_ALLOWED:
return wl_cursor_theme_get_cursor(theme, "not-allowed");
case jwm::MouseCursor::WAIT:
return wl_cursor_theme_get_cursor(theme, "watch");
case jwm::MouseCursor::WIN_UPARROW:
return wl_cursor_theme_get_cursor(theme, "up-arrow");
case jwm::MouseCursor::RESIZE_NS:
return wl_cursor_theme_get_cursor(theme, "ns-resize");
case jwm::MouseCursor::RESIZE_WE:
return wl_cursor_theme_get_cursor(theme, "ew-resize");
case jwm::MouseCursor::RESIZE_NESW:
return wl_cursor_theme_get_cursor(theme, "nesw-resize");
case jwm::MouseCursor::RESIZE_NWSE:
return wl_cursor_theme_get_cursor(theme, "nwse-resize");
}
return nullptr;
}
int WindowWayland::getLeft() {
int x, y;
getContentPosition(x, y);
Expand Down Expand Up @@ -221,18 +265,17 @@ void WindowWayland::setVisible(bool isVisible) {
}

void jwm::WindowWayland::setCursor(jwm::MouseCursor cursor) {
if (auto wayCursor = _windowManager._cursors[static_cast<int>(cursor)]) {
wl_surface_attach(_windowManager.cursorSurface,
wl_cursor_image_get_buffer(wayCursor),
0, 0);
wl_surface_commit(_windowManager.cursorSurface);
// TODO: hotspots?
} else {
auto otherCursor = _windowManager._cursors[static_cast<int>(jwm::MouseCursor::ARROW)];
wl_surface_attach(_windowManager.cursorSurface,
wl_cursor_image_get_buffer(otherCursor), 0, 0);
wl_surface_commit(_windowManager.cursorSurface);
}
printf("%s\n", jwm::mouseCursorToStr(cursor));
// ?????
// Doesn't work for higher numbers???
auto wayCursor = _getCursorFor(cursor)->images[0];
wl_surface_attach(_windowManager.cursorSurface,
wl_cursor_image_get_buffer(wayCursor),
0, 0);
wl_surface_set_buffer_scale(_windowManager.cursorSurface, _scale);
wl_surface_commit(_windowManager.cursorSurface);
wl_pointer_set_cursor(_windowManager.pointer, _windowManager.mouseSerial, _windowManager.cursorSurface,
wayCursor->hotspot_x / _scale, wayCursor->hotspot_y / _scale);
}

// what do???
Expand Down Expand Up @@ -378,6 +421,8 @@ void jwm::WindowWayland::_adaptSize(int newWidth, int newHeight) {
)
);
dispatch(eventWindowResize.get());

_makeCursors();
// In Java Wayland doesn't actually cause a frame:
// however decorFrameCommit will cause a redraw anyway.
// Not doing it in wayland lets me not cause an exception on hide.
Expand Down
3 changes: 3 additions & 0 deletions wayland/cc/WindowWayland.hh
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ namespace jwm {
void setCursor(jwm::MouseCursor cursor);
void setLayer(ILayerWayland* layer);

void _makeCursors();
wl_cursor* _getCursorFor(jwm::MouseCursor cursor);
ScreenInfo getScreen();

static void surfaceEnter(void* data, wl_surface* surface, wl_output* output);
Expand Down Expand Up @@ -106,6 +108,7 @@ namespace jwm {
wl_surface* _waylandWindow = nullptr;
libdecor_frame* _frame = nullptr;
Output* _output = nullptr;
wl_cursor_theme* theme = nullptr;

static wl_surface_listener _surfaceListener;

Expand Down

0 comments on commit 460744e

Please sign in to comment.