Skip to content

Commit

Permalink
Move keyboard handling to separate classes
Browse files Browse the repository at this point in the history
Encapsulate all the platform specific magic around keyboard in to
specific classes, in order to keep the core code more readable.
  • Loading branch information
CendioOssman committed Dec 4, 2024
1 parent 245c542 commit 0813780
Show file tree
Hide file tree
Showing 14 changed files with 1,609 additions and 1,294 deletions.
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 <[email protected]> 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 <[email protected]> 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

0 comments on commit 0813780

Please sign in to comment.