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

X11: setIconData and setClassHint #294

Merged
merged 3 commits into from
Sep 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions examples/dashboard/java/Example.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
import java.util.function.*;
import java.util.stream.*;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;

public class Example implements Consumer<Event> {
public static int PADDING = 10;
Expand Down Expand Up @@ -82,6 +85,17 @@ public Example() {
case MACOS -> {
window.setIcon(new File("examples/dashboard/resources/macos.icns"));
}
case X11 -> {
((WindowX11) window).setClassHint("jwm-dashboard-example"); // allows OS-wide identification of the window (e.g. icon themes, .desktop files)
try {
Bitmap i = Bitmap.makeFromImage(Image.makeDeferredFromEncodedBytes(Files.readAllBytes(Path.of("examples/dashboard/resources/linux/icon_48x48.png"))));
ImageInfo info = i.getImageInfo();

((WindowX11) window).setIconData(info.getWidth(), info.getHeight(), i.readPixels());
} catch (IOException e) {
e.printStackTrace();
}
}
}

window.setVisible(true);
Expand Down
1 change: 1 addition & 0 deletions linux/cc/WindowManagerX11.hh
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ namespace jwm {
DEFINE_ATOM(_MOTIF_WM_HINTS);
DEFINE_ATOM(_NET_WM_STATE);
DEFINE_ATOM(_NET_WM_NAME);
DEFINE_ATOM(_NET_WM_ICON);
DEFINE_ATOM(_NET_WM_STATE_MAXIMIZED_VERT);
DEFINE_ATOM(_NET_WM_STATE_MAXIMIZED_HORZ);
DEFINE_ATOM(_NET_FRAME_EXTENTS);
Expand Down
60 changes: 56 additions & 4 deletions linux/cc/WindowX11.cc
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,39 @@ void WindowX11::setTitle(const std::string& title) {
title.length());
}

void WindowX11::setClass(const std::string& name, const std::string& appClass) {
XClassHint *classHint = XAllocClassHint();
if (classHint) {
classHint->res_name = const_cast<char*>(name.c_str());
classHint->res_class = const_cast<char*>(appClass.c_str());
XSetClassHint(_windowManager.getDisplay(), _x11Window, classHint);
XFree(classHint);
}
}

void WindowX11::setIconData(int width, int height, const unsigned char* argb) {
size_t size = width * height;
size_t count = size + 2;
std::unique_ptr<long[]> buffer{new long[count]};
buffer[0] = width;
buffer[1] = height;
for (size_t i = 0; i < size; i++) {
uint32_t c = static_cast<uint32_t>(argb[i*4]) |
static_cast<uint32_t>(argb[i*4 + 1]) << 8 |
static_cast<uint32_t>(argb[i*4 + 2]) << 16 |
static_cast<uint32_t>(argb[i*4 + 3]) << 24;
buffer[i+2] = c;
}
XChangeProperty(_windowManager.getDisplay(),
_x11Window,
_windowManager.getAtoms()._NET_WM_ICON,
XA_CARDINAL,
32,
PropModeReplace,
reinterpret_cast<const unsigned char*>(buffer.get()),
count);
}

void WindowX11::setTitlebarVisible(bool isVisible) {
MotifHints motifHints = {0};

Expand Down Expand Up @@ -555,15 +588,34 @@ extern "C" JNIEXPORT void JNICALL Java_io_github_humbleui_jwm_WindowX11__1nClose
jwm::WindowX11* instance = reinterpret_cast<jwm::WindowX11*>(jwm::classes::Native::fromJava(env, obj));
instance->close();
}

static std::string bytesToString(JNIEnv* env, jbyteArray arr) {
jbyte* bytes = env->GetByteArrayElements(arr, nullptr);
std::string res = { bytes, bytes + env->GetArrayLength(arr) };
env->ReleaseByteArrayElements(arr, bytes, 0);
return res;
}

extern "C" JNIEXPORT void JNICALL Java_io_github_humbleui_jwm_WindowX11__1nSetTitle
(JNIEnv* env, jobject obj, jbyteArray title) {
jwm::WindowX11* instance = reinterpret_cast<jwm::WindowX11*>(jwm::classes::Native::fromJava(env, obj));
instance->setTitle(bytesToString(env, title));
}

jbyte* bytes = env->GetByteArrayElements(title, nullptr);
std::string titleS = { bytes, bytes + env->GetArrayLength(title) };
env->ReleaseByteArrayElements(title, bytes, 0);
extern "C" JNIEXPORT void JNICALL Java_io_github_humbleui_jwm_WindowX11__1nSetClassHint
(JNIEnv* env, jobject obj, jbyteArray name, jbyteArray appClass) {
jwm::WindowX11* instance = reinterpret_cast<jwm::WindowX11*>(jwm::classes::Native::fromJava(env, obj));

instance->setClass(bytesToString(env, name), bytesToString(env, appClass));
}

extern "C" JNIEXPORT void JNICALL Java_io_github_humbleui_jwm_WindowX11__1nSetIconData
(JNIEnv* env, jobject obj, jint width, jint height, jbyteArray data) {
jwm::WindowX11* instance = reinterpret_cast<jwm::WindowX11*>(jwm::classes::Native::fromJava(env, obj));

instance->setTitle(titleS);
jbyte* bytes = env->GetByteArrayElements(data, nullptr);
instance->setIconData(width, height, reinterpret_cast<const unsigned char*>(bytes));
env->ReleaseByteArrayElements(data, bytes, 0);
}

extern "C" JNIEXPORT void JNICALL Java_io_github_humbleui_jwm_WindowX11__1nSetTitlebarVisible
Expand Down
2 changes: 2 additions & 0 deletions linux/cc/WindowX11.hh
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ namespace jwm {
return _isRedrawRequested;
}
void setTitle(const std::string& title);
void setClass(const std::string& name, const std::string& class_);
void setIconData(int width, int height, const unsigned char* argb);
void setTitlebarVisible(bool isVisible);

void maximize();
Expand Down
39 changes: 39 additions & 0 deletions linux/java/WindowX11.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,43 @@ public Window setIcon(File icon) {
return this;
}

/**
* <p>Set the WM_CLASS window property.</p>
*
* @param appClass application class
* @return this
*/
public Window setClassHint(String appClass) {
setClassHint(appClass, appClass);
return this;
}

public Window setClassHint(String name, String appClass) {
assert _onUIThread() : "Should be run on UI thread";
try {
_nSetClassHint(name.getBytes("UTF-8"), appClass.getBytes("UTF-8"));
} catch (UnsupportedEncodingException ignored) {}
return this;
}

/**
* <p>Set window icon from raw image bytes.</p>
*
* <p>{@code data} must have a length of {@code width * height * 4}, representing per-pixel ARGB data.</p>
*
* @param width icon width in pixels
* @param height icon height in pixels
* @param data icon image data
* @return this
*/
@NotNull @Contract("-> this")
public Window setIconData(int width, int height, byte[] data) {
assert _onUIThread() : "Should be run on UI thread";
assert data.length == width*height*4 : "Incorrect icon data array length";
_nSetIconData(width, height, data);
return this;
}

@Override
public Window setTitlebarVisible(boolean value) {
_nSetTitlebarVisible(value);
Expand Down Expand Up @@ -226,6 +263,8 @@ public boolean isFullScreen() {
@ApiStatus.Internal public native void _nMinimize();
@ApiStatus.Internal public native void _nRestore();
@ApiStatus.Internal public native void _nSetTitle(byte[] title);
@ApiStatus.Internal public native void _nSetClassHint(byte[] name, byte[] appClass);
@ApiStatus.Internal public native void _nSetIconData(int width, int height, byte[] data);
@ApiStatus.Internal public native void _nSetTitlebarVisible(boolean isVisible);
@ApiStatus.Internal public native void _nSetFullScreen(boolean isFullScreen);
@ApiStatus.Internal public native boolean _nIsFullScreen();
Expand Down