zathura/zathura.c

228 lines
5.0 KiB
C
Raw Normal View History

/* See LICENSE file for license and copyright information */
2010-12-28 09:47:09 +01:00
#include <stdlib.h>
2010-11-12 13:48:18 +01:00
#include "callbacks.h"
#include "config.h"
#include "document.h"
2010-11-12 13:48:18 +01:00
#include "shortcuts.h"
2010-11-10 19:18:01 +01:00
#include "zathura.h"
2010-12-28 09:47:09 +01:00
#include "utils.h"
2009-12-26 17:57:46 +01:00
2010-11-10 19:18:01 +01:00
/* function implementation */
bool
init_zathura()
2010-06-03 18:05:34 +02:00
{
/* UI */
2011-02-09 12:29:09 +01:00
if (!(Zathura.UI.session = girara_session_create())) {
2010-12-26 01:52:17 +01:00
goto error_out;
2010-11-17 22:51:15 +01:00
}
2010-06-03 18:05:34 +02:00
2011-02-09 12:29:09 +01:00
if (!girara_session_init(Zathura.UI.session)) {
2010-12-26 01:52:17 +01:00
goto error_out;
2010-11-17 22:51:15 +01:00
}
2010-06-03 18:05:34 +02:00
2010-12-26 01:52:17 +01:00
Zathura.UI.statusbar.file = NULL;
Zathura.UI.statusbar.buffer = NULL;
Zathura.UI.statusbar.page_number = NULL;
2010-12-29 11:46:13 +01:00
Zathura.UI.page_view = NULL;
2011-02-10 04:33:28 +01:00
Zathura.UI.index = NULL;
2010-12-29 11:46:13 +01:00
/* page view */
Zathura.UI.page_view = gtk_vbox_new(FALSE, 0);
2011-02-09 12:29:09 +01:00
if (!Zathura.UI.page_view) {
2010-12-29 11:46:13 +01:00
goto error_free;
}
gtk_widget_show(Zathura.UI.page_view);
gtk_box_set_spacing(GTK_BOX(Zathura.UI.page_view), 0);
2010-12-26 01:52:17 +01:00
/* statusbar */
2010-11-18 21:22:43 +01:00
Zathura.UI.statusbar.file = girara_statusbar_item_add(Zathura.UI.session, TRUE, TRUE, TRUE, NULL);
2011-02-09 12:29:09 +01:00
if (!Zathura.UI.statusbar.file) {
2010-12-26 01:52:17 +01:00
goto error_free;
2010-11-13 12:40:48 +01:00
}
2010-11-18 21:22:43 +01:00
Zathura.UI.statusbar.buffer = girara_statusbar_item_add(Zathura.UI.session, FALSE, FALSE, FALSE, NULL);
2011-02-09 12:29:09 +01:00
if (!Zathura.UI.statusbar.buffer) {
2010-12-26 01:52:17 +01:00
goto error_free;
2010-11-18 21:22:43 +01:00
}
Zathura.UI.statusbar.page_number = girara_statusbar_item_add(Zathura.UI.session, FALSE, FALSE, FALSE, NULL);
2011-02-09 12:29:09 +01:00
if (!Zathura.UI.statusbar.page_number) {
2010-12-26 01:52:17 +01:00
goto error_free;
2010-11-18 21:22:43 +01:00
}
girara_statusbar_item_set_text(Zathura.UI.session, Zathura.UI.statusbar.file, "[No Name]");
2010-11-12 13:48:18 +01:00
/* signals */
g_signal_connect(G_OBJECT(Zathura.UI.session->gtk.window), "destroy", G_CALLBACK(cb_destroy), NULL);
2010-12-29 11:46:13 +01:00
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);
2010-11-13 12:40:48 +01:00
/* girara events */
Zathura.UI.session->events.buffer_changed = buffer_changed;
2011-03-05 21:00:41 +01:00
/* load plugins */
zathura_document_plugins_load();
2010-11-12 13:48:18 +01:00
/* configuration */
config_load_default();
2011-04-01 09:55:37 +02:00
/* load global configuration files */
config_load_file(GLOBAL_RC);
/* load local configuration files */
char* config_dir = girara_setting_get(Zathura.UI.session, "config-dir");
char* configuration_file = g_build_filename(config_dir ? config_dir : CONFIG_DIR, ZATHURA_RC, NULL);
config_load_file(configuration_file);
free(config_dir);
free(configuration_file);
2010-11-10 19:18:01 +01:00
return true;
2010-12-26 01:52:17 +01:00
error_free:
2011-02-09 12:29:09 +01:00
if (Zathura.UI.page_view) {
2010-12-29 11:46:13 +01:00
g_object_unref(Zathura.UI.page_view);
}
2010-12-26 01:52:17 +01:00
girara_session_destroy(Zathura.UI.session);
error_out:
return false;
2010-06-03 18:05:34 +02:00
}
2010-12-12 22:04:42 +01:00
bool
document_open(const char* path, const char* password)
{
2011-02-09 12:29:09 +01:00
if (!path) {
2010-12-29 11:46:13 +01:00
goto error_out;
2010-12-12 22:04:42 +01:00
}
zathura_document_t* document = zathura_document_open(path, password);
2011-02-09 12:29:09 +01:00
if (!document) {
2010-12-29 11:46:13 +01:00
goto error_out;
2010-12-12 22:04:42 +01:00
}
Zathura.document = document;
2010-12-29 11:46:13 +01:00
/* create blank pages */
2011-02-09 12:29:09 +01:00
for (unsigned int i = 0; i < document->number_of_pages; i++)
2010-12-28 09:47:09 +01:00
{
2010-12-29 11:46:13 +01:00
/* create blank page */
GtkWidget* image = page_blank(document->pages[i]->width, document->pages[i]->height);
2010-12-28 09:47:09 +01:00
2011-02-09 12:29:09 +01:00
if (!image) {
2010-12-29 11:46:13 +01:00
goto error_free;
2010-12-28 09:47:09 +01:00
}
2010-12-29 11:46:13 +01:00
/* pack to page view */
2011-01-06 09:40:56 +01:00
gtk_box_pack_start(GTK_BOX(Zathura.UI.page_view), image, TRUE, TRUE, 1);
2010-12-28 09:47:09 +01:00
}
2010-12-29 11:46:13 +01:00
girara_set_view(Zathura.UI.session, Zathura.UI.page_view);
2010-12-28 09:47:09 +01:00
2011-01-24 12:43:39 +01:00
/* threads */
Zathura.Sync.render_thread = render_init();
2011-02-09 12:29:09 +01:00
if (!Zathura.Sync.render_thread) {
2011-01-24 12:43:39 +01:00
goto error_free;
}
2010-12-29 11:46:13 +01:00
/* first page */
2011-02-09 12:29:09 +01:00
if (!page_set(0)) {
2010-12-29 11:46:13 +01:00
goto error_free;
}
2010-12-12 22:04:42 +01:00
return true;
2010-12-29 11:46:13 +01:00
error_free:
zathura_document_free(document);
error_out:
return false;
2010-12-12 22:04:42 +01:00
}
bool
document_close()
{
2011-02-09 12:29:09 +01:00
if (!Zathura.document) {
2010-12-12 22:04:42 +01:00
return false;
}
2011-02-09 12:29:09 +01:00
if (Zathura.Sync.render_thread) {
2011-01-24 12:43:39 +01:00
render_free(Zathura.Sync.render_thread);
}
2010-12-12 22:04:42 +01:00
zathura_document_free(Zathura.document);
return true;
}
bool
page_set(unsigned int page_id)
{
2011-02-09 12:29:09 +01:00
if (!Zathura.document || !Zathura.document->pages) {
2010-12-26 01:52:17 +01:00
goto error_out;
2010-12-12 22:04:42 +01:00
}
2011-02-09 12:29:09 +01:00
if (page_id >= Zathura.document->number_of_pages) {
2010-12-26 01:52:17 +01:00
goto error_out;
2010-12-12 22:04:42 +01:00
}
/* render page */
2010-12-29 11:46:13 +01:00
zathura_page_t* page = Zathura.document->pages[page_id];
2010-12-27 09:44:28 +01:00
2011-02-09 12:29:09 +01:00
if (!page) {
goto error_out;
}
2010-12-29 11:46:13 +01:00
GtkAdjustment* view_vadjustment = gtk_scrolled_window_get_vadjustment(GTK_SCROLLED_WINDOW(Zathura.UI.session->gtk.view));
2010-12-29 12:26:47 +01:00
cb_view_vadjustment_value_changed(view_vadjustment, NULL);
/* update page number */
2010-12-12 22:04:42 +01:00
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;
2010-12-26 01:52:17 +01:00
error_out:
return false;
2010-12-12 22:04:42 +01:00
}
2009-08-11 23:18:50 +02:00
/* main function */
int main(int argc, char* argv[])
2009-08-11 23:18:50 +02:00
{
g_thread_init(NULL);
gdk_threads_init();
2009-08-11 23:18:50 +02:00
gtk_init(&argc, &argv);
2011-02-09 12:29:09 +01:00
if (!init_zathura()) {
2011-04-02 23:40:57 +02:00
girara_error("Coult not initialize zathura\n");
2010-11-18 14:51:13 +01:00
return -1;
}
2010-11-18 02:35:33 +01:00
2011-02-09 12:29:09 +01:00
if (argc > 1) {
if (!document_open(argv[1], NULL)) {
2011-04-02 23:40:57 +02:00
girara_error("Failed to open document\n");
2010-11-18 02:35:33 +01:00
return -1;
}
}
2010-01-01 13:54:21 +01:00
2010-11-18 14:51:13 +01:00
gdk_threads_enter();
gtk_main();
gdk_threads_leave();
2009-08-11 23:18:50 +02:00
return 0;
}