source: roaraudio/roarclients/roarlight.c @ 5940:1e86fff9251a

Last change on this file since 5940:1e86fff9251a was 5934:29b946216873, checked in by phi, 11 years ago

updated docs

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