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
RevLine 
[0]1//basic.c:
2
[690]3/*
[4708]4 *      Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2008-2011
[690]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
[3517]21 *  the Free Software Foundation, 51 Franklin Street, Fifth Floor,
22 *  Boston, MA 02110-1301, USA.
[690]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
[0]36#include "libroar.h"
37
38int roar_connect_raw (char * server) {
[4653]39 struct roar_libroar_config * config = roar_libroar_get_config();
[0]40 char user_sock[80];
41 char * roar_server;
[701]42 int i = 0;
[0]43 int port = 0;
44 int fh = -1;
[501]45 int is_decnet = 0;
[516]46 char * obj = NULL;
[1475]47#if !defined(ROAR_TARGET_WIN32) && !defined(ROAR_TARGET_MICROCONTROLLER)
[1026]48 struct passwd * pwd;
[1393]49#endif
[828]50#ifdef ROAR_HAVE_LIBDNET
51 struct stat decnet_stat;
52#endif
[3372]53#ifdef ROAR_HAVE_LIBX11
54 struct roar_x11_connection * x11con;
55#endif
[0]56
[807]57 roar_errno = ROAR_ERROR_UNKNOWN;
58
[2567]59 if ( server == NULL )
60  server = roar_libroar_get_server();
61
[61]62 if ( server == NULL && (roar_server = getenv("ROAR_SERVER")) != NULL )
[0]63  server = roar_server;
64
[3372]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
[1436]74#if !defined(ROAR_TARGET_WIN32) && !defined(ROAR_TARGET_MICROCONTROLLER)
[448]75 if ( server == NULL && (i = readlink("/etc/roarserver", user_sock, 79)) != -1 ) {
76   user_sock[i] = 0;
77   server = user_sock;
78 }
[1093]79#endif
[448]80
[3077]81 if ( server != NULL && !strcasecmp(server, "+slp") ) {
[3076]82  server = roar_slp_find_roard(0);
83  if ( server == NULL ) {
84   return -1;
85  }
86 }
87
[61]88 if ( server == NULL || *server == 0 ) {
[0]89  /* connect via defaults */
90
[1764]91#ifdef ROAR_HAVE_UNIX
[1436]92#ifndef ROAR_TARGET_MICROCONTROLLER
[1026]93  roar_server = getenv("HOME");
[1435]94#else
95  roar_server = NULL;
96#endif
[1026]97
98  if ( roar_server == NULL ) {
[1436]99#if !defined(ROAR_TARGET_WIN32) && !defined(ROAR_TARGET_MICROCONTROLLER)
[1026]100   if ( (pwd = getpwuid(getuid())) == NULL ) {
101    roar_server = "/NX-HOME-DIR";
102   } else {
103    roar_server = pwd->pw_dir;
104   }
[1078]105#else
106   roar_server = "/WIN32-SUCKS";
107#endif
[1026]108  }
109
110  snprintf(user_sock, 79, "%s/%s", roar_server, ROAR_DEFAULT_SOCK_USER);
[0]111
112  if ( (fh = roar_socket_connect(user_sock, 0)) != -1 )
113   return fh;
114
[237]115  if ( (fh = roar_socket_connect(ROAR_DEFAULT_SOCK_GLOBAL, 0)) != -1 )
116   return fh;
[1764]117#endif
[237]118
[0]119  if ( (fh = roar_socket_connect(ROAR_DEFAULT_HOST, ROAR_DEFAULT_PORT)) != -1 )
120   return fh;
121
[828]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);
[2010]126    if ( (fh = roar_socket_connect(user_sock, ROAR_DEFAULT_NUM)) != -1 )
127     return fh;
[828]128   }
[522]129  }
[828]130#endif
[2007]131
[4109]132  if ( (fh = roar_socket_connect("+abstract", 0)) != -1 )
133   return fh;
134
[2007]135#ifdef ROAR_HAVE_LIBSLP
[4653]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;
[2014]140
[4653]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 }
[2007]148#endif
[522]149
[2014]150 return -1;
151
[0]152 } else {
153  /* connect via (char*)server */
[501]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;
[516]160      obj = &server[i+2];
[501]161      break;
162     }
163
164     port = atoi(server+i+1);
165     server[i] = 0;
166     break;
167    }
[0]168   }
169  }
170
[521]171  if ( is_decnet ) {
172    *user_sock = 0;
173   if ( *server == ':' ) {
174    if ( roar_socket_get_local_nodename() )
[1066]175     strncat(user_sock, roar_socket_get_local_nodename(), 6);
[521]176   }
177
[1066]178   strncat(user_sock, server, 79);
[516]179   server = user_sock;
[521]180   if ( *obj == 0 ) {
[1066]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
[521]186   }
[516]187  }
188
[501]189  if ( port || is_decnet ) {
[0]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
[807]198 if ( fh == -1 )
199  roar_errno = ROAR_ERROR_CONNREFUSED;
200
[0]201 ROAR_DBG("roar_connect_raw(*) = %i", fh);
202
203 return fh;
204}
205
206int roar_connect    (struct roar_connection * con, char * server) {
[1315]207 int fh;
208
209 if ( con == NULL ) {
210  roar_errno = ROAR_ERROR_INVAL;
211  return -1;
212 }
213
[807]214 roar_errno = ROAR_ERROR_UNKNOWN;
[1315]215 fh = roar_connect_raw(server);
[0]216
[1315]217 if ( fh == -1 )
[0]218  return -1;
219
[1315]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
[3869]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;
[1315]241
[807]242 roar_errno = ROAR_ERROR_NONE;
[0]243 return 0;
244}
245
[1660]246int roar_get_connection_fh (struct roar_connection * con) {
[3869]247 roar_debug_warn_sysio("roar_get_connection_fh", "roar_get_connection_vio2", NULL);
[2809]248
[2043]249 if ( con == NULL )
250  return -1;
251
[3869]252 if ( !(con->flags & ROAR_CON_FLAGS_FH) )
253  return -1;
254
[1660]255 return con->__fh;
256}
257
[2043]258int roar_get_connection_vio (struct roar_connection * con, struct roar_vio_calls * vio) {
[3869]259 roar_debug_warn_obsolete("roar_get_connection_vio", "roar_get_connection_vio2", NULL);
260
[2043]261 if ( con == NULL || vio == NULL )
262  return -1;
263
[3869]264 if ( !(con->flags & ROAR_CON_FLAGS_FH) )
265  return -1;
266
[2809]267 return roar_vio_open_fh_socket(vio, con->__fh);
[2043]268}
269
[3869]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
[0]282int roar_disconnect (struct roar_connection * con) {
[3869]283 struct roar_vio_calls * vio;
[0]284 struct roar_message m;
285
[3875]286 memset(&m, 0, sizeof(m));
287
[0]288 m.datalen = 0;
289 m.stream  = 0;
[133]290 m.pos     = 0;
[0]291 m.cmd     = ROAR_CMD_QUIT;
292
293 roar_req(con, &m, NULL);
294
[3869]295 if ( (vio = roar_get_connection_vio2(con)) != NULL ) {
296  roar_vio_close(vio);
[2809]297 }
[0]298
[1660]299 roar_connect_fh(con, -2);
[0]300
[807]301 roar_errno = ROAR_ERROR_NONE;
302
[0]303 return 0;
304}
305
[3913]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
[3914]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}
[3913]328
[3836]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
[0]343int roar_identify   (struct roar_connection * con, char * name) {
344 struct roar_message mes;
345 pid_t pid;
346 int max_len;
347
[807]348 roar_errno = ROAR_ERROR_UNKNOWN;
349
[0]350 ROAR_DBG("roar_identify(*): try to identify myself...");
351
[3875]352 memset(&mes, 0, sizeof(mes));
353
[0]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.