Added virtual keyboard

This commit is contained in:
fabio.olimpieri 2011-11-26 09:27:25 +00:00
parent 84ec8dd8c6
commit 63a899dbdc
4 changed files with 258 additions and 226 deletions

View File

@ -1,203 +1,194 @@
/********************************************************************* /*********************************************************************
* *
* Copyright (C) 2009, Simon Kagstrom * Copyright (C) 2009, Simon Kagstrom
* *
* Filename: VirtualKeyboard.c * Filename: VirtualKeyboard.c
* Author: Simon Kagstrom <simon.kagstrom@gmail.com> * Author: Simon Kagstrom <simon.kagstrom@gmail.com>
* Description: A virtual keyboard * Description: A virtual keyboard
* *
* $Id$ * $Id$
* *
********************************************************************/ ********************************************************************/
#include <SDL.h> #include <SDL.h>
#include <SDL_ttf.h> #include <SDL_ttf.h>
#include "menu.h" #include "menu.h"
#include "VirtualKeyboard.h" #include "VirtualKeyboard.h"
struct virtkey; class VirtualKeyboard
{
class VirtualKeyboard public:
{ VirtualKeyboard(SDL_Surface *screen, TTF_Font *font);
public: struct virtkey* get_key();
VirtualKeyboard(SDL_Surface *screen, TTF_Font *font);
const char* get_key(); private:
struct virtkey* get_key_internal();
private: void draw();
struct virtkey *get_key_internal(); void select_next(int dx, int dy);
void draw(); void toggle_shift();
void select_next(int dx, int dy);
void toggle_shift(); SDL_Surface *screen;
TTF_Font *font;
SDL_Surface *screen; int sel_x;
TTF_Font *font; int sel_y;
int sel_x;
int sel_y; char buf[255];
};
char buf[255];
};
typedef struct virtkey #define K(name, sdl_code) \
{ { name, "KEY_"name, sdl_code, false }
const char *name; #define N(name, key_name, sdl_code) \
const char *ev_name; { name, "KEY_"key_name, sdl_code, false }
bool is_done; #define D(name) \
} virtkey_t; { name, "None", 0, true }
#define KNL() \
#define K(name) \ { NULL, NULL, 0, false }
{ name, "KEY_"name, false } #define NJ(name, joy_name) \
#define N(name, key_name) \ { name, joy_name, 0, false }
{ name, "KEY_"key_name, false }
#define D(name) \ #define KEY_COLS 14
{ name, "None", true } #define KEY_ROWS 8
#define KNL() \
{ NULL, NULL, false } static virtkey_t keys[KEY_COLS * KEY_ROWS] = {
#define NJ(name, joy_name) \ N("Esc","ESC",27), KNL(), K("F1",282),K("F2",283),K("F3",284),K("F4",285),K("F5",286),K("F6",287),K("F7",288),K("F8",289),K("F9",290),K("F10",291), N("BS","BACKSPACE",8),N("Help", "HELP",277),
{ name, joy_name, false } N("~`","BACKQUOTE",96),KNL(),K("1",49),K("2",50), K("3",51), K("4",52), K("5",53), K("6",54), K("7",55), K("8",56), K("9",57), K("0",48), N("-","SUB",45),N("=","EQUALS",61),
N("Tab", "TAB", 9), KNL(), K("Q",113), K("W",119), K("E",101), K("R",114), K("T",116), K("Y",121), K("U",117), K("I",105), K("O",111), K("P",112),N("[","LEFTBRACKET",91),N("]","RIGHTBRACKET",93),
#define KEY_COLS 14 N("Ctrl","CTRL",306),KNL(), K("A",97), K("S",115), K("D",100), K("F",102), K("G",103), K("H",104), K("J",106), K("K",107), K("L",108),N(":;","SEMICOLON",59),N("'","SINGLEQUOTE",96),N("Ret","RETURN",13),
#define KEY_ROWS 8 N("Sft","SHIFT_LEFT",304),KNL(),K("Z",122),K("X",120),K("C",99), K("V",118), K("B",98), K("N",110), K("M",109),N("<,","COMMA",44),N(">.","PERIOD",46), N("/","SLASH",47),N("\\","KEY_BACKSLASH",92), N("Sft","SHIFT_RIGHT",303),
N("Alt","ALT_LEFT",308),KNL(), N("Amg","AMIGA_LEFT",310),KNL(),N("space", "SPACE",32),KNL(),KNL(),KNL(),N("Up","CURSOR_UP",273),KNL(),KNL(),N("Amg","AMIGA_RIGHT",309),KNL(),N("Alt","ALT_RIGHT",307),
static virtkey_t keys[KEY_COLS * KEY_ROWS] = { D("None"), KNL(), KNL(), KNL(), KNL(), KNL(), N("Lft","CURSOR_LEFT",276),KNL(), N("Dwn","CURSOR_DOWN",274), KNL(), N("Rgt", "CURSOR_RIGHT",275),KNL(), N("Enter","ENTER",271),KNL(),
N("Esc", "ESC"), KNL(), K("F1"),K("F2"),K("F3"),K("F4"),K("F5"),K("F6"),K("F7"),K("F8"),K("F9"),K("F10"), N("Del","DEL"),N("Help", "HELP"), NJ("Fire","JOY_FIRE_BUTTON"),KNL(),KNL(),NJ("Joy 2nd button","JOY_2ND_BUTTON"),KNL(),KNL(),KNL(),KNL(),KNL(),NJ("Joy 3rd button","JOY_3RD_BUTTON"),KNL(),KNL(),KNL(),KNL()
N("~`","BACKQUOTE"),KNL(),K("1"), K("2"), K("3"), K("4"), K("5"), K("6"), K("7"), K("8"), K("9"), K("0"), N("-", "SUB"),N("+", "PLUS"), };
N("Tab", "TAB"), KNL(), K("Q"), K("W"), K("E"), K("R"), K("T"), K("Y"), K("U"), K("I"), K("O"), K("P"),N("[", "LEFTBRACKET"),N("]","RIGHTBRACKET"),
N("Sft","SHIFT_LEFT"),KNL(), K("A"), K("S"), K("D"), K("F"), K("G"), K("H"), K("J"), K("K"), K("L"), N(":;", "SEMICOLON"), N("@#", "??"), N("Sft", "SHIFT_RIGHT"), VirtualKeyboard::VirtualKeyboard(SDL_Surface *screen, TTF_Font *font)
N("Ctrl","CTRL"),KNL(),K("Z"),K("X"), K("C"), K("V"), K("B"), K("N"), K("M"),N("<,", "COMMA"),N(">.", "PERIOD"), N("\\","KEY_BACKSLASH"), N("/", "SLASH"),N("Ret", "RETURN"), {
N("Alt","ALT_LEFT"),KNL(), N("Amg","AMIGA_LEFT"),KNL(),N("space", "SPACE"),KNL(),KNL(),KNL(), N("Up", "CURSOR_UP"),KNL(),KNL(),N("Amg","AMIGA_RIGHT"),KNL(),N("Alt","ALT_RIGHT"), this->screen = screen;
D("None"), KNL(), KNL(), KNL(), KNL(), KNL(), N("Lft", "CURSOR_LEFT"),KNL(), N("Dwn", "CURSOR_DOWN"), KNL(), N("Rgt", "CURSOR_RIGHT"),KNL(), N("Enter", "ENTER"),KNL(), this->font = font;
NJ("Fire","JOY_FIRE_BUTTON"),KNL(),KNL(),NJ("Joy 2nd button","JOY_2ND_BUTTON"),KNL(),KNL(),KNL(),KNL(),KNL(),NJ("Joy 3rd button","JOY_3RD_BUTTON"),KNL(),KNL(),KNL(),KNL() this->sel_x = 0;
}; this->sel_y = 0;
VirtualKeyboard::VirtualKeyboard(SDL_Surface *screen, TTF_Font *font) memset(this->buf, 0, sizeof(this->buf));
{ }
this->screen = screen;
this->font = font; void VirtualKeyboard::draw()
this->sel_x = 0; {
this->sel_y = 0; int screen_w = this->screen->w;
int screen_h = this->screen->h;
memset(this->buf, 0, sizeof(this->buf)); int key_w = 36;
} int key_h = 36;
int border_x = (screen_w - (key_w * KEY_COLS)) / 2;
void VirtualKeyboard::draw() int border_y = (screen_h - (key_h * KEY_ROWS)) / 2;
{ SDL_Rect bg_rect = {border_x, border_y,
int screen_w = this->screen->w; key_w * KEY_COLS, key_h * KEY_ROWS};
int screen_h = this->screen->h;
int key_w = 36; SDL_FillRect(this->screen, &bg_rect,
int key_h = 36; SDL_MapRGB(screen->format, 0xff, 0xff, 0xff));
int border_x = (screen_w - (key_w * KEY_COLS)) / 2;
int border_y = (screen_h - (key_h * KEY_ROWS)) / 2; for (int y = 0; y < KEY_ROWS; y++ )
SDL_Rect bg_rect = {border_x, border_y, {
key_w * KEY_COLS, key_h * KEY_ROWS}; for (int x = 0; x < KEY_COLS; x++ )
{
SDL_FillRect(this->screen, &bg_rect, int which = y * KEY_COLS + x;
SDL_MapRGB(screen->format, 0xff, 0xff, 0xff)); virtkey_t key = keys[which];
int r = 64, g = 64, b = 64;
for (int y = 0; y < KEY_ROWS; y++ ) const char *what = key.name;
{
for (int x = 0; x < KEY_COLS; x++ ) /* Skip empty positions */
{ if (key.name == NULL)
int which = y * KEY_COLS + x; continue;
virtkey_t key = keys[which];
int r = 64, g = 64, b = 64; if ( key.is_done )
const char *what = key.name; r = 255;
if ( (x == this->sel_x && y == this->sel_y))
/* Skip empty positions */ g = 200;
if (key.name == NULL)
continue; menu_print_font(this->screen, r, g, b,
x * key_w + border_x, y * key_h + border_y,
if ( key.is_done ) what);
r = 255; }
if ( (x == this->sel_x && y == this->sel_y)) }
g = 200; }
menu_print_font(this->screen, r, g, b, void VirtualKeyboard::select_next(int dx, int dy)
x * key_w + border_x, y * key_h + border_y, {
what); int next_x = (this->sel_x + dx) % KEY_COLS;
} int next_y = (this->sel_y + dy) % KEY_ROWS;
} virtkey_t key;
}
if (next_x < 0)
void VirtualKeyboard::select_next(int dx, int dy) next_x = KEY_COLS + next_x;
{ if (next_y < 0)
int next_x = (this->sel_x + dx) % KEY_COLS; next_y = KEY_ROWS + next_y;
int next_y = (this->sel_y + dy) % KEY_ROWS; this->sel_x = next_x;
virtkey_t key; this->sel_y = next_y;
if (next_x < 0) key = keys[ next_y * KEY_COLS + next_x ];
next_x = KEY_COLS + next_x;
if (next_y < 0) /* Skip the empty spots */
next_y = KEY_ROWS + next_y; if (key.name == NULL)
this->sel_x = next_x; {
this->sel_y = next_y; if (dy != 0) /* Look left */
this->select_next(-1, 0);
key = keys[ next_y * KEY_COLS + next_x ]; else
this->select_next(dx, dy);
/* Skip the empty spots */ }
if (key.name == NULL) }
{
if (dy != 0) /* Look left */ struct virtkey *VirtualKeyboard::get_key_internal()
this->select_next(-1, 0); {
else while(1)
this->select_next(dx, dy); {
} uint32_t k;
}
this->draw();
struct virtkey *VirtualKeyboard::get_key_internal() SDL_Flip(this->screen);
{
while(1) k = menu_wait_key_press();
{
uint32_t k; if (k & KEY_UP)
this->select_next(0, -1);
this->draw(); else if (k & KEY_DOWN)
SDL_Flip(this->screen); this->select_next(0, 1);
else if (k & KEY_LEFT)
k = menu_wait_key_press(); this->select_next(-1, 0);
else if (k & KEY_RIGHT)
if (k & KEY_UP) this->select_next(1, 0);
this->select_next(0, -1); else if (k & KEY_ESCAPE)
else if (k & KEY_DOWN) return NULL;
this->select_next(0, 1); else if (k & KEY_SELECT)
else if (k & KEY_LEFT) {
this->select_next(-1, 0); virtkey_t *key = &keys[ this->sel_y * KEY_COLS + this->sel_x ];
else if (k & KEY_RIGHT)
this->select_next(1, 0); return key;
else if (k & KEY_ESCAPE) }
return NULL; }
else if (k & KEY_SELECT)
{ return NULL;
virtkey_t *key = &keys[ this->sel_y * KEY_COLS + this->sel_x ]; }
return key; struct virtkey* VirtualKeyboard::get_key()
} {
} virtkey_t *key;
SDL_Rect rect = {32, 32, FULL_DISPLAY_X-64, FULL_DISPLAY_Y-96};
return NULL;
} SDL_FillRect(this->screen, &rect, SDL_MapRGB(screen->format, 0xff, 0xff, 0xff));
const char* VirtualKeyboard::get_key() key = this->get_key_internal();
{
virtkey_t *key; return key;
SDL_Rect rect = {32, 32, FULL_DISPLAY_X-64, FULL_DISPLAY_Y-96}; }
SDL_FillRect(this->screen, &rect, SDL_MapRGB(screen->format, 0xff, 0xff, 0xff)); /* C interface */
static VirtualKeyboard *virtual_keyboard;
key = this->get_key_internal(); void virtkbd_init(SDL_Surface *surf, TTF_Font *fnt)
if (key == NULL) {
return NULL; virtual_keyboard = new VirtualKeyboard(surf, fnt);
}
return key->ev_name;
} struct virtkey *virtkbd_get_key(void)
{
/* C interface */ return virtual_keyboard->get_key();
static VirtualKeyboard *virtual_keyboard; }
void virtkbd_init(SDL_Surface *surf, TTF_Font *fnt)
{
virtual_keyboard = new VirtualKeyboard(surf, fnt);
}
const char *virtkbd_get_key(void)
{
return virtual_keyboard->get_key();
}

