zathura.c 5.78 KB
Newer Older
1
2
/* See LICENSE file for license and copyright information */

Moritz Lipp's avatar
Moritz Lipp committed
3
4
#include <stdlib.h>

Moritz Lipp's avatar
Moritz Lipp committed
5
6
#include "callbacks.h"
#include "config.h"
7
#include "document.h"
Moritz Lipp's avatar
Moritz Lipp committed
8
#include "shortcuts.h"
Moritz Lipp's avatar
Moritz Lipp committed
9
#include "zathura.h"
Moritz Lipp's avatar
Moritz Lipp committed
10
#include "utils.h"
Moritz Lipp's avatar
Moritz Lipp committed
11

Moritz Lipp's avatar
Moritz Lipp committed
12
13
14
/* function implementation */
bool
init_zathura()
15
{
16
  /* UI */
Moritz Lipp's avatar
Moritz Lipp committed
17
  if (!(Zathura.UI.session = girara_session_create())) {
Moritz Lipp's avatar
Moritz Lipp committed
18
    goto error_out;
Moritz Lipp's avatar
Moritz Lipp committed
19
  }
20

Moritz Lipp's avatar
Moritz Lipp committed
21
  if (!girara_session_init(Zathura.UI.session)) {
Moritz Lipp's avatar
Moritz Lipp committed
22
    goto error_out;
Moritz Lipp's avatar
Moritz Lipp committed
23
  }
24

Moritz Lipp's avatar
Moritz Lipp committed
25
26
27
  Zathura.UI.statusbar.file        = NULL;
  Zathura.UI.statusbar.buffer      = NULL;
  Zathura.UI.statusbar.page_number = NULL;
28
  Zathura.UI.page_view             = NULL;
29
  Zathura.UI.index                 = NULL;
30
31
32

  /* page view */
  Zathura.UI.page_view = gtk_vbox_new(FALSE, 0);
Moritz Lipp's avatar
Moritz Lipp committed
33
  if (!Zathura.UI.page_view) {
34
35
36
37
38
    goto error_free;
  }

  gtk_widget_show(Zathura.UI.page_view);
  gtk_box_set_spacing(GTK_BOX(Zathura.UI.page_view), 0);
Moritz Lipp's avatar
Moritz Lipp committed
39

40
  /* statusbar */
Moritz Lipp's avatar
Moritz Lipp committed
41
  Zathura.UI.statusbar.file = girara_statusbar_item_add(Zathura.UI.session, TRUE, TRUE, TRUE, NULL);
Moritz Lipp's avatar
Moritz Lipp committed
42
  if (!Zathura.UI.statusbar.file) {
Moritz Lipp's avatar
Moritz Lipp committed
43
    goto error_free;
Moritz Lipp's avatar
Moritz Lipp committed
44
45
  }

Moritz Lipp's avatar
Moritz Lipp committed
46
  Zathura.UI.statusbar.buffer = girara_statusbar_item_add(Zathura.UI.session, FALSE, FALSE, FALSE, NULL);
Moritz Lipp's avatar
Moritz Lipp committed
47
  if (!Zathura.UI.statusbar.buffer) {
Moritz Lipp's avatar
Moritz Lipp committed
48
    goto error_free;
Moritz Lipp's avatar
Moritz Lipp committed
49
50
51
  }

  Zathura.UI.statusbar.page_number = girara_statusbar_item_add(Zathura.UI.session, FALSE, FALSE, FALSE, NULL);
Moritz Lipp's avatar
Moritz Lipp committed
52
  if (!Zathura.UI.statusbar.page_number) {
Moritz Lipp's avatar
Moritz Lipp committed
53
    goto error_free;
Moritz Lipp's avatar
Moritz Lipp committed
54
55
56
57
  }

  girara_statusbar_item_set_text(Zathura.UI.session, Zathura.UI.statusbar.file, "[No Name]");

Moritz Lipp's avatar
Moritz Lipp committed
58
59
60
  /* signals */
  g_signal_connect(G_OBJECT(Zathura.UI.session->gtk.window), "destroy", G_CALLBACK(cb_destroy), NULL);

61
62
63
  GtkAdjustment* view_vadjustment = gtk_scrolled_window_get_vadjustment(GTK_SCROLLED_WINDOW(Zathura.UI.session->gtk.view));
  g_signal_connect(G_OBJECT(view_vadjustment), "value-changed", G_CALLBACK(cb_view_vadjustment_value_changed), NULL);

Moritz Lipp's avatar
Moritz Lipp committed
64
65
66
  /* girara events */
  Zathura.UI.session->events.buffer_changed = buffer_changed;

Moritz Lipp's avatar
Moritz Lipp committed
67
68
69
  /* load plugins */
  zathura_document_plugins_load();

Moritz Lipp's avatar
Moritz Lipp committed
70
71
72
  /* configuration */
  config_load_default();

Moritz Lipp's avatar
Moritz Lipp committed
73
  return true;
Moritz Lipp's avatar
Moritz Lipp committed
74
75
76

error_free:

Moritz Lipp's avatar
Moritz Lipp committed
77
  if (Zathura.UI.page_view) {
78
79
80
    g_object_unref(Zathura.UI.page_view);
  }

Moritz Lipp's avatar
Moritz Lipp committed
81
82
83
84
85
  girara_session_destroy(Zathura.UI.session);

error_out:

  return false;
86
87
}

