2014-10-22 19:46:51 +00:00
|
|
|
#include "internal.h"
|
2014-04-12 19:12:44 +00:00
|
|
|
#include "version.h"
|
|
|
|
|
2014-10-22 19:46:51 +00:00
|
|
|
#include "3rdparty/tinydir.h"
|
|
|
|
#include "3rdparty/cdl.h"
|
|
|
|
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <assert.h>
|
|
|
|
|
|
|
|
static struct list renderers;
|
|
|
|
|
|
|
|
static void
|
|
|
|
bm_renderer_free(struct bm_renderer *renderer)
|
|
|
|
{
|
|
|
|
assert(renderer);
|
|
|
|
|
|
|
|
if (renderer->handle)
|
|
|
|
chckDlUnload(renderer->handle);
|
|
|
|
|
|
|
|
free(renderer->name);
|
|
|
|
free(renderer->file);
|
|
|
|
free(renderer);
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
|
|
|
load(const char *file, struct bm_renderer *renderer)
|
|
|
|
{
|
|
|
|
void *handle;
|
|
|
|
const char *error = NULL;
|
|
|
|
|
|
|
|
if (!(handle = chckDlLoad(file, &error)))
|
2014-10-25 17:02:34 +00:00
|
|
|
goto load_fail;
|
2014-10-22 19:46:51 +00:00
|
|
|
|
|
|
|
const char* (*regfun)(struct render_api*);
|
|
|
|
if (!(regfun = chckDlLoadSymbol(handle, "register_renderer", &error)))
|
2014-10-25 13:39:39 +00:00
|
|
|
goto load_fail;
|
2014-10-22 19:46:51 +00:00
|
|
|
|
2014-10-24 22:38:30 +00:00
|
|
|
const char *name;
|
|
|
|
if (!(name = regfun(&renderer->api)))
|
|
|
|
goto fail;
|
|
|
|
|
2014-10-25 13:39:39 +00:00
|
|
|
if (strcmp(renderer->api.version, BM_PLUGIN_VERSION))
|
|
|
|
goto mismatch_fail;
|
2014-10-24 22:38:30 +00:00
|
|
|
|
2014-10-22 19:46:51 +00:00
|
|
|
renderer->handle = handle;
|
2014-10-24 22:38:30 +00:00
|
|
|
renderer->name = bm_strdup(name);
|
|
|
|
renderer->file = bm_strdup(file);
|
2014-10-22 19:46:51 +00:00
|
|
|
return true;
|
|
|
|
|
2014-10-25 13:39:39 +00:00
|
|
|
load_fail:
|
|
|
|
fprintf(stderr, "%s\n", error);
|
|
|
|
goto fail;
|
|
|
|
mismatch_fail:
|
|
|
|
fprintf(stderr, "%s: version mismatch (%s != %s)\n", name, renderer->api.version, BM_PLUGIN_VERSION);
|
2014-10-22 19:46:51 +00:00
|
|
|
fail:
|
|
|
|
if (handle)
|
|
|
|
chckDlUnload(handle);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-10-24 22:38:30 +00:00
|
|
|
static int
|
|
|
|
compare(const void *a, const void *b)
|
|
|
|
{
|
|
|
|
const struct bm_renderer *ra = *(struct bm_renderer**)a, *rb = *(struct bm_renderer**)b;
|
|
|
|
return (ra->api.prioritory > rb->api.prioritory);
|
|
|
|
}
|
|
|
|
|
2014-10-22 19:46:51 +00:00
|
|
|
static bool
|
|
|
|
load_to_list(const char *file)
|
|
|
|
{
|
|
|
|
struct bm_renderer *renderer;
|
|
|
|
if (!(renderer = calloc(1, sizeof(struct bm_renderer))))
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
if (!load(file, renderer))
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
chckDlUnload(renderer->handle);
|
2014-10-24 22:38:30 +00:00
|
|
|
|
|
|
|
if (!list_add_item(&renderers, renderer))
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
list_sort(&renderers, compare);
|
|
|
|
return true;
|
2014-10-22 19:46:51 +00:00
|
|
|
|
|
|
|
fail:
|
|
|
|
if (renderer)
|
|
|
|
bm_renderer_free(renderer);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2014-10-22 20:10:11 +00:00
|
|
|
bm_renderer_activate(struct bm_renderer *renderer, struct bm_menu *menu)
|
2014-10-22 19:46:51 +00:00
|
|
|
{
|
|
|
|
assert(renderer);
|
|
|
|
|
|
|
|
if (!load(renderer->file, renderer))
|
|
|
|
return false;
|
|
|
|
|
2014-10-24 22:38:30 +00:00
|
|
|
menu->renderer = renderer;
|
|
|
|
|
2014-10-22 20:10:11 +00:00
|
|
|
if (!renderer->api.constructor(menu))
|
2014-10-22 19:52:14 +00:00
|
|
|
goto fail;
|
|
|
|
|
2014-10-22 19:46:51 +00:00
|
|
|
return true;
|
2014-10-22 19:52:14 +00:00
|
|
|
|
|
|
|
fail:
|
|
|
|
chckDlUnload(renderer->handle);
|
2014-10-24 22:38:30 +00:00
|
|
|
menu->renderer = NULL;
|
2014-10-22 19:52:14 +00:00
|
|
|
return false;
|
2014-10-22 19:46:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
bm_init(void)
|
|
|
|
{
|
|
|
|
static const char *rpath = INSTALL_PREFIX "/lib/bemenu";
|
|
|
|
|
|
|
|
const char *path = getenv("BEMENU_RENDERER");
|
|
|
|
|
|
|
|
if (path)
|
|
|
|
return load_to_list(path);
|
|
|
|
|
|
|
|
path = getenv("BEMENU_RENDERERS");
|
|
|
|
|
|
|
|
if (!path || access(path, R_OK) == -1)
|
|
|
|
path = rpath;
|
|
|
|
|
|
|
|
tinydir_dir dir;
|
|
|
|
if (tinydir_open(&dir, path) == -1)
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
size_t registered = 0;
|
|
|
|
while (dir.has_next) {
|
|
|
|
tinydir_file file;
|
|
|
|
memset(&file, 0, sizeof(file));
|
|
|
|
tinydir_readfile(&dir, &file);
|
|
|
|
|
|
|
|
if (!file.is_dir && !strncmp(file.name, "bemenu-renderer-", strlen("bemenu-renderer-"))) {
|
|
|
|
size_t len = snprintf(NULL, 0, "%s/%s", file.name, path);
|
|
|
|
|
|
|
|
char *fpath;
|
|
|
|
if ((fpath = calloc(1, len + 1))) {
|
|
|
|
sprintf(fpath, "%s/%s", path, file.name);
|
|
|
|
|
|
|
|
if (load_to_list(fpath))
|
|
|
|
registered++;
|
|
|
|
|
|
|
|
free(fpath);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
tinydir_next(&dir);
|
|
|
|
}
|
|
|
|
|
|
|
|
tinydir_close(&dir);
|
|
|
|
return (registered > 0 ? true : false);
|
|
|
|
|
|
|
|
fail:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
const struct bm_renderer**
|
|
|
|
bm_get_renderers(uint32_t *out_nmemb)
|
|
|
|
{
|
|
|
|
assert(out_nmemb);
|
|
|
|
return list_get_items(&renderers, out_nmemb);
|
|
|
|
}
|
|
|
|
|
|
|
|
const char*
|
|
|
|
bm_version(void)
|
2014-04-12 19:12:44 +00:00
|
|
|
{
|
|
|
|
return BM_VERSION;
|
|
|
|
}
|
|
|
|
|
2014-10-22 19:46:51 +00:00
|
|
|
const char*
|
|
|
|
bm_renderer_get_name(const struct bm_renderer *renderer)
|
|
|
|
{
|
|
|
|
assert(renderer);
|
|
|
|
return renderer->name;
|
|
|
|
}
|
|
|
|
|
2014-10-24 22:38:30 +00:00
|
|
|
enum bm_prioritory
|
|
|
|
bm_renderer_get_prioritory(const struct bm_renderer *renderer)
|
|
|
|
{
|
|
|
|
assert(renderer);
|
|
|
|
return renderer->api.prioritory;
|
|
|
|
}
|
|
|
|
|
2014-04-12 19:12:44 +00:00
|
|
|
/* vim: set ts=8 sw=4 tw=0 :*/
|