plugin.c 11.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13
/* See LICENSE file for license and copyright information */

#include "plugin.h"

#include <stdlib.h>
#include <glib/gi18n.h>

#include <girara/datastructures.h>
#include <girara/utils.h>
#include <girara/statusbar.h>
#include <girara/session.h>
#include <girara/settings.h>

14 15 16
/**
 * Document plugin structure
 */
Moritz Lipp's avatar
Moritz Lipp committed
17
struct zathura_plugin_s {
18 19 20 21 22 23 24 25 26 27 28 29
  girara_list_t* content_types; /**< List of supported content types */
  zathura_plugin_register_function_t register_function; /**< Document open function */
  zathura_plugin_functions_t functions; /**< Document functions */
  GModule* handle; /**< DLL handle */
  char* name; /**< Name of the plugin */
  char* path; /**< Path to the plugin */
  zathura_plugin_version_t version; /**< Version information */
};

/**
 * Plugin mapping
 */
Moritz Lipp's avatar
Moritz Lipp committed
30
typedef struct zathura_type_plugin_mapping_s {
31 32 33
  const gchar* type; /**< Plugin type */
  zathura_plugin_t* plugin; /**< Mapped plugin */
} zathura_type_plugin_mapping_t;
34

35
/**
Moritz Lipp's avatar
Moritz Lipp committed
36
 * Plugin manager
37
 */
Moritz Lipp's avatar
Moritz Lipp committed
38
struct zathura_plugin_manager_s {
Moritz Lipp's avatar
Moritz Lipp committed
39 40 41 42 43
  girara_list_t* plugins; /**< List of plugins */
  girara_list_t* path; /**< List of plugin paths */
  girara_list_t* type_plugin_mapping; /**< List of type -> plugin mappings */
};

44 45 46 47 48
typedef void (*zathura_plugin_register_service_t)(zathura_plugin_t*);
typedef unsigned int (*zathura_plugin_api_version_t)(void);
typedef unsigned int (*zathura_plugin_abi_version_t)(void);
typedef unsigned int (*zathura_plugin_version_function_t)(void);

Moritz Lipp's avatar
Moritz Lipp committed
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
static bool register_plugin(zathura_plugin_manager_t* plugin_manager, zathura_plugin_t* plugin);
static bool plugin_mapping_new(zathura_plugin_manager_t* plugin_manager, const gchar* type, zathura_plugin_t* plugin);
static void zathura_plugin_free(zathura_plugin_t* plugin);
static void zathura_type_plugin_mapping_free(zathura_type_plugin_mapping_t* mapping);

zathura_plugin_manager_t*
zathura_plugin_manager_new()
{
  zathura_plugin_manager_t* plugin_manager = g_malloc0(sizeof(zathura_plugin_manager_t));

  plugin_manager->plugins = girara_list_new2((girara_free_function_t) zathura_plugin_free);
  plugin_manager->path    = girara_list_new2(g_free);
  plugin_manager->type_plugin_mapping = girara_list_new2((girara_free_function_t)zathura_type_plugin_mapping_free);

  if (plugin_manager->plugins == NULL
      || plugin_manager->path == NULL
      || plugin_manager->type_plugin_mapping == NULL) {
    zathura_plugin_manager_free(plugin_manager);
    return NULL;
  }

  return plugin_manager;
}
72 73

void
Moritz Lipp's avatar
Moritz Lipp committed
74
zathura_plugin_manager_add_dir(zathura_plugin_manager_t* plugin_manager, const char* dir)
75
{
Moritz Lipp's avatar
Moritz Lipp committed
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
  if (plugin_manager == NULL || plugin_manager->path == NULL) {
    return;
  }

  girara_list_append(plugin_manager->path, g_strdup(dir));
}

