goptions: block gtk signal handlers instead of hacking around them
[fio.git] / goptions.c
index 6452ef244e3968ef06c737dcdea25b027a1d6e38..94d60d6c6f1be80561406a5df8edad5ac36915dd 100644 (file)
@@ -15,6 +15,7 @@ struct gopt {
        GtkWidget *box;
        unsigned int opt_index;
        unsigned int opt_type;
+       gulong sig_handler;
 };
 
 struct gopt_combo {
@@ -24,6 +25,7 @@ struct gopt_combo {
 
 struct gopt_int {
        struct gopt gopt;
+       unsigned int lastval;
        GtkWidget *spin;
 };
 
@@ -141,6 +143,7 @@ static struct gopt *gopt_new_str_store(struct fio_option *o, const char *text, u
        GtkWidget *label;
 
        s = malloc(sizeof(*s));
+       memset(s, 0, sizeof(*s));
 
        s->gopt.box = gtk_hbox_new(FALSE, 3);
        if (!o->lname)
@@ -153,13 +156,14 @@ 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);
-       g_signal_connect(GTK_OBJECT(s->entry), "changed", G_CALLBACK(gopt_str_changed), s);
+       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);
 
        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;
        return &s->gopt;
 }
 
@@ -177,6 +181,7 @@ static struct gopt_combo *__gopt_new_combo(struct fio_option *o, unsigned int id
        GtkWidget *label;
 
        c = malloc(sizeof(*c));
+       memset(c, 0, sizeof(*c));
 
        c->gopt.box = gtk_hbox_new(FALSE, 3);
        if (!o->lname)
@@ -186,11 +191,12 @@ 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);
-       g_signal_connect(GTK_OBJECT(c->combo), "changed", G_CALLBACK(gopt_combo_changed), c);
+       c->gopt.sig_handler = g_signal_connect(GTK_OBJECT(c->combo), "changed", G_CALLBACK(gopt_combo_changed), 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);
 
+       o->gui_data = c;
        return c;
 }
 
@@ -248,6 +254,7 @@ static struct gopt *gopt_new_str_multi(struct fio_option *o, unsigned int idx)
        int i;
 
        m = malloc(sizeof(*m));
+       memset(m, 0, sizeof(*m));
        m->gopt.box = gtk_hbox_new(FALSE, 3);
        gopt_mark_index(&m->gopt, idx);
 
@@ -276,8 +283,26 @@ static void gopt_int_changed(GtkSpinButton *spin, gpointer data)
 {
        struct gopt_int *i = (struct gopt_int *) data;
        struct fio_option *o = &fio_options[i->gopt.opt_index];
+       GtkAdjustment *adj;
+       int value, delta;
 
-       printf("int %s changed\n", o->name);
+       adj = gtk_spin_button_get_adjustment(spin);
+       value = gtk_adjustment_get_value(adj);
+       delta = value - i->lastval;
+       i->lastval = value;
+
+       if (o->inv_opt) {
+               struct gopt_int *i_inv = o->inv_opt->gui_data;
+               int cur_val;
+
+               assert(o->type == o->inv_opt->type);
+
+               cur_val = gtk_spin_button_get_value(GTK_SPIN_BUTTON(i_inv->spin));
+               cur_val -= delta;
+               g_signal_handler_block(G_OBJECT(i_inv->spin), i_inv->gopt.sig_handler);
+               gtk_spin_button_set_value(GTK_SPIN_BUTTON(i_inv->spin), cur_val);
+               g_signal_handler_unblock(G_OBJECT(i_inv->spin), i_inv->gopt.sig_handler);
+       }
 }
 
 static struct gopt_int *__gopt_new_int(struct fio_option *o, unsigned long long *p,
@@ -289,6 +314,7 @@ static struct gopt_int *__gopt_new_int(struct fio_option *o, unsigned long long
        GtkWidget *label;
 
        i = malloc(sizeof(*i));
+       memset(i, 0, sizeof(*i));
        i->gopt.box = gtk_hbox_new(FALSE, 3);
        if (!o->lname)
                label = gtk_label_new(o->name);
@@ -317,11 +343,13 @@ static struct gopt_int *__gopt_new_int(struct fio_option *o, unsigned long long
        gopt_mark_index(&i->gopt, idx);
        gtk_spin_button_set_update_policy(GTK_SPIN_BUTTON(i->spin), GTK_UPDATE_IF_VALID);
        gtk_spin_button_set_value(GTK_SPIN_BUTTON(i->spin), defval);
-       g_signal_connect(G_OBJECT(i->spin), "value-changed", G_CALLBACK(gopt_int_changed), i);
+       i->lastval = defval;
+       i->gopt.sig_handler = g_signal_connect(G_OBJECT(i->spin), "value-changed", G_CALLBACK(gopt_int_changed), 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);
 
+       o->gui_data = i;
        return i;
 }
 
@@ -355,6 +383,17 @@ static void gopt_bool_toggled(GtkToggleButton *button, gpointer data)
        gboolean set;
 
        set = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(b->check));
+
+       if (o->inv_opt) {
+               struct gopt_bool *b_inv = o->inv_opt->gui_data;
+
+               assert(o->type == o->inv_opt->type);
+
+               g_signal_handler_block(G_OBJECT(b_inv->check), b_inv->gopt.sig_handler);
+               gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(b_inv->check), !set);
+               g_signal_handler_unblock(G_OBJECT(b_inv->check), b_inv->gopt.sig_handler);
+       }
+
        gopt_set_children_visible(o, set);
 }
 
