source: roaraudio/roarclients/roarpluginrunner.c @ 5341:6999cabaaf63

Last change on this file since 5341:6999cabaaf63 was 5341:6999cabaaf63, checked in by phi, 12 years ago

added support to set args, appname and abiversion

File size: 7.3 KB
Line 
1//roarpluginrunner.c:
2
3/*
4 *      Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2011
5 *
6 *  This file is part of roarclients 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 *  RoarAudio 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 */
25
26int g_verbose = 0;
27#define ROAR_DBG_INFOVAR g_verbose
28
29#include <roaraudio.h>
30
31enum action {
32 RUN,
33 EXPLAIN
34};
35
36static struct roar_dl_librarypara * g_para = NULL;
37
38void usage (const char * progname) {
39 fprintf(stderr, "Usage: %s [OPTIONS]... PLUGIN\n", progname);
40
41 fprintf(stderr, "\nOptions:\n\n");
42
43 fprintf(stderr, " -h --help         - This help.\n"
44                 "    --verbose      - Be verbose. Can be used multiple times.\n"
45                 "    --run          - Run plugin.\n"
46                 "    --explain      - Explain plugin.\n");
47}
48
49static int do_run(const char * name) {
50 struct roar_dl_lhandle * lhandle = roar_dl_open(name, ROAR_DL_FLAG_DEFAULTS, 1, g_para);
51
52 if ( lhandle == NULL )
53  return -1;
54
55 roar_dl_appsched_trigger(lhandle, ROAR_DL_APPSCHED_INIT);
56
57 while (roar_dl_appsched_trigger(lhandle, ROAR_DL_APPSCHED_WAIT) == 0)
58  roar_dl_appsched_trigger(lhandle, ROAR_DL_APPSCHED_UPDATE);
59
60 roar_dl_appsched_trigger(lhandle, ROAR_DL_APPSCHED_UPDATE);
61 roar_dl_appsched_trigger(lhandle, ROAR_DL_APPSCHED_FREE);
62
63 roar_dl_unref(lhandle);
64 return 0;
65}
66
67static const char * _ptr2str(const void * p) {
68 static char buf[24];
69
70 if ( p == NULL )
71  return "<not set>";
72
73 snprintf(buf, sizeof(buf), "%p", p);
74
75 return buf;
76}
77
78static const char * _ptrrange2str(const void * p, size_t len) {
79 static char buf[48];
80
81 if ( p == NULL )
82  return "<not set>";
83
84 if ( len == 0 )
85  return _ptr2str(p);
86
87 snprintf(buf, sizeof(buf), "%p-%p", p, p + len);
88
89 return buf;
90}
91
92static int do_explain(const char * name) {
93 struct roar_dl_lhandle * lhandle = roar_dl_open(name, ROAR_DL_FLAG_DEFAULTS, 0, NULL);
94 struct roar_dl_libraryinst * (*func)(struct roar_dl_librarypara * para);
95 struct roar_dl_libraryinst * lib;
96 int libok = 0, libnameok = 0;
97 int tmp;
98 int i;
99
100 if ( lhandle == NULL )
101  return -1;
102
103 func = roar_dl_getsym(lhandle, "_roaraudio_library_init", -1);
104 if ( func == NULL ) {
105  fprintf(stderr, "Warning: Not a RA lib: %s\n", name);
106  roar_dl_unref(lhandle);
107  return 0;
108 }
109
110 lib = func(g_para);
111 if ( lib == NULL ) {
112  fprintf(stderr, "Warning: Can not RA init: %s\n", name);
113  roar_dl_unref(lhandle);
114  return 0;
115 }
116
117 if ( lib->version == ROAR_DL_LIBINST_VERSION && lib->len == sizeof(*lib) ) {
118  libok = 1;
119 }
120
121 printf("lib                     = %s\n", _ptr2str(lib));
122 printf("|-> version             = %i (%smatch)\n", lib->version, lib->version == ROAR_DL_LIBINST_VERSION ? "" : "no ");
123 printf("%c-> len                 = %zu (%smatch)\n", libok ? '|' : '\\', lib->len, lib->len == sizeof(*lib) ? "" : "no ");
124
125 if ( libok ) {
126  printf("|-> unload              = %s\n", _ptr2str(lib->unload));
127  printf("|-> func                = {");
128  for (i = 0, tmp = 0; i < ROAR_DL_FN_MAX; i++) {
129   if ( lib->func[i] != NULL ) {
130    tmp = 1;
131    break;
132   }
133  }
134  if ( tmp ) {
135   for (i = 0; i < ROAR_DL_FN_MAX; i++)
136    printf("%s%s", _ptr2str(lib->func[i]), i < (ROAR_DL_FN_MAX-1) ? ", " : "");
137  } else {
138   printf("%i x <not set>", (int)ROAR_DL_FN_MAX);
139  }
140  printf("}\n");
141  printf("|-> libname             = %s\n", _ptr2str(lib->libname));
142  if ( lib->libname != NULL ) {
143   if ( lib->libname->version == ROAR_DL_LIBNAME_VERSION && lib->libname->len == sizeof(*(lib->libname)) ) {
144    libnameok = 1;
145   }
146   printf("|   |-> version         = %i (%smatch)\n", lib->libname->version,
147                                                      lib->libname->version == ROAR_DL_LIBNAME_VERSION ? "" : "no ");
148   printf("|   %c-> len             = %zu (%smatch)\n", libnameok ? '|' : '\\', lib->libname->len,
149                                                      lib->libname->len == sizeof(*(lib->libname)) ? "" : "no ");
150   if ( libnameok ) {
151#define _ps(c,name) \
152    tmp = (lib->libname->name) != NULL; \
153    printf("|   %c-> %-15s = %s%s%s\n", (c), #name, tmp ? "\"" : "", \
154                                    tmp ? (lib->libname->name) : "<not set>", tmp ? "\"" : "");
155
156    _ps('|', name);
157    _ps('|', libname);
158    _ps('|', libversion);
159    _ps('|', abiversion);
160    _ps('|', description);
161    _ps('|', contact);
162    _ps('|', authors);
163    _ps('\\', license);
164#undef _ps
165   }
166  }
167  printf("|-> global_data_len     = %zu\n", lib->global_data_len);
168  printf("|-> global_data_init    = %s\n", _ptrrange2str(lib->global_data_init, lib->global_data_len));
169  printf("|-> global_data_pointer = %s\n", _ptr2str(lib->global_data_pointer));
170  printf("|-> libdep              = %s\n", _ptr2str(lib->libdep));
171  printf("|-> libdep_len          = %zu\n", lib->libdep_len);
172  printf("\\-> appsched            = %s\n", _ptr2str(lib->appsched));
173  if ( lib->appsched != NULL ) {
174   printf("    |-> init            = %s\n", _ptr2str(lib->appsched->init));
175   printf("    |-> free            = %s\n", _ptr2str(lib->appsched->free));
176   printf("    |-> update          = %s\n", _ptr2str(lib->appsched->update));
177   printf("    |-> tick            = %s\n", _ptr2str(lib->appsched->tick));
178   printf("    \\-> wait            = %s\n", _ptr2str(lib->appsched->wait));
179  }
180 }
181
182 roar_dl_unref(lhandle);
183 return 0;
184}
185
186static int do_plugin(enum action action, const char * name) {
187 switch (action) {
188  case EXPLAIN:
189    return do_explain(name);
190   break;
191  case RUN:
192    return do_run(name);
193   break;
194  default:
195    roar_err_set(ROAR_ERROR_BADRQC);
196    return -1;
197   break;
198 }
199}
200
201static inline void _clear_para(void) {
202 if ( g_para == NULL )
203  return;
204
205 roar_dl_para_unref(g_para);
206 g_para = NULL;
207}
208
209int main (int argc, char * argv[]) {
210 const char * appname    = "roarpluginrunner";
211 const char * abiversion = NULL;
212 const char * pluginargs = NULL;
213 enum action action = RUN;
214 int ret = 0;
215 int i;
216 char * k;
217
218 for (i = 1; i < argc; i++) {
219  k = argv[i];
220
221  if ( !strcmp(k, "-h") || !strcmp(k, "--help") ) {
222   usage(argv[0]);
223   return 0;
224  } else if ( !strcmp(k, "--run") ) {
225   action = RUN;
226  } else if ( !strcmp(k, "--explain") ) {
227   action = EXPLAIN;
228  } else if ( !strcmp(k, "--verbose") ) {
229   g_verbose++;
230  } else if ( !strcmp(k, "--appname") ) {
231   appname = argv[++i];
232   _clear_para();
233  } else if ( !strcmp(k, "--abiversion") ) {
234   abiversion = argv[++i];
235   _clear_para();
236  } else if ( !strcmp(k, "--args") ) {
237   pluginargs = argv[++i];
238   _clear_para();
239  } else {
240   if ( g_para == NULL )
241    g_para = roar_dl_para_new(pluginargs, NULL, appname, abiversion);
242   roar_err_set(ROAR_ERROR_NONE);
243   if ( do_plugin(action, k) == -1 ) {
244    fprintf(stderr, "Error loading plugin: %s\n",
245                    roar_error != ROAR_ERROR_NONE ? roar_error2str(roar_error) : roar_dl_errstr(NULL));
246    ret = 1;
247   }
248  }
249 }
250
251 _clear_para();
252
253 return ret;
254}
255
256//ll
Note: See TracBrowser for help on using the repository browser.