Skip to content

Instantly share code, notes, and snippets.

@allquixotic
Created June 6, 2019 21:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save allquixotic/faf6d2af6bbc1c2f681c41066cf66506 to your computer and use it in GitHub Desktop.
Save allquixotic/faf6d2af6bbc1c2f681c41066cf66506 to your computer and use it in GitHub Desktop.
ByteBuddy Java Agent to force Raritan VKC to fullscreen on macOS
package agent;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.InputEvent;
import java.beans.PropertyChangeListener;
import java.lang.instrument.Instrumentation;
import javax.swing.Action;
import javax.swing.JFrame;
import javax.swing.KeyStroke;
import net.bytebuddy.ByteBuddy;
import net.bytebuddy.agent.builder.AgentBuilder;
import net.bytebuddy.asm.Advice;
import net.bytebuddy.implementation.Implementation;
import net.bytebuddy.matcher.ElementMatchers;
public class Agent {
public static void premain(String agentArgs, Instrumentation inst) {
System.err.println("In premain");
final ByteBuddy byteBuddy = new ByteBuddy().with(Implementation.Context.Disabled.Factory.INSTANCE);
final Object agentBuilder = new AgentBuilder.Default().ignore(ElementMatchers.none()).with(byteBuddy)
.disableClassFormatChanges().with(AgentBuilder.InitializationStrategy.Minimal.INSTANCE)
.with(AgentBuilder.RedefinitionStrategy.RETRANSFORMATION)
.with(AgentBuilder.TypeStrategy.Default.DECORATE).type((ElementMatchers.named("java.awt.Window")))
.transform((builder, typeDescription, classLoader, module) -> {
return builder.visit(Advice.to(Why.class).on(ElementMatchers.any()));
}).installOn(inst);
}
static class Why {
@Advice.OnMethodEnter
static void enter(@Advice.Origin String method) throws Exception {}
@Advice.OnMethodExit
static void exit(@Advice.Origin("#t") String clayuss, @Advice.Origin("#m") String method,
@Advice.This(optional = true) Object thiz) throws Exception {
try {
if (thiz instanceof JFrame && method.equals("setVisible")) {
final Window window = (Window) thiz;
final JFrame jf = (JFrame) thiz;
jf.setJMenuBar(null);
Class.forName("com.apple.eawt.FullScreenUtilities").getMethod("setWindowCanFullScreen", Window.class, Boolean.TYPE).invoke(null, window, true);
Class app = Class.forName("com.apple.eawt.Application");
Object geta = app.getMethod("getApplication").invoke(null);
geta.getClass().getMethod("requestToggleFullScreen", Window.class).invoke(geta, window);
System.err.println(method + " called on JFrame " + thiz.toString());
}
} catch (Throwable t) {
t.printStackTrace();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment