From b8a286137fce63fdaae0ac2cc0db3bf3de0bb33a Mon Sep 17 00:00:00 2001 From: Avery Cowan Date: Wed, 7 Dec 2022 12:22:14 -0800 Subject: [PATCH] Initial Commit --- src/META-INF/MANIFEST.MF | 3 ++ .../screenprotector/LockMechanism.java | 40 ++++++++++++++ src/com/averycowan/screenprotector/Main.java | 54 +++++++++++++++++++ .../screenprotector/VerboseJFrame.java | 24 +++++++++ 4 files changed, 121 insertions(+) create mode 100644 src/META-INF/MANIFEST.MF create mode 100644 src/com/averycowan/screenprotector/LockMechanism.java create mode 100644 src/com/averycowan/screenprotector/Main.java create mode 100644 src/com/averycowan/screenprotector/VerboseJFrame.java diff --git a/src/META-INF/MANIFEST.MF b/src/META-INF/MANIFEST.MF new file mode 100644 index 0000000..0bdb785 --- /dev/null +++ b/src/META-INF/MANIFEST.MF @@ -0,0 +1,3 @@ +Manifest-Version: 1.0 +Main-Class: com.averycowan.screenprotector.Main + diff --git a/src/com/averycowan/screenprotector/LockMechanism.java b/src/com/averycowan/screenprotector/LockMechanism.java new file mode 100644 index 0000000..7e0836a --- /dev/null +++ b/src/com/averycowan/screenprotector/LockMechanism.java @@ -0,0 +1,40 @@ +package com.averycowan.screenprotector; + +import java.awt.*; +import java.awt.event.WindowEvent; + +public class LockMechanism { + public static void lock() { + // Run the applescript + String[] command = { + "osascript", + "-e", + "tell app \"System Events\" to key code 12 using {control down, command down}" + }; + try { + System.err.println("Locking screen"); + Process p = Runtime.getRuntime().exec(command); + p.getInputStream().transferTo(System.out); + p.getErrorStream().transferTo(System.err); + p.waitFor(); + Thread.sleep(200); + } catch (Exception e) { + e.printStackTrace(); + } + } + + public static class Listener implements java.awt.event.AWTEventListener { + @Override + public void eventDispatched(AWTEvent event) { + if (event instanceof WindowEvent) { + WindowEvent windowEvent = (WindowEvent) event; + if (windowEvent.getID() == WindowEvent.WINDOW_ACTIVATED) { + return; + } + } + System.out.println(event); + LockMechanism.lock(); + System.exit(0); + } + } +} diff --git a/src/com/averycowan/screenprotector/Main.java b/src/com/averycowan/screenprotector/Main.java new file mode 100644 index 0000000..50eedee --- /dev/null +++ b/src/com/averycowan/screenprotector/Main.java @@ -0,0 +1,54 @@ +package com.averycowan.screenprotector; + +import javax.swing.JFrame; +import java.awt.*; +import java.awt.event.AWTEventListener; +import java.util.concurrent.locks.Lock; + +public class Main { + public static final int WINDOW_SIZE = 2; + + public static final int WAIT_TIME_MS = 1500; + + public static void main(String[] args){ + VerboseJFrame frame = new VerboseJFrame(); + frame.setSize(WINDOW_SIZE, WINDOW_SIZE); + frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + frame.setUndecorated(true); + Dimension screensize = Toolkit.getDefaultToolkit().getScreenSize(); + frame.setSize(screensize); + frame.setVisible(true); + // Wait the specified amount of time before showing the frame + System.out.println("Waiting " + WAIT_TIME_MS + "ms before showing frame"); + try { + Thread.sleep(WAIT_TIME_MS); + } catch (InterruptedException e) { + e.printStackTrace(); + } + // Set location to around the mouse + frame.setVisible(false); + frame.setSize(WINDOW_SIZE, WINDOW_SIZE); + Point mouseLocation = MouseInfo.getPointerInfo().getLocation(); + Point windowLocation = new Point(mouseLocation.x - WINDOW_SIZE / 2, mouseLocation.y - WINDOW_SIZE / 2); + frame.setLocation(windowLocation); + System.out.println("Showing frame"); + frame.setVisible(true); + Point windowLocationAfter = frame.getLocationOnScreen(); + // Check that the window was correctly moved + if (!windowLocationAfter.equals(windowLocation)) { + System.err.println("Window was not moved to the correct location"); + System.err.println("Expected: " + windowLocation); + System.err.println("Actual: " + windowLocationAfter); + System.err.println("Mouse location: " + mouseLocation); + System.exit(1); + } + // Sleep for 200ms to allow the window to be drawn + try { + Thread.sleep(200); + } catch (InterruptedException e) { + e.printStackTrace(); + } + System.out.println("Adding listener"); + frame.addAWTEventListener(new LockMechanism.Listener()); + } +} \ No newline at end of file diff --git a/src/com/averycowan/screenprotector/VerboseJFrame.java b/src/com/averycowan/screenprotector/VerboseJFrame.java new file mode 100644 index 0000000..0b7b5f5 --- /dev/null +++ b/src/com/averycowan/screenprotector/VerboseJFrame.java @@ -0,0 +1,24 @@ +package com.averycowan.screenprotector; + +import javax.swing.JFrame; +import java.awt.AWTEvent; +import java.awt.event.AWTEventListener; +import java.util.List; + +public class VerboseJFrame extends JFrame { + List listeners = new java.util.ArrayList(); + public VerboseJFrame() { + super(); + this.enableEvents(-1l); + } + @Override + public void processEvent(AWTEvent e) { + for (AWTEventListener listener : listeners) { + listener.eventDispatched(e); + } + super.processEvent(e); + } + public void addAWTEventListener(AWTEventListener listener) { + listeners.add(listener); + } +}