Commit 4ade2c14 authored by Moritz Lipp's avatar Moritz Lipp

Introduced plugin.h and plugin-api.h

parent 28265953
......@@ -6,7 +6,7 @@ include common.mk
PROJECT = zathura
OSOURCE = $(wildcard *.c)
HEADER = $(wildcard *.h)
HEADERINST = version.h zathura.h document.h macros.h page.h types.h
HEADERINST = version.h zathura.h document.h macros.h page.h types.h plugin-api.h
ifneq (${WITH_SQLITE},0)
INCS += $(SQLITE_INC)
......
......@@ -302,7 +302,7 @@ cmd_search(girara_session_t* session, const char* input, girara_argument_t* argu
}
bool firsthit = true;
zathura_plugin_error_t error = ZATHURA_PLUGIN_ERROR_OK;
zathura_error_t error = ZATHURA_ERROR_OK;
for (unsigned int page_id = 0; page_id < zathura->document->number_of_pages; ++page_id) {
zathura_page_t* page = zathura->document->pages[(page_id + zathura->document->current_page_number) % zathura->document->number_of_pages];
......@@ -318,7 +318,7 @@ cmd_search(girara_session_t* session, const char* input, girara_argument_t* argu
girara_list_free(result);
g_object_set(page_widget, "search-results", NULL, NULL);
if (error == ZATHURA_PLUGIN_ERROR_NOT_IMPLEMENTED) {
if (error == ZATHURA_ERROR_NOT_IMPLEMENTED) {
break;
} else {
continue;
......
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
......@@ -16,16 +16,16 @@
* @return Page object or NULL if an error occured
*/
zathura_page_t* zathura_page_new(zathura_document_t* document, unsigned int
index, zathura_plugin_error_t* error);
index, zathura_error_t* error);
/**
* Frees the page object
*
* @param page The page object
* @return ZATHURA_PLUGIN_ERROR_OK when no error occured, otherwise see
* zathura_plugin_error_t
* @return ZATHURA_ERROR_OK when no error occured, otherwise see
* zathura_error_t
*/
zathura_plugin_error_t zathura_page_free(zathura_page_t* page);
zathura_error_t zathura_page_free(zathura_page_t* page);
/**
* Returns the associated document
......@@ -125,80 +125,80 @@ void zathura_page_set_data(zathura_page_t* page, void* data);
*
* @param page The page object
* @param text Search item
* @param error Set to an error value (see \ref zathura_plugin_error_t) if an
* @param error Set to an error value (see \ref zathura_error_t) if an
* error occured
* @return List of results
*/
girara_list_t* zathura_page_search_text(zathura_page_t* page, const char* text, zathura_plugin_error_t* error);
girara_list_t* zathura_page_search_text(zathura_page_t* page, const char* text, zathura_error_t* error);
/**
* Get page links
*
* @param page The page object
* @param error Set to an error value (see \ref zathura_plugin_error_t) if an
* @param error Set to an error value (see \ref zathura_error_t) if an
* error occured
* @return List of links
*/
girara_list_t* zathura_page_links_get(zathura_page_t* page, zathura_plugin_error_t* error);
girara_list_t* zathura_page_links_get(zathura_page_t* page, zathura_error_t* error);
/**
* Free page links
*
* @param list List of links
* @return ZATHURA_PLUGIN_ERROR_OK when no error occured, otherwise see
* zathura_plugin_error_t
* @return ZATHURA_ERROR_OK when no error occured, otherwise see
* zathura_error_t
*/
zathura_plugin_error_t zathura_page_links_free(girara_list_t* list);
zathura_error_t zathura_page_links_free(girara_list_t* list);
/**
* Get list of form fields
*
* @param page The page object
* @param error Set to an error value (see \ref zathura_plugin_error_t) if an
* @param error Set to an error value (see \ref zathura_error_t) if an
* error occured
* @return List of form fields
*/
girara_list_t* zathura_page_form_fields_get(zathura_page_t* page, zathura_plugin_error_t* error);
girara_list_t* zathura_page_form_fields_get(zathura_page_t* page, zathura_error_t* error);
/**
* Free list of form fields
*
* @param list List of form fields
* @return ZATHURA_PLUGIN_ERROR_OK when no error occured, otherwise see
* zathura_plugin_error_t
* @return ZATHURA_ERROR_OK when no error occured, otherwise see
* zathura_error_t
*/
zathura_plugin_error_t zathura_page_form_fields_free(girara_list_t* list);
zathura_error_t zathura_page_form_fields_free(girara_list_t* list);
/**
* Get list of images
*
* @param page Page
* @param error Set to an error value (see \ref zathura_plugin_error_t) if an
* @param error Set to an error value (see \ref zathura_error_t) if an
* error occured
* @return List of images or NULL if an error occured
*/
girara_list_t* zathura_page_images_get(zathura_page_t* page, zathura_plugin_error_t* error);
girara_list_t* zathura_page_images_get(zathura_page_t* page, zathura_error_t* error);
/**
* Get image
*
* @param page Page
* @param image Image identifier
* @param error Set to an error value (see \ref zathura_plugin_error_t) if an
* @param error Set to an error value (see \ref zathura_error_t) if an
* error occured
* @return The cairo image surface or NULL if an error occured
*/
cairo_surface_t* zathura_page_image_get_cairo(zathura_page_t* page, zathura_image_t* image, zathura_plugin_error_t* error);
cairo_surface_t* zathura_page_image_get_cairo(zathura_page_t* page, zathura_image_t* image, zathura_error_t* error);
/**
* Get text for selection
* @param page Page
* @param rectangle Selection
* @param error Set to an error value (see \ref zathura_plugin_error_t) if an error
* @param error Set to an error value (see \ref zathura_error_t) if an error
* occured
* @return The selected text (needs to be deallocated with g_free)
*/
char* zathura_page_get_text(zathura_page_t* page, zathura_rectangle_t rectangle, zathura_plugin_error_t* error);
char* zathura_page_get_text(zathura_page_t* page, zathura_rectangle_t rectangle, zathura_error_t* error);
/**
* Render page
......@@ -206,9 +206,9 @@ char* zathura_page_get_text(zathura_page_t* page, zathura_rectangle_t rectangle,
* @param page The page object
* @param cairo Cairo object
* @param printing render for printing
* @return ZATHURA_PLUGIN_ERROR_OK when no error occured, otherwise see
* zathura_plugin_error_t
* @return ZATHURA_ERROR_OK when no error occured, otherwise see
* zathura_error_t
*/
zathura_plugin_error_t zathura_page_render(zathura_page_t* page, cairo_t* cairo, bool printing);
zathura_error_t zathura_page_render(zathura_page_t* page, cairo_t* cairo, bool printing);
#endif // PAGE_H
/* See LICENSE file for license and copyright information */
#ifndef PLUGIN_API_H
#define PLUGIN_API_H
#include "page.h"
#include "document.h"
#include "version.h"
typedef struct zathura_document_functions_s zathura_document_functions_t;
/**
* Functions register function
*
* @param functions The functions struct
*/
typedef void (*zathura_plugin_register_function_t)(zathura_document_functions_t* functions);
/**
* Sets the functions register function of the plugin
*
* @param plugin The plugin
*/
void zathura_plugin_set_register_functions_function(zathura_plugin_t* plugin, zathura_plugin_register_function_t register_function);
/**
* Sets the functions register function of the plugin
*
* @param plugin The plugin
*/
void zathura_plugin_add_mimetype(zathura_plugin_t* plugin, const char* mime_type);
/**
* Register a plugin.
*
* @param plugin_name the name of the plugin
* @param major the plugin's major version
* @param minor the plugin's minor version
* @param rev the plugin's revision
* @param register_functions function to register the plugin's document functions
* @param mimetypes a char array of mime types supported by the plugin
*/
#define PLUGIN_REGISTER(plugin_name, major, minor, rev, register_functions, mimetypes) \
unsigned int plugin_version_major() { return major; } \
unsigned int plugin_version_minor() { return minor; } \
unsigned int plugin_version_revision() { return rev; } \
unsigned int plugin_api_version() { return ZATHURA_API_VERSION; } \
unsigned int plugin_abi_version() { return ZATHURA_ABI_VERSION; } \
\
void plugin_register(zathura_plugin_t* plugin) \
{ \
if (plugin == NULL) { \
return; \
} \
zathura_plugin_set_register_functions_function(plugin, register_functions); \
static const char* mime_types[] = mimetypes; \
for (size_t s = 0; s != sizeof(mime_types) / sizeof(const char*); ++s) { \
zathura_plugin_add_mimetype(plugin, mime_types[s]); \
} \
} \
#define PLUGIN_MIMETYPES(...) __VA_ARGS__
struct zathura_document_functions_s
{
/**
* Opens a document
*/
zathura_error_t (*document_open)(zathura_document_t* document);
/**
* Frees the document
*/
zathura_error_t (*document_free)(zathura_document_t* document);
/**
* Generates the document index
*/
girara_tree_node_t* (*document_index_generate)(zathura_document_t* document, zathura_error_t* error);
/**
* Save the document
*/
zathura_error_t (*document_save_as)(zathura_document_t* document, const char* path);
/**
* Get list of attachments
*/
girara_list_t* (*document_attachments_get)(zathura_document_t* document, zathura_error_t* error);
/**
* Save attachment to a file
*/
zathura_error_t (*document_attachment_save)(zathura_document_t* document, const char* attachment, const char* file);
/**
* Get document information
*/
char* (*document_meta_get)(zathura_document_t* document, zathura_document_meta_t info, zathura_error_t* error);
/**
* Gets the page object
*/
zathura_error_t (*page_init)(zathura_page_t* page);
/**
* Free page
*/
zathura_error_t (*page_clear)(zathura_page_t* page);
/**
* Search text
*/
girara_list_t* (*page_search_text)(zathura_page_t* page, const char* text, zathura_error_t* error);
/**
* Get links on a page
*/
girara_list_t* (*page_links_get)(zathura_page_t* page, zathura_error_t* error);
/**
* Get form fields
*/
girara_list_t* (*page_form_fields_get)(zathura_page_t* page, zathura_error_t* error);
/**
* Get list of images
*/
girara_list_t* (*page_images_get)(zathura_page_t* page, zathura_error_t* error);
/**
* Get the image
*/
cairo_surface_t* (*page_image_get_cairo)(zathura_page_t* page, zathura_image_t* image, zathura_error_t* error);
/**
* Get text for selection
*/
char* (*page_get_text)(zathura_page_t* page, zathura_rectangle_t rectangle, zathura_error_t* error);
/**
* Renders the page
*/
zathura_image_buffer_t* (*page_render)(zathura_page_t* page, zathura_error_t* error);
/**
* Renders the page
*/
zathura_error_t (*page_render_cairo)(zathura_page_t* page, cairo_t* cairo, bool printing);
};
#endif // PLUGIN_API_H
/* See LICENSE file for license and copyright information */
#include "plugin.h"
#include <stdlib.h>
#include <dlfcn.h>
#include <glib/gi18n.h>
#include <girara/datastructures.h>
#include <girara/utils.h>
#include <girara/statusbar.h>
#include <girara/session.h>
#include <girara/settings.h>
/**
* Register document plugin
*/
static bool zathura_document_plugin_register(zathura_t* zathura, zathura_plugin_t* new_plugin);
void
zathura_document_plugins_load(zathura_t* zathura)
{
GIRARA_LIST_FOREACH(zathura->plugins.path, char*, iter, plugindir)
/* read all files in the plugin directory */
GDir* dir = g_dir_open(plugindir, 0, NULL);
if (dir == NULL) {
girara_error("could not open plugin directory: %s", plugindir);
girara_list_iterator_next(iter);
continue;
}
char* name = NULL;
while ((name = (char*) g_dir_read_name(dir)) != NULL) {
char* path = g_build_filename(plugindir, name, NULL);
if (g_file_test(path, G_FILE_TEST_IS_REGULAR) == 0) {
girara_info("%s is not a regular file. Skipping.", path);
g_free(path);
continue;
}
void* handle = NULL;
zathura_plugin_t* plugin = NULL;
/* load plugin */
handle = dlopen(path, RTLD_NOW);
if (handle == NULL) {
girara_error("could not load plugin %s (%s)", path, dlerror());
g_free(path);
continue;
}
/* resolve symbols and check API and ABI version*/
zathura_plugin_api_version_t api_version;
*(void**)(&api_version) = dlsym(handle, PLUGIN_API_VERSION_FUNCTION);
if (api_version != NULL) {
if (api_version() != ZATHURA_API_VERSION) {
girara_error("plugin %s has been built againt zathura with a different API version (plugin: %d, zathura: %d)",
path, api_version(), ZATHURA_API_VERSION);
g_free(path);
dlclose(handle);
continue;
}
} else {
#if ZATHURA_API_VERSION == 1
girara_warning("could not find '%s' function in plugin %s ... loading anyway", PLUGIN_API_VERSION_FUNCTION, path);
#else
girara_error("could not find '%s' function in plugin %s", PLUGIN_API_VERSION_FUNCTION, path);
g_free(path);
dlclose(handle);
continue;
#endif
}
zathura_plugin_abi_version_t abi_version;
*(void**)(&abi_version) = dlsym(handle, PLUGIN_ABI_VERSION_FUNCTION);
if (abi_version != NULL) {
if (abi_version() != ZATHURA_ABI_VERSION) {
girara_error("plugin %s has been built againt zathura with a different ABI version (plugin: %d, zathura: %d)",
path, abi_version(), ZATHURA_ABI_VERSION);
g_free(path);
dlclose(handle);
continue;
}
} else {
#if ZATHURA_API_VERSION == 1
girara_warning("could not find '%s' function in plugin %s ... loading anyway", PLUGIN_ABI_VERSION_FUNCTION, path);
#else
girara_error("could not find '%s' function in plugin %s", PLUGIN_ABI_VERSION_FUNCTION, path);
g_free(path);
dlclose(handle);
continue;
#endif
}
zathura_plugin_register_service_t register_plugin;
*(void**)(&register_plugin) = dlsym(handle, PLUGIN_REGISTER_FUNCTION);
if (register_plugin == NULL) {
girara_error("could not find '%s' function in plugin %s", PLUGIN_REGISTER_FUNCTION, path);
g_free(path);
dlclose(handle);
continue;
}
plugin = g_malloc0(sizeof(zathura_plugin_t));
plugin->content_types = girara_list_new2(g_free);
plugin->handle = handle;
register_plugin(plugin);
/* register functions */
if (plugin->register_function == NULL) {
girara_error("plugin has no document functions register function");
g_free(path);
dlclose(handle);
continue;
}
plugin->register_function(&(plugin->functions));
bool r = zathura_document_plugin_register(zathura, plugin);
if (r == false) {
girara_error("could not register plugin %s", path);
zathura_document_plugin_free(plugin);
} else {
girara_info("successfully loaded plugin %s", path);
}
g_free(path);
}
g_dir_close(dir);
GIRARA_LIST_FOREACH_END(zathura->plugins.path, char*, iter, plugindir);
}
void
zathura_document_plugin_free(zathura_plugin_t* plugin)
{
if (plugin == NULL) {
return;
}
dlclose(plugin->handle);
girara_list_free(plugin->content_types);
g_free(plugin);
}
static bool
zathura_document_plugin_register(zathura_t* zathura, zathura_plugin_t* new_plugin)
{
if (new_plugin == NULL || new_plugin->content_types == NULL || new_plugin->register_function == NULL) {
girara_error("plugin: could not register\n");
return false;
}
bool atleastone = false;
GIRARA_LIST_FOREACH(new_plugin->content_types, gchar*, iter, type)
if (!zathura_type_plugin_mapping_new(zathura, type, new_plugin)) {
girara_error("plugin: already registered for filetype %s\n", type);
} else {
atleastone = true;
}
GIRARA_LIST_FOREACH_END(new_plugin->content_types, gchar*, iter, type);
if (atleastone) {
girara_list_append(zathura->plugins.plugins, new_plugin);
}
return atleastone;
}
bool
zathura_type_plugin_mapping_new(zathura_t* zathura, const gchar* type, zathura_plugin_t* plugin)
{
g_return_val_if_fail(zathura && type && plugin, false);
GIRARA_LIST_FOREACH(zathura->plugins.type_plugin_mapping, zathura_type_plugin_mapping_t*, iter, mapping)
if (g_content_type_equals(type, mapping->type)) {
girara_list_iterator_free(iter);
return false;
}
GIRARA_LIST_FOREACH_END(zathura->plugins.type_plugin_mapping, zathura_type_plugin_mapping_t*, iter, mapping);
zathura_type_plugin_mapping_t* mapping = g_malloc(sizeof(zathura_type_plugin_mapping_t));
mapping->type = g_strdup(type);
mapping->plugin = plugin;
girara_list_append(zathura->plugins.type_plugin_mapping, mapping);
return true;
}
void
zathura_type_plugin_mapping_free(zathura_type_plugin_mapping_t* mapping)
{
if (mapping == NULL) {
return;
}
g_free((void*)mapping->type);
g_free(mapping);
}
void
zathura_plugin_set_register_functions_function(zathura_plugin_t* plugin, zathura_plugin_register_function_t register_function)
{
if (plugin == NULL || register_function == NULL) {
return;
}
plugin->register_function = register_function;
}
void
zathura_plugin_add_mimetype(zathura_plugin_t* plugin, const char* mime_type)
{
if (plugin == NULL || mime_type == NULL) {
return;
}
girara_list_append(plugin->content_types, g_content_type_from_mime_type(mime_type));
}
/* See LICENSE file for license and copyright information */
#ifndef PLUGIN_H
#define PLUGIN_H
#include <girara/types.h>
#include "types.h"
#include "plugin-api.h"
#include "version.h"
#include "zathura.h"
#define PLUGIN_REGISTER_FUNCTION "plugin_register"
#define PLUGIN_API_VERSION_FUNCTION "plugin_api_version"
#define PLUGIN_ABI_VERSION_FUNCTION "plugin_abi_version"
/**
* Plugin mapping
*/
typedef struct zathura_type_plugin_mapping_s
{
const gchar* type; /**< Plugin type */
zathura_plugin_t* plugin; /**< Mapped plugin */
} zathura_type_plugin_mapping_t;
/**
* Document plugin structure
*/
struct zathura_plugin_s
{
girara_list_t* content_types; /**< List of supported content types */
zathura_plugin_register_function_t register_function; /**< Document open function */
zathura_document_functions_t functions; /**< Document functions */
void* handle; /**< DLL handle */
};
/**
* Function prototype that is called to register a document plugin
*
* @param The document plugin
*/
typedef void (*zathura_plugin_register_service_t)(zathura_plugin_t*);
/**
* Function prototype that is called to get the plugin's API version.
*
* @return plugin's API version
*/
typedef unsigned int (*zathura_plugin_api_version_t)();
/**
* Function prototype that is called to get the ABI version the plugin is built
* against.
*
* @return plugin's ABI version
*/
typedef unsigned int (*zathura_plugin_abi_version_t)();
/**
* Load all document plugins
*
* @param zathura the zathura session
*/
void zathura_document_plugins_load(zathura_t* zathura);
/**
* Free a document plugin
*
* @param plugin The plugin
*/
void zathura_document_plugin_free(zathura_plugin_t* plugin);
/**
* Add type -> plugin mapping
* @param zathura zathura instance
* @param type content type
* @param plugin plugin instance
* @return true on success, false if another plugin is already registered for
* that type
*/
bool zathura_type_plugin_mapping_new(zathura_t* zathura, const gchar* type, zathura_plugin_t* plugin);
/**
* Free type -> plugin mapping
* @param mapping To be freed
*/
void zathura_type_plugin_mapping_free(zathura_type_plugin_mapping_t* mapping);
#endif // PLUGIN_H
......@@ -115,7 +115,7 @@ render(zathura_t* zathura, zathura_page_t* page)
cairo_scale(cairo, zathura->document->scale, zathura->document->scale);
}
if (zathura_page_render(page, cairo, false) != ZATHURA_PLUGIN_ERROR_OK) {
if (zathura_page_render(page, cairo, false) != ZATHURA_ERROR_OK) {
cairo_destroy(cairo);
cairo_surface_destroy(surface);
return false;
......
/* See LICENSE file for license and copyright information */
#include <stdlib.h>
#include "types.h"
zathura_link_t*
......@@ -78,3 +80,68 @@ zathura_link_get_target(zathura_link_t* link)
return link->target;
}
zathura_index_element_t*
zathura_index_element_new(const char* title)
{
if (title == NULL) {
return NULL;
}
zathura_index_element_t* res = g_malloc0(sizeof(zathura_index_element_t));
res->title = g_strdup(title);
return res;
}
void
zathura_index_element_free(zathura_index_element_t* index)
{
if (index == NULL) {
return;
}
g_free(index->title);
if (index->type == ZATHURA_LINK_EXTERNAL) {
g_free(index->target.uri);
}
g_free(index);
}
zathura_image_buffer_t*
zathura_image_buffer_create(unsigned int width, unsigned int height)
{
zathura_image_buffer_t* image_buffer = malloc(sizeof(zathura_image_buffer_t));
if (image_buffer == NULL) {
return NULL;
}
image_buffer->data = calloc(width * height * 3, sizeof(unsigned char));
if (image_buffer->data == NULL) {
free(image_buffer);
return NULL;
}
image_buffer->width = width;
image_buffer->height = height;
image_buffer->rowstride = width * 3;
return image_buffer;
}
void
zathura_image_buffer_free(zathura_image_buffer_t* image_buffer)
{
if (image_buffer == NULL) {
return;
}
free(image_buffer->data);
free(image_buffer);
}
......@@ -5,6 +5,24 @@
#include "zathura.h"
/**
* Plugin
*/
typedef struct zathura_plugin_s zathura_plugin_t;
/**
* Error types
*/
typedef enum zathura_plugin_error_e
{
ZATHURA_ERROR_OK, /**< No error occured */
ZATHURA_ERROR_UNKNOWN, /**< An unknown error occured */
ZATHURA_ERROR_OUT_OF_MEMORY, /**< Out of memory */
ZATHURA_ERROR_NOT_IMPLEMENTED, /**< The called function has not been implemented */
ZATHURA_ERROR_INVALID_ARGUMENTS, /**< Invalid arguments have been passed */
ZATHURA_ERROR_INVALID_PASSWORD /**< The provided password is invalid */
} zathura_error_t;
/**
* Image buffer
*/
......@@ -133,7 +151,7 @@ void zathura_index_element_free(zathura_index_element_t* index);
* @param type Type of the link
* @param position Position of the link
* @param target Target
* @return New zathura link
* @return New zathura link
*/
zathura_link_t* zathura_link_new(zathura_link_type_t type, zathura_rectangle_t position,
zathura_link_target_t target);
......@@ -149,14 +167,14 @@ void zathura_link_free(zathura_link_t* link);
* Returns the type of the link
*
* @param link The link
* @return The target type of the link
* @return The target type of the link
*/
zathura_link_type_t zathura_link_get_type(zathura_link_t* link);
/**
* Returns the position of the link
*
* @param link The link
* @param link The link
* @return The position of the link
*/
zathura_rectangle_t zathura_link_get_position(zathura_link_t* link);
......
......@@ -13,6 +13,7 @@
#include "zathura.h"
#include "document.h"