Skip to content

Commit 39c8c96

Browse files
committed
alsa: refactor get_info a bit
1 parent fede74d commit 39c8c96

3 files changed

Lines changed: 135 additions & 52 deletions

File tree

include/libremidi/backends/alsa_seq/helpers.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -159,15 +159,15 @@ struct alsa_data
159159
snd.seq.set_client_name(seq, configuration.client_name.data());
160160

161161
#if __has_include(<alsa/ump.h>)
162-
if (snd.seq.ump.set_client_midi_version)
162+
if (snd.seq.set_client_midi_version)
163163
{
164164
switch (configuration.midi_version)
165165
{
166166
case 1:
167-
snd.seq.ump.set_client_midi_version(seq, SND_SEQ_CLIENT_LEGACY_MIDI);
167+
snd.seq.set_client_midi_version(seq, SND_SEQ_CLIENT_LEGACY_MIDI);
168168
break;
169169
case 2:
170-
snd.seq.ump.set_client_midi_version(seq, SND_SEQ_CLIENT_UMP_MIDI_2_0);
170+
snd.seq.set_client_midi_version(seq, SND_SEQ_CLIENT_UMP_MIDI_2_0);
171171
break;
172172
}
173173
}

include/libremidi/backends/alsa_seq/observer.hpp

Lines changed: 41 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@ struct port_info
2222
std::string port_name;
2323
int client{};
2424
int port{};
25-
bool isInput{};
26-
bool isOutput{};
25+
bool is_input{};
26+
bool is_output{};
2727
std::optional<int> card{};
28-
libremidi::port_information::port_type type{};
28+
libremidi::transport_type type{};
2929
};
3030

3131
template <typename ConfigurationImpl>
@@ -93,57 +93,67 @@ class observer_impl
9393
}
9494
}
9595

96-
std::optional<port_info> get_info(int client, int port) const noexcept
96+
std::optional<port_info> get_info(
97+
int client, int port, snd_seq_client_info_t& cinfo,
98+
snd_seq_port_info_t& pinfo) const noexcept
9799
{
100+
const auto tp = snd.seq.port_info_get_type(&pinfo);
101+
const auto cap = snd.seq.port_info_get_capability(&pinfo);
102+
if ((cap & SND_SEQ_PORT_CAP_NO_EXPORT) != 0)
103+
return std::nullopt;
104+
98105
port_info p;
99106
p.client = client;
100107
p.port = port;
101108

102-
snd_seq_client_info_t* cinfo;
103-
snd_seq_client_info_alloca(&cinfo);
104-
if (int err = snd.seq.get_any_client_info(seq, client, cinfo); err < 0)
105-
return std::nullopt;
106-
107-
snd_seq_port_info_t* pinfo;
108-
snd_seq_port_info_alloca(&pinfo);
109-
if (int err = snd.seq.get_any_port_info(seq, client, port, pinfo); err < 0)
110-
return std::nullopt;
111-
112-
const auto tp = snd.seq.port_info_get_type(pinfo);
113109
bool ok = this->configuration.track_any;
114110

115111
static constexpr auto virtual_port = SND_SEQ_PORT_TYPE_SOFTWARE | SND_SEQ_PORT_TYPE_SYNTHESIZER
116112
| SND_SEQ_PORT_TYPE_APPLICATION;
117113

118114
if ((tp & SND_SEQ_PORT_TYPE_HARDWARE) && this->configuration.track_hardware)
119115
{
120-
p.type = libremidi::port_information::port_type::hardware;
116+
p.type = libremidi::transport_type::hardware;
121117
ok = true;
122118
}
123119
else if ((tp & virtual_port) && this->configuration.track_virtual)
124120
{
125-
p.type = libremidi::port_information::port_type::software;
121+
p.type = libremidi::transport_type::software;
126122
ok = true;
127123
}
128124
if (!ok)
129125
return {};
130126

131-
if (auto name = snd.seq.client_info_get_name(cinfo))
127+
if (auto name = snd.seq.client_info_get_name(&cinfo))
132128
p.client_name = name;
133129

134-
if (auto name = snd.seq.port_info_get_name(pinfo))
130+
if (auto name = snd.seq.port_info_get_name(&pinfo))
135131
p.port_name = name;
136132

137-
if (int card = snd.seq.client_info_get_card(cinfo); card >= 0)
133+
if (int card = snd.seq.client_info_get_card(&cinfo); card >= 0)
138134
p.card = card;
139135

140-
const auto cap = snd.seq.port_info_get_capability(pinfo);
141-
p.isInput = (cap & SND_SEQ_PORT_CAP_DUPLEX) | (cap & SND_SEQ_PORT_CAP_READ);
142-
p.isOutput = (cap & SND_SEQ_PORT_CAP_DUPLEX) | (cap & SND_SEQ_PORT_CAP_WRITE);
136+
p.is_input = (cap & SND_SEQ_PORT_CAP_DUPLEX) | (cap & SND_SEQ_PORT_CAP_READ);
137+
p.is_output = (cap & SND_SEQ_PORT_CAP_DUPLEX) | (cap & SND_SEQ_PORT_CAP_WRITE);
143138

144139
return p;
145140
}
146141

