-
Notifications
You must be signed in to change notification settings - Fork 1
/
gdbus.c
272 lines (252 loc) · 10.1 KB
/
gdbus.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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
#include "gdbus.h"
#include "draw.h"
static void on_watch_sig_changed(GDBusProxy *p, gchar *sender_name, gchar *signal_name, GVariant *param, gpointer user_data);
static void on_item_sig_changed(GDBusProxy *p, gchar *sender_name, gchar *signal_name, GVariant *param, gpointer user_data);
static void init_item_data(const gchar *name, const gchar *path, ItemData *data);
static void watcher_appeared_handler(GDBusConnection *c, const gchar *name, const gchar *sender, gpointer user_data);
static void watcher_vanished_handler(GDBusConnection *c, const gchar *name, gpointer user_data);
static void on_name_acquired(GDBusConnection *c, const gchar *name, gpointer user_data);
static void on_name_lost(GDBusConnection *c, const gchar *name, gpointer user_data);
static gchar host[50] = "org.freedesktop.StatusNotifierHost-";
static const gchar watcher[] = "org.kde.StatusNotifierWatcher";
static const gchar watcher_path[] = "/StatusNotifierWatcher";
//put in ya_bar_t:
GList *list = NULL;
static gchar *theme = NULL;
//this will be height or something like that
static int size = 24;
void call_method(int click_type, int event_x, int event_y, int root_x, int root_y) {
printf("Event %d at (%d, %d), root (%d, %d)\n", click_type, event_x, event_y, root_x, root_y);
ItemData *i = g_list_nth_data(list, event_x / size);
printf("Interacted with %s\n", i->id);
// find specific application
// call org.kde.StatusNotifierItem.*
switch(click_type) {
case PRIMARY:
g_dbus_proxy_call_sync(i->proxy, "org.kde.StatusNotifierItem.Activate",
g_variant_new("(ii)", root_x, root_y), G_DBUS_CALL_FLAGS_NONE, -1, NULL, NULL);
break;
case SECONDARY:
g_dbus_proxy_call_sync(i->proxy, "org.kde.StatusNotifierItem.SecondaryActivate",
g_variant_new("(ii)", root_x, root_y), G_DBUS_CALL_FLAGS_NONE, -1, NULL, NULL);
break;
case CONTEXT:
g_dbus_proxy_call_sync(i->proxy, "org.kde.StatusNotifierItem.ContextMenu",
g_variant_new("(ii)", root_x, root_y), G_DBUS_CALL_FLAGS_NONE, -1, NULL, NULL);
break;
case SCROLL:
default:
printf("lel\n");
}
}
static void print_data(ItemData *data) {
printf("dbus_name: %s\n", data->dbus_name);
printf("category: %s\n", data->category);
printf("id: %s\n", data->id);
printf("title: %s\n", data->title);
printf("status: %s\n", data->status);
printf("window_id: %u\n", data->win_id);
printf("icon_name: %s\n", data->icon_name);
printf("icon_path: %s\n", data->icon_path);
printf("overlay_name: %s\n", data->overlay_name);
printf("att_name: %s\n", data->att_name);
printf("movie_name: %s\n", data->movie_name);
}
static void on_watch_sig_changed(GDBusProxy *p, gchar *sender_name, gchar *signal_name,
GVariant *param, gpointer user_data) {
const gchar *item;
gchar *just_name;
if(g_strcmp0(signal_name, "StatusNotifierItemRegistered") == 0) {
g_variant_get(param, "(&s)", &item);
just_name = g_strndup(item, g_strstr_len(item, -1, "/") - item);
printf("Item %s has been registered\n", item);
//extract param and add it to the list
ItemData *data = g_new0(ItemData, 1);
//extract object path from item name
init_item_data(just_name, g_strstr_len(item, -1, "/"), data);
list = g_list_prepend(list, data);
}
else if(g_strcmp0(signal_name, "StatusNotifierItemUnregistered") == 0) {
g_variant_get(param, "(&s)", &item);
just_name = g_strndup(item, g_strstr_len(item, -1, "/") - item);
printf("Item %s has been unregistered\n", item);
//remove param from the list
GList *l = list;
while(l != NULL) {
GList *next = l->next;
ItemData *d = l->data;
if(g_strcmp0(d->dbus_name, just_name) == 0) {
list = g_list_remove(user_data, l->data);
}
l = next;
}
}
draw_tray();
}
static gchar * get_property_string(GDBusProxy *p, gchar *prop) {
gchar *retstr = NULL;
GVariant *val = g_dbus_proxy_call_sync(p, "org.freedesktop.DBus.Properties.Get",
g_variant_new("(ss)", "org.kde.StatusNotifierItem", prop), G_DBUS_CALL_FLAGS_NONE,
-1, NULL, NULL);
if(val != NULL) {
GVariant *variant = NULL;
g_variant_get(val, "(v)", &variant);
if(variant != NULL) {
retstr = g_variant_dup_string(variant, NULL);
printf("%s: %s\n", prop, retstr);
g_variant_unref(variant);
}
g_variant_unref(val);
}
return retstr;
}
static inline void ensure_icon_path(GDBusProxy *p, gchar *icon, gchar **output) {
//printf("%s %s\n", icon, *output);
//if((icon != NULL) && (*output == NULL)) {
if(icon != NULL) {
*output = find_icon(icon, size, theme);
printf("%s\n", *output);
}
}
static inline void apply_cached_prop_pixmap(GDBusProxy *p, const gchar *name, gpointer output) {
GVariant *var = g_dbus_proxy_get_cached_property(p, name);
Pixmap *pix = g_new0(Pixmap, 1);
//gint32 w, h;
if(var != NULL) {
GVariantIter *iter;
g_variant_get(var, "a(iiay)", &iter);
while(g_variant_iter_loop(iter, "(iiay)", &(pix->width), &(pix->height), pix->pixmap))
printf("%s: %d x %d\n", name, pix->width, pix->height);
g_variant_iter_free(iter);
g_variant_unref(var);
return;
}
output = NULL;
}
static void on_item_sig_changed(GDBusProxy *p, gchar *sender_name, gchar *signal_name,
GVariant *param, gpointer user_data) {
GVariant *item = NULL;
ItemData *data = user_data;
//const gchar *prop;
printf("Item %s emitted signal %s\n", sender_name, signal_name);
if(g_strcmp0(signal_name, "NewTitle") == 0) {
data->title = get_property_string(p, "Title");
printf("New title: %s\n", data->title);
}
else if(g_strcmp0(signal_name, "NewIcon") == 0) {
if((data->icon_name = get_property_string(p, "IconName")) != NULL)
ensure_icon_path(p, data->icon_name, &(data->icon_path));
apply_cached_prop_pixmap(p, "IconPixmap", &(data->icon_pixmap));
printf("New icon name: %s\n", data->icon_name);
}
else if(g_strcmp0(signal_name, "NewAttentionIcon") == 0) {
//maybe check for pixmap and/or movie too
data->att_name = get_property_string(p, "AttentionIconName");
printf("New attention icon name: %s\n", data->att_name);
}
else if(g_strcmp0(signal_name, "NewOverlayIcon") == 0) {
//maybe check for pixmap too
data->overlay_name = get_property_string(p, "OverlayIconName");
printf("New overlay icon name: %s\n", data->overlay_name);
}
else if(g_strcmp0(signal_name, "NewToolTip") == 0) {
/*
g_variant_get(item, "(&s)", &prop);
printf("New tooltip: %s\n", prop);
*/
}
else if(g_strcmp0(signal_name, "NewStatus") == 0) {
data->status = get_property_string(p, "Status");
printf("New status: %s\n", data->status);
}
g_free(item);
draw_tray();
}
static void init_item_data(const gchar *name, const gchar *path, ItemData *data) {
printf("name: %s, path: %s\n", name, path);
GDBusProxy *proxy = g_dbus_proxy_new_for_bus_sync(G_BUS_TYPE_SESSION,
G_DBUS_PROXY_FLAGS_NONE, NULL, name, path,
//G_DBUS_PROXY_FLAGS_GET_INVALIDATED_PROPERTIES, NULL, name, path,
"org.kde.StatusNotifierItem", NULL, NULL);
g_signal_connect(proxy, "g-signal", G_CALLBACK(on_item_sig_changed), data);
data->proxy = proxy;
data->dbus_name = g_strdup(name);
data->category = get_property_string(proxy, "Category");
data->id = get_property_string(proxy, "Id");
data->title = get_property_string(proxy, "Title");
data->status = get_property_string(proxy, "Status");
//windowid
//printf("%s\n", get_property_string(proxy, "IconName"));
if((data->icon_name = get_property_string(proxy, "IconName")) != NULL) {
printf("%s\n", data->icon_name);
ensure_icon_path(proxy, data->icon_name, &(data->icon_path));
}
data->theme_path = get_property_string(proxy, "IconThemePath");
apply_cached_prop_pixmap(proxy, "IconPixmap", &(data->icon_pixmap));
data->overlay_name = get_property_string(proxy, "OverlayIconName");
data->att_name = get_property_string(proxy, "AttentionIconName");
data->movie_name = get_property_string(proxy, "AttentionMovieName");
//tooltip
//print_data(data);
}
static void watcher_appeared_handler(GDBusConnection *c, const gchar *name, const gchar *sender, gpointer user_data) {
GDBusProxy *proxy;
g_dbus_connection_call_sync(c, watcher, watcher_path, watcher, "RegisterStatusNotifierHost",
g_variant_new("(s)", host), NULL, G_DBUS_CALL_FLAGS_NONE, -1, NULL, NULL);
proxy = g_dbus_proxy_new_for_bus_sync(G_BUS_TYPE_SESSION, G_DBUS_PROXY_FLAGS_NONE, NULL, watcher,
watcher_path, watcher, NULL, NULL);
g_signal_connect(proxy, "g-signal", G_CALLBACK(on_watch_sig_changed), user_data);
//initialize the list
GVariant *items = g_dbus_proxy_get_cached_property(proxy, "RegisteredStatusNotifierItems");
GVariantIter *it = g_variant_iter_new(items);
GVariant *content;
ItemData *data;
int i = 0;
while((content = g_variant_iter_next_value(it))) {
const gchar *it_name = g_variant_get_string(content, NULL);
gchar *just_name = g_strndup(it_name, g_strstr_len(it_name, -1, "/") - it_name);
printf("%d: %s\n", i, it_name);
data = g_new0(ItemData, 1);
//extract object path from item name
init_item_data(just_name, g_strstr_len(it_name, -1, "/"), data);
list = g_list_prepend(list, data);
i++;
}
g_variant_iter_free(it);
g_variant_unref(items);
draw_tray();
}
static void watcher_vanished_handler(GDBusConnection *c, const gchar *name, gpointer user_data) {
printf("Watcher is nowhere to be found... rip\n");
exit(1);
}
static void on_name_acquired(GDBusConnection *c, const gchar *name, gpointer user_data) {
printf("I am acquired\n");
guint watcher_id = g_bus_watch_name(G_BUS_TYPE_SESSION, watcher, G_BUS_NAME_OWNER_FLAGS_NONE,
watcher_appeared_handler, watcher_vanished_handler, user_data, NULL);
}
static void on_name_lost(GDBusConnection *c, const gchar *name, gpointer user_data) {
printf("Couldn't get name\n");
exit(1);
}
int main() {
theme = get_icon_theme();
printf("%s\n", theme);
//gchar *icon = find_icon("nm-signal-50", 24, theme);
//printf("%s\n", icon);
GMainLoop *loop;
GWaterXcbSource *source;
guint id;
sprintf(host + strlen(host), "%ld", (long) getpid());
printf("name: %s\n", host);
init_window();
loop = g_main_loop_new(NULL, FALSE);
source = g_water_xcb_source_new_for_connection(NULL, c, callback, NULL, NULL);
id = g_bus_own_name(G_BUS_TYPE_SESSION, (const gchar *) host,
G_BUS_NAME_OWNER_FLAGS_NONE, NULL, on_name_acquired, on_name_lost, NULL, NULL);
g_main_loop_run(loop);
g_bus_unown_name(id);
g_main_loop_unref(loop);
g_water_xcb_source_free(source);
return 0;
}