source: roaraudio/libroar/basic.c @ 4780:814516280a3b

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

avoid some warnings on win32

File size: 10.1 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#ifdef ROAR_HAVE_LIBSLP
40 struct roar_libroar_config * config = roar_libroar_get_config();
41#endif
42 char user_sock[80];
43 char * roar_server;
44 int i = 0;
45 int port = 0;
46 int fh = -1;
47 int is_decnet = 0;
48 char * obj = NULL;
49#if !defined(ROAR_TARGET_WIN32) && !defined(ROAR_TARGET_MICROCONTROLLER)
50 struct passwd * pwd;
51#endif
52#ifdef ROAR_HAVE_LIBDNET
53 struct stat decnet_stat;
54#endif
55#ifdef ROAR_HAVE_LIBX11
56 struct roar_x11_connection * x11con;
57#endif
58
59 roar_errno = ROAR_ERROR_UNKNOWN;
60
61 if ( server == NULL )
62  server = roar_libroar_get_server();
63
64 if ( server == NULL && (roar_server = getenv("ROAR_SERVER")) != NULL )
65  server = roar_server;
66
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
76#if !defined(ROAR_TARGET_WIN32) && !defined(ROAR_TARGET_MICROCONTROLLER)
77 if ( server == NULL && (i = readlink("/etc/roarserver", user_sock, 79)) != -1 ) {
78   user_sock[i] = 0;
79   server = user_sock;
80 }
81#endif
82
83 if ( server != NULL && !strcasecmp(server, "+slp") ) {
84  server = roar_slp_find_roard(0);
85  if ( server == NULL ) {
86   return -1;
87  }
88 }
89
90 if ( server == NULL || *server == 0 ) {
91  /* connect via defaults */
92
93#ifdef ROAR_HAVE_UNIX
94#ifndef ROAR_TARGET_MICROCONTROLLER
95  roar_server = getenv("HOME");
96#else
97  roar_server = NULL;
98#endif
99
100  if ( roar_server == NULL ) {
101#if !defined(ROAR_TARGET_WIN32) && !defined(ROAR_TARGET_MICROCONTROLLER)
102   if ( (pwd = getpwuid(getuid())) == NULL ) {
103    roar_server = "/NX-HOME-DIR";
104   } else {
105    roar_server = pwd->pw_dir;
106   }
107#else
108   roar_server = "/WIN32-SUCKS";
109#endif
110  }
111
112  snprintf(user_sock, 79, "%s/%s", roar_server, ROAR_DEFAULT_SOCK_USER);
113
114  if ( (fh = roar_socket_connect(user_sock, 0)) != -1 )
115   return fh;
116
117  if ( (fh = roar_socket_connect(ROAR_DEFAULT_SOCK_GLOBAL, 0)) != -1 )
118   return fh;
119#endif
120
121  if ( (fh = roar_socket_connect(ROAR_DEFAULT_HOST, ROAR_DEFAULT_PORT)) != -1 )
122   return fh;
123
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);
128    if ( (fh = roar_socket_connect(user_sock, ROAR_DEFAULT_NUM)) != -1 )
129     return fh;
130   }
131  }
132#endif
133
134  if ( (fh = roar_socket_connect("+abstract", 0)) != -1 )
135   return fh;
136
137#ifdef ROAR_HAVE_LIBSLP
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;
142
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 }
150#endif
151
152 return -1;
153
154 } else {
155  /* connect via (char*)server */
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;
162      obj = &server[i+2];
163      break;
164     }
165
166     port = atoi(server+i+1);
167     server[i] = 0;
168     break;
169    }
170   }
171  }
172
173  if ( is_decnet ) {
174    *user_sock = 0;
175   if ( *server == ':' ) {
176    if ( roar_socket_get_local_nodename() )
177     strncat(user_sock, roar_socket_get_local_nodename(), 6);
178   }
179
180   strncat(user_sock, server, 79);
181   server = user_sock;
182   if ( *obj == 0 ) {
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
188   }
189  }
190
191  if ( port || is_decnet ) {
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
200 if ( fh == -1 )
201  roar_errno = ROAR_ERROR_CONNREFUSED;
202
203 ROAR_DBG("roar_connect_raw(*) = %i", fh);
204
205 return fh;
206}
207
208int roar_connect    (struct roar_connection * con, char * server) {
209 int fh;
210
211 if ( con == NULL ) {
212  roar_errno = ROAR_ERROR_INVAL;
213  return -1;
214 }
215
216 roar_errno = ROAR_ERROR_UNKNOWN;
217 fh = roar_connect_raw(server);
218
219 if ( fh == -1 )
220  return -1;
221
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
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;
243
244 roar_errno = ROAR_ERROR_NONE;
245 return 0;
246}
247
248int roar_get_connection_fh (struct roar_connection * con) {
249 roar_debug_warn_sysio("roar_get_connection_fh", "roar_get_connection_vio2", NULL);
250
251 if ( con == NULL )
252  return -1;
253
254 if ( !(con->flags & ROAR_CON_FLAGS_FH) )
255  return -1;
256
257 return con->__fh;
258}
259
260int roar_get_connection_vio (struct roar_connection * con, struct roar_vio_calls * vio) {
261 roar_debug_warn_obsolete("roar_get_connection_vio", "roar_get_connection_vio2", NULL);
262
263 if ( con == NULL || vio == NULL )
264  return -1;
265
266 if ( !(con->flags & ROAR_CON_FLAGS_FH) )
267  return -1;
268
269 return roar_vio_open_fh_socket(vio, con->__fh);
270}
271
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
284int roar_disconnect (struct roar_connection * con) {
285 struct roar_vio_calls * vio;
286 struct roar_message m;
287
288 memset(&m, 0, sizeof(m));
289
290 m.datalen = 0;
291 m.stream  = 0;
292 m.pos     = 0;
293 m.cmd     = ROAR_CMD_QUIT;
294
295 roar_req(con, &m, NULL);
296
297 if ( (vio = roar_get_connection_vio2(con)) != NULL ) {
298  roar_vio_close(vio);
299 }
300
301 roar_connect_fh(con, -2);
302
303 roar_errno = ROAR_ERROR_NONE;
304
305 return 0;
306}
307
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
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}
330
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
345int roar_identify   (struct roar_connection * con, char * name) {
346 struct roar_message mes;
347 pid_t pid;
348 int max_len;
349
350 roar_errno = ROAR_ERROR_UNKNOWN;
351
352 ROAR_DBG("roar_identify(*): try to identify myself...");
353
354 memset(&mes, 0, sizeof(mes));
355
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.