-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d6e6d5f
commit cf28678
Showing
7 changed files
with
264 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
common/src/main/java/moe/caramel/chat/driver/arch/wayland/Driver_Wayland.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package moe.caramel.chat.driver.arch.wayland; | ||
|
||
import com.sun.jna.Callback; | ||
import com.sun.jna.Library; | ||
import com.sun.jna.Pointer; | ||
import com.sun.jna.WString; | ||
|
||
/** | ||
* caramelChat Wayland Driver | ||
*/ | ||
public interface Driver_Wayland extends Library { | ||
|
||
/** | ||
* Initialize caramelChat Wayland Driver. | ||
* | ||
* @param wlDisplay Wayland Display | ||
* @param preEdit PreEdit Callback | ||
* @param preEditNull PreEdit Null Callback | ||
* @param done Done Callback | ||
* @param rect Rect Callback | ||
* @param log Log Info Callback | ||
* @param error Log Error Callback | ||
* @param debug Log Debug Callback | ||
*/ | ||
void initialize( | ||
final long wlDisplay, | ||
final PreeditCallback preEdit, | ||
final PreeditNullCallback preEditNull, | ||
final DoneCallback done, | ||
final RectCallback rect, | ||
final LogInfoCallback log, | ||
final LogErrorCallback error, | ||
final LogDebugCallback debug | ||
); | ||
|
||
/** | ||
* Set whether to focus or not. | ||
* | ||
* @param flag focus | ||
*/ | ||
void setFocus(final boolean flag); | ||
|
||
// ================================ | ||
|
||
interface PreeditCallback extends Callback { | ||
void invoke(final WString string); | ||
} | ||
|
||
interface PreeditNullCallback extends Callback { | ||
void invoke(); | ||
} | ||
|
||
interface DoneCallback extends Callback { | ||
void invoke(final WString string); | ||
} | ||
|
||
interface RectCallback extends Callback { | ||
int invoke(final Pointer pointer); | ||
} | ||
|
||
interface LogInfoCallback extends Callback { | ||
void invoke(final String log); | ||
} | ||
|
||
interface LogErrorCallback extends Callback { | ||
void invoke(final String log); | ||
} | ||
|
||
interface LogDebugCallback extends Callback { | ||
void invoke(final String log); | ||
} | ||
} |
108 changes: 108 additions & 0 deletions
108
common/src/main/java/moe/caramel/chat/driver/arch/wayland/WaylandController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
package moe.caramel.chat.driver.arch.wayland; | ||
|
||
import com.mojang.blaze3d.platform.Window; | ||
import com.sun.jna.Native; | ||
import moe.caramel.chat.Main; | ||
import moe.caramel.chat.controller.ScreenController; | ||
import moe.caramel.chat.driver.IController; | ||
import moe.caramel.chat.driver.IOperator; | ||
import moe.caramel.chat.util.ModLogger; | ||
import moe.caramel.chat.wrapper.AbstractIMEWrapper; | ||
import net.minecraft.client.Minecraft; | ||
import net.minecraft.client.gui.screens.Screen; | ||
import org.lwjgl.glfw.GLFWNativeWayland; | ||
|
||
/** | ||
* Wayland Controller | ||
*/ | ||
public final class WaylandController implements IController { | ||
|
||
static WaylandOperator focused; | ||
|
||
private final Driver_Wayland driver; | ||
|
||
/** | ||
* Create Wayland Controller | ||
*/ | ||
public WaylandController() { | ||
ModLogger.log("[Native] Load the Wayland Controller."); | ||
this.driver = Native.load(Main.copyLibrary("libcaramelchatwl.so"), Driver_Wayland.class); | ||
|
||
this.driver.initialize( | ||
// Wayland Display Id | ||
GLFWNativeWayland.glfwGetWaylandDisplay(), | ||
// PreEdit | ||
(str) -> { | ||
if (focused != null) { | ||
ModLogger.debug("[Native|Java] Preedit Callback (" + str.toString() + ")"); | ||
focused.getWrapper().appendPreviewText(str.toString()); | ||
} | ||
}, | ||
// PreEdit (Null) | ||
() -> { | ||
if (focused != null) { | ||
ModLogger.debug("[Native|Java] Preedit Null Callback"); | ||
focused.getWrapper().appendPreviewText(""); | ||
} | ||
}, | ||
// Done | ||
(str) -> { | ||
if (focused != null) { | ||
ModLogger.debug("[Native|Java] Done Callback (" + str.toString() + ")"); | ||
focused.getWrapper().insertText(str.toString()); | ||
} | ||
}, | ||
// Rect | ||
(rect) -> { | ||
if (focused != null) { | ||
ModLogger.debug("[Native|Java] Rect Callback"); | ||
final Window window = Minecraft.getInstance().getWindow(); | ||
final int osScale = (window.getHeight() / window.getScreenHeight()); | ||
|
||
final float[] buff = focused.getWrapper().getRect().copy(); | ||
final float factor = (float) window.getGuiScale(); | ||
buff[0] *= factor; | ||
buff[1] *= factor; | ||
buff[2] *= factor; | ||
buff[3] *= factor; | ||
|
||
buff[1] /= osScale; | ||
|
||
rect.write(0, buff, 0, 4); | ||
return 0; | ||
} | ||
return 1; | ||
}, | ||
// Info | ||
(log) -> ModLogger.log("[Native|C] " + log), | ||
// Error | ||
(log) -> ModLogger.error("[Native|C] " + log), | ||
// Debug | ||
(log) -> ModLogger.debug("[Native|C] " + log) | ||
); | ||
|
||
this.setFocus(false); | ||
} | ||
|
||
@Override | ||
public IOperator createOperator(final AbstractIMEWrapper wrapper) { | ||
return new WaylandOperator(this, wrapper); | ||
} | ||
|
||
@Override | ||
public void changeFocusedScreen(final Screen screen) { | ||
if (screen instanceof ScreenController) { | ||
return; | ||
} | ||
|
||
if (WaylandController.focused != null) { | ||
WaylandController.focused.setFocused(false); | ||
WaylandController.focused = null; | ||
} | ||
} | ||
|
||
@Override | ||
public void setFocus(final boolean focus) { | ||
this.driver.setFocus(focus); | ||
} | ||
} |
59 changes: 59 additions & 0 deletions
59
common/src/main/java/moe/caramel/chat/driver/arch/wayland/WaylandOperator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package moe.caramel.chat.driver.arch.wayland; | ||
|
||
import moe.caramel.chat.driver.IController; | ||
import moe.caramel.chat.driver.IOperator; | ||
import moe.caramel.chat.util.ModLogger; | ||
import moe.caramel.chat.wrapper.AbstractIMEWrapper; | ||
|
||
/** | ||
* Wayland IME Operator | ||
*/ | ||
public final class WaylandOperator implements IOperator { | ||
|
||
private final WaylandController controller; | ||
private final AbstractIMEWrapper wrapper; | ||
private boolean nowFocused; | ||
|
||
public WaylandOperator(final WaylandController controller, final AbstractIMEWrapper wrapper) { | ||
this.controller = controller; | ||
this.wrapper = wrapper; | ||
} | ||
|
||
/** | ||
* Gets the IME wrapper. | ||
* | ||
* @return IME wrapper | ||
*/ | ||
public AbstractIMEWrapper getWrapper() { | ||
return wrapper; | ||
} | ||
|
||
@Override | ||
public IController getController() { | ||
return controller; | ||
} | ||
|
||
@Override | ||
public void setFocused(final boolean focus) { | ||
if (focus == this.nowFocused) { | ||
return; | ||
} | ||
|
||
ModLogger.debug("[Native|Java] Called setFocused: " + focus); | ||
this.nowFocused = focus; | ||
|
||
if (focus) { | ||
WaylandController.focused = this; | ||
this.controller.setFocus(true); | ||
} else if (WaylandController.focused == this) { | ||
this.wrapper.insertText(""); | ||
WaylandController.focused = null; | ||
this.controller.setFocus(false); | ||
} | ||
} | ||
|
||
@Override | ||
public boolean isFocused() { | ||
return nowFocused; | ||
} | ||
} |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters