From 2ee93bab27b4ff3bb73f2b4e261512c2137bac8b Mon Sep 17 00:00:00 2001 From: "simon.kagstrom" Date: Sat, 24 Jan 2009 08:36:47 +0000 Subject: [PATCH] Add ability to type with the virtual keyboard --- Src/C64.h | 4 +- Src/C64_SDL.h | 65 ++++++++++++++++++++------------ Src/VirtualKeyboard.cpp | 83 +++++++++++++++++++++++++++-------------- Src/VirtualKeyboard.h | 4 +- 4 files changed, 102 insertions(+), 54 deletions(-) diff --git a/Src/C64.h b/Src/C64.h index dea6fe4..735b58b 100644 --- a/Src/C64.h +++ b/Src/C64.h @@ -196,7 +196,9 @@ public: char save_game_name[256]; void select_disc(Prefs *np); - void run_fake_key_sequence(Prefs *np); + void select_fake_key_sequence(Prefs *np); + void start_fake_key_sequence(const char *str); + void run_fake_key_sequence(); char * bind_one_key(Prefs *np, int which); void bind_keys(Prefs *np); void other_options(Prefs *np); diff --git a/Src/C64_SDL.h b/Src/C64_SDL.h index 8c13a67..087552e 100644 --- a/Src/C64_SDL.h +++ b/Src/C64_SDL.h @@ -355,7 +355,35 @@ void C64::other_options(Prefs *np) menu_fini(&display_menu); } -void C64::run_fake_key_sequence(Prefs *np) +/* From dreamcast port but heavily modified */ +void C64::run_fake_key_sequence() +{ + int kc = this->virtual_keyboard->char_to_keycode(this->fake_key_str[this->fake_key_index]); + + TheDisplay->FakeKeyPress(kc, TheCIA1->KeyMatrix, TheCIA1->RevMatrix); + + this->fake_key_keytime --; + if (this->fake_key_keytime == 0) + { + this->fake_key_keytime = 4; + this->fake_key_index ++; + + if (this->fake_key_str[this->fake_key_index] == '\0') + { + this->fake_key_sequence = false; + this->fake_key_index = 0; + this->fake_key_keytime = 5; + } + } +} + +void C64::start_fake_key_sequence(const char *str) +{ + this->fake_key_str = str; + this->fake_key_sequence = true; +} + +void C64::select_fake_key_sequence(Prefs *np) { static const char *fake_key_sequences[] = { "\nLOAD \"*\",8,1\nRUN\n", @@ -368,6 +396,7 @@ void C64::run_fake_key_sequence(Prefs *np) "LOAD \"?\",8", "LIST", "10 PRINT \"HELLO WORLD\" and 20 GOTO 10", + "Type with virtual keyboard", NULL}; menu_t fake_key_menu; int opt; @@ -380,8 +409,15 @@ void C64::run_fake_key_sequence(Prefs *np) if (opt < 0) return; - this->fake_key_str = fake_key_sequences[opt]; - this->fake_key_sequence = true; + if (opt == 4) + { + const char *seq = this->virtual_keyboard->get_string(); + + if (seq != NULL) + this->start_fake_key_sequence(seq); + } + else + this->start_fake_key_sequence(fake_key_sequences[opt]); } void C64::save_load_state(Prefs *np) @@ -489,27 +525,8 @@ void C64::VBlank(bool draw_frame) if (TheDisplay->quit_requested) quit_thyself = true; - /* From dreamcast port */ if (this->fake_key_sequence) - { - int kc = this->virtual_keyboard->char_to_keycode(this->fake_key_str[this->fake_key_index]); - - TheDisplay->FakeKeyPress(kc, TheCIA1->KeyMatrix, TheCIA1->RevMatrix); - - this->fake_key_keytime --; - if (this->fake_key_keytime == 0) - { - this->fake_key_keytime = 4; - this->fake_key_index ++; - - if (this->fake_key_str[this->fake_key_index] == '\0') - { - this->fake_key_sequence = false; - this->fake_key_index = 0; - this->fake_key_keytime = 5; - } - } - } + this->run_fake_key_sequence(); #ifndef GEKKO // Joystick keyboard emulation if (TheDisplay->NumLock()) @@ -563,7 +580,7 @@ void C64::VBlank(bool draw_frame) switch(opt) { case 0: /* Load disc/tape */ - this->run_fake_key_sequence(&np); + this->select_fake_key_sequence(&np); break; case 1: /* Insert disc/tape */ this->select_disc(&np); diff --git a/Src/VirtualKeyboard.cpp b/Src/VirtualKeyboard.cpp index 184929b..6f1101a 100644 --- a/Src/VirtualKeyboard.cpp +++ b/Src/VirtualKeyboard.cpp @@ -15,11 +15,12 @@ #include "menu.h" #include "VirtualKeyboard.h" -typedef struct +typedef struct virtkey { const char *name; int kc; bool is_shift; + bool is_done; } virtkey_t; /* @@ -38,11 +39,13 @@ typedef struct #define MATRIX(a,b) (((a) << 3) | (b)) #define K(name, a,b) \ - { name, MATRIX(a,b), false } + { name, MATRIX(a,b), false, false } #define S(name, a,b) \ - { name, MATRIX(a,b), true } + { name, MATRIX(a,b), true, false } #define N(name) \ - { name, -1, false } + { name, -1, false, false } +#define D(name) \ + { name, -1, false, true } #define KEY_COLS 15 #define KEY_ROWS 5 @@ -52,7 +55,7 @@ static virtkey_t keys[KEY_COLS * KEY_ROWS] = { K("Cr", 7,2), K("Q", 7,6), K("W", 1,1), K("E", 1,6), K("R", 2,1), K("T", 2,6), K("Y", 3,1), K("U", 3,6), K("I", 4,1), K("O", 4,6), K("P", 5,1), K("@", 5,6), K("*", 6,1), K("Au", 6,6),K("Rstr",4,0), K("R/Stp", 7,7), K(NULL,0,0), K("A", 1,2), K("S", 1,5), K("D", 2,2), K("F", 2,5), K("G", 3,2), K("H", 3,5), K("J", 4,2), K("K", 4,5), K("L", 5,2), K(":", 5,5), K(";", 6,2), K("=", 6,5), K("Ret", 0,1), K("C=", 7,5), S("Shft",1,7),K(NULL,0,0),K("Z", 1,4), K("X", 2,7), K("C", 2,4), K("V", 3,7), K("B", 3,4), K("N", 4,7), K("M", 4,4), K(",", 5,7), K(".", 5,4), K("/", 6,7), K("Dwn",0,7),K("Rgt", 0,2), - N("None"), K(NULL,0,0), K(NULL,0,0), K("space", 7,4),K(0, 0,0),K(NULL,0,0), K("f1", 0,4),K("f3", 0,5),K("f5", 0,6),K("f7", 0,3),K(NULL,0,0), K(NULL,0,0), K(NULL,0,0), K(NULL,0,0), K("Del", 0,0), + N("None"), K(NULL,0,0), K(NULL,0,0), K("space", 7,4),K(0, 0,0),K(NULL,0,0), K("f1", 0,4),K("f3", 0,5),K("f5", 0,6),K("f7", 0,3),K("Del",0,0),K(NULL,0,0), K(NULL,0,0), K(NULL,0,0), D("DONE"), }; static const char *shifted_names[KEY_COLS * KEY_ROWS] = { @@ -60,7 +63,7 @@ static const char *shifted_names[KEY_COLS * KEY_ROWS] = { NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "[", "]", NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "<", ">", "?", "Up", "Lft", - NULL, NULL, NULL, NULL, NULL, NULL, "f2", "f4", "f6", "f8", NULL, NULL, NULL, NULL, "Ins", + NULL, NULL, NULL, NULL, NULL, NULL, "f2", "f4", "f6", "f8", "Ins", NULL, NULL, NULL, NULL, }; VirtualKeyboard::VirtualKeyboard(SDL_Surface *screen, TTF_Font *font) @@ -196,7 +199,6 @@ int VirtualKeyboard::char_to_keycode(char c) } } - printf("Vobb! c=%d\n", c); return -1; } @@ -206,10 +208,8 @@ const char VirtualKeyboard::get_char(int kc) return this->keycode_to_string(kc)[0]; } -int VirtualKeyboard::get_key_internal() +struct virtkey *VirtualKeyboard::get_key_internal() { - int kc = -1; - while(1) { uint32_t k; @@ -228,30 +228,34 @@ int VirtualKeyboard::get_key_internal() else if (k & KEY_RIGHT) this->select_next(1, 0); else if (k & KEY_ESCAPE) - return -2; + return NULL; else if (k & KEY_SELECT) { - virtkey_t key = keys[ this->sel_y * KEY_COLS + this->sel_x ]; + virtkey_t *key = &keys[ this->sel_y * KEY_COLS + this->sel_x ]; - kc = key.kc; - if (this->shift_on) - kc |= 0x80; - - if (key.is_shift == true) + if (key->is_shift == true) this->toggle_shift(); else - return kc; + return key; } } - return kc; + return NULL; } int VirtualKeyboard::get_key() { + virtkey_t *key; + SDL_FillRect(this->screen, 0, SDL_MapRGB(screen->format, 0x00, 0x80, 0x80)); - return this->get_key_internal(); + key = this->get_key_internal(); + if (key == NULL) + return -2; + + if (key->is_shift) + return key->kc | 0x80; + return key->kc; } const char *VirtualKeyboard::get_string() @@ -263,18 +267,41 @@ const char *VirtualKeyboard::get_string() while (true) { - int kc = this->get_key_internal(); + virtkey_t *key = this->get_key_internal(); + char c; - /* Abort or None */ - if (kc == -2 || kc == -1) + /* Abort */ + if (key == NULL) return NULL; - /* Return */ - if (kc == MATRIX(0, 1)) - return this->buf; - this->buf[cnt] = this->get_char(kc); + /* Done */ + if (key->is_done) + return this->buf; + /* Skip None */ + if (key->kc == -1) + continue; + + /* Special-case for delete */ + if (strcmp(key->name, "Del") == 0) + { + if (cnt < 1) + continue; + this->buf[cnt - 1] = ' '; + cnt -= 2; + } + else + { + c = this->get_char( this->shift_on ? key->kc | 0x80 : key->kc ); + + if (strcmp(key->name, "space") == 0) + c = ' '; + else if (strcmp(key->name, "Ret") == 0) + c = '\n'; + this->buf[cnt] = c; + } + cnt++; - if (cnt >= sizeof(this->buf)) + if (cnt >= sizeof(this->buf) - 1) return this->buf; /* SDL_Flip is done in get_key_internal() */ diff --git a/Src/VirtualKeyboard.h b/Src/VirtualKeyboard.h index 45964cb..a568273 100644 --- a/Src/VirtualKeyboard.h +++ b/Src/VirtualKeyboard.h @@ -12,6 +12,8 @@ #include #include +struct virtkey; + class VirtualKeyboard { public: @@ -23,7 +25,7 @@ public: private: const char get_char(int kc); - int get_key_internal(); + struct virtkey *get_key_internal(); void draw(); void select_next(int dx, int dy); void toggle_shift();