Made the keyboard device more resistant against any kind of error:

* Scancodes greater 255 are now handled correctly, and will no longer crash
  the input_server. This fixes bug #830.
* Use new (std:nothrow) instead of a simple new
* Now checks all allocations and appropriately handle failure
* Cleanup.


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@18694 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Axel Dörfler
2006-08-29 14:14:45 +00:00
parent 458b6f4b90
commit 304d592760
2 changed files with 214 additions and 176 deletions
@@ -13,6 +13,7 @@
#include <String.h> #include <String.h>
#include <errno.h> #include <errno.h>
#include <new>
#include <stdlib.h> #include <stdlib.h>
#include <unistd.h> #include <unistd.h>
@@ -327,7 +328,7 @@ extern "C"
BInputServerDevice * BInputServerDevice *
instantiate_input_device() instantiate_input_device()
{ {
return new KeyboardInputDevice(); return new (std::nothrow) KeyboardInputDevice();
} }
@@ -409,20 +410,19 @@ KeyboardInputDevice::InitFromSettings(void *cookie, uint32 opcode)
keyboard_device *device = (keyboard_device *)cookie; keyboard_device *device = (keyboard_device *)cookie;
if (opcode == 0 if (opcode == 0 || opcode == B_KEY_REPEAT_RATE_CHANGED) {
|| opcode == B_KEY_REPEAT_RATE_CHANGED) {
if (get_key_repeat_rate(&device->settings.key_repeat_rate) != B_OK) if (get_key_repeat_rate(&device->settings.key_repeat_rate) != B_OK)
LOG_ERR("error when get_key_repeat_rate\n"); LOG_ERR("error when get_key_repeat_rate\n");
else else if (ioctl(device->fd, KB_SET_KEY_REPEAT_RATE,
if (ioctl(device->fd, KB_SET_KEY_REPEAT_RATE, &device->settings.key_repeat_rate)!=B_OK) &device->settings.key_repeat_rate) != B_OK)
LOG_ERR("error when KB_SET_KEY_REPEAT_RATE, fd:%d\n", device->fd); LOG_ERR("error when KB_SET_KEY_REPEAT_RATE, fd:%d\n", device->fd);
} }
if (opcode == 0 || opcode == B_KEY_REPEAT_DELAY_CHANGED) { if (opcode == 0 || opcode == B_KEY_REPEAT_DELAY_CHANGED) {
if (get_key_repeat_delay(&device->settings.key_repeat_delay) != B_OK) if (get_key_repeat_delay(&device->settings.key_repeat_delay) != B_OK)
LOG_ERR("error when get_key_repeat_delay\n"); LOG_ERR("error when get_key_repeat_delay\n");
else if (ioctl(device->fd, KB_SET_KEY_REPEAT_DELAY, else if (ioctl(device->fd, KB_SET_KEY_REPEAT_DELAY,
&device->settings.key_repeat_delay)!=B_OK) &device->settings.key_repeat_delay) != B_OK)
LOG_ERR("error when KB_SET_KEY_REPEAT_DELAY, fd:%d\n", device->fd); LOG_ERR("error when KB_SET_KEY_REPEAT_DELAY, fd:%d\n", device->fd);
} }
@@ -566,8 +566,8 @@ status_t
KeyboardInputDevice::AddDevice(const char *path) KeyboardInputDevice::AddDevice(const char *path)
{ {
CALLED(); CALLED();
keyboard_device *device = new keyboard_device(path); keyboard_device *device = new (std::nothrow) keyboard_device(path);
if (!device) if (device == NULL)
return B_NO_MEMORY; return B_NO_MEMORY;
device->fd = -1; device->fd = -1;
@@ -634,6 +634,7 @@ KeyboardInputDevice::DeviceWatcher(void *arg)
bigtime_t timestamp = 0; bigtime_t timestamp = 0;
LOG("KB_READ :"); LOG("KB_READ :");
if (dev->isAT) { if (dev->isAT) {
at_kbd_io *at_kbd = (at_kbd_io *)buffer; at_kbd_io *at_kbd = (at_kbd_io *)buffer;
if (at_kbd->scancode > 0) if (at_kbd->scancode > 0)
@@ -653,27 +654,29 @@ KeyboardInputDevice::DeviceWatcher(void *arg)
LOG(" %Ld, %02x, %02lx\n", timestamp, isKeyDown, keycode); LOG(" %Ld, %02x, %02lx\n", timestamp, isKeyDown, keycode);
if (isKeyDown if (isKeyDown && keycode == 0x68) {
&& (keycode == 0x68) ) { // MENU KEY for OpenTracker 5.2.0+ // MENU KEY for OpenTracker 5.2.0+
bool nokey = true; bool noOtherKeyPressed = true;
for (int32 i = 0; i < 16; i++) { for (int32 i = 0; i < 16; i++) {
if (states[i] != 0) { if (states[i] != 0) {
nokey = false; noOtherKeyPressed = false;
break; break;
} }
} }
if (nokey) { if (noOtherKeyPressed) {
BMessenger msger("application/x-vnd.Be-TSKB"); BMessenger deskbar("application/x-vnd.Be-TSKB");
if (msger.IsValid()) if (deskbar.IsValid())
msger.SendMessage('BeMn'); deskbar.SendMessage('BeMn');
} }
} }
if (isKeyDown) if (keycode < 256) {
states[(keycode)>>3] |= (1 << (7 - (keycode & 0x7))); if (isKeyDown)
else states[(keycode) >> 3] |= (1 << (7 - (keycode & 0x7)));
states[(keycode)>>3] &= (!(1 << (7 - (keycode & 0x7)))); else
states[(keycode) >> 3] &= (!(1 << (7 - (keycode & 0x7))));
}
if (isKeyDown if (isKeyDown
&& keycode == 0x34 // DELETE KEY && keycode == 0x34 // DELETE KEY
@@ -682,24 +685,30 @@ KeyboardInputDevice::DeviceWatcher(void *arg)
LOG("TeamMonitor called\n"); LOG("TeamMonitor called\n");
// show the team monitor // show the team monitor
if (!dev->owner->fTMWindow) if (dev->owner->fTMWindow == NULL)
dev->owner->fTMWindow = new TMWindow(); dev->owner->fTMWindow = new (std::nothrow) TMWindow();
dev->owner->fTMWindow->Enable(); if (dev->owner->fTMWindow != NULL) {
dev->owner->fTMWindow->Enable();
// cancel timer only for R5 // cancel timer only for R5
if (ioctl(dev->fd, KB_CANCEL_CONTROL_ALT_DEL, NULL) == B_OK) if (ioctl(dev->fd, KB_CANCEL_CONTROL_ALT_DEL, NULL) == B_OK)
LOG("KB_CANCEL_CONTROL_ALT_DEL : OK\n"); LOG("KB_CANCEL_CONTROL_ALT_DEL : OK\n");
}
} }
uint32 modifiers = keymap->Modifier(keycode); uint32 modifiers = keymap->Modifier(keycode);
if (modifiers if (modifiers
&& (!(modifiers & (B_CAPS_LOCK | B_NUM_LOCK | B_SCROLL_LOCK)) && (!(modifiers & (B_CAPS_LOCK | B_NUM_LOCK | B_SCROLL_LOCK))
|| isKeyDown)) { || isKeyDown)) {
BMessage *msg = new BMessage; BMessage *msg = new BMessage;
if (msg == NULL)
continue;
msg->AddInt64("when", timestamp); msg->AddInt64("when", timestamp);
msg->what = B_MODIFIERS_CHANGED; msg->what = B_MODIFIERS_CHANGED;
msg->AddInt32("be:old_modifiers", dev->modifiers); msg->AddInt32("be:old_modifiers", dev->modifiers);
if ((isKeyDown && !(modifiers & (B_CAPS_LOCK | B_NUM_LOCK | B_SCROLL_LOCK))) if ((isKeyDown && !(modifiers & (B_CAPS_LOCK | B_NUM_LOCK | B_SCROLL_LOCK)))
|| (isKeyDown && !(dev->modifiers & modifiers))) || (isKeyDown && !(dev->modifiers & modifiers)))
dev->modifiers |= modifiers; dev->modifiers |= modifiers;
@@ -708,6 +717,7 @@ KeyboardInputDevice::DeviceWatcher(void *arg)
msg->AddInt32("modifiers", dev->modifiers); msg->AddInt32("modifiers", dev->modifiers);
msg->AddData("states", B_UINT8_TYPE, states, 16); msg->AddData("states", B_UINT8_TYPE, states, 16);
if (dev->owner->EnqueueMessage(msg)!=B_OK) if (dev->owner->EnqueueMessage(msg)!=B_OK)
delete msg; delete msg;
@@ -715,10 +725,7 @@ KeyboardInputDevice::DeviceWatcher(void *arg)
dev->owner->SetLeds(dev); dev->owner->SetLeds(dev);
} }
char *str = NULL, *str2 = NULL;
int32 numBytes = 0, numBytes2 = 0;
uint8 newDeadKey = 0; uint8 newDeadKey = 0;
if (activeDeadKey == 0) if (activeDeadKey == 0)
newDeadKey = keymap->IsDeadKey(keycode, dev->modifiers); newDeadKey = keymap->IsDeadKey(keycode, dev->modifiers);
@@ -732,10 +739,15 @@ KeyboardInputDevice::DeviceWatcher(void *arg)
// R5-like dead key behaviour // R5-like dead key behaviour
if (newDeadKey == 0) { if (newDeadKey == 0) {
char *str = NULL, *str2 = NULL;
int32 numBytes = 0, numBytes2 = 0;
keymap->GetChars(keycode, dev->modifiers, activeDeadKey, &str, &numBytes); keymap->GetChars(keycode, dev->modifiers, activeDeadKey, &str, &numBytes);
keymap->GetChars(keycode, 0, 0, &str2, &numBytes2); keymap->GetChars(keycode, 0, 0, &str2, &numBytes2);
BMessage *msg = new BMessage; BMessage *msg = new BMessage;
if (msg == NULL)
continue;
if (numBytes > 0) if (numBytes > 0)
msg->what = isKeyDown ? B_KEY_DOWN : B_KEY_UP; msg->what = isKeyDown ? B_KEY_DOWN : B_KEY_UP;
else else
@@ -768,6 +780,7 @@ KeyboardInputDevice::DeviceWatcher(void *arg)
if (numBytes2 > 0) if (numBytes2 > 0)
msg->AddInt32("raw_char", (uint32)((uint8)str2[0] & 0x7f)); msg->AddInt32("raw_char", (uint32)((uint8)str2[0] & 0x7f));
delete[] str2; delete[] str2;
if (dev->owner->EnqueueMessage(msg) != B_OK) if (dev->owner->EnqueueMessage(msg) != B_OK)
@@ -1,86 +1,91 @@
// ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ /*
// * Copyright 2004-2006, Haiku, Inc. All rights reserved.
// Copyright (c) 2004, Haiku * Distributed under the terms of the MIT License.
// *
// This software is part of the Haiku distribution and is covered * Authors:
// by the Haiku license. * Jérôme Duval
// */
//
// File: Keymap.h
// Author: Jérôme Duval
// Description: Keyboard input server addon
// Created : September 7, 2004
//
// ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
#include "Keymap.h" #include "Keymap.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ByteOrder.h> #include <ByteOrder.h>
#include <File.h> #include <File.h>
#include <new>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
static void static void
print_key( char *chars, int32 offset ) print_key(char *chars, int32 offset)
{ {
int size = chars[offset++]; int size = chars[offset++];
switch( size ) { switch (size) {
case 0: case 0:
// Not mapped // Not mapped
printf( "N/A" ); printf("N/A");
break; break;
case 1: case 1:
// 1-byte UTF-8/ASCII character // 1-byte UTF-8/ASCII character
printf( "%c", chars[offset] ); printf("%c", chars[offset]);
break; break;
default: default:
// 2-, 3-, or 4-byte UTF-8 character {
{ // 2-, 3-, or 4-byte UTF-8 character
char *str = new char[size + 1]; char *str = new (std::nothrow) char[size + 1];
strncpy( str, &(chars[offset]), size ); if (str == NULL)
str[size] = 0; break;
printf( "%s", str );
delete [] str; strncpy(str, &(chars[offset]), size);
} str[size] = 0;
break; printf("%s", str);
} delete [] str;
break;
printf( "\t" ); }
}
printf("\t");
} }
// #pragma mark -
void void
Keymap::DumpKeymap() Keymap::DumpKeymap()
{ {
// Print a chart of the normal, shift, option, and option+shift // Print a chart of the normal, shift, option, and option+shift
// keys. // keys.
printf( "Key #\tNormal\tShift\tCaps\tC+S\tOption\tO+S\tO+C\tO+C+S\tControl\n" ); printf("Key #\tNormal\tShift\tCaps\tC+S\tOption\tO+S\tO+C\tO+C+S\tControl\n");
for( int idx = 0; idx < 128; idx++ ) {
printf( " 0x%x\t", idx );
print_key( fChars, fKeys.normal_map[idx] );
print_key( fChars, fKeys.shift_map[idx] );
print_key( fChars, fKeys.caps_map[idx] );
print_key( fChars, fKeys.caps_shift_map[idx] );
print_key( fChars, fKeys.option_map[idx] );
print_key( fChars, fKeys.option_shift_map[idx] );
print_key( fChars, fKeys.option_caps_map[idx] );
print_key( fChars, fKeys.option_caps_shift_map[idx] );
print_key( fChars, fKeys.control_map[idx] );
printf( "\n" );
}
for (int i = 0; i < 128; i++) {
printf(" 0x%x\t", i);
print_key(fChars, fKeys.normal_map[i]);
print_key(fChars, fKeys.shift_map[i]);
print_key(fChars, fKeys.caps_map[i]);
print_key(fChars, fKeys.caps_shift_map[i]);
print_key(fChars, fKeys.option_map[i]);
print_key(fChars, fKeys.option_shift_map[i]);
print_key(fChars, fKeys.option_caps_map[i]);
print_key(fChars, fKeys.option_caps_shift_map[i]);
print_key(fChars, fKeys.control_map[i]);
printf("\n");
}
} }
Keymap::Keymap() : Keymap::Keymap()
:
fChars(NULL) fChars(NULL)
{ {
key_map *keys; key_map *keys;
get_key_map(&keys, &fChars); get_key_map(&keys, &fChars);
if (keys) { if (keys) {
memcpy(&fKeys, keys, sizeof(key_map)); memcpy(&fKeys, keys, sizeof(key_map));
free(keys); free(keys);
@@ -90,8 +95,7 @@ Keymap::Keymap() :
Keymap::~Keymap() Keymap::~Keymap()
{ {
if (fChars) free(fChars);
free(fChars);
} }
@@ -101,25 +105,22 @@ Keymap::~Keymap()
bool bool
Keymap::IsModifierKey(uint32 keyCode) Keymap::IsModifierKey(uint32 keyCode)
{ {
if ((keyCode == fKeys.caps_key) return keyCode == fKeys.caps_key
|| (keyCode == fKeys.num_key) || keyCode == fKeys.num_key
|| (keyCode == fKeys.scroll_key) || keyCode == fKeys.scroll_key
|| (keyCode == fKeys.left_shift_key) || keyCode == fKeys.left_shift_key
|| (keyCode == fKeys.right_shift_key) || keyCode == fKeys.right_shift_key
|| (keyCode == fKeys.left_command_key) || keyCode == fKeys.left_command_key
|| (keyCode == fKeys.right_command_key) || keyCode == fKeys.right_command_key
|| (keyCode == fKeys.left_control_key) || keyCode == fKeys.left_control_key
|| (keyCode == fKeys.right_control_key) || keyCode == fKeys.right_control_key
|| (keyCode == fKeys.left_option_key) || keyCode == fKeys.left_option_key
|| (keyCode == fKeys.right_option_key) || keyCode == fKeys.right_option_key
|| (keyCode == fKeys.menu_key)) || keyCode == fKeys.menu_key;
return true;
return false;
} }
/* we need to know a modifier for a key //! We need to know a modifier for a key
*/
uint32 uint32
Keymap::Modifier(uint32 keyCode) Keymap::Modifier(uint32 keyCode)
{ {
@@ -147,14 +148,18 @@ Keymap::Modifier(uint32 keyCode)
return B_RIGHT_OPTION_KEY | B_OPTION_KEY; return B_RIGHT_OPTION_KEY | B_OPTION_KEY;
if (keyCode == fKeys.menu_key) if (keyCode == fKeys.menu_key)
return B_MENU_KEY; return B_MENU_KEY;
return 0; return 0;
} }
// tell if a key is a dead key, needed for draw a dead key //! Tell if a key is a dead key, needed for draw a dead key
uint8 uint8
Keymap::IsDeadKey(uint32 keyCode, uint32 modifiers) Keymap::IsDeadKey(uint32 keyCode, uint32 modifiers)
{ {
if (keyCode >= 256)
return 0;
int32 offset; int32 offset;
uint32 tableMask = 0; uint32 tableMask = 0;
@@ -169,18 +174,18 @@ Keymap::IsDeadKey(uint32 keyCode, uint32 modifiers)
case B_CONTROL_KEY: offset = fKeys.control_map[keyCode]; tableMask = B_CONTROL_TABLE; break; case B_CONTROL_KEY: offset = fKeys.control_map[keyCode]; tableMask = B_CONTROL_TABLE; break;
default: offset = fKeys.normal_map[keyCode]; tableMask = B_NORMAL_TABLE; break; default: offset = fKeys.normal_map[keyCode]; tableMask = B_NORMAL_TABLE; break;
} }
if (offset<=0) if (offset <= 0)
return 0; return 0;
uint32 numBytes = fChars[offset]; uint32 numBytes = fChars[offset];
if (!numBytes) if (!numBytes)
return 0; return 0;
char chars[4]; char chars[4];
strncpy(chars, &(fChars[offset+1]), numBytes ); strncpy(chars, &(fChars[offset+1]), numBytes );
chars[numBytes] = 0; chars[numBytes] = 0;
int32 deadOffsets[] = { int32 deadOffsets[] = {
fKeys.acute_dead_key[1], fKeys.acute_dead_key[1],
fKeys.grave_dead_key[1], fKeys.grave_dead_key[1],
@@ -188,7 +193,7 @@ Keymap::IsDeadKey(uint32 keyCode, uint32 modifiers)
fKeys.dieresis_dead_key[1], fKeys.dieresis_dead_key[1],
fKeys.tilde_dead_key[1] fKeys.tilde_dead_key[1]
}; };
uint32 deadTables[] = { uint32 deadTables[] = {
fKeys.acute_tables, fKeys.acute_tables,
fKeys.grave_tables, fKeys.grave_tables,
@@ -196,36 +201,34 @@ Keymap::IsDeadKey(uint32 keyCode, uint32 modifiers)
fKeys.dieresis_tables, fKeys.dieresis_tables,
fKeys.tilde_tables fKeys.tilde_tables
}; };
for (int32 i=0; i<5; i++) { for (int32 i = 0; i < 5; i++) {
if ((deadTables[i] & tableMask) == 0) if ((deadTables[i] & tableMask) == 0)
continue; continue;
if (offset == deadOffsets[i]) if (offset == deadOffsets[i])
return i+1; return i + 1;
uint32 deadNumBytes = fChars[deadOffsets[i]]; uint32 deadNumBytes = fChars[deadOffsets[i]];
if (!deadNumBytes) if (!deadNumBytes)
continue; continue;
if (strncmp(chars, &(fChars[deadOffsets[i]+1]), deadNumBytes ) == 0) { if (!strncmp(chars, &(fChars[deadOffsets[i] + 1]), deadNumBytes))
return i+1; return i + 1;
}
} }
return 0; return 0;
} }
// tell if a key is a dead second key, needed for draw a dead second key //! Tell if a key is a dead second key, needed for draw a dead second key
bool bool
Keymap::IsDeadSecondKey(uint32 keyCode, uint32 modifiers, uint8 activeDeadKey) Keymap::IsDeadSecondKey(uint32 keyCode, uint32 modifiers, uint8 activeDeadKey)
{ {
if (!activeDeadKey) if (!activeDeadKey || keyCode >= 256)
return false; return false;
int32 offset; int32 offset;
switch (modifiers & 0xcf) { switch (modifiers & 0xcf) {
case B_SHIFT_KEY: offset = fKeys.shift_map[keyCode]; break; case B_SHIFT_KEY: offset = fKeys.shift_map[keyCode]; break;
case B_CAPS_LOCK: offset = fKeys.caps_map[keyCode]; break; case B_CAPS_LOCK: offset = fKeys.caps_map[keyCode]; break;
@@ -240,11 +243,11 @@ Keymap::IsDeadSecondKey(uint32 keyCode, uint32 modifiers, uint8 activeDeadKey)
if (offset <= 0) if (offset <= 0)
return false; return false;
uint32 numBytes = fChars[offset]; uint32 numBytes = fChars[offset];
if (!numBytes) if (!numBytes)
return false; return false;
int32* deadOffsets[] = { int32* deadOffsets[] = {
fKeys.acute_dead_key, fKeys.acute_dead_key,
fKeys.grave_dead_key, fKeys.grave_dead_key,
@@ -252,37 +255,40 @@ Keymap::IsDeadSecondKey(uint32 keyCode, uint32 modifiers, uint8 activeDeadKey)
fKeys.dieresis_dead_key, fKeys.dieresis_dead_key,
fKeys.tilde_dead_key fKeys.tilde_dead_key
}; };
int32 *deadOffset = deadOffsets[activeDeadKey-1]; int32 *deadOffset = deadOffsets[activeDeadKey-1];
for (int32 i=0; i<32; i++) { for (int32 i=0; i<32; i++) {
if (offset == deadOffset[i]) if (offset == deadOffset[i])
return true; return true;
uint32 deadNumBytes = fChars[deadOffset[i]]; uint32 deadNumBytes = fChars[deadOffset[i]];
if (!deadNumBytes) if (!deadNumBytes)
continue; continue;
if (strncmp(&(fChars[offset+1]), &(fChars[deadOffset[i]+1]), deadNumBytes ) == 0) if (!strncmp(&(fChars[offset+1]), &(fChars[deadOffset[i]+1]), deadNumBytes))
return true; return true;
i++; i++;
} }
return false; return false;
} }
// get the char for a key given modifiers and active dead key //! Get the char for a key given modifiers and active dead key
void void
Keymap::GetChars(uint32 keyCode, uint32 modifiers, uint8 activeDeadKey, char** chars, int32* numBytes) Keymap::GetChars(uint32 keyCode, uint32 modifiers, uint8 activeDeadKey,
char** chars, int32* numBytes)
{ {
int32 offset;
*numBytes = 0; *numBytes = 0;
*chars = NULL; *chars = NULL;
if (keyCode >= 256)
return;
// here we take NUMLOCK into account // here we take NUMLOCK into account
if (modifiers & B_NUM_LOCK) if (modifiers & B_NUM_LOCK) {
switch (keyCode) { switch (keyCode) {
case 0x37: case 0x37:
case 0x38: case 0x38:
@@ -296,7 +302,11 @@ Keymap::GetChars(uint32 keyCode, uint32 modifiers, uint8 activeDeadKey, char** c
case 0x64: case 0x64:
case 0x65: case 0x65:
modifiers ^= B_SHIFT_KEY; modifiers ^= B_SHIFT_KEY;
} break;
}
}
int32 offset;
// here we choose the right map given the modifiers // here we choose the right map given the modifiers
switch (modifiers & 0xcf) { switch (modifiers & 0xcf) {
@@ -308,30 +318,37 @@ Keymap::GetChars(uint32 keyCode, uint32 modifiers, uint8 activeDeadKey, char** c
case B_OPTION_KEY|B_CAPS_LOCK: offset = fKeys.option_caps_map[keyCode]; break; case B_OPTION_KEY|B_CAPS_LOCK: offset = fKeys.option_caps_map[keyCode]; break;
case B_OPTION_KEY|B_SHIFT_KEY|B_CAPS_LOCK: offset = fKeys.option_caps_shift_map[keyCode]; break; case B_OPTION_KEY|B_SHIFT_KEY|B_CAPS_LOCK: offset = fKeys.option_caps_shift_map[keyCode]; break;
case B_CONTROL_KEY: offset = fKeys.control_map[keyCode]; break; case B_CONTROL_KEY: offset = fKeys.control_map[keyCode]; break;
default: offset = fKeys.normal_map[keyCode]; break;
default:
offset = fKeys.normal_map[keyCode];
break;
} }
if (offset <= 0) if (offset <= 0)
return; return;
// here we get the char size // here we get the char size
*numBytes = fChars[offset]; *numBytes = fChars[offset];
if (!*numBytes) if (!*numBytes)
return; return;
// here we take an potential active dead key // here we take an potential active dead key
int32 *dead_key; int32 *deadKey;
switch(activeDeadKey) { switch (activeDeadKey) {
case 1: dead_key = fKeys.acute_dead_key; break; case 1: deadKey = fKeys.acute_dead_key; break;
case 2: dead_key = fKeys.grave_dead_key; break; case 2: deadKey = fKeys.grave_dead_key; break;
case 3: dead_key = fKeys.circumflex_dead_key; break; case 3: deadKey = fKeys.circumflex_dead_key; break;
case 4: dead_key = fKeys.dieresis_dead_key; break; case 4: deadKey = fKeys.dieresis_dead_key; break;
case 5: dead_key = fKeys.tilde_dead_key; break; case 5: deadKey = fKeys.tilde_dead_key; break;
default: default:
{ {
// if not dead, we copy and return the char // if not dead, we copy and return the char
char *str = *chars = new char[*numBytes + 1]; char *str = *chars = new (std::nothrow) char[*numBytes + 1];
strncpy(str, &(fChars[offset+1]), *numBytes ); if (str == NULL) {
*numBytes = 0;
return;
}
strncpy(str, &(fChars[offset + 1]), *numBytes);
str[*numBytes] = 0; str[*numBytes] = 0;
return; return;
} }
@@ -339,23 +356,27 @@ Keymap::GetChars(uint32 keyCode, uint32 modifiers, uint8 activeDeadKey, char** c
// if dead key, we search for our current offset char in the dead key offset table // if dead key, we search for our current offset char in the dead key offset table
// string comparison is needed // string comparison is needed
for (int32 i=0; i<32; i++) { for (int32 i = 0; i < 32; i++) {
if (strncmp(&(fChars[offset+1]), &(fChars[dead_key[i]+1]), *numBytes ) == 0) { if (strncmp(&(fChars[offset + 1]), &(fChars[deadKey[i] + 1]), *numBytes) == 0) {
*numBytes = fChars[dead_key[i+1]]; *numBytes = fChars[deadKey[i + 1]];
switch( *numBytes ) { switch (*numBytes) {
case 0: case 0:
// Not mapped // Not mapped
*chars = NULL; *chars = NULL;
break; break;
default: default:
// 1-, 2-, 3-, or 4-byte UTF-8 character {
{ // 1-, 2-, 3-, or 4-byte UTF-8 character
char *str = *chars = new char[*numBytes + 1]; char *str = *chars = new (std::nothrow) char[*numBytes + 1];
strncpy(str, &fChars[dead_key[i+1]+1], *numBytes ); if (str == NULL) {
str[*numBytes] = 0; *numBytes = 0;
} return;
break; }
strncpy(str, &fChars[deadKey[i + 1] + 1], *numBytes);
str[*numBytes] = 0;
break;
}
} }
return; return;
} }
@@ -363,10 +384,14 @@ Keymap::GetChars(uint32 keyCode, uint32 modifiers, uint8 activeDeadKey, char** c
} }
// if not found we return the current char mapped // if not found we return the current char mapped
*chars = new char[*numBytes + 1]; *chars = new (std::nothrow) char[*numBytes + 1];
if (*chars == NULL) {
*numBytes = 0;
return;
}
strncpy(*chars, &(fChars[offset+1]), *numBytes ); strncpy(*chars, &(fChars[offset+1]), *numBytes );
(*chars)[*numBytes] = 0; (*chars)[*numBytes] = 0;
} }
@@ -374,9 +399,9 @@ status_t
Keymap::LoadCurrent() Keymap::LoadCurrent()
{ {
key_map *keys = NULL; key_map *keys = NULL;
if (fChars) free(fChars);
free(fChars);
fChars = NULL; fChars = NULL;
get_key_map(&keys, &fChars); get_key_map(&keys, &fChars);
if (!keys) { if (!keys) {
fprintf(stderr, "error while getting current keymap!\n"); fprintf(stderr, "error while getting current keymap!\n");