142+
std::optional<port_info> get_info(int client, int port) const noexcept
143+
{
144+
snd_seq_client_info_t* cinfo;
145+
snd_seq_client_info_alloca(&cinfo);
146+
if (int err = snd.seq.get_any_client_info(seq, client, cinfo); err < 0)
147+
return std::nullopt;
148+
149+
snd_seq_port_info_t* pinfo;
150+
snd_seq_port_info_alloca(&pinfo);
151+
if (int err = snd.seq.get_any_port_info(seq, client, port, pinfo); err < 0)
152+
return std::nullopt;
153+
154+
return get_info(client, port, *cinfo, *pinfo);
155+
}
156+
147157
template <bool Input>
148158
auto to_port_info(const port_info& p) const noexcept
149159
-> std::conditional_t<Input, input_port, output_port>
@@ -209,8 +219,8 @@ class observer_impl
209219
snd, this->seq, [this, &ret](snd_seq_client_info_t& client, snd_seq_port_info_t& port) {
210220
int clt = snd.seq.client_info_get_client(&client);
211221
int pt = snd.seq.port_info_get_port(&port);
212-
if (auto p = get_info(clt, pt))
213-
if (p->isInput)
222+
if (auto p = get_info(clt, pt, client, port))
223+
if (p->is_input)
214224
ret.push_back(to_port_info<true>(*p));
215225
});
216226
return ret;
@@ -223,8 +233,8 @@ class observer_impl
223233
snd, this->seq, [this, &ret](snd_seq_client_info_t& client, snd_seq_port_info_t& port) {
224234
int clt = snd.seq.client_info_get_client(&client);
225235
int pt = snd.seq.port_info_get_port(&port);
226-
if (auto p = get_info(clt, pt))
227-
if (p->isOutput)
236+
if (auto p = get_info(clt, pt, client, port))
237+
if (p->is_output)
228238
ret.push_back(to_port_info<false>(*p));
229239
});
230240
return ret;
@@ -240,12 +250,12 @@ class observer_impl
240250
return;
241251

242252
m_knownClients[{p.client, p.port}] = p;
243-
if (p.isInput && configuration.input_added)
253+
if (p.is_input && configuration.input_added)
244254
{
245255
configuration.input_added(to_port_info<true>(p));
246256
}
247257

248-
if (p.isOutput && configuration.output_added)
258+
if (p.is_output && configuration.output_added)
249259
{
250260
configuration.output_added(to_port_info<false>(p));
251261
}
@@ -259,12 +269,12 @@ class observer_impl
259269
auto p = it->second;
260270
m_knownClients.erase(it);
261271

262-
if (p.isInput && configuration.input_removed)
272+
if (p.is_input && configuration.input_removed)
263273
{
264274
configuration.input_removed(to_port_info<true>(p));
265275
}
266276

267-
if (p.isOutput && configuration.output_removed)
277+
if (p.is_output && configuration.output_removed)
268278
{
269279
configuration.output_removed(to_port_info<false>(p));
270280
}

include/libremidi/backends/linux/alsa.hpp

Lines changed: 91 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,12 @@ struct libasound
292292
LIBREMIDI_SYMBOL_INIT(snd_seq, set_queue_tempo)
293293
LIBREMIDI_SYMBOL_INIT(snd_seq, subscribe_port)
294294
LIBREMIDI_SYMBOL_INIT(snd_seq, unsubscribe_port)
295+
296+
#if LIBREMIDI_ALSA_HAS_UMP
297+
LIBREMIDI_SYMBOL_INIT(snd_seq, set_client_midi_version)
298+
LIBREMIDI_SYMBOL_INIT(snd_seq, get_ump_endpoint_info)
299+
LIBREMIDI_SYMBOL_INIT(snd_seq, get_ump_block_info)
300+
#endif
295301
}
296302

