From 9448f4abe5bfd61e2fb7b8584f492ca41d576f7e Mon Sep 17 00:00:00 2001
From: Jonathan Wilkes <jancsika@yahoo.com>
Date: Tue, 21 Apr 2015 00:43:10 -0400
Subject: [PATCH] hooked audio and midi properties into the prefs dialog

---
 pd/nw/dialog_prefs.html |   8 ++-
 pd/nw/pdgui.js          |  28 ++++++++++-
 pd/nw/todo.txt          |   2 +
 pd/src/s_audio.c        |  32 ++++++++++++
 pd/src/s_inter.c        |  40 ++++++++++++---
 pd/src/s_midi.c         | 108 ++++++++++++++++++++++++++++++----------
 pd/src/s_stuff.h        |   4 ++
 7 files changed, 189 insertions(+), 33 deletions(-)

diff --git a/pd/nw/dialog_prefs.html b/pd/nw/dialog_prefs.html
index 164b8c5f0..6601cae44 100644
--- a/pd/nw/dialog_prefs.html
+++ b/pd/nw/dialog_prefs.html
@@ -312,10 +312,16 @@ pdgui.gui_post("guistub is " + gfxstub);
     }
 
     function audio_prefs_callback(attrs) {
-        pdgui.gui_post("attrs are " + attrs);
+        pdgui.gui_post("audio attrs are " + attrs);
         pdgui.gui_post("attrs length " + attrs.length);
     }
 
+    function midi_prefs_callback(attrs) {
+        pdgui.gui_post("midi attrs are " + attrs);
+        pdgui.gui_post("attrs length " + attrs.length);
+    }
+
+
     // This gets called from the nw_create_window function in index.html
     // It provides us with our window id from the C side.  Once we have it
     // we can create the menu and register event callbacks
