source: roaraudio/libroar/ctl.c @ 690:cee9bf5fa456

Last change on this file since 690:cee9bf5fa456 was 690:cee9bf5fa456, checked in by phi, 16 years ago

added copyright statements

File size: 8.9 KB
Line 
1//ctl.c:
2
3/*
4 *      Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2008
5 *
6 *  This file is part of libroar 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 *  libroar 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 *  NOTE for everyone want's to change something and send patches:
24 *  read README and HACKING! There a addition information on
25 *  the license of this document you need to read before you send
26 *  any patches.
27 *
28 *  NOTE for uses of non-GPL (LGPL,...) software using libesd, libartsc
29 *  or libpulse*:
30 *  The libs libroaresd, libroararts and libroarpulse link this lib
31 *  and are therefore GPL. Because of this it may be illigal to use
32 *  them with any software that uses libesd, libartsc or libpulse*.
33 */
34
35#include "libroar.h"
36
37int roar_server_oinfo   (struct roar_connection * con, struct roar_stream * sa) {
38 struct roar_message mes;
39
40 memset(&mes, 0, sizeof(struct roar_message)); // make valgrind happy!
41
42 mes.cmd     = ROAR_CMD_SERVER_OINFO;
43 mes.datalen = 0;
44
45 if ( roar_req(con, &mes, NULL) == -1 )
46  return -1;
47
48 if ( mes.cmd != ROAR_CMD_OK )
49  return -1;
50
51 if ( roar_stream_m2s(sa, &mes) == -1 )
52  return -1;
53
54 return 0;
55}
56
57int roar_get_standby   (struct roar_connection * con) {
58 struct roar_message mes;
59
60 memset(&mes, 0, sizeof(struct roar_message)); // make valgrind happy!
61
62 mes.cmd = ROAR_CMD_GET_STANDBY;
63 mes.datalen = 0;
64
65 if ( roar_req(con, &mes, NULL) == -1 )
66  return -1;
67
68 if ( mes.cmd != ROAR_CMD_OK )
69  return -1;
70
71 return ROAR_NET2HOST16(*((uint16_t*)mes.data));
72}
73
74int roar_set_standby   (struct roar_connection * con, int state) {
75 struct roar_message mes;
76
77 memset(&mes, 0, sizeof(struct roar_message)); // make valgrind happy!
78
79 mes.cmd = ROAR_CMD_SET_STANDBY;
80 mes.datalen = 2;
81
82 *((uint16_t*)mes.data) = ROAR_HOST2NET16((unsigned) state);
83
84 if ( roar_req(con, &mes, NULL) == -1 )
85  return -1;
86
87 if ( mes.cmd != ROAR_CMD_OK )
88  return -1;
89
90 return 0;
91}
92
93int roar_exit   (struct roar_connection * con) {
94 return roar_terminate(con, 0);
95}
96
97int roar_terminate (struct roar_connection * con, int terminate) {
98 struct roar_message mes;
99
100 memset(&mes, 0, sizeof(struct roar_message)); // make valgrind happy!
101
102 mes.cmd     = ROAR_CMD_EXIT;
103 mes.datalen = 1;
104 mes.data[0] = terminate;
105
106 if ( roar_req(con, &mes, NULL) == -1 )
107  return -1;
108
109 if ( mes.cmd != ROAR_CMD_OK )
110  return -1;
111
112 return 0;
113}
114
115int roar_list         (struct roar_connection * con, int * items,   int max, int cmd) {
116 struct roar_message m;
117
118 memset(&m, 0, sizeof(struct roar_message)); // make valgrind happy!
119
120 roar_ctl_f2m_any(&m);
121 m.cmd = cmd;
122
123 if ( roar_req(con, &m, NULL) == -1 )
124  return -1;
125
126 return roar_ctl_m2ia(&m, items, max);
127}
128
129int roar_get_client   (struct roar_connection * con, struct roar_client * client, int id) {
130 struct roar_message m;
131
132 memset(&m, 0, sizeof(struct roar_message)); // make valgrind happy!
133
134 m.cmd     = ROAR_CMD_GET_CLIENT;
135 m.datalen = 1;
136 m.data[0] = id;
137
138 ROAR_DBG("roar_get_client(*): id = %i", id);
139
140 if ( roar_req(con, &m, NULL) == -1 )
141  return -1;
142
143 if ( m.cmd != ROAR_CMD_OK )
144  return -1;
145
146 ROAR_DBG("roar_get_client(*): got ok");
147
148 return roar_ctl_m2c(&m, client);
149}
150
151int roar_get_stream   (struct roar_connection * con, struct roar_stream * stream, int id) {
152 struct roar_message m;
153
154 memset(&m, 0, sizeof(struct roar_message)); // make valgrind happy!
155
156 m.cmd     = ROAR_CMD_GET_STREAM;
157 m.datalen = 1;
158 m.data[0] = id;
159
160 if ( roar_req(con, &m, NULL) == -1 )
161  return -1;
162
163 if ( m.cmd != ROAR_CMD_OK )
164  return -1;
165
166 return roar_stream_m2s(stream, &m);
167}
168
169int roar_kick         (struct roar_connection * con, int type, int id) {
170 struct roar_message m;
171 uint16_t * info = (uint16_t *) m.data;
172
173 memset(&m, 0, sizeof(struct roar_message)); // make valgrind happy!
174
175 m.cmd     = ROAR_CMD_KICK;
176 m.datalen = 4;
177 info[0] = ROAR_HOST2NET16(type);
178 info[1] = ROAR_HOST2NET16(id);
179
180 if ( roar_req(con, &m, NULL) == -1 )
181  return -1;
182
183 if ( m.cmd != ROAR_CMD_OK )
184  return -1;
185
186 return 0;
187}
188
189int roar_set_vol      (struct roar_connection * con, int id, struct roar_mixer_settings * mixer, int channels) {
190 struct roar_message m;
191 uint16_t * info = (uint16_t *) m.data;
192 int i;
193
194 memset(&m, 0, sizeof(struct roar_message)); // make valgrind happy!
195
196 m.cmd     = ROAR_CMD_SET_VOL;
197 m.datalen = (3 + channels) * 2;
198 info[0] = 0;
199 info[1] = ROAR_HOST2NET16(id);
200 info[2] = ROAR_HOST2NET16(ROAR_SET_VOL_ALL);
201
202 for (i = 0; i < channels; i++)
203  info[i+3] = ROAR_HOST2NET16(mixer->mixer[i]);
204
205 if ( roar_req(con, &m, NULL) == -1 )
206  return -1;
207
208 if ( m.cmd != ROAR_CMD_OK )
209  return -1;
210
211 return 0;
212}
213
214int roar_get_vol      (struct roar_connection * con, int id, struct roar_mixer_settings * mixer, int * channels) {
215 struct roar_message m;
216 uint16_t * info = (uint16_t *) m.data;
217 int i;
218
219 memset(&m, 0, sizeof(struct roar_message)); // make valgrind happy!
220
221 m.cmd     = ROAR_CMD_GET_VOL;
222 m.datalen = 2*2;
223 info[0] = 0;
224 info[1] = ROAR_HOST2NET16(id);
225
226 if ( roar_req(con, &m, NULL) == -1 )
227  return -1;
228
229 if ( m.cmd != ROAR_CMD_OK )
230  return -1;
231
232 if ( info[0] != 0 )
233  return -1;
234
235 info[1] = ROAR_NET2HOST16(info[1]);
236
237 if ( channels != NULL )
238  *channels = info[1];
239
240 if ( info[1] > ROAR_MAX_CHANNELS )
241  return -1;
242
243 for (i = 0; i < info[1]; i++)
244  mixer->mixer[i] = ROAR_NET2HOST16(info[i+2]);
245
246 return 0;
247}
248
249// converts: *_m2*, *_*2m
250
251int roar_ctl_f2m      (struct roar_message * m, unsigned char   filter, unsigned char   cmp, uint32_t   id) {
252
253 m->datalen = 7;
254
255 m->data[0] = 0;
256 m->data[1] = filter;
257 m->data[2] = cmp;
258 *((uint32_t*)&(m->data[3])) = ROAR_HOST2NET32(id);
259
260 return 0;
261}
262int roar_ctl_m2f      (struct roar_message * m, unsigned char * filter, unsigned char * cmp, uint32_t * id) {
263
264 if ( m->datalen != 7 )
265  return -1;
266
267 if ( m->data[0] != 0 ) {
268  ROAR_ERR("roar_ctl_m2f(*): version %i not supported!", m->data[0]);
269  return -1;
270 }
271
272 *filter = m->data[1];
273 *cmp    = m->data[2];
274
275 *id = ROAR_NET2HOST32(*((uint32_t*)&(m->data[3])));
276
277 return 0;
278}
279
280int roar_ctl_ia2m     (struct roar_message * m, int * data, int len) {
281 int i;
282
283 if ( len > LIBROAR_BUFFER_MSGDATA )
284  return -1;
285
286 m->datalen = len;
287
288 for (i = 0; i < len; i++)
289  m->data[i] = data[i];
290
291 return 0;
292}
293int roar_ctl_m2ia     (struct roar_message * m, int * data, int len) {
294 int i;
295
296 if ( m->datalen > len )
297  return -1;
298
299 for (i = 0; i < m->datalen; i++)
300  data[i] = m->data[i];
301
302 return m->datalen;
303}
304
305int roar_ctl_c2m      (struct roar_message * m, struct roar_client * c) {
306 int cur = 0;
307 int h;
308 int i;
309 int max_len;
310 uint32_t pid;
311
312 if ( c == NULL )
313  return -1;
314
315 m->data[cur++] = 0;                       // 0: Version
316 m->data[cur++] = c->execed;               // 1: execed
317
318 h = 0;
319 for (i = 0; i < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; i++) {
320  if ( c->streams[i] != -1 )
321   m->data[cur+1+h++] = c->streams[i];
322 }
323
324 m->data[cur++] = h;                       // 2: num of streams
325 cur += h;
326
327 max_len = strlen(c->name);
328
329 // TODO: add some code to check if this fits in the pkg
330 // NOTE: add this code after we are sure how long this pkg will be
331 //       and fully decieded about this function.
332
333 m->data[cur++] = max_len;
334
335 strncpy((m->data)+cur, c->name, max_len);
336
337 cur += max_len;
338
339 pid = ROAR_HOST2NET32(c->pid);
340 memcpy(&(m->data[cur]), &pid, 4);
341 cur += 4;
342
343 pid = ROAR_HOST2NET32(c->uid);
344 memcpy(&(m->data[cur]), &pid, 4);
345 cur += 4;
346
347 pid = ROAR_HOST2NET32(c->gid);
348 memcpy(&(m->data[cur]), &pid, 4);
349 cur += 4;
350
351 m->datalen = cur;
352
353 return 0;
354}
355
356int roar_ctl_m2c      (struct roar_message * m, struct roar_client * c) {
357 int i;
358 int cur;
359 uint32_t pid;
360
361 if ( m == NULL || c == NULL )
362  return -1;
363
364 if ( m->datalen == 0 )
365  return -1;
366
367 ROAR_DBG("roar_ctl_m2c(*): got data!, len = %i", m->datalen);
368
369 if ( m->data[0] != 0 ) {
370  ROAR_DBG("roar_ctl_m2c(*): wrong version!");
371  return -1;
372 }
373
374 if ( m->datalen < 3 )
375  return -1;
376
377 ROAR_DBG("roar_ctl_m2c(*): have usable data!");
378
379 c->execed = m->data[1];
380
381 for (i = 0; i < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; i++)
382  c->streams[i] = -1;
383
384 for (i = 0; i < m->data[2]; i++)
385  c->streams[i] = m->data[3+i];
386
387 cur = 3 + m->data[2];
388
389 strncpy(c->name, (m->data)+cur+1, m->data[cur]);
390 c->name[(int)m->data[cur]] = 0;
391
392 cur += m->data[cur] + 1;
393
394 memcpy(&pid, &(m->data[cur]), 4);
395 c->pid = ROAR_NET2HOST32(pid);
396 cur += 4;
397
398 memcpy(&pid, &(m->data[cur]), 4);
399 c->uid = ROAR_NET2HOST32(pid);
400 cur += 4;
401
402 memcpy(&pid, &(m->data[cur]), 4);
403 c->gid = ROAR_NET2HOST32(pid);
404 cur += 4;
405
406 return 0;
407}
408
409//ll
Note: See TracBrowser for help on using the repository browser.