nesemu/debugger/dialog.c

88 lines
2.2 KiB
C

//
// Created by william on 1/7/24.
//
#include <string.h>
#include "dialog.h"
#define DIALOG_KEY_CONFIRM 10
#define DIALOG_KEY_EXIT 27
#define DIALOG_KEY_DELETE 127
#define DIALOG_KEY_DIGIT_MIN 48
#define DIALOG_KEY_DIGIT_MAX 57
#define DIALOG_KEY_ALPHA_MIN 97
#define DIALOG_KEY_ALPHA_MAX 102
Dialog dialog_create(char *message) {
Dialog dialog;
int termHeight = getmaxy(stdscr);
int termWidth = getmaxx(stdscr);
int height = 3;
int width = (int) strlen(message) + 2;
if (width % 2 == 1) {
width += 1;
}
int y = termHeight / 2 - (height / 2);
int x = termWidth / 2 - (width / 2);
WINDOW *window = newwin(height, width, y, x);
box(window, 0, 0);
mvwprintw(window, 0, 1, "%s", message);
wmove(window, 1, 1);
dialog.panel = new_panel(window);
return dialog;
}
address dialog_get_address(Dialog *dialog, bool *cancelled) {
int input_length = 0;
address out = 0;
int keycode;
while ((keycode = wgetch(dialog->panel->win)) != DIALOG_KEY_CONFIRM) {
if (keycode == DIALOG_KEY_EXIT) {
*cancelled = true;
return 0;
}
if (input_length > 0 && keycode == DIALOG_KEY_DELETE) {
int offset = 16 - (4 * input_length);
address mask = 0xf << offset;
out &= ~mask;
input_length--;
mvwprintw(dialog->panel->win, 1, 1 + input_length, " ");
wmove(dialog->panel->win, 1, 1 + input_length);
}
if (input_length < 4 && keycode >= DIALOG_KEY_DIGIT_MIN && keycode <= DIALOG_KEY_ALPHA_MAX) {
int digit = keycode - DIALOG_KEY_DIGIT_MIN; // 0-9
if (digit > 9) {
if (keycode >= DIALOG_KEY_ALPHA_MIN) { // A-F
digit -= DIALOG_KEY_ALPHA_MIN - DIALOG_KEY_DIGIT_MAX - 1;
} else {
// Anything else
continue;
}
}
int offset = 12 - (4 * input_length);
out += digit << offset;
mvwprintw(dialog->panel->win, 1, 1 + input_length, "%c", keycode);
input_length++;
}
}
return out;
}
void dialog_remove(Dialog *dialog) {
del_panel(dialog->panel);
}