goptions: enable combo box hiding
[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 GtkWidget *gopt_get_group_frame(GtkWidget *box, unsigned int groupmask)
70 {
71         unsigned int mask, group;
72         struct opt_group *og;
73         GtkWidget *frame, *hbox;
74         struct gopt_frame_widget *gfw;
75
76         if (!groupmask)
77                 return 0;
78
79         mask = groupmask;
80         og = opt_group_cat_from_mask(&mask);
81         if (!og)
82                 return NULL;
83
84         group = ffz(~groupmask);
85         gfw = &gopt_g_widgets[group];
86         if (!gfw->vbox[0]) {
87                 frame = gtk_frame_new(og->name);
88                 gtk_box_pack_start(GTK_BOX(box), frame, FALSE, FALSE, 3);
89                 hbox = gtk_hbox_new(FALSE, 0);
90                 gtk_container_add(GTK_CONTAINER(frame), hbox);
91                 gfw->vbox[0] = gtk_vbox_new(TRUE, 5);
92                 gfw->vbox[1] = gtk_vbox_new(TRUE, 5);
93                 gtk_box_pack_start(GTK_BOX(hbox), gfw->vbox[0], TRUE, TRUE, 5);
94                 gtk_box_pack_start(GTK_BOX(hbox), gfw->vbox[1], TRUE, TRUE, 5);
95         }
96
97         hbox = gtk_hbox_new(FALSE, 3);
98         gtk_box_pack_start(GTK_BOX(gfw->vbox[gfw->nr++ & 1]), hbox, FALSE, FALSE, 5);
99         return hbox;
100 }
101
102 /*
103  * Mark children as invisible, if needed.
104  */
105 static void gopt_set_children_visible(struct fio_option *parent,
106                                       gboolean visible)
107 {
108         struct fio_option *o;
109         int i;
110
111         if (parent->hide_on_set)
112                 visible = !visible;
113
114         /*
115          * This isn't super fast, but it should not be an issue. If it is, we
116          * can speed it up by caching the lookup at least. Or we can do it
117          * once, at init time.
118          */
119         for (i = 0; fio_options[i].name; i++) {
120                 o = &fio_options[i];
121                 if (!o->parent || !o->hide)
122                         continue;
123
124                 if (strcmp(parent->name, o->parent))
125                         continue;
126
127                 if (gopt_widgets[i])
128                         gtk_widget_set_sensitive(gopt_widgets[i], visible);
129         }
130 }
131
132 static void gopt_str_changed(GtkEntry *entry, gpointer data)
133 {
134         struct gopt_str *s = (struct gopt_str *) data;
135         struct fio_option *o = &fio_options[s->gopt.opt_index];
136         const gchar *text;
137         int set;
138
139         text = gtk_entry_get_text(GTK_ENTRY(s->entry));
140         set = strcmp(text, "") != 0;
141         gopt_set_children_visible(o, set);
142 }
143
144 static void gopt_mark_index(struct gopt *gopt, unsigned int idx)
145 {
146         assert(!gopt_widgets[idx]);
147         gopt->opt_index = idx;
148         gopt_widgets[idx] = gopt->box;
149 }
150
151 static void gopt_str_destroy(GtkWidget *w, gpointer data)
152 {
153         struct gopt_str *s = (struct gopt_str *) data;
154
155         free(s);
156         gtk_widget_destroy(w);
157 }
158
159 static struct gopt *gopt_new_str_store(struct fio_option *o, const char *text,
160                                        unsigned int idx)
161 {
162         struct gopt_str *s;
163         GtkWidget *label;
164
165         s = malloc(sizeof(*s));
166         memset(s, 0, sizeof(*s));
167
168         s->gopt.box = gtk_hbox_new(FALSE, 3);
169         if (!o->lname)
170                 label = gtk_label_new(o->name);
171         else
172                 label = gtk_label_new(o->lname);
173
174         s->entry = gtk_entry_new();
175         gopt_mark_index(&s->gopt, idx);
176         if (text)
177                 gtk_entry_set_text(GTK_ENTRY(s->entry), text);
178         gtk_entry_set_editable(GTK_ENTRY(s->entry), 1);
179
180         if (o->def)
181                 gtk_entry_set_text(GTK_ENTRY(s->entry), o->def);
182
183         s->gopt.sig_handler = g_signal_connect(GTK_OBJECT(s->entry), "changed", G_CALLBACK(gopt_str_changed), s);
184         g_signal_connect(GTK_OBJECT(s->entry), "destroy", G_CALLBACK(gopt_str_destroy), s);
185
186         gtk_box_pack_start(GTK_BOX(s->gopt.box), s->entry, FALSE, FALSE, 0);
187         gtk_box_pack_start(GTK_BOX(s->gopt.box), label, FALSE, FALSE, 0);
188         o->gui_data = s;
189         return &s->gopt;
190 }
191
192 static void gopt_combo_changed(GtkComboBox *box, gpointer data)
193 {
194         struct gopt_combo *c = (struct gopt_combo *) data;
195         struct fio_option *o = &fio_options[c->gopt.opt_index];
196         unsigned int index;
197
198         index = gtk_combo_box_get_active(GTK_COMBO_BOX(c->combo));
199         gopt_set_children_visible(o, index);
200 }
201
202 static void gopt_combo_destroy(GtkWidget *w, gpointer data)
203 {
204         struct gopt_combo *c = (struct gopt_combo *) data;
205
206         free(c);
207         gtk_widget_destroy(w);
208 }
209
210 static struct gopt_combo *__gopt_new_combo(struct fio_option *o,
211                                            unsigned int idx)
212 {
213         struct gopt_combo *c;
214         GtkWidget *label;
215
216         c = malloc(sizeof(*c));
217         memset(c, 0, sizeof(*c));
218
219         c->gopt.box = gtk_hbox_new(FALSE, 3);
220         if (!o->lname)
221                 label = gtk_label_new(o->name);
222         else
223                 label = gtk_label_new(o->lname);
224
225         c->combo = gtk_combo_box_new_text();
226         gopt_mark_index(&c->gopt, idx);
227         g_signal_connect(GTK_OBJECT(c->combo), "destroy", G_CALLBACK(gopt_combo_destroy), c);
228
229         gtk_box_pack_start(GTK_BOX(c->gopt.box), c->combo, FALSE, FALSE, 0);
230         gtk_box_pack_start(GTK_BOX(c->gopt.box), label, FALSE, FALSE, 0);
231
232         o->gui_data = c;
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_int *i_inv = o->inv_opt->gui_data;
333                 int cur_val;
334
335                 assert(o->type == o->inv_opt->type);
336
337                 cur_val = gtk_spin_button_get_value(GTK_SPIN_BUTTON(i_inv->spin));
338                 cur_val -= delta;
339                 g_signal_handler_block(G_OBJECT(i_inv->spin), i_inv->gopt.sig_handler);
340                 gtk_spin_button_set_value(GTK_SPIN_BUTTON(i_inv->spin), cur_val);
341                 g_signal_handler_unblock(G_OBJECT(i_inv->spin), i_inv->gopt.sig_handler);
342         }
343 }
344
345 static void gopt_int_destroy(GtkWidget *w, gpointer data)
346 {
347         struct gopt_int *i = (struct gopt_int *) data;
348
349         free(i);
350         gtk_widget_destroy(w);
351 }
352
353 static struct gopt_int *__gopt_new_int(struct fio_option *o,
354                                        unsigned long long *p, unsigned int idx)
355 {
356         unsigned long long defval;
357         struct gopt_int *i;
358         guint maxval, interval;
359         GtkWidget *label;
360
361         i = malloc(sizeof(*i));
362         memset(i, 0, sizeof(*i));
363         i->gopt.box = gtk_hbox_new(FALSE, 3);
364         if (!o->lname)
365                 label = gtk_label_new(o->name);
366         else
367                 label = gtk_label_new(o->lname);
368
369         maxval = o->maxval;
370         if (!maxval)
371                 maxval = UINT_MAX;
372
373         defval = 0;
374         if (p)
375                 defval = *p;
376         else if (o->def) {
377                 long long val;
378
379                 check_str_bytes(o->def, &val, NULL);
380                 defval = val;
381         }
382
383         interval = 1.0;
384         if (o->interval)
385                 interval = o->interval;
386
387         i->spin = gtk_spin_button_new_with_range(o->minval, maxval, interval);
388         gopt_mark_index(&i->gopt, idx);
389         gtk_spin_button_set_update_policy(GTK_SPIN_BUTTON(i->spin), GTK_UPDATE_IF_VALID);
390         gtk_spin_button_set_value(GTK_SPIN_BUTTON(i->spin), defval);
391         i->lastval = defval;
392         i->gopt.sig_handler = g_signal_connect(G_OBJECT(i->spin), "value-changed", G_CALLBACK(gopt_int_changed), i);
393         g_signal_connect(G_OBJECT(i->spin), "destroy", G_CALLBACK(gopt_int_destroy), i);
394
395         gtk_box_pack_start(GTK_BOX(i->gopt.box), i->spin, FALSE, FALSE, 0);
396         gtk_box_pack_start(GTK_BOX(i->gopt.box), label, FALSE, FALSE, 0);
397
398         o->gui_data = i;
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_bool *b_inv = o->inv_opt->gui_data;
436
437                 assert(o->type == o->inv_opt->type);
438
439                 g_signal_handler_block(G_OBJECT(b_inv->check), b_inv->gopt.sig_handler);
440                 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(b_inv->check), !set);
441                 g_signal_handler_unblock(G_OBJECT(b_inv->check), b_inv->gopt.sig_handler);
442         }
443
444         gopt_set_children_visible(o, set);
445 }
446
447 static void gopt_bool_destroy(GtkWidget *w, gpointer data)
448 {
449         struct gopt_bool *b = (struct gopt_bool *) data;
450
451         free(b);
452         gtk_widget_destroy(w);
453 }
454
455 static struct gopt *gopt_new_bool(struct fio_option *o, unsigned int *val,
456                                   unsigned int idx)
457 {
458         struct gopt_bool *b;
459         GtkWidget *label;
460         int defstate = 0;
461
462         b = malloc(sizeof(*b));
463         memset(b, 0, sizeof(*b));
464         b->gopt.box = gtk_hbox_new(FALSE, 3);
465         if (!o->lname)
466                 label = gtk_label_new(o->name);
467         else
468                 label = gtk_label_new(o->lname);
469
470         b->check = gtk_check_button_new();
471         gopt_mark_index(&b->gopt, idx);
472         if (val)
473                 defstate = *val;
474         else if (o->def && !strcmp(o->def, "1"))
475                 defstate = 1;
476
477         if (o->neg)
478                 defstate = !defstate;
479
480         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(b->check), defstate);
481         b->gopt.sig_handler = g_signal_connect(G_OBJECT(b->check), "toggled", G_CALLBACK(gopt_bool_toggled), b);
482         g_signal_connect(G_OBJECT(b->check), "destroy", G_CALLBACK(gopt_bool_destroy), b);
483
484         gtk_box_pack_start(GTK_BOX(b->gopt.box), b->check, FALSE, FALSE, 0);
485         gtk_box_pack_start(GTK_BOX(b->gopt.box), label, FALSE, FALSE, 0);
486         o->gui_data = b;
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         o->gui_data = r;
587         return &r->gopt;
588 }
589
590 static void gopt_str_val_destroy(GtkWidget *w, gpointer data)
591 {
592         struct gopt_str_val *g = (struct gopt_str_val *) data;
593
594         free(g);
595         gtk_widget_destroy(w);
596 }
597
598 static void gopt_str_val_spin_wrapped(GtkSpinButton *spin, gpointer data)
599 {
600         struct gopt_str_val *g = (struct gopt_str_val *) data;
601         unsigned int val;
602         GtkAdjustment *adj;
603         gint index;
604
605         adj = gtk_spin_button_get_adjustment(spin);
606         val = gtk_adjustment_get_value(adj);
607
608         /*
609          * Can't rely on exact value, as fast changes increment >= 1
610          */
611         if (!val) {
612                 index = gtk_combo_box_get_active(GTK_COMBO_BOX(g->combo));
613                 if (index + 1 <= g->maxindex) {
614                         val = 1;
615                         gtk_combo_box_set_active(GTK_COMBO_BOX(g->combo), ++index);
616                 } else
617                         val = 1023;
618                 gtk_spin_button_set_value(spin, val);
619         } else {
620                 index = gtk_combo_box_get_active(GTK_COMBO_BOX(g->combo));
621                 if (index) {
622                         gtk_combo_box_set_active(GTK_COMBO_BOX(g->combo), --index);
623                         gtk_spin_button_set_value(spin, 1023);
624                 } else
625                         gtk_spin_button_set_value(spin, 0);
626         }
627 }
628
629 static struct gopt *gopt_new_str_val(struct fio_option *o,
630                                      unsigned long long *p, unsigned int idx)
631 {
632         struct gopt_str_val *g;
633         const gchar *postfix[] = { "B", "KB", "MB", "GB", "PB", "TB", "" };
634         GtkWidget *label;
635         int i;
636
637         g = malloc(sizeof(*g));
638         memset(g, 0, sizeof(*g));
639         g->gopt.box = gtk_hbox_new(FALSE, 3);
640         if (!o->lname)
641                 label = gtk_label_new(o->name);
642         else
643                 label = gtk_label_new(o->lname);
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         o->gui_data = g;
665         return &g->gopt;
666 }
667
668 static void gopt_add_option(GtkWidget *hbox, struct fio_option *o,
669                             unsigned int opt_index, struct thread_options *to)
670 {
671         struct gopt *go = NULL;
672
673         switch (o->type) {
674         case FIO_OPT_STR_VAL: {
675                 unsigned long long *ullp = NULL;
676
677                 if (o->off1)
678                         ullp = td_var(to, o->off1);
679
680                 go = gopt_new_str_val(o, ullp, opt_index);
681                 break;
682                 }
683         case FIO_OPT_STR_VAL_TIME: {
684                 unsigned long long *ullp = NULL;
685
686                 if (o->off1)
687                         ullp = td_var(to, o->off1);
688
689                 go = gopt_new_ullong(o, ullp, opt_index);
690                 break;
691                 }
692         case FIO_OPT_INT: {
693                 unsigned int *ip = NULL;
694
695                 if (o->off1)
696                         ip = td_var(to, o->off1);
697
698                 go = gopt_new_int(o, ip, opt_index);
699                 break;
700                 }
701         case FIO_OPT_STR_SET:
702         case FIO_OPT_BOOL: {
703                 unsigned int *ip = NULL;
704
705                 if (o->off1)
706                         ip = td_var(to, o->off1);
707
708                 go = gopt_new_bool(o, ip, opt_index);
709                 break;
710                 }
711         case FIO_OPT_STR: {
712                 if (o->posval[0].ival) {
713                         unsigned int *ip = NULL;
714
715                         if (o->off1)
716                                 ip = td_var(to, o->off1);
717
718                         go = gopt_new_combo_int(o, ip, opt_index);
719                 } else {
720                         /* TODO: usually ->cb, or unsigned int pointer */
721                         go = gopt_new_str_store(o, NULL, opt_index);
722                 }
723
724                 break;
725                 }
726         case FIO_OPT_STR_STORE: {
727                 char *text = NULL;
728
729                 if (o->off1) {
730                         char **p = td_var(to, o->off1);
731                         text = *p;
732                 }
733
734                 if (!o->posval[0].ival) {
735                         go = gopt_new_str_store(o, text, opt_index);
736                         break;
737                 }
738
739                 go = gopt_new_combo_str(o, text, opt_index);
740                 break;
741                 }
742         case FIO_OPT_STR_MULTI:
743                 go = gopt_new_str_multi(o, opt_index);
744                 break;
745         case FIO_OPT_RANGE: {
746                 unsigned int *ip[4] = { td_var(to, o->off1),
747                                         td_var(to, o->off2),
748                                         td_var(to, o->off3),
749                                         td_var(to, o->off4) };
750
751                 go = gopt_new_int_range(o, ip, opt_index);
752                 break;
753                 }
754         /* still need to handle this one */
755         case FIO_OPT_FLOAT_LIST:
756                 break;
757         case FIO_OPT_DEPRECATED:
758                 break;
759         default:
760                 printf("ignore type %u\n", o->type);
761                 break;
762         }
763
764         if (go) {
765                 GtkWidget *dest;
766
767                 if (o->help)
768                         gtk_widget_set_tooltip_text(go->box, o->help);
769
770                 go->opt_type = o->type;
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         /*
786          * First add all options
787          */
788         for (i = 0; fio_options[i].name; i++) {
789                 struct fio_option *o = &fio_options[i];
790                 unsigned int mask = o->category;
791                 struct opt_group *og;
792
793                 while ((og = opt_group_from_mask(&mask)) != NULL) {
794                         GtkWidget *vbox = vboxes[ffz(~og->mask)];
795
796                         hbox = gtk_hbox_new(FALSE, 3);
797                         gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 5);
798                         gopt_add_option(hbox, o, i, to);
799                 }
800         }
801 }
802
803 static GtkWidget *gopt_add_group_tab(GtkWidget *notebook, struct opt_group *og)
804 {
805         GtkWidget *box, *vbox, *scroll;
806
807         scroll = gtk_scrolled_window_new(NULL, NULL);
808         gtk_container_set_border_width(GTK_CONTAINER(scroll), 5);
809         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scroll), GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
810
811         vbox = gtk_vbox_new(FALSE, 3);
812         box = gtk_hbox_new(FALSE, 0);
813         gtk_box_pack_start(GTK_BOX(vbox), box, FALSE, FALSE, 5);
814         gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(scroll), vbox);
815         gtk_notebook_append_page(GTK_NOTEBOOK(notebook), scroll, gtk_label_new(og->name));
816
817         return vbox;
818 }
819
820 static void gopt_add_group_tabs(GtkWidget *notebook, GtkWidget **vbox)
821 {
822         struct opt_group *og;
823         unsigned int i;
824
825         i = 0;
826         do {
827                 unsigned int mask = (1U << i);
828
829                 og = opt_group_from_mask(&mask);
830                 if (!og)
831                         break;
832                 vbox[i] = gopt_add_group_tab(notebook, og);
833                 i++;
834         } while (1);
835 }
836
837 void gopt_get_options_window(GtkWidget *window, struct thread_options *o)
838 {
839         GtkWidget *dialog, *notebook;
840         GtkWidget *vboxes[__FIO_OPT_C_NR];
841
842         dialog = gtk_dialog_new_with_buttons("Fio options",
843                         GTK_WINDOW(window), GTK_DIALOG_DESTROY_WITH_PARENT,
844                         GTK_STOCK_OK, GTK_RESPONSE_ACCEPT,
845                         GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT, NULL);
846
847         gtk_widget_set_size_request(GTK_WIDGET(dialog), 1024, 768);
848
849         notebook = gtk_notebook_new();
850         gtk_notebook_set_scrollable(GTK_NOTEBOOK(notebook), 1);
851         gtk_notebook_popup_enable(GTK_NOTEBOOK(notebook));
852         gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox), notebook, TRUE, TRUE, 5);
853
854         gopt_add_group_tabs(notebook, vboxes);
855
856         gopt_add_options(vboxes, o);
857
858         gtk_widget_show_all(dialog);
859
860         gtk_dialog_run(GTK_DIALOG(dialog));
861
862         gtk_widget_destroy(dialog);
863         memset(gopt_widgets, 0, sizeof(gopt_widgets));
864         memset(gopt_g_widgets, 0, sizeof(gopt_g_widgets));
865 }