| 1 |
#include "internal.h" |
| 2 |
#include <wchar.h> |
| 3 |
#include <signal.h> |
| 4 |
#include <unistd.h> |
| 5 |
#include <string.h> |
| 6 |
#include <stdlib.h> |
| 7 |
#include <locale.h> |
| 8 |
#include <dlfcn.h> |
| 9 |
#include <assert.h> |
| 10 |
#include <math.h> |
| 11 |
|
| 12 |
#define _XOPEN_SOURCE_EXTENDED |
| 13 |
#define NCURSES_WIDECHAR 1 |
| 14 |
#include <curses.h> |
| 15 |
|
| 16 |
#if _WIN32 |
| 17 |
static const char *TTY = "CON"; |
| 18 |
#else |
| 19 |
static const char *TTY = "/dev/tty"; |
| 20 |
#endif |
| 21 |
|
| 22 |
#if NCURSES_EXT_FUNCS < 20150808 |
| 23 |
# define set_escdelay(x) ESCDELAY = (x) |
| 24 |
#endif |
| 25 |
|
| 26 |
static struct curses { |
| 27 |
WINDOW *stdscreen; |
| 28 |
struct sigaction abrt_action; |
| 29 |
struct sigaction segv_action; |
| 30 |
struct sigaction winch_action; |
| 31 |
char *buffer; |
| 32 |
size_t blen; |
| 33 |
int old_stdin; |
| 34 |
int old_stdout; |
| 35 |
bool polled_once; |
| 36 |
bool should_terminate; |
| 37 |
} curses; |
| 38 |
|
| 39 |
static inline void ignore_ret(int useless, ...) { (void)useless; } |
| 40 |
|
| 41 |
static void |
| 42 |
reopen_stdin(void) |
| 43 |
{ |
| 44 |
ignore_ret(0, freopen(TTY, "r", stdin)); |
| 45 |
} |
| 46 |
|
| 47 |
static void |
| 48 |
reopen_stdin_stdout(void) |
| 49 |
{ |
| 50 |
reopen_stdin(); |
| 51 |
ignore_ret(0, freopen(TTY, "w", stdout)); |
| 52 |
} |
| 53 |
|
| 54 |
static void |
| 55 |
store_stdin_stdout(void) |
| 56 |
{ |
| 57 |
curses.old_stdin = dup(STDIN_FILENO); |
| 58 |
curses.old_stdout = dup(STDOUT_FILENO); |
| 59 |
} |
| 60 |
|
| 61 |
static void |
| 62 |
restore_stdin(void) |
| 63 |
{ |
| 64 |
if (curses.old_stdin != -1) { |
| 65 |
dup2(curses.old_stdin, STDIN_FILENO); |
| 66 |
close(curses.old_stdin); |
| 67 |
curses.old_stdin = -1; |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
static void |
| 72 |
restore_stdin_stdout(void) |
| 73 |
{ |
| 74 |
restore_stdin(); |
| 75 |
|
| 76 |
if (curses.old_stdout != -1) { |
| 77 |
dup2(curses.old_stdout, STDOUT_FILENO); |
| 78 |
close(curses.old_stdout); |
| 79 |
curses.old_stdout = -1; |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
static void |
| 84 |
terminate(void) |
| 85 |
{ |
| 86 |
if (curses.buffer) { |
| 87 |
free(curses.buffer); |
| 88 |
curses.buffer = NULL; |
| 89 |
curses.blen = 0; |
| 90 |
} |
| 91 |
|
| 92 |
if (!curses.stdscreen) |
| 93 |
return; |
| 94 |
|
| 95 |
reopen_stdin_stdout(); |
| 96 |
refresh(); |
| 97 |
endwin(); |
| 98 |
restore_stdin_stdout(); |
| 99 |
curses.stdscreen = NULL; |
| 100 |
} |
| 101 |
|
| 102 |
static void |
| 103 |
crash_handler(int sig) |
| 104 |
{ |
| 105 |
(void)sig; |
| 106 |
terminate(); |
| 107 |
} |
| 108 |
|
| 109 |
static void |
| 110 |
resize_handler(int sig) |
| 111 |
{ |
| 112 |
(void)sig; |
| 113 |
if (!curses.stdscreen) |
| 114 |
return; |
| 115 |
|
| 116 |
refresh(); |
| 117 |
endwin(); |
| 118 |
} |
| 119 |
|
| 120 |
BM_LOG_ATTR(3, 4) static void |
| 121 |
draw_line(int32_t pair, int32_t y, const char *fmt, ...) |
| 122 |
{ |
| 123 |
assert(fmt); |
| 124 |
|
| 125 |
size_t ncols; |
| 126 |
if ((ncols = getmaxx(curses.stdscreen)) <= 0) |
| 127 |
return; |
| 128 |
|
| 129 |
va_list args; |
| 130 |
va_start(args, fmt); |
| 131 |
bool ret = bm_vrprintf(&curses.buffer, &curses.blen, fmt, args); |
| 132 |
va_end(args); |
| 133 |
|
| 134 |
if (!ret) |
| 135 |
return; |
| 136 |
|
| 137 |
size_t nlen = strlen(curses.buffer); |
| 138 |
size_t dw = 0, i = 0; |
| 139 |
while (dw < ncols && i < nlen) { |
| 140 |
if (curses.buffer[i] == '\t') curses.buffer[i] = ' '; |
| 141 |
int32_t next = bm_utf8_rune_next(curses.buffer, i); |
| 142 |
dw += bm_utf8_rune_width(curses.buffer + i, next); |
| 143 |
i += (next ? next : 1); |
| 144 |
} |
| 145 |
|
| 146 |
if (dw < ncols) { |
| 147 |
/* line is too short, widen it */ |
| 148 |
size_t offset = i + (ncols - dw); |
| 149 |
if (curses.blen <= offset && !bm_resize_buffer(&curses.buffer, &curses.blen, offset + 1)) |
| 150 |
return; |
| 151 |
|
| 152 |
memset(curses.buffer + nlen, ' ', offset - nlen); |
| 153 |
curses.buffer[offset] = 0; |
| 154 |
} else if (i < curses.blen) { |
| 155 |
/* line is too long, shorten it */ |
| 156 |
i -= bm_utf8_rune_prev(curses.buffer, i - (dw - ncols)) - 1; |
| 157 |
size_t cc = dw - (dw - ncols); |
| 158 |
|
| 159 |
size_t offset = i - (dw - ncols) + (ncols - cc) + 1; |
| 160 |
if (curses.blen <= offset) { |
| 161 |
int32_t diff = offset - curses.blen + 1; |
| 162 |
if (!bm_resize_buffer(&curses.buffer, &curses.blen, curses.blen + diff)) |
| 163 |
return; |
| 164 |
} |
| 165 |
|
| 166 |
memset(curses.buffer + i - (dw - ncols), ' ', (ncols - cc) + 1); |
| 167 |
curses.buffer[offset] = 0; |
| 168 |
} |
| 169 |
|
| 170 |
if (pair > 0) |
| 171 |
attron(COLOR_PAIR(pair)); |
| 172 |
|
| 173 |
mvprintw(y, 0, "%s", curses.buffer); |
| 174 |
|
| 175 |
if (pair > 0) |
| 176 |
attroff(COLOR_PAIR(pair)); |
| 177 |
} |
| 178 |
|
| 179 |
static bool |
| 180 |
render(struct bm_menu *menu) |
| 181 |
{ |
| 182 |
if (curses.should_terminate) { |
| 183 |
terminate(); |
| 184 |
curses.should_terminate = false; |
| 185 |
} |
| 186 |
|
| 187 |
if (!curses.stdscreen) { |
| 188 |
store_stdin_stdout(); |
| 189 |
reopen_stdin_stdout(); |
| 190 |
setlocale(LC_CTYPE, ""); |
| 191 |
|
| 192 |
if ((curses.stdscreen = initscr()) == NULL) |
| 193 |
return true; |
| 194 |
|
| 195 |
set_escdelay(25); |
| 196 |
flushinp(); |
| 197 |
keypad(curses.stdscreen, true); |
| 198 |
curs_set(1); |
| 199 |
noecho(); |
| 200 |
raw(); |
| 201 |
|
| 202 |
start_color(); |
| 203 |
use_default_colors(); |
| 204 |
init_pair(1, COLOR_BLACK, COLOR_RED); |
| 205 |
init_pair(2, COLOR_RED, -1); |
| 206 |
} |
| 207 |
|
| 208 |
erase(); |
| 209 |
|
| 210 |
uint32_t ncols = getmaxx(curses.stdscreen); |
| 211 |
uint32_t title_len = (menu->title ? strlen(menu->title) + 1 : 0); |
| 212 |
|
| 213 |
if (title_len >= ncols) |
| 214 |
title_len = 0; |
| 215 |
|
| 216 |
uint32_t ccols = ncols - title_len - 1; |
| 217 |
uint32_t dcols = 0, doffset = menu->cursor; |
| 218 |
|
| 219 |
while (doffset > 0 && dcols < ccols) { |
| 220 |
int prev = bm_utf8_rune_prev(menu->filter, doffset); |
| 221 |
dcols += bm_utf8_rune_width(menu->filter + doffset - prev, prev); |
| 222 |
doffset -= (prev ? prev : 1); |
| 223 |
} |
| 224 |
|
| 225 |
const char *filter_text = (menu->filter ? menu->filter + doffset : ""); |
| 226 |
if (menu->password) { |
| 227 |
draw_line(0, 0, "%*s", title_len, ""); |
| 228 |
} else { |
| 229 |
draw_line(0, 0, "%*s%s", title_len, "", filter_text); |
| 230 |
} |
| 231 |
|
| 232 |
if (menu->title && title_len > 0) { |
| 233 |
attron(COLOR_PAIR(1)); |
| 234 |
mvprintw(0, 0, "%s", menu->title); |
| 235 |
attroff(COLOR_PAIR(1)); |
| 236 |
} |
| 237 |
|
| 238 |
uint32_t count, cl = 0; |
| 239 |
const uint32_t lines = fmax(getmaxy(curses.stdscreen), 1) - 1; |
| 240 |
if (lines > 1) { |
| 241 |
struct bm_item **items = bm_menu_get_filtered_items(menu, &count); |
| 242 |
const bool scrollbar = (menu->scrollbar > BM_SCROLLBAR_NONE && (menu->scrollbar != BM_SCROLLBAR_AUTOHIDE || count > lines) ? true : false); |
| 243 |
const int32_t offset_x = title_len + (scrollbar && 2 > title_len ? 2 - title_len : 0); |
| 244 |
const int32_t prefix_x = (menu->prefix ? bm_utf8_string_screen_width(menu->prefix) : 0); |
| 245 |
|
| 246 |
const uint32_t page = menu->index / lines * lines; |
| 247 |
for (uint32_t i = page; i < count && cl < lines; ++i) { |
| 248 |
bool highlighted = (items[i] == bm_menu_get_highlighted_item(menu)); |
| 249 |
int32_t color = (highlighted ? 2 : (bm_menu_item_is_selected(menu, items[i]) ? 1 : 0)); |
| 250 |
|
| 251 |
if (menu->prefix && highlighted) { |
| 252 |
draw_line(color, 1 + cl++, "%*s%s %s", offset_x, "", menu->prefix, (items[i]->text ? items[i]->text : "")); |
| 253 |
} else { |
| 254 |
draw_line(color, 1 + cl++, "%*s%s%s", offset_x + prefix_x, "", (menu->prefix ? " " : ""), (items[i]->text ? items[i]->text : "")); |
| 255 |
} |
| 256 |
|
| 257 |
} |
| 258 |
|
| 259 |
if (scrollbar) { |
| 260 |
attron(COLOR_PAIR(1)); |
| 261 |
const float percent = fmin(((float)page / (count - lines)), 1.0f); |
| 262 |
const uint32_t size = fmax(lines * ((float)lines / count), 1.0f); |
| 263 |
const uint32_t posy = percent * (lines - size); |
| 264 |
for (uint32_t i = 0; i < size; ++i) |
| 265 |
mvprintw(1 + posy + i, 0, "▒"); |
| 266 |
attroff(COLOR_PAIR(1)); |
| 267 |
} |
| 268 |
} |
| 269 |
|
| 270 |
move(0, title_len + (menu->curses_cursor < ccols ? menu->curses_cursor : ccols)); |
| 271 |
refresh(); |
| 272 |
|
| 273 |
// Make it possible to read stdin even after rendering |
| 274 |
// Only make it impossible to read original stdin after poll_key is called once |
| 275 |
// This is mainly to make -f work even on curses backend |
| 276 |
if (!curses.polled_once) { |
| 277 |
reopen_stdin(); |
| 278 |
restore_stdin(); |
| 279 |
curses.should_terminate = true; |
| 280 |
} |
| 281 |
|
| 282 |
return true; |
| 283 |
} |
| 284 |
|
| 285 |
static uint32_t |
| 286 |
get_displayed_count(const struct bm_menu *menu) |
| 287 |
{ |
| 288 |
(void)menu; |
| 289 |
return (curses.stdscreen ? getmaxy(curses.stdscreen) : 0); |
| 290 |
} |
| 291 |
|
| 292 |
static enum bm_key |
| 293 |
poll_key(const struct bm_menu *menu, uint32_t *unicode) |
| 294 |
{ |
| 295 |
(void)menu; |
| 296 |
assert(unicode); |
| 297 |
*unicode = 0; |
| 298 |
curses.polled_once = true; |
| 299 |
|
| 300 |
if (!curses.stdscreen || curses.should_terminate) |
| 301 |
return BM_KEY_NONE; |
| 302 |
|
| 303 |
get_wch((wint_t*)unicode); |
| 304 |
|
| 305 |
switch (*unicode) { |
| 306 |
#if KEY_RESIZE |
| 307 |
case KEY_RESIZE: |
| 308 |
return BM_KEY_NONE; |
| 309 |
#endif |
| 310 |
|
| 311 |
case 16: /* C-p */ |
| 312 |
case KEY_UP: |
| 313 |
return BM_KEY_UP; |
| 314 |
|
| 315 |
case 14: /* C-n */ |
| 316 |
case KEY_DOWN: |
| 317 |
return BM_KEY_DOWN; |
| 318 |
|
| 319 |
case 2: /* C-b */ |
| 320 |
case KEY_LEFT: |
| 321 |
return BM_KEY_LEFT; |
| 322 |
|
| 323 |
case 6: /* C-f */ |
| 324 |
case KEY_RIGHT: |
| 325 |
return BM_KEY_RIGHT; |
| 326 |
|
| 327 |
case 1: /* C-a */ |
| 328 |
case 391: /* S-Home */ |
| 329 |
case KEY_HOME: |
| 330 |
return BM_KEY_HOME; |
| 331 |
|
| 332 |
case 5: /* C-e */ |
| 333 |
case 386: /* S-End */ |
| 334 |
case KEY_END: |
| 335 |
return BM_KEY_END; |
| 336 |
|
| 337 |
case KEY_PPAGE: /* Page up */ |
| 338 |
return BM_KEY_PAGE_UP; |
| 339 |
|
| 340 |
case 22: /* C-v */ |
| 341 |
case KEY_NPAGE: /* Page down */ |
| 342 |
return BM_KEY_PAGE_DOWN; |
| 343 |
|
| 344 |
case 550: /* C-Page up */ |
| 345 |
case 398: /* S-Page up */ |
| 346 |
return BM_KEY_SHIFT_PAGE_UP; |
| 347 |
|
| 348 |
case 545: /* C-Page down */ |
| 349 |
case 396: /* S-Page down */ |
| 350 |
return BM_KEY_SHIFT_PAGE_DOWN; |
| 351 |
|
| 352 |
case 8: /* C-h */ |
| 353 |
case 127: /* Delete */ |
| 354 |
case KEY_BACKSPACE: |
| 355 |
return BM_KEY_BACKSPACE; |
| 356 |
|
| 357 |
case 4: /* C-d */ |
| 358 |
case KEY_DC: |
| 359 |
return BM_KEY_DELETE; |
| 360 |
|
| 361 |
case 383: /* S-Del */ |
| 362 |
case 21: /* C-u */ |
| 363 |
return BM_KEY_LINE_DELETE_LEFT; |
| 364 |
|
| 365 |
case 11: /* C-k */ |
| 366 |
return BM_KEY_LINE_DELETE_RIGHT; |
| 367 |
|
| 368 |
case 23: /* C-w */ |
| 369 |
return BM_KEY_WORD_DELETE; |
| 370 |
|
| 371 |
case 9: /* Tab */ |
| 372 |
return BM_KEY_TAB; |
| 373 |
|
| 374 |
case 353: /* S-Tab */ |
| 375 |
return BM_KEY_SHIFT_TAB; |
| 376 |
|
| 377 |
case 18: /* C-r */ |
| 378 |
return BM_KEY_CONTROL_RETURN; |
| 379 |
|
| 380 |
case 20: /* C-t */ |
| 381 |
case 331: /* Insert */ |
| 382 |
terminate(); |
| 383 |
return BM_KEY_SHIFT_RETURN; |
| 384 |
|
| 385 |
case 10: /* Return */ |
| 386 |
case 13: /* C-m */ |
| 387 |
terminate(); |
| 388 |
return BM_KEY_RETURN; |
| 389 |
|
| 390 |
case 7: /* C-g */ |
| 391 |
case 27: /* Escape */ |
| 392 |
terminate(); |
| 393 |
return BM_KEY_ESCAPE; |
| 394 |
|
| 395 |
default: break; |
| 396 |
} |
| 397 |
|
| 398 |
return BM_KEY_UNICODE; |
| 399 |
} |
| 400 |
|
| 401 |
static void |
| 402 |
destructor(struct bm_menu *menu) |
| 403 |
{ |
| 404 |
(void)menu; |
| 405 |
terminate(); |
| 406 |
sigaction(SIGABRT, &curses.abrt_action, NULL); |
| 407 |
sigaction(SIGSEGV, &curses.segv_action, NULL); |
| 408 |
sigaction(SIGWINCH, &curses.winch_action, NULL); |
| 409 |
memset(&curses, 0, sizeof(curses)); |
| 410 |
} |
| 411 |
|
| 412 |
static bool |
| 413 |
constructor(struct bm_menu *menu) |
| 414 |
{ |
| 415 |
(void)menu; |
| 416 |
assert(!curses.stdscreen && "bemenu supports only one curses instance"); |
| 417 |
|
| 418 |
memset(&curses, 0, sizeof(curses)); |
| 419 |
curses.old_stdin = -1; |
| 420 |
curses.old_stdout = -1; |
| 421 |
|
| 422 |
struct sigaction action; |
| 423 |
memset(&action, 0, sizeof(struct sigaction)); |
| 424 |
action.sa_handler = crash_handler; |
| 425 |
sigaction(SIGABRT, &action, &curses.abrt_action); |
| 426 |
sigaction(SIGSEGV, &action, &curses.segv_action); |
| 427 |
|
| 428 |
action.sa_handler = resize_handler; |
| 429 |
sigaction(SIGWINCH, &action, &curses.winch_action); |
| 430 |
return true; |
| 431 |
} |
| 432 |
|
| 433 |
BM_PUBLIC extern const char* |
| 434 |
register_renderer(struct render_api *api) |
| 435 |
{ |
| 436 |
api->constructor = constructor; |
| 437 |
api->destructor = destructor; |
| 438 |
api->get_displayed_count = get_displayed_count; |
| 439 |
api->poll_key = poll_key; |
| 440 |
api->render = render; |
| 441 |
api->priorty = BM_PRIO_TERMINAL; |
| 442 |
api->version = BM_PLUGIN_VERSION; |
| 443 |
return "curses"; |
| 444 |
} |
| 445 |
|
| 446 |
/* vim: set ts=8 sw=4 tw=0 :*/ |