Merge branch 'dbus-variable' into 'develop'

Add $DBUS variable for exec

Closes #375

See merge request pwmt/zathura!85
This commit is contained in:
Sebastian Ramacher 2023-12-03 13:08:59 +01:00
commit 5012485b19
6 changed files with 47 additions and 17 deletions

View File

@ -230,7 +230,8 @@ close
exec
Execute an external command. ``$FILE`` expands to the current document path,
and ``$PAGE`` to the current page number
``$PAGE`` to the current page number, and ``$DBUS`` to the bus name of the
D-Bus interface
info
Show document information

View File

@ -232,7 +232,8 @@ They can also be combined with modifiers:
* ``exec``:
Execute an external command. ``$FILE`` expands to the current document path,
and ``$PAGE`` to the current page number.
``$PAGE`` to the current page number, and ``$DBUS`` to the bus name of the
D-Bus interface.
* ``focus_inputbar``

View File

@ -9,6 +9,7 @@
#include "commands.h"
#include "config.h"
#include "database.h"
#include "dbus-interface.h"
#include "document.h"
#include "internal.h"
#include "page-widget.h"
@ -544,6 +545,12 @@ cmd_exec(girara_session_t* session, girara_list_t* argument_list)
return false;
}
const char* bus_name = zathura_dbus_get_name(zathura);
GIRARA_LIST_FOREACH_BODY_WITH_ITER(
argument_list, char*, iter, value, char* s = girara_replace_substring(value, "$DBUS", bus_name); if (s != NULL) {
girara_list_iterator_set(iter, s);
});
if (zathura->document != NULL) {
const char* path = zathura_document_get_path(zathura->document);
unsigned int page = zathura_document_get_current_page_number(zathura->document);

View File

@ -40,6 +40,7 @@ typedef struct private_s {
GDBusConnection* connection;
guint owner_id;
guint registration_id;
char* bus_name;
} ZathuraDbusPrivate;
G_DEFINE_TYPE_WITH_CODE(ZathuraDbus, zathura_dbus, G_TYPE_OBJECT, G_ADD_PRIVATE(ZathuraDbus))
@ -71,6 +72,8 @@ finalize(GObject* object)
g_dbus_node_info_unref(priv->introspection_data);
}
g_free(priv->bus_name);
G_OBJECT_CLASS(zathura_dbus_parent_class)->finalize(object);
}
@ -91,6 +94,7 @@ zathura_dbus_init(ZathuraDbus* dbus)
priv->connection = NULL;
priv->owner_id = 0;
priv->registration_id = 0;
priv->bus_name = NULL;
}
static void
@ -168,13 +172,21 @@ zathura_dbus_new(zathura_t* zathura)
}
char* well_known_name = g_strdup_printf(DBUS_NAME_TEMPLATE, getpid());
priv->bus_name = well_known_name;
priv->owner_id = g_bus_own_name(G_BUS_TYPE_SESSION, well_known_name, G_BUS_NAME_OWNER_FLAGS_NONE, bus_acquired,
name_acquired, name_lost, dbus, NULL);
g_free(well_known_name);
return dbus;
}
const char*
zathura_dbus_get_name(zathura_t* zathura)
{
ZathuraDbusPrivate* priv = zathura_dbus_get_instance_private(zathura->dbus);
return priv->bus_name;
}
void
zathura_dbus_edit(ZathuraDbus* edit, unsigned int page, unsigned int x, unsigned int y)
{

View File

@ -40,6 +40,7 @@ struct zathura_dbus_class_s
GType zathura_dbus_get_type(void) G_GNUC_CONST;
ZathuraDbus* zathura_dbus_new(zathura_t* zathura);
const char* zathura_dbus_get_name(zathura_t* zathura);
/**
* Emit the 'Edit' signal on the D-Bus connection.

View File

@ -10,6 +10,7 @@
#include "callbacks.h"
#include "shortcuts.h"
#include "dbus-interface.h"
#include "document.h"
#include "zathura.h"
#include "render.h"
@ -1492,33 +1493,40 @@ sc_exec(girara_session_t* session, girara_argument_t* argument, girara_event_t*
return false;
}
girara_argument_t new_argument = *argument;
const char* bus_name = zathura_dbus_get_name(zathura);
char* s = girara_replace_substring(new_argument.data, "$DBUS", bus_name);
if (s == NULL) {
return false;
}
new_argument.data = s;
if (zathura->document != NULL) {
const char* path = zathura_document_get_path(zathura->document);
unsigned int page = zathura_document_get_current_page_number(zathura->document);
char page_buf[G_ASCII_DTOSTR_BUF_SIZE];
g_ascii_dtostr(page_buf, G_ASCII_DTOSTR_BUF_SIZE, page+1);
girara_argument_t new_argument = *argument;
char* r = girara_replace_substring(argument->data, "$FILE", path);
if (r == NULL) {
return false;
}
char* s = girara_replace_substring(r, "$PAGE", page_buf);
g_free(r);
s = girara_replace_substring(new_argument.data, "$FILE", path);
g_free(new_argument.data);
if (s == NULL) {
return false;
}
new_argument.data = s;
s = girara_replace_substring(new_argument.data, "$PAGE", page_buf);
g_free(new_argument.data);
if (s == NULL) {
return false;
}
new_argument.data = s;
const bool ret = girara_sc_exec(session, &new_argument, event, t);
g_free(s);
return ret;
}
return girara_sc_exec(session, argument, event, t);
const bool ret = girara_sc_exec(session, &new_argument, event, t);
g_free(new_argument.data);
return ret;
}
bool