From 950ccedd97411e9d3e975904e397e7a7582cb2b3 Mon Sep 17 00:00:00 2001 From: Zane Schaffer Date: Fri, 23 Dec 2022 19:44:07 -0800 Subject: [PATCH] Fix some memory leaks --- Makefile | 3 +-- README.md | 14 ++++++++++-- colors.c | 14 ++++++++---- config.c | 39 ++++++++++++++++++++++++++++++++ directory.c | 65 +++++++++++++++++++++++++++++++++++++++-------------- hotkeys.c | 20 ++++++++++++----- main.c | 57 +++++++++++++++++++++++----------------------- menu.c | 16 +++++++++---- ticker.c | 4 ++-- ui.c | 21 ++++++++--------- wax.h | 5 +++++ 11 files changed, 184 insertions(+), 74 deletions(-) create mode 100644 config.c diff --git a/Makefile b/Makefile index 82e228b..dec1dce 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,5 @@ BIN = wax -CFLAGS=-g -Wall $(shell pkg-config --cflags menu ncurses taglib_c) +CFLAGS=-g -Wall $(shell pkg-config --cflags menu ncurses taglib_c) LDFLAGS= $(shell pkg-config --libs menu ncurses taglib_c) PREFIX = /usr/local BINDIR = bin @@ -7,7 +7,6 @@ SOURCES = $(wildcard *.c) OBJECTS = $(SOURCES:.c=.o) all: $(BIN) - mkdir -p ~/.config/wax $(BIN): $(OBJECTS) $(CC) $^ $(CFLAGS) $(LDFLAGS) -o $@ diff --git a/README.md b/README.md index d7614b0..83ea5b4 100644 --- a/README.md +++ b/README.md @@ -17,11 +17,21 @@ sudo make install ## Usage Wax is configured through a plaintext file `config` located inside `$HOME/.config/wax/`. +This will get autogenerated for you with these defaults: ``` -music_dir = ~/Music +# Path to your music +music_dir = $HOME/Music + +# Background ANSI color +bg = 7 + +# Foreground ANSI color +fg = 0 + +# Highlight ANSI color +hl = 4 ``` -is the default. You can also pass in your music directory via the command line flag -d. diff --git a/colors.c b/colors.c index 45b55f7..2a209b6 100644 --- a/colors.c +++ b/colors.c @@ -1,10 +1,16 @@ +#include "curses.h" #include "wax.h" void setupColors() { start_color(); + assume_default_colors(config->fg, config->bg); + // main fg + bg + init_pair(1, config->fg, config->bg); + + // main highlight + init_pair(2, config->hl, config->bg); + + // reverse highlight + init_pair(3, config->bg, config->hl); - init_pair(0, COLOR_BLACK, COLOR_WHITE); - init_pair(1, COLOR_CYAN, COLOR_BLACK); - init_pair(2, COLOR_BLACK, COLOR_CYAN); - init_pair(3, COLOR_WHITE, COLOR_BLACK); } diff --git a/config.c b/config.c new file mode 100644 index 0000000..0ca55db --- /dev/null +++ b/config.c @@ -0,0 +1,39 @@ +#include "ncurses.h" +#include "wax.h" + +#include + +int parse_config(char *buf) { + + char dummy[256]; + char unformatted_dir[256]; + int temp_color; + if (sscanf(buf, " %s", dummy) == EOF) + return 0; // blank line + if (sscanf(buf, " %[#]", dummy) == 1) + return 0; // comment + if (sscanf(buf, " music_dir = %s", unformatted_dir) == 1) { + wordexp_t exp_result; + wordexp(unformatted_dir, &exp_result, 0); + strcpy(config->music_dir, exp_result.we_wordv[0]); + wordfree(&exp_result); + return 0; + } + if (sscanf(buf, " bg = %d", &temp_color) != 0) { + if (temp_color >= 0 && temp_color <= 15) { + config->bg = temp_color; + } + } + if (sscanf(buf, " fg = %d", &temp_color) != 0) { + if (temp_color >= 0 && temp_color <= 15) { + config->fg = temp_color; + } + } + if (sscanf(buf, " hl = %d", &temp_color) != 0) { + if (temp_color >= 0 && temp_color <= 15) { + config->hl = temp_color; + } + } + + return 3; +} diff --git a/directory.c b/directory.c index 02a999f..9d2a667 100644 --- a/directory.c +++ b/directory.c @@ -2,6 +2,7 @@ #include "wax.h" #include #include +#include #include #include #include @@ -13,28 +14,64 @@ size_t total_files = 0; library_t *Library; directory_t *current_directory_ = NULL; +void get_num_of_files(uint64_t *len, char *basePath) { + char *path = calloc(100, sizeof(char)); + struct dirent *d_ent; + DIR *dir = opendir(basePath); + + if (!dir) { + free(path); + return; + } + + while ((d_ent = readdir(dir)) != NULL) { + if (strcmp(d_ent->d_name, ".") != 0 && strcmp(d_ent->d_name, "..") != 0) { + + strcpy(path, basePath); + strcat(path, "/"); + strcat(path, d_ent->d_name); + + get_num_of_files(len, path); + + if (strstr(d_ent->d_name, ".mp3") || strstr(d_ent->d_name, ".wav") || + strstr(d_ent->d_name, ".flac")) { + *len += 1; + } + } + } + + free(path); + closedir(dir); +} + void get_files(int *len, char *basePath, char *target[]) { - char path[1000]; + char *path = calloc(100, sizeof(char)); struct dirent *d_ent; DIR *dir = opendir(basePath); - if (!dir) + if (!dir) { + free(path); return; + } while ((d_ent = readdir(dir)) != NULL) { if (strcmp(d_ent->d_name, ".") != 0 && strcmp(d_ent->d_name, "..") != 0) { - // fprintf(log_file, "%s\n", d_ent->d_name); strcpy(path, basePath); strcat(path, "/"); strcat(path, d_ent->d_name); get_files(len, path, target); - target[*len] = strdup(path); - *len += 1; + + if (strstr(d_ent->d_name, ".mp3") || strstr(d_ent->d_name, ".wav") || + strstr(d_ent->d_name, ".flac")) { + target[*len] = strdup(path); + *len += 1; + } } } + free(path); closedir(dir); } @@ -70,6 +107,7 @@ void populateArtistItems() { } currArtist = (char *)artist_items[0]->name.str; + free(ar); } void populateAlbumItems(char *artist) { if (NULL == Library->songs[0]) @@ -104,6 +142,7 @@ void populateAlbumItems(char *artist) { } currAlbum = (char *)album_items[0]->name.str; + free(al); } void swap(ITEM *xp, ITEM *yp) { @@ -157,18 +196,9 @@ int setupDir(char *dirPath) { Library = calloc(0, sizeof(*Library)); Library->num = 0; - current_directory_ = (directory_t *)malloc(sizeof(directory_t)); - - if (current_directory_ == NULL) { - printf("Error Occured.\n"); - exit(0); - } - - getcwd(current_directory_->cwd, sizeof(current_directory_->cwd)); - strcat(current_directory_->cwd, "/"); - strcat(current_directory_->cwd, dirPath); - - char *files[200]; + uint64_t file_num = 0; + get_num_of_files(&file_num, dirPath); + char *files[file_num]; get_files(&len, dirPath, files); Library->songs = calloc(len, sizeof(song *)); @@ -197,6 +227,7 @@ int setupDir(char *dirPath) { Library->num++; taglib_tag_free_strings(); taglib_file_free(file); + free(files[i]); } } diff --git a/hotkeys.c b/hotkeys.c index 462bfa1..056d096 100644 --- a/hotkeys.c +++ b/hotkeys.c @@ -2,15 +2,25 @@ typedef struct { char key; - char text[30]; + char *text; } Hotkey; +#define P_PAUSE "pause" +#define P_PLAY "play" + Hotkey hotkeys[6] = { {.key = '[', .text = "backward"}, {.key = ']', .text = "forward"}, {.key = ',', .text = "prev"}, {.key = '.', .text = "next"}, - {.key = 'p', .text = "play/pause"}, {.key = 'q', .text = "quit"}}; + {.key = 'p', .text = "play"}, {.key = 'q', .text = "quit"}}; void drawHotkeyBar() { + + if (isPlaying()) { + hotkeys[4].text = P_PAUSE; + } else { + hotkeys[4].text = P_PLAY; + } + int colBuffer = COLS / 10; int i; if ((LINES < 10) || (COLS < 75)) { @@ -18,12 +28,12 @@ void drawHotkeyBar() { } for (i = 0; i < 6; i++) { - wattron(stdscr, COLOR_PAIR(2)); + wattron(stdscr, COLOR_PAIR(3)); mvprintw(LINES - 2, ((((COLS - colBuffer) / 6)) * i) + colBuffer, " %c ", hotkeys[i].key); - wattron(stdscr, COLOR_PAIR(1)); + wattron(stdscr, COLOR_PAIR(2)); mvprintw(LINES - 2, ((((COLS - colBuffer) / 6)) * i) + colBuffer + 3, " %s ", hotkeys[i].text); - wattroff(stdscr, COLOR_PAIR(1)); + wattroff(stdscr, COLOR_PAIR(2)); } } diff --git a/main.c b/main.c index c85dd08..c5a3aeb 100644 --- a/main.c +++ b/main.c @@ -1,5 +1,6 @@ #include "wax.h" #include +#include #include #include #include @@ -7,45 +8,42 @@ FILE *log_file; CONFIG *config; -int parse_config(char *buf) { - char dummy[256]; - char unformatted_dir[256]; - if (sscanf(buf, " %s", dummy) == EOF) - return 0; // blank line - if (sscanf(buf, " %[#]", dummy) == 1) - return 0; // comment - if (sscanf(buf, " music_dir = %s", unformatted_dir) == 1) { - wordexp_t exp_result; - wordexp(unformatted_dir, &exp_result, 0); - strcpy(config->music_dir, exp_result.we_wordv[0]); - wordfree(&exp_result); - return 0; - } - return 3; -} - int main(int argc, char **argv) { - config = calloc(0, sizeof(CONFIG)); - FILE *c_f; + config = &(CONFIG){.bg = COLOR_BLACK, .fg = COLOR_WHITE, .hl = COLOR_CYAN}; bool dirflag; const char *home = getenv("HOME"); - char *configpath = calloc(0, 250 * sizeof(char)); + char *configfolder; + char *configfile; char *logfilepath = calloc(0, 250 * sizeof(char)); - strlcpy(configpath, home, 250); - strlcat(configpath, "/.config/wax", 250); - strlcat(configpath, "/config", 250); + asprintf(&configfolder, "%s/.config/wax", home); + asprintf(&configfile, "%s/config", configfolder); + + mkdir(configfolder, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH); - if (access(configpath, F_OK) == 0) { - c_f = fopen(configpath, "r"); - } else { - c_f = fopen(configpath, "w"); - fprintf(c_f, "music_dir = %s/Music\n", home); + if (access(configfile, F_OK) != 0) { + FILE *newConfig = fopen(configfile, "w"); + fprintf(newConfig, + "# Path to your music\n" + "music_dir = %s/Music\n\n" + "# Background ANSI color\n" + "bg = 7\n\n" + "# Foreground ANSI color\n" + "fg = 0\n\n" + "# Highlight ANSI color\n" + "hl = 4", + home); + fclose(newConfig); } + FILE *c_f = fopen(configfile, "r"); + free(configfile); + free(configfolder); + strlcpy(logfilepath, home, 250); strlcat(logfilepath, "/.config/wax", 250); strlcat(logfilepath, "/wax.log", 250); + log_file = fopen(logfilepath, "a+"); int opt; while ((opt = getopt(argc, argv, "cdh")) != -1) { @@ -60,9 +58,12 @@ int main(int argc, char **argv) { } char buf[256]; + while (fgets(buf, sizeof buf, c_f)) { parse_config(buf); } + + fclose(c_f); fprintf(log_file, "config: %s\n", config->music_dir); if (dirflag) { diff --git a/menu.c b/menu.c index 9feb2c5..2c8ed14 100644 --- a/menu.c +++ b/menu.c @@ -4,10 +4,18 @@ char title[200]; char *getCurrTitle(short index) { memset(&title[0], 0, sizeof(title)); - strcpy(title, "Now Playing: "); - strcat(title, currArtist); - strcat(title, " - "); - strcat(title, song_items[index]->description.str); + if (isPlaying()) { + strcpy(title, "Now Playing: "); + strcat(title, currArtist); + strcat(title, " - "); + strcat(title, song_items[index]->description.str); + } else { + + strcpy(title, "Paused: "); + strcat(title, currArtist); + strcat(title, " - "); + strcat(title, song_items[index]->description.str); + } return title; } diff --git a/ticker.c b/ticker.c index d56da1f..fbfb223 100644 --- a/ticker.c +++ b/ticker.c @@ -25,9 +25,9 @@ void drawTicker() { TICKER_END = COLS - (TICKER_START * 2) - 1; TICKER_LENGTH = TICKER_END - TICKER_START; - wattron(stdscr, COLOR_PAIR(1)); + wattron(stdscr, COLOR_PAIR(2)); mvhline(TICKER_HEIGHT, TICKER_START, ACS_VLINE, TICKER_END); - wattroff(stdscr, COLOR_PAIR(1)); + wattroff(stdscr, COLOR_PAIR(2)); mvhline(TICKER_HEIGHT, TICKER_START, ACS_CKBOARD, PLAYED_LENGTH()); refresh(); } diff --git a/ui.c b/ui.c index 27d3231..62f4782 100644 --- a/ui.c +++ b/ui.c @@ -121,10 +121,10 @@ void setupWindows() { void drawDefaultTitle() { if (LINES < HEIGHT_CUTOFF) { printMiddle(window, TITLE_HEIGHT_SHORT, (COLS / 2) - (strlen(title) / 2), - strlen(title), title, COLOR_PAIR(1)); + strlen(title), title, COLOR_PAIR(2)); } else { printMiddle(window, TITLE_HEIGHT, (COLS / 2) - (strlen(title) / 2), - strlen(title), title, COLOR_PAIR(1)); + strlen(title), title, COLOR_PAIR(2)); } } @@ -136,11 +136,11 @@ void drawWindow() { if (LINES < HEIGHT_CUTOFF) { printTime(window, TIME_HEIGHT_SHORT, COLS - 8, convertToMins(SONG_DUR), - COLOR_PAIR(1)); + COLOR_PAIR(2)); } else { printTime(window, TIME_HEIGHT, COLS - 8, convertToMins(SONG_DUR), - COLOR_PAIR(1)); + COLOR_PAIR(2)); } refresh(); drawHotkeyBar(); @@ -175,11 +175,11 @@ void drawMenus() { void printSongDuration() { if (LINES < HEIGHT_CUTOFF) { printTime(NULL, TIME_HEIGHT_SHORT, COLS - 8, convertToMins(SONG_DUR), - COLOR_PAIR(1)); + COLOR_PAIR(2)); } else { printTime(NULL, TIME_HEIGHT, COLS - 8, convertToMins(SONG_DUR), - COLOR_PAIR(1)); + COLOR_PAIR(2)); } } @@ -187,10 +187,10 @@ void printCurrTime() { if (LINES < HEIGHT_CUTOFF) { printTime(NULL, TIME_HEIGHT_SHORT, 2, convertToMins(SONG_CURRTIME), - COLOR_PAIR(1)); + COLOR_PAIR(2)); } else { printTime(NULL, TIME_HEIGHT, 2, convertToMins(SONG_CURRTIME), - COLOR_PAIR(1)); + COLOR_PAIR(2)); } } @@ -214,13 +214,13 @@ void redrawTitle() { (COLS / 2) - (strlen(getCurrTitle(song_menu->curitem->index)) / 2), strlen(getCurrTitle(song_menu->curitem->index)), - getCurrTitle(song_menu->curitem->index), COLOR_PAIR(1)); + getCurrTitle(song_menu->curitem->index), COLOR_PAIR(2)); } else { printMiddle(NULL, TITLE_HEIGHT, (COLS / 2) - (strlen(getCurrTitle(song_menu->curitem->index)) / 2), strlen(getCurrTitle(song_menu->curitem->index)), - getCurrTitle(song_menu->curitem->index), COLOR_PAIR(1)); + getCurrTitle(song_menu->curitem->index), COLOR_PAIR(2)); } printSongDuration(); refresh(); @@ -258,6 +258,7 @@ int setupUI() { printCurrTime(); drawTicker(); handleInput(ch); + drawHotkeyBar(); } cleanupUI(); diff --git a/wax.h b/wax.h index 34a755a..f4ca9fa 100644 --- a/wax.h +++ b/wax.h @@ -1,6 +1,7 @@ #ifndef WAX_H #define WAX_H +#include "ncurses.h" #include #include #include @@ -20,8 +21,12 @@ typedef struct { typedef struct { char music_dir[256]; + int bg; + int fg; + int hl; } CONFIG; +int parse_config(char *buf); extern CONFIG *config; Time convertToMins(int sec);