synctex-dbus.c 8.52 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/* 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>
#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";
17 18 19 20
/* object path */
static const char DBUS_OBJPATH[] = "/org/pwmt/zathura/synctex";
/* interface name */
static const char DBUS_INTERFACE[] = "org.pwmt.zathura.synctex";
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36

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))

/* Introspection data for the service we are exporting */
static const char SYNCTEX_DBUS_INTROSPECTION[] =
  "<node>\n"
37
  "  <interface name='org.pwmt.zathura.synctex'>\n"
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 89 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 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
  "    <method name='View'>\n"
  "      <arg type='s' name='position' direction='in' />\n"
  "      <arg type='b' name='return' direction='out' />\n"
  "    </method>\n"
  "    <property type='s' name='filename' access='read' />\n"
  "  </interface>\n"
  "</node>";

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;
  private_t* priv = GET_PRIVATE(dbus);

  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;
  priv->introspection_data = g_dbus_node_info_new_for_xml(SYNCTEX_DBUS_INTROSPECTION, &error);
  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),
    const gchar* UNUSED(sender), const gchar* UNUSED(object_path),
    const gchar* UNUSED(interface_name),
    const gchar* method_name, GVariant* parameters,
    GDBusMethodInvocation* invocation, void* data)
{
  ZathuraSynctexDbus* synctex_dbus = data;
169
  private_t* priv = GET_PRIVATE(synctex_dbus);
170 171 172 173 174

  if (g_strcmp0(method_name, "View") == 0) {
      gchar* position = NULL;
      g_variant_get(parameters, "(s)", &position);

175
      const bool ret = synctex_view(priv->zathura, position);
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
      g_free(position);

      GVariant* result = g_variant_new("(b)", ret);
      g_dbus_method_invocation_return_value(invocation, result);
  }
}

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,
    GError** UNUSED(error), void* data)
{
  ZathuraSynctexDbus* synctex_dbus = data;
  private_t* priv = GET_PRIVATE(synctex_dbus);

  if (g_strcmp0(property_name, "filename") == 0) {
    return g_variant_new_string(zathura_document_get_path(priv->zathura->document));
  }

  return NULL;
}

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

206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
static const unsigned int TIMEOUT = 3000;

bool
synctex_forward_position(const char* filename, const char* position)
{
  if (filename == NULL || position == NULL) {
    return false;
  }

  GError* error = NULL;
  GDBusConnection* connection = g_bus_get_sync(G_BUS_TYPE_SESSION,
      NULL, &error);
  /* GDBusProxy* proxy = g_dbus_proxy_new_for_bus_sync(G_BUS_TYPE_SESSION,
    G_DBUS_PROXY_FLAGS_NONE, NULL, "org.freedesktop.DBus",
    "/org/freedesktop/DBus", "org.freedesktop.DBus", 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(proxy);
    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",
253
      "Get", g_variant_new("(ss)", DBUS_INTERFACE, "filename"),
254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278
      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;

    GVariant* ret = g_dbus_connection_call_sync(connection,
279
      name, DBUS_OBJPATH, DBUS_INTERFACE, "View",
280 281 282 283 284 285 286 287 288 289 290
      g_variant_new("(s)", position), G_VARIANT_TYPE("(b)"),
      G_DBUS_CALL_FLAGS_NONE, TIMEOUT, NULL, &error);
    if (ret == NULL) {
      girara_error("Failed to run View on '%s': %s", name, error->message);
      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
291
  g_object_unref(connection);
292 293 294 295

  return found_one;
}