2012-01-28 19:54:10 +01:00
|
|
|
/* See LICENSE file for license and copyright information */
|
|
|
|
|
2012-08-05 15:34:10 +02:00
|
|
|
#include <girara/utils.h>
|
2014-07-26 16:14:29 +02:00
|
|
|
#include <girara/settings.h>
|
2013-12-27 20:40:06 +01:00
|
|
|
#include <glib/gi18n.h>
|
|
|
|
#include <glib/gstdio.h>
|
|
|
|
#include <limits.h>
|
2012-03-04 23:51:43 +01:00
|
|
|
#include <locale.h>
|
2013-12-27 20:40:06 +01:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2012-01-28 19:54:10 +01:00
|
|
|
|
|
|
|
#include "zathura.h"
|
2012-08-05 15:34:10 +02:00
|
|
|
#include "utils.h"
|
2014-01-12 22:23:29 +01:00
|
|
|
#include "dbus-interface.h"
|
2012-01-28 19:54:10 +01:00
|
|
|
|
|
|
|
/* main function */
|
2012-08-05 15:34:10 +02:00
|
|
|
int
|
|
|
|
main(int argc, char* argv[])
|
2012-01-28 19:54:10 +01:00
|
|
|
{
|
2012-08-05 15:34:10 +02:00
|
|
|
/* init locale */
|
2012-03-04 18:35:16 +01:00
|
|
|
setlocale(LC_ALL, "");
|
|
|
|
bindtextdomain(GETTEXT_PACKAGE, LOCALEDIR);
|
|
|
|
bind_textdomain_codeset(GETTEXT_PACKAGE, "UTF-8");
|
|
|
|
textdomain(GETTEXT_PACKAGE);
|
|
|
|
|
2012-08-05 15:34:10 +02:00
|
|
|
/* init gtk */
|
2013-02-08 19:05:36 +01:00
|
|
|
#if !GLIB_CHECK_VERSION(2, 31, 0)
|
2012-01-28 19:54:10 +01:00
|
|
|
g_thread_init(NULL);
|
2013-02-08 19:05:36 +01:00
|
|
|
#endif
|
2013-08-30 19:25:24 +02:00
|
|
|
#if !GTK_CHECK_VERSION(3, 6, 0)
|
2012-01-28 19:54:10 +01:00
|
|
|
gdk_threads_init();
|
2013-08-30 19:25:24 +02:00
|
|
|
#endif
|
2012-01-28 19:54:10 +01:00
|
|
|
gtk_init(&argc, &argv);
|
|
|
|
|
2012-08-05 15:34:10 +02:00
|
|
|
/* parse command line arguments */
|
|
|
|
gchar* config_dir = NULL;
|
|
|
|
gchar* data_dir = NULL;
|
2014-08-15 18:10:38 +02:00
|
|
|
gchar* cache_dir = NULL;
|
2012-08-05 15:34:10 +02:00
|
|
|
gchar* plugin_path = NULL;
|
|
|
|
gchar* loglevel = NULL;
|
|
|
|
gchar* password = NULL;
|
|
|
|
gchar* synctex_editor = NULL;
|
2013-12-24 01:05:40 +01:00
|
|
|
gchar* synctex_fwd = NULL;
|
2014-01-29 20:59:45 +01:00
|
|
|
gchar* mode = NULL;
|
2012-08-05 15:34:10 +02:00
|
|
|
bool forkback = false;
|
|
|
|
bool print_version = false;
|
2013-03-17 10:56:43 +01:00
|
|
|
int page_number = ZATHURA_PAGE_NUMBER_UNSPECIFIED;
|
2014-01-20 01:53:09 +01:00
|
|
|
int synctex_pid = -1;
|
2013-11-25 19:05:46 +01:00
|
|
|
Window embed = 0;
|
2012-08-05 15:34:10 +02:00
|
|
|
|
|
|
|
GOptionEntry entries[] = {
|
2013-12-24 01:05:40 +01:00
|
|
|
{ "reparent", 'e', 0, G_OPTION_ARG_INT, &embed, _("Reparents to window specified by xid"), "xid" },
|
|
|
|
{ "config-dir", 'c', 0, G_OPTION_ARG_FILENAME, &config_dir, _("Path to the config directory"), "path" },
|
|
|
|
{ "data-dir", 'd', 0, G_OPTION_ARG_FILENAME, &data_dir, _("Path to the data directory"), "path" },
|
2014-08-15 18:10:38 +02:00
|
|
|
{ "cache-dir", '\0', 0, G_OPTION_ARG_FILENAME, &cache_dir, _("Path to the cache directory"), "path"},
|
2013-12-24 01:05:40 +01:00
|
|
|
{ "plugins-dir", 'p', 0, G_OPTION_ARG_STRING, &plugin_path, _("Path to the directories containing plugins"), "path" },
|
|
|
|
{ "fork", '\0', 0, G_OPTION_ARG_NONE, &forkback, _("Fork into the background"), NULL },
|
|
|
|
{ "password", 'w', 0, G_OPTION_ARG_STRING, &password, _("Document password"), "password" },
|
|
|
|
{ "page", 'P', 0, G_OPTION_ARG_INT, &page_number, _("Page number to go to"), "number" },
|
|
|
|
{ "debug", 'l', 0, G_OPTION_ARG_STRING, &loglevel, _("Log level (debug, info, warning, error)"), "level" },
|
|
|
|
{ "version", 'v', 0, G_OPTION_ARG_NONE, &print_version, _("Print version information"), NULL },
|
|
|
|
{ "synctex-editor-command", 'x', 0, G_OPTION_ARG_STRING, &synctex_editor, _("Synctex editor (forwarded to the synctex command)"), "cmd" },
|
2013-12-27 21:15:05 +01:00
|
|
|
{ "synctex-forward", '\0', 0, G_OPTION_ARG_STRING, &synctex_fwd, _("Move to given synctex position"), "position" },
|
2014-01-20 01:53:09 +01:00
|
|
|
{ "synctex-pid", '\0', 0, G_OPTION_ARG_INT, &synctex_pid, _("Highlight given position in the given process"), "pid" },
|
2014-01-29 20:59:45 +01:00
|
|
|
{ "mode", '\0', 0, G_OPTION_ARG_STRING, &mode, _("Start in a non-default mode"), "mode" },
|
2012-08-05 15:34:10 +02:00
|
|
|
{ NULL, '\0', 0, 0, NULL, NULL, NULL }
|
|
|
|
};
|
|
|
|
|
|
|
|
GOptionContext* context = g_option_context_new(" [file1] [file2] [...]");
|
|
|
|
g_option_context_add_main_entries(context, entries, NULL);
|
|
|
|
|
|
|
|
GError* error = NULL;
|
|
|
|
if (g_option_context_parse(context, &argc, &argv, &error) == false) {
|
|
|
|
girara_error("Error parsing command line arguments: %s\n", error->message);
|
|
|
|
g_option_context_free(context);
|
|
|
|
g_error_free(error);
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
g_option_context_free(context);
|
|
|
|
|
2013-12-24 01:41:43 +01:00
|
|
|
/* Set log level. */
|
|
|
|
if (loglevel == NULL || g_strcmp0(loglevel, "info") == 0) {
|
|
|
|
girara_set_debug_level(GIRARA_INFO);
|
|
|
|
} else if (g_strcmp0(loglevel, "warning") == 0) {
|
|
|
|
girara_set_debug_level(GIRARA_WARNING);
|
|
|
|
} else if (g_strcmp0(loglevel, "error") == 0) {
|
|
|
|
girara_set_debug_level(GIRARA_ERROR);
|
|
|
|
}
|
|
|
|
|
2014-01-29 20:59:45 +01:00
|
|
|
/* handle synctex forward synchronization */
|
2013-12-24 01:05:40 +01:00
|
|
|
if (synctex_fwd != NULL) {
|
|
|
|
if (argc != 2) {
|
2014-01-05 14:57:48 +01:00
|
|
|
girara_error("Too many arguments or missing filename while running with --synctex-forward");
|
2013-12-24 01:05:40 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2014-08-15 18:34:21 +02:00
|
|
|
GFile* file = g_file_new_for_commandline_arg(argv[1]);
|
|
|
|
if (file == NULL) {
|
|
|
|
girara_error("Unable to handle argument '%s'.", argv[1]);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
char* real_path = g_file_get_path(file);
|
|
|
|
g_object_unref(file);
|
2013-12-27 20:40:06 +01:00
|
|
|
if (real_path == NULL) {
|
2014-08-15 18:34:21 +02:00
|
|
|
girara_error("Failed to determine path for '%s'", argv[1]);
|
2013-12-27 20:40:06 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2014-05-30 16:31:19 +02:00
|
|
|
char** split_fwd = g_strsplit(synctex_fwd, ":", 0);
|
|
|
|
if (split_fwd == NULL || split_fwd[0] == NULL || split_fwd[1] == NULL ||
|
|
|
|
split_fwd[2] == NULL || split_fwd[3] != NULL) {
|
|
|
|
girara_error("Failed to parse argument to --synctex-forward.");
|
2014-08-15 18:34:21 +02:00
|
|
|
g_free(real_path);
|
2014-05-30 16:31:19 +02:00
|
|
|
g_strfreev(split_fwd);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2014-10-21 21:17:59 +02:00
|
|
|
int line = MIN(INT_MAX, g_ascii_strtoll(split_fwd[0], NULL, 10));
|
|
|
|
int column = MIN(INT_MAX, g_ascii_strtoll(split_fwd[1], NULL, 10));
|
|
|
|
/* SyncTeX starts indexing at 1, but we use 0 */
|
|
|
|
if (line > 0) {
|
|
|
|
--line;
|
|
|
|
}
|
|
|
|
if (column > 0) {
|
|
|
|
--column;
|
|
|
|
}
|
|
|
|
|
2014-05-30 16:31:19 +02:00
|
|
|
const bool ret = zathura_dbus_synctex_position(real_path, split_fwd[2], line, column, synctex_pid);
|
|
|
|
g_strfreev(split_fwd);
|
|
|
|
|
2014-08-15 18:34:21 +02:00
|
|
|
if (ret == false) {
|
2014-05-30 16:31:19 +02:00
|
|
|
girara_error("Could not find open instance for '%s' or got no usable data from synctex.", real_path);
|
2013-12-24 01:05:40 +01:00
|
|
|
}
|
2014-08-15 18:34:21 +02:00
|
|
|
|
|
|
|
g_free(real_path);
|
|
|
|
return ret == true ? 0 : -1;
|
2013-12-24 01:05:40 +01:00
|
|
|
}
|
|
|
|
|
2014-01-29 20:59:45 +01:00
|
|
|
/* check mode */
|
|
|
|
if (mode != NULL && g_strcmp0(mode, "presentation") != 0 && g_strcmp0(mode, "fullscreen") != 0) {
|
|
|
|
girara_error("Invalid argument for --mode: %s", mode);
|
|
|
|
return -1;
|
|
|
|
}
|
2013-12-24 01:05:40 +01:00
|
|
|
|
2012-08-05 15:34:10 +02:00
|
|
|
/* Fork into the background if the user really wants to ... */
|
|
|
|
if (forkback == true) {
|
2013-12-24 01:05:40 +01:00
|
|
|
const int pid = fork();
|
2012-08-05 15:34:10 +02:00
|
|
|
if (pid > 0) { /* parent */
|
2013-12-24 01:05:40 +01:00
|
|
|
return 0;
|
2012-08-05 15:34:10 +02:00
|
|
|
} else if (pid < 0) { /* error */
|
|
|
|
girara_error("Couldn't fork.");
|
|
|
|
}
|
|
|
|
|
|
|
|
setsid();
|
|
|
|
}
|
|
|
|
|
2014-01-19 17:17:14 +01:00
|
|
|
/* create zathura session */
|
|
|
|
zathura_t* zathura = zathura_create();
|
|
|
|
if (zathura == NULL) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2012-08-05 15:34:10 +02:00
|
|
|
zathura_set_xid(zathura, embed);
|
|
|
|
zathura_set_config_dir(zathura, config_dir);
|
|
|
|
zathura_set_data_dir(zathura, data_dir);
|
2014-08-15 18:10:38 +02:00
|
|
|
zathura_set_cache_dir(zathura, cache_dir);
|
2012-08-05 15:34:10 +02:00
|
|
|
zathura_set_plugin_dir(zathura, plugin_path);
|
|
|
|
zathura_set_argv(zathura, argv);
|
|
|
|
|
|
|
|
/* Init zathura */
|
2013-11-04 19:57:23 +01:00
|
|
|
if (zathura_init(zathura) == false) {
|
2012-08-05 15:34:10 +02:00
|
|
|
girara_error("Could not initialize zathura.");
|
|
|
|
zathura_free(zathura);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2014-07-26 16:14:29 +02:00
|
|
|
if (synctex_editor != NULL) {
|
|
|
|
girara_setting_set(zathura->ui.session, "synctex-editor-command", synctex_editor);
|
|
|
|
}
|
2012-08-13 18:17:20 +02:00
|
|
|
|
2012-08-05 15:34:10 +02:00
|
|
|
/* Print version */
|
|
|
|
if (print_version == true) {
|
|
|
|
char* string = zathura_get_version_string(zathura, false);
|
|
|
|
if (string != NULL) {
|
|
|
|
fprintf(stdout, "%s\n", string);
|
|
|
|
}
|
|
|
|
zathura_free(zathura);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* open document if passed */
|
|
|
|
if (argc > 1) {
|
2013-03-17 10:56:43 +01:00
|
|
|
if (page_number > 0)
|
|
|
|
--page_number;
|
2014-01-29 20:59:45 +01:00
|
|
|
document_open_idle(zathura, argv[1], password, page_number, mode);
|
2012-08-05 15:34:10 +02:00
|
|
|
|
|
|
|
/* open additional files */
|
|
|
|
for (int i = 2; i < argc; i++) {
|
|
|
|
char* new_argv[] = {
|
|
|
|
*(zathura->global.arguments),
|
|
|
|
argv[i],
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
|
|
|
g_spawn_async(NULL, new_argv, NULL, G_SPAWN_SEARCH_PATH, NULL, NULL, NULL, NULL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* run zathura */
|
2013-08-30 19:25:24 +02:00
|
|
|
#if !GTK_CHECK_VERSION(3, 6, 0)
|
2012-01-28 19:54:10 +01:00
|
|
|
gdk_threads_enter();
|
2013-08-30 19:25:24 +02:00
|
|
|
#endif
|
2012-01-28 19:54:10 +01:00
|
|
|
gtk_main();
|
2013-08-30 19:25:24 +02:00
|
|
|
#if !GTK_CHECK_VERSION(3, 6, 0)
|
2012-01-28 19:54:10 +01:00
|
|
|
gdk_threads_leave();
|
2013-08-30 19:25:24 +02:00
|
|
|
#endif
|
2012-01-28 19:54:10 +01:00
|
|
|
|
2012-08-05 15:34:10 +02:00
|
|
|
/* free zathura */
|
2012-01-28 19:54:10 +01:00
|
|
|
zathura_free(zathura);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|