diff --git a/device-ui/CMakeLists.txt b/device-ui/CMakeLists.txt index 8e6ddd5..2336588 100644 --- a/device-ui/CMakeLists.txt +++ b/device-ui/CMakeLists.txt @@ -81,6 +81,9 @@ if(CP0_UI_BUILD_APP) endif() if(CP0_UI_TARGET_DEVICE) + if(NOT CMAKE_SYSTEM_NAME STREQUAL "Linux") + message(FATAL_ERROR "CP0_UI_TARGET_DEVICE=ON requires Linux (fbdev/evdev); got CMAKE_SYSTEM_NAME=${CMAKE_SYSTEM_NAME}") + endif() target_compile_definitions(cp0-sync-ui PRIVATE CP0_UI_TARGET_DEVICE=1) message(STATUS "Target backend: Linux fbdev + evdev") else() diff --git a/device-ui/src/app_ui.cpp b/device-ui/src/app_ui.cpp index 155da59..f2c68de 100644 --- a/device-ui/src/app_ui.cpp +++ b/device-ui/src/app_ui.cpp @@ -713,11 +713,39 @@ struct LvglAppView::Impl { #if defined(CP0_HAS_LVGL) lv_obj_t* screen = nullptr; - lv_style_t style_bg; - lv_style_t style_card; + lv_style_t style_bg{}; + lv_style_t style_card{}; bool lvgl_styles_inited = false; + + void sync_lvgl_styles() { + if (!lv_is_initialized()) { + lv_init(); + } + if (!lvgl_styles_inited) { + lv_style_init(&style_bg); + lv_style_init(&style_card); + lvgl_styles_inited = true; + } + lv_style_set_bg_color(&style_bg, lv_color_hex(pal.background)); + lv_style_set_bg_color(&style_card, lv_color_hex(pal.card)); + lv_style_set_border_color(&style_card, lv_color_hex(pal.border)); + if (screen != nullptr) { + lv_obj_report_style_change(&style_bg); + lv_obj_report_style_change(&style_card); + } + } #endif + ~Impl() { +#if defined(CP0_HAS_LVGL) + if (lvgl_styles_inited) { + lv_style_reset(&style_bg); + lv_style_reset(&style_card); + lvgl_styles_inited = false; + } +#endif + } + void render_top_bar(const UiState& state) { // Segmented TabsList pill container (radius 5px, padding 2px) const char* tab_labels[3] = {"1 Status", "2 Folders", "3 Logs"}; @@ -887,15 +915,15 @@ struct LvglAppView::Impl { std::string footer_msg; uint32_t footer_color = pal.muted_foreground; - if (!state.feedback_banner.empty()) { - footer_msg = state.feedback_banner; - footer_color = pal.status_online; - } else if (state.status.syncing && !state.status.current_file.empty()) { + if (state.status.syncing && !state.status.current_file.empty()) { footer_msg = "File: " + state.status.current_file; footer_color = pal.foreground; } else if (!state.status.last_error.empty() && state.status.last_error != "none") { footer_msg = "Err: " + state.status.last_error; footer_color = pal.status_error; + } else if (!state.status.ok && !state.status.error.empty()) { + footer_msg = state.status.error; + footer_color = pal.status_error; } else if (!state.status.last_synced_file.empty()) { footer_msg = "Last sync: " + state.status.last_synced_file; } else { @@ -1279,6 +1307,18 @@ struct LvglAppView::Impl { } } + void render_feedback_toast(const UiState& state) { + if (state.modal != ModalKind::None || state.feedback_banner.empty()) { + return; + } + uint32_t bg = state.feedback_is_error ? pal.status_error_bg : pal.status_online_bg; + uint32_t fg = state.feedback_is_error ? pal.status_error : pal.status_online; + canvas.draw_round_box(6, 129, 308, 19, 5, bg, fg, 1); + canvas.draw_circle(15.0f, 138.5f, 2.5f, fg, false); + std::string fit = canvas.truncate_right(FontRole::Mono, 9, state.feedback_banner, 282); + canvas.draw_text(FontRole::Mono, 9, 23, 134, fit, fg); + } + void render(const UiState& state) { if (state.theme != theme) { theme = state.theme; @@ -1299,22 +1339,12 @@ struct LvglAppView::Impl { break; } + render_feedback_toast(state); render_modal(state); render_footer(state); #if defined(CP0_HAS_LVGL) - if (!lvgl_styles_inited) { - lv_style_init(&style_bg); - lv_style_init(&style_card); - lvgl_styles_inited = true; - } - lv_style_set_bg_color(&style_bg, lv_color_hex(pal.background)); - lv_style_set_bg_color(&style_card, lv_color_hex(pal.card)); - lv_style_set_border_color(&style_card, lv_color_hex(pal.border)); - if (screen != nullptr) { - lv_obj_report_style_change(&style_bg); - lv_obj_report_style_change(&style_card); - } + sync_lvgl_styles(); #endif } }; @@ -1325,6 +1355,9 @@ LvglAppView::~LvglAppView() = default; void LvglAppView::apply_theme(ThemeMode mode) { impl_->theme = mode; impl_->pal = get_palette(mode); +#if defined(CP0_HAS_LVGL) + impl_->sync_lvgl_styles(); +#endif } void LvglAppView::render(const UiState& state) { diff --git a/device-ui/src/fb_blit.hpp b/device-ui/src/fb_blit.hpp new file mode 100644 index 0000000..fad6b89 --- /dev/null +++ b/device-ui/src/fb_blit.hpp @@ -0,0 +1,145 @@ +#pragma once + +#include +#include +#include +#include +#include + +namespace cp0 { + +struct FbChannel { + uint32_t offset = 0; + uint32_t length = 0; +}; + +struct FbBlitSpec { + uint32_t xres = 0; + uint32_t yres = 0; + uint32_t xoffset = 0; + uint32_t yoffset = 0; + uint32_t bits_per_pixel = 0; + uint32_t line_length = 0; + FbChannel red; + FbChannel green; + FbChannel blue; +}; + +inline uint32_t pack_fb_channel(uint32_t val8, const FbChannel& ch) { + uint32_t scaled = (ch.length >= 8) + ? (val8 << (ch.length - 8)) + : (val8 >> (8 - ch.length)); + uint32_t mask = (ch.length >= 32) ? 0xffffffffU : ((1U << ch.length) - 1U); + return (scaled & mask) << ch.offset; +} + +inline bool blit_framebuffer( + uint8_t* fb_mem, + size_t map_size, + const FbBlitSpec& spec, + const uint32_t* pixels, + int src_w, + int src_h +) { + if (fb_mem == nullptr || pixels == nullptr || map_size == 0 || src_w <= 0 || src_h <= 0) { + return false; + } + if (spec.bits_per_pixel != 16 && spec.bits_per_pixel != 24 && spec.bits_per_pixel != 32) { + return false; + } + auto valid_channel = [&](const FbChannel& ch) { + return ch.length > 0 && ch.length <= 16 && + ch.offset < spec.bits_per_pixel && + ch.offset + ch.length <= spec.bits_per_pixel; + }; + if (!valid_channel(spec.red) || !valid_channel(spec.green) || !valid_channel(spec.blue)) { + return false; + } + + size_t copy_w = std::min(static_cast(spec.xres), static_cast(src_w)); + size_t copy_h = std::min(static_cast(spec.yres), static_cast(src_h)); + if (copy_w == 0 || copy_h == 0) { + return false; + } + + size_t bytes_per_pixel = static_cast(spec.bits_per_pixel) / 8U; + size_t stride = static_cast(spec.line_length); + if (copy_w > std::numeric_limits::max() / bytes_per_pixel) { + return false; + } + size_t row_bytes = copy_w * bytes_per_pixel; + if (stride < row_bytes) { + return false; + } + if (static_cast(spec.xoffset) > std::numeric_limits::max() / bytes_per_pixel) { + return false; + } + size_t x_byte_offset = static_cast(spec.xoffset) * bytes_per_pixel; + if (x_byte_offset > stride - row_bytes) { + return false; + } + + bool fast_argb32 = + (spec.bits_per_pixel == 32 && + spec.red.offset == 16 && spec.red.length == 8 && + spec.green.offset == 8 && spec.green.length == 8 && + spec.blue.offset == 0 && spec.blue.length == 8); + + for (size_t y = 0; y < copy_h; ++y) { + size_t y_row = static_cast(spec.yoffset) + y; + if (y_row < static_cast(spec.yoffset)) { + return false; + } + if (y_row > std::numeric_limits::max() / stride) { + return false; + } + size_t row_base = y_row * stride; + if (row_base > map_size || x_byte_offset > map_size - row_base) { + return false; + } + size_t row_offset = row_base + x_byte_offset; + if (row_bytes > map_size - row_offset) { + return false; + } + + uint8_t* row = fb_mem + row_offset; + const uint32_t* src = pixels + y * static_cast(src_w); + + if (fast_argb32) { + std::memcpy(row, src, row_bytes); + } else if (spec.bits_per_pixel == 32) { + for (size_t x = 0; x < copy_w; ++x) { + uint32_t c = src[x]; + uint32_t packed = + pack_fb_channel((c >> 16) & 0xffU, spec.red) | + pack_fb_channel((c >> 8) & 0xffU, spec.green) | + pack_fb_channel(c & 0xffU, spec.blue); + std::memcpy(row + x * 4U, &packed, 4U); + } + } else if (spec.bits_per_pixel == 24) { + for (size_t x = 0; x < copy_w; ++x) { + uint32_t c = src[x]; + uint32_t packed = + pack_fb_channel((c >> 16) & 0xffU, spec.red) | + pack_fb_channel((c >> 8) & 0xffU, spec.green) | + pack_fb_channel(c & 0xffU, spec.blue); + row[x * 3U + 0U] = static_cast(packed & 0xffU); + row[x * 3U + 1U] = static_cast((packed >> 8) & 0xffU); + row[x * 3U + 2U] = static_cast((packed >> 16) & 0xffU); + } + } else { + for (size_t x = 0; x < copy_w; ++x) { + uint32_t c = src[x]; + uint16_t packed = static_cast( + pack_fb_channel((c >> 16) & 0xffU, spec.red) | + pack_fb_channel((c >> 8) & 0xffU, spec.green) | + pack_fb_channel(c & 0xffU, spec.blue) + ); + std::memcpy(row + x * 2U, &packed, 2U); + } + } + } + return true; +} + +} // namespace cp0 diff --git a/device-ui/src/main.cpp b/device-ui/src/main.cpp index 63a4cf4..46207f4 100644 --- a/device-ui/src/main.cpp +++ b/device-ui/src/main.cpp @@ -1,9 +1,14 @@ #include "app_ui.hpp" +#include "fb_blit.hpp" #include "ipc_client.hpp" #include "ui_controller.hpp" +#include +#include #include #include +#include +#include #include #include #include @@ -101,8 +106,8 @@ bool dispatch_action( return false; } - auto set_timed_feedback = [&](const std::string& msg) { - ctrl.set_feedback(msg); + auto set_timed_feedback = [&](const std::string& msg, bool is_error) { + ctrl.set_feedback(msg, is_error); feedback_until = std::chrono::steady_clock::now() + std::chrono::seconds(3); }; @@ -112,25 +117,38 @@ bool dispatch_action( return true; case ActionType::SyncNow: { auto res = ipc.sync_now(); - set_timed_feedback(res.ok ? "Sync triggered" : res.error); + set_timed_feedback( + res.ok ? "Sync triggered" : (res.error.empty() ? "Sync failed" : res.error), + !res.ok + ); ctrl.update_status(ipc.fetch_status()); return true; } case ActionType::AddFolder: { auto res = ipc.add_folder(action.folder_path, action.folder_label); - set_timed_feedback(res.ok ? "Folder added" : res.error); + set_timed_feedback( + res.ok ? "Folder added" : (res.error.empty() ? "Add folder failed" : res.error), + !res.ok + ); ctrl.update_status(ipc.fetch_status()); return true; } case ActionType::RemoveFolder: { auto res = ipc.remove_folder(action.folder_id); - set_timed_feedback(res.ok ? "Folder removed" : res.error); + set_timed_feedback( + res.ok ? "Folder removed" : (res.error.empty() ? "Remove folder failed" : res.error), + !res.ok + ); ctrl.update_status(ipc.fetch_status()); return true; } case ActionType::SetTarget: { auto res = ipc.set_target(action.target_host, action.target_port); - set_timed_feedback(res.ok ? ("Target: " + action.target_host) : res.error); + set_timed_feedback( + res.ok ? ("Target: " + action.target_host) + : (res.error.empty() ? "Set target failed" : res.error), + !res.ok + ); ctrl.update_status(ipc.fetch_status()); return true; } @@ -141,7 +159,10 @@ bool dispatch_action( } case ActionType::RetryFailed: { auto res = ipc.retry_failed(); - set_timed_feedback(res.ok ? "Retrying failed files" : res.error); + set_timed_feedback( + res.ok ? "Retrying failed files" : (res.error.empty() ? "Retry failed" : res.error), + !res.ok + ); ctrl.update_status(ipc.fetch_status()); return true; } @@ -201,40 +222,36 @@ char map_evdev_code(uint16_t code, bool shift) { case KEY_DOT: return shift ? '>' : '.'; case KEY_SLASH: return shift ? '?' : '/'; case KEY_BACKSLASH: return shift ? '|' : '\\'; + case KEY_GRAVE: return shift ? '~' : '`'; default: return 0; } } void blit_fbdev( uint8_t* fb_mem, + size_t map_size, const fb_var_screeninfo& vinfo, const fb_fix_screeninfo& finfo, const uint32_t* pixels ) { - int fb_w = static_cast(vinfo.xres); - int fb_h = static_cast(vinfo.yres); - int copy_w = std::min(fb_w, cp0::LvglAppView::kWidth); - int copy_h = std::min(fb_h, cp0::LvglAppView::kHeight); - int bpp = static_cast(vinfo.bits_per_pixel); - int stride = static_cast(finfo.line_length); - - for (int y = 0; y < copy_h; ++y) { - uint8_t* row = fb_mem + (y + static_cast(vinfo.yoffset)) * stride + - static_cast(vinfo.xoffset) * (bpp / 8); - const uint32_t* src = pixels + y * cp0::LvglAppView::kWidth; - if (bpp == 32) { - std::memcpy(row, src, static_cast(copy_w) * 4); - } else if (bpp == 16) { - auto* dst16 = reinterpret_cast(row); - for (int x = 0; x < copy_w; ++x) { - uint32_t c = src[x]; - uint16_t r = static_cast((c >> 19) & 0x1fU); - uint16_t g = static_cast((c >> 10) & 0x3fU); - uint16_t b = static_cast((c >> 3) & 0x1fU); - dst16[x] = static_cast((r << 11) | (g << 5) | b); - } - } - } + cp0::FbBlitSpec spec; + spec.xres = vinfo.xres; + spec.yres = vinfo.yres; + spec.xoffset = vinfo.xoffset; + spec.yoffset = vinfo.yoffset; + spec.bits_per_pixel = vinfo.bits_per_pixel; + spec.line_length = finfo.line_length; + spec.red = {vinfo.red.offset, vinfo.red.length}; + spec.green = {vinfo.green.offset, vinfo.green.length}; + spec.blue = {vinfo.blue.offset, vinfo.blue.length}; + cp0::blit_framebuffer( + fb_mem, + map_size, + spec, + pixels, + cp0::LvglAppView::kWidth, + cp0::LvglAppView::kHeight + ); } std::vector open_keyboard_evdevs() { @@ -294,6 +311,11 @@ int run_device_loop(cp0::IpcClient& ipc, cp0::UiController& ctrl, cp0::LvglAppVi } size_t map_size = finfo.smem_len; + if (map_size == 0) { + std::cerr << "Framebuffer reports zero memory length\n"; + ::close(fb_fd); + return 1; + } void* mapped = ::mmap(nullptr, map_size, PROT_READ | PROT_WRITE, MAP_SHARED, fb_fd, 0); if (mapped == MAP_FAILED) { std::cerr << "Failed to mmap framebuffer\n"; @@ -312,7 +334,7 @@ int run_device_loop(cp0::IpcClient& ipc, cp0::UiController& ctrl, cp0::LvglAppVi ctrl.update_status(ipc.fetch_status()); view.apply_theme(ctrl.state().theme); view.render(ctrl.state()); - blit_fbdev(fb_mem, vinfo, finfo, view.pixels()); + blit_fbdev(fb_mem, map_size, vinfo, finfo, view.pixels()); bool running = true; bool shift_down = false; @@ -323,78 +345,115 @@ int run_device_loop(cp0::IpcClient& ipc, cp0::UiController& ctrl, cp0::LvglAppVi bool dirty = false; if (!pfds.empty()) { int prc = ::poll(pfds.data(), static_cast(pfds.size()), 50); - if (prc > 0) { - for (const auto& p : pfds) { - if ((p.revents & POLLIN) == 0) { - continue; - } - struct input_event ev{}; - while (::read(p.fd, &ev, sizeof(ev)) == sizeof(ev)) { - if (ev.type != EV_KEY) { - continue; - } - if (ev.code == KEY_LEFTSHIFT || ev.code == KEY_RIGHTSHIFT) { - shift_down = (ev.value != 0); - continue; - } - if (ev.value != 1 && ev.value != 2) { - continue; - } - cp0::IpcAction act; - bool handled = true; - switch (ev.code) { - case KEY_UP: - act = ctrl.handle_key(0, cp0::KeyCode::Up); - break; - case KEY_DOWN: - act = ctrl.handle_key(0, cp0::KeyCode::Down); - break; - case KEY_LEFT: - act = ctrl.handle_key(0, cp0::KeyCode::Left); - break; - case KEY_RIGHT: - act = ctrl.handle_key(0, cp0::KeyCode::Right); - break; - case KEY_ENTER: - case KEY_KPENTER: - act = ctrl.handle_key(0, cp0::KeyCode::Enter); - break; - case KEY_ESC: - act = ctrl.handle_key(0, cp0::KeyCode::Escape); - break; - case KEY_BACKSPACE: - act = ctrl.handle_key(0, cp0::KeyCode::Backspace); - break; - case KEY_DELETE: - act = ctrl.handle_key(0, cp0::KeyCode::Delete); - break; - case KEY_SPACE: - act = ctrl.handle_key(' ', cp0::KeyCode::Space); - break; - default: { - char c = map_evdev_code(ev.code, shift_down); - if (c != 0) { - act = ctrl.handle_key(c, cp0::KeyCode::Char); - } else { - handled = false; + if (prc < 0) { + if (errno != EINTR) { + ::usleep(50000); + } + } else if (prc > 0) { + std::vector dead_indices; + for (size_t idx = 0; idx < pfds.size() && running && !g_signal_stop; ++idx) { + const auto& p = pfds[idx]; + bool remove_fd = (p.revents & (POLLERR | POLLHUP | POLLNVAL)) != 0; + if ((p.revents & POLLIN) != 0) { + while (running && !g_signal_stop) { + struct input_event ev{}; + ssize_t n = ::read(p.fd, &ev, sizeof(ev)); + if (n < 0) { + if (errno == EINTR) { + continue; } + if (errno == EAGAIN || errno == EWOULDBLOCK) { + break; + } + remove_fd = true; break; } - } - if (handled) { - dirty = true; - if (ctrl.consume_peer_refresh()) { - ctrl.set_peers(ipc.list_tailscale_peers()); + if (n == 0) { + remove_fd = true; + break; + } + if (n != static_cast(sizeof(ev))) { + break; + } + if (ev.type != EV_KEY) { + continue; + } + if (ev.code == KEY_LEFTSHIFT || ev.code == KEY_RIGHTSHIFT) { + shift_down = (ev.value != 0); + continue; + } + if (ev.value != 1 && ev.value != 2) { + continue; + } + cp0::IpcAction act; + bool handled = true; + switch (ev.code) { + case KEY_UP: + act = ctrl.handle_key(0, cp0::KeyCode::Up); + break; + case KEY_DOWN: + act = ctrl.handle_key(0, cp0::KeyCode::Down); + break; + case KEY_LEFT: + act = ctrl.handle_key(0, cp0::KeyCode::Left); + break; + case KEY_RIGHT: + act = ctrl.handle_key(0, cp0::KeyCode::Right); + break; + case KEY_ENTER: + case KEY_KPENTER: + act = ctrl.handle_key(0, cp0::KeyCode::Enter); + break; + case KEY_ESC: + act = ctrl.handle_key(0, cp0::KeyCode::Escape); + break; + case KEY_BACKSPACE: + act = ctrl.handle_key(0, cp0::KeyCode::Backspace); + break; + case KEY_DELETE: + act = ctrl.handle_key(0, cp0::KeyCode::Delete); + break; + case KEY_SPACE: + act = ctrl.handle_key(' ', cp0::KeyCode::Space); + break; + default: { + char c = map_evdev_code(ev.code, shift_down); + if (c != 0) { + act = ctrl.handle_key(c, cp0::KeyCode::Char); + } else { + handled = false; + } + break; + } + } + if (handled) { + dirty = true; + if (ctrl.consume_peer_refresh()) { + ctrl.set_peers(ipc.list_tailscale_peers()); + } + dispatch_action(ipc, ctrl, view, act, running, feedback_until); + if (!running || g_signal_stop) { + break; + } } - dispatch_action(ipc, ctrl, view, act, running, feedback_until); } } + if (remove_fd) { + ::close(p.fd); + dead_indices.push_back(idx); + } + } + for (auto it = dead_indices.rbegin(); it != dead_indices.rend(); ++it) { + pfds.erase(pfds.begin() + static_cast(*it)); } } } else { ::usleep(50000); } + if (!running || g_signal_stop) { + break; + } auto now = std::chrono::steady_clock::now(); if (!ctrl.state().feedback_banner.empty() && now >= feedback_until) { ctrl.clear_feedback(); @@ -405,14 +464,14 @@ int run_device_loop(cp0::IpcClient& ipc, cp0::UiController& ctrl, cp0::LvglAppVi last_poll = now; dirty = true; } - if (dirty) { + if (running && !g_signal_stop && dirty) { view.render(ctrl.state()); - blit_fbdev(fb_mem, vinfo, finfo, view.pixels()); + blit_fbdev(fb_mem, map_size, vinfo, finfo, view.pixels()); } } - for (int fd : ev_fds) { - ::close(fd); + for (const auto& p : pfds) { + ::close(p.fd); } ::munmap(mapped, map_size); ::close(fb_fd); @@ -516,7 +575,7 @@ int run_sdl_loop( continue; } if (ev.type == SDL_TEXTINPUT) { - for (int i = 0; ev.text.text[i] != '\0'; ++i) { + for (int i = 0; running && !g_signal_stop && ev.text.text[i] != '\0'; ++i) { char ch = ev.text.text[i]; cp0::IpcAction act = ctrl.handle_key(ch, cp0::KeyCode::Char); dirty = true; @@ -577,6 +636,9 @@ int run_sdl_loop( } while (running && SDL_PollEvent(&ev) != 0); } + if (!running || g_signal_stop) { + break; + } auto now = std::chrono::steady_clock::now(); if (!ctrl.state().feedback_banner.empty() && now >= feedback_until) { ctrl.clear_feedback(); @@ -587,7 +649,7 @@ int run_sdl_loop( last_poll = now; dirty = true; } - if (running && dirty) { + if (running && !g_signal_stop && dirty) { view.render(ctrl.state()); present(); } diff --git a/device-ui/src/ui_controller.cpp b/device-ui/src/ui_controller.cpp index fd9ae7f..816a543 100644 --- a/device-ui/src/ui_controller.cpp +++ b/device-ui/src/ui_controller.cpp @@ -209,12 +209,14 @@ void UiController::set_dir_picker_start_path(const std::string& path) { dir_picker_start_path_ = path; } -void UiController::set_feedback(const std::string& message) { +void UiController::set_feedback(const std::string& message, bool is_error) { state_.feedback_banner = message; + state_.feedback_is_error = is_error && !message.empty(); } void UiController::clear_feedback() { state_.feedback_banner.clear(); + state_.feedback_is_error = false; } bool UiController::consume_peer_refresh() { @@ -276,23 +278,24 @@ void UiController::load_directory(const std::string& path, const std::string& se state_.dir_current_path = resolved.string(); state_.dir_entries.clear(); - fs::directory_iterator it( - resolved, - fs::directory_options::skip_permission_denied, - ec - ); - if (!ec) { - for (const auto& entry : it) { - std::error_code entry_ec; - if (!entry.is_directory(entry_ec) || entry_ec) { - continue; - } - std::string name = entry.path().filename().string(); - if (name.empty() || name[0] == '.') { - continue; - } - state_.dir_entries.push_back(entry.path().string()); + for (fs::directory_iterator it( + resolved, + fs::directory_options::skip_permission_denied, + ec + ), + end; + !ec && it != end; + it.increment(ec)) { + const auto& entry = *it; + std::error_code entry_ec; + if (!entry.is_directory(entry_ec) || entry_ec) { + continue; } + std::string name = entry.path().filename().string(); + if (name.empty() || name[0] == '.') { + continue; + } + state_.dir_entries.push_back(entry.path().string()); } std::sort( diff --git a/device-ui/src/ui_controller.hpp b/device-ui/src/ui_controller.hpp index 9a45e04..a191c1d 100644 --- a/device-ui/src/ui_controller.hpp +++ b/device-ui/src/ui_controller.hpp @@ -93,6 +93,7 @@ struct UiState { std::string text_buffer; std::string modal_error; std::string feedback_banner; + bool feedback_is_error = false; bool needs_peer_refresh = false; }; @@ -114,7 +115,7 @@ public: void set_dir_picker_start_path(const std::string& path); /// Set or clear transient status feedback displayed in the UI. - void set_feedback(const std::string& message); + void set_feedback(const std::string& message, bool is_error = false); void clear_feedback(); /// Return true once when TargetPicker was opened and needs peer discovery. diff --git a/device-ui/tests/test_ui_navigation.cpp b/device-ui/tests/test_ui_navigation.cpp index 0c56df6..a08a7de 100644 --- a/device-ui/tests/test_ui_navigation.cpp +++ b/device-ui/tests/test_ui_navigation.cpp @@ -1,9 +1,13 @@ +#include "fb_blit.hpp" #include "ui_controller.hpp" #include #include #include +#include +#include +#include #include #include #include @@ -262,6 +266,24 @@ void test_target_picker_and_manual_host() { assert(no_peer_enter.type == cp0::ActionType::None); assert(ctrl.state().modal == cp0::ModalKind::TargetPicker); ctrl.handle_key(0, cp0::KeyCode::Escape); + + // IPv6 host with port and without port + ctrl.handle_key('c', cp0::KeyCode::Char); + ctrl.handle_key('t', cp0::KeyCode::Char); + type_text(ctrl, "[::1]:8765"); + auto ipv6_port = ctrl.handle_key(0, cp0::KeyCode::Enter); + assert(ipv6_port.type == cp0::ActionType::SetTarget); + assert(ipv6_port.target_host == "::1"); + assert(ipv6_port.target_port == 8765); + assert(ctrl.state().modal == cp0::ModalKind::None); + + ctrl.handle_key('c', cp0::KeyCode::Char); + ctrl.handle_key('t', cp0::KeyCode::Char); + type_text(ctrl, "[fd7a:115c:a1e0::1]"); + auto ipv6_default = ctrl.handle_key(0, cp0::KeyCode::Enter); + assert(ipv6_default.type == cp0::ActionType::SetTarget); + assert(ipv6_default.target_host == "fd7a:115c:a1e0::1"); + assert(ipv6_default.target_port == 8765); } void test_dir_picker_traversal_and_manual_path() { @@ -333,6 +355,42 @@ void test_dir_picker_traversal_and_manual_path() { assert(add_manual.type == cp0::ActionType::AddFolder); assert(add_manual.folder_path == "/media/sd/DCIM2"); assert(ctrl.state().modal == cp0::ModalKind::None); + + // '.' inside DirPicker adds dir_current_path directly (even when a subdir is highlighted) + ctrl.handle_key('a', cp0::KeyCode::Char); + assert(ctrl.state().modal == cp0::ModalKind::DirPicker); + ctrl.handle_key('j', cp0::KeyCode::Char); // highlight beta + auto add_dot = ctrl.handle_key('.', cp0::KeyCode::Char); + assert(add_dot.type == cp0::ActionType::AddFolder); + assert(fs::path(add_dot.folder_path) == fs::weakly_canonical(root)); + assert(ctrl.state().modal == cp0::ModalKind::None); + + // '~' tilde path expansion in FolderPath TextInput + const char* raw_home = std::getenv("HOME"); + bool had_home = (raw_home != nullptr); + std::string saved_home = had_home ? raw_home : ""; + ::setenv("HOME", "/home/cp0user", 1); + + ctrl.handle_key('a', cp0::KeyCode::Char); + ctrl.handle_key('t', cp0::KeyCode::Char); + type_text(ctrl, "~/Pictures/Camera"); + auto add_tilde = ctrl.handle_key(0, cp0::KeyCode::Enter); + assert(add_tilde.type == cp0::ActionType::AddFolder); + assert(add_tilde.folder_path == "/home/cp0user/Pictures/Camera"); + assert(ctrl.state().modal == cp0::ModalKind::None); + + ctrl.handle_key('a', cp0::KeyCode::Char); + ctrl.handle_key('t', cp0::KeyCode::Char); + type_text(ctrl, "~"); + auto add_tilde_home = ctrl.handle_key(0, cp0::KeyCode::Enter); + assert(add_tilde_home.type == cp0::ActionType::AddFolder); + assert(add_tilde_home.folder_path == "/home/cp0user"); + + if (had_home) { + ::setenv("HOME", saved_home.c_str(), 1); + } else { + ::unsetenv("HOME"); + } } void test_folders_selection_and_confirm_remove() { @@ -432,6 +490,152 @@ void test_logs_scrolling_and_retry_failed() { assert(retry.type == cp0::ActionType::RetryFailed); } +void test_feedback_banner_and_error_flag() { + cp0::UiController ctrl; + assert(ctrl.state().feedback_banner.empty()); + assert(ctrl.state().feedback_is_error == false); + + ctrl.set_feedback("Sync triggered"); + assert(ctrl.state().feedback_banner == "Sync triggered"); + assert(ctrl.state().feedback_is_error == false); + + ctrl.set_feedback("Daemon unreachable", true); + assert(ctrl.state().feedback_banner == "Daemon unreachable"); + assert(ctrl.state().feedback_is_error == true); + + ctrl.set_feedback("Folder added"); + assert(ctrl.state().feedback_banner == "Folder added"); + assert(ctrl.state().feedback_is_error == false); + + ctrl.set_feedback("", true); + assert(ctrl.state().feedback_banner.empty()); + assert(ctrl.state().feedback_is_error == false); + + ctrl.set_feedback("Failed", true); + assert(ctrl.state().feedback_is_error == true); + ctrl.clear_feedback(); + assert(ctrl.state().feedback_banner.empty()); + assert(ctrl.state().feedback_is_error == false); +} + +void test_fb_blit_pixel_formats_and_bounds() { + const uint32_t src[4] = { + 0xff123456U, 0xfff80000U, + 0xff00fc00U, 0xff0000f8U, + }; + + // 32-bit XRGB8888 fast path with xoffset/yoffset and padded stride + { + cp0::FbBlitSpec spec; + spec.xres = 2; + spec.yres = 2; + spec.xoffset = 1; + spec.yoffset = 1; + spec.bits_per_pixel = 32; + spec.line_length = 16; // 4 pixels per scanline + spec.red = {16, 8}; + spec.green = {8, 8}; + spec.blue = {0, 8}; + + std::vector buf(48, 0xaa); + assert(cp0::blit_framebuffer(buf.data(), buf.size(), spec, src, 2, 2)); + uint32_t p00 = 0; + uint32_t p11 = 0; + std::memcpy(&p00, buf.data() + 1 * 16 + 1 * 4, 4); + std::memcpy(&p11, buf.data() + 2 * 16 + 2 * 4, 4); + assert(p00 == 0xff123456U); + assert(p11 == 0xff0000f8U); + assert(buf[0] == 0xaa); + } + + // 32-bit XBGR8888 (red/blue swapped in fb layout) + { + cp0::FbBlitSpec spec; + spec.xres = 2; + spec.yres = 2; + spec.bits_per_pixel = 32; + spec.line_length = 8; + spec.red = {0, 8}; + spec.green = {8, 8}; + spec.blue = {16, 8}; + + std::vector buf(16, 0); + assert(cp0::blit_framebuffer(buf.data(), buf.size(), spec, src, 2, 2)); + uint32_t p00 = 0; + std::memcpy(&p00, buf.data(), 4); + assert(p00 == 0x00563412U); + } + + // 16-bit RGB565 & BGR565 with odd stride + { + cp0::FbBlitSpec spec; + spec.xres = 2; + spec.yres = 2; + spec.bits_per_pixel = 16; + spec.line_length = 5; // odd byte stride >= 4 + spec.red = {11, 5}; + spec.green = {5, 6}; + spec.blue = {0, 5}; + + std::vector buf(10, 0); + assert(cp0::blit_framebuffer(buf.data(), buf.size(), spec, src, 2, 2)); + uint16_t p01 = 0; // pure red 0xfff80000 -> 0xf800 + uint16_t p10 = 0; // pure green 0xff00fc00 -> 0x07e0 + std::memcpy(&p01, buf.data() + 2, 2); + std::memcpy(&p10, buf.data() + 5, 2); + assert(p01 == 0xf800U); + assert(p10 == 0x07e0U); + + spec.red = {0, 5}; + spec.blue = {11, 5}; + assert(cp0::blit_framebuffer(buf.data(), buf.size(), spec, src, 2, 2)); + std::memcpy(&p01, buf.data() + 2, 2); + assert(p01 == 0x001fU); + } + + // 24-bit BGR888 + { + cp0::FbBlitSpec spec; + spec.xres = 2; + spec.yres = 2; + spec.bits_per_pixel = 24; + spec.line_length = 6; + spec.red = {0, 8}; + spec.green = {8, 8}; + spec.blue = {16, 8}; + + std::vector buf(12, 0); + assert(cp0::blit_framebuffer(buf.data(), buf.size(), spec, src, 2, 2)); + assert(buf[0] == 0x12 && buf[1] == 0x34 && buf[2] == 0x56); + } + + // Bounds & invalid format checks (canary byte after map_size untouched) + { + cp0::FbBlitSpec spec; + spec.xres = 2; + spec.yres = 2; + spec.bits_per_pixel = 16; + spec.line_length = 4; + spec.red = {11, 5}; + spec.green = {5, 6}; + spec.blue = {0, 5}; + + std::vector buf(8, 0x55); + // Only 6 bytes mapped, so row 1 (bytes 4..7) overflows map_size + assert(!cp0::blit_framebuffer(buf.data(), 6, spec, src, 2, 2)); + assert(buf[6] == 0x55 && buf[7] == 0x55); + + // Stride smaller than row_bytes rejected + spec.line_length = 3; + assert(!cp0::blit_framebuffer(buf.data(), buf.size(), spec, src, 2, 2)); + + // Unsupported bpp rejected + spec.line_length = 4; + spec.bits_per_pixel = 8; + assert(!cp0::blit_framebuffer(buf.data(), buf.size(), spec, src, 2, 2)); + } +} + } // namespace int main() { @@ -441,6 +645,8 @@ int main() { test_dir_picker_traversal_and_manual_path(); test_folders_selection_and_confirm_remove(); test_logs_scrolling_and_retry_failed(); + test_feedback_banner_and_error_flag(); + test_fb_blit_pixel_formats_and_bounds(); std::cout << "All test_ui_navigation assertions passed.\n"; return 0; }