/
nsv-pulse-context.c
324 lines (262 loc) · 8.23 KB
/
nsv-pulse-context.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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
#include <glib-object.h>
#include <pulse/pulseaudio.h>
#include <pulse/glib-mainloop.h>
#include <pulse/ext-stream-restore.h>
#include <string.h>
#include "nsv-pulse-context.h"
#define NSV_TYPE_PULSE_CONTEXT (nsv_pulse_context_get_type ())
#define NSV_PULSE_CONTEXT(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), \
NSV_TYPE_PULSE_CONTEXT, NsvPulseContext))
typedef struct _NsvPulseContextClass NsvPulseContextClass;
typedef struct _NsvPulseContextPrivate NsvPulseContextPrivate;
enum
{
PROP_0,
PROP_CONTEXT
};
struct _NsvPulseContextClass {
GObjectClass parent_class;
};
struct _NsvPulseContext
{
GObject parent_instance;
NsvPulseContextPrivate *priv;
};
struct _NsvPulseContextPrivate
{
pa_glib_mainloop *pa_mainloop;
pa_context *pa_context;
pa_ext_stream_restore_info stream_restore;
gboolean stream_restore_write_pending;
};
G_DEFINE_TYPE(NsvPulseContext, nsv_pulse_context, G_TYPE_OBJECT);
static GObjectClass *parent_class = NULL;
static guint connecting_id;
static guint ready_id;
static guint terminated_id;
static guint failed_id;
static guint error_id;
static gboolean nsv_pulse_context_reinitialize(gpointer user_data);
static void nsv_pulse_context_terminate(NsvPulseContext *self);
static void
nsv_pulse_context_finalize(GObject *object)
{
NsvPulseContext *self = NSV_PULSE_CONTEXT(object);
nsv_pulse_context_terminate(self);
g_free(self->priv);
self->priv = NULL;
G_OBJECT_CLASS(parent_class)->finalize(object);
}
static void
nsv_pulse_context_get_property(GObject *object, guint prop_id, GValue *value,
GParamSpec *pspec)
{
switch (prop_id)
{
case PROP_CONTEXT:
{
g_value_set_pointer(value, NSV_PULSE_CONTEXT(object)->priv->pa_context);
break;
}
default:
{
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
}
static void
nsv_pulse_context_class_init(NsvPulseContextClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS(klass);
parent_class = g_type_class_peek_parent(klass);
object_class->finalize = nsv_pulse_context_finalize;
object_class->get_property = nsv_pulse_context_get_property;
g_object_class_install_property(
object_class, PROP_CONTEXT,
g_param_spec_pointer("context",
NULL, NULL,
G_PARAM_READABLE));
connecting_id = g_signal_new("connecting",
G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
0, NULL, NULL,
&g_cclosure_marshal_VOID__VOID,
G_TYPE_NONE, 0, G_TYPE_NONE);
ready_id = g_signal_new("ready",
G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
0, NULL, NULL,
&g_cclosure_marshal_VOID__VOID,
G_TYPE_NONE, 0, G_TYPE_NONE);
terminated_id = g_signal_new("terminated",
G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
0, NULL, NULL,
&g_cclosure_marshal_VOID__VOID,
G_TYPE_NONE, 0, G_TYPE_NONE);
failed_id = g_signal_new("failed",
G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
0, NULL, NULL,
&g_cclosure_marshal_VOID__VOID,
G_TYPE_NONE, 0, G_TYPE_NONE);
error_id = g_signal_new("error",
G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
0, NULL, NULL,
&g_cclosure_marshal_VOID__VOID,
G_TYPE_NONE, 0, G_TYPE_NONE);
}
static void
nsv_pulse_context_pa_ext_stream_restore_write(
NsvPulseContext *self, const pa_ext_stream_restore_info *info)
{
NsvPulseContextPrivate *priv = self->priv;
pa_operation *pa_op =
pa_ext_stream_restore_write(priv->pa_context, PA_UPDATE_REPLACE,
info, 1, TRUE, NULL, NULL);
if (pa_op)
pa_operation_unref(pa_op);
}
static void
_nsv_pulse_context_pa_context_state_cb(pa_context *c, void *userdata)
{
NsvPulseContext *self = NSV_PULSE_CONTEXT(userdata);
NsvPulseContextPrivate *priv = self->priv;
switch (pa_context_get_state(c))
{
case PA_CONTEXT_READY:
{
if (priv->stream_restore_write_pending)
{
priv->stream_restore_write_pending = FALSE;
nsv_pulse_context_pa_ext_stream_restore_write(self,
&priv->stream_restore);
}
g_signal_emit(self, ready_id, 0);
break;
}
case PA_CONTEXT_FAILED:
{
g_signal_emit(self, failed_id, 0);
g_timeout_add_seconds(1, nsv_pulse_context_reinitialize, self);
break;
}
case PA_CONTEXT_TERMINATED:
{
g_signal_emit(self, terminated_id, 0);
break;
}
case PA_CONTEXT_CONNECTING:
{
g_signal_emit(self, connecting_id, 0);
break;
}
default: /*make the compiler happy */
break;
}
}
static void
nsv_pulse_context_real_init(NsvPulseContext *self)
{
NsvPulseContextPrivate *priv = self->priv;
pa_proplist *proplist;
pa_mainloop_api *api;
priv->pa_mainloop = pa_glib_mainloop_new(g_main_context_default());
if (!priv->pa_mainloop)
goto emit_error;
api = pa_glib_mainloop_get_api(priv->pa_mainloop);
if (!api)
goto emit_error;
proplist = pa_proplist_new();
pa_proplist_sets(proplist, "application.name", "notification-daemon");
pa_proplist_sets(proplist, "application.id", "notification-daemon");
pa_proplist_sets(proplist, "application.version", "0.50");
priv->pa_context = pa_context_new_with_proplist(api, "notification-daemon",
proplist);
pa_proplist_free(proplist);
if (!priv->pa_context)
goto emit_error;
pa_context_set_state_callback(
priv->pa_context, _nsv_pulse_context_pa_context_state_cb, self);
if (pa_context_connect(priv->pa_context, NULL,
PA_CONTEXT_NOFAIL|PA_CONTEXT_NOAUTOSPAWN, 0) < 0)
{
goto emit_error;
}
return;
emit_error:
g_signal_emit(self, error_id, 0);
}
static void
nsv_pulse_context_terminate(NsvPulseContext *self)
{
NsvPulseContextPrivate *priv = self->priv;
if (priv->pa_context)
{
pa_context_set_state_callback(priv->pa_context, NULL, NULL);
pa_context_disconnect(priv->pa_context);
pa_context_unref(priv->pa_context);
priv->pa_context = NULL;
}
if (priv->pa_mainloop)
{
pa_glib_mainloop_free(priv->pa_mainloop);
priv->pa_mainloop = NULL;
}
}
static gboolean
nsv_pulse_context_reinitialize(gpointer user_data)
{
NsvPulseContext *self = NSV_PULSE_CONTEXT(user_data);
nsv_pulse_context_terminate(self);
nsv_pulse_context_real_init(self);
return FALSE;
}
static void
nsv_pulse_context_init(NsvPulseContext *self)
{
self->priv = g_new0(NsvPulseContextPrivate, 1);
nsv_pulse_context_real_init(self);
}
gboolean
nsv_pulse_context_is_ready(NsvPulseContext *self)
{
if (self->priv->pa_context)
return pa_context_get_state(self->priv->pa_context) == PA_CONTEXT_READY;
return FALSE;
}
void
nsv_pulse_context_set_rule_volume(NsvPulseContext *self, const char *rule,
int volume)
{
NsvPulseContextPrivate *priv = self->priv;
pa_cvolume cvol;
pa_volume_t v;
if (volume < 0)
volume = 0;
v = ((double)volume / 100.0 * 65536.0);
pa_cvolume_set(&cvol, 1, v);
priv->stream_restore.name = rule;
priv->stream_restore.channel_map.channels = 1;
priv->stream_restore.channel_map.map[0] = 0;
priv->stream_restore.volume = cvol;
priv->stream_restore.device = NULL;
priv->stream_restore.mute = FALSE;
if (priv->pa_context &&
pa_context_get_state(priv->pa_context) == PA_CONTEXT_READY)
{
nsv_pulse_context_pa_ext_stream_restore_write(self, &priv->stream_restore);
}
else
priv->stream_restore_write_pending = TRUE;
}
pa_context *
nsv_pulse_context_get_context(NsvPulseContext *self)
{
return self->priv->pa_context;
}
NsvPulseContext *
nsv_pulse_context_get_instance()
{
static NsvPulseContext *instance = NULL;
if (!instance)
instance = NSV_PULSE_CONTEXT(g_object_new(NSV_TYPE_PULSE_CONTEXT, NULL));
return instance;
}