Commit 5e4d5843 authored by Moritz Lipp's avatar Moritz Lipp
Browse files

Merge branch 'render' into girara

Conflicts:
	Makefile
	config.c
	config.h
	document.c
	ft/djvu/Makefile
	ft/pdf-mupdf/Makefile
	ft/pdf-poppler/Makefile
	render.c
	zathura.c
parents 12793b35 5c5df5bf
......@@ -3,3 +3,4 @@
*~
*.rej
*.swp
.depend
......@@ -19,11 +19,13 @@ options:
%.o: %.c
@echo CC $<
@${CC} -c ${CFLAGS} -o $@ $<
@mkdir -p .depend
@${CC} -c ${CFLAGS} -o $@ $< -MMD -MF .depend/$@.dep
%.do: %.c
@echo CC $<
@${CC} -c ${CFLAGS} ${DFLAGS} -o $@ $<
@mkdir -p .depend
@${CC} -c ${CFLAGS} ${DFLAGS} -o $@ $< -MMD -MF .depend/$@.dep
${OBJECTS}: config.mk
${DOBJECTS}: config.mk
......@@ -34,7 +36,7 @@ ${PROJECT}: ${OBJECTS}
clean:
@rm -rf ${PROJECT} ${OBJECTS} ${PROJECT}-${VERSION}.tar.gz \
${DOBJECTS} ${PROJECT}-debug
${DOBJECTS} ${PROJECT}-debug .depend
@${MAKE} -C ft clean
${PROJECT}-debug: ${DOBJECTS}
......@@ -77,4 +79,6 @@ uninstall:
@rm -f ${DESTDIR}${MANPREFIX}/man1/${PROJECT}.1
@${MAKE} -C ft uninstall
-include $(wildcard .depend/*.dep)
.PHONY: all options clean debug valgrind gdb dist install uninstall
......@@ -8,18 +8,12 @@
#include "zathura.h"
#include "render.h"
#include "document.h"
#include "utils.h"
gboolean
cb_destroy(GtkWidget* widget, gpointer data)
{
if (Zathura.UI.session) {
girara_session_destroy(Zathura.UI.session);
}
document_close();
/* free registered plugins */
zathura_document_plugins_free();
zathura_free(data);
return TRUE;
}
......@@ -28,21 +22,25 @@ void
buffer_changed(girara_session_t* session)
{
g_return_if_fail(session != NULL);
g_return_if_fail(session->global.data != NULL);
zathura_t* zathura = session->global.data;
char* buffer = girara_buffer_get(session);
if (buffer) {
girara_statusbar_item_set_text(session, Zathura.UI.statusbar.buffer, buffer);
girara_statusbar_item_set_text(session, zathura->ui.statusbar.buffer, buffer);
free(buffer);
} else {
girara_statusbar_item_set_text(session, Zathura.UI.statusbar.buffer, "");
girara_statusbar_item_set_text(session, zathura->ui.statusbar.buffer, "");
}
}
void
cb_view_vadjustment_value_changed(GtkAdjustment *adjustment, gpointer data)
{
if (!Zathura.document || !Zathura.document->pages || !Zathura.UI.page_view) {
zathura_t* zathura = data;
if (!zathura || !zathura->document || !zathura->document->pages || !zathura->ui.page_view) {
return;
}
......@@ -51,23 +49,32 @@ cb_view_vadjustment_value_changed(GtkAdjustment *adjustment, gpointer data)
gdouble upper = lower + gtk_adjustment_get_page_size(adjustment);
/* find page that fits */
for (unsigned int page_id = 0; page_id < Zathura.document->number_of_pages; page_id++)
for (unsigned int page_id = 0; page_id < zathura->document->number_of_pages; page_id++)
{
zathura_page_t* page = Zathura.document->pages[page_id];
zathura_page_t* page = zathura->document->pages[page_id];
/* check for rendered attribute */
if (page->rendered) {
page_offset_t* offset = page_calculate_offset(page);
if (offset == NULL) {
continue;
}
double begin = page->offset;
double end = page->offset + page->height;
double begin = offset->y;
double end = offset->y + page->height;
if ( ( (begin >= lower) && (end <= upper) ) /* page is in viewport */
|| ( (begin <= lower) && (end >= lower) && (end <= upper) ) /* end of the page is in viewport */
|| ( (begin >= lower) && (end >= upper) && (begin <= upper) ) /* begin of the page is in viewport */
if ( ( (begin >= lower) && (end <= upper) ) /* [> page is in viewport <]*/
|| ( (begin <= lower) && (end >= lower) && (end <= upper) ) /* [> end of the page is in viewport <] */
|| ( (begin >= lower) && (end >= upper) && (begin <= upper) ) /* [> begin of the page is in viewport <] */
) {
render_page(Zathura.Sync.render_thread, Zathura.document->pages[page_id]);
page->visible = true;
if (page->surface == NULL) {
render_page(zathura->sync.render_thread, page);
}
} else {
page->visible = false;
cairo_surface_destroy(page->surface);
page->surface = NULL;
}
free(offset);
}
}
......@@ -24,7 +24,12 @@ cmd_bookmark_open(girara_session_t* session, girara_list_t* argument_list)
bool
cmd_close(girara_session_t* session, girara_list_t* argument_list)
{
document_close();
g_return_val_if_fail(session != NULL, false);
g_return_val_if_fail(session->global.data != NULL, false);
zathura_t* zathura = session->global.data;
g_return_val_if_fail(zathura->document != NULL, false);
document_close(zathura);
return true;
}
......
This diff is collapsed.
......@@ -5,18 +5,21 @@
#define GLOBAL_RC "/etc/zathurarc"
#define ZATHURA_RC "zathurarc"
#define CONFIG_DIR "~/.config/zathura"
#include "zathura.h"
/**
* This function loads the default values of the configuration
*
* @param zathura the zathura session
*/
void config_load_default(void);
void config_load_default(zathura_t* zathura);
/**
* Loads and evaluates a configuration file
*
* @param path Path to the configuration file
*/
void config_load_file(char* path);
void config_load_file(zathura_t* zathura, char* path);
#endif // CONFIG_H
/* See LICENSE file for license and copyright information */
#define _BSD_SOURCE
#define _XOPEN_SOURCE 500
#define _XOPEN_SOURCE 700
// TODO: Implement realpath
#include <stdlib.h>
......@@ -12,156 +12,163 @@
#include <sys/stat.h>
#include <dirent.h>
#include <dlfcn.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include "document.h"
#include "utils.h"
#include "zathura.h"
#include "render.h"
#define LENGTH(x) (sizeof(x)/sizeof((x)[0]))
zathura_document_plugin_t* zathura_document_plugins = NULL;
void
zathura_document_plugins_load(void)
zathura_document_plugins_load(zathura_t* zathura)
{
/* read all files in the plugin directory */
DIR* dir = opendir(PLUGIN_DIR);
if (dir == NULL) {
girara_error("Could not open plugin directory: %s", PLUGIN_DIR);
girara_list_iterator_t* iter = girara_list_iterator(zathura->plugins.path);
if (iter == NULL) {
return;
}
struct dirent* entry;
while ((entry = readdir(dir)) != NULL) {
void* handle = NULL;
zathura_document_plugin_t* plugin = NULL;
char* path = NULL;
struct stat fstat;
/* get full path */
path = string_concat(PLUGIN_DIR, "/", entry->d_name, NULL);
if (path == NULL || stat(path, &fstat) != 0) {
goto error_continue;
}
do
{
char* plugindir = girara_list_iterator_data(iter);
/* check if entry is a file. */
if (!(fstat.st_mode & S_IFREG)) {
/* read all files in the plugin directory */
DIR* dir = opendir(plugindir);
if (dir == NULL) {
girara_error("could not open plugin directory: %s", plugindir);
continue;
}
/* load plugin */
handle = dlopen(path, RTLD_NOW);
if (handle == NULL) {
girara_error("Could not load plugin (%s)", dlerror());
goto error_free;
}
int fddir = dirfd(dir);
struct dirent* entry;
while ((entry = readdir(dir)) != NULL) {
struct stat statbuf;
if (fstatat(fddir, entry->d_name, &statbuf, 0) != 0) {
girara_error("failed to fstatat %s/%s; errno is %d.", plugindir, entry->d_name, errno);
continue;
}
/* resolve symbol */
zathura_plugin_register_service_t register_plugin;
*(void**)(&register_plugin) = dlsym(handle, PLUGIN_REGISTER_FUNCTION);
/* check if entry is a file */
if (S_ISREG(statbuf.st_mode) == 0) {
girara_info("%s/%s is not a regular file. Skipping.", plugindir, entry->d_name);
continue;
}
if (register_plugin == NULL) {
girara_error("Could not find '%s' in plugin", PLUGIN_REGISTER_FUNCTION);
goto error_free;
}
void* handle = NULL;
zathura_document_plugin_t* plugin = NULL;
char* path = NULL;
plugin = malloc(sizeof(zathura_document_plugin_t));
/* get full path */
path = g_build_filename(plugindir, entry->d_name, NULL);
if (path == NULL) {
g_error("failed to allocate memory!");
break;
}
if (plugin == NULL) {
goto error_free;
}
/* load plugin */
handle = dlopen(path, RTLD_NOW);
if (handle == NULL) {
girara_error("could not load plugin %s (%s)", path, dlerror());
g_free(path);
continue;
}
plugin->file_extension = NULL;
plugin->open_function = NULL;
/* resolve symbol */
zathura_plugin_register_service_t register_plugin;
*(void**)(&register_plugin) = dlsym(handle, PLUGIN_REGISTER_FUNCTION);
register_plugin(plugin);
if (register_plugin == NULL) {
girara_error("could not find '%s' function in plugin %s", PLUGIN_REGISTER_FUNCTION, path);
g_free(path);
dlclose(handle);
continue;
}
bool r = zathura_document_plugin_register(plugin, handle);
plugin = malloc(sizeof(zathura_document_plugin_t));
if (r == false) {
girara_error("Could not register plugin (%s)", path);
goto error_free;
}
if (plugin == NULL) {
g_error("failed to allocate memory!");
break;
}
free(path);
plugin->file_extension = NULL;
plugin->open_function = NULL;
continue;
register_plugin(plugin);
error_free:
bool r = zathura_document_plugin_register(zathura, plugin, handle);
free(path);
free(plugin);
if (r == false) {
girara_error("could not register plugin %s", path);
free(plugin);
dlclose(handle);
}
else {
girara_info("successfully loaded plugin %s", path);
}
if (handle) {
dlclose(handle);
g_free(path);
}
error_continue:
continue;
}
if (closedir(dir) == -1) {
girara_error("Could not close plugin directory: %s", PLUGIN_DIR);
}
if (closedir(dir) == -1) {
girara_error("could not close plugin directory %s", plugindir);
}
} while (girara_list_iterator_next(iter));
girara_list_iterator_free(iter);
}
void
zathura_document_plugins_free(void)
zathura_document_plugins_free(zathura_t* zathura)
{
/* free registered plugins */
zathura_document_plugin_t* plugin = zathura_document_plugins;
while (plugin) {
zathura_document_plugin_t* tmp = plugin->next;
free(plugin->file_extension);
free(plugin);
plugin = tmp;
if (zathura == NULL) {
return;
}
zathura_document_plugins = NULL;
girara_list_iterator_t* iter = girara_list_iterator(zathura->plugins.plugins);
if (iter == NULL) {
return;
}
do {
zathura_document_plugin_t* plugin = (zathura_document_plugin_t*) girara_list_iterator_data(iter);
free(plugin->file_extension);
free(plugin);
} while (girara_list_iterator_next(iter));
girara_list_iterator_free(iter);
}
bool
zathura_document_plugin_register(zathura_document_plugin_t* new_plugin, void* handle)
zathura_document_plugin_register(zathura_t* zathura, zathura_document_plugin_t* new_plugin, void* handle)
{
if( (new_plugin == NULL) || (new_plugin->file_extension == NULL) || (new_plugin->open_function == NULL)
if( (new_plugin == NULL) || (new_plugin->file_extension == NULL) || (new_plugin->open_function == NULL)
|| (handle == NULL) ) {
girara_error("plugin: could not register\n");
return false;
}
/* search existing plugins */
zathura_document_plugin_t* plugin = zathura_document_plugins;
while (plugin) {
if (!strcmp(plugin->file_extension, new_plugin->file_extension)) {
girara_warning("%s-plugin already registered", plugin->file_extension);
return false;
}
if (plugin->next == NULL) {
break;
}
plugin = plugin->next;
}
/* create new plugin */
new_plugin->handle = handle;
new_plugin->next = NULL;
/* append to list */
if (plugin == NULL) {
zathura_document_plugins = new_plugin;
} else {
plugin->next = new_plugin;
}
girara_list_iterator_t* iter = girara_list_iterator(zathura->plugins.plugins);
if (iter) {
do {
zathura_document_plugin_t* plugin = (zathura_document_plugin_t*) girara_list_iterator_data(iter);
if (!strcmp(plugin->file_extension, new_plugin->file_extension)) {
girara_error("plugin: already registered for filetype %s\n", plugin->file_extension);
girara_list_iterator_free(iter);
return false;
}
} while (girara_list_iterator_next(iter));
girara_list_iterator_free(iter);
}
girara_list_append(zathura->plugins.plugins, new_plugin);
return true;
}
zathura_document_t*
zathura_document_open(const char* path, const char* password)
zathura_document_open(zathura_t* zathura, const char* path, const char* password)
{
if (!path) {
goto error_out;
......@@ -213,6 +220,7 @@ zathura_document_open(const char* path, const char* password)
document->rotate = 0;
document->data = NULL;
document->pages = NULL;
document->zathura = zathura;
document->functions.document_free = NULL;
document->functions.document_index_generate = NULL;
......@@ -225,14 +233,19 @@ zathura_document_open(const char* path, const char* password)
document->functions.page_form_fields_get = NULL;
document->functions.page_render = NULL;
/* init plugin with associated file type */
zathura_document_plugin_t* plugin = zathura_document_plugins;
while (plugin) {
girara_list_iterator_t* iter = girara_list_iterator(zathura->plugins.plugins);
if (iter == NULL) {
goto error_free;
}
do {
zathura_document_plugin_t* plugin = (zathura_document_plugin_t*) girara_list_iterator_data(iter);
if (!strcmp(file_extension, plugin->file_extension)) {
girara_list_iterator_free(iter);
if (plugin->open_function) {
if (plugin->open_function(document)) {
/* update statusbar */
girara_statusbar_item_set_text(Zathura.UI.session, Zathura.UI.statusbar.file, real_path);
girara_statusbar_item_set_text(zathura->ui.session, zathura->ui.statusbar.file, real_path);
/* read all pages */
document->pages = calloc(document->number_of_pages, sizeof(zathura_page_t*));
......@@ -240,30 +253,26 @@ zathura_document_open(const char* path, const char* password)
goto error_free;
}
double offset = 0;
for (unsigned int page_id = 0; page_id < document->number_of_pages; page_id++) {
zathura_page_t* page = zathura_page_get(document, page_id);
if (!page) {
goto error_free;
}
page->offset = offset;
offset += page->height;
document->pages[page_id] = page;
}
return document;
} else {
girara_error("could not open file\n");
goto error_free;
}
}
}
} while (girara_list_iterator_next(iter));
girara_list_iterator_free(iter);
plugin = plugin->next;
}
girara_error("Unknown file type");
girara_error("unknown file type\n");
error_free:
......@@ -389,8 +398,16 @@ zathura_page_get(zathura_document_t* document, unsigned int page_id)
zathura_page_t* page = document->functions.page_get(document, page_id);
if (page) {
page->number = page_id;
page->rendered = false;
page->number = page_id;
page->visible = false;
page->event_box = gtk_event_box_new();
page->drawing_area = gtk_drawing_area_new();
page->surface = NULL;
g_signal_connect(page->drawing_area, "expose-event", G_CALLBACK(page_expose_event), page);
gtk_widget_set_size_request(page->drawing_area, page->width * document->scale, page->height * document->scale);
gtk_container_add(GTK_CONTAINER(page->event_box), page->drawing_area);
g_static_mutex_init(&(page->lock));
}
......@@ -469,7 +486,7 @@ zathura_page_form_fields_free(zathura_list_t* list)
return false;
}
GtkWidget*
zathura_image_buffer_t*
zathura_page_render(zathura_page_t* page)
{
if (!page || !page->document) {
......@@ -481,13 +498,8 @@ zathura_page_render(zathura_page_t* page)
return NULL;
}
GtkWidget* widget = page->document->functions.page_render(page);
if (widget) {
page->rendered = true;
}
return widget;
zathura_image_buffer_t* buffer = page->document->functions.page_render(page);
return buffer;
}
zathura_index_element_t*
......@@ -523,3 +535,37 @@ zathura_index_element_free(zathura_index_element_t* index)
g_free(index);
}
zathura_image_buffer_t*
zathura_image_buffer_create(unsigned int width, unsigned int height)
{
zathura_image_buffer_t* image_buffer = malloc(sizeof(zathura_image_buffer_t));
if (image_buffer == NULL) {
return NULL;
}
image_buffer->data = calloc(width * height * 3, sizeof(unsigned char));
if (image_buffer->data == NULL) {
free(image_buffer);
return NULL;
}
image_buffer->width = width;
image_buffer->height = height;
image_buffer->rowstride = width * 3;
return image_buffer;
}
void
zathura_image_buffer_free(zathura_image_buffer_t* image_buffer)
{
if (image_buffer == NULL) {
return;
}
free(image_buffer->data);
free(image_buffer);
}
......@@ -7,12 +7,12 @@
#include <stdbool.h>
#include <girara-datastructures.h>
#include "zathura.h"
#define PLUGIN_DIR "/usr/lib/zathura"
#define PLUGIN_REGISTER_FUNCTION "plugin_register"
typedef struct zathura_list_s zathura_list_t;
typedef struct zathura_document_s zathura_document_t;
// typedef struct zathura_document_s zathura_document_t;
typedef bool (*zathura_document_open_t)(zathura_document_t* document);
......@@ -24,7 +24,6 @@ typedef struct zathura_document_plugin_s
char* file_extension; /**> File extension */
zathura_document_open_t open_function; /**> Document open function */
void* handle; /**> DLL handle */
struct zathura_document_plugin_s *next; /**> Next plugin */ // TODO: Use list_t
} zathura_document_plugin_t;
/**
......@@ -43,6 +42,33 @@ struct zathura_list_s
struct zathura_list_s* next; /**> Next element in the list */
};
/**
* Image buffer
*/
typedef struct zathura_image_buffer_s
{
unsigned char* data; /**> Image buffer data */
unsigned int height; /**> Height of the image */
unsigned int width; /**> Width of the image */
unsigned int rowstride; /**> Rowstride of the image */
} zathura_image_buffer_t;
/**
* Creates an image buffer