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
*
* Filename: VirtualKeyboard.c
* Author: Simon Kagstrom <simon.kagstrom@gmail.com>
* Description: A virtual keyboard
*
* $Id$
*
********************************************************************/
#include <SDL.h>
#include <SDL_ttf.h>
#include "menu.h"
#include "VirtualKeyboard.h"
struct virtkey;
class VirtualKeyboard
{
public:
VirtualKeyboard(SDL_Surface *screen, TTF_Font *font);
const char* get_key();
private:
struct virtkey *get_key_internal();
void draw();
void select_next(int dx, int dy);
void toggle_shift();
SDL_Surface *screen;
TTF_Font *font;
int sel_x;
int sel_y;
char buf[255];
};
typedef struct virtkey
{
const char *name;
const char *ev_name;
bool is_done;
} virtkey_t;
#define K(name) \
{ name, "KEY_"name, false }
#define N(name, key_name) \
{ name, "KEY_"key_name, false }
#define D(name) \
{ name, "None", true }
#define KNL() \
{ NULL, NULL, false }
#define NJ(name, joy_name) \
{ name, joy_name, false }
#define KEY_COLS 14
#define KEY_ROWS 8
static virtkey_t keys[KEY_COLS * KEY_ROWS] = {
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"),
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"),
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"),
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(),
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()
};
VirtualKeyboard::VirtualKeyboard(SDL_Surface *screen, TTF_Font *font)
{
this->screen = screen;
this->font = font;
this->sel_x = 0;
this->sel_y = 0;
memset(this->buf, 0, sizeof(this->buf));
}
void VirtualKeyboard::draw()
{
int screen_w = this->screen->w;
int screen_h = this->screen->h;
int key_w = 36;
int key_h = 36;
int border_x = (screen_w - (key_w * KEY_COLS)) / 2;
int border_y = (screen_h - (key_h * KEY_ROWS)) / 2;
SDL_Rect bg_rect = {border_x, border_y,
key_w * KEY_COLS, key_h * KEY_ROWS};
SDL_FillRect(this->screen, &bg_rect,
SDL_MapRGB(screen->format, 0xff, 0xff, 0xff));
for (int y = 0; y < KEY_ROWS; y++ )
{
for (int x = 0; x < KEY_COLS; x++ )
{
int which = y * KEY_COLS + x;
virtkey_t key = keys[which];
int r = 64, g = 64, b = 64;
const char *what = key.name;
/* Skip empty positions */
if (key.name == NULL)
continue;
if ( key.is_done )
r = 255;
if ( (x == this->sel_x && y == this->sel_y))
g = 200;
menu_print_font(this->screen, r, g, b,
x * key_w + border_x, y * key_h + border_y,
what);
}
}
}
void VirtualKeyboard::select_next(int dx, int dy)
{
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)
next_x = KEY_COLS + next_x;
if (next_y < 0)
next_y = KEY_ROWS + next_y;
this->sel_x = next_x;
this->sel_y = next_y;
key = keys[ next_y * KEY_COLS + next_x ];
/* Skip the empty spots */
if (key.name == NULL)
{
if (dy != 0) /* Look left */
this->select_next(-1, 0);
else
this->select_next(dx, dy);
}
}
struct virtkey *VirtualKeyboard::get_key_internal()
{
while(1)
{
uint32_t k;
this->draw();
SDL_Flip(this->screen);
k = menu_wait_key_press();
if (k & KEY_UP)
this->select_next(0, -1);
else if (k & KEY_DOWN)
this->select_next(0, 1);
else if (k & KEY_LEFT)
this->select_next(-1, 0);
else if (k & KEY_RIGHT)
this->select_next(1, 0);
else if (k & KEY_ESCAPE)
return NULL;
else if (k & KEY_SELECT)
{
virtkey_t *key = &keys[ this->sel_y * KEY_COLS + this->sel_x ];
return key;
}
}
return NULL;
}
const char* VirtualKeyboard::get_key()
{
virtkey_t *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));
key = this->get_key_internal();
if (key == NULL)
return NULL;
return key->ev_name;
}
/* C interface */
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();
}
/*********************************************************************
*
* Copyright (C) 2009, Simon Kagstrom
*
* Filename: VirtualKeyboard.c
* Author: Simon Kagstrom <simon.kagstrom@gmail.com>
* Description: A virtual keyboard
*
* $Id$
*
********************************************************************/
#include <SDL.h>
#include <SDL_ttf.h>
#include "menu.h"
#include "VirtualKeyboard.h"
class VirtualKeyboard
{
public:
VirtualKeyboard(SDL_Surface *screen, TTF_Font *font);
struct virtkey* get_key();
private:
struct virtkey* get_key_internal();
void draw();
void select_next(int dx, int dy);
void toggle_shift();
SDL_Surface *screen;
TTF_Font *font;
int sel_x;
int sel_y;
char buf[255];
};
#define K(name, sdl_code) \
{ name, "KEY_"name, sdl_code, false }
#define N(name, key_name, sdl_code) \
{ name, "KEY_"key_name, sdl_code, false }
#define D(name) \
{ name, "None", 0, true }
#define KNL() \
{ NULL, NULL, 0, false }
#define NJ(name, joy_name) \
{ name, joy_name, 0, false }
#define KEY_COLS 14
#define KEY_ROWS 8
static virtkey_t keys[KEY_COLS * KEY_ROWS] = {
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),
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),
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),
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),
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(),
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()
};
VirtualKeyboard::VirtualKeyboard(SDL_Surface *screen, TTF_Font *font)
{
this->screen = screen;
this->font = font;
this->sel_x = 0;
this->sel_y = 0;
memset(this->buf, 0, sizeof(this->buf));
}
void VirtualKeyboard::draw()
{
int screen_w = this->screen->w;
int screen_h = this->screen->h;
int key_w = 36;
int key_h = 36;
int border_x = (screen_w - (key_w * KEY_COLS)) / 2;
int border_y = (screen_h - (key_h * KEY_ROWS)) / 2;
SDL_Rect bg_rect = {border_x, border_y,
key_w * KEY_COLS, key_h * KEY_ROWS};
SDL_FillRect(this->screen, &bg_rect,
SDL_MapRGB(screen->format, 0xff, 0xff, 0xff));
for (int y = 0; y < KEY_ROWS; y++ )
{
for (int x = 0; x < KEY_COLS; x++ )
{
int which = y * KEY_COLS + x;
virtkey_t key = keys[which];
int r = 64, g = 64, b = 64;
const char *what = key.name;
/* Skip empty positions */
if (key.name == NULL)
continue;
if ( key.is_done )
r = 255;
if ( (x == this->sel_x && y == this->sel_y))
g = 200;
menu_print_font(this->screen, r, g, b,
x * key_w + border_x, y * key_h + border_y,
what);
}
}
}
void VirtualKeyboard::select_next(int dx, int dy)
{
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)
next_x = KEY_COLS + next_x;
if (next_y < 0)
next_y = KEY_ROWS + next_y;
this->sel_x = next_x;
this->sel_y = next_y;
key = keys[ next_y * KEY_COLS + next_x ];
/* Skip the empty spots */
if (key.name == NULL)
{
if (dy != 0) /* Look left */
this->select_next(-1, 0);
else
this->select_next(dx, dy);
}
}
struct virtkey *VirtualKeyboard::get_key_internal()
{
while(1)
{
uint32_t k;
this->draw();
SDL_Flip(this->screen);
k = menu_wait_key_press();
if (k & KEY_UP)
this->select_next(0, -1);
else if (k & KEY_DOWN)
this->select_next(0, 1);
else if (k & KEY_LEFT)
this->select_next(-1, 0);
else if (k & KEY_RIGHT)
this->select_next(1, 0);
else if (k & KEY_ESCAPE)
return NULL;
else if (k & KEY_SELECT)
{
virtkey_t *key = &keys[ this->sel_y * KEY_COLS + this->sel_x ];
return key;
}
}
return NULL;
}
struct virtkey* VirtualKeyboard::get_key()
{
virtkey_t *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));
key = this->get_key_internal();
return key;
}
/* C interface */
static VirtualKeyboard *virtual_keyboard;
void virtkbd_init(SDL_Surface *surf, TTF_Font *fnt)
{
virtual_keyboard = new VirtualKeyboard(surf, fnt);
}
struct virtkey *virtkbd_get_key(void)
{
return virtual_keyboard->get_key();
}

