Files

436 lines
13 KiB
Objective-C

/*
* launcher-mobile: a multiplatform flexVDI/SPICE client
*
* Copyright (C) 2016 flexVDI (Flexible Software Solutions S.L.)
*
* This file is part of launcher-mobile.
*
* launcher-mobile is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* launcher-mobile is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with launcher-mobile. If not, see <http://www.gnu.org/licenses/>.
*/
#import "KeyboardView.h"
#import "globals.h"
#import "spice.h"
#import "draw.h"
#import "ctype.h"
typedef struct {
char tc;
int prekey;
int special_prekey;
int special_key;
int key;
} charToKeys;
typedef struct {
char tc;
char ext1;
char ext2;
int prekey;
int special_prekey;
int special_key;
int key;
} extCharToKeys;
charToKeys *keyboardMap = NULL;
extCharToKeys *extKeyboardMap = NULL;
int keyboardMapSize = 0;
int extKeyboardMapSize = 0;
extCharToKeys pc104_es_ext[] = {
{194, 161, 0, 0x0, 0x0, 0x0, 0x0d}, // ¡
{194, 191, 0, 0x0, 0x0, 0x36, 0x0d}, // ¿
{194, 186, 0, 0x0, 0x0, 0x0, 0x29}, // º
{194, 170, 0, 0x0, 0x0, 0x36, 0x29}, // ª
{194, 169, 0, 0x0, 0x0, 0x1d, 0x2e}, // Ctrl + C
{194, 174, 0, 0x0, 0x0, 0x1d, 0x13},
{195, 177, 0, 0x0, 0x0, 0x0, 0x27}, // ñ
{195, 145, 0, 0x0, 0x0, 0x36, 0x27}, // ñ
{195, 167, 0, 0x0, 0x0, 0x0, 0x2b}, // ç
{195, 135, 0, 0x0, 0x0, 0x36, 0x2b}, // Ç
{195, 161, 0, 0x28, 0x0, 0x0, 0x1e}, // á
{195, 169, 0, 0x28, 0x0, 0x0, 0x12}, // é
{195, 173, 0, 0x28, 0x0, 0x0, 0x17}, // í
{195, 179, 0, 0x28, 0x0, 0x0, 0x18}, // ó
{195, 186, 0, 0x28, 0x0, 0x0, 0x16}, // ú
{195, 188, 0, 0x28, 0x36, 0x0, 0x16}, // ü
{195, 129, 0, 0x28, 0x0, 0x36, 0x1e}, // Á
{195, 137, 0, 0x28, 0x0, 0x36, 0x12}, // É
{195, 141, 0, 0x28, 0x0, 0x36, 0x17}, // Í
{195, 147, 0, 0x28, 0x0, 0x36, 0x18}, // Ó
{195, 154, 0, 0x28, 0x0, 0x36, 0x16}, // Ú
{195, 156, 0, 0x28, 0x36, 0x36, 0x16}, // Ü
{195, 159, 0, 0x0, 0x0, 0x1d, 0x30}, // Ctrl + B
{197, 147, 0, 0x0, 0x0, 0x38, 0x0f}, // Alt + Tab
{198, 146, 0, 0x0, 0x0, 0x1d, 0x21}, // Ctrl + F
{206, 169, 0, 0x0, 0x0, 0x1d, 0x2c}, // Ctrl + Z
{226, 130, 172, 0x0, 0x0, 0x138, 0x12}, // Euro
{226, 137, 164, 0x0, 0x0, 0x138, 0x29}, // Backslash
{226, 136, 145, 0x0, 0x0, 0x1d, 0x2d}, // Ctrl + X
{226, 136, 154, 0x0, 0x0, 0x1d, 0x2f}, // Ctrl + V
{226, 136, 130, 0x0, 0x0, 0x1d, 0x20} // Ctrl + D
};
charToKeys pc104_es[] = {
{9, 0x0, 0x0, 0x0, 0xf}, // Tab
{'a', 0x0, 0x0, 0x0, 0x1e},
{'b', 0x0, 0x0, 0x0, 0x30},
{'c', 0x0, 0x0, 0x0, 0x2e},
{'d', 0x0, 0x0, 0x0, 0x20},
{'e', 0x0, 0x0, 0x0, 0x12},
{'f', 0x0, 0x0, 0x0, 0x21},
{'g', 0x0, 0x0, 0x0, 0x22},
{'h', 0x0, 0x0, 0x0, 0x23},
{'i', 0x0, 0x0, 0x0, 0x17},
{'j', 0x0, 0x0, 0x0, 0x24},
{'k', 0x0, 0x0, 0x0, 0x25},
{'l', 0x0, 0x0, 0x0, 0x26},
{'m', 0x0, 0x0, 0x0, 0x32},
{'n', 0x0, 0x0, 0x0, 0x31},
{'o', 0x0, 0x0, 0x0, 0x18},
{'p', 0x0, 0x0, 0x0, 0x19},
{'q', 0x0, 0x0, 0x0, 0x10},
{'r', 0x0, 0x0, 0x0, 0x13},
{'s', 0x0, 0x0, 0x0, 0x1f},
{'t', 0x0, 0x0, 0x0, 0x14},
{'u', 0x0, 0x0, 0x0, 0x16},
{'v', 0x0, 0x0, 0x0, 0x2f},
{'w', 0x0, 0x0, 0x0, 0x11},
{'x', 0x0, 0x0, 0x0, 0x2d},
{'y', 0x0, 0x0, 0x0, 0x15},
{'z', 0x0, 0x0, 0x0, 0x2c},
{'1', 0x0, 0x0, 0x0, 0x02},
{'2', 0x0, 0x0, 0x0, 0x03},
{'3', 0x0, 0x0, 0x0, 0x04},
{'4', 0x0, 0x0, 0x0, 0x05},
{'5', 0x0, 0x0, 0x0, 0x06},
{'6', 0x0, 0x0, 0x0, 0x07},
{'7', 0x0, 0x0, 0x0, 0x08},
{'8', 0x0, 0x0, 0x0, 0x09},
{'9', 0x0, 0x0, 0x0, 0x0a},
{'0', 0x0, 0x0, 0x0, 0x0b},
{' ', 0x0, 0x0, 0x0, 0x39},
{'!', 0x0, 0x0, 0x36, 0x02},
{'@', 0x0, 0x0, 0x138, 0x03},
{'"', 0x0, 0x0, 0x36, 0x03},
{'\'', 0x0, 0x0, 0x0, 0x0c},
{'#', 0x0, 0x0, 0x138, 0x04},
{'~', 0x0, 0x0, 0x138, 0x05},
{'$', 0x0, 0x0, 0x36, 0x05},
{'%', 0x0, 0x0, 0x36, 0x06},
{'&', 0x0, 0x0, 0x36, 0x07},
{'/', 0x0, 0x0, 0x36, 0x08},
{'(', 0x0, 0x0, 0x36, 0x09},
{')', 0x0, 0x0, 0x36, 0x0a},
{'=', 0x0, 0x0, 0x36, 0x0b},
{'?', 0x0, 0x0, 0x36, 0x0c},
{'-', 0x0, 0x0, 0x0, 0x35},
{'_', 0x0, 0x0, 0x36, 0x35},
{';', 0x0, 0x0, 0x36, 0x33},
{',', 0x0, 0x0, 0x0, 0x33},
{'.', 0x0, 0x0, 0x0, 0x34},
{':', 0x0, 0x0, 0x36, 0x34},
{'{', 0x0, 0x0, 0x138, 0x28},
{'}', 0x0, 0x0, 0x138, 0x2b},
{'[', 0x0, 0x0, 0x138, 0x1a},
{']', 0x0, 0x0, 0x138, 0x1b},
{'*', 0x0, 0x0, 0x36, 0x1b},
{'+', 0x0, 0x0, 0x0, 0x1b},
{'\\', 0x0, 0x0, 0x138, 0x29},
{'|', 0x0, 0x0, 0x138, 0x02},
{'^', 0x0, 0x0, 0x36, 0x1a},
{'`', 0x0, 0x0, 0x0, 0x1a},
{'<', 0x0, 0x0, 0x0, 0x56},
{'>', 0x0, 0x0, 0x36, 0x56},
{'\n', 0x0, 0x0, 0x0, 0x1c},
};
extCharToKeys pc104_us_ext[] = {
{195, 167, 0, 0x0, 0x0, 0x1d, 0x2e}, // Ctrl + C
{197, 147, 0, 0x0, 0x0, 0x38, 0x0f}, // Alt + Tab
{198, 146, 0, 0x0, 0x0, 0x1d, 0x21}, // Ctrl + F
{206, 169, 0, 0x0, 0x0, 0x1d, 0x2c}, // Ctrl + Z
{226, 137, 136, 0x0, 0x0, 0x1d, 0x2d}, // Ctrl + X
{226, 136, 154, 0x0, 0x0, 0x1d, 0x2f}, // Ctrl + V
{226, 136, 171, 0x0, 0x0, 0x1d, 0x30}, // Ctrl + B
{226, 136, 130, 0x0, 0x0, 0x1d, 0x20} // Ctrl + D
};
charToKeys pc104_us[] = {
{9, 0x0, 0x0, 0x0, 0xf}, // Tab
{'a', 0x0, 0x0, 0x0, 0x1e},
{'b', 0x0, 0x0, 0x0, 0x30},
{'c', 0x0, 0x0, 0x0, 0x2e},
{'d', 0x0, 0x0, 0x0, 0x20},
{'e', 0x0, 0x0, 0x0, 0x12},
{'f', 0x0, 0x0, 0x0, 0x21},
{'g', 0x0, 0x0, 0x0, 0x22},
{'h', 0x0, 0x0, 0x0, 0x23},
{'i', 0x0, 0x0, 0x0, 0x17},
{'j', 0x0, 0x0, 0x0, 0x24},
{'k', 0x0, 0x0, 0x0, 0x25},
{'l', 0x0, 0x0, 0x0, 0x26},
{'m', 0x0, 0x0, 0x0, 0x32},
{'n', 0x0, 0x0, 0x0, 0x31},
{'o', 0x0, 0x0, 0x0, 0x18},
{'p', 0x0, 0x0, 0x0, 0x19},
{'q', 0x0, 0x0, 0x0, 0x10},
{'r', 0x0, 0x0, 0x0, 0x13},
{'s', 0x0, 0x0, 0x0, 0x1f},
{'t', 0x0, 0x0, 0x0, 0x14},
{'u', 0x0, 0x0, 0x0, 0x16},
{'v', 0x0, 0x0, 0x0, 0x2f},
{'w', 0x0, 0x0, 0x0, 0x11},
{'x', 0x0, 0x0, 0x0, 0x2d},
{'y', 0x0, 0x0, 0x0, 0x15},
{'z', 0x0, 0x0, 0x0, 0x2c},
{'1', 0x0, 0x0, 0x0, 0x02},
{'2', 0x0, 0x0, 0x0, 0x03},
{'3', 0x0, 0x0, 0x0, 0x04},
{'4', 0x0, 0x0, 0x0, 0x05},
{'5', 0x0, 0x0, 0x0, 0x06},
{'6', 0x0, 0x0, 0x0, 0x07},
{'7', 0x0, 0x0, 0x0, 0x08},
{'8', 0x0, 0x0, 0x0, 0x09},
{'9', 0x0, 0x0, 0x0, 0x0a},
{'0', 0x0, 0x0, 0x0, 0x0b},
{' ', 0x0, 0x0, 0x0, 0x39},
{'!', 0x0, 0x0, 0x36, 0x02},
{'@', 0x0, 0x0, 0x36, 0x03},
{'"', 0x0, 0x0, 0x36, 0x28},
{'\'', 0x0, 0x0, 0x0, 0x28},
{'#', 0x0, 0x0, 0x36, 0x04},
{'~', 0x0, 0x0, 0x36, 0x29},
{'$', 0x0, 0x0, 0x36, 0x05},
{'%', 0x0, 0x0, 0x36, 0x06},
{'&', 0x0, 0x0, 0x36, 0x08},
{'/', 0x0, 0x0, 0x0, 0x35},
{'(', 0x0, 0x0, 0x36, 0x0a},
{')', 0x0, 0x0, 0x36, 0x0b},
{'=', 0x0, 0x0, 0x0, 0x0d},
{'+', 0x0, 0x0, 0x36, 0x0d},
{'?', 0x0, 0x0, 0x36, 0x35},
{'-', 0x0, 0x0, 0x0, 0x0c},
{'_', 0x0, 0x0, 0x36, 0x0c},
{';', 0x0, 0x0, 0x36, 0x27},
{',', 0x0, 0x0, 0x0, 0x33},
{'.', 0x0, 0x0, 0x0, 0x34},
{':', 0x0, 0x0, 0x0, 0x27},
{'{', 0x0, 0x0, 0x36, 0x1a},
{'}', 0x0, 0x0, 0x36, 0x1b},
{'[', 0x0, 0x0, 0x0, 0x1a},
{']', 0x0, 0x0, 0x0, 0x1b},
{'*', 0x0, 0x0, 0x36, 0x09},
{'+', 0x0, 0x0, 0x0, 0x1b},
{'\\', 0x0, 0x0, 0x0, 0x2b},
{'|', 0x0, 0x0, 0x36, 0x2b},
{'^', 0x0, 0x0, 0x36, 0x07},
{'`', 0x0, 0x0, 0x0, 0x29},
{'<', 0x0, 0x0, 0x36, 0x33},
{'>', 0x0, 0x0, 0x36, 0x34},
{'\n', 0x0, 0x0, 0x0, 0x1c},
};
int getIndexForChar(char tc)
{
int i;
for (i = 0; i < keyboardMapSize; i++) {
if (tc == keyboardMap[i].tc) {
return i;
}
}
return -1;
}
int getIndexForExtChar(char tc, char ext1)
{
int i;
for (i = 0; i < extKeyboardMapSize; i++) {
if (tc == extKeyboardMap[i].tc &&
ext1 == extKeyboardMap[i].ext1) {
return i;
}
}
return -1;
}
int getIndexForExt2Char(char tc, char ext1, char ext2)
{
int i;
for (i = 0; i < extKeyboardMapSize; i++) {
if (tc == extKeyboardMap[i].tc &&
ext1 == extKeyboardMap[i].ext1 &&
ext2 == extKeyboardMap[i].ext2) {
return i;
}
}
return -1;
}
@implementation KeyboardView
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self setAutocapitalizationType:UITextAutocapitalizationTypeNone];
[self setAutocorrectionType:UITextAutocorrectionTypeNo];
[self setKeyboardType:UIKeyboardTypeASCIICapable];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
self.keyboardVisible = false;
}
NSString *language = [[NSLocale preferredLanguages] objectAtIndex:0];
if ([language isEqual: @"es-ES"]) {
keyboardMap = pc104_es;
keyboardMapSize = sizeof(pc104_es);
extKeyboardMap = pc104_es_ext;
extKeyboardMapSize = sizeof(pc104_es_ext);
} else {
keyboardMap = pc104_us;
keyboardMapSize = sizeof(pc104_us);
extKeyboardMap = pc104_us_ext;
extKeyboardMapSize = sizeof(pc104_us_ext);
}
return self;
}
-(void)insertText:(NSString *)text {
NSLog(@"insertText: %@, length=%lu", text, (unsigned long)text.length);
const char *ctext=[text UTF8String];
unsigned long ctext_len = strlen(ctext);
NSLog(@"ctext length=%lu\n", ctext_len);
unsigned char tc = ctext[0];
int keycode = 0;
int special = 0;
int prekey = 0;
int prekey_special = 0;
int is_upper = false;
int index = -1;
if (isalpha(tc)) {
if (isupper(tc)) {
tc = tolower(tc);
is_upper = true;
}
}
switch (ctext_len) {
case 1:
NSLog(@"char=%d\n", tc);
index = getIndexForChar(tc);
if (index != -1) {
keycode = keyboardMap[index].key;
special = keyboardMap[index].special_key;
}
break;
case 2:
NSLog(@"char=%d\n", tc);
NSLog(@"ext1=%d\n", (unsigned char) ctext[1]);
index = getIndexForExtChar(tc, ctext[1]);
if (index != -1) {
keycode = extKeyboardMap[index].key;
special = extKeyboardMap[index].special_key;
prekey = extKeyboardMap[index].prekey;
prekey_special = extKeyboardMap[index].special_prekey;
}
break;
case 3:
NSLog(@"char=%d\n", tc);
NSLog(@"ext1=%d\n", (unsigned char) ctext[1]);
NSLog(@"ext2=%d\n", (unsigned char) ctext[2]);
index = getIndexForExt2Char(tc, ctext[1], ctext[2]);
if (index != -1) {
keycode = extKeyboardMap[index].key;
special = extKeyboardMap[index].special_key;
prekey = extKeyboardMap[index].prekey;
prekey_special = extKeyboardMap[index].special_prekey;
}
break;
}
if (keycode) {
if (is_upper) {
special = 0x2A;
}
if (prekey) {
if (prekey_special) {
engine_spice_keyboard_event(prekey_special, 1);
}
engine_spice_keyboard_event(prekey, 1);
engine_spice_keyboard_event(prekey, 0);
if (prekey_special) {
engine_spice_keyboard_event(prekey_special, 0);
}
}
if (special) {
engine_spice_keyboard_event(special, 1);
}
engine_spice_keyboard_event(keycode, 1);
engine_spice_keyboard_event(keycode, 0);
if (special) {
engine_spice_keyboard_event(special, 0);
}
}
}
-(void)deleteBackward {
/* Unused since this was changed to use UITextView */
NSLog(@"DeleteKey");
engine_spice_keyboard_event(0x0E, 1);
engine_spice_keyboard_event(0x0E, 0);
}
-(BOOL)hasText {
NSLog(@"hasText");
return YES;
}
-(BOOL)canBecomeFirstResponder {
return YES;
}
- (void)keyboardWillShow:(NSNotification *)notification {
if (global_state.width > global_state.height) {
engine_set_keyboard_offset(0.2);
}
self.keyboardVisible = true;
}
- (void)keyboardWillHide:(NSNotification *)notification {
engine_set_keyboard_offset(0.0);
self.keyboardVisible = false;
}
@end