File: player_command.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 (248 lines) | stat: -rw-r--r-- 5,589 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
/* ncmpc (Ncurses MPD Client)
 * (c) 2004-2010 The Music Player Daemon Project
 * Project homepage: http://musicpd.org
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY 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, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include "player_command.h"
#include "mpdclient.h"
#include "options.h"
#include "i18n.h"
#include "screen_client.h"
#include "screen_status.h"

int seek_id = -1;
int seek_target_time;

static guint seek_source_id;

static void
commit_seek(struct mpdclient *c)
{
	if (seek_id < 0)
		return;

	struct mpd_connection *connection = mpdclient_get_connection(c);
	if (connection == NULL) {
		seek_id = -1;
		return;
	}

	if (c->song != NULL && (unsigned)seek_id == mpd_song_get_id(c->song))
		if (!mpd_run_seek_id(connection, seek_id, seek_target_time))
			mpdclient_handle_error(c);

	seek_id = -1;
}

/**
 * This timer is invoked after seeking when the user hasn't typed a
 * key for 500ms.  It is used to do the real seeking.
 */
static gboolean
seek_timer(gpointer data)
{
	struct mpdclient *c = data;

	seek_source_id = 0;
	commit_seek(c);
	mpdclient_put_connection(c);
	return false;
}

static void
schedule_seek_timer(struct mpdclient *c)
{
	assert(seek_source_id == 0);

	seek_source_id = g_timeout_add(500, seek_timer, c);
}

void
cancel_seek_timer(void)
{
	if (seek_source_id != 0) {
		g_source_remove(seek_source_id);
		seek_source_id = 0;
	}
}

static bool
setup_seek(struct mpdclient *c)
{
	if (!mpdclient_is_playing(c))
		return false;

	if (seek_id != (int)mpd_status_get_song_id(c->status)) {
		seek_id = mpd_status_get_song_id(c->status);
		seek_target_time = mpd_status_get_elapsed_time(c->status);
	}

	schedule_seek_timer(c);

	return true;
}

bool
handle_player_command(struct mpdclient *c, command_t cmd)
{
	if (!mpdclient_is_connected(c) || c->status == NULL)
		return false;

	cancel_seek_timer();

	switch(cmd) {
		struct mpd_connection *connection;

		/*
	case CMD_PLAY:
		mpdclient_cmd_play(c, MPD_PLAY_AT_BEGINNING);
		break;
		*/
	case CMD_PAUSE:
		connection = mpdclient_get_connection(c);
		if (connection == NULL)
			break;

		if (!mpd_run_pause(connection,
				   mpd_status_get_state(c->status) != MPD_STATE_PAUSE))
			mpdclient_handle_error(c);
		break;
	case CMD_STOP:
		connection = mpdclient_get_connection(c);
		if (connection == NULL)
			break;

		if (!mpd_run_stop(connection))
			mpdclient_handle_error(c);
		break;
	case CMD_CROP:
		mpdclient_cmd_crop(c);
		break;
	case CMD_SEEK_FORWARD:
		if (!setup_seek(c))
			break;

		seek_target_time += options.seek_time;
		if (seek_target_time > (int)mpd_status_get_total_time(c->status))
			seek_target_time = mpd_status_get_total_time(c->status);
		break;

	case CMD_TRACK_NEXT:
		connection = mpdclient_get_connection(c);
		if (connection == NULL)
			break;

		if (!mpd_run_next(connection))
			mpdclient_handle_error(c);
		break;
	case CMD_SEEK_BACKWARD:
		if (!setup_seek(c))
			break;

		seek_target_time -= options.seek_time;
		if (seek_target_time < 0)
			seek_target_time = 0;
		break;

	case CMD_TRACK_PREVIOUS:
		connection = mpdclient_get_connection(c);
		if (connection == NULL)
			break;

		if (!mpd_run_previous(connection))
			mpdclient_handle_error(c);
		break;
	case CMD_SHUFFLE:
		connection = mpdclient_get_connection(c);
		if (connection == NULL)
			break;

		if (mpd_run_shuffle(connection))
			screen_status_message(_("Shuffled queue"));
		else
			mpdclient_handle_error(c);
		break;
	case CMD_CLEAR:
		connection = mpdclient_get_connection(c);
		if (connection == NULL)
			break;

		if (mpdclient_cmd_clear(c))
			screen_status_message(_("Cleared queue"));
		break;
	case CMD_REPEAT:
		connection = mpdclient_get_connection(c);
		if (connection == NULL)
			break;

		if (!mpd_run_repeat(connection,
				    !mpd_status_get_repeat(c->status)))
			mpdclient_handle_error(c);
		break;
	case CMD_RANDOM:
		connection = mpdclient_get_connection(c);
		if (connection == NULL)
			break;

		if (!mpd_run_random(connection,
				    !mpd_status_get_random(c->status)))
			mpdclient_handle_error(c);
		break;
	case CMD_SINGLE:
		connection = mpdclient_get_connection(c);
		if (connection == NULL)
			break;

		if (!mpd_run_single(connection,
				    !mpd_status_get_single(c->status)))
			mpdclient_handle_error(c);
		break;
	case CMD_CONSUME:
		connection = mpdclient_get_connection(c);
		if (connection == NULL)
			break;

		if (!mpd_run_consume(connection,
				     !mpd_status_get_consume(c->status)))
			mpdclient_handle_error(c);
		break;
	case CMD_CROSSFADE:
		connection = mpdclient_get_connection(c);
		if (connection == NULL)
			break;

		if (!mpd_run_crossfade(connection,
				       mpd_status_get_crossfade(c->status) > 0
				       ? 0 : options.crossfade_time))
			mpdclient_handle_error(c);
		break;
	case CMD_DB_UPDATE:
		screen_database_update(c, NULL);
		break;
	case CMD_VOLUME_UP:
		mpdclient_cmd_volume_up(c);
		break;
	case CMD_VOLUME_DOWN:
		mpdclient_cmd_volume_down(c);
		break;

	default:
		return false;
	}

	return true;
}