Refactor file monitor and add option to reload on SIGHUP

Signed-off-by: Sebastian Ramacher <sebastian+dev@ramacher.at>
This commit is contained in:
Sebastian Ramacher 2016-04-24 18:26:54 +02:00
parent d428ba3c77
commit 09c7f540ab
13 changed files with 509 additions and 62 deletions

View file

@ -661,13 +661,21 @@ values are "plain", "sqlite" (if built with sqlite support) and "null". If
* Default value: plain
dbus-service
^^^^^^^^^^^^^^^^^^^
^^^^^^^^^^^^
En/Disables the D-Bus service. If the services is disabled, SyncTeX forward
synchronization is not available.
* Value type: Boolean
* Default value: true
filemonitor
^^^^^^^^^^^
Defines the filemonitor backend. Possible values are "glib" and "signal" (if
signal handling is supported).
* Value type: String
* Default value: glib
incremental-search
^^^^^^^^^^^^^^^^^^
En/Disables incremental search (search while typing).

View file

@ -376,20 +376,12 @@ file_monitor_reload(void* data)
}
void
cb_file_monitor(GFileMonitor* monitor, GFile* file, GFile* UNUSED(other_file), GFileMonitorEvent event, girara_session_t* session)
cb_file_monitor(ZathuraFileMonitor* monitor, girara_session_t* session)
{
g_return_if_fail(monitor != NULL);
g_return_if_fail(file != NULL);
g_return_if_fail(session != NULL);
switch (event) {
case G_FILE_MONITOR_EVENT_CHANGES_DONE_HINT:
case G_FILE_MONITOR_EVENT_CREATED:
g_main_context_invoke(NULL, file_monitor_reload, session);
break;
default:
return;
}
g_main_context_invoke(NULL, file_monitor_reload, session);
}
static gboolean

View file

@ -125,13 +125,9 @@ bool cb_sc_display_link(GtkEntry* entry, girara_session_t* session);
* Emitted when file has been changed
*
* @param monitor The file monitor
* @param file The file
* @param other_file A file or NULL
* @param event The monitor event
* @param session The girara session
*/
void cb_file_monitor(GFileMonitor* monitor, GFile* file, GFile* other_file,
GFileMonitorEvent event, girara_session_t* session);
void cb_file_monitor(ZathuraFileMonitor* monitor, girara_session_t* session);
/**
* Callback to read new password for file that should be opened

View file

@ -142,6 +142,7 @@ config_load_default(zathura_t* zathura)
/* zathura settings */
girara_setting_add(gsession, "database", "plain", STRING, true, _("Database backend"), NULL, NULL);
girara_setting_add(gsession, "filemonitor", "glib", STRING, true, _("File monitor backend"), NULL, NULL);
int_value = 10;
girara_setting_add(gsession, "zoom-step", &int_value, INT, false, _("Zoom step"), NULL, NULL);
int_value = 1;

107
zathura/file-monitor-glib.c Normal file
View file

