Send rectangles not on the main page too

Signed-off-by: Sebastian Ramacher <sebastian+dev@ramacher.at>
This commit is contained in:
Sebastian Ramacher 2014-01-14 15:05:09 +01:00
parent 7e6112d883
commit 74fd28b4b2
5 changed files with 114 additions and 24 deletions

View File

@ -22,6 +22,7 @@
<method name='HighlightRects'>
<arg type='i' name='page' direction='in' />
<arg type='a(dddd)' name='rectangles' direction='in' />
<arg type='a(idddd)' name='secondary_rectangles' direction='in' />
<arg type='b' name='return' direction='out' />
</method>
<property type='s' name='filename' access='read' />

View File

@ -5,6 +5,8 @@
#include "macros.h"
#include "zathura.h"
#include "document.h"
#include "utils.h"
#include <girara/utils.h>
#include <gio/gio.h>
#include <sys/types.h>
@ -149,6 +151,28 @@ zathura_dbus_new(zathura_t* zathura)
/* D-Bus handler */
static void
highlight_rects(zathura_t* zathura, unsigned int page,
girara_list_t** rectangles)
{
const unsigned int number_of_pages = zathura_document_get_number_of_pages(zathura->document);
for (unsigned int p = 0; p != number_of_pages; ++p) {
GObject* widget = G_OBJECT(priv->zathura->pages[p]);
g_object_set(widget, "draw-links", FALSE, "search-results", rectangles[p],
NULL);
if (p == page) {
g_object_set(widget, "search-current", 0, NULL);
} else {
g_object_set(widget, "search-current", -1, NULL);
}
}
page_set(zathura, page);
document_draw_search_results(zathura, true);
}
static void
handle_method_call(GDBusConnection* UNUSED(connection),
const gchar* UNUSED(sender), const gchar* object_path,
@ -211,29 +235,52 @@ handle_method_call(GDBusConnection* UNUSED(connection),
} else if (g_strcmp0(method_name, "HighlightRects") == 0) {
gint page = ZATHURA_PAGE_NUMBER_UNSPECIFIED;
GVariantIter* iter = NULL;
g_variant_get(parameters, "(ia(dddd))", &page, &iter);
GVariantIter* secondary_iter = NULL;
g_variant_get(parameters, "(ia(dddd)a(idddd))", &page, &iter,
&secondary_iter);
if (page < 1 || (unsigned int)page >= number_of_pages) {
GVariant* result = g_variant_new("(b)", false);
g_variant_iter_free(iter);
g_variant_iter_free(secondary_iter);
g_dbus_method_invocation_return_value(invocation, result);
return;
}
/* get rectangles */
girara_list_t* rectangles = girara_list_new2(g_free);
girara_list_t** rectangles = g_malloc0(number_of_pages * sizeof(girara_list_t*));
rectangles[page - 1] = girara_list_new2(g_free);
zathura_rectangle_t temp_rect;
while (g_variant_iter_loop(iter, "(dddd)", &temp_rect.x1, &temp_rect.x2,
&temp_rect.y1, &temp_rect.y2)) {
zathura_rectangle_t* rect = g_malloc0(sizeof(zathura_rectangle_t));
memcpy(rect, &temp_rect, sizeof(zathura_rectangle_t));
girara_list_append(rectangles, rect);
girara_list_append(rectangles[page - 1], rect);
}
g_variant_iter_free(iter);
page_set(priv->zathura, page - 1);
/* get secondary rectangles */
int temp_page = ZATHURA_PAGE_NUMBER_UNSPECIFIED;
while (g_variant_iter_loop(secondary_iter, "(idddd)", &temp_page,
&temp_rect.x1, &temp_rect.x2, &temp_rect.y1, &temp_rect.y2)) {
if (temp_page < 0 || (unsigned int)temp_page >= number_of_pages) {
/* error out here? */
continue;
}
GObject* widget = G_OBJECT(priv->zathura->pages[page - 1]);
g_object_set(widget, "draw-links", FALSE, "search-results", rectangles,
"search-current", 0, "draw-search-results", TRUE, NULL);
if (rectangles[temp_page] == NULL) {
rectangles[temp_page] = girara_list_new2(g_free);
}
zathura_rectangle_t* rect = g_malloc0(sizeof(zathura_rectangle_t));
memcpy(rect, &temp_rect, sizeof(zathura_rectangle_t));
girara_list_append(rectangles[temp_page], rect);
}
g_variant_iter_free(secondary_iter);
highlight_rects(priv->zathura, page - 1, rectangles);
g_free(rectangles);
}
}
@ -268,7 +315,7 @@ static const unsigned int TIMEOUT = 3000;
bool
zathura_dbus_goto_page_and_highlight(const char* filename, int page,
girara_list_t* rectangles)
girara_list_t* rectangles, girara_list_t* secondary_rects)
{
/* note: page is [1, number_of_pages] here */
@ -343,9 +390,17 @@ zathura_dbus_goto_page_and_highlight(const char* filename, int page,
GIRARA_LIST_FOREACH_END(rectangles, zathura_rectangle_t*, iter, rect);
}
GVariantBuilder* second_builder = g_variant_builder_new(G_VARIANT_TYPE("a(idddd)"));
if (secondary_rects != NULL) {
GIRARA_LIST_FOREACH(secondary_rects, synctex_page_rect_t*, iter, rect)
g_variant_builder_add(builder, "(idddd)", rect->page, rect->rect.x1,
rect->rect.x2, rect->rect.y1, rect->rect.y2);
GIRARA_LIST_FOREACH_END(secondary_rects, synctex_page_rect_t*, iter, rect);
}
GVariant* ret = g_dbus_connection_call_sync(connection,
name, DBUS_OBJPATH, DBUS_INTERFACE, "HighlightRects",
g_variant_new("(ia(dddd))", page, builder),
g_variant_new("(ia(dddd)a(idddd))", page, builder, second_builder),
G_VARIANT_TYPE("(b)"), G_DBUS_CALL_FLAGS_NONE, TIMEOUT, NULL, &error);
g_variant_builder_unref(builder);
if (ret == NULL) {
@ -370,12 +425,16 @@ zathura_dbus_synctex_position(const char* filename, const char* position)
}
int page = ZATHURA_PAGE_NUMBER_UNSPECIFIED;
girara_list_t* rectangles = synctex_rectangles_from_position(filename, position, &page);
girara_list_t* secondary_rects = NULL;
girara_list_t* rectangles = synctex_rectangles_from_position(filename,
position, &page, &secondary_rects);
if (rectangles == NULL) {
return false;
}
bool ret = zathura_dbus_goto_page_and_highlight(filename, page, rectangles);
const bool ret = zathura_dbus_goto_page_and_highlight(filename, page,
rectangles, secondary_rects);
girara_list_free(rectangles);
girara_list_free(secondary_rects);
return ret;
}