diff --git a/pd/nw/pdgui.js b/pd/nw/pdgui.js
index c348907b2..75680fd19 100644
--- a/pd/nw/pdgui.js
+++ b/pd/nw/pdgui.js
@@ -501,7 +501,8 @@ function gui_check_unique (unique) {
 
 
 
-function gui_startup(version, apilist, midiapilist, fontname_from_pd, fontweight_from_pd) {
+function gui_startup(version, fontname_from_pd, fontweight_from_pd,
+    apilist, midiapilist) {
     console.log("we're starting up...");
     // # tb: user defined typefaces
     // our args:
@@ -3282,6 +3283,7 @@ function gui_audio_properties(gfxstub, sys_indevs, sys_outdevs,
     pd_indevs, pd_inchans, pd_outdevs, pd_outchans, audio_attrs) {
     
     var attrs = audio_attrs.concat([
+        "audio_apis", pd_apilist,
         "sys_indevs", sys_indevs,
         "sys_outdevs", sys_outdevs,
         "pd_indevs", pd_indevs,
@@ -3302,3 +3304,27 @@ function gui_audio_properties(gfxstub, sys_indevs, sys_outdevs,
         );
     }
 }
+
+function gui_midi_properties(gfxstub, sys_indevs, sys_outdevs,
+    pd_indevs, pd_outdevs, midi_attrs) {
+    
+    var attrs = midi_attrs.concat([
+        "midi_apis", pd_midiapilist,
+        "sys_indevs", sys_indevs,
+        "sys_outdevs", sys_outdevs,
+        "pd_indevs", pd_indevs,
+        "pd_outdevs", pd_outdevs,
+        ]);
+
+    gui_post("got back some midi props...");
+    for (var i = 0; i < arguments.length; i++) {
+        gui_post("arg " + i + " is " + arguments[i]);
+    }
+
+    if (dialogwin['prefs'] !== null) {
+        dialogwin['prefs'].eval(null,
+            'midi_prefs_callback('  +
+            JSON.stringify(attrs) + ');'
+        );
+    }
+}
diff --git a/pd/nw/todo.txt b/pd/nw/todo.txt
index 9b84187e0..c53597694 100644
--- a/pd/nw/todo.txt
+++ b/pd/nw/todo.txt
@@ -139,6 +139,8 @@ either nw_create_window should take fewer parameters or it should take an
     object with name: value pairs
 getting the sense that glist_isselected should _always_ have
     glist_getcanvas(x) as its first parameter
+get rid of the old tcl specific sys_get_audio_apis
+
 Crashers
 --------
 
diff --git a/pd/src/s_audio.c b/pd/src/s_audio.c
index 2e88da04f..e478c60e7 100644
--- a/pd/src/s_audio.c
+++ b/pd/src/s_audio.c
@@ -1029,6 +1029,38 @@ void sys_get_audio_apis(char *buf)
         strcpy(buf, "{}");
 }
 
+void sys_get_audio_apis2(t_binbuf *buf)
+{
+    int n = 0;
+#ifdef USEAPI_OSS
+    binbuf_addv(buf, "si", gensym("OSS"), API_OSS); n++;
+#endif
+#ifdef USEAPI_MMIO
+    binbuf_addv(buf, "si", gensym("standard (MMIO)"), API_MMIO); n++;
+#endif
+#ifdef USEAPI_ALSA
+    binbuf_addv(buf, "si", gensym("ALSA"), API_ALSA); n++;
+#endif
+#ifdef USEAPI_PORTAUDIO
+#ifdef MSW
+    binbuf_addv(buf, "si", gensym("ASIO (via portaudio)"), API_PORTAUDIO);
+#else
+#ifdef OSX
+    binbuf_addv(buf, "si", gensym("standard (portaudio)"), API_PORTAUDIO);
+#else
+    binbuf_addv(buf, "si", gensym("portaudio"), API_PORTAUDIO);
+#endif
+#endif
+     n++;
+#endif
+#ifdef USEAPI_JACK
+    binbuf_addv(buf, "si", gensym("JACK"), API_JACK); n++;
+#endif
+       /* then again, if only one API (or none) we don't offer any choice. */
+//    if (n < 2)
+//        strcpy(buf, "{}");
+}
+
 #ifdef USEAPI_ALSA
 void alsa_putzeros(int n);
 void alsa_getzeros(int n);
diff --git a/pd/src/s_inter.c b/pd/src/s_inter.c
index 15658b24b..dd58c5a95 100644
--- a/pd/src/s_inter.c
+++ b/pd/src/s_inter.c
@@ -821,13 +821,15 @@ void gui_start_vmess(const char *sel, char *fmt, ...)
 }
 
 static int gui_array_head;
+static int gui_array_tail;
 void gui_start_array(void)
 {
-    if (gui_array_head)
+    if (gui_array_head && !gui_array_tail)
         sys_gui("[");
     else
         sys_gui(",[");
     gui_array_head = 1;
+    gui_array_tail = 0;
 }
 
 void gui_f(t_float f)
@@ -839,6 +841,7 @@ void gui_f(t_float f)
     }
     else
         sys_vgui(",%g", f);
+    gui_array_tail = 0;
 }
 
 void gui_i(int i)
@@ -850,6 +853,7 @@ void gui_i(int i)
     }
     else
         sys_vgui(",%d", i);
+    gui_array_tail = 0;
 }
 
 void gui_s(const char *s) 
@@ -861,11 +865,13 @@ void gui_s(const char *s)
     }
     else
         sys_vgui(",\"%s\"", escape_double_quotes(s));
+    gui_array_tail = 0;
 }
 
 void gui_end_array(void)
 {
     sys_gui("]");
+    gui_array_tail = 1;
 }
 
 void gui_end_vmess(void)
@@ -1270,9 +1276,9 @@ int sys_startgui(const char *guidir)
 
 /* SUPERHACK - let's just load node-webkit and see what happens */
             sprintf(cmdbuf,
-                  "/home/nu/Downloads/nwjs-v0.12.0-linux-ia32/nw "
+                  "/home/bud/Downloads/nwjs-v0.12.1-linux-x64/nw "
 //                  "/home/nu/Downloads/nwjs-v0.12.0-alpha2-linux-ia32/nw "
-                "/home/nu/Downloads/test/ %d localhost %s\n",
+                "/home/bud/pd-nw/pd/nw/ %d localhost %s\n",
                 portno,
                 (sys_k12_mode ? "pd-l2ork-k12" : "pd-l2ork"));
 
@@ -1473,12 +1479,34 @@ int sys_startgui(const char *guidir)
 //         sys_vgui("pdtk_pd_startup {%s} %s %s {%s} %s\n", pd_version, buf, buf2, 
 //                  sys_font, sys_fontweight); 
 
