first commit

This commit is contained in:
2026-09-08 20:28:54 +08:00
commit 2ada8d3d5a
37380 changed files with 4886169 additions and 0 deletions
+29
View File
@@ -0,0 +1,29 @@
(function() {
var Client, Connection, Monkey;
Client = require('./monkey/client');
Connection = require('./monkey/connection');
Monkey = (function() {
function Monkey() {}
Monkey.connect = function(options) {
return new Connection().connect(options);
};
Monkey.connectStream = function(stream) {
return new Client().connect(stream);
};
return Monkey;
})();
Monkey.Connection = Connection;
Monkey.Client = Client;
module.exports = Monkey;
}).call(this);
+276
View File
@@ -0,0 +1,276 @@
(function() {
var Api, EventEmitter, _ref,
__hasProp = {}.hasOwnProperty,
__extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; };
EventEmitter = require('events').EventEmitter;
Api = (function(_super) {
__extends(Api, _super);
function Api() {
_ref = Api.__super__.constructor.apply(this, arguments);
return _ref;
}
Api.prototype.send = function() {
throw new Error("send is not implemented");
};
Api.prototype.keyDown = function(keyCode, callback) {
this.send("key down " + keyCode, callback);
return this;
};
Api.prototype.keyUp = function(keyCode, callback) {
this.send("key up " + keyCode, callback);
return this;
};
Api.prototype.touchDown = function(x, y, callback) {
this.send("touch down " + x + " " + y, callback);
return this;
};
Api.prototype.touchUp = function(x, y, callback) {
this.send("touch up " + x + " " + y, callback);
return this;
};
Api.prototype.touchMove = function(x, y, callback) {
this.send("touch move " + x + " " + y, callback);
return this;
};
Api.prototype.trackball = function(dx, dy, callback) {
this.send("trackball " + dx + " " + dy, callback);
return this;
};
Api.prototype.flipOpen = function(callback) {
this.send("flip open", callback);
return this;
};
Api.prototype.flipClose = function(callback) {
this.send("flip close", callback);
return this;
};
Api.prototype.wake = function(callback) {
this.send("wake", callback);
return this;
};
Api.prototype.tap = function(x, y, callback) {
this.send("tap " + x + " " + y, callback);
return this;
};
Api.prototype.press = function(keyCode, callback) {
this.send("press " + keyCode, callback);
return this;
};
Api.prototype.type = function(str, callback) {
str = str.replace(/"/g, '\\"');
if (str.indexOf(' ') === -1) {
this.send("type " + str, callback);
} else {
this.send("type \"" + str + "\"", callback);
}
return this;
};
Api.prototype.list = function(callback) {
var _this = this;
this.send("listvar", function(err, vars) {
if (err) {
return _this(callback(err));
}
if (err) {
return callback(err);
} else {
return callback(null, vars.split(/\s+/g));
}
});
return this;
};
Api.prototype.get = function(name, callback) {
this.send("getvar " + name, callback);
return this;
};
Api.prototype.quit = function(callback) {
this.send("quit", callback);
return this;
};
Api.prototype.done = function(callback) {
this.send("done", callback);
return this;
};
Api.prototype.sleep = function(ms, callback) {
this.send("sleep " + ms, callback);
return this;
};
Api.prototype.getAmCurrentAction = function(callback) {
this.get('am.current.action', callback);
return this;
};
Api.prototype.getAmCurrentCategories = function(callback) {
this.get('am.current.categories', callback);
return this;
};
Api.prototype.getAmCurrentCompClass = function(callback) {
this.get('am.current.comp.class', callback);
return this;
};
Api.prototype.getAmCurrentCompPackage = function(callback) {
this.get('am.current.comp.package', callback);
return this;
};
Api.prototype.getAmCurrentData = function(callback) {
this.get('am.current.data', callback);
return this;
};
Api.prototype.getAmCurrentPackage = function(callback) {
this.get('am.current.package', callback);
return this;
};
Api.prototype.getBuildBoard = function(callback) {
this.get('build.board', callback);
return this;
};
Api.prototype.getBuildBrand = function(callback) {
this.get('build.brand', callback);
return this;
};
Api.prototype.getBuildCpuAbi = function(callback) {
this.get('build.cpu_abi', callback);
return this;
};
Api.prototype.getBuildDevice = function(callback) {
this.get('build.device', callback);
return this;
};
Api.prototype.getBuildDisplay = function(callback) {
this.get('build.display', callback);
return this;
};
Api.prototype.getBuildFingerprint = function(callback) {
this.get('build.fingerprint', callback);
return this;
};
Api.prototype.getBuildHost = function(callback) {
this.get('build.host', callback);
return this;
};
Api.prototype.getBuildId = function(callback) {
this.get('build.id', callback);
return this;
};
Api.prototype.getBuildManufacturer = function(callback) {
this.get('build.manufacturer', callback);
return this;
};
Api.prototype.getBuildModel = function(callback) {
this.get('build.model', callback);
return this;
};
Api.prototype.getBuildProduct = function(callback) {
this.get('build.product', callback);
return this;
};
Api.prototype.getBuildTags = function(callback) {
this.get('build.tags', callback);
return this;
};
Api.prototype.getBuildType = function(callback) {
this.get('build.type', callback);
return this;
};
Api.prototype.getBuildUser = function(callback) {
this.get('build.user', callback);
return this;
};
Api.prototype.getBuildVersionCodename = function(callback) {
this.get('build.version.codename', callback);
return this;
};
Api.prototype.getBuildVersionIncremental = function(callback) {
this.get('build.version.incremental', callback);
return this;
};
Api.prototype.getBuildVersionRelease = function(callback) {
this.get('build.version.release', callback);
return this;
};
Api.prototype.getBuildVersionSdk = function(callback) {
this.get('build.version.sdk', callback);
return this;
};
Api.prototype.getClockMillis = function(callback) {
this.get('clock.millis', callback);
return this;
};
Api.prototype.getClockRealtime = function(callback) {
this.get('clock.realtime', callback);
return this;
};
Api.prototype.getClockUptime = function(callback) {
this.get('clock.uptime', callback);
return this;
};
Api.prototype.getDisplayDensity = function(callback) {
this.get('display.density', callback);
return this;
};
Api.prototype.getDisplayHeight = function(callback) {
this.get('display.height', callback);
return this;
};
Api.prototype.getDisplayWidth = function(callback) {
this.get('display.width', callback);
return this;
};
return Api;
})(EventEmitter);
module.exports = Api;
}).call(this);
+98
View File
@@ -0,0 +1,98 @@
(function() {
var Api, Client, Command, Multi, Parser, Queue, Reply,
__hasProp = {}.hasOwnProperty,
__extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; };
Api = require('./api');
Command = require('./command');
Reply = require('./reply');
Queue = require('./queue');
Multi = require('./multi');
Parser = require('./parser');
Client = (function(_super) {
__extends(Client, _super);
function Client() {
this.commandQueue = new Queue;
this.parser = new Parser;
this.stream = null;
}
Client.prototype._hook = function() {
var _this = this;
this.stream.on('data', function(data) {
return _this.parser.parse(data);
});
this.stream.on('error', function(err) {
return _this.emit('error', err);
});
this.stream.on('end', function() {
return _this.emit('end');
});
this.stream.on('finish', function() {
return _this.emit('finish');
});
this.parser.on('reply', function(reply) {
return _this._consume(reply);
});
this.parser.on('error', function(err) {
return _this.emit('error', err);
});
};
Client.prototype._consume = function(reply) {
var command;
if (command = this.commandQueue.dequeue()) {
if (reply.isError()) {
command.callback(reply.toError(), null, command.command);
} else {
command.callback(null, reply.value, command.command);
}
} else {
throw new Error("Command queue depleted, but replies still coming in");
}
};
Client.prototype.connect = function(stream) {
this.stream = stream;
this._hook();
return this;
};
Client.prototype.end = function() {
this.stream.end();
return this;
};
Client.prototype.send = function(commands, callback) {
var command, _i, _len;
if (Array.isArray(commands)) {
for (_i = 0, _len = commands.length; _i < _len; _i++) {
command = commands[_i];
this.commandQueue.enqueue(new Command(command, callback));
}
this.stream.write("" + (commands.join('\n')) + "\n");
} else {
this.commandQueue.enqueue(new Command(commands, callback));
this.stream.write("" + commands + "\n");
}
return this;
};
Client.prototype.multi = function() {
return new Multi(this);
};
return Client;
})(Api);
module.exports = Client;
}).call(this);
+17
View File
@@ -0,0 +1,17 @@
(function() {
var Command;
Command = (function() {
function Command(command, callback) {
this.command = command;
this.callback = callback;
this.next = null;
}
return Command;
})();
module.exports = Command;
}).call(this);
+42
View File
@@ -0,0 +1,42 @@
(function() {
var Client, Connection, Net, _ref,
__hasProp = {}.hasOwnProperty,
__extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; };
Net = require('net');
Client = require('./client');
Connection = (function(_super) {
__extends(Connection, _super);
function Connection() {
_ref = Connection.__super__.constructor.apply(this, arguments);
return _ref;
}
Connection.prototype.connect = function(options) {
var stream;
stream = Net.connect(options);
stream.setNoDelay(true);
return Connection.__super__.connect.call(this, stream);
};
Connection.prototype._hook = function() {
var _this = this;
this.stream.on('connect', function() {
return _this.emit('connect');
});
this.stream.on('close', function(hadError) {
return _this.emit('close', hadError);
});
return Connection.__super__._hook.call(this);
};
return Connection;
})(Client);
module.exports = Connection;
}).call(this);
+85
View File
@@ -0,0 +1,85 @@
(function() {
var Api, Command, Multi,
__hasProp = {}.hasOwnProperty,
__extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; };
Api = require('./api');
Command = require('./command');
Multi = (function(_super) {
__extends(Multi, _super);
function Multi(monkey) {
var _this = this;
this.monkey = monkey;
this.commands = [];
this.replies = [];
this.errors = [];
this.counter = 0;
this.sent = false;
this.callback = null;
this.collector = function(err, result, cmd) {
if (err) {
_this.errors.push("" + cmd + ": " + err.message);
}
_this.replies.push(result);
_this.counter -= 1;
return _this._maybeFinish();
};
}
Multi.prototype._maybeFinish = function() {
var _this = this;
if (this.counter === 0) {
if (this.errors.length) {
setImmediate(function() {
return _this.callback(new Error(_this.errors.join(', ')));
});
} else {
setImmediate(function() {
return _this.callback(null, _this.replies);
});
}
}
};
Multi.prototype._forbidReuse = function() {
if (this.sent) {
throw new Error("Reuse not supported");
}
};
Multi.prototype.send = function(command) {
this._forbidReuse();
this.commands.push(new Command(command, this.collector));
};
Multi.prototype.execute = function(callback) {
var command, parts, _i, _len, _ref;
this._forbidReuse();
this.counter = this.commands.length;
this.sent = true;
this.callback = callback;
if (this.counter === 0) {
return;
}
parts = [];
_ref = this.commands;
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
command = _ref[_i];
this.monkey.commandQueue.enqueue(command);
parts.push(command.command);
}
parts.push('');
this.commands = [];
this.monkey.stream.write(parts.join('\n'));
};
return Multi;
})(Api);
module.exports = Multi;
}).call(this);
+66
View File
@@ -0,0 +1,66 @@
(function() {
var EventEmitter, Parser, Reply,
__hasProp = {}.hasOwnProperty,
__extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; };
EventEmitter = require('events').EventEmitter;
Reply = require('./reply');
Parser = (function(_super) {
__extends(Parser, _super);
function Parser(options) {
this.column = 0;
this.buffer = new Buffer('');
}
Parser.prototype.parse = function(chunk) {
this.buffer = Buffer.concat([this.buffer, chunk]);
while (this.column < this.buffer.length) {
if (this.buffer[this.column] === 0x0a) {
this._parseLine(this.buffer.slice(0, this.column));
this.buffer = this.buffer.slice(this.column + 1);
this.column = 0;
}
this.column += 1;
}
if (this.buffer.length) {
this.emit('wait');
} else {
this.emit('drain');
}
};
Parser.prototype._parseLine = function(line) {
switch (line[0]) {
case 0x4f:
if (line.length === 2) {
this.emit('reply', new Reply(Reply.OK, null));
} else {
this.emit('reply', new Reply(Reply.OK, line.toString('ascii', 3)));
}
break;
case 0x45:
if (line.length === 5) {
this.emit('reply', new Reply(Reply.ERROR, null));
} else {
this.emit('reply', new Reply(Reply.ERROR, line.toString('ascii', 6)));
}
break;
default:
this._complain(line);
}
};
Parser.prototype._complain = function(line) {
this.emit('error', new SyntaxError("Unparseable line '" + line + "'"));
};
return Parser;
})(EventEmitter);
module.exports = Parser;
}).call(this);
+38
View File
@@ -0,0 +1,38 @@
(function() {
var Queue;
Queue = (function() {
function Queue() {
this.head = null;
this.tail = null;
}
Queue.prototype.enqueue = function(item) {
if (this.tail) {
this.tail.next = item;
} else {
this.head = item;
}
this.tail = item;
};
Queue.prototype.dequeue = function() {
var item;
item = this.head;
if (item) {
if (item === this.tail) {
this.tail = null;
}
this.head = item.next;
item.next = null;
}
return item;
};
return Queue;
})();
module.exports = Queue;
}).call(this);
+31
View File
@@ -0,0 +1,31 @@
(function() {
var Reply;
Reply = (function() {
Reply.ERROR = 'ERROR';
Reply.OK = 'OK';
function Reply(type, value) {
this.type = type;
this.value = value;
}
Reply.prototype.isError = function() {
return this.type === Reply.ERROR;
};
Reply.prototype.toError = function() {
if (!this.isError()) {
throw new Error('toError() cannot be called for non-errors');
}
return new Error(this.value);
};
return Reply;
})();
module.exports = Reply;
}).call(this);