map

minimalist audio player
git clone https://git.porkepik.fr/map
Log | Files | Refs | README | LICENSE

commit bd329eea72ea2f9861920d3cd825f5db3a81aaff
parent e4f8ec7ea5e76d35e987eee3bbb349ff0a510865
Author: Thomas Philippe <dev@porkepik.fr>
Date:   Thu, 10 Sep 2020 11:23:56 +0200

fix pausing

Diffstat:
Malsa.c | 4++--
Mmap.c | 70++++++++++++++++++++++++++++++----------------------------------------
Mterm.c | 49++++++++++++++++++++++++++-----------------------
3 files changed, 58 insertions(+), 65 deletions(-)

diff --git a/alsa.c b/alsa.c @@ -106,13 +106,13 @@ alsa_open(int bits, int rate, int channels) void alsa_pause(void) { - snd_pcm_pause(hdl, 1); + snd_pcm_drop(hdl); } void alsa_resume(void) { - snd_pcm_pause(hdl, 0); + snd_pcm_prepare(hdl); } int diff --git a/map.c b/map.c @@ -30,12 +30,11 @@ struct track { /* term.c */ -int write_term(const char *, ...); +int write_term(int, int, const char *, ...); int init_term(void); void end_term(void); int get_term_size(int *, int *); void clear_term(void); -void set_cursor(int, int); char poll_key_event(void); /* alsa.c */ @@ -67,10 +66,8 @@ static int volume; static void print_cursor(int oldx, int newx) { - set_cursor(oldx, 4); - write_term(" "); - set_cursor(newx, 4); - write_term(">"); + write_term(oldx, 4, " "); + write_term(newx, 4, ">"); } static void @@ -78,17 +75,17 @@ print_playlist(struct track **tracks, int start_index, int cursorid) { int i; - set_cursor(rows - 4, 1); - write_term("\x1b[1J"); + write_term(rows - 4, 1, "\x1b[1J"); for (i = start_index; i < start_index + rows - 6; i++) { if (i == ntracks) break; - set_cursor(i - start_index + 1, 7); if (tracks[i]->current) - write_term("\x1b[1K\x1b[1m%s\x1b[0m", tracks[i]->name); + write_term(i - start_index + 1, 7, + "\x1b[1K\x1b[1m%s\x1b[0m", tracks[i]->name); else - write_term("\x1b[1K%s", tracks[i]->name); + write_term(i - start_index + 1, 7, + "\x1b[1K%s", tracks[i]->name); } print_cursor(cursorid, cursorid); } @@ -96,43 +93,32 @@ print_playlist(struct track **tracks, int start_index, int cursorid) static void print_info(struct track track) { - if (track.artist) { - set_cursor(rows - 4, 1); - write_term(" Artist: %s\x1b[K", track.artist); - } - - if (track.album) { - set_cursor(rows - 3, 1); - write_term(" Album: %s\x1b[K", track.album); - } + if (track.artist) + write_term(rows - 4, 1, " Artist: %s\x1b[K", track.artist); + if (track.album) + write_term(rows - 3, 1, " Album: %s\x1b[K", track.album); - set_cursor(rows - 2, 1); - write_term(" Playing: %s\x1b[K", track.title); - - set_cursor(rows - 1, 32); - write_term("Format: %s", track.format); + write_term(rows - 2, 1, " Playing: %s\x1b[K", track.title); + write_term(rows - 1, 32, "Format: %s", track.format); } static void print_timer(int timersec, int track_sec) { - set_cursor(rows - 1, 12); - write_term("\x1b[1K %d:%02d", timersec / 60, timersec % 60); - write_term(" / %d:%02d ", track_sec / 60, track_sec % 60); + write_term(rows - 1, 12, "\x1b[1K %d:%02d", timersec / 60, timersec % 60); + write_term(0, 0, " / %d:%02d ", track_sec / 60, track_sec % 60); } static void print_pause(void) { - set_cursor(rows - 1, 11); - write_term("||"); + write_term(rows - 1, 11, "||"); } static void print_volume(int volume) { - set_cursor(rows - 1, 48); - write_term("Volume: %-3d", volume); + write_term(rows - 1, 48, "Volume: %-3d", volume); } static int @@ -250,12 +236,12 @@ play(void) int gapless = 0; long done, bufsize, tmpbuf_len; float *buf, *newbuf; - time_t start, pause_t; + time_t start, pause; struct track curtrack; while (trackid < ntracks) { next: - pause_t = timersec = 0; + pause = timersec = 0; start = time(NULL); curtrack = *tracks[trackid]; @@ -302,13 +288,18 @@ next: case ' ': tracks[trackid]->current = 0; trackid = toptrack + cursorid - 1; + if (pause) + alsa_resume(); goto endtrack; /* resume/pause */ case 'c': - if (pause_t) - start += difftime(time(NULL), pause_t); - pause_t = pause_t ? 0 : time(NULL); - alsa_resume(); + if (pause) { + start += difftime(time(NULL), pause); + alsa_resume(); + } else { + alsa_pause(); + } + pause = pause ? 0 : time(NULL); break; /* rewind */ case 'h': @@ -353,9 +344,8 @@ next: print_volume(volume); } - if (pause_t) { + if (pause) { print_pause(); - alsa_pause(); usleep(10000); continue; } diff --git a/term.c b/term.c @@ -11,25 +11,38 @@ static struct termios orig_term; ssize_t -write_term(const char *format_str, ...) +write_term(int cx, int cy, const char *format_str, ...) { - char *temp_str; - int temp_len; - size_t format_len; + char *tmpstr; + size_t tmplen; ssize_t bytes_written; va_list args1, args2; + if (cx == 0 && cy == 0) + goto print; + if (cx < 1) + cx = 1; + if (cy < 1) + cy = 1; + tmplen = snprintf(NULL, 0, "\x1b[%d;%dH", cx, cy); + tmpstr = malloc(tmplen + 1); + if (!tmpstr) + return -1; + tmplen = sprintf(tmpstr, "\x1b[%d;%dH", cx, cy); + write(STDOUT_FILENO, tmpstr, tmplen); + +print: va_start(args1, format_str); va_copy(args2, args1); - format_len = vsnprintf(NULL, 0, format_str, args1); - temp_str = malloc(format_len + 1); - if (!temp_str) + tmplen = vsnprintf(NULL, 0, format_str, args1); + tmpstr = malloc(tmplen + 1); + if (!tmpstr) return -1; - temp_len = vsprintf(temp_str, format_str, args2); + tmplen = vsprintf(tmpstr, format_str, args2); va_end(args1); va_end(args2); - bytes_written = write(STDOUT_FILENO, temp_str, temp_len); - free(temp_str); + bytes_written = write(STDOUT_FILENO, tmpstr, tmplen); + free(tmpstr); return bytes_written; } @@ -48,7 +61,7 @@ init_term(void) if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &raw) == -1) return -1; - write_term("\x1b[?25l\x1b[2J\x1b[H"); + write_term(1, 1, "\x1b[?25l\x1b[2J\x1b[H"); return 0; } @@ -56,13 +69,13 @@ void end_term(void) { tcsetattr(STDIN_FILENO, TCSAFLUSH, &orig_term); - write_term("\x1b[?25h"); + write_term(1, 1, "\x1b[?25h"); } void clear_term(void) { - write_term("\x1b[2J\x1b[H"); + write_term(1, 1, "\x1b[2J\x1b[H"); } int @@ -79,16 +92,6 @@ get_term_size(int *rows, int *cols) return 0; } -void -set_cursor(int cx, int cy) -{ - if (cx < 1) - cx = 1; - if (cy < 1) - cy = 1; - write_term("\x1b[%d;%dH", cx, cy); -} - static int kbhit(void) {