Skip to content

Commit

Permalink
refactor: native classes and interfaces
Browse files Browse the repository at this point in the history
  • Loading branch information
litwak913 committed Feb 18, 2025
1 parent b6e8210 commit a7247fb
Show file tree
Hide file tree
Showing 12 changed files with 321 additions and 278 deletions.
73 changes: 73 additions & 0 deletions core/src/cn/harryh/arkpets/natives/CoreGraphicsHelper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package cn.harryh.arkpets.natives;

import com.sun.jna.Library;
import com.sun.jna.Native;
import com.sun.jna.Structure;
import com.sun.jna.platform.mac.CoreFoundation;


public class CoreGraphicsHelper {
public static CoreFoundation.CFStringRef kCGWindowNumber;
public static CoreFoundation.CFStringRef kCGWindowLayer;
public static CoreFoundation.CFStringRef kCGWindowBounds;
public static CoreFoundation.CFStringRef kCGWindowName;
public static CoreFoundation.CFStringRef kCGWindowOwnerName;

public static final int kCGWindowListExcludeDesktopElements = (1 << 4);
public static final int kCGWindowListOptionOnScreenOnly = 1;

public static final int NSStatusWindowLevel = 25;
public static final int NSNormalWindowLevel = 0;

public static void initCG() {
kCGWindowNumber = CoreFoundation.CFStringRef.createCFString("kCGWindowNumber");
kCGWindowBounds = CoreFoundation.CFStringRef.createCFString("kCGWindowBounds");
kCGWindowLayer = CoreFoundation.CFStringRef.createCFString("kCGWindowLayer");
kCGWindowName = CoreFoundation.CFStringRef.createCFString("kCGWindowName");
kCGWindowOwnerName = CoreFoundation.CFStringRef.createCFString("kCGWindowOwnerName");
}

public static void freeCG() {
kCGWindowNumber.release();
kCGWindowLayer.release();
kCGWindowName.release();
kCGWindowBounds.release();
kCGWindowOwnerName.release();
}

public interface CGInterface extends Library {
CGInterface INSTANCE = Native.load("CoreGraphics", CGInterface.class);

CoreFoundation.CFArrayRef CGWindowListCopyWindowInfo(int option, int relativeToWindow);

CoreFoundation.CFArrayRef CGWindowListCreateDescriptionFromArray(CoreFoundation.CFArrayRef windowArray);

boolean CGRectMakeWithDictionaryRepresentation(CoreFoundation.CFDictionaryRef dict, CGRect.ByReference rect);

CoreFoundation.CFDictionaryRef CGSessionCopyCurrentDictionary();
}

@Structure.FieldOrder({"origin", "size"})
public static class CGRect extends Structure {
public CGPoint origin;
public CGSize size;

public static class ByReference extends CGRect implements Structure.ByReference {
}

public static class ByValue extends CGRect implements Structure.ByValue {
}
}

@Structure.FieldOrder({"x", "y"})
public static class CGPoint extends Structure {
public double x;
public double y;
}

@Structure.FieldOrder({"width", "height"})
public static class CGSize extends Structure {
public double width;
public double height;
}
}
59 changes: 59 additions & 0 deletions core/src/cn/harryh/arkpets/natives/KWinDBusInterface.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package cn.harryh.arkpets.natives;

import org.freedesktop.dbus.Struct;
import org.freedesktop.dbus.annotations.DBusInterfaceName;
import org.freedesktop.dbus.annotations.Position;
import org.freedesktop.dbus.interfaces.DBusInterface;
import org.freedesktop.dbus.types.UInt32;

import java.util.List;


@DBusInterfaceName("org.kde.KWin.ArkPets")
public interface KWinDBusInterface extends DBusInterface {
void MoveResize(String uuid, int x, int y, UInt32 width, UInt32 height);

void Activate(String uuid);

void Above(String uuid, boolean enable);

void Stick(String uuid, boolean enable);

List<DetailsStruct> List();

DetailsStruct Details(String winid);

boolean IsActive(String uuid);

UInt32 Version();

class DetailsStruct extends Struct {
@Position(0)
public final int x;
@Position(1)
public final int y;
@Position(2)
public final UInt32 w;
@Position(3)
public final UInt32 h;
@Position(4)
public final String title;
@Position(5)
public final String wclass;
@Position(6)
public final boolean visible;
@Position(7)
public final String id;

public DetailsStruct(int x, int y, UInt32 w, UInt32 h, String title, String wClass, boolean visible, String id) {
this.x = x;
this.y = y;
this.w = w;
this.h = h;
this.title = title;
this.wclass = wClass;
this.visible = visible;
this.id = id;
}
}
}
59 changes: 59 additions & 0 deletions core/src/cn/harryh/arkpets/natives/MutterDBusInterface.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package cn.harryh.arkpets.natives;

import org.freedesktop.dbus.Struct;
import org.freedesktop.dbus.annotations.DBusInterfaceName;
import org.freedesktop.dbus.annotations.Position;
import org.freedesktop.dbus.interfaces.DBusInterface;
import org.freedesktop.dbus.types.UInt32;

import java.util.List;


