summaryrefslogtreecommitdiff
path: root/src/ui_act.c
blob: 60525ff4f3ed5252d3948f320da3736b3fc4b7bc (plain)
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
/*
Copyright (c) 2008-2009
	Lars-Dominik Braun <PromyLOPh@lavabit.com>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

/* functions responding to user's keystrokes */

#include <string.h>
#include <unistd.h>
#include <pthread.h>

#include "ui.h"
#include "ui_act.h"
#include "ui_readline.h"

#define RETURN_IF_NO_STATION if (*curStation == NULL) { \
		BarUiMsg (MSG_ERR, "No station selected.\n"); \
		return; }

#define RETURN_IF_NO_SONG if (*curStation == NULL || *curSong == NULL) { \
		BarUiMsg (MSG_ERR, "No song playing.\n"); \
		return; }

/*	helper to _really_ skip a song (unlock mutex, quit player)
 *	@param player handle
 */
inline void BarUiDoSkipSong (struct audioPlayer *player) {
	player->doQuit = 1;
	pthread_mutex_unlock (&player->pauseMutex);
}

/*	transform station if necessary to allow changes like rename, rate, ...
 *	@param piano handle
 *	@param transform this station
 *	@return 0 = error, 1 = everything went well
 */
int BarTransformIfShared (PianoHandle_t *ph, PianoStation_t *station) {
	/* shared stations must be transformed */
	if (!station->isCreator) {
		BarUiMsg (MSG_INFO, "Transforming station... ");
		if (BarUiPrintPianoStatus (PianoTransformShared (ph, station)) !=
				PIANO_RET_OK) {
			return 0;
		}
	}
	return 1;
}

/*	print current shortcut configuration
 */
void BarUiActHelp (BAR_KS_ARGS) {
	BarKeyShortcut_t *curShortcut = settings->keys;

	BarUiMsg (MSG_NONE, "\r");
	while (curShortcut != NULL) {
		if (curShortcut->description != NULL) {
			BarUiMsg (MSG_LIST, "%c    %s\n", curShortcut->key,
					curShortcut->description);
		}
		curShortcut = curShortcut->next;
	}
}

/*	add more music to current station
 */
void BarUiActAddMusic (BAR_KS_ARGS) {
	char *musicId;
	PianoReturn_t pRet = PIANO_RET_ERR;

	RETURN_IF_NO_STATION;

	musicId = BarUiSelectMusicId (ph, curFd);
	if (musicId != NULL) {
		if (!BarTransformIfShared (ph, *curStation)) {
			return;
		}
		BarUiMsg (MSG_INFO, "Adding music to station... ");
		pRet = BarUiPrintPianoStatus (PianoStationAddMusic (ph, *curStation, musicId));
		free (musicId);

		BarUiStartEventCmd (settings, "stationaddmusic", *curStation, *curSong, pRet);
	}
}

/*	ban song
 */
void BarUiActBanSong (BAR_KS_ARGS) {
	PianoReturn_t pRet = PIANO_RET_ERR;

	RETURN_IF_NO_SONG;

	if (!BarTransformIfShared (ph, *curStation)) {
		return;
	}
	BarUiMsg (MSG_INFO, "Banning song... ");
	if ((pRet = BarUiPrintPianoStatus (PianoRateTrack (ph, *curSong,
			PIANO_RATE_BAN))) == PIANO_RET_OK) {
		BarUiDoSkipSong (player);
	}
	BarUiStartEventCmd (settings, "songban", *curStation, *curSong, pRet);
}

/*	create new station
 */
void BarUiActCreateStation (BAR_KS_ARGS) {
	char *musicId;
	PianoReturn_t pRet = PIANO_RET_ERR;

	musicId = BarUiSelectMusicId (ph, curFd);
	if (musicId != NULL) {
		BarUiMsg (MSG_INFO, "Creating station... ");
		pRet = BarUiPrintPianoStatus (PianoCreateStation (ph, "mi", musicId));
		free (musicId);
		BarUiStartEventCmd (settings, "stationcreate", *curStation, *curSong, pRet);
	}
}

/*	add shared station by id
 */
void BarUiActAddSharedStation (BAR_KS_ARGS) {
	char stationId[50];
	PianoReturn_t pRet = PIANO_RET_ERR;

	BarUiMsg (MSG_QUESTION, "Station id: ");
	if (BarReadline (stationId, sizeof (stationId), "0123456789", 0, 0,
			curFd) > 0) {
		BarUiMsg (MSG_INFO, "Adding shared station... ");
		pRet = BarUiPrintPianoStatus (PianoCreateStation (ph, "sh",
				(char *) stationId));
		BarUiStartEventCmd (settings, "stationaddshared", *curStation, *curSong, pRet);
	}
}