Moritz Lipp's avatar
Moritz Lipp committed
88
89
90
bool
document_open(const char* path, const char* password)
{
Moritz Lipp's avatar
Moritz Lipp committed
91
  if (!path) {
92
    goto error_out;
Moritz Lipp's avatar
Moritz Lipp committed
93
94
95
96
  }

  zathura_document_t* document = zathura_document_open(path, password);

Moritz Lipp's avatar
Moritz Lipp committed
97
  if (!document) {
98
    goto error_out;
Moritz Lipp's avatar
Moritz Lipp committed
99
100
101
102
  }

  Zathura.document = document;

103
104
  /* init view */
  create_blank_pages();
Moritz Lipp's avatar
Moritz Lipp committed
105

106
107
108
109
110
  /* view mode */
  int* value = girara_setting_get(Zathura.UI.session, "pages-per-row");
  int pages_per_row = (value) ? *value : 1;
  free(value);
  page_view_set_mode(pages_per_row);
Moritz Lipp's avatar
Moritz Lipp committed
111

112
  girara_set_view(Zathura.UI.session, Zathura.UI.page_view);
Moritz Lipp's avatar
Moritz Lipp committed
113

Moritz Lipp's avatar
Moritz Lipp committed
114
115
116
  /* threads */
  Zathura.Sync.render_thread = render_init();

Moritz Lipp's avatar
Moritz Lipp committed
117
  if (!Zathura.Sync.render_thread) {
Moritz Lipp's avatar
Moritz Lipp committed
118
119
120
    goto error_free;
  }

121
  /* first page */
Moritz Lipp's avatar
Moritz Lipp committed
122
  if (!page_set(0)) {
123
124
    goto error_free;
  }
Moritz Lipp's avatar
Moritz Lipp committed
125
126

  return true;
127
128
129
130
131
132
133
134

error_free:

  zathura_document_free(document);

error_out:

  return false;
Moritz Lipp's avatar
Moritz Lipp committed
135
136
137
138
139
}

bool
document_close()
{
Moritz Lipp's avatar
Moritz Lipp committed
140
  if (!Zathura.document) {
Moritz Lipp's avatar
Moritz Lipp committed
141
142
143
    return false;
  }

Moritz Lipp's avatar
Moritz Lipp committed
144
  if (Zathura.Sync.render_thread) {
Moritz Lipp's avatar
Moritz Lipp committed
145
146
147
    render_free(Zathura.Sync.render_thread);
  }

Moritz Lipp's avatar
Moritz Lipp committed
148
149
150
151
152
153
154
155
  zathura_document_free(Zathura.document);

  return true;
}