@ -0,0 +1,107 @@
/* See LICENSE file for license and copyright information */
#include "file-monitor-glib.h"
#include "macros.h"
#include <gio/gio.h>
struct zathura_glibfilemonitor_s
{
ZathuraFileMonitor parent;
GFileMonitor* monitor; /**< File monitor */
GFile* file; /**< File for file monitor */
};
G_DEFINE_TYPE(ZathuraGLibFileMonitor, zathura_glibfilemonitor,
ZATHURA_TYPE_FILEMONITOR)
static void
file_changed(GFileMonitor* UNUSED(monitor), GFile* UNUSED(file),
GFile* UNUSED(other_file), GFileMonitorEvent event,
ZathuraGLibFileMonitor* file_monitor)
{
switch (event) {
case G_FILE_MONITOR_EVENT_CHANGES_DONE_HINT:
case G_FILE_MONITOR_EVENT_CREATED:
g_signal_emit_by_name(file_monitor, "reload-file");
break;
default:
return;
}
}
static void
start(ZathuraFileMonitor* file_monitor)
{
ZathuraGLibFileMonitor* glib_file_monitor =
ZATHURA_GLIBFILEMONITOR(file_monitor);
const char* file_path =
zathura_filemonitor_get_filepath(file_monitor);
/* install file monitor */
char* file_uri = g_filename_to_uri(file_path, NULL, NULL);
if (file_uri == NULL) {
return;
}
glib_file_monitor->file = g_file_new_for_uri(file_uri);
g_free(file_uri);
if (glib_file_monitor->file == NULL) {
return;
}
glib_file_monitor->monitor = g_file_monitor_file(
glib_file_monitor->file, G_FILE_MONITOR_NONE, NULL, NULL);
if (glib_file_monitor->monitor != NULL) {
g_signal_connect_object(G_OBJECT(glib_file_monitor->monitor), "changed",
G_CALLBACK(file_changed), glib_file_monitor, 0);
}
}
static void
stop(ZathuraFileMonitor* file_monitor)
{
ZathuraGLibFileMonitor* glib_file_monitor =
ZATHURA_GLIBFILEMONITOR(file_monitor);
if (glib_file_monitor->monitor != NULL) {
g_file_monitor_cancel(glib_file_monitor->monitor);
}
g_clear_object(&glib_file_monitor->monitor);
g_clear_object(&glib_file_monitor->file);
}
static void
dispose(GObject* object)
{
stop(ZATHURA_FILEMONITOR(object));
G_OBJECT_CLASS(zathura_glibfilemonitor_parent_class)->dispose(object);
}
static void
finalize(GObject* object)
{
G_OBJECT_CLASS(zathura_glibfilemonitor_parent_class)->finalize(object);
}
static void
zathura_glibfilemonitor_class_init(ZathuraGLibFileMonitorClass* class)
{
ZathuraFileMonitorClass* filemonitor_class = ZATHURA_FILEMONITOR_CLASS(class);
filemonitor_class->start = start;
filemonitor_class->stop = stop;
GObjectClass* object_class = G_OBJECT_CLASS(class);
object_class->dispose = dispose;
object_class->finalize = finalize;
}
static void
zathura_glibfilemonitor_init(ZathuraGLibFileMonitor* glibfilemonitor)
{
glibfilemonitor->monitor = NULL;
glibfilemonitor->file = NULL;
}

View file

@ -0,0 +1,33 @@
/* See LICENSE file for license and copyright information */
#ifndef FILEMONITOR_GLIB_H
#define FILEMONITOR_GLIB_H
#include "file-monitor.h"
#define ZATHURA_TYPE_GLIBFILEMONITOR (zathura_glibfilemonitor_get_type())
#define ZATHURA_GLIBFILEMONITOR(obj) \
(G_TYPE_CHECK_INSTANCE_CAST((obj), ZATHURA_TYPE_GLIBFILEMONITOR, \
ZathuraGLibFileMonitor))
#define ZATHURA_GLIBFILEMONITOR_CLASS(obj) \
(G_TYPE_CHECK_CLASS_CAST((obj), ZATHURA_TYPE_GLIBFILEMONITOR, \
ZathuraGLibFileMonitorClass))
#define ZATHURA_IS_GLIBFILEMONITOR(obj) \
(G_TYPE_CHECK_INSTANCE_TYPE((obj), ZATHURA_TYPE_GLIBFILEMONITOR))
#define ZATHURA_IS_GLIBFILEMONITOR_CLASS(obj) \
(G_TYPE_CHECK_CLASS_TYPE((obj), ZATHURA_TYPE_GLIBFILEMONITOR))
#define ZATHURA_GLIBFILEMONITOR_GET_CLASS(obj) \
(G_TYPE_INSTANCE_GET_CLASS((obj), ZATHURA_TYPE_GLIBFILEMONITOR, \
ZathuraGLibFileMonitorClass))
typedef struct zathura_glibfilemonitor_s ZathuraGLibFileMonitor;
typedef struct zathura_glibfilemonitor_class_s ZathuraGLibFileMonitorClass;
struct zathura_glibfilemonitor_class_s
{
ZathuraFileMonitorClass parent_class;
};
GType zathura_glibfilemonitor_get_type(void);
#endif

