commit 56eb233c1645ebba2be47d4f35ccb40cdd02564d
parent 3f125e06f49a363e8ccfaa4024d5ca2ca2df3045
Author: Sweets <Sweets@users.noreply.github.com>
Date: Wed, 24 Jun 2020 07:15:51 -0700
Merge pull request #2 from bit9tream/master
add ability to output information in json
Diffstat:
3 files changed, 39 insertions(+), 0 deletions(-)
diff --git a/README.md b/README.md
@@ -41,6 +41,8 @@ Summary
Body
```
+also tiramisu can output information in json so it can be easily parsed.
+
If any specific setting is disabled in the configuration, the line is omitted
from the output (making all of the output one line shorter).
diff --git a/config.h b/config.h
@@ -2,4 +2,5 @@
// #define RECEIVE_APP_ICON
// #define RECEIVE_REPLACES_ID
// #define RECEIVE_EXPIRE_TIMEOUT
+#define PRINT_JSON
#define OUTPUT_DELIMITER "\n"
diff --git a/format.c b/format.c
@@ -3,6 +3,19 @@
#include "config.h"
#include "format.h"
+char* escape_quotes(char *str, char *out) {
+ memset(out, 0, strlen(out));
+ while (*str) {
+ if (*str == '"')
+ strcat(out, "\\\"");
+ else
+ out[strlen(out)] = *str;
+ str++;
+ }
+
+ return out;
+}
+
void output_notification(gchar *app_name, guint32 replaces_id, gchar *app_icon,
gchar *summary, gchar *body, GVariant *actions, GVariant *hints,
gint32 timeout) {
@@ -11,6 +24,7 @@ void output_notification(gchar *app_name, guint32 replaces_id, gchar *app_icon,
/* 2048 characters should be significantly long enough*/
char *string = (char *)calloc(2048, sizeof(char));
+#ifndef PRINT_JSON
strcat(string, app_name);
#ifdef RECEIVE_APP_ICON
@@ -26,9 +40,31 @@ void output_notification(gchar *app_name, guint32 replaces_id, gchar *app_icon,
sprintf(string, "%s%s%d%s%s%s%s", string, OUTPUT_DELIMITER,
timeout, OUTPUT_DELIMITER, summary, OUTPUT_DELIMITER, body);
+#else
+ char *escaped_str = (char *)calloc(512, sizeof(char));
+
+ sprintf(string, "{ \"app_name\": \"%s\"", escape_quotes(app_name, escaped_str));
+
+#ifdef RECEIVE_APP_ICON
+ sprintf(string, "%s, \"app_icon\": \"%s\"", string, escape_quotes(app_icon, escaped_str));
+#endif
+
+ /* TODO: actions */
+ /* TODO: hints */
+
+#ifdef RECEIVE_REPLACES_ID
+ sprintf(string, "%s, \"replaces_id\": %lu", string, replaces_id);
+#endif
+
+ sprintf(string, "%s, \"timeout\": %d, \"summary\": \"%s\"", string, timeout, escape_quotes(summary, escaped_str));
+
+ sprintf(string, "%s, \"body\": \"%s\" }", string, escape_quotes(body, escaped_str));
+
+#endif
printf("%s\n", string);
fflush(stdout);
free(string);
+ free(escaped_str);
}