1
2 /*
3 * Diverse Bristol midi routines.
4 * Copyright (c) by Nick Copeland <nickycopeland@hotmail.com> 1996,2012
5 *
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, see <http://www.gnu.org/licenses/>.
19 *
20 */
21
22 //#define DEBUG
23
24 #include "bristol.h"
25 #include "bristolmidi.h"
26
27 extern bristolMidiMain bmidi;
28
29 int
bristolMidiFindDev(char * name)30 bristolMidiFindDev(char *name)
31 {
32 int i;
33
34 #ifdef DEBUG
35 if (name == (char *) NULL)
36 printf("bristolMidiFindDev(NULL)\n");
37 else
38 printf("bristolMidiFindDev(\"%s\")\n", name);
39 #endif
40
41 /*
42 * If we have a name, find that name, or return bad handle.
43 * If we do not have a name then return the first free handle we found.
44 */
45 for (i = 0; i < BRISTOL_MIDI_DEVCOUNT; i++)
46 {
47 if (bmidi.dev[i].state == -1)
48 {
49 if (name == (char *) NULL)
50 return(i);
51 } else {
52 if (name != (char *) NULL)
53 {
54 if (strcmp(name, bmidi.dev[i].name) == 0)
55 {
56 #ifdef DEBUG
57 printf("found dev %s\n", name);
58 #endif
59 return(i);
60 }
61 }
62 }
63 }
64
65 return(BRISTOL_MIDI_DEVICE);
66 }
67
68 int
bristolMidiDevSanity(int dev)69 bristolMidiDevSanity(int dev)
70 {
71 #ifdef DEBUG
72 printf("bristolMidiDevSanity(%i)\n", dev);
73 #endif
74
75 if ((dev < 0) || (dev > BRISTOL_MIDI_DEVCOUNT))
76 return(BRISTOL_MIDI_DEV);
77 if (bmidi.dev[dev].state < 0)
78 return(BRISTOL_MIDI_DEV);
79 if (bmidi.dev[dev].flags <= 0)
80 return(BRISTOL_MIDI_DEV);
81 if (bmidi.dev[dev].fd <= 0)
82 return(BRISTOL_MIDI_DEV);
83 return(BRISTOL_MIDI_OK);
84 }
85
86 void
bristolMidiPrintHandle(handle)87 bristolMidiPrintHandle(handle)
88 {
89 printf(" Handle: %i\n", bmidi.handle[handle].handle);
90 printf(" State: %i\n", bmidi.handle[handle].state);
91 printf(" Channel: %i\n", bmidi.handle[handle].channel);
92 printf(" Dev: %i\n", bmidi.handle[handle].dev);
93 printf(" Flags: %x\n", bmidi.handle[handle].flags);
94
95 if ((bmidi.handle[handle].dev < 0)
96 || (bmidi.handle[handle].dev >= BRISTOL_MIDI_DEVCOUNT))
97 return;
98
99 printf(" devfd: %i\n", bmidi.dev[bmidi.handle[handle].dev].fd);
100 printf(" state: %i\n", bmidi.dev[bmidi.handle[handle].dev].state);
101 printf(" hcount: %i\n", bmidi.dev[bmidi.handle[handle].dev].handleCount);
102 }
103
104 int
bristolMidiSanity(handle)105 bristolMidiSanity(handle)
106 {
107 #ifdef DEBUG
108 printf("bristolMidiSanity(%i)\n", handle);
109 #endif
110
111 if ((handle < 0) || (handle > BRISTOL_MIDI_HANDLES))
112 return(BRISTOL_MIDI_HANDLE);
113
114 /*
115 * So we can at least print it
116 bristolMidiPrintHandle(handle);
117 */
118
119 if (bmidi.handle[handle].state < 0)
120 return(BRISTOL_MIDI_HANDLE);
121
122 if ((bmidi.handle[handle].handle < 0)
123 || (bmidi.handle[handle].handle >= BRISTOL_MIDI_HANDLES))
124 return(BRISTOL_MIDI_HANDLE);
125
126 if ((bmidi.handle[handle].dev < 0)
127 || (bmidi.handle[handle].dev >= BRISTOL_MIDI_DEVCOUNT))
128 return(BRISTOL_MIDI_DEVICE);
129
130 if (bmidi.dev[bmidi.handle[handle].dev].state < 0)
131 return(BRISTOL_MIDI_DEVICE);
132
133 if (bmidi.dev[bmidi.handle[handle].dev].handleCount < 1)
134 return(BRISTOL_MIDI_DEVICE);
135
136 return(BRISTOL_MIDI_OK);
137 }
138
139 int
bristolMidiFindFreeHandle()140 bristolMidiFindFreeHandle()
141 {
142 int i;
143
144 #ifdef DEBUG
145 printf("bristolMidiFindFreeHandle()\n");
146 #endif
147
148 for (i = 0; i < BRISTOL_MIDI_HANDLES; i++)
149 if (bmidi.handle[i].state == -1)
150 return(i);
151
152 return(BRISTOL_MIDI_HANDLE);
153 }
154
155 int
bristolFreeHandle(int handle)156 bristolFreeHandle(int handle)
157 {
158 #ifdef DEBUG
159 printf("bristolMidiFreeHandle()\n");
160 #endif
161
162 bmidi.handle[handle].handle = -1;
163 bmidi.handle[handle].state = -1;
164 bmidi.handle[handle].channel = -1;
165 bmidi.handle[handle].dev = -1;
166 bmidi.handle[handle].flags = -1;
167 bmidi.handle[handle].messagemask = -1;
168 bmidi.handle[handle].callback = NULL;
169
170 return(0);
171 }
172
173 int
bristolFreeDevice(int dev)174 bristolFreeDevice(int dev)
175 {
176 #ifdef DEBUG
177 printf("bristolFreeDevice(%i, %i)\n", dev, bmidi.dev[dev].fd);
178 #endif
179
180 if (bmidi.dev[dev].fd > 0)
181 close(bmidi.dev[dev].fd);
182
183 bmidi.dev[dev].lastcommand = -1;
184 bmidi.dev[dev].lastcommstate = -1;
185 bmidi.dev[dev].lastchan = -1;
186 bmidi.dev[dev].fd = -1;
187 bmidi.dev[dev].state = -1;
188 bmidi.dev[dev].flags = -1;
189 bmidi.dev[dev].handleCount = -1;
190 bmidi.dev[dev].name[0] = '\0';
191 bmidi.dev[dev].bufcount = 0;
192 bmidi.dev[dev].bufindex = 0;
193
194 return(0);
195 }
196
197 /*
198 * A routine to initalise any of our internal structures.
199 */
200 void
initMidiLib(int flags)201 initMidiLib(int flags)
202 {
203 int i;
204
205 if (bmidi.flags & BRISTOL_MIDI_INITTED)
206 return;
207
208 bmidi.msgforwarder = NULL;
209 bmidi.flags = 0;
210
211 /*
212 * The first handle we open should be a control socket. I prefer not to use
213 * TCP sockets at the moment, but this will be the eventual goal.
214 */
215
216 for (i = 0; i < BRISTOL_MIDI_DEVCOUNT; i++)
217 bristolFreeDevice(i);
218 for (i = 0; i < BRISTOL_MIDI_HANDLES; i++)
219 bristolFreeHandle(i);
220
221 bmidi.flags |= BRISTOL_MIDI_INITTED|(BRISTOL_MIDI_WAIT & flags);
222 }
223
224