View file

@ -0,0 +1,80 @@
/* See LICENSE file for license and copyright information */
#include "file-monitor-signal.h"
#ifdef G_OS_UNIX
#include <glib-unix.h>
#endif
struct zathura_signalfilemonitor_s
{
ZathuraFileMonitor parent;
gint handle;
};
G_DEFINE_TYPE(ZathuraSignalFileMonitor, zathura_signalfilemonitor,
ZATHURA_TYPE_FILEMONITOR)
static gboolean
signal_handler(gpointer data)
{
if (data == NULL) {
return TRUE;
}
ZathuraSignalFileMonitor* signalfilemonitor = data;
g_signal_emit_by_name(signalfilemonitor, "reload-file");
return TRUE;
}
static void
start(ZathuraFileMonitor* file_monitor)
{
#ifdef G_OS_UNIX
ZathuraSignalFileMonitor* signal_file_monitor =
ZATHURA_SIGNALFILEMONITOR(file_monitor);
signal_file_monitor->handle =
g_unix_signal_add(SIGHUP, signal_handler, signal_file_monitor);
#endif
}
static void
stop(ZathuraFileMonitor* file_monitor)
{
#ifdef G_OS_UNIX
ZathuraSignalFileMonitor* signal_file_monitor =
ZATHURA_SIGNALFILEMONITOR(file_monitor);
if (signal_file_monitor->handle > 0) {
g_source_remove(signal_file_monitor->handle);
}
#endif
}
static void
zathura_signalfilemonitor_finalize(GObject* object)
{
stop(ZATHURA_FILEMONITOR(object));
G_OBJECT_CLASS(zathura_signalfilemonitor_parent_class)->finalize(object);
}
static void
zathura_signalfilemonitor_class_init(ZathuraSignalFileMonitorClass* class)
{
ZathuraFileMonitorClass* filemonitor_class = ZATHURA_FILEMONITOR_CLASS(class);
filemonitor_class->start = start;
filemonitor_class->stop = stop;
GObjectClass* object_class = G_OBJECT_CLASS(class);
object_class->finalize = zathura_signalfilemonitor_finalize;
}
static void
zathura_signalfilemonitor_init(ZathuraSignalFileMonitor* signalfilemonitor)
{
signalfilemonitor->handle = 0;
}

View file

@ -0,0 +1,33 @@
/* See LICENSE file for license and copyright information */
#ifndef FILEMONITOR_SIGNAL_H
#define FILEMONITOR_SIGNAL_H
#include "file-monitor.h"
#define ZATHURA_TYPE_SIGNALFILEMONITOR (zathura_signalfilemonitor_get_type())
#define ZATHURA_SIGNALFILEMONITOR(obj) \
(G_TYPE_CHECK_INSTANCE_CAST((obj), ZATHURA_TYPE_SIGNALFILEMONITOR, \
ZathuraSignalFileMonitor))
#define ZATHURA_SIGNALFILEMONITOR_CLASS(obj) \
(G_TYPE_CHECK_CLASS_CAST((obj), ZATHURA_TYPE_SIGNALFILEMONITOR, \
ZathuraSignalFileMonitorClass))
#define ZATHURA_IS_SIGNALFILEMONITOR(obj) \
(G_TYPE_CHECK_INSTANCE_TYPE((obj), ZATHURA_TYPE_SIGNALFILEMONITOR))
#define ZATHURA_IS_SIGNALFILEMONITOR_CLASS(obj) \
(G_TYPE_CHECK_CLASS_TYPE((obj), ZATHURA_TYPE_SIGNALFILEMONITOR))
#define ZATHURA_SIGNALFILEMONITOR_GET_CLASS(obj) \
(G_TYPE_INSTANCE_GET_CLASS((obj), ZATHURA_TYPE_SIGNALFILEMONITOR, \
ZathuraSignalFileMonitorClass))
typedef struct zathura_signalfilemonitor_s ZathuraSignalFileMonitor;
typedef struct zathura_signalfilemonitor_class_s ZathuraSignalFileMonitorClass;
struct zathura_signalfilemonitor_class_s
{
ZathuraFileMonitorClass parent_class;
};
GType zathura_signalfilemonitor_get_type(void);
#endif

