C-Menu 0.2.9
A User Interface Toolkit
Loading...
Searching...
No Matches
ui_ncurses_input.c
Go to the documentation of this file.
1/** @file ui_ncurses_input.c
2 @ingroup ui_ncurses
3 @brief NCurses UI backend — input handling.
4
5 Translates raw NCurses key codes and mouse events into the portable
6 UiEvent representation defined in ui_backend.h.
7*/
8
9#include "ui_backend.h"
11#include <stddef.h>
12#include <string.h>
13#include <termios.h>
14/* -------------------------------------------------------------------------
15 Key translation
16 ------------------------------------------------------------------------- */
17
18static UiKey translate_key(int ch) {
19 switch (ch) {
20 case ERR:
21 return UI_KEY_NONE;
22 case '\n':
23 case '\r':
24 return UI_KEY_ENTER;
25 case 27:
26 return UI_KEY_ESCAPE;
27 case '\t':
28 return UI_KEY_TAB;
29#ifdef KEY_BTAB
30 case KEY_BTAB:
31 return UI_KEY_BTAB;
32#endif
33 case KEY_UP:
34 return UI_KEY_UP;
35 case KEY_DOWN:
36 return UI_KEY_DOWN;
37 case KEY_LEFT:
38 return UI_KEY_LEFT;
39 case KEY_RIGHT:
40 return UI_KEY_RIGHT;
41 case KEY_HOME:
42 return UI_KEY_HOME;
43 case KEY_END:
44 return UI_KEY_END;
45 case KEY_PPAGE:
46 return UI_KEY_PGUP;
47 case KEY_NPAGE:
48 return UI_KEY_PGDN;
49 case KEY_IC:
50 return UI_KEY_INSERT;
51 case KEY_DC:
52 return UI_KEY_DELETE;
53 case KEY_BACKSPACE:
54 return UI_KEY_BACKSPACE;
55#ifdef KEY_RESIZE
56 case KEY_RESIZE:
57 return UI_KEY_RESIZE;
58#endif
59 case KEY_MOUSE:
60 return UI_KEY_MOUSE;
61 case KEY_F(1):
62 return UI_KEY_F1;
63 case KEY_F(2):
64 return UI_KEY_F2;
65 case KEY_F(3):
66 return UI_KEY_F3;
67 case KEY_F(4):
68 return UI_KEY_F4;
69 case KEY_F(5):
70 return UI_KEY_F5;
71 case KEY_F(6):
72 return UI_KEY_F6;
73 case KEY_F(7):
74 return UI_KEY_F7;
75 case KEY_F(8):
76 return UI_KEY_F8;
77 case KEY_F(9):
78 return UI_KEY_F9;
79 case KEY_F(10):
80 return UI_KEY_F10;
81 case KEY_F(11):
82 return UI_KEY_F11;
83 case KEY_F(12):
84 return UI_KEY_F12;
85 default:
86 if (ch >= 32 && ch <= 126)
87 return UI_KEY_CHAR;
88 return UI_KEY_NONE;
89 }
90}
91
92/* -------------------------------------------------------------------------
93 Event retrieval
94 ------------------------------------------------------------------------- */
95
96/** @brief Wait for an input event on @p target (or stdscr if NULL).
97 @param ui UI runtime context (unused — event comes from the window).
98 @param target Surface to read from, or NULL for stdscr.
99 @param ev Output UiEvent structure.
100 @param timeout_ms Milliseconds to wait; -1 = block indefinitely.
101 @return 0 on success, -1 if @p ev is NULL.
102*/
103int ui_get_event(UiSurface *s, uint w, UiEvent *ev, int timeout_ms) {
104 if (!ev)
105 return -1;
106 memset(ev, 0, sizeof(*ev));
107 tcflush(2, TCIFLUSH);
108 if (timeout_ms <= 0) {
109 timeout_ms = -1;
110 } else
111 wtimeout(s->mwin[w], timeout_ms);
112 mousemask(ALL_MOUSE_EVENTS | REPORT_MOUSE_POSITION | BUTTON_SHIFT | BUTTON_CTRL | BUTTON_ALT, NULL);
113 curs_set(2);
114 int ch = wgetch(s->mwin[w]);
115 curs_set(0);
116 ev->key = translate_key(ch);
117 if (ev->key == UI_KEY_CHAR) {
118 ev->ch = (uint32_t)ch;
119 } else if (ev->key == UI_KEY_MOUSE) {
120 MEVENT me;
121 if (getmouse(&me) == OK) {
122 ev->y = me.y;
123 ev->x = me.x;
124 if (me.bstate & BUTTON4_PRESSED)
126 else if (me.bstate & BUTTON5_PRESSED)
128 else if (me.bstate & BUTTON1_PRESSED)
130 else if (me.bstate & BUTTON1_RELEASED)
132 if (wenclose(s->mwin[w], me.y, me.x) &&
133 wmouse_trafo(s->mwin[w], &me.y, &me.x, false)) {
134 ev->bstate = me.bstate;
135 ev->mouse_inside = true;
136 ev->y = me.y;
137 ev->x = me.x;
138 } else {
139 ev->mouse_inside = false;
140 }
141 }
142 }
143 return ch;
144}
145
146int ui_get_event_multi(UiSurface *s, uint w, UiEvent *ev, int timeout_ms) {
147 int i;
148 if (!ev)
149 return -1;
150 memset(ev, 0, sizeof(*ev));
151 keypad(s->mwin[w], true);
152 if (timeout_ms <= 0) {
153 timeout_ms = -1;
154 } else
155 wtimeout(s->mwin[w], timeout_ms);
156 mousemask(ALL_MOUSE_EVENTS | REPORT_MOUSE_POSITION | BUTTON_SHIFT | BUTTON_CTRL | BUTTON_ALT, NULL);
157 ev->chyron = -1;
158 qiflush();
159 tcflush(2, TCIFLUSH);
160 cbreak();
161 curs_set(2);
162 int ch = wgetch(s->mwin[w]);
163 curs_set(0);
165 ev->key = translate_key(ch);
166 if (ev->key == UI_KEY_CHAR) {
167 ev->ch = (uint32_t)ch;
168 } else if (ev->key == UI_KEY_MOUSE) {
169 MEVENT me;
170 if (getmouse(&me) == OK) {
171 ev->y = me.y;
172 ev->x = me.x;
173 if (me.bstate & BUTTON4_PRESSED)
175 else if (me.bstate & BUTTON5_PRESSED)
177 else if (me.bstate & BUTTON1_CLICKED ||
178 me.bstate & BUTTON1_PRESSED ||
179 me.bstate & BUTTON1_DOUBLE_CLICKED) {
181 ev->in_win = -1;
182 for (i = WIN; i < SUB_SFC_MAX; i++) {
183 if (s->mwin[i] != NULL &&
184 wenclose(s->mwin[i], me.y, me.x) &&
185 wmouse_trafo(s->mwin[i], &me.y, &me.x, false)) {
186 ev->in_win = i;
187 ev->key = 0;
188 break;
189 }
190 }
191 ev->bstate = me.bstate;
192 ev->y = me.y;
193 ev->x = me.x;
194 ev->key = 0;
195 return 0;
196 }
197 }
198 }
199 curs_set(0);
200 return ch;
201}
202
203int ui_get_event_no_mouse(UiSurface *s, uint w, UiEvent *ev) {
204 int ch;
205 mousemask(0, NULL);
206
207 qiflush();
208 tcflush(2, TCIFLUSH);
209 cbreak();
210 do {
211 curs_set(2);
212 ch = wgetch(s->mwin[w]);
213 curs_set(0);
214 ev->key = translate_key(ch);
215 if (ev->key == UI_KEY_CHAR) {
216 ev->ch = (uint32_t)ch;
217 break;
218 }
219 } while (ch == ERR);
220 return ch;
221}
222/* -------------------------------------------------------------------------
223 Mice
224 ------------------------------------------------------------------------- */
225int ui_mousemask(int mask) {
226 if (!ui)
227 return -1;
228 if (mask)
229 mousemask(mask, nullptr);
230 else
231 mousemask(ALL_MOUSE_EVENTS | REPORT_MOUSE_POSITION, nullptr);
232 return 0;
233}
234int ui_mice_enable(int mask) {
235 if (!ui)
236 return -1;
237 if (mask)
238 mousemask(mask, nullptr);
239 else
240 mousemask(ALL_MOUSE_EVENTS | REPORT_MOUSE_POSITION, nullptr);
241 return 0;
242}
int ui_mice_enable(int mask)
int ui_get_event_no_mouse(UiSurface *surface, uint w, UiEvent *ev)
@ UI_KEY_F8
Definition ui_backend.h:69
@ UI_KEY_BACKSPACE
Definition ui_backend.h:47
@ UI_KEY_F9
Definition ui_backend.h:70
@ UI_KEY_TAB
Definition ui_backend.h:48
@ UI_KEY_F2
Definition ui_backend.h:63
@ UI_KEY_F7
Definition ui_backend.h:68
@ UI_KEY_DELETE
Definition ui_backend.h:59
@ UI_KEY_CHAR
Definition ui_backend.h:44
@ UI_KEY_MOUSE
Definition ui_backend.h:61
@ UI_KEY_F11
Definition ui_backend.h:72
@ UI_KEY_DOWN
Definition ui_backend.h:51
@ UI_KEY_F4
Definition ui_backend.h:65
@ UI_KEY_F3
Definition ui_backend.h:64
@ UI_KEY_HOME
Definition ui_backend.h:54
@ UI_KEY_F6
Definition ui_backend.h:67
@ UI_KEY_PGDN
Definition ui_backend.h:57
@ UI_KEY_PGUP
Definition ui_backend.h:56
@ UI_KEY_NONE
Definition ui_backend.h:43
@ UI_KEY_BTAB
Definition ui_backend.h:49
@ UI_KEY_END
Definition ui_backend.h:55
@ UI_KEY_F5
Definition ui_backend.h:66
@ UI_KEY_ESCAPE
Definition ui_backend.h:46
@ UI_KEY_LEFT
Definition ui_backend.h:52
@ UI_KEY_F1
Definition ui_backend.h:62
@ UI_KEY_ENTER
Definition ui_backend.h:45
@ UI_KEY_UP
Definition ui_backend.h:50
@ UI_KEY_F12
Definition ui_backend.h:73
@ UI_KEY_RESIZE
Definition ui_backend.h:60
@ UI_KEY_INSERT
Definition ui_backend.h:58
@ UI_KEY_RIGHT
Definition ui_backend.h:53
@ UI_KEY_F10
Definition ui_backend.h:71
int ui_get_event(UiSurface *s, uint w, UiEvent *ev, int timeout_ms)
Wait for an input event on target (or stdscr if NULL).
#define ERR
Definition ui_backend.h:249
UiRuntime * ui
int ui_mousemask(int mask)
int ui_get_event_multi(UiSurface *s, uint w, UiEvent *ev, int timeout_ms)
@ UI_MOUSE_SCROLL_DOWN
Definition ui_backend.h:82
@ UI_MOUSE_PRESS
Definition ui_backend.h:78
@ UI_MOUSE_SCROLL_UP
Definition ui_backend.h:81
@ UI_MOUSE_NONE
Definition ui_backend.h:77
@ UI_MOUSE_RELEASE
Definition ui_backend.h:79
#define KEY_RIGHT
#define KEY_DC
#define KEY_BACKSPACE
#define KEY_DOWN
#define KEY_PPAGE
#define KEY_RESIZE
#define KEY_NPAGE
#define KEY_END
#define KEY_MOUSE
#define KEY_IC
#define KEY_LEFT
#define KEY_HOME
#define KEY_UP
uint32_t ch
Definition ui_backend.h:102
UiMouseAction mouse_action
Definition ui_backend.h:112
uint in_win
Definition ui_backend.h:110
int bstate
Definition ui_backend.h:111
int chyron
Definition ui_backend.h:109
UiKey key
Definition ui_backend.h:101
bool mouse_inside
Definition ui_backend.h:113