forked from reserveword/IMBlocker
-
Notifications
You must be signed in to change notification settings - Fork 0
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
158be6a
commit 395b12a
Showing
4 changed files
with
97 additions
and
2 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
47 changes: 47 additions & 0 deletions
47
common/src/main/java/io/github/reserveword/imblocker/common/IMManagerLinux.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,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; | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
common/src/main/java/io/github/reserveword/imblocker/common/LinuxIMFramework.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,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); | ||
} | ||
} |
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