kee

Offline IOU signer with QR as transport
git clone https://holbrook.no/src/kee
Info | Log | Files | Refs | README | LICENSE

commit 4e187f7e6c3c012bfe74f57f349dbdeea55ff49b
parent 90b639eba51370ef07426f271f70a58085a3118a
Author: lash <dev@holbrook.no>
Date:   Sun, 26 May 2024 10:19:30 +0100

WIP replace navigation with beamenu

Diffstat:
MMakefile | 2+-
Msrc/Makefile | 6+++---
Msrc/aux/Makefile | 18+++++++++---------
Msrc/aux/beamenu/Makefile | 8++++----
Msrc/aux/beamenu/beamenu.c | 36+++++++++++++++++++++++-------------
Msrc/aux/beamenu/beamenu.h | 7+++++--
Msrc/aux/beamenu/gen.c | 55++++++++++++++++++++++++++++++++++++++++++++++++-------
Msrc/aux/beamenu/import.c | 1-
Msrc/aux/llog/llog.c | 4----
Msrc/aux/rerr/rerr.c | 1-
Msrc/gtk/Makefile | 7++++++-
Msrc/gtk/kee-entry-list.c | 3++-
Msrc/gtk/kee-menu.c | 40++++++++++++++++++++++++++--------------
Msrc/gtk/kee-menu.h | 4+++-
Msrc/gtk/kee-transport.c | 2+-
Msrc/gtk/nav.c | 115++++++++++++++++++++++++++++++++++++++-----------------------------------------
Msrc/gtk/nav.h | 29+++++++++++++++--------------
17 files changed, 201 insertions(+), 137 deletions(-)