/*	delete current station
 */
void BarUiActDeleteStation (BAR_KS_ARGS) {
	PianoReturn_t pRet = PIANO_RET_ERR;

	RETURN_IF_NO_STATION;

	BarUiMsg (MSG_QUESTION, "Really delete \"%s\"? [yN] ",
			(*curStation)->name);
	if (BarReadlineYesNo (0, curFd)) {
		BarUiMsg (MSG_INFO, "Deleting station... ");
		if ((pRet = BarUiPrintPianoStatus (PianoDeleteStation (ph,
				*curStation))) == PIANO_RET_OK) {
			BarUiDoSkipSong (player);
			PianoDestroyPlaylist (ph);
			*curSong = NULL;
			*curStation = NULL;
		}
		BarUiStartEventCmd (settings, "stationdelete", *curStation, *curSong, pRet);
	}
}

/*	explain pandora's song choice
 */
void BarUiActExplain (BAR_KS_ARGS) {
	char *explanation;
	PianoReturn_t pRet = PIANO_RET_ERR;

	RETURN_IF_NO_STATION;

	BarUiMsg (MSG_INFO, "Receiving explanation... ");
	if ((pRet = BarUiPrintPianoStatus (PianoExplain (ph, *curSong,
			&explanation))) == PIANO_RET_OK) {
		BarUiMsg (MSG_INFO, "%s\n", explanation);
		free (explanation);
	}
	BarUiStartEventCmd (settings, "songexplain", *curStation, *curSong, pRet);
}

/*	choose genre station and add it as shared station
 */
void BarUiActStationFromGenre (BAR_KS_ARGS) {
	/* use genre station */
	BarStationFromGenre (ph, curFd);
}

/*	print verbose song information
 */
void BarUiActSongInfo (BAR_KS_ARGS) {
	RETURN_IF_NO_SONG;

	BarUiPrintStation ((*curStation));
	/* print real station if quickmix */
	BarUiPrintSong ((*curSong), (*curStation)->isQuickMix ?
			PianoFindStationById (ph->stations, (*curSong)->stationId) : NULL);
}

/*	print some debugging information
 */
void BarUiActDebug (BAR_KS_ARGS) {
	RETURN_IF_NO_SONG;

	/* print debug-alike infos */
	BarUiMsg (MSG_NONE,
			"album:\t%s\n"
			"artist:\t%s\n"
			"audioFormat:\t%i\n"
			"audioUrl:\t%s\n"
			"fileGain:\t%f\n"
			"focusTraitId:\t%s\n"
			"identity:\t%s\n"
			"matchingSeed:\t%s\n"
			"musicId:\t%s\n"
			"rating:\t%i\n"
			"stationId:\t%s\n"
			"title:\t%s\n"
			"userSeed:\t%s\n",
			(*curSong)->album, (*curSong)->artist, (*curSong)->audioFormat,
			(*curSong)->audioUrl, (*curSong)->fileGain,
			(*curSong)->focusTraitId, (*curSong)->identity,
			(*curSong)->matchingSeed, (*curSong)->musicId, (*curSong)->rating,
			(*curSong)->stationId, (*curSong)->title, (*curSong)->userSeed);
}

/*	rate current song
 */
void BarUiActLoveSong (BAR_KS_ARGS) {
	PianoReturn_t pRet = PIANO_RET_ERR;

	RETURN_IF_NO_SONG;

	if (!BarTransformIfShared (ph, *curStation)) {
		return;
	}
	BarUiMsg (MSG_INFO, "Loving song... ");
	pRet = BarUiPrintPianoStatus (PianoRateTrack (ph, *curSong, PIANO_RATE_LOVE));
	BarUiStartEventCmd (settings, "songlove", *curStation, *curSong, pRet);
}

/*	skip song
 */
void BarUiActSkipSong (BAR_KS_ARGS) {
	BarUiDoSkipSong (player);
}

/*	move song to different station
 */
