~xdavidwu/motion-control

c3747ebc7d103a2848b171becb7a00758ab3bddc — xdavidwu 2 years ago 56cfac0
pointerc: support button events
1 files changed, 42 insertions(+), 25 deletions(-)

M tools/pointerc/main.ha
M tools/pointerc/main.ha => tools/pointerc/main.ha +42 -25
@@ 6,10 6,21 @@ use net::udp;
use os;
use proto::control;
use strconv;
use strings;
use time;

fn send(sock: io::file, bytes: []u8) void = {
	match (udp::send(sock, bytes)) {
	case let err: net::error =>
		fmt::fatalf("Cannot send(): {}", net::strerror(err));
	case size =>
		yield;
	};
};

export fn main() int = {
	if (len(os::args) != 5) {
		fmt::fatalf("Usage: {} addr port x y", os::args[0]);
	if (len(os::args) > 5 || len(os::args) < 4) {
		fmt::fatalf("Usage: {} addr port (x y | left | right)", os::args[0]);
	};
	const addr = match (ip::parse(os::args[1])) {
	case let addr: ip::addr =>


@@ 23,24 34,6 @@ export fn main() int = {
	case =>
		fmt::fatalf("Invalid port {}", os::args[2]);
	};
	const x = match (strconv::stoi8(os::args[3])) {
	case let n: i8 =>
		yield n;
	case =>
		fmt::fatalf("Invalid x {}", os::args[3]);
	};
	const y = match (strconv::stoi8(os::args[4])) {
	case let n: i8 =>
		yield n;
	case =>
		fmt::fatalf("Invalid y {}", os::args[4]);
	};

	const buf = control::request {
		code = control::code::RELATIVE_MOVEMENT,
		x = x,
		y = y,
	};

	const sock = match (udp::connect(addr, port)) {
	case let s: io::file =>


@@ 50,11 43,35 @@ export fn main() int = {
	};
	defer io::close(sock)!;

	match (udp::send(sock, buf.bytes)) {
	case let err: net::error =>
		fmt::fatalf("Cannot send(): {}", net::strerror(err));
	case size =>
		yield;
	let buf = control::request {...};

	if (strings::compare(os::args[3], "left") == 0) {
		buf.code = control::code::KEY_PRESSED;
		buf.key = control::key::BTN_LEFT;
		send(sock, buf.bytes);
		buf.code = control::code::KEY_RELEASED;
		send(sock, buf.bytes);
	} else if (strings::compare(os::args[3], "right") == 0) {
		buf.code = control::code::KEY_PRESSED;
		buf.key = control::key::BTN_RIGHT;
		send(sock, buf.bytes);
		buf.code = control::code::KEY_RELEASED;
		send(sock, buf.bytes);
	} else {
		buf.code = control::code::RELATIVE_MOVEMENT;
		buf.x = match (strconv::stoi8(os::args[3])) {
		case let n: i8 =>
			yield n;
		case =>
			fmt::fatalf("Invalid x {}", os::args[3]);
		};
		buf.y = match (strconv::stoi8(os::args[4])) {
		case let n: i8 =>
			yield n;
		case =>
			fmt::fatalf("Invalid y {}", os::args[4]);
		};
		send(sock, buf.bytes);
	};
	return 0;
};