/** * @file App.cpp * @author apio (cloudapio.eu) * @brief UI application event loop. * * @copyright Copyright (c) 2023, the Luna authors. * */ #include #include #include #include #include Result handle_ipc_client_event(os::LocalClient&, u8 id) { return ui::App::the().handle_ipc_event(id); } namespace ui { App* App::s_app { nullptr }; App::App() { s_app = this; } App::~App() { s_app = nullptr; } Result 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 App::run() { check(m_main_window); while (!m_should_close) { TRY(os::IPC::check_for_messages(*m_client)); } return 0; } App& App::the() { check(s_app); return *s_app; } Result App::register_window(OwnedPtr&& window, Badge) { int id = window->id(); check(TRY(m_windows.try_set(id, move(window)))); return {}; } void App::unregister_window(Window* window, Badge) { 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 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 {}; } case MOUSE_EVENT_REQUEST_ID: { MouseEventRequest request; TRY(m_client->recv_typed(request)); os::eprintln("ui: Mouse move to (%d, %d)", request.position.x, request.position.y); return {}; } default: fail("Unexpected IPC request from server!"); } } }