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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600
  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. struct setting *previous = NULL;
  114. unsigned int count = 0;
  115. /* Initialise structure */
  116. memset ( &widget->row, 0, sizeof ( widget->row ) );
  117. widget->current = index;
  118. widget->row.row = ( SETTINGS_LIST_ROW + index - widget->first_visible );
  119. widget->row.col = SETTINGS_LIST_COL;
  120. /* Include parent settings block, if applicable */
  121. if ( widget->settings->parent && ( count++ == index ) ) {
  122. widget->row.settings = widget->settings->parent;
  123. snprintf ( widget->row.value, sizeof ( widget->row.value ),
  124. "../" );
  125. }
  126. /* Include any child settings blocks, if applicable */
  127. list_for_each_entry ( settings, &widget->settings->children, siblings ){
  128. if ( count++ == index ) {
  129. widget->row.settings = settings;
  130. snprintf ( widget->row.value,
  131. sizeof ( widget->row.value ), "%s/",
  132. settings->name );
  133. }
  134. }
  135. /* Include any applicable settings */
  136. for_each_table_entry ( setting, SETTINGS ) {
  137. /* Skip inapplicable settings */
  138. if ( ! setting_applies ( widget->settings, setting ) )
  139. continue;
  140. /* Skip duplicate settings */
  141. if ( previous && ( setting_cmp ( setting, previous ) == 0 ) )
  142. continue;
  143. previous = setting;
  144. /* Read current setting value and origin */
  145. if ( count++ == index ) {
  146. fetchf_setting ( widget->settings, setting,
  147. &widget->row.origin,
  148. &widget->row.setting,
  149. widget->row.value,
  150. sizeof ( widget->row.value ) );
  151. }
  152. }
  153. /* Initialise edit box */
  154. init_editbox ( &widget->row.editbox, widget->row.value,
  155. sizeof ( widget->row.value ), NULL, widget->row.row,
  156. ( widget->row.col +
  157. offsetof ( struct setting_row_text, value ) ),
  158. sizeof ( ( ( struct setting_row_text * ) NULL )->value ),
  159. 0 );
  160. return count;
  161. }
  162. static size_t string_copy ( char *dest, const char *src, size_t len ) {
  163. size_t src_len;
  164. src_len = strlen ( src );
  165. if ( len > src_len )
  166. len = src_len;
  167. memcpy ( dest, src, len );
  168. return len;
  169. }
  170. /**
  171. * Draw setting row
  172. *
  173. * @v widget Setting widget
  174. */
  175. static void draw_setting_row ( struct setting_widget *widget ) {
  176. struct setting_row_text text;
  177. unsigned int curs_offset;
  178. char *value;
  179. /* Fill row with spaces */
  180. memset ( &text, ' ', sizeof ( text ) );
  181. text.nul = '\0';
  182. /* Construct row content */
  183. if ( widget->row.settings ) {
  184. /* Construct space-padded name */
  185. curs_offset = ( offsetof ( typeof ( text ), name ) +
  186. string_copy ( text.name, widget->row.value,
  187. sizeof ( text.name ) ) );
  188. } else {
  189. /* Construct dot-padded name */
  190. memset ( text.name, '.', sizeof ( text.name ) );
  191. string_copy ( text.name, widget->row.setting.name,
  192. sizeof ( text.name ) );
  193. /* Construct space-padded value */
  194. value = widget->row.value;
  195. if ( ! *value )
  196. value = "<not specified>";
  197. curs_offset = ( offsetof ( typeof ( text ), value ) +
  198. string_copy ( text.value, value,
  199. sizeof ( text.value ) ) );
  200. }
  201. /* Print row */
  202. if ( ( widget->row.origin == widget->settings ) ||
  203. ( widget->row.settings != NULL ) ) {
  204. attron ( A_BOLD );
  205. }
  206. mvprintw ( widget->row.row, widget->row.col, "%s", text.start );
  207. attroff ( A_BOLD );
  208. move ( widget->row.row, widget->row.col + curs_offset );
  209. }
  210. /**
  211. * Edit setting widget
  212. *
  213. * @v widget Setting widget
  214. * @v key Key pressed by user
  215. * @ret key Key returned to application, or zero
  216. */
  217. static int edit_setting ( struct setting_widget *widget, int key ) {
  218. assert ( widget->row.setting.name != NULL );
  219. widget->row.editing = 1;
  220. return edit_editbox ( &widget->row.editbox, key );
  221. }
  222. /**
  223. * Save setting widget value back to configuration settings
  224. *
  225. * @v widget Setting widget
  226. */
  227. static int save_setting ( struct setting_widget *widget ) {
  228. assert ( widget->row.setting.name != NULL );
  229. return storef_setting ( widget->settings, &widget->row.setting,
  230. widget->row.value );
  231. }
  232. /**
  233. * Print message centred on specified row
  234. *
  235. * @v row Row
  236. * @v fmt printf() format string
  237. * @v args printf() argument list
  238. */
  239. static void vmsg ( unsigned int row, const char *fmt, va_list args ) {
  240. char buf[COLS];
  241. size_t len;
  242. len = vsnprintf ( buf, sizeof ( buf ), fmt, args );
  243. mvprintw ( row, ( ( COLS - len ) / 2 ), "%s", buf );
  244. }
  245. /**
  246. * Print message centred on specified row
  247. *
  248. * @v row Row
  249. * @v fmt printf() format string
  250. * @v .. printf() arguments
  251. */
  252. static void msg ( unsigned int row, const char *fmt, ... ) {
  253. va_list args;
  254. va_start ( args, fmt );
  255. vmsg ( row, fmt, args );
  256. va_end ( args );
  257. }
  258. /**
  259. * Clear message on specified row
  260. *
  261. * @v row Row
  262. */
  263. static void clearmsg ( unsigned int row ) {
  264. move ( row, 0 );
  265. clrtoeol();
  266. }
  267. /**
  268. * Print alert message
  269. *
  270. * @v fmt printf() format string
  271. * @v args printf() argument list
  272. */
  273. static void valert ( const char *fmt, va_list args ) {
  274. clearmsg ( ALERT_ROW );
  275. color_set ( CPAIR_ALERT, NULL );
  276. vmsg ( ALERT_ROW, fmt, args );
  277. sleep ( 2 );
  278. color_set ( CPAIR_NORMAL, NULL );
  279. clearmsg ( ALERT_ROW );
  280. }
  281. /**
  282. * Print alert message
  283. *
  284. * @v fmt printf() format string
  285. * @v ... printf() arguments
  286. */
  287. static void alert ( const char *fmt, ... ) {
  288. va_list args;
  289. va_start ( args, fmt );
  290. valert ( fmt, args );
  291. va_end ( args );
  292. }
  293. /**
  294. * Draw title row
  295. *
  296. * @v widget Setting widget
  297. */
  298. static void draw_title_row ( struct setting_widget *widget ) {
  299. const char *name;
  300. clearmsg ( TITLE_ROW );
  301. name = settings_name ( widget->settings );
  302. attron ( A_BOLD );
  303. msg ( TITLE_ROW, "iPXE configuration settings%s%s",
  304. ( name[0] ? " - " : "" ), name );
  305. attroff ( A_BOLD );
  306. }
  307. /**
  308. * Draw information row
  309. *
  310. * @v widget Setting widget
  311. */
  312. static void draw_info_row ( struct setting_widget *widget ) {
  313. char buf[32];
  314. /* Draw nothing unless this row represents a setting */
  315. clearmsg ( INFO_ROW );
  316. clearmsg ( INFO_ROW + 1 );
  317. if ( ! widget->row.setting.name )
  318. return;
  319. /* Determine a suitable setting name */
  320. setting_name ( ( widget->row.origin ?
  321. widget->row.origin : widget->settings ),
  322. &widget->row.setting, buf, sizeof ( buf ) );
  323. /* Draw row */
  324. attron ( A_BOLD );
  325. msg ( INFO_ROW, "%s - %s", buf, widget->row.setting.description );
  326. attroff ( A_BOLD );
  327. color_set ( CPAIR_URL, NULL );
  328. msg ( ( INFO_ROW + 1 ), "http://ipxe.org/cfg/%s",
  329. widget->row.setting.name );
  330. color_set ( CPAIR_NORMAL, NULL );
  331. }
  332. /**
  333. * Draw instruction row
  334. *
  335. * @v widget Setting widget
  336. */
  337. static void draw_instruction_row ( struct setting_widget *widget ) {
  338. clearmsg ( INSTRUCTION_ROW );
  339. if ( widget->row.editing ) {
  340. msg ( INSTRUCTION_ROW,
  341. "Enter - accept changes" INSTRUCTION_PAD
  342. "Ctrl-C - discard changes" );
  343. } else {
  344. msg ( INSTRUCTION_ROW,
  345. "%sCtrl-X - exit configuration utility",
  346. ( ( widget->row.origin == widget->settings ) ?
  347. "Ctrl-D - delete setting" INSTRUCTION_PAD : "" ) );
  348. }
  349. }
  350. /**
  351. * Reveal setting row
  352. *
  353. * @v widget Setting widget
  354. * @v index Index of setting row
  355. */
  356. static void reveal_setting_row ( struct setting_widget *widget,
  357. unsigned int index ) {
  358. unsigned int i;
  359. /* Simply return if setting N is already on-screen. */
  360. if ( index - widget->first_visible < SETTINGS_LIST_ROWS )
  361. return;
  362. /* Jump scroll to make the specified setting row visible. */
  363. while ( widget->first_visible < index )
  364. widget->first_visible += SETTINGS_LIST_ROWS;
  365. while ( widget->first_visible > index )
  366. widget->first_visible -= SETTINGS_LIST_ROWS;
  367. /* Draw ellipses before and/or after the settings list to
  368. * represent any invisible settings.
  369. */
  370. mvaddstr ( SETTINGS_LIST_ROW - 1,
  371. SETTINGS_LIST_COL + 1,
  372. widget->first_visible > 0 ? "..." : " " );
  373. mvaddstr ( SETTINGS_LIST_ROW + SETTINGS_LIST_ROWS,
  374. SETTINGS_LIST_COL + 1,
  375. ( ( widget->first_visible + SETTINGS_LIST_ROWS )
  376. < widget->num_rows ? "..." : " " ) );
  377. /* Draw visible settings. */
  378. for ( i = 0; i < SETTINGS_LIST_ROWS; i++ ) {
  379. if ( ( widget->first_visible + i ) < widget->num_rows ) {
  380. select_setting_row ( widget,
  381. widget->first_visible + i );
  382. draw_setting_row ( widget );
  383. } else {
  384. clearmsg ( SETTINGS_LIST_ROW + i );
  385. }
  386. }
  387. }
  388. /**
  389. * Reveal setting row
  390. *
  391. * @v widget Setting widget
  392. * @v settings Settings block
  393. */
  394. static void init_widget ( struct setting_widget *widget,
  395. struct settings *settings ) {
  396. widget->settings = settings_target ( settings );
  397. widget->num_rows = select_setting_row ( widget, 0 );
  398. widget->first_visible = SETTINGS_LIST_ROWS;
  399. draw_title_row ( widget );
  400. reveal_setting_row ( widget, 0 );
  401. select_setting_row ( widget, 0 );
  402. }
  403. static int main_loop ( struct settings *settings ) {
  404. struct setting_widget widget;
  405. int redraw = 1;
  406. int move;
  407. unsigned int next;
  408. int key;
  409. int rc;
  410. /* Print initial screen content */
  411. color_set ( CPAIR_NORMAL, NULL );
  412. memset ( &widget, 0, sizeof ( widget ) );
  413. init_widget ( &widget, settings );
  414. while ( 1 ) {
  415. /* Redraw rows if necessary */
  416. if ( redraw ) {
  417. draw_info_row ( &widget );
  418. draw_instruction_row ( &widget );
  419. color_set ( ( widget.row.editing ?
  420. CPAIR_EDIT : CPAIR_SELECT ), NULL );
  421. draw_setting_row ( &widget );
  422. color_set ( CPAIR_NORMAL, NULL );
  423. curs_set ( widget.row.editing );
  424. redraw = 0;
  425. }
  426. if ( widget.row.editing ) {
  427. /* Sanity check */
  428. assert ( widget.row.setting.name != NULL );
  429. /* Redraw edit box */
  430. color_set ( CPAIR_EDIT, NULL );
  431. draw_editbox ( &widget.row.editbox );
  432. color_set ( CPAIR_NORMAL, NULL );
  433. /* Process keypress */
  434. key = edit_setting ( &widget, getkey ( 0 ) );
  435. switch ( key ) {
  436. case CR:
  437. case LF:
  438. if ( ( rc = save_setting ( &widget ) ) != 0 )
  439. alert ( " %s ", strerror ( rc ) );
  440. /* Fall through */
  441. case CTRL_C:
  442. select_setting_row ( &widget, widget.current );
  443. redraw = 1;
  444. break;
  445. default:
  446. /* Do nothing */
  447. break;
  448. }
  449. } else {
  450. /* Process keypress */
  451. key = getkey ( 0 );
  452. move = 0;
  453. switch ( key ) {
  454. case KEY_UP:
  455. move = -1;
  456. break;
  457. case KEY_DOWN:
  458. move = +1;
  459. break;
  460. case KEY_PPAGE:
  461. move = ( widget.first_visible -
  462. widget.current - 1 );
  463. break;
  464. case KEY_NPAGE:
  465. move = ( widget.first_visible - widget.current
  466. + SETTINGS_LIST_ROWS );
  467. break;
  468. case KEY_HOME:
  469. move = -widget.num_rows;
  470. break;
  471. case KEY_END:
  472. move = +widget.num_rows;
  473. break;
  474. case CTRL_D:
  475. if ( ! widget.row.setting.name )
  476. break;
  477. if ( ( rc = delete_setting ( widget.settings,
  478. &widget.row.setting ) ) != 0 ) {
  479. alert ( " %s ", strerror ( rc ) );
  480. }
  481. select_setting_row ( &widget, widget.current );
  482. redraw = 1;
  483. break;
  484. case CTRL_X:
  485. return 0;
  486. case CR:
  487. case LF:
  488. if ( widget.row.settings ) {
  489. init_widget ( &widget,
  490. widget.row.settings );
  491. redraw = 1;
  492. }
  493. /* Fall through */
  494. default:
  495. if ( widget.row.setting.name ) {
  496. edit_setting ( &widget, key );
  497. redraw = 1;
  498. }
  499. break;
  500. }
  501. if ( move ) {
  502. next = ( widget.current + move );
  503. if ( ( int ) next < 0 )
  504. next = 0;
  505. if ( next >= widget.num_rows )
  506. next = ( widget.num_rows - 1 );
  507. if ( next != widget.current ) {
  508. draw_setting_row ( &widget );
  509. redraw = 1;
  510. reveal_setting_row ( &widget, next );
  511. select_setting_row ( &widget, next );
  512. }
  513. }
  514. }
  515. }
  516. }
  517. int settings_ui ( struct settings *settings ) {
  518. int rc;
  519. initscr();
  520. start_color();
  521. init_pair ( CPAIR_NORMAL, COLOR_NORMAL_FG, COLOR_NORMAL_BG );
  522. init_pair ( CPAIR_SELECT, COLOR_SELECT_FG, COLOR_SELECT_BG );
  523. init_pair ( CPAIR_EDIT, COLOR_EDIT_FG, COLOR_EDIT_BG );
  524. init_pair ( CPAIR_ALERT, COLOR_ALERT_FG, COLOR_ALERT_BG );
  525. init_pair ( CPAIR_URL, COLOR_URL_FG, COLOR_URL_BG );
  526. color_set ( CPAIR_NORMAL, NULL );
  527. curs_set ( 0 );
  528. erase();
  529. rc = main_loop ( settings );
  530. endwin();
  531. return rc;
  532. }