void
zathura_plugin_manager_load(zathura_plugin_manager_t* plugin_manager)
{
  if (plugin_manager == NULL || plugin_manager->path == NULL) {
    return;
  }

  GIRARA_LIST_FOREACH(plugin_manager->path, char*, iter, plugindir)
Moritz Lipp's avatar
Moritz Lipp committed
91 92 93 94 95 96 97 98 99 100 101 102
  /* read all files in the plugin directory */
  GDir* dir = g_dir_open(plugindir, 0, NULL);
  if (dir == NULL) {
    girara_error("could not open plugin directory: %s", plugindir);
    girara_list_iterator_next(iter);
    continue;
  }

  char* name = NULL;
  while ((name = (char*) g_dir_read_name(dir)) != NULL) {
    char* path = g_build_filename(plugindir, name, NULL);
    if (g_file_test(path, G_FILE_TEST_IS_REGULAR) == 0) {
103
      girara_debug("%s is not a regular file. Skipping.", path);
Moritz Lipp's avatar
Moritz Lipp committed
104
      g_free(path);
105 106 107
      continue;
    }

Moritz Lipp's avatar
Moritz Lipp committed
108
    zathura_plugin_t* plugin = NULL;
109

Moritz Lipp's avatar
Moritz Lipp committed
110 111 112 113 114 115 116
    /* load plugin */
    GModule* handle = g_module_open(path, G_MODULE_BIND_LOCAL);
    if (handle == NULL) {
      girara_error("could not load plugin %s (%s)", path, g_module_error());
      g_free(path);
      continue;
    }
117

Moritz Lipp's avatar
Moritz Lipp committed
118 119 120 121 122 123 124 125 126
    /* resolve symbols and check API and ABI version*/
    zathura_plugin_api_version_t api_version = NULL;
    if (g_module_symbol(handle, PLUGIN_API_VERSION_FUNCTION, (gpointer*) &api_version) == FALSE ||
        api_version == NULL) {
      girara_error("could not find '%s' function in plugin %s", PLUGIN_API_VERSION_FUNCTION, path);
      g_free(path);
      g_module_close(handle);
      continue;
    }
127

Moritz Lipp's avatar
Moritz Lipp committed
128 129 130 131 132 133 134
    if (api_version() != ZATHURA_API_VERSION) {
      girara_error("plugin %s has been built againt zathura with a different API version (plugin: %d, zathura: %d)",
                   path, api_version(), ZATHURA_API_VERSION);
      g_free(path);
      g_module_close(handle);
      continue;
    }
135

Moritz Lipp's avatar
Moritz Lipp committed
136 137 138 139 140 141 142 143
    zathura_plugin_abi_version_t abi_version = NULL;
    if (g_module_symbol(handle, PLUGIN_ABI_VERSION_FUNCTION, (gpointer*) &abi_version) == FALSE ||
        abi_version == NULL) {
      girara_error("could not find '%s' function in plugin %s", PLUGIN_ABI_VERSION_FUNCTION, path);
      g_free(path);
      g_module_close(handle);
      continue;
    }
144

Moritz Lipp's avatar
Moritz Lipp committed
145 146 147 148 149 150 151
    if (abi_version() != ZATHURA_ABI_VERSION) {
      girara_error("plugin %s has been built againt zathura with a different ABI version (plugin: %d, zathura: %d)",
                   path, abi_version(), ZATHURA_ABI_VERSION);
      g_free(path);
      g_module_close(handle);
      continue;
    }
152

Moritz Lipp's avatar
Moritz Lipp committed
153 154 155 156 157 158 159 160
    zathura_plugin_register_service_t register_service = NULL;
    if (g_module_symbol(handle, PLUGIN_REGISTER_FUNCTION, (gpointer*) &register_service) == FALSE ||
        register_service == NULL) {
      girara_error("could not find '%s' function in plugin %s", PLUGIN_REGISTER_FUNCTION, path);
      g_free(path);
      g_module_close(handle);
      continue;
    }
161

Moritz Lipp's avatar
Moritz Lipp committed
162 163 164
    plugin = g_malloc0(sizeof(zathura_plugin_t));
    plugin->content_types = girara_list_new2(g_free);
    plugin->handle = handle;
165

Moritz Lipp's avatar
Moritz Lipp committed
166
    register_service(plugin);
167

Moritz Lipp's avatar
Moritz Lipp committed
168 169 170 171 172 173 174 175
    /* register functions */
    if (plugin->register_function == NULL) {
      girara_error("plugin has no document functions register function");
      g_free(path);
      g_free(plugin);
      g_module_close(handle);
      continue;
    }
176

Moritz Lipp's avatar
Moritz Lipp committed
177 178
    plugin->register_function(&(plugin->functions));
    plugin->path = path;
179

Moritz Lipp's avatar
Moritz Lipp committed
180 181 182 183 184
    bool ret = register_plugin(plugin_manager, plugin);
    if (ret == false) {
      girara_error("could not register plugin %s", path);
      zathura_plugin_free(plugin);
    } else {
185
      girara_debug("successfully loaded plugin %s", path);
Moritz Lipp's avatar
Moritz Lipp committed
186 187 188 189 190 191 192 193 194 195 196 197

      zathura_plugin_version_function_t plugin_major = NULL, plugin_minor = NULL, plugin_rev = NULL;
      g_module_symbol(handle, PLUGIN_VERSION_MAJOR_FUNCTION,    (gpointer*) &plugin_major);
      g_module_symbol(handle, PLUGIN_VERSION_MINOR_FUNCTION,    (gpointer*) &plugin_minor);
      g_module_symbol(handle, PLUGIN_VERSION_REVISION_FUNCTION, (gpointer*) &plugin_rev);
      if (plugin_major != NULL && plugin_minor != NULL && plugin_rev != NULL) {
        plugin->version.major = plugin_major();
        plugin->version.minor = plugin_minor();
        plugin->version.rev   = plugin_rev();
        girara_debug("plugin '%s': version %u.%u.%u", path,
                     plugin->version.major, plugin->version.minor,
                     plugin->version.rev);
198 199
      }
    }
Moritz Lipp's avatar
Moritz Lipp committed
200 201
  }
  g_dir_close(dir);
202 203 204
  GIRARA_LIST_FOREACH_END(zathura->plugins.path, char*, iter, plugindir);
}

