source: roaraudio/libroar/enumdev.c @ 4413:827dec01806f

Last change on this file since 4413:827dec01806f was 4356:33238471cffd, checked in by phi, 14 years ago

force ROAR_ENUM_FLAG_NONBLOCK if ROAR_ENUM_FLAG_HARDNONBLOCK is set

File size: 5.3 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, unic;
120 ssize_t r;
121 int testflags = flags;
122 int is_uniq;
123
124 // load config:
125 roar_libroar_get_config();
126
127 if ( flags & ROAR_ENUM_FLAG_HARDNONBLOCK )
128  flags |= ROAR_ENUM_FLAG_NONBLOCK;
129
130 if ( testflags & ROAR_ENUM_FLAG_DESC )
131  testflags -= ROAR_ENUM_FLAG_DESC;
132 if ( testflags & ROAR_ENUM_FLAG_LOCATION )
133  testflags -= ROAR_ENUM_FLAG_LOCATION;
134
135 for (i = 0; i < sizeof(_libroar_locmod)/sizeof(*_libroar_locmod); i++) {
136  if ( (_libroar_locmod[i].supflags & testflags) == testflags ) {
137   r = _libroar_locmod[i].func(flags, dir, socktype, &(servers[have-1]), (sizeof(servers)/sizeof(*servers)) - have);
138   if ( r > 0 )
139    have += r;
140  }
141 }
142
143 ret = roar_mm_malloc(have*sizeof(struct roar_server));
144
145 if (ret == NULL)
146  return NULL;
147
148 have--;
149
150 for (i = cp = 0; i < have; i++) {
151  c = &(ret[cp]);
152  c->server = servers[i];
153  c->description = NULL;
154  c->location = NULL;
155
156  // uniq test:
157  is_uniq = 1;
158  for (unic = 0; unic < cp; unic++)
159   if ( !strcmp(ret[unic].server, servers[i]) )
160    is_uniq = 0;
161
162  if ( is_uniq && _test_server(c, flags) == 0 ) {
163   cp++;
164  } else {
165   roar_mm_free(servers[i]);
166  }
167 }
168
169 ret[cp].server = NULL;
170 ret[cp].description = roar_mm_strdup("Default server");
171 ret[cp].location = NULL;
172
173 return ret;
174}
175
176int roar_enum_servers_free(struct roar_server * servs) {
177 struct roar_server * c;
178 int i;
179
180 if ( servs == NULL )
181  return -1;
182
183 for (i = 0; (c = &(servs[i]))->server != NULL; i++) {
184  roar_mm_free((void*)c->server);
185  if ( c->description != NULL )
186   roar_mm_free((void*)c->description);
187  if ( c->location != NULL )
188   roar_mm_free((void*)c->location);
189 }
190
191 if ( c->description != NULL )
192  roar_mm_free((void*)c->description);
193 if ( c->location != NULL )
194  roar_mm_free((void*)c->location);
195
196 roar_mm_free(servs);
197
198 return 0;
199}
200
201ssize_t roar_enum_servers_num(struct roar_server * servs) {
202 size_t ret;
203
204 if ( servs == NULL )
205  return -1;
206
207 for (ret = 0; servs[ret].server != NULL; ret++);
208
209 return ret;
210}
211
212//ll
Note: See TracBrowser for help on using the repository browser.