shithub: orca

Download patch

ref: d7934262d341216d70d3c3d67ea25f69d78dbf1b
parent: 5068a37553e1514cd1e0c347cca9b0db5993bad1
author: cancel <cancel@cancel.fm>
date: Sun Jan 5 01:47:52 EST 2020

Add live setting of MIDI device from menu

Still WIP

--- a/term_util.c
+++ b/term_util.c
@@ -73,6 +73,7 @@
   MENU* ncurses_menu;
   ITEM* ncurses_items[32];
   Usz items_count;
+  ITEM* initial_item;
   int id;
 };
 
@@ -299,6 +300,7 @@
   qm->ncurses_menu = NULL;
   qm->ncurses_items[0] = NULL;
   qm->items_count = 0;
+  qm->initial_item = NULL;
   qm->id = id;
   return qm;
 }
@@ -322,6 +324,22 @@
   ++qm->items_count;
   qm->ncurses_items[qm->items_count] = NULL;
 }
+void qmenu_set_current_item(Qmenu* qm, int id) {
+  ITEM* found = NULL;
+  for (Usz i = 0, n = qm->items_count; i < n; i++) {
+    if (item_userptr(qm->ncurses_items[i]) != (void*)(intptr_t)id)
+      continue;
+    found = qm->ncurses_items[i];
+    break;
+  }
+  if (!found)
+    return;
+  if (qm->ncurses_menu) {
+    set_current_item(qm->ncurses_menu, found);
+  } else {
+    qm->initial_item = found;
+  }
+}
 void qmenu_set_displayed_active(Qmenu* qm, bool active) {
   // Could add a flag in the Qmenu to avoid redundantly changing this stuff.
   set_menu_fore(qm->ncurses_menu, active ? A_BOLD : A_DIM);
@@ -350,6 +368,8 @@
     if (title_w > menu_min_w)
       menu_min_w = title_w;
   }
+  if (qm->initial_item)
+    set_current_item(qm->ncurses_menu, qm->initial_item);
   qnav_stack_push(&qm->qblock, menu_min_h, menu_min_w);
   set_menu_win(qm->ncurses_menu, qm->qblock.outer_window);
   set_menu_sub(qm->ncurses_menu, qm->qblock.content_window);
--- a/term_util.h
+++ b/term_util.h
@@ -135,6 +135,7 @@
 void qmenu_set_title(Qmenu* qm, char const* title);
 void qmenu_add_choice(Qmenu* qm, char const* text, int id);
 void qmenu_add_spacer(Qmenu* qm);
+void qmenu_set_current_item(Qmenu* qm, int id);
 void qmenu_set_displayed_active(Qmenu* qm, bool active);
 void qmenu_push_to_nav(Qmenu* qm);
 bool qmenu_drive(Qmenu* qm, int key, Qmenu_action* out_action);
--- a/tui_main.c
+++ b/tui_main.c
@@ -2172,7 +2172,7 @@
 }
 
 #ifdef FEAT_PORTMIDI
-void push_portmidi_output_device_menu(void) {
+void push_portmidi_output_device_menu(Midi_mode const* midi_mode) {
   Qmenu* qm = qmenu_create(Portmidi_output_device_menu_id);
   qmenu_set_title(qm, "PortMidi Device Selection");
   PmError e = portmidi_init_if_necessary();
@@ -2199,6 +2199,10 @@
                      "No PortMidi output devices found.");
     return;
   }
+  if (midi_mode->any.type == Midi_mode_type_portmidi) {
+    int dev_id = midi_mode->portmidi.device_id;
+    qmenu_set_current_item(qm, dev_id);
+  }
   qmenu_push_to_nav(qm);
 }
 #endif
@@ -2892,7 +2896,7 @@
                 break;
 #ifdef FEAT_PORTMIDI
               case Main_menu_choose_portmidi_output:
-                push_portmidi_output_device_menu();
+                push_portmidi_output_device_menu(&midi_mode);
                 break;
 #endif
               }
@@ -2958,6 +2962,18 @@
               } break;
               }
             }
+#ifdef FEAT_PORTMIDI
+            else if (qmenu_id(qm) == Portmidi_output_device_menu_id) {
+              midi_mode_deinit(&midi_mode);
+              PmError pme = midi_mode_init_portmidi(&midi_mode, act.picked.id);
+              qnav_stack_pop();
+              if (pme) {
+                qmsg_printf_push("PortMidi Error",
+                                 "Error setting PortMidi output device:\n%s",
+                                 Pm_GetErrorText(pme));
+              }
+            }
+#endif
           } break;
           }
         }