Moritz Lipp's avatar
Moritz Lipp committed
205 206 207 208 209 210 211 212 213
zathura_plugin_t*
zathura_plugin_manager_get_plugin(zathura_plugin_manager_t* plugin_manager, const char* type)
{
  if (plugin_manager == NULL || plugin_manager->type_plugin_mapping == NULL || type == NULL) {
    return NULL;
  }

  zathura_plugin_t* plugin = NULL;
  GIRARA_LIST_FOREACH(plugin_manager->type_plugin_mapping, zathura_type_plugin_mapping_t*, iter, mapping)
Moritz Lipp's avatar
Moritz Lipp committed
214 215 216 217
  if (g_content_type_equals(type, mapping->type)) {
    plugin = mapping->plugin;
    break;
  }
Moritz Lipp's avatar
Moritz Lipp committed
218 219 220 221 222
  GIRARA_LIST_FOREACH_END(plugin_manager->type_plugin_mapping, zathura_type_plugin_mapping_t*, iter, mapping);

  return plugin;
}

223 224 225 226 227 228 229 230 231 232
girara_list_t*
zathura_plugin_manager_get_plugins(zathura_plugin_manager_t* plugin_manager)
{
  if (plugin_manager == NULL || plugin_manager->plugins == NULL) {
    return NULL;
  }

  return plugin_manager->plugins;
}

233
void
Moritz Lipp's avatar
Moritz Lipp committed
234
zathura_plugin_manager_free(zathura_plugin_manager_t* plugin_manager)
235
{
Moritz Lipp's avatar
Moritz Lipp committed
236
  if (plugin_manager == NULL) {
237 238 239
    return;
  }

Moritz Lipp's avatar
Moritz Lipp committed
240 241 242 243 244 245 246 247 248 249 250 251 252
  if (plugin_manager->plugins != NULL) {
    girara_list_free(plugin_manager->plugins);
  }

  if (plugin_manager->path != NULL) {
    girara_list_free(plugin_manager->path);
  }

  if (plugin_manager->type_plugin_mapping != NULL) {
    girara_list_free(plugin_manager->type_plugin_mapping);
  }

  g_free(plugin_manager);
253 254 255
}

static bool
Moritz Lipp's avatar
Moritz Lipp committed
256
register_plugin(zathura_plugin_manager_t* plugin_manager, zathura_plugin_t* plugin)
257
{
Moritz Lipp's avatar
Moritz Lipp committed
258 259 260 261 262
  if (plugin == NULL
      || plugin->content_types == NULL
      || plugin->register_function == NULL
      || plugin_manager == NULL
      || plugin_manager->plugins == NULL) {
263 264 265 266
    girara_error("plugin: could not register\n");
    return false;
  }

Moritz Lipp's avatar
Moritz Lipp committed
267 268
  bool at_least_one = false;
  GIRARA_LIST_FOREACH(plugin->content_types, gchar*, iter, type)
Moritz Lipp's avatar
Moritz Lipp committed
269 270 271 272 273
  if (plugin_mapping_new(plugin_manager, type, plugin) == false) {
    girara_error("plugin: already registered for filetype %s\n", type);
  } else {
    at_least_one = true;
  }
Moritz Lipp's avatar
Moritz Lipp committed
274
  GIRARA_LIST_FOREACH_END(plugin->content_types, gchar*, iter, type);
275

Moritz Lipp's avatar
Moritz Lipp committed
276 277
  if (at_least_one == true) {
    girara_list_append(plugin_manager->plugins, plugin);
278
  }
Moritz Lipp's avatar
Moritz Lipp committed
279 280

  return at_least_one;
281 282
}

