From 13eeebc9c8452868df32b7ea95d65ea46073df58 Mon Sep 17 00:00:00 2001 From: Sebastian Ramacher Date: Thu, 29 Sep 2011 11:46:52 +0200 Subject: [PATCH 1/5] fix build error --- database-sqlite.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/database-sqlite.c b/database-sqlite.c index 346294c..bdb9d72 100644 --- a/database-sqlite.c +++ b/database-sqlite.c @@ -204,7 +204,7 @@ zathura_db_set_fileinfo(zathura_database_t* db, const char* file, unsigned int p bool zathura_db_get_fileinfo(zathura_database_t* db, const char* file, unsigned int* page, int* offset, float* scale) { - g_return_val_if_fail(db && file && page && ofset && scale, false); + g_return_val_if_fail(db && file && page && offset && scale, false); static const char SQL_FILEINFO_GET[] = "SELECT page, offset, scale FROM fileinfo WHERE file = ?;"; @@ -214,7 +214,7 @@ zathura_db_get_fileinfo(zathura_database_t* db, const char* file, unsigned int* return false; } - if (sqlite3_bind_text(stmt, 1, file, -1, NULL) != SQLITE_OK) + if (sqlite3_bind_text(stmt, 1, file, -1, NULL) != SQLITE_OK) { sqlite3_finalize(stmt); girara_error("Failed to bind arguments."); return false; From 9f5d6cdd61d079d8e1e6e4f856f83cce34d186dd Mon Sep 17 00:00:00 2001 From: Moritz Lipp Date: Thu, 29 Sep 2011 12:07:07 +0200 Subject: [PATCH 2/5] Added page_render_cairo function --- document.c | 24 ++++-------------------- document.h | 6 ++++++ 2 files changed, 10 insertions(+), 20 deletions(-) diff --git a/document.c b/document.c index 326e006..0d52706 100644 --- a/document.c +++ b/document.c @@ -206,32 +206,16 @@ zathura_document_open(zathura_t* zathura, const char* path, const char* password goto error_free; } - document = malloc(sizeof(zathura_document_t)); + document = g_malloc0(sizeof(zathura_document_t)); if (document == NULL) { goto error_free; } document->file_path = real_path; document->password = password; - document->current_page_number = 0; - document->number_of_pages = 0; document->scale = 1.0; - document->rotate = 0; - document->data = NULL; - document->pages = NULL; document->zathura = zathura; - document->functions.document_free = NULL; - document->functions.document_index_generate = NULL; - document->functions.document_save_as = NULL; - document->functions.document_attachments_get = NULL; - document->functions.page_get = NULL; - document->functions.page_free = NULL; - document->functions.page_search_text = NULL; - document->functions.page_links_get = NULL; - document->functions.page_form_fields_get = NULL; - document->functions.page_render = NULL; - girara_list_iterator_t* iter = girara_list_iterator(zathura->plugins.plugins); if (iter == NULL) { goto error_free; @@ -285,7 +269,7 @@ error_free: free(document->pages); } - free(document); + g_free(document); error_out: @@ -315,7 +299,7 @@ zathura_document_free(zathura_document_t* document) free(document->file_path); } - free(document); + g_free(document); return true; } @@ -325,7 +309,7 @@ zathura_document_free(zathura_document_t* document) free(document->file_path); } - free(document); + g_free(document); return r; } diff --git a/document.h b/document.h index 63517c4..3abc4bd 100644 --- a/document.h +++ b/document.h @@ -3,6 +3,7 @@ #ifndef DOCUMENT_H #define DOCUMENT_H +#include #include #include @@ -213,6 +214,11 @@ struct zathura_document_s */ zathura_image_buffer_t* (*page_render)(zathura_page_t* page); + /** + * Renders the page + */ + bool (*page_render_cairo)(zathura_page_t* page, cairo_t* cairo); + /** * Free page */ From c3ad7faf1aa1f6e34316e51524cbb799ddc879a8 Mon Sep 17 00:00:00 2001 From: Moritz Lipp Date: Thu, 29 Sep 2011 12:35:52 +0200 Subject: [PATCH 3/5] Use cairo to render page --- document.c | 10 ++--- document.h | 7 ++-- print.c | 43 ++++++++++---------- print.h | 2 +- render.c | 116 +++++++++++++++++++++++++++-------------------------- render.h | 2 +- 6 files changed, 92 insertions(+), 88 deletions(-) diff --git a/document.c b/document.c index 0d52706..3e39d5a 100644 --- a/document.c +++ b/document.c @@ -471,19 +471,19 @@ zathura_page_form_fields_free(zathura_list_t* UNUSED(list)) return false; } -zathura_image_buffer_t* -zathura_page_render(zathura_page_t* page) +bool +zathura_page_render(zathura_page_t* page, cairo_t* cairo) { - if (page == NULL || page->document == NULL) { + if (page == NULL || page->document == NULL || cairo == NULL) { return NULL; } - if (page->document->functions.page_render == NULL) { + if (page->document->functions.page_render_cairo == NULL) { girara_error("%s not implemented", __FUNCTION__); return NULL; } - return page->document->functions.page_render(page); + return page->document->functions.page_render_cairo(page, cairo); } zathura_index_element_t* diff --git a/document.h b/document.h index 3abc4bd..557ba1c 100644 --- a/document.h +++ b/document.h @@ -150,7 +150,7 @@ struct zathura_page_s GtkWidget* event_box; /**> Widget wrapper for mouse events */ GtkWidget* drawing_area; /**> Drawing area */ GStaticMutex lock; /**> Lock */ - cairo_surface_t* surface; /** Cairo surface */ + cairo_surface_t* surface; /** Cairo surface */ }; /** @@ -363,9 +363,10 @@ bool zathura_page_form_fields_free(zathura_list_t* list); * Render page * * @param page The page object - * @return Image buffer or NULL if an error occured + * @param cairo Cairo object + * @return True if no error occured, otherwise false */ -zathura_image_buffer_t* zathura_page_render(zathura_page_t* page); +bool zathura_page_render(zathura_page_t* page, cairo_t* cairo); /** * Create new index element diff --git a/print.c b/print.c index a9dd536..bf57f5b 100644 --- a/print.c +++ b/print.c @@ -51,32 +51,33 @@ void cb_print_draw_page(GtkPrintOperation* UNUSED(print_operation), GtkPrintContext* context, gint page_number, zathura_t* zathura) { - cairo_t* cairo = gtk_print_context_get_cairo_context(context); + /* TODO: Implement with cairo */ + /*cairo_t* cairo = gtk_print_context_get_cairo_context(context);*/ - girara_info("Printing page %d", page_number); + /*girara_info("Printing page %d", page_number);*/ - zathura_page_t* page = zathura->document->pages[page_number]; + /*zathura_page_t* page = zathura->document->pages[page_number];*/ - double requested_with = gtk_print_context_get_width(context); - double tmp_scale = zathura->document->scale; - zathura->document->scale = requested_with / page->width; + /*double requested_with = gtk_print_context_get_width(context);*/ + /*double tmp_scale = zathura->document->scale;*/ + /*zathura->document->scale = requested_with / page->width;*/ - g_static_mutex_lock(&(page->lock)); - zathura_image_buffer_t* image_buffer = zathura_page_render(page); - g_static_mutex_unlock(&(page->lock)); + /*g_static_mutex_lock(&(page->lock));*/ + /*zathura_image_buffer_t* image_buffer = zathura_page_render(page);*/ + /*g_static_mutex_unlock(&(page->lock));*/ - for (unsigned int y = 0; y < image_buffer->height; y++) { - unsigned char* src = image_buffer->data + y * image_buffer->rowstride; - for (unsigned int x = 0; x < image_buffer->width; x++) { - if (src[0] != 255 && src[1] != 255 && src[2] != 255) { - cairo_set_source_rgb(cairo, src[0], src[1], src[2]); - cairo_rectangle(cairo, x, y, 1, 1); - cairo_fill(cairo); - } + /*for (unsigned int y = 0; y < image_buffer->height; y++) {*/ + /*unsigned char* src = image_buffer->data + y * image_buffer->rowstride;*/ + /*for (unsigned int x = 0; x < image_buffer->width; x++) {*/ + /*if (src[0] != 255 && src[1] != 255 && src[2] != 255) {*/ + /*cairo_set_source_rgb(cairo, src[0], src[1], src[2]);*/ + /*cairo_rectangle(cairo, x, y, 1, 1);*/ + /*cairo_fill(cairo);*/ + /*}*/ - src += 3; - } - } + /*src += 3;*/ + /*}*/ + /*}*/ - zathura->document->scale = tmp_scale; + /*zathura->document->scale = tmp_scale;*/ } diff --git a/print.h b/print.h index a8fcb5a..628d399 100644 --- a/print.h +++ b/print.h @@ -21,6 +21,6 @@ void print(zathura_t* zathura); * @param zathura Zathura object */ void cb_print_draw_page(GtkPrintOperation* print_operation, GtkPrintContext* - context, gint page_number, zathura_t* zathura); + context, gint page_number, zathura_t* zathura); #endif // PRINT_H diff --git a/render.c b/render.c index e6831e9..c3503a5 100644 --- a/render.c +++ b/render.c @@ -151,36 +151,73 @@ render(zathura_t* zathura, zathura_page_t* page) gdk_threads_enter(); g_static_mutex_lock(&(page->lock)); - zathura_image_buffer_t* image_buffer = zathura_page_render(page); - if (image_buffer == NULL) { + /* create cairo surface */ + unsigned int page_width = 0; + unsigned int page_height = 0; + + if (page->document->rotate == 0 || page->document->rotate == 180) { + page_width = page->width * zathura->document->scale; + page_height = page->height * zathura->document->scale; + } else { + page_width = page->height * zathura->document->scale; + page_height = page->width * zathura->document->scale; + } + + cairo_surface_t* surface = cairo_image_surface_create(CAIRO_FORMAT_RGB24, page_width, page_height); + + if (surface == NULL) { g_static_mutex_unlock(&(page->lock)); gdk_threads_leave(); return false; } - /* create cairo surface */ - unsigned int page_width = page->width * zathura->document->scale; - unsigned int page_height = page->height * zathura->document->scale; + cairo_t* cairo = cairo_create(surface); - cairo_surface_t* surface = cairo_image_surface_create(CAIRO_FORMAT_RGB24, page_width, page_height); + if (cairo == NULL) { + cairo_surface_destroy(surface); + g_static_mutex_unlock(&(page->lock)); + gdk_threads_leave(); + return false; + } + + cairo_save(cairo); + cairo_set_source_rgb(cairo, 1, 1, 1); + cairo_rectangle(cairo, 0, 0, page_width, page_height); + cairo_fill(cairo); + cairo_restore(cairo); + cairo_save(cairo); + + switch(page->document->rotate) { + case 90: + cairo_translate(cairo, page_width, 0); + break; + case 180: + cairo_translate(cairo, page_width, page_height); + break; + case 270: + cairo_translate(cairo, 0, page_height); + break; + } + + if (page->document->rotate != 0) { + cairo_rotate(cairo, page->document->rotate * G_PI / 180.0); + } + + if (zathura_page_render(page, cairo) == false) { + cairo_destroy(cairo); + cairo_surface_destroy(surface); + g_static_mutex_unlock(&(page->lock)); + gdk_threads_leave(); + return false; + } + + cairo_restore(cairo); + cairo_destroy(cairo); int rowstride = cairo_image_surface_get_stride(surface); unsigned char* image = cairo_image_surface_get_data(surface); - for (unsigned int y = 0; y < page_height; y++) { - unsigned char* dst = image + y * rowstride; - unsigned char* src = image_buffer->data + y * image_buffer->rowstride; - - for (unsigned int x = 0; x < page_width; x++) { - dst[0] = src[2]; - dst[1] = src[1]; - dst[2] = src[0]; - src += 3; - dst += 4; - } - } - /* recolor */ if (zathura->global.recolor) { /* recolor code based on qimageblitz library flatten() function @@ -214,49 +251,14 @@ render(zathura_t* zathura, zathura_page_t* page) } } - /* rotate */ - unsigned int width = page_width; - unsigned int height = page_height; - if (page->document->rotate == 90 || page->document->rotate == 270) { - width = page_height; - height = page_width; - } - - cairo_surface_t* final_surface = cairo_image_surface_create(CAIRO_FORMAT_RGB24, width, height); - cairo_t* cairo = cairo_create(final_surface); - - switch(page->document->rotate) - { - case 90: - cairo_translate(cairo, width, 0); - break; - case 180: - cairo_translate(cairo, width, height); - break; - case 270: - cairo_translate(cairo, 0, height); - break; - } - - if (page->document->rotate != 0) { - cairo_rotate(cairo, page->document->rotate * G_PI / 180.0); - } - - cairo_set_source_surface(cairo, surface, 0, 0); - cairo_set_operator(cairo, CAIRO_OPERATOR_SOURCE); - cairo_paint(cairo); - cairo_destroy(cairo); - cairo_surface_destroy(surface); - /* draw to gtk widget */ - page->surface = final_surface; - gtk_widget_set_size_request(page->drawing_area, width, height); + page->surface = surface; + gtk_widget_set_size_request(page->drawing_area, page_width, page_height); gtk_widget_queue_draw(page->drawing_area); - zathura_image_buffer_free(image_buffer); g_static_mutex_unlock(&(page->lock)); - gdk_threads_leave(); + return true; } diff --git a/render.h b/render.h index 73f2a27..a8628cf 100644 --- a/render.h +++ b/render.h @@ -16,7 +16,7 @@ struct render_thread_s GThread* thread; /**> The thread object */ GMutex* lock; /**> Lock */ GCond* cond; /**> Condition */ - zathura_t* zathura; /**> Zathura object */ + zathura_t* zathura; /**> Zathura object */ }; /** From ede8a7b236958d002d96a955c4ccd09055f0da66 Mon Sep 17 00:00:00 2001 From: Sebastian Ramacher Date: Thu, 29 Sep 2011 13:18:09 +0200 Subject: [PATCH 4/5] Change NEED_DL to DL_LIB to be consistent with other options. Run you don't need do link against dl set DL_LIB to a empty value. --- Makefile | 4 ---- config.mk | 6 +++--- 2 files changed, 3 insertions(+), 7 deletions(-) diff --git a/Makefile b/Makefile index a9d8902..649c8bc 100644 --- a/Makefile +++ b/Makefile @@ -8,10 +8,6 @@ SOURCE = $(shell find . -iname "*.c") OBJECTS = $(patsubst %.c, %.o, $(SOURCE)) DOBJECTS = $(patsubst %.c, %.do, $(SOURCE)) -ifneq "$(NEEDS_DL)" "0" -LIBS += -ldl -endif - all: options ${PROJECT} options: diff --git a/config.mk b/config.mk index 3363edc..7a4d602 100644 --- a/config.mk +++ b/config.mk @@ -10,8 +10,6 @@ MANPREFIX ?= ${PREFIX}/share/man # libs # set this to 0 if you don't need to link against dl -NEEDS_DL ?= 1 - GTK_INC ?= $(shell pkg-config --cflags gtk+-2.0) GTK_LIB ?= $(shell pkg-config --libs gtk+-2.0 gthread-2.0) @@ -21,8 +19,10 @@ GIRARA_LIB ?= $(shell pkg-config --libs girara-gtk2) SQLITE_INC ?= $(shell pkg-config --cflags sqlite3) SQLITE_LIB ?= $(shell pkg-config --libs sqlite3) +DL_LIB ?= -ldl + INCS = ${GIRARA_INC} ${GTK_INC} $(SQLITE_INC) -LIBS = ${GIRARA_LIB} ${GTK_LIB} $(SQLITE_LIB) -lpthread -lm +LIBS = ${GIRARA_LIB} ${GTK_LIB} $(SQLITE_LIB) $(DL_LIB) -lpthread -lm # flags CFLAGS += -std=c99 -pedantic -Wall -Wno-format-zero-length -Wextra $(INCS) From b34d21afaf0151ddc2cf1dfc696454f3f08eb613 Mon Sep 17 00:00:00 2001 From: Sebastian Ramacher Date: Thu, 29 Sep 2011 15:23:13 +0200 Subject: [PATCH 5/5] implement content type support --- bookmarks.c | 3 +- callbacks.c | 2 +- config.mk | 2 +- database-sqlite.c | 4 +- document.c | 166 +++++++++++++++++++++++++--------------------- document.h | 30 +++++++-- print.c | 2 +- utils.c | 25 +++---- zathura.c | 6 +- zathura.h | 1 + 10 files changed, 139 insertions(+), 102 deletions(-) diff --git a/bookmarks.c b/bookmarks.c index c4c660c..3df9487 100644 --- a/bookmarks.c +++ b/bookmarks.c @@ -27,7 +27,7 @@ zathura_bookmark_add(zathura_t* zathura, const gchar* id, unsigned int page) girara_warning("Failed to add bookmark to database."); } } - + return bookmark; } @@ -94,4 +94,3 @@ zathura_bookmarks_load(zathura_t* zathura, const gchar* file) { return true; } - diff --git a/callbacks.c b/callbacks.c index aedd9b5..688a866 100644 --- a/callbacks.c +++ b/callbacks.c @@ -72,7 +72,7 @@ cb_view_vadjustment_value_changed(GtkAdjustment *adjustment, gpointer data) cairo_surface_destroy(page->surface); page->surface = NULL; } - + free(offset); } } diff --git a/config.mk b/config.mk index 7a4d602..1edeeec 100644 --- a/config.mk +++ b/config.mk @@ -28,7 +28,7 @@ LIBS = ${GIRARA_LIB} ${GTK_LIB} $(SQLITE_LIB) $(DL_LIB) -lpthread -lm CFLAGS += -std=c99 -pedantic -Wall -Wno-format-zero-length -Wextra $(INCS) # debug -DFLAGS = -g +DFLAGS ?= -g # ld LDFLAGS += -rdynamic diff --git a/database-sqlite.c b/database-sqlite.c index bdb9d72..1cad534 100644 --- a/database-sqlite.c +++ b/database-sqlite.c @@ -15,7 +15,7 @@ zathura_database_t* zathura_db_init(const char* path) { zathura_database_t* db = g_malloc0(sizeof(zathura_database_t)); - + /* create bookmarks database */ static const char SQL_BOOKMARK_INIT[] = "CREATE TABLE IF NOT EXISTS bookmarks (" @@ -167,7 +167,7 @@ zathura_db_load_bookmarks(zathura_database_t* db, const char* file) zathura_bookmark_t* bookmark = g_malloc0(sizeof(zathura_bookmark_t)); bookmark->id = g_strdup((const char*) sqlite3_column_text(stmt, 0)); bookmark->page = sqlite3_column_int(stmt, 1); - + girara_list_append(result, bookmark); } sqlite3_finalize(stmt); diff --git a/document.c b/document.c index 3e39d5a..b7955fe 100644 --- a/document.c +++ b/document.c @@ -94,8 +94,9 @@ zathura_document_plugins_load(zathura_t* zathura) break; } - plugin->file_extension = NULL; plugin->open_function = NULL; + plugin->content_types = girara_list_new(); + girara_list_set_free_function(plugin->content_types, g_free); register_plugin(plugin); @@ -121,67 +122,55 @@ zathura_document_plugins_load(zathura_t* zathura) } void -zathura_document_plugins_free(zathura_t* zathura) +zathura_document_plugin_free(zathura_document_plugin_t* plugin) { - if (zathura == NULL) { + if (plugin == NULL) { return; } - 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); - } while (girara_list_iterator_next(iter)); - girara_list_iterator_free(iter); + girara_list_free(plugin->content_types); + free(plugin); } bool 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) + if( (new_plugin == NULL) || (new_plugin->content_types == NULL) || (new_plugin->open_function == NULL) || (handle == NULL) ) { girara_error("plugin: could not register\n"); return false; } - /* search existing plugins */ - 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) == 0) { - 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); - } + 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); + } + atleastone = true; + GIRARA_LIST_FOREACH_END(new_plugin->content_types, gchar*, iter, type) - girara_list_append(zathura->plugins.plugins, new_plugin); - return true; + if (atleastone) { + girara_list_append(zathura->plugins.plugins, new_plugin); + } + return atleastone; } zathura_document_t* zathura_document_open(zathura_t* zathura, const char* path, const char* password) { if (path == NULL) { - goto error_out; + return NULL; } if (file_exists(path) == false) { girara_error("File does not exist"); - goto error_out; + return NULL; } - const char* file_extension = file_get_extension(path); - if (file_extension == NULL) { + const gchar* content_type = g_content_type_guess(path, NULL, 0, NULL); + if (content_type == NULL) { girara_error("Could not determine file type"); - goto error_out; + return NULL; } /* determine real path */ @@ -199,11 +188,28 @@ zathura_document_open(zathura_t* zathura, const char* path, const char* password real_path = malloc(sizeof(char) * path_max); if (real_path == NULL) { - goto error_out; + g_free((void*)content_type); + return NULL; } if (realpath(path, real_path) == NULL) { - goto error_free; + g_free((void*)content_type); + free(real_path); + return NULL; + } + + zathura_document_plugin_t* plugin = NULL; + GIRARA_LIST_FOREACH(zathura->plugins.type_plugin_mapping, zathura_type_plugin_mapping_t*, iter, mapping) + if (g_content_type_equals(content_type, mapping->type)) { + plugin = mapping->plugin; + break; + } + GIRARA_LIST_FOREACH_END(zathura->plugins.type_plugin_mapping, zathura_type_plugin_mapping_t*, iter, mapping) + g_free((void*)content_type); + + if (plugin == NULL) { + girara_error("unknown file type\n"); + free(real_path); } document = g_malloc0(sizeof(zathura_document_t)); @@ -216,46 +222,31 @@ zathura_document_open(zathura_t* zathura, const char* path, const char* password document->scale = 1.0; document->zathura = zathura; - girara_list_iterator_t* iter = girara_list_iterator(zathura->plugins.plugins); - if (iter == NULL) { - goto error_free; - } + if (plugin->open_function != NULL) { + if (plugin->open_function(document) == true) { + /* update statusbar */ + girara_statusbar_item_set_text(zathura->ui.session, zathura->ui.statusbar.file, real_path); - do { - zathura_document_plugin_t* plugin = (zathura_document_plugin_t*) girara_list_iterator_data(iter); - if (strcmp(file_extension, plugin->file_extension) == 0) { - girara_list_iterator_free(iter); - if (plugin->open_function != NULL) { - if (plugin->open_function(document) == true) { - /* update statusbar */ - 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*)); + if (document->pages == NULL) { + goto error_free; + } - /* read all pages */ - document->pages = calloc(document->number_of_pages, sizeof(zathura_page_t*)); - if (document->pages == NULL) { - goto error_free; - } - - for (unsigned int page_id = 0; page_id < document->number_of_pages; page_id++) { - zathura_page_t* page = zathura_page_get(document, page_id); - if (page == NULL) { - goto error_free; - } - - document->pages[page_id] = page; - } - - return document; - } else { - girara_error("could not open file\n"); + for (unsigned int page_id = 0; page_id < document->number_of_pages; page_id++) { + zathura_page_t* page = zathura_page_get(document, page_id); + if (page == NULL) { goto error_free; } - } - } - } while (girara_list_iterator_next(iter)); - girara_list_iterator_free(iter); - girara_error("unknown file type\n"); + document->pages[page_id] = page; + } + + return document; + } + } + + girara_error("could not open file\n"); error_free: @@ -270,9 +261,6 @@ error_free: } g_free(document); - -error_out: - return NULL; } @@ -553,3 +541,33 @@ zathura_image_buffer_free(zathura_image_buffer_t* image_buffer) free(image_buffer->data); free(image_buffer); } + +bool +zathura_type_plugin_mapping_new(zathura_t* zathura, const gchar* type, zathura_document_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); +} diff --git a/document.h b/document.h index 557ba1c..181199f 100644 --- a/document.h +++ b/document.h @@ -22,11 +22,17 @@ typedef bool (*zathura_document_open_t)(zathura_document_t* document); */ typedef struct zathura_document_plugin_s { - char* file_extension; /**> File extension */ + girara_list_t* content_types; /**> List of supported content types */ zathura_document_open_t open_function; /**> Document open function */ void* handle; /**> DLL handle */ } zathura_document_plugin_t; +typedef struct zathura_type_plugin_mapping_s +{ + const gchar* type; + zathura_document_plugin_t* plugin; +} zathura_type_plugin_mapping_t; + /** * Function prototype that is called to register a document plugin * @@ -239,11 +245,11 @@ struct zathura_document_s void zathura_document_plugins_load(zathura_t* zathura); /** - * Free all document plugins + * Free a document plugin * - * @param zathura the zathura session + * @param plugin The plugin */ -void zathura_document_plugins_free(zathura_t* zathura); +void zathura_document_plugin_free(zathura_document_plugin_t* plugin); /** * Register document plugin @@ -383,4 +389,20 @@ zathura_index_element_t* zathura_index_element_new(const char* title); */ void zathura_index_element_free(zathura_index_element_t* index); +/** + * 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_document_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 // DOCUMENT_H diff --git a/print.c b/print.c index bf57f5b..4d84982 100644 --- a/print.c +++ b/print.c @@ -17,7 +17,7 @@ print(zathura_t* zathura) if (zathura->print.page_setup != NULL) { gtk_print_operation_set_default_page_setup(print_operation, zathura->print.page_setup); } - + gtk_print_operation_set_allow_async(print_operation, TRUE); gtk_print_operation_set_n_pages(print_operation, zathura->document->number_of_pages); gtk_print_operation_set_current_page(print_operation, zathura->document->current_page_number); diff --git a/utils.c b/utils.c index 45bd735..53de7d2 100644 --- a/utils.c +++ b/utils.c @@ -55,26 +55,21 @@ file_valid_extension(zathura_t* zathura, const char* path) return false; } - const char* file_extension = file_get_extension(path); - - if (file_extension == NULL) { + const gchar* content_type = g_content_type_guess(path, NULL, 0, NULL); + if (content_type == NULL) { return false; } - girara_list_iterator_t* iter = girara_list_iterator(zathura->plugins.plugins); - if (iter == NULL) { - return false; - } - - do { - zathura_document_plugin_t* plugin = (zathura_document_plugin_t*) girara_list_iterator_data(iter); - if (!strcmp(file_extension, plugin->file_extension)) { - return true; + bool result = false; + GIRARA_LIST_FOREACH(zathura->plugins.type_plugin_mapping, zathura_type_plugin_mapping_t*, iter, mapping) + if (g_content_type_equals(content_type, mapping->type)) { + result = true; + break; } - } while (girara_list_iterator_next(iter)); - girara_list_iterator_free(iter); + GIRARA_LIST_FOREACH_END(zathura->plugins.type_plugin_mapping, zathura_type_plugin_mapping_t*, iter, mapping) - return false; + g_free(content_type); + return result; } bool diff --git a/zathura.c b/zathura.c index 6a8a75b..287b9fb 100644 --- a/zathura.c +++ b/zathura.c @@ -30,7 +30,7 @@ zathura_init(int argc, char* argv[]) /* parse command line options */ GdkNativeWindow embed = 0; gchar* config_dir = NULL, *data_dir = NULL, *plugin_path = NULL; - GOptionEntry entries[] = + GOptionEntry entries[] = { { "reparent", 'e', 0, G_OPTION_ARG_INT, &embed, "Reparents to window specified by xid", "xid" }, { "config-dir", 'c', 0, G_OPTION_ARG_FILENAME, &config_dir, "Path to the config directory", "path" }, @@ -63,8 +63,11 @@ zathura_init(int argc, char* argv[]) /* plugins */ zathura->plugins.plugins = girara_list_new(); + girara_list_set_free_function(zathura->plugins.plugins, zathura_document_plugin_free); zathura->plugins.path = girara_list_new(); girara_list_set_free_function(zathura->plugins.path, g_free); + zathura->plugins.type_plugin_mapping = girara_list_new(); + girara_list_set_free_function(zathura->plugins.type_plugin_mapping, zathura_type_plugin_mapping_free); if (config_dir) { zathura->config.config_dir = g_strdup(config_dir); @@ -263,7 +266,6 @@ zathura_free(zathura_t* zathura) } /* free registered plugins */ - zathura_document_plugins_free(zathura); girara_list_free(zathura->plugins.plugins); girara_list_free(zathura->plugins.path); diff --git a/zathura.h b/zathura.h index 7035542..163d094 100644 --- a/zathura.h +++ b/zathura.h @@ -69,6 +69,7 @@ typedef struct zathura_s { girara_list_t* plugins; /**> List of plugins */ girara_list_t* path; /**> List of plugin paths */ + girara_list_t* type_plugin_mapping; /**> List of type -> plugin mappings */ } plugins; struct