summaryrefslogtreecommitdiff
path: root/examples/get-volume.c
blob: 9dbdac63e0f020a21ce735ccfefbf7cdc79c083f (plain)
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
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice, this
// list of conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
//         SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
// HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
// LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
// OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
// DAMAGE.

#include <glib.h>
#include <libgupnp/gupnp.h>

char CONTENT_DIR[] = "urn:schemas-upnp-org:service:RenderingControl:1";

gboolean on_timeout(gpointer user_data)
{
        g_main_loop_quit ((GMainLoop*)(user_data));

        return FALSE;
}

void
gvalue_free (gpointer value)
{
        g_value_unset ((GValue *) value);
        g_free (value);
}

void
on_introspection (GObject *object, GAsyncResult *res, gpointer user_data)
{
        GError *error = NULL;

        GUPnPServiceIntrospection *i = gupnp_service_info_introspect_finish (
                GUPNP_SERVICE_INFO (object),
                res,
                &error);

        if (error != NULL) {
                g_critical ("%s", error->message);
                g_clear_error (&error);
        }

        const GUPnPServiceActionInfo *info =
                gupnp_service_introspection_get_action (i, "GetVolume");
        const char *state_variable_name =
                ((GUPnPServiceActionArgInfo *) info->arguments->next->data)
                        ->related_state_variable;

        const char *channel = gupnp_service_introspection_get_state_variable (
                                      i,
                                      state_variable_name)
                                      ->allowed_values->data;
        g_print ("Calling GetVolume for channel %s...", channel);

        GUPnPServiceProxyAction *a =
                gupnp_service_proxy_action_new ("GetVolume",
                                                "InstanceID", G_TYPE_INT, 0,
                                                "Channel", G_TYPE_STRING, channel,
                                                NULL);
        gupnp_service_proxy_call_action (GUPNP_SERVICE_PROXY (object),
                                         a,
                                         NULL,
                                         &error);

        GList *out_names = NULL;
        out_names = g_list_prepend (out_names, "CurrentVolume");
        GList *out_types = NULL;
        out_types =
                g_list_prepend (out_types, GSIZE_TO_POINTER (G_TYPE_STRING));
        GList *out_values = NULL;


        gupnp_service_proxy_action_get_result_list (a,
                                                    out_names,
                                                    out_types,
                                                    &out_values,
                                                    &error);
        g_list_free (out_types);
        g_list_free (out_names);

        if (error != NULL) {
                g_critical ("%s", error->message);
                g_clear_error (&error);
        } else {
                g_print ("Current volume: %s\n",
                         g_value_get_string (out_values->data));
        }
        g_list_free_full (out_values, gvalue_free);

        gupnp_service_proxy_action_unref (a);
        g_object_unref (i);
}


void on_proxy_available (GUPnPControlPoint *cp, GUPnPServiceProxy *proxy, gpointer user_data)
{
        g_autofree char *id =
                gupnp_service_info_get_id (GUPNP_SERVICE_INFO (proxy));

        g_print ("Got ServiceProxy %s at %s\n",
                 id,
                 gupnp_service_info_get_location (GUPNP_SERVICE_INFO (proxy)));
        g_print ("Introspecting service ...\n");
        gupnp_service_info_introspect_async (GUPNP_SERVICE_INFO (proxy),
                                             NULL,
                                             on_introspection,
                                             NULL);
}

int main(int argc, char *argv[])
{
        GError *error = NULL;
        GMainLoop *loop = g_main_loop_new (NULL, FALSE);

        GUPnPContext *context = gupnp_context_new ("wlp3s0", 0, &error);

        if (error != NULL) {
                g_error ("%s", error->message);
        }

        GUPnPControlPoint *cp = gupnp_control_point_new (context, CONTENT_DIR);
        g_signal_connect (cp, "service-proxy-available", G_CALLBACK (on_proxy_available), NULL);
        gssdp_resource_browser_set_active (GSSDP_RESOURCE_BROWSER (cp), TRUE);

        g_timeout_add_seconds (10, on_timeout, loop);

        g_main_loop_run (loop);

        g_object_unref (cp);
        g_object_unref (context);
        g_main_loop_unref (loop);

        return 0;
}