mirror of
https://github.com/cottongin/cardputerzero-sync.git
synced 2026-11-20 14:44:47 -06:00
fix(device-ui): harden fbdev blit, evdev loop, LVGL lifecycle, and feedback toasts
This commit is contained in:
@@ -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) {
|
||||
|
||||
145
device-ui/src/fb_blit.hpp
Normal file
145
device-ui/src/fb_blit.hpp
Normal file
@@ -0,0 +1,145 @@
|
||||
#pragma once
|
||||
|
||||
#include <algorithm>
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <cstring>
|
||||
#include <limits>
|
||||
|
||||
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<size_t>(spec.xres), static_cast<size_t>(src_w));
|
||||
size_t copy_h = std::min(static_cast<size_t>(spec.yres), static_cast<size_t>(src_h));
|
||||
if (copy_w == 0 || copy_h == 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
size_t bytes_per_pixel = static_cast<size_t>(spec.bits_per_pixel) / 8U;
|
||||
size_t stride = static_cast<size_t>(spec.line_length);
|
||||
if (copy_w > std::numeric_limits<size_t>::max() / bytes_per_pixel) {
|
||||
return false;
|
||||
}
|
||||
size_t row_bytes = copy_w * bytes_per_pixel;
|
||||
if (stride < row_bytes) {
|
||||
return false;
|
||||
}
|
||||
if (static_cast<size_t>(spec.xoffset) > std::numeric_limits<size_t>::max() / bytes_per_pixel) {
|
||||
return false;
|
||||
}
|
||||
size_t x_byte_offset = static_cast<size_t>(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<size_t>(spec.yoffset) + y;
|
||||
if (y_row < static_cast<size_t>(spec.yoffset)) {
|
||||
return false;
|
||||
}
|
||||
if (y_row > std::numeric_limits<size_t>::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<size_t>(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<uint8_t>(packed & 0xffU);
|
||||
row[x * 3U + 1U] = static_cast<uint8_t>((packed >> 8) & 0xffU);
|
||||
row[x * 3U + 2U] = static_cast<uint8_t>((packed >> 16) & 0xffU);
|
||||
}
|
||||
} else {
|
||||
for (size_t x = 0; x < copy_w; ++x) {
|
||||
uint32_t c = src[x];
|
||||
uint16_t packed = static_cast<uint16_t>(
|
||||
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
|
||||
@@ -1,9 +1,14 @@
|
||||
#include "app_ui.hpp"
|
||||
#include "fb_blit.hpp"
|
||||
#include "ipc_client.hpp"
|
||||
#include "ui_controller.hpp"
|
||||
|
||||
#include <algorithm>
|
||||
#include <cerrno>
|
||||
#include <chrono>
|
||||
#include <csignal>
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
#include <iostream>
|
||||
@@ -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<int>(vinfo.xres);
|
||||
int fb_h = static_cast<int>(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<int>(vinfo.bits_per_pixel);
|
||||
int stride = static_cast<int>(finfo.line_length);
|
||||
|
||||
for (int y = 0; y < copy_h; ++y) {
|
||||
uint8_t* row = fb_mem + (y + static_cast<int>(vinfo.yoffset)) * stride +
|
||||
static_cast<int>(vinfo.xoffset) * (bpp / 8);
|
||||
const uint32_t* src = pixels + y * cp0::LvglAppView::kWidth;
|
||||
if (bpp == 32) {
|
||||
std::memcpy(row, src, static_cast<size_t>(copy_w) * 4);
|
||||
} else if (bpp == 16) {
|
||||
auto* dst16 = reinterpret_cast<uint16_t*>(row);
|
||||
for (int x = 0; x < copy_w; ++x) {
|
||||
uint32_t c = src[x];
|
||||
uint16_t r = static_cast<uint16_t>((c >> 19) & 0x1fU);
|
||||
uint16_t g = static_cast<uint16_t>((c >> 10) & 0x3fU);
|
||||
uint16_t b = static_cast<uint16_t>((c >> 3) & 0x1fU);
|
||||
dst16[x] = static_cast<uint16_t>((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<int> 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<nfds_t>(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<size_t> 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<ssize_t>(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<std::ptrdiff_t>(*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();
|
||||
}
|
||||
|
||||
@@ -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(
|
||||
|
||||
@@ -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.
|
||||
|
||||
Reference in New Issue
Block a user