shithub: orca

Download patch

ref: 02b9f2ba8aabe7bf471920b497eb5741cb1daa81
parent: 8b4a3bb6919f5ce3a6c27774e489d48be91d5ae7
author: cancel <cancel@cancel.fm>
date: Sun Jan 5 20:50:44 EST 2020

Add better display of portmidi menu items

--- a/term_util.c
+++ b/term_util.c
@@ -248,8 +248,10 @@
   if (!buffer)
     exit(1);
   va_start(ap, fmt);
-  vsnprintf(buffer, (Usz)msgbytes + 1, fmt, ap);
+  int printedbytes = vsnprintf(buffer, (Usz)msgbytes + 1, fmt, ap);
   va_end(ap);
+  if (printedbytes != msgbytes)
+    exit(1); // todo better handling?
   int lines = 1;
   int curlinewidth = 0;
   int maxlinewidth = 0;
@@ -373,6 +375,29 @@
   set_item_userptr(items[0], (void*)(uintptr_t)idx);
   extras[0].user_id = id;
   extras[0].owns_string = false;
+  extras[0].is_spacer = false;
+}
+void qmenu_add_printf(Qmenu* qm, int id, char const* fmt, ...) {
+  va_list ap;
+  va_start(ap, fmt);
+  int textsize = vsnprintf(NULL, 0, fmt, ap);
+  va_end(ap);
+  char* buffer = malloc((Usz)textsize + 1);
+  if (!buffer)
+    exit(1);
+  va_start(ap, fmt);
+  int printedsize = vsnprintf(buffer, (Usz)textsize + 1, fmt, ap);
+  va_end(ap);
+  if (printedsize != textsize)
+    exit(1); // todo better handling?
+  Usz idx;
+  ITEM** items;
+  struct Qmenu_item_extra* extras;
+  qmenu_allocitems(qm, 1, &idx, &items, &extras);
+  items[0] = new_item(buffer, NULL);
+  set_item_userptr(items[0], (void*)(uintptr_t)idx);
+  extras[0].user_id = id;
+  extras[0].owns_string = true;
   extras[0].is_spacer = false;
 }
 void qmenu_add_spacer(Qmenu* qm) {
--- a/term_util.h
+++ b/term_util.h
@@ -134,6 +134,10 @@
 int qmenu_id(Qmenu const* qm);
 void qmenu_set_title(Qmenu* qm, char const* title);
 void qmenu_add_choice(Qmenu* qm, int id, char const* text);
+#ifdef __GNUC__
+__attribute__((format(printf, 3, 4)))
+#endif
+void qmenu_add_printf(Qmenu* qm, int id, char const* fmt, ...);
 void qmenu_add_spacer(Qmenu* qm);
 void qmenu_set_current_item(Qmenu* qm, int id);
 void qmenu_set_displayed_active(Qmenu* qm, bool active);
--- a/tui_main.c
+++ b/tui_main.c
@@ -2186,12 +2186,19 @@
   }
   int num = Pm_CountDevices();
   int output_devices = 0;
+  int cur_dev_id = 0;
+  bool has_cur_dev_id = false;
+  if (midi_mode->any.type == Midi_mode_type_portmidi) {
+    cur_dev_id = midi_mode->portmidi.device_id;
+    has_cur_dev_id = true;
+  }
   for (int i = 0; i < num; ++i) {
     PmDeviceInfo const* info = Pm_GetDeviceInfo(i);
     if (!info || !info->output)
       continue;
-    qmenu_add_choice(qm, i, info->name);
-    // printf("ID: %-4d Name: %s\n", i, info->name);
+    bool is_cur_dev_id = has_cur_dev_id && cur_dev_id == i;
+    qmenu_add_printf(qm, i, "[%c] #%d - %s", is_cur_dev_id ? '*' : ' ', i,
+                     info->name);
     ++output_devices;
   }
   if (output_devices == 0) {
@@ -2200,9 +2207,8 @@
                      "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);
+  if (has_cur_dev_id) {
+    qmenu_set_current_item(qm, cur_dev_id);
   }
   qmenu_push_to_nav(qm);
 }