File: plugin.c

package info (click to toggle)
lebiniou 3.18-1
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 5,396 kB
  • sloc: ansic: 20,369; sh: 4,169; makefile: 834; awk: 194
file content (266 lines) | stat: -rw-r--r-- 6,104 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
/*
 *  Copyright 1994-2012 Olivier Girondel
 *
 *  This file is part of lebiniou.
 *
 *  lebiniou is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  lebiniou is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with lebiniou. If not, see <http://www.gnu.org/licenses/>.
 */

#include <inttypes.h>
#include "biniou.h"
#include "plugin.h"

/* Note: dlsym prevents us from fully compiling in -pedantic..
 * see http://www.trilithium.com/johan/2004/12/problem-with-dlsym/
 */

#ifdef XDEBUG
#define B_DLSYM(VAR, SYM)						\
  (VAR) = (void (*)(struct Context_s *)) dlsym(p->handle, (SYM));	\
  if (libbiniou_verbose && ((VAR) != NULL))				\
    printf("[p] >> %s has '%s'\n", p->name, (SYM))
#define B_GOTSYM(VAR, SYM) if ((VAR) != NULL)		                \
    printf("[p] >> %s has '%s'\n", p->name, (SYM));
#else
#define B_DLSYM(VAR, SYM)						\
  (VAR) = (void (*)(struct Context_s *)) dlsym(p->handle, (SYM))
#define B_GOTSYM(VAR, SYM) { }
#endif


static Plugin_t *
Plugin_load(Plugin_t *p)
{
  const char *error = NULL;
  u_long *_tmp;

  assert(p != NULL);
	
  p->handle = dlopen(p->file, RTLD_NOW);
	
  if (p->handle == NULL) {
    error = dlerror();
    fprintf(stderr, "[!] Failed to load plugin '%s': %s\n",
	    p->name, error);
    xfree(p->name);
    xfree(p->file);
    xfree(p);

    return NULL;
  } else {
    if (libbiniou_verbose)
      printf("[p] Loading plugin '%s'", p->name);
  }
  fflush(stdout);

  _tmp = (u_long *) dlsym(p->handle, "id");
  if (_tmp == NULL) {
    error = dlerror();
    fprintf(stderr, "\n");
    xerror("Plugin MUST define an id (%s)\n", error);
  } else {
    p->id = *_tmp;
    if (libbiniou_verbose)
#ifdef XDEBUG
      printf(" (id= %li)\n", p->id);
#else
      printf("\n");
#endif
  }

  _tmp = (u_long *) dlsym(p->handle, "options");
  if (_tmp == NULL) {
    error = dlerror();
    xerror("Plugin MUST define options (%s)\n", error);
  } else
    p->options = _tmp;

  _tmp = (u_long *) dlsym(p->handle, "mode");
  p->mode = _tmp;

  /* get display name */
  p->dname = (char *) dlsym(p->handle, "dname");
  B_GOTSYM(p->dname, "dname");
  if (p->dname == NULL)
    p->dname = p->name;

  /* get description */
  p->desc = (char *) dlsym(p->handle, "desc");
  B_GOTSYM(p->desc, "desc");

  B_DLSYM(p->create, "create");
  B_DLSYM(p->destroy, "destroy");
  B_DLSYM(p->run, "run");
  B_DLSYM(p->on_switch_on, "on_switch_on");
  B_DLSYM(p->on_switch_off, "on_switch_off");
  p->on_key = (void (*)(u_char)) dlsym(p->handle, "on_key");
  B_GOTSYM(p->on_key, "on_key");

  /* Output plugin stuff */
  /* TODO: remove, use on_key callbacks */
  B_DLSYM(p->set_cmap, "set_cmap");
  p->fullscreen = (void (*)(int)) dlsym(p->handle, "fullscreen");
  B_GOTSYM(p->fullscreen, "fullscreen");
  p->switch_cursor = (void (*)(void)) dlsym(p->handle, "switch_cursor");
  B_GOTSYM(p->switch_cursor, "switch_cursor");
	
  /* Input plugin stuff (? mainly -to check --oliv3) */
  p->jthread = (void *(*)(void *)) dlsym(p->handle, "jthread");
  B_GOTSYM(p->jthread, "jthread");

  return p;
}


static void
Plugin_unload(Plugin_t *p)
{
  assert (p != NULL);
	
  /* FIXME error checking there, ie if plugin fails to destroy */
  if (p->jthread != NULL) {
    if (libbiniou_verbose) {
      printf("[p] Joining thread from plugin '%s'... ", p->name);
      fflush(stdout);
    }
    pthread_join(p->thread, NULL);
  } else {
    if (libbiniou_verbose) {
      printf("[p] Unloading plugin '%s' (%li call%s)... ", p->name, p->calls,
	     ((p->calls == 1) ? "" : "s"));
      fflush(stdout);
    }
  }
    
  if (p->destroy != NULL)
    p->destroy(context);

  dlclose(p->handle);

  if (libbiniou_verbose)
    printf("done.\n");
}


Plugin_t *
Plugin_new(const char *directory, const char *name, const enum PluginType type)
{
  Plugin_t *p = xcalloc(1, sizeof(Plugin_t));

  assert(name != NULL);
  assert(directory != NULL);

  p->name = strdup(name);
  p->calls = 0;

  if (type == PL_INPUT)
    p->file = g_strdup_printf("%s/input/%s/%s.so", directory, name, name);
  else if (type == PL_MAIN)
    p->file = g_strdup_printf("%s/main/%s/%s.so", directory, name, name);
  else if (type == PL_OUTPUT)
    p->file = g_strdup_printf("%s/output/%s/%s.so", directory, name, name);

  return Plugin_load(p);
}


void
Plugin_delete(Plugin_t *p)
{
  assert(p != NULL);
  Plugin_unload(p);
  xfree(p->name);
  g_free(p->file);
  xfree(p);
}


void
Plugin_reload(Plugin_t *p)
{
  assert(p != NULL);
  Plugin_unload(p);
  Plugin_load(p);
  if (libbiniou_verbose)
    printf("[p] Reloaded plugin '%s'\n", p->name);
}


void
Plugin_init(Plugin_t *p)
{
  assert(p != NULL);

  if (p->create != NULL) {
    if (libbiniou_verbose)
      printf("[+] Initializing plugin %s\n", p->name);
    p->create(context);
  }

  if (p->jthread != NULL) {
    pthread_create(&p->thread, NULL, p->jthread, (void *)context);
    if (libbiniou_verbose)
      printf("[p] Launched thread %s\n", p->name);
  }
}


static char *
Plugin_uppercase(const char *str)
{
  char *tmp;

  assert(str != NULL);
  tmp = strdup(str);

  assert(tmp != NULL);
  assert(tmp[0] != '\0');
  tmp[0] = (char)toupper((int)tmp[0]);

  return tmp;
}


char *
Plugin_name(const Plugin_t *p)
{
  assert(p != NULL);
  return Plugin_uppercase(p->name);
}


char *
Plugin_dname(const Plugin_t *p)
{
  assert(p != NULL);
  return Plugin_uppercase(p->dname);
}


void
Plugin_on_key(const uint32_t id, const u_char k)
{
  Plugin_t *p = Plugins_find(id);

  if (NULL == p) {
    printf("[!] Plugin id %"PRIu32" not found\n", id);
    return;
  }

  if (NULL == p->on_key) {
    printf("[!] Plugin id %"PRIu32" has no on_key callback\n", id);
    return;
  } else
    p->on_key(k);
}