aboutsummaryrefslogtreecommitdiff
path: root/src/sound-service-dbus.c
blob: 319e72a17050ad4ce3eeac5999d8ea4271ff0084 (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
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
/*
 * Copyright 2010 Canonical Ltd.
 *
 * Authors:
 *     Conor Curran <conor.curran@canonical.com>
 *     Ted Gould <ted.gould@canonical.com>
 *
 * This program is free software: you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 3, as published
 * by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranties of
 * MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
 * PURPOSE.  See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <gio/gio.h>
#include "gen-sound-service.xml.h"
#include "dbus-shared-names.h"
#include "sound-service-dbus.h"
#include "common-defs.h"
#include "pulse-manager.h"

// DBUS methods
static void bus_method_call (GDBusConnection * connection,
                             const gchar * sender,
                             const gchar * path,
                             const gchar * interface,
                             const gchar * method,
                             GVariant * params, 
                             GDBusMethodInvocation * invocation,
                             gpointer user_data);


typedef struct _SoundServiceDbusPrivate SoundServiceDbusPrivate;

struct _SoundServiceDbusPrivate {
  GDBusConnection *connection;
  gboolean        mute;
  gboolean        sink_availability;
};

/* Signals */
enum {
  SINK_INPUT_WHILE_MUTED,
  SINK_MUTE_UPDATE,
  SINK_AVAILABLE_UPDATE,
  LAST_SIGNAL
};

static GDBusNodeInfo *            node_info = NULL;
static GDBusInterfaceInfo *       interface_info = NULL;

static guint signals[LAST_SIGNAL] = { 0 };

#define SOUND_SERVICE_DBUS_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), SOUND_SERVICE_DBUS_TYPE, SoundServiceDbusPrivate))

static void sound_service_dbus_class_init (SoundServiceDbusClass *klass);
static void sound_service_dbus_init       (SoundServiceDbus *self);
static void sound_service_dbus_dispose    (GObject *object);
static void sound_service_dbus_finalize   (GObject *object);

/* GObject Boilerplate */
G_DEFINE_TYPE (SoundServiceDbus, sound_service_dbus, G_TYPE_OBJECT);

static void
sound_service_dbus_class_init (SoundServiceDbusClass *klass)
{
  GObjectClass *object_class = G_OBJECT_CLASS (klass);

  g_type_class_add_private (object_class, sizeof(SoundServiceDbusPrivate));

  object_class->dispose = sound_service_dbus_dispose;
  object_class->finalize = sound_service_dbus_finalize;

  g_assert(klass != NULL);
  dbus_g_object_type_install_info (SOUND_SERVICE_DBUS_TYPE,
                                   &dbus_glib__sound_service_server_object_info);

  signals[SINK_INPUT_WHILE_MUTED] =  g_signal_new ("sink-input-while-muted",
                                                   G_TYPE_FROM_CLASS (klass),
                                                   G_SIGNAL_RUN_LAST,
                                                   0,
                                                   NULL, NULL,
                                                   g_cclosure_marshal_VOID__BOOLEAN,
                                                   G_TYPE_NONE, 1, G_TYPE_BOOLEAN);

  signals[SINK_MUTE_UPDATE] =  g_signal_new ("sink-mute-update",
                                             G_TYPE_FROM_CLASS (klass),
                                             G_SIGNAL_RUN_LAST,
                                             0,
                                             NULL, NULL,
                                             g_cclosure_marshal_VOID__BOOLEAN,
                                             G_TYPE_NONE, 1, G_TYPE_BOOLEAN);
  signals[SINK_AVAILABLE_UPDATE] =  g_signal_new ("sink-available-update",
                                                  G_TYPE_FROM_CLASS (klass),
                                                  G_SIGNAL_RUN_LAST,
                                                  0,
                                                  NULL, NULL,
                                                  g_cclosure_marshal_VOID__BOOLEAN,
                                                  G_TYPE_NONE, 1, G_TYPE_BOOLEAN);
	if (node_info == NULL) {
		GError * error = NULL;

		node_info = g_dbus_node_info_new_for_xml(_indicator_service, &error);
		if (error != NULL) {
			g_error("Unable to parse Indicator Service Interface description: %s",
               error->message);
			g_error_free(error);
		}
	}

	if (interface_info == NULL) {
		interface_info = g_dbus_node_info_lookup_interface (node_info,
                                                        INDICATOR_SOUND_SERVICE_DBUS_INTERFACE);

		if (interface_info == NULL) {
			g_error("Unable to find interface '" INDICATOR_SOUND_SERVICE_DBUS_INTERFACE "'");
		}
	}
}

static void
sound_service_dbus_init (SoundServiceDbus *self)
{
  GError *error = NULL;
  SoundServiceDbusPrivate * priv = SOUND_SERVICE_DBUS_GET_PRIVATE(self);

  priv->connection = NULL;
  priv->mute = FALSE;
  priv->sink_availability = FALSE;

  /* Fetch the session bus */
  priv->connection = g_bus_get_sync (DBUS_BUS_SESSION, NULL, &error);

  if (error != NULL) {
    g_error("sound-service-dbus:Unable to connect to the session bus when
             creating indicator sound service : %s", error->message);
    g_error_free(error);
    return;
  }
  /* register the service on it */
  g_dbus_connection_register_object (priv->connection,
                                     INDICATOR_SOUND_SERVICE_DBUS_OBJECT,
                                     interface_info,
                                     NULL,
                                     NULL,
                                     NULL,
                                     &error);
	if (error != NULL) {
		g_error("Unable to register the object to DBus: %s", error->message);
		g_error_free(error);
		return;
	}
}

static void
sound_service_dbus_dispose (GObject *object)
{
  G_OBJECT_CLASS (sound_service_dbus_parent_class)->dispose (object);
  return;
}

static void
sound_service_dbus_finalize (GObject *object)
{
  G_OBJECT_CLASS (sound_service_dbus_parent_class)->finalize (object);
  return;
}

/* A method has been called from our dbus inteface.  Figure out what it
   is and dispatch it. */
static void
bus_method_call (GDBusConnection * connection,
                 const gchar * sender,
                 const gchar * path,
                 const gchar * interface,
                 const gchar * method,
                 GVariant * params,
                 GDBusMethodInvocation * invocation,
                 gpointer user_data) 
{ 
  IndicatorService * service = INDICATOR_SERVICE(user_data); 
  GVariant * retval = NULL;
  
  if (g_strcmp0(method, "GetSinkMute") == 0) {
    SoundServiceDbusPrivate *priv = SOUND_SERVICE_DBUS_GET_PRIVATE (self);
    g_debug("Get sink mute - sound service dbus!,
             about to send over mute_value of  %i", priv->mute);
    retval =  g_variant_new_boolean (priv->mute);    
  } 
  else if (g_strcmp0(method, "GetSinkAvailability") == 0) {
    SoundServiceDbusPrivate *priv = SOUND_SERVICE_DBUS_GET_PRIVATE (self);
    g_debug("Get sink availability - sound service dbus!,
            about to send over availability_value of  %i", priv->sink_availability);
    retval =  g_variant_new_boolean (priv->sink_availability);
  }
  else {
    g_warning("Calling method '%s' on the sound service but it's unknown", method); 
  }
  g_dbus_method_invocation_return_value(invocation, retval);
}


/**
SIGNALS
Utility methods to emit signals from the service into the ether.
**/
void sound_service_dbus_sink_input_while_muted(SoundServiceDbus* obj,
                                               gboolean block_value)
{
  g_debug("Emitting signal: SINK_INPUT_WHILE_MUTED, with  block_value: %i",
           block_value);
  SoundServiceDbusPrivate *priv = SOUND_SERVICE_DBUS_GET_PRIVATE (obj);
  GVariant* v_output = g_variant_new_boolean (block_value);

  GError * error = NULL;

  g_dbus_connection_emit_signal( priv->connection,
                                 INDICATOR_SOUND_DBUS_NAME,
                                 INDICATOR_SOUND_DBUS_OBJECT,
                                 INDICATOR_SOUND_SERVICE_DBUS_INTERFACE,
                                 "SinkInputWhileMuted",
                                  v_output,
                                  &error );
  if (error != NULL) {
    g_error("Unable to emit signal 'sinkinputwhilemuted' because : %s", error->message);
    g_error_free(error);
    return;
  }
}

void sound_service_dbus_update_sink_mute(SoundServiceDbus* obj,
                                         gboolean sink_mute)
{
  g_debug("Emitting signal: SINK_MUTE_UPDATE, with sink mute %i", sink_mute);
  SoundServiceDbusPrivate *priv = SOUND_SERVICE_DBUS_GET_PRIVATE (obj);
  GVariant* v_output = g_variant_new_boolean (sink_mute);

  GError * error = NULL;

  g_dbus_connection_emit_signal( priv->connection,
                                 INDICATOR_SOUND_DBUS_NAME,
                                 INDICATOR_SOUND_DBUS_OBJECT,
                                 INDICATOR_SOUND_SERVICE_DBUS_INTERFACE,
                                 "SinkMuteUpdate",
                                  v_output,
                                  &error );
  if (error != NULL) {
    g_error("Unable to emit signal 'sinkmuteupdate' because : %s", error->message);
    g_error_free(error);
    return;
  }
}

void sound_service_dbus_update_sink_availability(SoundServiceDbus* obj,
                                                 gboolean sink_availability)
{
  /*    g_debug("Emitting signal: SINK_AVAILABILITY_UPDATE, with value %i", sink_availability);*/
  g_debug("Emitting signal: SinkAvailableUpdate, with  %i", sink_availability);
  SoundServiceDbusPrivate *priv = SOUND_SERVICE_DBUS_GET_PRIVATE (obj);
  GVariant* v_output = g_variant_new_boolean (sink_availability);

  GError * error = NULL;

  g_dbus_connection_emit_signal( priv->connection,
                                 INDICATOR_SOUND_DBUS_NAME,
                                 INDICATOR_SOUND_DBUS_OBJECT,
                                 INDICATOR_SOUND_SERVICE_DBUS_INTERFACE,
                                 "SinkAvailableUpdate",
                                  v_output,
                                  &error );
  if (error != NULL) {
    g_error("Unable to emit signal 'SinkAvailableUpdate' because : %s", error->message);
    g_error_free(error);
    return;
  }
}