feat: allow MAC spoofing in Bluetooth stack, show paired devices in Bluetooth app
This commit is contained in:
@@ -182,6 +182,13 @@ namespace Montauk {
|
||||
// Paginated directory read (path, names, max, startIndex)
|
||||
static constexpr uint64_t SYS_READDIR_AT = 136;
|
||||
|
||||
// Set adapter BD_ADDR (6-byte buffer, addr[0] = LSB)
|
||||
static constexpr uint64_t SYS_BTSETADDR = 137;
|
||||
|
||||
// List bonded (paired) devices / forget a bond
|
||||
static constexpr uint64_t SYS_BTBONDS = 138;
|
||||
static constexpr uint64_t SYS_BTFORGET = 139;
|
||||
|
||||
// Graceful power-off request actions (SYS_POWER_REQUEST). The desktop posts
|
||||
// a pending action and exits; login.elf reads it, runs the shutdown stages,
|
||||
// then issues the matching SYS_SHUTDOWN / SYS_RESET.
|
||||
@@ -433,6 +440,12 @@ namespace Montauk {
|
||||
char name[64];
|
||||
};
|
||||
|
||||
// Bluetooth bonded (paired) device (returned by SYS_BTBONDS)
|
||||
struct BtBondInfo {
|
||||
uint8_t bdAddr[6];
|
||||
uint8_t _pad[2];
|
||||
};
|
||||
|
||||
struct ThermalInfo {
|
||||
char name[32]; // short zone name (e.g. "THRM", "TZ00")
|
||||
int32_t temperature; // tenths of degrees Celsius, or -1 if unavailable
|
||||
|
||||
@@ -536,6 +536,20 @@ namespace montauk {
|
||||
inline int bt_disconnect(const uint8_t* bdAddr) {
|
||||
return (int)syscall1(Montauk::SYS_BTDISCONNECT, (uint64_t)bdAddr);
|
||||
}
|
||||
// Change the adapter's BD_ADDR. bdAddr is a 6-byte buffer with bdAddr[0] as
|
||||
// the least-significant octet (same order as BtAdapterInfo::bdAddr). Returns
|
||||
// 0 on success, negative on failure. Persist separately to bluetooth.toml.
|
||||
inline int bt_set_addr(const uint8_t* bdAddr) {
|
||||
return (int)syscall1(Montauk::SYS_BTSETADDR, (uint64_t)bdAddr);
|
||||
}
|
||||
// List bonded (paired) devices. Returns count written to buf (may be 0).
|
||||
inline int bt_bonds(Montauk::BtBondInfo* buf, int maxCount) {
|
||||
return (int)syscall2(Montauk::SYS_BTBONDS, (uint64_t)buf, (uint64_t)maxCount);
|
||||
}
|
||||
// Forget a paired device (removes the bond; it must re-pair next time).
|
||||
inline int bt_forget(const uint8_t* bdAddr) {
|
||||
return (int)syscall1(Montauk::SYS_BTFORGET, (uint64_t)bdAddr);
|
||||
}
|
||||
inline int bt_list(Montauk::BtDevInfo* buf, int maxCount) {
|
||||
return (int)syscall2(Montauk::SYS_BTLIST, (uint64_t)buf, (uint64_t)maxCount);
|
||||
}
|
||||
|
||||
Binary file not shown.
Binary file not shown.
+391
-43
@@ -7,6 +7,7 @@
|
||||
#include <montauk/syscall.h>
|
||||
#include <montauk/string.h>
|
||||
#include <montauk/heap.h>
|
||||
#include <montauk/config.h>
|
||||
#include <gui/mtk.hpp>
|
||||
#include <gui/standalone.hpp>
|
||||
|
||||
@@ -31,9 +32,13 @@ static constexpr int BTN_H = 28;
|
||||
static constexpr int STATUS_H = 28;
|
||||
static constexpr int SCAN_BTN_W = 100;
|
||||
static constexpr int SCROLL_STEP = 20;
|
||||
static constexpr int FIELD_H = 32;
|
||||
static constexpr int A_PAD_TOP = 18;
|
||||
|
||||
static constexpr int MAX_SCAN = 16;
|
||||
static constexpr int MAX_CONNECTED = 8;
|
||||
// Up to 4 active links + up to 8 stored bonds shown together in the Devices tab.
|
||||
static constexpr int MAX_CONNECTED = 16;
|
||||
static constexpr int FORGET_W = 64;
|
||||
|
||||
static constexpr Color BG_COLOR = Color::from_rgb(0xFF, 0xFF, 0xFF);
|
||||
static constexpr Color TEXT_COLOR = Color::from_rgb(0x33, 0x33, 0x33);
|
||||
@@ -52,12 +57,14 @@ static constexpr Color RED = Color::from_rgb(0xCC, 0x33, 0x33);
|
||||
enum Tab {
|
||||
TAB_DEVICES = 0,
|
||||
TAB_SCAN = 1,
|
||||
TAB_COUNT = 2,
|
||||
TAB_ADAPTER = 2,
|
||||
TAB_COUNT = 3,
|
||||
};
|
||||
|
||||
static const char* g_tab_labels[TAB_COUNT] = {
|
||||
"Devices",
|
||||
"Scan",
|
||||
"Adapter",
|
||||
};
|
||||
|
||||
static int g_win_w = WIN_W;
|
||||
@@ -82,6 +89,12 @@ static Montauk::BtScanResult g_scan[MAX_SCAN];
|
||||
static int g_scan_count = 0;
|
||||
static bool g_scanning = false;
|
||||
|
||||
// Adapter MAC editing
|
||||
static char g_mac_value[24] = {};
|
||||
static mtk::TextInputState g_mac_input = {};
|
||||
static bool g_mac_focus = false;
|
||||
static uint64_t g_rng = 0;
|
||||
|
||||
// Scroll
|
||||
static int g_scroll = 0;
|
||||
|
||||
@@ -157,8 +170,17 @@ static Rect row_action_button_rect(const Rect& row) {
|
||||
return {g_win_w - PAD - BTN_W, row.y + (ROW_H - BTN_H) / 2, BTN_W, BTN_H};
|
||||
}
|
||||
|
||||
// Secondary (Forget) button, left of the primary action button. Used for
|
||||
// paired-but-disconnected rows that show "Connect" + "Forget".
|
||||
static Rect row_forget_button_rect(const Rect& row) {
|
||||
Rect a = row_action_button_rect(row);
|
||||
return {a.x - 8 - FORGET_W, a.y, FORGET_W, BTN_H};
|
||||
}
|
||||
|
||||
static int current_row_count() {
|
||||
return g_tab == TAB_DEVICES ? g_device_count : g_scan_count;
|
||||
if (g_tab == TAB_DEVICES) return g_device_count;
|
||||
if (g_tab == TAB_SCAN) return g_scan_count;
|
||||
return 0; // Adapter tab is not a scrollable list
|
||||
}
|
||||
|
||||
static int current_list_top() {
|
||||
@@ -240,8 +262,29 @@ static void refresh_adapter() {
|
||||
}
|
||||
|
||||
static void refresh_devices() {
|
||||
g_device_count = montauk::bt_list(g_devices, MAX_CONNECTED);
|
||||
if (g_device_count < 0) g_device_count = 0;
|
||||
int active = montauk::bt_list(g_devices, MAX_CONNECTED);
|
||||
if (active < 0) active = 0;
|
||||
g_device_count = active;
|
||||
|
||||
// Append bonded (paired) devices that aren't currently connected, so the
|
||||
// Devices tab remembers them across sessions/reboots.
|
||||
Montauk::BtBondInfo bonds[8];
|
||||
int nb = montauk::bt_bonds(bonds, 8);
|
||||
for (int i = 0; i < nb && g_device_count < MAX_CONNECTED; i++) {
|
||||
bool already = false;
|
||||
for (int j = 0; j < active; j++) {
|
||||
if (memcmp(g_devices[j].bdAddr, bonds[i].bdAddr, 6) == 0) { already = true; break; }
|
||||
}
|
||||
if (already) continue;
|
||||
|
||||
Montauk::BtDevInfo d = {};
|
||||
montauk::memcpy(d.bdAddr, bonds[i].bdAddr, 6);
|
||||
d.connected = 0; // paired but not connected
|
||||
d.encrypted = 0;
|
||||
d.handle = 0;
|
||||
d.linkType = 0x01;
|
||||
g_devices[g_device_count++] = d;
|
||||
}
|
||||
|
||||
for (int i = 0; i < g_device_count; i++) {
|
||||
bool found = false;
|
||||
@@ -295,6 +338,14 @@ static void do_disconnect(const uint8_t* addr) {
|
||||
clamp_scroll();
|
||||
}
|
||||
|
||||
static void do_forget(const uint8_t* addr) {
|
||||
int r = montauk::bt_forget(addr);
|
||||
if (r >= 0) set_status("Device forgotten");
|
||||
else set_status("Forget failed");
|
||||
refresh_devices();
|
||||
clamp_scroll();
|
||||
}
|
||||
|
||||
static bool is_connected(const uint8_t* addr) {
|
||||
for (int i = 0; i < g_device_count; i++) {
|
||||
if (memcmp(g_devices[i].bdAddr, addr, 6) == 0 && g_devices[i].connected)
|
||||
@@ -338,9 +389,144 @@ static void set_tab(Tab tab) {
|
||||
g_tab = tab;
|
||||
g_scroll = 0;
|
||||
g_hover_row = -1;
|
||||
if (tab != TAB_ADAPTER)
|
||||
g_mac_focus = false;
|
||||
clamp_scroll();
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// Adapter MAC address
|
||||
// ============================================================================
|
||||
|
||||
static bool is_mac_char(char ch) {
|
||||
return (ch >= '0' && ch <= '9') || (ch >= 'a' && ch <= 'f') ||
|
||||
(ch >= 'A' && ch <= 'F') || ch == ':';
|
||||
}
|
||||
|
||||
static bool accept_mac_char(char ch, void* userdata) {
|
||||
(void)userdata;
|
||||
return is_mac_char(ch);
|
||||
}
|
||||
|
||||
static bool hex_nibble(char c, uint8_t& out) {
|
||||
if (c >= '0' && c <= '9') { out = (uint8_t)(c - '0'); return true; }
|
||||
if (c >= 'a' && c <= 'f') { out = (uint8_t)(c - 'a' + 10); return true; }
|
||||
if (c >= 'A' && c <= 'F') { out = (uint8_t)(c - 'A' + 10); return true; }
|
||||
return false;
|
||||
}
|
||||
|
||||
// Parse "XX:XX:XX:XX:XX:XX" (':' or '-' separators, optional) into 6 bytes,
|
||||
// out[0] = first printed octet. Returns false unless exactly six octets and no
|
||||
// trailing junk remain.
|
||||
static bool parse_mac(const char* s, uint8_t out[6]) {
|
||||
int byteIdx = 0;
|
||||
const char* p = s;
|
||||
while (byteIdx < 6) {
|
||||
while (*p == ':' || *p == '-' || *p == ' ') p++;
|
||||
uint8_t hi = 0, lo = 0;
|
||||
if (!hex_nibble(p[0], hi) || !hex_nibble(p[1], lo)) return false;
|
||||
out[byteIdx++] = (uint8_t)((hi << 4) | lo);
|
||||
p += 2;
|
||||
}
|
||||
while (*p == ':' || *p == '-' || *p == ' ') p++;
|
||||
return *p == '\0';
|
||||
}
|
||||
|
||||
static uint32_t rng_next() {
|
||||
if (g_rng == 0) g_rng = montauk::get_milliseconds() | 1ULL;
|
||||
g_rng ^= g_rng << 13;
|
||||
g_rng ^= g_rng >> 7;
|
||||
g_rng ^= g_rng << 17;
|
||||
return (uint32_t)g_rng;
|
||||
}
|
||||
|
||||
static void randomize_mac() {
|
||||
uint8_t b[6];
|
||||
for (int i = 0; i < 6; i++) b[i] = (uint8_t)(rng_next() & 0xFF);
|
||||
// First printed octet: locally-administered + unicast (clear multicast bit).
|
||||
b[0] = (uint8_t)((b[0] & 0xFC) | 0x02);
|
||||
format_addr(g_mac_value, sizeof(g_mac_value), b);
|
||||
g_mac_focus = true;
|
||||
int len = (int)strlen(g_mac_value);
|
||||
mtk::text_input_set_cursor(g_mac_input, len, len, false);
|
||||
}
|
||||
|
||||
// Prefill the editor from the saved config (if any), else from the live adapter.
|
||||
static void load_mac_config() {
|
||||
montauk::toml::Doc cfg = montauk::config::load("bluetooth");
|
||||
const char* m = cfg.get_string("adapter.mac", "");
|
||||
if (m && m[0]) {
|
||||
snprintf(g_mac_value, sizeof(g_mac_value), "%s", m);
|
||||
} else if (g_adapter_ok) {
|
||||
format_addr(g_mac_value, sizeof(g_mac_value), g_adapter.bdAddr);
|
||||
} else {
|
||||
g_mac_value[0] = '\0';
|
||||
}
|
||||
cfg.destroy();
|
||||
}
|
||||
|
||||
static void apply_mac() {
|
||||
uint8_t bytes[6];
|
||||
if (!parse_mac(g_mac_value, bytes)) {
|
||||
set_status("Invalid MAC address");
|
||||
return;
|
||||
}
|
||||
|
||||
if (montauk::bt_set_addr(bytes) < 0) {
|
||||
set_status("Failed to set MAC");
|
||||
return;
|
||||
}
|
||||
|
||||
// Normalize and persist to 0:/config/bluetooth.toml so it is re-applied
|
||||
// on every boot by the kernel.
|
||||
char norm[24];
|
||||
format_addr(norm, sizeof(norm), bytes);
|
||||
montauk::toml::Doc cfg = montauk::config::load("bluetooth");
|
||||
montauk::config::set_string(&cfg, "adapter.mac", norm);
|
||||
montauk::config::save("bluetooth", &cfg);
|
||||
cfg.destroy();
|
||||
|
||||
snprintf(g_mac_value, sizeof(g_mac_value), "%s", norm);
|
||||
g_mac_focus = false;
|
||||
set_status("MAC address changed");
|
||||
refresh_adapter();
|
||||
refresh_devices();
|
||||
}
|
||||
|
||||
static void use_default_mac() {
|
||||
montauk::toml::Doc cfg = montauk::config::load("bluetooth");
|
||||
bool had = montauk::config::unset(&cfg, "adapter.mac");
|
||||
montauk::config::save("bluetooth", &cfg);
|
||||
cfg.destroy();
|
||||
set_status(had ? "Factory address restored on reboot" : "No custom MAC set");
|
||||
}
|
||||
|
||||
// ---- Adapter tab layout ----
|
||||
|
||||
static int a_cur_label_y() { return content_rect().y + A_PAD_TOP; }
|
||||
static int a_cur_value_y() { return a_cur_label_y() + system_font_height() + 4; }
|
||||
static int a_new_label_y() { return a_cur_value_y() + system_font_height() + 20; }
|
||||
|
||||
static Rect adapter_input_rect() {
|
||||
int y = a_new_label_y() + system_font_height() + 6;
|
||||
return {PAD, y, g_win_w - PAD * 2, FIELD_H};
|
||||
}
|
||||
|
||||
static Rect adapter_apply_btn() {
|
||||
Rect f = adapter_input_rect();
|
||||
return {g_win_w - PAD - BTN_W, f.y + f.h + 16, BTN_W, BTN_H};
|
||||
}
|
||||
|
||||
static Rect adapter_random_btn() {
|
||||
Rect a = adapter_apply_btn();
|
||||
return {PAD, a.y, 80, BTN_H};
|
||||
}
|
||||
|
||||
static Rect adapter_default_btn() {
|
||||
Rect r = adapter_random_btn();
|
||||
return {r.x + r.w + 8, r.y, 110, BTN_H};
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// Hover and hit testing
|
||||
// ============================================================================
|
||||
@@ -351,7 +537,7 @@ static int get_hover_row(int mx, int my) {
|
||||
if (devices_row_rect(i).contains(mx, my))
|
||||
return i;
|
||||
}
|
||||
} else {
|
||||
} else if (g_tab == TAB_SCAN) {
|
||||
for (int i = 0; i < g_scan_count; i++) {
|
||||
if (scan_row_rect(i).contains(mx, my))
|
||||
return i;
|
||||
@@ -391,12 +577,24 @@ static bool handle_click(int mx, int my) {
|
||||
for (int i = 0; i < g_device_count; i++) {
|
||||
Rect row = devices_row_rect(i);
|
||||
if (!row.contains(mx, my)) continue;
|
||||
if (!g_devices[i].connected) continue;
|
||||
|
||||
Rect action = row_action_button_rect(row);
|
||||
if (action.contains(mx, my)) {
|
||||
do_disconnect(g_devices[i].bdAddr);
|
||||
return true;
|
||||
if (g_devices[i].connected) {
|
||||
if (action.contains(mx, my)) {
|
||||
do_disconnect(g_devices[i].bdAddr);
|
||||
return true;
|
||||
}
|
||||
} else {
|
||||
// Paired but not connected: Connect (primary) + Forget.
|
||||
Rect forget = row_forget_button_rect(row);
|
||||
if (action.contains(mx, my)) {
|
||||
do_connect(g_devices[i].bdAddr);
|
||||
return true;
|
||||
}
|
||||
if (forget.contains(mx, my)) {
|
||||
do_forget(g_devices[i].bdAddr);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -404,6 +602,69 @@ static bool handle_click(int mx, int my) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Adapter-tab pointer handling: text field (focus/cursor/selection/context)
|
||||
// plus the Apply / Random / Use Default buttons. The tab bar is handled by the
|
||||
// caller before this runs. Returns true if the view should be redrawn.
|
||||
static bool handle_adapter_mouse(int mx, int my, uint8_t buttons,
|
||||
uint8_t prev_buttons, int view_w, int view_h) {
|
||||
bool left_press = (buttons & 1) && !(prev_buttons & 1);
|
||||
Rect field = adapter_input_rect();
|
||||
|
||||
// While a context menu is open or a drag is in progress, let the widget
|
||||
// consume the event regardless of where the pointer is.
|
||||
if (g_mac_input.context.open || g_mac_input.dragging) {
|
||||
int res = mtk::text_input_handle_mouse(
|
||||
g_mac_input, field, g_mac_value, (int)sizeof(g_mac_value),
|
||||
mx, my, buttons, prev_buttons, view_w, view_h,
|
||||
g_mac_focus, false, accept_mac_char);
|
||||
return res != mtk::TEXT_INPUT_NONE;
|
||||
}
|
||||
|
||||
if (left_press) {
|
||||
if (g_adapter_ok && adapter_apply_btn().contains(mx, my)) { apply_mac(); return true; }
|
||||
if (g_adapter_ok && adapter_random_btn().contains(mx, my)) { randomize_mac(); return true; }
|
||||
if (adapter_default_btn().contains(mx, my)) { use_default_mac(); return true; }
|
||||
}
|
||||
|
||||
if (field.contains(mx, my)) {
|
||||
g_mac_focus = true;
|
||||
mtk::text_input_handle_mouse(
|
||||
g_mac_input, field, g_mac_value, (int)sizeof(g_mac_value),
|
||||
mx, my, buttons, prev_buttons, view_w, view_h,
|
||||
true, false, accept_mac_char);
|
||||
return true;
|
||||
}
|
||||
|
||||
if (left_press) {
|
||||
g_mac_focus = false;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
static bool handle_adapter_key(const Montauk::KeyEvent& key) {
|
||||
if (key.ascii == '\n' || key.ascii == '\r') { apply_mac(); return true; }
|
||||
if (key.ascii == '\t') { g_mac_focus = !g_mac_focus; return true; }
|
||||
|
||||
if (g_mac_focus) {
|
||||
int res = mtk::text_input_key(g_mac_input, g_mac_value,
|
||||
(int)sizeof(g_mac_value), key, accept_mac_char);
|
||||
return res != mtk::TEXT_INPUT_NONE;
|
||||
}
|
||||
|
||||
// Not editing: number keys still switch tabs; any MAC char starts editing.
|
||||
if (key.ascii == '1') { set_tab(TAB_DEVICES); return true; }
|
||||
if (key.ascii == '2') { set_tab(TAB_SCAN); return true; }
|
||||
if (key.ascii == '3') { return true; }
|
||||
if (is_mac_char(key.ascii)) {
|
||||
g_mac_focus = true;
|
||||
int res = mtk::text_input_key(g_mac_input, g_mac_value,
|
||||
(int)sizeof(g_mac_value), key, accept_mac_char);
|
||||
return res != mtk::TEXT_INPUT_NONE;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// Render
|
||||
// ============================================================================
|
||||
@@ -413,7 +674,7 @@ static void render_devices_tab(Canvas& canvas, const mtk::Theme& theme) {
|
||||
int fh = system_font_height();
|
||||
|
||||
if (g_device_count == 0) {
|
||||
draw_centered_text(canvas, list.y, list.h, "No connected devices", theme.text_subtle);
|
||||
draw_centered_text(canvas, list.y, list.h, "No paired devices", theme.text_subtle);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -426,21 +687,30 @@ static void render_devices_tab(Canvas& canvas, const mtk::Theme& theme) {
|
||||
else
|
||||
mtk::draw_list_row(canvas, row, false, (i & 1) != 0, theme);
|
||||
|
||||
Color dot_color = g_devices[i].connected ? GREEN : theme.text_subtle;
|
||||
bool connected = g_devices[i].connected;
|
||||
Color dot_color = connected ? GREEN : theme.text_subtle;
|
||||
fill_circle(canvas, PAD + 6, row.y + ROW_H / 2, 5, dot_color);
|
||||
|
||||
Rect action = row_action_button_rect(row);
|
||||
Rect forget = row_forget_button_rect(row);
|
||||
int text_x = PAD + 20;
|
||||
int text_w_max = (g_devices[i].connected ? action.x : g_win_w - PAD) - text_x - 10;
|
||||
// Connected rows have one button (Disconnect); paired rows have two
|
||||
// (Connect + Forget), so the text must stop before the leftmost button.
|
||||
int text_w_max = (connected ? action.x : forget.x) - text_x - 10;
|
||||
draw_text_fit(canvas, text_x, row.y + 8, g_device_names[i], text_w_max, theme.text);
|
||||
|
||||
char addr_str[24];
|
||||
format_addr(addr_str, sizeof(addr_str), g_devices[i].bdAddr);
|
||||
draw_text_fit(canvas, text_x, row.y + 8 + fh + 2, addr_str, text_w_max, theme.text_subtle);
|
||||
|
||||
if (g_devices[i].connected) {
|
||||
if (connected) {
|
||||
mtk::draw_button(canvas, action, "Disconnect", mtk::BUTTON_DANGER,
|
||||
button_state(action, true), theme);
|
||||
} else {
|
||||
mtk::draw_button(canvas, action, "Connect", mtk::BUTTON_PRIMARY,
|
||||
button_state(action, true), theme);
|
||||
mtk::draw_button(canvas, forget, "Forget", mtk::BUTTON_SECONDARY,
|
||||
button_state(forget, true), theme);
|
||||
}
|
||||
|
||||
mtk::draw_separator(canvas, PAD, row.y + ROW_H - 1, g_win_w - 2 * PAD, theme);
|
||||
@@ -499,6 +769,45 @@ static void render_scan_tab(Canvas& canvas, const mtk::Theme& theme) {
|
||||
}
|
||||
}
|
||||
|
||||
static void render_adapter_tab(Canvas& canvas, const mtk::Theme& theme) {
|
||||
// Current address
|
||||
canvas.text(PAD, a_cur_label_y(), "Current Address", theme.text_subtle);
|
||||
if (g_adapter_ok) {
|
||||
char cur[24];
|
||||
format_addr(cur, sizeof(cur), g_adapter.bdAddr);
|
||||
canvas.text(PAD, a_cur_value_y(), cur, theme.text);
|
||||
} else {
|
||||
canvas.text(PAD, a_cur_value_y(), "No adapter found", theme.danger);
|
||||
}
|
||||
|
||||
// Editable new address
|
||||
canvas.text(PAD, a_new_label_y(), "New Address", theme.text_muted);
|
||||
Rect field = adapter_input_rect();
|
||||
mtk::draw_text_field(canvas, field, g_mac_value, g_mac_input.cursor,
|
||||
g_mac_focus, false, theme, g_mac_input.selection_anchor);
|
||||
|
||||
bool enabled = g_adapter_ok;
|
||||
Rect apply = adapter_apply_btn();
|
||||
Rect rnd = adapter_random_btn();
|
||||
Rect def = adapter_default_btn();
|
||||
mtk::draw_button(canvas, apply, "Apply", mtk::BUTTON_PRIMARY,
|
||||
button_state(apply, enabled), theme);
|
||||
mtk::draw_button(canvas, rnd, "Random", mtk::BUTTON_SECONDARY,
|
||||
button_state(rnd, enabled), theme);
|
||||
mtk::draw_button(canvas, def, "Use Default", mtk::BUTTON_SECONDARY,
|
||||
button_state(def, true), theme);
|
||||
|
||||
draw_text_fit(canvas, PAD, apply.y + BTN_H + 16,
|
||||
"Saved to config and re-applied on boot.",
|
||||
g_win_w - PAD * 2, theme.text_subtle);
|
||||
|
||||
// Right-click context menu (cut/copy/paste) for the field, drawn last.
|
||||
mtk::draw_text_input_context_menu(
|
||||
canvas, g_mac_input, theme,
|
||||
mtk::text_input_has_selection(g_mac_input, g_mac_value,
|
||||
(int)sizeof(g_mac_value)));
|
||||
}
|
||||
|
||||
static void render_status(Canvas& canvas, const mtk::Theme& theme) {
|
||||
Rect status = status_bar_rect();
|
||||
int sy = status.y + (status.h - system_font_height()) / 2;
|
||||
@@ -514,12 +823,16 @@ static void render_status(Canvas& canvas, const mtk::Theme& theme) {
|
||||
|
||||
int left_max = g_win_w - PAD * 2 - (show_status ? status_w + 12 : 0);
|
||||
if (g_adapter_ok) {
|
||||
int connected = 0;
|
||||
for (int i = 0; i < g_device_count; i++)
|
||||
if (g_devices[i].connected) connected++;
|
||||
|
||||
char adapter_info[96];
|
||||
char addr_str[24];
|
||||
format_addr(addr_str, sizeof(addr_str), g_adapter.bdAddr);
|
||||
const char* name = g_adapter.name[0] ? g_adapter.name : "Adapter";
|
||||
snprintf(adapter_info, sizeof(adapter_info), "%s | %s | %d connected",
|
||||
name, addr_str, g_device_count);
|
||||
name, addr_str, connected);
|
||||
draw_text_fit(canvas, PAD, sy, adapter_info, left_max, theme.text_subtle);
|
||||
} else {
|
||||
draw_text_fit(canvas, PAD, sy, "No adapter found", left_max, theme.danger);
|
||||
@@ -536,8 +849,10 @@ static void render(Canvas& canvas) {
|
||||
|
||||
if (g_tab == TAB_DEVICES)
|
||||
render_devices_tab(canvas, theme);
|
||||
else
|
||||
else if (g_tab == TAB_SCAN)
|
||||
render_scan_tab(canvas, theme);
|
||||
else
|
||||
render_adapter_tab(canvas, theme);
|
||||
|
||||
render_status(canvas, theme);
|
||||
}
|
||||
@@ -552,6 +867,7 @@ extern "C" void _start() {
|
||||
|
||||
refresh_adapter();
|
||||
refresh_devices();
|
||||
load_mac_config();
|
||||
clamp_scroll();
|
||||
g_status[0] = 0;
|
||||
|
||||
@@ -614,18 +930,34 @@ extern "C" void _start() {
|
||||
}
|
||||
|
||||
if (ev.type == 0 && ev.key.pressed) {
|
||||
if (ev.key.scancode == 0x01) break;
|
||||
if ((ev.key.ascii == 's' || ev.key.ascii == 'S') && !g_scanning) {
|
||||
do_scan();
|
||||
redraw = true;
|
||||
}
|
||||
if (ev.key.ascii == '1') {
|
||||
set_tab(TAB_DEVICES);
|
||||
redraw = true;
|
||||
}
|
||||
if (ev.key.ascii == '2') {
|
||||
set_tab(TAB_SCAN);
|
||||
redraw = true;
|
||||
if (g_tab == TAB_ADAPTER) {
|
||||
// Esc defocuses the field if editing, otherwise closes.
|
||||
if (ev.key.scancode == 0x01 && g_mac_focus) {
|
||||
g_mac_focus = false;
|
||||
redraw = true;
|
||||
} else if (ev.key.scancode == 0x01) {
|
||||
break;
|
||||
} else if (handle_adapter_key(ev.key)) {
|
||||
redraw = true;
|
||||
}
|
||||
} else {
|
||||
if (ev.key.scancode == 0x01) break;
|
||||
if ((ev.key.ascii == 's' || ev.key.ascii == 'S') && !g_scanning) {
|
||||
do_scan();
|
||||
redraw = true;
|
||||
}
|
||||
if (ev.key.ascii == '1') {
|
||||
set_tab(TAB_DEVICES);
|
||||
redraw = true;
|
||||
}
|
||||
if (ev.key.ascii == '2') {
|
||||
set_tab(TAB_SCAN);
|
||||
redraw = true;
|
||||
}
|
||||
if (ev.key.ascii == '3') {
|
||||
set_tab(TAB_ADAPTER);
|
||||
redraw = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -634,24 +966,40 @@ extern "C" void _start() {
|
||||
g_mouse_x = ev.mouse.x;
|
||||
g_mouse_y = ev.mouse.y;
|
||||
|
||||
int new_hover = get_hover_row(g_mouse_x, g_mouse_y);
|
||||
if (new_hover != g_hover_row) {
|
||||
g_hover_row = new_hover;
|
||||
redraw = true;
|
||||
}
|
||||
if (g_tab == TAB_ADAPTER) {
|
||||
if (moved) redraw = true;
|
||||
bool clicked = (ev.mouse.buttons & 1) && !(ev.mouse.prev_buttons & 1);
|
||||
int tab = clicked
|
||||
? mtk::hit_tab_bar(tab_bar_rect(), TAB_COUNT, g_mouse_x, g_mouse_y)
|
||||
: -1;
|
||||
if (tab >= 0) {
|
||||
set_tab((Tab)tab);
|
||||
redraw = true;
|
||||
} else if (handle_adapter_mouse(g_mouse_x, g_mouse_y,
|
||||
ev.mouse.buttons, ev.mouse.prev_buttons,
|
||||
win.width, win.height)) {
|
||||
redraw = true;
|
||||
}
|
||||
} else {
|
||||
int new_hover = get_hover_row(g_mouse_x, g_mouse_y);
|
||||
if (new_hover != g_hover_row) {
|
||||
g_hover_row = new_hover;
|
||||
redraw = true;
|
||||
}
|
||||
|
||||
if (moved)
|
||||
redraw = true;
|
||||
if (moved)
|
||||
redraw = true;
|
||||
|
||||
bool clicked = (ev.mouse.buttons & 1) && !(ev.mouse.prev_buttons & 1);
|
||||
if (clicked && handle_click(ev.mouse.x, ev.mouse.y))
|
||||
redraw = true;
|
||||
bool clicked = (ev.mouse.buttons & 1) && !(ev.mouse.prev_buttons & 1);
|
||||
if (clicked && handle_click(ev.mouse.x, ev.mouse.y))
|
||||
redraw = true;
|
||||
|
||||
if (ev.mouse.scroll) {
|
||||
g_scroll -= ev.mouse.scroll * SCROLL_STEP;
|
||||
clamp_scroll();
|
||||
g_hover_row = get_hover_row(g_mouse_x, g_mouse_y);
|
||||
redraw = true;
|
||||
if (ev.mouse.scroll) {
|
||||
g_scroll -= ev.mouse.scroll * SCROLL_STEP;
|
||||
clamp_scroll();
|
||||
g_hover_row = get_hover_row(g_mouse_x, g_mouse_y);
|
||||
redraw = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user