1 | //asyncctl.c: |
---|
2 | |
---|
3 | /* |
---|
4 | * Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2010 |
---|
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, 51 Franklin Street, Fifth Floor, |
---|
22 | * Boston, MA 02110-1301, USA. |
---|
23 | * |
---|
24 | * NOTE for everyone want's to change something and send patches: |
---|
25 | * read README and HACKING! There a addition information on |
---|
26 | * the license of this document you need to read before you send |
---|
27 | * any patches. |
---|
28 | * |
---|
29 | * NOTE for uses of non-GPL (LGPL,...) software using libesd, libartsc |
---|
30 | * or libpulse*: |
---|
31 | * The libs libroaresd, libroararts and libroarpulse link this lib |
---|
32 | * and are therefore GPL. Because of this it may be illigal to use |
---|
33 | * them with any software that uses libesd, libartsc or libpulse*. |
---|
34 | */ |
---|
35 | |
---|
36 | #include "libroar.h" |
---|
37 | |
---|
38 | int roar_wait (struct roar_connection * con, struct roar_event * triggered, struct roar_event * events, size_t num) { |
---|
39 | struct roar_message m; |
---|
40 | uint16_t * u16 = (uint16_t *) m.data; |
---|
41 | void * vp = m.data; |
---|
42 | size_t i, len_left, len_have, tmp; |
---|
43 | |
---|
44 | if ( con == NULL || triggered == NULL ) |
---|
45 | return -1; |
---|
46 | |
---|
47 | if ( num == 0 ) |
---|
48 | return 0; |
---|
49 | |
---|
50 | if ( events == NULL ) |
---|
51 | return -1; |
---|
52 | |
---|
53 | memset(&m, 0, sizeof(m)); |
---|
54 | |
---|
55 | m.cmd = ROAR_CMD_WAIT; |
---|
56 | m.stream = -1; |
---|
57 | |
---|
58 | u16[0] = ROAR_HOST2NET16(0); // version |
---|
59 | u16[1] = ROAR_HOST2NET16(0); // flags |
---|
60 | |
---|
61 | vp += 4; // we have 4 byte header (version + flags) |
---|
62 | |
---|
63 | len_left = sizeof(m.data) - 4; |
---|
64 | len_have = 4; |
---|
65 | |
---|
66 | for (i = 0; i < num; i++) { |
---|
67 | tmp = len_left; |
---|
68 | if ( roar_event_to_blob(&(events[i]), vp, &tmp) == -1 ) |
---|
69 | return -1; |
---|
70 | |
---|
71 | len_left -= tmp; |
---|
72 | len_have += tmp; |
---|
73 | vp += tmp; |
---|
74 | } |
---|
75 | |
---|
76 | m.datalen = len_have; |
---|
77 | |
---|
78 | if ( roar_req(con, &m, NULL) != 0 ) |
---|
79 | return -1; |
---|
80 | |
---|
81 | if ( m.cmd != ROAR_CMD_OK ) |
---|
82 | return -1; |
---|
83 | |
---|
84 | u16[0] = ROAR_NET2HOST16(u16[0]); |
---|
85 | u16[1] = ROAR_NET2HOST16(u16[1]); |
---|
86 | |
---|
87 | // check if we have a complet header: |
---|
88 | if ( m.datalen < 2 ) |
---|
89 | return -1; |
---|
90 | |
---|
91 | // check if we have a unsupported version or flags: |
---|
92 | if ( u16[0] != 0 || u16[1] != 0 ) |
---|
93 | return -1; |
---|
94 | |
---|
95 | vp = (void*)m.data; |
---|
96 | vp += 4; |
---|
97 | len_have = tmp = m.datalen - 4; |
---|
98 | |
---|
99 | memset(triggered, 0, sizeof(struct roar_event)); |
---|
100 | |
---|
101 | if ( roar_event_from_blob(triggered, vp, &tmp) != 0 ) |
---|
102 | return -1; |
---|
103 | |
---|
104 | // test if we have a length match: |
---|
105 | if ( len_have != tmp ) |
---|
106 | return -1; |
---|
107 | |
---|
108 | // we now have an event. |
---|
109 | |
---|
110 | // we maybe have some data, need to reset this as it is of a local buffer: |
---|
111 | triggered->arg2 = NULL; |
---|
112 | triggered->arg2_len = 0; |
---|
113 | |
---|
114 | return 0; |
---|
115 | } |
---|
116 | |
---|
117 | //ll |
---|