1 |
// example_server.c |
2 |
// |
3 |
/**************************************************************************** |
4 |
Copyright (C) 2004, rncbc aka Rui Nuno Capela. All rights reserved. |
5 |
|
6 |
This program is free software; you can redistribute it and/or |
7 |
modify it under the terms of the GNU General Public License |
8 |
as published by the Free Software Foundation; either version 2 |
9 |
of the License, or (at your option) any later version. |
10 |
|
11 |
This program is distributed in the hope that it will be useful, |
12 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 |
GNU General Public License for more details. |
15 |
|
16 |
You should have received a copy of the GNU General Public License |
17 |
along with this program; if not, write to the Free Software |
18 |
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
19 |
|
20 |
*****************************************************************************/ |
21 |
|
22 |
#include "server.h" |
23 |
#include "parser.h" |
24 |
|
25 |
#include <time.h> |
26 |
|
27 |
#define SERVER_PORT 8888 |
28 |
|
29 |
#if defined(WIN32) |
30 |
static WSADATA _wsaData; |
31 |
#endif |
32 |
|
33 |
//////////////////////////////////////////////////////////////////////// |
34 |
|
35 |
lscp_status_t server_callback ( lscp_connect_t *pConnect, const char *pchBuffer, int cchBuffer, void *pvData ) |
36 |
{ |
37 |
lscp_status_t ret = LSCP_OK; |
38 |
lscp_parser_t tok; |
39 |
const char *pszResult = NULL; |
40 |
char szTemp[256]; |
41 |
int i; |
42 |
static int iSamplerChannel = 0; |
43 |
static int iAudioDevice = 0; |
44 |
static int iMidiDevice = 0; |
45 |
|
46 |
if (pchBuffer == NULL) { |
47 |
fprintf(stderr, "server_callback: addr=%s port=%d: ", |
48 |
inet_ntoa(pConnect->client.addr.sin_addr), |
49 |
htons(pConnect->client.addr.sin_port)); |
50 |
switch (cchBuffer) { |
51 |
case LSCP_CONNECT_OPEN: |
52 |
fprintf(stderr, "New client connection.\n"); |
53 |
break; |
54 |
case LSCP_CONNECT_CLOSE: |
55 |
fprintf(stderr, "Connection closed.\n"); |
56 |
break; |
57 |
} |
58 |
return ret; |
59 |
} |
60 |
|
61 |
lscp_socket_trace("server_callback", &(pConnect->client.addr), pchBuffer, cchBuffer); |
62 |
|
63 |
lscp_parser_init(&tok, pchBuffer, cchBuffer); |
64 |
|
65 |
if (lscp_parser_test(&tok, "GET")) { |
66 |
if (lscp_parser_test(&tok, "CHANNEL")) { |
67 |
if (lscp_parser_test(&tok, "INFO")) { |
68 |
// Getting sampler channel informations: |
69 |
// GET CHANNEL INFO <sampler-channel> |
70 |
pszResult = "ENGINE_NAME: DummyEngine\r\n" |
71 |
"INSTRUMENT_FILE: DummyInstrument.gig\r\n" |
72 |
"INSTRUMENT_NR: 0\r\n" |
73 |
"INSTRUMENT_STATUS: 100\r\n" |
74 |
"AUDIO_OUTPUT_DEVICE: 0\r\n" |
75 |
"AUDIO_OUTPUT_CHANNELS: 2\r\n" |
76 |
"AUDIO_OUTPUT_ROUTING: 0,1\r\n" |
77 |
"MIDI_INPUT_DEVICE: 0\r\n" |
78 |
"MIDI_INPUT_PORT: 0\r\n" |
79 |
"MIDI_INPUT_CHANNEL: ALL\r\n" |
80 |
"VOLUME: 0.5\r\n"; |
81 |
} |
82 |
else if (lscp_parser_test(&tok, "VOICE_COUNT")) { |
83 |
// Current number of active voices: |
84 |
// GET CHANNEL VOICE_COUNT <sampler-channel> |
85 |
sprintf(szTemp, "%d", rand() % 100); |
86 |
pszResult = szTemp; |
87 |
} |
88 |
else if (lscp_parser_test(&tok, "STREAM_COUNT")) { |
89 |
// Current number of active disk streams: |
90 |
// GET CHANNEL STREAM_COUNT <sampler-channel> |
91 |
pszResult = "3\r\n"; |
92 |
} |
93 |
else if (lscp_parser_test(&tok, "BUFFER_FILL")) { |
94 |
if (lscp_parser_test(&tok, "BYTES")) { |
95 |
// Current fill state of disk stream buffers: |
96 |
// GET CHANNEL BUFFER_FILL BYTES <sampler-channel> |
97 |
sprintf(szTemp, "[1]%d,[2]%d,[3]%d\r\n", rand(), rand(), rand()); |
98 |
pszResult = szTemp; |
99 |
} |
100 |
else if (lscp_parser_test(&tok, "PERCENTAGE")) { |
101 |
// Current fill state of disk stream buffers: |
102 |
// GET CHANNEL BUFFER_FILL PERCENTAGE <sampler-channel> |
103 |
sprintf(szTemp, "[1]%d%%,[2]%d%%,[3]%d%%\r\n", rand() % 100, rand() % 100, rand() % 100); |
104 |
pszResult = szTemp; |
105 |
} |
106 |
else ret = LSCP_FAILED; |
107 |
} |
108 |
else ret = LSCP_FAILED; |
109 |
} |
110 |
else if (lscp_parser_test(&tok, "CHANNELS")) { |
111 |
// Current number of sampler channels: |
112 |
// GET CHANNELS |
113 |
sprintf(szTemp, "%d", iSamplerChannel); |
114 |
pszResult = szTemp; |
115 |
} |
116 |
else if (lscp_parser_test(&tok, "AVAILABLE_AUDIO_OUTPUT_DRIVERS")) { |
117 |
// Getting all available audio output drivers. |
118 |
// GET AVAILABLE_AUDIO_OUTPUT_DRIVERS |
119 |
pszResult = "Alsa,Jack\r\n"; |
120 |
} |
121 |
else if (lscp_parser_test(&tok, "AVAILABLE_MIDI_INPUT_DRIVERS")) { |
122 |
// Getting all available MIDI input drivers. |
123 |
// GET AVAILABLE_MIDI_INPUT_DRIVERS |
124 |
pszResult = "Alsa\r\n"; |
125 |
} |
126 |
else if (lscp_parser_test2(&tok, "AUDIO_OUTPUT_DRIVER", "INFO")) { |
127 |
// Getting informations about a specific audio output driver. |
128 |
// GET AUDIO_OUTPUT_DRIVER INFO <audio-output-type> |
129 |
if (lscp_parser_test(&tok, "Alsa")) { |
130 |
pszResult = "DESCRIPTION: 'ALSA PCM'\r\n" |
131 |
"VERSION: '1.0'\r\n" |
132 |
"PARAMETERS: channels,samplerate,active\r\n"; |
133 |
} |
134 |
else if (lscp_parser_test(&tok, "Jack")) { |
135 |
pszResult = "DESCRIPTION: JACK Audio Connection Kit\r\n" |
136 |
"VERSION: 0.98.1\r\n" |
137 |
"PARAMETERS: channels,samplerate,active\r\n"; |
138 |
} |
139 |
else ret = LSCP_FAILED; |
140 |
} |
141 |
else if (lscp_parser_test2(&tok, "MIDI_INPUT_DRIVER", "INFO")) { |
142 |
// Getting informations about a specific MIDI input driver. |
143 |
// GET MIDI_INPUT_DRIVER INFO <midi-input-type> |
144 |
if (lscp_parser_test(&tok, "Alsa")) { |
145 |
pszResult = "DESCRIPTION: ALSA Sequencer\r\n" |
146 |
"VERSION: 1.0\r\n" |
147 |
"PARAMETERS: ports,active\r\n"; |
148 |
} |
149 |
else ret = LSCP_FAILED; |
150 |
} |
151 |
else if (lscp_parser_test2(&tok, "AUDIO_OUTPUT_DRIVER_PARAMETER", "INFO")) { |
152 |
// Getting informations about a specific audio output driver parameter. |
153 |
// GET AUDIO_OUTPUT_DRIVER_PARAMETER INFO <audio-output-type> <param> |
154 |
if (lscp_parser_test(&tok, "Alsa")) { |
155 |
if (lscp_parser_test(&tok, "active")) { |
156 |
pszResult = "DESCRIPTION: 'ALSA PCM active state'\r\n" |
157 |
"TYPE: BOOL\r\n" |
158 |
"MANDATORY: TRUE\r\n" |
159 |
"FIX: TRUE\r\n" |
160 |
"MULTIPLICITY: FALSE\r\n" |
161 |
"DEPENDS: channels,samplerate,card\r\n" |
162 |
"DEFAULT: TRUE\r\n" |
163 |
"RANGE_MIN: FALSE\r\n" |
164 |
"RANGE_MAX: TRUE\r\n" |
165 |
"POSSIBILITIES: FALSE,TRUE\r\n"; |
166 |
} |
167 |
else if (lscp_parser_test(&tok, "channels")) { |
168 |
pszResult = "DESCRIPTION: 'Number of ALSA PCM channels'\r\n" |
169 |
"TYPE: INT\r\n" |
170 |
"MANDATORY: TRUE\r\n" |
171 |
"FIX: TRUE\r\n" |
172 |
"MULTIPLICITY: FALSE\r\n" |
173 |
"DEFAULT: 2\r\n"; |
174 |
} |
175 |
else if (lscp_parser_test(&tok, "samplerate")) { |
176 |
pszResult = "DESCRIPTION: 'ALSA PCM sample rate'\r\n" |
177 |
"TYPE: INT\r\n" |
178 |
"MANDATORY: TRUE\r\n" |
179 |
"FIX: TRUE\r\n" |
180 |
"MULTIPLICITY: TRUE\r\n" |
181 |
"DEFAULT: 44100\r\n" |
182 |
"POSSIBILITIES: 44100,48000,96000\r\n"; |
183 |
} |
184 |
else ret = LSCP_FAILED; |
185 |
} |
186 |
else if (lscp_parser_test(&tok, "Jack")) { |
187 |
if (lscp_parser_test(&tok, "active")) { |
188 |
pszResult = "DESCRIPTION: 'JACK active state'\r\n" |
189 |
"TYPE: BOOL\r\n" |
190 |
"MANDATORY: TRUE\r\n" |
191 |
"FIX: TRUE\r\n" |
192 |
"MULTIPLICITY: FALSE\r\n" |
193 |
"DEPENDS: channels,samplerate\r\n" |
194 |
"DEFAULT: TRUE\r\n" |
195 |
"RANGE_MIN: FALSE\r\n" |
196 |
"RANGE_MAX: TRUE\r\n" |
197 |
"POSSIBILITIES: FALSE,TRUE\r\n"; |
198 |
} |
199 |
else if (lscp_parser_test(&tok, "channels")) { |
200 |
pszResult = "DESCRIPTION: 'Number of JACK audio channels'\r\n" |
201 |
"TYPE: INT\r\n" |
202 |
"MANDATORY: TRUE\r\n" |
203 |
"FIX: TRUE\r\n" |
204 |
"MULTIPLICITY: FALSE\r\n" |
205 |
"DEFAULT: 2\r\n"; |
206 |
} |
207 |
else if (lscp_parser_test(&tok, "samplerate")) { |
208 |
pszResult = "DESCRIPTION: 'JACK sample rate'\r\n" |
209 |
"TYPE: INT\r\n" |
210 |
"MANDATORY: TRUE\r\n" |
211 |
"FIX: TRUE\r\n" |
212 |
"MULTIPLICITY: TRUE\r\n" |
213 |
"DEFAULT: 44100\r\n" |
214 |
"POSSIBILITIES: 44100,48000,96000\r\n"; |
215 |
} |
216 |
else ret = LSCP_FAILED; |
217 |
} |
218 |
else ret = LSCP_FAILED; |
219 |
} |
220 |
else if (lscp_parser_test2(&tok, "MIDI_INPUT_DRIVER_PARAMETER", "INFO")) { |
221 |
// Getting informations about a specific MIDI input driver parameter. |
222 |
// GET MIDI_INPUT_DRIVER_PARAMETER INFO <midi-input-type> <param> |
223 |
if (lscp_parser_test(&tok, "Alsa")) { |
224 |
if (lscp_parser_test(&tok, "active")) { |
225 |
pszResult = "DESCRIPTION: 'ALSA Sequencer device active state'\r\n" |
226 |
"TYPE: BOOL\r\n" |
227 |
"MANDATORY: TRUE\r\n" |
228 |
"FIX: TRUE\r\n" |
229 |
"MULTIPLICITY: FALSE\r\n" |
230 |
"DEPENDS: channels,ports\r\n" |
231 |
"DEFAULT: TRUE\r\n" |
232 |
"RANGE_MIN: FALSE\r\n" |
233 |
"RANGE_MAX: TRUE\r\n" |
234 |
"POSSIBILITIES: FALSE,TRUE\r\n"; |
235 |
} |
236 |
else if (lscp_parser_test(&tok, "ports")) { |
237 |
pszResult = "DESCRIPTION: 'Number of ALSA Sequencer ports'\r\n" |
238 |
"TYPE: INT\r\n" |
239 |
"MANDATORY: FALSE\r\n" |
240 |
"FIX: FALSE\r\n" |
241 |
"MULTIPLICITY: FALSE\r\n" |
242 |
"DEFAULT: 1\r\n" |
243 |
"RANGE_MIN: 1\r\n" |
244 |
"RANGE_MAX: 4\r\n"; |
245 |
} |
246 |
else ret = LSCP_FAILED; |
247 |
} |
248 |
else ret = LSCP_FAILED; |
249 |
} |
250 |
else if (lscp_parser_test2(&tok, "AUDIO_OUTPUT_DEVICE", "INFO")) { |
251 |
// Getting informations about a specific audio output device. |
252 |
// GET AUDIO_OUTPUT_DEVICE INFO <audio-device-id> |
253 |
if (lscp_parser_nextint(&tok) < iAudioDevice) { |
254 |
pszResult = "driver: Alsa\r\n" |
255 |
"active: TRUE\r\n" |
256 |
"channels: 2\r\n" |
257 |
"samplerate: 44100\r\n"; |
258 |
} |
259 |
else ret = LSCP_FAILED; |
260 |
} |
261 |
else if (lscp_parser_test2(&tok, "MIDI_INPUT_DEVICE", "INFO")) { |
262 |
// Getting informations about a specific MIDI input device. |
263 |
// GET MIDI_INPUT_DEVICE INFO <midi-device-id> |
264 |
if (lscp_parser_nextint(&tok) < iMidiDevice) { |
265 |
pszResult = "driver: Alsa\r\n" |
266 |
"active: TRUE\r\n" |
267 |
"channels: 16\r\n" |
268 |
"ports: 1\r\n"; |
269 |
} |
270 |
else ret = LSCP_FAILED; |
271 |
} |
272 |
else if (lscp_parser_test2(&tok, "AUDIO_OUTPUT_CHANNEL", "INFO")) { |
273 |
// Getting informations about an audio channel. |
274 |
// GET AUDIO_OUTPUT_CHANNEL INFO <audio-device-id> <audio-channel> |
275 |
if (lscp_parser_nextint(&tok) < iAudioDevice) { |
276 |
pszResult = "name: DummyMonitor\r\n" |
277 |
"is_mix_channel: FALSE\r\n" |
278 |
"mix_channel_destination: 0\r\n"; |
279 |
} |
280 |
else ret = LSCP_FAILED; |
281 |
} |
282 |
else if (lscp_parser_test2(&tok, "MIDI_INPUT_PORT", "INFO")) { |
283 |
// Getting informations about a MIDI port. |
284 |
// GET MIDI_INPUT_PORT INFO <midi-device-id> <midi-port> |
285 |
if (lscp_parser_nextint(&tok) < iMidiDevice) { |
286 |
pszResult = "name: DummyKeyboard\r\n" |
287 |
"alsa_seq_bindings: '64:0'\r\n"; |
288 |
} |
289 |
else ret = LSCP_FAILED; |
290 |
} |
291 |
else if (lscp_parser_test2(&tok, "AUDIO_OUTPUT_CHANNEL_PARAMETER", "INFO")) { |
292 |
// Getting informations about specific audio channel parameter. |
293 |
// GET AUDIO_OUTPUT_CHANNEL_PARAMETER INFO <audio-device-id> <audio-channel> <param> |
294 |
if (lscp_parser_nextint(&tok) < iAudioDevice) { |
295 |
lscp_parser_nextint(&tok); |
296 |
if (lscp_parser_test(&tok, "is_mix_channel")) { |
297 |
pszResult = "DESCRIPTION: 'Whether this is an audio mix channel'\r\n" |
298 |
"TYPE: BOOL\r\n" |
299 |
"MANDATORY: TRUE\r\n" |
300 |
"FIX: FALSE\r\n" |
301 |
"MULTIPLICITY: FALSE\r\n" |
302 |
"POSSIBILITIES: FALSE,TRUE\r\n"; |
303 |
} |
304 |
else if (lscp_parser_test(&tok, "mix_channel_destination")) { |
305 |
pszResult = "DESCRIPTION: 'Audio mix channel destination'\r\n" |
306 |
"TYPE: INT\r\n" |
307 |
"MANDATORY: TRUE\r\n" |
308 |
"FIX: FALSE\r\n" |
309 |
"MULTIPLICITY: TRUE\r\n" |
310 |
"POSSIBILITIES: 0,1\r\n"; |
311 |
} |
312 |
else ret = LSCP_FAILED; |
313 |
} |
314 |
else ret = LSCP_FAILED; |
315 |
} |
316 |
else if (lscp_parser_test2(&tok, "MIDI_INPUT_PORT_PARAMETER", "INFO")) { |
317 |
// Getting informations about specific MIDI port parameter. |
318 |
// GET MIDI_INPUT_PORT_PARAMETER INFO <midi-device-id> <midi-port> <param> |
319 |
if (lscp_parser_nextint(&tok) < iMidiDevice) { |
320 |
lscp_parser_nextint(&tok); |
321 |
if (lscp_parser_test(&tok, "alsa_seq_bindings")) { |
322 |
pszResult = "DESCRIPTION: 'Alsa sequencer port bindings'\r\n" |
323 |
"TYPE: STRING\r\n" |
324 |
"MANDATORY: TRUE\r\n" |
325 |
"FIX: FALSE\r\n" |
326 |
"MULTIPLICITY: TRUE\r\n" |
327 |
"POSSIBILITIES: '64:0','68:0','68:1'\r\n"; |
328 |
} |
329 |
else ret = LSCP_FAILED; |
330 |
} |
331 |
else ret = LSCP_FAILED; |
332 |
} |
333 |
else if (lscp_parser_test(&tok, "AUDIO_OUTPUT_DEVICES")) { |
334 |
// Getting all created audio output device count. |
335 |
// GET AUDIO_OUTPUT_DEVICES |
336 |
sprintf(szTemp, "%d\r\n", iAudioDevice); |
337 |
pszResult = szTemp; |
338 |
} |
339 |
else if (lscp_parser_test(&tok, "MIDI_INPUT_DEVICES")) { |
340 |
// Getting all created MID input device count. |
341 |
// GET MIDI_INPUT_DEVICES |
342 |
sprintf(szTemp, "%d\r\n", iMidiDevice); |
343 |
pszResult = szTemp; |
344 |
} |
345 |
else if (lscp_parser_test(&tok, "AVAILABLE_ENGINES")) { |
346 |
// Getting all available engines: |
347 |
// GET AVAILABLE_ENGINES |
348 |
pszResult = "GigEngine,DLSEngine,AkaiEngine\r\n"; |
349 |
} |
350 |
else if (lscp_parser_test2(&tok, "ENGINE", "INFO")) { |
351 |
// Getting information about an engine. |
352 |
// GET ENGINE INFO <engine-name> |
353 |
if (lscp_parser_test(&tok, "GigEngine")) { |
354 |
pszResult = "DESCRIPTION: GigaSampler Engine\r\n" |
355 |
"VERSION: 0.3\r\n"; |
356 |
} |
357 |
else if (lscp_parser_test(&tok, "DLSEngine")) { |
358 |
pszResult = "DESCRIPTION: 'DLS Generic Engine'\r\n" |
359 |
"VERSION: 0.2\r\n"; |
360 |
} |
361 |
else if (lscp_parser_test(&tok, "AkaiEngine")) { |
362 |
pszResult = "DESCRIPTION: Akai Sampler Engine\r\n" |
363 |
"VERSION: 0.1\r\n"; |
364 |
} |
365 |
else ret = LSCP_FAILED; |
366 |
} |
367 |
else ret = LSCP_FAILED; |
368 |
} |
369 |
else if (lscp_parser_test(&tok, "LIST")) { |
370 |
if (lscp_parser_test(&tok, "CHANNELS")) { |
371 |
// Getting all created sampler channel list. |
372 |
// LIST CHANNELS |
373 |
if (iSamplerChannel > 0) { |
374 |
strcpy(szTemp, "0"); |
375 |
for (i = 1; i < iSamplerChannel && strlen(szTemp) < sizeof(szTemp) - 8; i++) |
376 |
sprintf(szTemp + strlen(szTemp), ",%d", i); |
377 |
strcat(szTemp, "\r\n"); |
378 |
pszResult = szTemp; |
379 |
} |
380 |
else ret = LSCP_FAILED; |
381 |
} |
382 |
else if (lscp_parser_test(&tok, "AUDIO_OUTPUT_DEVICES")) { |
383 |
// Getting all created audio output device list. |
384 |
// LIST AUDIO_OUTPUT_DEVICES |
385 |
if (iAudioDevice > 0) { |
386 |
strcpy(szTemp, "0"); |
387 |
for (i = 1; i < iAudioDevice && strlen(szTemp) < sizeof(szTemp) - 8; i++) |
388 |
sprintf(szTemp + strlen(szTemp), ",%d", i); |
389 |
strcat(szTemp, "\r\n"); |
390 |
pszResult = szTemp; |
391 |
} |
392 |
else ret = LSCP_FAILED; |
393 |
} |
394 |
else if (lscp_parser_test(&tok, "MIDI_INPUT_DEVICES")) { |
395 |
// Getting all created MID input device list. |
396 |
// LIST MIDI_INPUT_DEVICES |
397 |
if (iMidiDevice > 0) { |
398 |
strcpy(szTemp, "0"); |
399 |
for (i = 1; i < iMidiDevice && strlen(szTemp) < sizeof(szTemp) - 8; i++) |
400 |
sprintf(szTemp + strlen(szTemp), ",%d", i); |
401 |
strcat(szTemp, "\r\n"); |
402 |
pszResult = szTemp; |
403 |
} |
404 |
else ret = LSCP_FAILED; |
405 |
} |
406 |
else ret = LSCP_FAILED; |
407 |
} |
408 |
else if (lscp_parser_test(&tok, "SET")) { |
409 |
if (lscp_parser_test(&tok, "CHANNEL")) { |
410 |
if (lscp_parser_test(&tok, "VOLUME")) { |
411 |
// Setting channel volume: |
412 |
// SET CHANNEL VOLUME <sampler-channel> <volume> |
413 |
} |
414 |
else if (lscp_parser_test(&tok, "AUDIO_OUTPUT_TYPE")) { |
415 |
// Setting audio output type: |
416 |
// SET CHANNEL AUDIO_OUTPUT_TYPE <sampler-channel> <audio-output-type> |
417 |
} |
418 |
else if (lscp_parser_test(&tok, "AUDIO_OUTPUT_DEVICE")) { |
419 |
// Setting audio output device: |
420 |
// SET CHANNEL AUDIO_OUTPUT_DEVICE <sampler-channel> <device-id> |
421 |
} |
422 |
else if (lscp_parser_test(&tok, "AUDIO_OUTPUT_CHANNEL")) { |
423 |
// Setting audio output channel: |
424 |
// SET CHANNEL AUDIO_OUTPUT_CHANNEL <sampler-channel> <audio-in> <audio-out> |
425 |
} |
426 |
else if (lscp_parser_test(&tok, "MIDI_INPUT_TYPE")) { |
427 |
// Setting MIDI input type: |
428 |
// SET CHANNEL MIDI_INPUT_TYPE <sampler-channel> <midi-input-type> |
429 |
} |
430 |
else if (lscp_parser_test(&tok, "MIDI_INPUT_DEVICE")) { |
431 |
// Setting MIDI input device: |
432 |
// SET CHANNEL MIDI_INPUT_DEVICE <sampler-channel> <device-id> |
433 |
} |
434 |
else if (lscp_parser_test(&tok, "MIDI_INPUT_PORT")) { |
435 |
// Setting MIDI input port: |
436 |
// SET CHANNEL MIDI_INPUT_PORT <sampler-channel> <midi-input-port> |
437 |
} |
438 |
else if (lscp_parser_test(&tok, "MIDI_INPUT_CHANNEL")) { |
439 |
// Setting MIDI input channel: |
440 |
// SET CHANNEL MIDI_INPUT_CHANNEL <sampler-channel> <midi-input-chan> |
441 |
} |
442 |
else ret = LSCP_FAILED; |
443 |
} |
444 |
else ret = LSCP_FAILED; |
445 |
} |
446 |
else if (lscp_parser_test(&tok, "LOAD")) { |
447 |
if (lscp_parser_test(&tok, "ENGINE")) { |
448 |
// Loading a sampler engine: |
449 |
// LOAD ENGINE <engine-name> <sampler-channel> |
450 |
} |
451 |
else if (lscp_parser_test(&tok, "INSTRUMENT")) { |
452 |
// Loading an instrument: |
453 |
// LOAD INSTRUMENT [NON_MODAL] <filename> <instr-index> <sampler-channel> |
454 |
} |
455 |
else ret = LSCP_FAILED; |
456 |
} |
457 |
else if (lscp_parser_test2(&tok, "ADD", "CHANNEL")) { |
458 |
// Adding a new sampler channel: |
459 |
// ADD CHANNEL |
460 |
if (iSamplerChannel < 16) { |
461 |
sprintf(szTemp, "OK[%d]", iSamplerChannel++); |
462 |
pszResult = szTemp; |
463 |
} else { |
464 |
iSamplerChannel = 0; |
465 |
ret = LSCP_FAILED; |
466 |
} |
467 |
} |
468 |
else if (lscp_parser_test2(&tok, "REMOVE", "CHANNEL")) { |
469 |
// Removing a sampler channel: |
470 |
// REMOVE CHANNEL <sampler-channel> |
471 |
if (lscp_parser_nextint(&tok) < iSamplerChannel) |
472 |
iSamplerChannel--; |
473 |
else |
474 |
ret = LSCP_FAILED; |
475 |
} |
476 |
else if (lscp_parser_test(&tok, "RESET")) { |
477 |
if (lscp_parser_test(&tok, "CHANNEL")) { |
478 |
// Resetting a sampler channel: |
479 |
// RESET CHANNEL <sampler-channel> |
480 |
if (lscp_parser_nextint(&tok) > iSamplerChannel) |
481 |
ret = LSCP_FAILED; |
482 |
} else { |
483 |
// Reset sampler: |
484 |
// RESET |
485 |
iSamplerChannel = 0; |
486 |
iAudioDevice = 0; |
487 |
iMidiDevice = 0; |
488 |
} |
489 |
} |
490 |
else if (lscp_parser_test(&tok, "CREATE")) { |
491 |
if (lscp_parser_test(&tok, "AUDIO_OUTPUT_DEVICE")) { |
492 |
// Creating an audio output device. |
493 |
// CREATE AUDIO_OUTPUT_DEVICE <audio-output-driver> [<params>] |
494 |
if (iAudioDevice < 8) { |
495 |
sprintf(szTemp, "OK[%d]", iAudioDevice++); |
496 |
pszResult = szTemp; |
497 |
} else { |
498 |
iAudioDevice = 0; |
499 |
ret = LSCP_FAILED; |
500 |
} |
501 |
} |
502 |
else if (lscp_parser_test(&tok, "MIDI_INPUT_DEVICE")) { |
503 |
// Creating an MIDI input device. |
504 |
// CREATE MIDI_INPUT_DEVICE <midi-input-driver> [<params>] |
505 |
if (iMidiDevice < 8) { |
506 |
sprintf(szTemp, "OK[%d]", iMidiDevice++); |
507 |
pszResult = szTemp; |
508 |
} else { |
509 |
iMidiDevice = 0; |
510 |
ret = LSCP_FAILED; |
511 |
} |
512 |
} |
513 |
else ret = LSCP_FAILED; |
514 |
} |
515 |
else if (lscp_parser_test(&tok, "DESTROY")) { |
516 |
if (lscp_parser_test(&tok, "AUDIO_OUTPUT_DEVICE")) { |
517 |
// Destroying an audio output device. |
518 |
// DESTROY AUDIO_OUTPUT_DEVICE <audio-device-id> |
519 |
if (lscp_parser_nextint(&tok) < iAudioDevice) |
520 |
iAudioDevice--; |
521 |
else |
522 |
ret = LSCP_FAILED; |
523 |
} |
524 |
else if (lscp_parser_test(&tok, "MIDI_INPUT_DEVICE")) { |
525 |
// Destroying an MIDI intput device. |
526 |
// DESTROY MIDI_INPUT_DEVICE <midi-device-id> |
527 |
if (lscp_parser_nextint(&tok) < iMidiDevice) |
528 |
iMidiDevice--; |
529 |
else |
530 |
ret = LSCP_FAILED; |
531 |
} |
532 |
else ret = LSCP_FAILED; |
533 |
} |
534 |
else if (lscp_parser_test(&tok, "SUBSCRIBE")) { |
535 |
// Register frontend for receiving event notification messages: |
536 |
// SUBSCRIBE <event> |
537 |
ret = lscp_server_subscribe(pConnect, lscp_event_from_text(lscp_parser_next(&tok))); |
538 |
} |
539 |
else if (lscp_parser_test(&tok, "UNSUBSCRIBE")) { |
540 |
// Deregister frontend for not receiving event notification messages anymore: |
541 |
// UNSUBSCRIBE <event> |
542 |
ret = lscp_server_unsubscribe(pConnect, lscp_event_from_text(lscp_parser_next(&tok))); |
543 |
} |
544 |
else if (lscp_parser_test(&tok, "QUIT")) { |
545 |
// Close client connection: |
546 |
// QUIT |
547 |
lscp_parser_free(&tok); |
548 |
return LSCP_FAILED; // Disconnect. |
549 |
} |
550 |
else ret = LSCP_FAILED; |
551 |
|
552 |
lscp_parser_free(&tok); |
553 |
|
554 |
if (pszResult == NULL) |
555 |
pszResult = (ret == LSCP_OK ? "OK\r\n" : "ERR:1:Failed\r\n"); |
556 |
|
557 |
fprintf(stderr, "> %s", pszResult); |
558 |
|
559 |
return lscp_server_result(pConnect, pszResult, strlen(pszResult)); |
560 |
} |
561 |
|
562 |
//////////////////////////////////////////////////////////////////////// |
563 |
|
564 |
|
565 |
void server_usage (void) |
566 |
{ |
567 |
printf("\n %s %s (Build: %s)\n", lscp_server_package(), lscp_server_version(), lscp_server_build()); |
568 |
|
569 |
fputs("\n Available server commands: help, exit, quit, list", stdout); |
570 |
fputs("\n (all else are broadcast verbatim to subscribers)\n\n", stdout); |
571 |
} |
572 |
|
573 |
void server_prompt (void) |
574 |
{ |
575 |
fputs("lscp_server> ", stdout); |
576 |
} |
577 |
|
578 |
int main (int argc, char *argv[] ) |
579 |
{ |
580 |
lscp_server_t *pServer; |
581 |
char szLine[200]; |
582 |
int cchLine; |
583 |
lscp_connect_t *p; |
584 |
|
585 |
#if defined(WIN32) |
586 |
if (WSAStartup(MAKEWORD(1, 1), &_wsaData) != 0) { |
587 |
fprintf(stderr, "lscp_server: WSAStartup failed.\n"); |
588 |
return -1; |
589 |
} |
590 |
#endif |
591 |
|
592 |
srand(time(NULL)); |
593 |
|
594 |
pServer = lscp_server_create(SERVER_PORT, server_callback, NULL); |
595 |
if (pServer == NULL) |
596 |
return -1; |
597 |
|
598 |
server_usage(); |
599 |
server_prompt(); |
600 |
|
601 |
while (fgets(szLine, sizeof(szLine), stdin)) { |
602 |
|
603 |
cchLine = strlen(szLine); |
604 |
while (cchLine > 0 && (szLine[cchLine - 1] == '\n' || szLine[cchLine - 1] == '\r')) |
605 |
cchLine--; |
606 |
szLine[cchLine] = '\0'; |
607 |
|
608 |
if (strcmp(szLine, "exit") == 0 || strcmp(szLine, "quit") == 0) |
609 |
break; |
610 |
else |
611 |
if (strcmp(szLine, "list") == 0) { |
612 |
for (p = pServer->connects.first; p; p = p->next) { |
613 |
printf("client: sock=%d addr=%s port=%d events=0x%04x.\n", |
614 |
p->client.sock, |
615 |
inet_ntoa(p->client.addr.sin_addr), |
616 |
ntohs(p->client.addr.sin_port), |
617 |
(int) p->events |
618 |
); |
619 |
} |
620 |
} |
621 |
else |
622 |
if (cchLine > 0 && strcmp(szLine, "help") != 0) |
623 |
lscp_server_broadcast(pServer, LSCP_EVENT_MISCELLANEOUS, szLine, strlen(szLine)); |
624 |
else |
625 |
server_usage(); |
626 |
|
627 |
server_prompt(); |
628 |
} |
629 |
|
630 |
lscp_server_destroy(pServer); |
631 |
|
632 |
#if defined(WIN32) |
633 |
WSACleanup(); |
634 |
#endif |
635 |
|
636 |
return 0; |
637 |
} |
638 |
|
639 |
// end of example_server.c |