source: roaraudio/roarclients/roarlight.c @ 5933:e2de7f786235

Last change on this file since 5933:e2de7f786235 was 5933:e2de7f786235, checked in by phi, 11 years ago

allow reading commands from file

File size: 6.3 KB
Line 
1//roarlight.c:
2
3/*
4 *      Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2009-2013
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       );
56}
57
58static int cmd_sset (char * arg) {
59 char * next = arg;
60 char * k, * v;
61 int32_t chan, val;
62 struct roar_roardmx_message mes;
63
64 roar_roardmx_message_new_sset(&mes);
65
66 while (next != NULL) {
67  arg  = next;
68  next = strstr(next, ",");
69  if ( next != NULL ) {
70   *next = 0;
71    next++;
72  }
73
74  k = arg;
75  v = strstr(arg, "=");
76  if ( v == NULL )
77   return -1;
78
79  *v = 0;
80   v++;
81
82  chan = atoi(k);
83  val  = atoi(v);
84//  printf("k='%s'(%i), v='%s'(%i)\n", k, chan, v, val);
85  if ( roar_roardmx_message_add_chanval(&mes, chan, val) == -1 )
86   return -1;
87 }
88
89 if ( roar_roardmx_message_send(&mes, g_stream) == -1 ) {
90  return -1;
91 }
92
93 return 0;
94}
95
96static int cmd_event (char * arg) {
97 struct roar_roardmx_message mes;
98 int event;
99 char * p;
100
101 if ( roar_roardmx_message_new_event(&mes) == -1 )
102  return -1;
103
104 while (arg != NULL) {
105  p = strstr(arg, ",");
106  if ( p != NULL ) {
107   *p = 0;
108    p++;
109  }
110
111  event = roar_roardmx_str2event(arg);
112  if ( event == -1 )
113   return -1;
114
115  roar_roardmx_message_add_event(&mes, event);
116
117  arg = p;
118 }
119
120 if ( roar_roardmx_message_send(&mes, g_stream) == -1 )
121  return -1;
122
123 return 0;
124}
125
126static int __run_argv(int argc, char * argv[]) {
127 char * k;
128 int i;
129
130 for (i = 0; i < argc; i++) {
131  k = argv[i];
132  // cmd is in k
133
134  printf("--- [ %s ] ---\n", k);
135
136  if ( !strcmp(k, "help") ) {
137   usage();
138
139  } else if ( !strcmp(k, "sleep") ) {
140   roar_sleep(atoi(argv[++i]));
141
142  } else if ( !strcmp(k, "sset") || !strcmp(k, "set") ) {
143   i++;
144   if ( cmd_sset(argv[i]) == -1 ) {
145    fprintf(stderr, "Error: can not set channels\n");
146   } else {
147    printf("channels changed\n");
148   }
149  } else if ( !strcmp(k, "event") ) {
150   i++;
151   if ( cmd_event(argv[i]) == -1 ) {
152    fprintf(stderr, "Error: can not send event\n");
153   } else {
154    printf("event send\n");
155   }
156
157  } else if ( *k == '@' ) {
158   __open_and_run_file(k);
159  } else {
160   fprintf(stderr, "Error: invalid command: %s\n", k);
161   return 1;
162  }
163 }
164
165 return 0;
166}
167
168static inline void __strip_space(char ** str) {
169 for (; **str == ' '; (*str)++);
170}
171
172static int __parse_line_and_run(char * line) {
173#define MAX_ARGS 16
174 int argc = 0;
175 char * argv[MAX_ARGS];
176 char * p;
177
178 while (line != NULL) {
179  if ( argc == MAX_ARGS ) {
180   fprintf(stderr, "Error: too many arguments.\n");
181   return 1;
182  }
183
184  p = strstr(line, " ");
185  if ( p != NULL ) {
186   *p = 0;
187    p++;
188   __strip_space(&p);
189  }
190
191  argv[argc++] = line;
192
193  line = p;
194 }
195
196 return __run_argv(argc, argv);
197}
198
199static int __run_file(FILE * file) {
200 char buffer[1024];
201 ssize_t len;
202 int ret;
203
204 while (fgets(buffer, sizeof(buffer), file) != NULL) {
205  len = roar_mm_strlen(buffer);
206  if ( len > 0 && buffer[len-1] == '\n' )
207   buffer[len-1] = 0;
208
209  if ( !len || buffer[0] == 0 )
210   continue;
211
212  ret = __parse_line_and_run(buffer);
213  if ( ret != 0 )
214   return ret;
215 }
216
217 return 0;
218}
219
220static int __open_and_run_file(const char * file) {
221 FILE * input;
222 int ret;
223
224 if ( !strcmp(file, "@-") ) {
225  return __run_file(stdin);
226 } else {
227  input = fopen(file+1, "r");
228  if ( input == NULL ) {
229   fprintf(stderr, "Error: can not open input file: %s\n", file+1);
230   return 1;
231  }
232  ret = __run_file(input);
233  fclose(input);
234  return ret;
235 }
236}
237
238int main (int argc, char * argv[]) {
239 struct roar_connection con;
240 struct roar_vio_calls vio;
241 int    mixer = -1; // -1 = Default
242 const char * server   = NULL;
243 const char * k;
244 int    i;
245 int    ret;
246
247 for (i = 1; i < argc; i++) {
248  k = argv[i];
249
250  if ( !strcmp(k, "--server") || !strcmp(k, "-s") ) {
251   server = argv[++i];
252  } else if ( !strcmp(k, "--mixer") ) {
253   mixer = atoi(argv[++i]);
254  } else if ( !strcmp(k, "--codec") ) {
255
256  } else if ( !strcmp(k, "--help") || !strcmp(k, "-h") ) {
257   usage();
258   return 0;
259  } else if ( *k == '-' ) {
260   fprintf(stderr, "Error: unknown argument: %s\n", k);
261   usage();
262   return 1;
263  } else {
264   break;
265  }
266 }
267
268 if ( i == argc ) {
269  fprintf(stderr, "Error: No Commands given\n");
270  return 0; // this is not a fatal error...
271 }
272
273 if ( roar_simple_connect(&con, server, "roarlight") == -1 ) {
274  fprintf(stderr, "Error: Can not connect to server\n");
275  return 1;
276 }
277
278 if ( roar_vio_simple_new_stream_obj(&vio, &con, NULL,
279                                     ROAR_RATE_DEFAULT, ROAR_CHANNELS_DEFAULT, ROAR_BITS_DEFAULT,
280                                     ROAR_CODEC_ROARDMX, ROAR_DIR_LIGHT_IN, mixer) == -1 ) {
281 }
282
283 g_connection = &con;
284 g_stream = &vio;
285
286 ret = __run_argv(argc - i, argv + i);
287
288 // try to flush all data:
289
290 roar_vio_sync(&vio);
291 roar_vio_close(&vio);
292 roar_usleep(100);
293
294 roar_disconnect(&con);
295
296 return ret;
297}
298
299//ll
Note: See TracBrowser for help on using the repository browser.