--- a/sources/interface.cpp Tue Dec 16 23:50:56 2014 +0200 +++ b/sources/interface.cpp Mon May 04 15:51:03 2015 +0300 @@ -45,25 +45,25 @@ INPUTSTATE_CONFIRM_DISCONNECTION, }; -static StringList g_input; -static int g_inputCursor = 0; -static int g_cursor = 0; -static int g_pan = 0; -static bool g_needRefresh = false; -static bool g_needStatusBarRender = false; -static bool g_needInputRender = false; -static bool g_needOutputRender = false; -static bool g_needNicklistRender = false; -static struct { char ch; int x; } g_cursorChar; -static Vector<ColoredLine> g_output;; -static int g_outputScroll = 0; -static String g_title; -static InputState g_inputState = INPUTSTATE_NORMAL; -static Function<void (void)> g_disconnectConfirmFunction = nullptr; -static IPAddress g_address; -static String g_statusBarText; -static StringList g_playerNames; -static String g_pasteBuffer; +static StringList InputHistory; +static int InputCursor = 0; +static int CursorPosition = 0; +static int InputPanning = 0; +static bool NeedRefresh = false; +static bool NeedStatusBarRender = false; +static bool NeedInputRender = false; +static bool NeedOutputRender = false; +static bool NeedNicklistRender = false; +static struct { char ch; int x; } CursorCharacter; +static Vector<ColoredLine> OutputLines; +static int OutputScroll = 0; +static String Title; +static InputState CurrentInputState = INPUTSTATE_NORMAL; +static Function<void (void)> DisconnectConfirmFunction = nullptr; +static IPAddress CurrentAddress; +static String StatusBarText; +static StringList PlayerNames; +static String PasteBuffer; // ------------------------------------------------------------------------------------------------- // @@ -78,7 +78,7 @@ static FUNCTION current_input() -> const String& { - return g_input[g_inputCursor]; + return InputHistory[InputCursor]; } // ------------------------------------------------------------------------------------------------- @@ -88,10 +88,10 @@ static FUNCTION detach_input() -> void { - if (g_inputCursor > 0) + if (InputCursor > 0) { - g_input[0] = current_input(); - g_inputCursor = 0; + InputHistory[0] = current_input(); + InputCursor = 0; } } @@ -102,7 +102,7 @@ mutable_current_input() -> String& { detach_input(); - return g_input[g_inputCursor]; + return InputHistory[InputCursor]; } // ------------------------------------------------------------------------------------------------- @@ -111,19 +111,19 @@ move_input_cursor (int delta) -> void { // No input history when inputting addresses or passwords - if (g_inputState != INPUTSTATE_NORMAL) + if (CurrentInputState != INPUTSTATE_NORMAL) { - g_inputCursor = 0; + InputCursor = 0; return; } - int oldcursor = g_inputCursor; - g_inputCursor = clamp (g_inputCursor + delta, 0, g_input.size() - 1); + int oldcursor = InputCursor; + InputCursor = clamp (InputCursor + delta, 0, InputHistory.size() - 1); - if (g_inputCursor != oldcursor) + if (InputCursor != oldcursor) { - g_cursor = current_input().length(); - g_needInputRender = true; + CursorPosition = current_input().length(); + NeedInputRender = true; } } @@ -134,7 +134,7 @@ { String prompt; - switch (g_inputState) + switch (CurrentInputState) { case INPUTSTATE_NORMAL: prompt = ">"; break; case INPUTSTATE_ADDRESS: prompt = "address:"; break; @@ -152,25 +152,25 @@ { // Clear the input row (unless going to or from confirm state) if (newstate != INPUTSTATE_CONFIRM_DISCONNECTION - and g_inputState != INPUTSTATE_CONFIRM_DISCONNECTION) + and CurrentInputState != INPUTSTATE_CONFIRM_DISCONNECTION) { - g_inputCursor = 0; + InputCursor = 0; mutable_current_input().clear(); } switch (newstate) { case INPUTSTATE_ADDRESS: - if (g_address.host != 0) - mutable_current_input() = g_address.to_string (IP_WITH_PORT); + if (CurrentAddress.host != 0) + mutable_current_input() = CurrentAddress.to_string (IP_WITH_PORT); break; default: break; } - g_inputState = newstate; - g_needInputRender = true; + CurrentInputState = newstate; + NeedInputRender = true; } // ------------------------------------------------------------------------------------------------- @@ -186,11 +186,11 @@ ::refresh(); ::timeout (0); ::use_default_colors(); - g_input.clear(); - g_input << ""; - g_output.clear(); - g_output << ColoredLine(); - g_title = format (APPNAME " %1 (%2)", full_version_string(), changeset_date_string()); + InputHistory.clear(); + InputHistory << ""; + OutputLines.clear(); + OutputLines << ColoredLine(); + Title = format (APPNAME " %1 (%2)", full_version_string(), changeset_date_string()); for (int i = 0; i < NUM_COLORS; ++i) for (int j = 0; j < NUM_COLORS; ++j) @@ -202,7 +202,7 @@ render_full(); refresh(); - g_needRefresh = false; + NeedRefresh = false; } // ------------------------------------------------------------------------------------------------- @@ -210,19 +210,19 @@ static FUNCTION interface_render_titlebar() -> void { - if (g_title.length() <= COLS) + if (Title.length() <= COLS) { int pair = interface_color_pair (WHITE, BLUE); - int startx = (COLS - g_title.length()) / 2; - int endx = startx + g_title.length(); + int startx = (COLS - Title.length()) / 2; + int endx = startx + Title.length(); attron (pair); - mvprintw (0, startx, "%s", g_title.chars()); + mvprintw (0, startx, "%s", Title.chars()); mvhline (0, 0, ' ', startx); mvhline (0, endx, ' ', COLS - endx); attroff (pair); } - g_needRefresh = true; + NeedRefresh = true; } // ------------------------------------------------------------------------------------------------- @@ -230,7 +230,7 @@ FUNCTION Interface::set_title (const String& title) -> void { - g_title = title; + Title = title; interface_render_titlebar(); } @@ -241,7 +241,7 @@ { if (RCONSession::get_session()->is_active()) { - g_disconnectConfirmFunction = afterwards; + DisconnectConfirmFunction = afterwards; set_input_state (INPUTSTATE_CONFIRM_DISCONNECTION); } else @@ -331,15 +331,15 @@ static FUNCTION interface_render_output() -> void { - if (g_output.size() == 1) + if (OutputLines.size() == 1) return; - g_outputScroll = clamp (g_outputScroll, 0, g_output.size() - 1); + OutputScroll = clamp (OutputScroll, 0, OutputLines.size() - 1); int height = LINES - 3; int width = COLS - interface_nicklist_width(); int printOffset = 0; - int end = g_output.size() - 1 - g_outputScroll; + int end = OutputLines.size() - 1 - OutputScroll; int start = end; int usedHeight = 0; int y = 1; @@ -348,7 +348,7 @@ // Where to start? while (start > 0) { - int rows = g_output[start - 1].rows (width); + int rows = OutputLines[start - 1].rows (width); if (usedHeight + rows > height) { @@ -364,9 +364,9 @@ // See if there's any more rows to use (end may be too small) if (not tightFit) { - while (end < g_output.size()) + while (end < OutputLines.size()) { - int rows = g_output[end].rows (width); + int rows = OutputLines[end].rows (width); if (usedHeight + rows > height) { @@ -382,7 +382,7 @@ if (start > 0) printOffset = height - usedHeight; - g_outputScroll = g_output.size() - 1 - end; + OutputScroll = OutputLines.size() - 1 - end; if (start < 0 or start == end or printOffset >= height) return; @@ -397,10 +397,10 @@ y += printOffset; for (int i = start; i < end; ++i) - y = interface_render_colorline (y, 0, width, g_output[i], true); + y = interface_render_colorline (y, 0, width, OutputLines[i], true); - g_needOutputRender = false; - g_needRefresh = true; + NeedOutputRender = false; + NeedRefresh = true; } // ------------------------------------------------------------------------------------------------- @@ -420,9 +420,9 @@ { mvhline (y, x, ' ', width); - if (i < g_playerNames.size()) + if (i < PlayerNames.size()) { - String displaynick = g_playerNames[i]; + String displaynick = PlayerNames[i]; if (displaynick.length() > width) { @@ -436,8 +436,8 @@ y++; } - g_needNicklistRender = false; - g_needRefresh = true; + NeedNicklistRender = false; + NeedRefresh = true; } // ------------------------------------------------------------------------------------------------- @@ -449,13 +449,13 @@ // If we're asking the user if they want to disconnect, we don't render any input strings, // just the confirmation message. - if (g_inputState == INPUTSTATE_CONFIRM_DISCONNECTION) + if (CurrentInputState == INPUTSTATE_CONFIRM_DISCONNECTION) { attron (promptColor); mvhline (LINES - 2, 0, ' ', COLS); mvprintw (LINES - 2, 0, "Are you sure you want to disconnect? y/n"); attroff (promptColor); - g_needRefresh = true; + NeedRefresh = true; return; } @@ -465,25 +465,25 @@ int y = LINES - 2; // If we're inputting a password, replace it with asterisks - if (g_inputState == INPUTSTATE_PASSWORD) + if (CurrentInputState == INPUTSTATE_PASSWORD) { for (char& ch : displayString) ch = '*'; } // Ensure the cursor is within bounds - g_cursor = clamp (g_cursor, 0, displayString.length()); + CursorPosition = clamp (CursorPosition, 0, displayString.length()); // Ensure that the cursor is always in view, adjust panning if this is not the case - if (g_cursor > g_pan + displayLength) - g_pan = g_cursor - displayLength; // cursor went too far right - else if (g_cursor < g_pan) - g_pan = g_cursor; // cursor went past the pan value to the left + if (CursorPosition > InputPanning + displayLength) + InputPanning = CursorPosition - displayLength; // cursor went too far right + else if (CursorPosition < InputPanning) + InputPanning = CursorPosition; // cursor went past the pan value to the left // What part of the string to draw? - int start = g_pan; + int start = InputPanning; int end = min<int> (displayString.length(), start + displayLength); - assert (g_cursor >= start and g_cursor <= end); + assert (CursorPosition >= start and CursorPosition <= end); // Render the input string mvhline (LINES - 2, 0, ' ', COLS); @@ -496,10 +496,10 @@ // Store in memory where the cursor is now (so that we can re-draw it to position the terminal // cursor). - g_cursorChar.ch = g_cursor != 0 ? displayString[g_cursor - 1] : '\0'; - g_cursorChar.x = prompt.length() + (g_cursor - g_pan); - g_needRefresh = true; - g_needInputRender = false; + CursorCharacter.ch = CursorPosition != 0 ? displayString[CursorPosition - 1] : '\0'; + CursorCharacter.x = prompt.length() + (CursorPosition - InputPanning); + NeedRefresh = true; + NeedInputRender = false; } // ------------------------------------------------------------------------------------------------- @@ -511,10 +511,10 @@ int y = LINES - 1; attron (color); mvhline (y, 0, ' ', COLS); - mvprintw (y, 0, "%s", g_statusBarText.chars()); + mvprintw (y, 0, "%s", StatusBarText.chars()); attroff (color); - g_needRefresh = true; - g_needStatusBarRender = false; + NeedRefresh = true; + NeedStatusBarRender = false; } // ------------------------------------------------------------------------------------------------- @@ -551,10 +551,10 @@ text += "^N to connect, ^Q to quit"; - if (text != g_statusBarText) + if (text != StatusBarText) { - g_statusBarText = text; - g_needStatusBarRender = true; + StatusBarText = text; + NeedStatusBarRender = true; } } @@ -577,13 +577,13 @@ interface_position_cursor() -> void { // This is only relevant if the input string is being drawn - if (g_inputState == INPUTSTATE_CONFIRM_DISCONNECTION) + if (CurrentInputState == INPUTSTATE_CONFIRM_DISCONNECTION) return; int y = LINES - 2; - if (g_cursorChar.ch != '\0') - mvprintw (y, g_cursorChar.x, "%c", g_cursorChar.ch); + if (CursorCharacter.ch != '\0') + mvprintw (y, CursorCharacter.x, "%c", CursorCharacter.ch); else mvprintw (y, interface_prompt_string().length(), " "); } @@ -594,7 +594,7 @@ interface_find_previous_word() -> int { const String& input = current_input(); - int pos = g_cursor; + int pos = CursorPosition; // Move past whitespace while (pos > 0 and isspace (input[pos - 1])) @@ -613,7 +613,7 @@ interface_find_next_word() -> int { const String& input = current_input(); - int pos = g_cursor; + int pos = CursorPosition; // Move past current whitespace while (pos < input.length() and isspace (input[pos])) @@ -634,13 +634,13 @@ if (a >= b) return; - if (g_cursor > a and g_cursor <= b) - g_cursor = a; + if (CursorPosition > a and CursorPosition <= b) + CursorPosition = a; String& input = mutable_current_input(); - g_pasteBuffer = input.mid (a, b); + PasteBuffer = input.mid (a, b); input.remove (a, b - a); - g_needInputRender = true; + NeedInputRender = true; } // ------------------------------------------------------------------------------------------------- @@ -657,12 +657,12 @@ return; } - if (g_inputState == INPUTSTATE_CONFIRM_DISCONNECTION) + if (CurrentInputState == INPUTSTATE_CONFIRM_DISCONNECTION) { if (ch == 'y' or ch == 'Y') { RCONSession::get_session()->disconnect(); - g_disconnectConfirmFunction(); + DisconnectConfirmFunction(); } else if (ch == 'n' or ch == 'N') set_input_state (INPUTSTATE_NORMAL); @@ -672,13 +672,13 @@ if (ch >= 0x20 and ch <= 0x7E) { - mutable_current_input().insert (g_cursor++, char (ch)); - g_needInputRender = true; + mutable_current_input().insert (CursorPosition++, char (ch)); + NeedInputRender = true; } else switch (ch) { case 'Q' - 'A' + 1: // ^Q - switch (g_inputState) + switch (CurrentInputState) { case INPUTSTATE_CONFIRM_DISCONNECTION: break; @@ -712,19 +712,19 @@ case KEY_LEFT: case 'B' - 'A' + 1: // readline ^B - if (g_cursor > 0) + if (CursorPosition > 0) { - g_cursor--; - g_needInputRender = true; + CursorPosition--; + NeedInputRender = true; } break; case KEY_RIGHT: case 'F' - 'A' + 1: // readline ^F - if (g_cursor < current_input().length()) + if (CursorPosition < current_input().length()) { - g_cursor++; - g_needInputRender = true; + CursorPosition++; + NeedInputRender = true; } break; @@ -735,77 +735,77 @@ case KEY_HOME: case 'A' - 'A' + 1: // readline ^A - if (g_cursor != 0) + if (CursorPosition != 0) { - g_cursor = 0; - g_needInputRender = true; + CursorPosition = 0; + NeedInputRender = true; } break; case KEY_END: case 'E' - 'A' + 1: // readline ^E - if (g_cursor != current_input().length()) + if (CursorPosition != current_input().length()) { - g_cursor = current_input().length(); - g_needInputRender = true; + CursorPosition = current_input().length(); + NeedInputRender = true; } break; case KEY_BACKSPACE: - if (g_cursor > 0) + if (CursorPosition > 0) { - mutable_current_input().remove_at (--g_cursor); - g_needInputRender = true; + mutable_current_input().remove_at (--CursorPosition); + NeedInputRender = true; } break; case KEY_DC: case 'D' - 'A' + 1: // readline ^D - if (g_cursor < current_input().length()) + if (CursorPosition < current_input().length()) { - mutable_current_input().remove_at (g_cursor); - g_needInputRender = true; + mutable_current_input().remove_at (CursorPosition); + NeedInputRender = true; } break; case KEY_PPAGE: - g_outputScroll += min (g_pageSize, LINES / 2); - g_needOutputRender = true; + OutputScroll += min (g_pageSize, LINES / 2); + NeedOutputRender = true; break; case KEY_NPAGE: - g_outputScroll -= min (g_pageSize, LINES / 2); - g_needOutputRender = true; + OutputScroll -= min (g_pageSize, LINES / 2); + NeedOutputRender = true; break; case 'U' - 'A' + 1: // readline ^U - delete from start to cursor - if (g_cursor > 0) + if (CursorPosition > 0) { - yank (0, g_cursor); - g_cursor = 0; + yank (0, CursorPosition); + CursorPosition = 0; } break; case 'K' - 'A' + 1: // readline ^K - delete from cursor to end - yank (g_cursor, mutable_current_input().length()); + yank (CursorPosition, mutable_current_input().length()); break; case 'W' - 'A' + 1: // readline ^W - delete from previous word bounary to current - yank (interface_find_previous_word(), g_cursor); + yank (interface_find_previous_word(), CursorPosition); break; case 'Y' - 'A' + 1: // readline ^Y - paste previously deleted text - if (not g_pasteBuffer.is_empty()) + if (not PasteBuffer.is_empty()) { - mutable_current_input().insert (g_cursor, g_pasteBuffer); - g_cursor += g_pasteBuffer.length(); - g_needInputRender = true; + mutable_current_input().insert (CursorPosition, PasteBuffer); + CursorPosition += PasteBuffer.length(); + NeedInputRender = true; } break; case '\n': case KEY_ENTER: - switch (g_inputState) + switch (CurrentInputState) { case INPUTSTATE_CONFIRM_DISCONNECTION: break; // handled above @@ -813,7 +813,7 @@ case INPUTSTATE_ADDRESS: try { - g_address = IPAddress::from_string (current_input()); + CurrentAddress = IPAddress::from_string (current_input()); } catch (std::exception& e) { @@ -821,19 +821,19 @@ return; } - if (g_address.port == 0) - g_address.port = 10666; + if (CurrentAddress.port == 0) + CurrentAddress.port = 10666; set_input_state (INPUTSTATE_PASSWORD); break; case INPUTSTATE_PASSWORD: - if (g_inputState == INPUTSTATE_PASSWORD and not current_input().is_empty()) + if (CurrentInputState == INPUTSTATE_PASSWORD and not current_input().is_empty()) { RCONSession* session = RCONSession::get_session(); session->disconnect(); session->set_password (current_input()); - session->connect (g_address); + session->connect (CurrentAddress); set_input_state (INPUTSTATE_NORMAL); } break; @@ -841,15 +841,15 @@ case INPUTSTATE_NORMAL: if (RCONSession::get_session()->send_command (current_input())) { - g_input.insert (0, ""); - g_needInputRender = true; + InputHistory.insert (0, ""); + NeedInputRender = true; } break; } break; case 'N' - 'A' + 1: // ^N - if (g_inputState == INPUTSTATE_NORMAL) + if (CurrentInputState == INPUTSTATE_NORMAL) safe_disconnect ([]() {set_input_state (INPUTSTATE_ADDRESS);}); break; @@ -864,30 +864,30 @@ case 'b': case 'B': // readline alt-b - move one word to the left - g_cursor = interface_find_previous_word(); - g_needInputRender = true; + CursorPosition = interface_find_previous_word(); + NeedInputRender = true; break; case 'f': case 'F': // readline alt-f - move one word to the right - g_cursor = interface_find_next_word(); - g_needInputRender = true; + CursorPosition = interface_find_next_word(); + NeedInputRender = true; break; case 'd': case 'D': // readline alt-d - delete from here till next word boundary - yank (g_cursor, interface_find_next_word()); + yank (CursorPosition, interface_find_next_word()); break; } } else { // No alt-key, handle pure escape - if (g_inputState == INPUTSTATE_PASSWORD) + if (CurrentInputState == INPUTSTATE_PASSWORD) set_input_state (INPUTSTATE_ADDRESS); - else if (g_inputState == INPUTSTATE_ADDRESS) + else if (CurrentInputState == INPUTSTATE_ADDRESS) set_input_state (INPUTSTATE_NORMAL); } break; @@ -899,16 +899,16 @@ FUNCTION Interface::render() -> void { - if (g_needStatusBarRender) interface_render_statusbar(); - if (g_needInputRender) interface_render_input(); - if (g_needOutputRender) interface_render_output(); - if (g_needNicklistRender) interface_render_nicklist(); + if (NeedStatusBarRender) interface_render_statusbar(); + if (NeedInputRender) interface_render_input(); + if (NeedOutputRender) interface_render_output(); + if (NeedNicklistRender) interface_render_nicklist(); - if (g_needRefresh) + if (NeedRefresh) { interface_position_cursor(); refresh(); - g_needRefresh = false; + NeedRefresh = false; } } @@ -924,12 +924,12 @@ { if (ch == '\n') { - g_output.last().finalize(); - g_output << ColoredLine(); + OutputLines.last().finalize(); + OutputLines << ColoredLine(); continue; } - if (g_output.last().length() == 0) + if (OutputLines.last().length() == 0) { time_t now; time (&now); @@ -937,13 +937,13 @@ strftime (timestamp, sizeof timestamp, "[%H:%M:%S] ", localtime (&now)); for (char* cp = timestamp; *cp != '\0'; ++cp) - g_output.last().add_char (*cp); + OutputLines.last().add_char (*cp); } - g_output.last().add_char (ch); + OutputLines.last().add_char (ch); } - g_needOutputRender = true; + NeedOutputRender = true; } // ------------------------------------------------------------------------------------------------- @@ -953,7 +953,7 @@ { try { - g_address = IPAddress::from_string (address); + CurrentAddress = IPAddress::from_string (address); } catch (std::exception& e) { @@ -961,13 +961,13 @@ return; } - if (g_address.port == 0) - g_address.port = 10666; + if (CurrentAddress.port == 0) + CurrentAddress.port = 10666; RCONSession* session = RCONSession::get_session(); session->disconnect(); session->set_password (password); - session->connect (g_address); + session->connect (CurrentAddress); } // ------------------------------------------------------------------------------------------------- @@ -975,6 +975,6 @@ FUNCTION Interface::set_player_names (const StringList& names) -> void { - g_playerNames = names; - g_needNicklistRender = true; + PlayerNames = names; + NeedNicklistRender = true; }