tmus

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

commit 44070fb5518e2b54748d58da5a5c61f476ef8700
parent 964d816726b8c765277b043894d534e9b1e0d60c
Author: Louis Burda <quent.burda@gmail.com>
Date:   Thu, 12 Jan 2023 00:01:27 +0100

Only write to index files on data_save where necessary

Diffstat:
Msrc/data.c | 35++++++++++++++++++++---------------
Msrc/data.h | 11++++++-----
Msrc/mpris.c | 1+
Msrc/tui.c | 36++++++++++++++++++------------------
4 files changed, 45 insertions(+), 38 deletions(-)

diff --git a/src/data.c b/src/data.c @@ -19,6 +19,8 @@ struct list tracks; /* struct track (link) */ struct list tags; /* struct track (link) */ struct list tags_sel; /* struct tag (link_sel) */ +bool playlist_outdated; + static int get_fid(const char *path); static struct tag *tag_alloc(const char *path, const char *fname); @@ -48,10 +50,9 @@ tag_alloc(const char *path, const char *fname) tag->fpath = aprintf("%s/%s", path, fname); tag->name = astrdup(fname); - + tag->index_dirty = false; tag->link = LINK_EMPTY; tag->link_sel = LINK_EMPTY; - list_init(&tag->tracks); return tag; @@ -76,9 +77,7 @@ track_alloc(const char *dir, const char *fname) track->fpath = aprintf("%s/%s", dir, fname); track->name = astrdup(fname); - track->tag = NULL; - track->link = LINK_EMPTY; track->link_pl = LINK_EMPTY; track->link_tt = LINK_EMPTY; @@ -115,7 +114,6 @@ tracks_load(struct tag *tag) FILE *file; index_path = aprintf("%s/index", tag->fpath); - file = fopen(index_path, "r"); if (file == NULL) { index_update(tag); /* create index */ @@ -130,6 +128,8 @@ tracks_load(struct tag *tag) track_add(tag, linebuf); } + tag->index_dirty = false; + fclose(file); free(index_path); } @@ -339,6 +339,8 @@ tracks_update(struct tag *tag) track_add(tag, ent->d_name); } + tag->index_dirty = false; + closedir(dir); return true; @@ -362,20 +364,14 @@ playlist_clear(void) } void -playlist_update(bool exec) +playlist_update(void) { - static bool update = false; struct link *link, *link2; struct track *track; struct tag *tag; - if (!exec) { - update = true; + if (!playlist_outdated) return; - } - - if (!update) return; - update = false; playlist_clear(); @@ -387,6 +383,8 @@ playlist_update(bool exec) list_push_back(&player.playlist, &track->link_pl); } } + + playlist_outdated = false; } struct tag * @@ -490,7 +488,9 @@ track_add(struct tag *tag, const char *fname) /* if track's tag is selected, update playlist */ if (link_inuse(&tag->link_sel)) - playlist_update(false); + playlist_outdated = true; + + tag->index_dirty = true; return track; } @@ -548,6 +548,8 @@ track_rename(struct track *track, const char *name) free(track->name); track->name = astrdup(name); + track->tag->index_dirty = true; + return true; } @@ -644,6 +646,8 @@ data_load(void) free(path); } + playlist_outdated = true; + closedir(dir); } @@ -655,7 +659,8 @@ data_save(void) for (LIST_ITER(&tags, link)) { tag = UPCAST(link, struct tag, link); - tracks_save(tag); + if (tag->index_dirty) + tracks_save(tag); } release_lock(datadir); diff --git a/src/data.h b/src/data.h @@ -7,7 +7,7 @@ struct tag { char *name, *fpath; struct list tracks; - bool dirty; + bool index_dirty; struct link link; /* tags list */ struct link link_sel; /* selected tags list */ @@ -39,7 +39,7 @@ bool tracks_update(struct tag *tag); struct track *tracks_vis_track(struct link *link); void playlist_clear(void); -void playlist_update(bool exec); +void playlist_update(void); struct tag *tag_add(const char *fname); struct tag *tag_find(const char *name); @@ -59,7 +59,8 @@ void data_free(void); extern const char *datadir; -extern struct list tracks; /* struct ref */ -extern struct list tags; /* struct tag */ -extern struct list tags_sel; /* struct ref */ +extern struct list tracks; /* struct track (link) */ +extern struct list tags; /* struct track (link) */ +extern struct list tags_sel; /* struct tag (link_sel) */ +extern bool playlist_outdated; diff --git a/src/mpris.c b/src/mpris.c @@ -3,6 +3,7 @@ #include "player.h" #include "util.h" +#include <err.h> #include <stdbool.h> static void dbus_handle_getall(DBusMessage *msg); diff --git a/src/tui.c b/src/tui.c @@ -82,6 +82,20 @@ static void main_vis(void); static void tui_curses_init(void); static void tui_resize(void); +static const char imode_prefix[IMODE_COUNT] = { + [IMODE_EXECUTE] = ':', + [IMODE_TRACK_PLAY] = '!', + [IMODE_TRACK_SELECT] = '/', + [IMODE_TRACK_VIS_SELECT] = '~', + [IMODE_TAG_SELECT] = '?', +}; + +static const char player_state_chars[] = { + [PLAYER_STATE_PAUSED] = '|', + [PLAYER_STATE_PLAYING] = '>', + [PLAYER_STATE_STOPPED] = '#' +}; + static int scrw, scrh; static int quit; @@ -117,20 +131,6 @@ struct listnav track_nav; char *cmd_status; int cmd_status_uptime; -const char imode_prefix[IMODE_COUNT] = { - [IMODE_EXECUTE] = ':', - [IMODE_TRACK_PLAY] = '!', - [IMODE_TRACK_SELECT] = '/', - [IMODE_TRACK_VIS_SELECT] = '~', - [IMODE_TAG_SELECT] = '?', -}; - -static const char player_state_chars[] = { - [PLAYER_STATE_PAUSED] = '|', - [PLAYER_STATE_PLAYING] = '>', - [PLAYER_STATE_STOPPED] = '#' -}; - void pane_title(struct pane *pane, bool highlight, const char *fmtstr, ...) { @@ -390,11 +390,11 @@ tag_pane_input(wint_t c) break; case KEY_SPACE: /* toggle tag */ toggle_current_tag(); - playlist_update(false); + playlist_outdated = true; break; case KEY_ENTER: /* select only current tag */ select_only_current_tag(); - playlist_update(false); + playlist_outdated = true; break; case KEY_PPAGE: /* seek half a page up */ listnav_update_sel(&tag_nav, tag_nav.sel - tag_nav.wlen / 2); @@ -416,7 +416,7 @@ tag_pane_input(wint_t c) break; case L'D': /* delete tag */ delete_selected_tag(); - playlist_update(false); + playlist_outdated = true; break; default: return false; @@ -1273,7 +1273,7 @@ tui_update(void) if (!handled) main_input(c); } - playlist_update(true); + playlist_update(); update_tracks_vis(); refresh();