You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

settings_ui.c 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592
  1. /*
  2. * Copyright (C) 2006 Michael Brown <mbrown@fensystems.co.uk>.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License as
  6. * published by the Free Software Foundation; either version 2 of the
  7. * License, or any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  17. * 02110-1301, USA.
  18. */
  19. FILE_LICENCE ( GPL2_OR_LATER );
  20. #include <stdio.h>
  21. #include <stdarg.h>
  22. #include <unistd.h>
  23. #include <string.h>
  24. #include <curses.h>
  25. #include <ipxe/console.h>
  26. #include <ipxe/settings.h>
  27. #include <ipxe/editbox.h>
  28. #include <ipxe/keys.h>
  29. #include <ipxe/settings_ui.h>
  30. #include <config/colour.h>
  31. /** @file
  32. *
  33. * Option configuration console
  34. *
  35. */
  36. /* Colour pairs */
  37. #define CPAIR_NORMAL 1
  38. #define CPAIR_SELECT 2
  39. #define CPAIR_EDIT 3
  40. #define CPAIR_ALERT 4
  41. #define CPAIR_URL 5
  42. /* Screen layout */
  43. #define TITLE_ROW 1
  44. #define SETTINGS_LIST_ROW 3
  45. #define SETTINGS_LIST_COL 1
  46. #define SETTINGS_LIST_ROWS 15
  47. #define INFO_ROW 19
  48. #define ALERT_ROW 22
  49. #define INSTRUCTION_ROW 22
  50. #define INSTRUCTION_PAD " "
  51. /** Layout of text within a setting widget */
  52. struct setting_row_text {
  53. char start[0];
  54. char pad1[1];
  55. char name[15];
  56. char pad2[1];
  57. char value[60];
  58. char pad3[1];
  59. char nul;
  60. } __attribute__ (( packed ));
  61. /** A setting row widget */
  62. struct setting_row_widget {
  63. /** Target configuration settings block
  64. *
  65. * Valid only for rows that lead to new settings blocks.
  66. */
  67. struct settings *settings;
  68. /** Configuration setting origin
  69. *
  70. * Valid only for rows that represent individual settings.
  71. */
  72. struct settings *origin;
  73. /** Configuration setting
  74. *
  75. * Valid only for rows that represent individual settings.
  76. */
  77. struct setting setting;
  78. /** Screen row */
  79. unsigned int row;
  80. /** Screen column */
  81. unsigned int col;
  82. /** Edit box widget used for editing setting */
  83. struct edit_box editbox;
  84. /** Editing in progress flag */
  85. int editing;
  86. /** Buffer for setting's value */
  87. char value[256]; /* enough size for a DHCP string */
  88. };
  89. /** A settings widget */
  90. struct setting_widget {
  91. /** Settings block */
  92. struct settings *settings;
  93. /** Number of rows */
  94. unsigned int num_rows;
  95. /** Current row index */
  96. unsigned int current;
  97. /** Index of the first visible row, for scrolling. */
  98. unsigned int first_visible;
  99. /** Active row */
  100. struct setting_row_widget row;
  101. };
  102. /**
  103. * Select a setting row
  104. *
  105. * @v widget Setting widget
  106. * @v index Index of setting row
  107. * @ret count Number of settings rows
  108. */
  109. static unsigned int select_setting_row ( struct setting_widget *widget,
  110. unsigned int index ) {
  111. struct settings *settings;
  112. struct setting *setting;
  113. unsigned int count = 0;
  114. /* Initialise structure */
  115. memset ( &widget->row, 0, sizeof ( widget->row ) );
  116. widget->current = index;
  117. widget->row.row = ( SETTINGS_LIST_ROW + index - widget->first_visible );
  118. widget->row.col = SETTINGS_LIST_COL;
  119. /* Include parent settings block, if applicable */
  120. if ( widget->settings->parent && ( count++ == index ) ) {
  121. widget->row.settings = widget->settings->parent;
  122. snprintf ( widget->row.value, sizeof ( widget->row.value ),
  123. "../" );
  124. }
  125. /* Include any child settings blocks, if applicable */
  126. list_for_each_entry ( settings, &widget->settings->children, siblings ){
  127. if ( count++ == index ) {
  128. widget->row.settings = settings;
  129. snprintf ( widget->row.value,
  130. sizeof ( widget->row.value ), "%s/",
  131. settings->name );
  132. }
  133. }
  134. /* Include any applicable settings */
  135. for_each_table_entry ( setting, SETTINGS ) {
  136. if ( ! setting_applies ( widget->settings, setting ) )
  137. continue;
  138. if ( count++ == index ) {
  139. /* Read current setting value and origin */
  140. fetchf_setting ( widget->settings, setting,
  141. &widget->row.origin,
  142. &widget->row.setting,
  143. widget->row.value,
  144. sizeof ( widget->row.value ) );
  145. }
  146. }
  147. /* Initialise edit box */
  148. init_editbox ( &widget->row.editbox, widget->row.value,
  149. sizeof ( widget->row.value ), NULL, widget->row.row,
  150. ( widget->row.col +
  151. offsetof ( struct setting_row_text, value ) ),
  152. sizeof ( ( ( struct setting_row_text * ) NULL )->value ),
  153. 0 );
  154. return count;
  155. }
  156. static size_t string_copy ( char *dest, const char *src, size_t len ) {
  157. size_t src_len;
  158. src_len = strlen ( src );
  159. if ( len > src_len )
  160. len = src_len;
  161. memcpy ( dest, src, len );
  162. return len;
  163. }
  164. /**
  165. * Draw setting row
  166. *
  167. * @v widget Setting widget
  168. */
  169. static void draw_setting_row ( struct setting_widget *widget ) {
  170. struct setting_row_text text;
  171. unsigned int curs_offset;
  172. char *value;
  173. /* Fill row with spaces */
  174. memset ( &text, ' ', sizeof ( text ) );
  175. text.nul = '\0';
  176. /* Construct row content */
  177. if ( widget->row.settings ) {
  178. /* Construct space-padded name */
  179. curs_offset = ( offsetof ( typeof ( text ), name ) +
  180. string_copy ( text.name, widget->row.value,
  181. sizeof ( text.name ) ) );
  182. } else {
  183. /* Construct dot-padded name */
  184. memset ( text.name, '.', sizeof ( text.name ) );
  185. string_copy ( text.name, widget->row.setting.name,
  186. sizeof ( text.name ) );
  187. /* Construct space-padded value */
  188. value = widget->row.value;
  189. if ( ! *value )
  190. value = "<not specified>";
  191. curs_offset = ( offsetof ( typeof ( text ), value ) +
  192. string_copy ( text.value, value,
  193. sizeof ( text.value ) ) );
  194. }
  195. /* Print row */
  196. if ( ( widget->row.origin == widget->settings ) ||
  197. ( widget->row.settings != NULL ) ) {
  198. attron ( A_BOLD );
  199. }
  200. mvprintw ( widget->row.row, widget->row.col, "%s", text.start );
  201. attroff ( A_BOLD );
  202. move ( widget->row.row, widget->row.col + curs_offset );
  203. }
  204. /**
  205. * Edit setting widget
  206. *
  207. * @v widget Setting widget
  208. * @v key Key pressed by user
  209. * @ret key Key returned to application, or zero
  210. */
  211. static int edit_setting ( struct setting_widget *widget, int key ) {
  212. assert ( widget->row.setting.name != NULL );
  213. widget->row.editing = 1;
  214. return edit_editbox ( &widget->row.editbox, key );
  215. }
  216. /**
  217. * Save setting widget value back to configuration settings
  218. *
  219. * @v widget Setting widget
  220. */
  221. static int save_setting ( struct setting_widget *widget ) {
  222. assert ( widget->row.setting.name != NULL );
  223. return storef_setting ( widget->settings, &widget->row.setting,
  224. widget->row.value );
  225. }
  226. /**
  227. * Print message centred on specified row
  228. *
  229. * @v row Row
  230. * @v fmt printf() format string
  231. * @v args printf() argument list
  232. */
  233. static void vmsg ( unsigned int row, const char *fmt, va_list args ) {
  234. char buf[COLS];
  235. size_t len;
  236. len = vsnprintf ( buf, sizeof ( buf ), fmt, args );
  237. mvprintw ( row, ( ( COLS - len ) / 2 ), "%s", buf );
  238. }
  239. /**
  240. * Print message centred on specified row
  241. *
  242. * @v row Row
  243. * @v fmt printf() format string
  244. * @v .. printf() arguments
  245. */
  246. static void msg ( unsigned int row, const char *fmt, ... ) {
  247. va_list args;
  248. va_start ( args, fmt );
  249. vmsg ( row, fmt, args );
  250. va_end ( args );
  251. }
  252. /**
  253. * Clear message on specified row
  254. *
  255. * @v row Row
  256. */
  257. static void clearmsg ( unsigned int row ) {
  258. move ( row, 0 );
  259. clrtoeol();
  260. }
  261. /**
  262. * Print alert message
  263. *
  264. * @v fmt printf() format string
  265. * @v args printf() argument list
  266. */
  267. static void valert ( const char *fmt, va_list args ) {
  268. clearmsg ( ALERT_ROW );
  269. color_set ( CPAIR_ALERT, NULL );
  270. vmsg ( ALERT_ROW, fmt, args );
  271. sleep ( 2 );
  272. color_set ( CPAIR_NORMAL, NULL );
  273. clearmsg ( ALERT_ROW );
  274. }
  275. /**
  276. * Print alert message
  277. *
  278. * @v fmt printf() format string
  279. * @v ... printf() arguments
  280. */
  281. static void alert ( const char *fmt, ... ) {
  282. va_list args;
  283. va_start ( args, fmt );
  284. valert ( fmt, args );
  285. va_end ( args );
  286. }
  287. /**
  288. * Draw title row
  289. *
  290. * @v widget Setting widget
  291. */
  292. static void draw_title_row ( struct setting_widget *widget ) {
  293. const char *name;
  294. clearmsg ( TITLE_ROW );
  295. name = settings_name ( widget->settings );
  296. attron ( A_BOLD );
  297. msg ( TITLE_ROW, "iPXE configuration settings%s%s",
  298. ( name[0] ? " - " : "" ), name );
  299. attroff ( A_BOLD );
  300. }
  301. /**
  302. * Draw information row
  303. *
  304. * @v widget Setting widget
  305. */
  306. static void draw_info_row ( struct setting_widget *widget ) {
  307. char buf[32];
  308. /* Draw nothing unless this row represents a setting */
  309. clearmsg ( INFO_ROW );
  310. clearmsg ( INFO_ROW + 1 );
  311. if ( ! widget->row.setting.name )
  312. return;
  313. /* Determine a suitable setting name */
  314. setting_name ( ( widget->row.origin ?
  315. widget->row.origin : widget->settings ),
  316. &widget->row.setting, buf, sizeof ( buf ) );
  317. /* Draw row */
  318. attron ( A_BOLD );
  319. msg ( INFO_ROW, "%s - %s", buf, widget->row.setting.description );
  320. attroff ( A_BOLD );
  321. color_set ( CPAIR_URL, NULL );
  322. msg ( ( INFO_ROW + 1 ), "http://ipxe.org/cfg/%s",
  323. widget->row.setting.name );
  324. color_set ( CPAIR_NORMAL, NULL );
  325. }
  326. /**
  327. * Draw instruction row
  328. *
  329. * @v widget Setting widget
  330. */
  331. static void draw_instruction_row ( struct setting_widget *widget ) {
  332. clearmsg ( INSTRUCTION_ROW );
  333. if ( widget->row.editing ) {
  334. msg ( INSTRUCTION_ROW,
  335. "Enter - accept changes" INSTRUCTION_PAD
  336. "Ctrl-C - discard changes" );
  337. } else {
  338. msg ( INSTRUCTION_ROW,
  339. "%sCtrl-X - exit configuration utility",
  340. ( ( widget->row.origin == widget->settings ) ?
  341. "Ctrl-D - delete setting" INSTRUCTION_PAD : "" ) );
  342. }
  343. }
  344. /**
  345. * Reveal setting row
  346. *
  347. * @v widget Setting widget
  348. * @v index Index of setting row
  349. */
  350. static void reveal_setting_row ( struct setting_widget *widget,
  351. unsigned int index ) {
  352. unsigned int i;
  353. /* Simply return if setting N is already on-screen. */
  354. if ( index - widget->first_visible < SETTINGS_LIST_ROWS )
  355. return;
  356. /* Jump scroll to make the specified setting row visible. */
  357. while ( widget->first_visible < index )
  358. widget->first_visible += SETTINGS_LIST_ROWS;
  359. while ( widget->first_visible > index )
  360. widget->first_visible -= SETTINGS_LIST_ROWS;
  361. /* Draw ellipses before and/or after the settings list to
  362. * represent any invisible settings.
  363. */
  364. mvaddstr ( SETTINGS_LIST_ROW - 1,
  365. SETTINGS_LIST_COL + 1,
  366. widget->first_visible > 0 ? "..." : " " );
  367. mvaddstr ( SETTINGS_LIST_ROW + SETTINGS_LIST_ROWS,
  368. SETTINGS_LIST_COL + 1,
  369. ( ( widget->first_visible + SETTINGS_LIST_ROWS )
  370. < widget->num_rows ? "..." : " " ) );
  371. /* Draw visible settings. */
  372. for ( i = 0; i < SETTINGS_LIST_ROWS; i++ ) {
  373. if ( ( widget->first_visible + i ) < widget->num_rows ) {
  374. select_setting_row ( widget,
  375. widget->first_visible + i );
  376. draw_setting_row ( widget );
  377. } else {
  378. clearmsg ( SETTINGS_LIST_ROW + i );
  379. }
  380. }
  381. }
  382. /**
  383. * Reveal setting row
  384. *
  385. * @v widget Setting widget
  386. * @v settings Settings block
  387. */
  388. static void init_widget ( struct setting_widget *widget,
  389. struct settings *settings ) {
  390. widget->settings = settings_target ( settings );
  391. widget->num_rows = select_setting_row ( widget, 0 );
  392. widget->first_visible = SETTINGS_LIST_ROWS;
  393. draw_title_row ( widget );
  394. reveal_setting_row ( widget, 0 );
  395. select_setting_row ( widget, 0 );
  396. }
  397. static int main_loop ( struct settings *settings ) {
  398. struct setting_widget widget;
  399. int redraw = 1;
  400. int move;
  401. unsigned int next;
  402. int key;
  403. int rc;
  404. /* Print initial screen content */
  405. color_set ( CPAIR_NORMAL, NULL );
  406. memset ( &widget, 0, sizeof ( widget ) );
  407. init_widget ( &widget, settings );
  408. while ( 1 ) {
  409. /* Redraw rows if necessary */
  410. if ( redraw ) {
  411. draw_info_row ( &widget );
  412. draw_instruction_row ( &widget );
  413. color_set ( ( widget.row.editing ?
  414. CPAIR_EDIT : CPAIR_SELECT ), NULL );
  415. draw_setting_row ( &widget );
  416. color_set ( CPAIR_NORMAL, NULL );
  417. curs_set ( widget.row.editing );
  418. redraw = 0;
  419. }
  420. if ( widget.row.editing ) {
  421. /* Sanity check */
  422. assert ( widget.row.setting.name != NULL );
  423. /* Redraw edit box */
  424. color_set ( CPAIR_EDIT, NULL );
  425. draw_editbox ( &widget.row.editbox );
  426. color_set ( CPAIR_NORMAL, NULL );
  427. /* Process keypress */
  428. key = edit_setting ( &widget, getkey ( 0 ) );
  429. switch ( key ) {
  430. case CR:
  431. case LF:
  432. if ( ( rc = save_setting ( &widget ) ) != 0 )
  433. alert ( " %s ", strerror ( rc ) );
  434. /* Fall through */
  435. case CTRL_C:
  436. select_setting_row ( &widget, widget.current );
  437. redraw = 1;
  438. break;
  439. default:
  440. /* Do nothing */
  441. break;
  442. }
  443. } else {
  444. /* Process keypress */
  445. key = getkey ( 0 );
  446. move = 0;
  447. switch ( key ) {
  448. case KEY_UP:
  449. move = -1;
  450. break;
  451. case KEY_DOWN:
  452. move = +1;
  453. break;
  454. case KEY_PPAGE:
  455. move = ( widget.first_visible -
  456. widget.current - 1 );
  457. break;
  458. case KEY_NPAGE:
  459. move = ( widget.first_visible - widget.current
  460. + SETTINGS_LIST_ROWS );
  461. break;
  462. case KEY_HOME:
  463. move = -widget.num_rows;
  464. break;
  465. case KEY_END:
  466. move = +widget.num_rows;
  467. break;
  468. case CTRL_D:
  469. if ( ! widget.row.setting.name )
  470. break;
  471. if ( ( rc = delete_setting ( widget.settings,
  472. &widget.row.setting ) ) != 0 ) {
  473. alert ( " %s ", strerror ( rc ) );
  474. }
  475. select_setting_row ( &widget, widget.current );
  476. redraw = 1;
  477. break;
  478. case CTRL_X:
  479. return 0;
  480. case CR:
  481. case LF:
  482. if ( widget.row.settings ) {
  483. init_widget ( &widget,
  484. widget.row.settings );
  485. redraw = 1;
  486. }
  487. /* Fall through */
  488. default:
  489. if ( widget.row.setting.name ) {
  490. edit_setting ( &widget, key );
  491. redraw = 1;
  492. }
  493. break;
  494. }
  495. if ( move ) {
  496. next = ( widget.current + move );
  497. if ( ( int ) next < 0 )
  498. next = 0;
  499. if ( next >= widget.num_rows )
  500. next = ( widget.num_rows - 1 );
  501. if ( next != widget.current ) {
  502. draw_setting_row ( &widget );
  503. redraw = 1;
  504. reveal_setting_row ( &widget, next );
  505. select_setting_row ( &widget, next );
  506. }
  507. }
  508. }
  509. }
  510. }
  511. int settings_ui ( struct settings *settings ) {
  512. int rc;
  513. initscr();
  514. start_color();
  515. init_pair ( CPAIR_NORMAL, COLOR_NORMAL_FG, COLOR_NORMAL_BG );
  516. init_pair ( CPAIR_SELECT, COLOR_SELECT_FG, COLOR_SELECT_BG );
  517. init_pair ( CPAIR_EDIT, COLOR_EDIT_FG, COLOR_EDIT_BG );
  518. init_pair ( CPAIR_ALERT, COLOR_ALERT_FG, COLOR_ALERT_BG );
  519. init_pair ( CPAIR_URL, COLOR_URL_FG, COLOR_URL_BG );
  520. color_set ( CPAIR_NORMAL, NULL );
  521. curs_set ( 0 );
  522. erase();
  523. rc = main_loop ( settings );
  524. endwin();
  525. return rc;
  526. }