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