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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
|
#include "../internal.h"
#define _XOPEN_SOURCE 700
#include <wchar.h>
#include <string.h>
#include <stdlib.h>
#include <locale.h>
#include <ncurses.h>
#include <dlfcn.h>
#include <signal.h>
#include <assert.h>
/* ncurses.h likes to define stuff for us.
* This unforunately mangles with our struct. */
#undef erase
#undef get_wch
#undef refresh
#undef mvprintw
#undef move
#undef init_pair
#undef attroff
#undef attron
#undef getmaxx
#undef getmaxy
#undef timeout
/**
* Dynamically loaded curses API.
*/
static struct curses {
struct sigaction action;
void *handle;
WINDOW *stdscr;
WINDOW* (*initscr)(void);
int (*endwin)(void);
int (*refresh)(void);
int (*erase)(void);
int (*get_wch)(wint_t *wch);
int (*mvprintw)(int x, int y, const char *fmt, ...);
int (*move)(int x, int y);
int (*init_pair)(short color, short f, short b);
int (*attroff)(int attrs);
int (*attron)(int attrs);
int (*start_color)(void);
int (*getmaxx)(WINDOW *win);
int (*getmaxy)(WINDOW *win);
int (*keypad)(WINDOW *win, bool bf);
int *ESCDELAY;
} curses;
static void _bmDrawCursesDrawLine(int pair, int y, const char *format, ...)
{
static int ncols = 0;
static char *buffer = NULL;
int newNcols = curses.getmaxx(curses.stdscr);
if (newNcols <= 0)
return;
if (!buffer || newNcols > ncols) {
if (buffer)
free(buffer);
ncols = newNcols;
if (!(buffer = calloc(1, ncols + 1)))
return;
}
va_list args;
va_start(args, format);
int tlen = vsnprintf(NULL, 0, format, args) + 1;
if (tlen > ncols)
tlen = ncols;
va_end(args);
va_start(args, format);
vsnprintf(buffer, tlen, format, args);
va_end(args);
memset(buffer + tlen - 1, ' ', ncols - tlen + 1);
if (pair > 0)
curses.attron(COLOR_PAIR(pair));
curses.mvprintw(y, 0, buffer);
if (pair > 0)
curses.attroff(COLOR_PAIR(pair));
}
static void _bmDrawCursesRender(const bmMenu *menu)
{
if (!curses.stdscr) {
freopen("/dev/tty", "r", stdin);
setlocale(LC_CTYPE, "");
if ((curses.stdscr = curses.initscr()) == NULL)
return;
*curses.ESCDELAY = 25;
curses.keypad(curses.stdscr, true);
curses.start_color();
curses.init_pair(1, COLOR_BLACK, COLOR_RED);
curses.init_pair(2, COLOR_RED, COLOR_BLACK);
}
const unsigned int lines = curses.getmaxy(curses.stdscr);
curses.erase();
size_t titleLen = (menu->title ? strlen(menu->title) + 1 : 0);
_bmDrawCursesDrawLine(0, 0, "%*s%s", titleLen, "", menu->filter);
if (menu->title) {
curses.attron(COLOR_PAIR(1));
curses.mvprintw(0, 0, menu->title);
curses.attroff(COLOR_PAIR(1));
}
unsigned int i, cl = 1;
unsigned int itemsCount;
bmItem **items = bmMenuGetFilteredItems(menu, &itemsCount);
for (i = (menu->index / (lines - 1)) * (lines - 1); i < itemsCount && cl < lines; ++i) {
int highlighted = (items[i] == bmMenuGetHighlightedItem(menu));
int color = (highlighted ? 2 : (_bmMenuItemIsSelected(menu, items[i]) ? 1 : 0));
_bmDrawCursesDrawLine(color, cl++, "%s%s", (highlighted ? ">> " : " "), items[i]->text);
}
curses.move(0, titleLen + menu->cursesCursor);
curses.refresh();
}
static void _bmDrawCursesEndWin(void)
{
if (curses.refresh)
curses.refresh();
if (curses.endwin)
curses.endwin();
curses.stdscr = NULL;
freopen("/dev/tty", "w", stdout);
}
static bmKey _bmDrawCursesGetKey(unsigned int *unicode)
{
assert(unicode);
*unicode = 0;
if (!curses.stdscr)
return BM_KEY_NONE;
curses.get_wch(unicode);
switch (*unicode) {
case 16: /* C-p */
case KEY_UP:
return BM_KEY_UP;
case 14: /* C-n */
case KEY_DOWN:
return BM_KEY_DOWN;
case 2: /* C-b */
case KEY_LEFT:
return BM_KEY_LEFT;
case 6: /* C-f */
case KEY_RIGHT:
return BM_KEY_RIGHT;
case 1: /* C-a */
case KEY_HOME:
return BM_KEY_HOME;
case 5: /* C-e */
case KEY_END:
return BM_KEY_END;
case KEY_PPAGE: /* PAGE UP */
return BM_KEY_PAGE_UP;
case KEY_NPAGE: /* PAGE DOWN */
return BM_KEY_PAGE_DOWN;
case 8: /* C-h */
case KEY_BACKSPACE:
return BM_KEY_BACKSPACE;
case 4: /* C-d */
case KEY_DC:
return BM_KEY_DELETE;
case 21: /* C-u */
return BM_KEY_LINE_DELETE_LEFT;
case 11: /* C-k */
return BM_KEY_LINE_DELETE_RIGHT;
case 23: /* C-w */
return BM_KEY_WORD_DELETE;
case 9: /* Tab */
return BM_KEY_TAB;
case 18: /* C-r */
return BM_KEY_SHIFT_RETURN;
case 10: /* Return */
_bmDrawCursesEndWin();
return BM_KEY_RETURN;
case 7: /* C-g */
case 27: /* Escape */
_bmDrawCursesEndWin();
return BM_KEY_ESCAPE;
default: break;
}
return BM_KEY_UNICODE;
}
static void _bmDrawCursesFree(void)
{
_bmDrawCursesEndWin();
if (curses.handle)
dlclose(curses.handle);
sigaction(SIGABRT, &curses.action, NULL);
sigaction(SIGSEGV, &curses.action, NULL);
memset(&curses, 0, sizeof(curses));
}
static void _bmDrawCursesCrashHandler(int sig)
{
(void)sig;
_bmDrawCursesFree();
}
int _bmDrawCursesInit(struct _bmRenderApi *api)
{
memset(&curses, 0, sizeof(curses));
/* FIXME: hardcoded and not cross-platform */
curses.handle = dlopen("/usr/lib/libncursesw.so.5", RTLD_LAZY);
if (!curses.handle)
return 0;
#define bmLoadFunction(x) (curses.x = dlsym(curses.handle, #x))
if (!bmLoadFunction(initscr))
goto function_pointer_exception;
if (!bmLoadFunction(endwin))
goto function_pointer_exception;
if (!bmLoadFunction(refresh))
goto function_pointer_exception;
if (!bmLoadFunction(get_wch))
goto function_pointer_exception;
if (!bmLoadFunction(erase))
goto function_pointer_exception;
if (!bmLoadFunction(mvprintw))
goto function_pointer_exception;
if (!bmLoadFunction(move))
goto function_pointer_exception;
if (!bmLoadFunction(init_pair))
goto function_pointer_exception;
if (!bmLoadFunction(attroff))
goto function_pointer_exception;
if (!bmLoadFunction(attron))
goto function_pointer_exception;
if (!bmLoadFunction(start_color))
goto function_pointer_exception;
if (!bmLoadFunction(getmaxx))
goto function_pointer_exception;
if (!bmLoadFunction(getmaxy))
goto function_pointer_exception;
if (!bmLoadFunction(keypad))
goto function_pointer_exception;
if (!bmLoadFunction(ESCDELAY))
goto function_pointer_exception;
#undef bmLoadFunction
api->getKey = _bmDrawCursesGetKey;
api->render = _bmDrawCursesRender;
api->free = _bmDrawCursesFree;
struct sigaction action;
memset(&action, 0, sizeof(struct sigaction));
action.sa_handler = _bmDrawCursesCrashHandler;
sigaction(SIGABRT, &action, &curses.action);
sigaction(SIGSEGV, &action, &curses.action);
return 1;
function_pointer_exception:
_bmDrawCursesFree();
return 0;
}
/* vim: set ts=8 sw=4 tw=0 :*/
|