Skip to content

Commit

Permalink
grund-device: Initial implementation of keyboard event broadcast.
Browse files Browse the repository at this point in the history
  • Loading branch information
sleepy-monax committed Dec 26, 2024
1 parent 239f46c commit 2413bf1
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 11 deletions.
19 changes: 19 additions & 0 deletions src/libs/karm-io/pack.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <karm-base/enum.h>
#include <karm-base/tuple.h>
#include <karm-base/vec.h>
#include <karm-math/vec.h>
#include <karm-meta/nocopy.h>
#include <karm-meta/visit.h>
#include <karm-sys/_handle.h>
Expand Down Expand Up @@ -275,4 +276,22 @@ struct Packer<Tuple<Ts...>> {
}
};

// MARK: Math ------------------------------------------------------------------

template <typename T>
struct Packer<Math::Vec2<T>> {
static Res<> pack(PackEmit &e, Math::Vec2<T> const &val) {
try$(Io::pack(e, val.x));
try$(Io::pack(e, val.y));
return Ok();
}

static Res<Math::Vec2<T>> unpack(PackScan &s) {
return Ok(Math::Vec2<T>{
try$(Io::unpack<T>(s)),
try$(Io::unpack<T>(s)),
});
}
};

} // namespace Karm::Io
11 changes: 10 additions & 1 deletion src/libs/karm-sys/rpc.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,25 @@ namespace Karm::Sys {
struct Port : public Distinct<u64, struct _PortTag> {
static Port const INVALID;
static Port const BUS;
static Port const BROADCAST;

using Distinct::Distinct;

void repr(Io::Emit &e) const {
e("{}", value());
if (*this == INVALID)
e("invalid");
else if (*this == BUS)
e("bus");
else if (*this == BROADCAST)
e("broadcast");
else
e("{}", value());
}
};

constexpr Port Port::INVALID{0};
constexpr Port Port::BUS{Limits<u64>::MAX};
constexpr Port Port::BROADCAST{Limits<u64>::MAX - 1};

struct Header {
u64 seq;
Expand Down
27 changes: 23 additions & 4 deletions src/srvs/grund-bus/bus.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,10 @@ Async::Task<> Service::runAsync() {
auto msg = co_trya$(Sys::rpcRecvAsync(_con));

auto res = dispatch(msg);
if (not res)
if (not res) {
logError("{}: dispatch failed: {}", id(), res);
co_try$(Sys::rpcSend<Error>(_con, port(), msg.header().seq, res.none()));
}
}
}

Expand Down Expand Up @@ -154,7 +156,8 @@ Res<> Locator::send(Sys::Message &msg) {
return Error::notFound("service not found");
}

return Error::invalidInput("invalid message");
logWarn("unexpected message: {}", msg.header());
return Ok();
}

// MARK: Bus -------------------------------------------------------------------
Expand All @@ -178,9 +181,25 @@ Karm::Res<> Bus::attach(Strong<Endpoint> endpoint) {

Res<> Bus::dispatch(Sys::Message &msg) {
for (auto &endpoint : _endpoints) {
if (endpoint->port() == msg.header().to)
return endpoint->send(msg);
bool broadcast = msg.header().to == Sys::Port::BROADCAST and
msg.header().from != endpoint->port();

if (broadcast or endpoint->port() == msg.header().to) {
auto res = endpoint->send(msg);
if (not res) {
logError("{}: send failed: {}", endpoint->id(), res);
if (not broadcast)
return res;
}

if (not broadcast)
return Ok();
}
}

if (msg.header().to == Sys::Port::BROADCAST)
return Ok();

return Error::notFound("service not found");
}

Expand Down
32 changes: 30 additions & 2 deletions src/srvs/grund-device/main.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
#include <hjert-api/api.h>
#include <karm-app/inputs.h>
#include <karm-base/map.h>
#include <karm-logger/logger.h>
#include <karm-sys/entry.h>
#include <karm-sys/rpc.h>

#include "cmos.h"
#include "io.h"
Expand All @@ -21,11 +23,37 @@ struct IsaRootBus : public Node {
}
};

struct RootBus : public Node {
Sys::Rpc &rpc;

RootBus(Sys::Rpc &rpc)
: rpc{rpc} {}

Res<> init() override {
try$(attach(makeStrong<IsaRootBus>()));
return Ok();
}

Res<> bubble(App::Event &e) override {
if (auto me = e.is<App::MouseEvent>()) {
try$(rpc.send<App::MouseEvent>(Sys::Port::BROADCAST, *me));
e.accept();
} else if (auto ke = e.is<App::KeyboardEvent>()) {
try$(rpc.send<App::KeyboardEvent>(Sys::Port::BROADCAST, *ke));
e.accept();
}

return Node::bubble(e);
}
};

} // namespace Grund::Device

Async::Task<> entryPointAsync(Sys::Context &) {
Async::Task<> entryPointAsync(Sys::Context &ctx) {
auto rpc = Sys::Rpc::create(ctx);

logInfo("devices: building device tree...");
auto root = makeStrong<Grund::Device::IsaRootBus>();
auto root = makeStrong<Grund::Device::RootBus>(rpc);
co_try$(root->init());

logInfo("devices: binding IRQs...");
Expand Down
16 changes: 14 additions & 2 deletions src/srvs/grund-device/ps2.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <karm-app/inputs.h>
#include <karm-logger/logger.h>
#include <karm-sys/rpc.h>

#include "ps2.h"

Expand Down Expand Up @@ -110,12 +111,24 @@ Res<> Keyboard::event(App::Event &e) {
if (_esc) {
App::Key key = {App::Key::Code((data & 0x7F) + 0x80)};
logInfo("ps2: keyboard key {} {}", key.name(), data & 0x80 ? "pressed" : "released");
auto event = App::makeEvent<App::KeyboardEvent>(
data & 0x80 ? App::KeyboardEvent::PRESS : App::KeyboardEvent::RELEASE,
key,
key
);
try$(bubble(*event));
_esc = false;
} else if (data == 0xE0) {
_esc = true;
} else {
App::Key key = {App::Key::Code(data & 0x7F)};
logInfo("ps2: keyboard key {} {}", key.name(), data & 0x80 ? "pressed" : "released");
auto event = App::makeEvent<App::KeyboardEvent>(
data & 0x80 ? App::KeyboardEvent::PRESS : App::KeyboardEvent::RELEASE,
key,
key
);
try$(bubble(*event));
}
status = try$(ctrl().readStatus());
}
Expand Down Expand Up @@ -170,9 +183,8 @@ Res<> Mouse::decode() {
offy -= 0x100;

int scroll = 0;
if (_hasWheel) {
if (_hasWheel)
scroll = (i8)_buf[3];
}

logInfo("ps2: mouse move {} {} {}", offx, offy, scroll);
return Ok();
Expand Down
3 changes: 1 addition & 2 deletions src/srvs/grund-shell/host.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,8 @@ struct RootHost :
}

void flip(Slice<Math::Recti> dirty) override {
for (auto d : dirty) {
for (auto d : dirty)
Gfx::blitUnsafe(_front.clip(d), _back->pixels().clip(d));
}
}

Res<> wait(TimeStamp until) override {
Expand Down

0 comments on commit 2413bf1

Please sign in to comment.