source: roaraudio/libroar/enumdev.c @ 4444:f3c427934bd3

Last change on this file since 4444:f3c427934bd3 was 4444:f3c427934bd3, checked in by phi, 13 years ago

only read server infos if requested

File size: 6.6 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 struct roar_server_info * info;
43
44 if ( c->server == NULL )
45  return -1;
46
47 if ( flags & ROAR_ENUM_FLAG_NONBLOCK )
48  return 0;
49
50 if ( roar_connect(&con, (char*)c->server) == -1 )
51  return -1;
52
53 if ( (flags & ROAR_ENUM_FLAG_DESC) || (flags & ROAR_ENUM_FLAG_LOCATION) ) {
54  info = roar_server_info(&con);
55  if ( info != NULL ) {
56   if ( info->location != NULL )
57    c->location = roar_mm_strdup(info->location);
58
59   if ( info->description != NULL )
60    c->description = roar_mm_strdup(info->description);
61
62   roar_server_info_free(info);
63  }
64 }
65
66 roar_disconnect(&con);
67
68 return 0;
69}
70
71#define _add(x) if ( (x) != NULL ) servers[ret++] = roar_mm_strdup((x))
72static ssize_t _esl_defaults(int flags, int dir, int socktype, char ** servers, size_t maxlen) {
73#ifdef ROAR_HAVE_LIBX11
74 struct roar_x11_connection * x11con;
75#endif
76 ssize_t ret = 0;
77 const char * new;
78 char buf[1024];
79 int i;
80
81 if ( maxlen < 10 )
82  return -1;
83
84 new = roar_libroar_get_server();
85 _add(new);
86
87 new = getenv("ROAR_SERVER");
88 _add(new);
89
90#ifdef ROAR_HAVE_LIBX11
91 if ( (x11con = roar_x11_connect(NULL)) != NULL ) {
92  new = roar_x11_get_prop(x11con, "ROAR_SERVER");
93  _add(new);
94  roar_x11_disconnect(x11con);
95 }
96#endif
97
98#if !defined(ROAR_TARGET_WIN32) && !defined(ROAR_TARGET_MICROCONTROLLER)
99 if ( (i = readlink("/etc/roarserver", buf, sizeof(buf)-1)) != -1 ) {
100   buf[i] = 0;
101   _add(buf);
102 }
103#endif
104
105 if ( (new = roar_env_get_home(0)) != NULL ) {
106  snprintf(buf, sizeof(buf)-1, "%s/%s", new, ROAR_DEFAULT_SOCK_USER);
107  buf[sizeof(buf)-1] = 0;
108  _add(buf);
109 }
110
111 servers[ret++] = roar_mm_strdup(ROAR_DEFAULT_SOCK_GLOBAL);
112 servers[ret++] = roar_mm_strdup(ROAR_DEFAULT_HOST);
113 servers[ret++] = roar_mm_strdup("::" ROAR_DEFAULT_OBJECT);
114 servers[ret++] = roar_mm_strdup("+abstract");
115 servers[ret++] = roar_mm_strdup("/tmp/muroard");
116
117 return ret;
118}
119
120static ssize_t _esl_slp(int flags, int dir, int socktype, char ** servers, size_t maxlen) {
121 struct roar_slp_cookie cookie;
122 int offset;
123 char * url;
124 size_t i;
125 ssize_t ret = 0;
126
127 if ( roar_slp_cookie_init(&cookie, NULL) == -1 )
128  return -1;
129
130 if ( roar_slp_search(&cookie, ROAR_SLP_URL_TYPE) == -1 )
131  return -1;
132
133 if ( cookie.matchcount == 0 )
134  return -1;
135
136 ROAR_DBG("_esl_slp(*): cookie.matchcount=%i", (int)cookie.matchcount);
137
138 for (i = 0; i < (size_t)cookie.matchcount && (ssize_t)maxlen > ret; i++) {
139  url = cookie.match[i].url;
140  ROAR_DBG("_esl_slp(*): cookie.match[%i].url='%s'", (int)i, url);
141
142  offset = 0;
143
144  if ( !strncmp(url, ROAR_SLP_URL_TYPE "://", ROAR_SLP_URL_TYPE_LEN + 3) )
145   offset = ROAR_SLP_URL_TYPE_LEN + 3;
146
147  ROAR_DBG("_esl_slp(*): url=%p, offset=%i", url, offset);
148  url = &(url[offset]);
149  ROAR_DBG("_esl_slp(*): url='%s'", url);
150
151  if ( *url == 0 )
152   continue;
153
154  _add(url);
155 }
156
157 return ret;
158}
159
160struct locmed {
161 int supflags;
162 ssize_t (*func)(int flags, int dir, int socktype, char ** servers, size_t maxlen);
163};
164
165static struct locmed _libroar_locmod[] = {
166 {ROAR_ENUM_FLAG_NONBLOCK|ROAR_ENUM_FLAG_HARDNONBLOCK, _esl_defaults},
167 {ROAR_ENUM_FLAG_NONE,                                 _esl_slp}
168};
169
170struct roar_server * roar_enum_servers(int flags, int dir, int socktype) {
171 struct roar_server * ret = NULL;
172 struct roar_server * c;
173 char * servers[64];
174 size_t have = 1;
175 size_t i, cp, unic;
176 ssize_t r;
177 int testflags = flags;
178 int is_uniq;
179
180 // load config:
181 roar_libroar_get_config();
182
183 if ( flags & ROAR_ENUM_FLAG_HARDNONBLOCK )
184  flags |= ROAR_ENUM_FLAG_NONBLOCK;
185
186 if ( testflags & ROAR_ENUM_FLAG_DESC )
187  testflags -= ROAR_ENUM_FLAG_DESC;
188 if ( testflags & ROAR_ENUM_FLAG_LOCATION )
189  testflags -= ROAR_ENUM_FLAG_LOCATION;
190
191 for (i = 0; i < sizeof(_libroar_locmod)/sizeof(*_libroar_locmod); i++) {
192  if ( (_libroar_locmod[i].supflags & testflags) == testflags ) {
193   r = _libroar_locmod[i].func(flags, dir, socktype, &(servers[have-1]), (sizeof(servers)/sizeof(*servers)) - have);
194   if ( r > 0 )
195    have += r;
196  }
197 }
198
199 ret = roar_mm_malloc(have*sizeof(struct roar_server));
200
201 if (ret == NULL)
202  return NULL;
203
204 have--;
205
206 for (i = cp = 0; i < have; i++) {
207  c = &(ret[cp]);
208  c->server = servers[i];
209  c->description = NULL;
210  c->location = NULL;
211
212  // uniq test:
213  is_uniq = 1;
214  for (unic = 0; unic < cp; unic++)
215   if ( !strcmp(ret[unic].server, servers[i]) )
216    is_uniq = 0;
217
218  if ( is_uniq && _test_server(c, flags) == 0 ) {
219   cp++;
220  } else {
221   roar_mm_free(servers[i]);
222  }
223 }
224
225 ret[cp].server = NULL;
226 ret[cp].description = roar_mm_strdup("Default server");
227 ret[cp].location = NULL;
228
229 return ret;
230}
231
232int roar_enum_servers_free(struct roar_server * servs) {
233 struct roar_server * c;
234 int i;
235
236 if ( servs == NULL )
237  return -1;
238
239 for (i = 0; (c = &(servs[i]))->server != NULL; i++) {
240  roar_mm_free((void*)c->server);
241  if ( c->description != NULL )
242   roar_mm_free((void*)c->description);
243  if ( c->location != NULL )
244   roar_mm_free((void*)c->location);
245 }
246
247 if ( c->description != NULL )
248  roar_mm_free((void*)c->description);
249 if ( c->location != NULL )
250  roar_mm_free((void*)c->location);
251
252 roar_mm_free(servs);
253
254 return 0;
255}
256
257ssize_t roar_enum_servers_num(struct roar_server * servs) {
258 size_t ret;
259
260 if ( servs == NULL )
261  return -1;
262
263 for (ret = 0; servs[ret].server != NULL; ret++);
264
265 return ret;
266}
267
268//ll
Note: See TracBrowser for help on using the repository browser.