From 5ad335043da4a9c447f4d6d648f9bade3e94d35d Mon Sep 17 00:00:00 2001 From: Moritz Lipp Date: Wed, 13 Jun 2012 16:08:24 +0200 Subject: [PATCH] Hide plugin structure and introduce new functions --- document.c | 35 ++++++++++------- page.c | 46 +++++++++++++--------- plugin-api.h | 9 +++++ plugin.c | 109 ++++++++++++++++++++++++++++++++++++++++++++++++--- plugin.h | 62 +++++++++++++++-------------- 5 files changed, 195 insertions(+), 66 deletions(-) diff --git a/document.c b/document.c index 474d4b5..1dc6637 100644 --- a/document.c +++ b/document.c @@ -121,12 +121,13 @@ zathura_document_open(zathura_plugin_manager_t* plugin_manager, const char* document->adjust_mode = ZATHURA_ADJUST_NONE; /* open document */ - if (plugin->functions.document_open == NULL) { + zathura_plugin_functions_t* functions = zathura_plugin_get_functions(plugin); + if (functions->document_open == NULL) { girara_error("plugin has no open function\n"); goto error_free; } - zathura_error_t int_error = plugin->functions.document_open(document); + zathura_error_t int_error = functions->document_open(document); if (int_error != ZATHURA_ERROR_OK) { if (error != NULL) { *error = int_error; @@ -186,10 +187,11 @@ zathura_document_free(zathura_document_t* document) /* free document */ zathura_error_t error = ZATHURA_ERROR_OK; - if (document->plugin->functions.document_free == NULL) { + zathura_plugin_functions_t* functions = zathura_plugin_get_functions(document->plugin); + if (functions->document_free == NULL) { error = ZATHURA_ERROR_NOT_IMPLEMENTED; } else { - error = document->plugin->functions.document_free(document, document->data); + error = functions->document_free(document, document->data); } if (document->file_path != NULL) { @@ -395,11 +397,12 @@ zathura_document_save_as(zathura_document_t* document, const char* path) return ZATHURA_ERROR_UNKNOWN; } - if (document->plugin->functions.document_save_as == NULL) { + zathura_plugin_functions_t* functions = zathura_plugin_get_functions(document->plugin); + if (functions->document_save_as == NULL) { return ZATHURA_ERROR_NOT_IMPLEMENTED; } - return document->plugin->functions.document_save_as(document, document->data, path); + return functions->document_save_as(document, document->data, path); } girara_tree_node_t* @@ -412,14 +415,15 @@ zathura_document_index_generate(zathura_document_t* document, zathura_error_t* e return NULL; } - if (document->plugin->functions.document_index_generate == NULL) { + zathura_plugin_functions_t* functions = zathura_plugin_get_functions(document->plugin); + if (functions->document_index_generate == NULL) { if (error != NULL) { *error = ZATHURA_ERROR_NOT_IMPLEMENTED; } return NULL; } - return document->plugin->functions.document_index_generate(document, document->data, error); + return functions->document_index_generate(document, document->data, error); } girara_list_t* @@ -432,14 +436,15 @@ zathura_document_attachments_get(zathura_document_t* document, zathura_error_t* return NULL; } - if (document->plugin->functions.document_attachments_get == NULL) { + zathura_plugin_functions_t* functions = zathura_plugin_get_functions(document->plugin); + if (functions->document_attachments_get == NULL) { if (error != NULL) { *error = ZATHURA_ERROR_NOT_IMPLEMENTED; } return NULL; } - return document->plugin->functions.document_attachments_get(document, document->data, error); + return functions->document_attachments_get(document, document->data, error); } zathura_error_t @@ -449,11 +454,12 @@ zathura_document_attachment_save(zathura_document_t* document, const char* attac return ZATHURA_ERROR_INVALID_ARGUMENTS; } - if (document->plugin->functions.document_attachment_save == NULL) { + zathura_plugin_functions_t* functions = zathura_plugin_get_functions(document->plugin); + if (functions->document_attachment_save == NULL) { return ZATHURA_ERROR_NOT_IMPLEMENTED; } - return document->plugin->functions.document_attachment_save(document, document->data, attachment, file); + return functions->document_attachment_save(document, document->data, attachment, file); } girara_list_t* @@ -466,14 +472,15 @@ zathura_document_get_information(zathura_document_t* document, zathura_error_t* return NULL; } - if (document->plugin->functions.document_get_information == NULL) { + zathura_plugin_functions_t* functions = zathura_plugin_get_functions(document->plugin); + if (functions->document_get_information == NULL) { if (error != NULL) { *error = ZATHURA_ERROR_NOT_IMPLEMENTED; } return NULL; } - girara_list_t* result = document->plugin->functions.document_get_information(document, document->data, error); + girara_list_t* result = functions->document_get_information(document, document->data, error); if (result != NULL) { girara_list_set_free_function(result, (girara_free_function_t) zathura_document_information_entry_free); } diff --git a/page.c b/page.c index ff70a10..d31af46 100644 --- a/page.c +++ b/page.c @@ -39,14 +39,16 @@ zathura_page_new(zathura_document_t* document, unsigned int index, zathura_error /* init plugin */ zathura_plugin_t* plugin = zathura_document_get_plugin(document); - if (plugin->functions.page_init == NULL) { + zathura_plugin_functions_t* functions = zathura_plugin_get_functions(plugin); + + if (functions->page_init == NULL) { if (error != NULL) { *error = ZATHURA_ERROR_NOT_IMPLEMENTED; } goto error_ret; } - zathura_error_t ret = plugin->functions.page_init(page); + zathura_error_t ret = functions->page_init(page); if (ret != ZATHURA_ERROR_OK) { if (error != NULL) { *error = ret; @@ -80,11 +82,12 @@ zathura_page_free(zathura_page_t* page) } zathura_plugin_t* plugin = zathura_document_get_plugin(page->document); - if (plugin->functions.page_clear == NULL) { + zathura_plugin_functions_t* functions = zathura_plugin_get_functions(plugin); + if (functions->page_clear == NULL) { return ZATHURA_ERROR_NOT_IMPLEMENTED; } - zathura_error_t error = plugin->functions.page_clear(page, page->data); + zathura_error_t error = functions->page_clear(page, page->data); g_free(page); @@ -202,14 +205,15 @@ zathura_page_search_text(zathura_page_t* page, const char* text, zathura_error_t } zathura_plugin_t* plugin = zathura_document_get_plugin(page->document); - if (plugin->functions.page_search_text == NULL) { + zathura_plugin_functions_t* functions = zathura_plugin_get_functions(plugin); + if (functions->page_search_text == NULL) { if (error != NULL) { *error = ZATHURA_ERROR_NOT_IMPLEMENTED; } return NULL; } - return plugin->functions.page_search_text(page, page->data, text, error); + return functions->page_search_text(page, page->data, text, error); } girara_list_t* @@ -223,14 +227,15 @@ zathura_page_links_get(zathura_page_t* page, zathura_error_t* error) } zathura_plugin_t* plugin = zathura_document_get_plugin(page->document); - if (plugin->functions.page_links_get == NULL) { + zathura_plugin_functions_t* functions = zathura_plugin_get_functions(plugin); + if (functions->page_links_get == NULL) { if (error != NULL) { *error = ZATHURA_ERROR_NOT_IMPLEMENTED; } return NULL; } - return plugin->functions.page_links_get(page, page->data, error); + return functions->page_links_get(page, page->data, error); } zathura_error_t @@ -250,14 +255,15 @@ zathura_page_form_fields_get(zathura_page_t* page, zathura_error_t* error) } zathura_plugin_t* plugin = zathura_document_get_plugin(page->document); - if (plugin->functions.page_form_fields_get == NULL) { + zathura_plugin_functions_t* functions = zathura_plugin_get_functions(plugin); + if (functions->page_form_fields_get == NULL) { if (error != NULL) { *error = ZATHURA_ERROR_NOT_IMPLEMENTED; } return NULL; } - return plugin->functions.page_form_fields_get(page, page->data, error); + return functions->page_form_fields_get(page, page->data, error); } zathura_error_t @@ -277,14 +283,15 @@ zathura_page_images_get(zathura_page_t* page, zathura_error_t* error) } zathura_plugin_t* plugin = zathura_document_get_plugin(page->document); - if (plugin->functions.page_images_get == NULL) { + zathura_plugin_functions_t* functions = zathura_plugin_get_functions(plugin); + if (functions->page_images_get == NULL) { if (error != NULL) { *error = ZATHURA_ERROR_NOT_IMPLEMENTED; } return NULL; } - return plugin->functions.page_images_get(page, page->data, error); + return functions->page_images_get(page, page->data, error); } cairo_surface_t* @@ -298,14 +305,15 @@ zathura_page_image_get_cairo(zathura_page_t* page, zathura_image_t* image, zathu } zathura_plugin_t* plugin = zathura_document_get_plugin(page->document); - if (plugin->functions.page_image_get_cairo == NULL) { + zathura_plugin_functions_t* functions = zathura_plugin_get_functions(plugin); + if (functions->page_image_get_cairo == NULL) { if (error != NULL) { *error = ZATHURA_ERROR_NOT_IMPLEMENTED; } return NULL; } - return plugin->functions.page_image_get_cairo(page, page->data, image, error); + return functions->page_image_get_cairo(page, page->data, image, error); } char* zathura_page_get_text(zathura_page_t* page, zathura_rectangle_t rectangle, zathura_error_t* error) @@ -318,14 +326,15 @@ char* zathura_page_get_text(zathura_page_t* page, zathura_rectangle_t rectangle, } zathura_plugin_t* plugin = zathura_document_get_plugin(page->document); - if (plugin->functions.page_get_text == NULL) { + zathura_plugin_functions_t* functions = zathura_plugin_get_functions(plugin); + if (functions->page_get_text == NULL) { if (error) { *error = ZATHURA_ERROR_NOT_IMPLEMENTED; } return NULL; } - return plugin->functions.page_get_text(page, page->data, rectangle, error); + return functions->page_get_text(page, page->data, rectangle, error); } zathura_error_t @@ -336,9 +345,10 @@ zathura_page_render(zathura_page_t* page, cairo_t* cairo, bool printing) } zathura_plugin_t* plugin = zathura_document_get_plugin(page->document); - if (plugin->functions.page_render_cairo == NULL) { + zathura_plugin_functions_t* functions = zathura_plugin_get_functions(plugin); + if (functions->page_render_cairo == NULL) { return ZATHURA_ERROR_NOT_IMPLEMENTED; } - return plugin->functions.page_render_cairo(page, page->data, cairo, printing); + return functions->page_render_cairo(page, page->data, cairo, printing); } diff --git a/plugin-api.h b/plugin-api.h index 95946c4..4043129 100644 --- a/plugin-api.h +++ b/plugin-api.h @@ -27,6 +27,14 @@ typedef void (*zathura_plugin_register_function_t)(zathura_plugin_functions_t* f void zathura_plugin_set_register_functions_function(zathura_plugin_t* plugin, zathura_plugin_register_function_t register_function); +/** + * Sets the name of the plugin + * + * @param plugin The plugin + * @param name The name of the plugin + */ +void zathura_plugin_set_name(zathura_plugin_t* plugin, const char* name); + /** * Sets the functions register function of the plugin * @@ -58,6 +66,7 @@ void zathura_plugin_add_mimetype(zathura_plugin_t* plugin, const char* mime_type return; \ } \ zathura_plugin_set_register_functions_function(plugin, register_functions); \ + zathura_plugin_set_name(plugin, plugin_name); \ 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]); \ diff --git a/plugin.c b/plugin.c index 3197bb5..6e74e35 100644 --- a/plugin.c +++ b/plugin.c @@ -11,7 +11,28 @@ #include #include -typedef unsigned int (*zathura_plugin_version_t)(void); +/** + * 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_plugin_functions_t functions; /**< Document functions */ + GModule* handle; /**< DLL handle */ + char* name; /**< Name of the plugin */ + char* path; /**< Path to the plugin */ + zathura_plugin_version_t version; /**< Version information */ +}; + +/** + * 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; /** * Plugin manager @@ -23,6 +44,11 @@ struct zathura_plugin_manager_s girara_list_t* type_plugin_mapping; /**< List of type -> plugin mappings */ }; +typedef void (*zathura_plugin_register_service_t)(zathura_plugin_t*); +typedef unsigned int (*zathura_plugin_api_version_t)(void); +typedef unsigned int (*zathura_plugin_abi_version_t)(void); +typedef unsigned int (*zathura_plugin_version_function_t)(void); + static bool register_plugin(zathura_plugin_manager_t* plugin_manager, zathura_plugin_t* plugin); static bool plugin_mapping_new(zathura_plugin_manager_t* plugin_manager, const gchar* type, zathura_plugin_t* plugin); static void zathura_plugin_free(zathura_plugin_t* plugin); @@ -149,11 +175,13 @@ zathura_plugin_manager_load(zathura_plugin_manager_t* plugin_manager) if (plugin->register_function == NULL) { girara_error("plugin has no document functions register function"); g_free(path); + g_free(plugin); g_module_close(handle); continue; } plugin->register_function(&(plugin->functions)); + plugin->path = path; bool ret = register_plugin(plugin_manager, plugin); if (ret == false) { @@ -162,16 +190,19 @@ zathura_plugin_manager_load(zathura_plugin_manager_t* plugin_manager) } else { girara_info("successfully loaded plugin %s", path); - zathura_plugin_version_t major = NULL, minor = NULL, rev = NULL; + zathura_plugin_version_function_t major = NULL, minor = NULL, rev = NULL; g_module_symbol(handle, PLUGIN_VERSION_MAJOR_FUNCTION, (gpointer*) &major); g_module_symbol(handle, PLUGIN_VERSION_MINOR_FUNCTION, (gpointer*) &minor); g_module_symbol(handle, PLUGIN_VERSION_REVISION_FUNCTION, (gpointer*) &rev); if (major != NULL && minor != NULL && rev != NULL) { - girara_debug("plugin '%s': version %u.%u.%u", path, major(), minor(), rev()); + plugin->version.major = major(); + plugin->version.minor = minor(); + plugin->version.rev = rev(); + girara_debug("plugin '%s': version %u.%u.%u", path, + plugin->version.major, plugin->version.minor, + plugin->version.rev); } } - - g_free(path); } g_dir_close(dir); GIRARA_LIST_FOREACH_END(zathura->plugins.path, char*, iter, plugindir); @@ -195,6 +226,16 @@ zathura_plugin_manager_get_plugin(zathura_plugin_manager_t* plugin_manager, cons return plugin; } +girara_list_t* +zathura_plugin_manager_get_plugins(zathura_plugin_manager_t* plugin_manager) +{ + if (plugin_manager == NULL || plugin_manager->plugins == NULL) { + return NULL; + } + + return plugin_manager->plugins; +} + void zathura_plugin_manager_free(zathura_plugin_manager_t* plugin_manager) { @@ -285,8 +326,17 @@ zathura_plugin_free(zathura_plugin_t* plugin) return; } + if (plugin->name != NULL) { + g_free(plugin->name); + } + + if (plugin->path != NULL) { + g_free(plugin->path); + } + g_module_close(plugin->handle); girara_list_free(plugin->content_types); + g_free(plugin); } @@ -310,3 +360,52 @@ zathura_plugin_add_mimetype(zathura_plugin_t* plugin, const char* mime_type) girara_list_append(plugin->content_types, g_content_type_from_mime_type(mime_type)); } + +zathura_plugin_functions_t* +zathura_plugin_get_functions(zathura_plugin_t* plugin) +{ + if (plugin != NULL) { + return &(plugin->functions); + } else { + return NULL; + } +} + +void +zathura_plugin_set_name(zathura_plugin_t* plugin, const char* name) +{ + if (plugin != NULL && name != NULL) { + plugin->name = g_strdup(name); + } +} + +char* +zathura_plugin_get_name(zathura_plugin_t* plugin) +{ + if (plugin != NULL) { + return plugin->name; + } else { + return NULL; + } +} + +char* +zathura_plugin_get_path(zathura_plugin_t* plugin) +{ + if (plugin != NULL) { + return plugin->path; + } else { + return NULL; + } +} + +zathura_plugin_version_t +zathura_plugin_get_version(zathura_plugin_t* plugin) +{ + if (plugin != NULL) { + return plugin->version; + } + + zathura_plugin_version_t version = { 0 }; + return version; +} diff --git a/plugin.h b/plugin.h index abc08cc..9512a0a 100644 --- a/plugin.h +++ b/plugin.h @@ -18,16 +18,11 @@ #define PLUGIN_VERSION_MINOR_FUNCTION "zathura_plugin_version_minor" #define PLUGIN_VERSION_REVISION_FUNCTION "zathura_plugin_version_revision" -/** - * 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_plugin_functions_t functions; /**< Document functions */ - GModule* handle; /**< DLL handle */ -}; +typedef struct zathura_plugin_version_s { + unsigned int major; /**< Major */ + unsigned int minor; /**< Minor */ + unsigned int rev; /**< Revision */ +} zathura_plugin_version_t; /** * Creates a new instance of the plugin manager @@ -36,6 +31,13 @@ struct zathura_plugin_s */ zathura_plugin_manager_t* zathura_plugin_manager_new(); +/** + * Frees the plugin manager + * + * @param plugin_manager + */ +void zathura_plugin_manager_free(zathura_plugin_manager_t* plugin_manager); + /** * Adds a plugin directory to the plugin manager * @@ -61,41 +63,43 @@ void zathura_plugin_manager_load(zathura_plugin_manager_t* plugin_manager); zathura_plugin_t* zathura_plugin_manager_get_plugin(zathura_plugin_manager_t* plugin_manager, const char* type); /** - * Frees the plugin manager + * Returns a list with the plugin objects * - * @param plugin_manager + * @param plugin_manager The plugin manager + * @return List of plugins or NULL */ -void zathura_plugin_manager_free(zathura_plugin_manager_t* plugin_manager); +girara_list_t* zathura_plugin_manager_get_plugins(zathura_plugin_manager_t* plugin_manager); /** - * Plugin mapping + * Returns the plugin functions + * + * @param plugin The plugin + * @return The plugin functions */ -typedef struct zathura_type_plugin_mapping_s -{ - const gchar* type; /**< Plugin type */ - zathura_plugin_t* plugin; /**< Mapped plugin */ -} zathura_type_plugin_mapping_t; +zathura_plugin_functions_t* zathura_plugin_get_functions(zathura_plugin_t* plugin); /** - * Function prototype that is called to register a document plugin + * Returns the name of the plugin * - * @param The document plugin + * @param plugin The plugin + * @return The name of the plugin or NULL */ -typedef void (*zathura_plugin_register_service_t)(zathura_plugin_t*); +char* zathura_plugin_get_name(zathura_plugin_t* plugin); /** - * Function prototype that is called to get the plugin's API version. + * Returns the path to the plugin * - * @return plugin's API version + * @param plugin The plugin + * @return The path of the plugin or NULL */ -typedef unsigned int (*zathura_plugin_api_version_t)(); +char* zathura_plugin_get_path(zathura_plugin_t* plugin); /** - * Function prototype that is called to get the ABI version the plugin is built - * against. + * Returns the version information of the plugin * - * @return plugin's ABI version + * @param plugin The plugin + * @return The version information of the plugin */ -typedef unsigned int (*zathura_plugin_abi_version_t)(); +zathura_plugin_version_t zathura_plugin_get_version(zathura_plugin_t* plugin); #endif // PLUGIN_H