├── LICENSE ├── Makefile ├── amidiauto.cpp ├── amidiauto.service └── debian ├── DEBIAN ├── control ├── postinst ├── postrm └── prerm └── usr ├── lib └── systemd │ └── system │ └── amidiauto.service └── share ├── doc └── amidiauto │ ├── changelog │ ├── changelog.Debian │ └── copyright └── man └── man1 └── amidiauto.1 /LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 2, June 1991 3 | 4 | Copyright (C) 1989, 1991 Free Software Foundation, Inc., 5 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 6 | Everyone is permitted to copy and distribute verbatim copies 7 | of this license document, but changing it is not allowed. 8 | 9 | Preamble 10 | 11 | The licenses for most software are designed to take away your 12 | freedom to share and change it. By contrast, the GNU General Public 13 | License is intended to guarantee your freedom to share and change free 14 | software--to make sure the software is free for all its users. This 15 | General Public License applies to most of the Free Software 16 | Foundation's software and to any other program whose authors commit to 17 | using it. (Some other Free Software Foundation software is covered by 18 | the GNU Lesser General Public License instead.) You can apply it to 19 | your programs, too. 20 | 21 | When we speak of free software, we are referring to freedom, not 22 | price. Our General Public Licenses are designed to make sure that you 23 | have the freedom to distribute copies of free software (and charge for 24 | this service if you wish), that you receive source code or can get it 25 | if you want it, that you can change the software or use pieces of it 26 | in new free programs; and that you know you can do these things. 27 | 28 | To protect your rights, we need to make restrictions that forbid 29 | anyone to deny you these rights or to ask you to surrender the rights. 30 | These restrictions translate to certain responsibilities for you if you 31 | distribute copies of the software, or if you modify it. 32 | 33 | For example, if you distribute copies of such a program, whether 34 | gratis or for a fee, you must give the recipients all the rights that 35 | you have. You must make sure that they, too, receive or can get the 36 | source code. And you must show them these terms so they know their 37 | rights. 38 | 39 | We protect your rights with two steps: (1) copyright the software, and 40 | (2) offer you this license which gives you legal permission to copy, 41 | distribute and/or modify the software. 42 | 43 | Also, for each author's protection and ours, we want to make certain 44 | that everyone understands that there is no warranty for this free 45 | software. If the software is modified by someone else and passed on, we 46 | want its recipients to know that what they have is not the original, so 47 | that any problems introduced by others will not reflect on the original 48 | authors' reputations. 49 | 50 | Finally, any free program is threatened constantly by software 51 | patents. We wish to avoid the danger that redistributors of a free 52 | program will individually obtain patent licenses, in effect making the 53 | program proprietary. To prevent this, we have made it clear that any 54 | patent must be licensed for everyone's free use or not licensed at all. 55 | 56 | The precise terms and conditions for copying, distribution and 57 | modification follow. 58 | 59 | GNU GENERAL PUBLIC LICENSE 60 | TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 61 | 62 | 0. This License applies to any program or other work which contains 63 | a notice placed by the copyright holder saying it may be distributed 64 | under the terms of this General Public License. The "Program", below, 65 | refers to any such program or work, and a "work based on the Program" 66 | means either the Program or any derivative work under copyright law: 67 | that is to say, a work containing the Program or a portion of it, 68 | either verbatim or with modifications and/or translated into another 69 | language. (Hereinafter, translation is included without limitation in 70 | the term "modification".) Each licensee is addressed as "you". 71 | 72 | Activities other than copying, distribution and modification are not 73 | covered by this License; they are outside its scope. The act of 74 | running the Program is not restricted, and the output from the Program 75 | is covered only if its contents constitute a work based on the 76 | Program (independent of having been made by running the Program). 77 | Whether that is true depends on what the Program does. 78 | 79 | 1. You may copy and distribute verbatim copies of the Program's 80 | source code as you receive it, in any medium, provided that you 81 | conspicuously and appropriately publish on each copy an appropriate 82 | copyright notice and disclaimer of warranty; keep intact all the 83 | notices that refer to this License and to the absence of any warranty; 84 | and give any other recipients of the Program a copy of this License 85 | along with the Program. 86 | 87 | You may charge a fee for the physical act of transferring a copy, and 88 | you may at your option offer warranty protection in exchange for a fee. 89 | 90 | 2. You may modify your copy or copies of the Program or any portion 91 | of it, thus forming a work based on the Program, and copy and 92 | distribute such modifications or work under the terms of Section 1 93 | above, provided that you also meet all of these conditions: 94 | 95 | a) You must cause the modified files to carry prominent notices 96 | stating that you changed the files and the date of any change. 97 | 98 | b) You must cause any work that you distribute or publish, that in 99 | whole or in part contains or is derived from the Program or any 100 | part thereof, to be licensed as a whole at no charge to all third 101 | parties under the terms of this License. 102 | 103 | c) If the modified program normally reads commands interactively 104 | when run, you must cause it, when started running for such 105 | interactive use in the most ordinary way, to print or display an 106 | announcement including an appropriate copyright notice and a 107 | notice that there is no warranty (or else, saying that you provide 108 | a warranty) and that users may redistribute the program under 109 | these conditions, and telling the user how to view a copy of this 110 | License. (Exception: if the Program itself is interactive but 111 | does not normally print such an announcement, your work based on 112 | the Program is not required to print an announcement.) 113 | 114 | These requirements apply to the modified work as a whole. If 115 | identifiable sections of that work are not derived from the Program, 116 | and can be reasonably considered independent and separate works in 117 | themselves, then this License, and its terms, do not apply to those 118 | sections when you distribute them as separate works. But when you 119 | distribute the same sections as part of a whole which is a work based 120 | on the Program, the distribution of the whole must be on the terms of 121 | this License, whose permissions for other licensees extend to the 122 | entire whole, and thus to each and every part regardless of who wrote it. 123 | 124 | Thus, it is not the intent of this section to claim rights or contest 125 | your rights to work written entirely by you; rather, the intent is to 126 | exercise the right to control the distribution of derivative or 127 | collective works based on the Program. 128 | 129 | In addition, mere aggregation of another work not based on the Program 130 | with the Program (or with a work based on the Program) on a volume of 131 | a storage or distribution medium does not bring the other work under 132 | the scope of this License. 133 | 134 | 3. You may copy and distribute the Program (or a work based on it, 135 | under Section 2) in object code or executable form under the terms of 136 | Sections 1 and 2 above provided that you also do one of the following: 137 | 138 | a) Accompany it with the complete corresponding machine-readable 139 | source code, which must be distributed under the terms of Sections 140 | 1 and 2 above on a medium customarily used for software interchange; or, 141 | 142 | b) Accompany it with a written offer, valid for at least three 143 | years, to give any third party, for a charge no more than your 144 | cost of physically performing source distribution, a complete 145 | machine-readable copy of the corresponding source code, to be 146 | distributed under the terms of Sections 1 and 2 above on a medium 147 | customarily used for software interchange; or, 148 | 149 | c) Accompany it with the information you received as to the offer 150 | to distribute corresponding source code. (This alternative is 151 | allowed only for noncommercial distribution and only if you 152 | received the program in object code or executable form with such 153 | an offer, in accord with Subsection b above.) 154 | 155 | The source code for a work means the preferred form of the work for 156 | making modifications to it. For an executable work, complete source 157 | code means all the source code for all modules it contains, plus any 158 | associated interface definition files, plus the scripts used to 159 | control compilation and installation of the executable. However, as a 160 | special exception, the source code distributed need not include 161 | anything that is normally distributed (in either source or binary 162 | form) with the major components (compiler, kernel, and so on) of the 163 | operating system on which the executable runs, unless that component 164 | itself accompanies the executable. 165 | 166 | If distribution of executable or object code is made by offering 167 | access to copy from a designated place, then offering equivalent 168 | access to copy the source code from the same place counts as 169 | distribution of the source code, even though third parties are not 170 | compelled to copy the source along with the object code. 171 | 172 | 4. You may not copy, modify, sublicense, or distribute the Program 173 | except as expressly provided under this License. Any attempt 174 | otherwise to copy, modify, sublicense or distribute the Program is 175 | void, and will automatically terminate your rights under this License. 176 | However, parties who have received copies, or rights, from you under 177 | this License will not have their licenses terminated so long as such 178 | parties remain in full compliance. 179 | 180 | 5. You are not required to accept this License, since you have not 181 | signed it. However, nothing else grants you permission to modify or 182 | distribute the Program or its derivative works. These actions are 183 | prohibited by law if you do not accept this License. Therefore, by 184 | modifying or distributing the Program (or any work based on the 185 | Program), you indicate your acceptance of this License to do so, and 186 | all its terms and conditions for copying, distributing or modifying 187 | the Program or works based on it. 188 | 189 | 6. Each time you redistribute the Program (or any work based on the 190 | Program), the recipient automatically receives a license from the 191 | original licensor to copy, distribute or modify the Program subject to 192 | these terms and conditions. You may not impose any further 193 | restrictions on the recipients' exercise of the rights granted herein. 194 | You are not responsible for enforcing compliance by third parties to 195 | this License. 196 | 197 | 7. If, as a consequence of a court judgment or allegation of patent 198 | infringement or for any other reason (not limited to patent issues), 199 | conditions are imposed on you (whether by court order, agreement or 200 | otherwise) that contradict the conditions of this License, they do not 201 | excuse you from the conditions of this License. If you cannot 202 | distribute so as to satisfy simultaneously your obligations under this 203 | License and any other pertinent obligations, then as a consequence you 204 | may not distribute the Program at all. For example, if a patent 205 | license would not permit royalty-free redistribution of the Program by 206 | all those who receive copies directly or indirectly through you, then 207 | the only way you could satisfy both it and this License would be to 208 | refrain entirely from distribution of the Program. 209 | 210 | If any portion of this section is held invalid or unenforceable under 211 | any particular circumstance, the balance of the section is intended to 212 | apply and the section as a whole is intended to apply in other 213 | circumstances. 214 | 215 | It is not the purpose of this section to induce you to infringe any 216 | patents or other property right claims or to contest validity of any 217 | such claims; this section has the sole purpose of protecting the 218 | integrity of the free software distribution system, which is 219 | implemented by public license practices. Many people have made 220 | generous contributions to the wide range of software distributed 221 | through that system in reliance on consistent application of that 222 | system; it is up to the author/donor to decide if he or she is willing 223 | to distribute software through any other system and a licensee cannot 224 | impose that choice. 225 | 226 | This section is intended to make thoroughly clear what is believed to 227 | be a consequence of the rest of this License. 228 | 229 | 8. If the distribution and/or use of the Program is restricted in 230 | certain countries either by patents or by copyrighted interfaces, the 231 | original copyright holder who places the Program under this License 232 | may add an explicit geographical distribution limitation excluding 233 | those countries, so that distribution is permitted only in or among 234 | countries not thus excluded. In such case, this License incorporates 235 | the limitation as if written in the body of this License. 236 | 237 | 9. The Free Software Foundation may publish revised and/or new versions 238 | of the General Public License from time to time. Such new versions will 239 | be similar in spirit to the present version, but may differ in detail to 240 | address new problems or concerns. 241 | 242 | Each version is given a distinguishing version number. If the Program 243 | specifies a version number of this License which applies to it and "any 244 | later version", you have the option of following the terms and conditions 245 | either of that version or of any later version published by the Free 246 | Software Foundation. If the Program does not specify a version number of 247 | this License, you may choose any version ever published by the Free Software 248 | Foundation. 249 | 250 | 10. If you wish to incorporate parts of the Program into other free 251 | programs whose distribution conditions are different, write to the author 252 | to ask for permission. For software which is copyrighted by the Free 253 | Software Foundation, write to the Free Software Foundation; we sometimes 254 | make exceptions for this. Our decision will be guided by the two goals 255 | of preserving the free status of all derivatives of our free software and 256 | of promoting the sharing and reuse of software generally. 257 | 258 | NO WARRANTY 259 | 260 | 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY 261 | FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN 262 | OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES 263 | PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED 264 | OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 265 | MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS 266 | TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE 267 | PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, 268 | REPAIR OR CORRECTION. 269 | 270 | 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 271 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR 272 | REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, 273 | INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING 274 | OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED 275 | TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY 276 | YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER 277 | PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE 278 | POSSIBILITY OF SUCH DAMAGES. 279 | 280 | END OF TERMS AND CONDITIONS 281 | 282 | How to Apply These Terms to Your New Programs 283 | 284 | If you develop a new program, and you want it to be of the greatest 285 | possible use to the public, the best way to achieve this is to make it 286 | free software which everyone can redistribute and change under these terms. 287 | 288 | To do so, attach the following notices to the program. It is safest 289 | to attach them to the start of each source file to most effectively 290 | convey the exclusion of warranty; and each file should have at least 291 | the "copyright" line and a pointer to where the full notice is found. 292 | 293 | {description} 294 | Copyright (C) {year} {fullname} 295 | 296 | This program is free software; you can redistribute it and/or modify 297 | it under the terms of the GNU General Public License as published by 298 | the Free Software Foundation; either version 2 of the License, or 299 | (at your option) any later version. 300 | 301 | This program is distributed in the hope that it will be useful, 302 | but WITHOUT ANY WARRANTY; without even the implied warranty of 303 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 304 | GNU General Public License for more details. 305 | 306 | You should have received a copy of the GNU General Public License along 307 | with this program; if not, write to the Free Software Foundation, Inc., 308 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 309 | 310 | Also add information on how to contact you by electronic and paper mail. 311 | 312 | If the program is interactive, make it output a short notice like this 313 | when it starts in an interactive mode: 314 | 315 | Gnomovision version 69, Copyright (C) year name of author 316 | Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 317 | This is free software, and you are welcome to redistribute it 318 | under certain conditions; type `show c' for details. 319 | 320 | The hypothetical commands `show w' and `show c' should show the appropriate 321 | parts of the General Public License. Of course, the commands you use may 322 | be called something other than `show w' and `show c'; they could even be 323 | mouse-clicks or menu items--whatever suits your program. 324 | 325 | You should also get your employer (if you work as a programmer) or your 326 | school, if any, to sign a "copyright disclaimer" for the program, if 327 | necessary. Here is a sample; alter the names: 328 | 329 | Yoyodyne, Inc., hereby disclaims all copyright interest in the program 330 | `Gnomovision' (which makes passes at compilers) written by James Hacker. 331 | 332 | {signature of Ty Coon}, 1 April 1989 333 | Ty Coon, President of Vice 334 | 335 | This General Public License does not permit incorporating your program into 336 | proprietary programs. If your program is a subroutine library, you may 337 | consider it more useful to permit linking proprietary applications with the 338 | library. If this is what you want to do, use the GNU Lesser General 339 | Public License instead of this License. 340 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | # amidiauto - ALSA MIDI autoconnect daemon. 2 | # Copyright (C) 2019 Vilniaus Blokas UAB, https://blokas.io/ 3 | # 4 | # This program is free software; you can redistribute it and/or 5 | # modify it under the terms of the GNU General Public License 6 | # as published by the Free Software Foundation; version 2 of the 7 | # License. 8 | # 9 | # This program is distributed in the hope that it will be useful, 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | # GNU General Public License for more details. 13 | # 14 | # You should have received a copy of the GNU General Public License 15 | # along with this program; if not, write to the Free Software 16 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 17 | # 18 | 19 | BINARY_DIR ?= /usr/local/bin 20 | 21 | all: amidiauto 22 | 23 | CXXFLAGS ?= -O3 24 | LDFLAGS ?= -lasound 25 | 26 | CXX?=g++-4.9 27 | 28 | amidiauto: amidiauto.o 29 | $(CXX) $^ -o $@ $(LDFLAGS) 30 | strip $@ 31 | 32 | %.o: %.cpp 33 | $(CXX) -c $(CXXFLAGS) $^ -o $@ 34 | 35 | install: all 36 | @systemctl stop amidiauto > /dev/null 2>&1 || true 37 | @cp -p amidiauto $(BINARY_DIR)/ 38 | @cp -p amidiauto.service /usr/lib/systemd/system/ 39 | @systemctl daemon-reload > /dev/null 2>&1 40 | @systemctl enable amidiauto > /dev/null 2>&1 41 | @systemctl start amidiauto > /dev/null 2>&1 42 | 43 | clean: 44 | rm -f amidiauto *.o 45 | rm -f amidiauto.deb 46 | rm -f debian/usr/bin/amidiauto 47 | gunzip `find . | grep gz` > /dev/null 2>&1 || true 48 | 49 | amidiauto.deb: amidiauto 50 | @gzip --best -n ./debian/usr/share/doc/amidiauto/changelog ./debian/usr/share/doc/amidiauto/changelog.Debian ./debian/usr/share/man/man1/amidiauto.1 51 | @mkdir -p debian/usr/bin 52 | @cp -p amidiauto debian/usr/bin/ 53 | @fakeroot dpkg --build debian 54 | @mv debian.deb amidiauto.deb 55 | @gunzip `find . | grep gz` > /dev/null 2>&1 56 | -------------------------------------------------------------------------------- /amidiauto.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * amidiauto - ALSA MIDI autoconnect daemon. 3 | * Copyright (C) 2019 Vilniaus Blokas UAB, https://blokas.io/ 4 | * 5 | * This program is free software; you can redistribute it and/or 6 | * modify it under the terms of the GNU General Public License 7 | * as published by the Free Software Foundation; version 2 of the 8 | * License. 9 | * 10 | * This program is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU General Public License 16 | * along with this program; if not, write to the Free Software 17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 18 | */ 19 | 20 | #include 21 | #include 22 | #include 23 | #include 24 | #include 25 | #include 26 | #include 27 | 28 | #include 29 | #include 30 | #include 31 | 32 | #define HOMEPAGE_URL "https://blokas.io/" 33 | #define AMIDIAUTO_VERSION 0x0101 34 | 35 | static snd_seq_t *g_seq = NULL; 36 | static int g_port = -1; 37 | 38 | enum PortDir 39 | { 40 | DIR_UNKNOWN = 0, 41 | DIR_INPUT = 1 << 0, 42 | DIR_OUTPUT = 1 << 1, 43 | DIR_DUPLEX = DIR_INPUT | DIR_OUTPUT 44 | }; 45 | 46 | enum PortType 47 | { 48 | TYPE_SOFTWARE = 0, 49 | TYPE_HARDWARE = 1, 50 | }; 51 | 52 | inline static bool operator <(const snd_seq_addr_t &a, const snd_seq_addr_t &b) 53 | { 54 | return std::make_pair(a.client, a.port) < std::make_pair(b.client, b.port); 55 | } 56 | 57 | inline static bool operator ==(const snd_seq_addr_t &a, const snd_seq_addr_t &b) 58 | { 59 | return a.client == b.client && a.port == b.port; 60 | } 61 | 62 | static std::string getClientName(snd_seq_addr_t addr) 63 | { 64 | snd_seq_client_info_t *clientInfo; 65 | snd_seq_client_info_alloca(&clientInfo); 66 | if (snd_seq_get_any_client_info(g_seq, addr.client, clientInfo) < 0) 67 | return ""; 68 | 69 | const char *name = snd_seq_client_info_get_name(clientInfo); 70 | 71 | return name ? name : ""; 72 | } 73 | 74 | class ConnectionRules 75 | { 76 | public: 77 | enum Type 78 | { 79 | TYPE_UNKNOWN = -1, 80 | TYPE_ALLOW = 0, 81 | TYPE_DISALLOW = 1, 82 | }; 83 | 84 | enum Strength 85 | { 86 | STRENGTH_NONE = 0, // Rule does not apply. 87 | STRENGTH_VERY_VAGUE = 1, // Wildcard matched wildcard. 88 | STRENGTH_VAGUE = 2, // One of the sides is a specific name, the other is a wildcard. 89 | STRENGTH_SPECIFIC = 3, // Both sides is a specific name. 90 | }; 91 | 92 | void addRule(Type type, const char *output, const char *input); 93 | 94 | bool hasRules() const; 95 | 96 | bool isConnectionAllowed(snd_seq_addr_t output, snd_seq_addr_t input, Strength minimumStrength) const; 97 | 98 | private: 99 | typedef std::pair rule_t; 100 | typedef std::multimap rules_t; 101 | 102 | Strength evaluate(const rules_t &rules, snd_seq_addr_t output, snd_seq_addr_t input) const; 103 | static Strength evaluate(const rule_t &rule, const std::string &outputName, const std::string &inputName); 104 | 105 | rules_t m_allowRules; 106 | rules_t m_disallowRules; 107 | }; 108 | 109 | void ConnectionRules::addRule(Type type, const char * output, const char * input) 110 | { 111 | if (!output || !input || type == TYPE_UNKNOWN) 112 | return; 113 | 114 | if (strchr(output, '*') != NULL && strlen(output) > 1) 115 | return; 116 | 117 | if (strchr(input, '*') != NULL && strlen(input) > 1) 118 | return; 119 | 120 | rules_t &rules = type == TYPE_ALLOW ? m_allowRules : m_disallowRules; 121 | 122 | fprintf(stderr, "%s '%s' -> '%s'\n", type == TYPE_ALLOW ? "Allowing" : "Disallowing", output, input); 123 | 124 | rules.insert(std::make_pair(output, input)); 125 | } 126 | 127 | bool ConnectionRules::hasRules() const 128 | { 129 | return !m_allowRules.empty() || !m_disallowRules.empty(); 130 | } 131 | 132 | bool ConnectionRules::isConnectionAllowed(snd_seq_addr_t output, snd_seq_addr_t input, Strength minimumStrength) const 133 | { 134 | Strength allowStrength = evaluate(m_allowRules, output, input); 135 | Strength disallowStrength = evaluate(m_disallowRules, output, input); 136 | 137 | return allowStrength >= minimumStrength && allowStrength >= disallowStrength; 138 | } 139 | 140 | ConnectionRules::Strength ConnectionRules::evaluate(const rules_t &rules, snd_seq_addr_t output, snd_seq_addr_t input) const 141 | { 142 | std::string outputName = getClientName(output); 143 | std::string inputName = getClientName(input); 144 | 145 | Strength strength = STRENGTH_NONE; 146 | 147 | for (rules_t::const_iterator itr = rules.begin(); itr != rules.end(); ++itr) 148 | { 149 | Strength s = evaluate(*itr, outputName, inputName); 150 | if (strength < s) 151 | strength = s; 152 | } 153 | 154 | return strength; 155 | } 156 | 157 | ConnectionRules::Strength ConnectionRules::evaluate(const rule_t & rule, const std::string &outputName, const std::string &inputName) 158 | { 159 | if (rule.first == "*") 160 | { 161 | if (rule.second == "*") 162 | { 163 | return STRENGTH_VERY_VAGUE; 164 | } 165 | else if (inputName.find(rule.second) != std::string::npos) 166 | { 167 | return STRENGTH_VAGUE; 168 | } 169 | } 170 | else if (outputName.find(rule.first) != std::string::npos) 171 | { 172 | if (rule.second == "*") 173 | { 174 | return STRENGTH_VAGUE; 175 | } 176 | else if (inputName.find(rule.second) != std::string::npos) 177 | { 178 | return STRENGTH_SPECIFIC; 179 | } 180 | } 181 | 182 | return STRENGTH_NONE; 183 | } 184 | 185 | static ConnectionRules g_rules; 186 | 187 | // Keeps track of one input and one output port. 188 | // This is to try and keep things simple and app performance under control. 189 | // For example, some software may create many input ports, all for the same function, 190 | // if a MIDI note gets sent to all of them, that will cause many duplicate notes 191 | // to be played. 192 | class Client 193 | { 194 | public: 195 | Client(); 196 | Client(int clientId); 197 | 198 | void setInput(snd_seq_addr_t addr); 199 | void setOutput(snd_seq_addr_t addr); 200 | 201 | bool isInputSet() const; 202 | bool isOutputSet() const; 203 | 204 | void clearInput(); 205 | void clearOutput(); 206 | 207 | const snd_seq_addr_t *getInput() const; 208 | const snd_seq_addr_t *getOutput() const; 209 | 210 | bool operator <(const Client &rhs) const; 211 | 212 | private: 213 | int m_clientId; 214 | 215 | bool m_inputInitialized; 216 | bool m_outputInitialized; 217 | 218 | snd_seq_addr_t m_input; 219 | snd_seq_addr_t m_output; 220 | }; 221 | 222 | Client::Client() 223 | :m_clientId(-1) 224 | ,m_inputInitialized(false) 225 | ,m_outputInitialized(false) 226 | { 227 | } 228 | 229 | Client::Client(int clientId) 230 | :m_clientId(clientId) 231 | ,m_inputInitialized(false) 232 | ,m_outputInitialized(false) 233 | { 234 | } 235 | 236 | void Client::setInput(snd_seq_addr_t addr) 237 | { 238 | m_inputInitialized = true; 239 | m_input = addr; 240 | } 241 | 242 | void Client::setOutput(snd_seq_addr_t addr) 243 | { 244 | m_outputInitialized = true; 245 | m_output = addr; 246 | } 247 | 248 | bool Client::isInputSet() const 249 | { 250 | return m_inputInitialized; 251 | } 252 | 253 | bool Client::isOutputSet() const 254 | { 255 | return m_outputInitialized; 256 | } 257 | 258 | void Client::clearInput() 259 | { 260 | m_inputInitialized = false; 261 | } 262 | 263 | void Client::clearOutput() 264 | { 265 | m_outputInitialized = false; 266 | } 267 | 268 | const snd_seq_addr_t * Client::getInput() const 269 | { 270 | return m_inputInitialized ? &m_input : NULL; 271 | } 272 | 273 | const snd_seq_addr_t * Client::getOutput() const 274 | { 275 | return m_outputInitialized ? &m_output : NULL; 276 | } 277 | 278 | bool Client::operator <(const Client &rhs) const 279 | { 280 | return m_clientId < rhs.m_clientId; 281 | } 282 | 283 | typedef std::map clients_t; 284 | 285 | enum ClientType 286 | { 287 | CLIENT_SOFTWARE, 288 | CLIENT_HARDWARE 289 | }; 290 | 291 | static clients_t g_swClients; 292 | static clients_t g_hwClients; 293 | 294 | static Client &getClient(clients_t &list, int clientId) 295 | { 296 | clients_t::iterator item = list.find(clientId); 297 | if (item != list.end()) 298 | { 299 | return item->second; 300 | } 301 | else 302 | { 303 | return list.insert(std::make_pair(clientId, Client(clientId))).first->second; 304 | } 305 | } 306 | 307 | Client *findClientForPort(snd_seq_addr_t addr, ClientType *type = NULL) 308 | { 309 | int clientId = addr.client; 310 | 311 | clients_t::iterator item = g_swClients.find(clientId); 312 | if (item == g_swClients.end()) 313 | { 314 | item = g_hwClients.find(clientId); 315 | if (item == g_hwClients.end()) 316 | { 317 | return NULL; 318 | } 319 | else 320 | { 321 | if (type) *type = CLIENT_HARDWARE; 322 | } 323 | } 324 | else 325 | { 326 | if (type) *type = CLIENT_SOFTWARE; 327 | } 328 | 329 | return &item->second; 330 | } 331 | 332 | static void connect(snd_seq_addr_t output, snd_seq_addr_t input) 333 | { 334 | printf("Connecting %d:%d to %d:%d\n", output.client, output.port, input.client, input.port); 335 | 336 | snd_seq_port_subscribe_t *subs; 337 | snd_seq_port_subscribe_alloca(&subs); 338 | snd_seq_port_subscribe_set_sender(subs, &output); 339 | snd_seq_port_subscribe_set_dest(subs, &input); 340 | snd_seq_subscribe_port(g_seq, subs); 341 | } 342 | 343 | static PortDir portGetDir(const snd_seq_port_info_t &portInfo) 344 | { 345 | unsigned result = 0; 346 | 347 | unsigned int caps = snd_seq_port_info_get_capability(&portInfo); 348 | 349 | if (caps & SND_SEQ_PORT_CAP_READ) 350 | { 351 | result |= DIR_OUTPUT; 352 | } 353 | if (caps & SND_SEQ_PORT_CAP_WRITE) 354 | { 355 | result |= DIR_INPUT; 356 | } 357 | 358 | return (PortDir)result; 359 | } 360 | 361 | static PortType portGetType(const snd_seq_port_info_t &portInfo) 362 | { 363 | const char *name = snd_seq_port_info_get_name(&portInfo); 364 | 365 | if (strncmp("TouchOSC Bridge", name, 15) == 0) 366 | { 367 | // Treat touchosc2midi ports as 'hardware' ones. 368 | return TYPE_HARDWARE; 369 | } 370 | 371 | unsigned int type = snd_seq_port_info_get_type(&portInfo); 372 | 373 | return (type & SND_SEQ_PORT_TYPE_APPLICATION) ? TYPE_SOFTWARE : TYPE_HARDWARE; 374 | } 375 | 376 | static bool portAdd(const snd_seq_port_info_t &portInfo) 377 | { 378 | unsigned int caps = snd_seq_port_info_get_capability(&portInfo); 379 | 380 | if (caps & SND_SEQ_PORT_CAP_NO_EXPORT) 381 | return false; 382 | 383 | // Ignore System client and its Announce and Timer ports. 384 | int clientId = snd_seq_port_info_get_client(&portInfo); 385 | if (clientId == SND_SEQ_CLIENT_SYSTEM) 386 | return false; 387 | 388 | snd_seq_client_info_t *clientInfo; 389 | snd_seq_client_info_alloca(&clientInfo); 390 | if (snd_seq_get_any_client_info(g_seq, clientId, clientInfo) < 0) 391 | return false; 392 | 393 | // Ignore through ports. 394 | const char *name = snd_seq_client_info_get_name(clientInfo); 395 | if (!name || strncmp(name, "Midi Through", 12) == 0) 396 | return false; 397 | 398 | PortType type = portGetType(portInfo); 399 | clients_t &list = (type == TYPE_SOFTWARE) ? g_swClients : g_hwClients; 400 | 401 | PortDir dir = portGetDir(portInfo); 402 | 403 | bool gotAdded = false; 404 | 405 | snd_seq_addr_t addr = *snd_seq_port_info_get_addr(&portInfo); 406 | 407 | Client &client = getClient(list, addr.client); 408 | if (dir & DIR_OUTPUT) 409 | { 410 | if (!client.isOutputSet()) 411 | { 412 | client.setOutput(addr); 413 | gotAdded = true; 414 | } 415 | } 416 | if (dir & DIR_INPUT) 417 | { 418 | if (!client.isInputSet()) 419 | { 420 | client.setInput(addr); 421 | gotAdded = true; 422 | } 423 | } 424 | 425 | return gotAdded; 426 | } 427 | 428 | static void portRemove(snd_seq_addr_t addr) 429 | { 430 | Client *client = findClientForPort(addr); 431 | 432 | if (!client) 433 | return; 434 | 435 | if (client->isInputSet() && *client->getInput() == addr) 436 | { 437 | client->clearInput(); 438 | } 439 | if (client->isOutputSet() && *client->getOutput() == addr) 440 | { 441 | client->clearOutput(); 442 | } 443 | } 444 | 445 | static void portAutoConnect(snd_seq_addr_t addr, PortDir dir) 446 | { 447 | ClientType type; 448 | Client *client = findClientForPort(addr, &type); 449 | assert(client); 450 | 451 | if (!client) 452 | return; 453 | 454 | clients_t *lists[2] = { &g_swClients, &g_hwClients }; 455 | ConnectionRules::Strength strengths[2] = 456 | { 457 | type == CLIENT_SOFTWARE ? ConnectionRules::STRENGTH_SPECIFIC : ConnectionRules::STRENGTH_VERY_VAGUE, 458 | type == CLIENT_SOFTWARE ? ConnectionRules::STRENGTH_VERY_VAGUE : ConnectionRules::STRENGTH_SPECIFIC 459 | }; 460 | 461 | for (int i=0; i<2; ++i) 462 | { 463 | clients_t &list = *lists[i]; 464 | ConnectionRules::Strength strength = strengths[i]; 465 | 466 | for (clients_t::iterator itr = list.begin(); itr != list.end(); ++itr) 467 | { 468 | Client &client = itr->second; 469 | 470 | const snd_seq_addr_t *input = client.getInput(); 471 | const snd_seq_addr_t *output = client.getOutput(); 472 | 473 | if ((dir & DIR_INPUT) && output) 474 | { 475 | if (g_rules.isConnectionAllowed(*output, addr, strength)) 476 | connect(*output, addr); 477 | } 478 | if (dir & DIR_OUTPUT && input) 479 | { 480 | if (g_rules.isConnectionAllowed(addr, *input, strength)) 481 | connect(addr, *input); 482 | } 483 | } 484 | } 485 | } 486 | 487 | static void portsConnectAll(const clients_t &listA, const clients_t &listB, ConnectionRules::Strength minimumStrength) 488 | { 489 | std::set > handled; 490 | 491 | for (clients_t::const_iterator listAItr = listA.begin(); listAItr != listA.end(); ++listAItr) 492 | { 493 | const Client &aClient = listAItr->second; 494 | 495 | const snd_seq_addr_t *aInput = aClient.getInput(); 496 | const snd_seq_addr_t *aOutput = aClient.getOutput(); 497 | 498 | if (!aInput && !aOutput) 499 | continue; 500 | 501 | for (clients_t::const_iterator listBItr = listB.begin(); listBItr != listB.end(); ++listBItr) 502 | { 503 | const Client &bClient = listBItr->second; 504 | 505 | const snd_seq_addr_t *bInput = bClient.getInput(); 506 | const snd_seq_addr_t *bOutput = bClient.getOutput(); 507 | 508 | if (bInput && aOutput && handled.find(std::make_pair(*aOutput, *bInput)) == handled.end()) 509 | { 510 | if (g_rules.isConnectionAllowed(*aOutput, *bInput, minimumStrength)) 511 | { 512 | connect(*aOutput, *bInput); 513 | handled.insert(std::make_pair(*aOutput, *bInput)); 514 | } 515 | } 516 | if (bOutput && aInput && handled.find(std::make_pair(*bOutput, *aInput)) == handled.end()) 517 | { 518 | if (g_rules.isConnectionAllowed(*bOutput, *aInput, minimumStrength)) 519 | { 520 | connect(*bOutput, *aInput); 521 | handled.insert(std::make_pair(*bOutput, *aInput)); 522 | } 523 | } 524 | } 525 | } 526 | } 527 | 528 | static int portsInit() 529 | { 530 | snd_seq_client_info_t *clientInfo; 531 | snd_seq_port_info_t *portInfo; 532 | 533 | snd_seq_client_info_alloca(&clientInfo); 534 | snd_seq_port_info_alloca(&portInfo); 535 | snd_seq_client_info_set_client(clientInfo, -1); 536 | while (snd_seq_query_next_client(g_seq, clientInfo) >= 0) 537 | { 538 | int clientId = snd_seq_client_info_get_client(clientInfo); 539 | 540 | snd_seq_port_info_set_client(portInfo, clientId); 541 | snd_seq_port_info_set_port(portInfo, -1); 542 | 543 | while (snd_seq_query_next_port(g_seq, portInfo) >= 0) 544 | { 545 | portAdd(*portInfo); 546 | } 547 | } 548 | 549 | // Initially connect everything together. 550 | portsConnectAll(g_hwClients, g_swClients, ConnectionRules::STRENGTH_VERY_VAGUE); 551 | portsConnectAll(g_hwClients, g_hwClients, ConnectionRules::STRENGTH_SPECIFIC); 552 | portsConnectAll(g_swClients, g_swClients, ConnectionRules::STRENGTH_SPECIFIC); 553 | } 554 | 555 | static void seqUninit() 556 | { 557 | if (g_port >= 0) 558 | { 559 | snd_seq_delete_simple_port(g_seq, g_port); 560 | g_port = -1; 561 | } 562 | if (g_seq) 563 | { 564 | snd_seq_close(g_seq); 565 | g_seq = NULL; 566 | } 567 | } 568 | 569 | static int seqInit() 570 | { 571 | if (g_seq != NULL) 572 | { 573 | fprintf(stderr, "Already initialized!\n"); 574 | return -EINVAL; 575 | } 576 | 577 | int result = snd_seq_open(&g_seq, "default", SND_SEQ_OPEN_DUPLEX, 0); 578 | if (result < 0) 579 | { 580 | fprintf(stderr, "Couldn't open ALSA sequencer! (%d)\n", result); 581 | goto error; 582 | } 583 | 584 | result = snd_seq_set_client_name(g_seq, "amidiauto"); 585 | if (result < 0) 586 | { 587 | fprintf(stderr, "Failed setting client name! (%d)\n", result); 588 | goto error; 589 | } 590 | 591 | result = snd_seq_create_simple_port( 592 | g_seq, 593 | "amidiauto", 594 | SND_SEQ_PORT_CAP_WRITE | 595 | SND_SEQ_PORT_CAP_NO_EXPORT, 596 | SND_SEQ_PORT_TYPE_APPLICATION 597 | ); 598 | 599 | if (result < 0) 600 | { 601 | fprintf(stderr, "Couldn't create a virtual MIDI port! (%d)\n", result); 602 | goto error; 603 | } 604 | 605 | g_port = result; 606 | 607 | result = snd_seq_connect_from(g_seq, g_port, SND_SEQ_CLIENT_SYSTEM, SND_SEQ_PORT_SYSTEM_ANNOUNCE); 608 | 609 | if (result < 0) 610 | { 611 | fprintf(stderr, "Couldn't connect to System::Anounce port! (%d)\n", result); 612 | goto error; 613 | } 614 | 615 | return 0; 616 | 617 | error: 618 | seqUninit(); 619 | return result; 620 | } 621 | 622 | static bool handleSeqEvent(snd_seq_t *seq, int port) 623 | { 624 | do 625 | { 626 | snd_seq_event_t *ev; 627 | snd_seq_event_input(seq, &ev); 628 | 629 | switch (ev->type) 630 | { 631 | case SND_SEQ_EVENT_PORT_START: 632 | { 633 | printf("%d:%d port appeared.\n", ev->data.addr.client, ev->data.addr.port); 634 | 635 | snd_seq_port_info_t *portInfo; 636 | snd_seq_port_info_alloca(&portInfo); 637 | 638 | int result = snd_seq_get_any_port_info(g_seq, ev->data.addr.client, ev->data.addr.port, portInfo); 639 | if (result >= 0) 640 | { 641 | if (portAdd(*portInfo)) 642 | portAutoConnect(ev->data.addr, portGetDir(*portInfo)); 643 | } 644 | else 645 | { 646 | fprintf(stderr, "Failed getting port %d:%d info: %d\n", ev->data.addr.client, ev->data.addr.port, result); 647 | } 648 | } 649 | break; 650 | case SND_SEQ_EVENT_PORT_EXIT: 651 | printf("%d:%d port removed.\n", ev->data.addr.client, ev->data.addr.port); 652 | 653 | portRemove(ev->data.addr); 654 | break; 655 | default: 656 | break; 657 | } 658 | 659 | snd_seq_free_event(ev); 660 | } while (snd_seq_event_input_pending(seq, 0) > 0); 661 | 662 | return false; 663 | } 664 | 665 | static int run() 666 | { 667 | bool done = false; 668 | int npfd = 0; 669 | 670 | int result = seqInit(); 671 | 672 | if (result < 0) 673 | goto cleanup; 674 | 675 | portsInit(); 676 | 677 | npfd = snd_seq_poll_descriptors_count(g_seq, POLLIN); 678 | if (npfd != 1) 679 | { 680 | fprintf(stderr, "Unexpected count (%d) of seq fds! Expected 1!\n", npfd); 681 | result = -EINVAL; 682 | goto cleanup; 683 | } 684 | 685 | pollfd fds[1]; 686 | snd_seq_poll_descriptors(g_seq, &fds[0], 1, POLLIN); 687 | 688 | while (!done) 689 | { 690 | int n = poll(fds, npfd, -1); 691 | if (n < 0) 692 | { 693 | fprintf(stderr, "Polling failed! (%d)\n", errno); 694 | result = -errno; 695 | goto cleanup; 696 | } 697 | 698 | if (fds[0].revents) 699 | { 700 | --n; 701 | done = handleSeqEvent(g_seq, g_port); 702 | } 703 | 704 | assert(n == 0); 705 | } 706 | 707 | cleanup: 708 | seqUninit(); 709 | 710 | return result; 711 | } 712 | 713 | static void printVersion(void) 714 | { 715 | printf("Version %x.%02x, Copyright (C) Blokas Labs " HOMEPAGE_URL "\n", AMIDIAUTO_VERSION >> 8, AMIDIAUTO_VERSION & 0xff); 716 | } 717 | 718 | static void printUsage() 719 | { 720 | printf("Usage: amidiauto\n\nThat's all there is to it! :)\n\n"); 721 | printVersion(); 722 | } 723 | 724 | static char *trimWhiteSpace(char *str) 725 | { 726 | char *end = strchr(str, '\0')-1; 727 | 728 | while (end > str && isspace(*end)) 729 | --end; 730 | 731 | *(end+1) = '\0'; 732 | 733 | while (isspace(*str)) 734 | ++str; 735 | 736 | return str; 737 | } 738 | 739 | static int parseRuleFile(ConnectionRules &rules, const char *fileName) 740 | { 741 | if (!fileName) 742 | return -EINVAL; 743 | 744 | FILE *f = fopen(fileName, "rt"); 745 | if (!f) 746 | return -ENOENT; 747 | 748 | fprintf(stderr, "Reading rules in '%s'...\n", fileName); 749 | 750 | enum { MAX_LENGTH = 1024 }; 751 | char l[MAX_LENGTH]; 752 | 753 | unsigned int i=0; 754 | 755 | ConnectionRules::Type type = ConnectionRules::TYPE_UNKNOWN; 756 | 757 | while (!feof(f) && fgets(l, MAX_LENGTH, f) != NULL) 758 | { 759 | char *commentMarker = strchr(l, '#'); 760 | if (commentMarker) 761 | *commentMarker = '\0'; // Cut the string at the beginning of a comment. 762 | 763 | char *line = trimWhiteSpace(l); 764 | ++i; 765 | 766 | if (strlen(line) == 0) 767 | { 768 | continue; 769 | } 770 | else if (line[0] == '[') 771 | { 772 | if (strcmp(line+1, "allow]") == 0) 773 | { 774 | type = ConnectionRules::TYPE_ALLOW; 775 | continue; 776 | } 777 | else if (strcmp(line+1, "disallow]") == 0) 778 | { 779 | type = ConnectionRules::TYPE_DISALLOW; 780 | continue; 781 | } 782 | else 783 | { 784 | fprintf(stderr, "Unknown section on line %u!\n", i-1); 785 | type = ConnectionRules::TYPE_UNKNOWN; 786 | continue; 787 | } 788 | } 789 | 790 | if (type == ConnectionRules::TYPE_UNKNOWN) 791 | { 792 | fprintf(stderr, "Ignoring line %u which is not within [allow] or [disallow] section!\n", i-1); 793 | continue; 794 | } 795 | 796 | PortDir dir; 797 | 798 | char *specifier; 799 | 800 | if ((specifier = strstr(line, "<->")) != NULL) 801 | { 802 | dir = DIR_DUPLEX; 803 | } 804 | else if ((specifier = strstr(line, "->")) != NULL) 805 | { 806 | dir = DIR_OUTPUT; 807 | } 808 | else if ((specifier = strstr(line, "<-")) != NULL) 809 | { 810 | dir = DIR_INPUT; 811 | } 812 | else 813 | { 814 | fprintf(stderr, "Ignoring line %u, it's missing a direction specifier!\n", i); 815 | continue; 816 | } 817 | 818 | *specifier = '\0'; 819 | 820 | char *left = line; 821 | left = trimWhiteSpace(left); 822 | 823 | char *right = specifier + (dir == DIR_DUPLEX ? 3 : 2); 824 | right = trimWhiteSpace(right); 825 | 826 | if (*left == '\0' || *right == '\0') 827 | { 828 | fprintf(stderr, "Ignoring line %u, it's incomplete!\n", i); 829 | continue; 830 | } 831 | 832 | switch (dir) 833 | { 834 | case DIR_DUPLEX: 835 | rules.addRule(type, left, right); 836 | if (strcmp(left, right) != 0) 837 | rules.addRule(type, right, left); 838 | break; 839 | case DIR_INPUT: 840 | rules.addRule(type, right, left); 841 | break; 842 | case DIR_OUTPUT: 843 | rules.addRule(type, left, right); 844 | break; 845 | } 846 | } 847 | 848 | fclose(f); 849 | 850 | return 0; 851 | } 852 | 853 | int main(int argc, char **argv, char **envp) 854 | { 855 | if (argc == 2 && (strcmp(argv[1], "-v") == 0 || strcmp(argv[1], "--version") == 0)) 856 | { 857 | printVersion(); 858 | return 0; 859 | } 860 | else if (argc != 1) 861 | { 862 | printUsage(); 863 | return 0; 864 | } 865 | 866 | int result = -ENOENT; 867 | for (int i=0; envp[i] != NULL; ++i) 868 | { 869 | if (strncmp("AMIDIAUTO_CFG=", envp[i], 14) == 0) 870 | { 871 | const char *cfg = &envp[i][14]; 872 | result = parseRuleFile(g_rules, cfg); 873 | if (result < 0) 874 | { 875 | fprintf(stderr, "Failed reading rules from $AMIDIAUTO_CFG='%s' (%d)\n", cfg, result); 876 | } 877 | break; 878 | } 879 | } 880 | 881 | if (result < 0) 882 | { 883 | result = parseRuleFile(g_rules, "/etc/amidiauto.conf"); 884 | 885 | if (result < 0) 886 | { 887 | fprintf(stderr, "Reading '/etc/amidiauto.conf' failed! (%d)\n", result); 888 | } 889 | } 890 | 891 | if (!g_rules.hasRules()) 892 | { 893 | printf("Using default 'allow all' rule.\n", result); 894 | g_rules.addRule(ConnectionRules::TYPE_ALLOW, "*", "*"); 895 | } 896 | 897 | result = run(); 898 | 899 | if (result < 0) 900 | fprintf(stderr, "Error %d!\n", result); 901 | 902 | return result; 903 | } 904 | -------------------------------------------------------------------------------- /amidiauto.service: -------------------------------------------------------------------------------- 1 | [Unit] 2 | Description=ALSA MIDI autoconnect daemon 3 | After=sound.target 4 | 5 | [Service] 6 | ExecStart=/usr/local/bin/amidiauto 7 | EnvironmentFile=/etc/environment 8 | 9 | [Install] 10 | WantedBy=multi-user.target 11 | 12 | -------------------------------------------------------------------------------- /debian/DEBIAN/control: -------------------------------------------------------------------------------- 1 | Package: amidiauto 2 | Version: 1.01-2 3 | Section: sound 4 | Priority: optional 5 | Architecture: armhf 6 | Depends: libc6 (>= 2.19), libgcc1 (>= 1:4.9.2-10), libasound2 (>= 1.1) 7 | Maintainer: Blokas Labs 8 | Description: ALSA MIDI autoconnect daemon. 9 | See https://github.com/BlokasLabs/amidiauto/ for more information. 10 | -------------------------------------------------------------------------------- /debian/DEBIAN/postinst: -------------------------------------------------------------------------------- 1 | # Automatically added by dh_installinit 2 | if [ "$1" = "configure" ] || [ "$1" = "abort-upgrade" ]; then 3 | systemctl enable amidiauto 4 | systemctl start amidiauto 5 | fi 6 | # End automatically added section 7 | 8 | -------------------------------------------------------------------------------- /debian/DEBIAN/postrm: -------------------------------------------------------------------------------- 1 | # In case this system is running systemd, we make systemd reload the unit files 2 | # to pick up changes. 3 | if [ -d /run/systemd/system ] ; then 4 | systemctl --system daemon-reload >/dev/null || true 5 | fi 6 | 7 | -------------------------------------------------------------------------------- /debian/DEBIAN/prerm: -------------------------------------------------------------------------------- 1 | # Automatically added by dh_installinit 2 | systemctl stop amidiauto 3 | systemctl disable amidiauto 4 | # End automatically added section 5 | -------------------------------------------------------------------------------- /debian/usr/lib/systemd/system/amidiauto.service: -------------------------------------------------------------------------------- 1 | [Unit] 2 | Description=ALSA MIDI autoconnect daemon 3 | After=sound.target 4 | 5 | [Service] 6 | ExecStart=/usr/bin/amidiauto 7 | EnvironmentFile=/etc/environment 8 | 9 | [Install] 10 | WantedBy=multi-user.target 11 | 12 | -------------------------------------------------------------------------------- /debian/usr/share/doc/amidiauto/changelog: -------------------------------------------------------------------------------- 1 | amidiauto (1.01-1) jessie; urgency=medium 2 | 3 | * Added rule configuration via /etc/amidiauto.conf. 4 | 5 | -- Giedrius Trainavičius Thu, 4 Apr 2019 13:45:10 +0200 6 | 7 | 8 | amidiauto (1.0-1) jessie; urgency=medium 9 | 10 | * Initial .deb package release. 11 | 12 | -- Giedrius Trainavičius Tue, 12 Feb 2019 16:45:10 +0200 13 | -------------------------------------------------------------------------------- /debian/usr/share/doc/amidiauto/changelog.Debian: -------------------------------------------------------------------------------- 1 | amidiauto (1.01-1) jessie; urgency=medium 2 | 3 | * Added rule configuration via /etc/amidiauto.conf. 4 | 5 | -- Giedrius Trainavičius Thu, 4 Apr 2019 13:45:10 +0200 6 | 7 | 8 | amidiauto (1.0-1) jessie; urgency=medium 9 | 10 | * Initial .deb package release. 11 | 12 | -- Giedrius Trainavičius Tue, 12 Feb 2019 16:45:10 +0200 13 | -------------------------------------------------------------------------------- /debian/usr/share/doc/amidiauto/copyright: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 2, June 1991 3 | 4 | Copyright (C) 1989, 1991 Free Software Foundation, Inc., 5 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 6 | Everyone is permitted to copy and distribute verbatim copies 7 | of this license document, but changing it is not allowed. 8 | 9 | Preamble 10 | 11 | The licenses for most software are designed to take away your 12 | freedom to share and change it. By contrast, the GNU General Public 13 | License is intended to guarantee your freedom to share and change free 14 | software--to make sure the software is free for all its users. This 15 | General Public License applies to most of the Free Software 16 | Foundation's software and to any other program whose authors commit to 17 | using it. (Some other Free Software Foundation software is covered by 18 | the GNU Lesser General Public License instead.) You can apply it to 19 | your programs, too. 20 | 21 | When we speak of free software, we are referring to freedom, not 22 | price. Our General Public Licenses are designed to make sure that you 23 | have the freedom to distribute copies of free software (and charge for 24 | this service if you wish), that you receive source code or can get it 25 | if you want it, that you can change the software or use pieces of it 26 | in new free programs; and that you know you can do these things. 27 | 28 | To protect your rights, we need to make restrictions that forbid 29 | anyone to deny you these rights or to ask you to surrender the rights. 30 | These restrictions translate to certain responsibilities for you if you 31 | distribute copies of the software, or if you modify it. 32 | 33 | For example, if you distribute copies of such a program, whether 34 | gratis or for a fee, you must give the recipients all the rights that 35 | you have. You must make sure that they, too, receive or can get the 36 | source code. And you must show them these terms so they know their 37 | rights. 38 | 39 | We protect your rights with two steps: (1) copyright the software, and 40 | (2) offer you this license which gives you legal permission to copy, 41 | distribute and/or modify the software. 42 | 43 | Also, for each author's protection and ours, we want to make certain 44 | that everyone understands that there is no warranty for this free 45 | software. If the software is modified by someone else and passed on, we 46 | want its recipients to know that what they have is not the original, so 47 | that any problems introduced by others will not reflect on the original 48 | authors' reputations. 49 | 50 | Finally, any free program is threatened constantly by software 51 | patents. We wish to avoid the danger that redistributors of a free 52 | program will individually obtain patent licenses, in effect making the 53 | program proprietary. To prevent this, we have made it clear that any 54 | patent must be licensed for everyone's free use or not licensed at all. 55 | 56 | The precise terms and conditions for copying, distribution and 57 | modification follow. 58 | 59 | GNU GENERAL PUBLIC LICENSE 60 | TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 61 | 62 | 0. This License applies to any program or other work which contains 63 | a notice placed by the copyright holder saying it may be distributed 64 | under the terms of this General Public License. The "Program", below, 65 | refers to any such program or work, and a "work based on the Program" 66 | means either the Program or any derivative work under copyright law: 67 | that is to say, a work containing the Program or a portion of it, 68 | either verbatim or with modifications and/or translated into another 69 | language. (Hereinafter, translation is included without limitation in 70 | the term "modification".) Each licensee is addressed as "you". 71 | 72 | Activities other than copying, distribution and modification are not 73 | covered by this License; they are outside its scope. The act of 74 | running the Program is not restricted, and the output from the Program 75 | is covered only if its contents constitute a work based on the 76 | Program (independent of having been made by running the Program). 77 | Whether that is true depends on what the Program does. 78 | 79 | 1. You may copy and distribute verbatim copies of the Program's 80 | source code as you receive it, in any medium, provided that you 81 | conspicuously and appropriately publish on each copy an appropriate 82 | copyright notice and disclaimer of warranty; keep intact all the 83 | notices that refer to this License and to the absence of any warranty; 84 | and give any other recipients of the Program a copy of this License 85 | along with the Program. 86 | 87 | You may charge a fee for the physical act of transferring a copy, and 88 | you may at your option offer warranty protection in exchange for a fee. 89 | 90 | 2. You may modify your copy or copies of the Program or any portion 91 | of it, thus forming a work based on the Program, and copy and 92 | distribute such modifications or work under the terms of Section 1 93 | above, provided that you also meet all of these conditions: 94 | 95 | a) You must cause the modified files to carry prominent notices 96 | stating that you changed the files and the date of any change. 97 | 98 | b) You must cause any work that you distribute or publish, that in 99 | whole or in part contains or is derived from the Program or any 100 | part thereof, to be licensed as a whole at no charge to all third 101 | parties under the terms of this License. 102 | 103 | c) If the modified program normally reads commands interactively 104 | when run, you must cause it, when started running for such 105 | interactive use in the most ordinary way, to print or display an 106 | announcement including an appropriate copyright notice and a 107 | notice that there is no warranty (or else, saying that you provide 108 | a warranty) and that users may redistribute the program under 109 | these conditions, and telling the user how to view a copy of this 110 | License. (Exception: if the Program itself is interactive but 111 | does not normally print such an announcement, your work based on 112 | the Program is not required to print an announcement.) 113 | 114 | These requirements apply to the modified work as a whole. If 115 | identifiable sections of that work are not derived from the Program, 116 | and can be reasonably considered independent and separate works in 117 | themselves, then this License, and its terms, do not apply to those 118 | sections when you distribute them as separate works. But when you 119 | distribute the same sections as part of a whole which is a work based 120 | on the Program, the distribution of the whole must be on the terms of 121 | this License, whose permissions for other licensees extend to the 122 | entire whole, and thus to each and every part regardless of who wrote it. 123 | 124 | Thus, it is not the intent of this section to claim rights or contest 125 | your rights to work written entirely by you; rather, the intent is to 126 | exercise the right to control the distribution of derivative or 127 | collective works based on the Program. 128 | 129 | In addition, mere aggregation of another work not based on the Program 130 | with the Program (or with a work based on the Program) on a volume of 131 | a storage or distribution medium does not bring the other work under 132 | the scope of this License. 133 | 134 | 3. You may copy and distribute the Program (or a work based on it, 135 | under Section 2) in object code or executable form under the terms of 136 | Sections 1 and 2 above provided that you also do one of the following: 137 | 138 | a) Accompany it with the complete corresponding machine-readable 139 | source code, which must be distributed under the terms of Sections 140 | 1 and 2 above on a medium customarily used for software interchange; or, 141 | 142 | b) Accompany it with a written offer, valid for at least three 143 | years, to give any third party, for a charge no more than your 144 | cost of physically performing source distribution, a complete 145 | machine-readable copy of the corresponding source code, to be 146 | distributed under the terms of Sections 1 and 2 above on a medium 147 | customarily used for software interchange; or, 148 | 149 | c) Accompany it with the information you received as to the offer 150 | to distribute corresponding source code. (This alternative is 151 | allowed only for noncommercial distribution and only if you 152 | received the program in object code or executable form with such 153 | an offer, in accord with Subsection b above.) 154 | 155 | The source code for a work means the preferred form of the work for 156 | making modifications to it. For an executable work, complete source 157 | code means all the source code for all modules it contains, plus any 158 | associated interface definition files, plus the scripts used to 159 | control compilation and installation of the executable. However, as a 160 | special exception, the source code distributed need not include 161 | anything that is normally distributed (in either source or binary 162 | form) with the major components (compiler, kernel, and so on) of the 163 | operating system on which the executable runs, unless that component 164 | itself accompanies the executable. 165 | 166 | If distribution of executable or object code is made by offering 167 | access to copy from a designated place, then offering equivalent 168 | access to copy the source code from the same place counts as 169 | distribution of the source code, even though third parties are not 170 | compelled to copy the source along with the object code. 171 | 172 | 4. You may not copy, modify, sublicense, or distribute the Program 173 | except as expressly provided under this License. Any attempt 174 | otherwise to copy, modify, sublicense or distribute the Program is 175 | void, and will automatically terminate your rights under this License. 176 | However, parties who have received copies, or rights, from you under 177 | this License will not have their licenses terminated so long as such 178 | parties remain in full compliance. 179 | 180 | 5. You are not required to accept this License, since you have not 181 | signed it. However, nothing else grants you permission to modify or 182 | distribute the Program or its derivative works. These actions are 183 | prohibited by law if you do not accept this License. Therefore, by 184 | modifying or distributing the Program (or any work based on the 185 | Program), you indicate your acceptance of this License to do so, and 186 | all its terms and conditions for copying, distributing or modifying 187 | the Program or works based on it. 188 | 189 | 6. Each time you redistribute the Program (or any work based on the 190 | Program), the recipient automatically receives a license from the 191 | original licensor to copy, distribute or modify the Program subject to 192 | these terms and conditions. You may not impose any further 193 | restrictions on the recipients' exercise of the rights granted herein. 194 | You are not responsible for enforcing compliance by third parties to 195 | this License. 196 | 197 | 7. If, as a consequence of a court judgment or allegation of patent 198 | infringement or for any other reason (not limited to patent issues), 199 | conditions are imposed on you (whether by court order, agreement or 200 | otherwise) that contradict the conditions of this License, they do not 201 | excuse you from the conditions of this License. If you cannot 202 | distribute so as to satisfy simultaneously your obligations under this 203 | License and any other pertinent obligations, then as a consequence you 204 | may not distribute the Program at all. For example, if a patent 205 | license would not permit royalty-free redistribution of the Program by 206 | all those who receive copies directly or indirectly through you, then 207 | the only way you could satisfy both it and this License would be to 208 | refrain entirely from distribution of the Program. 209 | 210 | If any portion of this section is held invalid or unenforceable under 211 | any particular circumstance, the balance of the section is intended to 212 | apply and the section as a whole is intended to apply in other 213 | circumstances. 214 | 215 | It is not the purpose of this section to induce you to infringe any 216 | patents or other property right claims or to contest validity of any 217 | such claims; this section has the sole purpose of protecting the 218 | integrity of the free software distribution system, which is 219 | implemented by public license practices. Many people have made 220 | generous contributions to the wide range of software distributed 221 | through that system in reliance on consistent application of that 222 | system; it is up to the author/donor to decide if he or she is willing 223 | to distribute software through any other system and a licensee cannot 224 | impose that choice. 225 | 226 | This section is intended to make thoroughly clear what is believed to 227 | be a consequence of the rest of this License. 228 | 229 | 8. If the distribution and/or use of the Program is restricted in 230 | certain countries either by patents or by copyrighted interfaces, the 231 | original copyright holder who places the Program under this License 232 | may add an explicit geographical distribution limitation excluding 233 | those countries, so that distribution is permitted only in or among 234 | countries not thus excluded. In such case, this License incorporates 235 | the limitation as if written in the body of this License. 236 | 237 | 9. The Free Software Foundation may publish revised and/or new versions 238 | of the General Public License from time to time. Such new versions will 239 | be similar in spirit to the present version, but may differ in detail to 240 | address new problems or concerns. 241 | 242 | Each version is given a distinguishing version number. If the Program 243 | specifies a version number of this License which applies to it and "any 244 | later version", you have the option of following the terms and conditions 245 | either of that version or of any later version published by the Free 246 | Software Foundation. If the Program does not specify a version number of 247 | this License, you may choose any version ever published by the Free Software 248 | Foundation. 249 | 250 | 10. If you wish to incorporate parts of the Program into other free 251 | programs whose distribution conditions are different, write to the author 252 | to ask for permission. For software which is copyrighted by the Free 253 | Software Foundation, write to the Free Software Foundation; we sometimes 254 | make exceptions for this. Our decision will be guided by the two goals 255 | of preserving the free status of all derivatives of our free software and 256 | of promoting the sharing and reuse of software generally. 257 | 258 | NO WARRANTY 259 | 260 | 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY 261 | FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN 262 | OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES 263 | PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED 264 | OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 265 | MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS 266 | TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE 267 | PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, 268 | REPAIR OR CORRECTION. 269 | 270 | 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 271 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR 272 | REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, 273 | INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING 274 | OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED 275 | TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY 276 | YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER 277 | PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE 278 | POSSIBILITY OF SUCH DAMAGES. 279 | 280 | END OF TERMS AND CONDITIONS 281 | 282 | How to Apply These Terms to Your New Programs 283 | 284 | If you develop a new program, and you want it to be of the greatest 285 | possible use to the public, the best way to achieve this is to make it 286 | free software which everyone can redistribute and change under these terms. 287 | 288 | To do so, attach the following notices to the program. It is safest 289 | to attach them to the start of each source file to most effectively 290 | convey the exclusion of warranty; and each file should have at least 291 | the "copyright" line and a pointer to where the full notice is found. 292 | 293 | {description} 294 | Copyright (C) {year} {fullname} 295 | 296 | This program is free software; you can redistribute it and/or modify 297 | it under the terms of the GNU General Public License as published by 298 | the Free Software Foundation; either version 2 of the License, or 299 | (at your option) any later version. 300 | 301 | This program is distributed in the hope that it will be useful, 302 | but WITHOUT ANY WARRANTY; without even the implied warranty of 303 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 304 | GNU General Public License for more details. 305 | 306 | You should have received a copy of the GNU General Public License along 307 | with this program; if not, write to the Free Software Foundation, Inc., 308 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 309 | 310 | Also add information on how to contact you by electronic and paper mail. 311 | 312 | If the program is interactive, make it output a short notice like this 313 | when it starts in an interactive mode: 314 | 315 | Gnomovision version 69, Copyright (C) year name of author 316 | Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 317 | This is free software, and you are welcome to redistribute it 318 | under certain conditions; type `show c' for details. 319 | 320 | The hypothetical commands `show w' and `show c' should show the appropriate 321 | parts of the General Public License. Of course, the commands you use may 322 | be called something other than `show w' and `show c'; they could even be 323 | mouse-clicks or menu items--whatever suits your program. 324 | 325 | You should also get your employer (if you work as a programmer) or your 326 | school, if any, to sign a "copyright disclaimer" for the program, if 327 | necessary. Here is a sample; alter the names: 328 | 329 | Yoyodyne, Inc., hereby disclaims all copyright interest in the program 330 | `Gnomovision' (which makes passes at compilers) written by James Hacker. 331 | 332 | {signature of Ty Coon}, 1 April 1989 333 | Ty Coon, President of Vice 334 | 335 | This General Public License does not permit incorporating your program into 336 | proprietary programs. If your program is a subroutine library, you may 337 | consider it more useful to permit linking proprietary applications with the 338 | library. If this is what you want to do, use the GNU Lesser General 339 | Public License instead of this License. 340 | -------------------------------------------------------------------------------- /debian/usr/share/man/man1/amidiauto.1: -------------------------------------------------------------------------------- 1 | .TH amidiauto 1 2 | .SH NAME 3 | amidiauto \- ALSA MIDI autoconnect daemon. 4 | .SH SYNOPSIS 5 | .B amidiauto 6 | 7 | Example: 8 | 9 | amidiauto 10 | .SH DESCRIPTION 11 | .B amidiauto 12 | ALSA MIDI autoconnect daemon. 13 | 14 | Automatically detects port changes, and makes the connection between the software and hardware MIDI ports. If software or hardware provides more than one input and one output port, only the first ones get connected. 15 | 16 | See https://github.com/BlokasLabs/amidiauto/ for more information. 17 | --------------------------------------------------------------------------------