diff --git a/Makefile b/Makefile index 408d01d..c3efd9d 100644 --- a/Makefile +++ b/Makefile @@ -4,7 +4,7 @@ include config.mk PROJECT = zathura -SOURCE = $(shell find . -iname "*.c" -a ! -iwholename "*./doc*") +SOURCE = $(shell find . -iname "*.c" -a ! -iwholename "*./doc*|*./ft*") OBJECTS = $(patsubst %.c, %.o, $(SOURCE)) DOBJECTS = $(patsubst %.c, %.do, $(SOURCE)) diff --git a/callbacks.c b/callbacks.c index fd1c1a0..66c166b 100644 --- a/callbacks.c +++ b/callbacks.c @@ -7,7 +7,7 @@ #include "zathura.h" #include "render.h" -#include "ft/document.h" +#include "document.h" gboolean cb_destroy(GtkWidget* widget, gpointer data) @@ -18,6 +18,9 @@ cb_destroy(GtkWidget* widget, gpointer data) document_close(); + /* free registered plugins */ + zathura_document_plugin_free(); + return TRUE; } diff --git a/ft/document.c b/document.c similarity index 82% rename from ft/document.c rename to document.c index e672257..3fa9d5d 100644 --- a/ft/document.c +++ b/document.c @@ -10,17 +10,70 @@ #include #include "document.h" -#include "pdf/pdf.h" -#include "djvu/djvu.h" -#include "../utils.h" -#include "../zathura.h" +#include "utils.h" +#include "zathura.h" #define LENGTH(x) (sizeof(x)/sizeof((x)[0])) -zathura_document_plugin_t zathura_document_plugins[] = { - { "pdf", pdf_document_open }, - { "djvu", djvu_document_open }, -}; +zathura_document_plugin_t* zathura_document_plugins = NULL; + +bool +zathura_document_register_plugin(char* file_extension, zathura_document_open_t open_function) +{ + if( (file_extension == NULL) || (open_function == NULL) ) { + fprintf(stderr, "plugin: could not register\n"); + return false; + } + + /* search existing plugins */ + zathura_document_plugin_t* plugin = zathura_document_plugins; + while (plugin) { + if (!strcmp(plugin->file_extension, file_extension)) { + fprintf(stderr, "plugin: already registered for filetype %s\n", file_extension); + return false; + } + + if (plugin->next == NULL) { + break; + } + + plugin = plugin->next; + } + + /* create new plugin */ + zathura_document_plugin_t* new_plugin = malloc(sizeof(zathura_document_plugin_t)); + + if (new_plugin == NULL) { + return false; + } + + new_plugin->file_extension = file_extension; + new_plugin->open_function = open_function; + new_plugin->next = NULL; + + /* append to list */ + if (plugin == NULL) { + zathura_document_plugins = new_plugin; + } else { + plugin->next = new_plugin; + } + + return true; +} + +void +zathura_document_plugin_free(void) +{ + /* free registered plugins */ + zathura_document_plugin_t* plugin = zathura_document_plugins; + while (plugin) { + zathura_document_plugin_t* tmp = plugin->next; + free(plugin); + plugin = tmp; + } + + zathura_document_plugins = NULL; +} zathura_document_t* zathura_document_open(const char* path, const char* password) @@ -88,11 +141,11 @@ zathura_document_open(const char* path, const char* password) document->functions.page_render = NULL; /* init plugin with associated file type */ - for (unsigned int i = 0; i < LENGTH(zathura_document_plugins); i++) - { - if (!strcmp(file_extension, zathura_document_plugins[i].file_extension)) { - if (zathura_document_plugins[i].open_function) { - if (zathura_document_plugins[i].open_function(document)) { + zathura_document_plugin_t* plugin = zathura_document_plugins; + while (plugin) { + if (!strcmp(file_extension, plugin->file_extension)) { + if (plugin->open_function) { + if (plugin->open_function(document)) { /* update statusbar */ girara_statusbar_item_set_text(Zathura.UI.session, Zathura.UI.statusbar.file, real_path); @@ -122,6 +175,8 @@ zathura_document_open(const char* path, const char* password) } } } + + plugin = plugin->next; } fprintf(stderr, "error: unknown file type\n"); diff --git a/ft/document.h b/document.h similarity index 94% rename from ft/document.h rename to document.h index 725c8e8..b8e03d3 100644 --- a/ft/document.h +++ b/document.h @@ -15,8 +15,9 @@ typedef bool (*zathura_document_open_t)(zathura_document_t* document); typedef struct zathura_document_plugin_s { - const char* file_extension; + char* file_extension; zathura_document_open_t open_function; + struct zathura_document_plugin_s *next; } zathura_document_plugin_t; struct zathura_list_s @@ -113,6 +114,9 @@ struct zathura_document_s zathura_page_t** pages; }; +bool zathura_document_plugin_register(char* file_extension, zathura_document_open_t open_function); +void zathura_document_plugin_free(void); + zathura_document_t* zathura_document_open(const char* path, const char* password); bool zathura_document_free(zathura_document_t* document); bool zathura_document_save_as(zathura_document_t* document, const char* path); diff --git a/ft/djvu/djvu.h b/ft/djvu/djvu.h index 5f6d894..f043f69 100644 --- a/ft/djvu/djvu.h +++ b/ft/djvu/djvu.h @@ -6,7 +6,7 @@ #include #include -#include "../document.h" +#include "../../document.h" typedef struct djvu_document_s { diff --git a/ft/pdf/pdf.h b/ft/pdf/pdf.h index 39afbe0..5b959d0 100644 --- a/ft/pdf/pdf.h +++ b/ft/pdf/pdf.h @@ -6,7 +6,7 @@ #include #include -#include "../document.h" +#include "../../document.h" typedef struct pdf_document_s { diff --git a/render.h b/render.h index 4e430ab..1f48f95 100644 --- a/render.h +++ b/render.h @@ -7,7 +7,7 @@ #include #include -#include "ft/document.h" +#include "document.h" #include "callbacks.h" typedef struct render_thread_s diff --git a/zathura.c b/zathura.c index 26bedde..7295957 100644 --- a/zathura.c +++ b/zathura.c @@ -4,7 +4,7 @@ #include "callbacks.h" #include "config.h" -#include "ft/document.h" +#include "document.h" #include "shortcuts.h" #include "zathura.h" #include "utils.h" diff --git a/zathura.h b/zathura.h index 7b283c5..2002e27 100644 --- a/zathura.h +++ b/zathura.h @@ -7,7 +7,7 @@ #include #include -#include "ft/document.h" +#include "document.h" enum { NEXT, PREVIOUS, LEFT, RIGHT, UP, DOWN, BOTTOM, TOP, HIDE, HIGHLIGHT, DELETE_LAST_WORD, DELETE_LAST_CHAR, DEFAULT, ERROR, WARNING, NEXT_GROUP,