void BarUiActMoveSong (BAR_KS_ARGS) {
	PianoStation_t *moveStation, *fromStation;
	PianoReturn_t pRet = PIANO_RET_ERR;

	RETURN_IF_NO_SONG;

	moveStation = BarUiSelectStation (ph, "Move song to station: ", curFd);
	if (moveStation != NULL) {
		if (!BarTransformIfShared (ph, *curStation) ||
				!BarTransformIfShared (ph, moveStation)) {
			return;
		}
		BarUiMsg (MSG_INFO, "Moving song to \"%s\"... ", moveStation->name);
		fromStation = PianoFindStationById (ph->stations, (*curSong)->stationId);
		if (fromStation == NULL) {
			BarUiMsg (MSG_ERR, "Station not found\n");
			return;
		}
		if ((pRet = BarUiPrintPianoStatus (PianoMoveSong (ph, fromStation,
				moveStation, *curSong))) == PIANO_RET_OK) {
			BarUiDoSkipSong (player);
		}
		BarUiStartEventCmd (settings, "songmove", *curStation, *curSong, pRet);
	}
}

/*	pause
 */
void BarUiActPause (BAR_KS_ARGS) {
	/* already locked => unlock/unpause */
	if (pthread_mutex_trylock (&player->pauseMutex) == EBUSY) {
		pthread_mutex_unlock (&player->pauseMutex);
	}
}

/*	rename current station
 */
void BarUiActRenameStation (BAR_KS_ARGS) {
	char lineBuf[100];
	PianoReturn_t pRet = PIANO_RET_ERR;

	RETURN_IF_NO_STATION;

	BarUiMsg (MSG_QUESTION, "New name: ");
	if (BarReadlineStr (lineBuf, sizeof (lineBuf), 0, curFd) > 0) {
		if (!BarTransformIfShared (ph, *curStation)) {
			return;
		}
		BarUiMsg (MSG_INFO, "Renaming station... ");
		pRet = BarUiPrintPianoStatus (PianoRenameStation (ph, *curStation,
				(char *) lineBuf));
		BarUiStartEventCmd (settings, "stationrename", *curStation, *curSong, pRet);
	}
}

/*	play another station
 */
void BarUiActSelectStation (BAR_KS_ARGS) {
	BarUiDoSkipSong (player);
	PianoDestroyPlaylist (ph);
	*curSong = NULL;
	*curStation = BarUiSelectStation (ph, "Select station: ", curFd);
	if (*curStation != NULL) {
		BarUiPrintStation ((*curStation));
	}
}

/*	ban song for 1 month
 */
void BarUiActTempBanSong (BAR_KS_ARGS) {
	PianoReturn_t pRet = PIANO_RET_ERR;

	RETURN_IF_NO_SONG;

	BarUiMsg (MSG_INFO, "Putting song on shelf... ");
	if ((pRet = BarUiPrintPianoStatus (PianoSongTired (ph, *curSong))) ==
			PIANO_RET_OK) {
		BarUiDoSkipSong (player);
	}
	BarUiStartEventCmd (settings, "songshelf", *curStation, *curSong, pRet);
}

/*	print upcoming songs
 */
void BarUiActPrintUpcoming (BAR_KS_ARGS) {
	RETURN_IF_NO_SONG;

	PianoSong_t *nextSong = (*curSong)->next;
	if (nextSong != NULL) {
		int i = 0;
		while (nextSong != NULL) {
			BarUiMsg (MSG_LIST, "%2i) \"%s\" by \"%s\"\n", i, nextSong->title,
					nextSong->artist);
			nextSong = nextSong->next;
			i++;
		}
	} else {
		BarUiMsg (MSG_INFO, "No songs in queue.\n");
	}
}

/*	if current station is a quickmix: select stations that are played in
 *	quickmix
 */
void BarUiActSelectQuickMix (BAR_KS_ARGS) {
	PianoReturn_t pRet = PIANO_RET_ERR;

	RETURN_IF_NO_STATION;

	if ((*curStation)->isQuickMix) {
		PianoStation_t *selStation;
		while ((selStation = BarUiSelectStation (ph,
				"Toggle quickmix for station: ", curFd)) != NULL) {
			selStation->useQuickMix = !selStation->useQuickMix;
		}
		BarUiMsg (MSG_INFO, "Setting quickmix stations... ");
		pRet = BarUiPrintPianoStatus (PianoSetQuickmix (ph));
		BarUiStartEventCmd (settings, "stationquickmixtoggle", *curStation,
				*curSong, pRet);
	} else {
		BarUiMsg (MSG_ERR, "Not a QuickMix station.\n");
	}
}

/*	quit
 */
void BarUiActQuit (BAR_KS_ARGS) {
	*doQuit = 1;
	BarUiDoSkipSong (player);
}