View File

@ -12,12 +12,20 @@
#include <SDL.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)
extern "C" {
#endif
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)
};

View File

@ -24,6 +24,14 @@
#define ID_BUTTON_OFFSET 0
#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 const char *strdup_path_expand (const char *src);
@ -38,13 +46,14 @@ static const char *main_menu_messages[] = {
/*04*/ "#1-------------------------------------",
/*05*/ "Wiimote configuration",
/*06*/ "^|Wiimote1|Wiimote2",
/*07*/ "Hardware options",
/*08*/ "Emulation options",
/*09*/ "Other options",
/*10*/ "Save confs",
/*11*/ "Reset UAE",
/*12*/ "Help",
/*13*/ "Quit",
/*07*/ "Virtual keyboard",
/*08*/ "Hardware options",
/*09*/ "Emulation options",
/*10*/ "Other options",
/*11*/ "Save confs",
/*12*/ "Reset UAE",
/*13*/ "Help",
/*14*/ "Quit",
NULL
};
@ -579,7 +588,8 @@ static void insert_keyboard_map(const char *key, const char *fmt, ...)
//printf("Mibb: %s:%s\n", 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"))
{
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;
inputdevice_config_change();
}
else
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);
else insert_keyboard_map("JOY1_HORIZ" ,"input.1.joystick.%d.axis.6", 1);}
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;
inputdevice_config_change();
}
return;
}
@ -632,17 +642,16 @@ static void input_options(int joy)
if (submenus[4])
{
changed_prefs.mouse_settings[1][joy].enabled = 0;
currprefs.mouse_settings[1][joy].enabled = 0;
}
else
{
changed_prefs.mouse_settings[1][joy].enabled = 1;
currprefs.mouse_settings[1][joy].enabled = 1;
}
inputdevice_config_change();
return;
}
key = virtkbd_get_key();
key = virtkbd_get_key()->ev_name;
if (key == NULL)
return;
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)
@ -881,32 +909,35 @@ void gui_display(int shortcut)
break;
case 5:
input_options(submenus[2]);
break;
break;
case 7:
hardware_options();
virtual_keyboard();
break;
case 8:
hardware_options();
break;
case 9:
emulation_options();
break;
case 9:
case 10:
graphic_options();
break;
case 10:
case 11:
save_configurations();
break;
case 11:
case 12:
uae_reset(1);
break;
case 12:
case 13:
help();
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();
break;
default:
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();
}

View File

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