File: lxmms2.c

package info (click to toggle)
lxmms2 0.1.3-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, jessie, jessie-kfreebsd, sid, stretch
  • size: 120 kB
  • ctags: 57
  • sloc: ansic: 506; makefile: 74
file content (301 lines) | stat: -rw-r--r-- 7,521 bytes parent folder | download
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
/*
 *  lxmms2.c - A program to control XMMS2 with a 
 *  LIRC compatible IR remote control.
 *
 *  Copyright (c) 2005-2010 Johannes Heimansberg
 *  http://wejp.k.vu/
 *
 *  Released under the GNU General Public License v2
 *  (See COPYING file for details.)
 */
#define VERSION "0.1.3"
#define DEBUG

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <errno.h>
#include <unistd.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <lirc/lirc_client.h>
#include <xmmsclient/xmmsclient.h>

#define TRUE 1
#define FALSE 0
#define SUCCESS 1
#define FAILURE 2

static int connected = FALSE;

void xmms2_disconnect_cb(void *userdata)
{
	connected = FALSE;
	printf("Connection to xmms2d lost.\n");
}

static int xmms2_connect(xmmsc_connection_t **connection)
{
	char *path = NULL;
	int   result = FALSE;

	path = getenv("XMMS_PATH");

	/* Initialize connection to XMMS2 daemon */
	if (!xmmsc_connect(*connection, path)) {
		printf("ERROR: %s\n", xmmsc_get_last_error(*connection));
		printf("XMMS_PATH: %s\n", path);
	} else {
		printf("SUCCESS: Connected to xmms2d.\n");
		xmmsc_disconnect_callback_set(*connection, xmms2_disconnect_cb, NULL);
		result = SUCCESS;
		connected = TRUE;
	}
	return result;
}

static int xmms2_do_reljump(xmmsc_connection_t *conn, int where)
{
	xmmsc_result_t *res;
	int             result = SUCCESS;

	if (connected) {
		res = xmmsc_playlist_set_next_rel(conn, where);
		xmmsc_result_wait(res);

		if (xmmsc_result_iserror(res)) {
			char *err = xmmsc_get_last_error(conn);
			fprintf(stderr, "ERROR: Couldn't advance in playlist: %s\n", err);
			if (strcmp(err, "Disconnected") == 0) result = FAILURE;
		} else {
			xmmsc_result_unref(res);
			res = xmmsc_playback_tickle(conn);
			xmmsc_result_wait(res);
			xmmsc_result_unref(res);
		}
	} else {
		result = FAILURE;
	}
	return result;
}

static int xmms2_play(xmmsc_connection_t *connection)
{
	xmmsc_result_t *res;
	int             result = SUCCESS;

	if (connected) {
		res = xmmsc_playback_start(connection);
		xmmsc_result_wait(res);
		if (xmmsc_result_iserror(res)) {
			char *err = xmmsc_get_last_error(connection);
			fprintf(stderr, "ERROR: Couldn't start playback: %s\n", err);
			if (err && strcmp(err, "Disconnected") == 0) result = FAILURE;
		} else {
			xmmsc_result_unref(res);
		}
	} else {
		result = FAILURE;
	}
	return result;
}

static int xmms2_stop(xmmsc_connection_t *connection)
{
	xmmsc_result_t *res;
	int             result = SUCCESS;

	if (connected) {
		res = xmmsc_playback_stop(connection);
		xmmsc_result_wait(res);
		if (xmmsc_result_iserror(res)) {
			char *err = xmmsc_get_last_error(connection);
			fprintf(stderr, "ERROR: Couldn't stop playback: %s\n", err);
			if (err && strcmp(err, "Disconnected") == 0) result = FAILURE;
		} else {
			xmmsc_result_unref(res);
		}
	} else {
		result = FAILURE;
	}
	return result;
}

static int xmms2_pause(xmmsc_connection_t *connection)
{
	xmmsc_result_t *res;
	int             result = SUCCESS;

	if (connected) {
		res = xmmsc_playback_pause(connection);
		xmmsc_result_wait(res);
		if (xmmsc_result_iserror(res)) {
			char *err = xmmsc_get_last_error(connection);
			fprintf(stderr, "ERROR: Couldn't pause playback: %s\n", err);
			if (err && strcmp(err, "Disconnected") == 0) result = FAILURE;
		} else {
			xmmsc_result_unref(res);
		}
	} else {
		result = FAILURE;
	}
	return result;
}

