goptions: don't emit change signals during setup
[fio.git] / goptions.c
index 94d60d6c6f1be80561406a5df8edad5ac36915dd..f7ba3038fafe1e7a9007394542f6d75160ed8278 100644 (file)
@@ -39,6 +39,13 @@ struct gopt_str {
        GtkWidget *entry;
 };
 
+struct gopt_str_val {
+       struct gopt gopt;
+       GtkWidget *spin;
+       GtkWidget *combo;
+       unsigned int maxindex;
+};
+
 #define GOPT_RANGE_SPIN        4
 
 struct gopt_range {
@@ -95,7 +102,8 @@ static GtkWidget *gopt_get_group_frame(GtkWidget *box, unsigned int groupmask)
 /*
  * Mark children as invisible, if needed.
  */
-static void gopt_set_children_visible(struct fio_option *parent, gboolean visible)
+static void gopt_set_children_visible(struct fio_option *parent,
+                                     gboolean visible)
 {
        struct fio_option *o;
        int i;
@@ -137,7 +145,16 @@ static void gopt_mark_index(struct gopt *gopt, unsigned int idx)
        gopt_widgets[idx] = gopt->box;
 }
 
-static struct gopt *gopt_new_str_store(struct fio_option *o, const char *text, unsigned int idx)
+static void gopt_str_destroy(GtkWidget *w, gpointer data)
+{
+       struct gopt_str *s = (struct gopt_str *) data;
+
+       free(s);
+       gtk_widget_destroy(w);
+}
+
+static struct gopt *gopt_new_str_store(struct fio_option *o, const char *text,
+                                      unsigned int idx)
 {
        struct gopt_str *s;
        GtkWidget *label;
@@ -156,11 +173,13 @@ static struct gopt *gopt_new_str_store(struct fio_option *o, const char *text, u
        if (text)
                gtk_entry_set_text(GTK_ENTRY(s->entry), text);
        gtk_entry_set_editable(GTK_ENTRY(s->entry), 1);
-       s->gopt.sig_handler = g_signal_connect(GTK_OBJECT(s->entry), "changed", G_CALLBACK(gopt_str_changed), s);
 
        if (o->def)
                gtk_entry_set_text(GTK_ENTRY(s->entry), o->def);
 
+       s->gopt.sig_handler = g_signal_connect(GTK_OBJECT(s->entry), "changed", G_CALLBACK(gopt_str_changed), s);
+       g_signal_connect(GTK_OBJECT(s->entry), "destroy", G_CALLBACK(gopt_str_destroy), s);
+
        gtk_box_pack_start(GTK_BOX(s->gopt.box), s->entry, FALSE, FALSE, 0);
        gtk_box_pack_start(GTK_BOX(s->gopt.box), label, FALSE, FALSE, 0);
        o->gui_data = s;
@@ -175,7 +194,16 @@ static void gopt_combo_changed(GtkComboBox *box, gpointer data)
        printf("combo %s changed\n", o->name);
 }
 
-static struct gopt_combo *__gopt_new_combo(struct fio_option *o, unsigned int idx)
+static void gopt_combo_destroy(GtkWidget *w, gpointer data)
+{
+       struct gopt_combo *c = (struct gopt_combo *) data;
+
+       free(c);
+       gtk_widget_destroy(w);
+}
+
+static struct gopt_combo *__gopt_new_combo(struct fio_option *o,
+                                          unsigned int idx)
 {
        struct gopt_combo *c;
        GtkWidget *label;
@@ -191,7 +219,7 @@ static struct gopt_combo *__gopt_new_combo(struct fio_option *o, unsigned int id
 
        c->combo = gtk_combo_box_new_text();
        gopt_mark_index(&c->gopt, idx);
-       c->gopt.sig_handler = g_signal_connect(GTK_OBJECT(c->combo), "changed", G_CALLBACK(gopt_combo_changed), c);
+       g_signal_connect(GTK_OBJECT(c->combo), "destroy", G_CALLBACK(gopt_combo_destroy), c);
 
        gtk_box_pack_start(GTK_BOX(c->gopt.box), c->combo, FALSE, FALSE, 0);
        gtk_box_pack_start(GTK_BOX(c->gopt.box), label, FALSE, FALSE, 0);
@@ -200,18 +228,19 @@ static struct gopt_combo *__gopt_new_combo(struct fio_option *o, unsigned int id
        return c;
 }
 
-static struct gopt *gopt_new_combo_str(struct fio_option *o, const char *text, unsigned int idx)
+static struct gopt *gopt_new_combo_str(struct fio_option *o, const char *text,
+                                      unsigned int idx)
 {
-       struct gopt_combo *combo;
+       struct gopt_combo *c;
        struct value_pair *vp;
        int i, active = 0;
 
-       combo = __gopt_new_combo(o, idx);
+       c = __gopt_new_combo(o, idx);
 
        i = 0;
        vp = &o->posval[0];
        while (vp->ival) {
-               gtk_combo_box_append_text(GTK_COMBO_BOX(combo->combo), vp->ival);
+               gtk_combo_box_append_text(GTK_COMBO_BOX(c->combo), vp->ival);
                if (o->def && !strcmp(vp->ival, o->def))
                        active = i;
                if (text && !strcmp(vp->ival, text))
@@ -220,30 +249,33 @@ static struct gopt *gopt_new_combo_str(struct fio_option *o, const char *text, u
                i++;
        }
 
-       gtk_combo_box_set_active(GTK_COMBO_BOX(combo->combo), active);
-       return &combo->gopt;
+       gtk_combo_box_set_active(GTK_COMBO_BOX(c->combo), active);
+       c->gopt.sig_handler = g_signal_connect(GTK_OBJECT(c->combo), "changed", G_CALLBACK(gopt_combo_changed), c);
+       return &c->gopt;
 }
 
-static struct gopt *gopt_new_combo_int(struct fio_option *o, unsigned int *ip, unsigned int idx)
+static struct gopt *gopt_new_combo_int(struct fio_option *o, unsigned int *ip,
+                                      unsigned int idx)
 {
-       struct gopt_combo *combo;
+       struct gopt_combo *c;
        struct value_pair *vp;
        int i, active = 0;
 
-       combo = __gopt_new_combo(o, idx);
+       c = __gopt_new_combo(o, idx);
 
        i = 0;
        vp = &o->posval[0];
        while (vp->ival) {
-               gtk_combo_box_append_text(GTK_COMBO_BOX(combo->combo), vp->ival);
+               gtk_combo_box_append_text(GTK_COMBO_BOX(c->combo), vp->ival);
                if (ip && vp->oval == *ip)
                        active = i;
                vp++;
                i++;
        }
 
-       gtk_combo_box_set_active(GTK_COMBO_BOX(combo->combo), active);
-       return &combo->gopt;
+       gtk_combo_box_set_active(GTK_COMBO_BOX(c->combo), active);
+       c->gopt.sig_handler = g_signal_connect(GTK_OBJECT(c->combo), "changed", G_CALLBACK(gopt_combo_changed), c);
+       return &c->gopt;
 }
 
 static struct gopt *gopt_new_str_multi(struct fio_option *o, unsigned int idx)
@@ -305,8 +337,16 @@ static void gopt_int_changed(GtkSpinButton *spin, gpointer data)
        }
 }
 
-static struct gopt_int *__gopt_new_int(struct fio_option *o, unsigned long long *p,
-                                      unsigned int idx)
+static void gopt_int_destroy(GtkWidget *w, gpointer data)
+{
+       struct gopt_int *i = (struct gopt_int *) data;
+
+       free(i);
+       gtk_widget_destroy(w);
+}
+
+static struct gopt_int *__gopt_new_int(struct fio_option *o,
+                                      unsigned long long *p, unsigned int idx)
 {
        unsigned long long defval;
        struct gopt_int *i;
@@ -345,6 +385,7 @@ static struct gopt_int *__gopt_new_int(struct fio_option *o, unsigned long long
        gtk_spin_button_set_value(GTK_SPIN_BUTTON(i->spin), defval);
        i->lastval = defval;
        i->gopt.sig_handler = g_signal_connect(G_OBJECT(i->spin), "value-changed", G_CALLBACK(gopt_int_changed), i);
+       g_signal_connect(G_OBJECT(i->spin), "destroy", G_CALLBACK(gopt_int_destroy), i);
 
        gtk_box_pack_start(GTK_BOX(i->gopt.box), i->spin, FALSE, FALSE, 0);
        gtk_box_pack_start(GTK_BOX(i->gopt.box), label, FALSE, FALSE, 0);
@@ -353,7 +394,8 @@ static struct gopt_int *__gopt_new_int(struct fio_option *o, unsigned long long
        return i;
 }
 
-static struct gopt *gopt_new_int(struct fio_option *o, unsigned int *ip, unsigned int idx)
+static struct gopt *gopt_new_int(struct fio_option *o, unsigned int *ip,
+                                unsigned int idx)
 {
        unsigned long long ullp;
        struct gopt_int *i;
@@ -397,7 +439,16 @@ static void gopt_bool_toggled(GtkToggleButton *button, gpointer data)
        gopt_set_children_visible(o, set);
 }
 
-static struct gopt *gopt_new_bool(struct fio_option *o, unsigned int *val, unsigned int idx)
+static void gopt_bool_destroy(GtkWidget *w, gpointer data)
+{
+       struct gopt_bool *b = (struct gopt_bool *) data;
+
+       free(b);
+       gtk_widget_destroy(w);
+}
+
+static struct gopt *gopt_new_bool(struct fio_option *o, unsigned int *val,
+                                 unsigned int idx)
 {
        struct gopt_bool *b;
        GtkWidget *label;
@@ -423,6 +474,7 @@ static struct gopt *gopt_new_bool(struct fio_option *o, unsigned int *val, unsig
 
        gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(b->check), defstate);
        b->gopt.sig_handler = g_signal_connect(G_OBJECT(b->check), "toggled", G_CALLBACK(gopt_bool_toggled), b);
+       g_signal_connect(G_OBJECT(b->check), "destroy", G_CALLBACK(gopt_bool_destroy), b);
 
        gtk_box_pack_start(GTK_BOX(b->gopt.box), b->check, FALSE, FALSE, 0);
        gtk_box_pack_start(GTK_BOX(b->gopt.box), label, FALSE, FALSE, 0);
@@ -470,6 +522,14 @@ static void range_value_changed(GtkSpinButton *spin, gpointer data)
        }
 }
 
+static void gopt_range_destroy(GtkWidget *w, gpointer data)
+{
+       struct gopt_range *r = (struct gopt_range *) data;
+
+       free(r);
+       gtk_widget_destroy(w);
+}
+
 static struct gopt *gopt_new_int_range(struct fio_option *o, unsigned int **ip,
                                       unsigned int idx)
 {
@@ -517,17 +577,104 @@ static struct gopt *gopt_new_int_range(struct fio_option *o, unsigned int **ip,
        }
 
        gtk_box_pack_start(GTK_BOX(r->gopt.box), label, FALSE, FALSE, 0);
+       g_signal_connect(G_OBJECT(r->gopt.box), "destroy", G_CALLBACK(gopt_range_destroy), r);
        o->gui_data = r;
        return &r->gopt;
 }
 
+static void gopt_str_val_destroy(GtkWidget *w, gpointer data)
+{
+       struct gopt_str_val *g = (struct gopt_str_val *) data;
+
+       free(g);
+       gtk_widget_destroy(w);
+}
+
+static void gopt_str_val_spin_wrapped(GtkSpinButton *spin, gpointer data)
+{
+       struct gopt_str_val *g = (struct gopt_str_val *) data;
+       unsigned int val;
+       GtkAdjustment *adj;
+       gint index;
+
+       adj = gtk_spin_button_get_adjustment(spin);
+       val = gtk_adjustment_get_value(adj);
+
+       /*
+        * Can't rely on exact value, as fast changes increment >= 1
+        */
+       if (!val) {
+               index = gtk_combo_box_get_active(GTK_COMBO_BOX(g->combo));
+               if (index + 1 <= g->maxindex) {
+                       val = 1;
+                       gtk_combo_box_set_active(GTK_COMBO_BOX(g->combo), ++index);
+               } else
+                       val = 1023;
+               gtk_spin_button_set_value(spin, val);
+       } else {
+               index = gtk_combo_box_get_active(GTK_COMBO_BOX(g->combo));
+               if (index) {
+                       gtk_combo_box_set_active(GTK_COMBO_BOX(g->combo), --index);
+                       gtk_spin_button_set_value(spin, 1023);
+               } else
+                       gtk_spin_button_set_value(spin, 0);
+       }
+}
+
+static struct gopt *gopt_new_str_val(struct fio_option *o,
+                                    unsigned long long *p, unsigned int idx)
+{
+       struct gopt_str_val *g;
+       const gchar *postfix[] = { "B", "KB", "MB", "GB", "PB", "TB", "" };
+       GtkWidget *label;
+       int i;
+
+       g = malloc(sizeof(*g));
+       memset(g, 0, sizeof(*g));
+       g->gopt.box = gtk_hbox_new(FALSE, 3);
+       if (!o->lname)
+               label = gtk_label_new(o->name);
+       else
+               label = gtk_label_new(o->lname);
+
+       g->spin = gtk_spin_button_new_with_range(0.0, 1023.0, 1.0);
+       gtk_spin_button_set_update_policy(GTK_SPIN_BUTTON(g->spin), GTK_UPDATE_IF_VALID);
+       gtk_spin_button_set_value(GTK_SPIN_BUTTON(g->spin), 0);
+       gtk_spin_button_set_wrap(GTK_SPIN_BUTTON(g->spin), 1);
+       gtk_box_pack_start(GTK_BOX(g->gopt.box), g->spin, FALSE, FALSE, 0);
+       g_signal_connect(G_OBJECT(g->spin), "wrapped", G_CALLBACK(gopt_str_val_spin_wrapped), g);
+
+       g->combo = gtk_combo_box_new_text();
+       i = 0;
+       while (strlen(postfix[i])) {
+               gtk_combo_box_append_text(GTK_COMBO_BOX(g->combo), postfix[i]);
+               i++;
+       }
+       g->maxindex = i - 1;
+       gtk_combo_box_set_active(GTK_COMBO_BOX(g->combo), 0);
+       gtk_box_pack_start(GTK_BOX(g->gopt.box), g->combo, FALSE, FALSE, 0);
+       gtk_box_pack_start(GTK_BOX(g->gopt.box), label, FALSE, FALSE, 3);
+
+       g_signal_connect(G_OBJECT(g->gopt.box), "destroy", G_CALLBACK(gopt_str_val_destroy), g);
+       o->gui_data = g;
+       return &g->gopt;
+}
+
 static void gopt_add_option(GtkWidget *hbox, struct fio_option *o,
                            unsigned int opt_index, struct thread_options *to)
 {
        struct gopt *go = NULL;
 
        switch (o->type) {
-       case FIO_OPT_STR_VAL:
+       case FIO_OPT_STR_VAL: {
+               unsigned long long *ullp = NULL;
+
+               if (o->off1)
+                       ullp = td_var(to, o->off1);
+
+               go = gopt_new_str_val(o, ullp, opt_index);
+               break;
+               }
        case FIO_OPT_STR_VAL_TIME: {
                unsigned long long *ullp = NULL;
 
@@ -708,4 +855,6 @@ void gopt_get_options_window(GtkWidget *window, struct thread_options *o)
        gtk_dialog_run(GTK_DIALOG(dialog));
 
        gtk_widget_destroy(dialog);
+       memset(gopt_widgets, 0, sizeof(gopt_widgets));
+       memset(gopt_g_widgets, 0, sizeof(gopt_g_widgets));
 }