source: roaraudio/libroar/enumdev.c @ 4353:afd8ee9a2999

Last change on this file since 4353:afd8ee9a2999 was 4353:afd8ee9a2999, checked in by phi, 14 years ago

support default address in enum support

File size: 5.0 KB
Line 
1//enumdev.c:
2
3/*
4 *      Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2010
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
38// TODO: we should put all the data in one big alloced block.
39
40static int _test_server(struct roar_server * c, int flags) {
41 struct roar_connection con;
42 if ( c->server == NULL )
43  return -1;
44
45 if ( flags & ROAR_ENUM_FLAG_NONBLOCK )
46  return 0;
47
48 if ( roar_connect(&con, (char*)c->server) == -1 )
49  return -1;
50
51 roar_disconnect(&con);
52
53 return 0;
54}
55
56#define _add(x) if ( (x) != NULL ) servers[ret++] = roar_mm_strdup((x))
57static ssize_t _esl_defaults(int flags, int dir, int socktype, char ** servers, size_t maxlen) {
58#ifdef ROAR_HAVE_LIBX11
59 struct roar_x11_connection * x11con;
60#endif
61 ssize_t ret = 0;
62 const char * new;
63 char buf[1024];
64 int i;
65
66 if ( maxlen < 10 )
67  return -1;
68
69 new = roar_libroar_get_server();
70 _add(new);
71
72 new = getenv("ROAR_SERVER");
73 _add(new);
74
75#ifdef ROAR_HAVE_LIBX11
76 if ( (x11con = roar_x11_connect(NULL)) != NULL ) {
77  new = roar_x11_get_prop(x11con, "ROAR_SERVER");
78  _add(new);
79  roar_x11_disconnect(x11con);
80 }
81#endif
82
83#if !defined(ROAR_TARGET_WIN32) && !defined(ROAR_TARGET_MICROCONTROLLER)
84 if ( (i = readlink("/etc/roarserver", buf, sizeof(buf)-1)) != -1 ) {
85   buf[i] = 0;
86   _add(buf);
87 }
88#endif
89
90 if ( (new = roar_env_get_home(0)) != NULL ) {
91  snprintf(buf, sizeof(buf)-1, "%s/%s", new, ROAR_DEFAULT_SOCK_USER);
92  buf[sizeof(buf)-1] = 0;
93  _add(buf);
94 }
95
96 servers[ret++] = roar_mm_strdup(ROAR_DEFAULT_SOCK_GLOBAL);
97 servers[ret++] = roar_mm_strdup(ROAR_DEFAULT_HOST);
98 servers[ret++] = roar_mm_strdup("::" ROAR_DEFAULT_OBJECT);
99 servers[ret++] = roar_mm_strdup("+abstract");
100 servers[ret++] = roar_mm_strdup("/tmp/muroard");
101
102 return ret;
103}
104
105struct locmed {
106 int supflags;
107 ssize_t (*func)(int flags, int dir, int socktype, char ** servers, size_t maxlen);
108};
109
110static struct locmed _libroar_locmod[] = {
111 {ROAR_ENUM_FLAG_NONBLOCK|ROAR_ENUM_FLAG_HARDNONBLOCK, _esl_defaults}
112};
113
114struct roar_server * roar_enum_servers(int flags, int dir, int socktype) {
115 struct roar_server * ret = NULL;
116 struct roar_server * c;
117 char * servers[64];
118 size_t have = 1;
119 size_t i, cp;
120 ssize_t r;
121 int testflags = flags;
122
123 // load config:
124 roar_libroar_get_config();
125
126 if ( testflags & ROAR_ENUM_FLAG_DESC )
127  testflags -= ROAR_ENUM_FLAG_DESC;
128 if ( testflags & ROAR_ENUM_FLAG_LOCATION )
129  testflags -= ROAR_ENUM_FLAG_LOCATION;
130
131 for (i = 0; i < sizeof(_libroar_locmod)/sizeof(*_libroar_locmod); i++) {
132  if ( (_libroar_locmod[i].supflags & testflags) == testflags ) {
133   r = _libroar_locmod[i].func(flags, dir, socktype, &(servers[have-1]), (sizeof(servers)/sizeof(*servers)) - have);
134   if ( r > 0 )
135    have += r;
136  }
137 }
138
139 ret = roar_mm_malloc(have*sizeof(struct roar_server));
140
141 if (ret == NULL)
142  return NULL;
143
144 have--;
145
146 for (i = cp = 0; i < have; i++) {
147  c = &(ret[cp]);
148  c->server = servers[i];
149  c->description = NULL;
150  c->location = NULL;
151  if ( _test_server(c, flags) == 0 ) {
152   cp++;
153  } else {
154   roar_mm_free(servers[i]);
155  }
156 }
157
158 ret[cp].server = NULL;
159 ret[cp].description = roar_mm_strdup("Default server");
160 ret[cp].location = NULL;
161
162 return ret;
163}
164
165int roar_enum_servers_free(struct roar_server * servs) {
166 struct roar_server * c;
167 int i;
168
169 if ( servs == NULL )
170  return -1;
171
172 for (i = 0; (c = &(servs[i]))->server != NULL; i++) {
173  roar_mm_free((void*)c->server);
174  if ( c->description != NULL )
175   roar_mm_free((void*)c->description);
176  if ( c->location != NULL )
177   roar_mm_free((void*)c->location);
178 }
179
180 if ( c->description != NULL )
181  roar_mm_free((void*)c->description);
182 if ( c->location != NULL )
183  roar_mm_free((void*)c->location);
184
185 roar_mm_free(servs);
186
187 return 0;
188}
189
190ssize_t roar_enum_servers_num(struct roar_server * servs) {
191 size_t ret;
192
193 if ( servs == NULL )
194  return -1;
195
196 for (ret = 0; servs[ret].server != NULL; ret++);
197
198 return ret;
199}
200
201//ll
Note: See TracBrowser for help on using the repository browser.