Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move keyboard handling to separate classes #1850

Merged
merged 3 commits into from
Dec 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 87 additions & 0 deletions common/rfb/CConnection.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@
#include <rfb/CConnection.h>
#include <rfb/util.h>

#define XK_MISCELLANY
#define XK_XKB_KEYS
#include <rfb/keysymdef.h>

#include <rfb/LogWriter.h>

#include <rdr/InStream.h>
Expand Down Expand Up @@ -693,6 +697,89 @@ void CConnection::sendClipboardData(const char* data)
}
}

void CConnection::sendKeyPress(int systemKeyCode,
uint32_t keyCode, uint32_t keySym)
{
// For the first few years, there wasn't a good consensus on what the
// Windows keys should be mapped to for X11. So we need to help out a
// bit and map all variants to the same key...
switch (keySym) {
case XK_Hyper_L:
keySym = XK_Super_L;
break;
case XK_Hyper_R:
keySym = XK_Super_R;
break;
// There has been several variants for Shift-Tab over the years.
// RFB states that we should always send a normal tab.
case XK_ISO_Left_Tab:
keySym = XK_Tab;
break;
}

#ifdef __APPLE__
// Alt on OS X behaves more like AltGr on other systems, and to get
// sane behaviour we should translate things in that manner for the
// remote VNC server. However that means we lose the ability to use
// Alt as a shortcut modifier. Do what RealVNC does and hijack the
// left command key as an Alt replacement.
switch (keySym) {
case XK_Super_L:
keySym = XK_Alt_L;
break;
case XK_Super_R:
keySym = XK_Super_L;
break;
case XK_Alt_L:
keySym = XK_Mode_switch;
break;
case XK_Alt_R:
keySym = XK_ISO_Level3_Shift;
break;
}
#endif

// Because of the way keyboards work, we cannot expect to have the same
// symbol on release as when pressed. This breaks the VNC protocol however,
// so we need to keep track of what keysym a key _code_ generated on press
// and send the same on release.
downKeys[systemKeyCode].keyCode = keyCode;
downKeys[systemKeyCode].keySym = keySym;

vlog.debug("Key pressed: %d => 0x%02x / XK_%s (0x%04x)",
systemKeyCode, keyCode, KeySymName(keySym), keySym);

writer()->writeKeyEvent(keySym, keyCode, true);
}

void CConnection::sendKeyRelease(int systemKeyCode)
{
DownMap::iterator iter;

iter = downKeys.find(systemKeyCode);
if (iter == downKeys.end()) {
// These occur somewhat frequently so let's not spam them unless
// logging is turned up.
vlog.debug("Unexpected release of key code %d", systemKeyCode);
return;
}

vlog.debug("Key released: %d => 0x%02x / XK_%s (0x%04x)",
systemKeyCode, iter->second.keyCode,
KeySymName(iter->second.keySym), iter->second.keySym);

writer()->writeKeyEvent(iter->second.keySym,
iter->second.keyCode, false);

downKeys.erase(iter);
}

void CConnection::releaseAllKeys()
{
while (!downKeys.empty())
sendKeyRelease(downKeys.begin()->first);
}

