main.c 5.11 KB
Newer Older
Moritz Lipp's avatar
Moritz Lipp committed
1
2
3
/* See LICENSE file for license and copyright information */

#include <stdio.h>
4
#include <stdlib.h>
Moritz Lipp's avatar
Moritz Lipp committed
5
#include <glib/gstdio.h>
6
#include <glib/gi18n.h>
7
#include <girara/utils.h>
Moritz Lipp's avatar
Moritz Lipp committed
8
#include <locale.h>
Sebastian Ramacher's avatar
Sebastian Ramacher committed
9
#include <unistd.h>
Moritz Lipp's avatar
Moritz Lipp committed
10
11

#include "zathura.h"
12
#include "utils.h"
Moritz Lipp's avatar
Moritz Lipp committed
13
14

/* main function */
15
16
int
main(int argc, char* argv[])
Moritz Lipp's avatar
Moritz Lipp committed
17
{
18
  /* init locale */
19
20
21
22
23
  setlocale(LC_ALL, "");
  bindtextdomain(GETTEXT_PACKAGE, LOCALEDIR);
  bind_textdomain_codeset(GETTEXT_PACKAGE, "UTF-8");
  textdomain(GETTEXT_PACKAGE);

24
  /* init gtk */
25
#if !GLIB_CHECK_VERSION(2, 31, 0)
Moritz Lipp's avatar
Moritz Lipp committed
26
  g_thread_init(NULL);
27
#endif
Moritz Lipp's avatar
Moritz Lipp committed
28
29
30
  gdk_threads_init();
  gtk_init(&argc, &argv);

31
32
  /* create zathura session */
  zathura_t* zathura = zathura_create();
Moritz Lipp's avatar
Moritz Lipp committed
33
34
35
36
  if (zathura == NULL) {
    return -1;
  }

37
38
39
40
41
42
43
44
45
  /* parse command line arguments */
  gchar* config_dir     = NULL;
  gchar* data_dir       = NULL;
  gchar* plugin_path    = NULL;
  gchar* loglevel       = NULL;
  gchar* password       = NULL;
  gchar* synctex_editor = NULL;
  bool forkback         = false;
  bool print_version    = false;
Moritz Lipp's avatar
Moritz Lipp committed
46
  bool synctex          = false;
47
  int page_number       = ZATHURA_PAGE_NUMBER_UNSPECIFIED;
48
49
50
51
52
53
54
55
56
57
58
59
60
61

#if (GTK_MAJOR_VERSION == 3)
  Window embed = 0;
#else
  GdkNativeWindow embed = 0;
#endif

  GOptionEntry entries[] = {
    { "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" },
    { "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" },
62
    { "page",                   'P', 0, G_OPTION_ARG_INT,      &page_number,    _("Page number to go to"),                              "number" },
63
64
    { "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 },
Moritz Lipp's avatar
Moritz Lipp committed
65
    { "synctex",                's', 0, G_OPTION_ARG_NONE,     &synctex,        _("Enable synctex support"),                            NULL },
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
    { "synctex-editor-command", 'x', 0, G_OPTION_ARG_STRING,   &synctex_editor, _("Synctex editor (forwarded to the synctex command)"), "cmd" },
    { 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);

  /* Fork into the background if the user really wants to ... */
  if (forkback == true) {
    int pid = fork();
    if (pid > 0) { /* parent */
      exit(0);
    } else if (pid < 0) { /* error */
      girara_error("Couldn't fork.");
    }

    setsid();
  }

  /* 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);
  }

  zathura_set_xid(zathura, embed);
  zathura_set_config_dir(zathura, config_dir);
  zathura_set_data_dir(zathura, data_dir);
  zathura_set_plugin_dir(zathura, plugin_path);
  zathura_set_synctex_editor_command(zathura, synctex_editor);
  zathura_set_argv(zathura, argv);

  /* Init zathura */
  if(zathura_init(zathura) == false) {
    girara_error("Could not initialize zathura.");
    zathura_free(zathura);
    return -1;
  }

Moritz Lipp's avatar
Moritz Lipp committed
118
  /* Enable/Disable synctex support */
119
  zathura_set_synctex(zathura, synctex);
Moritz Lipp's avatar
Moritz Lipp committed
120

121
122
123
124
125
126
127
128
129
130
131
132
133
  /* 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) {
134
135
136
    if (page_number > 0)
      --page_number;
    document_open_idle(zathura, argv[1], password, page_number);
137
138
139
140
141
142
143
144
145
146
147
148
149
150

    /* 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 */
Moritz Lipp's avatar
Moritz Lipp committed
151
152
153
154
  gdk_threads_enter();
  gtk_main();
  gdk_threads_leave();

155
  /* free zathura */
Moritz Lipp's avatar
Moritz Lipp committed
156
157
158
159
  zathura_free(zathura);

  return 0;
}