source: roaraudio/roarclients/roarlight.c @ 5961:06e7fd9e4c25

Last change on this file since 5961:06e7fd9e4c25 was 5961:06e7fd9e4c25, checked in by phi, 10 years ago

Updates of copyright and license headers

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