Skip to content

Commit df88986

Browse files
committed
pikvm/pikvm#880: Fixed mouse position at edges
1 parent 5273199 commit df88986

File tree

5 files changed

+13
-17
lines changed

5 files changed

+13
-17
lines changed

kvmd/apps/vnc/rfb/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -590,8 +590,8 @@ async def __handle_pointer_event(self) -> None:
590590
move = (self._width, self._height, to_x, to_y)
591591
if self.__mouse_move != move:
592592
await self._on_mouse_move_event(
593-
tools.remap(to_x, 0, self._width, *MouseRange.RANGE),
594-
tools.remap(to_y, 0, self._height, *MouseRange.RANGE),
593+
tools.remap(to_x, 0, self._width - 1, *MouseRange.RANGE),
594+
tools.remap(to_y, 0, self._height - 1, *MouseRange.RANGE),
595595
)
596596
self.__mouse_move = move
597597

kvmd/tools.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@
3333

3434
# =====
3535
def remap(value: int, in_min: int, in_max: int, out_min: int, out_max: int) -> int:
36-
return int((value - in_min) * (out_max - out_min) // (in_max - in_min) + out_min)
36+
result = int((value - in_min) * (out_max - out_min) // ((in_max - in_min) or 1) + out_min)
37+
return min(max(result, out_min), out_max)
3738

3839

3940
# =====

web/share/js/kvm/mouse.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -380,8 +380,8 @@ export function Mouse(__getGeometry, __recordWsEvent) {
380380
if (pos !== null && (pos.x !== __sent_pos.x || pos.y !== __sent_pos.y)) {
381381
let geo = __getGeometry();
382382
let to = {
383-
"x": tools.remap(pos.x, geo.x, geo.width, -32768, 32767),
384-
"y": tools.remap(pos.y, geo.y, geo.height, -32768, 32767),
383+
"x": tools.remap(pos.x - geo.x, 0, geo.width - 1, -32768, 32767),
384+
"y": tools.remap(pos.y - geo.y, 0, geo.height - 1, -32768, 32767),
385385
};
386386
tools.debug("Mouse: moved:", to);
387387
__sendEvent("mouse_move", {"to": to});

web/share/js/kvm/ocr.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -137,10 +137,10 @@ export function Ocr(__getGeometry) {
137137
let rel_bottom = Math.max(__start_pos.y, __end_pos.y) - rect.top + offset;
138138
let geo = __getGeometry();
139139
__sel = {
140-
"left": tools.remap(rel_left, geo.x, geo.width, 0, geo.real_width),
141-
"right": tools.remap(rel_right, geo.x, geo.width, 0, geo.real_width),
142-
"top": tools.remap(rel_top, geo.y, geo.height, 0, geo.real_height),
143-
"bottom": tools.remap(rel_bottom, geo.y, geo.height, 0, geo.real_height),
140+
"left": tools.remap(rel_left - geo.x, 0, geo.width, 0, geo.real_width),
141+
"right": tools.remap(rel_right - geo.x, 0, geo.width, 0, geo.real_width),
142+
"top": tools.remap(rel_top - geo.y, 0, geo.height, 0, geo.real_height),
143+
"bottom": tools.remap(rel_bottom - geo.y, 0, geo.height, 0, geo.real_height),
144144
};
145145
} else {
146146
__sel = null;

web/share/js/tools.js

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -136,14 +136,9 @@ export var tools = new function() {
136136
return `${hours}:${mins}:${secs}.${millis}`;
137137
};
138138

139-
self.remap = function(x, a1, b1, a2, b2) {
140-
let remapped = Math.round((x - a1) / b1 * (b2 - a2) + a2);
141-
if (remapped < a2) {
142-
return a2;
143-
} else if (remapped > b2) {
144-
return b2;
145-
}
146-
return remapped;
139+
self.remap = function(value, in_min, in_max, out_min, out_max) {
140+
let result = Math.round((value - in_min) * (out_max - out_min) / ((in_max - in_min) || 1) + out_min);
141+
return Math.min(Math.max(result, out_min), out_max);
147142
};
148143

149144
self.getRandomInt = function(min, max) {

0 commit comments

Comments
 (0)