2023-08-15 08:23:37 +00:00
|
|
|
/**
|
|
|
|
* @file App.cpp
|
|
|
|
* @author apio (cloudapio.eu)
|
|
|
|
* @brief UI application event loop.
|
|
|
|
*
|
|
|
|
* @copyright Copyright (c) 2023, the Luna authors.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <os/ArgumentParser.h>
|
2023-08-15 09:20:17 +00:00
|
|
|
#include <os/File.h>
|
2023-08-15 08:23:37 +00:00
|
|
|
#include <os/IPC.h>
|
|
|
|
#include <ui/App.h>
|
2023-08-15 09:20:17 +00:00
|
|
|
#include <ui/ipc/Client.h>
|
|
|
|
|
|
|
|
Result<void> handle_ipc_client_event(os::LocalClient&, u8 id)
|
|
|
|
{
|
|
|
|
return ui::App::the().handle_ipc_event(id);
|
|
|
|
}
|
2023-08-15 08:23:37 +00:00
|
|
|
|
|
|
|
namespace ui
|
|
|
|
{
|
|
|
|
App* App::s_app { nullptr };
|
|
|
|
|
|
|
|
App::App()
|
|
|
|
{
|
|
|
|
s_app = this;
|
|
|
|
}
|
|
|
|
|
|
|
|
App::~App()
|
|
|
|
{
|
|
|
|
s_app = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
Result<void> App::init(int argc, char** argv)
|
|
|
|
{
|
|
|
|
StringView socket_path = "/tmp/wind.sock";
|
|
|
|
|
|
|
|
os::ArgumentParser parser;
|
|
|
|
parser.add_description("A UI application."_sv);
|
|
|
|
parser.add_system_program_info(argv[0]);
|
|
|
|
parser.add_value_argument(socket_path, 's', "socket"_sv, "the path for the local IPC socket"_sv);
|
|
|
|
parser.parse(argc, argv);
|
|
|
|
|
|
|
|
m_client = TRY(os::LocalClient::connect(socket_path, true));
|
|
|
|
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
Result<int> App::run()
|
|
|
|
{
|
2023-08-15 09:20:17 +00:00
|
|
|
check(m_main_window);
|
2023-08-15 08:23:37 +00:00
|
|
|
while (!m_should_close) { TRY(os::IPC::check_for_messages(*m_client)); }
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
App& App::the()
|
|
|
|
{
|
|
|
|
check(s_app);
|
|
|
|
return *s_app;
|
|
|
|
}
|
2023-08-15 09:20:17 +00:00
|
|
|
|
|
|
|
Result<void> App::register_window(OwnedPtr<Window>&& window, Badge<Window>)
|
|
|
|
{
|
|
|
|
int id = window->id();
|
|
|
|
check(TRY(m_windows.try_set(id, move(window))));
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
void App::unregister_window(Window* window, Badge<Window>)
|
|
|
|
{
|
|
|
|
int id = window->id();
|
|
|
|
check(m_windows.try_remove(id));
|
|
|
|
}
|
|
|
|
|
|
|
|
Window* App::find_window(int id)
|
|
|
|
{
|
|
|
|
auto* window = m_windows.try_get_ref(id);
|
|
|
|
check(window);
|
|
|
|
return window->ptr();
|
|
|
|
}
|
|
|
|
|
|
|
|
Result<void> App::handle_ipc_event(u8 id)
|
|
|
|
{
|
|
|
|
switch (id)
|
|
|
|
{
|
|
|
|
case WINDOW_CLOSE_REQUEST_ID: {
|
|
|
|
WindowCloseRequest request;
|
|
|
|
TRY(m_client->recv_typed(request));
|
|
|
|
os::eprintln("ui: Window close request from server! Shall comply.");
|
|
|
|
auto* window = find_window(request.window);
|
|
|
|
window->close();
|
|
|
|
return {};
|
|
|
|
}
|
2023-08-15 09:31:01 +00:00
|
|
|
case MOUSE_EVENT_REQUEST_ID: {
|
|
|
|
MouseEventRequest request;
|
|
|
|
TRY(m_client->recv_typed(request));
|
2023-08-15 10:28:47 +00:00
|
|
|
auto* window = find_window(request.window);
|
|
|
|
window->handle_mousemove(request.position);
|
|
|
|
window->draw();
|
2023-08-15 09:31:01 +00:00
|
|
|
return {};
|
|
|
|
}
|
2023-08-15 09:20:17 +00:00
|
|
|
default: fail("Unexpected IPC request from server!");
|
|
|
|
}
|
|
|
|
}
|
2023-08-15 08:23:37 +00:00
|
|
|
}
|