ui/ncurses: Add widgetset_set_window
[petitboot] / ui / ncurses / nc-widgets.c
1 /*
2  *  Copyright (C) 2013 IBM Corporation
3  *
4  *  This program is free software; you can redistribute it and/or modify
5  *  it under the terms of the GNU General Public License as published by
6  *  the Free Software Foundation; version 2 of the License.
7  *
8  *  This program is distributed in the hope that it will be useful,
9  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
10  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  *  GNU General Public License for more details.
12  *
13  *  You should have received a copy of the GNU General Public License
14  *  along with this program; if not, write to the Free Software
15  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16  */
17
18 #define _GNU_SOURCE
19
20 #include "config.h"
21
22 #include <linux/input.h> /* This must be included before ncurses.h */
23 #if defined HAVE_NCURSESW_CURSES_H
24 #  include <ncursesw/curses.h>
25 #elif defined HAVE_NCURSESW_H
26 #  include <ncursesw.h>
27 #elif defined HAVE_NCURSES_CURSES_H
28 #  include <ncurses/curses.h>
29 #elif defined HAVE_NCURSES_H
30 #  include <ncurses.h>
31 #elif defined HAVE_CURSES_H
32 #  include <curses.h>
33 #else
34 #  error "Curses header file not found."
35 #endif
36
37 #if defined HAVE_NCURSESW_FORM_H
38 #  include <ncursesw/form.h>
39 #elif defined HAVE_NCURSES_FORM_H
40 #  include <ncurses/form.h>
41 #elif defined HAVE_FORM_H
42 #  include <form.h>
43 #else
44 #  error "Curses form.h not found."
45 #endif
46
47 #include <string.h>
48 #include <ctype.h>
49
50 #include <talloc/talloc.h>
51 #include <types/types.h>
52 #include <log/log.h>
53 #include <util/util.h>
54
55 #include "config.h"
56 #include "nc-cui.h"
57 #include "nc-widgets.h"
58
59 #undef move
60
61 #define to_checkbox(w) container_of(w, struct nc_widget_checkbox, widget)
62 #define to_textbox(w) container_of(w, struct nc_widget_textbox, widget)
63 #define to_button(w) container_of(w, struct nc_widget_button, widget)
64 #define to_select(w) container_of(w, struct nc_widget_select, widget)
65
66 static const char *checkbox_checked_str = "[*]";
67 static const char *checkbox_unchecked_str = "[ ]";
68
69 static const char *select_selected_str = "(*)";
70 static const char *select_unselected_str = "( )";
71
72 struct nc_widgetset {
73         WINDOW  *mainwin;
74         WINDOW  *subwin;
75         FORM    *form;
76         FIELD   **fields;
77         int     n_fields, n_alloc_fields;
78         void    (*widget_focus)(struct nc_widget *, void *);
79         void    *widget_focus_arg;
80         FIELD   *cur_field;
81 };
82
83 struct nc_widget {
84         FIELD   *field;
85         bool    (*process_key)(struct nc_widget *, FORM *, int);
86         void    (*set_visible)(struct nc_widget *, bool);
87         void    (*move)(struct nc_widget *, int, int);
88         void    (*field_focus)(struct nc_widget *, FIELD *);
89         int     focussed_attr;
90         int     unfocussed_attr;
91         int     height;
92         int     width;
93         int     focus_y;
94         int     x;
95         int     y;
96 };
97
98 struct nc_widget_label {
99         struct nc_widget        widget;
100         const char              *text;
101 };
102
103 struct nc_widget_checkbox {
104         struct nc_widget        widget;
105         bool                    checked;
106 };
107
108 struct nc_widget_textbox {
109         struct nc_widget        widget;
110 };
111
112 struct nc_widget_select {
113         struct nc_widget        widget;
114         struct select_option {
115                 char            *str;
116                 int             val;
117                 FIELD           *field;
118         } *options;
119         int                     top, left, size;
120         int                     n_options, selected_option;
121         struct nc_widgetset     *set;
122         void                    (*on_change)(void *, int);
123         void                    *on_change_arg;
124 };
125
126 struct nc_widget_button {
127         struct nc_widget        widget;
128         void                    (*click)(void *arg);
129         void                    *arg;
130 };
131
132 static void widgetset_add_field(struct nc_widgetset *set, FIELD *field);
133 static void widgetset_remove_field(struct nc_widgetset *set, FIELD *field);
134
135 static bool process_key_nop(struct nc_widget *widget __attribute__((unused)),
136                 FORM *form __attribute((unused)),
137                 int key __attribute__((unused)))
138 {
139         return false;
140 }
141
142 static void field_set_visible(FIELD *field, bool visible)
143 {
144         int opts = field_opts(field) & ~O_VISIBLE;
145         if (visible)
146                 opts |= O_VISIBLE;
147         set_field_opts(field, opts);
148 }
149
150 static void field_move(FIELD *field, int y, int x)
151 {
152         move_field(field, y, x);
153 }
154
155 static int label_destructor(void *ptr)
156 {
157         struct nc_widget_label *label = ptr;
158         free_field(label->widget.field);
159         return 0;
160 }
161
162
163 struct nc_widget_label *widget_new_label(struct nc_widgetset *set,
164                 int y, int x, char *str)
165 {
166         struct nc_widget_label *label;
167         FIELD *f;
168         int len;
169
170         len = strlen(str);
171
172         label = talloc_zero(set, struct nc_widget_label);
173         label->widget.height = 1;
174         label->widget.width = len;
175         label->widget.x = x;
176         label->widget.y = y;
177         label->widget.process_key = process_key_nop;
178         label->widget.field = f = new_field(1, len, y, x, 0, 0);
179         label->widget.focussed_attr = A_NORMAL;
180         label->widget.unfocussed_attr = A_NORMAL;
181
182         field_opts_off(f, O_ACTIVE);
183         set_field_buffer(f, 0, str);
184         set_field_userptr(f, &label->widget);
185
186         widgetset_add_field(set, label->widget.field);
187         talloc_set_destructor(label, label_destructor);
188
189         return label;
190 }
191
192 bool widget_checkbox_get_value(struct nc_widget_checkbox *checkbox)
193 {
194         return checkbox->checked;
195 }
196
197 static void checkbox_set_buffer(struct nc_widget_checkbox *checkbox)
198 {
199         const char *str;
200         str = checkbox->checked ? checkbox_checked_str : checkbox_unchecked_str;
201         set_field_buffer(checkbox->widget.field, 0, str);
202 }
203
204 static bool checkbox_process_key(struct nc_widget *widget,
205                 FORM *form __attribute__((unused)), int key)
206 {
207         struct nc_widget_checkbox *checkbox = to_checkbox(widget);
208
209         if (key != ' ')
210                 return false;
211
212         checkbox->checked = !checkbox->checked;
213         checkbox_set_buffer(checkbox);
214
215         return true;
216 }
217
218 static int checkbox_destructor(void *ptr)
219 {
220         struct nc_widget_checkbox *checkbox = ptr;
221         free_field(checkbox->widget.field);
222         return 0;
223 }
224
225 struct nc_widget_checkbox *widget_new_checkbox(struct nc_widgetset *set,
226                 int y, int x, bool checked)
227 {
228         struct nc_widget_checkbox *checkbox;
229         FIELD *f;
230
231         checkbox = talloc_zero(set, struct nc_widget_checkbox);
232         checkbox->checked = checked;
233         checkbox->widget.height = 1;
234         checkbox->widget.width = strlen(checkbox_checked_str);
235         checkbox->widget.x = x;
236         checkbox->widget.y = y;
237         checkbox->widget.process_key = checkbox_process_key;
238         checkbox->widget.focussed_attr = A_REVERSE;
239         checkbox->widget.unfocussed_attr = A_NORMAL;
240         checkbox->widget.field = f = new_field(1, strlen(checkbox_checked_str),
241                         y, x, 0, 0);
242
243         field_opts_off(f, O_EDIT);
244         set_field_userptr(f, &checkbox->widget);
245         checkbox_set_buffer(checkbox);
246
247         widgetset_add_field(set, checkbox->widget.field);
248         talloc_set_destructor(checkbox, checkbox_destructor);
249
250         return checkbox;
251 }
252
253 static char *strip_string(char *str)
254 {
255         int len, i;
256
257         len = strlen(str);
258
259         /* clear trailing space */
260         for (i = len - 1; i >= 0; i--) {
261                 if (!isspace(str[i]))
262                         break;
263                 str[i] = '\0';
264         }
265
266         /* increment str past leading space */
267         for (i = 0; i < len; i++) {
268                 if (str[i] == '\0' || !isspace(str[i]))
269                         break;
270         }
271
272         return str + i;
273 }
274
275 char *widget_textbox_get_value(struct nc_widget_textbox *textbox)
276 {
277         char *str = field_buffer(textbox->widget.field, 0);
278         return str ? strip_string(str) : NULL;
279 }
280
281 static bool textbox_process_key(
282                 struct nc_widget *widget __attribute__((unused)),
283                 FORM *form, int key)
284 {
285         switch (key) {
286         case KEY_HOME:
287                 form_driver(form, REQ_BEG_FIELD);
288                 break;
289         case KEY_END:
290                 form_driver(form, REQ_END_FIELD);
291                 break;
292         case KEY_LEFT:
293                 form_driver(form, REQ_LEFT_CHAR);
294                 break;
295         case KEY_RIGHT:
296                 form_driver(form, REQ_RIGHT_CHAR);
297                 break;
298         case KEY_BACKSPACE:
299                 if (form_driver(form, REQ_LEFT_CHAR) != E_OK)
300                         break;
301                 /* fall through */
302         case KEY_DC:
303                 form_driver(form, REQ_DEL_CHAR);
304                 break;
305         default:
306                 form_driver(form, key);
307                 break;
308         }
309
310         return true;
311 }
312
313 static int textbox_destructor(void *ptr)
314 {
315         struct nc_widget_textbox *textbox = ptr;
316         free_field(textbox->widget.field);
317         return 0;
318 }
319
320 struct nc_widget_textbox *widget_new_textbox(struct nc_widgetset *set,
321                 int y, int x, int len, char *str)
322 {
323         struct nc_widget_textbox *textbox;
324         FIELD *f;
325
326         textbox = talloc_zero(set, struct nc_widget_textbox);
327         textbox->widget.height = 1;
328         textbox->widget.width = len;
329         textbox->widget.x = x;
330         textbox->widget.y = y;
331         textbox->widget.process_key = textbox_process_key;
332         textbox->widget.field = f = new_field(1, len, y, x, 0, 0);
333         textbox->widget.focussed_attr = A_REVERSE;
334         textbox->widget.unfocussed_attr = A_UNDERLINE;
335
336         field_opts_off(f, O_STATIC | O_WRAP | O_BLANK);
337         set_field_buffer(f, 0, str);
338         set_field_back(f, textbox->widget.unfocussed_attr);
339         set_field_userptr(f, &textbox->widget);
340
341         widgetset_add_field(set, textbox->widget.field);
342         talloc_set_destructor(textbox, textbox_destructor);
343
344         return textbox;
345 }
346
347 static void select_option_change(struct select_option *opt, bool selected)
348 {
349         const char *str;
350
351         str = selected ? select_selected_str : select_unselected_str;
352
353         memcpy(opt->str, str, strlen(str));
354         set_field_buffer(opt->field, 0, opt->str);
355 }
356
357 static bool select_process_key(struct nc_widget *w, FORM *form, int key)
358 {
359         struct nc_widget_select *select = to_select(w);
360         struct select_option *new_opt, *old_opt;
361         int i, new_idx;
362         FIELD *field;
363
364         switch (key) {
365         case ' ':
366         case KEY_ENTER:
367                 break;
368         default:
369                 return false;
370         }
371
372         field = current_field(form);
373         new_opt = NULL;
374
375         for (i = 0; i < select->n_options; i++) {
376                 if (select->options[i].field == field) {
377                         new_opt = &select->options[i];
378                         new_idx = i;
379                         break;
380                 }
381         }
382
383         if (!new_opt)
384                 return true;
385
386         if (new_idx == select->selected_option)
387                 return true;
388
389         old_opt = &select->options[select->selected_option];
390
391         select_option_change(old_opt, false);
392         select_option_change(new_opt, true);
393
394         select->selected_option = new_idx;
395
396         if (select->on_change)
397                 select->on_change(select->on_change_arg, new_opt->val);
398
399         return true;
400 }
401
402 static void select_set_visible(struct nc_widget *widget, bool visible)
403 {
404         struct nc_widget_select *select = to_select(widget);
405         int i;
406
407         for (i = 0; i < select->n_options; i++)
408                 field_set_visible(select->options[i].field, visible);
409 }
410
411 static void select_move(struct nc_widget *widget, int y, int x)
412 {
413         struct nc_widget_select *select = to_select(widget);
414         int i;
415
416         for (i = 0; i < select->n_options; i++)
417                 field_move(select->options[i].field, y + i, x);
418 }
419
420 static void select_field_focus(struct nc_widget *widget, FIELD *field)
421 {
422         struct nc_widget_select *select = to_select(widget);
423         int i;
424
425         for (i = 0; i < select->n_options; i++) {
426                 if (field != select->options[i].field)
427                         continue;
428                 widget->focus_y = i;
429                 return;
430         }
431 }
432
433 static int select_destructor(void *ptr)
434 {
435         struct nc_widget_select *select = ptr;
436         int i;
437
438         for (i = 0; i < select->n_options; i++)
439                 free_field(select->options[i].field);
440
441         return 0;
442 }
443
444 struct nc_widget_select *widget_new_select(struct nc_widgetset *set,
445                 int y, int x, int len)
446 {
447         struct nc_widget_select *select;
448
449         select = talloc_zero(set, struct nc_widget_select);
450         select->widget.width = len;
451         select->widget.height = 0;
452         select->widget.x = x;
453         select->widget.y = y;
454         select->widget.process_key = select_process_key;
455         select->widget.set_visible = select_set_visible;
456         select->widget.move = select_move;
457         select->widget.field_focus = select_field_focus;
458         select->widget.focussed_attr = A_REVERSE;
459         select->widget.unfocussed_attr = A_NORMAL;
460         select->top = y;
461         select->left = x;
462         select->size = len;
463         select->set = set;
464
465         talloc_set_destructor(select, select_destructor);
466
467         return select;
468 }
469
470 void widget_select_add_option(struct nc_widget_select *select, int value,
471                 const char *text, bool selected)
472 {
473         const char *str;
474         FIELD *f;
475         int i;
476
477         /* if we never see an option with selected set, we want the first
478          * one to be selected */
479         if (select->n_options == 0)
480                 selected = true;
481         else if (selected)
482                 select_option_change(&select->options[select->selected_option],
483                                         false);
484
485         if (selected) {
486                 select->selected_option = select->n_options;
487                 str = select_selected_str;
488         } else
489                 str = select_unselected_str;
490
491         i = select->n_options++;
492         select->widget.height = select->n_options;
493
494         select->options = talloc_realloc(select, select->options,
495                                 struct select_option, i + 2);
496         select->options[i].val = value;
497         select->options[i].str = talloc_asprintf(select->options,
498                                         "%s %s", str, text);
499
500         select->options[i].field = f = new_field(1, select->size,
501                                                 select->top + i,
502                                                 select->left, 0, 0);
503
504         field_opts_off(f, O_WRAP | O_EDIT);
505         set_field_userptr(f, &select->widget);
506         set_field_buffer(f, 0, select->options[i].str);
507
508         widgetset_add_field(select->set, f);
509 }
510
511 int widget_select_get_value(struct nc_widget_select *select)
512 {
513         if (!select->n_options)
514                 return -1;
515         return select->options[select->selected_option].val;
516 }
517
518 int widget_select_height(struct nc_widget_select *select)
519 {
520         return select->n_options;
521 }
522
523 void widget_select_on_change(struct nc_widget_select *select,
524                 void (*on_change)(void *, int), void *arg)
525 {
526         select->on_change = on_change;
527         select->on_change_arg = arg;
528 }
529
530 void widget_select_drop_options(struct nc_widget_select *select)
531 {
532         struct nc_widgetset *set = select->set;
533         int i;
534
535         for (i = 0; i < select->n_options; i++) {
536                 FIELD *field = select->options[i].field;
537                 widgetset_remove_field(set, field);
538                 if (field == set->cur_field)
539                         set->cur_field = NULL;
540                 free_field(select->options[i].field);
541         }
542
543         talloc_free(select->options);
544         select->options = NULL;
545         select->n_options = 0;
546         select->widget.height = 0;
547         select->widget.focus_y = 0;
548
549 }
550
551 static bool button_process_key(struct nc_widget *widget,
552                 FORM *form __attribute__((unused)), int key)
553 {
554         struct nc_widget_button *button = to_button(widget);
555
556         if (!button->click)
557                 return false;
558
559         switch (key) {
560         case ' ':
561         case '\r':
562         case '\n':
563                 button->click(button->arg);
564                 return true;
565         }
566
567         return false;
568 }
569
570 static int button_destructor(void *ptr)
571 {
572         struct nc_widget_button *button = ptr;
573         free_field(button->widget.field);
574         return 0;
575 }
576
577 struct nc_widget_button *widget_new_button(struct nc_widgetset *set,
578                 int y, int x, int size, const char *str,
579                 void (*click)(void *), void *arg)
580 {
581         struct nc_widget_button *button;
582         char *text;
583         FIELD *f;
584         int idx, len;
585
586         button = talloc_zero(set, struct nc_widget_button);
587         button->widget.height = 1;
588         button->widget.width = size;
589         button->widget.x = x;
590         button->widget.y = y;
591         button->widget.field = f = new_field(1, size + 2, y, x, 0, 0);
592         button->widget.process_key = button_process_key;
593         button->widget.focussed_attr = A_REVERSE;
594         button->widget.unfocussed_attr = A_NORMAL;
595         button->click = click;
596         button->arg = arg;
597
598         field_opts_off(f, O_EDIT);
599         set_field_userptr(f, &button->widget);
600
601         /* center str in a size-char buffer, but don't overrun */
602         len = strlen(str);
603         len = min(len, size);
604         idx = (size - len) / 2;
605
606         text = talloc_array(button, char, size + 3);
607         memset(text, ' ', size + 2);
608         memcpy(text + idx + 1, str, len);
609         text[0] = '[';
610         text[size + 1] = ']';
611         text[size + 2] = '\0';
612
613         set_field_buffer(f, 0, text);
614
615         widgetset_add_field(set, button->widget.field);
616         talloc_set_destructor(button, button_destructor);
617
618         return button;
619 }
620
621 static void widget_focus_change(struct nc_widget *widget, FIELD *field,
622                 bool focussed)
623 {
624         int attr = focussed ? widget->focussed_attr : widget->unfocussed_attr;
625         set_field_back(field, attr);
626 }
627
628 bool widgetset_process_key(struct nc_widgetset *set, int key)
629 {
630         struct nc_widget *widget;
631         FIELD *field;
632         int req = 0;
633
634         field = current_field(set->form);
635         assert(field);
636
637         /* handle field change events */
638         switch (key) {
639         case KEY_BTAB:
640         case KEY_UP:
641                 req = REQ_PREV_FIELD;
642                 break;
643         case '\t':
644         case KEY_DOWN:
645                 req = REQ_NEXT_FIELD;
646                 break;
647         case KEY_PPAGE:
648                 req = REQ_FIRST_FIELD;
649                 break;
650         case KEY_NPAGE:
651                 req = REQ_LAST_FIELD;
652                 break;
653         }
654
655         widget = field_userptr(field);
656         if (req) {
657                 widget_focus_change(widget, field, false);
658                 form_driver(set->form, req);
659                 form_driver(set->form, REQ_END_FIELD);
660                 field = current_field(set->form);
661                 widget = field_userptr(field);
662                 widget_focus_change(widget, field, true);
663                 if (widget->field_focus)
664                         widget->field_focus(widget, field);
665                 if (set->widget_focus)
666                         set->widget_focus(widget, set->widget_focus_arg);
667                 return true;
668         }
669
670         if (!widget->process_key)
671                 return false;
672
673         return widget->process_key(widget, set->form, key);
674 }
675
676 static int widgetset_destructor(void *ptr)
677 {
678         struct nc_widgetset *set = ptr;
679         free_form(set->form);
680         return 0;
681 }
682
683 struct nc_widgetset *widgetset_create(void *ctx, WINDOW *main, WINDOW *sub)
684 {
685         struct nc_widgetset *set;
686
687         set = talloc_zero(ctx, struct nc_widgetset);
688         set->n_alloc_fields = 8;
689         set->mainwin = main;
690         set->subwin = sub;
691         set->fields = talloc_array(set, FIELD *, set->n_alloc_fields);
692         talloc_set_destructor(set, widgetset_destructor);
693
694         return set;
695 }
696
697 void widgetset_set_windows(struct nc_widgetset *set,
698                 WINDOW *main, WINDOW *sub)
699 {
700         set->mainwin = main;
701         set->subwin = sub;
702 }
703
704 void widgetset_set_widget_focus(struct nc_widgetset *set,
705                 widget_focus_cb cb, void *arg)
706 {
707         set->widget_focus = cb;
708         set->widget_focus_arg = arg;
709 }
710
711 void widgetset_post(struct nc_widgetset *set)
712 {
713         struct nc_widget *widget;
714         FIELD *field;
715
716         set->form = new_form(set->fields);
717         set_form_win(set->form, set->mainwin);
718         set_form_sub(set->form, set->subwin);
719         post_form(set->form);
720         form_driver(set->form, REQ_END_FIELD);
721
722         if (set->cur_field) {
723                 set_current_field(set->form, set->cur_field);
724                 field = set->cur_field;
725         }
726
727         field = current_field(set->form);
728         widget = field_userptr(field);
729         widget_focus_change(widget, field, true);
730         if (set->widget_focus)
731                 set->widget_focus(widget, set->widget_focus_arg);
732 }
733
734 void widgetset_unpost(struct nc_widgetset *set)
735 {
736         set->cur_field = current_field(set->form);
737         unpost_form(set->form);
738         free_form(set->form);
739         set->form = NULL;
740 }
741
742 static void widgetset_add_field(struct nc_widgetset *set, FIELD *field)
743 {
744         if (set->n_fields == set->n_alloc_fields - 1) {
745                 set->n_alloc_fields *= 2;
746                 set->fields = talloc_realloc(set, set->fields,
747                                 FIELD *, set->n_alloc_fields);
748         }
749
750         set->n_fields++;
751         set->fields[set->n_fields - 1] = field;
752         set->fields[set->n_fields] = NULL;
753 }
754
755 static void widgetset_remove_field(struct nc_widgetset *set, FIELD *field)
756 {
757         int i;
758
759         for (i = 0; i < set->n_fields; i++) {
760                 if (set->fields[i] == field)
761                         break;
762         }
763
764         if (i == set->n_fields)
765                 return;
766
767         memmove(&set->fields[i], &set->fields[i+i],
768                         (set->n_fields - i) * sizeof(set->fields[i]));
769         set->n_fields--;
770 }
771
772 #define DECLARE_BASEFN(type) \
773         struct nc_widget *widget_ ## type ## _base              \
774                 (struct nc_widget_ ## type *w)                  \
775         { return &w->widget; }
776
777 DECLARE_BASEFN(textbox);
778 DECLARE_BASEFN(checkbox);
779 DECLARE_BASEFN(select);
780 DECLARE_BASEFN(label);
781 DECLARE_BASEFN(button);
782
783 void widget_set_visible(struct nc_widget *widget, bool visible)
784 {
785         if (widget->set_visible)
786                 widget->set_visible(widget, visible);
787         else
788                 field_set_visible(widget->field, visible);
789 }
790
791 void widget_move(struct nc_widget *widget, int y, int x)
792 {
793         if (widget->move)
794                 widget->move(widget, y, x);
795         else
796                 field_move(widget->field, y, x);
797
798         widget->x = x;
799         widget->y = y;
800 }
801
802 int widget_height(struct nc_widget *widget)
803 {
804         return widget->height;
805 }
806
807 int widget_width(struct nc_widget *widget)
808 {
809         return widget->width;
810 }
811
812 int widget_x(struct nc_widget *widget)
813 {
814         return widget->x;
815 }
816
817 int widget_y(struct nc_widget *widget)
818 {
819         return widget->y;
820 }
821
822 int widget_focus_y(struct nc_widget *widget)
823 {
824         return widget->focus_y;
825 }
826