1 | //plugins.c: |
---|
2 | |
---|
3 | /* |
---|
4 | * Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2010-2011 |
---|
5 | * |
---|
6 | * This file is part of roard 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 | |
---|
26 | #include "roard.h" |
---|
27 | |
---|
28 | #define MAX_PLUGINS 8 |
---|
29 | |
---|
30 | static struct _roard_plugin { |
---|
31 | struct roar_dl_lhandle * lhandle; |
---|
32 | struct roard_plugins_sched * sched; |
---|
33 | } g_plugins[MAX_PLUGINS]; |
---|
34 | static struct _roard_plugin * _pp = NULL; |
---|
35 | |
---|
36 | static struct _roard_plugin * _find_free(void) { |
---|
37 | int i; |
---|
38 | |
---|
39 | for (i = 0; i < MAX_PLUGINS; i++) { |
---|
40 | if ( g_plugins[i].lhandle == NULL ) { |
---|
41 | memset(&(g_plugins[i]), 0, sizeof(struct _roard_plugin)); |
---|
42 | return &(g_plugins[i]); |
---|
43 | } |
---|
44 | } |
---|
45 | |
---|
46 | return NULL; |
---|
47 | } |
---|
48 | |
---|
49 | int plugins_preinit (void) { |
---|
50 | memset(g_plugins, 0, sizeof(g_plugins)); |
---|
51 | |
---|
52 | return 0; |
---|
53 | } |
---|
54 | |
---|
55 | int plugins_init (void) { |
---|
56 | int i; |
---|
57 | |
---|
58 | for (i = 0; i < MAX_PLUGINS; i++) { |
---|
59 | if ( g_plugins[i].lhandle != NULL ) { |
---|
60 | _pp = &(g_plugins[i]); |
---|
61 | |
---|
62 | _pp->sched = NULL; |
---|
63 | |
---|
64 | roar_dl_ra_init(g_plugins[i].lhandle, NULL); |
---|
65 | |
---|
66 | if ( g_plugins[i].sched != NULL ) |
---|
67 | if ( g_plugins[i].sched->init != NULL ) |
---|
68 | g_plugins[i].sched->init(); |
---|
69 | |
---|
70 | _pp = NULL; |
---|
71 | } |
---|
72 | } |
---|
73 | |
---|
74 | return 0; |
---|
75 | } |
---|
76 | |
---|
77 | int plugins_free (void) { |
---|
78 | int i; |
---|
79 | |
---|
80 | for (i = 0; i < MAX_PLUGINS; i++) { |
---|
81 | if ( g_plugins[i].lhandle != NULL ) { |
---|
82 | if ( g_plugins[i].sched != NULL ) |
---|
83 | if ( g_plugins[i].sched->free != NULL ) |
---|
84 | g_plugins[i].sched->free(); |
---|
85 | |
---|
86 | roar_dl_close(g_plugins[i].lhandle); |
---|
87 | } |
---|
88 | } |
---|
89 | |
---|
90 | return plugins_preinit(); |
---|
91 | } |
---|
92 | |
---|
93 | int plugins_update (void) { |
---|
94 | int ret = 0; |
---|
95 | int i; |
---|
96 | |
---|
97 | for (i = 0; i < MAX_PLUGINS; i++) |
---|
98 | if ( g_plugins[i].lhandle != NULL ) |
---|
99 | if ( g_plugins[i].sched != NULL ) |
---|
100 | if ( g_plugins[i].sched->update != NULL ) |
---|
101 | if ( g_plugins[i].sched->update() == -1 ) |
---|
102 | ret = -1; |
---|
103 | |
---|
104 | return ret; |
---|
105 | } |
---|
106 | |
---|
107 | int plugins_load (const char * filename) { |
---|
108 | struct _roard_plugin * next = _find_free(); |
---|
109 | |
---|
110 | if ( next == NULL ) |
---|
111 | return -1; |
---|
112 | |
---|
113 | next->lhandle = roar_dl_open(filename, -1, 0 /* we delay this until plugins_init() */); |
---|
114 | if ( next->lhandle == NULL ) { |
---|
115 | ROAR_ERR("plugins_load(filename='%s'): can not load plugin: %s", filename, roar_dl_errstr(NULL)); |
---|
116 | return -1; |
---|
117 | } |
---|
118 | |
---|
119 | return 0; |
---|
120 | } |
---|
121 | |
---|
122 | int plugins_reg_sched(struct roard_plugins_sched * sched) { |
---|
123 | if ( _pp == NULL ) |
---|
124 | return -1; |
---|
125 | |
---|
126 | _pp->sched = sched; |
---|
127 | |
---|
128 | return 0; |
---|
129 | } |
---|
130 | |
---|
131 | int plugins_reg_proto(struct roard_proto * proto) { |
---|
132 | if ( _pp == NULL ) |
---|
133 | return -1; |
---|
134 | |
---|
135 | return clients_register_proto(proto); |
---|
136 | } |
---|
137 | |
---|
138 | //ll |
---|