/** * @file Window.cpp * @author apio (cloudapio.eu) * @brief UI windows. * * @copyright Copyright (c) 2023, the Luna authors. * */ #include #include #include #include #include #include #include static Result create_shm_region(const char* path, int* outfd, ui::Rect rect) { int fd = shm_open(path, O_RDWR, 0600); shm_unlink(path); if (fd < 0) return err(errno); usize size = rect.width * rect.height * 4; // 4 bytes per pixel void* p = mmap(nullptr, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); if (p == MAP_FAILED) { shm_unlink(path); close(fd); return 0; } if (outfd) *outfd = fd; else close(fd); return (u32*)p; } namespace ui { Result Window::create(Rect rect) { auto window = TRY(make_owned()); ui::CreateWindowRequest request; request.rect = rect; auto response = TRY(os::IPC::send_sync(App::the().client(), request)); u32* pixels = TRY(create_shm_region(response.shm_path, nullptr, rect)); window->m_canvas = ui::Canvas { rect.width, rect.height, rect.width, (u8*)pixels }; window->m_id = response.window; Window* p = window.ptr(); App::the().register_window(move(window), {}); return p; } Window::~Window() { if (m_canvas.ptr) munmap(m_canvas.ptr, ((usize)m_canvas.width) * ((usize)m_canvas.height) * 4); } void Window::set_title(StringView title) { ui::SetWindowTitleRequest request; request.window = m_id; SET_IPC_STRING(request.title, title.chars()); os::IPC::send_async(App::the().client(), request); } void Window::update() { ui::InvalidateRequest request; request.window = m_id; os::IPC::send_async(App::the().client(), request); } void Window::close() { App& app = App::the(); ui::CloseWindowRequest request; request.window = m_id; os::IPC::send_async(app.client(), request); if (this == app.main_window()) app.set_should_close(true); app.unregister_window(this, {}); } Result Window::draw() { m_canvas.fill(m_background); if (m_main_widget) return m_main_widget->draw(m_canvas); update(); return {}; } Result Window::handle_mousemove(ui::Point position) { if (m_main_widget) TRY(m_main_widget->handle_mousemove(position)); return {}; } }