146
zathura/file-monitor.c Normal file
View file

@ -0,0 +1,146 @@
/* See LICENSE file for license and copyright information */
#include "file-monitor.h"
#include "file-monitor-glib.h"
#include "file-monitor-signal.h"
#include "macros.h"
G_DEFINE_TYPE(ZathuraFileMonitor, zathura_filemonitor, G_TYPE_OBJECT)
typedef struct private_s {
char* file_path;
} private_t;
#define GET_PRIVATE(obj) \
(G_TYPE_INSTANCE_GET_PRIVATE((obj), ZATHURA_TYPE_FILEMONITOR, private_t))
enum {
PROP_0,
PROP_FILE_PATH
};
enum {
RELOAD_FILE,
LAST_SIGNAL
};
static guint signals[LAST_SIGNAL] = { 0 };
static void
finalize(GObject* object)
{
ZathuraFileMonitor* file_monitor = ZATHURA_FILEMONITOR(object);
private_t* private = GET_PRIVATE(file_monitor);
if (private->file_path != NULL) {
g_free(private->file_path);
}
G_OBJECT_CLASS(zathura_filemonitor_parent_class)->finalize(object);
}
static void
set_property(GObject* object, guint prop_id, const GValue* value, GParamSpec* pspec)
{
ZathuraFileMonitor* file_monitor = ZATHURA_FILEMONITOR(object);
private_t* private = GET_PRIVATE(file_monitor);
switch (prop_id) {
case PROP_FILE_PATH:
if (private->file_path != NULL) {
g_free(private->file_path);
}
private->file_path = g_value_dup_string(value);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
}
}
static void
get_property(GObject* object, guint prop_id, GValue* value, GParamSpec* pspec)
{
ZathuraFileMonitor* file_monitor = ZATHURA_FILEMONITOR(object);
private_t* private = GET_PRIVATE(file_monitor);
switch (prop_id) {
case PROP_FILE_PATH:
g_value_set_string(value, private->file_path);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
}
}
static void
zathura_filemonitor_class_init(ZathuraFileMonitorClass* class)
{
/* add private members */
g_type_class_add_private(class, sizeof(private_t));
/* set up methods */
class->start = NULL;
class->stop = NULL;
GObjectClass* object_class = G_OBJECT_CLASS(class);
object_class->finalize = finalize;
object_class->set_property = set_property;
object_class->get_property = get_property;
/* add properties */
g_object_class_install_property(
object_class, PROP_FILE_PATH,
g_param_spec_string("file-path", "file-path", "file path to monitor", NULL,
G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY |
G_PARAM_STATIC_STRINGS));
/* add signals */
signals[RELOAD_FILE] =
g_signal_new("reload-file", ZATHURA_TYPE_FILEMONITOR, G_SIGNAL_RUN_LAST, 0,
NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 0);
}
static void
zathura_filemonitor_init(ZathuraFileMonitor* file_monitor)
{
private_t* private = GET_PRIVATE(file_monitor);
private->file_path = NULL;
}
const char* zathura_filemonitor_get_filepath(ZathuraFileMonitor* filemonitor)
{
private_t* private = GET_PRIVATE(filemonitor);
return private->file_path;
}
ZathuraFileMonitor*
zathura_filemonitor_new(const char* file_path,
zathura_filemonitor_type_t filemonitor_type)
{
g_return_val_if_fail(file_path != NULL, NULL);
GObject* ret = NULL;
switch (filemonitor_type) {
case ZATHURA_FILEMONITOR_GLIB:
ret = g_object_new(ZATHURA_TYPE_GLIBFILEMONITOR, "file-path", file_path,
NULL);
break;
case ZATHURA_FILEMONITOR_SIGNAL:
ret = g_object_new(ZATHURA_TYPE_SIGNALFILEMONITOR, "file-path", file_path,
NULL);
break;
default:
g_return_val_if_fail(false, NULL);
}
if (ret == NULL) {
return NULL;
}
ZathuraFileMonitor* file_monitor = ZATHURA_FILEMONITOR(ret);
ZATHURA_FILEMONITOR_GET_CLASS(file_monitor)->start(file_monitor);
return file_monitor;
}

