| 1 |
/* Copyright (C) 2007 L. Donnie Smith <cwiid@abstrakraft.org> |
|---|
| 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 |
* ChangeLog: |
|---|
| 18 |
* 2007-06-05 L. Donnie Smith <cwiid@abstrakraft.org> |
|---|
| 19 |
* * Initial ChangeLog |
|---|
| 20 |
*/ |
|---|
| 21 |
|
|---|
| 22 |
#include <stdlib.h> |
|---|
| 23 |
|
|---|
| 24 |
#include <dlfcn.h> |
|---|
| 25 |
#include <sys/types.h> |
|---|
| 26 |
#include <sys/stat.h> |
|---|
| 27 |
#include <unistd.h> |
|---|
| 28 |
|
|---|
| 29 |
#include "cwiid.h" |
|---|
| 30 |
#include "conf.h" |
|---|
| 31 |
#include "util.h" |
|---|
| 32 |
#include "wmplugin.h" |
|---|
| 33 |
|
|---|
| 34 |
struct c_plugin { |
|---|
| 35 |
void *handle; |
|---|
| 36 |
wmplugin_init_t *init; |
|---|
| 37 |
wmplugin_exec_t *exec; |
|---|
| 38 |
}; |
|---|
| 39 |
|
|---|
| 40 |
static cwiid_wiimote_t *wiimote; |
|---|
| 41 |
|
|---|
| 42 |
int c_init(void) |
|---|
| 43 |
{ |
|---|
| 44 |
return 0; |
|---|
| 45 |
} |
|---|
| 46 |
|
|---|
| 47 |
int c_wiimote(cwiid_wiimote_t *arg_wiimote) |
|---|
| 48 |
{ |
|---|
| 49 |
wiimote = arg_wiimote; |
|---|
| 50 |
|
|---|
| 51 |
return 0; |
|---|
| 52 |
} |
|---|
| 53 |
|
|---|
| 54 |
void c_deinit(void) |
|---|
| 55 |
{ |
|---|
| 56 |
return; |
|---|
| 57 |
} |
|---|
| 58 |
|
|---|
| 59 |
#define PLUGIN_PATHNAME_LEN 128 |
|---|
| 60 |
int c_plugin_open(struct plugin *plugin, char *dir) |
|---|
| 61 |
{ |
|---|
| 62 |
char pathname[PLUGIN_PATHNAME_LEN]; |
|---|
| 63 |
struct stat buf; |
|---|
| 64 |
wmplugin_info_t *info; |
|---|
| 65 |
void *handle; |
|---|
| 66 |
|
|---|
| 67 |
snprintf(pathname, PLUGIN_PATHNAME_LEN, "%s/%s.so", dir, plugin->name); |
|---|
| 68 |
if (stat(pathname, &buf)) { |
|---|
| 69 |
return -1; |
|---|
| 70 |
} |
|---|
| 71 |
if (!(handle = dlopen(pathname, RTLD_NOW))) { |
|---|
| 72 |
wminput_err(dlerror()); |
|---|
| 73 |
return -1; |
|---|
| 74 |
} |
|---|
| 75 |
|
|---|
| 76 |
plugin->type = PLUGIN_C; |
|---|
| 77 |
if (!(plugin->p = malloc(sizeof(struct c_plugin)))) { |
|---|
| 78 |
wminput_err("malloc error"); |
|---|
| 79 |
return -1; |
|---|
| 80 |
} |
|---|
| 81 |
if (!(info = dlsym(handle, "wmplugin_info"))) { |
|---|
| 82 |
wminput_err("Unable to load plugin info function: %s", |
|---|
| 83 |
dlerror()); |
|---|
| 84 |
free(plugin->p); |
|---|
| 85 |
dlclose(handle); |
|---|
| 86 |
return -1; |
|---|
| 87 |
} |
|---|
| 88 |
if (!(plugin->info = (*(wmplugin_info_t *)info)())) { |
|---|
| 89 |
wminput_err("Invalid plugin info from %s", plugin->name); |
|---|
| 90 |
free(plugin->p); |
|---|
| 91 |
dlclose(handle); |
|---|
| 92 |
return -1; |
|---|
| 93 |
} |
|---|
| 94 |
if (!(((struct c_plugin *)plugin->p)->init = dlsym(handle, |
|---|
| 95 |
"wmplugin_init"))) { |
|---|
| 96 |
wminput_err("Unable to load plugin init function: %s", dlerror()); |
|---|
| 97 |
free(plugin->p); |
|---|
| 98 |
dlclose(handle); |
|---|
| 99 |
return -1; |
|---|
| 100 |
} |
|---|
| 101 |
if (!(((struct c_plugin *)plugin->p)->exec = dlsym(handle, |
|---|
| 102 |
"wmplugin_exec"))) { |
|---|
| 103 |
wminput_err("Unable to load plugin exec function: %s", dlerror()); |
|---|
| 104 |
free(plugin->p); |
|---|
| 105 |
dlclose(handle); |
|---|
| 106 |
return -1; |
|---|
| 107 |
} |
|---|
| 108 |
|
|---|
| 109 |
((struct c_plugin *)plugin->p)->handle = handle; |
|---|
| 110 |
|
|---|
| 111 |
return 0; |
|---|
| 112 |
} |
|---|
| 113 |
|
|---|
| 114 |
void c_plugin_close(struct plugin *plugin) |
|---|
| 115 |
{ |
|---|
| 116 |
dlclose(((struct c_plugin *)plugin->p)->handle); |
|---|
| 117 |
free(plugin->p); |
|---|
| 118 |
} |
|---|
| 119 |
|
|---|
| 120 |
int c_plugin_init(struct plugin *plugin, int id) |
|---|
| 121 |
{ |
|---|
| 122 |
return ((struct c_plugin *)plugin->p)->init(id, wiimote); |
|---|
| 123 |
} |
|---|
| 124 |
|
|---|
| 125 |
int c_plugin_exec(struct plugin *plugin, int mesg_count, |
|---|
| 126 |
union cwiid_mesg mesg[]) |
|---|
| 127 |
{ |
|---|
| 128 |
if (!(plugin->data = ((struct c_plugin *)plugin->p)->exec(mesg_count, |
|---|
| 129 |
mesg))) { |
|---|
| 130 |
return -1; |
|---|
| 131 |
} |
|---|
| 132 |
|
|---|
| 133 |
return 0; |
|---|
| 134 |
} |
|---|
| 135 |
|
|---|
| 136 |
int c_plugin_param_int(struct plugin *plugin, int i, int value) |
|---|
| 137 |
{ |
|---|
| 138 |
switch (plugin->info->param_info[i].type) { |
|---|
| 139 |
case WMPLUGIN_PARAM_INT: |
|---|
| 140 |
*(int *)plugin->info->param_info[i].ptr = value; |
|---|
| 141 |
break; |
|---|
| 142 |
case WMPLUGIN_PARAM_FLOAT: |
|---|
| 143 |
*(float *)plugin->info->param_info[i].ptr = value; |
|---|
| 144 |
break; |
|---|
| 145 |
} |
|---|
| 146 |
|
|---|
| 147 |
return 0; |
|---|
| 148 |
} |
|---|
| 149 |
|
|---|
| 150 |
int c_plugin_param_float(struct plugin *plugin, int i, float value) |
|---|
| 151 |
{ |
|---|
| 152 |
switch (plugin->info->param_info[i].type) { |
|---|
| 153 |
case WMPLUGIN_PARAM_INT: |
|---|
| 154 |
wminput_err("possible loss of precision: %s.%s (cast float to int)", |
|---|
| 155 |
plugin->name, plugin->info->param_info[i].name); |
|---|
| 156 |
*(int *)plugin->info->param_info[i].ptr = value; |
|---|
| 157 |
break; |
|---|
| 158 |
case WMPLUGIN_PARAM_FLOAT: |
|---|
| 159 |
*(float *)plugin->info->param_info[i].ptr = value; |
|---|
| 160 |
break; |
|---|
| 161 |
} |
|---|
| 162 |
|
|---|
| 163 |
return 0; |
|---|
| 164 |
} |
|---|
| 165 |
|
|---|
| 166 |
void wmplugin_err(int id, char *str, ...) |
|---|
| 167 |
{ |
|---|
| 168 |
va_list ap; |
|---|
| 169 |
|
|---|
| 170 |
va_start(ap, str); |
|---|
| 171 |
vfprintf(stderr, str, ap); |
|---|
| 172 |
fprintf(stderr, "\n"); |
|---|
| 173 |
va_end(ap); |
|---|
| 174 |
} |
|---|