synctex-dbus.c 11.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
/* See LICENSE file for license and copyright information */

#include "synctex-dbus.h"
#include "synctex.h"
#include "macros.h"
#include "zathura.h"
#include "document.h"
#include <girara/utils.h>
#include <gio/gio.h>
#include <sys/types.h>
11
#include <string.h>
12 13 14 15 16 17
#include <unistd.h>

G_DEFINE_TYPE(ZathuraSynctexDbus, zathura_synctex_dbus, G_TYPE_OBJECT)

/* template for bus name */
static const char DBUS_NAME_TEMPLATE[] = "org.pwmt.zathura.PID-%d";
18
/* object path */
19
static const char DBUS_OBJPATH[] = "/org/pwmt/zathura";
20
/* interface name */
21
static const char DBUS_INTERFACE[] = "org.pwmt.zathura";
22 23 24 25 26 27 28 29 30 31 32 33 34

typedef struct private_s {
  zathura_t* zathura;
  GDBusNodeInfo* introspection_data;
  GDBusConnection* connection;
  guint owner_id;
  guint registration_id;
} private_t;

#define GET_PRIVATE(obj) \
  (G_TYPE_INSTANCE_GET_PRIVATE((obj), ZATHURA_TYPE_SYNCTEX_DBUS, \
                               private_t))

35 36
/* in dbus-interface-definitions.c */
extern const char* DBUS_INTERFACE_XML;
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88

static const GDBusInterfaceVTable interface_vtable;

static void
finalize(GObject* object)
{
  ZathuraSynctexDbus* synctex_dbus = ZATHURA_SYNCTEX_DBUS(object);
  private_t* priv                  = GET_PRIVATE(synctex_dbus);

  if (priv->connection != NULL && priv->registration_id > 0) {
    g_dbus_connection_unregister_object(priv->connection, priv->registration_id);
  }

  if (priv->owner_id > 0) {
    g_bus_unown_name(priv->owner_id);
  }

  if (priv->introspection_data != NULL) {
    g_dbus_node_info_unref(priv->introspection_data);
  }

  G_OBJECT_CLASS(zathura_synctex_dbus_parent_class)->finalize(object);
}

static void
zathura_synctex_dbus_class_init(ZathuraSynctexDbusClass* class)
{
  /* add private members */
  g_type_class_add_private(class, sizeof(private_t));

  /* overwrite methods */
  GObjectClass* object_class = G_OBJECT_CLASS(class);
  object_class->finalize     = finalize;
}

static void
zathura_synctex_dbus_init(ZathuraSynctexDbus* synctex_dbus)
{
  private_t* priv          = GET_PRIVATE(synctex_dbus);
  priv->zathura            = NULL;
  priv->introspection_data = NULL;
  priv->connection         = NULL;
  priv->owner_id           = 0;
  priv->registration_id    = 0;
}

static void
bus_acquired(GDBusConnection* connection, const gchar* name, void* data)
{
  girara_debug("Bus acquired at '%s'.", name);

  ZathuraSynctexDbus* dbus = data;
89
  private_t* priv          = GET_PRIVATE(dbus);
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132

  GError* error = NULL;
  priv->registration_id = g_dbus_connection_register_object(connection,
      DBUS_OBJPATH, priv->introspection_data->interfaces[0],
      &interface_vtable, dbus, NULL, &error);
  if (priv->registration_id == 0) {
    girara_warning("Failed to register object on D-Bus connection: %s",
        error->message);
    g_error_free(error);
    return;
  }

  priv->connection = connection;
}

static void
name_acquired(GDBusConnection* UNUSED(connection), const gchar* name,
    void* UNUSED(data))
{
  girara_debug("Acquired '%s' on session bus.", name);
}

static void
name_lost(GDBusConnection* UNUSED(connection), const gchar* name,
    void* UNUSED(data))
{
  girara_debug("Lost connection or failed to acquire '%s' on session bus.",
      name);
}

ZathuraSynctexDbus*
zathura_synctex_dbus_new(zathura_t* zathura)
{
  GObject* obj = g_object_new(ZATHURA_TYPE_SYNCTEX_DBUS, NULL);
  if (obj == NULL) {
    return NULL;
  }

  ZathuraSynctexDbus* synctex_dbus = ZATHURA_SYNCTEX_DBUS(obj);
  private_t* priv                  = GET_PRIVATE(synctex_dbus);
  priv->zathura                    = zathura;

  GError* error = NULL;
133
  priv->introspection_data = g_dbus_node_info_new_for_xml(DBUS_INTERFACE_XML, &error);
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
  if (priv->introspection_data == NULL) {
    girara_warning("Failed to parse introspection data: %s", error->message);
    g_error_free(error);
    g_object_unref(obj);
    return NULL;
  }

  char* well_known_name = g_strdup_printf(DBUS_NAME_TEMPLATE, getpid());
  priv->owner_id = g_bus_own_name(G_BUS_TYPE_SESSION,
      well_known_name, G_BUS_NAME_OWNER_FLAGS_NONE, bus_acquired,
      name_acquired, name_lost, synctex_dbus, NULL);
  g_free(well_known_name);

  return synctex_dbus;
}

