Skip to content

Commit

Permalink
Support Linux(ibus/fcitx5).
Browse files Browse the repository at this point in the history
  • Loading branch information
LitnhJacuzzi committed Nov 10, 2024
1 parent 158be6a commit 395b12a
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
public final class IMManager {
private static final PlatformIMManager INSTANCE;

public sealed interface PlatformIMManager permits IMManagerWindows, IMManagerMac, IMManagerStub {
public sealed interface PlatformIMManager permits IMManagerWindows, IMManagerMac, IMManagerLinux, IMManagerStub {

void setState(boolean on);

Expand Down Expand Up @@ -39,6 +39,8 @@ public static boolean getState() {
INSTANCE = new IMManagerWindows();
}else if(Platform.isMac()) {
INSTANCE = new IMManagerMac();
}else if(Platform.isLinux()) {
INSTANCE = new IMManagerLinux();
}else {
Common.LOGGER.warn("Unsupported platform, using stub");
INSTANCE = new IMManagerStub();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package io.github.reserveword.imblocker.common;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

final class IMManagerLinux implements IMManager.PlatformIMManager {
private LinuxIMFramework imFramework = LinuxIMFramework.IBUS;
private static boolean state = false;

@Override
public void setState(boolean on) {
if(state != on) {
checkIMFramework();
imFramework.setState(on);
IMManagerLinux.state = on;
}
}

@Override
public void setImmOnState(boolean isEN) {

}

@Override
public void syncState() {

}

@Override
public boolean getState() {
return state;
}

private void checkIMFramework() {
String fcitx5State = "";
try {
Process process = Runtime.getRuntime().exec("pgrep -l fcitx5");
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
fcitx5State = reader.readLine();
} catch (IOException e) {
e.printStackTrace();
}

imFramework = fcitx5State == null ? LinuxIMFramework.IBUS : LinuxIMFramework.FCITX5;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package io.github.reserveword.imblocker.common;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public enum LinuxIMFramework {
IBUS("ibus engine", "ibus engine", "libpinyin", "xkb:us::eng", "libpinyin"),
FCITX5("fcitx5-remote", "fcitx5-remote", "-o", "-c", "2");

private final String setStateCommand;
private final String[] getStateCommand;
private final String onArgName;
private final String offArgName;
private final String onStateName;

LinuxIMFramework(String setStateCommand, String getStateCommand,
String onArgName, String offArgName, String onStateName) {
this.setStateCommand = setStateCommand;
this.getStateCommand = getStateCommand.split(" ");
this.onArgName = onArgName;
this.offArgName = offArgName;
this.onStateName = onStateName;
}

public void setState(boolean state) {
String[] command = (setStateCommand + " " + (state ? onArgName : offArgName)).split(" ");
try {
Runtime.getRuntime().exec(command);
} catch (IOException e) {
e.printStackTrace();
}
}

public boolean getState() {
String stateName = "";
try {
Process process = Runtime.getRuntime().exec(getStateCommand);
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
stateName = reader.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return stateName != null && stateName.equals(onStateName);
}
}
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ minecraft_version_range=1.19.4+
# mod
mod_id=imblocker
mod_name=IMBlocker
mod_version=4.0.11
mod_version=4.0.12
mod_logo=imblocker.png
mod_description=Blocks Input Methods when you're not using it.\\n在你不打字的时候自动关闭输入法
mod_url=https://github.com/reserveword/IMBlocker
Expand Down

0 comments on commit 395b12a

Please sign in to comment.