diff --git a/README b/README index c6c966a..d057887 100644 --- a/README +++ b/README @@ -6,6 +6,7 @@ girara user interface library and several document libraries. Requirements ------------ gtk2 (>= 2.28) +glib (>= 2.28) girara sqlite3 (optional, >= 3.5.9) check (for tests) diff --git a/callbacks.c b/callbacks.c index 992ff23..414bcb7 100644 --- a/callbacks.c +++ b/callbacks.c @@ -90,13 +90,14 @@ cb_view_vadjustment_value_changed(GtkAdjustment* GIRARA_UNUSED(adjustment), gpoi .height = zathura_page_get_height(page) * scale }; GtkWidget* page_widget = zathura_page_get_widget(zathura, page); + ZathuraPage* zathura_page_widget = ZATHURA_PAGE(page_widget); gtk_widget_translate_coordinates(page_widget, zathura->ui.session->gtk.view, 0, 0, &page_rect.x, &page_rect.y); if (gdk_rectangle_intersect(&view_rect, &page_rect, NULL) == TRUE) { if (zathura_page_get_visibility(page) == false) { zathura_page_set_visibility(page, true); - zathura_page_widget_update_view_time(ZATHURA_PAGE(page_widget)); + zathura_page_widget_update_view_time(zathura_page_widget); zathura_page_cache_add(zathura, zathura_page_get_index(page)); } if (zathura->global.update_page_number == true && updated == false @@ -106,11 +107,13 @@ cb_view_vadjustment_value_changed(GtkAdjustment* GIRARA_UNUSED(adjustment), gpoi } } else { zathura_page_set_visibility(page, false); + /* If a page becomes invisible, abort the render request. */ + zathura_page_widget_abort_render_request(zathura_page_widget); /* if the page is not visible and not cached, but still has a surface, we * need to get rid of the surface */ - if (zathura_page_widget_have_surface(ZATHURA_PAGE(page_widget)) == true && + if (zathura_page_widget_have_surface(zathura_page_widget) == true && zathura_page_cache_is_cached(zathura, zathura_page_get_index(page)) == false) { - zathura_page_widget_update_surface(ZATHURA_PAGE(page_widget), NULL); + zathura_page_widget_update_surface(zathura_page_widget, NULL); } girara_list_t* results = NULL; @@ -367,6 +370,13 @@ cb_sc_display_link(GtkEntry* entry, girara_session_t* session) return handle_link(entry, session, ZATHURA_LINK_ACTION_DISPLAY); } +static gboolean +file_monitor_reload(void* data) +{ + sc_reload((girara_session_t*) data, NULL, NULL, 0); + return FALSE; +} + void cb_file_monitor(GFileMonitor* monitor, GFile* file, GFile* UNUSED(other_file), GFileMonitorEvent event, girara_session_t* session) { @@ -377,9 +387,7 @@ cb_file_monitor(GFileMonitor* monitor, GFile* file, GFile* UNUSED(other_file), G switch (event) { case G_FILE_MONITOR_EVENT_CHANGES_DONE_HINT: case G_FILE_MONITOR_EVENT_CREATED: - gdk_threads_enter(); - sc_reload(session, NULL, NULL, 0); - gdk_threads_leave(); + g_main_context_invoke(NULL, file_monitor_reload, session); break; default: return; @@ -488,11 +496,11 @@ cb_setting_recolor_change(girara_session_t* session, const char* name, g_return_if_fail(name != NULL); zathura_t* zathura = session->global.data; - bool bool_value = *((bool*) value); + const bool bool_value = *((bool*) value); - if (zathura->global.recolor != bool_value) { - zathura->global.recolor = bool_value; - render_all(zathura); + if (zathura->sync.render_thread != NULL && zathura_renderer_recolor_enabled(zathura->sync.render_thread) != bool_value) { + zathura_renderer_enable_recolor(zathura->sync.render_thread, bool_value); + render_all(zathura); } } @@ -506,11 +514,11 @@ cb_setting_recolor_keep_hue_change(girara_session_t* session, const char* name, g_return_if_fail(name != NULL); zathura_t* zathura = session->global.data; - bool bool_value = *((bool*) value); + const bool bool_value = *((bool*) value); - if (zathura->global.recolor_keep_hue != bool_value) { - zathura->global.recolor_keep_hue = bool_value; - render_all(zathura); + if (zathura->sync.render_thread != NULL && zathura_renderer_recolor_hue_enabled(zathura->sync.render_thread) != bool_value) { + zathura_renderer_enable_recolor_hue(zathura->sync.render_thread, bool_value); + render_all(zathura); } } @@ -529,7 +537,8 @@ cb_unknown_command(girara_session_t* session, const char* input) } /* check for number */ - for (unsigned int i = 0; i < strlen(input); i++) { + const size_t size = strlen(input); + for (size_t i = 0; i < size; i++) { if (g_ascii_isdigit(input[i]) == FALSE) { return false; } diff --git a/commands.c b/commands.c index 76e2a27..b12c979 100644 --- a/commands.c +++ b/commands.c @@ -381,9 +381,9 @@ cmd_search(girara_session_t* session, const char* input, girara_argument_t* argu GtkWidget* page_widget = zathura_page_get_widget(zathura, page); g_object_set(page_widget, "draw-links", FALSE, NULL); - render_lock(zathura->sync.render_thread); + zathura_renderer_lock(zathura->sync.render_thread); girara_list_t* result = zathura_page_search_text(page, input, &error); - render_unlock(zathura->sync.render_thread); + zathura_renderer_unlock(zathura->sync.render_thread); if (result == NULL || girara_list_size(result) == 0) { girara_list_free(result); diff --git a/config.c b/config.c index ecb8075..e14d7e2 100644 --- a/config.c +++ b/config.c @@ -57,9 +57,13 @@ cb_color_change(girara_session_t* session, const char* name, } else if (g_strcmp0(name, "highlight-active-color") == 0) { gdk_color_parse(string_value, &(zathura->ui.colors.highlight_color_active)); } else if (g_strcmp0(name, "recolor-darkcolor") == 0) { - gdk_color_parse(string_value, &(zathura->ui.colors.recolor_dark_color)); + if (zathura->sync.render_thread != NULL) { + zathura_renderer_set_recolor_colors_str(zathura->sync.render_thread, NULL, string_value); + } } else if (g_strcmp0(name, "recolor-lightcolor") == 0) { - gdk_color_parse(string_value, &(zathura->ui.colors.recolor_light_color)); + if (zathura->sync.render_thread != NULL) { + zathura_renderer_set_recolor_colors_str(zathura->sync.render_thread, string_value, NULL); + } } else if (g_strcmp0(name, "render-loading-bg") == 0) { gdk_color_parse(string_value, &(zathura->ui.colors.render_loading_bg)); } else if (g_strcmp0(name, "render-loading-fg") == 0) { @@ -169,10 +173,8 @@ config_load_default(zathura_t* zathura) int_value = 2000; girara_setting_add(gsession, "jumplist-size", &int_value, INT, false, _("Number of positions to remember in the jumplist"), cb_jumplist_change, NULL); - girara_setting_add(gsession, "recolor-darkcolor", NULL, STRING, false, _("Recoloring (dark color)"), cb_color_change, NULL); - girara_setting_set(gsession, "recolor-darkcolor", "#FFFFFF"); - girara_setting_add(gsession, "recolor-lightcolor", NULL, STRING, false, _("Recoloring (light color)"), cb_color_change, NULL); - girara_setting_set(gsession, "recolor-lightcolor", "#000000"); + girara_setting_add(gsession, "recolor-darkcolor", "#FFFFFF", STRING, false, _("Recoloring (dark color)"), cb_color_change, NULL); + girara_setting_add(gsession, "recolor-lightcolor", "#000000", STRING, false, _("Recoloring (light color)"), cb_color_change, NULL); girara_setting_add(gsession, "highlight-color", NULL, STRING, false, _("Color for highlighting"), cb_color_change, NULL); girara_setting_set(gsession, "highlight-color", "#9FBC00"); girara_setting_add(gsession, "highlight-active-color", NULL, STRING, false, _("Color for highlighting (active)"), cb_color_change, NULL); diff --git a/config.mk b/config.mk index 30a0285..07b9ab6 100644 --- a/config.mk +++ b/config.mk @@ -52,6 +52,9 @@ GTHREAD_LIB ?= $(shell pkg-config --libs gthread-2.0) GMODULE_INC ?= $(shell pkg-config --cflags gmodule-no-export-2.0) GMODULE_LIB ?= $(shell pkg-config --libs gmodule-no-export-2.0) +GLIB_INC ?= $(shell pkg-config --cflags --atleast-version=2.28 glib-2.0) +GLIB_LIB ?= $(shell pkg-config --libs --atleast-version=2.28 glib-2.0) + GIRARA_INC ?= $(shell pkg-config --cflags girara-gtk${ZATHURA_GTK_VERSION}) GIRARA_LIB ?= $(shell pkg-config --libs girara-gtk${ZATHURA_GTK_VERSION}) @@ -65,8 +68,8 @@ MAGIC_INC ?= MAGIC_LIB ?= -lmagic endif -INCS = ${GIRARA_INC} ${GTK_INC} ${GTHREAD_INC} ${GMODULE_INC} -LIBS = ${GIRARA_LIB} ${GTK_LIB} ${GTHREAD_LIB} ${GMODULE_LIB} -lpthread -lm +INCS = ${GIRARA_INC} ${GTK_INC} ${GTHREAD_INC} ${GMODULE_INC} ${GLIB_INC} +LIBS = ${GIRARA_LIB} ${GTK_LIB} ${GTHREAD_LIB} ${GMODULE_LIB} ${GLIB_LIB} -lpthread -lm # flags CFLAGS += -std=c99 -pedantic -Wall -Wno-format-zero-length -Wextra $(INCS) diff --git a/main.c b/main.c index 26d561a..692aa67 100644 --- a/main.c +++ b/main.c @@ -25,7 +25,9 @@ main(int argc, char* argv[]) #if !GLIB_CHECK_VERSION(2, 31, 0) g_thread_init(NULL); #endif +#if !GTK_CHECK_VERSION(3, 6, 0) gdk_threads_init(); +#endif gtk_init(&argc, &argv); /* create zathura session */ @@ -46,7 +48,7 @@ main(int argc, char* argv[]) bool synctex = false; int page_number = ZATHURA_PAGE_NUMBER_UNSPECIFIED; -#if (GTK_MAJOR_VERSION == 3) +#if GTK_CHECK_VERSION(3, 0, 0) Window embed = 0; #else GdkNativeWindow embed = 0; @@ -148,9 +150,13 @@ main(int argc, char* argv[]) } /* run zathura */ +#if !GTK_CHECK_VERSION(3, 6, 0) gdk_threads_enter(); +#endif gtk_main(); +#if !GTK_CHECK_VERSION(3, 6, 0) gdk_threads_leave(); +#endif /* free zathura */ zathura_free(zathura); diff --git a/page-widget.c b/page-widget.c index 5f0cf26..094af15 100644 --- a/page-widget.c +++ b/page-widget.c @@ -15,6 +15,7 @@ #include "utils.h" #include "shortcuts.h" #include "synctex.h" +#include "zathura.h" G_DEFINE_TYPE(ZathuraPage, zathura_page_widget, GTK_TYPE_DRAWING_AREA) @@ -22,7 +23,7 @@ typedef struct zathura_page_widget_private_s { zathura_page_t* page; /**< Page object */ zathura_t* zathura; /**< Zathura object */ cairo_surface_t* surface; /**< Cairo surface */ - bool render_requested; /**< No surface and rendering has been requested */ + ZathuraRenderRequest* render_request; /* Request object */ gint64 last_view; /**< Last time the page has been viewed */ mutex lock; /**< Lock */ @@ -75,6 +76,7 @@ static gboolean cb_zathura_page_widget_motion_notify(GtkWidget* widget, GdkEvent static gboolean cb_zathura_page_widget_popup_menu(GtkWidget* widget); static void cb_menu_image_copy(GtkMenuItem* item, ZathuraPage* page); static void cb_menu_image_save(GtkMenuItem* item, ZathuraPage* page); +static void cb_update_surface(ZathuraRenderRequest* request, cairo_surface_t* surface, void* data); enum properties_e { PROP_0, @@ -174,7 +176,7 @@ zathura_page_widget_init(ZathuraPage* widget) zathura_page_widget_private_t* priv = ZATHURA_PAGE_GET_PRIVATE(widget); priv->page = NULL; priv->surface = NULL; - priv->render_requested = false; + priv->render_request = NULL; priv->last_view = g_get_real_time(); priv->links.list = NULL; @@ -208,7 +210,18 @@ zathura_page_widget_new(zathura_t* zathura, zathura_page_t* page) { g_return_val_if_fail(page != NULL, NULL); - return g_object_new(ZATHURA_TYPE_PAGE, "page", page, "zathura", zathura, NULL); + GObject* ret = g_object_new(ZATHURA_TYPE_PAGE, "page", page, "zathura", zathura, NULL); + if (ret == NULL) { + return NULL; + } + + ZathuraPage* widget = ZATHURA_PAGE(ret); + zathura_page_widget_private_t* priv = ZATHURA_PAGE_GET_PRIVATE(widget); + priv->render_request = zathura_render_request_new(zathura->sync.render_thread, page); + g_signal_connect_object(priv->render_request, "completed", + G_CALLBACK(cb_update_surface), widget, 0); + + return GTK_WIDGET(ret); } static void @@ -221,6 +234,10 @@ zathura_page_widget_finalize(GObject* object) cairo_surface_destroy(priv->surface); } + if (priv->render_request != NULL) { + g_object_unref(priv->render_request); + } + if (priv->search.list != NULL) { girara_list_free(priv->search.list); } @@ -311,7 +328,7 @@ zathura_page_widget_set_property(GObject* object, guint prop_id, const GValue* v */ if (priv->search.list != NULL && zathura_page_get_visibility(priv->page)) { - gtk_widget_queue_draw(GTK_WIDGET(object)); + gtk_widget_queue_draw(GTK_WIDGET(object)); } break; default: @@ -479,8 +496,9 @@ zathura_page_widget_draw(GtkWidget* widget, cairo_t* cairo) } } else { /* set background color */ - if (priv->zathura->global.recolor == true) { - GdkColor color = priv->zathura->ui.colors.recolor_light_color; + if (zathura_renderer_recolor_enabled(priv->zathura->sync.render_thread) == true) { + GdkColor color; + zathura_renderer_get_recolor_colors(priv->zathura->sync.render_thread, &color, NULL); cairo_set_source_rgb(cairo, color.red/65535.0, color.green/65535.0, color.blue/65535.0); } else { GdkColor color = priv->zathura->ui.colors.render_loading_bg; @@ -494,8 +512,9 @@ zathura_page_widget_draw(GtkWidget* widget, cairo_t* cairo) /* write text */ if (render_loading == true) { - if (priv->zathura->global.recolor == true) { - GdkColor color = priv->zathura->ui.colors.recolor_dark_color; + if (zathura_renderer_recolor_enabled(priv->zathura->sync.render_thread) == true) { + GdkColor color; + zathura_renderer_get_recolor_colors(priv->zathura->sync.render_thread, NULL, &color); cairo_set_source_rgb(cairo, color.red/65535.0, color.green/65535.0, color.blue/65535.0); } else { GdkColor color = priv->zathura->ui.colors.render_loading_fg; @@ -514,10 +533,7 @@ zathura_page_widget_draw(GtkWidget* widget, cairo_t* cairo) } /* render real page */ - if (priv->render_requested == false) { - priv->render_requested = true; - render_page(priv->zathura->sync.render_thread, priv->page); - } + zathura_render_request(priv->render_request, priv->last_view); } mutex_unlock(&(priv->lock)); return FALSE; @@ -539,11 +555,10 @@ zathura_page_widget_update_surface(ZathuraPage* widget, cairo_surface_t* surface cairo_surface_destroy(priv->surface); priv->surface = NULL; } - priv->render_requested = false; if (surface != NULL) { /* if we're not visible or not cached, we don't care about the surface */ - if (zathura_page_get_visibility(priv->page) == true || - zathura_page_cache_is_cached(priv->zathura, zathura_page_get_index(priv->page)) == true) { + /*if (zathura_page_get_visibility(priv->page) == true || + zathura_page_cache_is_cached(priv->zathura, zathura_page_get_index(priv->page)) == true) */ { priv->surface = surface; cairo_surface_reference(surface); } @@ -555,6 +570,14 @@ zathura_page_widget_update_surface(ZathuraPage* widget, cairo_surface_t* surface } } +static void +cb_update_surface(ZathuraRenderRequest* UNUSED(request), cairo_surface_t* surface, void* data) +{ + ZathuraPage* widget = data; + g_return_if_fail(ZATHURA_IS_PAGE(widget)); + zathura_page_widget_update_surface(widget, surface); +} + static void zathura_page_widget_size_allocate(GtkWidget* widget, GdkRectangle* allocation) { @@ -914,3 +937,11 @@ zathura_page_widget_have_surface(ZathuraPage* widget) return priv->surface != NULL; } +void +zathura_page_widget_abort_render_request(ZathuraPage* widget) +{ + g_return_if_fail(ZATHURA_IS_PAGE(widget)); + zathura_page_widget_private_t* priv = ZATHURA_PAGE_GET_PRIVATE(widget); + zathura_render_request_abort(priv->render_request); +} + diff --git a/page-widget.h b/page-widget.h index 1a54708..5f2c796 100644 --- a/page-widget.h +++ b/page-widget.h @@ -94,4 +94,11 @@ void zathura_page_widget_update_view_time(ZathuraPage* widget); */ bool zathura_page_widget_have_surface(ZathuraPage* widget); +/** + * Abort outstanding render requests + * + * @param widget the widget + */ +void zathura_page_widget_abort_render_request(ZathuraPage* widget); + #endif diff --git a/print.c b/print.c index b32e5b3..8f0a39c 100644 --- a/print.c +++ b/print.c @@ -109,9 +109,9 @@ cb_print_draw_page(GtkPrintOperation* print_operation, GtkPrintContext* /* Try to render the page without a temporary surface. This only works with * plugins that support rendering to any surface. */ girara_debug("printing page %d ...", page_number); - render_lock(zathura->sync.render_thread); + zathura_renderer_lock(zathura->sync.render_thread); int err = zathura_page_render(page, cairo, true); - render_unlock(zathura->sync.render_thread); + zathura_renderer_unlock(zathura->sync.render_thread); if (err == ZATHURA_ERROR_OK) { return; } @@ -144,9 +144,9 @@ cb_print_draw_page(GtkPrintOperation* print_operation, GtkPrintContext* /* Render the page to the temporary surface */ girara_debug("printing page %d ...", page_number); - render_lock(zathura->sync.render_thread); + zathura_renderer_lock(zathura->sync.render_thread); err = zathura_page_render(page, temp_cairo, true); - render_unlock(zathura->sync.render_thread); + zathura_renderer_unlock(zathura->sync.render_thread); if (err != ZATHURA_ERROR_OK) { cairo_destroy(temp_cairo); cairo_surface_destroy(surface); diff --git a/render.c b/render.c index e8bcde3..050cc4f 100644 --- a/render.c +++ b/render.c @@ -3,9 +3,6 @@ #include #include #include -#include -#include - #include "glib-compat.h" #include "render.h" #include "zathura.h" @@ -14,83 +11,354 @@ #include "page-widget.h" #include "utils.h" -static void render_job(void* data, void* user_data); -static bool render(zathura_t* zathura, zathura_page_t* page); -static gint render_thread_sort(gconstpointer a, gconstpointer b, gpointer data); +/* define the two types */ +G_DEFINE_TYPE(ZathuraRenderer, zathura_renderer, G_TYPE_OBJECT) +G_DEFINE_TYPE(ZathuraRenderRequest, zathura_render_request, G_TYPE_OBJECT) -struct render_thread_s { +/* private methods for ZathuraRenderer */ +static void zathura_renderer_finalize(GObject* object); +/* private methods for ZathuraRenderRequest */ +static void zathura_render_request_finalize(GObject* object); + +static void render_job(void* data, void* user_data); +static gint render_thread_sort(gconstpointer a, gconstpointer b, gpointer data); +static void color2double(const GdkColor* col, double* v); + + +/* private data for ZathuraRenderer */ +typedef struct private_s { GThreadPool* pool; /**< Pool of threads */ mutex mutex; /**< Render lock */ - bool about_to_close; /**< Render thread is to be freed */ -}; + volatile bool about_to_close; /**< Render thread is to be freed */ + + /* recolor information */ + struct { + bool enabled; + bool hue; + + double light[3]; + GdkColor light_gdk; + double dark[3]; + GdkColor dark_gdk; + } recolor; +} private_t; + +/* private data for ZathuraRenderRequest */ +typedef struct request_private_s { + ZathuraRenderer* renderer; + zathura_page_t* page; + bool requested; + bool aborted; + gint64 last_view_time; +} request_private_t; + +#define GET_PRIVATE(obj) \ + (G_TYPE_INSTANCE_GET_PRIVATE((obj), ZATHURA_TYPE_RENDERER, private_t)) +#define REQUEST_GET_PRIVATE(obj) \ + (G_TYPE_INSTANCE_GET_PRIVATE((obj), ZATHURA_TYPE_RENDER_REQUEST, \ + request_private_t)) + +/* init, new and free for ZathuraRenderer */ static void -render_job(void* data, void* user_data) +zathura_renderer_class_init(ZathuraRendererClass* class) { - zathura_page_t* page = data; - zathura_t* zathura = user_data; - if (page == NULL || zathura == NULL) { - return; + /* add private members */ + g_type_class_add_private(class, sizeof(private_t)); + + /* overwrite methods */ + GObjectClass* object_class = G_OBJECT_CLASS(class); + object_class->finalize = zathura_renderer_finalize; +} + +static void +zathura_renderer_init(ZathuraRenderer* renderer) +{ + private_t* priv = GET_PRIVATE(renderer); + priv->pool = g_thread_pool_new(render_job, renderer, 1, TRUE, NULL); + priv->about_to_close = false; + g_thread_pool_set_sort_function(priv->pool, render_thread_sort, NULL); + mutex_init(&priv->mutex); + + priv->recolor.enabled = false; + priv->recolor.hue = true; + + zathura_renderer_set_recolor_colors_str(renderer, "#000000", "#FFFFFF"); +} + +ZathuraRenderer* +zathura_renderer_new() +{ + GObject* obj = g_object_new(ZATHURA_TYPE_RENDERER, NULL); + return ZATHURA_RENDERER(obj); +} + +static void +zathura_renderer_finalize(GObject* object) +{ + ZathuraRenderer* renderer = ZATHURA_RENDERER(object); + private_t* priv = GET_PRIVATE(renderer); + + zathura_renderer_stop(renderer); + if (priv->pool) { + g_thread_pool_free(priv->pool, TRUE, TRUE); + } + mutex_free(&(priv->mutex)); +} + +/* init, new and free for ZathuraRenderRequest */ + +enum { + REQUEST_COMPLETED, + REQUEST_LAST_SIGNAL +}; + +static guint request_signals[REQUEST_LAST_SIGNAL] = { 0 }; + +static void +zathura_render_request_class_init(ZathuraRenderRequestClass* class) +{ + /* add private members */ + g_type_class_add_private(class, sizeof(request_private_t)); + + /* overwrite methods */ + GObjectClass* object_class = G_OBJECT_CLASS(class); + object_class->finalize = zathura_render_request_finalize; + + request_signals[REQUEST_COMPLETED] = g_signal_new("completed", + ZATHURA_TYPE_RENDER_REQUEST, + G_SIGNAL_RUN_LAST, + 0, + NULL, + NULL, + g_cclosure_marshal_generic, + G_TYPE_NONE, + 1, + G_TYPE_POINTER); +} + +static void +zathura_render_request_init(ZathuraRenderRequest* request) +{ + request_private_t* priv = REQUEST_GET_PRIVATE(request); + priv->renderer = NULL; + priv->page = NULL; +} + +ZathuraRenderRequest* +zathura_render_request_new(ZathuraRenderer* renderer, zathura_page_t* page) +{ + g_return_val_if_fail(renderer != NULL && page != NULL, NULL); + + GObject* obj = g_object_new(ZATHURA_TYPE_RENDER_REQUEST, NULL); + if (obj == NULL) { + return NULL; } - girara_debug("rendering page %d ...", zathura_page_get_index(page) + 1); - if (render(zathura, page) != true) { - girara_error("Rendering failed (page %d)\n", zathura_page_get_index(page) + 1); + ZathuraRenderRequest* request = ZATHURA_RENDER_REQUEST(obj); + request_private_t* priv = REQUEST_GET_PRIVATE(request); + /* we want to make sure that renderer lives long enough */ + priv->renderer = g_object_ref(renderer); + priv->page = page; + priv->aborted = false; + priv->requested = false; + + return request; +} + +static void +zathura_render_request_finalize(GObject* object) +{ + ZathuraRenderRequest* request = ZATHURA_RENDER_REQUEST(object); + request_private_t* priv = REQUEST_GET_PRIVATE(request); + + if (priv->renderer) { + g_object_unref(priv->renderer); } } -render_thread_t* -render_init(zathura_t* zathura) +/* renderer methods */ + +bool +zathura_renderer_recolor_enabled(ZathuraRenderer* renderer) { - render_thread_t* render_thread = g_malloc0(sizeof(render_thread_t)); + g_return_val_if_fail(ZATHURA_IS_RENDERER(renderer), false); - /* setup */ - render_thread->pool = g_thread_pool_new(render_job, zathura, 1, TRUE, NULL); - if (render_thread->pool == NULL) { - goto error_free; - } - - render_thread->about_to_close = false; - g_thread_pool_set_sort_function(render_thread->pool, render_thread_sort, zathura); - mutex_init(&render_thread->mutex); - - return render_thread; - -error_free: - - render_free(render_thread); - return NULL; + return GET_PRIVATE(renderer)->recolor.enabled; } void -render_free(render_thread_t* render_thread) +zathura_renderer_enable_recolor(ZathuraRenderer* renderer, bool enable) { - if (render_thread == NULL) { - return; - } + g_return_if_fail(ZATHURA_IS_RENDERER(renderer)); - render_thread->about_to_close = true; - if (render_thread->pool) { - g_thread_pool_free(render_thread->pool, TRUE, TRUE); - } - - mutex_free(&(render_thread->mutex)); - g_free(render_thread); + GET_PRIVATE(renderer)->recolor.enabled = enable; } bool -render_page(render_thread_t* render_thread, zathura_page_t* page) +zathura_renderer_recolor_hue_enabled(ZathuraRenderer* renderer) { - if (render_thread == NULL || page == NULL || render_thread->pool == NULL || render_thread->about_to_close == true) { - return false; - } + g_return_val_if_fail(ZATHURA_IS_RENDERER(renderer), false); - g_thread_pool_push(render_thread->pool, page, NULL); - return true; + return GET_PRIVATE(renderer)->recolor.hue; +} + +void +zathura_renderer_enable_recolor_hue(ZathuraRenderer* renderer, bool enable) +{ + g_return_if_fail(ZATHURA_IS_RENDERER(renderer)); + + GET_PRIVATE(renderer)->recolor.hue = enable; +} + +void +zathura_renderer_set_recolor_colors(ZathuraRenderer* renderer, + const GdkColor* light, const GdkColor* dark) +{ + g_return_if_fail(ZATHURA_IS_RENDERER(renderer)); + + private_t* priv = GET_PRIVATE(renderer); + if (light != NULL) { + priv->recolor.light_gdk.red = light->red; + priv->recolor.light_gdk.blue = light->blue; + priv->recolor.light_gdk.green = light->green; + color2double(light, priv->recolor.light); + } + if (dark != NULL) { + priv->recolor.dark_gdk.red = dark->red; + priv->recolor.dark_gdk.blue = dark->blue; + priv->recolor.dark_gdk.green = dark->green; + color2double(dark, priv->recolor.dark); + } +} + +void +zathura_renderer_set_recolor_colors_str(ZathuraRenderer* renderer, + const char* light, const char* dark) +{ + g_return_if_fail(ZATHURA_IS_RENDERER(renderer)); + + if (dark != NULL) { + GdkColor color; + gdk_color_parse(dark, &color); + zathura_renderer_set_recolor_colors(renderer, NULL, &color); + } + if (light != NULL) { + GdkColor color; + gdk_color_parse(light, &color); + zathura_renderer_set_recolor_colors(renderer, &color, NULL); + } +} + +void +zathura_renderer_get_recolor_colors(ZathuraRenderer* renderer, + GdkColor* light, GdkColor* dark) +{ + g_return_if_fail(ZATHURA_IS_RENDERER(renderer)); + + private_t* priv = GET_PRIVATE(renderer); + if (light != NULL) { + light->red = priv->recolor.light_gdk.red; + light->blue = priv->recolor.light_gdk.blue; + light->green = priv->recolor.light_gdk.green; + } + if (dark != NULL) { + dark->red = priv->recolor.dark_gdk.red; + dark->blue = priv->recolor.dark_gdk.blue; + dark->green = priv->recolor.dark_gdk.green; + } +} + +void +zathura_renderer_lock(ZathuraRenderer* renderer) +{ + g_return_if_fail(ZATHURA_IS_RENDERER(renderer)); + + private_t* priv = GET_PRIVATE(renderer); + mutex_lock(&priv->mutex); +} + +void +zathura_renderer_unlock(ZathuraRenderer* renderer) +{ + g_return_if_fail(ZATHURA_IS_RENDERER(renderer)); + + private_t* priv = GET_PRIVATE(renderer); + mutex_unlock(&priv->mutex); +} + +void +zathura_renderer_stop(ZathuraRenderer* renderer) +{ + g_return_if_fail(ZATHURA_IS_RENDERER(renderer)); + GET_PRIVATE(renderer)->about_to_close = true; +} + + +/* ZathuraRenderRequest methods */ + +void +zathura_render_request(ZathuraRenderRequest* request, gint64 last_view_time) +{ + g_return_if_fail(ZATHURA_IS_RENDER_REQUEST(request)); + + request_private_t* request_priv = REQUEST_GET_PRIVATE(request); + private_t* priv = GET_PRIVATE(request_priv->renderer); + if (request_priv->requested == false) { + request_priv->requested = true; + request_priv->aborted = false; + request_priv->last_view_time = last_view_time; + + g_thread_pool_push(priv->pool, request, NULL); + } +} + +void +zathura_render_request_abort(ZathuraRenderRequest* request) +{ + g_return_if_fail(ZATHURA_IS_RENDER_REQUEST(request)); + + request_private_t* request_priv = REQUEST_GET_PRIVATE(request); + if (request_priv->requested == true) { + request_priv->aborted = true; + } +} + + +/* render job */ + +typedef struct emit_completed_signal_s +{ + ZathuraRenderer* renderer; + ZathuraRenderRequest* request; + cairo_surface_t* surface; +} emit_completed_signal_t; + +static gboolean +emit_completed_signal(void* data) +{ + emit_completed_signal_t* ecs = data; + private_t* priv = GET_PRIVATE(ecs->renderer); + request_private_t* request_priv = REQUEST_GET_PRIVATE(ecs->request); + + if (priv->about_to_close == false && request_priv->aborted == false) { + /* emit the signal */ + g_signal_emit(ecs->request, request_signals[REQUEST_COMPLETED], 0, ecs->surface); + } + /* mark the request as done */ + request_priv->requested = false; + + /* clean up the data */ + cairo_surface_destroy(ecs->surface); + g_object_unref(ecs->renderer); + g_object_unref(ecs->request); + g_free(ecs); + + return FALSE; } static void -color2double(GdkColor* col, double* v) +color2double(const GdkColor* col, double* v) { v[0] = (double) col->red / 65535.; v[1] = (double) col->green / 65535.; @@ -101,7 +369,7 @@ color2double(GdkColor* col, double* v) Assumes that l is in the interval l1, l2 and corrects the value to force u=0 on l1 and l2 */ static double -colorumax(double* h, double l, double l1, double l2) +colorumax(const double* h, double l, double l1, double l2) { double u, uu, v, vv, lv; if (h[0] == 0 && h[1] == 0 && h[2] == 0) { @@ -141,13 +409,92 @@ colorumax(double* h, double l, double l1, double l2) return fmin(u, v); } +static void +recolor(private_t* priv, unsigned int page_width, unsigned int page_height, + cairo_surface_t* surface) +{ + /* uses a representation of a rgb color as follows: + - a lightness scalar (between 0,1), which is a weighted average of r, g, b, + - a hue vector, which indicates a radian direction from the grey axis, inside the equal lightness plane. + - a saturation scalar between 0,1. It is 0 when grey, 1 when the color is in the boundary of the rgb cube. + */ + + const int rowstride = cairo_image_surface_get_stride(surface); + unsigned char* image = cairo_image_surface_get_data(surface); + + /* RGB weights for computing lightness. Must sum to one */ + static const double a[] = {0.30, 0.59, 0.11}; + +#define rgb1 priv->recolor.dark +#define rgb2 priv->recolor.light + const double l1 = (a[0]*rgb1[0] + a[1]*rgb1[1] + a[2]*rgb1[2]); + const double l2 = (a[0]*rgb2[0] + a[1]*rgb2[1] + a[2]*rgb2[2]); + + const double rgb_diff[] = { + rgb2[0] - rgb1[0], + rgb2[1] - rgb1[1], + rgb2[2] - rgb1[2] + }; + + for (unsigned int y = 0; y < page_height; y++) { + unsigned char* data = image + y * rowstride; + + for (unsigned int x = 0; x < page_width; x++, data += 4) { + /* Careful. data color components blue, green, red. */ + const double rgb[3] = { + (double) data[2] / 256., + (double) data[1] / 256., + (double) data[0] / 256. + }; + + /* compute h, s, l data */ + double l = a[0]*rgb[0] + a[1]*rgb[1] + a[2]*rgb[2]; + + if (priv->recolor.hue == true) { + /* adjusting lightness keeping hue of current color. white and black + * go to grays of same ligtness as light and dark colors. */ + const double h[3] = { + rgb[0] - l, + rgb[1] - l, + rgb[2] - l + }; + + /* u is the maximum possible saturation for given h and l. s is a + * rescaled saturation between 0 and 1 */ + double u = colorumax(h, l, 0, 1); + double s = 0; + if (u != 0) { + s = 1/u; + } + + /* Interpolates lightness between light and dark colors. white goes to + * light, and black goes to dark. */ + l = l * (l2 - l1) + l1; + u = colorumax(h, l, l1, l2); + + data[2] = (unsigned char)round(255.*(l + s*u * h[0])); + data[1] = (unsigned char)round(255.*(l + s*u * h[1])); + data[0] = (unsigned char)round(255.*(l + s*u * h[2])); + } else { + /* linear interpolation between dark and light with color ligtness as + * a parameter */ + data[2] = (unsigned char)round(255.*(l * rgb_diff[0] + rgb1[0])); + data[1] = (unsigned char)round(255.*(l * rgb_diff[1] + rgb1[1])); + data[0] = (unsigned char)round(255.*(l * rgb_diff[2] + rgb1[2])); + } + } + } + +#undef rgb1 +#undef rgb2 +} static bool -render(zathura_t* zathura, zathura_page_t* page) +render(ZathuraRenderRequest* request, ZathuraRenderer* renderer) { - if (zathura == NULL || page == NULL || zathura->sync.render_thread->about_to_close == true) { - return false; - } + private_t* priv = GET_PRIVATE(renderer); + request_private_t* request_priv = REQUEST_GET_PRIVATE(request); + zathura_page_t* page = request_priv->page; /* create cairo surface */ unsigned int page_width = 0; @@ -155,13 +502,11 @@ render(zathura_t* zathura, zathura_page_t* page) const double real_scale = page_calc_height_width(page, &page_height, &page_width, false); cairo_surface_t* surface = cairo_image_surface_create(CAIRO_FORMAT_RGB24, page_width, page_height); - if (surface == NULL) { return false; } cairo_t* cairo = cairo_create(surface); - if (cairo == NULL) { cairo_surface_destroy(surface); return false; @@ -178,101 +523,70 @@ render(zathura_t* zathura, zathura_page_t* page) cairo_scale(cairo, real_scale, real_scale); } - render_lock(zathura->sync.render_thread); + zathura_renderer_lock(renderer); if (zathura_page_render(page, cairo, false) != ZATHURA_ERROR_OK) { - render_unlock(zathura->sync.render_thread); + zathura_renderer_unlock(renderer); cairo_destroy(cairo); cairo_surface_destroy(surface); return false; } - render_unlock(zathura->sync.render_thread); + zathura_renderer_unlock(renderer); cairo_restore(cairo); cairo_destroy(cairo); - const int rowstride = cairo_image_surface_get_stride(surface); - unsigned char* image = cairo_image_surface_get_data(surface); + /* before recoloring, check if we've been aborted */ + if (priv->about_to_close == true || request_priv->aborted == true) { + request_priv->requested = false; + cairo_surface_destroy(surface); + return true; + } /* recolor */ - /* uses a representation of a rgb color as follows: - - a lightness scalar (between 0,1), which is a weighted average of r, g, b, - - a hue vector, which indicates a radian direction from the grey axis, inside the equal lightness plane. - - a saturation scalar between 0,1. It is 0 when grey, 1 when the color is in the boundary of the rgb cube. - */ - if (zathura->global.recolor == true) { - /* RGB weights for computing lightness. Must sum to one */ - double a[] = {0.30, 0.59, 0.11}; - - double l1, l2, l, s, u, t; - double h[3]; - double rgb1[3], rgb2[3], rgb[3]; - - color2double(&zathura->ui.colors.recolor_dark_color, rgb1); - color2double(&zathura->ui.colors.recolor_light_color, rgb2); - - l1 = (a[0]*rgb1[0] + a[1]*rgb1[1] + a[2]*rgb1[2]); - l2 = (a[0]*rgb2[0] + a[1]*rgb2[1] + a[2]*rgb2[2]); - - for (unsigned int y = 0; y < page_height; y++) { - unsigned char* data = image + y * rowstride; - - for (unsigned int x = 0; x < page_width; x++) { - /* Careful. data color components blue, green, red. */ - rgb[0] = (double) data[2] / 256.; - rgb[1] = (double) data[1] / 256.; - rgb[2] = (double) data[0] / 256.; - - /* compute h, s, l data */ - l = a[0]*rgb[0] + a[1]*rgb[1] + a[2]*rgb[2]; - - h[0] = rgb[0] - l; - h[1] = rgb[1] - l; - h[2] = rgb[2] - l; - - /* u is the maximum possible saturation for given h and l. s is a rescaled saturation between 0 and 1 */ - u = colorumax(h, l, 0, 1); - if (u == 0) { - s = 0; - } else { - s = 1/u; - } - - /* Interpolates lightness between light and dark colors. white goes to light, and black goes to dark. */ - t = l; - l = t * (l2 - l1) + l1; - - if (zathura->global.recolor_keep_hue == true) { - /* adjusting lightness keeping hue of current color. white and black go to grays of same ligtness - as light and dark colors. */ - u = colorumax(h, l, l1, l2); - data[2] = (unsigned char)round(255.*(l + s*u * h[0])); - data[1] = (unsigned char)round(255.*(l + s*u * h[1])); - data[0] = (unsigned char)round(255.*(l + s*u * h[2])); - } else { - /* Linear interpolation between dark and light with color ligtness as a parameter */ - data[2] = (unsigned char)round(255.*(t * (rgb2[0] - rgb1[0]) + rgb1[0])); - data[1] = (unsigned char)round(255.*(t * (rgb2[1] - rgb1[1]) + rgb1[1])); - data[0] = (unsigned char)round(255.*(t * (rgb2[2] - rgb1[2]) + rgb1[2])); - } - - data += 4; - } - } + if (priv->recolor.enabled == true) { + recolor(priv, page_width, page_height, surface); } - if (zathura->sync.render_thread->about_to_close == false) { - /* update the widget */ - gdk_threads_enter(); - GtkWidget* widget = zathura_page_get_widget(zathura, page); - zathura_page_widget_update_surface(ZATHURA_PAGE(widget), surface); - gdk_threads_leave(); - } + emit_completed_signal_t* ecs = g_malloc(sizeof(ecs)); + ecs->renderer = renderer; + ecs->request = request; + ecs->surface = surface; + g_object_ref(renderer); + g_object_ref(request); + cairo_surface_reference(surface); + + /* emit signal from the main context, i.e. the main thread */ + g_main_context_invoke(NULL, emit_completed_signal, ecs); cairo_surface_destroy(surface); return true; } +static void +render_job(void* data, void* user_data) +{ + ZathuraRenderRequest* request = data; + ZathuraRenderer* renderer = user_data; + g_return_if_fail(ZATHURA_IS_RENDER_REQUEST(request)); + g_return_if_fail(ZATHURA_IS_RENDERER(renderer)); + + private_t* priv = GET_PRIVATE(renderer); + request_private_t* request_priv = REQUEST_GET_PRIVATE(request); + if (priv->about_to_close == true || request_priv->aborted == true) { + /* back out early */ + request_priv->requested = false; + return; + } + + girara_debug("Rendering page %d ...", zathura_page_get_index(request_priv->page) + 1); + if (render(request, renderer) != true) { + girara_error("Rendering failed (page %d)\n", zathura_page_get_index(request_priv->page) + 1); + request_priv->requested = false; + } +} + + void render_all(zathura_t* zathura) { @@ -294,50 +608,21 @@ render_all(zathura_t* zathura) } static gint -render_thread_sort(gconstpointer a, gconstpointer b, gpointer data) +render_thread_sort(gconstpointer a, gconstpointer b, gpointer UNUSED(data)) { - if (a == NULL || b == NULL || data == NULL) { + if (a == NULL || b == NULL) { return 0; } - zathura_page_t* page_a = (zathura_page_t*) a; - zathura_page_t* page_b = (zathura_page_t*) b; - zathura_t* zathura = (zathura_t*) data; - - unsigned int page_a_index = zathura_page_get_index(page_a); - unsigned int page_b_index = zathura_page_get_index(page_b); - - gint64 last_view_a = 0; - gint64 last_view_b = 0; - - g_object_get(zathura->pages[page_a_index], "last-view", &last_view_a, NULL); - g_object_get(zathura->pages[page_b_index], "last-view", &last_view_b, NULL); - - if (last_view_a < last_view_b) { - return -1; - } else if (last_view_a > last_view_b) { - return 1; + ZathuraRenderRequest* request_a = (ZathuraRenderRequest*) a; + ZathuraRenderRequest* request_b = (ZathuraRenderRequest*) b; + request_private_t* priv_a = REQUEST_GET_PRIVATE(request_a); + request_private_t* priv_b = REQUEST_GET_PRIVATE(request_b); + if (priv_a->aborted == priv_b->aborted) { + return priv_a->last_view_time < priv_b->last_view_time ? -1 : + (priv_a->last_view_time > priv_b->last_view_time ? 1 : 0); } - return 0; -} - -void -render_lock(render_thread_t* render_thread) -{ - if (render_thread == NULL) { - return; - } - - mutex_lock(&render_thread->mutex); -} - -void -render_unlock(render_thread_t* render_thread) -{ - if (render_thread == NULL) { - return; - } - - mutex_unlock(&render_thread->mutex); + /* sort aborted entries earlier so that the are thrown out of the queue */ + return priv_a->aborted ? 1 : -1; } diff --git a/render.h b/render.h index 2d7a12c..0963a9e 100644 --- a/render.h +++ b/render.h @@ -5,35 +5,177 @@ #include #include +#include +#include #include +#include "types.h" -#include "zathura.h" -#include "callbacks.h" +typedef struct zathura_renderer_class_s ZathuraRendererClass; + +struct zathura_renderer_s +{ + GObject parent; +}; + +struct zathura_renderer_class_s +{ + GObjectClass parent_class; +}; + +#define ZATHURA_TYPE_RENDERER \ + (zathura_renderer_get_type()) +#define ZATHURA_RENDERER(obj) \ + (G_TYPE_CHECK_INSTANCE_CAST((obj), ZATHURA_TYPE_RENDERER, ZathuraRenderer)) +#define ZATHURA_RENDERER_CLASS(obj) \ + (G_TYPE_CHECK_CLASS_CAST((obj), ZATHURA_TYPE_RENDERER, ZathuraRendererClass)) +#define ZATHURA_IS_RENDERER(obj) \ + (G_TYPE_CHECK_INSTANCE_TYPE((obj), ZATHURA_TYPE_RENDERER)) +#define ZATHURA_IS_RENDERER_CLASS(obj) \ + (G_TYPE_CHECK_CLASS_TYPE((obj), ZATHURA_TYPE_RENDERER)) +#define ZATHURA_RENDERER_GET_CLASS \ + (G_TYPE_INSTANCE_GET_CLASS((obj), ZATHURA_TYPE_RENDERER, ZathuraRendererClass)) /** - * This function initializes a render thread - * - * @param zathura object - * @return The render thread object or NULL if an error occured + * Returns the type of the renderer. + * @return the type */ -render_thread_t* render_init(zathura_t* zathura); +GType zathura_renderer_get_type(void); +/** + * Create a renderer. + * @return a renderer object + */ +ZathuraRenderer* zathura_renderer_new(void); /** - * This function destroys the render thread object - * - * @param render_thread The render thread object + * Return whether recoloring is enabled. + * @param renderer a renderer object + * @returns true if recoloring is enabled, false otherwise */ -void render_free(render_thread_t* render_thread); +bool zathura_renderer_recolor_enabled(ZathuraRenderer* renderer); +/** + * Enable/disable recoloring. + * @param renderer a renderer object + * @param enable wheter to enable or disable recoloring + */ +void zathura_renderer_enable_recolor(ZathuraRenderer* renderer, bool enable); +/** + * Return whether hue should be preserved while recoloring. + * @param renderer a renderer object + * @returns true if hue should be preserved, false otherwise + */ +bool zathura_renderer_recolor_hue_enabled(ZathuraRenderer* renderer); +/** + * Enable/disable preservation of hue while recoloring. + * @param renderer a renderer object + * @param enable wheter to enable or disable hue preservation + */ +void zathura_renderer_enable_recolor_hue(ZathuraRenderer* renderer, + bool enable); +/** + * Set light and dark colors for recoloring. + * @param renderer a renderer object + * @param light light color + * @param dark dark color + */ +void zathura_renderer_set_recolor_colors(ZathuraRenderer* renderer, + const GdkColor* light, const GdkColor* dark); +/** + * Set light and dark colors for recoloring. + * @param renderer a renderer object + * @param light light color + * @param dark dark color + */ +void zathura_renderer_set_recolor_colors_str(ZathuraRenderer* renderer, + const char* light, const char* dark); +/** + * Get light and dark colors for recoloring. + * @param renderer a renderer object + * @param light light color + * @param dark dark color + */ +void zathura_renderer_get_recolor_colors(ZathuraRenderer* renderer, + GdkColor* light, GdkColor* dark); +/** + * Stop rendering. + * @param renderer a render object + */ +void zathura_renderer_stop(ZathuraRenderer* renderer); + +/** + * Lock the render thread. This is useful if you want to render on your own (e.g + * for printing). + * + * @param renderer renderer object + */ +void zathura_renderer_lock(ZathuraRenderer* renderer); + +/** + * Unlock the render thread. + * + * @param renderer renderer object. + */ +void zathura_renderer_unlock(ZathuraRenderer* renderer); + + +typedef struct zathura_render_request_s ZathuraRenderRequest; +typedef struct zathura_render_request_class_s ZathuraRenderRequestClass; + +struct zathura_render_request_s +{ + GObject parent; +}; + +struct zathura_render_request_class_s +{ + GObjectClass parent_class; +}; + +#define ZATHURA_TYPE_RENDER_REQUEST \ + (zathura_render_request_get_type()) +#define ZATHURA_RENDER_REQUEST(obj) \ + (G_TYPE_CHECK_INSTANCE_CAST((obj), ZATHURA_TYPE_RENDER_REQUEST, \ + ZathuraRenderRequest)) +#define ZATHURA_RENDER_REQUEST_CLASS(obj) \ + (G_TYPE_CHECK_CLASS_CAST((obj), ZATHURA_TYPE_RENDER_REQUEST, \ + ZathuraRenderRequestClass)) +#define ZATHURA_IS_RENDER_REQUEST(obj) \ + (G_TYPE_CHECK_INSTANCE_TYPE((obj), ZATHURA_TYPE_RENDER_REQUEST)) +#define ZATHURA_IS_RENDER_REQUEST_CLASS(obj) \ + (G_TYPE_CHECK_CLASS_TYPE((obj), ZATHURA_TYPE_RENDER_REQUEST)) +#define ZATHURA_RENDER_REQUEST_GET_CLASS \ + (G_TYPE_INSTANCE_GET_CLASS((obj), ZATHURA_TYPE_RENDER_REQUEST, \ + ZathuraRenderRequestClass)) + +/** + * Returns the type of the render request. + * @return the type + */ +GType zathura_page_render_info_get_type(void); +/** + * Create a render request object + * @param renderer a renderer object + * @param page the page to be displayed + * @returns render request object + */ +ZathuraRenderRequest* zathura_render_request_new(ZathuraRenderer* renderer, + zathura_page_t* page); /** * This function is used to add a page to the render thread list * that should be rendered. * - * @param render_thread The render thread object - * @param page The page that should be rendered - * @return true if no error occured + * @param request request object of the page that should be renderer + * @param last_view_time last view time of the page */ -bool render_page(render_thread_t* render_thread, zathura_page_t* page); +void zathura_render_request(ZathuraRenderRequest* request, + gint64 last_view_time); + +/** + * Abort an existing render request. + * + * @param reqeust request that should be aborted + */ +void zathura_render_request_abort(ZathuraRenderRequest* request); /** * This function is used to unmark all pages as not rendered. This should @@ -44,19 +186,4 @@ bool render_page(render_thread_t* render_thread, zathura_page_t* page); */ void render_all(zathura_t* zathura); -/** - * Lock the render thread. This is useful if you want to render on your own (e.g - * for printing). - * - * @param render_thread The render thread object. - */ -void render_lock(render_thread_t* render_thread); - -/** - * Unlock the render thread. - * - * @param render_thread The render thread object. - */ -void render_unlock(render_thread_t* render_thread); - #endif // RENDER_H diff --git a/types.h b/types.h index e3d915e..433bcb5 100644 --- a/types.h +++ b/types.h @@ -30,6 +30,11 @@ typedef struct zathura_s zathura_t; */ typedef struct zathura_plugin_manager_s zathura_plugin_manager_t; +/** + * Renderer + */ +typedef struct zathura_renderer_s ZathuraRenderer; + /** * Error types */ diff --git a/zathura.c b/zathura.c index c647cc9..bbf2f42 100644 --- a/zathura.c +++ b/zathura.c @@ -68,7 +68,6 @@ zathura_create(void) zathura_t* zathura = g_malloc0(sizeof(zathura_t)); /* global settings */ - zathura->global.recolor = false; zathura->global.update_page_number = true; zathura->global.search_direction = FORWARD; @@ -711,6 +710,29 @@ document_open(zathura_t* zathura, const char* path, const char* password, zathura->document = document; + /* threads */ + zathura->sync.render_thread = zathura_renderer_new(); + + if (zathura->sync.render_thread == NULL) { + goto error_free; + } + + /* set up recolor info in ZathuraRenderer */ + char* recolor_dark = NULL; + char* recolor_light = NULL; + girara_setting_get(zathura->ui.session, "recolor-darkcolor", &recolor_dark); + girara_setting_get(zathura->ui.session, "recolor-lightcolor", &recolor_light); + zathura_renderer_set_recolor_colors_str(zathura->sync.render_thread, + recolor_light, recolor_dark); + g_free(recolor_dark); + g_free(recolor_light); + + bool recolor = false; + girara_setting_get(zathura->ui.session, "recolor", &recolor); + zathura_renderer_enable_recolor(zathura->sync.render_thread, recolor); + girara_setting_get(zathura->ui.session, "recolor-keephue", &recolor); + zathura_renderer_enable_recolor_hue(zathura->sync.render_thread, recolor); + /* create blank pages */ zathura->pages = calloc(number_of_pages, sizeof(GtkWidget*)); if (zathura->pages == NULL) { @@ -764,13 +786,6 @@ document_open(zathura_t* zathura, const char* path, const char* password, girara_set_view(zathura->ui.session, zathura->ui.page_widget_alignment); - /* threads */ - zathura->sync.render_thread = render_init(zathura); - - if (zathura->sync.render_thread == NULL) { - goto error_free; - } - for (unsigned int page_id = 0; page_id < number_of_pages; page_id++) { gtk_widget_realize(zathura->pages[page_id]); } @@ -888,6 +903,9 @@ document_close(zathura_t* zathura, bool keep_monitor) return false; } + /* stop rendering */ + zathura_renderer_stop(zathura->sync.render_thread); + /* remove monitor */ if (keep_monitor == false) { if (zathura->file_monitor.monitor != NULL) { @@ -950,7 +968,7 @@ document_close(zathura_t* zathura, bool keep_monitor) zathura->jumplist.size = 0; /* release render thread */ - render_free(zathura->sync.render_thread); + g_object_unref(zathura->sync.render_thread); zathura->sync.render_thread = NULL; /* remove widgets */ diff --git a/zathura.h b/zathura.h index 13df384..bea5d0a 100644 --- a/zathura.h +++ b/zathura.h @@ -31,10 +31,6 @@ enum { /* forward declaration for types from database.h */ typedef struct _ZathuraDatabase zathura_database_t; -/* forward declaration for types from render.h */ -struct render_thread_s; -typedef struct render_thread_s render_thread_t; - /** * Jump */ @@ -60,8 +56,6 @@ struct zathura_s struct { - GdkColor recolor_dark_color; /**< Dark color for recoloring */ - GdkColor recolor_light_color; /**< Light color for recoloring */ GdkColor highlight_color; /**< Color for highlighting */ GdkColor highlight_color_active; /** Color for highlighting */ GdkColor render_loading_bg; /**< Background color for render "Loading..." */ @@ -78,7 +72,7 @@ struct zathura_s struct { - render_thread_t* render_thread; /**< The thread responsible for rendering the pages */ + ZathuraRenderer* render_thread; /**< The thread responsible for rendering the pages */ } sync; struct @@ -106,8 +100,6 @@ struct zathura_s struct { - bool recolor_keep_hue; /**< Keep hue when recoloring */ - bool recolor; /**< Recoloring mode switch */ bool update_page_number; /**< Update current page number */ int search_direction; /**< Current search direction (FORWARD or BACKWARD) */ girara_list_t* marks; /**< Marker */