-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
199 lines (171 loc) · 4.55 KB
/
main.c
File metadata and controls
199 lines (171 loc) · 4.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#include <SDL2/SDL.h>
#include "SDL2/SDL_mixer.h"
#include <ncurses.h>
#include <dirent.h>
#include <time.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
char random_file[255];
WINDOW *window;
Mix_Music *music;
int NUM_OF_FILES;
void init_sdl() {
if (SDL_Init(SDL_INIT_AUDIO) < 0) {
printf("Failed to init SDL\n");
exit(1);
}
Mix_OpenAudio(22050, AUDIO_S16SYS, 2, 640);
}
void play(char *file){
music = Mix_LoadMUS(file);
Mix_PlayMusic(music, 1);
}
WINDOW *init_window(){
WINDOW *window = initscr();
curs_set(0);
cbreak();
noecho();
return window;
}
void refresh_screen(){
erase();
box(window, ACS_VLINE, ACS_HLINE);
move(0,2);
addstr("mini-mp3-player");
move(window->_maxy, window->_maxx - 33);
addstr("Press n to jump to the next song");
}
void display_title(char *title){
move(window->_maxy / 2, (window->_maxx / 2) - (strlen(title) / 2));
addstr(title);
}
void clear_conf_file(){
FILE *file;
file = fopen("/usr/local/etc/mini-mp3-player/mini-mp3-player.conf", "w");
fclose(file);
}
void append_to_conf_file(char *line){
FILE *file;
file = fopen("/usr/local/etc/mini-mp3-player/mini-mp3-player.conf", "a");
fprintf(file, "%s\n", line);
fclose(file);
}
// readdir also reads ".." and "." as directories, if this was not filtered, the recursion would be infinite
int is_valid_dir(char *dir_name){
return strcmp(dir_name,"..") != 0 && strcmp(dir_name,".") != 0;
}
char *get_file_extension(char *path){
char *ext = strrchr(path, '.');
if (!ext) {
return "";
} else {
return ext + 1;
}
}
// Recursive file reading from folder
void list_dir(char *path) {
DIR *d;
struct dirent *dir;
d = opendir(path);
if (d) {
while ((dir = readdir(d)) != NULL) {
char *join = "/";
char *buff = malloc(strlen(path) + strlen(join) + strlen(dir->d_name) + 1);
strcpy(buff,path);
strcat(buff,join);
strcat(buff,dir->d_name);
if (dir->d_type == DT_DIR && is_valid_dir(dir->d_name)){
list_dir(buff);
}else{
if(is_valid_dir(dir->d_name) && strcmp(get_file_extension(buff),"mp3") == 0)
append_to_conf_file(buff);
}
free(buff);
}
closedir(d);
}
return;
}
void read_file_line(char *line,char *path,int num){
FILE *file;
file = fopen(path, "r");
int i = 0;
while (fgets(line,255, file) && i <= num){
i++;
}
fclose(file);
}
void get_random_file( char *buff){
read_file_line(buff,"/usr/local/etc/mini-mp3-player/mini-mp3-player.conf",rand() % NUM_OF_FILES);
//Remove new line and white space in the end of the file names
buff[strlen(buff)-1] = '\0';
}
void seed_randomness(){
srand(time(NULL));
}
void play_next(){
Mix_FreeMusic(music);
refresh_screen();
get_random_file(random_file);
play(random_file);
display_title(random_file);
}
char *get_argument(char *argv[],int argc, char *flag){
for(int i = 0; i < argc; i++){
if(strcmp(argv[i],flag) == 0){
return argv[i+1];
}
}
return NULL;
}
int count_lines_in_file(char *path){
FILE *file = fopen(path,"r");
int lines = 0;
while(!feof(file)){
char ch = fgetc(file);
if(ch == '\n'){
lines++;
}
}
return lines;
}
void check_conf_file(){
if(!access("/usr/local/etc/mini-mp3-player/mini-mp3-player.conf", F_OK) == 0) {
fprintf( stderr, "ERROR: Collection file does not exist in /usr/local/etc/mini-mp3-player. Run mini-mp3-player with flag -c and the collection path to generate the file\n");
exit(0);
}
}
int main(int argc, char *argv[]) {
if(get_argument(argv,argc,"-c") != NULL){
clear_conf_file();
list_dir(get_argument(argv,argc,"-c"));
}
check_conf_file();
NUM_OF_FILES = count_lines_in_file("/usr/local/etc/mini-mp3-player/mini-mp3-player.conf");
init_sdl();
window = init_window();
seed_randomness();
get_random_file(random_file);
play(random_file);
refresh_screen();
display_title(random_file);
while (!SDL_QuitRequested()) {
SDL_Delay(250);
char c = getch();
timeout(1000);
if(c == 'q' || c == 'Q'){
break;
}
if(c == 'n' || c == 'N'){
play_next();
}
if(Mix_PlayingMusic() == 0){
play_next();
}
}
Mix_FreeMusic(music);
SDL_Quit();
endwin();
return 0;
}