parse: get rid of raw option offsets
[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 "gerror.h"
13 #include "parse.h"
14
15 struct gopt {
16         GtkWidget *box;
17         unsigned int opt_index;
18         unsigned int opt_type;
19         gulong sig_handler;
20         struct gopt_job_view *gjv;
21         struct flist_head changed_list;
22 };
23
24 struct gopt_combo {
25         struct gopt gopt;
26         GtkWidget *combo;
27 };
28
29 struct gopt_int {
30         struct gopt gopt;
31         unsigned long long lastval;
32         GtkWidget *spin;
33 };
34
35 struct gopt_bool {
36         struct gopt gopt;
37         GtkWidget *check;
38 };
39
40 struct gopt_str {
41         struct gopt gopt;
42         GtkWidget *entry;
43 };
44
45 struct gopt_str_val {
46         struct gopt gopt;
47         GtkWidget *spin;
48         GtkWidget *combo;
49         unsigned int maxindex;
50 };
51
52 #define GOPT_RANGE_SPIN 4
53
54 struct gopt_range {
55         struct gopt gopt;
56         GtkWidget *spins[GOPT_RANGE_SPIN];
57 };
58
59 struct gopt_str_multi {
60         struct gopt gopt;
61         GtkWidget *checks[PARSE_MAX_VP];
62 };
63
64 enum {
65         GOPT_COMBO_INT = 1,
66         GOPT_COMBO_STR,
67         GOPT_INT,
68         GOPT_BOOL,
69         GOPT_STR,
70         GOPT_STR_VAL,
71         GOPT_RANGE,
72         GOPT_STR_MULTI,
73 };
74
75 struct gopt_frame_widget {
76         GtkWidget *vbox[2];
77         unsigned int nr;
78 };
79
80 struct gopt_job_view {
81         struct gopt_frame_widget g_widgets[__FIO_OPT_G_NR];
82         GtkWidget *vboxes[__FIO_OPT_C_NR];
83         struct gopt *gopts[FIO_MAX_OPTS];
84         GtkWidget *dialog;
85         GtkWidget *job_combo;
86         struct gfio_client *client;
87         struct flist_head changed_list;
88         struct thread_options *o;
89         int in_job_switch;
90 };
91
92 static GNode *gopt_dep_tree;
93
94 static GtkWidget *gopt_get_group_frame(struct gopt_job_view *gjv,
95                                        GtkWidget *box, unsigned int groupmask)
96 {
97         unsigned int mask, group;
98         struct opt_group *og;
99         GtkWidget *frame, *hbox;
100         struct gopt_frame_widget *gfw;
101
102         if (!groupmask)
103                 return 0;
104
105         mask = groupmask;
106         og = opt_group_cat_from_mask(&mask);
107         if (!og)
108                 return NULL;
109
110         group = ffz(~groupmask);
111         gfw = &gjv->g_widgets[group];
112         if (!gfw->vbox[0]) {
113                 frame = gtk_frame_new(og->name);
114                 gtk_box_pack_start(GTK_BOX(box), frame, FALSE, FALSE, 3);
115                 hbox = gtk_hbox_new(FALSE, 0);
116                 gtk_container_add(GTK_CONTAINER(frame), hbox);
117                 gfw->vbox[0] = gtk_vbox_new(TRUE, 5);
118                 gfw->vbox[1] = gtk_vbox_new(TRUE, 5);
119                 gtk_box_pack_start(GTK_BOX(hbox), gfw->vbox[0], TRUE, TRUE, 5);
120                 gtk_box_pack_start(GTK_BOX(hbox), gfw->vbox[1], TRUE, TRUE, 5);
121         }
122
123         hbox = gtk_hbox_new(FALSE, 3);
124         gtk_box_pack_start(GTK_BOX(gfw->vbox[gfw->nr++ & 1]), hbox, FALSE, FALSE, 5);
125         return hbox;
126 }
127
128 /*
129  * Mark children as invisible, if needed.
130  */
131 static void gopt_set_children_visible(struct gopt_job_view *gjv,
132                                       struct fio_option *parent,
133                                       gboolean visible)
134 {
135         GNode *child, *node;
136
137         if (parent->hide_on_set)
138                 visible = !visible;
139
140         node = g_node_find(gopt_dep_tree, G_IN_ORDER, G_TRAVERSE_ALL, parent);
141         child = g_node_first_child(node);
142         while (child) {
143                 struct fio_option *o = child->data;
144                 struct gopt *g = o->gui_data;
145                 GtkWidget *widget = g->box;
146
147                 /*
148                  * Recurse into child, if it also has children
149                  */
150                 if (g_node_n_children(child))
151                         gopt_set_children_visible(gjv, o, visible);
152
153                 gtk_widget_set_sensitive(widget, visible);
154                 child = g_node_next_sibling(child);
155         }
156 }
157
158 static void gopt_mark_index(struct gopt_job_view *gjv, struct gopt *gopt,
159                             unsigned int idx, int type)
160 {
161         INIT_FLIST_HEAD(&gopt->changed_list);
162
163         assert(!gjv->gopts[idx]);
164         gopt->opt_index = idx;
165         gopt->opt_type = type;
166         gopt->gjv = gjv;
167         gjv->gopts[idx] = gopt;
168 }
169
170 static void gopt_dialog_update_apply_button(struct gopt_job_view *gjv)
171 {
172         GtkDialog *dialog = GTK_DIALOG(gjv->dialog);
173         gboolean set;
174
175         set = !flist_empty(&gjv->changed_list);
176         gtk_dialog_set_response_sensitive(dialog, GTK_RESPONSE_APPLY, set);
177
178         if (set) {
179                 gtk_widget_set_sensitive(gjv->job_combo, 0);
180                 gtk_widget_set_tooltip_text(gjv->job_combo, "Apply option changes before switching to a new job");
181         } else {
182                 gtk_widget_set_sensitive(gjv->job_combo, 1);
183                 gtk_widget_set_tooltip_text(gjv->job_combo, "Change current job");
184         }
185 }
186
187 static void gopt_changed(struct gopt *gopt)
188 {
189         struct gopt_job_view *gjv = gopt->gjv;
190
191         if (gjv->in_job_switch)
192                 return;
193
194         /*
195          * Add to changed list. This also prevents the option from being
196          * freed when the widget is destroyed.
197          */
198         if (flist_empty(&gopt->changed_list)) {
199                 flist_add_tail(&gopt->changed_list, &gjv->changed_list);
200                 gopt_dialog_update_apply_button(gjv);
201         }
202 }
203
204 static void gopt_str_changed(GtkEntry *entry, gpointer data)
205 {
206         struct gopt_str *s = (struct gopt_str *) data;
207         struct fio_option *o = &fio_options[s->gopt.opt_index];
208         const gchar *text;
209         int set;
210
211         gopt_changed(&s->gopt);
212
213         text = gtk_entry_get_text(GTK_ENTRY(s->entry));
214         set = strcmp(text, "") != 0;
215
216         gopt_set_children_visible(s->gopt.gjv, o, set);
217 }
218
219 static void gopt_str_destroy(GtkWidget *w, gpointer data)
220 {
221         struct gopt_str *s = (struct gopt_str *) data;
222
223         free(s);
224         gtk_widget_destroy(w);
225 }
226
227 static void gopt_str_store_set_val(struct gopt_str *s, const char *text)
228 {
229         if (text)
230                 gtk_entry_set_text(GTK_ENTRY(s->entry), text);
231 }
232
233 static struct gopt *gopt_new_str_store(struct gopt_job_view *gjv,
234                                        struct fio_option *o, const char *text,
235                                        unsigned int idx)
236 {
237         struct gopt_str *s;
238         GtkWidget *label;
239
240         s = calloc(1, sizeof(*s));
241
242         s->gopt.box = gtk_hbox_new(FALSE, 3);
243         if (!o->lname)
244                 label = gtk_label_new(o->name);
245         else
246                 label = gtk_label_new(o->lname);
247
248         s->entry = gtk_entry_new();
249         gopt_mark_index(gjv, &s->gopt, idx, GOPT_STR);
250         gtk_editable_set_editable(GTK_EDITABLE(s->entry), 1);
251
252         if (text)
253                 gopt_str_store_set_val(s, text);
254         else if (o->def)
255                 gopt_str_store_set_val(s, o->def);
256
257         s->gopt.sig_handler = g_signal_connect(G_OBJECT(s->entry), "changed", G_CALLBACK(gopt_str_changed), s);
258         g_signal_connect(G_OBJECT(s->entry), "destroy", G_CALLBACK(gopt_str_destroy), s);
259
260         gtk_box_pack_start(GTK_BOX(s->gopt.box), s->entry, FALSE, FALSE, 0);
261         gtk_box_pack_start(GTK_BOX(s->gopt.box), label, FALSE, FALSE, 0);
262         return &s->gopt;
263 }
264
265 static void gopt_combo_changed(GtkComboBox *box, gpointer data)
266 {
267         struct gopt_combo *c = (struct gopt_combo *) data;
268         struct fio_option *o = &fio_options[c->gopt.opt_index];
269         unsigned int index;
270
271         gopt_changed(&c->gopt);
272
273         index = gtk_combo_box_get_active(GTK_COMBO_BOX(c->combo));
274
275         gopt_set_children_visible(c->gopt.gjv, o, index);
276 }
277
278 static void gopt_combo_destroy(GtkWidget *w, gpointer data)
279 {
280         struct gopt_combo *c = (struct gopt_combo *) data;
281
282         free(c);
283         gtk_widget_destroy(w);
284 }
285
286 static struct gopt_combo *__gopt_new_combo(struct gopt_job_view *gjv,
287                                            struct fio_option *o,
288                                            unsigned int idx, int type)
289 {
290         struct gopt_combo *c;
291         GtkWidget *label;
292
293         c = calloc(1, sizeof(*c));
294
295         c->gopt.box = gtk_hbox_new(FALSE, 3);
296         if (!o->lname)
297                 label = gtk_label_new(o->name);
298         else
299                 label = gtk_label_new(o->lname);
300
301         c->combo = gtk_combo_box_text_new();
302         gopt_mark_index(gjv, &c->gopt, idx, type);
303         g_signal_connect(G_OBJECT(c->combo), "destroy", G_CALLBACK(gopt_combo_destroy), c);
304
305         gtk_box_pack_start(GTK_BOX(c->gopt.box), c->combo, FALSE, FALSE, 0);
306         gtk_box_pack_start(GTK_BOX(c->gopt.box), label, FALSE, FALSE, 0);
307
308         return c;
309 }
310
311 static void gopt_combo_str_set_val(struct gopt_combo *c, const char *text)
312 {
313         struct fio_option *o = &fio_options[c->gopt.opt_index];
314         struct value_pair *vp;
315         int i;
316
317         i = 0;
318         vp = &o->posval[0];
319         while (vp->ival) {
320                 if (!strcmp(vp->ival, text)) {
321                         gtk_combo_box_set_active(GTK_COMBO_BOX(c->combo), i);
322                         break;
323                 }
324                 vp++;
325                 i++;
326         }
327 }
328
329 static struct gopt *gopt_new_combo_str(struct gopt_job_view *gjv,
330                                        struct fio_option *o, const char *text,
331                                        unsigned int idx)
332 {
333         struct gopt_combo *c;
334         struct value_pair *vp;
335         int i, active = 0;
336
337         c = __gopt_new_combo(gjv, o, idx, GOPT_COMBO_STR);
338
339         i = 0;
340         vp = &o->posval[0];
341         while (vp->ival) {
342                 gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(c->combo), vp->ival);
343                 if (o->def && !strcmp(vp->ival, o->def))
344                         active = i;
345                 vp++;
346                 i++;
347         }
348
349         gtk_combo_box_set_active(GTK_COMBO_BOX(c->combo), active);
350         if (text)
351                 gopt_combo_str_set_val(c, text);
352         c->gopt.sig_handler = g_signal_connect(G_OBJECT(c->combo), "changed", G_CALLBACK(gopt_combo_changed), c);
353         return &c->gopt;
354 }
355
356 static void gopt_combo_int_set_val(struct gopt_combo *c, unsigned int ip)
357 {
358         struct fio_option *o = &fio_options[c->gopt.opt_index];
359         struct value_pair *vp;
360         int i;
361
362         i = 0;
363         vp = &o->posval[0];
364         while (vp->ival) {
365                 if (vp->oval == ip) {
366                         gtk_combo_box_set_active(GTK_COMBO_BOX(c->combo), i);
367                         break;
368                 }
369                 vp++;
370                 i++;
371         }
372 }
373
374 static struct gopt *gopt_new_combo_int(struct gopt_job_view *gjv,
375                                        struct fio_option *o, unsigned int *ip,
376                                        unsigned int idx)
377 {
378         struct gopt_combo *c;
379         struct value_pair *vp;
380         int i, active = 0;
381
382         c = __gopt_new_combo(gjv, o, idx, GOPT_COMBO_INT);
383
384         i = 0;
385         vp = &o->posval[0];
386         while (vp->ival) {
387                 gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(c->combo), vp->ival);
388                 if (ip && vp->oval == *ip)
389                         active = i;
390                 vp++;
391                 i++;
392         }
393
394         gtk_combo_box_set_active(GTK_COMBO_BOX(c->combo), active);
395         if (ip)
396                 gopt_combo_int_set_val(c, *ip);
397         c->gopt.sig_handler = g_signal_connect(G_OBJECT(c->combo), "changed", G_CALLBACK(gopt_combo_changed), c);
398         return &c->gopt;
399 }
400
401 static void gopt_str_multi_toggled(GtkToggleButton *button, gpointer data)
402 {
403         struct gopt_str_multi *m = (struct gopt_str_multi *) data;
404
405         gopt_changed(&m->gopt);
406 }
407
408 static void gopt_str_multi_destroy(GtkWidget *w, gpointer data)
409 {
410         struct gopt_str_multi *m = (struct gopt_str_multi *) data;
411
412         free(m);
413         gtk_widget_destroy(w);
414 }
415
416 static void gopt_str_multi_set_val(struct gopt_str_multi *m, int val)
417 {
418 }
419
420 static struct gopt *gopt_new_str_multi(struct gopt_job_view *gjv,
421                                        struct fio_option *o, unsigned int idx)
422 {
423         struct gopt_str_multi *m;
424         struct value_pair *vp;
425         GtkWidget *frame, *hbox;
426         int i;
427
428         m = calloc(1, sizeof(*m));
429         m->gopt.box = gtk_hbox_new(FALSE, 3);
430         gopt_mark_index(gjv, &m->gopt, idx, GOPT_STR_MULTI);
431
432         if (!o->lname)
433                 frame = gtk_frame_new(o->name);
434         else
435                 frame = gtk_frame_new(o->lname);
436         gtk_box_pack_start(GTK_BOX(m->gopt.box), frame, FALSE, FALSE, 3);
437
438         hbox = gtk_hbox_new(FALSE, 3);
439         gtk_container_add(GTK_CONTAINER(frame), hbox);
440
441         i = 0;
442         vp = &o->posval[0];
443         while (vp->ival) {
444                 m->checks[i] = gtk_check_button_new_with_label(vp->ival);
445                 gtk_widget_set_tooltip_text(m->checks[i], vp->help);
446                 gtk_box_pack_start(GTK_BOX(hbox), m->checks[i], FALSE, FALSE, 3);
447                 g_signal_connect(G_OBJECT(m->checks[i]), "toggled", G_CALLBACK(gopt_str_multi_toggled), m);
448                 vp++;
449                 i++;
450         }
451
452         gopt_str_multi_set_val(m, 0);
453         g_signal_connect(G_OBJECT(m->gopt.box), "destroy", G_CALLBACK(gopt_str_multi_destroy), m);
454         return &m->gopt;
455 }
456
457 static void gopt_int_changed(GtkSpinButton *spin, gpointer data)
458 {
459         struct gopt_int *i = (struct gopt_int *) data;
460         struct fio_option *o = &fio_options[i->gopt.opt_index];
461         GtkAdjustment *adj;
462         int value, delta;
463
464         gopt_changed(&i->gopt);
465
466         adj = gtk_spin_button_get_adjustment(spin);
467         value = gtk_adjustment_get_value(adj);
468         delta = value - i->lastval;
469         i->lastval = value;
470
471         if (o->inv_opt) {
472                 struct gopt *b_inv = o->inv_opt->gui_data;
473                 struct gopt_int *i_inv = container_of(b_inv, struct gopt_int, gopt);
474                 int cur_val;
475
476                 assert(o->type == o->inv_opt->type);
477
478                 cur_val = gtk_spin_button_get_value(GTK_SPIN_BUTTON(i_inv->spin));
479                 cur_val -= delta;
480                 g_signal_handler_block(G_OBJECT(i_inv->spin), i_inv->gopt.sig_handler);
481                 gtk_spin_button_set_value(GTK_SPIN_BUTTON(i_inv->spin), cur_val);
482                 g_signal_handler_unblock(G_OBJECT(i_inv->spin), i_inv->gopt.sig_handler);
483         }
484 }
485
486 static void gopt_int_destroy(GtkWidget *w, gpointer data)
487 {
488         struct gopt_int *i = (struct gopt_int *) data;
489
490         free(i);
491         gtk_widget_destroy(w);
492 }
493
494 static void gopt_int_set_val(struct gopt_int *i, unsigned long long p)
495 {
496         gtk_spin_button_set_value(GTK_SPIN_BUTTON(i->spin), p);
497         i->lastval = p;
498 }
499
500 static struct gopt_int *__gopt_new_int(struct gopt_job_view *gjv,
501                                        struct fio_option *o,
502                                        unsigned long long *p, unsigned int idx)
503 {
504         unsigned long long defval;
505         struct gopt_int *i;
506         guint maxval, interval;
507         GtkWidget *label;
508
509         i = calloc(1, sizeof(*i));
510         i->gopt.box = gtk_hbox_new(FALSE, 3);
511         if (!o->lname)
512                 label = gtk_label_new(o->name);
513         else
514                 label = gtk_label_new(o->lname);
515
516         maxval = o->maxval;
517         if (!maxval)
518                 maxval = UINT_MAX;
519
520         defval = 0;
521         if (p)
522                 defval = *p;
523         else if (o->def) {
524                 long long val;
525
526                 check_str_bytes(o->def, &val, o);
527                 defval = val;
528         }
529
530         interval = 1.0;
531         if (o->interval)
532                 interval = o->interval;
533
534         i->spin = gtk_spin_button_new_with_range(o->minval, maxval, interval);
535         gopt_mark_index(gjv, &i->gopt, idx, GOPT_INT);
536         gtk_spin_button_set_update_policy(GTK_SPIN_BUTTON(i->spin), GTK_UPDATE_IF_VALID);
537         if (p)
538                 gopt_int_set_val(i, *p);
539         else
540                 gopt_int_set_val(i, defval);
541         i->gopt.sig_handler = g_signal_connect(G_OBJECT(i->spin), "value-changed", G_CALLBACK(gopt_int_changed), i);
542         g_signal_connect(G_OBJECT(i->spin), "destroy", G_CALLBACK(gopt_int_destroy), i);
543
544         gtk_box_pack_start(GTK_BOX(i->gopt.box), i->spin, FALSE, FALSE, 0);
545         gtk_box_pack_start(GTK_BOX(i->gopt.box), label, FALSE, FALSE, 0);
546
547         return i;
548 }
549
550 static struct gopt *gopt_new_int(struct gopt_job_view *gjv,
551                                  struct fio_option *o, unsigned int *ip,
552                                  unsigned int idx)
553 {
554         unsigned long long ullp;
555         struct gopt_int *i;
556
557         if (ip) {
558                 ullp = *ip;
559                 i = __gopt_new_int(gjv, o, &ullp, idx);
560         } else
561                 i = __gopt_new_int(gjv, o, NULL, idx);
562
563         return &i->gopt;
564 }
565
566 static struct gopt *gopt_new_ullong(struct gopt_job_view *gjv,
567                                     struct fio_option *o, unsigned long long *p,
568                                     unsigned int idx)
569 {
570         struct gopt_int *i;
571
572         i = __gopt_new_int(gjv, o, p, idx);
573         return &i->gopt;
574 }
575
576 static void gopt_bool_toggled(GtkToggleButton *button, gpointer data)
577 {
578         struct gopt_bool *b = (struct gopt_bool *) data;
579         struct fio_option *o = &fio_options[b->gopt.opt_index];
580         gboolean set;
581
582         gopt_changed(&b->gopt);
583
584         set = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(b->check));
585
586         if (o->inv_opt) {
587                 struct gopt *g_inv = o->inv_opt->gui_data;
588                 struct gopt_bool *b_inv = container_of(g_inv, struct gopt_bool, gopt);
589
590                 assert(o->type == o->inv_opt->type);
591
592                 g_signal_handler_block(G_OBJECT(b_inv->check), b_inv->gopt.sig_handler);
593                 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(b_inv->check), !set);
594                 g_signal_handler_unblock(G_OBJECT(b_inv->check), b_inv->gopt.sig_handler);
595         }
596
597         gopt_set_children_visible(b->gopt.gjv, o, set);
598 }
599
600 static void gopt_bool_destroy(GtkWidget *w, gpointer data)
601 {
602         struct gopt_bool *b = (struct gopt_bool *) data;
603
604         free(b);
605         gtk_widget_destroy(w);
606 }
607
608 static void gopt_bool_set_val(struct gopt_bool *b, unsigned int val)
609 {
610         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(b->check), val);
611 }
612
613 static struct gopt *gopt_new_bool(struct gopt_job_view *gjv,
614                                   struct fio_option *o, unsigned int *val,
615                                   unsigned int idx)
616 {
617         struct gopt_bool *b;
618         GtkWidget *label;
619         int defstate = 0;
620
621         b = calloc(1, sizeof(*b));
622         b->gopt.box = gtk_hbox_new(FALSE, 3);
623         if (!o->lname)
624                 label = gtk_label_new(o->name);
625         else
626                 label = gtk_label_new(o->lname);
627
628         b->check = gtk_check_button_new();
629         gopt_mark_index(gjv, &b->gopt, idx, GOPT_BOOL);
630         if (o->def && !strcmp(o->def, "1"))
631                 defstate = 1;
632
633         if (o->neg)
634                 defstate = !defstate;
635
636         if (val)
637                 gopt_bool_set_val(b, *val);
638         else
639                 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(b->check), defstate);
640         b->gopt.sig_handler = g_signal_connect(G_OBJECT(b->check), "toggled", G_CALLBACK(gopt_bool_toggled), b);
641         g_signal_connect(G_OBJECT(b->check), "destroy", G_CALLBACK(gopt_bool_destroy), b);
642
643         gtk_box_pack_start(GTK_BOX(b->gopt.box), b->check, FALSE, FALSE, 0);
644         gtk_box_pack_start(GTK_BOX(b->gopt.box), label, FALSE, FALSE, 0);
645         return &b->gopt;
646 }
647
648 /*
649  * These are paired 0/1 and 2/3. 0/2 are min values, 1/3 are max values.
650  * If the max is made smaller than min, adjust min down.
651  * If the min is made larger than max, adjust the max.
652  */
653 static void range_value_changed(GtkSpinButton *spin, gpointer data)
654 {
655         struct gopt_range *r = (struct gopt_range *) data;
656         int changed = -1, i;
657         gint val, mval;
658
659         gopt_changed(&r->gopt);
660
661         for (i = 0; i < GOPT_RANGE_SPIN; i++) {
662                 if (GTK_SPIN_BUTTON(r->spins[i]) == spin) {
663                         changed = i;
664                         break;
665                 }
666         }
667
668         assert(changed != -1);
669
670         /*
671          * Min changed
672          */
673         if (changed == 0 || changed == 2) {
674                 GtkWidget *mspin = r->spins[changed + 1];
675
676                 val = gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(r->spins[changed]));
677                 mval = gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(mspin));
678                 if (val > mval)
679                         gtk_spin_button_set_value(GTK_SPIN_BUTTON(mspin), val);
680         } else {
681                 GtkWidget *mspin = r->spins[changed - 1];
682
683                 val = gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(r->spins[changed]));
684                 mval = gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(mspin));
685                 if (val < mval)
686                         gtk_spin_button_set_value(GTK_SPIN_BUTTON(mspin), val);
687         }
688 }
689
690 static void gopt_range_destroy(GtkWidget *w, gpointer data)
691 {
692         struct gopt_range *r = (struct gopt_range *) data;
693
694         free(r);
695         gtk_widget_destroy(w);
696 }
697
698 static void gopt_int_range_set_val(struct gopt_range *r, unsigned int *vals)
699 {
700         int i;
701
702         for (i = 0; i < GOPT_RANGE_SPIN; i++)
703                 gtk_spin_button_set_value(GTK_SPIN_BUTTON(r->spins[i]), vals[i]);
704 }
705
706 static struct gopt *gopt_new_int_range(struct gopt_job_view *gjv,
707                                        struct fio_option *o, unsigned int **ip,
708                                        unsigned int idx)
709 {
710         struct gopt_range *r;
711         GtkWidget *label;
712         guint interval;
713         unsigned int defvals[GOPT_RANGE_SPIN];
714         gint maxval;
715         int i;
716
717         r = calloc(1, sizeof(*r));
718         r->gopt.box = gtk_hbox_new(FALSE, 3);
719         gopt_mark_index(gjv, &r->gopt, idx, GOPT_RANGE);
720         if (!o->lname)
721                 label = gtk_label_new(o->name);
722         else
723                 label = gtk_label_new(o->lname);
724
725         maxval = o->maxval;
726         if (!maxval)
727                 maxval = INT_MAX;
728
729         memset(defvals, 0, sizeof(defvals));
730         if (o->def) {
731                 long long val;
732
733                 check_str_bytes(o->def, &val, o);
734                 for (i = 0; i < GOPT_RANGE_SPIN; i++)
735                         defvals[i] = val;
736         }
737
738         interval = 1.0;
739         if (o->interval)
740                 interval = o->interval;
741
742         for (i = 0; i < GOPT_RANGE_SPIN; i++) {
743                 r->spins[i] = gtk_spin_button_new_with_range(o->minval, maxval, interval);
744                 gtk_spin_button_set_update_policy(GTK_SPIN_BUTTON(r->spins[i]), GTK_UPDATE_IF_VALID);
745                 gtk_box_pack_start(GTK_BOX(r->gopt.box), r->spins[i], FALSE, FALSE, 0);
746         }
747
748         if (ip)
749                 gopt_int_range_set_val(r, *ip);
750         else
751                 gopt_int_range_set_val(r, defvals);
752
753         for (i = 0; i < GOPT_RANGE_SPIN; i++)
754                 g_signal_connect(G_OBJECT(r->spins[i]), "value-changed", G_CALLBACK(range_value_changed), r);
755
756         gtk_box_pack_start(GTK_BOX(r->gopt.box), label, FALSE, FALSE, 0);
757         g_signal_connect(G_OBJECT(r->gopt.box), "destroy", G_CALLBACK(gopt_range_destroy), r);
758         return &r->gopt;
759 }
760
761 static void gopt_str_val_destroy(GtkWidget *w, gpointer data)
762 {
763         struct gopt_str_val *g = (struct gopt_str_val *) data;
764
765         free(g);
766         gtk_widget_destroy(w);
767 }
768
769 static void gopt_str_val_spin_wrapped(GtkSpinButton *spin, gpointer data)
770 {
771         struct gopt_str_val *g = (struct gopt_str_val *) data;
772         unsigned int val;
773         GtkAdjustment *adj;
774         gint index;
775
776         adj = gtk_spin_button_get_adjustment(spin);
777         val = gtk_adjustment_get_value(adj);
778
779         /*
780          * Can't rely on exact value, as fast changes increment >= 1
781          */
782         if (!val) {
783                 index = gtk_combo_box_get_active(GTK_COMBO_BOX(g->combo));
784                 if (index + 1 <= g->maxindex) {
785                         val = 1;
786                         gtk_combo_box_set_active(GTK_COMBO_BOX(g->combo), ++index);
787                 } else
788                         val = 1023;
789                 gtk_spin_button_set_value(spin, val);
790         } else {
791                 index = gtk_combo_box_get_active(GTK_COMBO_BOX(g->combo));
792                 if (index) {
793                         gtk_combo_box_set_active(GTK_COMBO_BOX(g->combo), --index);
794                         gtk_spin_button_set_value(spin, 1023);
795                 } else
796                         gtk_spin_button_set_value(spin, 0);
797         }
798 }
799
800 static void gopt_str_val_changed(GtkSpinButton *spin, gpointer data)
801 {
802         struct gopt_str_val *g = (struct gopt_str_val *) data;
803
804         gopt_changed(&g->gopt);
805 }
806
807 static void gopt_str_val_set_val(struct gopt_str_val *g, unsigned long long val)
808 {
809         int i = 0;
810
811         do {
812                 if (!val || (val % 1024))
813                         break;
814
815                 i++;
816                 val /= 1024;
817         } while (1);
818
819         gtk_spin_button_set_value(GTK_SPIN_BUTTON(g->spin), val);
820         gtk_combo_box_set_active(GTK_COMBO_BOX(g->combo), i);
821 }
822
823 static struct gopt *gopt_new_str_val(struct gopt_job_view *gjv,
824                                      struct fio_option *o,
825                                      unsigned long long *p, unsigned int idx)
826 {
827         struct gopt_str_val *g;
828         const gchar *postfix[] = { "B", "KB", "MB", "GB", "PB", "TB", "" };
829         GtkWidget *label;
830         int i;
831
832         g = calloc(1, sizeof(*g));
833         g->gopt.box = gtk_hbox_new(FALSE, 3);
834         if (!o->lname)
835                 label = gtk_label_new(o->name);
836         else
837                 label = gtk_label_new(o->lname);
838         gopt_mark_index(gjv, &g->gopt, idx, GOPT_STR_VAL);
839
840         g->spin = gtk_spin_button_new_with_range(0.0, 1023.0, 1.0);
841         gtk_spin_button_set_update_policy(GTK_SPIN_BUTTON(g->spin), GTK_UPDATE_IF_VALID);
842         gtk_spin_button_set_value(GTK_SPIN_BUTTON(g->spin), 0);
843         gtk_spin_button_set_wrap(GTK_SPIN_BUTTON(g->spin), 1);
844         gtk_box_pack_start(GTK_BOX(g->gopt.box), g->spin, FALSE, FALSE, 0);
845         g_signal_connect(G_OBJECT(g->spin), "wrapped", G_CALLBACK(gopt_str_val_spin_wrapped), g);
846         g_signal_connect(G_OBJECT(g->spin), "changed", G_CALLBACK(gopt_str_val_changed), g);
847
848         g->combo = gtk_combo_box_text_new();
849         i = 0;
850         while (strlen(postfix[i])) {
851                 gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(g->combo), postfix[i]);
852                 i++;
853         }
854         g->maxindex = i - 1;
855         gtk_combo_box_set_active(GTK_COMBO_BOX(g->combo), 0);
856         gtk_box_pack_start(GTK_BOX(g->gopt.box), g->combo, FALSE, FALSE, 0);
857         gtk_box_pack_start(GTK_BOX(g->gopt.box), label, FALSE, FALSE, 3);
858
859         if (p)
860                 gopt_str_val_set_val(g, *p);
861
862         g_signal_connect(G_OBJECT(g->combo), "changed", G_CALLBACK(gopt_str_val_changed), g);
863
864         g_signal_connect(G_OBJECT(g->gopt.box), "destroy", G_CALLBACK(gopt_str_val_destroy), g);
865         return &g->gopt;
866 }
867
868 static void gopt_set_option(struct gopt_job_view *gjv, struct fio_option *o,
869                             struct gopt *gopt, struct thread_options *to)
870 {
871         switch (o->type) {
872         case FIO_OPT_STR_VAL: {
873                 unsigned long long *ullp = NULL;
874                 struct gopt_str_val *g;
875
876                 if (o->off1)
877                         ullp = td_var(to, o->off1);
878
879                 g = container_of(gopt, struct gopt_str_val, gopt);
880                 if (ullp)
881                         gopt_str_val_set_val(g, *ullp);
882                 break;
883                 }
884         case FIO_OPT_STR_VAL_TIME: {
885                 unsigned long long *ullp = NULL;
886                 struct gopt_int *i;
887
888                 if (o->off1) {
889                         if (o->prof_opts)
890                                 ullp = td_var(o->prof_opts, o->off1);
891                         else
892                                 ullp = td_var(to, o->off1);
893                 }
894
895                 i = container_of(gopt, struct gopt_int, gopt);
896                 if (ullp)
897                         gopt_int_set_val(i, *ullp);
898                 break;
899                 }
900         case FIO_OPT_INT:
901                 if (o->posval[0].ival) {
902                         unsigned int *ip = NULL;
903                         struct gopt_combo *c;
904
905                         if (o->off1) {
906                                 if (o->prof_opts)
907                                         ip = td_var(o->prof_opts, o->off1);
908                                 else
909                                         ip = td_var(to, o->off1);
910                         }
911
912                         c = container_of(gopt, struct gopt_combo, gopt);
913                         if (ip)
914                                 gopt_combo_int_set_val(c, *ip);
915                 } else {
916                         unsigned int *ip = NULL;
917                         struct gopt_int *i;
918
919                         if (o->off1) {
920                                 if (o->prof_opts)
921                                         ip = td_var(o->prof_opts, o->off1);
922                                 else
923                                         ip = td_var(to, o->off1);
924                         }
925
926                         i = container_of(gopt, struct gopt_int, gopt);
927                         if (ip)
928                                 gopt_int_set_val(i, *ip);
929                 }
930                 break;
931         case FIO_OPT_STR_SET:
932         case FIO_OPT_BOOL: {
933                 unsigned int *ip = NULL;
934                 struct gopt_bool *b;
935
936                 if (o->off1) {
937                         if (o->prof_opts)
938                                 ip = td_var(o->prof_opts, o->off1);
939                         else
940                                 ip = td_var(to, o->off1);
941                 }
942
943                 b = container_of(gopt, struct gopt_bool, gopt);
944                 if (ip)
945                         gopt_bool_set_val(b, *ip);
946                 break;
947                 }
948         case FIO_OPT_STR: {
949                 if (o->posval[0].ival) {
950                         unsigned int *ip = NULL;
951                         struct gopt_combo *c;
952
953                         if (o->off1) {
954                                 if (o->prof_opts)
955                                         ip = td_var(o->prof_opts, o->off1);
956                                 else
957                                         ip = td_var(to, o->off1);
958                         }
959
960                         c = container_of(gopt, struct gopt_combo, gopt);
961                         if (ip)
962                                 gopt_combo_int_set_val(c, *ip);
963                 } else {
964                         struct gopt_str *s;
965                         char *text = NULL;
966
967                         if (o->off1) {
968                                 char **p;
969
970                                 if (o->prof_opts)
971                                         p = td_var(o->prof_opts, o->off1);
972                                 else
973                                         p = td_var(to, o->off1);
974
975                                 text = *p;
976                         }
977
978                         s = container_of(gopt, struct gopt_str, gopt);
979                         gopt_str_store_set_val(s, text);
980                 }
981
982                 break;
983                 }
984         case FIO_OPT_STR_STORE: {
985                 struct gopt_combo *c;
986                 char *text = NULL;
987
988                 if (o->off1) {
989                         char **p;
990
991                         if (o->prof_opts)
992                                 p = td_var(o->prof_opts, o->off1);
993                         else
994                                 p = td_var(to, o->off1);
995
996                         text = *p;
997                 }
998
999                 if (!o->posval[0].ival) {
1000                         struct gopt_str *s;
1001
1002                         s = container_of(gopt, struct gopt_str, gopt);
1003                         gopt_str_store_set_val(s, text);
1004                         break;
1005                 }
1006
1007                 c = container_of(gopt, struct gopt_combo, gopt);
1008                 if (text)
1009                         gopt_combo_str_set_val(c, text);
1010                 break;
1011                 }
1012         case FIO_OPT_STR_MULTI:
1013                 /* HANDLE ME */
1014                 break;
1015         case FIO_OPT_RANGE: {
1016                 struct gopt_range *r;
1017                 unsigned int *ip[4];
1018
1019                 if (o->prof_opts) {
1020                         ip[0] = td_var(o->prof_opts, o->off1);
1021                         ip[1] = td_var(o->prof_opts, o->off2);
1022                         ip[2] = td_var(o->prof_opts, o->off3);
1023                         ip[3] = td_var(o->prof_opts, o->off4);
1024                 } else {
1025                         ip[0] = td_var(to, o->off1);
1026                         ip[1] = td_var(to, o->off2);
1027                         ip[2] = td_var(to, o->off3);
1028                         ip[3] = td_var(to, o->off4);
1029                 }
1030
1031                 r = container_of(gopt, struct gopt_range, gopt);
1032                 gopt_int_range_set_val(r, *ip);
1033                 break;
1034                 }
1035         /* still need to handle this one */
1036         case FIO_OPT_FLOAT_LIST:
1037                 break;
1038         case FIO_OPT_DEPRECATED:
1039                 break;
1040         default:
1041                 printf("ignore type %u\n", o->type);
1042                 break;
1043         }
1044 }
1045
1046 static void gopt_add_option(struct gopt_job_view *gjv, GtkWidget *hbox,
1047                             struct fio_option *o, unsigned int opt_index,
1048                             struct thread_options *to)
1049 {
1050         struct gopt *go = NULL;
1051
1052         switch (o->type) {
1053         case FIO_OPT_STR_VAL: {
1054                 unsigned long long *ullp = NULL;
1055
1056                 if (o->off1) {
1057                         if (o->prof_opts)
1058                                 ullp = td_var(o->prof_opts, o->off1);
1059                         else
1060                                 ullp = td_var(to, o->off1);
1061                 }
1062
1063                 go = gopt_new_str_val(gjv, o, ullp, opt_index);
1064                 break;
1065                 }
1066         case FIO_OPT_STR_VAL_TIME: {
1067                 unsigned long long *ullp = NULL;
1068
1069                 if (o->off1) {
1070                         if (o->prof_opts)
1071                                 ullp = td_var(o->prof_opts, o->off1);
1072                         else
1073                                 ullp = td_var(to, o->off1);
1074                 }
1075
1076                 go = gopt_new_ullong(gjv, o, ullp, opt_index);
1077                 break;
1078                 }
1079         case FIO_OPT_INT:
1080                 if (o->posval[0].ival) {
1081                         unsigned int *ip = NULL;
1082
1083                         if (o->off1) {
1084                                 if (o->prof_opts)
1085                                         ip = td_var(o->prof_opts, o->off1);
1086                                 else
1087                                         ip = td_var(to, o->off1);
1088                         }
1089
1090                         go = gopt_new_combo_int(gjv, o, ip, opt_index);
1091                 } else {
1092                         unsigned int *ip = NULL;
1093
1094                         if (o->off1) {
1095                                 if (o->prof_opts)
1096                                         ip = td_var(o->prof_opts, o->off1);
1097                                 else
1098                                         ip = td_var(to, o->off1);
1099                         }
1100
1101                         go = gopt_new_int(gjv, o, ip, opt_index);
1102                 }
1103                 break;
1104         case FIO_OPT_STR_SET:
1105         case FIO_OPT_BOOL: {
1106                 unsigned int *ip = NULL;
1107
1108                 if (o->off1) {
1109                         if (o->prof_opts)
1110                                 ip = td_var(o->prof_opts, o->off1);
1111                         else
1112                                 ip = td_var(to, o->off1);
1113                 }
1114
1115                 go = gopt_new_bool(gjv, o, ip, opt_index);
1116                 break;
1117                 }
1118         case FIO_OPT_STR: {
1119                 if (o->posval[0].ival) {
1120                         unsigned int *ip = NULL;
1121
1122                         if (o->off1) {
1123                                 if (o->prof_opts)
1124                                         ip = td_var(o->prof_opts, o->off1);
1125                                 else
1126                                         ip = td_var(to, o->off1);
1127                         }
1128
1129                         go = gopt_new_combo_int(gjv, o, ip, opt_index);
1130                 } else {
1131                         /* TODO: usually ->cb, or unsigned int pointer */
1132                         go = gopt_new_str_store(gjv, o, NULL, opt_index);
1133                 }
1134
1135                 break;
1136                 }
1137         case FIO_OPT_STR_STORE: {
1138                 char *text = NULL;
1139
1140                 if (o->off1) {
1141                         char **p;
1142
1143                         if (o->prof_opts)
1144                                 p = td_var(o->prof_opts, o->off1);
1145                         else
1146                                 p = td_var(to, o->off1);
1147
1148                         text = *p;
1149                 }
1150
1151                 if (!o->posval[0].ival) {
1152                         go = gopt_new_str_store(gjv, o, text, opt_index);
1153                         break;
1154                 }
1155
1156                 go = gopt_new_combo_str(gjv, o, text, opt_index);
1157                 break;
1158                 }
1159         case FIO_OPT_STR_MULTI:
1160                 go = gopt_new_str_multi(gjv, o, opt_index);
1161                 break;
1162         case FIO_OPT_RANGE: {
1163                 unsigned int *ip[4];
1164
1165                 if (o->prof_opts) {
1166                         ip[0] = td_var(o->prof_opts, o->off1);
1167                         ip[1] = td_var(o->prof_opts, o->off2);
1168                         ip[2] = td_var(o->prof_opts, o->off3);
1169                         ip[3] = td_var(o->prof_opts, o->off4);
1170                 } else {
1171                         ip[0] = td_var(to, o->off1);
1172                         ip[1] = td_var(to, o->off2);
1173                         ip[2] = td_var(to, o->off3);
1174                         ip[3] = td_var(to, o->off4);
1175                 }
1176
1177                 go = gopt_new_int_range(gjv, o, ip, opt_index);
1178                 break;
1179                 }
1180         /* still need to handle this one */
1181         case FIO_OPT_FLOAT_LIST:
1182                 break;
1183         case FIO_OPT_DEPRECATED:
1184                 break;
1185         default:
1186                 printf("ignore type %u\n", o->type);
1187                 break;
1188         }
1189
1190         if (go) {
1191                 GtkWidget *dest;
1192
1193                 if (o->help)
1194                         gtk_widget_set_tooltip_text(go->box, o->help);
1195
1196                 o->gui_data = go;
1197
1198                 dest = gopt_get_group_frame(gjv, hbox, o->group);
1199                 if (!dest)
1200                         gtk_box_pack_start(GTK_BOX(hbox), go->box, FALSE, FALSE, 5);
1201                 else
1202                         gtk_box_pack_start(GTK_BOX(dest), go->box, FALSE, FALSE, 5);
1203         }
1204 }
1205
1206 static void gopt_add_options(struct gopt_job_view *gjv,
1207                              struct thread_options *to)
1208 {
1209         GtkWidget *hbox = NULL;
1210         int i;
1211
1212         /*
1213          * First add all options
1214          */
1215         for (i = 0; fio_options[i].name; i++) {
1216                 struct fio_option *o = &fio_options[i];
1217                 unsigned int mask = o->category;
1218                 struct opt_group *og;
1219
1220                 while ((og = opt_group_from_mask(&mask)) != NULL) {
1221                         GtkWidget *vbox = gjv->vboxes[ffz(~og->mask)];
1222
1223                         hbox = gtk_hbox_new(FALSE, 3);
1224                         gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 5);
1225                         gopt_add_option(gjv, hbox, o, i, to);
1226                 }
1227         }
1228 }
1229
1230 static void gopt_set_options(struct gopt_job_view *gjv,
1231                              struct thread_options *to)
1232 {
1233         int i;
1234
1235         for (i = 0; fio_options[i].name; i++) {
1236                 struct fio_option *o = &fio_options[i];
1237                 struct gopt *gopt = gjv->gopts[i];
1238
1239                 gopt_set_option(gjv, o, gopt, to);
1240         }
1241 }
1242
1243 static GtkWidget *gopt_add_tab(GtkWidget *notebook, const char *name)
1244 {
1245         GtkWidget *box, *vbox, *scroll;
1246
1247         scroll = gtk_scrolled_window_new(NULL, NULL);
1248         gtk_container_set_border_width(GTK_CONTAINER(scroll), 5);
1249         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scroll), GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
1250
1251         vbox = gtk_vbox_new(FALSE, 3);
1252         box = gtk_hbox_new(FALSE, 0);
1253         gtk_box_pack_start(GTK_BOX(vbox), box, FALSE, FALSE, 5);
1254         gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(scroll), vbox);
1255         gtk_notebook_append_page(GTK_NOTEBOOK(notebook), scroll, gtk_label_new(name));
1256         return vbox;
1257 }
1258
1259 static GtkWidget *gopt_add_group_tab(GtkWidget *notebook, struct opt_group *og)
1260 {
1261         return gopt_add_tab(notebook, og->name);
1262 }
1263
1264 static void gopt_add_group_tabs(GtkWidget *notebook, struct gopt_job_view *gjv)
1265 {
1266         struct opt_group *og;
1267         unsigned int i;
1268
1269         i = 0;
1270         do {
1271                 unsigned int mask = (1U << i);
1272
1273                 og = opt_group_from_mask(&mask);
1274                 if (!og)
1275                         break;
1276                 gjv->vboxes[i] = gopt_add_group_tab(notebook, og);
1277                 i++;
1278         } while (1);
1279 }
1280
1281 static void gopt_handle_str_multi_changed(struct gopt_job_view *gjv,
1282                                           struct gopt_str_multi *m,
1283                                           struct fio_option *o)
1284 {
1285         struct value_pair *vp;
1286         unsigned int *ip;
1287         gboolean set;
1288         guint val = 0;
1289         int i;
1290
1291         if (o->prof_opts)
1292                 ip = td_var(o->prof_opts, o->off1);
1293         else
1294                 ip = td_var(gjv->o, o->off1);
1295
1296         i = 0;
1297         vp = &o->posval[0];
1298         while (vp->ival) {
1299                 if (!m->checks[i])
1300                         break;
1301                 set = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(m->checks[i]));
1302                 if (set) {
1303                         if (vp->or)
1304                                 val |= vp->oval;
1305                         else
1306                                 val = vp->oval;
1307                 }
1308                 i++;
1309                 vp++;
1310         }
1311
1312         if (o->off1)
1313                 *ip = val;
1314 }
1315
1316 static void gopt_handle_range_changed(struct gopt_job_view *gjv,
1317                                       struct gopt_range *r,
1318                                       struct fio_option *o)
1319 {
1320         unsigned int *ip[4];
1321         gint val;
1322         int i;
1323
1324         if (o->prof_opts) {
1325                 ip[0] = td_var(o->prof_opts, o->off1);
1326                 ip[1] = td_var(o->prof_opts, o->off2);
1327                 ip[2] = td_var(o->prof_opts, o->off3);
1328                 ip[3] = td_var(o->prof_opts, o->off4);
1329         } else {
1330                 ip[0] = td_var(gjv->o, o->off1);
1331                 ip[1] = td_var(gjv->o, o->off2);
1332                 ip[2] = td_var(gjv->o, o->off3);
1333                 ip[3] = td_var(gjv->o, o->off4);
1334         }
1335
1336         for (i = 0; i < GOPT_RANGE_SPIN; i++) {
1337                 val = gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(r->spins[i]));
1338                 *ip[i] = val;
1339         }
1340 }
1341
1342 static void gopt_handle_str_val_changed(struct gopt_job_view *gjv,
1343                                         struct gopt_str_val *s,
1344                                         struct fio_option *o)
1345 {
1346         unsigned long long *ullp;
1347         GtkAdjustment *adj;
1348         gint index;
1349
1350         if (o->prof_opts)
1351                 ullp = td_var(o->prof_opts, o->off1);
1352         else
1353                 ullp = td_var(gjv->o, o->off1);
1354
1355         if (!ullp)
1356                 return;
1357
1358         /*
1359          * Numerical value
1360          */
1361         adj = gtk_spin_button_get_adjustment(GTK_SPIN_BUTTON(s->spin));
1362         *ullp = gtk_adjustment_get_value(adj);
1363
1364         /*
1365          * Multiplier
1366          */
1367         index = gtk_combo_box_get_active(GTK_COMBO_BOX(s->combo));
1368         while (index--)
1369                 *ullp *= 1024ULL;
1370 }
1371
1372 static void gopt_handle_str_changed(struct gopt_job_view *gjv,
1373                                     struct gopt_str *s, struct fio_option *o)
1374 {
1375         char **p;
1376
1377         if (o->prof_opts)
1378                 p = td_var(o->prof_opts, o->off1);
1379         else
1380                 p = td_var(gjv->o, o->off1);
1381
1382         if (*p)
1383                 free(*p);
1384
1385         *p = strdup(gtk_entry_get_text(GTK_ENTRY(s->entry)));
1386 }
1387
1388 static void gopt_handle_bool_changed(struct gopt_job_view *gjv,
1389                                      struct gopt_bool *b, struct fio_option *o)
1390 {
1391         unsigned int *ip;
1392         gboolean set;
1393
1394         if (o->prof_opts)
1395                 ip = td_var(o->prof_opts, o->off1);
1396         else
1397                 ip = td_var(gjv->o, o->off1);
1398
1399         set = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(b->check));
1400         *ip = set;
1401 }
1402
1403 static void gopt_handle_int_changed(struct gopt_job_view *gjv,
1404                                     struct gopt_int *i, struct fio_option *o)
1405 {
1406         GtkAdjustment *adj;
1407         unsigned int *ip;
1408         guint val;
1409
1410         if (o->prof_opts)
1411                 ip = td_var(o->prof_opts, o->off1);
1412         else
1413                 ip = td_var(gjv->o, o->off1);
1414
1415         adj = gtk_spin_button_get_adjustment(GTK_SPIN_BUTTON(i->spin));
1416         val = gtk_adjustment_get_value(adj);
1417         *ip = val;
1418 }
1419
1420 static void gopt_handle_combo_str_changed(struct gopt_job_view *gjv,
1421                                           struct gopt_combo *c,
1422                                           struct fio_option *o)
1423 {
1424         char **p;
1425
1426         if (o->prof_opts)
1427                 p = td_var(o->prof_opts, o->off1);
1428         else
1429                 p = td_var(gjv->o, o->off1);
1430
1431         if (*p)
1432                 free(*p);
1433
1434         *p = strdup(gtk_combo_box_text_get_active_text(GTK_COMBO_BOX_TEXT(c->combo)));
1435 }
1436
1437 static void gopt_handle_combo_int_changed(struct gopt_job_view *gjv,
1438                                           struct gopt_combo *c,
1439                                           struct fio_option *o)
1440 {
1441         unsigned int *ip;
1442         gint index;
1443
1444         if (o->prof_opts)
1445                 ip = td_var(o->prof_opts, o->off1);
1446         else
1447                 ip = td_var(gjv->o, o->off1);
1448
1449         index = gtk_combo_box_get_active(GTK_COMBO_BOX(c->combo));
1450         *ip = o->posval[index].oval;
1451 }
1452
1453 static void gopt_handle_changed(struct gopt *gopt)
1454 {
1455         struct fio_option *o = &fio_options[gopt->opt_index];
1456         struct gopt_job_view *gjv = gopt->gjv;
1457
1458         switch (gopt->opt_type) {
1459         case GOPT_COMBO_INT: {
1460                 struct gopt_combo *c;
1461
1462                 c = container_of(gopt, struct gopt_combo, gopt);
1463                 gopt_handle_combo_int_changed(gjv, c, o);
1464                 break;
1465                 }
1466         case GOPT_COMBO_STR: {
1467                 struct gopt_combo *c;
1468
1469                 c = container_of(gopt, struct gopt_combo, gopt);
1470                 gopt_handle_combo_str_changed(gjv, c, o);
1471                 break;
1472                 }
1473         case GOPT_INT: {
1474                 struct gopt_int *i;
1475
1476                 i = container_of(gopt, struct gopt_int, gopt);
1477                 gopt_handle_int_changed(gjv, i, o);
1478                 break;
1479                 }
1480         case GOPT_BOOL: {
1481                 struct gopt_bool *b;
1482
1483                 b = container_of(gopt, struct gopt_bool, gopt);
1484                 gopt_handle_bool_changed(gjv, b, o);
1485                 break;
1486                 }
1487         case GOPT_STR: {
1488                 struct gopt_str *s;
1489
1490                 s = container_of(gopt, struct gopt_str, gopt);
1491                 gopt_handle_str_changed(gjv, s, o);
1492                 break;
1493                 }
1494         case GOPT_STR_VAL: {
1495                 struct gopt_str_val *s;
1496
1497                 s = container_of(gopt, struct gopt_str_val, gopt);
1498                 gopt_handle_str_val_changed(gjv, s, o);
1499                 break;
1500                 }
1501         case GOPT_RANGE: {
1502                 struct gopt_range *r;
1503
1504                 r = container_of(gopt, struct gopt_range, gopt);
1505                 gopt_handle_range_changed(gjv, r, o);
1506                 break;
1507                 }
1508         case GOPT_STR_MULTI: {
1509                 struct gopt_str_multi *m;
1510
1511                 m = container_of(gopt, struct gopt_str_multi, gopt);
1512                 gopt_handle_str_multi_changed(gjv, m, o);
1513                 break;
1514                 }
1515         default:
1516                 log_err("gfio: bad option type: %d\n", gopt->opt_type);
1517                 break;
1518         }
1519 }
1520
1521 static void gopt_report_update_status(struct gopt_job_view *gjv)
1522 {
1523         struct gfio_client *gc = gjv->client;
1524         char tmp[80];
1525
1526         sprintf(tmp, "\nCompleted with error: %d\n", gc->update_job_status);
1527         gfio_report_info(gc->ge->ui, "Update job", tmp);
1528 }
1529
1530 static int gopt_handle_changed_options(struct gopt_job_view *gjv)
1531 {
1532         struct gfio_client *gc = gjv->client;
1533         struct flist_head *entry;
1534         uint64_t waitid = 0;
1535         struct gopt *gopt;
1536         int ret;
1537
1538         flist_for_each(entry, &gjv->changed_list) {
1539                 gopt = flist_entry(entry, struct gopt, changed_list);
1540                 gopt_handle_changed(gopt);
1541         }
1542
1543         gc->update_job_status = 0;
1544         gc->update_job_done = 0;
1545
1546         ret = fio_client_update_options(gc->client, gjv->o, &waitid);
1547         if (ret)
1548                 goto done;
1549
1550         ret = fio_client_wait_for_reply(gc->client, waitid);
1551         if (ret)
1552                 goto done;
1553
1554         assert(gc->update_job_done);
1555         if (gc->update_job_status)
1556                 goto done;
1557
1558         while (!flist_empty(&gjv->changed_list)) {
1559                 gopt = flist_entry(gjv->changed_list.next, struct gopt, changed_list);
1560                 flist_del_init(&gopt->changed_list);
1561         }
1562
1563 done:
1564         gopt_dialog_update_apply_button(gjv);
1565         return ret;
1566 }
1567
1568 static gint gopt_dialog_cancel(gint response)
1569 {
1570         switch (response) {
1571         case GTK_RESPONSE_NONE:
1572         case GTK_RESPONSE_REJECT:
1573         case GTK_RESPONSE_DELETE_EVENT:
1574         case GTK_RESPONSE_CANCEL:
1575         case GTK_RESPONSE_NO:
1576                 return 1;
1577         default:
1578                 return 0;
1579         }
1580 }
1581
1582 static gint gopt_dialog_done(gint response)
1583 {
1584         switch (response) {
1585         case GTK_RESPONSE_ACCEPT:
1586         case GTK_RESPONSE_OK:
1587         case GTK_RESPONSE_YES:
1588                 return 1;
1589         default:
1590                 return 0;
1591         }
1592 }
1593
1594 static void gopt_handle_option_dialog(struct gopt_job_view *gjv)
1595 {
1596         gint response;
1597
1598         do {
1599                 response = gtk_dialog_run(GTK_DIALOG(gjv->dialog));
1600
1601                 if (gopt_dialog_cancel(response) ||
1602                     gopt_dialog_done(response))
1603                         break;
1604
1605                 /*
1606                  * Apply
1607                  */
1608                 gopt_handle_changed_options(gjv);
1609                 gopt_report_update_status(gjv);
1610         } while (1);
1611
1612         if (gopt_dialog_cancel(response))
1613                 return;
1614
1615         gopt_handle_changed_options(gjv);
1616 }
1617
1618 static void gopt_job_changed(GtkComboBox *box, gpointer data)
1619 {
1620         struct gopt_job_view *gjv = (struct gopt_job_view *) data;
1621         struct gfio_client_options *gco = NULL;
1622         struct gfio_client *gc = gjv->client;
1623         struct flist_head *entry;
1624         gchar *job;
1625
1626         /*
1627          * The switch act should be sensitized appropriately, so that we
1628          * never get here with modified options.
1629          */
1630         if (!flist_empty(&gjv->changed_list)) {
1631                 gfio_report_info(gc->ge->ui, "Internal Error", "Modified options on job switch.\nThat should not be possible!\n");
1632                 return;
1633         }
1634
1635         job = gtk_combo_box_text_get_active_text(GTK_COMBO_BOX_TEXT(gjv->job_combo));
1636         flist_for_each(entry, &gc->o_list) {
1637                 const char *name;
1638
1639                 gco = flist_entry(entry, struct gfio_client_options, list);
1640                 name = gco->o.name;
1641                 if (!name || !strlen(name))
1642                         name = "Default job";
1643
1644                 if (!strcmp(name, job))
1645                         break;
1646
1647                 gco = NULL;
1648         }
1649
1650         if (!gco) {
1651                 gfio_report_info(gc->ge->ui, "Internal Error", "Could not find job description.\nThat should not be possible!\n");
1652                 return;
1653         }
1654
1655         gjv->in_job_switch = 1;
1656         gopt_set_options(gjv, &gco->o);
1657         gjv->in_job_switch = 0;
1658 }
1659
1660 void gopt_get_options_window(GtkWidget *window, struct gfio_client *gc)
1661 {
1662         GtkWidget *dialog, *notebook, *vbox, *topvbox, *combo;
1663         struct gfio_client_options *gco;
1664         struct flist_head *entry;
1665         struct gopt_job_view *gjv;
1666
1667         dialog = gtk_dialog_new_with_buttons("Fio options",
1668                         GTK_WINDOW(window), GTK_DIALOG_DESTROY_WITH_PARENT,
1669                         GTK_STOCK_OK, GTK_RESPONSE_ACCEPT,
1670                         GTK_STOCK_APPLY, GTK_RESPONSE_APPLY,
1671                         GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT, NULL);
1672
1673         combo = gtk_combo_box_text_new();
1674         flist_for_each(entry, &gc->o_list) {
1675                 struct thread_options *o;
1676                 const char *name;
1677
1678                 gco = flist_entry(entry, struct gfio_client_options, list);
1679                 o = &gco->o;
1680                 name = o->name;
1681                 if (!name || !strlen(name))
1682                         name = "Default job";
1683
1684                 gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(combo), name);
1685         }
1686         gtk_combo_box_set_active(GTK_COMBO_BOX(combo), 0);
1687
1688         gtk_widget_set_size_request(GTK_WIDGET(dialog), 1024, 768);
1689
1690         topvbox = gtk_dialog_get_content_area(GTK_DIALOG(dialog));
1691         gtk_box_pack_start(GTK_BOX(topvbox), combo, FALSE, FALSE, 5);
1692
1693         vbox = gtk_vbox_new(TRUE, 5);
1694         gtk_box_pack_start(GTK_BOX(topvbox), vbox, TRUE, TRUE, 5);
1695
1696         notebook = gtk_notebook_new();
1697         gtk_notebook_set_scrollable(GTK_NOTEBOOK(notebook), 1);
1698         gtk_notebook_popup_enable(GTK_NOTEBOOK(notebook));
1699         gtk_box_pack_start(GTK_BOX(vbox), notebook, TRUE, TRUE, 5);
1700
1701         gjv = calloc(1, sizeof(*gjv));
1702         INIT_FLIST_HEAD(&gjv->changed_list);
1703         gco = flist_entry(gc->o_list.next, struct gfio_client_options, list);
1704         gjv->o = &gco->o;
1705         gjv->dialog = dialog;
1706         gjv->client = gc;
1707         gjv->job_combo = combo;
1708         gopt_add_group_tabs(notebook, gjv);
1709         gopt_add_options(gjv, &gco->o);
1710         gopt_dialog_update_apply_button(gjv);
1711
1712         g_signal_connect(G_OBJECT(combo), "changed", G_CALLBACK(gopt_job_changed), gjv);
1713
1714         gtk_widget_show_all(dialog);
1715
1716         gopt_handle_option_dialog(gjv);
1717
1718         gtk_widget_destroy(dialog);
1719         free(gjv);
1720 }
1721
1722 /*
1723  * Build n-ary option dependency tree
1724  */
1725 void gopt_init(void)
1726 {
1727         int i;
1728
1729         gopt_dep_tree = g_node_new(NULL);
1730
1731         for (i = 0; fio_options[i].name; i++) {
1732                 struct fio_option *o = &fio_options[i];
1733                 GNode *node, *nparent;
1734
1735                 /*
1736                  * Insert node with either the root parent, or an
1737                  * option parent.
1738                  */
1739                 node = g_node_new(o);
1740                 nparent = gopt_dep_tree;
1741                 if (o->parent) {
1742                         struct fio_option *parent;
1743
1744                         parent = fio_option_find(o->parent);
1745                         nparent = g_node_find(gopt_dep_tree, G_IN_ORDER, G_TRAVERSE_ALL, parent);
1746                         if (!nparent) {
1747                                 log_err("fio: did not find parent %s for opt %s\n", o->name, o->parent);
1748                                 nparent = gopt_dep_tree;
1749                         }
1750                 }
1751
1752                 g_node_insert(nparent, -1, node);
1753         }
1754 }
1755
1756 void gopt_exit(void)
1757 {
1758         g_node_destroy(gopt_dep_tree);
1759         gopt_dep_tree = NULL;
1760 }