Skip to content

Commit

Permalink
Initial Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
averycowan committed Dec 7, 2022
0 parents commit b8a2861
Show file tree
Hide file tree
Showing 4 changed files with 121 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Manifest-Version: 1.0
Main-Class: com.averycowan.screenprotector.Main

40 changes: 40 additions & 0 deletions src/com/averycowan/screenprotector/LockMechanism.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
54 changes: 54 additions & 0 deletions src/com/averycowan/screenprotector/Main.java
Original file line number Diff line number Diff line change
@@ -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());
}
}
24 changes: 24 additions & 0 deletions src/com/averycowan/screenprotector/VerboseJFrame.java
Original file line number Diff line number Diff line change
@@ -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<AWTEventListener> listeners = new java.util.ArrayList<AWTEventListener>();
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);
}
}

0 comments on commit b8a2861

Please sign in to comment.