| 1 | /* Copyright (C) 2007 L. Donnie Smith <donnie.smith@gatech.edu> |
|---|
| 2 | * |
|---|
| 3 | * This program is free software; you can redistribute it and/or modify |
|---|
| 4 | * it under the terms of the GNU General Public License as published by |
|---|
| 5 | * the Free Software Foundation; either version 2 of the License, or |
|---|
| 6 | * (at your option) any later version. |
|---|
| 7 | * |
|---|
| 8 | * This program is distributed in the hope that it will be useful, |
|---|
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 11 | * GNU General Public License for more details. |
|---|
| 12 | * |
|---|
| 13 | * You should have received a copy of the GNU General Public License |
|---|
| 14 | * along with this program; if not, write to the Free Software |
|---|
| 15 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
|---|
| 16 | * |
|---|
| 17 | */ |
|---|
| 18 | |
|---|
| 19 | #include <stdlib.h> |
|---|
| 20 | |
|---|
| 21 | #include <dlfcn.h> |
|---|
| 22 | #include <sys/types.h> |
|---|
| 23 | #include <sys/stat.h> |
|---|
| 24 | #include <unistd.h> |
|---|
| 25 | |
|---|
| 26 | #include "cwiid.h" |
|---|
| 27 | #include "conf.h" |
|---|
| 28 | #include "util.h" |
|---|
| 29 | #include "wmplugin.h" |
|---|
| 30 | |
|---|
| 31 | struct c_plugin { |
|---|
| 32 | void *handle; |
|---|
| 33 | wmplugin_init_t *init; |
|---|
| 34 | wmplugin_exec_t *exec; |
|---|
| 35 | }; |
|---|
| 36 | |
|---|
| 37 | static cwiid_wiimote_t *wiimote; |
|---|
| 38 | |
|---|
| 39 | int c_init(void) |
|---|
| 40 | { |
|---|
| 41 | return 0; |
|---|
| 42 | } |
|---|
| 43 | |
|---|
| 44 | int c_wiimote(cwiid_wiimote_t *arg_wiimote) |
|---|
| 45 | { |
|---|
| 46 | wiimote = arg_wiimote; |
|---|
| 47 | |
|---|
| 48 | return 0; |
|---|
| 49 | } |
|---|
| 50 | |
|---|
| 51 | void c_wiimote_deinit() |
|---|
| 52 | { |
|---|
| 53 | return; |
|---|
| 54 | } |
|---|
| 55 | |
|---|
| 56 | void c_deinit(void) |
|---|
| 57 | { |
|---|
| 58 | return; |
|---|
| 59 | } |
|---|
| 60 | |
|---|
| 61 | #define PLUGIN_PATHNAME_LEN 128 |
|---|
| 62 | int c_plugin_open(struct plugin *plugin, char *dir) |
|---|
| 63 | { |
|---|
| 64 | char pathname[PLUGIN_PATHNAME_LEN]; |
|---|
| 65 | struct stat buf; |
|---|
| 66 | wmplugin_info_t *info; |
|---|
| 67 | void *handle; |
|---|
| 68 | |
|---|
| 69 | snprintf(pathname, PLUGIN_PATHNAME_LEN, "%s/%s.so", dir, plugin->name); |
|---|
| 70 | if (stat(pathname, &buf)) { |
|---|
| 71 | return -1; |
|---|
| 72 | } |
|---|
| 73 | if (!(handle = dlopen(pathname, RTLD_NOW))) { |
|---|
| 74 | wminput_err(dlerror()); |
|---|
| 75 | return -1; |
|---|
| 76 | } |
|---|
| 77 | |
|---|
| 78 | plugin->type = PLUGIN_C; |
|---|
| 79 | if (!(plugin->p = malloc(sizeof(struct c_plugin)))) { |
|---|
| 80 | wminput_err("malloc error"); |
|---|
| 81 | return -1; |
|---|
| 82 | } |
|---|
| 83 | if (!(info = dlsym(handle, "wmplugin_info"))) { |
|---|
| 84 | wminput_err("Unable to load plugin info function: %s", |
|---|
| 85 | dlerror()); |
|---|
| 86 | free(plugin->p); |
|---|
| 87 | dlclose(handle); |
|---|
| 88 | return -1; |
|---|
| 89 | } |
|---|
| 90 | if (!(plugin->info = (*(wmplugin_info_t *)info)())) { |
|---|
| 91 | wminput_err("Invalid plugin info from %s", plugin->name); |
|---|
| 92 | free(plugin->p); |
|---|
| 93 | dlclose(handle); |
|---|
| 94 | return -1; |
|---|
| 95 | } |
|---|
| 96 | if (!(((struct c_plugin *)plugin->p)->init = dlsym(handle, |
|---|
| 97 | "wmplugin_init"))) { |
|---|
| 98 | wminput_err("Unable to load plugin init function: %s", dlerror()); |
|---|
| 99 | free(plugin->p); |
|---|
| 100 | dlclose(handle); |
|---|
| 101 | return -1; |
|---|
| 102 | } |
|---|
| 103 | if (!(((struct c_plugin *)plugin->p)->exec = dlsym(handle, |
|---|
| 104 | "wmplugin_exec"))) { |
|---|
| 105 | wminput_err("Unable to load plugin exec function: %s", dlerror()); |
|---|
| 106 | free(plugin->p); |
|---|
| 107 | dlclose(handle); |
|---|
| 108 | return -1; |
|---|
| 109 | } |
|---|
| 110 | |
|---|
| 111 | ((struct c_plugin *)plugin->p)->handle = handle; |
|---|
| 112 | |
|---|
| 113 | return 0; |
|---|
| 114 | } |
|---|
| 115 | |
|---|
| 116 | void c_plugin_close(struct plugin *plugin) |
|---|
| 117 | { |
|---|
| 118 | dlclose(((struct c_plugin *)plugin->p)->handle); |
|---|
| 119 | free(plugin->p); |
|---|
| 120 | } |
|---|
| 121 | |
|---|
| 122 | int c_plugin_init(struct plugin *plugin, int id) |
|---|
| 123 | { |
|---|
| 124 | return ((struct c_plugin *)plugin->p)->init(id, wiimote); |
|---|
| 125 | } |
|---|
| 126 | |
|---|
| 127 | int c_plugin_exec(struct plugin *plugin, int mesg_count, |
|---|
| 128 | union cwiid_mesg mesg[]) |
|---|
| 129 | { |
|---|
| 130 | if (!(plugin->data = ((struct c_plugin *)plugin->p)->exec(mesg_count, |
|---|
| 131 | mesg))) { |
|---|
| 132 | return -1; |
|---|
| 133 | } |
|---|
| 134 | |
|---|
| 135 | return 0; |
|---|
| 136 | } |
|---|
| 137 | |
|---|
| 138 | int c_plugin_param_int(struct plugin *plugin, int i, int value) |
|---|
| 139 | { |
|---|
| 140 | switch (plugin->info->param_info[i].type) { |
|---|
| 141 | case WMPLUGIN_PARAM_INT: |
|---|
| 142 | *(int *)plugin->info->param_info[i].ptr = value; |
|---|
| 143 | break; |
|---|
| 144 | case WMPLUGIN_PARAM_FLOAT: |
|---|
| 145 | *(float *)plugin->info->param_info[i].ptr = value; |
|---|
| 146 | break; |
|---|
| 147 | } |
|---|
| 148 | |
|---|
| 149 | return 0; |
|---|
| 150 | } |
|---|
| 151 | |
|---|
| 152 | int c_plugin_param_float(struct plugin *plugin, int i, float value) |
|---|
| 153 | { |
|---|
| 154 | switch (plugin->info->param_info[i].type) { |
|---|
| 155 | case WMPLUGIN_PARAM_INT: |
|---|
| 156 | wminput_err("possible loss of precision: %s.%s (cast float to int)", |
|---|
| 157 | plugin->name, plugin->info->param_info[i].name); |
|---|
| 158 | *(int *)plugin->info->param_info[i].ptr = value; |
|---|
| 159 | break; |
|---|
| 160 | case WMPLUGIN_PARAM_FLOAT: |
|---|
| 161 | *(float *)plugin->info->param_info[i].ptr = value; |
|---|
| 162 | break; |
|---|
| 163 | } |
|---|
| 164 | |
|---|
| 165 | return 0; |
|---|
| 166 | } |
|---|
| 167 | |
|---|
| 168 | void wmplugin_err(int id, char *str, ...) |
|---|
| 169 | { |
|---|
| 170 | va_list ap; |
|---|
| 171 | |
|---|
| 172 | va_start(ap, str); |
|---|
| 173 | vfprintf(stderr, str, ap); |
|---|
| 174 | fprintf(stderr, "\n"); |
|---|
| 175 | va_end(ap); |
|---|
| 176 | } |
|---|