diff --git a/Makefile b/Makefile @@ -8,7 +8,7 @@ gtk: core src: subs subs: glade - make -C src + make -C src all glade: gtk4-builder-tool simplify --3to4 glade.ui > src/gtk/main.ui diff --git a/src/Makefile b/src/Makefile @@ -3,7 +3,6 @@ INCLUDES := `pkg-config --cflags libgcrypt lmdb libxdg-basedir libqrencode zbar` CFLAGS += $(INCLUDES) -Wall LIBS := `pkg-config --libs libgcrypt zlib lmdb libxdg-basedir libqrencode zbar` -lb64 -llash LDFLAGS += $(LIBS) -AUXLIBS := `pkg-config --libs kee` #all: aux resource $(OBJS) # $(CC) $(CFLAGS) main.c -o a.out $(OBJS) $(LDFLAGS) aux/varint/varint.o @@ -17,13 +16,14 @@ asn: $(CC) $(CFLAGS) -c $< -o $@ $(LDFLAGS) aux: - make -C aux + echo "foo" + make -C aux all clean: rm -vf *.o make -C gtk clean make -C asn1 clean make -C tests clean -# make -C aux clean + make -C aux clean .PHONY: clean aux diff --git a/src/aux/Makefile b/src/aux/Makefile @@ -1,15 +1,15 @@ -all: lash local - -lash: llog rerr beamenu - -llog: +all: make -C llog - -rerr: make -C rerr - -beamenu: make -C beamenu + make -C beamenu gen local: -make -f Makefile.local + +clean: + make -C llog clean + make -C rerr clean + make -C beamenu clean + +.PHONY: clean diff --git a/src/aux/beamenu/Makefile b/src/aux/beamenu/Makefile @@ -1,9 +1,9 @@ OBJS := $(patsubst %.c,%.o,$(filter-out test.c gen.c,$(wildcard *.c))) INCLUDES := -I. -CFLAGS += $(INCLUDES) -DBEAMENU_N_EXITS=3 -DBEAMENU_N_DST=4 +CFLAGS += $(INCLUDES) VERSION = 0.0.1 -all: $(OBJS) gen +all: $(OBJS) test: all $(CC) $(CFLAGS) test.c beamenu.o export.o import.o -o test.out $(LDFLAGS) @@ -16,10 +16,10 @@ clean: rm -vf *.out rm -vf *.tar.gz rm -vf beamenu_gen - rm -vf beamenu_defs.c + rm -vf beamenu_defs.h archive: git archive --format=tar.gz HEAD -o beamenu-$(VERSION).tar.gz -gen: +gen: all $(CC) $(CFLAGS) gen.c beamenu.o export.o import.o -o beamenu_gen $(LDFLAGS) diff --git a/src/aux/beamenu/beamenu.c b/src/aux/beamenu/beamenu.c @@ -11,7 +11,6 @@ int beamenu_now; int beamenu_register(int idx, char *cn) { int i; int l; - char *p; l = strlen(cn) + 1; if (node[idx].cn) { @@ -25,12 +24,12 @@ int beamenu_register(int idx, char *cn) { for (i = 0; i < BEAMENU_N_EXITS; i++) { node[idx].dst[i] = 0; } + node[idx].i = idx; return 0; } void beamenu_free() { int i; - char *p; for (i = 0; i <= BEAMENU_N_DST; i++) { if (node[i].cn) { @@ -47,27 +46,39 @@ void beamenu_set(int idx_node, int idx_exit, int idx_dst) { o->dst[idx_exit] = idx_dst; } -int beamenu_move(int idx_exit) { - struct beamenu_node *o; - int r; +int beamenu_use_exit(int idx_exit) { int idx; - o = beamenu_get(beamenu_now); - idx = idx_exit; - r = o->dst[idx_exit]; - switch(r) { + idx = beamenu_get_exit(idx_exit); + switch(idx) { case BEAMENU_INACTIVE: - return 1; + return -1; break; case BEAMENU_ROOT: idx = 0; break; default: - idx = r; + if (idx_exit < 0 || idx_exit >= BEAMENU_N_EXITS) { + return -2; + } + } + + return beamenu_jump(idx); +} + +int beamenu_get_exit(int idx_exit) { + struct beamenu_node *o; + + if (idx_exit == BEAMENU_ROOT) { + return idx_exit; } + o = beamenu_get(beamenu_now); + return o->dst[idx_exit]; +} +int beamenu_jump(int idx) { beamenu_now = idx; - return 0; + return idx; } struct beamenu_node *beamenu_get(int idx_node) { @@ -76,4 +87,3 @@ struct beamenu_node *beamenu_get(int idx_node) { } return &node[idx_node]; } - diff --git a/src/aux/beamenu/beamenu.h b/src/aux/beamenu/beamenu.h @@ -6,7 +6,7 @@ #define BEAMENU_CN_MAXLEN 32 #ifndef BEAMENU_N_DST -#define BEAMENU_N_DST 0 +#define BEAMENU_N_DST 1 #endif #ifndef BEAMENU_N_EXITS @@ -17,6 +17,7 @@ struct beamenu_node { + int i; char *cn; int dst[BEAMENU_N_EXITS]; // all are 0 }; @@ -26,7 +27,9 @@ void beamenu_free(); void beamenu_set(int idx_node, int idx_exit, int idx_dst); struct beamenu_node *beamenu_get(int idx_node); int beamenu_load_file(const char *path, int msize); -int beamenu_move(int idx_exit); int beamenu_export(char *out, int width); +int beamenu_use_exit(int idx_exit); +int beamenu_get_exit(int idx_exit); +int beamenu_jump(int idx); #endif diff --git a/src/aux/beamenu/gen.c b/src/aux/beamenu/gen.c @@ -78,7 +78,6 @@ int addkey(char *k, int v) { int scan(int f, int l) { int r; - char v; if (tmpm == MODE_READ) { while(tmpbi < l) { @@ -204,10 +203,11 @@ int write_data() { int c; int f; int l; + int r; buf = malloc(BEAMENU_N_DST * (BEAMENU_CN_MAXLEN + BEAMENU_N_EXITS + 1)); if (!buf) { - return 1; + return 0; } l = beamenu_export(buf, BEAMENU_EXIT_SIZE); @@ -217,19 +217,23 @@ int write_data() { if (!c) { close(f); free(buf); - return 1; + return 0; } l -= c; + r += c; } close(f); free(buf); - return 0; + return r; } int write_defs() { + char *p; + char *pr; struct beamenu_node *o; char buf[1024]; + char buf_r[1024]; char k[KEYMAXLEN + 1]; int c; int f; @@ -237,11 +241,29 @@ int write_defs() { int r; int i; + p = "\nchar *beamenu_dst_r[] = {\n"; + strcpy(buf_r, p); + l = strlen(p); + pr = buf_r + l; + + f = open("beamenu_defs.h", O_WRONLY | O_CREAT, S_IRWXU); + r = 0; - f = open("beamenu_defs.c", O_WRONLY | O_CREAT, S_IRWXU); + p = "#ifndef BEAMENU_DEFS_H_\n#define BEAMENU_DEFS_H_\n\n"; + strcpy(buf, p); + l = strlen(p); + c = write(f, buf, l); + if (c != l) { + close(f); + return 0; + } + r += c; + for (i = 0; i < BEAMENU_N_DST; i++) { o = beamenu_get(i); strcpy(k, o->cn); + c = sprintf(pr, "\t\"%s\",\n", k); + pr += c; r = uc(k); sprintf(buf, "#define BEAMENU_DST_%s %d\n", k, i); l = strlen(buf); @@ -252,6 +274,25 @@ int write_defs() { } r += c; } + sprintf(pr, "};\n"); + l = strlen(buf_r); + c = write(f, buf_r, l); + if (c != l) { + close(f); + return 0; + } + r += c; + + p = "\n#endif\n"; + strcpy(buf, p); + l = strlen(p); + c = write(f, buf, l); + if (c != l) { + close(f); + return 0; + } + r += c; + close(f); return r; } @@ -331,12 +372,12 @@ int main(int argc, char **argv) { hdestroy(); r = write_data(); - if (r) { + if (!r) { return 1; } r = write_defs(); - if (r) { + if (!r) { return 1; } diff --git a/src/aux/beamenu/import.c b/src/aux/beamenu/import.c @@ -12,7 +12,6 @@ int beamenu_load_file(const char *path, int msize) { int ii; size_t c; char p[BEAMENU_CN_MAXLEN + 1]; - struct beamenu_node *o; f = open(path, O_RDONLY); if (f < 0) { diff --git a/src/aux/llog/llog.c b/src/aux/llog/llog.c @@ -184,14 +184,10 @@ char* llog_add_x(const char *k, long long v) { char *p; int c; char i; - long long r; char *b; - r = 0; p = kvstart((char*)k); c = 0; - //b = (char*)&r; - //b += (sizeof(r) - 1); b = (char*)&v; b += (sizeof(v) - 1); diff --git a/src/aux/rerr/rerr.c b/src/aux/rerr/rerr.c @@ -67,7 +67,6 @@ char* rerrstr(int code, char *buf) { char v; char *src; char *dst; - int i; splitcode(code, &k, &v); diff --git a/src/gtk/Makefile b/src/gtk/Makefile @@ -6,7 +6,7 @@ LIBS := `pkg-config --libs gtk4 zlib lmdb libgcrypt libxdg-basedir gstreamer-1.0 LDFLAGS += $(LIBS) AUXLIBS := `pkg-config --libs kee` -all: resource $(OBJS) +all: menu resource $(OBJS) $(CC) $(CFLAGS) main.c -o a.out $(LINKOBJS) $(LDFLAGS) $(AUXLIBS) %.o: %.c @@ -20,7 +20,12 @@ clean: rm -vf a.out rm -vf a_dev.out rm -vf resources.c + rm -vf beamenu_defs.h + rm -vf beamenu.dat make -C tests clean resource: glib-compile-resources kee.gresource.xml --target=resources.c --generate-source + +menu: + ../aux/beamenu/beamenu_gen menu.txt diff --git a/src/gtk/kee-entry-list.c b/src/gtk/kee-entry-list.c @@ -27,7 +27,8 @@ static void kee_entry_list_handle_select(GtkListView *view, guint i, KeeMenu *me o = KEE_ENTRY(gtk_single_selection_get_selected_item(sel)); g_object_take_ref(G_OBJECT(o)); - kee_menu_next(menu, "entry"); + //kee_menu_next(menu, "entry"); + kee_menu_next(menu, BEAMENU_DST_NEW); if (kee_entry_modeswitch(o, KEE_ENTRY_VIEWMODE_FULL)) { kee_menu_set(menu, GTK_WIDGET(o)); } diff --git a/src/gtk/kee-menu.c b/src/gtk/kee-menu.c @@ -18,7 +18,7 @@ struct _KeeMenu { GtkApplicationWindow parent; GtkHeaderBar *head; GtkStack *stack; - struct KeeNav nav; + //struct KeeNav nav; struct kee_context *ctx; }; @@ -35,7 +35,8 @@ static void kee_menu_act_back(GAction *act, GVariant *param, KeeMenu *menu) { static void kee_menu_act_import(GAction *act, GVariant *param, KeeMenu *menu) { g_log(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "act import"); //gtk_stack_set_visible_child_name(stack, "import"); - kee_menu_next(menu, "import"); + //kee_menu_next(menu, "import"); + kee_menu_next(menu, BEAMENU_DST_IMPORT); } static void kee_menu_act_new_entry(GAction *act, GVariant *param, KeeMenu *menu) { @@ -43,7 +44,8 @@ static void kee_menu_act_new_entry(GAction *act, GVariant *param, KeeMenu *menu) g_log(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "act new entry"); //gtk_stack_set_visible_child_name(stack, "import"); - kee_menu_next(menu, "entry"); + //kee_menu_next(menu, "entry"); + kee_menu_next(menu, BEAMENU_DST_NEW); o = g_object_new(KEE_TYPE_ENTRY, "orientation", GTK_ORIENTATION_VERTICAL, NULL); kee_entry_set_signer(o, &menu->ctx->gpg); @@ -97,7 +99,8 @@ static void kee_menu_act_import_entry(GAction *act, GVariant *param, KeeMenu *me g_log(G_LOG_DOMAIN, G_LOG_LEVEL_ERROR, "fail set entry widget view mode"); return; } - kee_menu_next(menu, "entry"); + //kee_menu_next(menu, "entry"); + kee_menu_next(menu, BEAMENU_DST_NEW); r = kee_menu_set(menu, GTK_WIDGET(entry)); if (r) { g_log(G_LOG_DOMAIN, G_LOG_LEVEL_ERROR, "fail replace menu entry content"); @@ -122,9 +125,9 @@ static void kee_menu_class_init(KeeMenuClass *kls) { static void kee_menu_init(KeeMenu *o) { //memset(&o->nav, 0, sizeof(struct KeeNav)); - o->nav.c = 0; - o->nav.widgets[0] = 0; - o->nav.now = 0; + //o->nav.c = 0; + //o->nav.widgets[0] = 0; + //o->nav.now = 0; o->head = GTK_HEADER_BAR(gtk_header_bar_new()); o->stack = GTK_STACK(gtk_stack_new()); } @@ -134,6 +137,8 @@ KeeMenu* kee_menu_new(GtkApplication *gapp, struct kee_context *ctx) { GtkWidget *butt; GSimpleAction *act; + kee_nav_init((char*)settings_get(ctx->settings, SETTINGS_DATA)); + o = g_object_new(KEE_TYPE_MENU, "application", gapp, NULL); o->ctx = ctx; gtk_widget_set_vexpand(GTK_WIDGET(o->stack), true); @@ -211,11 +216,15 @@ int kee_menu_add(KeeMenu *o, const char *label, GtkWidget *widget) { return ERR_OK; } -GtkWidget* kee_menu_next(KeeMenu *o, const char *label) { +//GtkWidget* kee_menu_next(KeeMenu *o, const char *label) { +GtkWidget* kee_menu_next(KeeMenu *o, int menu_id) { GtkWidget *widget; + char *label; + label = beamenu_dst_r[menu_id]; widget = gtk_stack_get_child_by_name(o->stack, label); - kee_nav_push(&o->nav, widget); + //kee_nav_push(&o->nav, widget); + kee_nav_set(widget, menu_id); gtk_stack_set_visible_child(o->stack, widget); kee_menu_header_update(o, label); return widget; @@ -225,7 +234,8 @@ int kee_menu_set(KeeMenu *o, GtkWidget *widget) { GtkBox *container; GtkWidget *widget_old; - container = GTK_BOX(o->nav.now); + //container = GTK_BOX(o->nav.now); + container = GTK_BOX(KEE_NAV_NOW); widget_old = gtk_widget_get_first_child(GTK_WIDGET(container)); if (widget_old) { @@ -236,12 +246,14 @@ int kee_menu_set(KeeMenu *o, GtkWidget *widget) { } int kee_menu_prev(KeeMenu *o) { + GtkWidget *widget; const char *label; - kee_nav_pop(&o->nav); - gtk_stack_set_visible_child(o->stack, o->nav.now); - label = gtk_stack_get_visible_child_name(o->stack); - kee_menu_header_update(o, label); + widget = kee_nav_back(); + gtk_stack_set_visible_child(o->stack, widget); + + //label = gtk_stack_get_visible_child_name(o->stack); + kee_menu_header_update(o, KEE_NAV_LABEL); return ERR_OK; } diff --git a/src/gtk/kee-menu.h b/src/gtk/kee-menu.h @@ -4,6 +4,7 @@ #include <glib-object.h> #include <gtk/gtk.h> +#include "beamenu_defs.h" #include "context.h" /// \todo rename to kee-win @@ -24,7 +25,8 @@ G_DECLARE_FINAL_TYPE(KeeMenu, kee_menu, KEE, MENU, GtkApplicationWindow); KeeMenu* kee_menu_new(GtkApplication *app, struct kee_context *ctx); int kee_menu_add(KeeMenu *o, const char *k, GtkWidget *v); -GtkWidget* kee_menu_next(KeeMenu *o, const char *k); +//GtkWidget* kee_menu_next(KeeMenu *o, const char *k); +GtkWidget* kee_menu_next(KeeMenu *o, int menu_id); int kee_menu_prev(KeeMenu *o); int kee_menu_set(KeeMenu *o, GtkWidget *widget); diff --git a/src/gtk/kee-transport.c b/src/gtk/kee-transport.c @@ -67,7 +67,7 @@ static void kee_transport_render(KeeTransport *o) { widget = gtk_widget_get_ancestor(GTK_WIDGET(o), KEE_TYPE_MENU); menu = KEE_MENU(widget); - kee_menu_next(menu, "transport"); + kee_menu_next(menu, BEAMENU_DST_TRANSPORT); } /// \todo share buffer with image data? diff --git a/src/gtk/nav.c b/src/gtk/nav.c @@ -1,76 +1,71 @@ +#include <string.h> + #include <gtk/gtk.h> +#include <beamenu.h> #include "nav.h" +#include "beamenu_defs.h" + + +static GtkWidget* widgets[KEE_NAV_N_DST]; + +int kee_nav_init(const char *path) { + char *p; + char fullpath[1024]; + + p = stpcpy(fullpath, path); + if (*(p-1) != '/') { + *p = '/'; + p++; + } + p = strcpy(p, "beamenu.dat"); + + return beamenu_load_file(fullpath, 1); +} -//static void kee_nav_log(struct KeeNav *nav) { -// char s[128]; -// char out[1024]; -// int c; -// int i; -// -// c = 0; -// for (i = 0; i < nav->c + 1; i++) { -// sprintf(s, "[%d:%p] ", i, nav->widgets[i]); -// sprintf(out+c, s); -// c += strlen(s); -// } -// g_log(G_LOG_DOMAIN, G_LOG_LEVEL_INFO, "nav now %p: %s", nav->now, out); -//} +int kee_nav_set(GtkWidget *widget, int menu_id) { + if (widgets[menu_id]) { + return 1; + } + widgets[menu_id] = widget; + beamenu_jump(menu_id); + return 0; +} -int kee_nav_push(struct KeeNav *nav, GtkWidget *page) { - nav->c++; - nav->widgets[nav->c] = page; - nav->now = nav->widgets[nav->c]; -// kee_nav_log(nav); +int kee_nav_unset(int menu_id) { + if (widgets[menu_id] == NULL) { + return 1; + } + widgets[menu_id] = 0x0; return 0; } -GtkWidget* kee_nav_pop(struct KeeNav *nav) { - if (nav->c == 0) { +GtkWidget* kee_nav_back() { + GtkWidget *widget; + int r; + + r = beamenu_use_exit(KEE_NAV_EXIT_BACK); + if (r < 0) { + return NULL; + } + if (widgets[r] == NULL) { return NULL; } - //r = nav->widgets[nav->c]; - nav->c--; - nav->now = nav->widgets[nav->c]; - return nav->now; -// kee_nav_log(nav); + widget = widgets[r]; + widgets[r] = 0x0; + return widget; } +GtkWidget* kee_nav_get() { + struct beamenu_node *o; -int kee_nav_is_top(struct KeeNav *nav) { - return nav->c == 0; + o = beamenu_get(-1); + return widgets[o->i]; } +char *kee_nav_get_label() { + struct beamenu_node *o; -// -//void kee_nav_push(struct KeeNav *nav, GtkWidget *page) { -// struct KeeNav *nav_old; -// -// nav_old = malloc(sizeof(struct KeeNav)); -// nav_old->prev = nav->prev; -// nav_old->now = nav->now; -// nav->prev = nav_old; -// nav->now = page; -//} -// -// -//GtkWidget* kee_nav_pop(struct KeeNav *nav) { -// struct KeeNav *nav_old; -// GtkWidget *r; -// -// if (!nav->prev) { -// return NULL; -// } -// -// r = nav->now; -// nav_old = nav->prev->prev; -// nav->now = nav->prev->now; -// free(nav->prev); -// nav->prev = nav_old; -// return r; -//} -// -// -//int kee_nav_is_top(struct KeeNav *nav) { -// return !nav->prev; -//} + o = beamenu_get(-1); + return beamenu_dst_r[o->i]; +} diff --git a/src/gtk/nav.h b/src/gtk/nav.h @@ -3,22 +3,23 @@ #include <gtk/gtk.h> +#include <beamenu_defs.h> + +#define KEE_NAV_NOW kee_nav_get() +#define KEE_NAV_LABEL kee_nav_label() + #ifndef KEE_NAV_N_DST -#define KEE_NAV_N_DST 0 +#define KEE_NAV_N_DST BEAMENU_N_DST + 1 #endif -struct KeeNav { -// GtkWidget *now; -// struct KeeNav *prev; - GtkWidget *now; - GtkWidget *widgets[128]; - int c; - int flags; - void *dst[KEE_NAV_N_DST + 1]; -}; - -int kee_nav_push(struct KeeNav *nav, GtkWidget *page); -GtkWidget* kee_nav_pop(struct KeeNav *nav); -int kee_nav_is_top(struct KeeNav *nav); + +#define KEE_NAV_EXIT_BACK 0 + +int kee_nav_init(const char *path); +int kee_nav_set(GtkWidget *, int idx); +int kee_nav_unset(int idx); +GtkWidget* kee_nav_get(); +char* kee_nav_label(); +GtkWidget* kee_nav_back(); // returns new current widget #endif // _KEE_NAV_H