| 1 |
/* |
| 2 |
term: terminal control |
| 3 |
|
| 4 |
copyright ?-2023 by the mpg123 project - free software under the terms of the LGPL 2.1 |
| 5 |
see COPYING and AUTHORS files in distribution or http://mpg123.org |
| 6 |
initially written by Michael Hipp |
| 7 |
*/ |
| 8 |
|
| 9 |
#include "mpg123app.h" |
| 10 |
#include <ctype.h> |
| 11 |
|
| 12 |
#include "term.h" |
| 13 |
#include "terms.h" |
| 14 |
#include "common.h" |
| 15 |
#include "playlist.h" |
| 16 |
#include "metaprint.h" |
| 17 |
#include "common/debug.h" |
| 18 |
|
| 19 |
static int term_enable = 0; |
| 20 |
static const char *extrabreak = ""; |
| 21 |
static int seeking = FALSE; |
| 22 |
|
| 23 |
extern out123_handle *ao; |
| 24 |
|
| 25 |
static const int helplen = 18; |
| 26 |
#define HELPFMT "%-18s" |
| 27 |
|
| 28 |
/* Hm, next step would be some system in this, plus configurability... |
| 29 |
Two keys for everything? It's just stop/pause for now... */ |
| 30 |
struct keydef { const char key; const char key2; const char* desc; }; |
| 31 |
struct keydef term_help[] = |
| 32 |
{ |
| 33 |
{ MPG123_STOP_KEY, ' ', "(un)pause playback" } |
| 34 |
,{ MPG123_NEXT_KEY, 0, "next track" } |
| 35 |
,{ MPG123_PREV_KEY, 0, "previous track" } |
| 36 |
,{ MPG123_NEXT_DIR_KEY, 0, "next directory" } |
| 37 |
,{ MPG123_PREV_DIR_KEY, 0, "previous directory" } |
| 38 |
,{ MPG123_BACK_KEY, 0, "back to beginning" } |
| 39 |
,{ MPG123_LOOP_KEY, 0, "A-B loop" } |
| 40 |
,{ MPG123_PAUSE_KEY, 0, "preset loop" } |
| 41 |
,{ MPG123_FORWARD_KEY, 0, "forward" } |
| 42 |
,{ MPG123_REWIND_KEY, 0, "rewind" } |
| 43 |
,{ MPG123_FAST_FORWARD_KEY, 0, "fast forward" } |
| 44 |
,{ MPG123_FAST_REWIND_KEY, 0, "fast rewind" } |
| 45 |
,{ MPG123_FINE_FORWARD_KEY, 0, "fine forward" } |
| 46 |
,{ MPG123_FINE_REWIND_KEY, 0, "fine rewind" } |
| 47 |
,{ MPG123_VOL_UP_KEY, 0, "volume up" } |
| 48 |
,{ MPG123_VOL_DOWN_KEY, 0, "volume down" } |
| 49 |
,{ MPG123_VOL_MUTE_KEY, 0, "(un)mute volume" } |
| 50 |
,{ MPG123_RVA_KEY, 0, "cycle RVA modes" } |
| 51 |
,{ MPG123_VERBOSE_KEY, 0, "cycle verbosity" } |
| 52 |
,{ MPG123_PLAYLIST_KEY, 0, "show playlist" } |
| 53 |
,{ MPG123_TAG_KEY, 0, "tag info" } |
| 54 |
,{ MPG123_MPEG_KEY, 0, "MPEG header info" } |
| 55 |
,{ MPG123_PITCH_UP_KEY, MPG123_PITCH_BUP_KEY, "pitch up + ++" } |
| 56 |
,{ MPG123_PITCH_DOWN_KEY, MPG123_PITCH_BDOWN_KEY, "pitch down - --" } |
| 57 |
,{ MPG123_PITCH_ZERO_KEY, 0, "zero pitch" } |
| 58 |
,{ MPG123_BOOKMARK_KEY, 0, "print bookmark" } |
| 59 |
,{ MPG123_HELP_KEY, 0, "this help" } |
| 60 |
,{ MPG123_QUIT_KEY, 0, "quit" } |
| 61 |
,{ MPG123_EQ_RESET_KEY, 0, "flat equalizer" } |
| 62 |
,{ MPG123_EQ_SHOW_KEY, 0, "show equalizer" } |
| 63 |
,{ MPG123_BASS_UP_KEY, 0, "more bass" } |
| 64 |
,{ MPG123_BASS_DOWN_KEY, 0, "less bass" } |
| 65 |
,{ MPG123_MID_UP_KEY, 0, "more mids" } |
| 66 |
,{ MPG123_MID_DOWN_KEY, 0, "less mids" } |
| 67 |
,{ MPG123_TREBLE_UP_KEY, 0, "more treble" } |
| 68 |
,{ MPG123_TREBLE_DOWN_KEY, 0, "less treble" } |
| 69 |
}; |
| 70 |
|
| 71 |
/* initialze terminal */ |
| 72 |
int term_init(void) |
| 73 |
{ |
| 74 |
const char hide_cursor[] = "\x1b[?25l"; |
| 75 |
debug("term_init"); |
| 76 |
|
| 77 |
if(term_have_fun(STDERR_FILENO, param.term_visual)) |
| 78 |
fprintf(stderr, "%s", hide_cursor); |
| 79 |
|
| 80 |
if(param.verbose) |
| 81 |
extrabreak = "\n"; |
| 82 |
debug1("param.term_ctrl: %i", param.term_ctrl); |
| 83 |
if(!param.term_ctrl) |
| 84 |
return 0; |
| 85 |
|
| 86 |
term_enable = 0; |
| 87 |
errno = 0; |
| 88 |
if(term_setup() < 0) |
| 89 |
{ |
| 90 |
if(errno) |
| 91 |
merror("failed to set up terminal: %s", INT123_strerror(errno)); |
| 92 |
else |
| 93 |
error("failed to set up terminal"); |
| 94 |
return -1; |
| 95 |
} |
| 96 |
term_enable = 1; |
| 97 |
return 0; |
| 98 |
} |
| 99 |
|
| 100 |
void term_hint(void) |
| 101 |
{ |
| 102 |
if(term_enable) |
| 103 |
fprintf(stderr, "\nTerminal control enabled, press 'h' for listing of keys and functions.\n\n"); |
| 104 |
} |
| 105 |
|
| 106 |
static void term_handle_input(mpg123_handle *, out123_handle *, int); |
| 107 |
|
| 108 |
// A-B looping sets pause_cycle at runtime baseon the difference to |
| 109 |
// pause_begin. Keeping the broken wording for 'pause' for now. To the |
| 110 |
// outside world, it is 'looping'. |
| 111 |
static double pause_begin = -1; |
| 112 |
static off_t pause_cycle; |
| 113 |
|
| 114 |
static int print_index(mpg123_handle *mh) |
| 115 |
{ |
| 116 |
int err; |
| 117 |
size_t c, fill; |
| 118 |
off_t *index; |
| 119 |
off_t step; |
| 120 |
err = mpg123_index(mh, &index, &step, &fill); |
| 121 |
if(err == MPG123_ERR) |
| 122 |
{ |
| 123 |
fprintf(stderr, "Error accessing frame index: %s\n", mpg123_strerror(mh)); |
| 124 |
return err; |
| 125 |
} |
| 126 |
for(c=0; c < fill;++c) |
| 127 |
fprintf(stderr, "[%lu] %lu: %li (+%li)\n", |
| 128 |
(unsigned long) c, |
| 129 |
(unsigned long) (c*step), |
| 130 |
(long) index[c], |
| 131 |
(long) (c ? index[c]-index[c-1] : 0)); |
| 132 |
return MPG123_OK; |
| 133 |
} |
| 134 |
|
| 135 |
static off_t offset = 0; |
| 136 |
|
| 137 |
void term_new_track(void) |
| 138 |
{ |
| 139 |
playstate = STATE_PLAYING; |
| 140 |
pause_begin = -1; |
| 141 |
} |
| 142 |
|
| 143 |
/* Go back to the start for the cyclic pausing. */ |
| 144 |
void pause_recycle(mpg123_handle *fr) |
| 145 |
{ |
| 146 |
/* Take care not to go backwards in time in steps of 1 frame |
| 147 |
That is what the +1 is for. */ |
| 148 |
pause_cycle=(off_t)(param.pauseloop/mpg123_tpf(fr)); |
| 149 |
offset-=pause_cycle; |
| 150 |
} |
| 151 |
|
| 152 |
off_t term_control(mpg123_handle *fr, out123_handle *ao) |
| 153 |
{ |
| 154 |
offset = 0; |
| 155 |
debug2("control for frame: %" PRIiMAX ", enable: %i", (intmax_t)mpg123_tellframe(fr), term_enable); |
| 156 |
if(!term_enable) return 0; |
| 157 |
|
| 158 |
if(playstate==STATE_LOOPING) |
| 159 |
{ |
| 160 |
/* pause_cycle counts the remaining frames _after_ this one, thus <0, not ==0 . */ |
| 161 |
if(--pause_cycle < 0) |
| 162 |
pause_recycle(fr); |
| 163 |
} |
| 164 |
|
| 165 |
do |
| 166 |
{ |
| 167 |
off_t old_offset = offset; |
| 168 |
term_handle_input(fr, ao, seeking); |
| 169 |
if((offset < 0) && (-offset > framenum)) offset = - framenum; |
| 170 |
if(param.verbose && offset != old_offset) |
| 171 |
print_stat(fr,offset,ao,1,¶m); |
| 172 |
} while (!intflag && playstate==STATE_STOPPED); |
| 173 |
|
| 174 |
/* Make the seeking experience with buffer less annoying. |
| 175 |
No sound during seek, but at least it is possible to go backwards. */ |
| 176 |
if(offset) |
| 177 |
{ |
| 178 |
if((offset = mpg123_seek_frame(fr, offset, SEEK_CUR)) >= 0) |
| 179 |
debug1("seeked to %" PRIiMAX, (intmax_t)offset); |
| 180 |
else error1("seek failed: %s!", mpg123_strerror(fr)); |
| 181 |
/* Buffer resync already happened on un-stop? */ |
| 182 |
/* if(param.usebuffer) audio_drop(ao);*/ |
| 183 |
} |
| 184 |
return 0; |
| 185 |
} |
| 186 |
|
| 187 |
/* Stop playback while seeking if buffer is involved. */ |
| 188 |
static void seekmode(mpg123_handle *mh, out123_handle *ao) |
| 189 |
{ |
| 190 |
if(param.usebuffer && playstate!=STATE_STOPPED) |
| 191 |
{ |
| 192 |
int channels = 0; |
| 193 |
int encoding = 0; |
| 194 |
int pcmframe; |
| 195 |
off_t back_samples = 0; |
| 196 |
|
| 197 |
playstate = STATE_STOPPED; |
| 198 |
out123_pause(ao); |
| 199 |
if(param.verbose) |
| 200 |
print_stat(mh, 0, ao, 0, ¶m); |
| 201 |
mpg123_getformat(mh, NULL, &channels, &encoding); |
| 202 |
pcmframe = out123_encsize(encoding)*channels; |
| 203 |
if(pcmframe > 0) |
| 204 |
back_samples = out123_buffered(ao)/pcmframe; |
| 205 |
if(param.verbose > 2) |
| 206 |
fprintf(stderr, "\nseeking back %" PRIiMAX " samples from %" PRIiMAX "\n" |
| 207 |
, (intmax_t)back_samples, (intmax_t)mpg123_tell(mh)); |
| 208 |
mpg123_seek(mh, -back_samples, SEEK_CUR); |
| 209 |
out123_drop(ao); |
| 210 |
if(param.verbose > 2) |
| 211 |
fprintf(stderr, "\ndropped, now at %" PRIiMAX "\n" |
| 212 |
, (intmax_t)mpg123_tell(mh)); |
| 213 |
fprintf(stderr, "%s", MPG123_STOPPED_STRING); |
| 214 |
if(param.verbose) |
| 215 |
print_stat(mh, 0, ao, 1, ¶m); |
| 216 |
} |
| 217 |
} |
| 218 |
|
| 219 |
static void print_term_help(struct keydef *def) |
| 220 |
{ |
| 221 |
if(def->key2) |
| 222 |
{ |
| 223 |
if(isspace(def->key2)) |
| 224 |
fprintf(stderr, "%c '%c'", def->key, def->key2); |
| 225 |
else |
| 226 |
fprintf(stderr, "%c %c ", def->key, def->key2); |
| 227 |
} |
| 228 |
else fprintf(stderr, "%c ", def->key); |
| 229 |
|
| 230 |
fprintf(stderr, " " HELPFMT, def->desc); |
| 231 |
} |
| 232 |
|
| 233 |
static void term_handle_key(mpg123_handle *fr, out123_handle *ao, char val) |
| 234 |
{ |
| 235 |
debug1("term_handle_key: %c", val); |
| 236 |
switch(val) |
| 237 |
{ |
| 238 |
case MPG123_BACK_KEY: |
| 239 |
out123_pause(ao); |
| 240 |
out123_drop(ao); |
| 241 |
// Revisit: What does that really achieve? |
| 242 |
if(playstate==STATE_LOOPING) |
| 243 |
pause_cycle=(int)(param.pauseloop/mpg123_tpf(fr)); |
| 244 |
|
| 245 |
if(mpg123_seek_frame(fr, 0, SEEK_SET) < 0) |
| 246 |
error1("Seek to begin failed: %s", mpg123_strerror(fr)); |
| 247 |
|
| 248 |
framenum=0; |
| 249 |
break; |
| 250 |
case MPG123_NEXT_KEY: |
| 251 |
out123_pause(ao); |
| 252 |
out123_drop(ao); |
| 253 |
next_track(); |
| 254 |
break; |
| 255 |
case MPG123_NEXT_DIR_KEY: |
| 256 |
out123_pause(ao); |
| 257 |
out123_drop(ao); |
| 258 |
next_dir(); |
| 259 |
break; |
| 260 |
case MPG123_QUIT_KEY: |
| 261 |
debug("QUIT"); |
| 262 |
if(playstate==STATE_STOPPED) |
| 263 |
{ |
| 264 |
if(param.verbose) |
| 265 |
print_stat(fr,0,ao,0,¶m); |
| 266 |
|
| 267 |
playstate=STATE_PLAYING; // really necessary/sensible? |
| 268 |
out123_pause(ao); /* no chance for annoying underrun warnings */ |
| 269 |
out123_drop(ao); |
| 270 |
} |
| 271 |
set_intflag(); |
| 272 |
offset = 0; |
| 273 |
break; |
| 274 |
case MPG123_LOOP_KEY: |
| 275 |
// In paused (looping) state, the loop key ends the loop just like the other one. |
| 276 |
// Otherwise, it starts playback and enters A-? mode. If in A-? mode, it |
| 277 |
// sets the loop interval and then again falls through. |
| 278 |
if(playstate != STATE_LOOPING) |
| 279 |
{ |
| 280 |
playstate = STATE_AB; |
| 281 |
// Careful with positioning, output might have |
| 282 |
long outrate = 0; |
| 283 |
int outframesize = 0; |
| 284 |
long inrate = 0; |
| 285 |
if(out123_getformat(ao, &outrate, NULL, NULL, &outframesize) || outrate==0) |
| 286 |
break; |
| 287 |
if(mpg123_getformat(fr, &inrate, NULL, NULL) || inrate==0) |
| 288 |
break; |
| 289 |
|
| 290 |
double position = (double)mpg123_tell(fr)/inrate + (double)out123_buffered(ao)/(outrate * outframesize); |
| 291 |
if(pause_begin < 0) |
| 292 |
{ |
| 293 |
pause_begin = position; |
| 294 |
if(param.verbose) |
| 295 |
print_stat(fr, 0, ao, 1, ¶m); |
| 296 |
else |
| 297 |
fprintf(stderr, "%s", MPG123_AB_STRING); |
| 298 |
break; |
| 299 |
} else if(position <= pause_begin) |
| 300 |
{ |
| 301 |
// Pathological situation: You seeked around, whatever. No loop. |
| 302 |
playstate=STATE_LOOPING; // Let fall-through fix up things. |
| 303 |
} |
| 304 |
{ |
| 305 |
param.pauseloop = (position > pause_begin) ? (position-pause_begin) : mpg123_tpf(fr); |
| 306 |
// Fall throuth to start looping. |
| 307 |
} |
| 308 |
} |
| 309 |
case MPG123_PAUSE_KEY: |
| 310 |
{ |
| 311 |
playstate = playstate == STATE_LOOPING ? STATE_PLAYING : STATE_LOOPING; |
| 312 |
pause_begin = -1; |
| 313 |
size_t buffered = out123_buffered(ao); |
| 314 |
out123_pause(ao); /* underrun awareness */ |
| 315 |
out123_drop(ao); |
| 316 |
if(playstate == STATE_LOOPING) |
| 317 |
{ |
| 318 |
// Make output buffer react immediately, dropping decoded audio |
| 319 |
// and (at least trying to) seeking back in input. |
| 320 |
out123_param_float(ao, OUT123_PRELOAD, 0.); |
| 321 |
if(buffered) |
| 322 |
{ |
| 323 |
int framesize = 1; |
| 324 |
if(!out123_getformat(ao, NULL, NULL, NULL, &framesize)) |
| 325 |
{ |
| 326 |
buffered /= framesize; |
| 327 |
mpg123_seek(fr, -buffered, SEEK_CUR); |
| 328 |
} |
| 329 |
} |
| 330 |
pause_recycle(fr); |
| 331 |
} |
| 332 |
else |
| 333 |
out123_param_float(ao, OUT123_PRELOAD, param.preload); |
| 334 |
if(param.verbose) |
| 335 |
print_stat(fr, 0, ao, 1, ¶m); |
| 336 |
else |
| 337 |
fprintf(stderr, "%s", (playstate == STATE_LOOPING) ? MPG123_PAUSED_STRING : MPG123_EMPTY_STRING); |
| 338 |
} |
| 339 |
break; |
| 340 |
case MPG123_STOP_KEY: |
| 341 |
case ' ': |
| 342 |
/* TODO: Verify/ensure that there is no "chirp from the past" when |
| 343 |
seeking while stopped. */ |
| 344 |
if(playstate == STATE_LOOPING) |
| 345 |
offset -= pause_cycle; |
| 346 |
playstate = playstate == STATE_STOPPED ? STATE_PLAYING : STATE_STOPPED; |
| 347 |
if(playstate == STATE_STOPPED) |
| 348 |
out123_pause(ao); |
| 349 |
else |
| 350 |
{ |
| 351 |
if(offset) /* If position changed, old is outdated. */ |
| 352 |
out123_drop(ao); |
| 353 |
/* No out123_continue(), that's triggered by out123_play(). */ |
| 354 |
} |
| 355 |
if(param.verbose) |
| 356 |
print_stat(fr, 0, ao, 1, ¶m); |
| 357 |
else |
| 358 |
fprintf(stderr, "%s", (playstate==STATE_STOPPED) ? MPG123_STOPPED_STRING : MPG123_EMPTY_STRING); |
| 359 |
break; |
| 360 |
case MPG123_FINE_REWIND_KEY: |
| 361 |
seekmode(fr, ao); |
| 362 |
offset--; |
| 363 |
break; |
| 364 |
case MPG123_FINE_FORWARD_KEY: |
| 365 |
seekmode(fr, ao); |
| 366 |
offset++; |
| 367 |
break; |
| 368 |
case MPG123_REWIND_KEY: |
| 369 |
seekmode(fr, ao); |
| 370 |
offset-=10; |
| 371 |
break; |
| 372 |
case MPG123_FORWARD_KEY: |
| 373 |
seekmode(fr, ao); |
| 374 |
offset+=10; |
| 375 |
break; |
| 376 |
case MPG123_FAST_REWIND_KEY: |
| 377 |
seekmode(fr, ao); |
| 378 |
offset-=50; |
| 379 |
break; |
| 380 |
case MPG123_FAST_FORWARD_KEY: |
| 381 |
seekmode(fr, ao); |
| 382 |
offset+=50; |
| 383 |
break; |
| 384 |
case MPG123_VOL_UP_KEY: |
| 385 |
mpg123_volume_change_db(fr, +1); |
| 386 |
break; |
| 387 |
case MPG123_VOL_DOWN_KEY: |
| 388 |
mpg123_volume_change_db(fr, -1); |
| 389 |
break; |
| 390 |
case MPG123_VOL_MUTE_KEY: |
| 391 |
set_mute(ao, muted=!muted); |
| 392 |
break; |
| 393 |
case MPG123_EQ_RESET_KEY: |
| 394 |
mpg123_reset_eq(fr); |
| 395 |
break; |
| 396 |
case MPG123_EQ_SHOW_KEY: |
| 397 |
{ |
| 398 |
if(param.verbose) |
| 399 |
print_stat(fr,0,ao,0,¶m); |
| 400 |
// Assuming only changes happen via terminal control, these 3 values |
| 401 |
// are what counts. |
| 402 |
fprintf( stderr, "%s\nbass: %.3f\nmid: %.3f\ntreble: %.3f\n\n" |
| 403 |
, extrabreak |
| 404 |
, mpg123_geteq(fr, MPG123_LEFT, 0) |
| 405 |
, mpg123_geteq(fr, MPG123_LEFT, 1) |
| 406 |
, mpg123_geteq(fr, MPG123_LEFT, 2) |
| 407 |
); |
| 408 |
} |
| 409 |
break; |
| 410 |
case MPG123_BASS_UP_KEY: |
| 411 |
mpg123_eq_change(fr, MPG123_LR, 0, 0, +1); |
| 412 |
break; |
| 413 |
case MPG123_BASS_DOWN_KEY: |
| 414 |
mpg123_eq_change(fr, MPG123_LR, 0, 0, -1); |
| 415 |
break; |
| 416 |
case MPG123_MID_UP_KEY: |
| 417 |
mpg123_eq_change(fr, MPG123_LR, 1, 1, +1); |
| 418 |
break; |
| 419 |
case MPG123_MID_DOWN_KEY: |
| 420 |
mpg123_eq_change(fr, MPG123_LR, 1, 1, -1); |
| 421 |
break; |
| 422 |
case MPG123_TREBLE_UP_KEY: |
| 423 |
mpg123_eq_change(fr, MPG123_LR, 2, 31, +1); |
| 424 |
break; |
| 425 |
case MPG123_TREBLE_DOWN_KEY: |
| 426 |
mpg123_eq_change(fr, MPG123_LR, 2, 31, -1); |
| 427 |
break; |
| 428 |
case MPG123_PITCH_UP_KEY: |
| 429 |
case MPG123_PITCH_BUP_KEY: |
| 430 |
case MPG123_PITCH_DOWN_KEY: |
| 431 |
case MPG123_PITCH_BDOWN_KEY: |
| 432 |
case MPG123_PITCH_ZERO_KEY: |
| 433 |
{ |
| 434 |
double new_pitch = param.pitch; |
| 435 |
switch(val) /* Not tolower here! */ |
| 436 |
{ |
| 437 |
case MPG123_PITCH_UP_KEY: new_pitch += MPG123_PITCH_VAL; break; |
| 438 |
case MPG123_PITCH_BUP_KEY: new_pitch += MPG123_PITCH_BVAL; break; |
| 439 |
case MPG123_PITCH_DOWN_KEY: new_pitch -= MPG123_PITCH_VAL; break; |
| 440 |
case MPG123_PITCH_BDOWN_KEY: new_pitch -= MPG123_PITCH_BVAL; break; |
| 441 |
case MPG123_PITCH_ZERO_KEY: new_pitch = 0.0; break; |
| 442 |
} |
| 443 |
if(param.verbose) |
| 444 |
print_stat(fr,0,ao,0,¶m); |
| 445 |
set_pitch(fr, ao, new_pitch); |
| 446 |
if(param.verbose > 1) |
| 447 |
fprintf(stderr, "\nNew pitch: %f\n", param.pitch); |
| 448 |
if(param.verbose) |
| 449 |
print_stat(fr,0,ao,1,¶m); |
| 450 |
} |
| 451 |
break; |
| 452 |
case MPG123_VERBOSE_KEY: |
| 453 |
param.verbose++; |
| 454 |
if(param.verbose > VERBOSE_MAX) |
| 455 |
{ |
| 456 |
param.verbose = 0; |
| 457 |
clear_stat(); |
| 458 |
extrabreak = ""; |
| 459 |
} else |
| 460 |
extrabreak = "\n"; |
| 461 |
mpg123_param(fr, MPG123_VERBOSE, param.verbose, 0); |
| 462 |
break; |
| 463 |
case MPG123_RVA_KEY: |
| 464 |
if(++param.rva > MPG123_RVA_MAX) param.rva = 0; |
| 465 |
mpg123_param(fr, MPG123_RVA, param.rva, 0); |
| 466 |
mpg123_volume_change(fr, 0.); |
| 467 |
if(param.verbose) |
| 468 |
print_stat(fr,0,ao,1,¶m); |
| 469 |
break; |
| 470 |
case MPG123_PREV_KEY: |
| 471 |
out123_pause(ao); |
| 472 |
out123_drop(ao); |
| 473 |
|
| 474 |
prev_track(); |
| 475 |
break; |
| 476 |
case MPG123_PREV_DIR_KEY: |
| 477 |
out123_pause(ao); |
| 478 |
out123_drop(ao); |
| 479 |
prev_dir(); |
| 480 |
break; |
| 481 |
case MPG123_PLAYLIST_KEY: |
| 482 |
if(param.verbose) |
| 483 |
print_stat(fr,0,ao,0,¶m); |
| 484 |
fprintf(stderr, "%s\nPlaylist (\">\" indicates current track):\n", param.verbose ? "\n" : ""); |
| 485 |
print_playlist(stderr, 1); |
| 486 |
fprintf(stderr, "\n"); |
| 487 |
break; |
| 488 |
case MPG123_TAG_KEY: |
| 489 |
if(param.verbose) |
| 490 |
print_stat(fr,0,ao,0,¶m); |
| 491 |
fprintf(stderr, "%s", extrabreak); |
| 492 |
print_id3_tag(fr, param.long_id3, stderr, term_width(STDERR_FILENO)); |
| 493 |
break; |
| 494 |
case MPG123_MPEG_KEY: |
| 495 |
if(param.verbose) |
| 496 |
print_stat(fr,0,ao,0,¶m); |
| 497 |
fprintf(stderr, "%s", extrabreak); |
| 498 |
if(param.verbose > 1) |
| 499 |
print_header(fr); |
| 500 |
else |
| 501 |
print_header_compact(fr); |
| 502 |
break; |
| 503 |
case MPG123_HELP_KEY: |
| 504 |
{ /* This is more than the one-liner before, but it's less spaghetti. */ |
| 505 |
int i; |
| 506 |
if(param.verbose) |
| 507 |
print_stat(fr,0,ao,0,¶m); |
| 508 |
fprintf(stderr,"%s\n -= terminal control keys =-\n\n", extrabreak); |
| 509 |
int linelen = term_width(STDERR_FILENO); |
| 510 |
int colwidth = helplen+6; |
| 511 |
int columns = linelen > colwidth ? ((linelen+2)/(colwidth+2)) : 1; |
| 512 |
int j = 0; |
| 513 |
for(i=0; i<(sizeof(term_help)/sizeof(struct keydef)); ++i) |
| 514 |
{ |
| 515 |
if(j) |
| 516 |
fprintf(stderr, " "); |
| 517 |
print_term_help(term_help+i); |
| 518 |
j = (j+1)%columns; |
| 519 |
if(!j) |
| 520 |
fprintf(stderr, "\n"); |
| 521 |
} |
| 522 |
fprintf(stderr, "\n\nNumber row jumps in 10%% steps.\n\n"); |
| 523 |
} |
| 524 |
break; |
| 525 |
case MPG123_FRAME_INDEX_KEY: |
| 526 |
case MPG123_VARIOUS_INFO_KEY: |
| 527 |
if(param.verbose) |
| 528 |
{ |
| 529 |
print_stat(fr,0,ao,0,¶m); |
| 530 |
fprintf(stderr, "\n"); |
| 531 |
} |
| 532 |
switch(val) /* because of tolower() ... */ |
| 533 |
{ |
| 534 |
case MPG123_FRAME_INDEX_KEY: |
| 535 |
print_index(fr); |
| 536 |
{ |
| 537 |
long accurate; |
| 538 |
if(mpg123_getstate(fr, MPG123_ACCURATE, &accurate, NULL) == MPG123_OK) |
| 539 |
fprintf(stderr, "Accurate position: %s\n", (accurate == 0 ? "no" : "yes")); |
| 540 |
else |
| 541 |
error1("Unable to get state: %s", mpg123_strerror(fr)); |
| 542 |
} |
| 543 |
break; |
| 544 |
case MPG123_VARIOUS_INFO_KEY: |
| 545 |
{ |
| 546 |
const char* curdec = mpg123_current_decoder(fr); |
| 547 |
if(curdec == NULL) fprintf(stderr, "Cannot get decoder info!\n"); |
| 548 |
else fprintf(stderr, "Active decoder: %s\n", curdec); |
| 549 |
} |
| 550 |
} |
| 551 |
break; |
| 552 |
case '0': |
| 553 |
case '1': |
| 554 |
case '2': |
| 555 |
case '3': |
| 556 |
case '4': |
| 557 |
case '5': |
| 558 |
case '6': |
| 559 |
case '7': |
| 560 |
case '8': |
| 561 |
case '9': |
| 562 |
{ |
| 563 |
off_t len; |
| 564 |
int num; |
| 565 |
num = val == '0' ? 10 : val - '0'; |
| 566 |
--num; /* from 0 to 9 */ |
| 567 |
|
| 568 |
/* Do not swith to seekmode() here, as we are jumping once to a |
| 569 |
specific position. Dropping buffer contents is enough and there |
| 570 |
is no race filling the buffer or waiting for more incremental |
| 571 |
seek orders. */ |
| 572 |
len = mpg123_length(fr); |
| 573 |
out123_pause(ao); |
| 574 |
out123_drop(ao); |
| 575 |
if(len > 0) |
| 576 |
mpg123_seek(fr, (off_t)( (num/10.)*len ), SEEK_SET); |
| 577 |
else |
| 578 |
error("Not seeking as track length cannot be determined."); |
| 579 |
} |
| 580 |
break; |
| 581 |
case MPG123_BOOKMARK_KEY: |
| 582 |
continue_msg("BOOKMARK"); |
| 583 |
break; |
| 584 |
default: |
| 585 |
; |
| 586 |
} |
| 587 |
} |
| 588 |
|
| 589 |
#ifdef DEBUG |
| 590 |
#define SEQ_INIT int seq_len = 1 |
| 591 |
#define SEQ_INCR ++seq_len |
| 592 |
#else |
| 593 |
#define SEQ_INIT |
| 594 |
#define SEQ_INCR |
| 595 |
#endif |
| 596 |
|
| 597 |
static void term_handle_input(mpg123_handle *fr, out123_handle *ao, int do_delay) |
| 598 |
{ |
| 599 |
char val; |
| 600 |
if(term_get_key(playstate==STATE_STOPPED, do_delay ? 1 : 0, &val)) |
| 601 |
{ |
| 602 |
// Crude ignoring of 7-bit escape sequences. 8-bit ones you better really handle |
| 603 |
// in the terminal and do not pass on. The idea is that pressing cursor keys |
| 604 |
// that contain plain characters in the triggered escape sequences should |
| 605 |
// not confuse mpg123 terminal control At the same time, the user pressing |
| 606 |
// ESC on its own should not confuse mpg123 into starting a sequence. |
| 607 |
// So the assumption is quick succession of the bytes for real sequences. |
| 608 |
// If the sequence has been ignoret, return, as this has been handled. Come |
| 609 |
// back for real control. |
| 610 |
if(val == 0x1b) // ESC, get next key, if any |
| 611 |
{ |
| 612 |
SEQ_INIT; |
| 613 |
if(term_get_key(FALSE, -1, &val)) |
| 614 |
{ |
| 615 |
SEQ_INCR; |
| 616 |
if(val == 'Z') // SCI, single character to drop, if any |
| 617 |
term_get_key(FALSE, -1, &val); |
| 618 |
else if(val == '[') // sequence begin |
| 619 |
{ |
| 620 |
while(term_get_key(FALSE, -1, &val) && strchr("0123456789;", val)) |
| 621 |
SEQ_INCR; |
| 622 |
} // else simple single-character escape |
| 623 |
} |
| 624 |
mdebug("dropped escape sequence of length %d", seq_len); |
| 625 |
return; |
| 626 |
} else |
| 627 |
mdebug("got key: %02x", val); |
| 628 |
term_handle_key(fr, ao, val); |
| 629 |
} |
| 630 |
} |
| 631 |
|
| 632 |
void term_exit(void) |
| 633 |
{ |
| 634 |
mdebug("term_enable=%i", term_enable); |
| 635 |
const char cursor_restore[] = "\x1b[?25h"; |
| 636 |
/* Bring cursor back. */ |
| 637 |
if(term_have_fun(STDERR_FILENO, param.term_visual)) |
| 638 |
fprintf(stderr, "%s", cursor_restore); |
| 639 |
|
| 640 |
if(!term_enable) return; |
| 641 |
|
| 642 |
term_restore(); |
| 643 |
} |