[2046] | 1 | //roarlight.c: |
---|
| 2 | |
---|
| 3 | /* |
---|
[5823] | 4 | * Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2009-2013 |
---|
[2046] | 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 |
---|
[3517] | 21 | * the Free Software Foundation, 51 Franklin Street, Fifth Floor, |
---|
| 22 | * Boston, MA 02110-1301, USA. |
---|
[2046] | 23 | * |
---|
| 24 | */ |
---|
| 25 | |
---|
| 26 | #include <roaraudio.h> |
---|
| 27 | #include <libroarlight/libroarlight.h> |
---|
| 28 | |
---|
| 29 | #define CONVAR struct roar_connection * con |
---|
| 30 | |
---|
[5929] | 31 | static struct roar_connection * g_connection; |
---|
| 32 | static struct roar_vio_calls * g_stream; |
---|
| 33 | |
---|
| 34 | static int __run_argv(int argc, char * argv[]); |
---|
| 35 | |
---|
[2046] | 36 | void usage (void) { |
---|
| 37 | printf("roarlight [OPTIONS]... command [command...]\n"); |
---|
| 38 | |
---|
| 39 | printf("\nOptions:\n\n"); |
---|
| 40 | |
---|
| 41 | printf(" --server SERVER - Set server hostname\n" |
---|
[5289] | 42 | " --mixer MIXERID - ID of the light mixer to use\n" |
---|
[2046] | 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" |
---|
[5930] | 50 | " sset chan=val - Set a DMX Channel\n" |
---|
| 51 | " set chan=val - Same as sset\n" |
---|
[5932] | 52 | " event EVENT - Send event EVENT\n" |
---|
[2046] | 53 | ); |
---|
| 54 | } |
---|
| 55 | |
---|
[5930] | 56 | static int cmd_sset (char * arg) { |
---|
[2046] | 57 | char * next = arg; |
---|
| 58 | char * k, * v; |
---|
| 59 | int32_t chan, val; |
---|
| 60 | struct roar_roardmx_message mes; |
---|
| 61 | |
---|
| 62 | roar_roardmx_message_new_sset(&mes); |
---|
| 63 | |
---|
| 64 | while (next != NULL) { |
---|
| 65 | arg = next; |
---|
| 66 | next = strstr(next, ","); |
---|
| 67 | if ( next != NULL ) { |
---|
| 68 | *next = 0; |
---|
| 69 | next++; |
---|
| 70 | } |
---|
| 71 | |
---|
| 72 | k = arg; |
---|
| 73 | v = strstr(arg, "="); |
---|
| 74 | if ( v == NULL ) |
---|
| 75 | return -1; |
---|
| 76 | |
---|
| 77 | *v = 0; |
---|
| 78 | v++; |
---|
| 79 | |
---|
| 80 | chan = atoi(k); |
---|
| 81 | val = atoi(v); |
---|
| 82 | // printf("k='%s'(%i), v='%s'(%i)\n", k, chan, v, val); |
---|
| 83 | if ( roar_roardmx_message_add_chanval(&mes, chan, val) == -1 ) |
---|
| 84 | return -1; |
---|
| 85 | } |
---|
| 86 | |
---|
[5929] | 87 | if ( roar_roardmx_message_send(&mes, g_stream) == -1 ) { |
---|
[2048] | 88 | return -1; |
---|
| 89 | } |
---|
| 90 | |
---|
[5929] | 91 | return 0; |
---|
| 92 | } |
---|
| 93 | |
---|
[5932] | 94 | static int cmd_event (char * arg) { |
---|
| 95 | struct roar_roardmx_message mes; |
---|
| 96 | int event; |
---|
| 97 | char * p; |
---|
| 98 | |
---|
| 99 | if ( roar_roardmx_message_new_event(&mes) == -1 ) |
---|
| 100 | return -1; |
---|
| 101 | |
---|
| 102 | while (arg != NULL) { |
---|
| 103 | p = strstr(arg, ","); |
---|
| 104 | if ( p != NULL ) { |
---|
| 105 | *p = 0; |
---|
| 106 | p++; |
---|
| 107 | } |
---|
| 108 | |
---|
| 109 | event = roar_roardmx_str2event(arg); |
---|
| 110 | if ( event == -1 ) |
---|
| 111 | return -1; |
---|
| 112 | |
---|
| 113 | roar_roardmx_message_add_event(&mes, event); |
---|
| 114 | |
---|
| 115 | arg = p; |
---|
| 116 | } |
---|
| 117 | |
---|
| 118 | if ( roar_roardmx_message_send(&mes, g_stream) == -1 ) |
---|
| 119 | return -1; |
---|
| 120 | |
---|
| 121 | return 0; |
---|
| 122 | } |
---|
| 123 | |
---|
[5929] | 124 | static int __run_argv(int argc, char * argv[]) { |
---|
| 125 | char * k; |
---|
| 126 | int i; |
---|
| 127 | |
---|
| 128 | for (i = 0; i < argc; i++) { |
---|
| 129 | k = argv[i]; |
---|
| 130 | // cmd is in k |
---|
| 131 | |
---|
| 132 | printf("--- [ %s ] ---\n", k); |
---|
| 133 | |
---|
| 134 | if ( !strcmp(k, "help") ) { |
---|
| 135 | usage(); |
---|
| 136 | |
---|
| 137 | } else if ( !strcmp(k, "sleep") ) { |
---|
| 138 | roar_sleep(atoi(argv[++i])); |
---|
| 139 | |
---|
[5930] | 140 | } else if ( !strcmp(k, "sset") || !strcmp(k, "set") ) { |
---|
[5929] | 141 | i++; |
---|
[5930] | 142 | if ( cmd_sset(argv[i]) == -1 ) { |
---|
[5929] | 143 | fprintf(stderr, "Error: can not set channels\n"); |
---|
| 144 | } else { |
---|
| 145 | printf("channels changed\n"); |
---|
| 146 | } |
---|
[5932] | 147 | } else if ( !strcmp(k, "event") ) { |
---|
| 148 | i++; |
---|
| 149 | if ( cmd_event(argv[i]) == -1 ) { |
---|
| 150 | fprintf(stderr, "Error: can not send event\n"); |
---|
| 151 | } else { |
---|
| 152 | printf("event send\n"); |
---|
| 153 | } |
---|
[5929] | 154 | |
---|
| 155 | } else { |
---|
| 156 | fprintf(stderr, "Error: invalid command: %s\n", k); |
---|
| 157 | return 1; |
---|
| 158 | } |
---|
| 159 | } |
---|
[2048] | 160 | |
---|
[2046] | 161 | return 0; |
---|
| 162 | } |
---|
| 163 | |
---|
| 164 | int main (int argc, char * argv[]) { |
---|
[5289] | 165 | struct roar_connection con; |
---|
[5929] | 166 | struct roar_vio_calls vio; |
---|
[5289] | 167 | int mixer = -1; // -1 = Default |
---|
[5534] | 168 | const char * server = NULL; |
---|
| 169 | const char * k; |
---|
[2046] | 170 | int i; |
---|
[5929] | 171 | int ret; |
---|
[2046] | 172 | |
---|
| 173 | for (i = 1; i < argc; i++) { |
---|
| 174 | k = argv[i]; |
---|
| 175 | |
---|
| 176 | if ( !strcmp(k, "--server") || !strcmp(k, "-s") ) { |
---|
| 177 | server = argv[++i]; |
---|
[5289] | 178 | } else if ( !strcmp(k, "--mixer") ) { |
---|
| 179 | mixer = atoi(argv[++i]); |
---|
[2046] | 180 | } else if ( !strcmp(k, "--codec") ) { |
---|
| 181 | |
---|
| 182 | } else if ( !strcmp(k, "--help") || !strcmp(k, "-h") ) { |
---|
| 183 | usage(); |
---|
| 184 | return 0; |
---|
| 185 | } else if ( *k == '-' ) { |
---|
| 186 | fprintf(stderr, "Error: unknown argument: %s\n", k); |
---|
| 187 | usage(); |
---|
| 188 | return 1; |
---|
| 189 | } else { |
---|
| 190 | break; |
---|
| 191 | } |
---|
| 192 | } |
---|
| 193 | |
---|
| 194 | if ( i == argc ) { |
---|
| 195 | fprintf(stderr, "Error: No Commands given\n"); |
---|
| 196 | return 0; // this is not a fatal error... |
---|
| 197 | } |
---|
| 198 | |
---|
[5929] | 199 | if ( roar_simple_connect(&con, server, "roarlight") == -1 ) { |
---|
| 200 | fprintf(stderr, "Error: Can not connect to server\n"); |
---|
| 201 | return 1; |
---|
| 202 | } |
---|
[2046] | 203 | |
---|
[5929] | 204 | if ( roar_vio_simple_new_stream_obj(&vio, &con, NULL, |
---|
| 205 | ROAR_RATE_DEFAULT, ROAR_CHANNELS_DEFAULT, ROAR_BITS_DEFAULT, |
---|
| 206 | ROAR_CODEC_ROARDMX, ROAR_DIR_LIGHT_IN, mixer) == -1 ) { |
---|
| 207 | } |
---|
[2046] | 208 | |
---|
[5929] | 209 | g_connection = &con; |
---|
| 210 | g_stream = &vio; |
---|
| 211 | |
---|
| 212 | ret = __run_argv(argc - i, argv + i); |
---|
[2046] | 213 | |
---|
[5929] | 214 | // try to flush all data: |
---|
| 215 | |
---|
| 216 | roar_vio_sync(&vio); |
---|
| 217 | roar_vio_close(&vio); |
---|
| 218 | roar_usleep(100); |
---|
[2046] | 219 | |
---|
| 220 | roar_disconnect(&con); |
---|
| 221 | |
---|
[5929] | 222 | return ret; |
---|
[2046] | 223 | } |
---|
| 224 | |
---|
| 225 | //ll |
---|