From f57fe5dfde903b01e442faf6754f5715c522465f Mon Sep 17 00:00:00 2001 From: Sebastian Ramacher Date: Mon, 18 Apr 2011 18:19:41 +0200 Subject: [PATCH] update document.c/.h --- config.h | 2 ++ document.c | 92 ++++++++++++++++++++++++++---------------------------- document.h | 20 +++++++----- render.h | 6 ++-- zathura.c | 11 +++++-- zathura.h | 19 +++++++++-- 6 files changed, 85 insertions(+), 65 deletions(-) diff --git a/config.h b/config.h index 4103598..b5b152d 100644 --- a/config.h +++ b/config.h @@ -7,6 +7,8 @@ /** * This function loads the default values of the configuration + * + * @param zathura the zathura session */ void config_load_default(zathura_t* zathura); diff --git a/document.c b/document.c index 99a41fb..6a1d462 100644 --- a/document.c +++ b/document.c @@ -18,10 +18,8 @@ #define LENGTH(x) (sizeof(x)/sizeof((x)[0])) -zathura_document_plugin_t* zathura_document_plugins = NULL; - void -zathura_document_plugins_load(void) +zathura_document_plugins_load(zathura_t* zathura) { /* read all files in the plugin directory */ DIR* dir = opendir(PLUGIN_DIR); @@ -76,7 +74,7 @@ zathura_document_plugins_load(void) register_plugin(plugin); - bool r = zathura_document_plugin_register(plugin, handle); + bool r = zathura_document_plugin_register(zathura, plugin, handle); if (r == false) { fprintf(stderr, "error: could not register plugin (%s)\n", path); @@ -107,60 +105,54 @@ error_continue: } void -zathura_document_plugins_free(void) +zathura_document_plugins_free(zathura_t* zathura) { - /* free registered plugins */ - zathura_document_plugin_t* plugin = zathura_document_plugins; - while (plugin) { - zathura_document_plugin_t* tmp = plugin->next; - free(plugin->file_extension); - free(plugin); - plugin = tmp; + if (zathura == NULL) { + return; } - zathura_document_plugins = NULL; + girara_list_iterator_t* iter = girara_list_iterator(zathura->plugins.plugins); + if (iter == NULL) { + return; + } + + do { + zathura_document_plugin_t* plugin = (zathura_document_plugin_t*) girara_list_iterator_data(iter); + free(plugin->file_extension); + free(plugin); + } while (girara_list_iterator_next(iter)); + girara_list_iterator_free(iter); } bool -zathura_document_plugin_register(zathura_document_plugin_t* new_plugin, void* handle) +zathura_document_plugin_register(zathura_t* zathura, zathura_document_plugin_t* new_plugin, void* handle) { if( (new_plugin == NULL) || (new_plugin->file_extension == NULL) || (new_plugin->open_function == NULL) || (handle == NULL) ) { - fprintf(stderr, "plugin: could not register\n"); + girara_error("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, new_plugin->file_extension)) { - fprintf(stderr, "plugin: already registered for filetype %s\n", plugin->file_extension); - return false; - } - - if (plugin->next == NULL) { - break; - } - - plugin = plugin->next; - } - - /* create new plugin */ - new_plugin->handle = handle; - new_plugin->next = NULL; - - /* append to list */ - if (plugin == NULL) { - zathura_document_plugins = new_plugin; - } else { - plugin->next = new_plugin; + girara_list_iterator_t* iter = girara_list_iterator(zathura->plugins.plugins); + if (iter) { + do { + zathura_document_plugin_t* plugin = (zathura_document_plugin_t*) girara_list_iterator_data(iter); + if (!strcmp(plugin->file_extension, new_plugin->file_extension)) { + girara_error("plugin: already registered for filetype %s\n", plugin->file_extension); + girara_list_iterator_free(iter); + return false; + } + } while (girara_list_iterator_next(iter)); + girara_list_iterator_free(iter); } + girara_list_append(zathura->plugins.plugins, new_plugin); return true; } zathura_document_t* -zathura_document_open(const char* path, const char* password) +zathura_document_open(zathura_t* zathura, const char* path, const char* password) { if (!path) { goto error_out; @@ -224,14 +216,19 @@ zathura_document_open(const char* path, const char* password) document->functions.page_form_fields_get = NULL; document->functions.page_render = NULL; - /* init plugin with associated file type */ - zathura_document_plugin_t* plugin = zathura_document_plugins; - while (plugin) { + girara_list_iterator_t* iter = girara_list_iterator(zathura->plugins.plugins); + if (iter == NULL) { + return; + } + + do { + zathura_document_plugin_t* plugin = (zathura_document_plugin_t*) girara_list_iterator_data(iter); if (!strcmp(file_extension, plugin->file_extension)) { + girara_list_iterator_free(iter); 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); + girara_statusbar_item_set_text(zathura->ui.session, zathura->ui.statusbar.file, real_path); /* read all pages */ document->pages = calloc(document->number_of_pages, sizeof(zathura_page_t*)); @@ -250,16 +247,15 @@ zathura_document_open(const char* path, const char* password) return document; } else { - fprintf(stderr, "error: could not open file\n"); + girara_error("could not open file\n"); goto error_free; } } } + } while (girara_list_iterator_next(iter)); + girara_list_iterator_free(iter); - plugin = plugin->next; - } - - fprintf(stderr, "error: unknown file type\n"); + girara_error("unknown file type\n"); error_free: diff --git a/document.h b/document.h index 5fb78e2..2536958 100644 --- a/document.h +++ b/document.h @@ -7,12 +7,13 @@ #include #include +#include "zathura.h" #define PLUGIN_DIR "/usr/lib/zathura" #define PLUGIN_REGISTER_FUNCTION "plugin_register" typedef struct zathura_list_s zathura_list_t; -typedef struct zathura_document_s zathura_document_t; +// typedef struct zathura_document_s zathura_document_t; typedef bool (*zathura_document_open_t)(zathura_document_t* document); @@ -24,7 +25,6 @@ typedef struct zathura_document_plugin_s char* file_extension; /**> File extension */ zathura_document_open_t open_function; /**> Document open function */ void* handle; /**> DLL handle */ - struct zathura_document_plugin_s *next; /**> Next plugin */ // TODO: Use list_t } zathura_document_plugin_t; /** @@ -139,7 +139,7 @@ typedef struct zathura_form_s /** * Page */ -typedef struct zathura_page_s +struct zathura_page_s { double height; /**> Page height */ double width; /**> Page width */ @@ -150,7 +150,7 @@ typedef struct zathura_page_s GtkWidget* event_box; /**> Widget wrapper for mouse events */ GtkWidget* drawing_area; /**> Drawing area */ GStaticMutex lock; /**> Lock */ -} zathura_page_t; +}; /** * Document @@ -226,18 +226,22 @@ struct zathura_document_s /** * Load all document plugins + * + * @param zathura the zathura session */ -void zathura_document_plugins_load(void); +void zathura_document_plugins_load(zathura_t* zathura); /** * Free all document plugins + * + * @param zathura the zathura session */ -void zathura_document_plugins_free(void); +void zathura_document_plugins_free(zathura_t* zathura); /** * Register document plugin */ -bool zathura_document_plugin_register(zathura_document_plugin_t* new_plugin, void* handle); +bool zathura_document_plugin_register(zathura_t* zathura, zathura_document_plugin_t* new_plugin, void* handle); /** * Open the document @@ -246,7 +250,7 @@ bool zathura_document_plugin_register(zathura_document_plugin_t* new_plugin, voi * @param password Password of the document or NULL * @return The document object */ -zathura_document_t* zathura_document_open(const char* path, const char* password); +zathura_document_t* zathura_document_open(zathura_t* zathura, const char* path, const char* password); /** * Free the document diff --git a/render.h b/render.h index 1f48f95..362bafd 100644 --- a/render.h +++ b/render.h @@ -7,16 +7,16 @@ #include #include -#include "document.h" +#include "zathura.h" #include "callbacks.h" -typedef struct render_thread_s +struct render_thread_s { girara_list_t* list; /**> The list of pages */ GThread* thread; /**> The thread object */ GMutex* lock; /**> Lock */ GCond* cond; /**> Condition */ -} render_thread_t; +}; /** * This function initializes a render thread diff --git a/zathura.c b/zathura.c index 602cfb8..f1715ff 100644 --- a/zathura.c +++ b/zathura.c @@ -10,6 +10,7 @@ #include "shortcuts.h" #include "zathura.h" #include "utils.h" +#include "render.h" /* function implementation */ zathura_t* @@ -21,6 +22,10 @@ zathura_init(int argc, char* argv[]) return NULL; } + /* plugins */ + zathura->plugins.plugins = girara_list_new(); + zathura->plugins.path = NULL; + /* UI */ if ((zathura->ui.session = girara_session_create()) == NULL) { goto error_out; @@ -74,7 +79,7 @@ zathura_init(int argc, char* argv[]) zathura->ui.session->events.buffer_changed = buffer_changed; /* load plugins */ - zathura_document_plugins_load(); + zathura_document_plugins_load(zathura); /* configuration */ config_load_default(zathura); @@ -108,7 +113,7 @@ zathura_free(zathura_t* zathura) document_close(zathura); /* free registered plugins */ - zathura_document_plugins_free(); + zathura_document_plugins_free(zathura); } bool @@ -118,7 +123,7 @@ document_open(zathura_t* zathura, const char* path, const char* password) goto error_out; } - zathura_document_t* document = zathura_document_open(path, password); + zathura_document_t* document = zathura_document_open(zathura, path, password); if (!document) { goto error_out; diff --git a/zathura.h b/zathura.h index bb3a2cc..c90d5c1 100644 --- a/zathura.h +++ b/zathura.h @@ -6,9 +6,6 @@ #include #include -#include "render.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, PREVIOUS_GROUP, ZOOM_IN, ZOOM_OUT, ZOOM_ORIGINAL, ZOOM_SPECIFIC, FORWARD, @@ -24,6 +21,16 @@ enum { NEXT, PREVIOUS, LEFT, RIGHT, UP, DOWN, BOTTOM, TOP, HIDE, HIGHLIGHT, #define NORMAL (1 << 3) #define INSERT (1 << 4) +/* forward declaration for types from document.h */ +struct zathura_document_s; +struct zathura_page_s; +typedef struct zathura_document_s zathura_document_t; +typedef struct zathura_page_s zathura_page_t; + +/* forward declaration for types from render.h */ +struct render_thread_s; +typedef struct render_thread_s render_thread_t; + typedef struct zathura_s { struct @@ -46,6 +53,12 @@ typedef struct zathura_s render_thread_t* render_thread; /**> The thread responsible for rendering the pages */ } sync; + struct + { + girara_list_t* plugins; + girara_list_t* path; + } plugins; + zathura_document_t* document; /**> The current document */ } zathura_t;