Luna/wind/IPC.cpp

76 lines
1.9 KiB
C++
Raw Normal View History

2023-08-14 16:15:29 +00:00
#include "IPC.h"
#include <luna/String.h>
#include <os/File.h>
static Result<void> handle_create_window_message(Client& client)
{
ui::CreateWindowRequest request;
auto rc = client.conn.recv_typed(request);
if (rc.has_error())
{
if (rc.error() == EAGAIN)
{
client.rpc_in_progress = true;
client.rpc_id = ui::CREATE_WINDOW_ID;
return {};
}
else
return rc.release_error();
}
request.rect = request.rect.normalized();
request.rect.height += Window::titlebar_height(); // Make sure we provide the full contents rect that was asked for.
auto name = COPY_IPC_STRING(request.name);
auto* window = new (std::nothrow) Window(request.rect, request.color, move(name));
if (!window)
{
os::IPC::send_error(client.conn, ENOMEM);
return {};
}
int id = static_cast<int>(client.windows.size());
rc = client.windows.try_append(window);
if (rc.has_error())
{
delete window;
os::IPC::send_error(client.conn, rc.error());
return {};
}
ui::CreateWindowResponse response;
response.window = id;
os::IPC::send_async(client.conn, response);
return {};
}
namespace wind
{
Result<void> handle_ipc_message(Client& client, u8 id)
{
client.rpc_in_progress = false;
switch (id)
{
case ui::CREATE_WINDOW_ID: return handle_create_window_message(client);
default: os::eprintln("wind: Invalid IPC message from client!"); return err(EINVAL);
}
}
Result<void> handle_ipc(Client& client)
{
if (client.rpc_in_progress) return handle_ipc_message(client, client.rpc_id);
u8 id;
auto rc = client.conn.recv_typed(id);
if (rc.has_error())
{
if (rc.error() == EAGAIN) { return {}; }
else
return rc.release_error();
}
return handle_ipc_message(client, id);
}
}