static int xmms2_toggle_pause(xmmsc_connection_t *connection, int start_playback)
{
	xmmsc_result_t *res;
	int             status = -1;
	int             result = SUCCESS;
	xmmsv_t        *val;

	if (connected) {
		res = xmmsc_playback_status(connection);
		xmmsc_result_wait(res);
		val = xmmsc_result_get_value(res);
		xmmsv_get_int(val, &status);
		xmmsc_result_unref(res);
		if (status == XMMS_PLAYBACK_STATUS_PLAY)
			result = xmms2_pause(connection);
		else if (status == XMMS_PLAYBACK_STATUS_PAUSE || start_playback == TRUE)
			result = xmms2_play(connection);
	} else {
		result = FAILURE;
	}
	return result;
}

static int xmms2_volume_get(xmmsc_connection_t *connection,
                            int *volume_l, 
                            int *volume_r)
{
	xmmsc_result_t *res;
	int             result = -1;
	xmmsv_t        *val;

	if (connected) {
		res = xmmsc_playback_volume_get(connection);
		xmmsc_result_wait(res);
		val = xmmsc_result_get_value(res);

		if (xmmsc_result_iserror(res)) {
			fprintf(stderr, "ERROR: Couldn't get config value.\n");
		} else if (!xmmsv_is_error(val)) {
			xmmsv_dict_entry_get_int(val, "left", volume_l);
			xmmsv_dict_entry_get_int(val, "left", volume_r);
			result = SUCCESS;
		}
		xmmsc_result_unref(res);
	} else {
		result = FAILURE;
	}
	return result;
}

static int xmms2_volume_set(xmmsc_connection_t *connection,
                            int volume_l, int volume_r)
{
	xmmsc_result_t *res;
	int             result = SUCCESS;

	if (connected) {
		res = xmmsc_playback_volume_set(connection, "left",  volume_l);
		xmmsc_result_wait(res);
		xmmsc_result_unref(res);
		res = xmmsc_playback_volume_set(connection, "right", volume_r);
		xmmsc_result_wait(res);
		xmmsc_result_unref(res);
	} else {
		result = FAILURE;
	}

	return result;
}

static int xmms2_volume_up(xmmsc_connection_t *connection)
{
	int volume_l, volume_r, result = SUCCESS;

	if (xmms2_volume_get(connection, &volume_l, &volume_r) == SUCCESS) {
		volume_l += 4;
		volume_r += 4;
		if (volume_l <= 100 && volume_r <= 100)
			result = xmms2_volume_set(connection, volume_l, volume_r);
	} else {
		result = FAILURE;
	}

	return result;
}

static int xmms2_volume_down(xmmsc_connection_t *connection)
{
	int volume_l, volume_r, result = SUCCESS;

	if (xmms2_volume_get(connection, &volume_l, &volume_r) == SUCCESS) {
		volume_l -= 4;
		volume_r -= 4;
		if (volume_l >= 0 && volume_r >= 0)
			result = xmms2_volume_set(connection, volume_l, volume_r);
	} else {
		result = FAILURE;
	}

	return result;
}

int main(int argc, char *argv[])
{
	xmmsc_connection_t *connection = NULL;
	struct lirc_config *config;

	printf("lxmms2 v%s Copyright (c) 2005-2010 Johannes Heimansberg " \
	       "( http://wejp.k.vu/ )\n", VERSION);

	connection = xmmsc_init("lxmms2");

	if (lirc_init("lxmms2", 1) == -1)
		exit(EXIT_FAILURE);

	if (xmms2_connect(&connection) == FALSE)
		exit(EXIT_FAILURE);

	if (lirc_readconfig(argc == 2 ? argv[1] : NULL, &config, NULL) == 0) {
		char *code, *c;
		int  ret, result = SUCCESS;

		while (lirc_nextcode(&code) == 0 && result == SUCCESS) {
			if (code == NULL) continue;
			while ((ret = lirc_code2char(config, code, &c)) == 0 && c != NULL) {
				if (strcmp(c, "play") == 0)
					result = xmms2_play(connection);					
				else if (strcmp(c, "pause") == 0)
					result = xmms2_pause(connection);
				else if (strcmp(c, "stop") == 0)
					result = xmms2_stop(connection);
				else if (strcmp(c, "toggle_pause") == 0)
					result = xmms2_toggle_pause(connection, FALSE);
				else if (strcmp(c, "toggle_play_pause") == 0)
					result = xmms2_toggle_pause(connection, TRUE);
				else if (strcmp(c, "next") == 0)
					result = xmms2_do_reljump(connection, 1);				
				else if (strcmp(c, "prev") == 0)
					result = xmms2_do_reljump(connection, -1);
				else if (strcmp(c, "volume_up") == 0)
					result = xmms2_volume_up(connection);
				else if (strcmp(c, "volume_down") == 0)
					result = xmms2_volume_down(connection);
				else
					printf("Unknown command: %s\n", c);
			}
			free(code);
			if (ret == -1) break;
		}
		lirc_freeconfig(config);
	}
	lirc_deinit();
	exit(EXIT_SUCCESS);
}