mirror of
https://git.pwmt.org/pwmt/zathura.git
synced 2024-12-29 11:46:01 +01:00
update document.c/.h
This commit is contained in:
parent
daa523aaf5
commit
f57fe5dfde
6 changed files with 85 additions and 65 deletions
2
config.h
2
config.h
|
@ -7,6 +7,8 @@
|
|||
|
||||
/**
|
||||
* This function loads the default values of the configuration
|
||||
*
|
||||
* @param zathura the zathura session
|
||||
*/
|
||||
void config_load_default(zathura_t* zathura);
|
||||
|
||||
|
|
86
document.c
86
document.c
|
@ -18,10 +18,8 @@
|
|||
|
||||
#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);
|
||||
|
@ -76,7 +74,7 @@ zathura_document_plugins_load(void)
|
|||
|
||||
register_plugin(plugin);
|
||||
|
||||
bool r = zathura_document_plugin_register(plugin, handle);
|
||||
bool r = zathura_document_plugin_register(zathura, plugin, handle);
|
||||
|
||||
if (r == false) {
|
||||
fprintf(stderr, "error: could not register plugin (%s)\n", path);
|
||||
|
@ -107,60 +105,54 @@ error_continue:
|
|||
}
|
||||
|
||||
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)
|
||||
|| (handle == NULL) ) {
|
||||
fprintf(stderr, "plugin: could not register\n");
|
||||
girara_error("plugin: could not register\n");
|
||||
return false;
|
||||
}
|
||||
|
||||
/* search existing plugins */
|
||||
zathura_document_plugin_t* plugin = zathura_document_plugins;
|
||||
while (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)) {
|
||||
fprintf(stderr, "plugin: already registered for filetype %s\n", plugin->file_extension);
|
||||
girara_error("plugin: already registered for filetype %s\n", plugin->file_extension);
|
||||
girara_list_iterator_free(iter);
|
||||
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;
|
||||
} 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;
|
||||
|
@ -224,14 +216,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) {
|
||||
return;
|
||||
}
|
||||
|
||||
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*));
|
||||
|
@ -250,16 +247,15 @@ zathura_document_open(const char* path, const char* password)
|
|||
|
||||
return document;
|
||||
} else {
|
||||
fprintf(stderr, "error: could not open file\n");
|
||||
girara_error("could not open file\n");
|
||||
goto error_free;
|
||||
}
|
||||
}
|
||||
}
|
||||
} while (girara_list_iterator_next(iter));
|
||||
girara_list_iterator_free(iter);
|
||||
|
||||
plugin = plugin->next;
|
||||
}
|
||||
|
||||
fprintf(stderr, "error: unknown file type\n");
|
||||
girara_error("unknown file type\n");
|
||||
|
||||
error_free:
|
||||
|
||||
|
|
20
document.h
20
document.h
|
@ -7,12 +7,13 @@
|
|||
#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 +25,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;
|
||||
|
||||
/**
|
||||
|
@ -139,7 +139,7 @@ typedef struct zathura_form_s
|
|||
/**
|
||||
* Page
|
||||
*/
|
||||
typedef struct zathura_page_s
|
||||
struct zathura_page_s
|
||||
{
|
||||
double height; /**> Page height */
|
||||
double width; /**> Page width */
|
||||
|
@ -150,7 +150,7 @@ typedef struct zathura_page_s
|
|||
GtkWidget* event_box; /**> Widget wrapper for mouse events */
|
||||
GtkWidget* drawing_area; /**> Drawing area */
|
||||
GStaticMutex lock; /**> Lock */
|
||||
} zathura_page_t;
|
||||
};
|
||||
|
||||
/**
|
||||
* Document
|
||||
|
@ -226,18 +226,22 @@ struct zathura_document_s
|
|||
|
||||
/**
|
||||
* Load all document plugins
|
||||
*
|
||||
* @param zathura the zathura session
|
||||
*/
|
||||
void zathura_document_plugins_load(void);
|
||||
void zathura_document_plugins_load(zathura_t* zathura);
|
||||
|
||||
/**
|
||||
* Free all document plugins
|
||||
*
|
||||
* @param zathura the zathura session
|
||||
*/
|
||||
void zathura_document_plugins_free(void);
|
||||
void zathura_document_plugins_free(zathura_t* zathura);
|
||||
|
||||
/**
|
||||
* Register document plugin
|
||||
*/
|
||||
bool zathura_document_plugin_register(zathura_document_plugin_t* new_plugin, void* handle);
|
||||
bool zathura_document_plugin_register(zathura_t* zathura, zathura_document_plugin_t* new_plugin, void* handle);
|
||||
|
||||
/**
|
||||
* Open the document
|
||||
|
@ -246,7 +250,7 @@ bool zathura_document_plugin_register(zathura_document_plugin_t* new_plugin, voi
|
|||
* @param password Password of the document or NULL
|
||||
* @return The document object
|
||||
*/
|
||||
zathura_document_t* zathura_document_open(const char* path, const char* password);
|
||||
zathura_document_t* zathura_document_open(zathura_t* zathura, const char* path, const char* password);
|
||||
|
||||
/**
|
||||
* Free the document
|
||||
|
|
6
render.h
6
render.h
|
@ -7,16 +7,16 @@
|
|||
#include <stdlib.h>
|
||||
#include <girara-datastructures.h>
|
||||
|
||||
#include "document.h"
|
||||
#include "zathura.h"
|
||||
#include "callbacks.h"
|
||||
|
||||
typedef struct render_thread_s
|
||||
struct render_thread_s
|
||||
{
|
||||
girara_list_t* list; /**> The list of pages */
|
||||
GThread* thread; /**> The thread object */
|
||||
GMutex* lock; /**> Lock */
|
||||
GCond* cond; /**> Condition */
|
||||
} render_thread_t;
|
||||
};
|
||||
|
||||
/**
|
||||
* This function initializes a render thread
|
||||
|
|
11
zathura.c
11
zathura.c
|
@ -10,6 +10,7 @@
|
|||
#include "shortcuts.h"
|
||||
#include "zathura.h"
|
||||
#include "utils.h"
|
||||
#include "render.h"
|
||||
|
||||
/* function implementation */
|
||||
zathura_t*
|
||||
|
@ -21,6 +22,10 @@ zathura_init(int argc, char* argv[])
|
|||
return NULL;
|
||||
}
|
||||
|
||||
/* plugins */
|
||||
zathura->plugins.plugins = girara_list_new();
|
||||
zathura->plugins.path = NULL;
|
||||
|
||||
/* UI */
|
||||
if ((zathura->ui.session = girara_session_create()) == NULL) {
|
||||
goto error_out;
|
||||
|
@ -74,7 +79,7 @@ zathura_init(int argc, char* argv[])
|
|||
zathura->ui.session->events.buffer_changed = buffer_changed;
|
||||
|
||||
/* load plugins */
|
||||
zathura_document_plugins_load();
|
||||
zathura_document_plugins_load(zathura);
|
||||
|
||||
/* configuration */
|
||||
config_load_default(zathura);
|
||||
|
@ -108,7 +113,7 @@ zathura_free(zathura_t* zathura)
|
|||
document_close(zathura);
|
||||
|
||||
/* free registered plugins */
|
||||
zathura_document_plugins_free();
|
||||
zathura_document_plugins_free(zathura);
|
||||
}
|
||||
|
||||
bool
|
||||
|
@ -118,7 +123,7 @@ document_open(zathura_t* zathura, const char* path, const char* password)
|
|||
goto error_out;
|
||||
}
|
||||
|
||||
zathura_document_t* document = zathura_document_open(path, password);
|
||||
zathura_document_t* document = zathura_document_open(zathura, path, password);
|
||||
|
||||
if (!document) {
|
||||
goto error_out;
|
||||
|
|
19
zathura.h
19
zathura.h
|
@ -6,9 +6,6 @@
|
|||
#include <stdbool.h>
|
||||
#include <girara.h>
|
||||
|
||||
#include "render.h"
|
||||
#include "document.h"
|
||||
|
||||
enum { NEXT, PREVIOUS, LEFT, RIGHT, UP, DOWN, BOTTOM, TOP, HIDE, HIGHLIGHT,
|
||||
DELETE_LAST_WORD, DELETE_LAST_CHAR, DEFAULT, ERROR, WARNING, NEXT_GROUP,
|
||||
PREVIOUS_GROUP, ZOOM_IN, ZOOM_OUT, ZOOM_ORIGINAL, ZOOM_SPECIFIC, FORWARD,
|
||||
|
@ -24,6 +21,16 @@ enum { NEXT, PREVIOUS, LEFT, RIGHT, UP, DOWN, BOTTOM, TOP, HIDE, HIGHLIGHT,
|
|||
#define NORMAL (1 << 3)
|
||||
#define INSERT (1 << 4)
|
||||
|
||||
/* forward declaration for types from document.h */
|
||||
struct zathura_document_s;
|
||||
struct zathura_page_s;
|
||||
typedef struct zathura_document_s zathura_document_t;
|
||||
typedef struct zathura_page_s zathura_page_t;
|
||||
|
||||
/* forward declaration for types from render.h */
|
||||
struct render_thread_s;
|
||||
typedef struct render_thread_s render_thread_t;
|
||||
|
||||
typedef struct zathura_s
|
||||
{
|
||||
struct
|
||||
|
@ -46,6 +53,12 @@ typedef struct zathura_s
|
|||
render_thread_t* render_thread; /**> The thread responsible for rendering the pages */
|
||||
} sync;
|
||||
|
||||
struct
|
||||
{
|
||||
girara_list_t* plugins;
|
||||
girara_list_t* path;
|
||||
} plugins;
|
||||
|
||||
zathura_document_t* document; /**> The current document */
|
||||
} zathura_t;
|
||||
|
||||
|
|
Loading…
Reference in a new issue