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
75f164f
commit c3b3f12
Showing
34 changed files
with
239 additions
and
63 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
2 changes: 1 addition & 1 deletion
2
...serveword/imblocker/AxiomGuiAccessor.java → ...rd/imblocker/common/AxiomGuiAccessor.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
2 changes: 1 addition & 1 deletion
2
.../github/reserveword/imblocker/Common.java → .../reserveword/imblocker/common/Common.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
2 changes: 1 addition & 1 deletion
2
.../github/reserveword/imblocker/Config.java → .../reserveword/imblocker/common/Config.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
2 changes: 1 addition & 1 deletion
2
...rd/imblocker/FocusableWidgetAccessor.java → ...ocker/common/FocusableWidgetAccessor.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
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
43 changes: 43 additions & 0 deletions
43
common/src/main/java/io/github/reserveword/imblocker/common/IMManager.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,43 @@ | ||
package io.github.reserveword.imblocker.common; | ||
|
||
import com.sun.jna.Platform; | ||
|
||
public final class IMManager { | ||
private static final PlatformIMManager INSTANCE; | ||
|
||
public sealed interface PlatformIMManager permits IMManagerWindows, IMManagerMac, IMManagerStub { | ||
|
||
void setState(boolean on); | ||
|
||
void setImmOnState(boolean isEN); | ||
|
||
void syncState(); | ||
|
||
boolean getState(); | ||
} | ||
|
||
private IMManager() {} | ||
|
||
public static void setState(boolean on) { | ||
INSTANCE.setState(on); | ||
} | ||
|
||
public static void setImmOnState(boolean isEN) { | ||
INSTANCE.setImmOnState(isEN); | ||
} | ||
|
||
public static boolean getState() { | ||
return INSTANCE.getState(); | ||
} | ||
|
||
static { | ||
if(Platform.isWindows()) { | ||
INSTANCE = new IMManagerWindows(); | ||
}else if(Platform.isMac()) { | ||
INSTANCE = new IMManagerMac(); | ||
}else { | ||
Common.LOGGER.warn("Unsupported platform, using stub"); | ||
INSTANCE = new IMManagerStub(); | ||
} | ||
} | ||
} |
85 changes: 85 additions & 0 deletions
85
common/src/main/java/io/github/reserveword/imblocker/common/IMManagerMac.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,85 @@ | ||
package io.github.reserveword.imblocker.common; | ||
|
||
import com.sun.jna.Callback; | ||
import com.sun.jna.Library; | ||
import com.sun.jna.Native; | ||
import com.sun.jna.Pointer; | ||
|
||
import ca.weblite.objc.Runtime; | ||
import ca.weblite.objc.RuntimeUtils; | ||
|
||
final class IMManagerMac implements IMManager.PlatformIMManager { | ||
private static boolean state = false; | ||
static private final Pointer viewClass = Runtime.INSTANCE.objc_getClass("GLFWContentView"); | ||
static private Pointer view = null; | ||
static private final InterpretKeyEventsCallback Imp; | ||
static private final InterpretKeyEventsCallback NewImp; | ||
|
||
|
||
static { | ||
// see https://github.com/glfw/glfw/blob/b4c3ef9d0fdf46845f3e81e5d989dab06e71e6c1/src/cocoa_window.m#L571 | ||
// Replacing the method dynamically to determine whether to send text based on state | ||
// see reference for objc_runtime's dynamic manipulation at https://developer.apple.com/documentation/objectivec/objective-c_runtime | ||
var selector = RuntimeUtils.sel("interpretKeyEvents:"); | ||
var method = Runtime.INSTANCE.class_getInstanceMethod(viewClass, selector); | ||
Imp = ObjC.INSTANCE.method_getImplementation(method); | ||
NewImp = (self, sel, eventArray) -> { | ||
if (view == null) view = self; | ||
if (!state) { | ||
var textInputContext = RuntimeUtils.cls("NSTextInputContext"); | ||
var current = RuntimeUtils.msgPointer(textInputContext, "currentInputContext"); | ||
RuntimeUtils.msg(current, "discardMarkedText"); | ||
return; | ||
} | ||
Imp.invoke(self, sel, eventArray); | ||
}; | ||
ObjC.INSTANCE.class_replaceMethod(viewClass, selector, NewImp, "v@:@"); | ||
} | ||
|
||
/** | ||
* @see <a href="https://developer.apple.com/documentation/objectivec/objective-c_runtime">Apple Developer Documentation for objc_runtime:</a> | ||
*/ | ||
private interface ObjC extends Library { | ||
ObjC INSTANCE = Native.load("objc.A", ObjC.class); | ||
|
||
void class_replaceMethod(Pointer cls, Pointer selector, InterpretKeyEventsCallback imp, String types); | ||
|
||
InterpretKeyEventsCallback method_getImplementation(Pointer selector); | ||
} | ||
|
||
/** | ||
* The underlying native type is IMP, which should be a function pointer to the implementation of interpretKeyEvents: | ||
* @see <a href="https://developer.apple.com/documentation/objectivec/objective-c_runtime/imp">Documentation for IMP</a> | ||
* @see <a href="https://developer.apple.com/documentation/appkit/nsresponder/1531599-interpretkeyevents?language=objc">Documentation for interpretKeyEvents:</a> | ||
*/ | ||
private interface InterpretKeyEventsCallback extends Callback { | ||
/** | ||
* @param self "this" pointer for NSObject | ||
* @param selector selector for interpretKeyEvents: | ||
* @param eventArray an array of NSEvent objects | ||
*/ | ||
void invoke(Pointer self, Pointer selector, Pointer eventArray); | ||
} | ||
|
||
@Override | ||
public void setState(boolean on) { | ||
if(state != on) { | ||
state = on; | ||
} | ||
} | ||
|
||
@Override | ||
public void setImmOnState(boolean isEN) { | ||
|
||
} | ||
|
||
@Override | ||
public void syncState() { | ||
|
||
} | ||
|
||
@Override | ||
public boolean getState() { | ||
return state; | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
common/src/main/java/io/github/reserveword/imblocker/common/IMManagerStub.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,23 @@ | ||
package io.github.reserveword.imblocker.common; | ||
|
||
final class IMManagerStub implements IMManager.PlatformIMManager { | ||
private static boolean state = true; | ||
|
||
@Override | ||
public void setState(boolean on) { | ||
if(state != on) { | ||
state = on; | ||
} | ||
} | ||
|
||
@Override | ||
public void setImmOnState(boolean isEN) {} | ||
|
||
@Override | ||
public void syncState() {} | ||
|
||
@Override | ||
public boolean getState() { | ||
return state; | ||
} | ||
} |
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
2 changes: 1 addition & 1 deletion
2
...b/reserveword/imblocker/FabricCommon.java → ...veword/imblocker/fabric/FabricCommon.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
5 changes: 4 additions & 1 deletion
5
...b/reserveword/imblocker/FabricConfig.java → ...veword/imblocker/fabric/FabricConfig.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
5 changes: 3 additions & 2 deletions
5
...serveword/imblocker/FabricScreenInfo.java → ...rd/imblocker/fabric/FabricScreenInfo.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
4 changes: 3 additions & 1 deletion
4
...eserveword/imblocker/IMBlockerFabric.java → ...ord/imblocker/fabric/IMBlockerFabric.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
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
2 changes: 1 addition & 1 deletion
2
...word/imblocker/mixin/ChatScreenMixin.java → ...blocker/fabric/mixin/ChatScreenMixin.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
7 changes: 4 additions & 3 deletions
7
...imblocker/mixin/CottonTextFieldMixin.java → ...er/fabric/mixin/CottonTextFieldMixin.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
4 changes: 2 additions & 2 deletions
4
...imblocker/mixin/EmiSearchWidgetMixin.java → ...er/fabric/mixin/EmiSearchWidgetMixin.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
7 changes: 4 additions & 3 deletions
7
...rd/imblocker/mixin/FtbTextFieldMixin.java → ...ocker/fabric/mixin/FtbTextFieldMixin.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
5 changes: 3 additions & 2 deletions
5
...eword/imblocker/mixin/FtbWidgetMixin.java → ...mblocker/fabric/mixin/FtbWidgetMixin.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
Oops, something went wrong.