Moritz Lipp's avatar
Moritz Lipp committed
283 284
static bool
plugin_mapping_new(zathura_plugin_manager_t* plugin_manager, const gchar* type, zathura_plugin_t* plugin)
285
{
Moritz Lipp's avatar
Moritz Lipp committed
286 287 288
  g_return_val_if_fail(plugin_manager != NULL, false);
  g_return_val_if_fail(type           != NULL, false);
  g_return_val_if_fail(plugin         != NULL, false);
289

Moritz Lipp's avatar
Moritz Lipp committed
290
  GIRARA_LIST_FOREACH(plugin_manager->type_plugin_mapping, zathura_type_plugin_mapping_t*, iter, mapping)
Moritz Lipp's avatar
Moritz Lipp committed
291 292 293 294
  if (g_content_type_equals(type, mapping->type)) {
    girara_list_iterator_free(iter);
    return false;
  }
Moritz Lipp's avatar
Moritz Lipp committed
295
  GIRARA_LIST_FOREACH_END(plugin_manager->type_plugin_mapping, zathura_type_plugin_mapping_t*, iter, mapping);
296 297

  zathura_type_plugin_mapping_t* mapping = g_malloc(sizeof(zathura_type_plugin_mapping_t));
Moritz Lipp's avatar
Moritz Lipp committed
298
  mapping->type   = g_strdup(type);
299
  mapping->plugin = plugin;
Moritz Lipp's avatar
Moritz Lipp committed
300 301
  girara_list_append(plugin_manager->type_plugin_mapping, mapping);

302 303 304
  return true;
}

Moritz Lipp's avatar
Moritz Lipp committed
305
static void
306 307 308 309 310 311 312 313 314 315
zathura_type_plugin_mapping_free(zathura_type_plugin_mapping_t* mapping)
{
  if (mapping == NULL) {
    return;
  }

  g_free((void*)mapping->type);
  g_free(mapping);
}

Moritz Lipp's avatar
Moritz Lipp committed
316 317 318 319 320 321 322
static void
zathura_plugin_free(zathura_plugin_t* plugin)
{
  if (plugin == NULL) {
    return;
  }

323 324 325 326 327 328 329 330
  if (plugin->name != NULL) {
    g_free(plugin->name);
  }

  if (plugin->path != NULL) {
    g_free(plugin->path);
  }

331
  g_module_close(plugin->handle);
Moritz Lipp's avatar
Moritz Lipp committed
332
  girara_list_free(plugin->content_types);
333

Moritz Lipp's avatar
Moritz Lipp committed
334 335 336 337
  g_free(plugin);
}

/* plugin-api.h */
338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354
void
zathura_plugin_set_register_functions_function(zathura_plugin_t* plugin, zathura_plugin_register_function_t register_function)
{
  if (plugin == NULL || register_function == NULL) {
    return;
  }

  plugin->register_function = register_function;
}

void
zathura_plugin_add_mimetype(zathura_plugin_t* plugin, const char* mime_type)
{
  if (plugin == NULL || mime_type == NULL) {
    return;
  }

Moritz Lipp's avatar
Moritz Lipp committed
355
  girara_list_append(plugin->content_types, g_content_type_from_mime_type(mime_type));
356
}
357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402

zathura_plugin_functions_t*
zathura_plugin_get_functions(zathura_plugin_t* plugin)
{
  if (plugin != NULL) {
    return &(plugin->functions);
  } else {
    return NULL;
  }
}

void
zathura_plugin_set_name(zathura_plugin_t* plugin, const char* name)
{
  if (plugin != NULL && name != NULL) {
    plugin->name = g_strdup(name);
  }
}

char*
zathura_plugin_get_name(zathura_plugin_t* plugin)
{
  if (plugin != NULL) {
    return plugin->name;
  } else {
    return NULL;
  }
}

char*
zathura_plugin_get_path(zathura_plugin_t* plugin)
{
  if (plugin != NULL) {
    return plugin->path;
  } else {
    return NULL;
  }
}

zathura_plugin_version_t
zathura_plugin_get_version(zathura_plugin_t* plugin)
{
  if (plugin != NULL) {
    return plugin->version;
  }

403
  zathura_plugin_version_t version = { 0, 0, 0 };
404 405
  return version;
}