1999-09-27 18:44:28 +00:00
|
|
|
/* This file is part of
|
1999-11-15 12:01:38 +00:00
|
|
|
* ======================================================
|
1999-09-27 18:44:28 +00:00
|
|
|
*
|
1999-12-13 00:05:34 +00:00
|
|
|
* LyX, The Document Processor
|
1999-09-27 18:44:28 +00:00
|
|
|
*
|
1999-12-13 00:05:34 +00:00
|
|
|
* Copyright 1995 Matthias Ettrich
|
2001-05-30 13:53:44 +00:00
|
|
|
* Copyright 1995-2001 The LyX Team.
|
1999-09-27 18:44:28 +00:00
|
|
|
*
|
1999-11-15 12:01:38 +00:00
|
|
|
* ====================================================== */
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
|
|
#include <config.h>
|
2000-02-04 09:38:32 +00:00
|
|
|
|
2001-05-31 02:23:46 +00:00
|
|
|
//#include <cstring>
|
2000-01-21 17:41:57 +00:00
|
|
|
#include <X11/Xlib.h>
|
|
|
|
|
1999-09-27 18:44:28 +00:00
|
|
|
#ifdef __GNUG__
|
|
|
|
#pragma implementation
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "kbmap.h"
|
2000-02-04 09:38:32 +00:00
|
|
|
#include "kbsequence.h"
|
1999-10-07 18:44:17 +00:00
|
|
|
#include "debug.h"
|
1999-09-27 18:44:28 +00:00
|
|
|
|
2000-03-28 02:18:55 +00:00
|
|
|
using std::endl;
|
|
|
|
|
1999-09-27 18:44:28 +00:00
|
|
|
// The only modifiers that we handle. We want to throw away things
|
|
|
|
// like NumLock.
|
2000-02-04 09:38:32 +00:00
|
|
|
enum { ModsMask = ShiftMask | ControlMask | Mod1Mask };
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
|
|
|
1999-11-15 12:01:38 +00:00
|
|
|
// === static functions ===================================================
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
|
|
|
|
|
|
/* ---F+------------------------------------------------------------------ *\
|
|
|
|
Function : printKeysym
|
|
|
|
Called by : kb_sequence::print and printKeyMap. RVDK_PATCH_5
|
|
|
|
Purpose : prints a keysym, including modifiers.
|
|
|
|
Parameters: key - keysym
|
|
|
|
mod - modifiers
|
|
|
|
buf - string where the result goes
|
|
|
|
maxlen - length of string (including '\0')
|
|
|
|
Returns : length of printed string if ok, 0 otherwise.
|
|
|
|
\* ---F------------------------------------------------------------------- */
|
2000-02-04 09:38:32 +00:00
|
|
|
void printKeysym(unsigned int key, unsigned int mod, string & buf)
|
1999-09-27 18:44:28 +00:00
|
|
|
{
|
|
|
|
mod &= ModsMask;
|
|
|
|
|
2000-11-21 15:46:13 +00:00
|
|
|
char const * const s = XKeysymToString(key);
|
1999-09-27 18:44:28 +00:00
|
|
|
|
1999-12-16 06:43:25 +00:00
|
|
|
if (mod & ShiftMask) buf += "S-";
|
|
|
|
if (mod & ControlMask) buf += "C-";
|
|
|
|
if (mod & Mod1Mask) buf += "M-";
|
|
|
|
if (s) buf += s;
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* ---F+------------------------------------------------------------------ *\
|
|
|
|
Function : printKeyTab
|
|
|
|
Called by : kb_keymap::print
|
|
|
|
Purpose : print the keysyms found in the given key table. RVDK_PATCH_5
|
|
|
|
Parameters: tabPt - keytable pointer
|
|
|
|
buf - string where the result goes
|
|
|
|
maxLen - length of string (including '\0')
|
|
|
|
Returns : length of printed string.
|
|
|
|
\* ---F------------------------------------------------------------------- */
|
|
|
|
|
2000-02-04 09:38:32 +00:00
|
|
|
void kb_keymap::printKey(kb_key const & key, string & buf)
|
1999-09-27 18:44:28 +00:00
|
|
|
{
|
2000-02-04 09:38:32 +00:00
|
|
|
printKeysym(key.code, key.mod & 0xffff, buf);
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// This binds a key to an action
|
2000-09-14 17:53:12 +00:00
|
|
|
int kb_keymap::bind(string const & seq, int action)
|
1999-09-27 18:44:28 +00:00
|
|
|
{
|
2000-12-17 06:09:35 +00:00
|
|
|
if (lyxerr.debugging(Debug::KBMAP)) {
|
|
|
|
lyxerr << "BIND: Sequence `"
|
|
|
|
<< seq << "' Action `"
|
|
|
|
<< action << "'" << endl;
|
|
|
|
}
|
|
|
|
|
1999-09-27 18:44:28 +00:00
|
|
|
kb_sequence k;
|
|
|
|
|
2000-11-21 15:46:13 +00:00
|
|
|
int const res = k.parse(seq);
|
1999-09-27 18:44:28 +00:00
|
|
|
if (!res) {
|
|
|
|
defkey(&k, action);
|
|
|
|
} else
|
1999-10-07 18:44:17 +00:00
|
|
|
lyxerr[Debug::KBMAP] << "Parse error at position " << res
|
|
|
|
<< " in key sequence '" << seq << "'."
|
|
|
|
<< endl;
|
1999-09-27 18:44:28 +00:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* ---F+------------------------------------------------------------------ *\
|
|
|
|
Function : kb_keymap::lookup
|
|
|
|
Called by : [user], kb_sequence::add()
|
|
|
|
Purpose : look up a key press in a given keymap
|
|
|
|
Parameters: key - the keysym of the key press
|
|
|
|
mod - the modifier mask of the keypress
|
|
|
|
seq - the key-sequence retrieved so far
|
|
|
|
Returns : user defined action; 0 for prefix key, -1 if key not found
|
|
|
|
\* ---F------------------------------------------------------------------- */
|
|
|
|
|
2000-02-04 09:38:32 +00:00
|
|
|
int kb_keymap::lookup(unsigned int key,
|
|
|
|
unsigned int mod, kb_sequence * seq) const
|
1999-09-27 18:44:28 +00:00
|
|
|
{
|
2000-02-04 09:38:32 +00:00
|
|
|
if (table.empty()) {
|
1999-09-27 18:44:28 +00:00
|
|
|
seq->curmap = seq->stdmap;
|
|
|
|
seq->delseq();
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2000-11-21 15:46:13 +00:00
|
|
|
//unsigned int msk1, msk0;
|
2000-02-04 09:38:32 +00:00
|
|
|
//suppress modifier bits we do not handle
|
|
|
|
mod &= ModsMask;
|
1999-09-27 18:44:28 +00:00
|
|
|
|
2000-02-04 09:38:32 +00:00
|
|
|
for (Table::const_iterator cit = table.begin();
|
|
|
|
cit != table.end(); ++cit) {
|
2000-11-21 15:46:13 +00:00
|
|
|
unsigned int const msk1 = (*cit).mod & 0xffff;
|
|
|
|
unsigned int const msk0 = ((*cit).mod >> 16) & 0xffff;
|
2000-02-04 09:38:32 +00:00
|
|
|
if ((*cit).code == key && (mod & ~msk0) == msk1) {
|
|
|
|
// math found:
|
2001-03-07 16:18:05 +00:00
|
|
|
if ((*cit).table.get()) {
|
1999-12-16 06:43:25 +00:00
|
|
|
// this is a prefix key - set new map
|
2001-03-07 16:18:05 +00:00
|
|
|
seq->curmap = (*cit).table.get();
|
1999-09-27 18:44:28 +00:00
|
|
|
return 0;
|
|
|
|
} else {
|
1999-12-16 06:43:25 +00:00
|
|
|
// final key - reset map
|
1999-09-27 18:44:28 +00:00
|
|
|
seq->curmap = seq->stdmap;
|
|
|
|
seq->delseq();
|
2000-02-04 09:38:32 +00:00
|
|
|
return (*cit).action;
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2000-02-04 09:38:32 +00:00
|
|
|
|
1999-09-27 18:44:28 +00:00
|
|
|
// error - key not found:
|
|
|
|
seq->curmap = seq->stdmap;
|
|
|
|
seq->delseq();
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* ---F+------------------------------------------------------------------ *\
|
|
|
|
Function : kb_keymap::print
|
|
|
|
Called by : [user]
|
|
|
|
Purpose : Prints all the available keysyms. RVDK_PATCH_5
|
|
|
|
Parameters: buf - string where output goes.
|
|
|
|
maxLen - available length in string, including `\0'.
|
|
|
|
Returns : updated maxLen.
|
|
|
|
\* ---F------------------------------------------------------------------- */
|
|
|
|
|
1999-12-16 06:43:25 +00:00
|
|
|
void kb_keymap::print(string & buf) const
|
1999-09-27 18:44:28 +00:00
|
|
|
{
|
2000-02-04 09:38:32 +00:00
|
|
|
for (Table::const_iterator cit = table.begin();
|
|
|
|
cit != table.end(); ++cit) {
|
|
|
|
printKey((*cit), buf);
|
|
|
|
buf += ' ';
|
|
|
|
}
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* ---F+------------------------------------------------------------------ *\
|
|
|
|
Function : kb_keymap::defkey
|
|
|
|
Called by : [user]
|
|
|
|
Purpose : define an action for a key sequence
|
|
|
|
Parameters: seq - the key sequence
|
|
|
|
action - the action to be defined
|
|
|
|
idx - recursion depth
|
|
|
|
Returns : 0 if ok.
|
|
|
|
\* ---F------------------------------------------------------------------- */
|
|
|
|
|
1999-12-16 06:43:25 +00:00
|
|
|
int kb_keymap::defkey(kb_sequence * seq, int action, int idx /*= 0*/)
|
1999-09-27 18:44:28 +00:00
|
|
|
{
|
2000-11-21 15:46:13 +00:00
|
|
|
unsigned int const code = seq->sequence[idx];
|
2000-11-04 10:00:12 +00:00
|
|
|
if (code == NoSymbol) return -1;
|
1999-09-27 18:44:28 +00:00
|
|
|
|
2000-11-21 15:46:13 +00:00
|
|
|
unsigned int const modmsk = seq->modifiers[idx];
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
|
|
// --- check if key is already there --------------------------------
|
2000-08-03 21:17:52 +00:00
|
|
|
if (table.size() != 0) { // without this I get strange crashes
|
|
|
|
Table::iterator end = table.end();
|
|
|
|
for (Table::iterator it = table.begin(); it != end; ++it) {
|
2000-02-04 09:38:32 +00:00
|
|
|
if (code == (*it).code && modmsk == (*it).mod) {
|
|
|
|
// overwrite binding
|
|
|
|
if (idx + 1 == seq->length) {
|
1999-12-16 06:43:25 +00:00
|
|
|
string buf;
|
|
|
|
seq->print(buf, true);
|
2000-08-14 15:31:16 +00:00
|
|
|
lyxerr[Debug::KBMAP]
|
1999-10-07 18:44:17 +00:00
|
|
|
<< "Warning: New binding for '"
|
2000-02-04 09:38:32 +00:00
|
|
|
<< buf
|
1999-10-07 18:44:17 +00:00
|
|
|
<< "' is overriding old binding..."
|
|
|
|
<< endl;
|
2001-03-07 16:18:05 +00:00
|
|
|
if ((*it).table.get()) {
|
|
|
|
(*it).table.reset(0);
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
2000-02-04 09:38:32 +00:00
|
|
|
(*it).action = action;
|
1999-09-27 18:44:28 +00:00
|
|
|
return 0;
|
2001-03-07 16:18:05 +00:00
|
|
|
} else if (!(*it).table.get()) {
|
1999-12-16 06:43:25 +00:00
|
|
|
string buf;
|
|
|
|
seq->print(buf, true);
|
1999-10-07 18:44:17 +00:00
|
|
|
lyxerr << "Error: New binding for '" << buf
|
|
|
|
<< "' is overriding old binding..."
|
|
|
|
<< endl;
|
1999-09-27 18:44:28 +00:00
|
|
|
return -1;
|
2000-02-04 09:38:32 +00:00
|
|
|
} else {
|
|
|
|
return (*it).table->defkey(seq, action,
|
|
|
|
idx + 1);
|
|
|
|
}
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
|
|
|
}
|
2000-08-03 21:17:52 +00:00
|
|
|
}
|
|
|
|
|
2000-02-04 09:38:32 +00:00
|
|
|
Table::iterator newone = table.insert(table.end(), kb_key());
|
|
|
|
(*newone).code = code;
|
|
|
|
(*newone).mod = modmsk;
|
|
|
|
if (idx + 1 == seq->length) {
|
|
|
|
(*newone).action = action;
|
2001-03-07 16:18:05 +00:00
|
|
|
(*newone).table.reset(0);
|
1999-09-27 18:44:28 +00:00
|
|
|
return 0;
|
|
|
|
} else {
|
2001-03-07 16:18:05 +00:00
|
|
|
(*newone).table.reset(new kb_keymap);
|
2000-02-04 09:38:32 +00:00
|
|
|
return (*newone).table->defkey(seq, action, idx + 1);
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-09-14 17:53:12 +00:00
|
|
|
string const kb_keymap::keyname(kb_key const & k)
|
1999-12-16 06:43:25 +00:00
|
|
|
{
|
|
|
|
string buf;
|
|
|
|
printKeysym(k.code, k.mod, buf);
|
1999-09-27 18:44:28 +00:00
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
1999-12-16 06:43:25 +00:00
|
|
|
|
1999-09-27 18:44:28 +00:00
|
|
|
// Finds a key for a keyaction, if possible
|
2001-05-17 15:11:01 +00:00
|
|
|
string const kb_keymap::findbinding(int act, string const & prefix) const
|
1999-12-16 06:43:25 +00:00
|
|
|
{
|
1999-10-02 16:21:10 +00:00
|
|
|
string res;
|
2000-02-04 09:38:32 +00:00
|
|
|
if (table.empty()) return res;
|
1999-09-27 18:44:28 +00:00
|
|
|
|
2000-08-03 21:17:52 +00:00
|
|
|
Table::const_iterator end = table.end();
|
2000-10-11 21:06:43 +00:00
|
|
|
for (Table::const_iterator cit = table.begin();
|
2000-08-03 21:17:52 +00:00
|
|
|
cit != end; ++cit) {
|
2001-03-07 16:18:05 +00:00
|
|
|
if ((*cit).table.get()) {
|
2001-05-17 15:11:01 +00:00
|
|
|
res += (*cit).table->findbinding(act,
|
|
|
|
prefix
|
|
|
|
+ keyname((*cit))
|
|
|
|
+ " ");
|
2000-02-04 09:38:32 +00:00
|
|
|
} else if ((*cit).action == act) {
|
2000-10-11 21:06:43 +00:00
|
|
|
res += "[";
|
2001-05-17 15:11:01 +00:00
|
|
|
res += prefix + keyname((*cit));
|
2000-10-11 21:06:43 +00:00
|
|
|
res += "] ";
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* === End of File: kbmap.C ============================================== */
|