source: roaraudio/libroar/enumdev.c @ 4435:32e641dd7526

Last change on this file since 4435:32e641dd7526 was 4435:32e641dd7526, checked in by phi, 8 years ago

use data from server_info for devenum

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