-
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
0 parents
commit b8a2861
Showing
4 changed files
with
121 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
Manifest-Version: 1.0 | ||
Main-Class: com.averycowan.screenprotector.Main | ||
|
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,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); | ||
} | ||
} | ||
} |
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,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()); | ||
} | ||
} |
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,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); | ||
} | ||
} |