View File

@ -12,12 +12,20 @@
#include <SDL.h> #include <SDL.h>
#include <SDL_ttf.h> #include <SDL_ttf.h>
typedef struct virtkey
{
const char *name;
const char *ev_name;
int sdl_code;
int is_done;
} virtkey_t;
#if defined(__cplusplus) #if defined(__cplusplus)
extern "C" { extern "C" {
#endif #endif
extern void virtkbd_init(SDL_Surface *surf, TTF_Font *fnt); extern void virtkbd_init(SDL_Surface *surf, TTF_Font *fnt);
extern const char *virtkbd_get_key(void); extern struct virtkey *virtkbd_get_key(void);
#if defined(__cplusplus) #if defined(__cplusplus)
}; };

View File

@ -24,6 +24,14 @@
#define ID_BUTTON_OFFSET 0 #define ID_BUTTON_OFFSET 0
#define ID_AXIS_OFFSET 32 #define ID_AXIS_OFFSET 32
/* Uncomment for debugging output */
//#define DEBUG
#ifdef DEBUG
#define DEBUG_LOG write_log
#else
#define DEBUG_LOG(...) do ; while(0)
#endif
extern int usbismount, smbismount; extern int usbismount, smbismount;
extern const char *strdup_path_expand (const char *src); extern const char *strdup_path_expand (const char *src);
@ -38,13 +46,14 @@ static const char *main_menu_messages[] = {
/*04*/ "#1-------------------------------------", /*04*/ "#1-------------------------------------",
/*05*/ "Wiimote configuration", /*05*/ "Wiimote configuration",
/*06*/ "^|Wiimote1|Wiimote2", /*06*/ "^|Wiimote1|Wiimote2",
/*07*/ "Hardware options", /*07*/ "Virtual keyboard",
/*08*/ "Emulation options", /*08*/ "Hardware options",
/*09*/ "Other options", /*09*/ "Emulation options",
/*10*/ "Save confs", /*10*/ "Other options",
/*11*/ "Reset UAE", /*11*/ "Save confs",
/*12*/ "Help", /*12*/ "Reset UAE",
/*13*/ "Quit", /*13*/ "Help",
/*14*/ "Quit",
NULL NULL
}; };
@ -579,7 +588,8 @@ static void insert_keyboard_map(const char *key, const char *fmt, ...)
//printf("Mibb: %s:%s\n", buf, key); //printf("Mibb: %s:%s\n", buf, key);
read_inputdevice_config (&changed_prefs, buf, key); read_inputdevice_config (&changed_prefs, buf, key);
read_inputdevice_config (&currprefs, buf, key);
inputdevice_config_change();
} }
@ -587,8 +597,8 @@ static void setup_joystick(int joy, const char *key, int sdl_key)
{ {
if (!strcmp(key, "None")) if (!strcmp(key, "None"))
{ {
currprefs.joystick_settings[1][joy].eventid[ID_BUTTON_OFFSET + sdl_key][0] = 0;
changed_prefs.joystick_settings[1][joy].eventid[ID_BUTTON_OFFSET + sdl_key][0] = 0; changed_prefs.joystick_settings[1][joy].eventid[ID_BUTTON_OFFSET + sdl_key][0] = 0;
inputdevice_config_change();
} }
else else
insert_keyboard_map(key, "input.1.joystick.%d.button.%d", joy, sdl_key); insert_keyboard_map(key, "input.1.joystick.%d.button.%d", joy, sdl_key);
@ -621,8 +631,8 @@ static void input_options(int joy)
if (!joy) insert_keyboard_map("JOY2_HORIZ","input.1.joystick.%d.axis.6", 0); if (!joy) insert_keyboard_map("JOY2_HORIZ","input.1.joystick.%d.axis.6", 0);
else insert_keyboard_map("JOY1_HORIZ" ,"input.1.joystick.%d.axis.6", 1);} else insert_keyboard_map("JOY1_HORIZ" ,"input.1.joystick.%d.axis.6", 1);}
else{ else{
currprefs.joystick_settings[1][joy].eventid[ID_AXIS_OFFSET + 6][0] = 0;
changed_prefs.joystick_settings[1][joy].eventid[ID_AXIS_OFFSET + 6][0] = 0; changed_prefs.joystick_settings[1][joy].eventid[ID_AXIS_OFFSET + 6][0] = 0;
inputdevice_config_change();
} }
return; return;
} }
@ -632,17 +642,16 @@ static void input_options(int joy)
if (submenus[4]) if (submenus[4])
{ {
changed_prefs.mouse_settings[1][joy].enabled = 0; changed_prefs.mouse_settings[1][joy].enabled = 0;
currprefs.mouse_settings[1][joy].enabled = 0;
} }
else else
{ {
changed_prefs.mouse_settings[1][joy].enabled = 1; changed_prefs.mouse_settings[1][joy].enabled = 1;
currprefs.mouse_settings[1][joy].enabled = 1;
} }
inputdevice_config_change();
return; return;
} }
key = virtkbd_get_key(); key = virtkbd_get_key()->ev_name;
if (key == NULL) if (key == NULL)
return; return;
switch(opt) switch(opt)
@ -669,6 +678,25 @@ static void input_options(int joy)
} }
static void virtual_keyboard(void)
{
int key_code;
virtkey_t *key =virtkbd_get_key();
if (key) {key_code = key->sdl_code;} else return;
SDL_Event event_key;
event_key.type=SDL_KEYDOWN;
event_key.key.keysym.sym=key_code;
SDL_PushEvent(&event_key);
DEBUG_LOG ("Push Event: keycode %d %s\n", key_code, "SDL_KEYDOWN");
event_key.type=SDL_KEYUP;
SDL_PushEvent(&event_key);
DEBUG_LOG ("Push Event: keycode %d %s\n", key_code, "SDL_KEYUP");
}
static void hardware_options(void) static void hardware_options(void)
@ -881,32 +909,35 @@ void gui_display(int shortcut)
break; break;
case 5: case 5:
input_options(submenus[2]); input_options(submenus[2]);
break; break;
case 7: case 7:
hardware_options(); virtual_keyboard();
break; break;
case 8: case 8:
hardware_options();
break;
case 9:
emulation_options(); emulation_options();
break; break;
case 9: case 10:
graphic_options(); graphic_options();
break; break;
case 10: case 11:
save_configurations(); save_configurations();
break; break;
case 11: case 12:
uae_reset(1); uae_reset(1);
break; break;
case 12: case 13:
help(); help();
break; break;
case 13: case 14:
if (msgYesNo("Are you sure to quit?", 0, FULL_DISPLAY_X /2-138, FULL_DISPLAY_Y /2-48)) uae_quit(); if (msgYesNo("Are you sure to quit?", 0, FULL_DISPLAY_X /2-138, FULL_DISPLAY_Y /2-48)) uae_quit();
break; break;
default: default:
break; break;
} }
} while (opt == 0 || opt == 5 || opt == 7 || opt == 8 || opt == 9 || opt == 12); } while (opt == 0 || opt == 5 || opt == 8 || opt == 9 || opt == 10 || opt == 13);
resume_sound(); resume_sound();
} }

View File

@ -58,7 +58,6 @@ sound_output=normal
sound_frequency=32000 sound_frequency=32000
sound_latency=200 sound_latency=200
sdl.map_raw_keys=true
input.config=1 input.config=1
@ -164,3 +163,6 @@ floppy0type=0
floppy1type=0 floppy1type=0
floppy2type=-1 floppy2type=-1
floppy3type=-1 floppy3type=-1
# Set keyboard language. Possible values are de, dk, es, us, se, fr, it
kbd_lang=us