tmus

TUI Music Player
git clone https://git.sinitax.com/sinitax/tmus
Log | Files | Refs | Submodules | LICENSE | sfeed.txt

commit 231398ebfff82c68ac33205da6fd957ec1e5cf09
parent 852d8efa882fe80718d63352b5432d2a04c2de74
Author: Louis Burda <quent.burda@gmail.com>
Date:   Wed, 26 Jan 2022 17:34:30 +0100

Added index files, fixed cmd input bugs

Diffstat:
M.gitignore | 2++
MMakefile | 2+-
Msrc/main.c | 143+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++----------------
Msrc/player.c | 12++++++++++--
Msrc/tag.c | 2++
Msrc/tag.h | 2++
Msrc/track.c | 7++-----
Msrc/track.h | 2+-
8 files changed, 134 insertions(+), 38 deletions(-)

diff --git a/.gitignore b/.gitignore @@ -5,3 +5,5 @@ todo *.gch .cache log* +gmon.out* +profile.txt diff --git a/Makefile b/Makefile @@ -1,4 +1,4 @@ -CFLAGS = -I src -g $(shell pkg-config --cflags glib-2.0 dbus-1) +CFLAGS = -I src -g -pg $(shell pkg-config --cflags glib-2.0 dbus-1) LDLIBS = -lcurses -lmpdclient $(shell pkg-config --libs glib-2.0 dbus-1) DEPFLAGS = -MT $@ -MMD -MP -MF build/$*.d diff --git a/src/main.c b/src/main.c @@ -123,7 +123,8 @@ static void data_load(void); static void data_save(void); static void data_free(void); -static int tracks_load(struct tag *tag); +static void index_update(struct tag *tag); +static void tracks_load(struct tag *tag); static void tracks_save(struct tag *tag); static void pane_title(struct pane *pane, const char *title, int highlight); @@ -202,7 +203,7 @@ init(void) void cleanup(int exitcode, void* arg) { - tui_end(); + if (!exitcode) tui_end(); if (!exitcode) data_save(); data_free(); @@ -308,6 +309,8 @@ data_load(void) { struct dirent *ent; struct tag *tag; + struct stat st; + char *path; DIR *dir; tracks = LIST_HEAD; @@ -325,11 +328,16 @@ data_load(void) if (!strcmp(ent->d_name, "..")) continue; - tag = tag_init(datadir, ent->d_name); - if (tracks_load(tag)) - tag_free(tag); - else + path = aprintf("%s/%s", datadir, ent->d_name); + ASSERT(path != NULL); + + if (!stat(path, &st) && S_ISDIR(st.st_mode)) { + tag = tag_init(datadir, ent->d_name); + tracks_load(tag); list_push_back(&tags, LINK(tag)); + } + + free(path); } closedir(dir); @@ -367,28 +375,87 @@ data_free(void) } } -int -tracks_load(struct tag *tag) +void +index_update(struct tag *tag) { struct track *track, *track_iter; struct dirent *ent; struct link *iter; struct ref *ref; + struct stat st; + char *path; + FILE *file; DIR *dir; + int fid; + + path = aprintf("%s/index", tag->fpath); + ASSERT(path != NULL); + + file = fopen(path, "w+"); + ASSERT(file != NULL); + free(path); dir = opendir(tag->fpath); - if (!dir) return 1; + ASSERT(dir != NULL); + while ((ent = readdir(dir))) { if (!strcmp(ent->d_name, ".")) continue; if (!strcmp(ent->d_name, "..")) continue; + if (!strcmp(ent->d_name, "index")) + continue; /* skip files without extension */ if (!strchr(ent->d_name, '.')) continue; - track = track_init(tag->fpath, ent->d_name); + path = aprintf("%s/%s", tag->fpath, ent->d_name); + ASSERT(path != NULL); + fid = stat(path, &st) ? -1 : st.st_ino; + free(path); + + fprintf(file, "%i:%s\n", fid, ent->d_name); + } + + closedir(dir); + fclose(file); +} + +void +tracks_load(struct tag *tag) +{ + char linebuf[1024]; + struct link *link; + struct track *track; + struct track *track2; + struct ref *ref; + char *index_path; + char *track_name, *sep; + int track_fid; + FILE *file; + + index_path = aprintf("%s/index", tag->fpath); + ASSERT(index_path != NULL); + + file = fopen(index_path, "r"); + if (file == NULL) { + index_update(tag); + file = fopen(index_path, "r"); + ASSERT(file != NULL); + } + + while (fgets(linebuf, sizeof(linebuf), file)) { + sep = strchr(linebuf, '\n'); + if (sep) *sep = '\0'; + + sep = strchr(linebuf, ':'); + ASSERT(sep != NULL); + *sep = '\0'; + + track_fid = atoi(linebuf); + track_name = sep + 1; + track = track_init(tag->fpath, track_name, track_fid); ref = ref_init(tag); ASSERT(ref != NULL); @@ -398,27 +465,46 @@ tracks_load(struct tag *tag) ASSERT(ref != NULL); list_push_back(&tag->tracks, LINK(ref)); - for (iter = tracks.next; iter; iter = iter->next) { - track_iter = UPCAST(iter, struct ref)->data; - if (track->fid == track_iter->fid) + for (link = tracks.next; link; link = link->next) { + track2 = UPCAST(link, struct ref)->data; + if (track->fid == track2->fid) break; } - if (!iter) { + if (!link) { ref = ref_init(track); ASSERT(ref != NULL); list_push_back(&tracks, LINK(ref)); } } - closedir(dir); - return 0; + fclose(file); + free(index_path); } void tracks_save(struct tag *tag) { - + struct track *track; + struct link *link; + char *index_path; + FILE *file; + + /* write playlist back to index file */ + + index_path = aprintf("%s/index", tag->fpath); + ASSERT(index != NULL); + + file = fopen(index_path, "w+"); + ASSERT(file != NULL); + + for (link = tracks.next; link; link = link->next) { + track = UPCAST(link, struct ref)->data; + fprintf(file, "%i:%s\n", track->fid, track->fname); + } + + fclose(file); + free(index_path); } void @@ -771,21 +857,14 @@ int cmd_pane_input(wint_t c) { wchar_t *res; - - if (cmd_mode == IMODE_EXECUTE) { - history = &command_history; - completion = command_name_gen; - } else if (cmd_mode == IMODE_TRACK_SEARCH) { - history = &track_search_history; - completion = track_name_gen; - } else if (cmd_mode == IMODE_TAG_SEARCH) { - history = &tag_search_history; - completion = tag_name_gen; - } + int match; switch (c) { case KEY_ESC: - if (history->sel == history->input) + match = wcscmp(completion_query.buf, history->input->buf); + if (!completion_reset && match) + inputln_copy(history->input, &completion_query); + else if (history->sel == history->input) pane_sel = pane_top_sel; else history->sel = history->input; @@ -1060,16 +1139,22 @@ main_input(wint_t c) cmd_mode = IMODE_EXECUTE; pane_sel = &pane_bot; completion_reset = 1; + history = &command_history; + completion = command_name_gen; break; case L'/': cmd_mode = IMODE_TRACK_SEARCH; pane_sel = &pane_bot; completion_reset = 1; + history = &track_search_history; + completion = track_name_gen; break; case L'?': cmd_mode = IMODE_TAG_SEARCH; pane_sel = &pane_bot; completion_reset = 1; + history = &tag_search_history; + completion = tag_name_gen; break; case L'+': player_set_volume(MIN(100, player->volume + 5)); diff --git a/src/player.c b/src/player.c @@ -145,7 +145,11 @@ player_update(void) const char *tmp; status = mpd_run_status(player->conn); - ASSERT(status != NULL); + if (status == NULL) { + fprintf(stderr, "MPD Fatal Error: %s\n", + mpd_connection_get_error_message(player->conn)); + exit(1); + } song = mpd_run_current_song(player->conn); if (!song) { @@ -200,7 +204,11 @@ player_update(void) /* TODO move prev / next handling to own functions */ status = mpd_run_status(player->conn); - ASSERT(status != NULL); + if (status == NULL) { + fprintf(stderr, "MPD Fatal Error: %s\n", + mpd_connection_get_error_message(player->conn)); + exit(1); + } song = mpd_run_current_song(player->conn); if (song) { diff --git a/src/tag.c b/src/tag.c @@ -16,6 +16,8 @@ tag_init(const char *path, const char *fname) tag->fname = strdup(fname); ASSERT(tag->fname != NULL); + tag->new_fname = NULL; + tag->fpath = aprintf("%s/%s", path, fname); ASSERT(tag->fpath != NULL); diff --git a/src/tag.h b/src/tag.h @@ -7,6 +7,8 @@ struct tag { wchar_t *name; char *fname, *fpath; + char *new_fname; + struct link tracks; struct link link; diff --git a/src/track.c b/src/track.c @@ -7,10 +7,9 @@ struct track * -track_init(const char *dir, const char *fname) +track_init(const char *dir, const char *fname, int fid) { struct track *track; - struct stat info; int len; track = malloc(sizeof(struct track)); @@ -28,9 +27,7 @@ track_init(const char *dir, const char *fname) ASSERT(track->name != NULL); mbstowcs(track->name, track->fname, len + 1); - track->fid = -1; - if (!stat(track->fpath, &info)) - track->fid = info.st_ino; + track->fid = fid; track->tags = LIST_HEAD; diff --git a/src/track.h b/src/track.h @@ -11,5 +11,5 @@ struct track { int fid; }; -struct track *track_init(const char *dir, const char *file); +struct track *track_init(const char *dir, const char *file, int fid); void track_free(struct track *t);