1 | //roarlight.c: |
---|
2 | |
---|
3 | /* |
---|
4 | * Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2009 |
---|
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, 675 Mass Ave, Cambridge, MA 02139, USA. |
---|
22 | * |
---|
23 | */ |
---|
24 | |
---|
25 | #include <roaraudio.h> |
---|
26 | #include <libroarlight/libroarlight.h> |
---|
27 | |
---|
28 | #define CONVAR struct roar_connection * con |
---|
29 | |
---|
30 | void usage (void) { |
---|
31 | printf("roarlight [OPTIONS]... command [command...]\n"); |
---|
32 | |
---|
33 | printf("\nOptions:\n\n"); |
---|
34 | |
---|
35 | printf(" --server SERVER - Set server hostname\n" |
---|
36 | " --help - Show this help\n" |
---|
37 | ); |
---|
38 | |
---|
39 | printf("\nCommands:\n\n"); |
---|
40 | printf( |
---|
41 | " help - Show this help\n" |
---|
42 | " sleep TIME - Sleeps for TIME seconds\n" |
---|
43 | " set chan=val - Set a DMX Channel\n" |
---|
44 | ); |
---|
45 | } |
---|
46 | |
---|
47 | int cmd_set (CONVAR, char * arg) { |
---|
48 | char * next = arg; |
---|
49 | char * k, * v; |
---|
50 | int32_t chan, val; |
---|
51 | struct roar_roardmx_message mes; |
---|
52 | int fh; //TODO: we should use pure VIO here |
---|
53 | struct roar_vio_calls vio; |
---|
54 | |
---|
55 | roar_roardmx_message_new_sset(&mes); |
---|
56 | |
---|
57 | while (next != NULL) { |
---|
58 | arg = next; |
---|
59 | next = strstr(next, ","); |
---|
60 | if ( next != NULL ) { |
---|
61 | *next = 0; |
---|
62 | next++; |
---|
63 | } |
---|
64 | |
---|
65 | k = arg; |
---|
66 | v = strstr(arg, "="); |
---|
67 | if ( v == NULL ) |
---|
68 | return -1; |
---|
69 | |
---|
70 | *v = 0; |
---|
71 | v++; |
---|
72 | |
---|
73 | chan = atoi(k); |
---|
74 | val = atoi(v); |
---|
75 | // printf("k='%s'(%i), v='%s'(%i)\n", k, chan, v, val); |
---|
76 | if ( roar_roardmx_message_add_chanval(&mes, chan, val) == -1 ) |
---|
77 | return -1; |
---|
78 | } |
---|
79 | |
---|
80 | if ( (fh = roar_simple_new_stream(con, |
---|
81 | ROAR_RATE_DEFAULT, ROAR_CHANNELS_DEFAULT, ROAR_BITS_DEFAULT, |
---|
82 | ROAR_CODEC_ROARDMX, ROAR_DIR_LIGHT_IN |
---|
83 | )) == -1 ) |
---|
84 | return -1; |
---|
85 | |
---|
86 | if ( roar_vio_open_fh_socket(&vio, fh) == -1 ) { |
---|
87 | close(fh); |
---|
88 | return -1; |
---|
89 | } |
---|
90 | |
---|
91 | if ( roar_roardmx_message_send(&mes, &vio) == -1 ) { |
---|
92 | roar_vio_close(&vio); |
---|
93 | return -1; |
---|
94 | } |
---|
95 | |
---|
96 | roar_vio_close(&vio); |
---|
97 | |
---|
98 | return 0; |
---|
99 | } |
---|
100 | |
---|
101 | int main (int argc, char * argv[]) { |
---|
102 | char * server = NULL; |
---|
103 | char * k; |
---|
104 | int i; |
---|
105 | struct roar_connection con; |
---|
106 | |
---|
107 | for (i = 1; i < argc; i++) { |
---|
108 | k = argv[i]; |
---|
109 | |
---|
110 | if ( !strcmp(k, "--server") || !strcmp(k, "-s") ) { |
---|
111 | server = argv[++i]; |
---|
112 | } else if ( !strcmp(k, "--codec") ) { |
---|
113 | |
---|
114 | } else if ( !strcmp(k, "--help") || !strcmp(k, "-h") ) { |
---|
115 | usage(); |
---|
116 | return 0; |
---|
117 | } else if ( *k == '-' ) { |
---|
118 | fprintf(stderr, "Error: unknown argument: %s\n", k); |
---|
119 | usage(); |
---|
120 | return 1; |
---|
121 | } else { |
---|
122 | break; |
---|
123 | } |
---|
124 | } |
---|
125 | |
---|
126 | if ( roar_connect(&con, server) == -1 ) { |
---|
127 | fprintf(stderr, "Error: Can not connect to server\n"); |
---|
128 | return 1; |
---|
129 | } |
---|
130 | |
---|
131 | if ( roar_identify(&con, "roarlight") == -1 ) { |
---|
132 | fprintf(stderr, "Error: Can not identify to server\n"); |
---|
133 | roar_disconnect(&con); |
---|
134 | return 1; |
---|
135 | } |
---|
136 | |
---|
137 | if ( i == argc ) { |
---|
138 | fprintf(stderr, "Error: No Commands given\n"); |
---|
139 | return 0; // this is not a fatal error... |
---|
140 | } |
---|
141 | |
---|
142 | for (; i < argc; i++) { |
---|
143 | k = argv[i]; |
---|
144 | // cmd is in k |
---|
145 | |
---|
146 | printf("--- [ %s ] ---\n", k); |
---|
147 | |
---|
148 | if ( !strcmp(k, "help") ) { |
---|
149 | usage(); |
---|
150 | |
---|
151 | } else if ( !strcmp(k, "sleep") ) { |
---|
152 | sleep(atoi(argv[++i])); |
---|
153 | |
---|
154 | } else if ( !strcmp(k, "set") ) { |
---|
155 | i++; |
---|
156 | if ( cmd_set(&con, argv[i]) == -1 ) { |
---|
157 | fprintf(stderr, "Error: can not set channels\n"); |
---|
158 | } else { |
---|
159 | printf("channels changed\n"); |
---|
160 | } |
---|
161 | |
---|
162 | } else { |
---|
163 | fprintf(stderr, "Error: invalid command: %s\n", k); |
---|
164 | } |
---|
165 | } |
---|
166 | |
---|
167 | roar_disconnect(&con); |
---|
168 | |
---|
169 | return 0; |
---|
170 | } |
---|
171 | |
---|
172 | //ll |
---|