/[svn]/liblscp/trunk/examples/example_client.c
ViewVC logotype

Contents of /liblscp/trunk/examples/example_client.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 187 - (show annotations) (download)
Wed Jul 7 23:41:07 2004 UTC (15 years, 8 months ago) by capela
File MIME type: text/plain
File size: 15772 byte(s)
* lscp_isplit_create() gets wrong zero item if string list is empty: fixed.
* lscp_param_concat() was quitting prematurely when deplist is null: fixed.
* Lil'bit more informative example_client test run; new teststep command.

1 // example_client.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 "lscp/client.h"
23 #include "lscp/device.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 client_callback ( lscp_client_t *pClient, lscp_event_t event, const char *pchData, int cchData, void *pvData )
36 {
37 lscp_status_t ret = LSCP_FAILED;
38
39 char *pszData = (char *) malloc(cchData + 1);
40 if (pszData) {
41 memcpy(pszData, pchData, cchData);
42 pszData[cchData] = (char) 0;
43 printf("client_callback: event=%s (0x%04x) [%s]\n", lscp_event_to_text(event), (int) event, pszData);
44 free(pszData);
45 ret = LSCP_OK;
46 }
47
48 return ret;
49 }
50
51 ////////////////////////////////////////////////////////////////////////
52
53 void client_test_int ( int i )
54 {
55 printf("%d\n", i);
56 }
57
58 void client_test_status ( lscp_status_t s )
59 {
60 const char *pszStatus;
61
62 switch (s) {
63 case LSCP_OK: pszStatus = "OK"; break;
64 case LSCP_FAILED: pszStatus = "FAILED"; break;
65 case LSCP_ERROR: pszStatus = "ERROR"; break;
66 case LSCP_WARNING: pszStatus = "WARNING"; break;
67 case LSCP_TIMEOUT: pszStatus = "TIMEOUT"; break;
68 case LSCP_QUIT: pszStatus = "QUIT"; break;
69 default: pszStatus = "NONE"; break;
70 }
71
72 printf("%s\n", pszStatus);
73 }
74
75
76 void client_test_isplit ( int *piSplit )
77 {
78 int i;
79
80 printf("{");
81 for (i = 0; piSplit && piSplit[i] >= 0; i++) {
82 if (i > 0)
83 printf(",");
84 printf(" %d", piSplit[i]);
85 }
86 printf(" }\n");
87 }
88
89 void client_test_szsplit ( char **ppszSplit )
90 {
91 int i;
92
93 printf("{");
94 for (i = 0; ppszSplit && ppszSplit[i]; i++) {
95 if (i > 0)
96 printf(",");
97 printf(" %s", ppszSplit[i]);
98 }
99 printf(" }\n");
100 }
101
102 void client_test_params ( lscp_param_t *pParams )
103 {
104 int i;
105
106 printf("{");
107 for (i = 0; pParams && pParams[i].key; i++) {
108 if (i > 0)
109 printf(",");
110 printf(" %s='%s'", pParams[i].key, pParams[i].value);
111 }
112 printf(" }\n");
113 }
114
115 void client_test_param_info ( lscp_param_info_t *pParamInfo )
116 {
117 const char *pszType;
118
119 if (pParamInfo == NULL) {
120 printf("(nil)\n");
121 return;
122 }
123 switch (pParamInfo->type) {
124 case LSCP_TYPE_BOOL: pszType = "BOOL"; break;
125 case LSCP_TYPE_INT: pszType = "INT"; break;
126 case LSCP_TYPE_FLOAT: pszType = "FLOAT"; break;
127 case LSCP_TYPE_STRING: pszType = "STRING"; break;
128 default: pszType = "NONE"; break;
129 }
130 printf("{\n");
131 printf(" param_info.type = %d (%s)\n", (int) pParamInfo->type, pszType);
132 printf(" param_info.description = %s\n", pParamInfo->description);
133 printf(" param_info.mandatory = %d\n", pParamInfo->mandatory);
134 printf(" param_info.fix = %d\n", pParamInfo->fix);
135 printf(" param_info.multiplicity = %d\n", pParamInfo->multiplicity);
136 printf(" param_info.depends = "); client_test_szsplit(pParamInfo->depends);
137 printf(" param_info.defaultv = %s\n", pParamInfo->defaultv);
138 printf(" param_info.range_min = %s\n", pParamInfo->range_min);
139 printf(" param_info.range_max = %s\n", pParamInfo->range_max);
140 printf(" param_info.possibilities = "); client_test_szsplit(pParamInfo->possibilities);
141 printf(" }\n");
142 }
143
144 void client_test_driver_info ( lscp_driver_info_t *pDriverInfo )
145 {
146 if (pDriverInfo == NULL) {
147 printf("(nil)\n");
148 return;
149 }
150 printf("{\n");
151 printf(" driver_info.description = %s\n", pDriverInfo->description);
152 printf(" driver_info.version = %s\n", pDriverInfo->version);
153 printf(" driver_info.parameters = "); client_test_szsplit(pDriverInfo->parameters);
154 printf(" }\n");
155 }
156
157 void client_test_device_info ( lscp_device_info_t *pDeviceInfo )
158 {
159 if (pDeviceInfo == NULL) {
160 printf("(nil)\n");
161 return;
162 }
163 printf("{\n");
164 printf(" device_info.driver = %s\n", pDeviceInfo->driver);
165 printf(" device_info.params = "); client_test_params(pDeviceInfo->params);
166 printf(" }\n");
167 }
168
169 void client_test_device_port_info ( lscp_device_port_info_t *pDevicePortInfo )
170 {
171 if (pDevicePortInfo == NULL) {
172 printf("(nil)\n");
173 return;
174 }
175 printf("{\n");
176 printf(" device_port_info.name = %s\n", pDevicePortInfo->name);
177 printf(" device_port_info.params = "); client_test_params(pDevicePortInfo->params);
178 printf(" }\n");
179 }
180
181 void client_test_engine_info ( lscp_engine_info_t *pEngineInfo )
182 {
183 if (pEngineInfo == NULL) {
184 printf("(nil)\n");
185 return;
186 }
187 printf("{\n");
188 printf(" engine_info.description = %s\n", pEngineInfo->description);
189 printf(" engine_info.version = %s\n", pEngineInfo->version);
190 printf(" }\n");
191 }
192
193 void client_test_channel_info ( lscp_channel_info_t *pChannelInfo )
194 {
195 if (pChannelInfo == NULL) {
196 printf("(nil)\n");
197 return;
198 }
199 printf("{\n");
200 printf(" channel_info.engine_name = %s\n", pChannelInfo->engine_name);
201 printf(" channel_info.audio_device = %d\n", pChannelInfo->audio_device);
202 printf(" channel_info.audio_channels = %d\n", pChannelInfo->audio_channels);
203 printf(" channel_info.audio_routing = "); client_test_szsplit(pChannelInfo->audio_routing);
204 printf(" channel_info.instrument_file = %s\n", pChannelInfo->instrument_file);
205 printf(" channel_info.instrument_nr = %d\n", pChannelInfo->instrument_nr);
206 printf(" channel_info.instrument_status = %d\n", pChannelInfo->instrument_status);
207 printf(" channel_info.midi_device = %d\n", pChannelInfo->midi_device);
208 printf(" channel_info.midi_port = %d\n", pChannelInfo->midi_port);
209 printf(" channel_info.midi_channel = %d\n", pChannelInfo->midi_channel);
210 printf(" channel_info.volume = %g\n", pChannelInfo->volume);
211 printf(" }\n");
212 }
213
214 void client_test_buffer_fill ( lscp_buffer_fill_t *pBufferFill )
215 {
216 printf("{ <%p> }\n", pBufferFill);
217 }
218
219 ////////////////////////////////////////////////////////////////////////
220
221 static int g_teststep = 0;
222
223 void client_test_start ( clock_t *pclk ) { *pclk = clock(); }
224 float client_test_elapsed ( clock_t *pclk ) { return (float) ((long) clock() - *pclk) / (float) CLOCKS_PER_SEC; }
225
226 typedef int * isplit;
227 typedef char ** szsplit;
228 typedef lscp_status_t status;
229 typedef lscp_driver_info_t * driver_info;
230 typedef lscp_device_info_t * device_info;
231 typedef lscp_device_port_info_t * device_port_info;
232 typedef lscp_param_info_t * param_info;
233 typedef lscp_engine_info_t * engine_info;
234 typedef lscp_channel_info_t * channel_info;
235 typedef lscp_buffer_fill_t * buffer_fill;
236
237 #define CLIENT_TEST(p, t, x) { clock_t c; void *v; \
238 printf("\n" #x ":\n"); client_test_start(&c); v = (void *) (x); \
239 printf(" elapsed=%gs errno=%d result='%s...' ret=", \
240 client_test_elapsed(&c), \
241 lscp_client_get_errno(p), \
242 lscp_client_get_result(p)); \
243 client_test_##t((t)(v)); \
244 if (g_teststep) getchar(); }
245
246
247 void client_test ( lscp_client_t *pClient )
248 {
249 const char **ppszAudioDrivers, **ppszMidiDrivers, **ppszEngines;
250 const char *pszAudioDriver, *pszMidiDriver, *pszEngine;
251 int iAudioDriver, iMidiDriver, iEngine;
252 int iAudioDevice, iMidiDevice;
253 int iSamplerChannel;
254
255 CLIENT_TEST(pClient, szsplit, ppszAudioDrivers = lscp_get_available_audio_drivers(pClient));
256 if (ppszAudioDrivers == NULL) {
257 fprintf(stderr, "client_test: No audio drivers available.\n");
258 return;
259 }
260
261 CLIENT_TEST(pClient, szsplit, ppszMidiDrivers = lscp_get_available_midi_drivers(pClient));
262 if (ppszMidiDrivers == NULL) {
263 fprintf(stderr, "client_test: No MIDI drivers available.\n");
264 return;
265 }
266
267 CLIENT_TEST(pClient, szsplit, ppszEngines = lscp_get_available_engines(pClient));
268 if (ppszEngines == NULL) {
269 fprintf(stderr, "client_test: No engines available.\n");
270 return;
271 }
272
273 CLIENT_TEST(pClient, int, lscp_get_audio_devices(pClient));
274 CLIENT_TEST(pClient, isplit, lscp_list_audio_devices(pClient));
275
276 CLIENT_TEST(pClient, int, lscp_get_midi_devices(pClient));
277 CLIENT_TEST(pClient, isplit, lscp_list_midi_devices(pClient));
278
279 for (iAudioDriver = 0; ppszAudioDrivers[iAudioDriver]; iAudioDriver++) {
280 pszAudioDriver = ppszAudioDrivers[iAudioDriver];
281 printf("\n--- pszAudioDriver=\"%s\" ---\n", pszAudioDriver);
282 CLIENT_TEST(pClient, driver_info, lscp_get_audio_driver_info(pClient, pszAudioDriver));
283 CLIENT_TEST(pClient, param_info, lscp_get_audio_driver_param_info(pClient, pszAudioDriver, "active", NULL));
284 CLIENT_TEST(pClient, int, iAudioDevice = lscp_create_audio_device(pClient, pszAudioDriver, NULL));
285 CLIENT_TEST(pClient, device_info, lscp_get_audio_device_info(pClient, iAudioDevice));
286 for (iMidiDriver = 0; ppszMidiDrivers[iMidiDriver]; iMidiDriver++) {
287 pszMidiDriver = ppszMidiDrivers[iMidiDriver];
288 printf("\n--- pszMidiDriver=\"%s\" ---\n", pszMidiDriver);
289 CLIENT_TEST(pClient, driver_info, lscp_get_midi_driver_info(pClient, pszMidiDriver));
290 CLIENT_TEST(pClient, param_info, lscp_get_midi_driver_param_info(pClient, pszMidiDriver, "active", NULL));
291 CLIENT_TEST(pClient, int, iMidiDevice = lscp_create_midi_device(pClient, pszMidiDriver, NULL));
292 CLIENT_TEST(pClient, device_info, lscp_get_midi_device_info(pClient, iMidiDevice));
293 for (iEngine = 0; ppszEngines[iEngine]; iEngine++) {
294 pszEngine = ppszEngines[iEngine];
295 printf("\n--- pszEngine=\"%s\" ---\n", pszEngine);
296 CLIENT_TEST(pClient, engine_info, lscp_get_engine_info(pClient, pszEngine));
297 CLIENT_TEST(pClient, int, lscp_get_channels(pClient));
298 CLIENT_TEST(pClient, isplit, lscp_list_channels(pClient));
299 CLIENT_TEST(pClient, int, iSamplerChannel = lscp_add_channel(pClient));
300 CLIENT_TEST(pClient, channel_info, lscp_get_channel_info(pClient, iSamplerChannel));
301 CLIENT_TEST(pClient, status, lscp_load_engine(pClient, pszEngine, iSamplerChannel));
302 CLIENT_TEST(pClient, status, lscp_load_instrument(pClient, "DefaultInstrument.gig", 0, iSamplerChannel));
303 CLIENT_TEST(pClient, int, lscp_get_channel_voice_count(pClient, iSamplerChannel));
304 CLIENT_TEST(pClient, int, lscp_get_channel_stream_count(pClient, iSamplerChannel));
305 CLIENT_TEST(pClient, int, lscp_get_channel_stream_usage(pClient, iSamplerChannel));
306 CLIENT_TEST(pClient, buffer_fill, lscp_get_channel_buffer_fill(pClient, LSCP_USAGE_BYTES, iSamplerChannel));
307 CLIENT_TEST(pClient, buffer_fill, lscp_get_channel_buffer_fill(pClient, LSCP_USAGE_PERCENTAGE, iSamplerChannel));
308 CLIENT_TEST(pClient, status, lscp_set_channel_audio_type(pClient, iSamplerChannel, pszAudioDriver));
309 CLIENT_TEST(pClient, status, lscp_set_channel_audio_device(pClient, iSamplerChannel, 0));
310 CLIENT_TEST(pClient, status, lscp_set_channel_audio_channel(pClient, iSamplerChannel, 0, 1));
311 CLIENT_TEST(pClient, status, lscp_set_channel_midi_type(pClient, iSamplerChannel, pszMidiDriver));
312 CLIENT_TEST(pClient, status, lscp_set_channel_midi_device(pClient, iSamplerChannel, 0));
313 CLIENT_TEST(pClient, status, lscp_set_channel_midi_channel(pClient, iSamplerChannel, 0));
314 CLIENT_TEST(pClient, status, lscp_set_channel_midi_port(pClient, iSamplerChannel, 0));
315 CLIENT_TEST(pClient, status, lscp_set_channel_volume(pClient, iSamplerChannel, 0.5));
316 CLIENT_TEST(pClient, channel_info, lscp_get_channel_info(pClient, iSamplerChannel));
317 CLIENT_TEST(pClient, status, lscp_reset_channel(pClient, iSamplerChannel));
318 CLIENT_TEST(pClient, status, lscp_remove_channel(pClient, iSamplerChannel));
319 }
320 CLIENT_TEST(pClient, status, lscp_destroy_midi_device(pClient, iMidiDevice));
321 }
322 CLIENT_TEST(pClient, status, lscp_destroy_audio_device(pClient, iAudioDevice));
323 }
324
325 }
326
327 ////////////////////////////////////////////////////////////////////////
328
329 void client_usage (void)
330 {
331 printf("\n %s %s (Build: %s)\n", lscp_client_package(), lscp_client_version(), lscp_client_build());
332
333 fputs("\n Available client commands: help, test, exit, quit, subscribe, unsubscribe", stdout);
334 fputs("\n (all else are sent verbatim to server)\n\n", stdout);
335
336 }
337
338 void client_prompt (void)
339 {
340 fputs("lscp_client> ", stdout);
341 }
342
343 int main (int argc, char *argv[] )
344 {
345 lscp_client_t *pClient;
346 char *pszHost = "localhost";
347 char szLine[1024];
348 int cchLine;
349 lscp_status_t ret;
350
351 #if defined(WIN32)
352 if (WSAStartup(MAKEWORD(1, 1), &_wsaData) != 0) {
353 fprintf(stderr, "lscp_client: WSAStartup failed.\n");
354 return -1;
355 }
356 #endif
357
358 if (argc > 1)
359 pszHost = argv[1];
360
361 pClient = lscp_client_create(pszHost, SERVER_PORT, client_callback, NULL);
362 if (pClient == NULL)
363 return -1;
364
365 client_usage();
366 client_prompt();
367
368 while (fgets(szLine, sizeof(szLine) - 3, stdin)) {
369
370 cchLine = strlen(szLine);
371 while (cchLine > 0 && (szLine[cchLine - 1] == '\n' || szLine[cchLine - 1] == '\r'))
372 cchLine--;
373 szLine[cchLine] = '\0';
374
375 if (strcmp(szLine, "exit") == 0 || strcmp(szLine, "quit") == 0)
376 break;
377 else
378 if (strcmp(szLine, "subscribe") == 0)
379 lscp_client_subscribe(pClient, LSCP_EVENT_MISCELLANEOUS);
380 else
381 if (strcmp(szLine, "unsubscribe") == 0)
382 lscp_client_unsubscribe(pClient, LSCP_EVENT_MISCELLANEOUS);
383 else
384 if (strcmp(szLine, "test") == 0)
385 client_test(pClient);
386 else
387 if (strcmp(szLine, "teststep") == 0) {
388 g_teststep = 1;
389 client_test(pClient);
390 g_teststep = 0;
391 }
392 else
393 if (cchLine > 0 && strcmp(szLine, "help") != 0) {
394 szLine[cchLine++] = '\r';
395 szLine[cchLine++] = '\n';
396 szLine[cchLine] = '\0';
397 ret = lscp_client_query(pClient, szLine);
398 printf("%s\n(errno = %d)\n", lscp_client_get_result(pClient), lscp_client_get_errno(pClient));
399 if (ret == LSCP_QUIT)
400 break;
401 }
402 else client_usage();
403
404 client_prompt();
405 }
406
407 lscp_client_destroy(pClient);
408
409 #if defined(WIN32)
410 WSACleanup();
411 #endif
412
413 return 0;
414 }
415
416 // end of example_client.c

  ViewVC Help
Powered by ViewVC