blob: f1148bf44884578948db36adf6fc26d31b9a882e [file] [log] [blame]
Jens Axboe9af4a242012-03-16 10:13:49 +01001#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
14struct gopt {
15 GtkWidget *box;
16 unsigned int opt_index;
17 unsigned int opt_type;
Jens Axboe8f9e46a2012-03-19 22:01:20 +010018 gulong sig_handler;
Jens Axboe9af4a242012-03-16 10:13:49 +010019};
20
21struct gopt_combo {
22 struct gopt gopt;
23 GtkWidget *combo;
24};
25
26struct gopt_int {
27 struct gopt gopt;
Jens Axboe90265352012-03-19 20:29:44 +010028 unsigned int lastval;
Jens Axboe9af4a242012-03-16 10:13:49 +010029 GtkWidget *spin;
30};
31
32struct gopt_bool {
33 struct gopt gopt;
34 GtkWidget *check;
35};
36
37struct gopt_str {
38 struct gopt gopt;
39 GtkWidget *entry;
40};
41
Jens Axboe7fc35ff2012-03-20 09:32:35 +010042struct gopt_str_val {
43 struct gopt gopt;
44 GtkWidget *spin;
45 GtkWidget *combo;
Jens Axboe481c2422012-03-20 09:49:49 +010046 unsigned int maxindex;
Jens Axboe7fc35ff2012-03-20 09:32:35 +010047};
48
Jens Axboe1da10c12012-03-16 19:41:01 +010049#define GOPT_RANGE_SPIN 4
50
Jens Axboe9af4a242012-03-16 10:13:49 +010051struct gopt_range {
52 struct gopt gopt;
Jens Axboe1da10c12012-03-16 19:41:01 +010053 GtkWidget *spins[GOPT_RANGE_SPIN];
Jens Axboe9af4a242012-03-16 10:13:49 +010054};
55
Jens Axboe7386d4a2012-03-19 18:36:29 +010056struct gopt_str_multi {
57 struct gopt gopt;
58 GtkWidget *checks[PARSE_MAX_VP];
59};
60
Jens Axboee8b0e952012-03-19 14:37:08 +010061static GtkWidget *gopt_widgets[FIO_MAX_OPTS];
62
63struct gopt_frame_widget {
64 GtkWidget *vbox[2];
65 unsigned int nr;
Jens Axboeec0218f2012-03-16 21:45:05 +010066};
Jens Axboee8b0e952012-03-19 14:37:08 +010067static struct gopt_frame_widget gopt_g_widgets[__FIO_OPT_G_NR];
Jens Axboeec0218f2012-03-16 21:45:05 +010068
Jens Axboee8b0e952012-03-19 14:37:08 +010069static GtkWidget *gopt_get_group_frame(GtkWidget *box, unsigned int groupmask)
Jens Axboeec0218f2012-03-16 21:45:05 +010070{
Jens Axboee8b0e952012-03-19 14:37:08 +010071 unsigned int mask, group;
72 struct opt_group *og;
73 GtkWidget *frame, *hbox;
74 struct gopt_frame_widget *gfw;
Jens Axboeec0218f2012-03-16 21:45:05 +010075
Jens Axboee8b0e952012-03-19 14:37:08 +010076 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);
Jens Axboeec0218f2012-03-16 21:45:05 +010095 }
Jens Axboee8b0e952012-03-19 14:37:08 +010096
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;
Jens Axboeec0218f2012-03-16 21:45:05 +0100100}
Jens Axboeb6caa832012-03-16 21:22:56 +0100101
102/*
103 * Mark children as invisible, if needed.
104 */
Jens Axboe231edf62012-03-20 08:31:29 +0100105static void gopt_set_children_visible(struct fio_option *parent,
106 gboolean visible)
Jens Axboeb6caa832012-03-16 21:22:56 +0100107{
108 struct fio_option *o;
109 int i;
110
111 /*
112 * This isn't super fast, but it should not be an issue. If it is, we
113 * can speed it up by caching the lookup at least. Or we can do it
114 * once, at init time.
115 */
116 for (i = 0; fio_options[i].name; i++) {
117 o = &fio_options[i];
Jens Axboeec0218f2012-03-16 21:45:05 +0100118 if (!o->parent || !o->hide)
Jens Axboeb6caa832012-03-16 21:22:56 +0100119 continue;
120
121 if (strcmp(parent->name, o->parent))
122 continue;
123
Jens Axboee8b0e952012-03-19 14:37:08 +0100124 if (gopt_widgets[i])
125 gtk_widget_set_sensitive(gopt_widgets[i], visible);
Jens Axboeb6caa832012-03-16 21:22:56 +0100126 }
127}
128
129static void gopt_str_changed(GtkEntry *entry, gpointer data)
130{
131 struct gopt_str *s = (struct gopt_str *) data;
132 struct fio_option *o = &fio_options[s->gopt.opt_index];
133 const gchar *text;
134 int set;
135
136 text = gtk_entry_get_text(GTK_ENTRY(s->entry));
137 set = strcmp(text, "") != 0;
138 gopt_set_children_visible(o, set);
139}
140
Jens Axboeec0218f2012-03-16 21:45:05 +0100141static void gopt_mark_index(struct gopt *gopt, unsigned int idx)
Jens Axboeb6caa832012-03-16 21:22:56 +0100142{
Jens Axboee8b0e952012-03-19 14:37:08 +0100143 assert(!gopt_widgets[idx]);
Jens Axboeb6caa832012-03-16 21:22:56 +0100144 gopt->opt_index = idx;
Jens Axboee8b0e952012-03-19 14:37:08 +0100145 gopt_widgets[idx] = gopt->box;
Jens Axboeb6caa832012-03-16 21:22:56 +0100146}
147
Jens Axboe231edf62012-03-20 08:31:29 +0100148static void gopt_str_destroy(GtkWidget *w, gpointer data)
149{
150 struct gopt_str *s = (struct gopt_str *) data;
151
152 free(s);
153 gtk_widget_destroy(w);
154}
155
156static struct gopt *gopt_new_str_store(struct fio_option *o, const char *text,
157 unsigned int idx)
Jens Axboe9af4a242012-03-16 10:13:49 +0100158{
159 struct gopt_str *s;
160 GtkWidget *label;
161
162 s = malloc(sizeof(*s));
Jens Axboea01a1bc2012-03-19 21:13:01 +0100163 memset(s, 0, sizeof(*s));
Jens Axboe9af4a242012-03-16 10:13:49 +0100164
165 s->gopt.box = gtk_hbox_new(FALSE, 3);
Jens Axboee8b0e952012-03-19 14:37:08 +0100166 if (!o->lname)
167 label = gtk_label_new(o->name);
168 else
169 label = gtk_label_new(o->lname);
Jens Axboe9af4a242012-03-16 10:13:49 +0100170
171 s->entry = gtk_entry_new();
Jens Axboeec0218f2012-03-16 21:45:05 +0100172 gopt_mark_index(&s->gopt, idx);
Jens Axboe789f4cc2012-03-16 14:56:44 +0100173 if (text)
174 gtk_entry_set_text(GTK_ENTRY(s->entry), text);
Jens Axboe9af4a242012-03-16 10:13:49 +0100175 gtk_entry_set_editable(GTK_ENTRY(s->entry), 1);
Jens Axboe8f9e46a2012-03-19 22:01:20 +0100176 s->gopt.sig_handler = g_signal_connect(GTK_OBJECT(s->entry), "changed", G_CALLBACK(gopt_str_changed), s);
Jens Axboe231edf62012-03-20 08:31:29 +0100177 g_signal_connect(GTK_OBJECT(s->entry), "destroy", G_CALLBACK(gopt_str_destroy), s);
Jens Axboe9af4a242012-03-16 10:13:49 +0100178
179 if (o->def)
180 gtk_entry_set_text(GTK_ENTRY(s->entry), o->def);
181
182 gtk_box_pack_start(GTK_BOX(s->gopt.box), s->entry, FALSE, FALSE, 0);
Jens Axboee8b0e952012-03-19 14:37:08 +0100183 gtk_box_pack_start(GTK_BOX(s->gopt.box), label, FALSE, FALSE, 0);
Jens Axboe90265352012-03-19 20:29:44 +0100184 o->gui_data = s;
Jens Axboe9af4a242012-03-16 10:13:49 +0100185 return &s->gopt;
186}
187
Jens Axboeb6caa832012-03-16 21:22:56 +0100188static void gopt_combo_changed(GtkComboBox *box, gpointer data)
Jens Axboe9af4a242012-03-16 10:13:49 +0100189{
Jens Axboeb6caa832012-03-16 21:22:56 +0100190 struct gopt_combo *c = (struct gopt_combo *) data;
191 struct fio_option *o = &fio_options[c->gopt.opt_index];
Jens Axboe9af4a242012-03-16 10:13:49 +0100192
Jens Axboeb6caa832012-03-16 21:22:56 +0100193 printf("combo %s changed\n", o->name);
Jens Axboe39f04332012-03-16 16:34:06 +0100194}
195
Jens Axboe231edf62012-03-20 08:31:29 +0100196static void gopt_combo_destroy(GtkWidget *w, gpointer data)
197{
198 struct gopt_combo *c = (struct gopt_combo *) data;
199
200 free(c);
201 gtk_widget_destroy(w);
202}
203
204static struct gopt_combo *__gopt_new_combo(struct fio_option *o,
205 unsigned int idx)
Jens Axboeb6caa832012-03-16 21:22:56 +0100206{
207 struct gopt_combo *c;
208 GtkWidget *label;
209
210 c = malloc(sizeof(*c));
Jens Axboea01a1bc2012-03-19 21:13:01 +0100211 memset(c, 0, sizeof(*c));
Jens Axboeb6caa832012-03-16 21:22:56 +0100212
213 c->gopt.box = gtk_hbox_new(FALSE, 3);
Jens Axboee8b0e952012-03-19 14:37:08 +0100214 if (!o->lname)
215 label = gtk_label_new(o->name);
216 else
217 label = gtk_label_new(o->lname);
Jens Axboeb6caa832012-03-16 21:22:56 +0100218
219 c->combo = gtk_combo_box_new_text();
Jens Axboeec0218f2012-03-16 21:45:05 +0100220 gopt_mark_index(&c->gopt, idx);
Jens Axboe8f9e46a2012-03-19 22:01:20 +0100221 c->gopt.sig_handler = g_signal_connect(GTK_OBJECT(c->combo), "changed", G_CALLBACK(gopt_combo_changed), c);
Jens Axboe231edf62012-03-20 08:31:29 +0100222 g_signal_connect(GTK_OBJECT(c->combo), "destroy", G_CALLBACK(gopt_combo_destroy), c);
Jens Axboeb6caa832012-03-16 21:22:56 +0100223
Jens Axboee8b0e952012-03-19 14:37:08 +0100224 gtk_box_pack_start(GTK_BOX(c->gopt.box), c->combo, FALSE, FALSE, 0);
225 gtk_box_pack_start(GTK_BOX(c->gopt.box), label, FALSE, FALSE, 0);
226
Jens Axboe90265352012-03-19 20:29:44 +0100227 o->gui_data = c;
Jens Axboeb6caa832012-03-16 21:22:56 +0100228 return c;
229}
230
Jens Axboe231edf62012-03-20 08:31:29 +0100231static struct gopt *gopt_new_combo_str(struct fio_option *o, const char *text,
232 unsigned int idx)
Jens Axboe39f04332012-03-16 16:34:06 +0100233{
234 struct gopt_combo *combo;
235 struct value_pair *vp;
236 int i, active = 0;
237
Jens Axboeb6caa832012-03-16 21:22:56 +0100238 combo = __gopt_new_combo(o, idx);
Jens Axboe39f04332012-03-16 16:34:06 +0100239
Jens Axboe9af4a242012-03-16 10:13:49 +0100240 i = 0;
241 vp = &o->posval[0];
242 while (vp->ival) {
243 gtk_combo_box_append_text(GTK_COMBO_BOX(combo->combo), vp->ival);
244 if (o->def && !strcmp(vp->ival, o->def))
245 active = i;
Jens Axboe39f04332012-03-16 16:34:06 +0100246 if (text && !strcmp(vp->ival, text))
247 active = i;
Jens Axboe9af4a242012-03-16 10:13:49 +0100248 vp++;
249 i++;
250 }
251
252 gtk_combo_box_set_active(GTK_COMBO_BOX(combo->combo), active);
253 return &combo->gopt;
254}
255
Jens Axboe231edf62012-03-20 08:31:29 +0100256static struct gopt *gopt_new_combo_int(struct fio_option *o, unsigned int *ip,
257 unsigned int idx)
Jens Axboe9af4a242012-03-16 10:13:49 +0100258{
Jens Axboe39f04332012-03-16 16:34:06 +0100259 struct gopt_combo *combo;
260 struct value_pair *vp;
261 int i, active = 0;
262
Jens Axboeb6caa832012-03-16 21:22:56 +0100263 combo = __gopt_new_combo(o, idx);
Jens Axboe39f04332012-03-16 16:34:06 +0100264
265 i = 0;
266 vp = &o->posval[0];
267 while (vp->ival) {
268 gtk_combo_box_append_text(GTK_COMBO_BOX(combo->combo), vp->ival);
269 if (ip && vp->oval == *ip)
270 active = i;
271 vp++;
272 i++;
273 }
274
275 gtk_combo_box_set_active(GTK_COMBO_BOX(combo->combo), active);
276 return &combo->gopt;
277}
278
Jens Axboe7386d4a2012-03-19 18:36:29 +0100279static struct gopt *gopt_new_str_multi(struct fio_option *o, unsigned int idx)
280{
281 struct gopt_str_multi *m;
282 struct value_pair *vp;
283 GtkWidget *frame, *hbox;
284 int i;
285
286 m = malloc(sizeof(*m));
Jens Axboea01a1bc2012-03-19 21:13:01 +0100287 memset(m, 0, sizeof(*m));
Jens Axboe7386d4a2012-03-19 18:36:29 +0100288 m->gopt.box = gtk_hbox_new(FALSE, 3);
289 gopt_mark_index(&m->gopt, idx);
290
291 if (!o->lname)
292 frame = gtk_frame_new(o->name);
293 else
294 frame = gtk_frame_new(o->lname);
295 gtk_box_pack_start(GTK_BOX(m->gopt.box), frame, FALSE, FALSE, 3);
296
297 hbox = gtk_hbox_new(FALSE, 3);
298 gtk_container_add(GTK_CONTAINER(frame), hbox);
299
300 i = 0;
301 vp = &o->posval[0];
302 while (vp->ival) {
303 m->checks[i] = gtk_check_button_new_with_label(vp->ival);
304 gtk_widget_set_tooltip_text(m->checks[i], vp->help);
305 gtk_box_pack_start(GTK_BOX(hbox), m->checks[i], FALSE, FALSE, 3);
306 vp++;
307 }
308
309 return &m->gopt;
310}
311
Jens Axboeb6caa832012-03-16 21:22:56 +0100312static void gopt_int_changed(GtkSpinButton *spin, gpointer data)
313{
314 struct gopt_int *i = (struct gopt_int *) data;
315 struct fio_option *o = &fio_options[i->gopt.opt_index];
Jens Axboe90265352012-03-19 20:29:44 +0100316 GtkAdjustment *adj;
317 int value, delta;
Jens Axboeb6caa832012-03-16 21:22:56 +0100318
Jens Axboe90265352012-03-19 20:29:44 +0100319 adj = gtk_spin_button_get_adjustment(spin);
320 value = gtk_adjustment_get_value(adj);
321 delta = value - i->lastval;
322 i->lastval = value;
323
324 if (o->inv_opt) {
325 struct gopt_int *i_inv = o->inv_opt->gui_data;
326 int cur_val;
327
Jens Axboea01a1bc2012-03-19 21:13:01 +0100328 assert(o->type == o->inv_opt->type);
329
Jens Axboe90265352012-03-19 20:29:44 +0100330 cur_val = gtk_spin_button_get_value(GTK_SPIN_BUTTON(i_inv->spin));
331 cur_val -= delta;
Jens Axboe8f9e46a2012-03-19 22:01:20 +0100332 g_signal_handler_block(G_OBJECT(i_inv->spin), i_inv->gopt.sig_handler);
Jens Axboe90265352012-03-19 20:29:44 +0100333 gtk_spin_button_set_value(GTK_SPIN_BUTTON(i_inv->spin), cur_val);
Jens Axboe8f9e46a2012-03-19 22:01:20 +0100334 g_signal_handler_unblock(G_OBJECT(i_inv->spin), i_inv->gopt.sig_handler);
Jens Axboe90265352012-03-19 20:29:44 +0100335 }
Jens Axboeb6caa832012-03-16 21:22:56 +0100336}
337
Jens Axboe231edf62012-03-20 08:31:29 +0100338static void gopt_int_destroy(GtkWidget *w, gpointer data)
339{
340 struct gopt_int *i = (struct gopt_int *) data;
341
342 free(i);
343 gtk_widget_destroy(w);
344}
345
346static struct gopt_int *__gopt_new_int(struct fio_option *o,
347 unsigned long long *p, unsigned int idx)
Jens Axboe39f04332012-03-16 16:34:06 +0100348{
349 unsigned long long defval;
Jens Axboe9af4a242012-03-16 10:13:49 +0100350 struct gopt_int *i;
Jens Axboe20eb06b2012-03-16 19:57:23 +0100351 guint maxval, interval;
Jens Axboe9af4a242012-03-16 10:13:49 +0100352 GtkWidget *label;
353
354 i = malloc(sizeof(*i));
Jens Axboea01a1bc2012-03-19 21:13:01 +0100355 memset(i, 0, sizeof(*i));
Jens Axboe9af4a242012-03-16 10:13:49 +0100356 i->gopt.box = gtk_hbox_new(FALSE, 3);
Jens Axboee8b0e952012-03-19 14:37:08 +0100357 if (!o->lname)
358 label = gtk_label_new(o->name);
359 else
360 label = gtk_label_new(o->lname);
Jens Axboe9af4a242012-03-16 10:13:49 +0100361
362 maxval = o->maxval;
363 if (!maxval)
Jens Axboe39f04332012-03-16 16:34:06 +0100364 maxval = UINT_MAX;
Jens Axboe9af4a242012-03-16 10:13:49 +0100365
366 defval = 0;
Jens Axboe39f04332012-03-16 16:34:06 +0100367 if (p)
368 defval = *p;
369 else if (o->def) {
Jens Axboe9af4a242012-03-16 10:13:49 +0100370 long long val;
371
372 check_str_bytes(o->def, &val, NULL);
373 defval = val;
374 }
375
Jens Axboe20eb06b2012-03-16 19:57:23 +0100376 interval = 1.0;
377 if (o->interval)
378 interval = o->interval;
379
380 i->spin = gtk_spin_button_new_with_range(o->minval, maxval, interval);
Jens Axboeec0218f2012-03-16 21:45:05 +0100381 gopt_mark_index(&i->gopt, idx);
Jens Axboe9af4a242012-03-16 10:13:49 +0100382 gtk_spin_button_set_update_policy(GTK_SPIN_BUTTON(i->spin), GTK_UPDATE_IF_VALID);
383 gtk_spin_button_set_value(GTK_SPIN_BUTTON(i->spin), defval);
Jens Axboe90265352012-03-19 20:29:44 +0100384 i->lastval = defval;
Jens Axboe8f9e46a2012-03-19 22:01:20 +0100385 i->gopt.sig_handler = g_signal_connect(G_OBJECT(i->spin), "value-changed", G_CALLBACK(gopt_int_changed), i);
Jens Axboe231edf62012-03-20 08:31:29 +0100386 g_signal_connect(G_OBJECT(i->spin), "destroy", G_CALLBACK(gopt_int_destroy), i);
Jens Axboe9af4a242012-03-16 10:13:49 +0100387
388 gtk_box_pack_start(GTK_BOX(i->gopt.box), i->spin, FALSE, FALSE, 0);
Jens Axboee8b0e952012-03-19 14:37:08 +0100389 gtk_box_pack_start(GTK_BOX(i->gopt.box), label, FALSE, FALSE, 0);
Jens Axboeb6caa832012-03-16 21:22:56 +0100390
Jens Axboe90265352012-03-19 20:29:44 +0100391 o->gui_data = i;
Jens Axboeb6caa832012-03-16 21:22:56 +0100392 return i;
Jens Axboe9af4a242012-03-16 10:13:49 +0100393}
394
Jens Axboe231edf62012-03-20 08:31:29 +0100395static struct gopt *gopt_new_int(struct fio_option *o, unsigned int *ip,
396 unsigned int idx)
Jens Axboe39f04332012-03-16 16:34:06 +0100397{
398 unsigned long long ullp;
Jens Axboeb6caa832012-03-16 21:22:56 +0100399 struct gopt_int *i;
Jens Axboe39f04332012-03-16 16:34:06 +0100400
401 if (ip) {
402 ullp = *ip;
Jens Axboeb6caa832012-03-16 21:22:56 +0100403 i = __gopt_new_int(o, &ullp, idx);
404 } else
405 i = __gopt_new_int(o, NULL, idx);
Jens Axboe39f04332012-03-16 16:34:06 +0100406
Jens Axboeb6caa832012-03-16 21:22:56 +0100407 return &i->gopt;
Jens Axboe39f04332012-03-16 16:34:06 +0100408}
409
Jens Axboeb6caa832012-03-16 21:22:56 +0100410static struct gopt *gopt_new_ullong(struct fio_option *o, unsigned long long *p,
411 unsigned int idx)
Jens Axboe39f04332012-03-16 16:34:06 +0100412{
Jens Axboeb6caa832012-03-16 21:22:56 +0100413 struct gopt_int *i;
414
415 i = __gopt_new_int(o, p, idx);
416 return &i->gopt;
Jens Axboe39f04332012-03-16 16:34:06 +0100417}
418
Jens Axboeb6caa832012-03-16 21:22:56 +0100419static void gopt_bool_toggled(GtkToggleButton *button, gpointer data)
420{
421 struct gopt_bool *b = (struct gopt_bool *) data;
422 struct fio_option *o = &fio_options[b->gopt.opt_index];
423 gboolean set;
424
425 set = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(b->check));
Jens Axboea01a1bc2012-03-19 21:13:01 +0100426
427 if (o->inv_opt) {
428 struct gopt_bool *b_inv = o->inv_opt->gui_data;
429
430 assert(o->type == o->inv_opt->type);
431
Jens Axboe8f9e46a2012-03-19 22:01:20 +0100432 g_signal_handler_block(G_OBJECT(b_inv->check), b_inv->gopt.sig_handler);
Jens Axboea01a1bc2012-03-19 21:13:01 +0100433 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(b_inv->check), !set);
Jens Axboe8f9e46a2012-03-19 22:01:20 +0100434 g_signal_handler_unblock(G_OBJECT(b_inv->check), b_inv->gopt.sig_handler);
Jens Axboea01a1bc2012-03-19 21:13:01 +0100435 }
436
Jens Axboeb6caa832012-03-16 21:22:56 +0100437 gopt_set_children_visible(o, set);
438}
439
Jens Axboe231edf62012-03-20 08:31:29 +0100440static void gopt_bool_destroy(GtkWidget *w, gpointer data)
441{
442 struct gopt_bool *b = (struct gopt_bool *) data;
443
444 free(b);
445 gtk_widget_destroy(w);
446}
447
448static struct gopt *gopt_new_bool(struct fio_option *o, unsigned int *val,
449 unsigned int idx)
Jens Axboe9af4a242012-03-16 10:13:49 +0100450{
451 struct gopt_bool *b;
452 GtkWidget *label;
453 int defstate = 0;
454
455 b = malloc(sizeof(*b));
Jens Axboea01a1bc2012-03-19 21:13:01 +0100456 memset(b, 0, sizeof(*b));
Jens Axboe9af4a242012-03-16 10:13:49 +0100457 b->gopt.box = gtk_hbox_new(FALSE, 3);
Jens Axboee8b0e952012-03-19 14:37:08 +0100458 if (!o->lname)
459 label = gtk_label_new(o->name);
460 else
461 label = gtk_label_new(o->lname);
Jens Axboe9af4a242012-03-16 10:13:49 +0100462
463 b->check = gtk_check_button_new();
Jens Axboeec0218f2012-03-16 21:45:05 +0100464 gopt_mark_index(&b->gopt, idx);
Jens Axboe39f04332012-03-16 16:34:06 +0100465 if (val)
466 defstate = *val;
467 else if (o->def && !strcmp(o->def, "1"))
Jens Axboe9af4a242012-03-16 10:13:49 +0100468 defstate = 1;
469
Jens Axboed872fb42012-03-19 19:17:49 +0100470 if (o->neg)
471 defstate = !defstate;
472
Jens Axboe9af4a242012-03-16 10:13:49 +0100473 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(b->check), defstate);
Jens Axboe8f9e46a2012-03-19 22:01:20 +0100474 b->gopt.sig_handler = g_signal_connect(G_OBJECT(b->check), "toggled", G_CALLBACK(gopt_bool_toggled), b);
Jens Axboe231edf62012-03-20 08:31:29 +0100475 g_signal_connect(G_OBJECT(b->check), "destroy", G_CALLBACK(gopt_bool_destroy), b);
Jens Axboe9af4a242012-03-16 10:13:49 +0100476
477 gtk_box_pack_start(GTK_BOX(b->gopt.box), b->check, FALSE, FALSE, 0);
Jens Axboee8b0e952012-03-19 14:37:08 +0100478 gtk_box_pack_start(GTK_BOX(b->gopt.box), label, FALSE, FALSE, 0);
Jens Axboe90265352012-03-19 20:29:44 +0100479 o->gui_data = b;
Jens Axboe9af4a242012-03-16 10:13:49 +0100480 return &b->gopt;
481}
482
Jens Axboe1da10c12012-03-16 19:41:01 +0100483/*
484 * These are paired 0/1 and 2/3. 0/2 are min values, 1/3 are max values.
485 * If the max is made smaller than min, adjust min down.
486 * If the min is made larger than max, adjust the max.
487 */
488static void range_value_changed(GtkSpinButton *spin, gpointer data)
489{
490 struct gopt_range *r = (struct gopt_range *) data;
491 int changed = -1, i;
492 gint val, mval;
493
494 for (i = 0; i < GOPT_RANGE_SPIN; i++) {
495 if (GTK_SPIN_BUTTON(r->spins[i]) == spin) {
496 changed = i;
497 break;
498 }
499 }
500
501 assert(changed != -1);
502
503 /*
504 * Min changed
505 */
506 if (changed == 0 || changed == 2) {
507 GtkWidget *mspin = r->spins[changed + 1];
508
509 val = gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(r->spins[changed]));
510 mval = gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(mspin));
511 if (val > mval)
512 gtk_spin_button_set_value(GTK_SPIN_BUTTON(mspin), val);
513 } else {
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 }
521}
522
Jens Axboe231edf62012-03-20 08:31:29 +0100523static void gopt_range_destroy(GtkWidget *w, gpointer data)
524{
525 struct gopt_range *r = (struct gopt_range *) data;
526
527 free(r);
528 gtk_widget_destroy(w);
529}
530
Jens Axboeb6caa832012-03-16 21:22:56 +0100531static struct gopt *gopt_new_int_range(struct fio_option *o, unsigned int **ip,
532 unsigned int idx)
Jens Axboe9af4a242012-03-16 10:13:49 +0100533{
534 struct gopt_range *r;
535 gint maxval, defval;
536 GtkWidget *label;
Jens Axboe20eb06b2012-03-16 19:57:23 +0100537 guint interval;
Jens Axboe9af4a242012-03-16 10:13:49 +0100538 int i;
539
540 r = malloc(sizeof(*r));
Jens Axboea01a1bc2012-03-19 21:13:01 +0100541 memset(r, 0, sizeof(*r));
Jens Axboe9af4a242012-03-16 10:13:49 +0100542 r->gopt.box = gtk_hbox_new(FALSE, 3);
Jens Axboeec0218f2012-03-16 21:45:05 +0100543 gopt_mark_index(&r->gopt, idx);
Jens Axboee8b0e952012-03-19 14:37:08 +0100544 if (!o->lname)
545 label = gtk_label_new(o->name);
546 else
547 label = gtk_label_new(o->lname);
Jens Axboe9af4a242012-03-16 10:13:49 +0100548
549 maxval = o->maxval;
550 if (!maxval)
551 maxval = INT_MAX;
552
553 defval = 0;
554 if (o->def) {
555 long long val;
556
557 check_str_bytes(o->def, &val, NULL);
558 defval = val;
559 }
560
Jens Axboe20eb06b2012-03-16 19:57:23 +0100561 interval = 1.0;
562 if (o->interval)
563 interval = o->interval;
564
Jens Axboe1da10c12012-03-16 19:41:01 +0100565 for (i = 0; i < GOPT_RANGE_SPIN; i++) {
Jens Axboe20eb06b2012-03-16 19:57:23 +0100566 r->spins[i] = gtk_spin_button_new_with_range(o->minval, maxval, interval);
Jens Axboe9af4a242012-03-16 10:13:49 +0100567 gtk_spin_button_set_update_policy(GTK_SPIN_BUTTON(r->spins[i]), GTK_UPDATE_IF_VALID);
Jens Axboe39f04332012-03-16 16:34:06 +0100568 if (ip)
569 gtk_spin_button_set_value(GTK_SPIN_BUTTON(r->spins[i]), *ip[i]);
570 else
571 gtk_spin_button_set_value(GTK_SPIN_BUTTON(r->spins[i]), defval);
Jens Axboe9af4a242012-03-16 10:13:49 +0100572
573 gtk_box_pack_start(GTK_BOX(r->gopt.box), r->spins[i], FALSE, FALSE, 0);
Jens Axboe1da10c12012-03-16 19:41:01 +0100574 g_signal_connect(G_OBJECT(r->spins[i]), "value-changed", G_CALLBACK(range_value_changed), r);
Jens Axboe9af4a242012-03-16 10:13:49 +0100575 }
576
Jens Axboee8b0e952012-03-19 14:37:08 +0100577 gtk_box_pack_start(GTK_BOX(r->gopt.box), label, FALSE, FALSE, 0);
Jens Axboe231edf62012-03-20 08:31:29 +0100578 g_signal_connect(G_OBJECT(r->gopt.box), "destroy", G_CALLBACK(gopt_range_destroy), r);
Jens Axboe90265352012-03-19 20:29:44 +0100579 o->gui_data = r;
Jens Axboe9af4a242012-03-16 10:13:49 +0100580 return &r->gopt;
581}
582
Jens Axboe7fc35ff2012-03-20 09:32:35 +0100583static void gopt_str_val_destroy(GtkWidget *w, gpointer data)
584{
585 struct gopt_str_val *g = (struct gopt_str_val *) data;
586
587 free(g);
588 gtk_widget_destroy(w);
589}
590
591static void gopt_str_val_spin_wrapped(GtkSpinButton *spin, gpointer data)
592{
593 struct gopt_str_val *g = (struct gopt_str_val *) data;
594 unsigned int val;
595 GtkAdjustment *adj;
596 gint index;
597
598 adj = gtk_spin_button_get_adjustment(spin);
599 val = gtk_adjustment_get_value(adj);
600
601 /*
602 * Can't rely on exact value, as fast changes increment >= 1
603 */
604 if (!val) {
Jens Axboe7fc35ff2012-03-20 09:32:35 +0100605 index = gtk_combo_box_get_active(GTK_COMBO_BOX(g->combo));
Jens Axboe481c2422012-03-20 09:49:49 +0100606 if (index + 1 <= g->maxindex) {
607 val = 1;
608 gtk_combo_box_set_active(GTK_COMBO_BOX(g->combo), ++index);
609 } else
610 val = 1023;
Jens Axboe7fc35ff2012-03-20 09:32:35 +0100611 gtk_spin_button_set_value(spin, val);
612 } else {
613 index = gtk_combo_box_get_active(GTK_COMBO_BOX(g->combo));
614 if (index) {
615 gtk_combo_box_set_active(GTK_COMBO_BOX(g->combo), --index);
616 gtk_spin_button_set_value(spin, 1023);
617 } else
618 gtk_spin_button_set_value(spin, 0);
619 }
620}
621
622static struct gopt *gopt_new_str_val(struct fio_option *o,
623 unsigned long long *p, unsigned int idx)
624{
625 struct gopt_str_val *g;
626 const gchar *postfix[] = { "B", "KB", "MB", "GB", "PB", "TB", "" };
627 GtkWidget *label;
628 int i;
629
630 g = malloc(sizeof(*g));
631 memset(g, 0, sizeof(*g));
632 g->gopt.box = gtk_hbox_new(FALSE, 3);
633 if (!o->lname)
634 label = gtk_label_new(o->name);
635 else
636 label = gtk_label_new(o->lname);
637
638 g->spin = gtk_spin_button_new_with_range(0.0, 1023.0, 1.0);
639 gtk_spin_button_set_update_policy(GTK_SPIN_BUTTON(g->spin), GTK_UPDATE_IF_VALID);
640 gtk_spin_button_set_value(GTK_SPIN_BUTTON(g->spin), 0);
641 gtk_spin_button_set_wrap(GTK_SPIN_BUTTON(g->spin), 1);
642 gtk_box_pack_start(GTK_BOX(g->gopt.box), g->spin, FALSE, FALSE, 0);
643 g_signal_connect(G_OBJECT(g->spin), "wrapped", G_CALLBACK(gopt_str_val_spin_wrapped), g);
644
645 g->combo = gtk_combo_box_new_text();
646 i = 0;
647 while (strlen(postfix[i])) {
648 gtk_combo_box_append_text(GTK_COMBO_BOX(g->combo), postfix[i]);
649 i++;
650 }
Jens Axboe481c2422012-03-20 09:49:49 +0100651 g->maxindex = i - 1;
Jens Axboe7fc35ff2012-03-20 09:32:35 +0100652 gtk_combo_box_set_active(GTK_COMBO_BOX(g->combo), 0);
653 gtk_box_pack_start(GTK_BOX(g->gopt.box), g->combo, FALSE, FALSE, 0);
654 gtk_box_pack_start(GTK_BOX(g->gopt.box), label, FALSE, FALSE, 3);
655
656 g_signal_connect(G_OBJECT(g->gopt.box), "destroy", G_CALLBACK(gopt_str_val_destroy), g);
657 o->gui_data = g;
658 return &g->gopt;
659}
660
Jens Axboe9af4a242012-03-16 10:13:49 +0100661static void gopt_add_option(GtkWidget *hbox, struct fio_option *o,
Jens Axboe789f4cc2012-03-16 14:56:44 +0100662 unsigned int opt_index, struct thread_options *to)
Jens Axboe9af4a242012-03-16 10:13:49 +0100663{
664 struct gopt *go = NULL;
665
666 switch (o->type) {
Jens Axboe7fc35ff2012-03-20 09:32:35 +0100667 case FIO_OPT_STR_VAL: {
668 unsigned long long *ullp = NULL;
669
670 if (o->off1)
671 ullp = td_var(to, o->off1);
672
673 go = gopt_new_str_val(o, ullp, opt_index);
674 break;
675 }
Jens Axboe39f04332012-03-16 16:34:06 +0100676 case FIO_OPT_STR_VAL_TIME: {
677 unsigned long long *ullp = NULL;
Jens Axboe789f4cc2012-03-16 14:56:44 +0100678
Jens Axboe39f04332012-03-16 16:34:06 +0100679 if (o->off1)
680 ullp = td_var(to, o->off1);
681
Jens Axboeb6caa832012-03-16 21:22:56 +0100682 go = gopt_new_ullong(o, ullp, opt_index);
Jens Axboe9af4a242012-03-16 10:13:49 +0100683 break;
Jens Axboe789f4cc2012-03-16 14:56:44 +0100684 }
Jens Axboe39f04332012-03-16 16:34:06 +0100685 case FIO_OPT_INT: {
686 unsigned int *ip = NULL;
687
688 if (o->off1)
689 ip = td_var(to, o->off1);
690
Jens Axboeb6caa832012-03-16 21:22:56 +0100691 go = gopt_new_int(o, ip, opt_index);
Jens Axboe9af4a242012-03-16 10:13:49 +0100692 break;
Jens Axboe39f04332012-03-16 16:34:06 +0100693 }
Jens Axboe9af4a242012-03-16 10:13:49 +0100694 case FIO_OPT_STR_SET:
Jens Axboe39f04332012-03-16 16:34:06 +0100695 case FIO_OPT_BOOL: {
696 unsigned int *ip = NULL;
697
698 if (o->off1)
699 ip = td_var(to, o->off1);
700
Jens Axboeb6caa832012-03-16 21:22:56 +0100701 go = gopt_new_bool(o, ip, opt_index);
Jens Axboe9af4a242012-03-16 10:13:49 +0100702 break;
Jens Axboe39f04332012-03-16 16:34:06 +0100703 }
704 case FIO_OPT_STR: {
Jens Axboed872fb42012-03-19 19:17:49 +0100705 if (o->posval[0].ival) {
706 unsigned int *ip = NULL;
Jens Axboe39f04332012-03-16 16:34:06 +0100707
Jens Axboed872fb42012-03-19 19:17:49 +0100708 if (o->off1)
709 ip = td_var(to, o->off1);
Jens Axboe39f04332012-03-16 16:34:06 +0100710
Jens Axboed872fb42012-03-19 19:17:49 +0100711 go = gopt_new_combo_int(o, ip, opt_index);
712 } else {
713 /* TODO: usually ->cb, or unsigned int pointer */
714 go = gopt_new_str_store(o, NULL, opt_index);
715 }
716
Jens Axboe39f04332012-03-16 16:34:06 +0100717 break;
718 }
719 case FIO_OPT_STR_STORE: {
720 char *text = NULL;
721
Jens Axboed3944492012-03-16 18:53:32 +0100722 if (o->off1) {
Jens Axboe39f04332012-03-16 16:34:06 +0100723 char **p = td_var(to, o->off1);
724 text = *p;
725 }
726
Jens Axboe9af4a242012-03-16 10:13:49 +0100727 if (!o->posval[0].ival) {
Jens Axboeb6caa832012-03-16 21:22:56 +0100728 go = gopt_new_str_store(o, text, opt_index);
Jens Axboe9af4a242012-03-16 10:13:49 +0100729 break;
730 }
Jens Axboe39f04332012-03-16 16:34:06 +0100731
Jens Axboeb6caa832012-03-16 21:22:56 +0100732 go = gopt_new_combo_str(o, text, opt_index);
Jens Axboe39f04332012-03-16 16:34:06 +0100733 break;
734 }
Jens Axboe9af4a242012-03-16 10:13:49 +0100735 case FIO_OPT_STR_MULTI:
Jens Axboe7386d4a2012-03-19 18:36:29 +0100736 go = gopt_new_str_multi(o, opt_index);
Jens Axboe9af4a242012-03-16 10:13:49 +0100737 break;
Jens Axboe39f04332012-03-16 16:34:06 +0100738 case FIO_OPT_RANGE: {
739 unsigned int *ip[4] = { td_var(to, o->off1),
740 td_var(to, o->off2),
741 td_var(to, o->off3),
742 td_var(to, o->off4) };
743
Jens Axboeb6caa832012-03-16 21:22:56 +0100744 go = gopt_new_int_range(o, ip, opt_index);
Jens Axboe9af4a242012-03-16 10:13:49 +0100745 break;
Jens Axboe39f04332012-03-16 16:34:06 +0100746 }
Jens Axboe9af4a242012-03-16 10:13:49 +0100747 /* still need to handle this one */
748 case FIO_OPT_FLOAT_LIST:
749 break;
750 case FIO_OPT_DEPRECATED:
751 break;
752 default:
753 printf("ignore type %u\n", o->type);
754 break;
755 }
756
757 if (go) {
Jens Axboee8b0e952012-03-19 14:37:08 +0100758 GtkWidget *dest;
759
Jens Axboe9af4a242012-03-16 10:13:49 +0100760 if (o->help)
761 gtk_widget_set_tooltip_text(go->box, o->help);
Jens Axboee8b0e952012-03-19 14:37:08 +0100762
Jens Axboe9af4a242012-03-16 10:13:49 +0100763 go->opt_type = o->type;
Jens Axboee8b0e952012-03-19 14:37:08 +0100764
765 dest = gopt_get_group_frame(hbox, o->group);
766 if (!dest)
767 gtk_box_pack_start(GTK_BOX(hbox), go->box, FALSE, FALSE, 5);
768 else
769 gtk_box_pack_start(GTK_BOX(dest), go->box, FALSE, FALSE, 5);
Jens Axboe9af4a242012-03-16 10:13:49 +0100770 }
771}
772
Jens Axboe789f4cc2012-03-16 14:56:44 +0100773static void gopt_add_options(GtkWidget **vboxes, struct thread_options *to)
Jens Axboe9af4a242012-03-16 10:13:49 +0100774{
775 GtkWidget *hbox = NULL;
776 int i;
777
Jens Axboe90265352012-03-19 20:29:44 +0100778 /*
779 * First add all options
780 */
Jens Axboe9af4a242012-03-16 10:13:49 +0100781 for (i = 0; fio_options[i].name; i++) {
782 struct fio_option *o = &fio_options[i];
783 unsigned int mask = o->category;
784 struct opt_group *og;
785
786 while ((og = opt_group_from_mask(&mask)) != NULL) {
787 GtkWidget *vbox = vboxes[ffz(~og->mask)];
788
789 hbox = gtk_hbox_new(FALSE, 3);
790 gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 5);
Jens Axboe789f4cc2012-03-16 14:56:44 +0100791 gopt_add_option(hbox, o, i, to);
Jens Axboe9af4a242012-03-16 10:13:49 +0100792 }
793 }
794}
795
796static GtkWidget *gopt_add_group_tab(GtkWidget *notebook, struct opt_group *og)
797{
798 GtkWidget *box, *vbox, *scroll;
799
800 scroll = gtk_scrolled_window_new(NULL, NULL);
801 gtk_container_set_border_width(GTK_CONTAINER(scroll), 5);
802 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scroll), GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
803
804 vbox = gtk_vbox_new(FALSE, 3);
805 box = gtk_hbox_new(FALSE, 0);
806 gtk_box_pack_start(GTK_BOX(vbox), box, FALSE, FALSE, 5);
807 gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(scroll), vbox);
808 gtk_notebook_append_page(GTK_NOTEBOOK(notebook), scroll, gtk_label_new(og->name));
809
810 return vbox;
811}
812
813static void gopt_add_group_tabs(GtkWidget *notebook, GtkWidget **vbox)
814{
815 struct opt_group *og;
Jens Axboeec0218f2012-03-16 21:45:05 +0100816 unsigned int i;
Jens Axboe9af4a242012-03-16 10:13:49 +0100817
Jens Axboeec0218f2012-03-16 21:45:05 +0100818 i = 0;
Jens Axboe9af4a242012-03-16 10:13:49 +0100819 do {
820 unsigned int mask = (1U << i);
821
822 og = opt_group_from_mask(&mask);
823 if (!og)
824 break;
825 vbox[i] = gopt_add_group_tab(notebook, og);
826 i++;
827 } while (1);
828}
829
Jens Axboe789f4cc2012-03-16 14:56:44 +0100830void gopt_get_options_window(GtkWidget *window, struct thread_options *o)
Jens Axboe9af4a242012-03-16 10:13:49 +0100831{
832 GtkWidget *dialog, *notebook;
Jens Axboee8b0e952012-03-19 14:37:08 +0100833 GtkWidget *vboxes[__FIO_OPT_C_NR];
Jens Axboe9af4a242012-03-16 10:13:49 +0100834
835 dialog = gtk_dialog_new_with_buttons("Fio options",
836 GTK_WINDOW(window), GTK_DIALOG_DESTROY_WITH_PARENT,
837 GTK_STOCK_OK, GTK_RESPONSE_ACCEPT,
838 GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT, NULL);
839
840 gtk_widget_set_size_request(GTK_WIDGET(dialog), 1024, 768);
841
842 notebook = gtk_notebook_new();
843 gtk_notebook_set_scrollable(GTK_NOTEBOOK(notebook), 1);
844 gtk_notebook_popup_enable(GTK_NOTEBOOK(notebook));
845 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox), notebook, TRUE, TRUE, 5);
846
847 gopt_add_group_tabs(notebook, vboxes);
848
Jens Axboe789f4cc2012-03-16 14:56:44 +0100849 gopt_add_options(vboxes, o);
Jens Axboe9af4a242012-03-16 10:13:49 +0100850
851 gtk_widget_show_all(dialog);
852
853 gtk_dialog_run(GTK_DIALOG(dialog));
854
855 gtk_widget_destroy(dialog);
Stephen M. Camerona8baa342012-03-20 07:10:47 +0100856 memset(gopt_widgets, 0, sizeof(gopt_widgets));
857 memset(gopt_g_widgets, 0, sizeof(gopt_g_widgets));
Jens Axboe9af4a242012-03-16 10:13:49 +0100858}