[0] | 1 | //basic.c: |
---|
| 2 | |
---|
[690] | 3 | /* |
---|
[3372] | 4 | * Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2008-2010 |
---|
[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) { |
---|
| 39 | char user_sock[80]; |
---|
| 40 | char * roar_server; |
---|
[701] | 41 | int i = 0; |
---|
[0] | 42 | int port = 0; |
---|
| 43 | int fh = -1; |
---|
[501] | 44 | int is_decnet = 0; |
---|
[516] | 45 | char * obj = NULL; |
---|
[1475] | 46 | #if !defined(ROAR_TARGET_WIN32) && !defined(ROAR_TARGET_MICROCONTROLLER) |
---|
[1026] | 47 | struct passwd * pwd; |
---|
[1393] | 48 | #endif |
---|
[828] | 49 | #ifdef ROAR_HAVE_LIBDNET |
---|
| 50 | struct stat decnet_stat; |
---|
| 51 | #endif |
---|
[3372] | 52 | #ifdef ROAR_HAVE_LIBX11 |
---|
| 53 | struct roar_x11_connection * x11con; |
---|
| 54 | #endif |
---|
[0] | 55 | |
---|
[807] | 56 | roar_errno = ROAR_ERROR_UNKNOWN; |
---|
| 57 | |
---|
[2897] | 58 | // load config |
---|
| 59 | roar_libroar_get_config(); |
---|
| 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 |
---|
[2014] | 138 | if ( (server = roar_slp_find_roard(0)) != NULL ) |
---|
| 139 | if ( (fh = roar_connect_raw(server)) != -1 ) |
---|
| 140 | return fh; |
---|
| 141 | |
---|
| 142 | /* in case we can not connect to the server given this may be a cache problem, |
---|
| 143 | we do a new lookup with the cache disabled in this case */ |
---|
| 144 | ROAR_WARN("roar_connect_raw(*): Can not connect to SLP located server, disabling cache"); |
---|
| 145 | if ( (server = roar_slp_find_roard(1)) != NULL ) |
---|
| 146 | if ( (fh = roar_connect_raw(server)) != -1 ) |
---|
| 147 | return fh; |
---|
[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 | |
---|
| 206 | int 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 | |
---|
| 223 | int 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] | 246 | int 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] | 258 | int 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] | 270 | struct 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] | 282 | int 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] | 306 | int 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] | 320 | int roar_sync (struct roar_connection * con) { |
---|
| 321 | // wait for any non-client reqs |
---|
| 322 | return roar_wait_msg(con, 0x0000, 0x8000); |
---|
| 323 | } |
---|
| 324 | |
---|
| 325 | int roar_wait_msg (struct roar_connection * con, int16_t seq, int16_t seqmask) { |
---|
| 326 | return -1; |
---|
| 327 | } |
---|
[3913] | 328 | |
---|
[3836] | 329 | int 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] | 343 | int 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 | |
---|
| 383 | int 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 | |
---|
| 396 | int 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 |
---|