From 698e5b6f76f4cec712e147e86f6c373f811e28d0 Mon Sep 17 00:00:00 2001 From: alex Date: Sun, 5 Feb 2023 16:07:21 +0100 Subject: [PATCH] src,lib: zig fmt and clang-format all code C style is now fixed by .clang-format. a handy script to check all source code formatting is in tools/fmt-check.sh. --- .clang-format | 14 ++ lib/nif/build.zig | 10 +- lib/nif/nif.zig | 56 +++--- lib/nif/wpa.zig | 8 +- src/nd.zig | 6 +- src/test.zig | 6 +- src/types.zig | 1 - src/ui/c/drv_fbev.c | 92 +++++----- src/ui/c/drv_sdl2.c | 115 ++++++------ src/ui/c/ui.c | 425 ++++++++++++++++++++++---------------------- src/ui/c/ui.h | 2 +- tools/fmt-check.sh | 5 + 12 files changed, 380 insertions(+), 360 deletions(-) create mode 100644 .clang-format create mode 100755 tools/fmt-check.sh diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000..1516fdf --- /dev/null +++ b/.clang-format @@ -0,0 +1,14 @@ +# https://clang.llvm.org/docs/ClangFormat.html +# https://clang.llvm.org/docs/ClangFormatStyleOptions.html +--- +IndentWidth: 4 +ContinuationIndentWidth: 4 +UseTab: Never +ColumnLimit: 102 +PointerAlignment: Right +BreakBeforeBraces: Linux +AlignAfterOpenBracket: DontAlign +BinPackArguments: false +BinPackParameters: false +AllowAllArgumentsOnNextLine: true +AllowAllParametersOfDeclarationOnNextLine: true diff --git a/lib/nif/build.zig b/lib/nif/build.zig index 41a7c06..dfb9207 100644 --- a/lib/nif/build.zig +++ b/lib/nif/build.zig @@ -5,17 +5,17 @@ pub fn addPkg(b: *build.Builder, obj: *build.LibExeObjStep, prefix: []const u8) } pub fn pkgPath(b: *build.Builder, prefix: []const u8) []const u8 { - return b.pathJoin(&.{prefix, "nif.zig"}); + return b.pathJoin(&.{ prefix, "nif.zig" }); } pub fn library(b: *build.Builder, prefix: []const u8) *build.LibExeObjStep { - const lib = b.addStaticLibrary("nif", b.pathJoin(&.{prefix, "nif.zig"})); - lib.addIncludePath(b.pathJoin(&.{prefix, "wpa_supplicant"})); + const lib = b.addStaticLibrary("nif", b.pathJoin(&.{ prefix, "nif.zig" })); + lib.addIncludePath(b.pathJoin(&.{ prefix, "wpa_supplicant" })); lib.defineCMacro("CONFIG_CTRL_IFACE", null); lib.defineCMacro("CONFIG_CTRL_IFACE_UNIX", null); lib.addCSourceFiles(&.{ - b.pathJoin(&.{prefix, "wpa_supplicant/wpa_ctrl.c"}), - b.pathJoin(&.{prefix, "wpa_supplicant/os_unix.c"}), + b.pathJoin(&.{ prefix, "wpa_supplicant/wpa_ctrl.c" }), + b.pathJoin(&.{ prefix, "wpa_supplicant/os_unix.c" }), }, &.{ "-Wall", "-Wextra", diff --git a/lib/nif/nif.zig b/lib/nif/nif.zig index 40897ad..7a43961 100644 --- a/lib/nif/nif.zig +++ b/lib/nif/nif.zig @@ -28,36 +28,36 @@ extern "c" fn freeifaddrs(ptr: *ifaddrs) void; /// optionally filtering by the interface name. /// caller owns the returned value. pub fn pubAddresses(allocator: mem.Allocator, ifname: ?[]const u8) ![]net.Address { - var res: *ifaddrs = undefined; - if (getifaddrs(&res) != 0) { - return error.Getifaddrs; - } - defer freeifaddrs(res); + var res: *ifaddrs = undefined; + if (getifaddrs(&res) != 0) { + return error.Getifaddrs; + } + defer freeifaddrs(res); - var list = std.ArrayList(net.Address).init(allocator); - var it: ?*ifaddrs = res; - while (it) |ifa| : (it = ifa.next) { - const sa: *os.sockaddr = ifa.addr orelse continue; - if (sa.family != os.AF.INET and sa.family != os.AF.INET6) { - // not an IP address - continue; - } - if (ifa.flags & IFF_UP == 0 or ifa.flags & IFF_LOOPBACK != 0) { - // skip loopbacks and those which are not "up" - continue; - } - const ipaddr = net.Address.initPosix(@alignCast(4, sa)); // initPosix makes a copy - if (ipaddr.any.family == os.AF.INET6 and ipaddr.in6.sa.scope_id > 0) { - // want only global, with 0 scope - // non-zero scopes make sense for link-local addr only. + var list = std.ArrayList(net.Address).init(allocator); + var it: ?*ifaddrs = res; + while (it) |ifa| : (it = ifa.next) { + const sa: *os.sockaddr = ifa.addr orelse continue; + if (sa.family != os.AF.INET and sa.family != os.AF.INET6) { + // not an IP address + continue; + } + if (ifa.flags & IFF_UP == 0 or ifa.flags & IFF_LOOPBACK != 0) { + // skip loopbacks and those which are not "up" + continue; + } + const ipaddr = net.Address.initPosix(@alignCast(4, sa)); // initPosix makes a copy + if (ipaddr.any.family == os.AF.INET6 and ipaddr.in6.sa.scope_id > 0) { + // want only global, with 0 scope + // non-zero scopes make sense for link-local addr only. + continue; + } + if (ifname) |name| { + if (!mem.eql(u8, name, mem.sliceTo(ifa.name, 0))) { continue; } - if (ifname) |name| { - if (!mem.eql(u8, name, mem.sliceTo(ifa.name, 0))) { - continue; - } - } - try list.append(ipaddr); } - return list.toOwnedSlice(); + try list.append(ipaddr); + } + return list.toOwnedSlice(); } diff --git a/lib/nif/wpa.zig b/lib/nif/wpa.zig index c403fdd..1a9da2b 100644 --- a/lib/nif/wpa.zig +++ b/lib/nif/wpa.zig @@ -90,7 +90,7 @@ pub const Control = struct { /// add a new blank network, returning its ID. /// the newly added network can be configured with self.setNetworkParam. - pub fn addNetwork(self: *Self) (Error||std.fmt.ParseIntError)!u32 { + pub fn addNetwork(self: *Self) (Error || std.fmt.ParseIntError)!u32 { var buf: [10:0]u8 = undefined; const resp = self.request("ADD_NETWORK", &buf, null) catch return error.WpaCtrlAddNetwork; return std.fmt.parseUnsigned(u32, mem.trim(u8, resp, "\n "), 10); @@ -101,13 +101,13 @@ pub const Control = struct { const cmd = try std.fmt.bufPrintZ(&buf, "REMOVE_NETWORK {d}", .{id}); return self.reqOK(cmd) catch return error.WpaCtrlRemoveNetwork; } - + pub fn selectNetwork(self: *Self, id: u32) Error!void { var buf: [48:0]u8 = undefined; const cmd = try std.fmt.bufPrintZ(&buf, "SELECT_NETWORK {d}", .{id}); return self.reqOK(cmd) catch return error.WpaCtrlSelectNetwork; } - + pub fn enableNetwork(self: *Self, id: u32) Error!void { var buf: [48:0]u8 = undefined; const cmd = try std.fmt.bufPrintZ(&buf, "ENABLE_NETWORK {d}", .{id}); @@ -116,7 +116,7 @@ pub const Control = struct { pub fn setNetworkParam(self: *Self, id: u32, name: []const u8, value: []const u8) Error!void { var buf: [512:0]u8 = undefined; - const cmd = try std.fmt.bufPrintZ(&buf, "SET_NETWORK {d} {s} {s}", .{id, name, value}); + const cmd = try std.fmt.bufPrintZ(&buf, "SET_NETWORK {d} {s} {s}", .{ id, name, value }); return self.reqOK(cmd) catch return error.WpaCtrlSetNetworkParam; } diff --git a/src/nd.zig b/src/nd.zig index 45f1d1d..3c499b9 100644 --- a/src/nd.zig +++ b/src/nd.zig @@ -220,8 +220,8 @@ fn svShutdown(allocator: std.mem.Allocator) void { // http://smarden.org/runit/ const Argv = []const []const u8; const cmds: []const Argv = &.{ - &.{"sv", "-w", "600", "stop", "lnd"}, - &.{"sv", "-w", "600", "stop", "bitcoind"}, + &.{ "sv", "-w", "600", "stop", "lnd" }, + &.{ "sv", "-w", "600", "stop", "bitcoind" }, }; var procs: [cmds.len]?std.ChildProcess = undefined; for (cmds) |argv, i| { @@ -229,7 +229,7 @@ fn svShutdown(allocator: std.mem.Allocator) void { if (p.spawn()) { procs[i] = p; } else |err| { - logger.err("{s}: {any}", .{argv, err}); + logger.err("{s}: {any}", .{ argv, err }); } } for (procs) |_, i| { diff --git a/src/test.zig b/src/test.zig index 6e173f4..841acaa 100644 --- a/src/test.zig +++ b/src/test.zig @@ -4,13 +4,13 @@ export fn wifi_ssid_add_network(name: [*:0]const u8) void { _ = name; } -export fn lv_timer_del(timer: *opaque{}) void { +export fn lv_timer_del(timer: *opaque {}) void { _ = timer; } test { - std.testing.refAllDecls(@This()); - _ = @import("comm.zig"); _ = @import("ngui.zig"); + + std.testing.refAllDecls(@This()); } diff --git a/src/types.zig b/src/types.zig index 76d1e6f..ad4de50 100644 --- a/src/types.zig +++ b/src/types.zig @@ -11,4 +11,3 @@ pub const TestTimer = if (!builtin.is_test) @compileError("TestTimer is for test return self.value; } }; - diff --git a/src/ui/c/drv_fbev.c b/src/ui/c/drv_fbev.c index 840195e..93a0d88 100644 --- a/src/ui/c/drv_fbev.c +++ b/src/ui/c/drv_fbev.c @@ -2,55 +2,55 @@ * framebuffer display + evdev touchpad drivers init */ -#include "lvgl/lvgl.h" #include "lv_drivers/display/fbdev.h" #include "lv_drivers/indev/evdev.h" +#include "lvgl/lvgl.h" -#define DISP_BUF_SIZE NM_DISP_HOR * NM_DISP_VER / 10 +#define DISP_BUF_SIZE NM_DISP_HOR *NM_DISP_VER / 10 -lv_disp_t* drv_init(void) +lv_disp_t *drv_init(void) { - fbdev_init(); - - static lv_disp_draw_buf_t buf; - static lv_color_t cb[DISP_BUF_SIZE]; - lv_disp_draw_buf_init(&buf, cb, NULL, DISP_BUF_SIZE); - uint32_t hor, vert; - fbdev_get_sizes(&hor, &vert, NULL); - if (hor != NM_DISP_HOR || vert != NM_DISP_VER) { - LV_LOG_WARN("framebuffer display mismatch; expected %dx%d", NM_DISP_HOR, NM_DISP_VER); - } - - static lv_disp_drv_t disp_drv; - lv_disp_drv_init(&disp_drv); - disp_drv.draw_buf = &buf; - disp_drv.hor_res = NM_DISP_HOR; - disp_drv.ver_res = NM_DISP_VER; - disp_drv.antialiasing = 1; - disp_drv.flush_cb = fbdev_flush; - lv_disp_t* disp = lv_disp_drv_register(&disp_drv); - if (disp == NULL) { - return NULL; - } - - /* keypad input devices default group; - * future-proof: don't have any atm */ - lv_group_t* g = lv_group_create(); - if (g == NULL) { - return NULL; - } - lv_group_set_default(g); - - evdev_init(); - static lv_indev_drv_t touchpad_drv; - lv_indev_drv_init(&touchpad_drv); - touchpad_drv.type = LV_INDEV_TYPE_POINTER; - touchpad_drv.read_cb = evdev_read; - lv_indev_t* touchpad = lv_indev_drv_register(&touchpad_drv); - if (touchpad == NULL) { - /* TODO: or continue without the touchpad? */ - return NULL; - } - - return disp; + fbdev_init(); + + static lv_disp_draw_buf_t buf; + static lv_color_t cb[DISP_BUF_SIZE]; + lv_disp_draw_buf_init(&buf, cb, NULL, DISP_BUF_SIZE); + uint32_t hor, vert; + fbdev_get_sizes(&hor, &vert, NULL); + if (hor != NM_DISP_HOR || vert != NM_DISP_VER) { + LV_LOG_WARN("framebuffer display mismatch; expected %dx%d", NM_DISP_HOR, NM_DISP_VER); + } + + static lv_disp_drv_t disp_drv; + lv_disp_drv_init(&disp_drv); + disp_drv.draw_buf = &buf; + disp_drv.hor_res = NM_DISP_HOR; + disp_drv.ver_res = NM_DISP_VER; + disp_drv.antialiasing = 1; + disp_drv.flush_cb = fbdev_flush; + lv_disp_t *disp = lv_disp_drv_register(&disp_drv); + if (disp == NULL) { + return NULL; + } + + /* keypad input devices default group; + * future-proof: don't have any atm */ + lv_group_t *g = lv_group_create(); + if (g == NULL) { + return NULL; + } + lv_group_set_default(g); + + evdev_init(); + static lv_indev_drv_t touchpad_drv; + lv_indev_drv_init(&touchpad_drv); + touchpad_drv.type = LV_INDEV_TYPE_POINTER; + touchpad_drv.read_cb = evdev_read; + lv_indev_t *touchpad = lv_indev_drv_register(&touchpad_drv); + if (touchpad == NULL) { + /* TODO: or continue without the touchpad? */ + return NULL; + } + + return disp; } diff --git a/src/ui/c/drv_sdl2.c b/src/ui/c/drv_sdl2.c index f59bbfe..0f36480 100644 --- a/src/ui/c/drv_sdl2.c +++ b/src/ui/c/drv_sdl2.c @@ -2,72 +2,75 @@ * SDL2 drivers init for display, keyboard and mouse */ +#include "lv_drivers/sdl/sdl.h" #include "lvgl/lvgl.h" #include "lvgl/src/misc/lv_log.h" -#include "lv_drivers/sdl/sdl.h" -#define SDL_MAIN_HANDLED /*To fix SDL's "undefined reference to WinMain" issue*/ +#define SDL_MAIN_HANDLED /* suppress "undefined reference to WinMain" */ #include SDL_INCLUDE_PATH -lv_disp_t* drv_init(void) +lv_disp_t *drv_init(void) { - sdl_init(); - SDL_DisplayMode dm; - int dm_err = SDL_GetDesktopDisplayMode(0, &dm); - if (dm_err != 0) { - LV_LOG_WARN("SDL_GetDesktopDisplayMode: %i", dm_err); - } else { - unsigned char bpp = SDL_BITSPERPIXEL(dm.format); - LV_LOG_INFO("%ix%i %dbpp %s", dm.w, dm.h, bpp, SDL_GetPixelFormatName(dm.format)); - if (dm.w != NM_DISP_HOR || dm.h != NM_DISP_VER || bpp != LV_COLOR_DEPTH) { - LV_LOG_WARN("SDL display mismatch; expected %dx%d %dbpp", NM_DISP_HOR, NM_DISP_VER, LV_COLOR_DEPTH); - } - } + sdl_init(); + SDL_DisplayMode dm; + int dm_err = SDL_GetDesktopDisplayMode(0, &dm); + if (dm_err != 0) { + LV_LOG_WARN("SDL_GetDesktopDisplayMode: %i", dm_err); + } else { + unsigned char bpp = SDL_BITSPERPIXEL(dm.format); + LV_LOG_INFO("%ix%i %dbpp %s", dm.w, dm.h, bpp, SDL_GetPixelFormatName(dm.format)); + if (dm.w != NM_DISP_HOR || dm.h != NM_DISP_VER || bpp != LV_COLOR_DEPTH) { + LV_LOG_WARN("SDL display mismatch; expected %dx%d %dbpp", + NM_DISP_HOR, + NM_DISP_VER, + LV_COLOR_DEPTH); + } + } - static lv_disp_draw_buf_t buf; - static lv_color_t cb1[NM_DISP_HOR * 100]; - static lv_color_t cb2[NM_DISP_HOR * 100]; - lv_disp_draw_buf_init(&buf, cb1, cb2, NM_DISP_HOR * 100); + static lv_disp_draw_buf_t buf; + static lv_color_t cb1[NM_DISP_HOR * 100]; + static lv_color_t cb2[NM_DISP_HOR * 100]; + lv_disp_draw_buf_init(&buf, cb1, cb2, NM_DISP_HOR * 100); - static lv_disp_drv_t disp_drv; - lv_disp_drv_init(&disp_drv); - disp_drv.draw_buf = &buf; - disp_drv.flush_cb = sdl_display_flush; - disp_drv.hor_res = NM_DISP_HOR; - disp_drv.ver_res = NM_DISP_VER; - disp_drv.antialiasing = 1; - lv_disp_t* disp = lv_disp_drv_register(&disp_drv); - if (disp == NULL) { - return NULL; - } + static lv_disp_drv_t disp_drv; + lv_disp_drv_init(&disp_drv); + disp_drv.draw_buf = &buf; + disp_drv.flush_cb = sdl_display_flush; + disp_drv.hor_res = NM_DISP_HOR; + disp_drv.ver_res = NM_DISP_VER; + disp_drv.antialiasing = 1; + lv_disp_t *disp = lv_disp_drv_register(&disp_drv); + if (disp == NULL) { + return NULL; + } - static lv_indev_drv_t mouse_drv; - lv_indev_drv_init(&mouse_drv); - mouse_drv.type = LV_INDEV_TYPE_POINTER; - mouse_drv.read_cb = sdl_mouse_read; - lv_indev_t* mouse = lv_indev_drv_register(&mouse_drv); - if (mouse == NULL) { - LV_LOG_WARN("lv_indev_drv_register(&mouse_drv) returned NULL"); - } + static lv_indev_drv_t mouse_drv; + lv_indev_drv_init(&mouse_drv); + mouse_drv.type = LV_INDEV_TYPE_POINTER; + mouse_drv.read_cb = sdl_mouse_read; + lv_indev_t *mouse = lv_indev_drv_register(&mouse_drv); + if (mouse == NULL) { + LV_LOG_WARN("lv_indev_drv_register(&mouse_drv) returned NULL"); + } - /* keypad input devices default group */ - lv_group_t * g = lv_group_create(); - if (g == NULL) { - LV_LOG_WARN("lv_group_create returned NULL; won't set default group"); - } else { - lv_group_set_default(g); - } + /* keypad input devices default group */ + lv_group_t *g = lv_group_create(); + if (g == NULL) { + LV_LOG_WARN("lv_group_create returned NULL; won't set default group"); + } else { + lv_group_set_default(g); + } - static lv_indev_drv_t keyboard_drv; - lv_indev_drv_init(&keyboard_drv); - keyboard_drv.type = LV_INDEV_TYPE_KEYPAD; - keyboard_drv.read_cb = sdl_keyboard_read; - lv_indev_t *kb = lv_indev_drv_register(&keyboard_drv); - if (kb == NULL) { - LV_LOG_WARN("lv_indev_drv_register(&keyboard_drv) returned NULL"); - } else if (g) { - lv_indev_set_group(kb, g); - } + static lv_indev_drv_t keyboard_drv; + lv_indev_drv_init(&keyboard_drv); + keyboard_drv.type = LV_INDEV_TYPE_KEYPAD; + keyboard_drv.read_cb = sdl_keyboard_read; + lv_indev_t *kb = lv_indev_drv_register(&keyboard_drv); + if (kb == NULL) { + LV_LOG_WARN("lv_indev_drv_register(&keyboard_drv) returned NULL"); + } else if (g) { + lv_indev_set_group(kb, g); + } - return disp; + return disp; } diff --git a/src/ui/c/ui.c b/src/ui/c/ui.c index 6a16792..63f284d 100644 --- a/src/ui/c/ui.c +++ b/src/ui/c/ui.c @@ -7,18 +7,18 @@ #include #include -lv_disp_t* drv_init(void); +lv_disp_t *drv_init(void); static lv_style_t style_title; static lv_style_t style_text_muted; static lv_style_t style_btn_red; -static const lv_font_t* font_large; -static lv_obj_t* virt_keyboard; -static lv_obj_t* tabview; /* main tabs content parent; lv_tabview_create */ +static const lv_font_t *font_large; +static lv_obj_t *virt_keyboard; +static lv_obj_t *tabview; /* main tabs content parent; lv_tabview_create */ -static void textarea_event_cb(lv_event_t* e) +static void textarea_event_cb(lv_event_t *e) { - lv_obj_t* textarea = lv_event_get_target(e); + lv_obj_t *textarea = lv_event_get_target(e); lv_event_code_t code = lv_event_get_code(e); if (code == LV_EVENT_FOCUSED) { if (lv_indev_get_type(lv_indev_get_act()) != LV_INDEV_TYPE_KEYPAD) { @@ -38,31 +38,35 @@ static void textarea_event_cb(lv_event_t* e) lv_obj_set_height(tabview, NM_DISP_VER); lv_obj_add_flag(virt_keyboard, LV_OBJ_FLAG_HIDDEN); lv_obj_clear_state(textarea, LV_STATE_FOCUSED); - lv_indev_reset(NULL, textarea); /* forget the last clicked object to make it focusable again */ + lv_indev_reset(NULL, textarea); /* forget last obj to make it focusable again */ } } -static void create_bitcoin_panel(lv_obj_t* parent) +static void create_bitcoin_panel(lv_obj_t *parent) { - lv_obj_t* label = lv_label_create(parent); - lv_label_set_text_static(label, "bitcoin tab isn't designed yet\nfollow https://nakamochi.io"); - lv_obj_center(label); + lv_obj_t *label = lv_label_create(parent); + lv_label_set_text_static(label, + "bitcoin tab isn't designed yet\n" + "follow https://nakamochi.io"); + lv_obj_center(label); } -static void create_lnd_panel(lv_obj_t* parent) +static void create_lnd_panel(lv_obj_t *parent) { - lv_obj_t* label = lv_label_create(parent); - lv_label_set_text_static(label, "lightning tab isn't designed yet\nfollow https://nakamochi.io"); - lv_obj_center(label); + lv_obj_t *label = lv_label_create(parent); + lv_label_set_text_static(label, + "lightning tab isn't designed yet\n" + "follow https://nakamochi.io"); + lv_obj_center(label); } static struct { - lv_obj_t* wifi_spinner_obj; /* lv_spinner_create */ - lv_obj_t* wifi_status_obj; /* lv_label_create */ - lv_obj_t* wifi_connect_btn_obj; /* lv_btn_create */ - lv_obj_t* wifi_ssid_list_obj; /* lv_dropdown_create */ - lv_obj_t* wifi_pwd_obj; /* lv_textarea_create */ - lv_obj_t* power_halt_btn_obj; /* lv_btn_create */ + lv_obj_t *wifi_spinner_obj; /* lv_spinner_create */ + lv_obj_t *wifi_status_obj; /* lv_label_create */ + lv_obj_t *wifi_connect_btn_obj; /* lv_btn_create */ + lv_obj_t *wifi_ssid_list_obj; /* lv_dropdown_create */ + lv_obj_t *wifi_pwd_obj; /* lv_textarea_create */ + lv_obj_t *power_halt_btn_obj; /* lv_btn_create */ } settings; /** @@ -80,7 +84,7 @@ extern void ui_update_network_status(const char *text, const char *wifi_list) lv_label_set_text(settings.wifi_status_obj, text); } -static void wifi_connect_btn_callback(lv_event_t* e) +static void wifi_connect_btn_callback(lv_event_t *e) { (void)e; /* unused */ lv_obj_add_state(settings.wifi_connect_btn_obj, LV_STATE_DISABLED); @@ -111,143 +115,139 @@ static void power_halt_btn_callback(lv_event_t *e) /* first button must always be a "proceed", do shutdown; * text is irrelevant */ static const char *btns[] = {"PROCEED", "ABORT", NULL}; - lv_obj_t *msgbox = lv_msgbox_create( - NULL /* modal */, - "SHUTDOWN", /* title */ - "are you sure?", /* text */ - btns, - false /* close btn */); + lv_obj_t *msgbox = lv_msgbox_create(NULL, /* modal */ + "SHUTDOWN", /* title */ + "are you sure?", /* text */ + btns, /* */ + false /* close btn */); lv_obj_center(msgbox); lv_obj_add_event_cb(msgbox, power_halt_btn_callback, LV_EVENT_VALUE_CHANGED, msgbox); return; } -static void create_settings_panel(lv_obj_t* parent) +static void create_settings_panel(lv_obj_t *parent) { - /******************** - * wifi panel - ********************/ - lv_obj_t* wifi_panel = lv_obj_create(parent); - lv_obj_set_height(wifi_panel, LV_SIZE_CONTENT); - lv_obj_t * wifi_panel_title = lv_label_create(wifi_panel); - lv_label_set_text_static(wifi_panel_title, LV_SYMBOL_WIFI " WIFI"); - lv_obj_add_style(wifi_panel_title, &style_title, 0); - - lv_obj_t* wifi_spinner = lv_spinner_create(wifi_panel, 1000 /* speed */, 60 /* arc in deg */); - settings.wifi_spinner_obj = wifi_spinner; - lv_obj_add_flag(wifi_spinner, LV_OBJ_FLAG_HIDDEN); - lv_obj_set_size(wifi_spinner, 20, 20); - lv_obj_set_style_arc_width(wifi_spinner, 4, LV_PART_INDICATOR); - - lv_obj_t* wifi_status = lv_label_create(wifi_panel); - settings.wifi_status_obj = wifi_status; - lv_label_set_text_static(wifi_status, "unknown status"); - lv_label_set_long_mode(wifi_status, LV_LABEL_LONG_WRAP); - lv_obj_set_height(wifi_status, LV_SIZE_CONTENT); - lv_label_set_recolor(wifi_status, true); - - lv_obj_t* wifi_ssid_label = lv_label_create(wifi_panel); - lv_label_set_text_static(wifi_ssid_label, "network name"); - lv_obj_add_style(wifi_ssid_label, &style_text_muted, 0); - lv_obj_t* wifi_ssid = lv_dropdown_create(wifi_panel); - settings.wifi_ssid_list_obj = wifi_ssid; - lv_dropdown_clear_options(wifi_ssid); - - lv_obj_t* wifi_pwd_label = lv_label_create(wifi_panel); - lv_label_set_text_static(wifi_pwd_label, "password"); - lv_obj_add_style(wifi_pwd_label, &style_text_muted, 0); - lv_obj_t* wifi_pwd = lv_textarea_create(wifi_panel); - settings.wifi_pwd_obj = wifi_pwd; - lv_textarea_set_one_line(wifi_pwd, true); - lv_textarea_set_password_mode(wifi_pwd, true); - lv_obj_add_event_cb(wifi_pwd, textarea_event_cb, LV_EVENT_ALL, NULL); - - lv_obj_t* wifi_connect_btn = lv_btn_create(wifi_panel); - settings.wifi_connect_btn_obj = wifi_connect_btn; - lv_obj_set_height(wifi_connect_btn, LV_SIZE_CONTENT); - lv_obj_add_event_cb(wifi_connect_btn, wifi_connect_btn_callback, LV_EVENT_CLICKED, NULL); - lv_obj_t* wifi_connect_btn_label = lv_label_create(wifi_connect_btn); - lv_label_set_text_static(wifi_connect_btn_label, "CONNECT"); - lv_obj_center(wifi_connect_btn_label); - - /******************** - * power panel - ********************/ - lv_obj_t* power_panel = lv_obj_create(parent); - lv_obj_set_height(power_panel, LV_SIZE_CONTENT); - lv_obj_t * power_panel_title = lv_label_create(power_panel); - lv_label_set_text_static(power_panel_title, LV_SYMBOL_POWER " POWER"); - lv_obj_add_style(power_panel_title, &style_title, 0); - - lv_obj_t* poweroff_text = lv_label_create(power_panel); - lv_label_set_text_static(poweroff_text, "once shut down, the power cord\ncan be removed."); - lv_label_set_long_mode(poweroff_text, LV_LABEL_LONG_WRAP); - lv_obj_set_height(poweroff_text, LV_SIZE_CONTENT); - lv_label_set_recolor(poweroff_text, true); - - lv_obj_t* power_halt_btn = lv_btn_create(power_panel); - settings.power_halt_btn_obj = power_halt_btn; - lv_obj_set_height(power_halt_btn, LV_SIZE_CONTENT); - lv_obj_add_style(power_halt_btn, &style_btn_red, 0); - lv_obj_add_event_cb(power_halt_btn, power_halt_btn_callback, LV_EVENT_CLICKED, NULL); - lv_obj_t* power_halt_btn_label = lv_label_create(power_halt_btn); - lv_label_set_text_static(power_halt_btn_label, "SHUTDOWN"); - lv_obj_center(power_halt_btn_label); - - /******************** - * layout - ********************/ - static lv_coord_t parent_grid_cols[] = {LV_GRID_FR(1), LV_GRID_TEMPLATE_LAST}; - static lv_coord_t parent_grid_rows[] = { - LV_GRID_CONTENT, /* wifi panel */ - LV_GRID_CONTENT, /* power panel */ - LV_GRID_TEMPLATE_LAST - }; - lv_obj_set_grid_dsc_array(parent, parent_grid_cols, parent_grid_rows); - lv_obj_set_grid_cell(wifi_panel, LV_GRID_ALIGN_STRETCH, 0, 1, LV_GRID_ALIGN_CENTER, 0, 1); - lv_obj_set_grid_cell(power_panel, LV_GRID_ALIGN_STRETCH, 0, 1, LV_GRID_ALIGN_CENTER, 1, 1); - - static lv_coord_t wifi_grid_cols[] = {LV_GRID_FR(1), LV_GRID_FR(1), LV_GRID_TEMPLATE_LAST}; - static lv_coord_t wifi_grid_rows[] = { - LV_GRID_CONTENT, /* title */ - 5, /* separator */ - LV_GRID_CONTENT, /* wifi status text */ - 30, /* wifi selector */ - 5, /* separator */ - LV_GRID_CONTENT, /* password label */ - 30, /* password input */ - 5, /* separator */ - LV_GRID_CONTENT, /* connect btn */ - LV_GRID_TEMPLATE_LAST - }; - lv_obj_set_grid_dsc_array(wifi_panel, wifi_grid_cols, wifi_grid_rows); - lv_obj_set_grid_cell(wifi_panel_title, LV_GRID_ALIGN_STRETCH, 0, 1, LV_GRID_ALIGN_CENTER, 0, 1); - lv_obj_set_grid_cell(wifi_spinner, LV_GRID_ALIGN_END, 1, 1, LV_GRID_ALIGN_CENTER, 0, 1); - /* column 0 */ - lv_obj_set_grid_cell(wifi_status, LV_GRID_ALIGN_START, 0, 1, LV_GRID_ALIGN_START, 2, 7); - /* column 1 */ - lv_obj_set_grid_cell(wifi_ssid_label, LV_GRID_ALIGN_START, 1, 1, LV_GRID_ALIGN_START, 2, 1); - lv_obj_set_grid_cell(wifi_ssid, LV_GRID_ALIGN_STRETCH, 1, 1, LV_GRID_ALIGN_CENTER, 3, 1); - lv_obj_set_grid_cell(wifi_pwd_label, LV_GRID_ALIGN_START, 1, 1, LV_GRID_ALIGN_START, 5, 1); - lv_obj_set_grid_cell(wifi_pwd, LV_GRID_ALIGN_STRETCH, 1, 1, LV_GRID_ALIGN_CENTER, 6, 1); - lv_obj_set_grid_cell(wifi_connect_btn, LV_GRID_ALIGN_STRETCH, 1, 1, LV_GRID_ALIGN_CENTER, 8, 1); - - static lv_coord_t power_grid_cols[] = {LV_GRID_FR(1), LV_GRID_FR(1), LV_GRID_TEMPLATE_LAST}; - static lv_coord_t power_grid_rows[] = { - LV_GRID_CONTENT, /* title */ - 5, /* separator */ - LV_GRID_CONTENT, /* power off text and btn*/ - LV_GRID_TEMPLATE_LAST - }; - lv_obj_set_grid_dsc_array(power_panel, power_grid_cols, power_grid_rows); - lv_obj_set_grid_cell(power_panel_title, LV_GRID_ALIGN_STRETCH, 0, 2, LV_GRID_ALIGN_CENTER, 0, 1); - /* column 0 */ - lv_obj_set_grid_cell(poweroff_text, LV_GRID_ALIGN_START, 0, 1, LV_GRID_ALIGN_START, 2, 1); - /* column 1 */ - lv_obj_set_grid_cell(power_halt_btn, LV_GRID_ALIGN_STRETCH, 1, 1, LV_GRID_ALIGN_CENTER, 2, 1); + /******************** + * wifi panel + ********************/ + lv_obj_t *wifi_panel = lv_obj_create(parent); + lv_obj_set_height(wifi_panel, LV_SIZE_CONTENT); + lv_obj_t *wifi_panel_title = lv_label_create(wifi_panel); + lv_label_set_text_static(wifi_panel_title, LV_SYMBOL_WIFI " WIFI"); + lv_obj_add_style(wifi_panel_title, &style_title, 0); + + lv_obj_t *wifi_spinner = lv_spinner_create(wifi_panel, 1000 /* speed */, 60 /* arc in deg */); + settings.wifi_spinner_obj = wifi_spinner; + lv_obj_add_flag(wifi_spinner, LV_OBJ_FLAG_HIDDEN); + lv_obj_set_size(wifi_spinner, 20, 20); + lv_obj_set_style_arc_width(wifi_spinner, 4, LV_PART_INDICATOR); + + lv_obj_t *wifi_status = lv_label_create(wifi_panel); + settings.wifi_status_obj = wifi_status; + lv_label_set_text_static(wifi_status, "unknown status"); + lv_label_set_long_mode(wifi_status, LV_LABEL_LONG_WRAP); + lv_obj_set_height(wifi_status, LV_SIZE_CONTENT); + lv_label_set_recolor(wifi_status, true); + + lv_obj_t *wifi_ssid_label = lv_label_create(wifi_panel); + lv_label_set_text_static(wifi_ssid_label, "network name"); + lv_obj_add_style(wifi_ssid_label, &style_text_muted, 0); + lv_obj_t *wifi_ssid = lv_dropdown_create(wifi_panel); + settings.wifi_ssid_list_obj = wifi_ssid; + lv_dropdown_clear_options(wifi_ssid); + + lv_obj_t *wifi_pwd_label = lv_label_create(wifi_panel); + lv_label_set_text_static(wifi_pwd_label, "password"); + lv_obj_add_style(wifi_pwd_label, &style_text_muted, 0); + lv_obj_t *wifi_pwd = lv_textarea_create(wifi_panel); + settings.wifi_pwd_obj = wifi_pwd; + lv_textarea_set_one_line(wifi_pwd, true); + lv_textarea_set_password_mode(wifi_pwd, true); + lv_obj_add_event_cb(wifi_pwd, textarea_event_cb, LV_EVENT_ALL, NULL); + + lv_obj_t *wifi_connect_btn = lv_btn_create(wifi_panel); + settings.wifi_connect_btn_obj = wifi_connect_btn; + lv_obj_set_height(wifi_connect_btn, LV_SIZE_CONTENT); + lv_obj_add_event_cb(wifi_connect_btn, wifi_connect_btn_callback, LV_EVENT_CLICKED, NULL); + lv_obj_t *wifi_connect_btn_label = lv_label_create(wifi_connect_btn); + lv_label_set_text_static(wifi_connect_btn_label, "CONNECT"); + lv_obj_center(wifi_connect_btn_label); + + /******************** + * power panel + ********************/ + lv_obj_t *power_panel = lv_obj_create(parent); + lv_obj_set_height(power_panel, LV_SIZE_CONTENT); + lv_obj_t *power_panel_title = lv_label_create(power_panel); + lv_label_set_text_static(power_panel_title, LV_SYMBOL_POWER " POWER"); + lv_obj_add_style(power_panel_title, &style_title, 0); + + lv_obj_t *poweroff_text = lv_label_create(power_panel); + lv_label_set_text_static(poweroff_text, "once shut down, the power cord\ncan be removed."); + lv_label_set_long_mode(poweroff_text, LV_LABEL_LONG_WRAP); + lv_obj_set_height(poweroff_text, LV_SIZE_CONTENT); + lv_label_set_recolor(poweroff_text, true); + + lv_obj_t *power_halt_btn = lv_btn_create(power_panel); + settings.power_halt_btn_obj = power_halt_btn; + lv_obj_set_height(power_halt_btn, LV_SIZE_CONTENT); + lv_obj_add_style(power_halt_btn, &style_btn_red, 0); + lv_obj_add_event_cb(power_halt_btn, power_halt_btn_callback, LV_EVENT_CLICKED, NULL); + lv_obj_t *power_halt_btn_label = lv_label_create(power_halt_btn); + lv_label_set_text_static(power_halt_btn_label, "SHUTDOWN"); + lv_obj_center(power_halt_btn_label); + + /******************** + * layout + ********************/ + static lv_coord_t parent_grid_cols[] = {LV_GRID_FR(1), LV_GRID_TEMPLATE_LAST}; + static lv_coord_t parent_grid_rows[] = {/**/ + LV_GRID_CONTENT, /* wifi panel */ + LV_GRID_CONTENT, /* power panel */ + LV_GRID_TEMPLATE_LAST}; + lv_obj_set_grid_dsc_array(parent, parent_grid_cols, parent_grid_rows); + lv_obj_set_grid_cell(wifi_panel, LV_GRID_ALIGN_STRETCH, 0, 1, LV_GRID_ALIGN_CENTER, 0, 1); + lv_obj_set_grid_cell(power_panel, LV_GRID_ALIGN_STRETCH, 0, 1, LV_GRID_ALIGN_CENTER, 1, 1); + + static lv_coord_t wifi_grid_cols[] = {LV_GRID_FR(1), LV_GRID_FR(1), LV_GRID_TEMPLATE_LAST}; + static lv_coord_t wifi_grid_rows[] = {/**/ + LV_GRID_CONTENT, /* title */ + 5, /* separator */ + LV_GRID_CONTENT, /* wifi status text */ + 30, /* wifi selector */ + 5, /* separator */ + LV_GRID_CONTENT, /* password label */ + 30, /* password input */ + 5, /* separator */ + LV_GRID_CONTENT, /* connect btn */ + LV_GRID_TEMPLATE_LAST}; + lv_obj_set_grid_dsc_array(wifi_panel, wifi_grid_cols, wifi_grid_rows); + lv_obj_set_grid_cell(wifi_panel_title, LV_GRID_ALIGN_STRETCH, 0, 1, LV_GRID_ALIGN_CENTER, 0, 1); + lv_obj_set_grid_cell(wifi_spinner, LV_GRID_ALIGN_END, 1, 1, LV_GRID_ALIGN_CENTER, 0, 1); + /* column 0 */ + lv_obj_set_grid_cell(wifi_status, LV_GRID_ALIGN_START, 0, 1, LV_GRID_ALIGN_START, 2, 7); + /* column 1 */ + lv_obj_set_grid_cell(wifi_ssid_label, LV_GRID_ALIGN_START, 1, 1, LV_GRID_ALIGN_START, 2, 1); + lv_obj_set_grid_cell(wifi_ssid, LV_GRID_ALIGN_STRETCH, 1, 1, LV_GRID_ALIGN_CENTER, 3, 1); + lv_obj_set_grid_cell(wifi_pwd_label, LV_GRID_ALIGN_START, 1, 1, LV_GRID_ALIGN_START, 5, 1); + lv_obj_set_grid_cell(wifi_pwd, LV_GRID_ALIGN_STRETCH, 1, 1, LV_GRID_ALIGN_CENTER, 6, 1); + lv_obj_set_grid_cell(wifi_connect_btn, LV_GRID_ALIGN_STRETCH, 1, 1, LV_GRID_ALIGN_CENTER, 8, 1); + + static lv_coord_t power_grid_cols[] = {LV_GRID_FR(1), LV_GRID_FR(1), LV_GRID_TEMPLATE_LAST}; + static lv_coord_t power_grid_rows[] = {/**/ + LV_GRID_CONTENT, /* title */ + 5, /* separator */ + LV_GRID_CONTENT, /* power off text and btn*/ + LV_GRID_TEMPLATE_LAST}; + lv_obj_set_grid_dsc_array(power_panel, power_grid_cols, power_grid_rows); + lv_obj_set_grid_cell(power_panel_title, LV_GRID_ALIGN_STRETCH, 0, 2, LV_GRID_ALIGN_CENTER, 0, 1); + /* column 0 */ + lv_obj_set_grid_cell(poweroff_text, LV_GRID_ALIGN_START, 0, 1, LV_GRID_ALIGN_START, 2, 1); + /* column 1 */ + lv_obj_set_grid_cell(power_halt_btn, LV_GRID_ALIGN_STRETCH, 1, 1, LV_GRID_ALIGN_CENTER, 2, 1); } -static void tab_changed_event_cb(lv_event_t* e) +static void tab_changed_event_cb(lv_event_t *e) { (void)e; /* unused */ uint16_t n = lv_tabview_get_tab_act(tabview); @@ -262,66 +262,65 @@ static void tab_changed_event_cb(lv_event_t* e) extern int ui_init() { - lv_init(); - lv_disp_t* disp = drv_init(); - if (disp == NULL) { - return -1; - } - /* default theme is static */ - lv_theme_t* theme = lv_theme_default_init( - disp, - lv_palette_main(LV_PALETTE_BLUE), /* primary */ - lv_palette_main(LV_PALETTE_RED), /* secondary */ - true /*LV_THEME_DEFAULT_DARK*/, - LV_FONT_DEFAULT); - lv_disp_set_theme(disp, theme); - - font_large = &lv_font_courierprimecode_24; /* static */ - lv_style_init(&style_title); - lv_style_set_text_font(&style_title, font_large); - - lv_style_init(&style_text_muted); - lv_style_set_text_opa(&style_text_muted, LV_OPA_50); - - lv_style_init(&style_btn_red); - lv_style_set_bg_color(&style_btn_red, lv_palette_main(LV_PALETTE_RED)); - - /* global virtual keyboard */ - virt_keyboard = lv_keyboard_create(lv_scr_act()); - if (virt_keyboard == NULL) { - /* TODO: or continue without keyboard? */ - return -1; - } - lv_obj_add_flag(virt_keyboard, LV_OBJ_FLAG_HIDDEN); - - const lv_coord_t tabh = 60; - tabview = lv_tabview_create(lv_scr_act(), LV_DIR_TOP, tabh); - if (tabview == NULL) { - return -1; - } - - /** - * tab_changed_event_cb relies on the specific tab order, 0-based index: - * 0: bitcoin - * 1: lightning - * 2: settings - */ - lv_obj_t* tab_btc = lv_tabview_add_tab(tabview, NM_SYMBOL_BITCOIN " BITCOIN"); - if (tab_btc == NULL) { - return -1; - } - create_bitcoin_panel(tab_btc); - lv_obj_t* tab_lnd = lv_tabview_add_tab(tabview, NM_SYMBOL_BOLT " LIGHTNING"); - if (tab_lnd == NULL) { - return -1; - } - create_lnd_panel(tab_lnd); - lv_obj_t* tab_settings = lv_tabview_add_tab(tabview, LV_SYMBOL_SETTINGS " SETTINGS"); - if (tab_settings == NULL) { - return -1; - } - create_settings_panel(tab_settings); - - lv_obj_add_event_cb(tabview, tab_changed_event_cb, LV_EVENT_VALUE_CHANGED, NULL); - return 0; + lv_init(); + lv_disp_t *disp = drv_init(); + if (disp == NULL) { + return -1; + } + /* default theme is static */ + lv_theme_t *theme = lv_theme_default_init(disp, /**/ + lv_palette_main(LV_PALETTE_BLUE), /* primary */ + lv_palette_main(LV_PALETTE_RED), /* secondary */ + true, /* dark mode, LV_THEME_DEFAULT_DARK */ + LV_FONT_DEFAULT /* lv_conf.h def */); + lv_disp_set_theme(disp, theme); + + font_large = &lv_font_courierprimecode_24; /* static */ + lv_style_init(&style_title); + lv_style_set_text_font(&style_title, font_large); + + lv_style_init(&style_text_muted); + lv_style_set_text_opa(&style_text_muted, LV_OPA_50); + + lv_style_init(&style_btn_red); + lv_style_set_bg_color(&style_btn_red, lv_palette_main(LV_PALETTE_RED)); + + /* global virtual keyboard */ + virt_keyboard = lv_keyboard_create(lv_scr_act()); + if (virt_keyboard == NULL) { + /* TODO: or continue without keyboard? */ + return -1; + } + lv_obj_add_flag(virt_keyboard, LV_OBJ_FLAG_HIDDEN); + + const lv_coord_t tabh = 60; + tabview = lv_tabview_create(lv_scr_act(), LV_DIR_TOP, tabh); + if (tabview == NULL) { + return -1; + } + + /** + * tab_changed_event_cb relies on the specific tab order, 0-based index: + * 0: bitcoin + * 1: lightning + * 2: settings + */ + lv_obj_t *tab_btc = lv_tabview_add_tab(tabview, NM_SYMBOL_BITCOIN " BITCOIN"); + if (tab_btc == NULL) { + return -1; + } + create_bitcoin_panel(tab_btc); + lv_obj_t *tab_lnd = lv_tabview_add_tab(tabview, NM_SYMBOL_BOLT " LIGHTNING"); + if (tab_lnd == NULL) { + return -1; + } + create_lnd_panel(tab_lnd); + lv_obj_t *tab_settings = lv_tabview_add_tab(tabview, LV_SYMBOL_SETTINGS " SETTINGS"); + if (tab_settings == NULL) { + return -1; + } + create_settings_panel(tab_settings); + + lv_obj_add_event_cb(tabview, tab_changed_event_cb, LV_EVENT_VALUE_CHANGED, NULL); + return 0; } diff --git a/src/ui/c/ui.h b/src/ui/c/ui.h index c63d415..1029657 100644 --- a/src/ui/c/ui.h +++ b/src/ui/c/ui.h @@ -23,6 +23,6 @@ void nm_tab_settings_active(); * initiate connection to a wifi network with the given SSID and a password. * connection, if successful, is persisted in wpa_supplicant config. */ -int nm_wifi_start_connect(const char* ssid, const char* password); +int nm_wifi_start_connect(const char *ssid, const char *password); #endif diff --git a/tools/fmt-check.sh b/tools/fmt-check.sh new file mode 100755 index 0000000..009e3f6 --- /dev/null +++ b/tools/fmt-check.sh @@ -0,0 +1,5 @@ +#!/bin/sh +set -e +zig fmt --check . +C_FILES=$(find ./src -type f -name '*.c' ! -name 'lv_font*') +clang-format -style=file -dry-run -verbose -Werror $C_FILES