bool
page_set(unsigned int page_id)
{
Moritz Lipp's avatar
Moritz Lipp committed
156
  if (!Zathura.document || !Zathura.document->pages) {
Moritz Lipp's avatar
Moritz Lipp committed
157
    goto error_out;
Moritz Lipp's avatar
Moritz Lipp committed
158
159
  }

Moritz Lipp's avatar
Moritz Lipp committed
160
  if (page_id >= Zathura.document->number_of_pages) {
Moritz Lipp's avatar
Moritz Lipp committed
161
    goto error_out;
Moritz Lipp's avatar
Moritz Lipp committed
162
163
  }

164
  /* render page */
165
  zathura_page_t* page = Zathura.document->pages[page_id];
Moritz Lipp's avatar
Moritz Lipp committed
166

Moritz Lipp's avatar
Moritz Lipp committed
167
  if (!page) {
168
169
170
    goto error_out;
  }

171
  GtkAdjustment* view_vadjustment = gtk_scrolled_window_get_vadjustment(GTK_SCROLLED_WINDOW(Zathura.UI.session->gtk.view));
Moritz Lipp's avatar
Moritz Lipp committed
172
  cb_view_vadjustment_value_changed(view_vadjustment, NULL);
173
174

  /* update page number */
Moritz Lipp's avatar
Moritz Lipp committed
175
176
177
178
179
180
181
  Zathura.document->current_page_number = page_id;

  char* page_number = g_strdup_printf("[%d/%d]", page_id + 1, Zathura.document->number_of_pages);
  girara_statusbar_item_set_text(Zathura.UI.session, Zathura.UI.statusbar.page_number, page_number);
  g_free(page_number);

  return true;
Moritz Lipp's avatar
Moritz Lipp committed
182
183
184
185

error_out:

  return false;
Moritz Lipp's avatar
Moritz Lipp committed
186
187
}

188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
void
page_view_set_mode(unsigned int pages_per_row)
{
  /* empty page view */
  GList* container = gtk_container_get_children(GTK_CONTAINER(Zathura.UI.page_view));
  for (GList* child = container; child; child = g_list_next(child)) {
    gtk_container_remove(GTK_CONTAINER(Zathura.UI.page_view), child->data);
  }

  GtkWidget* row = NULL;

  /* create blank pages */
  for (unsigned int i = 0; i < Zathura.document->number_of_pages; i++)
  {
    if (i % pages_per_row == 0) {
      row = gtk_hbox_new(FALSE, 0);
    }

    /* pack row */
    gtk_box_pack_start(GTK_BOX(row), Zathura.document->pages[i]->event_box, FALSE, FALSE, 1);

    /* pack row to page view */
    if ((i + 1) % pages_per_row == 0) {
      gtk_box_pack_start(GTK_BOX(Zathura.UI.page_view), row, FALSE,  FALSE, 1);
    }
  }

  gtk_widget_show_all(Zathura.UI.page_view);
}

void
create_blank_pages()
{
  /* create blank pages */
  for (unsigned int i = 0; i < Zathura.document->number_of_pages; i++)
  {
    zathura_page_t* page = Zathura.document->pages[i];
    g_static_mutex_lock(&(page->lock));

Moritz Lipp's avatar
Moritz Lipp committed
227
228
229
230
231
    cairo_t* cairo = gdk_cairo_create(page->drawing_area->window);
    cairo_set_source_rgb(cairo, 1, 1, 1);
    cairo_rectangle(cairo, 0, 0, page->width, page->height);
    cairo_fill(cairo);
    cairo_destroy(cairo);
232
233
234
235
236

    g_static_mutex_unlock(&(page->lock));
  }
}

Moritz Lipp's avatar
Moritz Lipp committed
237
/* main function */
238
int main(int argc, char* argv[])
Moritz Lipp's avatar
Moritz Lipp committed
239
{
240
241
  g_thread_init(NULL);
  gdk_threads_init();
Moritz Lipp's avatar
Moritz Lipp committed
242
243
  gtk_init(&argc, &argv);

Moritz Lipp's avatar
Moritz Lipp committed
244
  if (!init_zathura()) {
Moritz Lipp's avatar
Moritz Lipp committed
245
246
247
    printf("error: coult not initialize zathura\n");
    return -1;
  }
Moritz Lipp's avatar
Moritz Lipp committed
248

Moritz Lipp's avatar
Moritz Lipp committed
249
250
  if (argc > 1) {
    if (!document_open(argv[1], NULL)) {
Moritz Lipp's avatar
Moritz Lipp committed
251
      printf("error: could not open document\n");
Moritz Lipp's avatar
Moritz Lipp committed
252
253
      return -1;
    }
254
  }
Moritz Lipp's avatar
Moritz Lipp committed
255

Moritz Lipp's avatar
Moritz Lipp committed
256
257
258
  gdk_threads_enter();
  gtk_main();
  gdk_threads_leave();
259

Moritz Lipp's avatar
Moritz Lipp committed
260
261
  return 0;
}