1
0
mirror of https://github.com/TomHarte/CLK.git synced 2024-12-01 11:49:58 +00:00
CLK/Machines/Apple/ADB/Mouse.cpp

77 lines
2.3 KiB
C++
Raw Normal View History

//
// Mouse.cpp
// Clock Signal
//
// Created by Thomas Harte on 12/02/2021.
// Copyright © 2021 Thomas Harte. All rights reserved.
//
#include "Mouse.hpp"
2021-11-26 23:10:29 +00:00
#include <algorithm>
using namespace Apple::ADB;
Mouse::Mouse(Bus &bus) : ReactiveDevice(bus, 3) {}
2021-02-16 01:49:16 +00:00
void Mouse::perform_command(const Command &command) {
2023-08-22 02:12:55 +00:00
// Mouse deltas are confined to a seven-bit signed field; this implementation keeps things symmetrical by
// limiting them to a maximum absolute value of 63 in any direction.
static constexpr int16_t max_delta = 63;
2023-08-20 19:32:48 +00:00
2021-02-16 01:49:16 +00:00
if(command.type == Command::Type::Talk && command.reg == 0) {
2023-08-20 19:32:48 +00:00
// Read and clamp current deltas and buttons.
//
// There's some small chance of creating negative feedback here — taking too much off delta_x_ or delta_y_
// due to a change in the underlying value between the load and the arithmetic. But if that occurs it means
// the user moved the mouse again in the interim, so it'll just play out as very slight latency.
auto delta_x = delta_x_.load(std::memory_order::memory_order_relaxed);
auto delta_y = delta_y_.load(std::memory_order::memory_order_relaxed);
if(abs(delta_x) > max_delta || abs(delta_y) > max_delta) {
int max = std::max(abs(delta_x), abs(delta_y));
delta_x = delta_x * max_delta / max;
delta_y = delta_y * max_delta / max;
}
2021-02-16 01:49:16 +00:00
2023-08-20 19:32:48 +00:00
const int buttons = button_flags_.load(std::memory_order::memory_order_relaxed);
delta_x_ -= delta_x;
delta_y_ -= delta_y;
2021-02-16 01:49:16 +00:00
// Figure out what that would look like, and don't respond if there's
2023-08-20 19:32:48 +00:00
// no change or deltas to report.
2021-02-16 01:49:16 +00:00
const uint16_t reg0 =
((buttons & 1) ? 0x0000 : 0x8000) |
((buttons & 2) ? 0x0000 : 0x0080) |
uint16_t(delta_x & 0x7f) |
uint16_t((delta_y & 0x7f) << 8);
2023-08-20 19:32:48 +00:00
if(!(reg0 & 0x7f7f) && (reg0 & 0x8080) == (last_posted_reg0_ & 0x8080)) return;
2021-02-16 01:49:16 +00:00
// Post change.
last_posted_reg0_ = reg0;
post_response({uint8_t(reg0 >> 8), uint8_t(reg0)});
}
}
void Mouse::move(int x, int y) {
delta_x_ += int16_t(x);
delta_y_ += int16_t(y);
post_service_request();
}
int Mouse::get_number_of_buttons() {
return 2;
}
void Mouse::set_button_pressed(int index, bool is_pressed) {
if(is_pressed)
button_flags_ |= (1 << index);
else
button_flags_ &= ~(1 << index);
post_service_request();
}
void Mouse::reset_all_buttons() {
button_flags_ = 0;
post_service_request();
}