source: roaraudio/roarclients/roarlight.c @ 6067:53fec3e5f6cf

Last change on this file since 6067:53fec3e5f6cf was 6067:53fec3e5f6cf, checked in by phi, 9 years ago

updated copyright headers

File size: 7.4 KB
Line 
1//roarlight.c:
2
3/*
4 *      Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2009-2015
5 *
6 *  This file is part of roarclients a part of RoarAudio,
7 *  a cross-platform sound system for both, home and professional use.
8 *  See README for details.
9 *
10 *  This file is free software; you can redistribute it and/or modify
11 *  it under the terms of the GNU General Public License version 3
12 *  as published by the Free Software Foundation.
13 *
14 *  RoarAudio is distributed in the hope that it will be useful,
15 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 *  GNU General Public License for more details.
18 *
19 *  You should have received a copy of the GNU General Public License
20 *  along with this software; see the file COPYING.  If not, write to
21 *  the Free Software Foundation, 51 Franklin Street, Fifth Floor,
22 *  Boston, MA 02110-1301, USA.
23 *
24 */
25
26#include <roaraudio.h>
27#include <libroarlight/libroarlight.h>
28#include <stdio.h>
29
30#define CONVAR struct roar_connection * con
31
32static struct roar_connection * g_connection;
33static struct roar_vio_calls  * g_stream;
34
35static int __run_argv(int argc, char * argv[]);
36static int __open_and_run_file(const char * file);
37
38void usage (void) {
39 printf("roarlight [OPTIONS]... command [command...]\n");
40
41 printf("\nOptions:\n\n");
42
43 printf("  --server    SERVER    - Set server hostname\n"
44        "  --mixer     MIXERID   - ID of the light mixer to use\n"
45        "  --help                - Show this help\n"
46       );
47
48 printf("\nCommands:\n\n");
49 printf(
50        "  help                    - Show this help\n"
51        "  sleep TIME              - Sleeps for TIME seconds\n"
52        "  sset  chan=val          - Set a DMX Channel\n"
53        "  set   chan=val          - Same as sset\n"
54        "  event EVENT             - Send event EVENT\n"
55        "  @FILE                   - Read commands from FILE or stdin in case of @-\n"
56       );
57}
58
59static int cmd_sset (char * arg) {
60 char * next = arg;
61 char * k, * v;
62 int32_t chan, val;
63 struct roar_roardmx_message mes;
64
65 roar_roardmx_message_new_sset(&mes);
66
67 while (next != NULL) {
68  arg  = next;
69  next = strstr(next, ",");
70  if ( next != NULL ) {
71   *next = 0;
72    next++;
73  }
74
75  k = arg;
76  v = strstr(arg, "=");
77  if ( v == NULL )
78   return -1;
79
80  *v = 0;
81   v++;
82
83  chan = atoi(k);
84  val  = atoi(v);
85//  printf("k='%s'(%i), v='%s'(%i)\n", k, chan, v, val);
86  if ( roar_roardmx_message_add_chanval(&mes, chan, val) == -1 )
87   return -1;
88 }
89
90 if ( roar_roardmx_message_send(&mes, g_stream) == -1 ) {
91  return -1;
92 }
93
94 return 0;
95}
96
97static int cmd_rangeset (char * arg) {
98 char * next = arg;
99 char * k, * v;
100 int32_t start, end, val;
101 struct roar_roardmx_message mes;
102
103 roar_roardmx_message_new_rangeset(&mes);
104
105 while (next != NULL) {
106  arg  = next;
107  next = strstr(next, ",");
108  if ( next != NULL ) {
109   *next = 0;
110    next++;
111  }
112
113  k = arg;
114  v = strstr(arg, "=");
115  if ( v == NULL )
116   return -1;
117
118  *v = 0;
119   v++;
120
121  val  = atoi(v);
122
123  v = strstr(arg, "-");
124  if ( v == NULL ) {
125   start = end = atoi(k);
126  } else {
127  *v = 0;
128   v++;
129   start = atoi(k);
130   end = atoi(v);
131  }
132
133//  printf("k='%s'(%i), v='%s'(%i)\n", k, chan, v, val);
134  if ( roar_roardmx_message_add_rangeval(&mes, start, end, val) == -1 ) {
135   return -1;
136  }
137 }
138
139 if ( roar_roardmx_message_send(&mes, g_stream) == -1 ) {
140  return -1;
141 }
142
143 return 0;
144}
145
146static int cmd_event (char * arg) {
147 struct roar_roardmx_message mes;
148 int event;
149 char * p;
150
151 if ( roar_roardmx_message_new_event(&mes) == -1 )
152  return -1;
153
154 while (arg != NULL) {
155  p = strstr(arg, ",");
156  if ( p != NULL ) {
157   *p = 0;
158    p++;
159  }
160
161  event = roar_roardmx_str2event(arg);
162  if ( event == -1 )
163   return -1;
164
165  roar_roardmx_message_add_event(&mes, event);
166
167  arg = p;
168 }
169
170 if ( roar_roardmx_message_send(&mes, g_stream) == -1 )
171  return -1;
172
173 return 0;
174}
175
176static int __run_argv(int argc, char * argv[]) {
177 char * k;
178 int i;
179
180 for (i = 0; i < argc; i++) {
181  k = argv[i];
182  // cmd is in k
183
184  printf("--- [ %s ] ---\n", k);
185
186  if ( !strcmp(k, "help") ) {
187   usage();
188
189  } else if ( !strcmp(k, "sleep") ) {
190   ROAR_CKHAVEARGS(1);
191   roar_sleep(atoi(argv[++i]));
192
193  } else if ( !strcmp(k, "sset") || !strcmp(k, "set") ) {
194   ROAR_CKHAVEARGS(1);
195   i++;
196   if ( cmd_sset(argv[i]) == -1 ) {
197    fprintf(stderr, "Error: can not set channels\n");
198   } else {
199    printf("channels changed\n");
200   }
201  } else if ( !strcmp(k, "rangeset") ) {
202   ROAR_CKHAVEARGS(1);
203   i++;
204   if ( cmd_rangeset(argv[i]) == -1 ) {
205    fprintf(stderr, "Error: can not set channels\n");
206   } else {
207    printf("channels changed\n");
208   }
209  } else if ( !strcmp(k, "event") ) {
210   ROAR_CKHAVEARGS(1);
211   i++;
212   if ( cmd_event(argv[i]) == -1 ) {
213    fprintf(stderr, "Error: can not send event\n");
214   } else {
215    printf("event send\n");
216   }
217
218  } else if ( *k == '@' ) {
219   __open_and_run_file(k);
220  } else {
221   fprintf(stderr, "Error: invalid command: %s\n", k);
222   return 1;
223  }
224 }
225
226 return 0;
227}
228
229static inline void __strip_space(char ** str) {
230 for (; **str == ' '; (*str)++);
231}
232
233static int __parse_line_and_run(char * line) {
234#define MAX_ARGS 16
235 int argc = 0;
236 char * argv[MAX_ARGS];
237 char * p;
238
239 while (line != NULL) {
240  if ( argc == MAX_ARGS ) {
241   fprintf(stderr, "Error: too many arguments.\n");
242   return 1;
243  }
244
245  p = strstr(line, " ");
246  if ( p != NULL ) {
247   *p = 0;
248    p++;
249   __strip_space(&p);
250  }
251
252  argv[argc++] = line;
253
254  line = p;
255 }
256
257 return __run_argv(argc, argv);
258}
259
260static int __run_file(FILE * file) {
261 char buffer[1024];
262 ssize_t len;
263 int ret;
264
265 while (fgets(buffer, sizeof(buffer), file) != NULL) {
266  len = roar_mm_strlen(buffer);
267  if ( len > 0 && buffer[len-1] == '\n' )
268   buffer[len-1] = 0;
269
270  if ( !len || buffer[0] == 0 )
271   continue;
272
273  ret = __parse_line_and_run(buffer);
274  if ( ret != 0 )
275   return ret;
276 }
277
278 return 0;
279}
280
281static int __open_and_run_file(const char * file) {
282 FILE * input;
283 int ret;
284
285 if ( !strcmp(file, "@-") ) {
286  return __run_file(stdin);
287 } else {
288  input = fopen(file+1, "r");
289  if ( input == NULL ) {
290   fprintf(stderr, "Error: can not open input file: %s\n", file+1);
291   return 1;
292  }
293  ret = __run_file(input);
294  fclose(input);
295  return ret;
296 }
297}
298
299int main (int argc, char * argv[]) {
300 struct roar_connection con;
301 struct roar_vio_calls vio;
302 int    mixer = -1; // -1 = Default
303 const char * server   = NULL;
304 const char * k;
305 int    i;
306 int    ret;
307
308 for (i = 1; i < argc; i++) {
309  k = argv[i];
310
311  if ( !strcmp(k, "--server") || !strcmp(k, "-s") ) {
312   ROAR_CKHAVEARGS(1);
313   server = argv[++i];
314  } else if ( !strcmp(k, "--mixer") ) {
315   ROAR_CKHAVEARGS(1);
316   mixer = atoi(argv[++i]);
317  } else if ( !strcmp(k, "--help") || !strcmp(k, "-h") ) {
318   usage();
319   return 0;
320  } else if ( *k == '-' ) {
321   fprintf(stderr, "Error: unknown argument: %s\n", k);
322   usage();
323   return 1;
324  } else {
325   break;
326  }
327 }
328
329 if ( i == argc ) {
330  fprintf(stderr, "Error: No Commands given\n");
331  return 0; // this is not a fatal error...
332 }
333
334 if ( roar_simple_connect(&con, server, "roarlight") == -1 ) {
335  fprintf(stderr, "Error: Can not connect to server\n");
336  return 1;
337 }
338
339 if ( roar_vio_simple_new_stream_obj(&vio, &con, NULL,
340                                     ROAR_RATE_DEFAULT, ROAR_CHANNELS_DEFAULT, ROAR_BITS_DEFAULT,
341                                     ROAR_CODEC_ROARDMX, ROAR_DIR_LIGHT_IN, mixer) == -1 ) {
342 }
343
344 g_connection = &con;
345 g_stream = &vio;
346
347 ret = __run_argv(argc - i, argv + i);
348
349 // try to flush all data:
350
351 roar_vio_sync(&vio);
352 roar_vio_close(&vio);
353 roar_usleep(100);
354
355 roar_disconnect(&con);
356
357 return ret;
358}
359
360//ll
Note: See TracBrowser for help on using the repository browser.