297303
bool available{true};
@@ -336,6 +342,7 @@ struct libasound
336342
LIBREMIDI_SYMBOL_DEF(snd_seq, port_info_sizeof)
337343
LIBREMIDI_SYMBOL_DEF(snd_seq, port_subscribe_free)
338344
LIBREMIDI_SYMBOL_DEF(snd_seq, port_subscribe_malloc)
345+
LIBREMIDI_SYMBOL_DEF(snd_seq, port_subscribe_sizeof)
339346
LIBREMIDI_SYMBOL_DEF(snd_seq, port_subscribe_set_dest)
340347
LIBREMIDI_SYMBOL_DEF(snd_seq, port_subscribe_set_sender)
341348
LIBREMIDI_SYMBOL_DEF(snd_seq, port_subscribe_set_time_real)
@@ -352,6 +359,12 @@ struct libasound
352359
LIBREMIDI_SYMBOL_DEF(snd_seq, subscribe_port)
353360
LIBREMIDI_SYMBOL_DEF(snd_seq, unsubscribe_port)
354361

362+
#if LIBREMIDI_ALSA_HAS_UMP
363+
LIBREMIDI_SYMBOL_DEF(snd_seq, set_client_midi_version)
364+
LIBREMIDI_SYMBOL_DEF(snd_seq, get_ump_endpoint_info)
365+
LIBREMIDI_SYMBOL_DEF(snd_seq, get_ump_block_info)
366+
#endif
367+
355368
#if LIBREMIDI_ALSA_HAS_UMP
356369
struct ump_t
357370
{
@@ -362,18 +375,17 @@ struct libasound
362375
available = false;
363376
return;
364377
}
365-
LIBREMIDI_SYMBOL_INIT(snd_seq, set_client_midi_version)
366378
LIBREMIDI_SYMBOL_INIT(snd_seq_ump, event_input)
367379
LIBREMIDI_SYMBOL_INIT(snd_seq_ump, event_output)
368380
LIBREMIDI_SYMBOL_INIT(snd_seq_ump, event_output_direct)
369381
}
370382

371383
bool available{true};
372384

373-
LIBREMIDI_SYMBOL_DEF(snd_seq, set_client_midi_version)
374385
LIBREMIDI_SYMBOL_DEF(snd_seq_ump, event_input)
375386
LIBREMIDI_SYMBOL_DEF(snd_seq_ump, event_output)
376387
LIBREMIDI_SYMBOL_DEF(snd_seq_ump, event_output_direct)
388+
377389
} ump;
378390
#endif
379391
} seq{library};
@@ -389,39 +401,98 @@ struct libasound
389401
return;
390402
}
391403