/* D-Bus handler */

static void
handle_method_call(GDBusConnection* UNUSED(connection),
154 155
    const gchar* UNUSED(sender), const gchar* object_path,
    const gchar* interface_name,
156 157 158 159
    const gchar* method_name, GVariant* parameters,
    GDBusMethodInvocation* invocation, void* data)
{
  ZathuraSynctexDbus* synctex_dbus = data;
160
  private_t* priv = GET_PRIVATE(synctex_dbus);
161

162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
  girara_debug("Handling call '%s.%s' on '%s'.", interface_name, method_name,
               object_path);

  /* methods that work without open document */
  if (g_strcmp0(method_name, "OpenDocument") == 0) {
    gchar* filename = NULL;
    gchar* password = NULL;
    gint page = ZATHURA_PAGE_NUMBER_UNSPECIFIED;
    g_variant_get(parameters, "(ssi)", &filename, &password, &page);

    document_close(priv->zathura, false);
    const bool ret = document_open(priv->zathura, filename,
        strlen(password) > 0 ? password : NULL, page);
    g_free(filename);
    g_free(password);

    GVariant* result = g_variant_new("(b)", ret);
    g_dbus_method_invocation_return_value(invocation, result);
    return;
  } else if (g_strcmp0(method_name, "CloseDocument") == 0) {
    const bool ret = document_close(priv->zathura, false);
183

184 185 186 187
    GVariant* result = g_variant_new("(b)", ret);
    g_dbus_method_invocation_return_value(invocation, result);
    return;
  }
188

189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
  if (priv->zathura->document == NULL) {
    g_dbus_method_invocation_return_dbus_error(invocation,
        "org.pwmt.zathura.NoOpenDocumen", "No document has been opened.");
    return;
  }

  const unsigned int number_of_pages = zathura_document_get_number_of_pages(priv->zathura->document);

  /* methods that require an open document */
  if (g_strcmp0(method_name, "GotoPage") == 0) {
    gint page = 0;
    g_variant_get(parameters, "(i)", &page);

    bool ret = true;
    if (page < 1 || (unsigned int)page >= number_of_pages) {
      ret = false;
    } else {
      page_set(priv->zathura, page - 1);
    }

    GVariant* result = g_variant_new("(b)", ret);
    g_dbus_method_invocation_return_value(invocation, result);
  } else if (g_strcmp0(method_name, "HighlightRects") == 0) {
    gint page = 0;
    GVariantIter* iter = NULL;
    g_variant_get(parameters, "(ia(dddd))", &page, &iter);

    if (page < 1 || (unsigned int)page >= number_of_pages) {
      GVariant* result = g_variant_new("(b)", false);
218
      g_dbus_method_invocation_return_value(invocation, result);
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
    }

    /* get rectangles */
    girara_list_t* rectangles = 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);
    }
    g_variant_iter_free(iter);

    page_set(priv->zathura, page - 1);

    GObject* widget = G_OBJECT(priv->zathura->pages[page - 1]);
    g_object_set(widget, "draw-links", FALSE, "search-results", rectangles,
        "search-current", 0, NULL);
237 238 239 240 241 242 243
  }
}

static GVariant*
handle_get_property(GDBusConnection* UNUSED(connection),
    const gchar* UNUSED(sender), const gchar* UNUSED(object_path),
    const gchar* UNUSED(interface_name), const gchar* property_name,
244
    GError** error, void* data)
245 246 247 248 249
{
  ZathuraSynctexDbus* synctex_dbus = data;
  private_t* priv = GET_PRIVATE(synctex_dbus);

  if (g_strcmp0(property_name, "filename") == 0) {
250 251 252 253 254
    if (priv->zathura->document == NULL) {
      g_set_error(error, G_IO_ERROR, G_IO_ERROR_FAILED, "No document open.");
    } else {
      return g_variant_new_string(zathura_document_get_path(priv->zathura->document));
    }
255 256 257 258 259 260 261 262 263 264 265 266
  }

  return NULL;
}