79
zathura/file-monitor.h Normal file
View file

@ -0,0 +1,79 @@
/* See LICENSE file for license and copyright information */
#ifndef FILEMONITOR_H
#define FILEMONITOR_H
#include <stdbool.h>
#include <girara/types.h>
#include <glib-object.h>
#define ZATHURA_TYPE_FILEMONITOR (zathura_filemonitor_get_type())
#define ZATHURA_FILEMONITOR(obj) \
(G_TYPE_CHECK_INSTANCE_CAST((obj), ZATHURA_TYPE_FILEMONITOR, \
ZathuraFileMonitor))
#define ZATHURA_FILEMONITOR_CLASS(obj) \
(G_TYPE_CHECK_CLASS_CAST((obj), ZATHURA_TYPE_FILEMONITOR, \
ZathuraFileMonitorClass))
#define ZATHURA_IS_FILEMONITOR(obj) \
(G_TYPE_CHECK_INSTANCE_TYPE((obj), ZATHURA_TYPE_FILEMONITOR))
#define ZATHURA_IS_FILEMONITOR_CLASS(obj) \
(G_TYPE_CHECK_CLASS_TYPE((obj), ZATHURA_TYPE_FILEMONITOR))
#define ZATHURA_FILEMONITOR_GET_CLASS(obj) \
(G_TYPE_INSTANCE_GET_CLASS((obj), ZATHURA_TYPE_FILEMONITOR, \
ZathuraFileMonitorClass))
typedef struct zathura_filemonitor_s ZathuraFileMonitor;
typedef struct zathura_filemonitor_class_s ZathuraFileMonitorClass;
/**
* Base class for all file monitors.
*
* The signal 'reload-file' is emitted if the monitored file changed.
*/
struct zathura_filemonitor_s
{
GObject parent;
};
struct zathura_filemonitor_class_s
{
GObjectClass parent_class;
void (*start)(ZathuraFileMonitor*);
void (*stop)(ZathuraFileMonitor*);
};
/**
* Get the type of the filemonitor.
*
* @return the type
*/
GType zathura_filemonitor_get_type(void);
/**
* Type of file monitor.
*/
typedef enum zathura_filemonitor_type_e {
ZATHURA_FILEMONITOR_GLIB, /**< Use filemonitor from GLib */
ZATHURA_FILEMONITOR_SIGNAL /**< Reload when receiving SIGHUP */
} zathura_filemonitor_type_t;
/**
* Create a new file monitor.
*
* @param file_path file to monitor
* @param filemonitor_type type of file monitor
* @return new file monitor instance
*/
ZathuraFileMonitor*
zathura_filemonitor_new(const char* file_path,
zathura_filemonitor_type_t filemonitor_type);
/**
* Get path of the monitored file.
*
* @return path of monitored file
*/
const char* zathura_filemonitor_get_filepath(ZathuraFileMonitor* filemonitor);
#endif

View file

