X-Git-Url: https://git.ucc.asn.au/?a=blobdiff_plain;f=src%2Fclient%2Fmain.c;h=1f38f7ed9ebd13b972eaf6520794ece034fb7426;hb=99cf95b138082d1c02705682ad0dfd8b67a27ac4;hp=ee93227ce39fec34540561ce2cb9dc2a39f93bd6;hpb=cbc8b9bd4b01261475453a5dfbecf2d508edc730;p=tpg%2Fopendispense2.git diff --git a/src/client/main.c b/src/client/main.c index ee93227..1f38f7e 100644 --- a/src/client/main.c +++ b/src/client/main.c @@ -14,6 +14,7 @@ #include // isspace #include #include +#include #include // close #include // gethostbyname @@ -30,6 +31,9 @@ typedef struct sItem { } tItem; // === PROTOTYPES === + int ShowNCursesUI(void); +void PrintAlign(int Row, int Col, int Width, const char *Left, char Pad1, const char *Mid, char Pad2, const char *Right, ...); + int sendf(int Socket, const char *Format, ...); int OpenConnection(const char *Host, int Port); void Authenticate(int Socket); @@ -159,6 +163,10 @@ int main(int argc, char *argv[]) // - Hmm... that would require standardising the item ID to be : // Oh, why not :) + #if 1 + i = ShowNCursesUI(); + #else + for(;;) { char *buf; @@ -181,36 +189,40 @@ int main(int argc, char *argv[]) printf("Bad item (should be between 0 and %i)\n", giNumItems); continue; } - - sendf(sock, "DISPENSE %s\n", gaItems[i].Ident); - - len = recv(sock, buffer, BUFSIZ-1, 0); - buffer[len] = '\0'; - trim(buffer); - - responseCode = atoi(buffer); - switch( responseCode ) - { - case 200: - printf("Dispense OK\n"); - break; - case 401: - printf("Not authenticated\n"); - break; - case 402: - printf("Insufficient balance\n"); - break; - case 406: - printf("Bad item name, bug report\n"); - break; - case 500: - printf("Item failed to dispense, is the slot empty?\n"); - break; - default: - printf("Unknown response code %i\n", responseCode); - break; - } - + break; + } + } + #endif + + if( i >= 0 ) + { + // Dispense! + sendf(sock, "DISPENSE %s\n", gaItems[i].Ident); + + len = recv(sock, buffer, BUFSIZ-1, 0); + buffer[len] = '\0'; + trim(buffer); + + responseCode = atoi(buffer); + switch( responseCode ) + { + case 200: + printf("Dispense OK\n"); + break; + case 401: + printf("Not authenticated\n"); + break; + case 402: + printf("Insufficient balance\n"); + break; + case 406: + printf("Bad item name, bug report\n"); + break; + case 500: + printf("Item failed to dispense, is the slot empty?\n"); + break; + default: + printf("Unknown response code %i\n", responseCode); break; } } @@ -220,6 +232,188 @@ int main(int argc, char *argv[]) return 0; } +void ShowItemAt(int Row, int Col, int Width, int Index) +{ + int _x, _y, times; + + move( Row, Col ); + + if( Index < 0 || Index >= giNumItems ) { + printw("%02i OOR", Index); + return ; + } + printw("%02i %s", Index, gaItems[Index].Desc); + + getyx(stdscr, _y, _x); + // Assumes max 4 digit prices + times = Width - 4 - (_x - Col); // TODO: Better handling for large prices + while(times--) addch(' '); + printw("%4i", gaItems[Index].Price); +} + +/** + */ +int ShowNCursesUI(void) +{ + int ch; + int i, times; + int xBase, yBase; + const int displayMinWidth = 34; + const int displayMinItems = 8; + char *titleString = "Dispense"; + int itemCount = displayMinItems; + int itemBase = 0; + + int height = itemCount + 3; + int width = displayMinWidth; + + // Enter curses mode + initscr(); + raw(); noecho(); + + xBase = COLS/2 - width/2; + yBase = LINES/2 - height/2; + + for( ;; ) + { + // Header + PrintAlign(yBase, xBase, width, "/", '-', titleString, '-', "\\"); + + // Items + for( i = 0; i < itemCount; i ++ ) + { + move( yBase + 1 + i, xBase ); + addch('|'); + addch(' '); + + // Check for ... row + if( i == 0 && itemBase > 0 ) { + printw(" ..."); + times = width - 1 - 8; + while(times--) addch(' '); + } + else if( i == itemCount - 1 && itemBase < giNumItems - itemCount ) { + printw(" ..."); + times = width - 1 - 8; + while(times--) addch(' '); + } + // Show an item + else { + ShowItemAt( yBase + 1 + i, xBase + 2, width - 4, itemBase + i); + addch(' '); + } + + // Scrollbar (if needed) + if( giNumItems > itemCount ) { + if( i == 0 ) { + addch('A'); + } + else if( i == itemCount - 1 ) { + addch('V'); + } + else { + int percentage = itemBase * 100 / (giNumItems-itemCount); + if( i-1 == percentage*(itemCount-3)/100 ) { + addch('#'); + } + else { + addch('|'); + } + } + } + else { + addch('|'); + } + } + + // Footer + PrintAlign(yBase+height-2, xBase, width, "\\", '-', "", '-', "/"); + + // Get input + ch = getch(); + + if( ch == '\x1B' ) { + ch = getch(); + if( ch == '[' ) { + ch = getch(); + + switch(ch) + { + case 'B': + if( itemBase < giNumItems - (itemCount) ) + itemBase ++; + break; + case 'A': + if( itemBase > 0 ) + itemBase --; + break; + } + } + else { + + } + } + else { + break; + } + + } + + + // Leave + endwin(); + return -1; +} + +void PrintAlign(int Row, int Col, int Width, const char *Left, char Pad1, const char *Mid, char Pad2, const char *Right, ...) +{ + int lLen, mLen, rLen; + int times; + + va_list args; + + // Get the length of the strings + va_start(args, Right); + lLen = vsnprintf(NULL, 0, Left, args); + mLen = vsnprintf(NULL, 0, Mid, args); + rLen = vsnprintf(NULL, 0, Right, args); + va_end(args); + + // Sanity check + if( lLen + mLen/2 > Width/2 || mLen/2 + rLen > Width/2 ) { + return ; // TODO: What to do? + } + + move(Row, Col); + + // Render strings + va_start(args, Right); + // - Left + { + char tmp[lLen+1]; + vsnprintf(tmp, lLen+1, Left, args); + addstr(tmp); + } + // - Left padding + times = Width/2 - mLen/2 - lLen; + while(times--) addch(Pad1); + // - Middle + { + char tmp[mLen+1]; + vsnprintf(tmp, mLen+1, Mid, args); + addstr(tmp); + } + // - Right Padding + times = Width/2 - mLen/2 - rLen; + while(times--) addch(Pad2); + // - Right + { + char tmp[rLen+1]; + vsnprintf(tmp, rLen+1, Right, args); + addstr(tmp); + } +} + // === HELPERS === int sendf(int Socket, const char *Format, ...) {