C-Menu 0.2.9
A User Interface Toolkit
Loading...
Searching...
No Matches
ui_ncurses_input.c
Go to the documentation of this file.
2#include <string.h>
3/** @file
4 @brief Input handling for ncurses-based UI.
5 @ingroup ui_ncurses
6
7 This file implements the input handling for the ncurses-based UI. It translates
8 ncurses key codes into the UI's internal key representation and handles mouse events.
9 */
10
11/**
12 @brief Translates a ncurses key code into the UI's internal key
13 representation.
14
15 @param ch The ncurses key code to translate.
16 @return The corresponding UiKey value.
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
86 default:
87 if (ch >= 32 && ch <= 126)
88 return UI_KEY_CHAR;
89 return UI_KEY_NONE;
90 }
91}
92
93/**
94 @brief Retrieves an input event from the specified UI surface.
95 @ingroup ui_ncurses
96
97 This function waits for an input event on the given UI surface (or the standard
98 screen if no surface is specified) and fills the provided UiEvent structure
99 with the details of the event. It handles both keyboard and mouse events.
100
101 @param ui The UI runtime context (unused in this implementation).
102 @param target The UI surface to monitor for events, or NULL to use stdscr.
103 @param ev A pointer to a UiEvent structure to be filled with event details.
104 @param timeout_ms The maximum time to wait for an event in milliseconds,
105 or -1 to wait indefinitely.
106 @return 0 on success, or -1 on error (e.g., if ev is NULL).
107 */
108int ui_get_event(UiRuntime *ui, UiSurface *target, UiEvent *ev, int timeout_ms) {
109 int ch;
110 WINDOW *win;
111
112 (void)ui;
113 if (!ev)
114 return -1;
115 memset(ev, 0, sizeof(*ev));
116
117 win = target ? target->win : stdscr;
118
119 if (timeout_ms < 0) {
120 wtimeout(win, -1);
121 } else {
122 wtimeout(win, timeout_ms);
123 }
124
125 ch = wgetch(win);
126 ev->key = translate_key(ch);
127
128 if (ev->key == UI_KEY_CHAR) {
129 ev->ch = (uint32_t)ch;
130 } else if (ev->key == UI_KEY_MOUSE) {
131 MEVENT me;
132 if (getmouse(&me) == OK) {
133 ev->y = me.y;
134 ev->x = me.x;
135#ifdef BUTTON4_PRESSED
136 if (me.bstate & BUTTON4_PRESSED) {
138#endif
139#ifdef BUTTON5_PRESSED
140 } else if (me.bstate & BUTTON5_PRESSED) {
142#endif
143 } else if (me.bstate & BUTTON1_PRESSED) {
145 } else if (me.bstate & BUTTON1_RELEASED) {
147 }
148 }
149 }
150
151 return 0;
152}
@ UI_KEY_F8
Definition ui_backend.h:85
@ UI_KEY_BACKSPACE
Definition ui_backend.h:63
@ UI_KEY_F9
Definition ui_backend.h:86
@ UI_KEY_TAB
Definition ui_backend.h:64
@ UI_KEY_F2
Definition ui_backend.h:79
@ UI_KEY_F7
Definition ui_backend.h:84
@ UI_KEY_DELETE
Definition ui_backend.h:75
@ UI_KEY_CHAR
Definition ui_backend.h:60
@ UI_KEY_MOUSE
Definition ui_backend.h:77
@ UI_KEY_F11
Definition ui_backend.h:88
@ UI_KEY_DOWN
Definition ui_backend.h:67
@ UI_KEY_F4
Definition ui_backend.h:81
@ UI_KEY_F3
Definition ui_backend.h:80
@ UI_KEY_HOME
Definition ui_backend.h:70
@ UI_KEY_F6
Definition ui_backend.h:83
@ UI_KEY_PGDN
Definition ui_backend.h:73
@ UI_KEY_PGUP
Definition ui_backend.h:72
@ UI_KEY_NONE
Definition ui_backend.h:59
@ UI_KEY_BTAB
Definition ui_backend.h:65
@ UI_KEY_END
Definition ui_backend.h:71
@ UI_KEY_F5
Definition ui_backend.h:82
@ UI_KEY_ESCAPE
Definition ui_backend.h:62
@ UI_KEY_LEFT
Definition ui_backend.h:68
@ UI_KEY_F1
Definition ui_backend.h:78
@ UI_KEY_ENTER
Definition ui_backend.h:61
@ UI_KEY_UP
Definition ui_backend.h:66
@ UI_KEY_F12
Definition ui_backend.h:89
@ UI_KEY_RESIZE
Definition ui_backend.h:76
@ UI_KEY_INSERT
Definition ui_backend.h:74
@ UI_KEY_RIGHT
Definition ui_backend.h:69
@ UI_KEY_F10
Definition ui_backend.h:87
@ UI_MOUSE_SCROLL_DOWN
Definition ui_backend.h:105
@ UI_MOUSE_PRESS
Definition ui_backend.h:101
@ UI_MOUSE_SCROLL_UP
Definition ui_backend.h:104
@ UI_MOUSE_RELEASE
Definition ui_backend.h:102
int ui_get_event(UiRuntime *ui, UiSurface *target, UiEvent *ev, int timeout_ms)
Retrieves an input event from the specified UI surface.
uint32_t ch
Definition ui_backend.h:180
UiMouseAction mouse_action
Definition ui_backend.h:186
UiKey key
Definition ui_backend.h:179