1 | //basic.c: |
---|
2 | |
---|
3 | /* |
---|
4 | * Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2008-2011 |
---|
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_connect_raw (char * server) { |
---|
39 | return roar_connect_raw2(server, 0, 0); |
---|
40 | } |
---|
41 | |
---|
42 | int roar_connect_raw2 (char * server, int flags, uint_least32_t timeout) { |
---|
43 | #ifdef ROAR_HAVE_LIBSLP |
---|
44 | struct roar_libroar_config * config = roar_libroar_get_config(); |
---|
45 | #endif |
---|
46 | char user_sock[80]; |
---|
47 | char * roar_server; |
---|
48 | int i = 0; |
---|
49 | int port = 0; |
---|
50 | int fh = -1; |
---|
51 | int is_decnet = 0; |
---|
52 | char * obj = NULL; |
---|
53 | #if !defined(ROAR_TARGET_WIN32) && !defined(ROAR_TARGET_MICROCONTROLLER) |
---|
54 | struct passwd * pwd; |
---|
55 | #endif |
---|
56 | #ifdef ROAR_HAVE_LIBDNET |
---|
57 | struct stat decnet_stat; |
---|
58 | #endif |
---|
59 | #ifdef ROAR_HAVE_LIBX11 |
---|
60 | struct roar_x11_connection * x11con; |
---|
61 | #endif |
---|
62 | struct roar_server * list; |
---|
63 | int workarounds_store; |
---|
64 | |
---|
65 | roar_err_set(ROAR_ERROR_UNKNOWN); |
---|
66 | |
---|
67 | if ( timeout != 0 ) { |
---|
68 | roar_err_set(ROAR_ERROR_INVAL); |
---|
69 | return -1; |
---|
70 | } |
---|
71 | |
---|
72 | if ( flags & ROAR_ENUM_FLAG_HARDNONBLOCK ) |
---|
73 | flags |= ROAR_ENUM_FLAG_NONBLOCK; |
---|
74 | |
---|
75 | |
---|
76 | if ( server == NULL ) |
---|
77 | server = roar_libroar_get_server(); |
---|
78 | |
---|
79 | if ( server == NULL && (roar_server = getenv("ROAR_SERVER")) != NULL ) |
---|
80 | server = roar_server; |
---|
81 | |
---|
82 | #ifdef ROAR_HAVE_LIBX11 |
---|
83 | if ( server == NULL ) { |
---|
84 | if ( (x11con = roar_x11_connect(NULL)) != NULL ) { |
---|
85 | server = roar_x11_get_prop(x11con, "ROAR_SERVER"); |
---|
86 | roar_x11_disconnect(x11con); |
---|
87 | } |
---|
88 | } |
---|
89 | #endif |
---|
90 | |
---|
91 | #if !defined(ROAR_TARGET_WIN32) && !defined(ROAR_TARGET_MICROCONTROLLER) |
---|
92 | if ( server == NULL && (i = readlink("/etc/roarserver", user_sock, 79)) != -1 ) { |
---|
93 | user_sock[i] = 0; |
---|
94 | server = user_sock; |
---|
95 | } |
---|
96 | #endif |
---|
97 | |
---|
98 | if ( server != NULL && !strcasecmp(server, "+slp") ) { |
---|
99 | server = roar_slp_find_roard(0); |
---|
100 | if ( server == NULL ) { |
---|
101 | return -1; |
---|
102 | } |
---|
103 | } |
---|
104 | |
---|
105 | if ( server == NULL || *server == 0 ) { |
---|
106 | /* connect via defaults */ |
---|
107 | |
---|
108 | #ifdef ROAR_HAVE_UNIX |
---|
109 | #ifndef ROAR_TARGET_MICROCONTROLLER |
---|
110 | roar_server = getenv("HOME"); |
---|
111 | #else |
---|
112 | roar_server = NULL; |
---|
113 | #endif |
---|
114 | |
---|
115 | if ( roar_server == NULL ) { |
---|
116 | #if !defined(ROAR_TARGET_WIN32) && !defined(ROAR_TARGET_MICROCONTROLLER) |
---|
117 | if ( (pwd = getpwuid(getuid())) == NULL ) { |
---|
118 | roar_server = "/NX-HOME-DIR"; |
---|
119 | } else { |
---|
120 | roar_server = pwd->pw_dir; |
---|
121 | } |
---|
122 | #else |
---|
123 | roar_server = "/WIN32-SUCKS"; |
---|
124 | #endif |
---|
125 | } |
---|
126 | |
---|
127 | snprintf(user_sock, 79, "%s/%s", roar_server, ROAR_DEFAULT_SOCK_USER); |
---|
128 | |
---|
129 | if ( (fh = roar_socket_connect(user_sock, 0)) != -1 ) |
---|
130 | return fh; |
---|
131 | |
---|
132 | if ( (fh = roar_socket_connect(ROAR_DEFAULT_SOCK_GLOBAL, 0)) != -1 ) |
---|
133 | return fh; |
---|
134 | #endif |
---|
135 | |
---|
136 | if ( (fh = roar_socket_connect(ROAR_DEFAULT_HOST, ROAR_DEFAULT_PORT)) != -1 ) |
---|
137 | return fh; |
---|
138 | |
---|
139 | #ifdef ROAR_HAVE_LIBDNET |
---|
140 | if ( stat(ROAR_PROC_NET_DECNET, &decnet_stat) == 0 ) { |
---|
141 | if ( roar_socket_get_local_nodename() ) { |
---|
142 | snprintf(user_sock, 79, "%s::%s", roar_socket_get_local_nodename(), ROAR_DEFAULT_OBJECT); |
---|
143 | if ( (fh = roar_socket_connect(user_sock, ROAR_DEFAULT_NUM)) != -1 ) |
---|
144 | return fh; |
---|
145 | } |
---|
146 | } |
---|
147 | #endif |
---|
148 | |
---|
149 | if ( (fh = roar_socket_connect("+abstract", 0)) != -1 ) |
---|
150 | return fh; |
---|
151 | |
---|
152 | #ifdef ROAR_HAVE_LIBSLP |
---|
153 | if ( !(config->workaround.workarounds & ROAR_LIBROAR_CONFIG_WAS_NO_SLP) && |
---|
154 | !(flags & ROAR_ENUM_FLAG_NONBLOCK) |
---|
155 | ) { |
---|
156 | if ( (server = roar_slp_find_roard(0)) != NULL ) { |
---|
157 | if ( (fh = roar_connect_raw(server)) != -1 ) |
---|
158 | return fh; |
---|
159 | |
---|
160 | /* in case we can not connect to the server given this may be a cache problem, |
---|
161 | we do a new lookup with the cache disabled in this case */ |
---|
162 | ROAR_WARN("roar_connect_raw(*): Can not connect to SLP located server, disabling cache"); |
---|
163 | if ( (server = roar_slp_find_roard(1)) != NULL ) |
---|
164 | if ( (fh = roar_connect_raw(server)) != -1 ) |
---|
165 | return fh; |
---|
166 | } |
---|
167 | } |
---|
168 | |
---|
169 | workarounds_store = config->workaround.workarounds; |
---|
170 | config->workaround.workarounds |= ROAR_LIBROAR_CONFIG_WAS_NO_SLP; |
---|
171 | list = roar_enum_servers(flags, -1, -1); |
---|
172 | config->workaround.workarounds = workarounds_store; |
---|
173 | if ( list != NULL ) { |
---|
174 | for (i = 0; list[i].server != NULL; i++) { |
---|
175 | if ( (fh = roar_connect_raw((char*)list[i].server)) != -1 ) { |
---|
176 | roar_enum_servers_free(list); |
---|
177 | return fh; |
---|
178 | } |
---|
179 | } |
---|
180 | roar_enum_servers_free(list); |
---|
181 | } |
---|
182 | #endif |
---|
183 | |
---|
184 | return -1; |
---|
185 | |
---|
186 | } else { |
---|
187 | /* connect via (char*)server */ |
---|
188 | // find a port: |
---|
189 | if ( *server != '/' ) { // don't test AF_UNIX sockets for ports |
---|
190 | for (i = 0; server[i] != 0; i++) { |
---|
191 | if ( server[i] == ':' ) { |
---|
192 | if ( server[i+1] == ':' ) { // DECnet, leave unchanged |
---|
193 | is_decnet = 1; |
---|
194 | obj = &server[i+2]; |
---|
195 | break; |
---|
196 | } |
---|
197 | |
---|
198 | port = atoi(server+i+1); |
---|
199 | server[i] = 0; |
---|
200 | break; |
---|
201 | } |
---|
202 | } |
---|
203 | } |
---|
204 | |
---|
205 | if ( is_decnet ) { |
---|
206 | *user_sock = 0; |
---|
207 | if ( *server == ':' ) { |
---|
208 | if ( roar_socket_get_local_nodename() ) |
---|
209 | strncat(user_sock, roar_socket_get_local_nodename(), 6); |
---|
210 | } |
---|
211 | |
---|
212 | strncat(user_sock, server, 79); |
---|
213 | server = user_sock; |
---|
214 | if ( *obj == 0 ) { |
---|
215 | #ifdef DN_MAXOBJL |
---|
216 | strncat(user_sock, ROAR_DEFAULT_OBJECT, DN_MAXOBJL+2); |
---|
217 | #else |
---|
218 | ROAR_ERR("roar_connect_raw(*): size of DECnet object unknown."); |
---|
219 | #endif |
---|
220 | } |
---|
221 | } |
---|
222 | |
---|
223 | if ( port || is_decnet ) { |
---|
224 | fh = roar_socket_connect(server, port); |
---|
225 | // restore the original string |
---|
226 | server[i] = ':'; |
---|
227 | } else { |
---|
228 | fh = roar_socket_connect(server, ROAR_DEFAULT_PORT); |
---|
229 | } |
---|
230 | } |
---|
231 | |
---|
232 | if ( fh == -1 ) |
---|
233 | roar_err_set(ROAR_ERROR_CONNREFUSED); |
---|
234 | |
---|
235 | ROAR_DBG("roar_connect_raw(*) = %i", fh); |
---|
236 | |
---|
237 | return fh; |
---|
238 | } |
---|
239 | |
---|
240 | int roar_connect (struct roar_connection * con, char * server) { |
---|
241 | return roar_connect2(con, server, 0, 0); |
---|
242 | } |
---|
243 | |
---|
244 | int roar_connect2 (struct roar_connection * con, char * server, int flags, uint_least32_t timeout) { |
---|
245 | int fh; |
---|
246 | |
---|
247 | if ( con == NULL ) { |
---|
248 | roar_err_set(ROAR_ERROR_FAULT); |
---|
249 | return -1; |
---|
250 | } |
---|
251 | |
---|
252 | roar_err_set(ROAR_ERROR_UNKNOWN); |
---|
253 | fh = roar_connect_raw2(server, flags, timeout); |
---|
254 | |
---|
255 | if ( fh == -1 ) |
---|
256 | return -1; |
---|
257 | |
---|
258 | return roar_connect_fh(con, fh); |
---|
259 | } |
---|
260 | |
---|
261 | int roar_connect_fh (struct roar_connection * con, int fh) { |
---|
262 | |
---|
263 | if ( con == NULL || fh == -1 ) { |
---|
264 | roar_err_set(ROAR_ERROR_INVAL); |
---|
265 | return -1; |
---|
266 | } |
---|
267 | |
---|
268 | // specal hack to set an ilegal value used internaly in libroar: |
---|
269 | if ( fh == -2 ) |
---|
270 | fh = -1; |
---|
271 | |
---|
272 | memset(con, 0, sizeof(struct roar_connection)); |
---|
273 | |
---|
274 | con->flags = ROAR_CON_FLAGS_FH; |
---|
275 | con->__fh = fh; |
---|
276 | |
---|
277 | if ( roar_vio_open_fh_socket(&(con->viocon), fh) != -1 ) |
---|
278 | con->flags |= ROAR_CON_FLAGS_VIO; |
---|
279 | |
---|
280 | roar_err_set(ROAR_ERROR_NONE); |
---|
281 | return 0; |
---|
282 | } |
---|
283 | |
---|
284 | int roar_get_connection_fh (struct roar_connection * con) { |
---|
285 | roar_debug_warn_sysio("roar_get_connection_fh", "roar_get_connection_vio2", NULL); |
---|
286 | |
---|
287 | if ( con == NULL ) |
---|
288 | return -1; |
---|
289 | |
---|
290 | if ( !(con->flags & ROAR_CON_FLAGS_FH) ) |
---|
291 | return -1; |
---|
292 | |
---|
293 | return con->__fh; |
---|
294 | } |
---|
295 | |
---|
296 | int roar_get_connection_vio (struct roar_connection * con, struct roar_vio_calls * vio) { |
---|
297 | roar_debug_warn_obsolete("roar_get_connection_vio", "roar_get_connection_vio2", NULL); |
---|
298 | |
---|
299 | if ( con == NULL || vio == NULL ) |
---|
300 | return -1; |
---|
301 | |
---|
302 | if ( !(con->flags & ROAR_CON_FLAGS_FH) ) |
---|
303 | return -1; |
---|
304 | |
---|
305 | return roar_vio_open_fh_socket(vio, con->__fh); |
---|
306 | } |
---|
307 | |
---|
308 | struct roar_vio_calls * roar_get_connection_vio2 (struct roar_connection * con) { |
---|
309 | if ( con == NULL ) |
---|
310 | return NULL; |
---|
311 | |
---|
312 | if ( con->flags & ROAR_CON_FLAGS_VIO ) |
---|
313 | return &(con->viocon); |
---|
314 | |
---|
315 | // TODO: try to open the VIO. |
---|
316 | |
---|
317 | return NULL; |
---|
318 | } |
---|
319 | |
---|
320 | int roar_disconnect (struct roar_connection * con) { |
---|
321 | struct roar_vio_calls * vio; |
---|
322 | struct roar_message m; |
---|
323 | |
---|
324 | memset(&m, 0, sizeof(m)); |
---|
325 | |
---|
326 | m.datalen = 0; |
---|
327 | m.stream = 0; |
---|
328 | m.pos = 0; |
---|
329 | m.cmd = ROAR_CMD_QUIT; |
---|
330 | |
---|
331 | roar_req(con, &m, NULL); |
---|
332 | |
---|
333 | if ( (vio = roar_get_connection_vio2(con)) != NULL ) { |
---|
334 | roar_vio_close(vio); |
---|
335 | } |
---|
336 | |
---|
337 | roar_connect_fh(con, -2); |
---|
338 | |
---|
339 | roar_err_set(ROAR_ERROR_NONE); |
---|
340 | |
---|
341 | return 0; |
---|
342 | } |
---|
343 | |
---|
344 | int roar_set_connection_callback(struct roar_connection * con, |
---|
345 | void (*cb)(struct roar_connection * con, |
---|
346 | struct roar_message * mes, |
---|
347 | void * userdata), |
---|
348 | void * userdata) { |
---|
349 | if ( con == NULL ) |
---|
350 | return -1; |
---|
351 | |
---|
352 | con->cb = cb; |
---|
353 | con->userdata = userdata; |
---|
354 | |
---|
355 | return 0; |
---|
356 | } |
---|
357 | |
---|
358 | int roar_sync (struct roar_connection * con) { |
---|
359 | // wait for any non-client reqs |
---|
360 | return roar_wait_msg(con, 0x0000, 0x8000); |
---|
361 | } |
---|
362 | |
---|
363 | int roar_wait_msg (struct roar_connection * con, int16_t seq, int16_t seqmask) { |
---|
364 | return -1; |
---|
365 | } |
---|
366 | |
---|
367 | int roar_noop (struct roar_connection * con) { |
---|
368 | struct roar_message mes; |
---|
369 | |
---|
370 | if ( con == NULL ) { |
---|
371 | return -1; |
---|
372 | } |
---|
373 | |
---|
374 | memset(&mes, 0, sizeof(mes)); |
---|
375 | |
---|
376 | mes.cmd = ROAR_CMD_NOOP; |
---|
377 | |
---|
378 | return roar_req(con, &mes, NULL); |
---|
379 | } |
---|
380 | |
---|
381 | int roar_identify (struct roar_connection * con, char * name) { |
---|
382 | struct roar_message mes; |
---|
383 | pid_t pid; |
---|
384 | int max_len; |
---|
385 | |
---|
386 | roar_err_set(ROAR_ERROR_UNKNOWN); |
---|
387 | |
---|
388 | ROAR_DBG("roar_identify(*): try to identify myself..."); |
---|
389 | |
---|
390 | memset(&mes, 0, sizeof(mes)); |
---|
391 | |
---|
392 | mes.cmd = ROAR_CMD_IDENTIFY; |
---|
393 | mes.stream = 0; |
---|
394 | mes.pos = 0; |
---|
395 | |
---|
396 | ROAR_DBG("roar_identify(*): name=%p", name); |
---|
397 | |
---|
398 | if ( name == NULL ) |
---|
399 | name = "libroar client"; |
---|
400 | |
---|
401 | ROAR_DBG("roar_identify(*): name=%p", name); |
---|
402 | |
---|
403 | max_len = strlen(name); |
---|
404 | ROAR_DBG("roar_identify(*): strlen(name) = %i", max_len); |
---|
405 | |
---|
406 | if ( max_len > (LIBROAR_BUFFER_MSGDATA - 5) ) |
---|
407 | max_len = LIBROAR_BUFFER_MSGDATA - 5; |
---|
408 | |
---|
409 | mes.datalen = 5 + max_len; |
---|
410 | mes.data[0] = 1; |
---|
411 | |
---|
412 | pid = getpid(); |
---|
413 | *(uint32_t*)(mes.data+1) = ROAR_HOST2NET32(pid); |
---|
414 | ROAR_DBG("roar_identify(*): pid = %i", pid); |
---|
415 | |
---|
416 | strncpy(mes.data+5, name, max_len); |
---|
417 | |
---|
418 | return roar_req(con, &mes, NULL); |
---|
419 | } |
---|
420 | |
---|
421 | int roar_debug_message_print (struct roar_message * mes) { |
---|
422 | if ( mes == NULL ) |
---|
423 | return -1; |
---|
424 | |
---|
425 | ROAR_DBG("roar_debug_message_print(*): Command: %i", mes->cmd); |
---|
426 | ROAR_DBG("roar_debug_message_print(*): Stream : %u", mes->stream); |
---|
427 | ROAR_DBG("roar_debug_message_print(*): Pos : %u", mes->pos); |
---|
428 | ROAR_DBG("roar_debug_message_print(*): Datalen: %i", mes->datalen); |
---|
429 | |
---|
430 | ROAR_DBG("roar_debug_message_print(*) = 0"); |
---|
431 | return 0; |
---|
432 | } |
---|
433 | |
---|
434 | int roar_debug_audio_info_print (struct roar_audio_info * info) { |
---|
435 | if ( info == NULL ) |
---|
436 | return -1; |
---|
437 | |
---|
438 | ROAR_DBG("roar_debug_audio_info_print(*): Rate : %i", info->rate); |
---|
439 | ROAR_DBG("roar_debug_audio_info_print(*): Channels: %i", info->channels); |
---|
440 | ROAR_DBG("roar_debug_audio_info_print(*): Bits : %i", info->bits); |
---|
441 | ROAR_DBG("roar_debug_audio_info_print(*): Codec : %i", info->codec); |
---|
442 | |
---|
443 | ROAR_DBG("roar_debug_audio_info_print(*) = 0"); |
---|
444 | return 0; |
---|
445 | } |
---|
446 | |
---|
447 | //ll |
---|