forked from Sweets/tiramisu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcallbacks.c
239 lines (201 loc) · 6.64 KB
/
callbacks.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
#include <stdio.h>
#include "tiramisu.h"
#include "callbacks.h"
unsigned int notification_id = 0;
char *sanitize(char *string, char *out) {
memset(out, 0, strlen(out));
while (*string) {
if (*string == '"')
strcat(out, "\\\"");
else if (*string == '\n')
strcat(out, "\\n");
else
out[strlen(out)] = *string;
string++;
}
return out;
}
void method_handler(GDBusConnection *connection, const gchar *sender,
const gchar *object, const gchar *interface, const gchar *method,
GVariant *parameters, GDBusMethodInvocation *invocation,
gpointer user_data) {
GVariantIter iterator;
gchar *app_name;
guint32 replaces_id;
gchar *app_icon;
gchar *summary;
gchar *body;
gchar **actions;
GVariant *hints;
gint32 timeout;
GVariant *return_value = NULL;
if (!strcmp(method, "Notify"))
goto output;
if (!strcmp(method, "GetServerInformation")) {
return_value = g_variant_new("(ssss)",
"tiramisu", "Sweets", "1.0", "1.2");
goto flush;
}
print("Unhandled: %s %s\n", method, sender);
output:
notification_id++;
g_variant_iter_init(&iterator, parameters);
g_variant_iter_next(&iterator, "s", &app_name);
g_variant_iter_next(&iterator, "u", &replaces_id);
g_variant_iter_next(&iterator, "s", &app_icon);
g_variant_iter_next(&iterator, "s", &summary);
g_variant_iter_next(&iterator, "s", &body);
g_variant_iter_next(&iterator, "^a&s", &actions);
g_variant_iter_next(&iterator, "@a{sv}", &hints);
g_variant_iter_next(&iterator, "i", &timeout);
char *sanitized = (char *)calloc(512, sizeof(char));
printf(
#ifdef PRINT_JSON
"{ \"app_name\": \"%s\", \"app_icon\": \"%s\", ",
#else
"app_name: %s\napp_icon: %s\n",
#endif
sanitize(app_name, sanitized),
sanitize(app_icon, sanitized));
printf(
#ifdef PRINT_JSON
"\"replaces_id\": \"%u\", \"timeout\": \"%d\", ",
#else
"replaces_id: %u\ntimeout: %d\n",
#endif
replaces_id, timeout);
#ifdef PRINT_JSON
printf("\"hints\": { ");
#else
printf("hints: ");
#endif
gchar *key;
GVariant *value;
#ifdef PRINT_JSON
const char *int_format = "\"%s\": %d";
const char *uint_format = "\"%s\": %u";
#else
const char *int_format = "\t%s: %d\n";
const char *uint_format = "\t%s: %u\n";
#endif
unsigned int index = 0;
g_variant_iter_init(&iterator, hints);
while (g_variant_iter_loop(&iterator, "{sv}", &key, NULL)) {
#ifdef PRINT_JSON
if (index > 0)
printf(", ");
#endif
/* There has to be a better way. glib, why? */
if ((value = g_variant_lookup_value(hints, key, GT_STRING)))
printf(
#ifdef PRINT_JSON
"\"%s\": \"%s\"",
#else
"\t%s: %s\n",
#endif
key, sanitize(g_variant_dup_string(value, NULL), sanitized));
else if ((value = g_variant_lookup_value(hints, key, GT_INT16)))
printf(int_format, key, g_variant_get_int16(value));
else if ((value = g_variant_lookup_value(hints, key, GT_INT32)))
printf(int_format, key, g_variant_get_int32(value));
else if ((value = g_variant_lookup_value(hints, key, GT_INT64)))
printf(int_format, key, g_variant_get_int64(value));
else if ((value = g_variant_lookup_value(hints, key, GT_UINT16)))
printf(uint_format, key, g_variant_get_uint16(value));
else if ((value = g_variant_lookup_value(hints, key, GT_UINT32)))
printf(uint_format, key, g_variant_get_uint32(value));
else if ((value = g_variant_lookup_value(hints, key, GT_UINT64)))
printf(uint_format, key, g_variant_get_uint64(value));
else if ((value = g_variant_lookup_value(hints, key, GT_DOUBLE)))
printf(
#ifdef PRINT_JSON
"\"%s\": %f",
#else
"\t%s: %f\n",
#endif
key, g_variant_get_double(value));
else if ((value = g_variant_lookup_value(hints, key, GT_BYTE)))
printf(
#ifdef PRINT_JSON
"\"%s\": %x",
#else
"\t%s: %x\n",
#endif
key, g_variant_get_byte(value));
else if ((value = g_variant_lookup_value(hints, key, GT_BOOL)))
printf(
#ifdef PRINT_JSON
"\"%s\": %d",
#else
"\t%s: %d\n",
#endif
key, g_variant_get_boolean(value));
index++;
}
index = 0;
#ifdef PRINT_JSON
printf("}, \"actions\": {");
#else
printf("actions: ");
#endif
while (actions[index] && actions[index + 1]) {
#ifdef PRINT_JSON
if (index > 0)
printf(", ");
printf("\"%s\": \"%s\"",
#else
printf("\t%s: %s\n",
#endif
actions[index + 1], actions[index]);
index += 2;
}
printf(
#ifdef PRINT_JSON
"}, \"summary\": \"%s\", \"body\": \"%s\" }\n",
#else
"summary: %s\nbody: %s\n",
#endif
sanitize(summary, sanitized),
sanitize(body, sanitized));
return_value = g_variant_new("(u)", notification_id);
fflush(stdout);
free(sanitized);
goto flush;
flush:
g_dbus_method_invocation_return_value(invocation, return_value);
g_dbus_connection_flush(connection, NULL, NULL, NULL);
return;
}
void bus_acquired(GDBusConnection *connection, const gchar *name,
gpointer user_data) {
print("%s\n", "Bus has been acquired.");
guint registered_object;
registered_object = g_dbus_connection_register_object(connection,
"/org/freedesktop/Notifications",
introspection->interfaces[0],
&(const GDBusInterfaceVTable){ method_handler },
NULL,
NULL,
NULL);
if (!registered_object) {
print("%s\n", "Unable to register.");
stop_main_loop(NULL);
}
}
void name_acquired(GDBusConnection *connection, const gchar *name,
gpointer user_data) {
dbus_connection = connection;
print("%s\n", "Name has been acquired.");
}
void name_lost(GDBusConnection *connection, const gchar *name,
gpointer user_data) {
// we lost the Notifications daemon name or couldn't acquire it, shutdown
if (!connection) {
printf("%s; %s\n",
"Unable to connect to acquire org.freedesktop.Notifications",
"could not connect to dbus.");
stop_main_loop(NULL);
}
else
print("%s\n", "Successfully acquired org.freedesktop.Notifications");
}