nconf.gui.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2008 Nir Tzachar <nir.tzachar@gmail.com>
  4. *
  5. * Derived from menuconfig.
  6. */
  7. #include "nconf.h"
  8. #include "lkc.h"
  9. int attr_normal;
  10. int attr_main_heading;
  11. int attr_main_menu_box;
  12. int attr_main_menu_fore;
  13. int attr_main_menu_back;
  14. int attr_main_menu_grey;
  15. int attr_main_menu_heading;
  16. int attr_scrollwin_text;
  17. int attr_scrollwin_heading;
  18. int attr_scrollwin_box;
  19. int attr_dialog_text;
  20. int attr_dialog_menu_fore;
  21. int attr_dialog_menu_back;
  22. int attr_dialog_box;
  23. int attr_input_box;
  24. int attr_input_heading;
  25. int attr_input_text;
  26. int attr_input_field;
  27. int attr_function_text;
  28. int attr_function_highlight;
  29. #define COLOR_ATTR(_at, _fg, _bg, _hl) \
  30. { .attr = &(_at), .has_color = true, .color_fg = _fg, .color_bg = _bg, .highlight = _hl }
  31. #define NO_COLOR_ATTR(_at, _hl) \
  32. { .attr = &(_at), .has_color = false, .highlight = _hl }
  33. #define COLOR_DEFAULT -1
  34. struct nconf_attr_param {
  35. int *attr;
  36. bool has_color;
  37. int color_fg;
  38. int color_bg;
  39. int highlight;
  40. };
  41. static const struct nconf_attr_param color_theme_params[] = {
  42. COLOR_ATTR(attr_normal, COLOR_DEFAULT, COLOR_DEFAULT, A_NORMAL),
  43. COLOR_ATTR(attr_main_heading, COLOR_MAGENTA, COLOR_DEFAULT, A_BOLD | A_UNDERLINE),
  44. COLOR_ATTR(attr_main_menu_box, COLOR_YELLOW, COLOR_DEFAULT, A_NORMAL),
  45. COLOR_ATTR(attr_main_menu_fore, COLOR_DEFAULT, COLOR_DEFAULT, A_REVERSE),
  46. COLOR_ATTR(attr_main_menu_back, COLOR_DEFAULT, COLOR_DEFAULT, A_NORMAL),
  47. COLOR_ATTR(attr_main_menu_grey, COLOR_DEFAULT, COLOR_DEFAULT, A_NORMAL),
  48. COLOR_ATTR(attr_main_menu_heading, COLOR_GREEN, COLOR_DEFAULT, A_BOLD),
  49. COLOR_ATTR(attr_scrollwin_text, COLOR_DEFAULT, COLOR_DEFAULT, A_NORMAL),
  50. COLOR_ATTR(attr_scrollwin_heading, COLOR_GREEN, COLOR_DEFAULT, A_BOLD),
  51. COLOR_ATTR(attr_scrollwin_box, COLOR_YELLOW, COLOR_DEFAULT, A_BOLD),
  52. COLOR_ATTR(attr_dialog_text, COLOR_DEFAULT, COLOR_DEFAULT, A_BOLD),
  53. COLOR_ATTR(attr_dialog_menu_fore, COLOR_RED, COLOR_DEFAULT, A_STANDOUT),
  54. COLOR_ATTR(attr_dialog_menu_back, COLOR_YELLOW, COLOR_DEFAULT, A_NORMAL),
  55. COLOR_ATTR(attr_dialog_box, COLOR_YELLOW, COLOR_DEFAULT, A_BOLD),
  56. COLOR_ATTR(attr_input_box, COLOR_YELLOW, COLOR_DEFAULT, A_NORMAL),
  57. COLOR_ATTR(attr_input_heading, COLOR_GREEN, COLOR_DEFAULT, A_BOLD),
  58. COLOR_ATTR(attr_input_text, COLOR_DEFAULT, COLOR_DEFAULT, A_NORMAL),
  59. COLOR_ATTR(attr_input_field, COLOR_DEFAULT, COLOR_DEFAULT, A_UNDERLINE),
  60. COLOR_ATTR(attr_function_text, COLOR_YELLOW, COLOR_DEFAULT, A_REVERSE),
  61. COLOR_ATTR(attr_function_highlight, COLOR_DEFAULT, COLOR_DEFAULT, A_BOLD),
  62. { /* sentinel */ }
  63. };
  64. static const struct nconf_attr_param no_color_theme_params[] = {
  65. NO_COLOR_ATTR(attr_normal, A_NORMAL),
  66. NO_COLOR_ATTR(attr_main_heading, A_BOLD | A_UNDERLINE),
  67. NO_COLOR_ATTR(attr_main_menu_box, A_NORMAL),
  68. NO_COLOR_ATTR(attr_main_menu_fore, A_STANDOUT),
  69. NO_COLOR_ATTR(attr_main_menu_back, A_NORMAL),
  70. NO_COLOR_ATTR(attr_main_menu_grey, A_NORMAL),
  71. NO_COLOR_ATTR(attr_main_menu_heading, A_BOLD),
  72. NO_COLOR_ATTR(attr_scrollwin_text, A_NORMAL),
  73. NO_COLOR_ATTR(attr_scrollwin_heading, A_BOLD),
  74. NO_COLOR_ATTR(attr_scrollwin_box, A_BOLD),
  75. NO_COLOR_ATTR(attr_dialog_text, A_NORMAL),
  76. NO_COLOR_ATTR(attr_dialog_menu_fore, A_STANDOUT),
  77. NO_COLOR_ATTR(attr_dialog_menu_back, A_NORMAL),
  78. NO_COLOR_ATTR(attr_dialog_box, A_BOLD),
  79. NO_COLOR_ATTR(attr_input_box, A_BOLD),
  80. NO_COLOR_ATTR(attr_input_heading, A_BOLD),
  81. NO_COLOR_ATTR(attr_input_text, A_NORMAL),
  82. NO_COLOR_ATTR(attr_input_field, A_UNDERLINE),
  83. NO_COLOR_ATTR(attr_function_text, A_REVERSE),
  84. NO_COLOR_ATTR(attr_function_highlight, A_BOLD),
  85. { /* sentinel */ }
  86. };
  87. void set_colors(void)
  88. {
  89. const struct nconf_attr_param *p;
  90. int pair = 0;
  91. if (has_colors()) {
  92. start_color();
  93. use_default_colors();
  94. p = color_theme_params;
  95. } else {
  96. p = no_color_theme_params;
  97. }
  98. for (; p->attr; p++) {
  99. int attr = p->highlight;
  100. if (p->has_color) {
  101. pair++;
  102. init_pair(pair, p->color_fg, p->color_bg);
  103. attr |= COLOR_PAIR(pair);
  104. }
  105. *p->attr = attr;
  106. }
  107. }
  108. /* this changes the windows attributes !!! */
  109. void print_in_middle(WINDOW *win, int y, int width, const char *str, int attrs)
  110. {
  111. wattrset(win, attrs);
  112. mvwprintw(win, y, (width - strlen(str)) / 2, "%s", str);
  113. }
  114. int get_line_no(const char *text)
  115. {
  116. int i;
  117. int total = 1;
  118. if (!text)
  119. return 0;
  120. for (i = 0; text[i] != '\0'; i++)
  121. if (text[i] == '\n')
  122. total++;
  123. return total;
  124. }
  125. const char *get_line(const char *text, int line_no)
  126. {
  127. int i;
  128. int lines = 0;
  129. if (!text)
  130. return NULL;
  131. for (i = 0; text[i] != '\0' && lines < line_no; i++)
  132. if (text[i] == '\n')
  133. lines++;
  134. return text+i;
  135. }
  136. int get_line_length(const char *line)
  137. {
  138. int res = 0;
  139. while (*line != '\0' && *line != '\n') {
  140. line++;
  141. res++;
  142. }
  143. return res;
  144. }
  145. /* print all lines to the window. */
  146. void fill_window(WINDOW *win, const char *text)
  147. {
  148. int x, y;
  149. int total_lines = get_line_no(text);
  150. int i;
  151. getmaxyx(win, y, x);
  152. /* do not go over end of line */
  153. total_lines = min(total_lines, y);
  154. for (i = 0; i < total_lines; i++) {
  155. char tmp[x+10];
  156. const char *line = get_line(text, i);
  157. int len = get_line_length(line);
  158. strncpy(tmp, line, min(len, x));
  159. tmp[len] = '\0';
  160. mvwprintw(win, i, 0, "%s", tmp);
  161. }
  162. }
  163. /* get the message, and buttons.
  164. * each button must be a char*
  165. * return the selected button
  166. *
  167. * this dialog is used for 2 different things:
  168. * 1) show a text box, no buttons.
  169. * 2) show a dialog, with horizontal buttons
  170. */
  171. int btn_dialog(WINDOW *main_window, const char *msg, int btn_num, ...)
  172. {
  173. va_list ap;
  174. char *btn;
  175. int btns_width = 0;
  176. int msg_lines = 0;
  177. int msg_width = 0;
  178. int total_width;
  179. int win_rows = 0;
  180. WINDOW *win;
  181. WINDOW *msg_win;
  182. WINDOW *menu_win;
  183. MENU *menu;
  184. ITEM *btns[btn_num+1];
  185. int i, x, y;
  186. int res = -1;
  187. va_start(ap, btn_num);
  188. for (i = 0; i < btn_num; i++) {
  189. btn = va_arg(ap, char *);
  190. btns[i] = new_item(btn, "");
  191. btns_width += strlen(btn)+1;
  192. }
  193. va_end(ap);
  194. btns[btn_num] = NULL;
  195. /* find the widest line of msg: */
  196. msg_lines = get_line_no(msg);
  197. for (i = 0; i < msg_lines; i++) {
  198. const char *line = get_line(msg, i);
  199. int len = get_line_length(line);
  200. if (msg_width < len)
  201. msg_width = len;
  202. }
  203. total_width = max(msg_width, btns_width);
  204. /* place dialog in middle of screen */
  205. y = (getmaxy(stdscr)-(msg_lines+4))/2;
  206. x = (getmaxx(stdscr)-(total_width+4))/2;
  207. /* create the windows */
  208. if (btn_num > 0)
  209. win_rows = msg_lines+4;
  210. else
  211. win_rows = msg_lines+2;
  212. win = newwin(win_rows, total_width+4, y, x);
  213. keypad(win, TRUE);
  214. menu_win = derwin(win, 1, btns_width, win_rows-2,
  215. 1+(total_width+2-btns_width)/2);
  216. menu = new_menu(btns);
  217. msg_win = derwin(win, win_rows-2, msg_width, 1,
  218. 1+(total_width+2-msg_width)/2);
  219. set_menu_fore(menu, attr_dialog_menu_fore);
  220. set_menu_back(menu, attr_dialog_menu_back);
  221. wattrset(win, attr_dialog_box);
  222. box(win, 0, 0);
  223. /* print message */
  224. wattrset(msg_win, attr_dialog_text);
  225. fill_window(msg_win, msg);
  226. set_menu_win(menu, win);
  227. set_menu_sub(menu, menu_win);
  228. set_menu_format(menu, 1, btn_num);
  229. menu_opts_off(menu, O_SHOWDESC);
  230. menu_opts_off(menu, O_SHOWMATCH);
  231. menu_opts_on(menu, O_ONEVALUE);
  232. menu_opts_on(menu, O_NONCYCLIC);
  233. set_menu_mark(menu, "");
  234. post_menu(menu);
  235. touchwin(win);
  236. refresh_all_windows(main_window);
  237. while ((res = wgetch(win))) {
  238. switch (res) {
  239. case KEY_LEFT:
  240. menu_driver(menu, REQ_LEFT_ITEM);
  241. break;
  242. case KEY_RIGHT:
  243. menu_driver(menu, REQ_RIGHT_ITEM);
  244. break;
  245. case 10: /* ENTER */
  246. case 27: /* ESCAPE */
  247. case ' ':
  248. case KEY_F(F_BACK):
  249. case KEY_F(F_EXIT):
  250. break;
  251. }
  252. touchwin(win);
  253. refresh_all_windows(main_window);
  254. if (res == 10 || res == ' ') {
  255. res = item_index(current_item(menu));
  256. break;
  257. } else if (res == 27 || res == KEY_F(F_BACK) ||
  258. res == KEY_F(F_EXIT)) {
  259. res = KEY_EXIT;
  260. break;
  261. }
  262. }
  263. unpost_menu(menu);
  264. free_menu(menu);
  265. for (i = 0; i < btn_num; i++)
  266. free_item(btns[i]);
  267. delwin(win);
  268. return res;
  269. }
  270. int dialog_inputbox(WINDOW *main_window,
  271. const char *title, const char *prompt,
  272. const char *init, char **resultp, int *result_len)
  273. {
  274. int prompt_lines = 0;
  275. int prompt_width = 0;
  276. WINDOW *win;
  277. WINDOW *prompt_win;
  278. WINDOW *form_win;
  279. PANEL *panel;
  280. int i, x, y, lines, columns, win_lines, win_cols;
  281. int res = -1;
  282. int cursor_position = strlen(init);
  283. int cursor_form_win;
  284. char *result = *resultp;
  285. getmaxyx(stdscr, lines, columns);
  286. if (strlen(init)+1 > *result_len) {
  287. *result_len = strlen(init)+1;
  288. *resultp = result = xrealloc(result, *result_len);
  289. }
  290. /* find the widest line of msg: */
  291. prompt_lines = get_line_no(prompt);
  292. for (i = 0; i < prompt_lines; i++) {
  293. const char *line = get_line(prompt, i);
  294. int len = get_line_length(line);
  295. prompt_width = max(prompt_width, len);
  296. }
  297. if (title)
  298. prompt_width = max(prompt_width, strlen(title));
  299. win_lines = min(prompt_lines+6, lines-2);
  300. win_cols = min(prompt_width+7, columns-2);
  301. prompt_lines = max(win_lines-6, 0);
  302. prompt_width = max(win_cols-7, 0);
  303. /* place dialog in middle of screen */
  304. y = (lines-win_lines)/2;
  305. x = (columns-win_cols)/2;
  306. strncpy(result, init, *result_len);
  307. /* create the windows */
  308. win = newwin(win_lines, win_cols, y, x);
  309. prompt_win = derwin(win, prompt_lines+1, prompt_width, 2, 2);
  310. form_win = derwin(win, 1, prompt_width, prompt_lines+3, 2);
  311. keypad(form_win, TRUE);
  312. wattrset(form_win, attr_input_field);
  313. wattrset(win, attr_input_box);
  314. box(win, 0, 0);
  315. wattrset(win, attr_input_heading);
  316. if (title)
  317. mvwprintw(win, 0, 3, "%s", title);
  318. /* print message */
  319. wattrset(prompt_win, attr_input_text);
  320. fill_window(prompt_win, prompt);
  321. mvwprintw(form_win, 0, 0, "%*s", prompt_width, " ");
  322. cursor_form_win = min(cursor_position, prompt_width-1);
  323. mvwprintw(form_win, 0, 0, "%s",
  324. result + cursor_position-cursor_form_win);
  325. /* create panels */
  326. panel = new_panel(win);
  327. /* show the cursor */
  328. curs_set(1);
  329. touchwin(win);
  330. refresh_all_windows(main_window);
  331. while ((res = wgetch(form_win))) {
  332. int len = strlen(result);
  333. switch (res) {
  334. case 10: /* ENTER */
  335. case 27: /* ESCAPE */
  336. case KEY_F(F_HELP):
  337. case KEY_F(F_EXIT):
  338. case KEY_F(F_BACK):
  339. break;
  340. case 8: /* ^H */
  341. case 127: /* ^? */
  342. case KEY_BACKSPACE:
  343. if (cursor_position > 0) {
  344. memmove(&result[cursor_position-1],
  345. &result[cursor_position],
  346. len-cursor_position+1);
  347. cursor_position--;
  348. cursor_form_win--;
  349. len--;
  350. }
  351. break;
  352. case KEY_DC:
  353. if (cursor_position >= 0 && cursor_position < len) {
  354. memmove(&result[cursor_position],
  355. &result[cursor_position+1],
  356. len-cursor_position+1);
  357. len--;
  358. }
  359. break;
  360. case KEY_UP:
  361. case KEY_RIGHT:
  362. if (cursor_position < len) {
  363. cursor_position++;
  364. cursor_form_win++;
  365. }
  366. break;
  367. case KEY_DOWN:
  368. case KEY_LEFT:
  369. if (cursor_position > 0) {
  370. cursor_position--;
  371. cursor_form_win--;
  372. }
  373. break;
  374. case KEY_HOME:
  375. cursor_position = 0;
  376. cursor_form_win = 0;
  377. break;
  378. case KEY_END:
  379. cursor_position = len;
  380. cursor_form_win = min(cursor_position, prompt_width-1);
  381. break;
  382. default:
  383. if ((isgraph(res) || isspace(res))) {
  384. /* one for new char, one for '\0' */
  385. if (len+2 > *result_len) {
  386. *result_len = len+2;
  387. *resultp = result = realloc(result,
  388. *result_len);
  389. }
  390. /* insert the char at the proper position */
  391. memmove(&result[cursor_position+1],
  392. &result[cursor_position],
  393. len-cursor_position+1);
  394. result[cursor_position] = res;
  395. cursor_position++;
  396. cursor_form_win++;
  397. len++;
  398. } else {
  399. mvprintw(0, 0, "unknown key: %d\n", res);
  400. }
  401. break;
  402. }
  403. if (cursor_form_win < 0)
  404. cursor_form_win = 0;
  405. else if (cursor_form_win > prompt_width-1)
  406. cursor_form_win = prompt_width-1;
  407. wmove(form_win, 0, 0);
  408. wclrtoeol(form_win);
  409. mvwprintw(form_win, 0, 0, "%*s", prompt_width, " ");
  410. mvwprintw(form_win, 0, 0, "%s",
  411. result + cursor_position-cursor_form_win);
  412. wmove(form_win, 0, cursor_form_win);
  413. touchwin(win);
  414. refresh_all_windows(main_window);
  415. if (res == 10) {
  416. res = 0;
  417. break;
  418. } else if (res == 27 || res == KEY_F(F_BACK) ||
  419. res == KEY_F(F_EXIT)) {
  420. res = KEY_EXIT;
  421. break;
  422. } else if (res == KEY_F(F_HELP)) {
  423. res = 1;
  424. break;
  425. }
  426. }
  427. /* hide the cursor */
  428. curs_set(0);
  429. del_panel(panel);
  430. delwin(prompt_win);
  431. delwin(form_win);
  432. delwin(win);
  433. return res;
  434. }
  435. /* refresh all windows in the correct order */
  436. void refresh_all_windows(WINDOW *main_window)
  437. {
  438. update_panels();
  439. touchwin(main_window);
  440. refresh();
  441. }
  442. /* layman's scrollable window... */
  443. void show_scroll_win(WINDOW *main_window,
  444. const char *title,
  445. const char *text)
  446. {
  447. int res;
  448. int total_lines = get_line_no(text);
  449. int x, y, lines, columns;
  450. int start_x = 0, start_y = 0;
  451. int text_lines = 0, text_cols = 0;
  452. int total_cols = 0;
  453. int win_cols = 0;
  454. int win_lines = 0;
  455. int i = 0;
  456. WINDOW *win;
  457. WINDOW *pad;
  458. PANEL *panel;
  459. getmaxyx(stdscr, lines, columns);
  460. /* find the widest line of msg: */
  461. total_lines = get_line_no(text);
  462. for (i = 0; i < total_lines; i++) {
  463. const char *line = get_line(text, i);
  464. int len = get_line_length(line);
  465. total_cols = max(total_cols, len+2);
  466. }
  467. /* create the pad */
  468. pad = newpad(total_lines+10, total_cols+10);
  469. wattrset(pad, attr_scrollwin_text);
  470. fill_window(pad, text);
  471. win_lines = min(total_lines+4, lines-2);
  472. win_cols = min(total_cols+2, columns-2);
  473. text_lines = max(win_lines-4, 0);
  474. text_cols = max(win_cols-2, 0);
  475. /* place window in middle of screen */
  476. y = (lines-win_lines)/2;
  477. x = (columns-win_cols)/2;
  478. win = newwin(win_lines, win_cols, y, x);
  479. keypad(win, TRUE);
  480. /* show the help in the help window, and show the help panel */
  481. wattrset(win, attr_scrollwin_box);
  482. box(win, 0, 0);
  483. wattrset(win, attr_scrollwin_heading);
  484. mvwprintw(win, 0, 3, " %s ", title);
  485. panel = new_panel(win);
  486. /* handle scrolling */
  487. do {
  488. copywin(pad, win, start_y, start_x, 2, 2, text_lines,
  489. text_cols, 0);
  490. print_in_middle(win,
  491. text_lines+2,
  492. text_cols,
  493. "<OK>",
  494. attr_dialog_menu_fore);
  495. wrefresh(win);
  496. res = wgetch(win);
  497. switch (res) {
  498. case KEY_NPAGE:
  499. case ' ':
  500. case 'd':
  501. start_y += text_lines-2;
  502. break;
  503. case KEY_PPAGE:
  504. case 'u':
  505. start_y -= text_lines+2;
  506. break;
  507. case KEY_HOME:
  508. start_y = 0;
  509. break;
  510. case KEY_END:
  511. start_y = total_lines-text_lines;
  512. break;
  513. case KEY_DOWN:
  514. case 'j':
  515. start_y++;
  516. break;
  517. case KEY_UP:
  518. case 'k':
  519. start_y--;
  520. break;
  521. case KEY_LEFT:
  522. case 'h':
  523. start_x--;
  524. break;
  525. case KEY_RIGHT:
  526. case 'l':
  527. start_x++;
  528. break;
  529. }
  530. if (res == 10 || res == 27 || res == 'q' ||
  531. res == KEY_F(F_HELP) || res == KEY_F(F_BACK) ||
  532. res == KEY_F(F_EXIT))
  533. break;
  534. if (start_y < 0)
  535. start_y = 0;
  536. if (start_y >= total_lines-text_lines)
  537. start_y = total_lines-text_lines;
  538. if (start_x < 0)
  539. start_x = 0;
  540. if (start_x >= total_cols-text_cols)
  541. start_x = total_cols-text_cols;
  542. } while (res);
  543. del_panel(panel);
  544. delwin(win);
  545. refresh_all_windows(main_window);
  546. }