sources/interface.cpp

changeset 30
21fba5183768
parent 29
e534f2f78196
child 31
b5b5a6a96d91
equal deleted inserted replaced
29:e534f2f78196 30:21fba5183768
122 ::raw(); 122 ::raw();
123 ::keypad (stdscr, true); 123 ::keypad (stdscr, true);
124 ::noecho(); 124 ::noecho();
125 ::refresh(); 125 ::refresh();
126 ::timeout (0); 126 ::timeout (0);
127 ::use_default_colors();
127 g_title = format (APPNAME " %1 (%2)", full_version_string(), changeset_date_string()); 128 g_title = format (APPNAME " %1 (%2)", full_version_string(), changeset_date_string());
128 129
129 for (int i = 0; i < NUM_COLORS; ++i) 130 for (int i = 0; i < NUM_COLORS; ++i)
130 for (int j = 0; j < NUM_COLORS; ++j) 131 for (int j = 0; j < NUM_COLORS; ++j)
131 { 132 {
172 // ------------------------------------------------------------------------------------------------- 173 // -------------------------------------------------------------------------------------------------
173 // 174 //
174 static FUNCTION 175 static FUNCTION
175 safe_disconnect (Function<void()> afterwards) -> void 176 safe_disconnect (Function<void()> afterwards) -> void
176 { 177 {
177 if (RCONSession::get_session() != nullptr 178 RCONSession* session = RCONSession::get_session();
178 and RCONSession::get_session()->state() != RCON_DISCONNECTED) 179
180 if (session and session->state() != RCON_DISCONNECTED)
179 { 181 {
180 g_disconnectConfirmFunction = afterwards; 182 g_disconnectConfirmFunction = afterwards;
181 set_input_state (INPUTSTATE_CONFIRM_DISCONNECTION); 183 set_input_state (INPUTSTATE_CONFIRM_DISCONNECTION);
182 } 184 }
183 else 185 else
245 // What part of the string to draw? 247 // What part of the string to draw?
246 int start = g_pan; 248 int start = g_pan;
247 int end = min<int> (g_input.length(), start + displaylength); 249 int end = min<int> (g_input.length(), start + displaylength);
248 assert (g_cursor >= start and g_cursor <= end); 250 assert (g_cursor >= start and g_cursor <= end);
249 251
252 String displayinput = g_input;
253
254 // If we're inputting a password, replace it with asterisks
255 if (g_inputState == INPUTSTATE_PASSWORD)
256 {
257 for (char& ch : displayinput)
258 ch = '*';
259 }
260
250 // Render the input string 261 // Render the input string
251 mvhline (LINES - 2, 0, ' ', COLS); 262 mvhline (LINES - 2, 0, ' ', COLS);
252 mvprintw (y, prompt.length() + 1, "%s", g_input.chars()); 263 mvprintw (y, prompt.length() + 1, "%s", displayinput.chars());
253 264
254 // Render the prompt 265 // Render the prompt
255 attron (promptColor); 266 attron (promptColor);
256 mvprintw (y, 0, "%s", prompt.chars()); 267 mvprintw (y, 0, "%s", prompt.chars());
257 attroff (promptColor); 268 attroff (promptColor);
258 269
259 // Store in memory where the cursor is now (so that we can re-draw it to position the terminal 270 // Store in memory where the cursor is now (so that we can re-draw it to position the terminal
260 // cursor). 271 // cursor).
261 g_cursorChar.ch = g_cursor != 0 ? g_input[g_cursor - 1] : '\0'; 272 g_cursorChar.ch = g_cursor != 0 ? displayinput[g_cursor - 1] : '\0';
262 g_cursorChar.x = prompt.length() + (g_cursor - g_pan); 273 g_cursorChar.x = prompt.length() + (g_cursor - g_pan);
263 g_needRefresh = true; 274 g_needRefresh = true;
264 g_needInputRender = false; 275 g_needInputRender = false;
265 } 276 }
266 277
364 int ch = ::getch(); 375 int ch = ::getch();
365 376
366 if (g_inputState == INPUTSTATE_CONFIRM_DISCONNECTION) 377 if (g_inputState == INPUTSTATE_CONFIRM_DISCONNECTION)
367 { 378 {
368 if (ch == 'y' or ch == 'Y') 379 if (ch == 'y' or ch == 'Y')
380 {
381 RCONSession::get_session()->disconnect();
369 g_disconnectConfirmFunction(); 382 g_disconnectConfirmFunction();
383 }
370 else if (ch == 'n' or ch == 'N') 384 else if (ch == 'n' or ch == 'N')
371 set_input_state (INPUTSTATE_NORMAL); 385 set_input_state (INPUTSTATE_NORMAL);
386
372 return; 387 return;
373 } 388 }
374 389
375 if (ch >= 0x20 and ch <= 0x7E) 390 if (ch >= 0x20 and ch <= 0x7E)
376 { 391 {
551 g_output[g_output.size() - 1] += ch; 566 g_output[g_output.size() - 1] += ch;
552 } 567 }
553 568
554 g_needOutputRender = true; 569 g_needOutputRender = true;
555 } 570 }
571
572 // -------------------------------------------------------------------------------------------------
573 //
574 FUNCTION
575 Interface::connect (String address, String password) -> void
576 {
577 try
578 {
579 g_address = IPAddress::from_string (address);
580 }
581 catch (std::exception& e)
582 {
583 print (e.what());
584 return;
585 }
586
587 if (g_address.port == 0)
588 g_address.port = 10666;
589
590 RCONSession* session = RCONSession::new_session();
591 session->set_password (password);
592 session->connect (g_address);
593 }

mercurial