2018-07-15 19:52:59 +02:00
|
|
|
/***********************************************************************
|
|
|
|
* fkeyboard.h - Read keyboard events *
|
|
|
|
* *
|
|
|
|
* This file is part of the Final Cut widget toolkit *
|
|
|
|
* *
|
|
|
|
* Copyright 2018 Markus Gans *
|
|
|
|
* *
|
|
|
|
* The Final Cut is free software; you can redistribute it and/or *
|
|
|
|
* modify it under the terms of the GNU Lesser General Public License *
|
|
|
|
* as published by the Free Software Foundation; either version 3 of *
|
|
|
|
* the License, or (at your option) any later version. *
|
|
|
|
* *
|
|
|
|
* The Final Cut 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 Lesser General Public License for more details. *
|
|
|
|
* *
|
|
|
|
* You should have received a copy of the GNU Lesser General Public *
|
|
|
|
* License along with this program. If not, see *
|
|
|
|
* <http://www.gnu.org/licenses/>. *
|
|
|
|
***********************************************************************/
|
|
|
|
|
|
|
|
/* Standalone class
|
|
|
|
* ════════════════
|
|
|
|
*
|
|
|
|
* ▕▔▔▔▔▔▔▔▔▔▔▔▏
|
|
|
|
* ▕ FKeyboard ▏
|
|
|
|
* ▕▁▁▁▁▁▁▁▁▁▁▁▏
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef FKEYBOARD_H
|
|
|
|
#define FKEYBOARD_H
|
|
|
|
|
|
|
|
#if !defined (USE_FINAL_H) && !defined (COMPILE_FINAL_CUT)
|
|
|
|
#error "Only <final/final.h> can be included directly."
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "final/fobject.h"
|
|
|
|
#include "final/ftypes.h"
|
|
|
|
|
|
|
|
#if defined(__linux__)
|
|
|
|
#include "final/ftermlinux.h"
|
|
|
|
#endif
|
|
|
|
|
2018-09-20 23:59:01 +02:00
|
|
|
namespace finalcut
|
|
|
|
{
|
|
|
|
|
2018-07-15 19:52:59 +02:00
|
|
|
// class forward declaration
|
|
|
|
class FApplication;
|
|
|
|
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
// class FKeyboardCommand
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
|
|
|
|
#pragma pack(push)
|
|
|
|
#pragma pack(1)
|
|
|
|
|
|
|
|
class FKeyboardCommand
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
// Constructor
|
2018-10-24 00:16:45 +02:00
|
|
|
explicit FKeyboardCommand (FApplication* = 0, void(FApplication::*)() = 0);
|
2018-07-15 19:52:59 +02:00
|
|
|
|
|
|
|
// Method
|
|
|
|
void execute();
|
|
|
|
|
|
|
|
private:
|
|
|
|
// Data Members
|
2018-12-03 03:22:36 +01:00
|
|
|
FApplication* instance{0};
|
|
|
|
void (FApplication::*handler)(){0};
|
2018-07-15 19:52:59 +02:00
|
|
|
};
|
|
|
|
#pragma pack(pop)
|
|
|
|
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
// class FKeyboard
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
|
|
|
|
#pragma pack(push)
|
|
|
|
#pragma pack(1)
|
|
|
|
|
|
|
|
class FKeyboard
|
|
|
|
{
|
|
|
|
public:
|
2018-10-11 03:46:37 +02:00
|
|
|
// Constants
|
2018-12-03 03:22:36 +01:00
|
|
|
static const std::size_t FIFO_BUF_SIZE{512};
|
2018-10-11 03:46:37 +02:00
|
|
|
|
|
|
|
// Typedef
|
|
|
|
typedef char keybuffer[FIFO_BUF_SIZE];
|
|
|
|
|
2018-07-15 19:52:59 +02:00
|
|
|
// Constructor
|
|
|
|
FKeyboard();
|
|
|
|
|
|
|
|
// Destructor
|
2018-07-22 23:07:49 +02:00
|
|
|
virtual ~FKeyboard();
|
2018-07-15 19:52:59 +02:00
|
|
|
|
|
|
|
// Accessors
|
2018-07-22 23:07:49 +02:00
|
|
|
virtual const char* getClassName() const;
|
2018-11-21 20:07:08 +01:00
|
|
|
FKey getKey();
|
|
|
|
const FString getKeyName (FKey);
|
2018-10-11 03:46:37 +02:00
|
|
|
keybuffer& getKeyBuffer();
|
2018-07-22 23:07:49 +02:00
|
|
|
timeval* getKeyPressedTime();
|
2018-07-15 19:52:59 +02:00
|
|
|
|
|
|
|
// Mutators
|
2018-07-22 23:07:49 +02:00
|
|
|
void setTermcapMap (fc::fkeymap*);
|
|
|
|
void setKeypressTimeout (const long);
|
|
|
|
void enableUTF8();
|
|
|
|
void disableUTF8();
|
|
|
|
void enableMouseSequences();
|
|
|
|
void disableMouseSequences();
|
2018-07-15 19:52:59 +02:00
|
|
|
|
|
|
|
#if defined(__linux__)
|
2018-07-22 23:07:49 +02:00
|
|
|
void setFTermLinux (FTermLinux*);
|
2018-07-15 19:52:59 +02:00
|
|
|
#endif
|
|
|
|
|
2018-07-22 23:07:49 +02:00
|
|
|
void setPressCommand (FKeyboardCommand);
|
|
|
|
void setReleaseCommand (FKeyboardCommand);
|
|
|
|
void setEscPressedCommand (FKeyboardCommand);
|
2018-07-15 19:52:59 +02:00
|
|
|
|
|
|
|
// Inquiry
|
2018-07-22 23:07:49 +02:00
|
|
|
bool isInputDataPending();
|
2018-07-15 19:52:59 +02:00
|
|
|
|
|
|
|
// Methods
|
2018-07-22 23:07:49 +02:00
|
|
|
bool& unprocessedInput();
|
|
|
|
bool isKeyPressed();
|
|
|
|
void clearKeyBuffer();
|
|
|
|
void clearKeyBufferOnTimeout();
|
|
|
|
void fetchKeyCode();
|
|
|
|
void escapeKeyHandling();
|
2018-07-15 19:52:59 +02:00
|
|
|
|
|
|
|
private:
|
|
|
|
// Constants
|
2018-12-03 03:22:36 +01:00
|
|
|
static const std::size_t READ_BUF_SIZE{1024};
|
2018-11-21 20:07:08 +01:00
|
|
|
static const FKey NOT_SET = static_cast<FKey>(-1);
|
2018-07-15 19:52:59 +02:00
|
|
|
|
|
|
|
// Disable copy constructor
|
|
|
|
FKeyboard (const FKeyboard&);
|
|
|
|
|
|
|
|
// Disable assignment operator (=)
|
|
|
|
FKeyboard& operator = (const FKeyboard&);
|
|
|
|
|
|
|
|
// Accessors
|
2018-11-21 20:07:08 +01:00
|
|
|
FKey getMouseProtocolKey();
|
|
|
|
FKey getTermcapKey();
|
|
|
|
FKey getMetaKey();
|
|
|
|
FKey getSingleKey();
|
2018-07-15 19:52:59 +02:00
|
|
|
|
|
|
|
// Mutators
|
2018-07-22 23:07:49 +02:00
|
|
|
bool setNonBlockingInput (bool);
|
|
|
|
bool setNonBlockingInput();
|
|
|
|
bool unsetNonBlockingInput();
|
2018-07-15 19:52:59 +02:00
|
|
|
|
|
|
|
// Inquiry
|
2018-07-22 23:07:49 +02:00
|
|
|
static bool isKeypressTimeout();
|
2018-07-15 19:52:59 +02:00
|
|
|
|
|
|
|
// Methods
|
2018-11-21 20:07:08 +01:00
|
|
|
FKey UTF8decode (const char[]);
|
2018-07-22 23:07:49 +02:00
|
|
|
ssize_t readKey();
|
|
|
|
void parseKeyBuffer();
|
2018-11-21 20:07:08 +01:00
|
|
|
FKey parseKeyString();
|
|
|
|
FKey keyCorrection (const FKey&);
|
2018-07-29 23:49:11 +02:00
|
|
|
void substringKeyHandling();
|
2018-07-22 23:07:49 +02:00
|
|
|
void keyPressed();
|
|
|
|
void keyReleased();
|
|
|
|
void escapeKeyPressed();
|
2018-07-15 19:52:59 +02:00
|
|
|
|
|
|
|
// Data Members
|
2018-12-03 03:22:36 +01:00
|
|
|
FKey key{0};
|
|
|
|
char read_buf[READ_BUF_SIZE]{};
|
|
|
|
char fifo_buf[FIFO_BUF_SIZE]{};
|
|
|
|
int fifo_offset{0};
|
|
|
|
bool fifo_in_use{false};
|
|
|
|
int stdin_status_flags{0};
|
2018-07-29 23:49:11 +02:00
|
|
|
static long key_timeout;
|
2018-12-03 03:22:36 +01:00
|
|
|
bool input_data_pending{false};
|
|
|
|
bool utf8_input{false};
|
|
|
|
bool mouse_support{true};
|
|
|
|
bool non_blocking_stdin{false};
|
|
|
|
FKeyboardCommand keypressed_cmd{};
|
|
|
|
FKeyboardCommand keyreleased_cmd{};
|
|
|
|
FKeyboardCommand escape_key_cmd{};
|
2018-07-29 23:49:11 +02:00
|
|
|
|
|
|
|
static timeval time_keypressed;
|
2018-12-03 03:22:36 +01:00
|
|
|
fc::fkeymap* key_map{0};
|
2018-07-15 19:52:59 +02:00
|
|
|
|
|
|
|
#if defined(__linux__)
|
|
|
|
#undef linux
|
2018-07-29 23:49:11 +02:00
|
|
|
static FTermLinux* linux;
|
2018-07-15 19:52:59 +02:00
|
|
|
#endif
|
|
|
|
};
|
|
|
|
#pragma pack(pop)
|
|
|
|
|
|
|
|
// FKeyboard inline functions
|
2018-07-22 23:07:49 +02:00
|
|
|
//----------------------------------------------------------------------
|
|
|
|
inline const char* FKeyboard::getClassName() const
|
|
|
|
{ return "FKeyboard"; }
|
|
|
|
|
2018-07-15 19:52:59 +02:00
|
|
|
//----------------------------------------------------------------------
|
2018-11-21 20:07:08 +01:00
|
|
|
inline FKey FKeyboard::getKey()
|
2018-07-15 19:52:59 +02:00
|
|
|
{ return key; }
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
2018-10-11 03:46:37 +02:00
|
|
|
inline FKeyboard::keybuffer& FKeyboard::getKeyBuffer()
|
|
|
|
{ return fifo_buf; }
|
2018-07-15 19:52:59 +02:00
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
2018-07-22 23:07:49 +02:00
|
|
|
inline timeval* FKeyboard::getKeyPressedTime()
|
2018-07-15 19:52:59 +02:00
|
|
|
{ return &time_keypressed; }
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
inline void FKeyboard::setKeypressTimeout (const long timeout)
|
|
|
|
{ key_timeout = timeout; }
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
inline void FKeyboard::enableUTF8()
|
|
|
|
{ utf8_input = true; }
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
inline void FKeyboard::disableUTF8()
|
|
|
|
{ utf8_input = false; }
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
inline void FKeyboard::enableMouseSequences()
|
|
|
|
{ mouse_support = true; }
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
inline void FKeyboard::disableMouseSequences()
|
|
|
|
{ mouse_support = false; }
|
|
|
|
|
|
|
|
#if defined(__linux__)
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
inline void FKeyboard::setFTermLinux (FTermLinux* linux_obj)
|
|
|
|
{ linux = linux_obj; }
|
|
|
|
#endif
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
inline void FKeyboard::setPressCommand (FKeyboardCommand cmd)
|
|
|
|
{ keypressed_cmd = cmd; }
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
inline void FKeyboard::setReleaseCommand (FKeyboardCommand cmd)
|
|
|
|
{ keyreleased_cmd = cmd; }
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
inline void FKeyboard::setEscPressedCommand (FKeyboardCommand cmd)
|
|
|
|
{ escape_key_cmd = cmd; }
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
inline bool FKeyboard::isInputDataPending()
|
|
|
|
{ return input_data_pending; }
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
inline bool FKeyboard::setNonBlockingInput()
|
|
|
|
{ return setNonBlockingInput(true); }
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
inline bool FKeyboard::unsetNonBlockingInput()
|
|
|
|
{ return setNonBlockingInput(false); }
|
|
|
|
|
2018-09-20 23:59:01 +02:00
|
|
|
} // namespace finalcut
|
|
|
|
|
2018-07-15 19:52:59 +02:00
|
|
|
#endif // FKEYBOARD_H
|