Skip to content

Commit

Permalink
Merge branch '131-do-not-import-everything'
Browse files Browse the repository at this point in the history
  • Loading branch information
davidmoreno committed Oct 21, 2024
2 parents 060866f + 81a3043 commit 93d920f
Show file tree
Hide file tree
Showing 14 changed files with 593 additions and 209 deletions.
29 changes: 29 additions & 0 deletions default.ini
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,35 @@ control=/var/run/rtpmidid/control.sock
name={{hostname}}
port=5004

# RTPMIDI discover
# some rules to just dont accept any announcements. If do not pass the rules,
# no ALSA port will be created to allow easy connect to the other side.
# name will be "servername:port/service". But as they are regex, just write the
# part you are interested in: for example:
#
# name_positive_regex=.* # will accept any name
# name_positive_regex=:5004/ # will accept any at port 5004
# name_positive_regex=(Peak|Ableton) # will accept only Peak or Ableton, any host any port
# name_negative_regex=.* # will reject any other name
#
# The check order is:
# 1. First we check the negative regex, if match reject.
# 2. Then the positive. If match accept.
# 3. If no positive match, reject.
#
# If you really need to reject and accept by name in a serious way, the regex should
# be complete, with ^ and $. For example:
#
# name_positive_regex=^(raspberry.local:5004/HYDRASYNTH KB-HYDRASYNTH KB MIDI 1)$
#
[rtpmidi_discover]
# enabled = true|false
enabled=true
# by default negative does nothing
name_negative_regex=^$
# and we accept everything
name_positive_regex=.*

