File: mpd.c

package info (click to toggle)
gkrellm-gkrellmpc 0.1~beta10-5
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 316 kB
  • sloc: ansic: 1,700; makefile: 109
file content (302 lines) | stat: -rw-r--r-- 7,184 bytes parent folder | download | duplicates (4)
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
/*
 * $Header: /cvsroot/gkrellmpc/mpd.c,v 1.6 2005/03/05 22:27:27 mina Exp $
 *
 * Holds all the functions needed to talk to MPD
 */

#include "globals.h"
#include "mpd.h"
#include "gkrellmpc.h"

#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>

GIOChannel   * mpc_mpd = NULL;

/*
 * Connects to the MPD server, sets up the mpd object, sets the status decal to ON
 */
gboolean mpc_mpd_connect() {
	int sockfd;
	struct hostent *server;
	struct sockaddr_in serv_addr;
	gchar * line;
	gchar ** parts;
	gboolean retval;

	if (mpc_mpd) {
		/*
		 * Close existing connection
		 */
		mpc_mpd_disconnect();
	}

	if (!mpc_conf_hostname || !mpc_conf_port) {
		return (FALSE);
	}

	if ((sockfd = socket(PF_INET, SOCK_STREAM, 0)) == -1) return(FALSE);
	if (!(server = gethostbyname(mpc_conf_hostname))) {
		close(sockfd);
		return(FALSE);
	}

	bzero((char *) &serv_addr, sizeof(serv_addr));
	serv_addr.sin_family = AF_INET;
	bcopy((char *)server->h_addr,
			(char *)&serv_addr.sin_addr.s_addr,
			server->h_length);
	serv_addr.sin_port = htons(mpc_conf_port);

	if (connect(sockfd, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) < 0) {
		close(sockfd);
		return(FALSE);
	}
	
	/* Getup the mpd object */
	mpc_mpd = g_io_channel_unix_new(sockfd);

	/*
	 * Find the OK otherwise we connected to something not-MPDish
	 */
	if (g_io_channel_read_line(mpc_mpd, &line, NULL, NULL, NULL) == G_IO_STATUS_NORMAL) {
		g_strchomp(line);
		parts = g_strsplit(line, " ", 2);
		if (strcmp(parts[0], "OK") == 0) {
			retval = TRUE;
		}
		else {
			mpc_mpd_disconnect();
			retval = FALSE;
		}
		g_strfreev(parts);
	}
	else {
		/* Read failed */
		mpc_mpd_disconnect();
		retval = FALSE;
	}

	if (retval) {
		gkrellm_draw_decal_pixmap(mpc_panel, mpc_status_decal, D_MISC_LED1);
		mpc_update_label(_("MPD"));
		mpc_update_songname("");
	}

	return(retval);
}

/*
 * Disconnects from MPD, destroys the mpd object, sets the status decal to off
 */
gboolean mpc_mpd_disconnect() {

	if (mpc_mpd) {
		g_io_channel_shutdown(mpc_mpd, FALSE, NULL);
		free(mpc_mpd);
		mpc_mpd = NULL;
	}
	
	gkrellm_draw_decal_pixmap(mpc_panel, mpc_status_decal, D_MISC_LED0);
	mpc_update_label(_("NO MPD"));
	mpc_update_songname("");
	return (TRUE);
}

/*
 * Sends a command to MPD
 * Returns true if successful, false if not
 * Use for bool-returning commands. Use mpd_get instead for data-returning commands
 */
gboolean mpc_mpd_do(gchar * command) {
	gchar *line;
	gboolean retval;

	if (!mpc_mpd && !mpc_mpd_connect()) {
		return (FALSE);
	}

	if (g_io_channel_write_chars(mpc_mpd, command, -1, NULL, NULL) == G_IO_STATUS_NORMAL) {
		g_io_channel_flush(mpc_mpd, NULL);
		if (g_io_channel_read_line(mpc_mpd, &line, NULL, NULL, NULL) == G_IO_STATUS_NORMAL) {
			g_strchomp(line);
			if (strcmp(line, "OK") == 0) {
				/* It's a success */
				retval = TRUE;
			}
			else {
				/* It's a failure */
				retval = FALSE;
			}
		}
		else {
			/* Read failed */
			retval = FALSE;
		}
	}
	else {
		/* Write Failed */
		retval = FALSE;
	}

	if (retval)
		mpc_sync_with_mpd();

	return (retval);
}

/*
 * Sends a command to MPD
 * Returns a hashref of the results (needs to be freed with g_hash_table_destroy  afterwards), NULL on error
 * Use for keypair-returning commands. Use mpd_do instead for boolean-returning commands
 * Note that all key names coming back from MPD are lowercased for consistency before being shoved in the hash
 */