@@ -365,6 +404,7 @@ static struct gopt *gopt_new_bool(struct fio_option *o, unsigned int *val, unsig
        int defstate = 0;
 
        b = malloc(sizeof(*b));
+       memset(b, 0, sizeof(*b));
        b->gopt.box = gtk_hbox_new(FALSE, 3);
        if (!o->lname)
                label = gtk_label_new(o->name);
@@ -378,11 +418,15 @@ static struct gopt *gopt_new_bool(struct fio_option *o, unsigned int *val, unsig
        else if (o->def && !strcmp(o->def, "1"))
                defstate = 1;
 
+       if (o->neg)
+               defstate = !defstate;
+
        gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(b->check), defstate);
-       g_signal_connect(G_OBJECT(b->check), "toggled", G_CALLBACK(gopt_bool_toggled), b);
+       b->gopt.sig_handler = g_signal_connect(G_OBJECT(b->check), "toggled", G_CALLBACK(gopt_bool_toggled), 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);
+       o->gui_data = b;
        return &b->gopt;
 }
 
@@ -436,6 +480,7 @@ static struct gopt *gopt_new_int_range(struct fio_option *o, unsigned int **ip,
        int i;
 
        r = malloc(sizeof(*r));
+       memset(r, 0, sizeof(*r));
        r->gopt.box = gtk_hbox_new(FALSE, 3);
        gopt_mark_index(&r->gopt, idx);
        if (!o->lname)
@@ -472,6 +517,7 @@ 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);
+       o->gui_data = r;
        return &r->gopt;
 }
 
@@ -511,12 +557,18 @@ static void gopt_add_option(GtkWidget *hbox, struct fio_option *o,
                break;
                }
        case FIO_OPT_STR: {
-               unsigned int *ip = NULL;
+               if (o->posval[0].ival) {
+                       unsigned int *ip = NULL;
 
-               if (o->off1)
-                       ip = td_var(to, o->off1);
+                       if (o->off1)
+                               ip = td_var(to, o->off1);
+
+                       go = gopt_new_combo_int(o, ip, opt_index);
+               } else {
+                       /* TODO: usually ->cb, or unsigned int pointer */
+                       go = gopt_new_str_store(o, NULL, opt_index);
+               }
 
-               go = gopt_new_combo_int(o, ip, opt_index);
                break;
                }
        case FIO_OPT_STR_STORE: {
@@ -578,6 +630,9 @@ static void gopt_add_options(GtkWidget **vboxes, struct thread_options *to)
        GtkWidget *hbox = NULL;
        int i;
 
+       /*
+        * First add all options
+        */
        for (i = 0; fio_options[i].name; i++) {
                struct fio_option *o = &fio_options[i];
                unsigned int mask = o->category;