Files
csoundbox/csoundbox.c

124 lines
2.8 KiB
C

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ncurses.h>
#include <getopt.h>
#include <ao/ao.h> //needed to release on close (ao_shutdown)
#include "csoundbox.h"
#include "config.h"
#include "sound.h"
#include "udpserver.h"
int main(int argc, char *argv[]) {
const char *short_options = "hsi:l";
struct option long_options[] = {
{"server", no_argument, NULL, 's'},
{"ip", required_argument, NULL, 'i'},
{"help", no_argument, NULL, 'h'},
{"local", no_argument, NULL, 'l'}
};
int c;
//initialize libconfig
if (!cfginit()) {
printf("Failed to initialize csoundbox.cfg. Does it exist?\n");
return 0;
}
ao_initialize();
while ( (c = getopt_long(argc, argv, short_options, long_options, NULL)) != -1 ) {
switch (c) {
case 's':
//start server
printf("Server.\n");
udpserver();
break;
case 'h':
//show the help page
showHelp();
break;
case 'i':
//connect to a csoundbox server
inputNetwork(optarg);
break;
case 'l':
default:
//start local
inputLocal();
break;
}
}
if (argc < 2) {
inputLocal();
}
cfgdestroy();
ao_shutdown();
return 0;
}
void inputLocal(void) {
initscr();
nonl(); //no newline
noecho();
raw(); //raw input (suppress ctrl + c)
keypad(stdscr, TRUE); //Disable the F1-12 keypad
curs_set(0); //Disable the cursor
printCursesWelcome();
int input;
//Key 13 --> ENTER
//Key 265 --> F1
while ((input = getch()) != 13 && input != 265) {
playSound(lookupSounds(input));
}
endwin();
}
void inputNetwork(char *server) {
initscr();
nonl(); //no newline
noecho();
raw(); //raw input (suppress ctrl + c)
keypad(stdscr, TRUE); //Disable the F1-12 keypad
curs_set(0); //Disable the cursor
printCursesWelcome();
char input[2];
int inp;
//Key 13 --> ENTER
//Key 265 --> F1
while ((input[0] = inp = getch()) != 13 && inp != 265) {
input[1] = '\0';
sendKeyUDP(server, input);
}
if (inp == 265) {
sendKeyUDP(server, "exit");
}
endwin();
}
void printCursesWelcome(void) {
int y, x;
getmaxyx(stdscr, y, x);
//center
y /= 2;
x /= 2;
const char *msg = "Press enter to exit.";
const char *msg2 = "Press F1 to kill the server + client";
mvprintw(y, x - (strlen(msg) / 2), msg);
mvprintw(y + 1, x - (strlen(msg2) / 2), msg2);
}
void showHelp(void) {
printf("-h: Show the help screen and exit\n");
printf("-s: Start the server\n");
printf("-i <ip>: connect to a server\n");
printf("-l or without arguments: Start local\n");
}