void CConnection::refreshFramebuffer()
{
forceNonincremental = true;
Expand Down
20 changes: 20 additions & 0 deletions common/rfb/CConnection.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#ifndef __RFB_CCONNECTION_H__
#define __RFB_CCONNECTION_H__

#include <map>
#include <string>

#include <rfb/CMsgHandler.h>
Expand Down Expand Up @@ -196,6 +197,18 @@ namespace rfb {
// clipboard via handleClipboardRequest().
virtual void sendClipboardData(const char* data);

// sendKeyPress()/sendKeyRelease() send keyboard events to the
// server
void sendKeyPress(int systemKeyCode, uint32_t keyCode, uint32_t keySym);
void sendKeyRelease(int systemKeyCode);

// releaseAllKeys() sends keyboard release events to the server for
// all keys that are currently pressed down by this client,
// avoiding keys getting stuck. This can be useful if the client
// loses keyboard focus or otherwise no longer gets keyboard events
// from the system.
void releaseAllKeys();

// refreshFramebuffer() forces a complete refresh of the entire
// framebuffer
void refreshFramebuffer();
Expand Down Expand Up @@ -313,6 +326,13 @@ namespace rfb {
bool hasRemoteClipboard;
bool hasLocalClipboard;
bool unsolicitedClipboardAttempt;

struct DownKey {
uint32_t keyCode;
uint32_t keySym;
};
typedef std::map<int, DownKey> DownMap;
DownMap downKeys;
};
}
#endif
13 changes: 5 additions & 8 deletions vncviewer/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ add_executable(vncviewer
PlatformPixelBuffer.cxx
Viewport.cxx
parameters.cxx
keysym2ucs.c
touch.cxx
MonitorIndicesParameter.cxx
vncviewer.cxx)
Expand All @@ -37,17 +36,15 @@ endif()

if(WIN32)
target_sources(vncviewer PRIVATE Win32TouchHandler.cxx win32.c)
elseif(APPLE)
target_sources(vncviewer PRIVATE cocoa.mm osx_to_qnum.c)
else()
target_sources(vncviewer PRIVATE GestureHandler.cxx XInputTouchHandler.cxx xkb_to_qnum.c)
endif()

if(WIN32)
target_sources(vncviewer PRIVATE KeyboardWin32.cxx keysym2ucs.c)
target_sources(vncviewer PRIVATE Surface_Win32.cxx)
elseif(APPLE)
target_sources(vncviewer PRIVATE cocoa.mm)
target_sources(vncviewer PRIVATE KeyboardMacOS.mm osx_to_qnum.c keysym2ucs.c)
target_sources(vncviewer PRIVATE Surface_OSX.cxx)
else()
target_sources(vncviewer PRIVATE GestureHandler.cxx XInputTouchHandler.cxx)
target_sources(vncviewer PRIVATE KeyboardX11.cxx xkb_to_qnum.c)
target_sources(vncviewer PRIVATE Surface_X11.cxx)
endif()

Expand Down
49 changes: 49 additions & 0 deletions vncviewer/Keyboard.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/* Copyright 2011-2021 Pierre Ossman <ossman@cendio.se> for Cendio AB
*
* This 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.
*
* This software 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 this software; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
* USA.
*/

#ifndef __KEYBOARD_H__
#define __KEYBOARD_H__

#include <stdint.h>

class KeyboardHandler
{
public:
virtual void handleKeyPress(int systemKeyCode,
uint32_t keyCode, uint32_t keySym) = 0;
virtual void handleKeyRelease(int systemKeyCode) = 0;
};

class Keyboard
{
public:
Keyboard(KeyboardHandler* handler_) : handler(handler_) {};
virtual ~Keyboard() {};

virtual bool handleEvent(const void* event) = 0;

virtual void reset() {};

virtual unsigned getLEDState() = 0;
virtual void setLEDState(unsigned state) = 0;

protected:
KeyboardHandler* handler;
};

#endif
60 changes: 60 additions & 0 deletions vncviewer/KeyboardMacOS.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/* Copyright 2011-2021 Pierre Ossman <ossman@cendio.se> for Cendio AB
*
* This 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.
*
* This software 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 this software; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
* USA.
*/

#ifndef __KEYBOARDMACOS_H__
#define __KEYBOARDMACOS_H__

#include "Keyboard.h"

#ifdef __OBJC__
@class NSEvent;
@class NSString;
#else
class NSEvent;
class NSString;
#endif

class KeyboardMacOS : public Keyboard
{
public:
KeyboardMacOS(KeyboardHandler* handler);
virtual ~KeyboardMacOS();

bool handleEvent(const void* event) override;

unsigned getLEDState() override;
void setLEDState(unsigned state) override;

// Special helper on macOS
static bool isKeyboardSync(const void* event);

protected:
bool isKeyboardEvent(const NSEvent* nsevent);
bool isKeyPress(const NSEvent* nsevent);
uint32_t translateSystemKeyCode(int systemKeyCode);
unsigned getSystemKeyCode(const NSEvent* nsevent);

NSString* keyTranslate(unsigned keyCode, unsigned modifierFlags);
uint32_t translateEventKeysym(const NSEvent* nsevent);

int openHID(unsigned int* ioc);
int getModifierLockState(int modifier, bool* on);
int setModifierLockState(int modifier, bool on);
};

#endif
Loading