202b0d2fddbfa61c5f9a8e71c412b763ff30914c
[fio.git] / goptions.c
1 #include <locale.h>
2 #include <malloc.h>
3 #include <string.h>
4
5 #include <glib.h>
6 #include <cairo.h>
7 #include <gtk/gtk.h>
8
9 #include "fio.h"
10 #include "gfio.h"
11 #include "ghelpers.h"
12 #include "parse.h"
13
14 struct gopt {
15         GtkWidget *box;
16         unsigned int opt_index;
17         unsigned int opt_type;
18         gulong sig_handler;
19 };
20
21 struct gopt_combo {
22         struct gopt gopt;
23         GtkWidget *combo;
24 };
25
26 struct gopt_int {
27         struct gopt gopt;
28         unsigned int lastval;
29         GtkWidget *spin;
30 };
31
32 struct gopt_bool {
33         struct gopt gopt;
34         GtkWidget *check;
35 };
36
37 struct gopt_str {
38         struct gopt gopt;
39         GtkWidget *entry;
40 };
41
42 struct gopt_str_val {
43         struct gopt gopt;
44         GtkWidget *spin;
45         GtkWidget *combo;
46         unsigned int maxindex;
47 };
48
49 #define GOPT_RANGE_SPIN 4
50
51 struct gopt_range {
52         struct gopt gopt;
53         GtkWidget *spins[GOPT_RANGE_SPIN];
54 };
55
56 struct gopt_str_multi {
57         struct gopt gopt;
58         GtkWidget *checks[PARSE_MAX_VP];
59 };
60
61 static GtkWidget *gopt_widgets[FIO_MAX_OPTS];
62
63 struct gopt_frame_widget {
64         GtkWidget *vbox[2];
65         unsigned int nr;
66 };
67 static struct gopt_frame_widget gopt_g_widgets[__FIO_OPT_G_NR];
68
69 static GNode *gopt_dep_tree;
70
71 static GtkWidget *gopt_get_group_frame(GtkWidget *box, unsigned int groupmask)
72 {
73         unsigned int mask, group;
74         struct opt_group *og;
75         GtkWidget *frame, *hbox;
76         struct gopt_frame_widget *gfw;
77
78         if (!groupmask)
79                 return 0;
80
81         mask = groupmask;
82         og = opt_group_cat_from_mask(&mask);
83         if (!og)
84                 return NULL;
85
86         group = ffz(~groupmask);
87         gfw = &gopt_g_widgets[group];
88         if (!gfw->vbox[0]) {
89                 frame = gtk_frame_new(og->name);
90                 gtk_box_pack_start(GTK_BOX(box), frame, FALSE, FALSE, 3);
91                 hbox = gtk_hbox_new(FALSE, 0);
92                 gtk_container_add(GTK_CONTAINER(frame), hbox);
93                 gfw->vbox[0] = gtk_vbox_new(TRUE, 5);
94                 gfw->vbox[1] = gtk_vbox_new(TRUE, 5);
95                 gtk_box_pack_start(GTK_BOX(hbox), gfw->vbox[0], TRUE, TRUE, 5);
96                 gtk_box_pack_start(GTK_BOX(hbox), gfw->vbox[1], TRUE, TRUE, 5);
97         }
98
99         hbox = gtk_hbox_new(FALSE, 3);
100         gtk_box_pack_start(GTK_BOX(gfw->vbox[gfw->nr++ & 1]), hbox, FALSE, FALSE, 5);
101         return hbox;
102 }
103
104 /*
105  * Mark children as invisible, if needed.
106  */
107 static void gopt_set_children_visible(struct fio_option *parent,
108                                       gboolean visible)
109 {
110         GNode *child, *node;
111
112         if (parent->hide_on_set)
113                 visible = !visible;
114
115         node = g_node_find(gopt_dep_tree, G_IN_ORDER, G_TRAVERSE_ALL, parent);
116         child = g_node_first_child(node);
117         while (child) {
118                 struct fio_option *o = child->data;
119                 struct gopt *g = o->gui_data;
120
121                 /*
122                  * Recurse into child, if it also has children
123                  */
124                 if (g_node_n_children(child))
125                         gopt_set_children_visible(o, visible);
126
127                 if (gopt_widgets[g->opt_index])
128                         gtk_widget_set_sensitive(gopt_widgets[g->opt_index], visible);
129
130                 child = g_node_next_sibling(child);
131         }
132 }
133
134 static void gopt_str_changed(GtkEntry *entry, gpointer data)
135 {
136         struct gopt_str *s = (struct gopt_str *) data;
137         struct fio_option *o = &fio_options[s->gopt.opt_index];
138         const gchar *text;
139         int set;
140
141         text = gtk_entry_get_text(GTK_ENTRY(s->entry));
142         set = strcmp(text, "") != 0;
143         gopt_set_children_visible(o, set);
144 }
145
146 static void gopt_mark_index(struct gopt *gopt, unsigned int idx)
147 {
148         assert(!gopt_widgets[idx]);
149         gopt->opt_index = idx;
150         gopt_widgets[idx] = gopt->box;
151 }
152
153 static void gopt_str_destroy(GtkWidget *w, gpointer data)
154 {
155         struct gopt_str *s = (struct gopt_str *) data;
156
157         free(s);
158         gtk_widget_destroy(w);
159 }
160
161 static struct gopt *gopt_new_str_store(struct fio_option *o, const char *text,
162                                        unsigned int idx)
163 {
164         struct gopt_str *s;
165         GtkWidget *label;
166
167         s = malloc(sizeof(*s));
168         memset(s, 0, sizeof(*s));
169
170         s->gopt.box = gtk_hbox_new(FALSE, 3);
171         if (!o->lname)
172                 label = gtk_label_new(o->name);
173         else
174                 label = gtk_label_new(o->lname);
175
176         s->entry = gtk_entry_new();
177         gopt_mark_index(&s->gopt, idx);
178         if (text)
179                 gtk_entry_set_text(GTK_ENTRY(s->entry), text);
180         gtk_entry_set_editable(GTK_ENTRY(s->entry), 1);
181
182         if (o->def)
183                 gtk_entry_set_text(GTK_ENTRY(s->entry), o->def);
184
185         s->gopt.sig_handler = g_signal_connect(GTK_OBJECT(s->entry), "changed", G_CALLBACK(gopt_str_changed), s);
186         g_signal_connect(GTK_OBJECT(s->entry), "destroy", G_CALLBACK(gopt_str_destroy), s);
187
188         gtk_box_pack_start(GTK_BOX(s->gopt.box), s->entry, FALSE, FALSE, 0);
189         gtk_box_pack_start(GTK_BOX(s->gopt.box), label, FALSE, FALSE, 0);
190         return &s->gopt;
191 }
192
193 static void gopt_combo_changed(GtkComboBox *box, gpointer data)
194 {
195         struct gopt_combo *c = (struct gopt_combo *) data;
196         struct fio_option *o = &fio_options[c->gopt.opt_index];
197         unsigned int index;
198
199         index = gtk_combo_box_get_active(GTK_COMBO_BOX(c->combo));
200         gopt_set_children_visible(o, index);
201 }
202
203 static void gopt_combo_destroy(GtkWidget *w, gpointer data)
204 {
205         struct gopt_combo *c = (struct gopt_combo *) data;
206
207         free(c);
208         gtk_widget_destroy(w);
209 }
210
211 static struct gopt_combo *__gopt_new_combo(struct fio_option *o,
212                                            unsigned int idx)
213 {
214         struct gopt_combo *c;
215         GtkWidget *label;
216
217         c = malloc(sizeof(*c));
218         memset(c, 0, sizeof(*c));
219
220         c->gopt.box = gtk_hbox_new(FALSE, 3);
221         if (!o->lname)
222                 label = gtk_label_new(o->name);
223         else
224                 label = gtk_label_new(o->lname);
225
226         c->combo = gtk_combo_box_new_text();
227         gopt_mark_index(&c->gopt, idx);
228         g_signal_connect(GTK_OBJECT(c->combo), "destroy", G_CALLBACK(gopt_combo_destroy), c);
229
230         gtk_box_pack_start(GTK_BOX(c->gopt.box), c->combo, FALSE, FALSE, 0);
231         gtk_box_pack_start(GTK_BOX(c->gopt.box), label, FALSE, FALSE, 0);
232
233         return c;
234 }
235
236 static struct gopt *gopt_new_combo_str(struct fio_option *o, const char *text,
237                                        unsigned int idx)
238 {
239         struct gopt_combo *c;
240         struct value_pair *vp;
241         int i, active = 0;
242
243         c = __gopt_new_combo(o, idx);
244
245         i = 0;
246         vp = &o->posval[0];
247         while (vp->ival) {
248                 gtk_combo_box_append_text(GTK_COMBO_BOX(c->combo), vp->ival);
249                 if (o->def && !strcmp(vp->ival, o->def))
250                         active = i;
251                 if (text && !strcmp(vp->ival, text))
252                         active = i;
253                 vp++;
254                 i++;
255         }
256
257         gtk_combo_box_set_active(GTK_COMBO_BOX(c->combo), active);
258         c->gopt.sig_handler = g_signal_connect(GTK_OBJECT(c->combo), "changed", G_CALLBACK(gopt_combo_changed), c);
259         return &c->gopt;
260 }
261
262 static struct gopt *gopt_new_combo_int(struct fio_option *o, unsigned int *ip,
263                                        unsigned int idx)
264 {
265         struct gopt_combo *c;
266         struct value_pair *vp;
267         int i, active = 0;
268
269         c = __gopt_new_combo(o, idx);
270
271         i = 0;
272         vp = &o->posval[0];
273         while (vp->ival) {
274                 gtk_combo_box_append_text(GTK_COMBO_BOX(c->combo), vp->ival);
275                 if (ip && vp->oval == *ip)
276                         active = i;
277                 vp++;
278                 i++;
279         }
280
281         gtk_combo_box_set_active(GTK_COMBO_BOX(c->combo), active);
282         c->gopt.sig_handler = g_signal_connect(GTK_OBJECT(c->combo), "changed", G_CALLBACK(gopt_combo_changed), c);
283         return &c->gopt;
284 }
285
286 static struct gopt *gopt_new_str_multi(struct fio_option *o, unsigned int idx)
287 {
288         struct gopt_str_multi *m;
289         struct value_pair *vp;
290         GtkWidget *frame, *hbox;
291         int i;
292
293         m = malloc(sizeof(*m));
294         memset(m, 0, sizeof(*m));
295         m->gopt.box = gtk_hbox_new(FALSE, 3);
296         gopt_mark_index(&m->gopt, idx);
297
298         if (!o->lname)
299                 frame = gtk_frame_new(o->name);
300         else
301                 frame = gtk_frame_new(o->lname);
302         gtk_box_pack_start(GTK_BOX(m->gopt.box), frame, FALSE, FALSE, 3);
303
304         hbox = gtk_hbox_new(FALSE, 3);
305         gtk_container_add(GTK_CONTAINER(frame), hbox);
306
307         i = 0;
308         vp = &o->posval[0];
309         while (vp->ival) {
310                 m->checks[i] = gtk_check_button_new_with_label(vp->ival);
311                 gtk_widget_set_tooltip_text(m->checks[i], vp->help);
312                 gtk_box_pack_start(GTK_BOX(hbox), m->checks[i], FALSE, FALSE, 3);
313                 vp++;
314         }
315
316         return &m->gopt;
317 }
318
319 static void gopt_int_changed(GtkSpinButton *spin, gpointer data)
320 {
321         struct gopt_int *i = (struct gopt_int *) data;
322         struct fio_option *o = &fio_options[i->gopt.opt_index];
323         GtkAdjustment *adj;
324         int value, delta;
325
326         adj = gtk_spin_button_get_adjustment(spin);
327         value = gtk_adjustment_get_value(adj);
328         delta = value - i->lastval;
329         i->lastval = value;
330
331         if (o->inv_opt) {
332                 struct gopt *b_inv = o->inv_opt->gui_data;
333                 struct gopt_int *i_inv = container_of(b_inv, struct gopt_int, gopt);
334                 int cur_val;
335
336                 assert(o->type == o->inv_opt->type);
337
338                 cur_val = gtk_spin_button_get_value(GTK_SPIN_BUTTON(i_inv->spin));
339                 cur_val -= delta;
340                 g_signal_handler_block(G_OBJECT(i_inv->spin), i_inv->gopt.sig_handler);
341                 gtk_spin_button_set_value(GTK_SPIN_BUTTON(i_inv->spin), cur_val);
342                 g_signal_handler_unblock(G_OBJECT(i_inv->spin), i_inv->gopt.sig_handler);
343         }
344 }
345
346 static void gopt_int_destroy(GtkWidget *w, gpointer data)
347 {
348         struct gopt_int *i = (struct gopt_int *) data;
349
350         free(i);
351         gtk_widget_destroy(w);
352 }
353
354 static struct gopt_int *__gopt_new_int(struct fio_option *o,
355                                        unsigned long long *p, unsigned int idx)
356 {
357         unsigned long long defval;
358         struct gopt_int *i;
359         guint maxval, interval;
360         GtkWidget *label;
361
362         i = malloc(sizeof(*i));
363         memset(i, 0, sizeof(*i));
364         i->gopt.box = gtk_hbox_new(FALSE, 3);
365         if (!o->lname)
366                 label = gtk_label_new(o->name);
367         else
368                 label = gtk_label_new(o->lname);
369
370         maxval = o->maxval;
371         if (!maxval)
372                 maxval = UINT_MAX;
373
374         defval = 0;
375         if (p)
376                 defval = *p;
377         else if (o->def) {
378                 long long val;
379
380                 check_str_bytes(o->def, &val, NULL);
381                 defval = val;
382         }
383
384         interval = 1.0;
385         if (o->interval)
386                 interval = o->interval;
387
388         i->spin = gtk_spin_button_new_with_range(o->minval, maxval, interval);
389         gopt_mark_index(&i->gopt, idx);
390         gtk_spin_button_set_update_policy(GTK_SPIN_BUTTON(i->spin), GTK_UPDATE_IF_VALID);
391         gtk_spin_button_set_value(GTK_SPIN_BUTTON(i->spin), defval);
392         i->lastval = defval;
393         i->gopt.sig_handler = g_signal_connect(G_OBJECT(i->spin), "value-changed", G_CALLBACK(gopt_int_changed), i);
394         g_signal_connect(G_OBJECT(i->spin), "destroy", G_CALLBACK(gopt_int_destroy), i);
395
396         gtk_box_pack_start(GTK_BOX(i->gopt.box), i->spin, FALSE, FALSE, 0);
397         gtk_box_pack_start(GTK_BOX(i->gopt.box), label, FALSE, FALSE, 0);
398
399         return i;
400 }
401
402 static struct gopt *gopt_new_int(struct fio_option *o, unsigned int *ip,
403                                  unsigned int idx)
404 {
405         unsigned long long ullp;
406         struct gopt_int *i;
407
408         if (ip) {
409                 ullp = *ip;
410                 i = __gopt_new_int(o, &ullp, idx);
411         } else
412                 i = __gopt_new_int(o, NULL, idx);
413
414         return &i->gopt;
415 }
416
417 static struct gopt *gopt_new_ullong(struct fio_option *o, unsigned long long *p,
418                                     unsigned int idx)
419 {
420         struct gopt_int *i;
421
422         i = __gopt_new_int(o, p, idx);
423         return &i->gopt;
424 }
425
426 static void gopt_bool_toggled(GtkToggleButton *button, gpointer data)
427 {
428         struct gopt_bool *b = (struct gopt_bool *) data;
429         struct fio_option *o = &fio_options[b->gopt.opt_index];
430         gboolean set;
431
432         set = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(b->check));
433
434         if (o->inv_opt) {
435                 struct gopt *g_inv = o->inv_opt->gui_data;
436                 struct gopt_bool *b_inv = container_of(g_inv, struct gopt_bool, gopt);
437
438                 assert(o->type == o->inv_opt->type);
439
440                 g_signal_handler_block(G_OBJECT(b_inv->check), b_inv->gopt.sig_handler);
441                 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(b_inv->check), !set);
442                 g_signal_handler_unblock(G_OBJECT(b_inv->check), b_inv->gopt.sig_handler);
443         }
444
445         gopt_set_children_visible(o, set);
446 }
447
448 static void gopt_bool_destroy(GtkWidget *w, gpointer data)
449 {
450         struct gopt_bool *b = (struct gopt_bool *) data;
451
452         free(b);
453         gtk_widget_destroy(w);
454 }
455
456 static struct gopt *gopt_new_bool(struct fio_option *o, unsigned int *val,
457                                   unsigned int idx)
458 {
459         struct gopt_bool *b;
460         GtkWidget *label;
461         int defstate = 0;
462
463         b = malloc(sizeof(*b));
464         memset(b, 0, sizeof(*b));
465         b->gopt.box = gtk_hbox_new(FALSE, 3);
466         if (!o->lname)
467                 label = gtk_label_new(o->name);
468         else
469                 label = gtk_label_new(o->lname);
470
471         b->check = gtk_check_button_new();
472         gopt_mark_index(&b->gopt, idx);
473         if (val)
474                 defstate = *val;
475         else if (o->def && !strcmp(o->def, "1"))
476                 defstate = 1;
477
478         if (o->neg)
479                 defstate = !defstate;
480
481         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(b->check), defstate);
482         b->gopt.sig_handler = g_signal_connect(G_OBJECT(b->check), "toggled", G_CALLBACK(gopt_bool_toggled), b);
483         g_signal_connect(G_OBJECT(b->check), "destroy", G_CALLBACK(gopt_bool_destroy), b);
484
485         gtk_box_pack_start(GTK_BOX(b->gopt.box), b->check, FALSE, FALSE, 0);
486         gtk_box_pack_start(GTK_BOX(b->gopt.box), label, FALSE, FALSE, 0);
487         return &b->gopt;
488 }
489
490 /*
491  * These are paired 0/1 and 2/3. 0/2 are min values, 1/3 are max values.
492  * If the max is made smaller than min, adjust min down.
493  * If the min is made larger than max, adjust the max.
494  */
495 static void range_value_changed(GtkSpinButton *spin, gpointer data)
496 {
497         struct gopt_range *r = (struct gopt_range *) data;
498         int changed = -1, i;
499         gint val, mval;
500
501         for (i = 0; i < GOPT_RANGE_SPIN; i++) {
502                 if (GTK_SPIN_BUTTON(r->spins[i]) == spin) {
503                         changed = i;
504                         break;
505                 }
506         }
507
508         assert(changed != -1);
509
510         /*
511          * Min changed
512          */
513         if (changed == 0 || changed == 2) {
514                 GtkWidget *mspin = r->spins[changed + 1];
515
516                 val = gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(r->spins[changed]));
517                 mval = gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(mspin));
518                 if (val > mval)
519                         gtk_spin_button_set_value(GTK_SPIN_BUTTON(mspin), val);
520         } else {
521                 GtkWidget *mspin = r->spins[changed - 1];
522
523                 val = gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(r->spins[changed]));
524                 mval = gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(mspin));
525                 if (val < mval)
526                         gtk_spin_button_set_value(GTK_SPIN_BUTTON(mspin), val);
527         }
528 }
529
530 static void gopt_range_destroy(GtkWidget *w, gpointer data)
531 {
532         struct gopt_range *r = (struct gopt_range *) data;
533
534         free(r);
535         gtk_widget_destroy(w);
536 }
537
538 static struct gopt *gopt_new_int_range(struct fio_option *o, unsigned int **ip,
539                                        unsigned int idx)
540 {
541         struct gopt_range *r;
542         gint maxval, defval;
543         GtkWidget *label;
544         guint interval;
545         int i;
546
547         r = malloc(sizeof(*r));
548         memset(r, 0, sizeof(*r));
549         r->gopt.box = gtk_hbox_new(FALSE, 3);
550         gopt_mark_index(&r->gopt, idx);
551         if (!o->lname)
552                 label = gtk_label_new(o->name);
553         else
554                 label = gtk_label_new(o->lname);
555
556         maxval = o->maxval;
557         if (!maxval)
558                 maxval = INT_MAX;
559
560         defval = 0;
561         if (o->def) {
562                 long long val;
563
564                 check_str_bytes(o->def, &val, NULL);
565                 defval = val;
566         }
567
568         interval = 1.0;
569         if (o->interval)
570                 interval = o->interval;
571
572         for (i = 0; i < GOPT_RANGE_SPIN; i++) {
573                 r->spins[i] = gtk_spin_button_new_with_range(o->minval, maxval, interval);
574                 gtk_spin_button_set_update_policy(GTK_SPIN_BUTTON(r->spins[i]), GTK_UPDATE_IF_VALID);
575                 if (ip)
576                         gtk_spin_button_set_value(GTK_SPIN_BUTTON(r->spins[i]), *ip[i]);
577                 else
578                         gtk_spin_button_set_value(GTK_SPIN_BUTTON(r->spins[i]), defval);
579
580                 gtk_box_pack_start(GTK_BOX(r->gopt.box), r->spins[i], FALSE, FALSE, 0);
581                 g_signal_connect(G_OBJECT(r->spins[i]), "value-changed", G_CALLBACK(range_value_changed), r);
582         }
583
584         gtk_box_pack_start(GTK_BOX(r->gopt.box), label, FALSE, FALSE, 0);
585         g_signal_connect(G_OBJECT(r->gopt.box), "destroy", G_CALLBACK(gopt_range_destroy), r);
586         return &r->gopt;
587 }
588
589 static void gopt_str_val_destroy(GtkWidget *w, gpointer data)
590 {
591         struct gopt_str_val *g = (struct gopt_str_val *) data;
592
593         free(g);
594         gtk_widget_destroy(w);
595 }
596
597 static void gopt_str_val_spin_wrapped(GtkSpinButton *spin, gpointer data)
598 {
599         struct gopt_str_val *g = (struct gopt_str_val *) data;
600         unsigned int val;
601         GtkAdjustment *adj;
602         gint index;
603
604         adj = gtk_spin_button_get_adjustment(spin);
605         val = gtk_adjustment_get_value(adj);
606
607         /*
608          * Can't rely on exact value, as fast changes increment >= 1
609          */
610         if (!val) {
611                 index = gtk_combo_box_get_active(GTK_COMBO_BOX(g->combo));
612                 if (index + 1 <= g->maxindex) {
613                         val = 1;
614                         gtk_combo_box_set_active(GTK_COMBO_BOX(g->combo), ++index);
615                 } else
616                         val = 1023;
617                 gtk_spin_button_set_value(spin, val);
618         } else {
619                 index = gtk_combo_box_get_active(GTK_COMBO_BOX(g->combo));
620                 if (index) {
621                         gtk_combo_box_set_active(GTK_COMBO_BOX(g->combo), --index);
622                         gtk_spin_button_set_value(spin, 1023);
623                 } else
624                         gtk_spin_button_set_value(spin, 0);
625         }
626 }
627
628 static struct gopt *gopt_new_str_val(struct fio_option *o,
629                                      unsigned long long *p, unsigned int idx)
630 {
631         struct gopt_str_val *g;
632         const gchar *postfix[] = { "B", "KB", "MB", "GB", "PB", "TB", "" };
633         GtkWidget *label;
634         int i;
635
636         g = malloc(sizeof(*g));
637         memset(g, 0, sizeof(*g));
638         g->gopt.box = gtk_hbox_new(FALSE, 3);
639         if (!o->lname)
640                 label = gtk_label_new(o->name);
641         else
642                 label = gtk_label_new(o->lname);
643         gopt_mark_index(&g->gopt, idx);
644
645         g->spin = gtk_spin_button_new_with_range(0.0, 1023.0, 1.0);
646         gtk_spin_button_set_update_policy(GTK_SPIN_BUTTON(g->spin), GTK_UPDATE_IF_VALID);
647         gtk_spin_button_set_value(GTK_SPIN_BUTTON(g->spin), 0);
648         gtk_spin_button_set_wrap(GTK_SPIN_BUTTON(g->spin), 1);
649         gtk_box_pack_start(GTK_BOX(g->gopt.box), g->spin, FALSE, FALSE, 0);
650         g_signal_connect(G_OBJECT(g->spin), "wrapped", G_CALLBACK(gopt_str_val_spin_wrapped), g);
651
652         g->combo = gtk_combo_box_new_text();
653         i = 0;
654         while (strlen(postfix[i])) {
655                 gtk_combo_box_append_text(GTK_COMBO_BOX(g->combo), postfix[i]);
656                 i++;
657         }
658         g->maxindex = i - 1;
659         gtk_combo_box_set_active(GTK_COMBO_BOX(g->combo), 0);
660         gtk_box_pack_start(GTK_BOX(g->gopt.box), g->combo, FALSE, FALSE, 0);
661         gtk_box_pack_start(GTK_BOX(g->gopt.box), label, FALSE, FALSE, 3);
662
663         g_signal_connect(G_OBJECT(g->gopt.box), "destroy", G_CALLBACK(gopt_str_val_destroy), g);
664         return &g->gopt;
665 }
666
667 static void gopt_add_option(GtkWidget *hbox, struct fio_option *o,
668                             unsigned int opt_index, struct thread_options *to)
669 {
670         struct gopt *go = NULL;
671
672         switch (o->type) {
673         case FIO_OPT_STR_VAL: {
674                 unsigned long long *ullp = NULL;
675
676                 if (o->off1)
677                         ullp = td_var(to, o->off1);
678
679                 go = gopt_new_str_val(o, ullp, opt_index);
680                 break;
681                 }
682         case FIO_OPT_STR_VAL_TIME: {
683                 unsigned long long *ullp = NULL;
684
685                 if (o->off1)
686                         ullp = td_var(to, o->off1);
687
688                 go = gopt_new_ullong(o, ullp, opt_index);
689                 break;
690                 }
691         case FIO_OPT_INT: {
692                 unsigned int *ip = NULL;
693
694                 if (o->off1)
695                         ip = td_var(to, o->off1);
696
697                 go = gopt_new_int(o, ip, opt_index);
698                 break;
699                 }
700         case FIO_OPT_STR_SET:
701         case FIO_OPT_BOOL: {
702                 unsigned int *ip = NULL;
703
704                 if (o->off1)
705                         ip = td_var(to, o->off1);
706
707                 go = gopt_new_bool(o, ip, opt_index);
708                 break;
709                 }
710         case FIO_OPT_STR: {
711                 if (o->posval[0].ival) {
712                         unsigned int *ip = NULL;
713
714                         if (o->off1)
715                                 ip = td_var(to, o->off1);
716
717                         go = gopt_new_combo_int(o, ip, opt_index);
718                 } else {
719                         /* TODO: usually ->cb, or unsigned int pointer */
720                         go = gopt_new_str_store(o, NULL, opt_index);
721                 }
722
723                 break;
724                 }
725         case FIO_OPT_STR_STORE: {
726                 char *text = NULL;
727
728                 if (o->off1) {
729                         char **p = td_var(to, o->off1);
730                         text = *p;
731                 }
732
733                 if (!o->posval[0].ival) {
734                         go = gopt_new_str_store(o, text, opt_index);
735                         break;
736                 }
737
738                 go = gopt_new_combo_str(o, text, opt_index);
739                 break;
740                 }
741         case FIO_OPT_STR_MULTI:
742                 go = gopt_new_str_multi(o, opt_index);
743                 break;
744         case FIO_OPT_RANGE: {
745                 unsigned int *ip[4] = { td_var(to, o->off1),
746                                         td_var(to, o->off2),
747                                         td_var(to, o->off3),
748                                         td_var(to, o->off4) };
749
750                 go = gopt_new_int_range(o, ip, opt_index);
751                 break;
752                 }
753         /* still need to handle this one */
754         case FIO_OPT_FLOAT_LIST:
755                 break;
756         case FIO_OPT_DEPRECATED:
757                 break;
758         default:
759                 printf("ignore type %u\n", o->type);
760                 break;
761         }
762
763         if (go) {
764                 GtkWidget *dest;
765
766                 if (o->help)
767                         gtk_widget_set_tooltip_text(go->box, o->help);
768
769                 go->opt_type = o->type;
770                 o->gui_data = go;
771
772                 dest = gopt_get_group_frame(hbox, o->group);
773                 if (!dest)
774                         gtk_box_pack_start(GTK_BOX(hbox), go->box, FALSE, FALSE, 5);
775                 else
776                         gtk_box_pack_start(GTK_BOX(dest), go->box, FALSE, FALSE, 5);
777         }
778 }
779
780 static void gopt_add_options(GtkWidget **vboxes, struct thread_options *to)
781 {
782         GtkWidget *hbox = NULL;
783         int i;
784
785         gopt_dep_tree = g_node_new(NULL);
786
787         /*
788          * First add all options
789          */
790         for (i = 0; fio_options[i].name; i++) {
791                 struct fio_option *o = &fio_options[i];
792                 unsigned int mask = o->category;
793                 struct opt_group *og;
794                 GNode *node, *nparent;
795
796                 /*
797                  * Insert node with either the root parent, or an
798                  * option parent.
799                  */
800                 node = g_node_new(o);
801                 nparent = gopt_dep_tree;
802                 if (o->parent) {
803                         struct fio_option *parent;
804
805                         parent = fio_option_find(o->parent);
806                         nparent = g_node_find(gopt_dep_tree, G_IN_ORDER, G_TRAVERSE_ALL, parent);
807                         if (!nparent) {
808                                 log_err("fio: did not find parent %s for opt %s\n", o->name, o->parent);
809                                 nparent = gopt_dep_tree;
810                         }
811                 }
812
813                 g_node_insert(nparent, -1, node);
814
815                 while ((og = opt_group_from_mask(&mask)) != NULL) {
816                         GtkWidget *vbox = vboxes[ffz(~og->mask)];
817
818                         hbox = gtk_hbox_new(FALSE, 3);
819                         gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 5);
820                         gopt_add_option(hbox, o, i, to);
821                 }
822         }
823 }
824
825 static GtkWidget *gopt_add_group_tab(GtkWidget *notebook, struct opt_group *og)
826 {
827         GtkWidget *box, *vbox, *scroll;
828
829         scroll = gtk_scrolled_window_new(NULL, NULL);
830         gtk_container_set_border_width(GTK_CONTAINER(scroll), 5);
831         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scroll), GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
832
833         vbox = gtk_vbox_new(FALSE, 3);
834         box = gtk_hbox_new(FALSE, 0);
835         gtk_box_pack_start(GTK_BOX(vbox), box, FALSE, FALSE, 5);
836         gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(scroll), vbox);
837         gtk_notebook_append_page(GTK_NOTEBOOK(notebook), scroll, gtk_label_new(og->name));
838
839         return vbox;
840 }
841
842 static void gopt_add_group_tabs(GtkWidget *notebook, GtkWidget **vbox)
843 {
844         struct opt_group *og;
845         unsigned int i;
846
847         i = 0;
848         do {
849                 unsigned int mask = (1U << i);
850
851                 og = opt_group_from_mask(&mask);
852                 if (!og)
853                         break;
854                 vbox[i] = gopt_add_group_tab(notebook, og);
855                 i++;
856         } while (1);
857 }
858
859 void gopt_get_options_window(GtkWidget *window, struct gfio_client *gc)
860 {
861         GtkWidget *dialog, *notebook;
862         GtkWidget *vboxes[__FIO_OPT_C_NR];
863         struct gfio_client_options *gco;
864         struct thread_options *o;
865
866         /*
867          * Just choose the first item, we need to make each options
868          * entry the main notebook, with the below options view as
869          * a sub-notebook
870          */
871         assert(!flist_empty(&gc->o_list));
872         gco = flist_entry(gc->o_list.next, struct gfio_client_options, list);
873         o = &gco->o;
874
875         dialog = gtk_dialog_new_with_buttons("Fio options",
876                         GTK_WINDOW(window), GTK_DIALOG_DESTROY_WITH_PARENT,
877                         GTK_STOCK_OK, GTK_RESPONSE_ACCEPT,
878                         GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT, NULL);
879
880         gtk_widget_set_size_request(GTK_WIDGET(dialog), 1024, 768);
881
882         notebook = gtk_notebook_new();
883         gtk_notebook_set_scrollable(GTK_NOTEBOOK(notebook), 1);
884         gtk_notebook_popup_enable(GTK_NOTEBOOK(notebook));
885         gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox), notebook, TRUE, TRUE, 5);
886
887         gopt_add_group_tabs(notebook, vboxes);
888
889         gopt_add_options(vboxes, o);
890
891         gtk_widget_show_all(dialog);
892
893         gtk_dialog_run(GTK_DIALOG(dialog));
894
895         gtk_widget_destroy(dialog);
896
897         g_node_destroy(gopt_dep_tree);
898         gopt_dep_tree = NULL;
899         memset(gopt_widgets, 0, sizeof(gopt_widgets));
900         memset(gopt_g_widgets, 0, sizeof(gopt_g_widgets));
901 }