char RecvBuf::getChar() { if (!_chunkedMode) return _getChar(); else { if (_currentChunkSize == 0) { // read the chunk header char ch, chunkStr[20]; int index = 0; while (!isspace(ch = _getChar()) ) chunkStr[index++] = ch; chunkStr[index] = '\0'; sscanf(chunkStr, "%x", &_currentChunkSize); if (ch != '\n') { char ch2 = _getChar(); if (ch != '\r' || ch2 != '\n') { Logger::logError(LOGERROR, "did not find CRLF after chunk"); } } if (_currentChunkSize == 0) throw EndOfChunking(); _currentChunkBytesRead = 1; return _buf[_curPos++]; } else if (_currentChunkBytesRead < _currentChunkSize) { // read a byte from the chunk _currentChunkBytesRead++; return _getChar(); } else { // read the chunk trailer char ch1 = _getChar(); char ch2 = _getChar(); if (ch1 != '\r' || ch2 != '\n') { Logger::logError(LOGERROR, "did not find CRLF after chunk"); }; _currentChunkSize = _currentChunkBytesRead = 0; return getChar(); }; }; };
unsigned int Console_read(char* dest, unsigned int maxlength) { int i = 0; char c = 0; // Read characters until newline or maxlength is reached while(i < maxlength-1 && c != '\n') { c = _getChar(); dest[i] = c; i++; } // Replace the newline with a string terminator dest[i-1] = '\0'; return i; }
void vrtkey_draw() { if (percentOpened == 0) return; int showX = startX; int showY = startY + ((100 - percentOpened) * height_tot) / 100; gfx_color borderColor = gfx_color_rgb(70, 70, 70); gfx_color bgColor = gfx_color_rgb(10, 10, 10); gfx_color bgColorBtn = gfx_color_rgb(40, 40, 40); gfx_color selectColor = gfx_color_rgb(0, 0, 140); gfx_color selectColorBg = gfx_color_rgb(0, 0, 125); int i, x, y, c, c2; gfx_rect_fill_draw(showX, showY, width_tot, height_tot, bgColor); c = caps ? 'A' : 'a'; c2 = '1'; for (y = 0; y < NMBR_KEYS_H; y++) { for (x = 0; x < NMBR_KEYS_W; x++) { int xp = showX + x * keyW; int yp = showY + textfieldH + y * keyH; gfx_rect_fill_draw(xp, yp, keyW - 2, keyH - 2, x == selectedX && y == selectedY ? selectColorBg : bgColorBtn); gfx_rect_draw(xp, yp, keyW - 2, keyH - 2, x == selectedX && y == selectedY ? selectColor : borderColor); gfx_font_print_char(xp + 4, yp + 2, font, _getChar(x, y)); } } char* specialButtonsText[] = {"Done", "Cancel", "Caps", "Space"}; for (i = 0; i < 4; i++) { gfx_color bg; gfx_color brdr; gfx_font_color_clear(font); if (i == 0) { bg = gfx_color_rgb(0, 125, 0); brdr = gfx_color_rgb(0, 140, 0); } else if (i == 1) { if (enableCancelButton) { bg = gfx_color_rgb(125, 0, 0); brdr = gfx_color_rgb(140, 0, 0); } else { gfx_font_color_set(font, gfx_color_rgb(70, 70, 70)); bg = bgColorBtn; brdr = gfx_color_rgb(140, 0, 0); } } else { bg = selectColorBg; brdr = selectColor; } gfx_rect_fill_draw(showX + width, showY + textfieldH + okCancelH * i, okCancelW, okCancelH - 2, NMBR_KEYS_W == selectedX && i == selectedY ? bg : bgColorBtn); gfx_rect_draw(showX + width, showY + textfieldH + okCancelH * i, okCancelW, okCancelH - 2, NMBR_KEYS_W == selectedX && i == selectedY ? brdr : borderColor); gfx_font_print_center_ex(showY + textfieldH + okCancelH * i + 2, okCancelW, showX + width, font, specialButtonsText[i]); } char* keysText = enableCancelButton ? "(A) Select (B) Cancel (X) Bckspc" : "(A) Select (X) Backspace"; gfx_font_print(showX, showY + textfieldH + height + 2, font, keysText); if (showTextfield) { int text_offset = 2; gfx_rect_fill_draw(showX, showY, width_tot, textfieldH - 2, COLOR_WHITE); gfx_rect_draw(showX, showY, width_tot, textfieldH - 2, borderColor); gfx_font_color_set(font, COLOR_BLACK); int charWidth = (font->texture->width >> 4); gfx_font_print(showX + text_offset, showY + 4, font, inputStr); gfx_rect_draw(showX + cursorPosition * charWidth + text_offset, showY + 2, 1, (textfieldH - 4 - 2), COLOR_BLACK); gfx_font_color_clear(font); } }
// Updates and checks key presses uint8_t vrtkey_update() { if (delayInput) { delayInput = false; return VRTKEY_NO_ACTION; } if (state == STATE_CLOSED && percentOpened > 0) percentOpened -= OPEN_CLOSE_SPEED; if (state == STATE_OPENED && percentOpened < 100) percentOpened += OPEN_CLOSE_SPEED; if (state == STATE_CLOSED) return VRTKEY_NO_ACTION; if (control_just_pressed(CONTROL_DPAD_UP)) selectedY = _mod(selectedY - 1, NMBR_KEYS_H); if (control_just_pressed(CONTROL_DPAD_DOWN)) selectedY = _mod(selectedY + 1, NMBR_KEYS_H); if (control_just_pressed(CONTROL_DPAD_LEFT)) selectedX = _mod(selectedX - 1, NMBR_KEYS_W + 1); if (control_just_pressed(CONTROL_DPAD_RIGHT)) selectedX = _mod(selectedX + 1, NMBR_KEYS_W + 1); if (control_just_pressed(CONTROL_BUTTON_A)) { bool charInput = false; if (selectedX == NMBR_KEYS_W) { switch (selectedY) { case 0: return VRTKEY_ACCEPT; case 1: if (enableCancelButton) return VRTKEY_CANCEL; else break; case 2: caps = !caps; break; case 3: charInput = true; break; } } else { charInput = true; } if (charInput) { // Insert key to string char c; // Check space button if (selectedX == NMBR_KEYS_W && selectedY == 3) c = ' '; else c = _getChar(selectedX, selectedY); // Make sure we have space int curLength = strlen(inputStr); if (curLength + 1 == inputLength) { return VRTKEY_NO_ACTION; } int i; for (i = curLength; i >= 0; i--) { inputStr[i + 1] = inputStr[i]; if (i == cursorPosition) break; } inputStr[cursorPosition] = c; cursorPosition++; } } if (control_just_pressed(CONTROL_BUTTON_B) && enableCancelButton) { return VRTKEY_CANCEL; } if (control_just_pressed(CONTROL_BUTTON_X)) { if (cursorPosition > 0) { int curLength = strlen(inputStr); int i; for (i = cursorPosition - 1; i <= curLength; i++) { inputStr[i] = inputStr[i + 1]; } cursorPosition--; } } if (showTextfield) { if (control_just_pressed(CONTROL_TRIGGER_RIGHT)) { if (cursorPosition < strlen(inputStr)) cursorPosition++; } if (control_just_pressed(CONTROL_TRIGGER_LEFT)) { if (cursorPosition > 0) cursorPosition--; } } return VRTKEY_NO_ACTION; }