392-
LIBREMIDI_SYMBOL_INIT(snd_ump, block_info_get_name)
393-
LIBREMIDI_SYMBOL_INIT(snd_ump, block_info_sizeof)
394-
LIBREMIDI_SYMBOL_INIT(snd_ump, close)
395-
LIBREMIDI_SYMBOL_INIT(snd_ump, endpoint_info_get_name)
396-
LIBREMIDI_SYMBOL_INIT(snd_ump, endpoint_info_sizeof)
404+
// Core UMP functions
397405
LIBREMIDI_SYMBOL_INIT(snd_ump, open)
398-
LIBREMIDI_SYMBOL_INIT(snd_ump, poll_descriptors)
399-
LIBREMIDI_SYMBOL_INIT(snd_ump, poll_descriptors_count)
400-
LIBREMIDI_SYMBOL_INIT(snd_ump, poll_descriptors_revents)
406+
LIBREMIDI_SYMBOL_INIT(snd_ump, close)
401407
LIBREMIDI_SYMBOL_INIT(snd_ump, rawmidi)
402408
LIBREMIDI_SYMBOL_INIT(snd_ump, rawmidi_params)
403409
LIBREMIDI_SYMBOL_INIT(snd_ump, rawmidi_params_current)
404410
LIBREMIDI_SYMBOL_INIT(snd_ump, read)
405411
LIBREMIDI_SYMBOL_INIT(snd_ump, tread)
406412
LIBREMIDI_SYMBOL_INIT(snd_ump, write)
413+
LIBREMIDI_SYMBOL_INIT(snd_ump, poll_descriptors)
414+
LIBREMIDI_SYMBOL_INIT(snd_ump, poll_descriptors_count)
415+
LIBREMIDI_SYMBOL_INIT(snd_ump, poll_descriptors_revents)
416+
417+
// Endpoint info functions
418+
LIBREMIDI_SYMBOL_INIT(snd_ump, endpoint_info)
419+
LIBREMIDI_SYMBOL_INIT(snd_ump, endpoint_info_sizeof)
420+
LIBREMIDI_SYMBOL_INIT(snd_ump, endpoint_info_get_card)
421+
LIBREMIDI_SYMBOL_INIT(snd_ump, endpoint_info_get_device)
422+
LIBREMIDI_SYMBOL_INIT(snd_ump, endpoint_info_get_flags)
423+
LIBREMIDI_SYMBOL_INIT(snd_ump, endpoint_info_get_protocol_caps)
424+
LIBREMIDI_SYMBOL_INIT(snd_ump, endpoint_info_get_protocol)
425+
LIBREMIDI_SYMBOL_INIT(snd_ump, endpoint_info_get_num_blocks)
426+
LIBREMIDI_SYMBOL_INIT(snd_ump, endpoint_info_get_version)
427+
LIBREMIDI_SYMBOL_INIT(snd_ump, endpoint_info_get_manufacturer_id)
428+
LIBREMIDI_SYMBOL_INIT(snd_ump, endpoint_info_get_family_id)
429+
LIBREMIDI_SYMBOL_INIT(snd_ump, endpoint_info_get_model_id)
430+
LIBREMIDI_SYMBOL_INIT(snd_ump, endpoint_info_get_sw_revision)
431+
LIBREMIDI_SYMBOL_INIT(snd_ump, endpoint_info_get_name)
432+
LIBREMIDI_SYMBOL_INIT(snd_ump, endpoint_info_get_product_id)
433+
434+
// Block info functions
435+
LIBREMIDI_SYMBOL_INIT(snd_ump, block_info)
436+
LIBREMIDI_SYMBOL_INIT(snd_ump, block_info_sizeof)
437+
LIBREMIDI_SYMBOL_INIT(snd_ump, block_info_set_block_id)
438+
LIBREMIDI_SYMBOL_INIT(snd_ump, block_info_get_block_id)
439+
LIBREMIDI_SYMBOL_INIT(snd_ump, block_info_get_active)
440+
LIBREMIDI_SYMBOL_INIT(snd_ump, block_info_get_flags)
441+
LIBREMIDI_SYMBOL_INIT(snd_ump, block_info_get_direction)
442+
LIBREMIDI_SYMBOL_INIT(snd_ump, block_info_get_first_group)
443+
LIBREMIDI_SYMBOL_INIT(snd_ump, block_info_get_num_groups)
444+
LIBREMIDI_SYMBOL_INIT(snd_ump, block_info_get_midi_ci_version)
445+
LIBREMIDI_SYMBOL_INIT(snd_ump, block_info_get_sysex8_streams)
446+
LIBREMIDI_SYMBOL_INIT(snd_ump, block_info_get_ui_hint)
447+
LIBREMIDI_SYMBOL_INIT(snd_ump, block_info_get_name)
407448
}
408449

