tmisu

Notification to stdout daemon
git clone https://git.inz.fi/tmisu/
Log | Files | Refs | README | LICENSE

commit 3eea16c68c32c074a33b7369e4c9fcd3a98f0f28
parent c7c0d8232fdc68ef475b748f8a62139fb5f820f4
Author: Santtu Lakkala <inz@inz.fi>
Date:   Tue, 22 Feb 2022 17:32:07 +0200

Fixes and cleanups

Diffstat:
MMakefile | 28++++++++++++++--------------
Msrc/output.c | 45+++++++++++++++++++++++++--------------------
Msrc/tmisu.c | 60++++++++++++++++++++++++------------------------------------
3 files changed, 63 insertions(+), 70 deletions(-)

diff --git a/Makefile b/Makefile @@ -1,26 +1,26 @@ - +.POSIX: TARGET = tmisu SRC := src/tmisu.c src/output.c -HEDRS := src/tmisu.h src/output.h +HDRS := src/tmisu.h src/output.h PREFIX ?= /usr/local CFLAGS += -W -Wall -std=c99 -IFLAGS = $(shell pkg-config --cflags dbus-1) -LFLAGS = $(shell pkg-config --libs dbus-1) -OBJ := $(patsubst %.c,%.o,$(SRC)) +IFLAGS = $$(pkg-config --cflags dbus-1) +LFLAGS = $$(pkg-config --libs dbus-1) +OBJ := ${SRC:.c=.o} -all: $(TARGET) +all: ${TARGET} -%.o: %.c $(HDRS) - $(CC) -c $< $(CFLAGS) $(IFLAGS) -o $@ +%.o: %.c ${HDRS} + ${CC} -c $< ${CFLAGS} ${IFLAGS} -o $@ -$(TARGET): $(OBJ) - $(CC) $(CFLAGS) $(IFLAGS) $(OBJ) $(LFLAGS) $(LDFLAGS) -o $(TARGET) +${TARGET}: ${OBJ} + ${CC} ${CFLAGS} ${IFLAGS} ${OBJ} ${LFLAGS} ${LDFLAGS} -o ${TARGET} -install: $(TARGET) - mkdir -p $(DESTDIR)$(PREFIX)/bin - install $(TARGET) $(DESTDIR)$(PREFIX)/bin/$(TARGET) +install: ${TARGET} + mkdir -p ${DESTDIR}${PREFIX}/bin + install ${TARGET} ${DESTDIR}${PREFIX}/bin/${TARGET} clean: - $(RM) -f $(OBJ) $(TARGET) + ${RM} -f ${OBJ} ${TARGET} diff --git a/src/output.c b/src/output.c @@ -21,9 +21,10 @@ static void print_sanitized(const char *string, const char *escape) { } } -static void hints_output_iterator(DBusMessageIter *hints, const char *key_prefix, const char *key_suffix, const char *str_prefix, const char *str_suffix, const char *escape, const char *delimiter) +static void hints_output_iterator(DBusMessageIter *hints, const char *key_prefix, const char *key_suffix, const char *str_prefix, const char *str_suffix, const char *escape, const char *delimiter, int join) { - for (; dbus_message_iter_get_arg_type(hints) != DBUS_TYPE_INVALID; dbus_message_iter_next(hints)) { + const char *d = ""; + for (; dbus_message_iter_get_arg_type(hints) != DBUS_TYPE_INVALID; dbus_message_iter_next(hints), d = delimiter) { DBusMessageIter dictentry; DBusMessageIter variant; DBusBasicValue value; @@ -34,12 +35,12 @@ static void hints_output_iterator(DBusMessageIter *hints, const char *key_prefix dbus_message_iter_next(&dictentry); dbus_message_iter_recurse(&dictentry, &variant); - printf("%s", key_prefix); + printf("%s%s", d, key_prefix); print_sanitized(key, escape); printf("%s", key_suffix); if (!dbus_type_is_basic(dbus_message_iter_get_arg_type(&variant))) { - printf("null%s", delimiter); + printf("null"); continue; } @@ -47,45 +48,48 @@ static void hints_output_iterator(DBusMessageIter *hints, const char *key_prefix switch (dbus_message_iter_get_arg_type(&variant)) { case DBUS_TYPE_BYTE: - printf("%" PRIu8 "%s", value.byt, delimiter); + printf("%" PRIu8, value.byt); break; case DBUS_TYPE_BOOLEAN: - printf("%s%s", value.bool_val ? "true" : "false", delimiter); + printf("%s", value.bool_val ? "true" : "false"); break; case DBUS_TYPE_INT16: - printf("%" PRId16 "%s", value.i16, delimiter); + printf("%" PRId16, value.i16); break; case DBUS_TYPE_UINT16: - printf("%" PRIu16 "%s", value.u16, delimiter); + printf("%" PRIu16, value.u16); break; case DBUS_TYPE_INT32: - printf("%" PRId32 "%s", value.i32, delimiter); + printf("%" PRId32, value.i32); break; case DBUS_TYPE_UINT32: - printf("%" PRIu32 "%s", value.u32, delimiter); + printf("%" PRIu32, value.u32); break; case DBUS_TYPE_INT64: - printf("%" PRId64 "%s", value.i64, delimiter); + printf("%" PRId64, value.i64); break; case DBUS_TYPE_UINT64: - printf("%" PRIu64 "%s", value.u64, delimiter); + printf("%" PRIu64, value.u64); break; case DBUS_TYPE_DOUBLE: - printf("%lf%s", value.dbl, delimiter); + printf("%lf", value.dbl); break; case DBUS_TYPE_STRING: case DBUS_TYPE_OBJECT_PATH: dbus_message_iter_get_basic(&variant, &value); printf("%s", str_prefix); print_sanitized(value.str, escape); - printf("%s%s", str_suffix, delimiter); + printf("%s", str_suffix); continue; break; default: - printf("null%s", delimiter); + printf("null%s", d); continue; } } + + if (!join && d == delimiter) + printf("%s", delimiter); } static void default_output(const char *app_name, const char *app_icon, dbus_uint32_t id, dbus_uint32_t replaces_id, @@ -102,7 +106,7 @@ static void default_output(const char *app_name, const char *app_icon, dbus_uint timeout, delimiter); printf("hints:%s", delimiter); - hints_output_iterator(hints, "\t", ": ", "", "", "\n\\", delimiter); + hints_output_iterator(hints, "\t", ": ", "", "", "\n\\", delimiter, 0); printf("actions:%s", delimiter); while (dbus_message_iter_get_arg_type(actions) != DBUS_TYPE_INVALID) { @@ -133,7 +137,7 @@ static void default_output(const char *app_name, const char *app_icon, dbus_uint static void json_output(const char *app_name, const char *app_icon, dbus_uint32_t id, dbus_uint32_t replaces_id, dbus_int32_t timeout, DBusMessageIter *hints, DBusMessageIter *actions, const char *summary, const char *body, const char *delimiter) { - + const char *sep = ""; printf("{" "\"id\": %" PRIu32 ", " "\"app_name\": \"", id); @@ -147,7 +151,7 @@ static void json_output(const char *app_name, const char *app_icon, dbus_uint32_ replaces_id, timeout); printf("\"hints\": {"); - hints_output_iterator(hints, "\"", "\": ", "\"", "\"", "\n\\\"", ", "); + hints_output_iterator(hints, "\"", "\": ", "\"", "\"", "\n\\\"", ", ", 1); printf("}, \"actions\": {"); while (dbus_message_iter_get_arg_type(actions) != DBUS_TYPE_INVALID) { @@ -163,9 +167,10 @@ static void json_output(const char *app_name, const char *app_icon, dbus_uint32_ printf("\""); print_sanitized(key, "\n\\\""); - printf("\""); + printf("\": \""); print_sanitized(value, "\n\\\""); - printf("\","); + printf("\"%s", sep); + sep = ", "; } printf("}, "); diff --git a/src/tmisu.c b/src/tmisu.c @@ -19,14 +19,14 @@ DBusHandlerResult handle_message(DBusConnection *connection, DBusMessage *messag { static unsigned notification_id = 0; struct conf *cnf = user_data; + DBusMessage *reply; if (dbus_message_is_method_call(message, "org.freedesktop.DBus.Introspectable", "Introspect")) { static const char *notificationpath = "/org/freedesktop/Notifications"; const char *path = dbus_message_get_path(message); size_t pl = strlen(path); - DBusMessage *reply; - if (strncmp(notificationpath, path, strlen(path))) + if (strncmp(notificationpath, path, pl)) return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; if (pl == 1) @@ -34,56 +34,39 @@ DBusHandlerResult handle_message(DBusConnection *connection, DBusMessage *messag if (notificationpath[pl] == '/') { size_t npl = strcspn(notificationpath + pl + 1, "/"); - char *buffer = malloc(sizeof(INTROSPECTION_NODE_XML) + npl); + char buffer[sizeof(INTROSPECTION_NODE_XML) + npl - 4]; sprintf(buffer, INTROSPECTION_NODE_XML, (int)npl, notificationpath + pl + 1); reply = dbus_message_new_method_return(message); dbus_message_append_args(reply, - DBUS_TYPE_STRING, &buffer, + DBUS_TYPE_STRING, &(const char *){ buffer }, DBUS_TYPE_INVALID); - free(buffer); - - dbus_connection_send(connection, reply, NULL); - dbus_message_unref(reply); - return DBUS_HANDLER_RESULT_HANDLED; - } - - if (notificationpath[pl]) + } else if (notificationpath[pl]) { return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; - - reply = dbus_message_new_method_return(message); - dbus_message_append_args(reply, - DBUS_TYPE_STRING, &(const char *){ INTROSPECTION_XML }, - DBUS_TYPE_INVALID); - dbus_connection_send(connection, reply, NULL); - dbus_message_unref(reply); - return DBUS_HANDLER_RESULT_HANDLED; + } else { + reply = dbus_message_new_method_return(message); + dbus_message_append_args(reply, + DBUS_TYPE_STRING, &(const char *){ INTROSPECTION_XML }, + DBUS_TYPE_INVALID); + } } else if (dbus_message_is_method_call(message, "org.freedesktop.Notifications", "GetServerInformation")) { - DBusMessage *reply = dbus_message_new_method_return(message); + reply = dbus_message_new_method_return(message); dbus_message_append_args(reply, DBUS_TYPE_STRING, &(const char *){ "tmisu" }, DBUS_TYPE_STRING, &(const char *){ "inz" }, DBUS_TYPE_STRING, &(const char *){ "1.0" }, DBUS_TYPE_STRING, &(const char *){ "1.2" }, DBUS_TYPE_INVALID); - dbus_connection_send(connection, reply, NULL); - dbus_message_unref(reply); - return DBUS_HANDLER_RESULT_HANDLED; } else if (dbus_message_is_method_call(message, "org.freedesktop.Notifications", "GetCapabilities")) { - DBusMessage *reply = dbus_message_new_method_return(message); DBusMessageIter i; DBusMessageIter j; + reply = dbus_message_new_method_return(message); dbus_message_iter_init_append(reply, &i); dbus_message_iter_open_container(&i, DBUS_TYPE_ARRAY, DBUS_TYPE_STRING_AS_STRING, &j); dbus_message_iter_append_basic(&j, DBUS_TYPE_STRING, &(const char *){ "body" }); dbus_message_iter_append_basic(&j, DBUS_TYPE_STRING, &(const char *){ "body-markup" }); dbus_message_iter_close_container(&i, &j); - - dbus_connection_send(connection, reply, NULL); - dbus_message_unref(reply); - return DBUS_HANDLER_RESULT_HANDLED; } else if (dbus_message_is_method_call(message, "org.freedesktop.Notifications", "Notify")) { - DBusMessage *reply; if (!dbus_message_has_signature(message, "susssasa{sv}i")) return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; reply = dbus_message_new_method_return(message); @@ -91,16 +74,17 @@ DBusHandlerResult handle_message(DBusConnection *connection, DBusMessage *messag dbus_message_append_args(reply, DBUS_TYPE_UINT32, &(dbus_uint32_t){ notification_id }, DBUS_TYPE_INVALID); - dbus_connection_send(connection, reply, NULL); - dbus_message_unref(reply); - - return DBUS_HANDLER_RESULT_HANDLED; + } else { + return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; } - return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; + dbus_connection_send(connection, reply, NULL); + dbus_message_unref(reply); + + return DBUS_HANDLER_RESULT_HANDLED; } -DBusConnection *connection = NULL; +static DBusConnection *connection = NULL; void sig_handler(int signal) { @@ -160,6 +144,10 @@ int main(int argc, char **argv) { while (dbus_connection_read_write_dispatch(connection, -1)); + dbus_bus_release_name(connection, "org.freedesktop.Notifications", NULL); + dbus_connection_remove_filter(connection, handle_message, &cnf); + dbus_bus_remove_match(connection, "interface=org.freedesktop.Notifications,path=/org/freedesktop/Notifications,type=method_call", NULL); + dbus_bus_remove_match(connection, "interface=org.freedesktop.DBus.Introspectable,method=Introspect,type=method_call", NULL); dbus_connection_unref(connection); return 0;