[5986] | 1 | //filter-slfi-alternative.c: |
---|
| 2 | |
---|
| 3 | /* |
---|
| 4 | * Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2012-2014 |
---|
| 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 <roaraudio.h> |
---|
| 27 | #include <libroarlight/libroarlight.h> |
---|
| 28 | |
---|
| 29 | #define MAX_FILTER 32 |
---|
| 30 | |
---|
| 31 | struct slfi_alternative { |
---|
| 32 | struct roar_slfi_inst * filter[MAX_FILTER]; |
---|
| 33 | size_t filter_len; |
---|
| 34 | size_t current; |
---|
| 35 | ssize_t channel; |
---|
| 36 | uint8_t event; |
---|
| 37 | }; |
---|
| 38 | |
---|
| 39 | static int __cb_event_add(struct roar_slfi_inst * inst, void * userdata, uint8_t event) { |
---|
| 40 | (void)inst; |
---|
| 41 | return roar_slfi_event_add(userdata, event); |
---|
| 42 | } |
---|
| 43 | |
---|
| 44 | static int __init(struct roar_slfi_inst * inst, const struct roar_keyval * para, ssize_t paralen) { |
---|
| 45 | struct slfi_alternative * self = roar_mm_malloc(sizeof(struct slfi_alternative)); |
---|
[5987] | 46 | const struct roar_keyval * kv; |
---|
| 47 | ssize_t i; |
---|
[5986] | 48 | |
---|
| 49 | if ( self == NULL ) |
---|
| 50 | return -1; |
---|
| 51 | |
---|
| 52 | self->filter_len = 0; |
---|
| 53 | self->current = 0; |
---|
| 54 | self->channel = -1; |
---|
| 55 | self->event = ROAR_ROARDMX_EVENT_STEP; |
---|
| 56 | inst->userdata = self; |
---|
| 57 | |
---|
[5987] | 58 | for (i = 0; i < paralen; i++) { |
---|
| 59 | kv = &(para[i]); |
---|
| 60 | if ( kv->key == NULL || kv->value == NULL ) |
---|
| 61 | continue; |
---|
| 62 | if ( !strcmp(kv->key, "channel") ) { |
---|
| 63 | self->channel = atoi(kv->value); |
---|
[5993] | 64 | if ( self->channel < 0 ) |
---|
| 65 | self->channel = 0; |
---|
[5987] | 66 | } else if ( !strcmp(kv->key, "event") ) { |
---|
| 67 | self->event = roar_roardmx_str2event(kv->value); |
---|
| 68 | } else { |
---|
| 69 | ROAR_WARN("__init(*): Unknown parameter: %s", kv->key); |
---|
| 70 | } |
---|
| 71 | } |
---|
| 72 | |
---|
[5986] | 73 | return 0; |
---|
| 74 | } |
---|
| 75 | |
---|
| 76 | static int __uninit(struct roar_slfi_inst * inst) { |
---|
| 77 | struct slfi_alternative * self = inst->userdata; |
---|
| 78 | size_t i; |
---|
| 79 | |
---|
| 80 | for (i = 0; i < self->filter_len; i++) { |
---|
| 81 | roar_slfi_unref(self->filter[i]); |
---|
| 82 | } |
---|
| 83 | |
---|
| 84 | return 0; |
---|
| 85 | } |
---|
| 86 | |
---|
| 87 | static int __update(struct roar_slfi_inst * inst, uint8_t * universe, ssize_t size_of_universe, int32_t usecspassed, const uint8_t * event, size_t eventlen) { |
---|
| 88 | struct slfi_alternative * self = inst->userdata; |
---|
| 89 | size_t i; |
---|
| 90 | |
---|
| 91 | // Don't do anything if we have no childs: |
---|
| 92 | if ( self->filter_len == 0 ) |
---|
| 93 | return 0; |
---|
| 94 | |
---|
| 95 | if ( self->channel == (ssize_t)-1 ) { |
---|
| 96 | for (i = 0; i < eventlen; i++) { |
---|
| 97 | if ( event[i] == self->event ) { |
---|
| 98 | self->current++; |
---|
| 99 | if ( self->current == self->filter_len ) |
---|
| 100 | self->current = 0; |
---|
| 101 | break; |
---|
| 102 | } |
---|
| 103 | } |
---|
| 104 | } else if ( size_of_universe > self->channel ) { |
---|
| 105 | self->current = universe[self->channel] / self->filter_len; |
---|
| 106 | if ( self->current >= self->filter_len ) |
---|
| 107 | self->current = self->filter_len - 1; |
---|
| 108 | } else { |
---|
| 109 | ROAR_WARN("__update(*): Universe too small for filter (source channel=%lu).", (unsigned long int)self->channel); |
---|
| 110 | roar_err_set(ROAR_ERROR_NOENT); |
---|
| 111 | return -1; |
---|
| 112 | } |
---|
| 113 | |
---|
| 114 | return roar_slfi_update(self->filter[self->current], universe, size_of_universe, usecspassed, event, eventlen); |
---|
| 115 | } |
---|
| 116 | |
---|
| 117 | static int __ctl(struct roar_slfi_inst * inst, enum roar_slfi_command command, void * argp) { |
---|
| 118 | struct slfi_alternative * self = inst->userdata; |
---|
| 119 | |
---|
| 120 | switch (command) { |
---|
| 121 | case ROAR_SLFI_CMD_PUSH: |
---|
| 122 | if ( self->filter_len == MAX_FILTER ) { |
---|
| 123 | roar_err_set(ROAR_ERROR_NOSPC); |
---|
| 124 | return -1; |
---|
| 125 | } |
---|
| 126 | if ( roar_slfi_ref(argp) == -1 ) |
---|
| 127 | return -1; |
---|
| 128 | roar_slfi_cb_set_event_add(argp, __cb_event_add, inst); |
---|
| 129 | self->filter[self->filter_len++] = argp; |
---|
| 130 | return 0; |
---|
| 131 | break; |
---|
| 132 | #ifndef DEBUG |
---|
| 133 | default: |
---|
| 134 | break; |
---|
| 135 | #endif |
---|
| 136 | } |
---|
| 137 | |
---|
| 138 | roar_err_set(ROAR_ERROR_BADRQC); |
---|
| 139 | return -1; |
---|
| 140 | } |
---|
| 141 | |
---|
| 142 | static const struct roar_slfi_filter filter[1] = { |
---|
| 143 | { |
---|
[5987] | 144 | .name = "alternative", |
---|
| 145 | .description = "Alternative SLFI filter", |
---|
[5986] | 146 | .flags = ROAR_SLFI_FLAG_NONE, |
---|
| 147 | .init = __init, |
---|
| 148 | .uninit = __uninit, |
---|
| 149 | .update = __update, |
---|
| 150 | .ctl = __ctl |
---|
| 151 | } |
---|
| 152 | }; |
---|
| 153 | |
---|
| 154 | ROAR_DL_PLUGIN_REG_SLFI(filter); |
---|
| 155 | |
---|
| 156 | // This is the plugin control block. |
---|
| 157 | ROAR_DL_PLUGIN_START(filter_slfi_alternative) { |
---|
| 158 | // Here we set the name and vendor of our plugin. |
---|
| 159 | // If you have no Vendor ID you need to use ROAR_DL_PLUGIN_META_PRODUCT_NV(). |
---|
| 160 | ROAR_DL_PLUGIN_META_PRODUCT_NIV("filter-slfi-alternative", ROAR_VID_ROARAUDIO, ROAR_VNAME_ROARAUDIO); |
---|
| 161 | |
---|
| 162 | // This sets the version of your plugin. |
---|
| 163 | ROAR_DL_PLUGIN_META_VERSION(ROAR_VERSION_STRING); |
---|
| 164 | |
---|
| 165 | // This sets the license of your plugin. |
---|
| 166 | // If there is no tag for the license you use you can just |
---|
| 167 | // use ROAR_DL_PLUGIN_META_LICENSE(). |
---|
| 168 | ROAR_DL_PLUGIN_META_LICENSE_TAG(GPLv3_0); |
---|
| 169 | |
---|
| 170 | // This sets the author and contact infos. |
---|
| 171 | // There are several other macros to do this with other parameters. |
---|
| 172 | // See ROAR_DL_PLUGIN_META_CONTACT*() in the header or documentation. |
---|
| 173 | ROAR_DL_PLUGIN_META_CONTACT_FLNE("Philipp", "Schafft", "ph3-der-loewe", "lion@lion.leolix.org"); |
---|
| 174 | |
---|
| 175 | // This sets the description for your plugin. |
---|
| 176 | ROAR_DL_PLUGIN_META_DESC("This is a container plugin for SLFI filter alternatives."); |
---|
| 177 | |
---|
| 178 | // Load filters. |
---|
| 179 | ROAR_DL_PLUGIN_REG_FNFUNC(ROAR_DL_FN_FILTER); |
---|
| 180 | |
---|
| 181 | // This is the end of the control block. |
---|
| 182 | } ROAR_DL_PLUGIN_END |
---|
| 183 | |
---|
| 184 | //ll |
---|