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