]> git.ipfire.org Git - thirdparty/shairport-sync.git/blame - shairport-sync-dbus-test-client.c
Update check_classic_systemd_full.yml
[thirdparty/shairport-sync.git] / shairport-sync-dbus-test-client.c
CommitLineData
1f75fe4e
MB
1/*
2 * This file is part of Shairport Sync.
3 * Copyright (c) Mike Brady 2019
4 * All rights reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person
7 * obtaining a copy of this software and associated documentation
8 * files (the "Software"), to deal in the Software without
9 * restriction, including without limitation the rights to use,
10 * copy, modify, merge, publish, distribute, sublicense, and/or
11 * sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be
15 * included in all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
19 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
21 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
22 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
24 * OTHER DEALINGS IN THE SOFTWARE.
25 */
c36a7822
MB
26#include "dbus-interface.h"
27#include <popt.h>
1e07e1e0 28#include <stdio.h>
63344824 29#include <stdlib.h>
1e07e1e0
MB
30#include <unistd.h>
31
32GMainLoop *loop;
33
8991f342 34void on_properties_changed(__attribute__((unused)) GDBusProxy *proxy, GVariant *changed_properties,
cbbe84d8 35 const gchar *const *invalidated_properties, gpointer user_data) {
1e07e1e0
MB
36 /* Note that we are guaranteed that changed_properties and
37 * invalidated_properties are never NULL
38 */
39
d343a851
MB
40 if (g_variant_n_children(changed_properties) > 0) {
41 GVariantIter *iter;
42 const gchar *key;
43 GVariant *value;
d343a851
MB
44 g_print(" *** Properties Changed:\n");
45 g_variant_get(changed_properties, "a{sv}", &iter);
46 while (g_variant_iter_loop(iter, "{&sv}", &key, &value)) {
47 gchar *value_str;
48 value_str = g_variant_print(value, TRUE);
cbbe84d8
MB
49 if (user_data)
50 g_print(" %s.%s -> %s\n", (char *)user_data, key, value_str);
51 else
52 g_print(" %s -> %s\n", key, value_str);
d343a851 53 g_free(value_str);
1e07e1e0 54 }
d343a851
MB
55 g_variant_iter_free(iter);
56 }
57
58 if (g_strv_length((GStrv)invalidated_properties) > 0) {
59 guint n;
60 g_print(" *** Properties Invalidated:\n");
61 for (n = 0; invalidated_properties[n] != NULL; n++) {
62 const gchar *key = invalidated_properties[n];
63 g_print(" %s\n", key);
1e07e1e0 64 }
d343a851 65 }
1e07e1e0
MB
66}
67
85b17cda 68void notify_loudness_callback(ShairportSync *proxy, __attribute__((unused)) gpointer user_data) {
263f4dd5 69 // printf("\"notify_loudness_callback\" called with a gpointer of
d343a851 70 // %lx.\n",(int64_t)user_data);
263f4dd5 71 gboolean ebl = shairport_sync_get_loudness(proxy);
1e07e1e0
MB
72 if (ebl == TRUE)
73 printf("Client reports loudness is enabled.\n");
74 else
75 printf("Client reports loudness is disabled.\n");
76}
77
8991f342
MB
78void notify_loudness_threshold_callback(ShairportSync *proxy,
79 __attribute__((unused)) gpointer user_data) {
d343a851
MB
80 gdouble th = shairport_sync_get_loudness_threshold(proxy);
81 printf("Client reports loudness threshold set to %.2f dB.\n", th);
1e07e1e0
MB
82}
83
8453778a
MB
84void notify_volume_callback(ShairportSyncAdvancedRemoteControl *proxy,
85 __attribute__((unused)) gpointer user_data) {
86 gdouble th = shairport_sync_advanced_remote_control_get_volume(proxy);
3198ec42 87 printf("Client reports volume set to %.2f.\n", th);
1e07e1e0
MB
88}
89
90pthread_t dbus_thread;
8991f342 91void *dbus_thread_func(__attribute__((unused)) void *arg) {
1e07e1e0
MB
92
93 loop = g_main_loop_new(NULL, FALSE);
d343a851 94
1e07e1e0
MB
95 g_main_loop_run(loop);
96
d343a851 97 // dbus_service_main(); // let it run inside a thread
d4065149 98 return NULL; // this is just to quieten a compiler warning.
1e07e1e0
MB
99}
100
63344824
MB
101int main(int argc, char *argv[]) {
102
103 GBusType gbus_type_selected = G_BUS_TYPE_SYSTEM; // set default
104 // get the options --system or --session for system bus or session bus
c36a7822
MB
105 signed char c; /* used for argument parsing */
106 poptContext optCon; /* context for parsing command-line options */
107
108 struct poptOption optionsTable[] = {
109 {"system", '\0', POPT_ARG_VAL, &gbus_type_selected, G_BUS_TYPE_SYSTEM,
110 "Listen on the D-Bus system bus -- pick this option or the \'--session\' option, but not "
111 "both. This is the default if no option is chosen.",
112 NULL},
113 {"session", '\0', POPT_ARG_VAL, &gbus_type_selected, G_BUS_TYPE_SESSION,
114 "Listen on the D-Bus session bus -- pick this option or the \'--system\' option, but not "
115 "both.",
116 NULL},
8991f342 117 POPT_AUTOHELP{NULL, 0, 0, NULL, 0, NULL, NULL}};
c36a7822
MB
118
119 optCon = poptGetContext(NULL, argc, (const char **)argv, optionsTable, 0);
120 poptSetOtherOptionHelp(optCon, "[--system | --session]");
121
63344824
MB
122 if (argc > 2) {
123 poptPrintHelp(optCon, stderr, 0);
57bcea52 124 exit(EXIT_FAILURE);
63344824
MB
125 }
126
c36a7822
MB
127 /* Now do options processing */
128 while ((c = poptGetNextOpt(optCon)) >= 0) {
129 }
63344824 130
c36a7822
MB
131 if (c < -1) {
132 /* an error occurred during option processing */
133 fprintf(stderr, "%s: %s\n", poptBadOption(optCon, POPT_BADOPTION_NOALIAS), poptStrerror(c));
134 return 1;
135 }
63344824 136
63344824 137 poptFreeContext(optCon);
c36a7822
MB
138
139 printf("Listening on the D-Bus %s bus.\n",
140 (gbus_type_selected == G_BUS_TYPE_SYSTEM) ? "system" : "session");
1e07e1e0
MB
141
142 pthread_create(&dbus_thread, NULL, &dbus_thread_func, NULL);
143
cbbe84d8 144 ShairportSync *proxy;
1e07e1e0
MB
145 GError *error = NULL;
146
cbbe84d8
MB
147 proxy = shairport_sync_proxy_new_for_bus_sync(gbus_type_selected, G_DBUS_PROXY_FLAGS_NONE,
148 "org.gnome.ShairportSync",
149 "/org/gnome/ShairportSync", NULL, &error);
1e07e1e0 150
d343a851
MB
151 // g_signal_connect(proxy, "notify::loudness-filter-active",
152 // G_CALLBACK(notify_loudness_filter_active_callback), NULL);
1e07e1e0 153
cbbe84d8
MB
154 g_signal_connect(proxy, "g-properties-changed", G_CALLBACK(on_properties_changed),
155 "ShairportSync");
d343a851 156 g_signal_connect(proxy, "notify::loudness-threshold",
cbbe84d8 157 G_CALLBACK(notify_loudness_threshold_callback), "ShairportSync");
1e07e1e0 158
d7a78841
MB
159 // Now, add notification of changes in diagnostics
160
161 ShairportSyncDiagnostics *proxy2;
162 GError *error2 = NULL;
163 proxy2 = shairport_sync_diagnostics_proxy_new_for_bus_sync(
164 gbus_type_selected, G_DBUS_PROXY_FLAGS_NONE, "org.gnome.ShairportSync",
165 "/org/gnome/ShairportSync", NULL, &error2);
cbbe84d8
MB
166 g_signal_connect(proxy2, "g-properties-changed", G_CALLBACK(on_properties_changed),
167 "ShairportSync.Diagnostics");
d7a78841
MB
168
169 // Now, add notification of changes in remote control
170
171 ShairportSyncRemoteControl *proxy3;
172 GError *error3 = NULL;
173 proxy3 = shairport_sync_remote_control_proxy_new_for_bus_sync(
174 gbus_type_selected, G_DBUS_PROXY_FLAGS_NONE, "org.gnome.ShairportSync",
175 "/org/gnome/ShairportSync", NULL, &error3);
cbbe84d8
MB
176 g_signal_connect(proxy3, "g-properties-changed", G_CALLBACK(on_properties_changed),
177 "ShairportSync.RemoteControl");
178
179 ShairportSyncAdvancedRemoteControl *proxy4;
180 GError *error4 = NULL;
181 proxy4 = shairport_sync_advanced_remote_control_proxy_new_for_bus_sync(
182 gbus_type_selected, G_DBUS_PROXY_FLAGS_NONE, "org.gnome.ShairportSync",
183 "/org/gnome/ShairportSync", NULL, &error4);
184 g_signal_connect(proxy4, "g-properties-changed", G_CALLBACK(on_properties_changed),
185 "ShairportSync.AdvancedRemoteControl");
186 g_signal_connect(proxy4, "notify::volume", G_CALLBACK(notify_volume_callback),
187 "ShairportSync.AdvancedRemoteControl");
d7a78841 188
1e07e1e0 189 g_print("Starting test...\n");
3198ec42 190
54d761ff
MB
191 g_print("Using the RemoteControl interface, play for five seconds, pause for five seconds and "
192 "then resume play...\n");
c75311c9
MB
193 g_print("Play...\n");
194 shairport_sync_remote_control_call_play(SHAIRPORT_SYNC_REMOTE_CONTROL(proxy3), NULL, NULL, 0);
195 sleep(5);
196 g_print("Pause...\n");
197 shairport_sync_remote_control_call_pause(SHAIRPORT_SYNC_REMOTE_CONTROL(proxy3), NULL, NULL, 0);
198 sleep(5);
199 g_print("Play...\n");
200 shairport_sync_remote_control_call_play(SHAIRPORT_SYNC_REMOTE_CONTROL(proxy3), NULL, NULL, 0);
201 sleep(5);
54d761ff
MB
202 g_print("Using the RemoteControl interface, set AirPlay Volume (range -30 to 0) to -30, -20, "
203 "-10, 0 and -15 for five seconds each...\n");
c75311c9 204 g_print("Set AirPlay Volume (range -30 to 0) to -30\n");
54d761ff
MB
205 shairport_sync_remote_control_call_set_airplay_volume(SHAIRPORT_SYNC_REMOTE_CONTROL(proxy3), -30,
206 NULL, NULL, 0);
c75311c9
MB
207 sleep(5);
208 g_print("Set AirPlay Volume (range -30 to 0) to -20\n");
54d761ff
MB
209 shairport_sync_remote_control_call_set_airplay_volume(SHAIRPORT_SYNC_REMOTE_CONTROL(proxy3), -20,
210 NULL, NULL, 0);
c75311c9
MB
211 sleep(5);
212 g_print("Set AirPlay Volume (range -30 to 0) to -10\n");
54d761ff
MB
213 shairport_sync_remote_control_call_set_airplay_volume(SHAIRPORT_SYNC_REMOTE_CONTROL(proxy3), -10,
214 NULL, NULL, 0);
c75311c9
MB
215 sleep(5);
216 g_print("Set AirPlay Volume (range -30 to 0) to -0\n");
54d761ff
MB
217 shairport_sync_remote_control_call_set_airplay_volume(SHAIRPORT_SYNC_REMOTE_CONTROL(proxy3), 0,
218 NULL, NULL, 0);
c75311c9
MB
219 sleep(5);
220 g_print("Set AirPlay Volume (range -30 to 0) to -15\n");
54d761ff
MB
221 shairport_sync_remote_control_call_set_airplay_volume(SHAIRPORT_SYNC_REMOTE_CONTROL(proxy3), -15,
222 NULL, NULL, 0);
c75311c9 223 sleep(5);
85b17cda 224
54d761ff
MB
225 g_print("Using the AdvancedRemoteControl interface, set Volume to 20%%, 100%%, 40%% and 60%% for "
226 "five seconds each...\n");
c75311c9 227 g_print("Set Volume to 20%%\n");
8453778a 228 shairport_sync_advanced_remote_control_call_set_volume(
cbbe84d8 229 SHAIRPORT_SYNC_ADVANCED_REMOTE_CONTROL(proxy4), 20, NULL, NULL, 0);
c75311c9
MB
230 sleep(5);
231 g_print("Set Volume to 100%%\n");
8453778a 232 shairport_sync_advanced_remote_control_call_set_volume(
cbbe84d8 233 SHAIRPORT_SYNC_ADVANCED_REMOTE_CONTROL(proxy4), 100, NULL, NULL, 0);
c75311c9
MB
234 sleep(5);
235 g_print("Set Volume to 40%%\n");
8453778a 236 shairport_sync_advanced_remote_control_call_set_volume(
cbbe84d8 237 SHAIRPORT_SYNC_ADVANCED_REMOTE_CONTROL(proxy4), 40, NULL, NULL, 0);
c75311c9
MB
238 sleep(5);
239 g_print("Set Volume to 50%%\n");
8453778a 240 shairport_sync_advanced_remote_control_call_set_volume(
c75311c9 241 SHAIRPORT_SYNC_ADVANCED_REMOTE_CONTROL(proxy4), 50, NULL, NULL, 0);
d734b7e7 242
c75311c9 243 sleep(5);
85b17cda
MB
244 g_print("Using the RemoteControl interface, increase volume for five seconds...\n");
245 shairport_sync_remote_control_call_volume_up(SHAIRPORT_SYNC_REMOTE_CONTROL(proxy3), NULL, NULL,
246 NULL);
d734b7e7 247 sleep(5);
85b17cda
MB
248 g_print("Using the RemoteControl interface, decrease volume...\n");
249 shairport_sync_remote_control_call_volume_down(SHAIRPORT_SYNC_REMOTE_CONTROL(proxy3), NULL, NULL,
250 NULL);
251
8453778a 252 /*
63344824 253 // sleep(1);
3198ec42
MB
254 shairport_sync_set_loudness_filter_active(SHAIRPORT_SYNC(proxy), TRUE);
255 sleep(10);
256 shairport_sync_set_loudness_threshold(SHAIRPORT_SYNC(proxy), -20.0);
257 sleep(5);
258 shairport_sync_set_loudness_filter_active(SHAIRPORT_SYNC(proxy), FALSE);
259 sleep(5);
260 shairport_sync_set_loudness_filter_active(SHAIRPORT_SYNC(proxy), TRUE);
261 sleep(5);
262 shairport_sync_set_loudness_threshold(SHAIRPORT_SYNC(proxy), -10.0);
263 sleep(10);
264 shairport_sync_set_loudness_filter_active(SHAIRPORT_SYNC(proxy), FALSE);
265 sleep(1);
266
267 shairport_sync_call_remote_command(SHAIRPORT_SYNC(proxy), "string",NULL,NULL,NULL);
cac65060 268 */
c75311c9 269 sleep(1);
8453778a 270 g_print("Finished test. Listening for property changes...\n");
c36a7822 271 // g_main_loop_quit(loop);
d343a851 272 pthread_join(dbus_thread, NULL);
1e07e1e0 273 printf("exiting program.\n");
d343a851 274
1e07e1e0 275 g_object_unref(proxy);
3198ec42 276
d4065149 277 return 0;
1e07e1e0 278}