alsa-visual/spark_ncurses.c

110 lines
1.5 KiB
C
Raw Normal View History

2024-03-24 06:37:10 +00:00
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <ncurses.h>
#include <sys/ioctl.h>
#include <ctype.h>
2024-03-30 14:11:03 +00:00
#define NARGS 1024
2024-03-24 06:37:10 +00:00
#define ARG_SIZE 50
int args;
2024-03-30 14:11:03 +00:00
int values[NARGS + 1];
2024-03-24 06:37:10 +00:00
struct winsize ws;
int add_value(const char *str) {
2024-03-30 14:11:03 +00:00
if (args >= sizeof(values) / sizeof(int) || args > ws.ws_col)
2024-03-24 06:37:10 +00:00
return 1;
2024-03-30 14:11:03 +00:00
values[args] = atoi(str);
2024-03-24 06:37:10 +00:00
args++;
return 0;
}
void stdin_read(void) {
char arg[ARG_SIZE + 1];
char *p = arg;
while (1) {
int c = getchar();
if (c == EOF)
return;
else if (c == ' ' && p != arg) {
*p = '\0';
p = arg;
if (add_value(arg))
break;
}
else {
*p = c;
if (p + 1 == arg + sizeof(arg))
break;
p++;
}
}
}
void sig_handler(int sig) {
(void)sig;
endwin();
exit(0);
}
2024-06-14 08:18:31 +00:00
void print(int middle, int k, int x) {
if (values[x] < 0)
mvprintw(middle + k, x, "@");
2024-04-01 17:46:47 +00:00
else
2024-06-14 08:18:31 +00:00
mvprintw(middle - k, x, "@");
2024-04-01 17:46:47 +00:00
}
2024-03-24 06:37:10 +00:00
int main(void) {
signal(SIGINT, sig_handler);
2024-03-30 14:11:03 +00:00
signal(SIGTERM, sig_handler);
2024-03-24 06:37:10 +00:00
if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) < 0)
return 1;
initscr();
2024-03-30 14:11:03 +00:00
noecho();
2024-03-24 06:37:10 +00:00
curs_set(0);
timeout(0);
2024-03-30 14:11:03 +00:00
int min = 0;
int max = 0;
2024-03-24 06:37:10 +00:00
2024-06-14 08:18:31 +00:00
int middle = (int)ws.ws_row / 2;
2024-03-24 06:37:10 +00:00
while (1) {
args = 0;
stdin_read();
clear();
2024-03-30 14:11:03 +00:00
for (int i = 0; i < args; i++) {
2024-04-01 17:46:47 +00:00
int val = values[i];
if (val < 0)
val *= -1;
2024-03-30 14:11:03 +00:00
2024-04-01 17:46:47 +00:00
if (val < min)
min = val;
2024-03-30 14:11:03 +00:00
2024-04-01 17:46:47 +00:00
else if (val > max)
max = val;
2024-06-14 08:18:31 +00:00
/* Main */
int lvl = (val - min) * (middle - 5) / ((max - min) + 1);
print(middle, lvl, i);
2024-03-30 14:11:03 +00:00
}
2024-03-24 06:37:10 +00:00
refresh();
}
}