C-Menu 0.2.9
A User Interface Toolkit
Loading...
Searching...
No Matches
ui_ncurses_input.c File Reference

NCurses UI backend — input handling. More...

#include "ui_backend.h"
#include "ui_ncurses_internal.h"
#include <stddef.h>
#include <string.h>
#include <termios.h>
Include dependency graph for ui_ncurses_input.c:

Go to the source code of this file.

Functions

int ui_get_event (UiSurface *s, uint w, UiEvent *ev, int timeout_ms)
 Wait for an input event on target (or stdscr if NULL).
int ui_get_event_multi (UiSurface *s, uint w, UiEvent *ev, int timeout_ms)
int ui_get_event_no_mouse (UiSurface *s, uint w, UiEvent *ev)
int ui_mice_enable (int mask)
int ui_mousemask (int mask)

Detailed Description

NCurses UI backend — input handling.

Translates raw NCurses key codes and mouse events into the portable UiEvent representation defined in ui_backend.h.

Definition in file ui_ncurses_input.c.

Function Documentation

◆ ui_get_event()

int ui_get_event ( UiSurface * s,
uint w,
UiEvent * ev,
int timeout_ms )

Wait for an input event on target (or stdscr if NULL).

Parameters
uiUI runtime context (unused — event comes from the window).
targetSurface to read from, or NULL for stdscr.
evOutput UiEvent structure.
timeout_msMilliseconds to wait; -1 = block indefinitely.
Returns
0 on success, -1 if ev is NULL.

Definition at line 103 of file ui_ncurses_input.c.

103 {
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}
@ UI_KEY_CHAR
Definition ui_backend.h:44
@ UI_KEY_MOUSE
Definition ui_backend.h:61
@ 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_RELEASE
Definition ui_backend.h:79
uint32_t ch
Definition ui_backend.h:102
UiMouseAction mouse_action
Definition ui_backend.h:112
int bstate
Definition ui_backend.h:111
UiKey key
Definition ui_backend.h:101
bool mouse_inside
Definition ui_backend.h:113
WINDOW * mwin[SUB_SFC_MAX]

◆ ui_get_event_multi()

int ui_get_event_multi ( UiSurface * s,
uint w,
UiEvent * ev,
int timeout_ms )

Definition at line 146 of file ui_ncurses_input.c.

146 {
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}
@ UI_MOUSE_NONE
Definition ui_backend.h:77
uint in_win
Definition ui_backend.h:110
int chyron
Definition ui_backend.h:109

◆ ui_get_event_no_mouse()

int ui_get_event_no_mouse ( UiSurface * s,
uint w,
UiEvent * ev )

Definition at line 203 of file ui_ncurses_input.c.

203 {
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}
#define ERR
Definition ui_backend.h:249

◆ ui_mice_enable()

int ui_mice_enable ( int mask)

Definition at line 234 of file ui_ncurses_input.c.

234 {
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}
UiRuntime * ui

◆ ui_mousemask()

int ui_mousemask ( int mask)

Definition at line 225 of file ui_ncurses_input.c.

225 {
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}