Remap meta key to left alt on FreeBSD console

This commit is contained in:
Markus Gans 2017-04-02 13:30:23 +02:00
parent bcfa7fb731
commit 0e5b1dfebd
6 changed files with 147 additions and 24 deletions

View File

@ -1,3 +1,9 @@
2017-04-02 Markus Gans <guru.mail@muenster.de>
* Remap the meta key to left alt key at runtime
on the FreeBSD console
(Console switching is still possible with
Ctrl-Alt-F1 through Ctrl-Alt-F8)
2017-03-30 Markus Gans <guru.mail@muenster.de> 2017-03-30 Markus Gans <guru.mail@muenster.de>
* Fixed bug: termcap "me" does not reset * Fixed bug: termcap "me" does not reset
the alternate character set the alternate character set

View File

@ -3,6 +3,23 @@
#CXX="clang++" #CXX="clang++"
PREFIX="/usr" PREFIX="/usr"
# Get number of logical processor cores
if command -v getconf >/dev/null 2>&1
then
CPU_COUNT="$(getconf _NPROCESSORS_ONLN 2>/dev/null || getconf NPROCESSORS_ONLN 2>/dev/null)" || CPU_COUNT="0"
fi
if [ "$CPU_COUNT" -eq 0 ]
then
if command -v nproc >/dev/null 2>&1
then
CPU_COUNT="$(nproc 2>/dev/null)" || CPU_COUNT="0"
fi
fi
test "$CPU_COUNT" -eq 0 && CPU_COUNT=1
# Build commands
case "$1" in case "$1" in
"--release"|"release") "--release"|"release")
./configure --prefix="$PREFIX" ./configure --prefix="$PREFIX"
@ -44,5 +61,7 @@ case "$1" in
;; ;;
esac esac
make V=1 -j10 JOBS="$((CPU_COUNT/2))"
test "$JOBS" -eq 0 && JOBS=1
make V=1 -j$JOBS
# make install # make install

View File

@ -116,6 +116,7 @@ fc::consoleCursorStyle FTerm::console_cursor_style;
unimapdesc FTerm::screen_unicode_map; unimapdesc FTerm::screen_unicode_map;
#endif #endif
uChar FTerm::bsd_alt_keymap = 0;
//---------------------------------------------------------------------- //----------------------------------------------------------------------
// class FTerm // class FTerm
@ -1575,7 +1576,7 @@ int FTerm::UTF8decode (const char utf8[])
// protected methods of FTerm // protected methods of FTerm
//---------------------------------------------------------------------- //----------------------------------------------------------------------
#if defined(__linux__) #if defined(__linux__)
void FTerm::init_consoleCharMap() void FTerm::initLinuxConsoleCharMap()
{ {
uInt c1, c2, c3, c4, c5; uInt c1, c2, c3, c4, c5;
@ -1744,11 +1745,8 @@ int FTerm::isLinuxConsole()
} }
#endif #endif
//----------------------------------------------------------------------
#if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
#include <sys/param.h>
#if defined(BSD) #if defined(BSD)
//----------------------------------------------------------------------
bool FTerm::isBSDConsole() bool FTerm::isBSDConsole()
{ {
keymap_t keymap; keymap_t keymap;
@ -1759,7 +1757,55 @@ bool FTerm::isBSDConsole()
return false; return false;
} }
#endif //----------------------------------------------------------------------
bool FTerm::saveBSDAltKey()
{
keymap_t keymap;
int ret;
static const int left_alt = 0x38;
ret = ioctl(0, GIO_KEYMAP, &keymap);
if ( ret < 0 )
return false;
// save current mapping
bsd_alt_keymap = keymap.key[left_alt].map[0];
return true;
}
//----------------------------------------------------------------------
bool FTerm::setBSDAltKey (uChar key)
{
keymap_t keymap;
int ret;
static const int left_alt = 0x38;
ret = ioctl(0, GIO_KEYMAP, &keymap);
if ( ret < 0 )
return false;
// map to meta key
keymap.key[left_alt].map[0] = key;
if ( (keymap.n_keys > 0) && (ioctl(0, PIO_KEYMAP, &keymap) < 0))
return false;
else
return true;
}
//----------------------------------------------------------------------
bool FTerm::setBSDAlt2Meta()
{
return setBSDAltKey (META);
}
//----------------------------------------------------------------------
bool FTerm::resetBSDAlt2Meta()
{
return setBSDAltKey(bsd_alt_keymap);
}
#endif #endif
#if defined(__linux__) #if defined(__linux__)
@ -2197,9 +2243,10 @@ int FTerm::setUnicodeMap (struct unimapdesc* unimap)
} }
//---------------------------------------------------------------------- //----------------------------------------------------------------------
void FTerm::init_console() void FTerm::initLinuxConsole()
{ {
// initialize terminal and Linux console // initialize Linux console
fd_tty = -1; fd_tty = -1;
screen_unicode_map.entries = 0; screen_unicode_map.entries = 0;
screen_font.data = 0; screen_font.data = 0;
@ -2223,6 +2270,23 @@ void FTerm::init_console()
} }
#endif #endif
#if defined(BSD)
//----------------------------------------------------------------------
void FTerm::initBSDConsole()
{
// initialize BSD console
if ( isBSDConsole() )
{
// save current left alt key mapping
saveBSDAltKey();
// map meta key to left alt key
setBSDAlt2Meta();
}
}
#endif
//---------------------------------------------------------------------- //----------------------------------------------------------------------
uInt FTerm::getBaudRate (const struct termios* termios_p) uInt FTerm::getBaudRate (const struct termios* termios_p)
{ {
@ -3044,8 +3108,6 @@ void FTerm::init_termcaps()
const_cast<char*>(CSI "29m"); const_cast<char*>(CSI "29m");
} }
#if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
#include <sys/param.h>
#if defined(BSD) #if defined(BSD)
if ( isBSDConsole() ) if ( isBSDConsole() )
{ {
@ -3057,9 +3119,14 @@ void FTerm::init_termcaps()
"q\304t\303u\264" "q\304t\303u\264"
"v\301w\302x\263" "v\301w\302x\263"
"y\363z\362~\371"); "y\363z\362~\371");
tcap[fc::t_set_attributes].string = \
const_cast<char*>(CSI "0%?%p1%p6%|"
"%t;1%;%?%p2%t;"
"4%;%?%p1%p3%|"
"%t;7%;%?%p4%t;"
"5%;m%?%p9%t\016%e\017%;");
FTermcap::attr_without_color = 18; FTermcap::attr_without_color = 18;
} }
#endif
#endif #endif
// read termcap key strings // read termcap key strings
@ -3341,8 +3408,13 @@ void FTerm::init()
term_name = ttyname(stdout_no); term_name = ttyname(stdout_no);
#if defined(__linux__) #if defined(__linux__)
// initialize terminal and Linux console // initialize Linux console
init_console(); initLinuxConsole();
#endif
#if defined(BSD)
// initialize BSD console
initBSDConsole();
#endif #endif
// save termios settings // save termios settings
@ -3682,6 +3754,10 @@ void FTerm::finish()
} }
#endif #endif
#if defined(BSD)
resetBSDAlt2Meta();
#endif
if ( kde_konsole ) if ( kde_konsole )
setKDECursor(fc::BlockCursor); setKDECursor(fc::BlockCursor);