-        gui_vmess("gui_startup", "sssss",
+        t_binbuf *aapis = binbuf_new(), *mapis = binbuf_new();
+        sys_get_audio_apis2(aapis);
+        sys_get_midi_apis2(mapis);
+        gui_start_vmess("gui_startup", "sss",
 		  pd_version,
-		  buf,
-		  buf2,
 		  sys_font,
 		  sys_fontweight);
+
+        int i;
+        gui_start_array(); // audio apis
+        for (i = 0; i < binbuf_getnatom(aapis); i+=2)
+        {
+            gui_s(atom_getsymbol(binbuf_getvec(aapis)+i)->s_name);
+            gui_i(atom_getint(binbuf_getvec(aapis)+i+1));
+        }
+        gui_end_array();
+
+        gui_start_array(); // midi apis
+        for (i = 0; i < binbuf_getnatom(mapis); i+=2)
+        {
+            gui_s(atom_getsymbol(binbuf_getvec(mapis)+i)->s_name);
+            gui_i(atom_getint(binbuf_getvec(mapis)+i+1));
+        }
+        gui_end_array();
+
+        gui_end_vmess();
+        binbuf_free(aapis);
+        binbuf_free(mapis);
     }
     return (0);
 
diff --git a/pd/src/s_midi.c b/pd/src/s_midi.c
index e9e6ac0bd..809e073f2 100644
--- a/pd/src/s_midi.c
+++ b/pd/src/s_midi.c
@@ -521,6 +521,23 @@ void sys_get_midi_apis(char *buf)
         strcpy(buf, "{}");
     
 }
+
+void sys_get_midi_apis2(t_binbuf *buf)
+{
+    int n = 0;
+#ifndef USEAPI_ALSA
+    binbuf_addv(buf, "si", gensym("OSS"), API_DEFAULT); n++;
+#else
+    n++;
+#endif
+#ifdef USEAPI_ALSA
+    binbuf_addv(buf, "si", gensym("ALSA"), API_ALSA); n++;
+#endif
+    /* then again, if only one API (or none) we don't offer any choice. */
+//    if (n < 2)
+//        strcpy(buf, "{}");
+}
+
 void sys_get_midi_params(int *pnmidiindev, int *pmidiindev,
     int *pnmidioutdev, int *pmidioutdev)
 {
@@ -678,15 +695,30 @@ void glob_midi_properties(t_pd *dummy, t_floatarg flongform)
     midi_getdevs(indevlist, &nindevs, outdevlist, &noutdevs,
         MAXNDEV, DEVDESCSIZE);
 
-    sys_gui("global midi_indevlist; set midi_indevlist {none}\n");
+    gui_start_vmess("gui_midi_properties", "s",
+        gfxstub_new2(&glob_pdobject, (void *)glob_midi_properties));
+
+    //sys_gui("global midi_indevlist; set midi_indevlist {none}\n");
+
+    gui_start_array();
     for (i = 0; i < nindevs; i++)
-        sys_vgui("lappend midi_indevlist {%s}\n",
-            indevlist + i * DEVDESCSIZE);
+    {
+        //sys_vgui("lappend midi_indevlist {%s}\n",
+        //    indevlist + i * DEVDESCSIZE);
+        gui_s(indevlist + i * DEVDESCSIZE);
+    }
+    gui_end_array();
+
+    //sys_gui("global midi_outdevlist; set midi_outdevlist {none}\n");
 
-    sys_gui("global midi_outdevlist; set midi_outdevlist {none}\n");
+    gui_start_array();
     for (i = 0; i < noutdevs; i++)
-        sys_vgui("lappend midi_outdevlist {%s}\n",
-            outdevlist + i * DEVDESCSIZE);
+    {
+        //sys_vgui("lappend midi_outdevlist {%s}\n",
+        //    outdevlist + i * DEVDESCSIZE);
+        gui_s(outdevlist + i * DEVDESCSIZE);
+    }
+    gui_end_array();
 
     sys_get_midi_params(&nindev, midiindev, &noutdev, midioutdev);
 
@@ -702,27 +734,53 @@ void glob_midi_properties(t_pd *dummy, t_floatarg flongform)
     midioutdev3 = (noutdev > 2 && midioutdev[2]>=0 ? midioutdev[2]+1 : 0);  
     midioutdev4 = (noutdev > 3 && midioutdev[3]>=0 ? midioutdev[3]+1 : 0);  
 
+    gui_start_array(); // input devices
+    gui_i(midiindev1);
+    gui_i(midiindev2);
+    gui_i(midiindev3);
+    gui_i(midiindev4);
+    gui_end_array();
+
+    gui_start_array(); // output devices
+    gui_i(midioutdev1);
+    gui_i(midioutdev2);
+    gui_i(midioutdev3);
+    gui_i(midioutdev4);
+    gui_end_array();
+
+    gui_start_array();
+    gui_s("flongform");
+    gui_i(flongform != 0);
+    gui_s("use_alsa");
+
 #ifdef USEAPI_ALSA
-      if (sys_midiapi == API_ALSA)
-    sprintf(buf,
-"pdtk_alsa_midi_dialog %%s \
-%d %d %d %d %d %d %d %d \
-%d 1\n",
-        midiindev1, midiindev2, midiindev3, midiindev4, 
-        midioutdev1, midioutdev2, midioutdev3, midioutdev4,
-        (flongform != 0));
-      else
+    if (sys_midiapi == API_ALSA)
+    {
+//    sprintf(buf,
+//"pdtk_alsa_midi_dialog %%s \
+//%d %d %d %d %d %d %d %d \
+//%d 1\n",
+//        midiindev1, midiindev2, midiindev3, midiindev4, 
+//        midioutdev1, midioutdev2, midioutdev3, midioutdev4,
+//        (flongform != 0));
+        gui_i(1);
+    }
+    else
 #endif
-    sprintf(buf,
-"pdtk_midi_dialog %%s \
-%d %d %d %d %d %d %d %d \
-%d\n",
-        midiindev1, midiindev2, midiindev3, midiindev4, 
-        midioutdev1, midioutdev2, midioutdev3, midioutdev4,
-        (flongform != 0));
-
-    gfxstub_deleteforkey(0);
-    gfxstub_new(&glob_pdobject, (void *)glob_midi_properties, buf);
+        gui_i(0);
+
+    gui_end_array();
+    gui_end_vmess();
+//    sprintf(buf,
+//"pdtk_midi_dialog %%s \
+//%d %d %d %d %d %d %d %d \
+//%d\n",
+//        midiindev1, midiindev2, midiindev3, midiindev4, 
+//        midioutdev1, midioutdev2, midioutdev3, midioutdev4,
+//        (flongform != 0));
+
+//    gfxstub_deleteforkey(0);
+//    gfxstub_new(&glob_pdobject, (void *)glob_midi_properties, buf);
 }
 
     /* new values from dialog window */
