2014-10-22 21:46:51 +02:00
|
|
|
#include "internal.h"
|
2014-04-12 21:12:44 +02:00
|
|
|
|
2014-10-22 21:46:51 +02:00
|
|
|
#include "3rdparty/cdl.h"
|
|
|
|
|
2020-02-07 14:56:50 +01:00
|
|
|
#include <stdlib.h>
|
2014-10-22 21:46:51 +02:00
|
|
|
#include <unistd.h>
|
2020-02-07 14:56:50 +01:00
|
|
|
#include <string.h>
|
|
|
|
#include <dirent.h>
|
2014-10-22 21:46:51 +02:00
|
|
|
#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 19:02:34 +02:00
|
|
|
goto load_fail;
|
2014-10-22 21:46:51 +02:00
|
|
|
|
2020-02-07 14:56:50 +01:00
|
|
|
union {
|
|
|
|
const char* (*fun)(struct render_api*);
|
|
|
|
void *ptr;
|
|
|
|
} reg;
|
|
|
|
|
|
|
|
if (!(reg.ptr = chckDlLoadSymbol(handle, "register_renderer", &error)))
|
2014-10-25 15:39:39 +02:00
|
|
|
goto load_fail;
|
2014-10-22 21:46:51 +02:00
|
|
|
|
2014-10-25 00:38:30 +02:00
|
|
|
const char *name;
|
2020-02-07 14:56:50 +01:00
|
|
|
if (!(name = reg.fun(&renderer->api)))
|
2014-10-25 00:38:30 +02:00
|
|
|
goto fail;
|
|
|
|
|
2014-10-25 15:39:39 +02:00
|
|
|
if (strcmp(renderer->api.version, BM_PLUGIN_VERSION))
|
|
|
|
goto mismatch_fail;
|
2014-10-25 00:38:30 +02:00
|
|
|
|
2014-10-25 21:16:09 +02:00
|
|
|
if (!renderer->name)
|
|
|
|
renderer->name = bm_strdup(name);
|
|
|
|
|
|
|
|
if (!renderer->file)
|
|
|
|
renderer->file = bm_strdup(file);
|
|
|
|
|
2014-10-22 21:46:51 +02:00
|
|
|
renderer->handle = handle;
|
|
|
|
return true;
|
|
|
|
|
2014-10-25 15:39:39 +02: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 21:46:51 +02:00
|
|
|
fail:
|
|
|
|
if (handle)
|
|
|
|
chckDlUnload(handle);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-10-25 00:38:30 +02:00
|
|
|
static int
|
|
|
|
compare(const void *a, const void *b)
|
|
|
|
{
|
|
|
|
const struct bm_renderer *ra = *(struct bm_renderer**)a, *rb = *(struct bm_renderer**)b;
|
2015-01-17 17:53:29 +01:00
|
|
|
return (ra->api.priorty > rb->api.priorty);
|
2014-10-25 00:38:30 +02:00
|
|
|
}
|
|
|
|
|
2014-10-22 21:46:51 +02: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-25 21:16:09 +02:00
|
|
|
renderer->handle = NULL;
|
2014-10-25 00:38:30 +02:00
|
|
|
|
|
|
|
if (!list_add_item(&renderers, renderer))
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
list_sort(&renderers, compare);
|
|
|
|
return true;
|
2014-10-22 21:46:51 +02:00
|
|
|
|
|
|
|
fail:
|
|
|
|
if (renderer)
|
|
|
|
bm_renderer_free(renderer);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2014-10-22 22:10:11 +02:00
|
|
|
bm_renderer_activate(struct bm_renderer *renderer, struct bm_menu *menu)
|
2014-10-22 21:46:51 +02:00
|
|
|
{
|
|
|
|
assert(renderer);
|
|
|
|
|
|
|
|
if (!load(renderer->file, renderer))
|
|
|
|
return false;
|
|
|
|
|
2014-10-25 00:38:30 +02:00
|
|
|
menu->renderer = renderer;
|
|
|
|
|
2014-10-22 22:10:11 +02:00
|
|
|
if (!renderer->api.constructor(menu))
|
2014-10-22 21:52:14 +02:00
|
|
|
goto fail;
|
|
|
|
|
2014-10-22 21:46:51 +02:00
|
|
|
return true;
|
2014-10-22 21:52:14 +02:00
|
|
|
|
|
|
|
fail:
|
|
|
|
chckDlUnload(renderer->handle);
|
2014-10-25 00:38:30 +02:00
|
|
|
menu->renderer = NULL;
|
2014-10-22 21:52:14 +02:00
|
|
|
return false;
|
2014-10-22 21:46:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
bm_init(void)
|
|
|
|
{
|
2014-10-25 21:16:09 +02:00
|
|
|
if (renderers.count > 0)
|
|
|
|
return true;
|
2014-10-22 21:46:51 +02:00
|
|
|
|
2015-09-21 22:18:14 +02:00
|
|
|
static const char *rpath = INSTALL_LIBDIR "/bemenu";
|
2015-01-17 17:52:21 +01:00
|
|
|
const char *path = secure_getenv("BEMENU_RENDERER");
|
2014-10-22 21:46:51 +02:00
|
|
|
|
|
|
|
if (path)
|
|
|
|
return load_to_list(path);
|
|
|
|
|
2015-01-17 17:52:21 +01:00
|
|
|
path = secure_getenv("BEMENU_RENDERERS");
|
2014-10-22 21:46:51 +02:00
|
|
|
|
|
|
|
if (!path || access(path, R_OK) == -1)
|
|
|
|
path = rpath;
|
|
|
|
|
2020-02-07 14:56:50 +01:00
|
|
|
DIR *dir;
|
|
|
|
if (!(dir = opendir(path)))
|
2014-10-22 21:46:51 +02:00
|
|
|
goto fail;
|
|
|
|
|
2020-02-07 14:56:50 +01:00
|
|
|
struct dirent *file;
|
|
|
|
while ((file = readdir(dir))) {
|
|
|
|
if (file->d_type != DT_DIR && !strncmp(file->d_name, "bemenu-renderer-", strlen("bemenu-renderer-"))) {
|
2014-10-22 21:46:51 +02:00
|
|
|
char *fpath;
|
2020-02-07 14:56:50 +01:00
|
|
|
if ((fpath = bm_dprintf("%s/%s", path, file->d_name))) {
|
2014-10-25 21:16:09 +02:00
|
|
|
load_to_list(fpath);
|
2014-10-22 21:46:51 +02:00
|
|
|
free(fpath);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-07 14:56:50 +01:00
|
|
|
closedir(dir);
|
2014-10-25 21:16:09 +02:00
|
|
|
return (renderers.count > 0 ? true : false);
|
2014-10-22 21:46:51 +02:00
|
|
|
|
|
|
|
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 21:12:44 +02:00
|
|
|
{
|
|
|
|
return BM_VERSION;
|
|
|
|
}
|
|
|
|
|
2014-10-22 21:46:51 +02:00
|
|
|
const char*
|
|
|
|
bm_renderer_get_name(const struct bm_renderer *renderer)
|
|
|
|
{
|
|
|
|
assert(renderer);
|
|
|
|
return renderer->name;
|
|
|
|
}
|
|
|
|
|
2015-01-17 17:53:29 +01:00
|
|
|
enum bm_priorty
|
|
|
|
bm_renderer_get_priorty(const struct bm_renderer *renderer)
|
2014-10-25 00:38:30 +02:00
|
|
|
{
|
|
|
|
assert(renderer);
|
2015-01-17 17:53:29 +01:00
|
|
|
return renderer->api.priorty;
|
2014-10-25 00:38:30 +02:00
|
|
|
}
|
|
|
|
|
2014-04-12 21:12:44 +02:00
|
|
|
/* vim: set ts=8 sw=4 tw=0 :*/
|