main.c 9.6 KB
Newer Older
1
/* SPDX-License-Identifier: Zlib */
2

3
#include <girara/settings.h>
Sebastian Ramacher's avatar
Sebastian Ramacher committed
4 5
#include <girara/log.h>

6 7
#include <glib/gi18n.h>
#include <glib/gstdio.h>
8
#include <errno.h>
9
#include <limits.h>
Moritz Lipp's avatar
Moritz Lipp committed
10
#include <locale.h>
11 12 13
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
14 15

#include "zathura.h"
16
#include "utils.h"
Sebastian Ramacher's avatar
Sebastian Ramacher committed
17
#include "dbus-interface.h"
18
#ifdef WITH_SYNCTEX
19
#include "synctex.h"
20
#endif
valoq's avatar
valoq committed
21

Sebastian Ramacher's avatar
Sebastian Ramacher committed
22 23 24
/* Init locale */
static void
init_locale(void)
25
{
26 27 28 29
  setlocale(LC_ALL, "");
  bindtextdomain(GETTEXT_PACKAGE, LOCALEDIR);
  bind_textdomain_codeset(GETTEXT_PACKAGE, "UTF-8");
  textdomain(GETTEXT_PACKAGE);
Sebastian Ramacher's avatar
Sebastian Ramacher committed
30 31 32 33 34 35 36
}

/* Set log level */
static void
set_log_level(const char* loglevel)
{
  if (loglevel == NULL || g_strcmp0(loglevel, "info") == 0) {
Sebastian Ramacher's avatar
Sebastian Ramacher committed
37
    girara_set_log_level(GIRARA_INFO);
Sebastian Ramacher's avatar
Sebastian Ramacher committed
38
  } else if (g_strcmp0(loglevel, "warning") == 0) {
Sebastian Ramacher's avatar
Sebastian Ramacher committed
39
    girara_set_log_level(GIRARA_WARNING);
Sebastian Ramacher's avatar
Sebastian Ramacher committed
40
  } else if (g_strcmp0(loglevel, "error") == 0) {
Sebastian Ramacher's avatar
Sebastian Ramacher committed
41
    girara_set_log_level(GIRARA_ERROR);
Sebastian Ramacher's avatar
Sebastian Ramacher committed
42 43 44 45 46 47 48
  }
}

/* Handle synctex forward synchronization */
#ifdef WITH_SYNCTEX
static int
run_synctex_forward(const char* synctex_fwd, const char* filename,
49
                    int synctex_pid)
Sebastian Ramacher's avatar
Sebastian Ramacher committed
50 51 52 53 54 55 56 57 58 59 60 61 62 63
{
  GFile* file = g_file_new_for_commandline_arg(filename);
  if (file == NULL) {
    girara_error("Unable to handle argument '%s'.", filename);
    return -1;
  }

  char* real_path = g_file_get_path(file);
  g_object_unref(file);
  if (real_path == NULL) {
    girara_error("Failed to determine path for '%s'", filename);
    return -1;
  }

64 65
  int   line       = 0;
  int   column     = 0;
Sebastian Ramacher's avatar
Sebastian Ramacher committed
66 67 68 69 70 71 72
  char* input_file = NULL;
  if (synctex_parse_input(synctex_fwd, &input_file, &line, &column) == false) {
    girara_error("Failed to parse argument to --synctex-forward.");
    g_free(real_path);
    return -1;
  }

73 74
  const int ret = zathura_dbus_synctex_position(real_path, input_file, line,
                                                column, synctex_pid);
Sebastian Ramacher's avatar
Sebastian Ramacher committed
75 76 77 78 79
  g_free(input_file);
  g_free(real_path);

  if (ret == -1) {
    /* D-Bus or SyncTeX failed */
80 81
    girara_error(
      "Got no usable data from SyncTeX or D-Bus failed in some way.");
Sebastian Ramacher's avatar
Sebastian Ramacher committed
82 83 84 85 86 87 88 89
  }

  return ret;
}
#endif

static zathura_t*
init_zathura(const char* config_dir, const char* data_dir,
90
             const char* cache_dir, const char* plugin_path, char** argv,
Sebastian Ramacher's avatar
Sebastian Ramacher committed
91
             const char* synctex_editor, Window embed)
Sebastian Ramacher's avatar
Sebastian Ramacher committed
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
{
  /* create zathura session */
  zathura_t* zathura = zathura_create();
  if (zathura == NULL) {
    return NULL;
  }

  zathura_set_xid(zathura, embed);
  zathura_set_config_dir(zathura, config_dir);
  zathura_set_data_dir(zathura, data_dir);
  zathura_set_cache_dir(zathura, cache_dir);
  zathura_set_plugin_dir(zathura, plugin_path);
  zathura_set_argv(zathura, argv);

  /* Init zathura */
  if (zathura_init(zathura) == false) {
    zathura_free(zathura);
    return NULL;
  }

  if (synctex_editor != NULL) {
113 114
    girara_setting_set(zathura->ui.session, "synctex-editor-command",
                       synctex_editor);
Sebastian Ramacher's avatar
Sebastian Ramacher committed
115 116 117 118 119 120 121
  }

  return zathura;
}