# Alsa announcement requires no firewall as it creates random
# ports for each connection. If you want to not have an alsa_announce
# section, comment it out or delete it.
Expand Down
3 changes: 2 additions & 1 deletion lib/mdns_rtpmidi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,8 @@ void rtpmidid::mdns_rtpmidi_t::resolve_callback(
// make unique? or filter on interface?
discovered_remote(rtpmidid::remote_announcement_t{
data.name,
avahi_address_str.data(),
data.host_name,
// avahi_address_str.data(),
data.port,
});
}
Expand Down
97 changes: 65 additions & 32 deletions src/argv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "ini.hpp"
#include "rtpmidid/logger.hpp"
#include "settings.hpp"
#include "stringpp.hpp"
#include <algorithm>
#include <array>
#include <fmt/core.h>
Expand Down Expand Up @@ -93,14 +94,13 @@ static std::string get_hostname() {

void help(const std::vector<argument_t> &arguments) {
fmt::print(CMDLINE_HELP, VERSION);
fmt::print("{} args", arguments.size());
for (auto &argument : arguments) {
fmt::print(" {:<30} {}\n", argument.arg, argument.comment);
}
}

// Setup the argument options
static std::vector<argument_t> setup_arguments() {
static std::vector<argument_t> setup_arguments(settings_t *settings) {
std::vector<argument_t> arguments;

arguments.emplace_back( //
Expand All @@ -111,52 +111,85 @@ static std::vector<argument_t> setup_arguments() {

arguments.emplace_back("--port", //
"Opens local port as server. Default 5004.",
[](const std::string &value) {
if (settings.rtpmidi_announces.size() == 0) {
settings.rtpmidi_announces.emplace_back();
settings.rtpmidi_announces.begin()->name =
[settings](const std::string &value) {
if (settings->rtpmidi_announces.size() == 0) {
settings->rtpmidi_announces.emplace_back();
settings->rtpmidi_announces.begin()->name =
get_hostname();
}
settings.rtpmidi_announces.begin()->port = value;
settings->rtpmidi_announces.begin()->port = value;
});
arguments.emplace_back( //
"--name", //
"Forces the alsa and rtpmidi name", [](const std::string &value) {
if (settings.rtpmidi_announces.size() == 0) {
settings.rtpmidi_announces.emplace_back();
"Forces the alsa and rtpmidi name", [settings](const std::string &value) {
if (settings->rtpmidi_announces.size() == 0) {
settings->rtpmidi_announces.emplace_back();
}
if (settings.alsa_announces.size() == 0) {
settings.alsa_announces.emplace_back();
if (settings->alsa_announces.size() == 0) {
settings->alsa_announces.emplace_back();
}

settings.rtpmidi_announces.begin()->name = value;
settings.alsa_announces.begin()->name = value;
settings.alsa_name = value;
settings->rtpmidi_announces.begin()->name = value;
settings->alsa_announces.begin()->name = value;
settings->alsa_name = value;
});
arguments.emplace_back( //
"--alsa-name", //
"Forces the alsa name", [](const std::string &value) {
if (settings.alsa_announces.size() == 0) {
settings.alsa_announces.emplace_back();
"Forces the alsa name", [settings](const std::string &value) {
if (settings->alsa_announces.size() == 0) {
settings->alsa_announces.emplace_back();
}
settings.alsa_announces.begin()->name = value;
settings->alsa_announces.begin()->name = value;
});
arguments.emplace_back( //
"--rtpmidid-name", //
"Forces the rtpmidi name", [](const std::string &value) {
if (settings.rtpmidi_announces.size() == 0) {
settings.rtpmidi_announces.emplace_back();
"Forces the rtpmidi name", [settings](const std::string &value) {
if (settings->rtpmidi_announces.size() == 0) {
settings->rtpmidi_announces.emplace_back();
}
settings.rtpmidi_announces.begin()->name = value;
settings->rtpmidi_announces.begin()->name = value;
});
arguments.emplace_back("--control",
"Creates a control socket. Check CONTROL.md. Default "
"`/var/run/rtpmidid/control.sock`",
[settings](const std::string &value) {
settings->control_filename = value;
});
arguments.emplace_back( //
"--rtpmidi-discover",
"Enable or disable rtpmidi discover. true | false | [posregex] | "
"![negregex]",
[settings](const std::string &value) {
if (value == "true") {
DEBUG("rtpmidi_discover.enabled = true");
settings->rtpmidi_discover.enabled = true;
} else if (value == "false") {
DEBUG("rtpmidi_discover.enabled = false");
settings->rtpmidi_discover.enabled = false;
} else if (std::startswith(value, "!")) {
DEBUG("rtpmidi_discover.name_negative_regex = {}", value.substr(1));
settings->rtpmidi_discover.name_negative_regex =
std::regex(value.substr(1, std::string::npos));
} else {
DEBUG("rtpmidi_discover.name_positive_regex = {}", value);
settings->rtpmidi_discover.name_positive_regex = std::regex(value);
}
});
arguments.emplace_back( //
"--rawmidi",
"Connects to a rawmidi device. For example `/dev/snd/midiC1D0`",
[settings](const std::string &value) {
if (value.size() == 0) {
ERROR("Empty rawmidi device. Doing nothing.");
return;
}
settings->rawmidi.emplace_back();
settings->rawmidi.back().device = value;
settings->rawmidi.back().name = rtpmididns::split(value, '/').back();
});
arguments.emplace_back(
"--control",
"Creates a control socket. Check CONTROL.md. Default "
"`/var/run/rtpmidid/control.sock`",
[](const std::string &value) { settings.control_filename = value; });
arguments.emplace_back( //
"--version", //
"Show version", [](const std::string &value) {
"Show version", [settings](const std::string &value) {
fmt::print("rtpmidid version {}/2\n", VERSION);
exit(0);
});
Expand All @@ -174,8 +207,8 @@ static std::vector<argument_t> setup_arguments() {
// Parses the argv and sets up the settings_t struct
// for parameters that affect a alsa and rtpmidi announcements, it changes the
// first announced, and creates it if needed
void parse_argv(const std::vector<std::string> &argv) {
std::vector<argument_t> arguments = setup_arguments();
void parse_argv(const std::vector<std::string> &argv, settings_t *settings) {
std::vector<argument_t> arguments = setup_arguments(settings);
// Necesary for two part arguments
argument_t *current_argument = nullptr;

Expand Down Expand Up @@ -215,7 +248,7 @@ void parse_argv(const std::vector<std::string> &argv) {
}
}

DEBUG("settings after argument parsing: {}", settings);
DEBUG("settings after argument parsing: {}", *settings);
}

} // namespace rtpmididns
28 changes: 28 additions & 0 deletions src/argv.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* Real Time Protocol Music Instrument Digital Interface Daemon
* Copyright (C) 2019-2024 David Moreno Montero <[email protected]>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

#include <string>
#include <vector>

namespace rtpmididns {

class settings_t;

// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
void parse_argv(const std::vector<std::string> &argv, settings_t *settings);
} // namespace rtpmididns
10 changes: 8 additions & 2 deletions src/hwautoannounce.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,14 @@ HwAutoAnnounce::HwAutoAnnounce(std::shared_ptr<aseq_t> aseq,
// {}:{}
// ",
// device_name, port_name, device_id, port_id);
added_port_announcement(device_name, type,
aseq_t::port_t{device_id, port_id});
try {
added_port_announcement(device_name, type,
aseq_t::port_t{device_id, port_id});

} catch (const std::exception &e) {
ERROR("Error adding port announcement {}:{} {}:{} exception={}",
device_name, port_name, device_id, port_id, e.what());
}
});
});

Expand Down
Loading

0 comments on commit 93d920f

Please sign in to comment.