View File

@ -254,8 +254,9 @@ class FTerm
protected: protected:
// Methods // Methods
#if defined(__linux__) #if defined(__linux__)
static void init_consoleCharMap(); static void initLinuxConsoleCharMap();
#endif #endif
static bool charEncodable (uInt); static bool charEncodable (uInt);
static uInt charEncode (uInt); static uInt charEncode (uInt);
static uInt charEncode (uInt, fc::encoding); static uInt charEncode (uInt, fc::encoding);
@ -318,7 +319,14 @@ class FTerm
#if defined(__linux__) #if defined(__linux__)
static int isLinuxConsole(); static int isLinuxConsole();
#endif #endif
#if defined(BSD)
static bool isBSDConsole(); static bool isBSDConsole();
static bool saveBSDAltKey();
static bool setBSDAltKey (uChar);
static bool setBSDAlt2Meta();
static bool resetBSDAlt2Meta();
#endif
// Methods // Methods
#if defined(__linux__) #if defined(__linux__)
@ -342,7 +350,11 @@ class FTerm
static int setScreenFont (uChar*, uInt, uInt, uInt, bool = false); static int setScreenFont (uChar*, uInt, uInt, uInt, bool = false);
static int setUnicodeMap (struct unimapdesc*); static int setUnicodeMap (struct unimapdesc*);
static int getUnicodeMap (); static int getUnicodeMap ();
static void init_console(); static void initLinuxConsole();
#endif
#if defined(BSD)
static void initBSDConsole();
#endif #endif
static uInt getBaudRate (const struct termios*); static uInt getBaudRate (const struct termios*);
@ -409,10 +421,10 @@ class FTerm
static bool resize_term; static bool resize_term;
static struct termios term_init; static struct termios term_init;
static fc::consoleCursorStyle console_cursor_style; static fc::consoleCursorStyle console_cursor_style;
static struct console_font_op screen_font; static struct console_font_op screen_font;
static struct unimapdesc screen_unicode_map; static struct unimapdesc screen_unicode_map;
static uChar bsd_alt_keymap;
static FOptiMove* opti_move; static FOptiMove* opti_move;
static FOptiAttr* opti_attr; static FOptiAttr* opti_attr;

View File

@ -1160,7 +1160,7 @@ void FWidget::show()
#if defined(__linux__) #if defined(__linux__)
// Important: Do not use setNewFont() or setVGAFont() after // Important: Do not use setNewFont() or setVGAFont() after
// the console character mapping has been initialized // the console character mapping has been initialized
init_consoleCharMap(); initLinuxConsoleCharMap();
#endif #endif
// set xterm underline cursor // set xterm underline cursor

View File

@ -1,6 +1,7 @@
// File: opti-move.cpp // File: opti-move.cpp
#include <iomanip> #include <iomanip>
#include <iostream>
#include "fapp.h" #include "fapp.h"
#include "ftermcap.h" #include "ftermcap.h"
#include "fvterm.h" #include "fvterm.h"
@ -51,18 +52,27 @@ void tcapString (std::string name, const char* cap_str)
for (uInt i=0; i < len; i++) for (uInt i=0; i < len; i++)
{ {
if ( cap_str[i] < 32 ) uChar c = cap_str[i];
if ( c < 32 )
{ {
if ( cap_str[i] == 27 ) if ( c == 27 )
sequence += "\\E"; sequence += "\\E";
else else
{ {
sequence += '^'; sequence += '^';
sequence += cap_str[i] + 64; sequence += c + 64;
} }
} }
else if ( c >= 127 )
{
std::ostringstream o;
o << std::oct << int(c);
sequence += "\\";
sequence += o.str();
}
else else
sequence += cap_str[i]; sequence += c;
} }
std::cout << sequence << " "; std::cout << sequence << " ";