source: roaraudio/libroar/basic.c @ 4708:c9d40761088a

Last change on this file since 4708:c9d40761088a was 4708:c9d40761088a, checked in by phi, 13 years ago

updated copyright statements

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