From 8ea7af2c95db1fb7110fd91695683d510e9c889b Mon Sep 17 00:00:00 2001 From: Sebastian Ramacher Date: Wed, 1 Dec 2010 11:19:35 +0100 Subject: [PATCH 1/7] Fix some memory issues related to bookmarks (closes #26). --- zathura.c | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/zathura.c b/zathura.c index ed796e2..9fa281a 100644 --- a/zathura.c +++ b/zathura.c @@ -609,6 +609,10 @@ init_directories(void) void init_bookmarks(void) { + /* init variables */ + Zathura.Bookmarks.number_of_bookmarks = 0; + Zathura.Bookmarks.bookmarks = NULL; + /* create or open existing bookmark file */ Zathura.Bookmarks.data = g_key_file_new(); gchar* bookmarks = g_build_filename(Zathura.Config.data_dir, BOOKMARK_FILE, NULL); @@ -1065,8 +1069,13 @@ close_file(gboolean keep_monitor) /* save bookmarks */ int i; for(i = 0; i < Zathura.Bookmarks.number_of_bookmarks; i++) + { g_key_file_set_integer(Zathura.Bookmarks.data, Zathura.PDF.file, Zathura.Bookmarks.bookmarks[i].id, Zathura.Bookmarks.bookmarks[i].page); + g_free(Zathura.Bookmarks.bookmarks[i].id); + } + free(Zathura.Bookmarks.bookmarks); + Zathura.Bookmarks.number_of_bookmarks = 0; /* convert file and save it */ gchar* bookmarks = g_key_file_to_data(Zathura.Bookmarks.data, NULL, NULL); @@ -1392,7 +1401,7 @@ open_file(char* path, char* password) Zathura.Bookmarks.bookmarks = realloc(Zathura.Bookmarks.bookmarks, (Zathura.Bookmarks.number_of_bookmarks + 1) * sizeof(Bookmark)); - Zathura.Bookmarks.bookmarks[Zathura.Bookmarks.number_of_bookmarks].id = keys[i]; + Zathura.Bookmarks.bookmarks[Zathura.Bookmarks.number_of_bookmarks].id = g_strdup(keys[i]); Zathura.Bookmarks.bookmarks[Zathura.Bookmarks.number_of_bookmarks].page = g_key_file_get_integer(Zathura.Bookmarks.data, file, keys[i], NULL); @@ -2966,7 +2975,7 @@ cmd_bookmark(int argc, char** argv) Zathura.Bookmarks.bookmarks = realloc(Zathura.Bookmarks.bookmarks, (Zathura.Bookmarks.number_of_bookmarks + 1) * sizeof(Bookmark)); - Zathura.Bookmarks.bookmarks[Zathura.Bookmarks.number_of_bookmarks].id = id->str; + Zathura.Bookmarks.bookmarks[Zathura.Bookmarks.number_of_bookmarks].id = g_strdup(id->str); Zathura.Bookmarks.bookmarks[Zathura.Bookmarks.number_of_bookmarks].page = Zathura.PDF.page_number; Zathura.Bookmarks.number_of_bookmarks++; @@ -3057,6 +3066,7 @@ cmd_delete_bookmark(int argc, char** argv) /* update key file */ g_key_file_remove_key(Zathura.Bookmarks.data, Zathura.PDF.file, Zathura.Bookmarks.bookmarks[i].id, NULL); + g_free(Zathura.Bookmarks.bookmarks[i].id); /* update bookmarks */ Zathura.Bookmarks.bookmarks[i].id = Zathura.Bookmarks.bookmarks[Zathura.Bookmarks.number_of_bookmarks - 1].id; Zathura.Bookmarks.bookmarks[i].page = Zathura.Bookmarks.bookmarks[Zathura.Bookmarks.number_of_bookmarks - 1].page; From d42692eae6525b02de7567fd22959a7d3d4aa2cd Mon Sep 17 00:00:00 2001 From: Sebastian Ramacher Date: Wed, 1 Dec 2010 16:47:58 +0100 Subject: [PATCH 2/7] typo --- config.def.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config.def.h b/config.def.h index ee5e2e9..0946b97 100644 --- a/config.def.h +++ b/config.def.h @@ -16,7 +16,7 @@ static const char FORMAT_DESCRIPTION[] = "%s"; /* Use XDG directory specification if no config and data directory are given on * the command line. Uncomment the next line if you just want to use CONFIG_DIR * and DATA_DIR instead (these will be the default locations if the XDG_* - * environment variebles are not set anyway) */ + * environment variables are not set anyway) */ /* #define ZATHURA_NO_XDG */ /* directories and files */ From 18da0bf22885b41affa06e8edcfe25df89d5cdce Mon Sep 17 00:00:00 2001 From: Sebastian Ramacher Date: Wed, 1 Dec 2010 23:07:09 +0100 Subject: [PATCH 3/7] Make it build without warnings if -Wno-zero-length is not set. --- zathura.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/zathura.c b/zathura.c index 9fa281a..2eb190d 100644 --- a/zathura.c +++ b/zathura.c @@ -687,7 +687,7 @@ init_zathura(void) Zathura.Global.show_inputbar = TRUE; Zathura.Global.show_statusbar = TRUE; - Zathura.State.pages = g_strdup_printf(""); + Zathura.State.pages = g_strdup(""); Zathura.State.scroll_percentage = 0; Zathura.Marker.markers = NULL; @@ -1102,7 +1102,7 @@ close_file(gboolean keep_monitor) g_free(Zathura.State.pages); gtk_window_set_title(GTK_WINDOW(Zathura.UI.window), "zathura"); - Zathura.State.pages = g_strdup_printf(""); + Zathura.State.pages = g_strdup(""); g_free(Zathura.State.filename); Zathura.State.filename = g_strdup((char*) default_text); From 2ce5b54c713242342e18a0c56c17ca220af4aea5 Mon Sep 17 00:00:00 2001 From: Sebastian Ramacher Date: Thu, 2 Dec 2010 12:27:47 +0100 Subject: [PATCH 4/7] drop -Wno-format-zero (not needed anymore) --- config.mk | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config.mk b/config.mk index cdd0ba0..9924f86 100644 --- a/config.mk +++ b/config.mk @@ -15,7 +15,7 @@ INCS = -I. -I/usr/include ${GTK_INC} LIBS = -lc ${GTK_LIB} -lpthread -lm # compiler flags -CFLAGS += -std=c99 -pedantic -Wall -Wno-format-zero-length $(INCS) +CFLAGS += -std=c99 -pedantic -Wall $(INCS) # debug flags DFLAGS = -g From af61d097edf924ac7f91a21ef876da139c7e85a1 Mon Sep 17 00:00:00 2001 From: Sebastian Ramacher Date: Sat, 11 Dec 2010 09:51:35 +0100 Subject: [PATCH 5/7] Reread bookmarks file before writing (closes #74) Applying patch from Minoru with some simple adapations. Therefor read_bookmarks_file doesn't leak (bookmarks is never freed). --- zathura.c | 50 ++++++++++++++++++++++++++++++-------------------- 1 file changed, 30 insertions(+), 20 deletions(-) diff --git a/zathura.c b/zathura.c index 2eb190d..e6235e8 100644 --- a/zathura.c +++ b/zathura.c @@ -371,6 +371,7 @@ gboolean open_stdin(gchar*); void open_uri(char*); void out_of_memory(void) NORETURN; void update_status(void); +void read_bookmarks_file(void); void read_configuration_file(const char*); void read_configuration(void); void recalcRectangle(int, PopplerRectangle*); @@ -613,26 +614,8 @@ init_bookmarks(void) Zathura.Bookmarks.number_of_bookmarks = 0; Zathura.Bookmarks.bookmarks = NULL; - /* create or open existing bookmark file */ - Zathura.Bookmarks.data = g_key_file_new(); - gchar* bookmarks = g_build_filename(Zathura.Config.data_dir, BOOKMARK_FILE, NULL); - - if(!g_file_test(bookmarks, G_FILE_TEST_IS_REGULAR)) - { - /* file does not exist */ - g_file_set_contents(bookmarks, "# Zathura bookmarks\n", -1, NULL); - } - - GError* error = NULL; - if(!g_key_file_load_from_file(Zathura.Bookmarks.data, bookmarks, - G_KEY_FILE_KEEP_COMMENTS | G_KEY_FILE_KEEP_TRANSLATIONS, &error)) - { - gchar* message = g_strdup_printf("Could not load bookmark file: %s", error->message); - notify(ERROR, message); - g_free(message); - } - - Zathura.Bookmarks.file = bookmarks; + Zathura.Bookmarks.file = g_build_filename(Zathura.Config.data_dir, BOOKMARK_FILE, NULL); + read_bookmarks_file(); } void @@ -1058,6 +1041,8 @@ close_file(gboolean keep_monitor) /* save bookmarks */ if(Zathura.Bookmarks.data) { + read_bookmarks_file(); + /* set current page */ g_key_file_set_integer(Zathura.Bookmarks.data, Zathura.PDF.file, BM_PAGE_ENTRY, Zathura.PDF.page_number); @@ -1529,6 +1514,31 @@ update_status(void) g_free(zoom_level); } +void +read_bookmarks_file(void) +{ + /* free it at first */ + if (Zathura.Bookmarks.data) + g_key_file_free(Zathura.Bookmarks.data); + + /* create or open existing bookmark file */ + Zathura.Bookmarks.data = g_key_file_new(); + if(!g_file_test(Zathura.Bookmarks.file, G_FILE_TEST_IS_REGULAR)) + { + /* file does not exist */ + g_file_set_contents(Zathura.Bookmarks.file, "# Zathura bookmarks\n", -1, NULL); + } + + GError* error = NULL; + if(!g_key_file_load_from_file(Zathura.Bookmarks.data, Zathura.Bookmarks.file, + G_KEY_FILE_KEEP_COMMENTS | G_KEY_FILE_KEEP_TRANSLATIONS, &error)) + { + gchar* message = g_strdup_printf("Could not load bookmark file: %s", error->message); + notify(ERROR, message); + g_free(message); + } +} + void read_configuration_file(const char* rcfile) { From 7627aa17529e26b00e7575332406454567b5827d Mon Sep 17 00:00:00 2001 From: Sebastian Ramacher Date: Sat, 11 Dec 2010 10:06:43 +0100 Subject: [PATCH 6/7] more memory cleanup --- zathura.c | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/zathura.c b/zathura.c index e6235e8..e1a9289 100644 --- a/zathura.c +++ b/zathura.c @@ -4005,8 +4005,16 @@ cb_destroy(GtkWidget* widget, gpointer data) if(Zathura.PDF.document) close_file(FALSE); - /* clean up other variables */ + /* clean up bookmarks */ g_free(Zathura.Bookmarks.file); + if (Zathura.Bookmarks.data) + g_key_file_free(Zathura.Bookmarks.data); + + /* destroy mutexes */ + g_static_mutex_free(&(Zathura.Lock.pdflib_lock)); + g_static_mutex_free(&(Zathura.Lock.search_lock)); + g_static_mutex_free(&(Zathura.Lock.pdf_obj_lock)); + g_static_mutex_free(&(Zathura.Lock.select_lock)); /* inotify */ if(Zathura.FileMonitor.monitor) From 205298bce7f443a19b74955418ed18adef327270 Mon Sep 17 00:00:00 2001 From: Sebastian Ramacher Date: Sat, 11 Dec 2010 10:12:50 +0100 Subject: [PATCH 7/7] Add option to disable auto-generated bookmarks (closes #73) Applying patch from Minoru with some modifications since we already have read_bookmarks_file. --- config.def.h | 2 ++ zathura.c | 19 +++++++++++-------- 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/config.def.h b/config.def.h index 0946b97..c028673 100644 --- a/config.def.h +++ b/config.def.h @@ -29,6 +29,7 @@ static const char DATA_DIR[] = "~/.local/share/zathura"; /* bookmarks */ static const char BM_PAGE_ENTRY[] = "page"; static const char BM_PAGE_OFFSET[] = "offset"; +int save_position = 1; /* look */ char* font = "monospace normal 9"; @@ -248,6 +249,7 @@ Setting settings[] = { {"recolor", &(Zathura.Global.recolor), 'b', TRUE, FALSE, "Invert the image" }, {"recolor_darkcolor", &(recolor_darkcolor), 's', FALSE, TRUE, "Recoloring (dark color)"}, {"recolor_lightcolor", &(recolor_lightcolor), 's', FALSE, TRUE, "Recoloring (light color)"}, + {"save_position", &(save_position), 'b', FALSE, FALSE, "Save position in file on quit and restore it on open"}, {"scroll_step", &(scroll_step), 'f', FALSE, FALSE, "Scroll step"}, {"scroll_wrap", &(scroll_wrap), 'b', FALSE, FALSE, "Wrap scolling at last page"}, {"scrollbars", &(show_scrollbars), 'b', FALSE, TRUE, "Show scrollbars"}, diff --git a/zathura.c b/zathura.c index e1a9289..32562f3 100644 --- a/zathura.c +++ b/zathura.c @@ -1043,13 +1043,16 @@ close_file(gboolean keep_monitor) { read_bookmarks_file(); - /* set current page */ - g_key_file_set_integer(Zathura.Bookmarks.data, Zathura.PDF.file, - BM_PAGE_ENTRY, Zathura.PDF.page_number); + if(save_position) + { + /* set current page */ + g_key_file_set_integer(Zathura.Bookmarks.data, Zathura.PDF.file, + BM_PAGE_ENTRY, Zathura.PDF.page_number); - /* set page offset */ - g_key_file_set_integer(Zathura.Bookmarks.data, Zathura.PDF.file, - BM_PAGE_OFFSET, Zathura.PDF.page_offset); + /* set page offset */ + g_key_file_set_integer(Zathura.Bookmarks.data, Zathura.PDF.file, + BM_PAGE_OFFSET, Zathura.PDF.page_offset); + } /* save bookmarks */ int i; @@ -1365,11 +1368,11 @@ open_file(char* path, char* password) if(Zathura.Bookmarks.data && g_key_file_has_group(Zathura.Bookmarks.data, file)) { /* get last opened page */ - if(g_key_file_has_key(Zathura.Bookmarks.data, file, BM_PAGE_ENTRY, NULL)) + if(save_position && g_key_file_has_key(Zathura.Bookmarks.data, file, BM_PAGE_ENTRY, NULL)) start_page = g_key_file_get_integer(Zathura.Bookmarks.data, file, BM_PAGE_ENTRY, NULL); /* get page offset */ - if(g_key_file_has_key(Zathura.Bookmarks.data, file, BM_PAGE_OFFSET, NULL)) + if(save_position && g_key_file_has_key(Zathura.Bookmarks.data, file, BM_PAGE_OFFSET, NULL)) Zathura.PDF.page_offset = g_key_file_get_integer(Zathura.Bookmarks.data, file, BM_PAGE_OFFSET, NULL); if((Zathura.PDF.page_offset != 0) && (Zathura.PDF.page_offset != GOTO_OFFSET)) Zathura.PDF.page_offset = GOTO_OFFSET;