static const GDBusInterfaceVTable interface_vtable =
{
  .method_call  = handle_method_call,
  .get_property = handle_get_property,
  .set_property = NULL
};

267 268 269
static const unsigned int TIMEOUT = 3000;

bool
270
zathura_dbus_goto_page_and_highlight(const char* filename, int page, girara_list_t* rectangles)
271
{
272
  if (filename == NULL) {
273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309
    return false;
  }

  GError* error = NULL;
  GDBusConnection* connection = g_bus_get_sync(G_BUS_TYPE_SESSION,
      NULL, &error);
  if (connection == NULL) {
    girara_error("Could not create proxy for 'org.freedesktop.DBus': %s",
        error->message);
    g_error_free(error);
    return false;
  }

  GVariant* vnames = g_dbus_connection_call_sync(connection,
      "org.freedesktop.DBus", "/org/freedesktop/DBus", "org.freedesktop.DBus",
      "ListNames", NULL, G_VARIANT_TYPE("(as)"), G_DBUS_CALL_FLAGS_NONE,
      TIMEOUT, NULL, &error);
  if (vnames == NULL) {
    girara_error("Could not list available names: %s", error->message);
    g_error_free(error);
    g_object_unref(connection);
    return false;
  }

  GVariantIter* iter = NULL;
  g_variant_get(vnames, "(as)", &iter);

  gchar* name = NULL;
  bool found_one = false;
  while (g_variant_iter_loop(iter, "s", &name) == TRUE) {
    if (g_str_has_prefix(name, "org.pwmt.zathura.PID") == FALSE) {
      continue;
    }
    girara_debug("Found name: %s", name);

    GVariant* vfilename = g_dbus_connection_call_sync(connection,
      name, DBUS_OBJPATH, "org.freedesktop.DBus.Properties",
310
      "Get", g_variant_new("(ss)", DBUS_INTERFACE, "filename"),
311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334
      G_VARIANT_TYPE("(v)"), G_DBUS_CALL_FLAGS_NONE,
      TIMEOUT, NULL, &error);
    if (vfilename == NULL) {
      girara_error("Failed to query 'filename' property from '%s': %s",
          name, error->message);
      g_error_free(error);
      continue;
    }

    GVariant* tmp = NULL;
    g_variant_get(vfilename, "(v)", &tmp);
    gchar* remote_filename = g_variant_dup_string(tmp, NULL);
    girara_debug("Filename from '%s': %s", name, remote_filename);
    g_variant_unref(tmp);
    g_variant_unref(vfilename);

    if (g_strcmp0(filename, remote_filename) != 0) {
      g_free(remote_filename);
      continue;
    }

    g_free(remote_filename);
    found_one = true;

335 336 337 338 339 340 341 342
    GVariantBuilder* builder = g_variant_builder_new(G_VARIANT_TYPE("a(dddd)"));
    if (rectangles != NULL) {
      GIRARA_LIST_FOREACH(rectangles, zathura_rectangle_t*, iter, rect)
        g_variant_builder_add(builder, "(dddd)", rect->x1, rect->x2, rect->y1,
            rect->y2);
      GIRARA_LIST_FOREACH_END(rectangles, zathura_rectangle_t*, iter, rect);
    }

343
    GVariant* ret = g_dbus_connection_call_sync(connection,
344 345 346 347
      name, DBUS_OBJPATH, DBUS_INTERFACE, "HighlightRects",
      g_variant_new("(ia(dddd))", page, builder),
      G_VARIANT_TYPE("(b)"), G_DBUS_CALL_FLAGS_NONE, TIMEOUT, NULL, &error);
    g_variant_builder_unref(builder);
348
    if (ret == NULL) {
349
      girara_error("Failed to run HighlightRects on '%s': %s", name, error->message);
350 351 352 353 354 355 356
      g_error_free(error);
    } else {
      g_variant_unref(ret);
    }
  }
  g_variant_iter_free(iter);
  g_variant_unref(vnames);
Sebastian Ramacher's avatar
Sebastian Ramacher committed
357
  g_object_unref(connection);
358 359 360 361

  return found_one;
}

362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378
bool
zathura_dbus_synctex_position(const char* filename, const char* position)
{
  if (filename == NULL || position == NULL) {
    return false;
  }

  int page = -1;
  girara_list_t* rectangles = synctex_rectangles_from_position(filename, position, &page);
  if (rectangles == NULL) {
    return false;
  }

  bool ret = zathura_dbus_goto_page_and_highlight(filename, page, rectangles);
  girara_list_free(rectangles);
  return ret;
}