-
Notifications
You must be signed in to change notification settings - Fork 3
/
Osu!Touchpad.js
388 lines (342 loc) · 8.56 KB
/
Osu!Touchpad.js
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
//This file is for nw.js version ONLY
//For pure checkout the pure branch
//PURE branch is most likely deprecated.
//includes
var app = require('express')();
var path = require('path');
var html = require('http').Server(app);
var io = require('socket.io')(html);
var robot = require('robotjs');
var ip = require('ip');
var parser = require('ua-parser-js');
var fs = require('fs');
//constants
var HTML_PATH = "/index.html";
var CSS_PATH = "/html deps/index.css"
var HAMMER_PATH = "/lib/hammer.min.js";
var HTML_JS_PATH = "/html deps/index.js";
var SETTING_NAME = "/Osu!Touchpad_settings.json"
var SETTING_PATH = __dirname + SETTING_NAME;
var PORT = 3000;
//later sets by settings or autoYPos
var iOS_Y_COMP = 0;
var LANDSCAPE = "LANDSCAPE";
var PORTRAIT = "PORTRAIT";
var server_ip = "null";
var pos_log = false;
var Y_COMP_VALUES = {
"iPad" : {
"LANDSCAPE" : 97,
"PORTRAIT" : 97,
},
"iPhone" : {
"LANDSCAPE" : 43,
"PORTRAIT" : 64,
},
"Android" : {
"LANDSCAPE" : 80,
"PORTRAIT" : 81,
}
}
//global
var ua_data;
//defaults landscape
var orientation = LANDSCAPE;
//ratios of client screen to server screen in pixels
var w_ratio, h_ratio;
//I <3 RobotJS!
var screen_size = robot.getScreenSize();
var OS_NAME, MODEL_NAME;
var auto_y_pos = false;
var auto_y_pos_val = 0;
var setting_data = {
Y_COMP : 0,
}
var queue = [];
function printToLog(data) {
queue.push(data);
};
printToLog("Found a server screen width of " + screen_size.width);
printToLog("Found a server screen height of " + screen_size.height);
module.exports = {
getQueue : function()
{
var temp = queue;
queue = [];
return temp;
},
getIp : function()
{
return "http://" + server_ip;
},
setCommand : function(data)
{
commands(data);
}
}
function commands(data)
{
var format = data.split(/[ ]+/);
var help_str = `
Commands:
setHeightMultiplier: sets the height multiplier of the client screen to the server.
setWidthMultiplier: sets the width multiplier from the client screen to the server.
setYComp: sets Y compensation from top of screen.
posLogging [0] [1]: disables/enables printing of x and y touch coordinates. Takes in either 0 for false and 1 for true.
loadSettings: loads settings
startAutoYPos: Starts collection to calibrate screen. Touch in the middle of the screen and scroll as high as you can while staying on the screen.
stopAutoYPos: Ends collection to calibrate screen.
for every set, there is also a get which returns the value
help: prints help
clear: clears the log
`;
function printHelp()
{
var frmt = help_str.replace(/(\t\t)/g, '').split(/(\n)/g);
for(var i = 0; i < frmt.length; i++)
{
if(frmt[i] == '')
{
continue;
}
printToLog(frmt[i]);
}
}
//I could have used case, but it's too late now.
if(format[0] == "help")
{
printHelp();
}
else if(format[0] == "setYComp")
{
if(format[1] == null)
{
return;
}
iOS_Y_COMP = format[1];
}
else if(format[0] == "getYComp")
{
printToLog(iOS_Y_COMP);
}
else if(format[0] == "setHeightMultiplier")
{
if(format[1] == null)
{
return;
}
h_ratio = format[1];
}
else if(format[0] == "getHeightMultiplier")
{
printToLog(h_ratio);
}
else if(format[0] == "setWidthMultiplier")
{
if(format[1] == null)
{
return;
}
w_ratio = format[1];
}
else if(format[0] == "getWidthMultiplier")
{
printToLog(w_ratio);
}
else if(format[0] == "posLogging")
{
pos_log = (format[1] == 0) ? false : true;
}
else if(format[0] == "loadSettings")
{
//lets load settings
fs.readFile(SETTING_PATH, function(err, data)
{
if(err)
{
printToLog("No settings file has been saved yet!");
}
printToLog("Loading saved settings");
setting_data = JSON.parse(data);
printToLog("Y_COMP is now " + setting_data.Y_COMP);
iOS_Y_COMP = setting_data.Y_COMP;
})
}
else if(format[0] == "saveSettings")
{
setting_data.Y_COMP = iOS_Y_COMP;
printToLog("Saving settings");
//write to save file
fs.writeFile(SETTING_PATH, JSON.stringify(setting_data), function(err)
{
if(err)
{
console.log("Failed to save settings");
printToLog("Failed to save settings");
console.log(err.message);
}
});
}
else if(format[0] == "startAutoYPos")
{
auto_y_pos = true;
printToLog("Move your finger as high as you can go, into the url bar!")
printToLog("Type 'stopAutoYPos' to end collection");
}
else if(format[0] == "stopAutoYPos")
{
auto_y_pos = false;
iOS_Y_COMP = Math.abs(auto_y_pos_val);
}
else
{
printToLog("Unknown command " + format[0]);
printHelp();
}
}
//lets load that html file
app.get('/', function(req, res)
{
//lets get that header
ua_data = parser(req.headers['user-agent']);
OS_NAME = ua_data.os.name;
console.log("OS: " + OS_NAME);
printToLog("OS Found: " + OS_NAME);
MODEL_NAME = ua_data.device.model;
printToLog("Model: " + MODEL_NAME);
//index.html
res.sendFile(path.join(__dirname + HTML_PATH) );
})
app.get('/hammer.min.js', function(req, res)
{
//hammer.min.js
res.sendFile(path.join(__dirname + HAMMER_PATH) );
})
app.get('/index.js', function(req, res)
{
//index.js
res.sendFile(path.join(__dirname + HTML_JS_PATH) );
})
app.get('/index.css', function(req, res)
{
//index.css
res.sendFile(path.join(__dirname + CSS_PATH) );
})
//sockest
io.on('connection', function(socket)
{
//client w and h unlikely to change
var client_w, client_h;
console.log("A user connected");
printToLog("A user connected");
//send verification message
io.emit('verify', {});
socket.on('verify', function(data)
{
//verfify connection
console.log("Verfied connection to client!");
printToLog("Verfied connection to client!");
})
socket.on('ORIENTATION', function(data)
{
var orient = Math.floor(data.orientation);
if(orient == 90 || orient == -90)
{
orientation = LANDSCAPE;
}
else
{
orientation = PORTRAIT;
}
printToLog("Updated orientation to " + orientation);
})
//lets grab that screen width & height
//key is SCREEN_DIMENSION
socket.on('SCREEN_DIMENSION', function(data)
{
//lets calculate what the proportional thing would be on the server's screen
client_w = data.W;
client_h = data.H;
//tested on ios
//assumes portrait mode
if((orientation == PORTRAIT && OS_NAME == "iOS") || (OS_NAME == "Android") )
{
w_ratio = (screen_size.width / client_w);
h_ratio = (screen_size.height / client_h);
}
else
{
//landscape
w_ratio = (screen_size.width / client_h);
h_ratio = (screen_size.height / client_w);
}
console.log("Found screen width of " + client_w.toString() );
printToLog("Found screen width of " + client_w.toString() );
console.log("Found screen height of " + client_h.toString() );
printToLog("Found screen height of " + client_h.toString() );
console.log("Found a server screen width of " + screen_size.width);
console.log("Found a server screen height of " + screen_size.height)
console.log("Found a width ratio of " + w_ratio.toString() );
console.log("Found a height ratio of " + h_ratio.toString() );
})
//Key is TOUCHPOS
socket.on('TOUCHPOS', function(data)
{
var touch_x = data.X;
var touch_y = data.Y;
//iOS returns negative? coordinates which is strange.
if((OS_NAME == 'iOS') && (auto_y_pos != true) && (iOS_Y_COMP == 0) )
{
//console.log("Y_COMP_VALUES." + MODEL_NAME + "." + orientation)
touch_y += eval("Y_COMP_VALUES." + MODEL_NAME + "." + orientation);
}
else if((OS_NAME == "Android") && (auto_y_pos != true) && (iOS_Y_COMP == 0) )
{
//specifically for android chrome only
touch_y += eval("Y_COMP_VALUES." + OS_NAME + "." + orientation);
}
else
{
touch_y += iOS_Y_COMP;
}
var move_x;
var move_y;
move_x = touch_x * w_ratio;
move_y = touch_y * h_ratio;
if(pos_log)
{
printToLog("X: " + touch_x.toString() );
printToLog("Y: " + touch_y.toString() );
}
if(auto_y_pos)
{
if(auto_y_pos_val > touch_y)
{
auto_y_pos_val = touch_y;
printToLog("Found minimum " + auto_y_pos_val);
}
}
//console.log("X: " + touch_x.toString() );
//console.log("Y: " + touch_y.toString() );
//did I mention I <3 RobotJS?
robot.moveMouse(move_x, move_y);
if(pos_log)
{
printToLog("Calc X: " + (touch_x * w_ratio).toString() );
printToLog("Calc Y: " + (touch_y * h_ratio).toString() );
}
//console.log("Calc X: " + (touch_x * w_ratio).toString() );
//console.log("Calc Y: " + (touch_y * h_ratio).toString() )
})
socket.on('disconnect', function(data)
{
console.log("Client disconnected")
printToLog("Client disconnected");
})
})
html.listen(PORT, "0.0.0.0", function()
{
server_ip = ip.address() + ":" + PORT.toString();
console.log("Running @ " + server_ip);
});
console.log("CTRL + C TO EXIT OUT");