@DBusInterfaceName("org.gnome.Shell.Extensions.ArkPets")
public interface MutterDBusInterface extends DBusInterface {
void MoveResize(UInt32 winid, int x, int y, UInt32 width, UInt32 height);

void Activate(UInt32 winid);

void Above(UInt32 winid, boolean above);

void Stick(UInt32 winid, boolean stick);

List<DetailsStruct> List();

DetailsStruct Details(UInt32 winid);

boolean IsActive(UInt32 winid);

String Version();

class DetailsStruct extends Struct {
@Position(0)
public final int x;
@Position(1)
public final int y;
@Position(2)
public final UInt32 w;
@Position(3)
public final UInt32 h;
@Position(4)
public final String title;
@Position(5)
public final String wclass;
@Position(6)
public final boolean visible;
@Position(7)
public final UInt32 id;

public DetailsStruct(int x, int y, UInt32 w, UInt32 h, String title, String wClass, boolean visible, UInt32 id) {
this.x = x;
this.y = y;
this.w = w;
this.h = h;
this.title = title;
this.wclass = wClass;
this.visible = visible;
this.id = id;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package cn.harryh.arkpets.utils;
package cn.harryh.arkpets.natives;

import com.sun.jna.*;

Expand All @@ -7,9 +7,9 @@
import java.util.Map;


public class ObjCWrapper {
private static Map<String, Pointer> selMap = new HashMap<>();
private static Map<String, Pointer> clsMap = new HashMap<>();
public class ObjCHelper {
private static final Map<String, Pointer> selMap = new HashMap<>();
private static final Map<String, Pointer> clsMap = new HashMap<>();
public static Function msgSend;
public static Function msgSend_stret;
private static Method lwtOnMain;
Expand Down
64 changes: 6 additions & 58 deletions core/src/cn/harryh/arkpets/platform/KWinHWndCtrl.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
package cn.harryh.arkpets.platform;

import cn.harryh.arkpets.natives.KWinDBusInterface;
import cn.harryh.arkpets.utils.Logger;
import org.freedesktop.dbus.Struct;
import org.freedesktop.dbus.annotations.DBusInterfaceName;
import org.freedesktop.dbus.annotations.Position;
import org.freedesktop.dbus.connections.impl.DBusConnection;
import org.freedesktop.dbus.connections.impl.DBusConnectionBuilder;
import org.freedesktop.dbus.exceptions.DBusException;
import org.freedesktop.dbus.interfaces.DBusInterface;
import org.freedesktop.dbus.types.UInt32;

import java.io.IOException;
Expand All @@ -18,11 +15,11 @@

public class KWinHWndCtrl extends HWndCtrl {
protected final String hWnd;
protected DetailsStruct details;
protected KWinDBusInterface.DetailsStruct details;
private static DBusConnection dBusConnection;
private static ArkPetsInterface dBusInterface;
private static KWinDBusInterface dBusInterface;

protected KWinHWndCtrl(DetailsStruct details) {
protected KWinHWndCtrl(KWinDBusInterface.DetailsStruct details) {
super(details.title, new WindowRect(details.y, details.y + details.h.intValue(), details.x, details.x + details.w.intValue()));
this.hWnd = details.id;
this.details = details;
Expand Down Expand Up @@ -87,7 +84,7 @@ protected static void init() {
try {
dBusConnection = DBusConnectionBuilder.forSessionBus().build();
Logger.info("System", "Connected to DBus");
dBusInterface = dBusConnection.getRemoteObject("org.kde.KWin", "/ArkPets", ArkPetsInterface.class);
dBusInterface = dBusConnection.getRemoteObject("org.kde.KWin", "/ArkPets", KWinDBusInterface.class);
Logger.info("System", "KDE Integration plugin version " + dBusInterface.Version());
} catch (DBusException e) {
throw new RuntimeException(e);
Expand Down Expand Up @@ -120,7 +117,7 @@ protected static List<KWinHWndCtrl> getWindowList(boolean onlyVisible) {
}

protected static KWinHWndCtrl getTopmostWindow() {
List<DetailsStruct> list = dBusInterface.List();
List<KWinDBusInterface.DetailsStruct> list = dBusInterface.List();
return new KWinHWndCtrl(list.get(list.size() - 1));
}

Expand All @@ -137,53 +134,4 @@ public boolean equals(Object o) {
public int hashCode() {
return hWnd.hashCode();
}

@DBusInterfaceName("org.kde.KWin.ArkPets")
private interface ArkPetsInterface extends DBusInterface {
void MoveResize(String uuid, int x, int y, UInt32 width, UInt32 height);

void Activate(String uuid);

void Above(String uuid, boolean enable);

void Stick(String uuid, boolean enable);

List<DetailsStruct> List();

DetailsStruct Details(String winid);

boolean IsActive(String uuid);

UInt32 Version();
}

public static class DetailsStruct extends Struct {
@Position(0)
public final int x;
@Position(1)
public final int y;
@Position(2)
public final UInt32 w;
@Position(3)
public final UInt32 h;
@Position(4)
public final String title;
@Position(5)
public final String wclass;
@Position(6)
public final boolean visible;
@Position(7)
public final String id;

public DetailsStruct(int x, int y, UInt32 w, UInt32 h, String title, String wClass, boolean visible, String id) {
this.x = x;
this.y = y;
this.w = w;
this.h = h;
this.title = title;
this.wclass = wClass;
this.visible = visible;
this.id = id;
}
}
}
Loading

0 comments on commit a7247fb

Please sign in to comment.