source: roaraudio/roarclients/roarlight.c @ 5929:e3e67f000dce

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

improve inner structure

File size: 4.2 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
29#define CONVAR struct roar_connection * con
30
31static struct roar_connection * g_connection;
32static struct roar_vio_calls  * g_stream;
33
34static int __run_argv(int argc, char * argv[]);
35
36void usage (void) {
37 printf("roarlight [OPTIONS]... command [command...]\n");
38
39 printf("\nOptions:\n\n");
40
41 printf("  --server    SERVER    - Set server hostname\n"
42        "  --mixer     MIXERID   - ID of the light mixer to use\n"
43        "  --help                - Show this help\n"
44       );
45
46 printf("\nCommands:\n\n");
47 printf(
48        "  help                    - Show this help\n"
49        "  sleep TIME              - Sleeps for TIME seconds\n"
50        "  set   chan=val          - Set a DMX Channel\n"
51       );
52}
53
54int cmd_set (char * arg) {
55 char * next = arg;
56 char * k, * v;
57 int32_t chan, val;
58 struct roar_roardmx_message mes;
59
60 roar_roardmx_message_new_sset(&mes);
61
62 while (next != NULL) {
63  arg  = next;
64  next = strstr(next, ",");
65  if ( next != NULL ) {
66   *next = 0;
67    next++;
68  }
69
70  k = arg;
71  v = strstr(arg, "=");
72  if ( v == NULL )
73   return -1;
74
75  *v = 0;
76   v++;
77
78  chan = atoi(k);
79  val  = atoi(v);
80//  printf("k='%s'(%i), v='%s'(%i)\n", k, chan, v, val);
81  if ( roar_roardmx_message_add_chanval(&mes, chan, val) == -1 )
82   return -1;
83 }
84
85 if ( roar_roardmx_message_send(&mes, g_stream) == -1 ) {
86  return -1;
87 }
88
89 return 0;
90}
91
92static int __run_argv(int argc, char * argv[]) {
93 char * k;
94 int i;
95
96 for (i = 0; i < argc; i++) {
97  k = argv[i];
98  // cmd is in k
99
100  printf("--- [ %s ] ---\n", k);
101
102  if ( !strcmp(k, "help") ) {
103   usage();
104
105  } else if ( !strcmp(k, "sleep") ) {
106   roar_sleep(atoi(argv[++i]));
107
108  } else if ( !strcmp(k, "set") ) {
109   i++;
110   if ( cmd_set(argv[i]) == -1 ) {
111    fprintf(stderr, "Error: can not set channels\n");
112   } else {
113    printf("channels changed\n");
114   }
115
116  } else {
117   fprintf(stderr, "Error: invalid command: %s\n", k);
118   return 1;
119  }
120 }
121
122 return 0;
123}
124
125int main (int argc, char * argv[]) {
126 struct roar_connection con;
127 struct roar_vio_calls vio;
128 int    mixer = -1; // -1 = Default
129 const char * server   = NULL;
130 const char * k;
131 int    i;
132 int    ret;
133
134 for (i = 1; i < argc; i++) {
135  k = argv[i];
136
137  if ( !strcmp(k, "--server") || !strcmp(k, "-s") ) {
138   server = argv[++i];
139  } else if ( !strcmp(k, "--mixer") ) {
140   mixer = atoi(argv[++i]);
141  } else if ( !strcmp(k, "--codec") ) {
142
143  } else if ( !strcmp(k, "--help") || !strcmp(k, "-h") ) {
144   usage();
145   return 0;
146  } else if ( *k == '-' ) {
147   fprintf(stderr, "Error: unknown argument: %s\n", k);
148   usage();
149   return 1;
150  } else {
151   break;
152  }
153 }
154
155 if ( i == argc ) {
156  fprintf(stderr, "Error: No Commands given\n");
157  return 0; // this is not a fatal error...
158 }
159
160 if ( roar_simple_connect(&con, server, "roarlight") == -1 ) {
161  fprintf(stderr, "Error: Can not connect to server\n");
162  return 1;
163 }
164
165 if ( roar_vio_simple_new_stream_obj(&vio, &con, NULL,
166                                     ROAR_RATE_DEFAULT, ROAR_CHANNELS_DEFAULT, ROAR_BITS_DEFAULT,
167                                     ROAR_CODEC_ROARDMX, ROAR_DIR_LIGHT_IN, mixer) == -1 ) {
168 }
169
170 g_connection = &con;
171 g_stream = &vio;
172
173 ret = __run_argv(argc - i, argv + i);
174
175 // try to flush all data:
176
177 roar_vio_sync(&vio);
178 roar_vio_close(&vio);
179 roar_usleep(100);
180
181 roar_disconnect(&con);
182
183 return ret;
184}
185
186//ll
Note: See TracBrowser for help on using the repository browser.