409450
bool available{true};
410-
LIBREMIDI_SYMBOL_DEF(snd_ump, block_info_get_name)
411-
LIBREMIDI_SYMBOL_DEF(snd_ump, block_info_sizeof)
412-
LIBREMIDI_SYMBOL_DEF(snd_ump, close)
413-
LIBREMIDI_SYMBOL_DEF(snd_ump, endpoint_info_get_name)
414-
LIBREMIDI_SYMBOL_DEF(snd_ump, endpoint_info_sizeof)
451+
452+
// Core UMP functions
415453
LIBREMIDI_SYMBOL_DEF(snd_ump, open)
416-
LIBREMIDI_SYMBOL_DEF(snd_ump, poll_descriptors)
417-
LIBREMIDI_SYMBOL_DEF(snd_ump, poll_descriptors_count)
418-
LIBREMIDI_SYMBOL_DEF(snd_ump, poll_descriptors_revents)
454+
LIBREMIDI_SYMBOL_DEF(snd_ump, close)
419455
LIBREMIDI_SYMBOL_DEF(snd_ump, rawmidi)
420456
LIBREMIDI_SYMBOL_DEF(snd_ump, rawmidi_params)
421457
LIBREMIDI_SYMBOL_DEF(snd_ump, rawmidi_params_current)
422458
LIBREMIDI_SYMBOL_DEF(snd_ump, read)
423459
LIBREMIDI_SYMBOL_DEF(snd_ump, tread)
424460
LIBREMIDI_SYMBOL_DEF(snd_ump, write)
461+
LIBREMIDI_SYMBOL_DEF(snd_ump, poll_descriptors)
462+
LIBREMIDI_SYMBOL_DEF(snd_ump, poll_descriptors_count)
463+
LIBREMIDI_SYMBOL_DEF(snd_ump, poll_descriptors_revents)
464+
465+
// Endpoint info functions
466+
LIBREMIDI_SYMBOL_DEF(snd_ump, endpoint_info)
467+
LIBREMIDI_SYMBOL_DEF(snd_ump, endpoint_info_sizeof)
468+
LIBREMIDI_SYMBOL_DEF(snd_ump, endpoint_info_get_card)
469+
LIBREMIDI_SYMBOL_DEF(snd_ump, endpoint_info_get_device)
470+
LIBREMIDI_SYMBOL_DEF(snd_ump, endpoint_info_get_flags)
471+
LIBREMIDI_SYMBOL_DEF(snd_ump, endpoint_info_get_protocol_caps)
472+
LIBREMIDI_SYMBOL_DEF(snd_ump, endpoint_info_get_protocol)
473+
LIBREMIDI_SYMBOL_DEF(snd_ump, endpoint_info_get_num_blocks)
474+
LIBREMIDI_SYMBOL_DEF(snd_ump, endpoint_info_get_version)
475+
LIBREMIDI_SYMBOL_DEF(snd_ump, endpoint_info_get_manufacturer_id)
476+
LIBREMIDI_SYMBOL_DEF(snd_ump, endpoint_info_get_family_id)
477+
LIBREMIDI_SYMBOL_DEF(snd_ump, endpoint_info_get_model_id)
478+
LIBREMIDI_SYMBOL_DEF(snd_ump, endpoint_info_get_sw_revision)
479+
LIBREMIDI_SYMBOL_DEF(snd_ump, endpoint_info_get_name)
480+
LIBREMIDI_SYMBOL_DEF(snd_ump, endpoint_info_get_product_id)
481+
482+
// Block info functions
483+
LIBREMIDI_SYMBOL_DEF(snd_ump, block_info)
484+
LIBREMIDI_SYMBOL_DEF(snd_ump, block_info_sizeof)
485+
LIBREMIDI_SYMBOL_DEF(snd_ump, block_info_set_block_id)
486+
LIBREMIDI_SYMBOL_DEF(snd_ump, block_info_get_block_id)
487+
LIBREMIDI_SYMBOL_DEF(snd_ump, block_info_get_active)
488+
LIBREMIDI_SYMBOL_DEF(snd_ump, block_info_get_flags)
489+
LIBREMIDI_SYMBOL_DEF(snd_ump, block_info_get_direction)
490+
LIBREMIDI_SYMBOL_DEF(snd_ump, block_info_get_first_group)
491+
LIBREMIDI_SYMBOL_DEF(snd_ump, block_info_get_num_groups)
492+
LIBREMIDI_SYMBOL_DEF(snd_ump, block_info_get_midi_ci_version)
493+
LIBREMIDI_SYMBOL_DEF(snd_ump, block_info_get_sysex8_streams)
494+
LIBREMIDI_SYMBOL_DEF(snd_ump, block_info_get_ui_hint)
495+
LIBREMIDI_SYMBOL_DEF(snd_ump, block_info_get_name)
425496
} ump{library};
426497
#endif
427498
};
@@ -449,6 +520,8 @@ struct libasound
449520
#define snd_seq_client_info_alloca(ptr) snd_dylib_alloca(ptr, seq, client_info)
450521
#undef snd_seq_port_info_alloca
451522
#define snd_seq_port_info_alloca(ptr) snd_dylib_alloca(ptr, seq, port_info)
523+
#undef snd_seq_port_subscribe_alloca
524+
#define snd_seq_port_subscribe_alloca(ptr) snd_dylib_alloca(ptr, seq, port_subscribe)
452525
#undef snd_seq_queue_tempo_alloca
453526
#define snd_seq_queue_tempo_alloca(ptr) snd_dylib_alloca(ptr, seq, queue_tempo)
454527

0 commit comments

Comments
 (0)