diff --git a/tui/main.cpp b/tui/main.cpp new file mode 100644 index 0000000..4021b5e --- /dev/null +++ b/tui/main.cpp @@ -0,0 +1,214 @@ +#include +#include +#include +#include +#include +#include +#include + +void red(WINDOW *window) { + wattron(window, COLOR_PAIR(1)); +} + +void cyan(WINDOW *window) { + wattron(window, COLOR_PAIR(5)); +} + +void green(WINDOW *window) { + wattron(window, COLOR_PAIR(2)); +} + +void magenta(WINDOW *window) { + wattron(window, COLOR_PAIR(6)); +} + +void interfaceMenu(WINDOW *window); +void interfaceSelect(WINDOW *window, QString interface); + +void mainmenu(WINDOW *window) { + wclear(window); + + cyan(window); + box(window, 0 , 0); + + red(window); + mvwaddstr(window, 1, 2, "System Configuration:"); + + green(window); + mvwaddstr(window, 3, 2, "1. Interfaces"); + + mvwaddstr(window, 4, 2, "2. ARP"); + + mvwaddstr(window, 5, 2, "3. Wireless"); + + cyan(window); + mvwaddstr(window, 7, 2, "0. Quit"); + + magenta(window); + mvwaddstr(window, 9, 2, "Enter selection: "); + + wrefresh(window); + + char c = getch(); + + if(c < '0' || c > '3') { + mainmenu(window); + }else{ + if(c == '1') { + interfaceMenu(window); + } + } +} + +void interfaceMenu(WINDOW *window) { + wclear(window); + + cyan(window); + box(window, 0 , 0); + + red(window); + mvwaddstr(window, 1, 2, "Interfaces:"); + + green(window); + + QStringList interfaceList = Interface::list(); + + int count = 1; + int y = 3; + int lastInterface = 0; + QHash interfaceHash; + + foreach(QString interface, interfaceList) { + QString orig = interface; + interface = QString::number(count) + ". " + interface; + QByteArray interfaceArray = interface.toLatin1(); + const char *interfaceStr = interfaceArray.constData(); + + mvwaddstr(window, y, 2, interfaceStr); + + lastInterface = count + '0'; + + interfaceHash.insert(lastInterface, orig); + + ++y; + ++count; + } + + cyan(window); + mvwaddstr(window, y + 1, 2, "0. Back to main menu"); + + magenta(window); + mvwaddstr(window, y + 3, 2, "Enter selection: "); + wrefresh(window); + + char c = getch(); + + if(c < '0' || c > lastInterface) { + interfaceMenu(window); + }else{ + if(c == '0') { + mainmenu(window); + }else{ + if(c > '0' && c <= lastInterface) { + interfaceSelect(window, interfaceHash.value(c)); + } + } + } +} + +void interfaceSelect(WINDOW *window, QString interface) { + wclear(window); + + cyan(window); + box(window, 0 , 0); + + red(window); + QString title = QString("IP Addresses on: ") + interface; + QByteArray array = title.toLatin1(); + const char *data = array.constData(); + + mvwaddstr(window, 1, 2, data); + + green(window); + + Interface obj(interface); + QStringList ipList = obj.addresses(); + + int count = 1; + int y = 3; + + foreach(QString ip, ipList) { + ip = QString::number(count) + ". " + ip; + QByteArray ipArray = ip.toLatin1(); + const char *ipStr = ipArray.constData(); + + mvwaddstr(window, y, 2, ipStr); + + ++y; + ++count; + } + + cyan(window); + mvwaddstr(window, y + 1, 2, "0. Back to interfaces"); + + magenta(window); + mvwaddstr(window, y + 3, 2, "Enter selection: "); + wrefresh(window); + + char c = getch(); + + if(c < '0' || c > '3') { + interfaceMenu(window); + }else{ + if(c == '0') { + interfaceMenu(window); + } + } +} + +int main(int argc, char *argv[]) { + QCoreApplication app(argc, argv); + + initscr(); + refresh(); + keypad(stdscr, TRUE); + nonl(); + cbreak(); + noecho(); + + if(has_colors()) { + start_color(); + + /* + * Simple color assignment, often all we need. Color pair 0 cannot +* be redefined. This example uses the same value for the color +* pair as for the foreground color, though of course that is not +* necessary: + */ + init_pair(1, COLOR_RED, COLOR_BLACK); + init_pair(2, COLOR_GREEN, COLOR_BLACK); + init_pair(3, COLOR_YELLOW, COLOR_BLACK); + init_pair(4, COLOR_BLUE, COLOR_BLACK); + init_pair(5, COLOR_CYAN, COLOR_BLACK); + init_pair(6, COLOR_MAGENTA, COLOR_BLACK); + init_pair(7, COLOR_WHITE, COLOR_BLACK); + }else{ + printf("Your terminal does not support colors\n"); + return 1; + } + + WINDOW *window = newwin(0, 0, 0, 0); + + //printf("\e[1;1H\e[2J"); + + mainmenu(window); + + delwin(window); + + endwin(); + + /*int ret = app.exec(); + + return ret;*/ + return 0; +} diff --git a/tui/tui.pro b/tui/tui.pro new file mode 100644 index 0000000..2c113f0 --- /dev/null +++ b/tui/tui.pro @@ -0,0 +1,24 @@ +###################################################################### +# Automatically generated by qmake (3.0) Mon Sep 2 19:47:22 2013 +###################################################################### + +TEMPLATE = app +TARGET = tui +QT += core + +SOURCES += main.cpp + +LIBS += -lncurses + +INCLUDEPATH += ../ + +LIBS += -L.. -lip + +QMAKE_CXXFLAGS += -Wall -Wextra -Werror -fsanitize=address -fPIE +QMAKE_CFLAGS += -Wall -Wextra -Werror -fsanitize=address -fPIE + +LIBS += -lasan + +QMAKE_LFLAGS += -Wl,-rpath,/home/bp/libip + +# Directories