File: gidle.c

package info (click to toggle)
ncmpc 0.25-0.1%2Bdeb9u1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 2,616 kB
  • sloc: ansic: 12,229; sh: 4,157; makefile: 282; ruby: 28
file content (403 lines) | stat: -rw-r--r-- 9,535 bytes parent folder | download | duplicates (2)
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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
/* ncmpc (Ncurses MPD Client)
   (c) 2004-2010 The Music Player Daemon Project
   Project homepage: http://musicpd.org

   Redistribution and use in source and binary forms, with or without
   modification, are permitted provided that the following conditions
   are met:

   - Redistributions of source code must retain the above copyright
   notice, this list of conditions and the following disclaimer.

   - 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 FOUNDATION 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 "gidle.h"
#include "Compiler.h"

#include <mpd/async.h>
#include <mpd/parser.h>

#include <glib.h>

#include <assert.h>
#include <string.h>
#include <errno.h>

struct mpd_glib_source {
	struct mpd_connection *connection;
	struct mpd_async *async;
	struct mpd_parser *parser;

	mpd_glib_callback_t callback;
	void *callback_ctx;

	GIOChannel *channel;

	enum mpd_async_event io_events;

	guint id;

	enum mpd_idle idle_events;

	/**
	 * This flag is a hack: it is set while mpd_glib_leave() is
	 * executed.  mpd_glib_leave() might invoke the callback, and
	 * the callback might invoke mpd_glib_enter(), awkwardly
	 * leaving mpd_glib_leave() in idle mode.  As long as this
	 * flag is set, mpd_glib_enter() is a no-op to prevent this.
	 */
	bool leaving;

	/**
	 * This flag is true when mpd_glib_free() has been called
	 * during a callback invoked from mpd_glib_leave().
	 * mpd_glib_leave() will do the real g_free() call then.
	 */
	bool destroyed;
};

struct mpd_glib_source *
mpd_glib_new(struct mpd_connection *connection,
	     mpd_glib_callback_t callback, void *callback_ctx)
{
	struct mpd_glib_source *source = g_new(struct mpd_glib_source, 1);

	source->connection = connection;
	source->async = mpd_connection_get_async(connection);
	source->parser = mpd_parser_new();
	/* XXX check source->parser!=NULL */

	source->callback = callback;
	source->callback_ctx = callback_ctx;

	source->channel = g_io_channel_unix_new(mpd_async_get_fd(source->async));
	source->io_events = 0;
	source->id = 0;
	source->leaving = false;
	source->destroyed = false;

	return source;
}

void
mpd_glib_free(struct mpd_glib_source *source)
{
	assert(!source->destroyed);

	if (source->id != 0)
		g_source_remove(source->id);

	g_io_channel_unref(source->channel);

	mpd_parser_free(source->parser);

	if (source->leaving)
		source->destroyed = true;
	else
		g_free(source);
}

static void
mpd_glib_invoke(const struct mpd_glib_source *source)
{
	assert(source->id == 0);
	assert(!source->destroyed);

	if (source->idle_events != 0)
		source->callback(MPD_ERROR_SUCCESS, 0, NULL,
				 source->idle_events, source->callback_ctx);
}

static void
mpd_glib_invoke_error(const struct mpd_glib_source *source,
		      enum mpd_error error, enum mpd_server_error server_error,
		      const char *message)
{
	assert(source->id == 0);
	assert(!source->destroyed);

	source->callback(error, server_error, message,
			 0, source->callback_ctx);
}

static void
mpd_glib_invoke_async_error(const struct mpd_glib_source *source)
{
	assert(source->id == 0);

	mpd_glib_invoke_error(source, mpd_async_get_error(source->async), 0,
			      mpd_async_get_error_message(source->async));
}

/**
 * Converts a GIOCondition bit mask to #mpd_async_event.
 */
static enum mpd_async_event
g_io_condition_to_mpd_async_event(GIOCondition condition)
{
	enum mpd_async_event events = 0;

	if (condition & G_IO_IN)
		events |= MPD_ASYNC_EVENT_READ;

	if (condition & G_IO_OUT)
		events |= MPD_ASYNC_EVENT_WRITE;

	if (condition & G_IO_HUP)
		events |= MPD_ASYNC_EVENT_HUP;

	if (condition & G_IO_ERR)
		events |= MPD_ASYNC_EVENT_ERROR;

	return events;
}

/**
 * Converts a #mpd_async_event bit mask to GIOCondition.
 */
static GIOCondition
mpd_async_events_to_g_io_condition(enum mpd_async_event events)
{
	GIOCondition condition = 0;

	if (events & MPD_ASYNC_EVENT_READ)
		condition |= G_IO_IN;

	if (events & MPD_ASYNC_EVENT_WRITE)
		condition |= G_IO_OUT;

	if (events & MPD_ASYNC_EVENT_HUP)
		condition |= G_IO_HUP;

	if (events & MPD_ASYNC_EVENT_ERROR)
		condition |= G_IO_ERR;

	return condition;
}

