Skip to content

Commit

Permalink
Fix some memory leaks
Browse files Browse the repository at this point in the history
  • Loading branch information
Zane Schaffer committed Dec 24, 2022
1 parent 800f49a commit 950cced
Show file tree
Hide file tree
Showing 11 changed files with 184 additions and 74 deletions.
3 changes: 1 addition & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
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
SOURCES = $(wildcard *.c)
OBJECTS = $(SOURCES:.c=.o)

all: $(BIN)
mkdir -p ~/.config/wax

$(BIN): $(OBJECTS)
$(CC) $^ $(CFLAGS) $(LDFLAGS) -o $@
Expand Down
14 changes: 12 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
14 changes: 10 additions & 4 deletions colors.c
Original file line number Diff line number Diff line change
@@ -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);
}
39 changes: 39 additions & 0 deletions config.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#include "ncurses.h"
#include "wax.h"

#include <wordexp.h>

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;
}
65 changes: 48 additions & 17 deletions directory.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include "wax.h"
#include <dirent.h>
#include <libgen.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
Expand All @@ -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);
}

Expand Down Expand Up @@ -70,6 +107,7 @@ void populateArtistItems() {
}

currArtist = (char *)artist_items[0]->name.str;
free(ar);
}
void populateAlbumItems(char *artist) {
if (NULL == Library->songs[0])
Expand Down Expand Up @@ -104,6 +142,7 @@ void populateAlbumItems(char *artist) {
}

currAlbum = (char *)album_items[0]->name.str;
free(al);
}

void swap(ITEM *xp, ITEM *yp) {
Expand Down Expand Up @@ -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 *));

Expand Down Expand Up @@ -197,6 +227,7 @@ int setupDir(char *dirPath) {
Library->num++;
taglib_tag_free_strings();
taglib_file_free(file);
free(files[i]);
}
}

Expand Down
20 changes: 15 additions & 5 deletions hotkeys.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,38 @@

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)) {
return;
}

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));
}
}
57 changes: 29 additions & 28 deletions main.c
Original file line number Diff line number Diff line change
@@ -1,51 +1,49 @@
#include "wax.h"
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <wordexp.h>
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) {
Expand All @@ -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) {
Expand Down
16 changes: 12 additions & 4 deletions menu.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
4 changes: 2 additions & 2 deletions ticker.c
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Loading

0 comments on commit 950cced

Please sign in to comment.