Skip to content

Instantly share code, notes, and snippets.

@jessicah
Last active August 29, 2015 14:07
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jessicah/86a230ffeba01ecd097c to your computer and use it in GitHub Desktop.
Save jessicah/86a230ffeba01ecd097c to your computer and use it in GitHub Desktop.
Initial reworking of keyboard support
From 11123cd57ad1a6628e93c6c8095bad490eac749e Mon Sep 17 00:00:00 2001
From: Jessica Hamilton <jessica.l.hamilton@gmail.com>
Date: Sun, 5 Oct 2014 01:39:07 +1300
Subject: [PATCH] synergy: redo keyboard support
---
.../devices/synergy/haiku-usynergy.cpp | 65 ++++++++--------------
.../input_server/devices/synergy/haiku-usynergy.h | 2 +-
.../input_server/devices/synergy/uSynergy.c | 21 +++----
.../input_server/devices/synergy/uSynergy.h | 2 +-
4 files changed, 35 insertions(+), 55 deletions(-)
diff --git a/src/add-ons/input_server/devices/synergy/haiku-usynergy.cpp b/src/add-ons/input_server/devices/synergy/haiku-usynergy.cpp
index 591df07..a158d30 100644
--- a/src/add-ons/input_server/devices/synergy/haiku-usynergy.cpp
+++ b/src/add-ons/input_server/devices/synergy/haiku-usynergy.cpp
@@ -29,6 +29,7 @@
#include <PathMonitor.h>
#include <Screen.h>
#include <TranslationUtils.h>
+#include <UnicodeChar.h>
#include <cstdlib>
#include <strings.h>
@@ -86,8 +87,8 @@ static void uScreenActive(_U* device, bool active) {
static void uMouseCallback(_U* device, uint16 x, uint16 y, int16 wheelX, int16 wheelY, bool buttonLeft, bool buttonRight, bool buttonMiddle) {
device->MouseCallback(x, y, wheelX, wheelY, buttonLeft, buttonRight, buttonMiddle);
}
-static void uKeyboardCallback(_U* device, uint16 key, uint16 modifiers, bool isKeyDown, bool isKeyRepeat) {
- device->KeyboardCallback(key, modifiers, isKeyDown, isKeyRepeat);
+static void uKeyboardCallback(_U* device, uint16 key, uint16 modifiers, uint16 scancode, bool isKeyDown, bool isKeyRepeat) {
+ device->KeyboardCallback(key, modifiers, scancode, isKeyDown, isKeyRepeat);
}
static void uJoystickCallback(_U* device, uint8_t joyNum, uint16_t buttons, int8_t leftStickX, int8_t leftStickY, int8_t rightStickX, int8_t rightStickY) {
device->JoystickCallback(joyNum, buttons, leftStickX, leftStickY, rightStickX, rightStickY);
@@ -502,7 +503,7 @@ uSynergyInputServerDevice::MouseCallback(uint16_t x, uint16_t y, int16_t wheelX,
void
-uSynergyInputServerDevice::KeyboardCallback(uint16_t scancode, uint16_t _modifiers, bool isKeyDown, bool isKeyRepeat)
+uSynergyInputServerDevice::KeyboardCallback(uint16_t key, uint16_t _modifiers, uint16_t scancode, bool isKeyDown, bool isKeyRepeat)
{
static uint32 lastScanCode = 0;
static uint32 repeatCount = 1;
@@ -510,22 +511,11 @@ uSynergyInputServerDevice::KeyboardCallback(uint16_t scancode, uint16_t _modifie
int64 timestamp = system_time();
- uint32_t keycode = 0;
- if (scancode > 0 && scancode < sizeof(kATKeycodeMap)/sizeof(uint32))
- keycode = kATKeycodeMap[scancode - 1];
- else {
- scancode = (uint8)(scancode | 0x80);
- if (scancode > 0 && scancode < sizeof(kATKeycodeMap)/sizeof(uint32))
- keycode = kATKeycodeMap[scancode - 1];
- }
-
- TRACE("synergy: scancode = 0x%02x, keycode = 0x%x\n", scancode, keycode);
-
- if (keycode < 256) {
+ if (key < 256) {
if (isKeyDown)
- states[(keycode) >> 3] |= (1 << (7 - (keycode & 0x7)));
+ states[(key) >> 3] |= (1 << (7 - (key & 0x7)));
else
- states[(keycode) >> 3] &= (!(1 << (7 - (keycode & 0x7))));
+ states[(key) >> 3] &= (!(1 << (7 - (key & 0x7))));
}
#if false
@@ -586,55 +576,44 @@ uSynergyInputServerDevice::KeyboardCallback(uint16_t scancode, uint16_t _modifie
}
}
- if (scancode == 0)
+ if (scancode == 0 || key == 0)
return;
BMessage* msg = new BMessage;
if (msg == NULL)
return;
- char* string = NULL;
- char* rawString = NULL;
- int32 numBytes = 0, rawNumBytes = 0;
- fKeymap.GetChars(keycode, fModifiers, 0, &string, &numBytes);
- fKeymap.GetChars(keycode, 0, 0, &rawString, &rawNumBytes);
+ char string[4];
+ char *end = string;
- if (numBytes > 0)
+ BUnicodeChar::ToUTF8(key, &end);
+
+ int32 numBytes = end - string;
+
+ if (numBytes > 0 && !(scancode >= 0xE000 && scancode <= 0xEFFF))
msg->what = isKeyDown ? B_KEY_DOWN : B_KEY_UP;
- else
+ else {
+ numBytes = 0;
msg->what = isKeyDown ? B_UNMAPPED_KEY_DOWN : B_UNMAPPED_KEY_UP;
+ }
msg->AddInt64("when", timestamp);
- msg->AddInt32("key", keycode);
+ msg->AddInt32("key", scancode);
msg->AddInt32("modifiers", fModifiers);
msg->AddData("states", B_UINT8_TYPE, states, 16);
if (numBytes > 0) {
- for (int i = 0; i < numBytes; i++) {
- TRACE("%02x:", (int8)string[i]);
+ for (int i = 0; i < numBytes; i++)
msg->AddInt8("byte", (int8)string[i]);
- }
- TRACE("\n");
msg->AddData("bytes", B_STRING_TYPE, string, numBytes + 1);
- if (rawNumBytes <= 0) {
- rawNumBytes = 1;
- delete[] rawString;
- rawString = string;
- } else
- delete[] string;
-
if (isKeyDown && isKeyRepeat) {
repeatCount++;
msg->AddInt32("be:key_repeat", repeatCount);
} else
repeatCount = 1;
- } else
- delete[] string;
-
- if (rawNumBytes > 0)
- msg->AddInt32("raw_char", (uint32)((uint8)rawString[0] & 0x7f));
+ } else {
+ // and what the hell do we do with the key now?
- delete[] rawString;
if (msg != NULL && EnqueueMessage(msg) != B_OK)
delete msg;
diff --git a/src/add-ons/input_server/devices/synergy/haiku-usynergy.h b/src/add-ons/input_server/devices/synergy/haiku-usynergy.h
index bfc8fe5..7511876 100644
--- a/src/add-ons/input_server/devices/synergy/haiku-usynergy.h
+++ b/src/add-ons/input_server/devices/synergy/haiku-usynergy.h
@@ -53,7 +53,7 @@ class uSynergyInputServerDevice : public BHandler, public BInputServerDevice {
void Trace(const char* text);
void ScreenActive(bool active);
void MouseCallback(uint16_t x, uint16_t y, int16_t wheelX, int16_t wheelY, bool buttonLeft, bool buttonRight, bool buttonMiddle);
- void KeyboardCallback(uint16_t key, uint16_t modifiers, bool isKeyDown, bool isKeyRepeat);
+ void KeyboardCallback(uint16_t key, uint16_t modifiers, uint16_t scancode, bool isKeyDown, bool isKeyRepeat);
void JoystickCallback(uint8_t joyNum, uint16_t buttons, int8_t leftStickX, int8_t leftStickY, int8_t rightStickX, int8_t rightStickY);
void ClipboardCallback(enum uSynergyClipboardFormat format, const uint8_t* data, uint32_t size);
diff --git a/src/add-ons/input_server/devices/synergy/uSynergy.c b/src/add-ons/input_server/devices/synergy/uSynergy.c
index d745258..d3d18f5 100644
--- a/src/add-ons/input_server/devices/synergy/uSynergy.c
+++ b/src/add-ons/input_server/devices/synergy/uSynergy.c
@@ -182,14 +182,14 @@ static void sSetDisconnected(uSynergyContext *context)
/**
@brief Send keyboard callback when a key has been pressed or released
**/
-static void sSendKeyboardCallback(uSynergyContext *context, uint16_t key, uint16_t modifiers, uSynergyBool down, uSynergyBool repeat)
+static void sSendKeyboardCallback(uSynergyContext *context, uint16_t key, uint16_t scancode, uint16_t modifiers, uSynergyBool down, uSynergyBool repeat)
{
// Skip if no callback is installed
if (context->m_keyboardCallback == 0L)
return;
// Send callback
- context->m_keyboardCallback(context->m_cookie, key, modifiers, down, repeat);
+ context->m_keyboardCallback(context->m_cookie, key, scancode, modifiers, down, repeat);
}
@@ -346,30 +346,31 @@ static void sProcessMessage(uSynergyContext *context, const uint8_t *message)
// Key down
// kMsgDKeyDown = "DKDN%2i%2i%2i"
// kMsgDKeyDown1_0 = "DKDN%2i%2i"
- //uint16_t id = sNetToNative16(message+8);
+ uint16_t key = sNetToNative16(message+8);
uint16_t mod = sNetToNative16(message+10);
- uint16_t key = sNetToNative16(message+12);
- sSendKeyboardCallback(context, key, mod, USYNERGY_TRUE, USYNERGY_FALSE);
+ uint16_t scancode = sNetToNative16(message+12);
+ sSendKeyboardCallback(context, key, mod, scancode, USYNERGY_TRUE, USYNERGY_FALSE);
}
else if (USYNERGY_IS_PACKET("DKRP"))
{
// Key repeat
// kMsgDKeyRepeat = "DKRP%2i%2i%2i%2i"
// kMsgDKeyRepeat1_0 = "DKRP%2i%2i%2i"
+ uint16_t key = sNetToNative16(message+8);
uint16_t mod = sNetToNative16(message+10);
// uint16_t count = sNetToNative16(message+12);
- uint16_t key = sNetToNative16(message+14);
- sSendKeyboardCallback(context, key, mod, USYNERGY_TRUE, USYNERGY_TRUE);
+ uint16_t scancode = sNetToNative16(message+14);
+ sSendKeyboardCallback(context, key, mod, scancode, USYNERGY_TRUE, USYNERGY_TRUE);
}
else if (USYNERGY_IS_PACKET("DKUP"))
{
// Key up
// kMsgDKeyUp = "DKUP%2i%2i%2i"
// kMsgDKeyUp1_0 = "DKUP%2i%2i"
- //uint16 id=Endian::sNetToNative(sbuf[4]);
+ uint16_t key = sNetToNative16(message+8);
uint16_t mod = sNetToNative16(message+10);
- uint16_t key = sNetToNative16(message+12);
- sSendKeyboardCallback(context, key, mod, USYNERGY_FALSE, USYNERGY_FALSE);
+ uint16_t scancode = sNetToNative16(message+12);
+ sSendKeyboardCallback(context, key, mod, scancode, USYNERGY_FALSE, USYNERGY_FALSE);
}
else if (USYNERGY_IS_PACKET("DGBT"))
{
diff --git a/src/add-ons/input_server/devices/synergy/uSynergy.h b/src/add-ons/input_server/devices/synergy/uSynergy.h
index b8d58dc..2bf77bf 100644
--- a/src/add-ons/input_server/devices/synergy/uSynergy.h
+++ b/src/add-ons/input_server/devices/synergy/uSynergy.h
@@ -272,7 +272,7 @@ This callback is called when a key is pressed or released.
@param down Down or up status, 1 is key is pressed down, 0 if key is released (up)
@param repeat Repeat flag, 1 if the key is down because the key is repeating, 0 if the key is initially pressed by the user
**/
-typedef void (*uSynergyKeyboardCallback)(uSynergyCookie cookie, uint16_t key, uint16_t modifiers, uSynergyBool down, uSynergyBool repeat);
+typedef void (*uSynergyKeyboardCallback)(uSynergyCookie cookie, uint16_t key, uint16_t scancode, uint16_t modifiers, uSynergyBool down, uSynergyBool repeat);
--
1.9.1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment