shithub: orca

Download patch

ref: fc8431f755beb623d1ef76809556fca2bda549f2
parent: cd4128f63f5486ca0f513c8e39ac97feaae4c446
author: cancel <cancel@cancel.fm>
date: Thu Dec 13 20:18:26 EST 2018

Change to use malloc/free for Qmenu

Storing a global guy was annoying

--- a/term_util.c
+++ b/term_util.c
@@ -136,12 +136,20 @@
 
 Qmsg* qmsg_of(Qnav_block* qb) { return ORCA_CONTAINER_OF(qb, Qmsg, nav_block); }
 
-void qmenu_start(Qmenu* qm) { memset(qm, 0, sizeof(Qmenu)); }
+Qmenu* qmenu_create(int id) {
+  Qmenu* qm = (Qmenu*)malloc(sizeof(Qmenu));
+  qm->ncurses_menu = NULL;
+  qm->ncurses_items[0] = NULL;
+  qm->items_count = 0;
+  qm->id = id;
+  return qm;
+}
 void qmenu_add_choice(Qmenu* qm, char const* text, int id) {
   ITEM* item = new_item(text, NULL);
   set_item_userptr(item, (void*)(intptr_t)(id));
   qm->ncurses_items[qm->items_count] = item;
   ++qm->items_count;
+  qm->ncurses_items[qm->items_count] = NULL;
 }
 void qmenu_add_spacer(Qmenu* qm) {
   ITEM* item = new_item(" ", NULL);
@@ -149,6 +157,7 @@
   set_item_userptr(item, &qmenu_spacer_user_unique);
   qm->ncurses_items[qm->items_count] = item;
   ++qm->items_count;
+  qm->ncurses_items[qm->items_count] = NULL;
 }
 void qmenu_push_to_nav(Qmenu* qm) {
   qm->ncurses_menu = new_menu(qm->ncurses_items);
@@ -168,6 +177,7 @@
   for (Usz i = 0; i < qm->items_count; ++i) {
     free_item(qm->ncurses_items[i]);
   }
+  free(qm);
 }
 
 void qnav_free_block(Qnav_block* qb) {
--- a/term_util.h
+++ b/term_util.h
@@ -66,6 +66,7 @@
   MENU* ncurses_menu;
   ITEM* ncurses_items[32];
   Usz items_count;
+  int id;
 } Qmenu;
 
 typedef enum {
@@ -100,7 +101,7 @@
 bool qmsg_drive(Qmsg* qm, int key);
 Qmsg* qmsg_of(Qnav_block* qb);
 
-void qmenu_start(Qmenu* qm);
+Qmenu* qmenu_create();
 void qmenu_add_choice(Qmenu* qm, char const* text, int id);
 void qmenu_add_spacer(Qmenu* qm);
 void qmenu_push_to_nav(Qmenu* qm);
--- a/tui_main.c
+++ b/tui_main.c
@@ -1439,6 +1439,10 @@
 //
 
 enum {
+  Main_menu_id = 1,
+};
+
+enum {
   Main_menu_quit = 1,
   Main_menu_controls,
   Main_menu_save,
@@ -1445,13 +1449,8 @@
   Main_menu_save_as,
 };
 
-struct {
-  Qmenu qmenu;
-} g_main_menu;
-
 void push_main_menu() {
-  Qmenu* qm = &g_main_menu.qmenu;
-  qmenu_start(qm);
+  Qmenu* qm = qmenu_create(Main_menu_id);
   qmenu_add_choice(qm, "Save", Main_menu_save);
   // qmenu_add_choice(qm, "Save As...", Main_menu_save_as);
   qmenu_add_spacer(qm);
@@ -1841,7 +1840,7 @@
             qnav_stack_pop();
           } break;
           case Qmenu_action_type_picked: {
-            if (qm == &g_main_menu.qmenu) {
+            if (qm->id == Main_menu_id) {
               switch (act.picked.id) {
               case Main_menu_quit:
                 goto quit;