Skip to content

Commit

Permalink
pikvm/pikvm#1041: Randomized recorder events
Browse files Browse the repository at this point in the history
  • Loading branch information
mdevaev committed Jul 7, 2023
1 parent 26d807a commit 907cc6f
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 6 deletions.
51 changes: 45 additions & 6 deletions web/share/js/kvm/recorder.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,37 +154,55 @@ export function Recorder() {
__checkType(event.event, "object", "Non-dict event");

if (event.event_type === "delay") {
__checkInt(event.event.millis, "Non-integer delay");
if (event.event.millis < 0) {
throw "Negative delay";
}
__checkUnsigned(event.event.millis, "Non-unsigned delay");
events_time += event.event.millis;

} else if (event.event_type === "print") {
__checkType(event.event.text, "string", "Non-string print text");

} else if (event.event_type === "key") {
__checkType(event.event.key, "string", "Non-string key code");
__checkType(event.event.state, "boolean", "Non-bool key state");

} else if (event.event_type === "mouse_button") {
__checkType(event.event.button, "string", "Non-string mouse button code");
__checkType(event.event.state, "boolean", "Non-bool mouse button state");

} else if (event.event_type === "mouse_move") {
__checkType(event.event.to, "object", "Non-object mouse move target");
__checkInt(event.event.to.x, "Non-int mouse move X");
__checkInt(event.event.to.y, "Non-int mouse move Y");

} else if (event.event_type === "mouse_relative") {
__checkMouseRelativeDelta(event.event.delta);
__checkType(event.event.squash, "boolean", "Non-boolean squash");

} else if (event.event_type === "mouse_wheel") {
__checkType(event.event.delta, "object", "Non-object mouse wheel delta");
__checkInt(event.event.delta.x, "Non-int mouse delta X");
__checkInt(event.event.delta.y, "Non-int mouse delta Y");

} else if (event.event_type === "atx_button") {
__checkType(event.event.button, "string", "Non-string ATX button");

} else if (event.event_type === "gpio_switch") {
__checkType(event.event.channel, "string", "Non-string GPIO channel");
__checkType(event.event.state, "boolean", "Non-bool GPIO state");

} else if (event.event_type === "gpio_pulse") {
__checkType(event.event.channel, "string", "Non-string GPIO channel");

} else if (event.event_type === "delay_random") {
__checkType(event.event.range, "object", "Non-object random delay range");
__checkUnsigned(event.event.range.min, "Non-unsigned random delay range min");
__checkUnsigned(event.event.range.max, "Non-unsigned random delay range max");
events_time += event.event.range.max;

} else if (event.event_type === "mouse_move_random") { // Hack for pikvm/pikvm#1041
__checkType(event.event.range, "object", "Non-object random mouse move range");
__checkInt(event.event.range.min, "Non-int random mouse move range min");
__checkInt(event.event.range.max, "Non-int random mouse move range max");

} else {
throw `Unknown event type: ${event.event_type}`;
}
Expand Down Expand Up @@ -217,6 +235,13 @@ export function Recorder() {
}
};

var __checkUnsigned = function(obj, msg) {
__checkInt(obj, msg);
if (obj < 0) {
throw "Negative delay";
}
};

var __checkArray = function (obj, msg) {
if (!Array.isArray(obj)) {
throw msg;
Expand All @@ -237,8 +262,13 @@ export function Recorder() {
__setCounters(__events.length - index + 1, __events_time - time);
let event = __events[index];

if (event.event_type === "delay") {
__play_timer = setTimeout(() => __runEvents(index + 1, time + event.event.millis), event.event.millis);
if (["delay", "delay_random"].includes(event.event_type)) {
let millis = (
event.event_type === "delay"
? event.event.millis
: tools.getRandomInt(event.event.range.min, event.event.range.max)
);
__play_timer = setTimeout(() => __runEvents(index + 1, time + millis), millis);
return;

} else if (event.event_type === "print") {
Expand Down Expand Up @@ -291,6 +321,15 @@ export function Recorder() {

} else if (["key", "mouse_button", "mouse_move", "mouse_wheel", "mouse_relative"].includes(event.event_type)) {
__ws.sendHidEvent(event);

} else if (event.event_type === "mouse_move_random") {
__ws.sendHidEvent({
"event_type": "mouse_move",
"event": {"to": {
"x": tools.getRandomInt(event.event.range.min, event.event.range.max),
"y": tools.getRandomInt(event.event.range.min, event.event.range.max),
}},
});
}

index += 1;
Expand Down
6 changes: 6 additions & 0 deletions web/share/js/tools.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,12 @@ export var tools = new function() {
return btoa(text).replace("=", "_");
};

self.getRandomInt = function(min, max) {
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min + 1)) + min;
};

self.formatSize = function(size) {
if (size > 0) {
let index = Math.floor( Math.log(size) / Math.log(1024) );
Expand Down

0 comments on commit 907cc6f

Please sign in to comment.