diff --git a/pd/src/s_stuff.h b/pd/src/s_stuff.h
index 5a9153ced..f1b71be2a 100644
--- a/pd/src/s_stuff.h
+++ b/pd/src/s_stuff.h
@@ -112,6 +112,7 @@ EXTERN void sys_get_audio_devs(char *indevlist, int *nindevs,
                           char *outdevlist, int *noutdevs, int *canmulti, int *cancallback, 
                           int maxndev, int devdescsize);
 EXTERN void sys_get_audio_apis(char *buf);
+EXTERN void sys_get_audio_apis2(t_binbuf *buf);
 
 /* s_midi.c */
 #define MAXMIDIINDEV 16         /* max. number of input ports */
@@ -126,6 +127,7 @@ void sys_open_midi(int nmidiin, int *midiinvec,
     int nmidiout, int *midioutvec, int enable);
 
 EXTERN void sys_get_midi_apis(char *buf);
+EXTERN void sys_get_midi_apis2(t_binbuf *buf);
 EXTERN void sys_get_midi_devs(char *indevlist, int *nindevs,
     char *outdevlist, int *noutdevs, 
    int maxndev, int devdescsize);
@@ -133,6 +135,7 @@ void sys_get_midi_params(int *pnmidiindev, int *pmidiindev,
     int *pnmidioutdev, int *pmidioutdev);
 
 void sys_get_midi_apis(char *buf);
+void sys_get_midi_apis2(t_binbuf *buf);
 
 void sys_reopen_midi( void);
 void sys_close_midi( void);
@@ -302,6 +305,7 @@ void sys_listmididevs(void);
 void sys_set_midi_api(int whichapi);
 void sys_set_audio_api(int whichapi);
 void sys_get_audio_apis(char *buf);
+void sys_get_audio_apis2(t_binbuf *buf);
 extern int sys_audioapi;
 void sys_set_audio_state(int onoff);
 
-- 
GitLab