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