-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
161 lines (147 loc) · 4.05 KB
/
main.c
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
// by mothcompute
// too lazy to include the unlicense but just pretend i did. thats the license
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <switch.h>
#include <unistd.h>
#include <dirent.h>
#include <SDL2/SDL.h>
#include <SDL2/SDL_mixer.h>
#include <xmp.h>
int playing = 0, files = HidNpadButton_Minus, p = 0;
void pmod(void *udata, uint8_t *stream, int len) {
if(playing && p) playing = xmp_play_buffer((xmp_context)udata, stream, len, 0) >= 0;
else memset(stream, 0, len);
}
int main(int argc, char** argv) {
consoleInit(NULL);
SDL_Init(SDL_INIT_AUDIO);
padConfigureInput(1, HidNpadStyleSet_NpadStandard);
PadState pad;
padInitializeDefault(&pad);
consoleUpdate(NULL);
xmp_context c = xmp_create_context();
struct xmp_module_info mi;
struct xmp_frame_info fi;
SDL_AudioSpec as;
as.freq = 48000;
as.format = AUDIO_S16;
as.channels = 2;
as.samples = 480;
as.callback = pmod;
as.userdata = c;
if(SDL_OpenAudio(&as, NULL)) return 1;
// calculated the number 50 from the avg. number of files in each directory on my computer (51)
// rounded down. subsequent resizes only add 16 to avoid reallocating too much memory
DIR* d;
struct dirent* dir = malloc(50*sizeof(struct dirent)), *tmpdir;
long num_dir = 0, sz_dir = 50, frame = 0, pos = 0;
chdir("/");
while (appletMainLoop()) {
printf("\e[1;1H");
padUpdate(&pad);
u64 kDown = padGetButtonsDown(&pad);
if (kDown & HidNpadButton_Plus) break;
int mchg = kDown & HidNpadButton_Minus;
files ^= mchg;
if(mchg) {
printf("\e[2J");
if(files) {
num_dir = 0;
d = opendir(".");
if(d) {
while((tmpdir = readdir(d)) != NULL) {
dir[num_dir++] = *tmpdir;
if(num_dir >= sz_dir-1) dir = realloc(dir, (sz_dir += 16)*sizeof(struct dirent));
}
closedir(d);
}
}
}
if(files) {
if((kDown & HidNpadButton_Down)) {
if(frame*44 + pos < num_dir - 1) pos++;
if(pos == 44) {
printf("\e[2J");
frame++;
pos = 0;
}
}
if((kDown & HidNpadButton_Up)) {
if(frame || pos > 0) pos--;
if(pos == -1) {
printf("\e[2J");
if(frame) frame--;
pos = 43;
}
}
if(kDown & HidNpadButton_B) {
printf("\e[2J");
num_dir = 0;
chdir("..");
d = opendir(".");
if(d) {
while((tmpdir = readdir(d)) != NULL) {
dir[num_dir++] = *tmpdir;
if(num_dir >= sz_dir-1) dir = realloc(dir, (sz_dir += 16)*sizeof(struct dirent));
}
closedir(d);
pos = 0;
frame = 0;
}
}
if(kDown & HidNpadButton_A) {
struct stat s;
stat(dir[pos + frame*44].d_name, &s);
printf("\e[2J");
if(S_ISDIR(s.st_mode)) {
num_dir = 0;
chdir(dir[pos + frame*44].d_name);
d = opendir(".");
if(d) {
while ((tmpdir = readdir(d)) != NULL) {
dir[num_dir++] = *tmpdir;
if(num_dir >= sz_dir-1) dir = realloc(dir, (sz_dir += 16)*sizeof(struct dirent));
}
closedir(d);
pos = 0;
frame = 0;
}
} else if(S_ISREG(s.st_mode)) {
struct xmp_test_info ti;
if(!xmp_test_module(dir[pos + frame*44].d_name, &ti)) {
if(playing) {
xmp_end_player(c);
xmp_release_module(c);
}
xmp_load_module(c, dir[pos + frame*44].d_name);
xmp_get_module_info(c, &mi);
if((playing = !xmp_start_player(c, 48000, 0))) SDL_PauseAudio(0);
p = HidNpadButton_A;
}
}
}
long s = num_dir-(44*frame) >= 44 ? 44*(frame+1) : num_dir;
for(long i = 44*frame; i < s; i++) {
//printf("\e[%im%li\n", i % 44 == pos, s);
//printf("\e[%im%.79s\n", i % 44 == pos, dir[i + frame*44].d_name);
printf("\e[%im%.79s\n", i % 44 == pos, dir[i].d_name);
//printf("\e[%im%li/%li %li %li\n", i % 44 == pos, i, num_dir-1, frame, pos);
}
} else {
if(playing) {
xmp_get_frame_info(c, &fi);
printf("%s\n%02X.%02X %02X/%02X\n%li %li %li %li", mi.mod->name, fi.pos, mi.mod->len-1, fi.row, fi.num_rows, sz_dir, num_dir, frame, pos);
}
p ^= kDown & HidNpadButton_A;
}
SDL_Delay(5);
consoleUpdate(NULL);
}
SDL_CloseAudio();
SDL_Quit();
consoleExit(NULL);
return 0;
}