From ba3c76f9eed6670cc7bf4ef022755c5036c36158 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Albert=20Gr=C3=A4f?= <aggraef@gmail.com> Date: Wed, 28 Dec 2022 02:43:39 +0100 Subject: [PATCH] Keep track of the actual device names in the MIDI prefs. This adds new midiindevname and midioutdevname keys to the preferences, which enables us to remap device indices on the fly at startup in case the device list has changed during invocations. (Which happens a lot, in particular, with the portmidi backend, due to MIDI devices being plugged and unplugged, change of virtual MIDI devices, etc.) Note that this doesn't provide hotplugging support, which is really what we'd like to have, but is currently impossible due to backend limitations. But at least Purr Data will not connect to the wrong devices on relaunch any more, which previously was a major annoyance. --- pd/src/s_file.c | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/pd/src/s_file.c b/pd/src/s_file.c index 5f8f570e6..63d575d12 100644 --- a/pd/src/s_file.c +++ b/pd/src/s_file.c @@ -609,6 +609,14 @@ void sys_loadpreferences( void) break; if (sscanf(prefbuf, "%d", &midiindev[i]) < 1) break; + /* AG: If we have a name for the device, find the proper device + index in case that there was a change to the device list + between invocations. */ + sprintf(keybuf, "midiindevname%d", i+1); + if (sys_getpreference(keybuf, prefbuf, MAXPDSTRING)) { + int d = sys_mididevnametonumber(0, prefbuf); + if (d >= 0) midiindev[i] = d; + } nmidiindev++; } } @@ -630,6 +638,14 @@ void sys_loadpreferences( void) break; if (sscanf(prefbuf, "%d", &midioutdev[i]) < 1) break; + /* AG: If we have a name for the device, find the proper device + index in case that there was a change to the device list + between invocations. */ + sprintf(keybuf, "midioutdevname%d", i+1); + if (sys_getpreference(keybuf, prefbuf, MAXPDSTRING)) { + int d = sys_mididevnametonumber(1, prefbuf); + if (d >= 0) midioutdev[i] = d; + } nmidioutdev++; } } @@ -788,6 +804,14 @@ void glob_savepreferences(t_pd *dummy) sprintf(buf1, "midiindev%d", i+1); sprintf(buf2, "%d", midiindev[i]); sys_putpreference(buf1, buf2); + /* AG: If we have a name for the device, store it with the device + index, so that we can find the proper device index after a change + to the device list between invocations. */ + sys_mididevnumbertoname(0, midiindev[i], buf2, MAXPDSTRING); + if (*buf2) { + sprintf(buf1, "midiindevname%d", i+1); + sys_putpreference(buf1, buf2); + } } sys_putpreference("nomidiout", (nmidioutdev <= 0 ? "True" : "False")); /* AG: nmidiout */ @@ -798,6 +822,14 @@ void glob_savepreferences(t_pd *dummy) sprintf(buf1, "midioutdev%d", i+1); sprintf(buf2, "%d", midioutdev[i]); sys_putpreference(buf1, buf2); + /* AG: If we have a name for the device, store it with the device + index, so that we can find the proper device index after a change + to the device list between invocations. */ + sys_mididevnumbertoname(1, midioutdev[i], buf2, MAXPDSTRING); + if (*buf2) { + sprintf(buf1, "midioutdevname%d", i+1); + sys_putpreference(buf1, buf2); + } } /* file search path */ -- GitLab