138 lines
3.2 KiB
C
138 lines
3.2 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"
|
|
#include "curseshelper.h"
|
|
|
|
int main(int argc, char *argv[]) {
|
|
const char *short_options = "hsi:lv";
|
|
struct option long_options[] = {
|
|
{"server", no_argument, NULL, 's'},
|
|
{"ip", required_argument, NULL, 'i'},
|
|
{"help", no_argument, NULL, 'h'},
|
|
{"local", no_argument, NULL, 'l'},
|
|
{"verbose", no_argument, NULL, 'v'}
|
|
};
|
|
int c;
|
|
|
|
//initialize libconfig
|
|
if (!cfginit()) {
|
|
printf("Failed to initialize csoundbox.cfg. Does it exist?\n");
|
|
return 0;
|
|
}
|
|
ao_initialize();
|
|
settings csset = {0};
|
|
csset.verbose = 0; //Off by default
|
|
|
|
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(&csset);
|
|
break;
|
|
case 'i':
|
|
//connect to a csoundbox server
|
|
inputNetwork(optarg);
|
|
break;
|
|
case 'v':
|
|
//set a verbose flag
|
|
csset.verbose = 1;
|
|
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);
|
|
printCenter(msg);
|
|
printCenterOffset(msg2, 2);
|
|
|
|
}
|
|
|
|
void showHelp(settings *s) {
|
|
if (s->verbose) {
|
|
printf("Test\n");
|
|
}
|
|
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");
|
|
|
|
}
|
|
|