View File

@ -47,11 +47,13 @@ ZathuraDbus* zathura_dbus_new(zathura_t* zathura);
* @param filename filename
* @param page page number
* @param rectangles list of rectangles to highlight
* @param secondary_rects list of synctex_page_rect_ts for rectangles not on the
* page given by page
* @returns true if a instance was found that has the given filename open, false
* otherwise
* otherwise
*/
bool zathura_dbus_goto_page_and_highlight(const char* filename, int page,
girara_list_t* rectangles);
girara_list_t* rectangles, girara_list_t* secondary_rects);
bool zathura_dbus_synctex_position(const char* filename, const char* position);

View File

@ -1,6 +1,7 @@
/* See LICENSE file for license and copyright information */
#include <glib.h>
#include <string.h>
#include "synctex.h"
@ -69,7 +70,8 @@ synctex_edit(zathura_t* zathura, zathura_page_t* page, int x, int y)
argv[0] = g_strdup("synctex");
argv[1] = g_strdup("edit");
argv[2] = g_strdup("-o");
argv[3] = g_strdup_printf("%d:%d:%d:%s", zathura_page_get_index(page) + 1, x, y, filename);
argv[3] = g_strdup_printf("%d:%d:%d:%s", zathura_page_get_index(page) + 1, x,
y, filename);
if (zathura->synctex.editor != NULL) {
argv[4] = g_strdup("-x");
argv[5] = g_strdup(zathura->synctex.editor);
@ -93,9 +95,10 @@ scan_float(GScanner* scanner)
}
girara_list_t*
synctex_rectangles_from_position(const char* filename, const char* position, int* page)
synctex_rectangles_from_position(const char* filename, const char* position,
int* page, girara_list_t** secondary_rects)
{
if (filename == NULL || position == NULL || page == NULL) {
if (filename == NULL || position == NULL || page) {
return NULL;
}
@ -107,7 +110,7 @@ synctex_rectangles_from_position(const char* filename, const char* position, int
argv[4] = g_strdup("-o");
argv[5] = g_strdup(filename);
gint output;
gint output = -1;
bool ret = g_spawn_async_with_pipes(NULL, argv, NULL,
G_SPAWN_SEARCH_PATH | G_SPAWN_STDERR_TO_DEV_NULL, NULL, NULL, NULL, NULL,
&output, NULL, NULL);
@ -147,9 +150,10 @@ synctex_rectangles_from_position(const char* filename, const char* position, int
}
}
*page = ZATHURA_PAGE_NUMBER_UNSPECIFIED;
int current_page;
girara_list_t* hitlist = girara_list_new2(g_free);;
int rpage = ZATHURA_PAGE_NUMBER_UNSPECIFIED;
int current_page = ZATHURA_PAGE_NUMBER_UNSPECIFIED;
girara_list_t* hitlist = girara_list_new2(g_free);
girara_list_t* other_rects = girara_list_new2(g_free);
zathura_rectangle_t* rectangle = NULL;
while (found_end == false) {
@ -167,13 +171,18 @@ synctex_rectangles_from_position(const char* filename, const char* position, int
case SYNCTEX_PROP_PAGE:
if (g_scanner_get_next_token(scanner) == G_TOKEN_INT) {
current_page = g_scanner_cur_value(scanner).v_int;
if (*page == ZATHURA_PAGE_NUMBER_UNSPECIFIED) {
*page = current_page;
if (rpage == ZATHURA_PAGE_NUMBER_UNSPECIFIED) {
rpage = current_page;
}
if (*page == current_page && rectangle != NULL) {
girara_list_append(hitlist, rectangle);
rectangle = NULL;
} else if (rectangle != NULL) {
synctex_page_rect_t* page_rect = g_malloc0(sizeof(synctex_page_rect_t));
page_rect->page = current_page;
memcpy(&page_rect->rect, rectangle, sizeof(zathura_rectangle_t));
girara_list_append(other_rects, page_rect);
}
g_free(rectangle);
@ -205,9 +214,13 @@ synctex_rectangles_from_position(const char* filename, const char* position, int
}
if (rectangle != NULL) {
if (current_page == *page) {
if (current_page == rpage) {
girara_list_append(hitlist, rectangle);
} else {
synctex_page_rect_t* page_rect = g_malloc0(sizeof(synctex_page_rect_t));
page_rect->page = current_page;
memcpy(&page_rect->rect, rectangle, sizeof(zathura_rectangle_t));
girara_list_append(other_rects, page_rect);
g_free(rectangle);
}
}
@ -215,5 +228,14 @@ synctex_rectangles_from_position(const char* filename, const char* position, int
g_scanner_destroy(scanner);
close(output);
if (page != NULL) {
*page = rpage;
}
if (secondary_rects != NULL) {
*secondary_rects = other_rects;
} else {
girara_list_free(other_rects);
}
return hitlist;
}

View File

@ -5,7 +5,13 @@
#include "types.h"
typedef struct synctex_page_rect_s {
int page;
zathura_rectangle_t rect;
} synctex_page_rect_t;
void synctex_edit(zathura_t* zathura, zathura_page_t* page, int x, int y);
girara_list_t* synctex_rectangles_from_position(const char* filename, const char* position, int* page);
girara_list_t* synctex_rectangles_from_position(const char* filename,
const char* position, int* page, girara_list_t** secondary_rects);
#endif