-
Notifications
You must be signed in to change notification settings - Fork 50
/
uikeydump.cpp
123 lines (105 loc) · 2.35 KB
/
uikeydump.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
// uikeydump.cpp
//
// Copyright (c) 2022-2024 Kristofer Berggren
// All rights reserved.
//
// nchat is distributed under the MIT license, see LICENSE for details.
#include "uikeydump.h"
#include <locale.h>
#include <unistd.h>
#include <sys/select.h>
#include <ncurses.h>
#include "uiconfig.h"
#include "uicontroller.h"
#include "uikeyconfig.h"
#include "uikeyinput.h"
// #define USE_KEY_CONFIG 1
void UiKeyDump::Run()
{
UiConfig::Init();
printf("\033[?1004h");
setlocale(LC_ALL, "");
initscr();
noecho();
cbreak();
UiConfig::GetBool("linefeed_on_enter") ? nl() : nonl();
raw();
keypad(stdscr, TRUE);
curs_set(0);
timeout(0);
printw("key code dump mode - press ctrl-c to exit\n");
refresh();
UiKeyConfig::Init(false);
UiController uiController;
uiController.Init();
#ifdef USE_KEY_CONFIG
std::map<std::string, std::string> keyParams = UiKeyConfig::GetMap();
std::map<wint_t, std::string> codeConfig;
for (auto& keyParam : keyParams)
{
std::string param = keyParam.first;
wint_t key = UiKeyConfig::GetKey(param);
codeConfig[key] = param;
}
#endif
bool running = true;
while (running)
{
fd_set fds;
FD_ZERO(&fds);
FD_SET(0, &fds);
int maxfd = STDIN_FILENO;
struct timeval tv = {1, 0};
select(maxfd + 1, &fds, NULL, NULL, &tv);
if (FD_ISSET(STDIN_FILENO, &fds))
{
int y = 0;
int x = 0;
int maxy = 0;
int maxx = 0;
getyx(stdscr, y, x);
getmaxyx(stdscr, maxy, maxx);
if (y == (maxy - 1))
{
clear();
refresh();
}
int count = 0;
wint_t key = 0;
wint_t keyOk = 0;
while (UiKeyInput::GetWch(&key) != ERR)
{
keyOk = key;
++count;
printw("\\%o", key);
if (key == 3)
{
running = false;
break;
}
}
if ((keyOk != 0) && (count == 1))
{
std::string keyName = UiKeyConfig::GetKeyName(keyOk);
if (!keyName.empty())
{
printw(" %s", keyName.c_str());
}
#ifdef USE_KEY_CONFIG
std::string keyParam = codeConfig[keyOk];
if (!keyParam.empty())
{
printw(" %s", keyParam.c_str());
}
#endif
}
printw("\n");
refresh();
}
}
uiController.Cleanup();
UiKeyConfig::Cleanup();
wclear(stdscr);
endwin();
printf("\033[?1004l");
}