pivariant, enum, tiny format

This commit is contained in:
2022-12-13 21:44:06 +03:00
parent 9d9357b0ca
commit c74ba871cd
15 changed files with 1914 additions and 851 deletions

View File

@@ -16,13 +16,14 @@
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/>.
*/
#include "piincludes_p.h"
#include "pikbdlistener.h"
#include "piincludes_p.h"
#ifndef WINDOWS
# include <termios.h>
#else
# include <wingdi.h>
# include <wincon.h>
# include <wingdi.h>
#endif
/** \class PIKbdListener
@@ -47,80 +48,82 @@ PIKbdListener * PIKbdListener::_object = 0;
#ifndef WINDOWS
// unix
const PIKbdListener::EscSeq PIKbdListener::esc_seq[] = {
{"OA", PIKbdListener::UpArrow, 0, 0 , 1 },
{"OA", PIKbdListener::UpArrow, 4, 0 , 0 },
{"[1A", PIKbdListener::UpArrow, 0, 0 , 0 },
{"[A", PIKbdListener::UpArrow, 0, vt_all , 0 },
{"OB", PIKbdListener::DownArrow, 0, 0 , 1 },
{"OB", PIKbdListener::DownArrow, 4, 0 , 0 },
{"[1B", PIKbdListener::DownArrow, 0, 0 , 0 },
{"[B", PIKbdListener::DownArrow, 0, vt_all , 0 },
{"OC", PIKbdListener::RightArrow, 0, 0 , 1 },
{"OC", PIKbdListener::RightArrow, 4, 0 , 0 },
{"[1C", PIKbdListener::RightArrow, 0, 0 , 0 },
{"[C", PIKbdListener::RightArrow, 0, vt_all , 0 },
{"OD", PIKbdListener::LeftArrow, 0, 0 , 1 },
{"OD", PIKbdListener::LeftArrow, 4, 0 , 0 },
{"[1D", PIKbdListener::LeftArrow, 0, 0 , 0 },
{"[D", PIKbdListener::LeftArrow, 0, vt_all , 0 },
{"[H", PIKbdListener::Home, 0, vt_xterm , 0 },
{"[1H", PIKbdListener::Home, 0, 0 , 0 },
{"OH", PIKbdListener::Home, 0, 0 , 1 },
{"[1~", PIKbdListener::Home, 0, vt_linux , 0 },
{"[F", PIKbdListener::End, 0, vt_xterm , 0 },
{"[1F", PIKbdListener::End, 0, 0 , 0 },
{"OF", PIKbdListener::End, 0, 0 , 1 },
{"[4~", PIKbdListener::End, 0, vt_linux , 0 },
{"[2~", PIKbdListener::Insert, 0, vt_all , 0 },
{"[3~", PIKbdListener::Delete, 0, vt_all , 0 },
{"[5~", PIKbdListener::PageUp, 0, vt_all , 0 },
{"[6~", PIKbdListener::PageDown, 0, vt_all , 0 },
{"[Z", PIKbdListener::Tab, 1, vt_xterm , 0 },
{"OP", PIKbdListener::F1, 0, vt_xterm , 0 },
{"[[A", PIKbdListener::F1, 0, vt_linux , 0 },
{"[11~", PIKbdListener::F1, 0, 0 , 0 },
{"[25~", PIKbdListener::F1, 1, vt_linux , 0 },
{"OQ", PIKbdListener::F2, 0, vt_xterm , 0 },
{"[[B", PIKbdListener::F2, 0, vt_linux , 0 },
{"[12~", PIKbdListener::F2, 0, 0 , 0 },
{"[26~", PIKbdListener::F2, 1, vt_linux , 0 },
{"OR", PIKbdListener::F3, 0, vt_xterm , 0 },
{"[[C", PIKbdListener::F3, 0, vt_linux , 0 },
{"[13~", PIKbdListener::F3, 0, 0 , 0 },
{"[28~", PIKbdListener::F3, 1, vt_linux , 0 },
{"OS", PIKbdListener::F4, 0, vt_xterm , 0 },
{"[[D", PIKbdListener::F4, 0, vt_linux , 0 },
{"[14~", PIKbdListener::F4, 0, 0 , 0 },
{"[29~", PIKbdListener::F4, 1, vt_linux , 0 },
{"[[E", PIKbdListener::F5, 0, vt_linux , 0 },
{"OT", PIKbdListener::F5, 0, 0 , 0 },
{"[15~", PIKbdListener::F5, 0, vt_xterm , 0 },
{"[31~", PIKbdListener::F5, 1, vt_linux , 0 },
{"OU", PIKbdListener::F6, 0, 0 , 0 },
{"[17~", PIKbdListener::F6, 0, vt_all , 0 },
{"[32~", PIKbdListener::F6, 1, vt_linux , 0 },
{"OV", PIKbdListener::F7, 0, 0 , 0 },
{"[18~", PIKbdListener::F7, 0, vt_all , 0 },
{"[33~", PIKbdListener::F7, 1, vt_linux , 0 },
{"OW", PIKbdListener::F8, 0, 0 , 0 },
{"[19~", PIKbdListener::F8, 0, vt_all , 0 },
{"[34~", PIKbdListener::F8, 1, vt_linux , 0 },
{"OX", PIKbdListener::F9, 0, 0 , 0 },
{"[20~", PIKbdListener::F9, 0, vt_all , 0 },
{"[35~", PIKbdListener::F9, 1, vt_linux , 0 },
{"OY", PIKbdListener::F10, 0, 0 , 0 },
{"[21~", PIKbdListener::F10, 0, vt_all , 0 },
{"[36~", PIKbdListener::F10, 1, vt_linux , 0 },
{"OZ", PIKbdListener::F11, 0, 0 , 0 },
{"[23~", PIKbdListener::F11, 0, vt_all , 0 },
{"O[", PIKbdListener::F12, 0, 0 , 0 },
{"[24~", PIKbdListener::F12, 0, vt_all , 0 },
// End
{0, 0, 0, 0, 0},
{"OA", PIKbdListener::UpArrow, 0, 0, 1},
{"OA", PIKbdListener::UpArrow, 4, 0, 0},
{"[1A", PIKbdListener::UpArrow, 0, 0, 0},
{"[A", PIKbdListener::UpArrow, 0, vt_all, 0},
{"OB", PIKbdListener::DownArrow, 0, 0, 1},
{"OB", PIKbdListener::DownArrow, 4, 0, 0},
{"[1B", PIKbdListener::DownArrow, 0, 0, 0},
{"[B", PIKbdListener::DownArrow, 0, vt_all, 0},
{"OC", PIKbdListener::RightArrow, 0, 0, 1},
{"OC", PIKbdListener::RightArrow, 4, 0, 0},
{"[1C", PIKbdListener::RightArrow, 0, 0, 0},
{"[C", PIKbdListener::RightArrow, 0, vt_all, 0},
{"OD", PIKbdListener::LeftArrow, 0, 0, 1},
{"OD", PIKbdListener::LeftArrow, 4, 0, 0},
{"[1D", PIKbdListener::LeftArrow, 0, 0, 0},
{"[D", PIKbdListener::LeftArrow, 0, vt_all, 0},
{"[H", PIKbdListener::Home, 0, vt_xterm, 0},
{"[1H", PIKbdListener::Home, 0, 0, 0},
{"OH", PIKbdListener::Home, 0, 0, 1},
{"[1~", PIKbdListener::Home, 0, vt_linux, 0},
{"[F", PIKbdListener::End, 0, vt_xterm, 0},
{"[1F", PIKbdListener::End, 0, 0, 0},
{"OF", PIKbdListener::End, 0, 0, 1},
{"[4~", PIKbdListener::End, 0, vt_linux, 0},
{"[2~", PIKbdListener::Insert, 0, vt_all, 0},
{"[3~", PIKbdListener::Delete, 0, vt_all, 0},
{"[5~", PIKbdListener::PageUp, 0, vt_all, 0},
{"[6~", PIKbdListener::PageDown, 0, vt_all, 0},
{"[Z", PIKbdListener::Tab, 1, vt_xterm, 0},
{"OP", PIKbdListener::F1, 0, vt_xterm, 0},
{"[[A", PIKbdListener::F1, 0, vt_linux, 0},
{"[11~", PIKbdListener::F1, 0, 0, 0},
{"[25~", PIKbdListener::F1, 1, vt_linux, 0},
{"OQ", PIKbdListener::F2, 0, vt_xterm, 0},
{"[[B", PIKbdListener::F2, 0, vt_linux, 0},
{"[12~", PIKbdListener::F2, 0, 0, 0},
{"[26~", PIKbdListener::F2, 1, vt_linux, 0},
{"OR", PIKbdListener::F3, 0, vt_xterm, 0},
{"[[C", PIKbdListener::F3, 0, vt_linux, 0},
{"[13~", PIKbdListener::F3, 0, 0, 0},
{"[28~", PIKbdListener::F3, 1, vt_linux, 0},
{"OS", PIKbdListener::F4, 0, vt_xterm, 0},
{"[[D", PIKbdListener::F4, 0, vt_linux, 0},
{"[14~", PIKbdListener::F4, 0, 0, 0},
{"[29~", PIKbdListener::F4, 1, vt_linux, 0},
{"[[E", PIKbdListener::F5, 0, vt_linux, 0},
{"OT", PIKbdListener::F5, 0, 0, 0},
{"[15~", PIKbdListener::F5, 0, vt_xterm, 0},
{"[31~", PIKbdListener::F5, 1, vt_linux, 0},
{"OU", PIKbdListener::F6, 0, 0, 0},
{"[17~", PIKbdListener::F6, 0, vt_all, 0},
{"[32~", PIKbdListener::F6, 1, vt_linux, 0},
{"OV", PIKbdListener::F7, 0, 0, 0},
{"[18~", PIKbdListener::F7, 0, vt_all, 0},
{"[33~", PIKbdListener::F7, 1, vt_linux, 0},
{"OW", PIKbdListener::F8, 0, 0, 0},
{"[19~", PIKbdListener::F8, 0, vt_all, 0},
{"[34~", PIKbdListener::F8, 1, vt_linux, 0},
{"OX", PIKbdListener::F9, 0, 0, 0},
{"[20~", PIKbdListener::F9, 0, vt_all, 0},
{"[35~", PIKbdListener::F9, 1, vt_linux, 0},
{"OY", PIKbdListener::F10, 0, 0, 0},
{"[21~", PIKbdListener::F10, 0, vt_all, 0},
{"[36~", PIKbdListener::F10, 1, vt_linux, 0},
{"OZ", PIKbdListener::F11, 0, 0, 0},
{"[23~", PIKbdListener::F11, 0, vt_all, 0},
{"O[", PIKbdListener::F12, 0, 0, 0},
{"[24~", PIKbdListener::F12, 0, vt_all, 0},
// End
{0, 0, 0, 0, 0},
};
void setupTerminal(bool on) {
printf("\e[?1000"); printf(on ? "h" : "l");
printf("\e[?1002"); printf(on ? "h" : "l");
printf("\e[?1000");
printf(on ? "h" : "l");
printf("\e[?1002");
printf(on ? "h" : "l");
fflush(0);
}
#endif
@@ -128,7 +131,7 @@ void setupTerminal(bool on) {
PRIVATE_DEFINITION_START(PIKbdListener)
#ifdef WINDOWS
void * hIn, * hOut;
void *hIn, *hOut;
DWORD smode, tmode;
CONSOLE_SCREEN_BUFFER_INFO sbi;
#else
@@ -139,7 +142,7 @@ PRIVATE_DEFINITION_START(PIKbdListener)
#else
int
#endif
ret;
ret;
PRIVATE_DEFINITION_END(PIKbdListener)
@@ -147,16 +150,16 @@ PIKbdListener::PIKbdListener(KBFunc slot, void * _d, bool startNow): PIThread()
setName("keyboard_listener");
_object = this;
#ifdef WINDOWS
PRIVATE->hIn = GetStdHandle(STD_INPUT_HANDLE);
PRIVATE->hIn = GetStdHandle(STD_INPUT_HANDLE);
PRIVATE->hOut = GetStdHandle(STD_OUTPUT_HANDLE);
GetConsoleMode(PRIVATE->hIn, &PRIVATE->smode);
#else
tcgetattr(0, &PRIVATE->sterm);
#endif
is_active = true;
ret_func = slot;
kbddata_ = _d;
dbl_interval = 400;
is_active = true;
ret_func = slot;
kbddata_ = _d;
dbl_interval = 400;
PIKbdListener::exiting = exit_enabled = false;
if (startNow) start();
}
@@ -191,9 +194,9 @@ PIKbdListener::KeyModifiers getModifiers(DWORD v, bool * shift = 0) {
bool shi = v & SHIFT_PRESSED;
bool alt = v & (LEFT_ALT_PRESSED | RIGHT_ALT_PRESSED);
if (ctrl) ret |= PIKbdListener::Ctrl;
if (shi) ret |= PIKbdListener::Shift;
if (alt) ret |= PIKbdListener::Alt;
//if (meta) ret |= PIKbdListener::Meta;
if (shi) ret |= PIKbdListener::Shift;
if (alt) ret |= PIKbdListener::Alt;
// if (meta) ret |= PIKbdListener::Meta;
if (v & CAPSLOCK_ON) shi = !shi;
if (shift) *shift = shi;
return ret;
@@ -201,7 +204,7 @@ PIKbdListener::KeyModifiers getModifiers(DWORD v, bool * shift = 0) {
PIKbdListener::MouseButtons getButtons(DWORD v) {
PIKbdListener::MouseButtons ret;
if (v & FROM_LEFT_1ST_BUTTON_PRESSED) ret |= PIKbdListener::MouseLeft;
if (v & RIGHTMOST_BUTTON_PRESSED) ret |= PIKbdListener::MouseRight;
if (v & RIGHTMOST_BUTTON_PRESSED) ret |= PIKbdListener::MouseRight;
if (v & FROM_LEFT_2ND_BUTTON_PRESSED) ret |= PIKbdListener::MouseMiddle;
return ret;
}
@@ -209,77 +212,160 @@ PIKbdListener::MouseButtons getButtons(DWORD v) {
void PIKbdListener::readKeyboard() {
ke.key = 0;
ke.key = 0;
ke.modifiers = 0;
memset(rc, 0, 8);
#ifdef WINDOWS
INPUT_RECORD ir;
ReadConsoleInput(PRIVATE->hIn, &ir, 1, &(PRIVATE->ret));
//piCout << ir.EventType;
// piCout << ir.EventType;
switch (ir.EventType) {
case KEY_EVENT: {
KEY_EVENT_RECORD ker = ir.Event.KeyEvent;
if (ker.bKeyDown) {
bool shift = false;
bool shift = false;
ke.modifiers = getModifiers(ker.dwControlKeyState, &shift);
//piCout << "key" << int(ker.wVirtualKeyCode) << int(ker.uChar.AsciiChar);
// piCout << "key" << int(ker.wVirtualKeyCode) << int(ker.uChar.AsciiChar);
switch (ker.wVirtualKeyCode) {
case 8: PRIVATE->ret = 1; ke.key = Backspace; break;
case 33: PRIVATE->ret = 1; ke.key = PageUp; break;
case 34: PRIVATE->ret = 1; ke.key = PageDown; break;
case 35: PRIVATE->ret = 1; ke.key = End; break;
case 36: PRIVATE->ret = 1; ke.key = Home; break;
case 37: PRIVATE->ret = 1; ke.key = LeftArrow; break;
case 38: PRIVATE->ret = 1; ke.key = UpArrow; break;
case 39: PRIVATE->ret = 1; ke.key = RightArrow; break;
case 40: PRIVATE->ret = 1; ke.key = DownArrow; break;
case 45: PRIVATE->ret = 1; ke.key = Insert; break;
case 46: PRIVATE->ret = 1; ke.key = Delete; break;
case 8:
PRIVATE->ret = 1;
ke.key = Backspace;
break;
case 33:
PRIVATE->ret = 1;
ke.key = PageUp;
break;
case 34:
PRIVATE->ret = 1;
ke.key = PageDown;
break;
case 35:
PRIVATE->ret = 1;
ke.key = End;
break;
case 36:
PRIVATE->ret = 1;
ke.key = Home;
break;
case 37:
PRIVATE->ret = 1;
ke.key = LeftArrow;
break;
case 38:
PRIVATE->ret = 1;
ke.key = UpArrow;
break;
case 39:
PRIVATE->ret = 1;
ke.key = RightArrow;
break;
case 40:
PRIVATE->ret = 1;
ke.key = DownArrow;
break;
case 45:
PRIVATE->ret = 1;
ke.key = Insert;
break;
case 46:
PRIVATE->ret = 1;
ke.key = Delete;
break;
case '\r':
case '\n': PRIVATE->ret = 1; ke.key = Return; break;
case ' ': PRIVATE->ret = 1; ke.key = Space; break;
case '\t': PRIVATE->ret = 1; ke.key = Tab; break;
case 112: PRIVATE->ret = 1; ke.key = F1; break;
case 113: PRIVATE->ret = 1; ke.key = F2; break;
case 114: PRIVATE->ret = 1; ke.key = F3; break;
case 115: PRIVATE->ret = 1; ke.key = F4; break;
case 116: PRIVATE->ret = 1; ke.key = F5; break;
case 117: PRIVATE->ret = 1; ke.key = F6; break;
case 118: PRIVATE->ret = 1; ke.key = F7; break;
case 119: PRIVATE->ret = 1; ke.key = F8; break;
case 120: PRIVATE->ret = 1; ke.key = F9; break;
case 121: PRIVATE->ret = 1; ke.key = F10; break;
case 122: PRIVATE->ret = 1; ke.key = F11; break;
case 123: PRIVATE->ret = 1; ke.key = F12; break;
case '\n':
PRIVATE->ret = 1;
ke.key = Return;
break;
case ' ':
PRIVATE->ret = 1;
ke.key = Space;
break;
case '\t':
PRIVATE->ret = 1;
ke.key = Tab;
break;
case 112:
PRIVATE->ret = 1;
ke.key = F1;
break;
case 113:
PRIVATE->ret = 1;
ke.key = F2;
break;
case 114:
PRIVATE->ret = 1;
ke.key = F3;
break;
case 115:
PRIVATE->ret = 1;
ke.key = F4;
break;
case 116:
PRIVATE->ret = 1;
ke.key = F5;
break;
case 117:
PRIVATE->ret = 1;
ke.key = F6;
break;
case 118:
PRIVATE->ret = 1;
ke.key = F7;
break;
case 119:
PRIVATE->ret = 1;
ke.key = F8;
break;
case 120:
PRIVATE->ret = 1;
ke.key = F9;
break;
case 121:
PRIVATE->ret = 1;
ke.key = F10;
break;
case 122:
PRIVATE->ret = 1;
ke.key = F11;
break;
case 123:
PRIVATE->ret = 1;
ke.key = F12;
break;
default:
PRIVATE->ret = 1;
rc[0] = 1;
rc[0] = 1;
{
PIChar ch = PIChar::fromConsole(ker.uChar.AsciiChar);
if (shift) ch = ch.toUpper();
ke.key = ch.unicode16Code();
PIChar ch = PIChar::fromConsole(ker.uChar.AsciiChar);
if (shift) ch = ch.toUpper();
ke.key = ch.unicode16Code();
}
break;
}
if (ke.key == 0) {piMSleep(10); return;}
} else {piMSleep(10); return;}
if (ke.key == 0) {
piMSleep(10);
return;
}
} else {
piMSleep(10);
return;
}
break;
} break;
case MOUSE_EVENT: {
MOUSE_EVENT_RECORD mer = ir.Event.MouseEvent;
GetConsoleScreenBufferInfo(PRIVATE->hOut, &PRIVATE->sbi);
me.modifiers = getModifiers(mer.dwControlKeyState);
me.modifiers = getModifiers(mer.dwControlKeyState);
MouseButtons mb = getButtons(mer.dwButtonState);
if (mer.dwEventFlags & MOUSE_WHEELED) {
memcpy((void*)(&we), (const void*)(&me), sizeof(me));
we.action = MouseWheel;
memcpy((void *)(&we), (const void *)(&me), sizeof(me));
we.action = MouseWheel;
we.direction = short((mer.dwButtonState >> 8) & 0xFFFF) > 0;
//piCout << "wheel" << we.direction;
// piCout << "wheel" << we.direction;
wheelEvent(we, kbddata_);
break;
} else {
me.x = mer.dwMousePosition.X;
me.y = mer.dwMousePosition.Y - PRIVATE->sbi.srWindow.Top;
me.x = mer.dwMousePosition.X;
me.y = mer.dwMousePosition.Y - PRIVATE->sbi.srWindow.Top;
bool move = mer.dwEventFlags & MOUSE_MOVED;
if (move) {
if (me.action == MouseButtonRelease) {
@@ -297,24 +383,23 @@ void PIKbdListener::readKeyboard() {
prev_p_me = me;
}
tm_dbl.reset();
} else if (mb < me.buttons)
me.action = MouseButtonRelease;
else {
if (mb != 0) piCoutObj << "WTF";
break;
}
else if (mb < me.buttons) me.action = MouseButtonRelease;
else {if (mb != 0) piCoutObj << "WTF"; break;}
}
me.buttons = mb;
if (piCompareBinary(&prev_me, &me, sizeof(me)))
break;
memcpy((void*)(&prev_me), (const void*)(&me), sizeof(me));
//PIString _s[] = {"press", "release", "dbl click", "move"};
//piCoutObj << _s[me.action] << me.buttons << ":" << me.x << me.y;
if (piCompareBinary(&prev_me, &me, sizeof(me))) break;
memcpy((void *)(&prev_me), (const void *)(&me), sizeof(me));
// PIString _s[] = {"press", "release", "dbl click", "move"};
// piCoutObj << _s[me.action] << me.buttons << ":" << me.x << me.y;
mouseEvent(me, kbddata_);
break;
}
}
break;
default:
piMSleep(10);
return;
} break;
default: piMSleep(10); return;
}
#else
tcsetattr(0, TCSANOW, &PRIVATE->tterm);
@@ -327,8 +412,14 @@ void PIKbdListener::readKeyboard() {
for (int i = 0; i < PRIVATE->ret; ++i)
std::cout << "'" << (char)(rc[i]) << "' " << (int)(uchar)(rc[i]);
std::cout << std::endl;*/
if (rc[0] == 0) {piMSleep(10); return;}
if (PRIVATE->ret < 0 || PRIVATE->ret > 7) {piMSleep(10); return;}
if (rc[0] == 0) {
piMSleep(10);
return;
}
if (PRIVATE->ret < 0 || PRIVATE->ret > 7) {
piMSleep(10);
return;
}
if (PRIVATE->ret == 1) {
if (rc[0] == 8)
ke.key = Backspace;
@@ -343,25 +434,26 @@ void PIKbdListener::readKeyboard() {
// 8 - ctrl+alt+shift 7
if (rc[0] == '\e') { // search for Alt
if (PRIVATE->ret == 2) {
mod = 2;
mod = 2;
ke.key = PIChar::fromConsole(rc[1]).unicode16Code();
} else {// escape-seq
} else { // escape-seq
if (rc[1] == '\e') { // search for Alt
for (int i = 1; i < 7; ++i) rc[i] = rc[i + 1];
for (int i = 1; i < 7; ++i)
rc[i] = rc[i + 1];
mod = 2;
PRIVATE->ret--;
}
if (rc[1] == '[') {
if (rc[2] == 'M') { // mouse
if (PRIVATE->ret >= 5) {
me.x = uchar(rc[4] - '!');
me.y = uchar(rc[5] - '!');
me.x = uchar(rc[4] - '!');
me.y = uchar(rc[5] - '!');
int b = rc[3] & 0x7, a = rc[3] & 0x60;
if (a == 0x60) {
memcpy((void*)(&we), (const void*)(&me), sizeof(me));
we.action = MouseWheel;
memcpy((void *)(&we), (const void *)(&me), sizeof(me));
we.action = MouseWheel;
we.direction = (b == 0);
//piCout << "wheel" << we.direction;
// piCout << "wheel" << we.direction;
wheelEvent(we, kbddata_);
} else {
switch (b) {
@@ -372,7 +464,7 @@ void PIKbdListener::readKeyboard() {
}
if (a == 0x20) {
if (b == 3) {
me.action = MouseButtonRelease;
me.action = MouseButtonRelease;
me.buttons = 0;
} else {
if (tm_dbl.elapsed_m() <= dbl_interval && prev_p_me.x == me.x && prev_p_me.y == me.y)
@@ -385,18 +477,19 @@ void PIKbdListener::readKeyboard() {
}
}
if (a == 0x40) me.action = MouseMove;
//PIString _s[] = {"press", "release", "dbl click", "move"};
//piCoutObj << _s[me.action] << me.buttons << ":" << me.x << me.y;
// PIString _s[] = {"press", "release", "dbl click", "move"};
// piCoutObj << _s[me.action] << me.buttons << ":" << me.x << me.y;
mouseEvent(me, kbddata_);
}
//piCout << me.x << me.y << PICoutManipulators::Hex << b << a;
// piCout << me.x << me.y << PICoutManipulators::Hex << b << a;
}
return;
} else {
for (int i = 2; i < 7; ++i) // search for modifier
if (rc[i] == ';') {
mod |= rc[i + 1] - '0' - 1;
for (int j = i; j < 6; ++j) rc[j] = rc[j + 2];
for (int j = i; j < 6; ++j)
rc[j] = rc[j + 2];
rc[6] = rc[7] = 0;
PRIVATE->ret -= 2;
break;
@@ -406,14 +499,15 @@ void PIKbdListener::readKeyboard() {
if (PRIVATE->ret >= 3 && rc[1] == 'O') { // search for modifier (F1-F4)
if (rc[2] >= '1' && rc[2] <= '8') {
mod |= rc[2] - '0' - 1;
for (int j = 2; j < 6; ++j) rc[j] = rc[j + 1];
for (int j = 2; j < 6; ++j)
rc[j] = rc[j + 1];
rc[7] = 0;
PRIVATE->ret--;
}
}
for (int i = 0; ; ++i) {
for (int i = 0;; ++i) {
if (!esc_seq[i].seq) break;
//piCout << "search" << rc[1] << esc_seq[i].seq;
// piCout << "search" << rc[1] << esc_seq[i].seq;
if (strcmp(esc_seq[i].seq, &(rc[1])) == 0) {
ke.key = esc_seq[i].key;
mod |= esc_seq[i].mod;
@@ -425,18 +519,16 @@ void PIKbdListener::readKeyboard() {
if (mod & 0x1) ke.modifiers |= Shift;
if (mod & 0x2) ke.modifiers |= Alt;
if (mod & 0x4) ke.modifiers |= Ctrl;
//if (mod & 0x8) ke.modifiers |= Meta;
// if (mod & 0x8) ke.modifiers |= Meta;
}
/*cout << "wo mods (" << mod << ")\n";
for (int i = 0; i < PRIVATE->ret; ++i)
cout << "'" << (char)(rc[i]) << "' ";
cout << endl;*/
/*cout << "wo mods (" << mod << ")\n";
for (int i = 0; i < PRIVATE->ret; ++i)
cout << "'" << (char)(rc[i]) << "' ";
cout << endl;*/
}
if (ke.key == 0 && PRIVATE->ret > 1)
ke.key = PIChar(rc).unicode16Code();
if (ke.key == 0 && PRIVATE->ret > 1) ke.key = PIChar(rc).unicode16Code();
#endif
if ((rc[0] == '\n' || rc[0] == '\r') && PRIVATE->ret == 1)
ke.key = Return;
if ((rc[0] == '\n' || rc[0] == '\r') && PRIVATE->ret == 1) ke.key = Return;
if (exit_enabled && ke.key == exit_key) {
PIKbdListener::exiting = true;
return;
@@ -449,7 +541,7 @@ void PIKbdListener::readKeyboard() {
void PIKbdListener::end() {
//cout << "list end" << endl;
// cout << "list end" << endl;
#ifdef WINDOWS
SetConsoleMode(PRIVATE->hIn, PRIVATE->smode);
#else

View File

@@ -27,61 +27,66 @@
#define PIKBDLISTENER_H
#include "pithread.h"
#include "pitime.h"
#define WAIT_FOR_EXIT while (!PIKbdListener::exiting) piMSleep(PIP_MIN_MSLEEP*5); // TODO: rewrite with condvar
#define WAIT_FOR_EXIT \
while (!PIKbdListener::exiting) \
piMSleep(PIP_MIN_MSLEEP * 5); // TODO: rewrite with condvar
class PIP_EXPORT PIKbdListener: public PIThread
{
class PIP_EXPORT PIKbdListener: public PIThread {
PIOBJECT_SUBCLASS(PIKbdListener, PIThread);
friend class PIConsole;
friend class PITerminal;
public:
public:
//! Special keyboard keys
enum SpecialKey {
Tab /** Tab key */ = 0x09,
Return /** Enter key */ = 0x0a,
Esc /** Escape key */ = 0x1b,
Space /** Space key */ = 0x20,
Backspace /** Backspace key */ = 0x7f,
UpArrow /** Up arrow key */ = -1,
DownArrow /** Down arrow key */ = -2,
Tab /** Tab key */ = 0x09,
Return /** Enter key */ = 0x0a,
Esc /** Escape key */ = 0x1b,
Space /** Space key */ = 0x20,
Backspace /** Backspace key */ = 0x7f,
UpArrow /** Up arrow key */ = -1,
DownArrow /** Down arrow key */ = -2,
RightArrow /** Right arrow key */ = -3,
LeftArrow /** Left arrow key */ = -4,
Home /** Home key */ = -5,
End /** End key */ = -6,
PageUp /** Page up key */ = -7,
PageDown /** Page down key */ = -8,
Insert /** Delete key */ = -9,
Delete /** Delete key */ = -10,
F1 /** F1 key */ = -11,
F2 /** F2 key */ = -12,
F3 /** F3 key */ = -13,
F4 /** F4 key */ = -14,
F5 /** F5 key */ = -15,
F6 /** F6 key */ = -16,
F7 /** F7 key */ = -17,
F8 /** F8 key */ = -18,
F9 /** F9 key */ = -19,
F10 /** F10 key */ = -20,
F11 /** F11 key */ = -21,
F12 /** F12 key */ = -22
LeftArrow /** Left arrow key */ = -4,
Home /** Home key */ = -5,
End /** End key */ = -6,
PageUp /** Page up key */ = -7,
PageDown /** Page down key */ = -8,
Insert /** Delete key */ = -9,
Delete /** Delete key */ = -10,
F1 /** F1 key */ = -11,
F2 /** F2 key */ = -12,
F3 /** F3 key */ = -13,
F4 /** F4 key */ = -14,
F5 /** F5 key */ = -15,
F6 /** F6 key */ = -16,
F7 /** F7 key */ = -17,
F8 /** F8 key */ = -18,
F9 /** F9 key */ = -19,
F10 /** F10 key */ = -20,
F11 /** F11 key */ = -21,
F12 /** F12 key */ = -22
};
//! Keyboard modifiers
enum KeyModifier {
Ctrl /** Control key */ = 0x1,
Shift /** Shift key */ = 0x2,
Alt /** Alt key */ = 0x4
//Meta /** Meta (windows) key */ = 0x8
Shift /** Shift key */ = 0x2,
Alt /** Alt key */ = 0x4
// Meta /** Meta (windows) key */ = 0x8
};
typedef PIFlags<KeyModifier> KeyModifiers;
//! This struct contains information about pressed keyboard key
struct PIP_EXPORT KeyEvent {
KeyEvent(int k = 0, KeyModifiers m = 0) {key = k; modifiers = m;}
KeyEvent(int k = 0, KeyModifiers m = 0) {
key = k;
modifiers = m;
}
//! Pressed key. It can be simple \b char or special key (see PIKbdListener::SpecialKey)
int key;
@@ -92,8 +97,8 @@ public:
//! Mouse buttons
enum MouseButton {
MouseLeft /** Left button */ = 0x01,
MouseRight /** Right button */ = 0x02,
MouseLeft /** Left button */ = 0x01,
MouseRight /** Right button */ = 0x02,
MouseMiddle /** Middle button */ = 0x04
};
@@ -110,7 +115,12 @@ public:
//! This struct contains information about mouse action
struct PIP_EXPORT MouseEvent {
MouseEvent(MouseAction a = MouseButtonPress, MouseButtons b = 0, KeyModifiers m = 0) {x = y = 0; action = a; buttons = b; modifiers = m;}
MouseEvent(MouseAction a = MouseButtonPress, MouseButtons b = 0, KeyModifiers m = 0) {
x = y = 0;
action = a;
buttons = b;
modifiers = m;
}
//! Event X coordinate in view-space, from 0
int x;
@@ -130,7 +140,7 @@ public:
//! This struct contains information about mouse wheel action
struct PIP_EXPORT WheelEvent: public MouseEvent {
WheelEvent(): MouseEvent() {direction = false;}
WheelEvent(): MouseEvent() { direction = false; }
//! Wheel direction, /b true - up, /b fasle - down
bool direction;
@@ -145,43 +155,48 @@ public:
//! Returns custom data
void * data() {return kbddata_;}
void * data() { return kbddata_; }
//! Set custom data to "_data"
void setData(void * _data) {kbddata_ = _data;}
void setData(void * _data) { kbddata_ = _data; }
//! Set external function to "slot"
void setSlot(KBFunc slot) {ret_func = slot;}
void setSlot(KBFunc slot) { ret_func = slot; }
//! Set external function to "slot"
void setSlot(std::function<void(KeyEvent)> slot) {ret_func = [slot](KeyEvent e, void *){slot(e);};}
void setSlot(std::function<void(KeyEvent)> slot) {
ret_func = [slot](KeyEvent e, void *) { slot(e); };
}
//! Returns if exit key if awaiting
bool exitCaptured() const {return exit_enabled;}
bool exitCaptured() const { return exit_enabled; }
//! Returns exit key, default 'Q'
int exitKey() const {return exit_key;}
int exitKey() const { return exit_key; }
double doubleClickInterval() const {return dbl_interval;}
void setDoubleClickInterval(double v) {dbl_interval = v;}
double doubleClickInterval() const { return dbl_interval; }
void setDoubleClickInterval(double v) { dbl_interval = v; }
void readKeyboard();
//! Returns if keyboard listening is active (not running!)
bool isActive() {return is_active;}
bool isActive() { return is_active; }
EVENT_HANDLER( void, enableExitCapture) {enableExitCapture('Q');}
EVENT_HANDLER1(void, enableExitCapture, int, key) {exit_enabled = true; exit_key = key;}
EVENT_HANDLER(void, disableExitCapture) {exit_enabled = false;}
EVENT_HANDLER(void, setActive) {setActive(true);}
EVENT_HANDLER(void, enableExitCapture) { enableExitCapture('Q'); }
EVENT_HANDLER1(void, enableExitCapture, int, key) {
exit_enabled = true;
exit_key = key;
}
EVENT_HANDLER(void, disableExitCapture) { exit_enabled = false; }
EVENT_HANDLER(void, setActive) { setActive(true); }
EVENT_HANDLER1(void, setActive, bool, yes);
EVENT2(keyPressed, PIKbdListener::KeyEvent, key, void * , data);
EVENT2(mouseEvent, PIKbdListener::MouseEvent, mouse, void * , data);
EVENT2(wheelEvent, PIKbdListener::WheelEvent, wheel, void * , data);
EVENT2(keyPressed, PIKbdListener::KeyEvent, key, void *, data);
EVENT2(mouseEvent, PIKbdListener::MouseEvent, mouse, void *, data);
EVENT2(wheelEvent, PIKbdListener::WheelEvent, wheel, void *, data);
//! \handlers
//! \{
//! \handlers
//! \{
//! \fn void enableExitCapture(int key = 'Q')
//! \brief Enable exit key "key" awaiting
@@ -192,21 +207,21 @@ public:
//! \fn void setActive(bool yes = true)
//! \brief Set keyboard listening is active or not
//! \}
//! \events
//! \{
//! \}
//! \events
//! \{
//! \fn void keyPressed(PIKbdListener::KeyEvent key, void * data)
//! \brief Raise on key "key" pressed, "data" is custom data
//! \}
//! \}
static bool exiting;
static PIKbdListener * instance() {return _object;}
static PIKbdListener * instance() { return _object; }
private:
void begin() override;
void run() override {readKeyboard();}
void run() override { readKeyboard(); }
void end() override;
#ifndef WINDOWS
@@ -225,7 +240,7 @@ private:
vt_none,
vt_xterm = 0x1,
vt_linux = 0x2,
vt_all = 0xFF
vt_all = 0xFF
};
static const EscSeq esc_seq[];
@@ -243,30 +258,41 @@ private:
MouseEvent me, prev_me, prev_p_me;
WheelEvent we;
static PIKbdListener * _object;
};
//! \relatesalso PIBinaryStream
//! \~english Store operator
//! \~russian Оператор сохранения
BINARY_STREAM_WRITE(PIKbdListener::MouseEvent) {s << v.x << v.y << v.action << v.buttons << v.modifiers; return s;}
BINARY_STREAM_WRITE(PIKbdListener::MouseEvent) {
s << v.x << v.y << v.action << v.buttons << v.modifiers;
return s;
}
//! \relatesalso PIBinaryStream
//! \~english Restore operator
//! \~russian Оператор извлечения
BINARY_STREAM_READ (PIKbdListener::MouseEvent) {s >> v.x >> v.y >> v.action >> v.buttons >> v.modifiers; return s;}
BINARY_STREAM_READ(PIKbdListener::MouseEvent) {
s >> v.x >> v.y >> v.action >> v.buttons >> v.modifiers;
return s;
}
//! \relatesalso PIBinaryStream
//! \~english Store operator
//! \~russian Оператор сохранения
BINARY_STREAM_WRITE(PIKbdListener::WheelEvent) {s << (*(PIKbdListener::MouseEvent*)&v) << v.direction; return s;}
BINARY_STREAM_WRITE(PIKbdListener::WheelEvent) {
s << (*(PIKbdListener::MouseEvent *)&v) << v.direction;
return s;
}
//! \relatesalso PIBinaryStream
//! \~english Restore operator
//! \~russian Оператор извлечения
BINARY_STREAM_READ (PIKbdListener::WheelEvent) {s >> (*(PIKbdListener::MouseEvent*)&v) >> v.direction; return s;}
BINARY_STREAM_READ(PIKbdListener::WheelEvent) {
s >> (*(PIKbdListener::MouseEvent *)&v) >> v.direction;
return s;
}
REGISTER_PIVARIANTSIMPLE(PIKbdListener::KeyEvent)