GHashTable * mpc_mpd_get(gchar * command) {
	gchar *line = NULL;
	gchar **parts;
	GHashTable *retval = NULL;
	gboolean success = TRUE;
	GIOStatus status;

	if (!mpc_mpd && !mpc_mpd_connect()) {
		return (NULL);
	}

	if (g_io_channel_write_chars(mpc_mpd, command, -1, NULL, NULL) == G_IO_STATUS_NORMAL) {
		g_io_channel_flush(mpc_mpd, NULL);

		retval = g_hash_table_new_full(g_str_hash, g_str_equal, free, free);

		while (1) {
		  	g_free(line);
			status = g_io_channel_read_line(mpc_mpd, &line, NULL, NULL, NULL);
			if (status != G_IO_STATUS_NORMAL) {
				/* Reading failed */
				success = FALSE;
				break;
			}
			g_strchomp(line);
			if (strcmp(line, "OK") == 0) {
				/* End of resultset with success */
				break;
			}
			else if (g_str_has_prefix(line, "ACK ")) {
				/* End of resultset with failure */
				success = FALSE;
				break;
			}
			else {
				/* A resultset entry */
				parts = g_strsplit(line, ": ", 2);
				if (!parts || !parts[0] || !parts[1]) {
					/* Resultset entry is not proper - ignore it */
				}
				else {
					/* nice pair - add to hash */
					g_hash_table_insert(retval, g_ascii_strdown(parts[0], -1), g_strdup(parts[1]));
				}
				g_strfreev(parts);
			}
		}

		g_free(line);

		if (!success) {
			g_hash_table_destroy(retval);
			retval = NULL;
		}

	}
	else {
		/* Write Failed */
		retval = NULL;
	}

	/* If we've reached here, all is well */
	return (retval);
}


/*
 * Sends a command to MPD
 * Returns an arrayref of hashrefs of the results, NULL on error
 * Use for keypair-returning commands that require clumping
 * Each hashref must be freed using g_hash_table_destroy, then the whole array must be freed with g_ptr_array_free
 * Note that all key names coming back from MPD are lowercased for consistency before being shoved in the hashes
 */
GPtrArray * mpc_mpd_get_clumps(gchar * command, gboolean clumpsingles) {
	gchar *line;
	gchar **parts;
	GPtrArray *retval = NULL;
	gboolean success = TRUE;
	GIOStatus status;
	int i;

	if (!mpc_mpd && !mpc_mpd_connect()) {
		return (NULL);
	}

	if (g_io_channel_write_chars(mpc_mpd, command, -1, NULL, NULL) == G_IO_STATUS_NORMAL) {
		g_io_channel_flush(mpc_mpd, NULL);

		retval = g_ptr_array_new();

		while (1) {
			status = g_io_channel_read_line(mpc_mpd, &line, NULL, NULL, NULL);
			if (status != G_IO_STATUS_NORMAL) {
				/* Reading failed */
				success = FALSE;
				break;
			}
			g_strchomp(line);
			if (strcmp(line, "OK") == 0) {
				/* End of resultset with success */
				break;
			}
			else if (g_str_has_prefix(line, "ACK ")) {
				/* End of resultset with failure */
				success = FALSE;
				break;
			}
			else {
				/* A resultset entry */
				parts = g_strsplit(line, ": ", 2);
				if (!parts || !parts[0] || !parts[1]) {
					/* Resultset entry is not proper - ignore it */
				}
				else {
					/* nice pair */
					if (clumpsingles || retval->len == 0 || g_hash_table_lookup_extended(g_ptr_array_index(retval, retval->len -1), g_ascii_strdown(parts[0], -1), NULL, NULL)) {
						/*
						 * Create a new hash and add it at the end of the array
						 * Either because the array is empty, or the last hash already has this key so we need a new clump
						 */
						g_ptr_array_add(retval, g_hash_table_new_full(g_str_hash, g_str_equal, free, free));
					}

					/* Add the key to the last hashref in the array */
					g_hash_table_insert(g_ptr_array_index(retval, retval->len -1), g_ascii_strdown(parts[0], -1), g_strdup(parts[1]));
				}
				g_strfreev(parts);
			}
		}

		if (!success) {
			for (i=0; i < retval->len; i++) {
				g_hash_table_destroy(g_ptr_array_index(retval, i));
			}
			g_ptr_array_free(retval, FALSE);
			retval = NULL;
		}

	}
	else {
		/* Write Failed */
		retval = NULL;
	}

	/* If we've reached here, all is well */
	return (retval);
}