/* main function */
122
GIRARA_VISIBLE int
Sebastian Ramacher's avatar
Sebastian Ramacher committed
123 124
main(int argc, char* argv[])
{
valoq's avatar
valoq committed
125

Sebastian Ramacher's avatar
Sebastian Ramacher committed
126
  init_locale();
127

128 129 130
  /* parse command line arguments */
  gchar* config_dir     = NULL;
  gchar* data_dir       = NULL;
Sebastian Ramacher's avatar
Sebastian Ramacher committed
131
  gchar* cache_dir      = NULL;
132 133 134 135
  gchar* plugin_path    = NULL;
  gchar* loglevel       = NULL;
  gchar* password       = NULL;
  gchar* synctex_editor = NULL;
136
  gchar* synctex_fwd    = NULL;
Sebastian Ramacher's avatar
Sebastian Ramacher committed
137
  gchar* mode           = NULL;
138 139 140
  bool   forkback       = false;
  bool   print_version  = false;
  int    page_number    = ZATHURA_PAGE_NUMBER_UNSPECIFIED;
141
#ifdef WITH_SYNCTEX
142
  int    synctex_pid    = -1;
143
#endif
Sebastian Ramacher's avatar
Sebastian Ramacher committed
144
  Window embed          = 0;
145 146

  GOptionEntry entries[] = {
Sebastian Ramacher's avatar
Sebastian Ramacher committed
147
    { "reparent",               'e',  0, G_OPTION_ARG_INT,      &embed,          _("Reparents to window specified by xid (X11)"),        "xid"  },
148 149
    { "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" },
Sebastian Ramacher's avatar
Sebastian Ramacher committed
150
    { "cache-dir",              '\0', 0, G_OPTION_ARG_FILENAME, &cache_dir,      _("Path to the cache directory"),                       "path"},
151 152 153 154
    { "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" },
155
    { "log-level",              'l',  0, G_OPTION_ARG_STRING,   &loglevel,       _("Log level (debug, info, warning, error)"),           "level" },
156
    { "version",                'v',  0, G_OPTION_ARG_NONE,     &print_version,  _("Print version information"),                         NULL },
157
#ifdef WITH_SYNCTEX
158
    { "synctex-editor-command", 'x',  0, G_OPTION_ARG_STRING,   &synctex_editor, _("Synctex editor (forwarded to the synctex command)"), "cmd" },
Sebastian Ramacher's avatar
Sebastian Ramacher committed
159
    { "synctex-forward",        '\0', 0, G_OPTION_ARG_STRING,   &synctex_fwd,    _("Move to given synctex position"),                    "position" },
160
    { "synctex-pid",            '\0', 0, G_OPTION_ARG_INT,      &synctex_pid,    _("Highlight given position in the given process"),     "pid" },
161
#endif
Sebastian Ramacher's avatar
Sebastian Ramacher committed
162
    { "mode",                   '\0', 0, G_OPTION_ARG_STRING,   &mode,           _("Start in a non-default mode"),                       "mode" },
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
    { 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);

Sebastian Ramacher's avatar
Sebastian Ramacher committed
179
  int ret = 0;
Sebastian Ramacher's avatar
Sebastian Ramacher committed
180
  set_log_level(loglevel);
Sebastian Ramacher's avatar
Sebastian Ramacher committed
181

182
#ifdef WITH_SYNCTEX
Sebastian Ramacher's avatar
Sebastian Ramacher committed
183
  /* handle synctex forward synchronization */
184 185
  if (synctex_fwd != NULL) {
    if (argc != 2) {
186 187
      girara_error("Too many arguments or missing filename while running with "
                   "--synctex-forward");
Sebastian Ramacher's avatar
Sebastian Ramacher committed
188 189
      ret = -1;
      goto free_and_ret;
190 191
    }

Sebastian Ramacher's avatar
Sebastian Ramacher committed
192
    ret = run_synctex_forward(synctex_fwd, argv[1], synctex_pid);
193 194
    if (ret > 0) {
      /* Instance found. */
Sebastian Ramacher's avatar
Sebastian Ramacher committed
195 196
      ret = 0;
      goto free_and_ret;
197 198 199
    }
    else if (ret < 0) {
      /* Error occurred. */
Sebastian Ramacher's avatar
Sebastian Ramacher committed
200 201
      ret = -1;
      goto free_and_ret;
202
    }
203

204
    girara_debug("No instance found. Starting new one.");
205
  }
206
#endif
207

Sebastian Ramacher's avatar
Sebastian Ramacher committed
208
  /* check mode */
209 210
  if (mode != NULL && g_strcmp0(mode, "presentation") != 0 &&
      g_strcmp0(mode, "fullscreen") != 0) {
Sebastian Ramacher's avatar
Sebastian Ramacher committed
211
    girara_error("Invalid argument for --mode: %s", mode);
Sebastian Ramacher's avatar
Sebastian Ramacher committed
212 213
    ret = -1;
    goto free_and_ret;
Sebastian Ramacher's avatar
Sebastian Ramacher committed
214
  }
215

216 217 218 219 220 221 222
  /* g_option_context_parse has some funny (documented) behavior:
   * * for "-- a b c" you get no -- in argv
   * * for "-- --" you get -- in argv twice
   * * for "-- -a" you get -- in argv
   *
   * So if there is one -- in argv, we need to ignore it. */
  const bool has_double_dash = argc > 1 && g_strcmp0(argv[1], "--") == 0;
223
  const int  file_idx_base   = has_double_dash ? 2 : 1;
224 225

  int file_idx = argc > file_idx_base ? file_idx_base : 0;
226
  /* Fork instances for other files. */
227 228
  if (print_version == false && argc > file_idx_base + 1) {
    for (int idx = file_idx_base + 1; idx < argc; ++idx) {
229 230 231 232
      const pid_t pid = fork();
      if (pid == 0) { /* child */
        file_idx = idx;
        if (setsid() == -1) {
233 234
          girara_error("Could not start new process group: %s",
                       strerror(errno));
Sebastian Ramacher's avatar
Sebastian Ramacher committed
235 236
          ret = -1;
          goto free_and_ret;
237
        }
238
        break;
239 240
      }
      else if (pid < 0) { /* error */
241
        girara_error("Could not fork: %s", strerror(errno));
Sebastian Ramacher's avatar
Sebastian Ramacher committed
242 243
        ret = -1;
        goto free_and_ret;
244 245 246 247
      }
    }
  }

248
  /* Fork into the background if the user really wants to ... */
249 250
  if (print_version == false && forkback == true &&
      file_idx < file_idx_base + 1) {
Sebastian Ramacher's avatar
Sebastian Ramacher committed
251
    const pid_t pid = fork();
252
    if (pid > 0) { /* parent */
Sebastian Ramacher's avatar
Sebastian Ramacher committed
253
      goto free_and_ret;
254 255
    }
    else if (pid < 0) { /* error */
256
      girara_error("Could not fork: %s", strerror(errno));
Sebastian Ramacher's avatar
Sebastian Ramacher committed
257 258
      ret = -1;
      goto free_and_ret;
259 260
    }

261 262
    if (setsid() == -1) {
      girara_error("Could not start new process group: %s", strerror(errno));
Sebastian Ramacher's avatar
Sebastian Ramacher committed
263 264
      ret = -1;
      goto free_and_ret;
265
    }
266 267
  }

268
  /* Initialize GTK+ */
269 270
  gtk_init(&argc, &argv);

271
  /* Create zathura session */
Sebastian Ramacher's avatar
Sebastian Ramacher committed
272
  zathura_t* zathura = init_zathura(config_dir, data_dir, cache_dir,
273
                                    plugin_path, argv, synctex_editor, embed);
Sebastian Ramacher's avatar
Sebastian Ramacher committed
274
  if (zathura == NULL) {
275
    girara_error("Could not initialize zathura.");
Sebastian Ramacher's avatar
Sebastian Ramacher committed
276 277
    ret = -1;
    goto free_and_ret;
278 279 280 281 282 283 284
  }

  /* Print version */
  if (print_version == true) {
    char* string = zathura_get_version_string(zathura, false);
    if (string != NULL) {
      fprintf(stdout, "%s\n", string);
Sebastian Ramacher's avatar
Sebastian Ramacher committed
285
      g_free(string);
286 287 288
    }
    zathura_free(zathura);

Sebastian Ramacher's avatar
Sebastian Ramacher committed
289
    goto free_and_ret;
290 291 292
  }

  /* open document if passed */
293
  if (file_idx != 0) {
Sebastian Ramacher's avatar
CS  
Sebastian Ramacher committed
294
    if (page_number > 0) {
295
      --page_number;
Sebastian Ramacher's avatar
CS  
Sebastian Ramacher committed
296
    }
297 298
    document_open_idle(zathura, argv[file_idx], password, page_number, mode,
                       synctex_fwd);
299 300 301
  }

  /* run zathura */
302 303
  gtk_main();

304
  /* free zathura */
305 306
  zathura_free(zathura);

Sebastian Ramacher's avatar
Sebastian Ramacher committed
307 308 309 310 311 312 313 314 315 316 317 318
free_and_ret:
  g_free(config_dir);
  g_free(data_dir);
  g_free(cache_dir);
  g_free(plugin_path);
  g_free(loglevel);
  g_free(password);
  g_free(synctex_editor);
  g_free(synctex_fwd);
  g_free(mode);

  return ret;
319
}