forked from zhuowei/CompRemote-server
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainThread.java
More file actions
173 lines (169 loc) · 4.85 KB
/
MainThread.java
File metadata and controls
173 lines (169 loc) · 4.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/**
* Write a description of class MainThread here.
*
* @author (your name)
* @version (a version number or a date)
*/
import java.net.*;
import java.io.*;
import java.awt.*;
import javax.swing.JOptionPane;
public class MainThread extends Thread{
private Socket sock;
private DataOutputStream out;
private DataInputStream in;
public boolean running=false;
private String password;
private Robot robot;
private Point prevPoint; //we need to keep some history. Read below!!!
/*
* constructor. The password should be hashed!
*/
public MainThread(Socket s, String password){
//System.out.println("mainthreadsdfsfsfsdfdf"+password);
try{
this.sock=s;
this.password=password;
out=new DataOutputStream(s.getOutputStream());
in=new DataInputStream(s.getInputStream());
/***************************
* new init for Robot with respect to the screen at point 0,0. We need this because, as in my case, if the primary screen does not contain
* the point 0, 0 of the pointer location we have problems.
***************************/
GraphicsDevice myGD = null;
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice[] gs = ge.getScreenDevices();
for(GraphicsDevice gd: gs) {
GraphicsConfiguration[] gc = gd.getConfigurations();
if(gc.length > 0) {
Rectangle rect = gc[0].getBounds();
if(rect.x == 0 && rect.y == 0) {
myGD = gd;
}
}
}
if(myGD != null) {
robot=new Robot(myGD);
}
else {
System.out.println("Could not find 0 0 screen");
System.exit(-1);
}
Runtime.getRuntime().addShutdownHook(new MouseReleaseThread());
//System.out.println("Password: " + password);
}
catch(AWTException ae){
System.err.println("Robot init failed: " + ae);
}
catch(Exception e){
System.err.println("Error during MainThread init: " + e);
}
if(!authenticate()){
try{
sock.close();
JOptionPane.showMessageDialog(null, "The password was incorrect.");
System.exit(0);
}
catch(Exception e){
e.printStackTrace();
System.err.println(e);
System.exit(42);
}
}
}
public boolean authenticate(){
//System.out.println(password);
try{
if(in.read()!=0){
return false;
}
if(password.equals(in.readUTF())){
return true;
}
return false;
}
catch(Exception e){
e.printStackTrace();
System.out.println("Auth fail:" + e);
return false;
}
}
public void run(){
running=true;
System.out.println("MainThread running..");
while(running&&!sock.isClosed()){
try{
int id=in.read();
System.out.println(id);
switch(id){
case 1: //keyboard keydown
robot.keyPress((int)in.readShort());
break;
case 2: //keyboard keyUp
robot.keyRelease((int)in.readShort());
break;
case 3:
robot.mousePress((int)in.readByte());
break;
case 4:
robot.mouseRelease((int)in.readByte());
break;
case 5:
/***************************************
* it has a problem that if the point goes outside the visible area the getPointerInfo throws a nullPointerException
* for example in my case the screen setup is:
* ----------------------
* | | |
* | | |
* ---------| |
* x -------------
*
* so if the pointer goes to 'x' it is not visible and null pointer exception is thrown(all other edges are checked by the X windows
* system and it does not allow the mouse to go outside). I have temporarily fixed it by keep a history and setting the point to the
* last known position. A better fix would be check that the pointer is not out of bounds. Which can be slightly complicated if multiple
* screens are arranged in some weird way. I have only tested this in Linux. Windows may not allow the pointer to go there.
***************************************/
Point curPoint;
try {
curPoint=MouseInfo.getPointerInfo().getLocation();
} catch(NullPointerException e) {
System.out.println("NulpointerException at point 1");
curPoint = prevPoint;
}
robot.mouseMove(curPoint.x + in.readInt(), curPoint.y + in.readInt());
prevPoint = curPoint;
break;
case 6: //mouseWheel
robot.mouseWheel(in.readByte());
break;
case -1:
System.out.println("Disconnected");
running=false;
System.exit(0);
break;
default:
System.err.println("unrecognized packet: " + id);
running=false;
break;
}
}
catch(IOException ie){
System.err.println("IOException: " + ie);
break;
}
}
try{
sock.close();
}
catch(Exception e){
}
running=false;
}
private class MouseReleaseThread extends Thread{
public void run(){
System.out.println("releasing mouse buttons");
robot.mouseRelease(16|8|4);
System.out.println("released mouse buttons");
}
}
}