Commit bbc17756 authored by Gaurav Arora (gaa721)'s avatar Gaurav Arora (gaa721)
Browse files

Study ready

parents
import com.sun.jna.platform.win32.*;
import gab.opencv.*;
import processing.video.*;
import java.awt.*;
import com.sun.jna.*;
import com.sun.jna.platform.win32.WinDef.*;
import org.jnativehook.*;
import org.jnativehook.keyboard.NativeKeyListener;
import org.jnativehook.mouse.NativeMouseInputListener;
import org.jnativehook.mouse.NativeMouseEvent;
import org.jnativehook.keyboard.NativeKeyEvent;
import com.sun.jna.platform.win32.WinUser.WINDOWINFO;
final String[] adjectives ={"sad","smelly","happy","greasy","beautiful","great","horrible","gelatinous","swift","real","fake","fun","evil","catastrophic","fantastic","amazing","entertaining","ruthless","devastating","tasty","lumpy","charming","quaint","pleasant","wasteful","harmless","stupid","ridiculous","impossible"};
final String[] pluralNouns = {"puppies","kittens","computers","politics","aliens","supernovas","shoes","people","governments","industries","ideas","societies","investments","animals","things","books","stairs","movies","children","roads","cars","beans","turnips","lakes","arguments","turtles","quantum mechanics","chemtrails","vegetables","tools","politicians"};
final String[] names = {"Bob","Doug","3xtr3m3","cool_dude","yoloSwag420","xX_ex_Xx","turtlefan25","Mike","MsPaint","MacKenzie"};
private static final int WS_ICONIC = 0x20000000; //for check minimized
Capture video;
OpenCV opencv;
Rectangle[] faces;
int strBuffSize;
char[] windowText;
String windowTitle;
PImage before,grayDiff,current,first;
PFont f;
ArrayList<RECT> aboveWindows;
RECT rectOfInterest;
HWND windowOfInterest;
long lastKeyInput;
long lastMouseInput;
long lastFace;
long lastMotion;
boolean wasMinimized;
boolean foreground;
boolean motionDetected;
boolean obscured;
boolean attention;
PGraphics chatBox;
ArrayList<ChatMessage> messages;
int scroll;
boolean upOver,downOver;
int messageDelay = 10;
String userName = "You";
color userColor;
String typedText = "";
int chatHeight = 400;
int chatWidth = 500;
int chatX = 10;
int chatY = 10;
int rowHeight = 13;
void setup() {
size(chatX+chatWidth+10, chatY+chatHeight+20+10);
strBuffSize = 512;
windowText = new char[strBuffSize];
windowTitle = "AttentionChat";
aboveWindows = new ArrayList<RECT>();
rectOfInterest = new RECT();
windowOfInterest = User32.INSTANCE.GetForegroundWindow();
video = new Capture(this, 640/2, 480/2);
opencv = new OpenCV(this, 640/2, 480/2);
opencv.loadCascade(OpenCV.CASCADE_FRONTALFACE);
video.start();
lastKeyInput = System.nanoTime();
lastMouseInput = System.nanoTime();
new InputReceiver(this);
chatBox = createGraphics(chatWidth,chatHeight);
messages = new ArrayList<ChatMessage>();
userColor = color(255,0,0);
}
void draw() {
background(0);
opencv.loadImage(video);
faces = opencv.detect();
opencv.diff(video);
opencv.threshold(70);
grayDiff = opencv.getOutput();
motionDetected = hasWhite(grayDiff);
obscured = isObscured();
long now = System.nanoTime();
wasMinimized = isMinimized(windowOfInterest);
if(User32.INSTANCE.GetForegroundWindow()!=null)
{
foreground = User32.INSTANCE.GetForegroundWindow().equals(windowOfInterest);
}
else
{
foreground = false;
}
attention = isPayingAttention();
messageDelay--;
if(messageDelay==0)
{
messageDelay = 5+(int)random(25);
ChatMessage c = generateMessage();
messages.add(c);
if(messages.size()*rowHeight-scroll==int(chatHeight/rowHeight)*rowHeight + rowHeight)
{
scroll+=rowHeight;
if(attention)
{
c.seen=true;
}
else
{
c.seen = false;
}
}
else if(messages.size()*rowHeight-scroll>=int(chatHeight/rowHeight)*rowHeight + rowHeight)
{
c.seen = false;
}
else
{
if(attention)
{
c.seen=true;
}
else
{
c.seen = false;
}
}
}
update(mouseX, mouseY);
chatBox.beginDraw();
chatBox.background(100);
chatBox.stroke(0,0,0,0);
int i = 0;
for(ChatMessage s : messages)
{
if(!s.seen)
{
chatBox.fill(150,0,0,255-3*s.framesSeen);
chatBox.rect(0,(i)*rowHeight-scroll+2,chatWidth,rowHeight);
}
chatBox.fill(s.userColor);
chatBox.text(s.user+": ",5,(i+1)*rowHeight-scroll);
chatBox.fill(255,255,255);
chatBox.text(s.message,5+textWidth(s.user+": "),(i+1)*rowHeight-scroll);
if((i+1)*rowHeight-scroll<=int(chatHeight/rowHeight)*rowHeight + rowHeight && (i+1)*rowHeight-scroll>0&&attention)
{
s.framesSeen++;
if(255-3*s.framesSeen<=0)
s.seen=true;
}
i++;
}
chatBox.fill(255,255,255);
chatBox.rect(chatWidth-40,0,40,40);
chatBox.rect(chatWidth-40,chatHeight-40,40,40);
chatBox.stroke(100,100,100,255);
chatBox.line(chatWidth-35,35,chatWidth-20,5);
chatBox.line(chatWidth-20,5,chatWidth-5,35);
chatBox.line(chatWidth-35,35,chatWidth-5,35);
chatBox.line(chatWidth-35,chatHeight-35,chatWidth-20,chatHeight-5);
chatBox.line(chatWidth-20,chatHeight-5,chatWidth-5,chatHeight-35);
chatBox.line(chatWidth-35,chatHeight-35,chatWidth-5,chatHeight-35);
chatBox.fill(255,255,255);
chatBox.endDraw();
image(chatBox,chatX,chatY);
fill(100);
rect(chatX-1,chatY+chatHeight+2,chatWidth+1,20);
fill(255);
text(typedText+(frameCount/10 % 2 == 0 ? "_" : ""), chatX+5, chatY+chatHeight+18);
}
void update(int x, int y) {
if ( x<chatX+chatWidth&&x>chatX+chatWidth-40&&y>chatY&&y<chatY+40 ) {
upOver = true;
} else if ( x<chatX+chatWidth&&x>chatX+chatWidth-40&&y>chatY+chatHeight-40&&y<chatY+chatHeight) {
downOver = true;
} else {
upOver = downOver = false;
}
}
public class ChatMessage
{
String user;
String message;
color userColor;
boolean seen;
int framesSeen;
ChatMessage(String inUser,String inMessage,int r,int g,int b)
{
user = inUser;
message = inMessage;
userColor = color(r,g,b);
seen=false;
framesSeen = 0;
}
ChatMessage(String inUser,String inMessage,color c,boolean s)
{
user = inUser;
message = inMessage;
userColor = c;
seen=s;
framesSeen = 0;
}
ChatMessage(String inUser,String inMessage,int r,int g,int b,boolean inSeen)
{
user = inUser;
message = inMessage;
userColor = color(r,g,b);
seen=inSeen;
framesSeen = 0;
}
}
void mousePressed() {
if (upOver) {
scroll-=rowHeight;
}
if (downOver) {
scroll+=rowHeight;
}
}
void keyReleased() {
if (key != CODED) {
switch(key) {
case BACKSPACE:
typedText = typedText.substring(0,max(0,typedText.length()-1));
break;
case TAB:
typedText += " ";
break;
case ENTER:
case RETURN:
// comment out the following two lines to disable line-breaks
messages.add(new ChatMessage(userName,typedText,userColor,true));
typedText = "";
if(messages.size()*rowHeight-scroll==int(chatHeight/rowHeight)*rowHeight + rowHeight)
{
scroll+=rowHeight;
}
break;
case ESC:
case DELETE:
break;
default:
typedText += key;
}
}
}
ChatMessage generateMessage()
{
int user = int(random(names.length));
String genName = names[user];
color genColor;
switch (user)
{
case 0:
genColor = color(0,255,255);
break;
case 1:
genColor = color(0,255,0);
break;
case 2:
genColor = color(0,0,255);
break;
case 3:
genColor = color(255,255,0);
break;
case 4:
genColor = color(50,200,100);
break;
case 5:
genColor = color(100,200,255);
break;
case 6:
genColor = color(255,50,255);
break;
case 7:
genColor = color(255,200,100);
break;
case 8:
genColor = color(200,150,50);
break;
case 9:
genColor = color(150,0,100);
break;
default:
genColor = color(0,0,0);
}
String genMessage;
String noun = pluralNouns[int(random(pluralNouns.length))];
String adjective = adjectives[int(random(adjectives.length))];
String toName="";
switch(int(random(30)))
{
case 0:
genMessage = "exactly";
break;
case 1:
genMessage = "no, you're wrong";
break;
case 2:
genMessage = "you're crazy";
break;
case 3:
genMessage = "don't change the subject";
break;
case 4:
genMessage = "personally, I feel that "+noun+" are " + adjective;
break;
case 5:
genMessage = "but " +noun+ " are definitely not "+adjective;
break;
case 6:
genMessage = "I disagree because "+noun+" are "+adjective;
break;
case 7:
genMessage = "I thought we were talking about "+noun;
break;
case 8:
genMessage = "it's been scientifically proven that "+noun+" are "+adjective;
break;
case 9:
genMessage = "think of the "+noun+" though. What about them?";
break;
case 10:
genMessage = "No! "+noun+" are "+adjective+"! period!";
break;
case 11:
genMessage = "what could possibly make you think that "+noun+" are "+adjective+"?";
break;
case 12:
genMessage = noun+" are "+adjective;
break;
case 13:
genMessage = noun+" are not "+adjective;
break;
case 14:
genMessage = "no";
break;
case 15:
genMessage = "yes";
break;
case 16:
toName = names[int(random(names.length))];
if(genName.equals(toName))
genMessage = "I'm right";
else
genMessage = toName+" is right";
break;
case 17:
toName = names[int(random(names.length))];
if(genName.equals(toName))
genMessage = "I was wrong";
else
genMessage = "no, "+toName+" you're wrong";
break;
case 18:
toName = names[int(random(names.length))];
if(genName.equals(toName))
genMessage = "I'm clearly "+adjective;
else
genMessage = toName+" is clearly "+adjective;
break;
case 19:
genMessage = "they're "+adjective+" too";
break;
case 20:
genMessage = "keep in mind that they aren't "+adjective;
break;
case 21:
toName = names[int(random(names.length))];
if(genName.equals(toName))
genMessage = "I was thinking of "+noun;
else
genMessage = toName+" you're thinking of "+noun;
break;
case 22:
toName = names[int(random(names.length))];
if(genName.equals(toName))
genMessage = "I am clearly not "+adjective;
else
genMessage = toName + " is clearly not "+adjective;
break;
case 23:
genMessage = "have you even heard of "+noun+"?";
break;
case 24:
genMessage = "Wow! really?";
break;
case 25:
genMessage = "that's "+ adjective;
break;
case 26:
genMessage = "I like "+noun;
break;
case 27:
genMessage = "I hate "+noun;
break;
case 28:
genMessage = "did you know that "+noun+" are actually "+pluralNouns[(int)random(pluralNouns.length)]+"?";
break;
case 29:
genMessage = noun+" are caused by "+pluralNouns[(int)random(pluralNouns.length)];
break;
default:
genMessage = "no";
}
return new ChatMessage(genName,genMessage,genColor,false);
}
boolean isPayingAttention()
{
long now = System.nanoTime();
if(windowingScore()>50&&(attentionScore()>50||now-lastKeyInput<1000000000||now-lastMouseInput<1000000000))
{
return true;
}
else
{
return false;
}
}
int windowingScore()
{
if(wasMinimized)
{
return 0;
}
if(foreground)
{
return 100;
}
if(!obscured)
{
return 80;
}
else
{
return 20;
}
}
int attentionScore()
{
long now = System.nanoTime();
if(faces.length>0)
lastFace = now;
if(motionDetected)
lastMotion = now;
if(faces.length>0&&motionDetected)
{
return 100;
}
else if(faces.length>0&&!motionDetected)
{
return 80;
}
else if(now-lastFace<1000000000&&motionDetected)
{
return 60;
}
else if(now-lastFace<1000000000&&now-lastFace<1000000000)
{
return 51;
}
else
{
return 0;
}
}
boolean isMinimized(HWND window)
{
WINDOWINFO info = new WINDOWINFO();
User32.INSTANCE.GetWindowInfo(window, info);
if ((info.dwStyle & WS_ICONIC) == WS_ICONIC) {
return true;
}
else
{
return false;
}
}
void onKeyInput()
{
lastKeyInput = System.nanoTime();
}
void onMouseInput()
{
lastMouseInput = System.nanoTime();
}
void captureEvent(Capture c) {
c.read();
}
boolean hasWhite(PImage image)
{
for(int i = 0; i< image.pixels.length;i++)
{
if(image.pixels[i]==color(255,255,255))
return true;
}
return false;
}
boolean isObscured()
{
HWND currWindow = User32.INSTANCE.GetForegroundWindow();
int i = 0;
//get all the size/positions of windows above the window of interest
while (i<1000)
{
/*if(stateChanged)
{
char[] asdf = new char[200];
User32.INSTANCE.GetWindowText(currWindow, asdf, 200);
println(Native.toString(asdf));
}*/
RECT currRect = new RECT();
User32.INSTANCE.GetWindowRect(currWindow, currRect);
User32.INSTANCE.GetWindowText(currWindow, windowText, strBuffSize);
//see we are at the window of interest
//if we are, save the size/position and exit search
if(Native.toString(windowText).compareTo(windowTitle) == 0)
{
rectOfInterest = currRect;
if(currWindow!=null)
windowOfInterest = currWindow;
break;
}
else
{
//add positions/size of current window and get next window
aboveWindows.add(currRect);
currWindow = User32.INSTANCE.GetWindow(currWindow, new DWORD(User32.GW_HWNDNEXT));
i++;
}
}
boolean r = checkOverlap();
aboveWindows.clear();
return r;
}
boolean checkOverlap()
{
//compare above window positions to window of interest to see if they overlap
for(RECT currRect: aboveWindows)
{
//four cases of non overlap
if(rectOfInterest.left > currRect.right) //completely to the right
{
continue;
}
if(rectOfInterest.right < currRect.left) //completely to the left
{
continue;
}
if(rectOfInterest.top > currRect.bottom) //completely below
{
continue;
}
if(rectOfInterest.bottom < currRect.top) //completely above
{
continue;
}
return true;
}
return false;
}
public class InputReceiver implements NativeKeyListener, NativeMouseInputListener
{
AttentionChat sketch;
InputReceiver(AttentionChat s){
try {
GlobalScreen.registerNativeHook();
}
catch (NativeHookException ex) {
System.err.println("There was a problem registering the native hook.");
System.err.println(ex.getMessage());
System.exit(1);
}
//Add the appropriate listeners for the example object.
GlobalScreen.addNativeMouseListener(this);
GlobalScreen.addNativeMouseMotionListener(this);
GlobalScreen.addNativeKeyListener(this);
sketch = s;
}