This repository has been archived by the owner on Feb 13, 2022. It is now read-only.
forked from ashish-yadav11/dsblocks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
kdeconnect.c
170 lines (141 loc) · 3.88 KB
/
kdeconnect.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
#include <gio/gio.h>
#include <glib.h>
#include <stdio.h>
#include "../util.h"
#include "kdeconnect.h"
#define ICON0 ""
#define PROPERTY_BUS_INTERFACE "org.freedesktop.DBus.Properties"
#define PROPERTY_BUS_METHOD "Get"
#define TARGET_BUS_NAME "org.kde.kdeconnect"
#define TARGET_BUS_DEVICE_INTERFACE "org.kde.kdeconnect.device"
#define TARGET_BUS_BATTERY_INTERFACE "org.kde.kdeconnect.device.battery"
#define TARGET_BUS_ISREACHABLE "isReachable"
#define TARGET_BUS_BATTERY "charge"
#define TARGET_BUS_BATTERY_CHARGING "isCharging"
static gboolean isreachable(GDBusConnection *connection, GError *error, const char *obj);
static gint32 getbattery(GDBusConnection *connection, GError *error, const char *obj);
gboolean
isreachable(GDBusConnection *connection, GError *error, const char *obj)
{
GVariant *response = g_dbus_connection_call_sync(
connection,
TARGET_BUS_NAME,
obj,
PROPERTY_BUS_INTERFACE,
PROPERTY_BUS_METHOD,
g_variant_new(
"(ss)",
TARGET_BUS_DEVICE_INTERFACE,
TARGET_BUS_ISREACHABLE
),
G_VARIANT_TYPE_TUPLE,
G_DBUS_CALL_FLAGS_NONE,
-1,
NULL,
&error
);
if (response == NULL || error != NULL) {
printf("%s\n", error->message);
return FALSE;
}
GVariant *methodresponse;
gboolean reachable;
g_variant_get(response, "(v)", &methodresponse);
g_variant_get(methodresponse, "b", &reachable);
g_variant_unref(response);
g_variant_unref(methodresponse);
return reachable;
}
gint32
getbattery(GDBusConnection *connection, GError *error, const char *obj)
{
GVariant *response = g_dbus_connection_call_sync(
connection,
TARGET_BUS_NAME,
obj,
PROPERTY_BUS_INTERFACE,
PROPERTY_BUS_METHOD,
g_variant_new(
"(ss)",
TARGET_BUS_BATTERY_INTERFACE,
TARGET_BUS_BATTERY
),
G_VARIANT_TYPE_TUPLE,
G_DBUS_CALL_FLAGS_NONE,
-1,
NULL,
&error
);
if (response == NULL || error != NULL) {
printf("%s\n", error->message);
return -1;
}
GVariant *methodresponse;
gint32 charge;
g_variant_get(response, "(v)", &methodresponse);
g_variant_get(methodresponse, "i", &charge);
g_variant_unref(response);
g_variant_unref(methodresponse);
return charge;
}
gboolean
getcharging(GDBusConnection *connection, GError *error, const char *obj)
{
GVariant *response = g_dbus_connection_call_sync(
connection,
TARGET_BUS_NAME,
obj,
PROPERTY_BUS_INTERFACE,
PROPERTY_BUS_METHOD,
g_variant_new(
"(ss)",
TARGET_BUS_BATTERY_INTERFACE,
TARGET_BUS_BATTERY_CHARGING
),
G_VARIANT_TYPE_TUPLE,
G_DBUS_CALL_FLAGS_NONE,
-1,
NULL,
&error
);
if (response == NULL || error != NULL) {
printf("%s\n", error->message);
return FALSE;
}
GVariant *methodresponse;
gboolean charging;
g_variant_get(response, "(v)", &methodresponse);
g_variant_get(methodresponse, "b", &charging);
g_variant_unref(response);
g_variant_unref(methodresponse);
return charging;
}
void
kdeconnectu(char *str, int sigval, BlockData *blockdata)
{
if (blockdata->reachableobj == NULL)
return;
if (blockdata->dbus == NULL) {
blockdata->dbus = createdbusconnection();
if (blockdata->dbus == NULL)
return;
}
GError *error = NULL;
if (blockdata->dbus != NULL && isreachable(blockdata->dbus, error, blockdata->reachableobj)) {
gint32 charge = getbattery(blockdata->dbus, error, blockdata->batteryobj);
gboolean charging = getcharging(blockdata->dbus, error, blockdata->batteryobj);
if (charging) {
snprintf(str, CMDLENGTH, BLOCK_SUCCESS(ICON(ICON0), "%d%%"), charge);
} else {
snprintf(str, CMDLENGTH, BLOCK_NORM(ICON(ICON0), "%d%%"), charge);
}
} else {
printempty(str);
}
if (error != NULL)
g_error_free(error);
}
void
kdeconnectc(int button, BlockData *blockdata)
{
}