source: roaraudio/libroar/basic.c @ 4805:f4f72e985020

Last change on this file since 4805:f4f72e985020 was 4780:814516280a3b, checked in by phi, 13 years ago

avoid some warnings on win32

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