/**
 * Parses a response line from MPD.
 *
 * @return true on success, false on error
 */
static bool
mpd_glib_feed(struct mpd_glib_source *source, char *line)
{
	enum mpd_parser_result result;

	result = mpd_parser_feed(source->parser, line);
	switch (result) {
	case MPD_PARSER_MALFORMED:
		source->id = 0;
		source->io_events = 0;

		mpd_glib_invoke_error(source, MPD_ERROR_MALFORMED, 0,
				      "Malformed MPD response");
		return false;

	case MPD_PARSER_SUCCESS:
		source->id = 0;
		source->io_events = 0;

		mpd_glib_invoke(source);
		return false;

	case MPD_PARSER_ERROR:
		source->id = 0;
		source->io_events = 0;

		mpd_glib_invoke_error(source, MPD_ERROR_SERVER,
				      mpd_parser_get_server_error(source->parser),
				      mpd_parser_get_message(source->parser));
		return false;

	case MPD_PARSER_PAIR:
		if (strcmp(mpd_parser_get_name(source->parser),
			   "changed") == 0)
			source->idle_events |=
				mpd_idle_name_parse(mpd_parser_get_value(source->parser));

		break;
	}

	return true;
}

/**
 * Receives and evaluates a portion of the MPD response.
 *
 * @return true on success, false on error
 */
static bool
mpd_glib_recv(struct mpd_glib_source *source)
{
	char *line;
	while ((line = mpd_async_recv_line(source->async)) != NULL) {
		if (!mpd_glib_feed(source, line))
			return false;
	}

	if (mpd_async_get_error(source->async) != MPD_ERROR_SUCCESS) {
		source->id = 0;
		source->io_events = 0;

		mpd_glib_invoke_async_error(source);
		return false;
	}

	return true;
}

static gboolean
mpd_glib_source_callback(gcc_unused GIOChannel *_source,
			 GIOCondition condition, gpointer data)
{
	struct mpd_glib_source *source = data;

	assert(source->id != 0);
	assert(source->io_events != 0);

	/* let libmpdclient do some I/O */

	if (!mpd_async_io(source->async,
			  g_io_condition_to_mpd_async_event(condition))) {
		source->id = 0;
		source->io_events = 0;

		mpd_glib_invoke_async_error(source);
		return false;
	}

	/* receive the response */

	if ((condition & G_IO_IN) != 0) {
		if (!mpd_glib_recv(source))
			return false;
	}

	/* continue polling? */

	enum mpd_async_event events = mpd_async_events(source->async);
	if (events == 0) {
		/* no events - disable watch */
		source->id = 0;
		source->io_events = 0;

		return false;
	} else if (events != source->io_events) {
		/* different event mask: make new watch */

		g_source_remove(source->id);

		condition = mpd_async_events_to_g_io_condition(events);
		source->id = g_io_add_watch(source->channel, condition,
					    mpd_glib_source_callback, source);
		source->io_events = events;

		return false;
	} else
		/* same event mask as before, enable the old watch */
		return true;
}

static void
mpd_glib_add_watch(struct mpd_glib_source *source)
{
	enum mpd_async_event events = mpd_async_events(source->async);

	assert(source->io_events == 0);
	assert(source->id == 0);

	GIOCondition condition = mpd_async_events_to_g_io_condition(events);
	source->id = g_io_add_watch(source->channel, condition,
				    mpd_glib_source_callback, source);
	source->io_events = events;
}

bool
mpd_glib_enter(struct mpd_glib_source *source)
{
	assert(source->io_events == 0);
	assert(source->id == 0);
	assert(!source->destroyed);

	if (source->leaving)
		return false;

	source->idle_events = 0;

	if (!mpd_async_send_command(source->async, "idle", NULL)) {
		mpd_glib_invoke_async_error(source);
		return false;
	}

	mpd_glib_add_watch(source);
	return true;
}

bool
mpd_glib_leave(struct mpd_glib_source *source)
{
	assert(!source->destroyed);

	if (source->id == 0)
		/* already left, callback was invoked */
		return true;

	g_source_remove(source->id);
	source->id = 0;
	source->io_events = 0;

	enum mpd_idle events = source->idle_events == 0
		? mpd_run_noidle(source->connection)
		: mpd_recv_idle(source->connection, false);

	source->leaving = true;

	if (events == 0 &&
	    mpd_connection_get_error(source->connection) != MPD_ERROR_SUCCESS) {
		enum mpd_error error =
			mpd_connection_get_error(source->connection);
		enum mpd_server_error server_error =
			error == MPD_ERROR_SERVER
			? mpd_connection_get_server_error(source->connection)
			: 0;

		mpd_glib_invoke_error(source, error, server_error,
				      mpd_connection_get_error_message(source->connection));

		if (source->destroyed) {
			g_free(source);
			return false;
		}

		source->leaving = false;
		return true;
	}

	source->idle_events |= events;
	mpd_glib_invoke(source);

	if (source->destroyed) {
		g_free(source);
		return false;
	}

	source->leaving = false;
	return true;
}