@ -434,7 +434,7 @@ sc_reload(girara_session_t* session, girara_argument_t* UNUSED(argument),
g_return_val_if_fail(session->global.data != NULL, false);
zathura_t* zathura = session->global.data;
if (zathura->file_monitor.file_path == NULL) {
if (zathura->file_monitor.monitor == NULL) {
return false;
}
@ -442,9 +442,9 @@ sc_reload(girara_session_t* session, girara_argument_t* UNUSED(argument),
document_close(zathura, true);
/* reopen document */
document_open(zathura, zathura->file_monitor.file_path, NULL,
zathura->file_monitor.password,
ZATHURA_PAGE_NUMBER_UNSPECIFIED);
document_open(
zathura, zathura_filemonitor_get_filepath(zathura->file_monitor.monitor),
NULL, zathura->file_monitor.password, ZATHURA_PAGE_NUMBER_UNSPECIFIED);
return false;
}

View file

@ -763,7 +763,6 @@ document_open(zathura_t* zathura, const char* path, const char* uri, const char*
goto error_out;
}
gchar* file_uri = NULL;
zathura_error_t error = ZATHURA_ERROR_OK;
zathura_document_t* document = zathura_document_open(zathura, path, uri, password, &error);
@ -882,31 +881,22 @@ document_open(zathura_t* zathura, const char* path, const char* uri, const char*
g_free(filename);
/* install file monitor */
file_uri = g_filename_to_uri(file_path, NULL, NULL);
if (file_uri == NULL) {
goto error_free;
}
if (zathura->file_monitor.file == NULL) {
zathura->file_monitor.file = g_file_new_for_uri(file_uri);
if (zathura->file_monitor.file == NULL) {
goto error_free;
}
}
if (zathura->file_monitor.monitor == NULL) {
zathura->file_monitor.monitor = g_file_monitor_file(zathura->file_monitor.file, G_FILE_MONITOR_NONE, NULL, NULL);
char* filemonitor_backend = NULL;
girara_setting_get(zathura->ui.session, "filemonitor", &filemonitor_backend);
zathura_filemonitor_type_t type = ZATHURA_FILEMONITOR_GLIB;
#ifdef G_OS_UNIX
if (g_strcmp0(filemonitor_backend, "signal") == 0) {
type = ZATHURA_FILEMONITOR_SIGNAL;
}
#endif
zathura->file_monitor.monitor = zathura_filemonitor_new(file_path, type);
if (zathura->file_monitor.monitor == NULL) {
goto error_free;
}
g_signal_connect(G_OBJECT(zathura->file_monitor.monitor), "changed", G_CALLBACK(cb_file_monitor), zathura->ui.session);
}
if (zathura->file_monitor.file_path == NULL) {
zathura->file_monitor.file_path = g_strdup(file_path);
if (zathura->file_monitor.file_path == NULL) {
goto error_free;
}
g_signal_connect(G_OBJECT(zathura->file_monitor.monitor), "reload-file",
G_CALLBACK(cb_file_monitor), zathura->ui.session);
}
if (password != NULL) {
@ -1055,8 +1045,6 @@ document_open(zathura_t* zathura, const char* path, const char* uri, const char*
girara_set_window_title(zathura->ui.session, formatted_filename);
g_free(formatted_filename);
g_free(file_uri);
/* adjust_view */
adjust_view(zathura);
for (unsigned int page_id = 0; page_id < number_of_pages; page_id++) {
@ -1092,10 +1080,6 @@ document_open(zathura_t* zathura, const char* path, const char* uri, const char*
error_free:
if (file_uri != NULL) {
g_free(file_uri);
}
zathura_document_free(document);
error_out:
@ -1233,21 +1217,10 @@ document_close(zathura_t* zathura, bool keep_monitor)
/* remove monitor */
if (keep_monitor == false) {
if (zathura->file_monitor.monitor != NULL) {
g_file_monitor_cancel(zathura->file_monitor.monitor);
g_object_unref(zathura->file_monitor.monitor);
zathura->file_monitor.monitor = NULL;
}
if (zathura->file_monitor.file != NULL) {
g_object_unref(zathura->file_monitor.file);
zathura->file_monitor.file = NULL;
}
if (zathura->file_monitor.file_path != NULL) {
g_free(zathura->file_monitor.file_path);
zathura->file_monitor.file_path = NULL;
}
if (zathura->file_monitor.password != NULL) {
g_free(zathura->file_monitor.password);
zathura->file_monitor.password = NULL;

View file

@ -12,6 +12,7 @@
#include "macros.h"
#include "types.h"
#include "jumplist.h"
#include "file-monitor.h"
enum {
NEXT,
@ -177,9 +178,7 @@ struct zathura_s
* File monitor
*/
struct {
GFileMonitor* monitor; /**< File monitor */
GFile* file; /**< File for file monitor */
gchar* file_path; /**< Save file path */
ZathuraFileMonitor* monitor; /**< File monitor */
gchar* password; /**< Save password */
} file_monitor;