Various GUI updates
authorJens Axboe <axboe@kernel.dk>
Wed, 29 Feb 2012 10:16:52 +0000 (11:16 +0100)
committerJens Axboe <axboe@kernel.dk>
Wed, 29 Feb 2012 10:16:52 +0000 (11:16 +0100)
- Add File menu, move job file to File -> Open
- Kill Quit button, move to File -> Quit
- Move client setup and load closer to what we want
- Use c99 style for client ops

Various little changes.

Signed-off-by: Jens Axboe <axboe@kernel.dk>
client.c
client.h
fio.h
gfio.c
init.c

index e109b93fcc86e4db88cdc2edf9d06a745e22f17e..3c6778565882b969df59874081e0da2217ff17b7 100644 (file)
--- a/client.c
+++ b/client.c
@@ -84,13 +84,13 @@ static void handle_eta(struct fio_client *client, struct fio_net_cmd *cmd);
 static void handle_probe(struct fio_client *client, struct fio_net_cmd *cmd);
 
 struct client_ops fio_client_ops = {
-       fio_client_text_op,
-       handle_du,
-       handle_ts,
-       handle_gs,
-       handle_eta,
-       handle_probe,
-       NULL, /* status display, if NULL, printf is used */
+       .text_op        = fio_client_text_op,
+       .disk_util      = handle_du,
+       .thread_status  = handle_ts,
+       .group_stats    = handle_gs,
+       .eta            = handle_eta,
+       .probe          = handle_probe,
+       /* status display, if NULL, printf is used */
 };
 
 static struct timeval eta_tv;
index 150d1334539b77d65b305f75663caa53ccb557e8..f5ec9a3e5dff7134fa8a598c21ce8e82c86f6ff2 100644 (file)
--- a/client.h
+++ b/client.h
@@ -24,7 +24,7 @@ struct client_ops {
        client_disk_util_op_func disk_util;
        client_thread_status_op thread_status;
        client_group_stats_op group_stats;
-       client_eta_op eta; 
+       client_eta_op eta;
        client_probe_op probe;
        client_thread_status_display_op thread_status_display;
 };
diff --git a/fio.h b/fio.h
index 9af047c2eed344fe990f0f534b09310c7d457315..7930aa0fb57b3f30029a4369033417745afa88a4 100644 (file)
--- a/fio.h
+++ b/fio.h
@@ -587,6 +587,7 @@ static inline int should_fsync(struct thread_data *td)
 /*
  * Init/option functions
  */
+extern int __must_check fio_init_options(void);
 extern int __must_check parse_options(int, char **);
 extern int parse_jobs_ini(char *, int, int);
 extern int parse_cmd_line(int, char **);
diff --git a/gfio.c b/gfio.c
index 18322203ff76c33f5b68c8cc47d896af0a6a0901..6186515eff97280919ad9d9353a4d363ff43d69b 100644 (file)
--- a/gfio.c
+++ b/gfio.c
@@ -44,13 +44,9 @@ static struct button_spec {
        { "Start Job",
                start_job_clicked,
                "Send current fio job to fio server to be executed" },
-#define QUIT_BUTTON 1
-       { "Quit", quit_clicked, "Quit gfio" },
 };
 
 struct gui {
-       int argc;
-       char **argv;
        GtkWidget *window;
        GtkWidget *vbox;
        GtkWidget *topvbox;
@@ -65,13 +61,16 @@ struct gui {
        GtkWidget *port_label;
        GtkWidget *port_entry;
        GtkWidget *hostname_combo_box; /* ipv4, ipv6 or socket */
-       GtkWidget *jobfile_hbox;
-       GtkWidget *jobfile_label;
-       GtkWidget *jobfile_entry;
        GtkWidget *scrolled_window;
        GtkWidget *textview;
+       GtkWidget *error_info_bar;
+       GtkWidget *error_label;
        GtkTextBuffer *text;
        pthread_t t;
+
+       void *cookie;
+       int nr_job_files;
+       char **job_files;
 } ui;
 
 static void gfio_text_op(struct fio_client *client,
@@ -134,13 +133,13 @@ static void gfio_update_thread_status(char *status_message, double perc)
 }
 
 struct client_ops gfio_client_ops = {
-       gfio_text_op,
-       gfio_disk_util_op,
-       gfio_thread_status_op,
-       gfio_group_stats_op,
-       gfio_eta_op,
-       gfio_probe_op,
-       gfio_update_thread_status,
+       .text_op                = gfio_text_op,
+       .disk_util              = gfio_disk_util_op,
+       .thread_status          = gfio_thread_status_op,
+       .group_stats            = gfio_group_stats_op,
+       .eta                    = gfio_eta_op,
+       .probe                  = gfio_probe_op,
+       .thread_status_display  = gfio_update_thread_status,
 };
 
 static void quit_clicked(__attribute__((unused)) GtkWidget *widget,
@@ -149,58 +148,40 @@ static void quit_clicked(__attribute__((unused)) GtkWidget *widget,
         gtk_main_quit();
 }
 
-static void add_arg(char **argv, int index, const char *value)
-{
-       argv[index] = malloc(strlen(value) + 1);
-       strcpy(argv[index], value);
-}
-
-static void free_args(int argc, char **argv)
-{
-       int i;
-
-       for (i = 0; i < argc; i++)
-               free(argv[i]);
-       free(argv);
-}
-
 static void *job_thread(void *arg)
 {
        struct gui *ui = arg;
 
        fio_handle_clients(&gfio_client_ops);
        gtk_widget_set_sensitive(ui->button[START_JOB_BUTTON], 1);
-       free_args(ui->argc, ui->argv);
        return NULL;
 }
 
-static void construct_options(struct gui *ui, int *argc, char ***argv)
+static int send_job_files(struct gui *ui)
 {
-       const char *hostname, *hostname_type, *port, *jobfile;
-       char newarg[200];
-       
-       hostname_type = gtk_entry_get_text(GTK_ENTRY(GTK_COMBO(ui->hostname_combo_box)->entry));
-       hostname = gtk_entry_get_text(GTK_ENTRY(ui->hostname_entry));
-       port = gtk_entry_get_text(GTK_ENTRY(ui->port_entry));
-       jobfile = gtk_entry_get_text(GTK_ENTRY(ui->jobfile_entry));
-
-       *argc = 3;
-       *argv = malloc(*argc * sizeof(**argv));         
-       add_arg(*argv, 0,  "gfio");
-       snprintf(newarg, sizeof(newarg) - 1, "--client=%s", hostname);
-       add_arg(*argv, 1, newarg);
-       add_arg(*argv, 2, jobfile);
+       int i, ret;
+
+       for (i = 0; i < ui->nr_job_files; i++) {
+               ret = fio_clients_send_ini(ui->job_files[i]);
+               free(ui->job_files[i]);
+               ui->job_files[i] = NULL;
+               if (ret)
+                       return ret;
+       }
+
+       return 0;
 }
 
 static void start_job_thread(pthread_t *t, struct gui *ui)
 {
-       construct_options(ui, &ui->argc, &ui->argv);
-       if (parse_options(ui->argc, ui->argv)) {
+       fio_clients_connect();
+
+       if (send_job_files(ui)) {
                printf("Yeah, I didn't really like those options too much.\n");
-               free_args(ui->argc, ui->argv);
                gtk_widget_set_sensitive(ui->button[START_JOB_BUTTON], 1);
                return;
        }
+               
        pthread_create(t, NULL, job_thread, ui);
 }
 
@@ -233,10 +214,184 @@ static void add_buttons(struct gui *ui,
                add_button(ui, i, ui->buttonbox, &buttonlist[i]);
 }
 
+static void on_info_bar_response(GtkWidget *widget, gint response,
+                                 gpointer data)
+{
+       if (response == GTK_RESPONSE_OK) {
+               gtk_widget_destroy(widget);
+               ui.error_info_bar = NULL;
+       }
+}
+
+void report_error(GError* error)
+{
+       if (ui.error_info_bar == NULL) {
+               ui.error_info_bar = gtk_info_bar_new_with_buttons(GTK_STOCK_OK,
+                                                              GTK_RESPONSE_OK,
+                                                              NULL);
+               g_signal_connect(ui.error_info_bar, "response", G_CALLBACK(on_info_bar_response), NULL);
+               gtk_info_bar_set_message_type(GTK_INFO_BAR(ui.error_info_bar),
+                                             GTK_MESSAGE_ERROR);
+               
+               ui.error_label = gtk_label_new(error->message);
+               GtkWidget *container = gtk_info_bar_get_content_area(GTK_INFO_BAR(ui.error_info_bar));
+               gtk_container_add(GTK_CONTAINER(container), ui.error_label);
+               
+               gtk_box_pack_start(GTK_BOX(ui.vbox), ui.error_info_bar, FALSE, FALSE, 0);
+               gtk_widget_show_all(ui.vbox);
+       } else {
+               char buffer[256];
+               snprintf(buffer, sizeof(buffer), "Failed to open file.");
+               gtk_label_set(GTK_LABEL(ui.error_label), buffer);
+       }
+}
+
+static void file_open(GtkWidget *w, gpointer data)
+{
+       GtkWidget *dialog;
+       GSList *filenames, *fn_glist;
+       GtkFileFilter *filter;
+
+       dialog = gtk_file_chooser_dialog_new("Open File",
+               GTK_WINDOW(ui.window),
+               GTK_FILE_CHOOSER_ACTION_OPEN,
+               GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
+               GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
+               NULL);
+       gtk_file_chooser_set_select_multiple(GTK_FILE_CHOOSER(dialog), TRUE);
+
+       filter = gtk_file_filter_new();
+       gtk_file_filter_add_pattern(filter, "*.fio");
+       gtk_file_filter_add_pattern(filter, "*.job");
+       gtk_file_filter_add_mime_type(filter, "text/fio");
+       gtk_file_filter_set_name(filter, "Fio job file");
+       gtk_file_chooser_set_filter(GTK_FILE_CHOOSER(dialog), filter);
+
+       if (gtk_dialog_run(GTK_DIALOG(dialog)) != GTK_RESPONSE_ACCEPT) {
+               gtk_widget_destroy(dialog);
+               return;
+       }
+
+       fn_glist = gtk_file_chooser_get_filenames(GTK_FILE_CHOOSER(dialog));
+       filenames = fn_glist;
+       while (filenames != NULL) {
+               const char *hostname;
+
+               ui.job_files = realloc(ui.job_files, (ui.nr_job_files + 1) * sizeof(char *));
+               ui.job_files[ui.nr_job_files] = strdup(filenames->data);
+               ui.nr_job_files++;
+
+               hostname = gtk_entry_get_text(GTK_ENTRY(ui.hostname_entry));
+               fio_client_add(hostname, &ui.cookie);
+#if 0
+               if (error) {
+                       report_error(error);
+                       g_error_free(error);
+                       error = NULL;
+               }
+#endif
+                       
+               g_free(filenames->data);
+               filenames = g_slist_next(filenames);
+       }
+       g_slist_free(fn_glist);
+       gtk_widget_destroy(dialog);
+}
+
+static void file_save(GtkWidget *w, gpointer data)
+{
+       GtkWidget *dialog;
+
+       dialog = gtk_file_chooser_dialog_new("Save File",
+               GTK_WINDOW(ui.window),
+               GTK_FILE_CHOOSER_ACTION_SAVE,
+               GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
+               GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT,
+               NULL);
+
+       gtk_file_chooser_set_do_overwrite_confirmation(GTK_FILE_CHOOSER(dialog), TRUE);
+       gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(dialog), "Untitled document");
+
+       if (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT) {
+               char *filename;
+
+               filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog));
+               // save_job_file(filename);
+               g_free(filename);
+       }
+       gtk_widget_destroy(dialog);
+}
+
+static void about_dialog(GtkWidget *w, gpointer data)
+{
+       gtk_show_about_dialog(NULL,
+               "program-name", "gfio",
+               "comments", "Gtk2 UI for fio",
+               "license", "GPLv2",
+               "version", fio_version_string,
+               "copyright", "Jens Axboe <axboe@kernel.dk> 2012",
+               "logo-icon-name", "fio",
+               /* Must be last: */
+               NULL, NULL,
+               NULL);
+}
+
+static GtkActionEntry menu_items[] = {
+        { "FileMenuAction", GTK_STOCK_FILE, "File", NULL, NULL, NULL},
+        { "HelpMenuAction", GTK_STOCK_HELP, "Help", NULL, NULL, NULL},
+       { "OpenFile",       GTK_STOCK_OPEN, NULL,   "<Control>O", NULL, G_CALLBACK(file_open) },
+        { "SaveFile",       GTK_STOCK_SAVE, NULL,   "<Control>S", NULL, G_CALLBACK(file_save) },
+        { "Quit",           GTK_STOCK_QUIT, NULL,   "<Control>Q", NULL, G_CALLBACK(quit_clicked) },
+       { "About",          GTK_STOCK_ABOUT, NULL,  NULL, NULL, G_CALLBACK(about_dialog) },
+};
+static gint nmenu_items = sizeof (menu_items) / sizeof(menu_items[0]);
+
+static const gchar *ui_string = " \
+       <ui> \
+               <menubar name=\"MainMenu\"> \
+                       <menu name=\"FileMenu\" action=\"FileMenuAction\"> \
+                               <menuitem name=\"Open\" action=\"OpenFile\" /> \
+                               <menuitem name=\"Save\" action=\"SaveFile\" /> \
+                               <separator name=\"Separator\"/> \
+                               <menuitem name=\"Quit\" action=\"Quit\" /> \
+                       </menu> \
+                       <menu name=\"Help\" action=\"HelpMenuAction\"> \
+                               <menuitem name=\"About\" action=\"About\" /> \
+                       </menu> \
+               </menubar> \
+       </ui> \
+";
+
+static GtkWidget *get_menubar_menu(GtkWidget *window, GtkUIManager *ui_manager)
+{
+       GtkActionGroup *action_group = gtk_action_group_new("Menu");
+       GError *error = 0;
+
+       action_group = gtk_action_group_new("Menu");
+       gtk_action_group_add_actions(action_group, menu_items, nmenu_items, 0);
+
+       gtk_ui_manager_insert_action_group(ui_manager, action_group, 0);
+       gtk_ui_manager_add_ui_from_string(GTK_UI_MANAGER(ui_manager), ui_string, -1, &error);
+
+       gtk_window_add_accel_group(GTK_WINDOW(window), gtk_ui_manager_get_accel_group(ui_manager));
+       return gtk_ui_manager_get_widget(ui_manager, "/MainMenu");
+}
+
+void gfio_ui_setup(GtkSettings *settings, GtkWidget *menubar,
+                   GtkWidget *vbox, GtkUIManager *ui_manager)
+{
+        gtk_box_pack_start(GTK_BOX(vbox), menubar, FALSE, FALSE, 0);
+}
+
 static void init_ui(int *argc, char **argv[], struct gui *ui)
 {
        GList *hostname_type_list = NULL;
        char portnum[20];
+       GtkSettings *settings;
+       GtkUIManager *uimanager;
+       GtkWidget *menu;
+
+       memset(ui, 0, sizeof(*ui));
 
        /* Magical g*thread incantation, you just need this thread stuff.
         * Without it, the update that happens in gfio_update_thread_status
@@ -247,17 +402,24 @@ static void init_ui(int *argc, char **argv[], struct gui *ui)
        gdk_threads_init();
 
        gtk_init(argc, argv);
+       settings = gtk_settings_get_default();
+       gtk_settings_set_long_property(settings, "gtk_tooltip_timeout", 10, "gfio setting");
+       g_type_init();
        
        ui->window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
         gtk_window_set_title(GTK_WINDOW(ui->window), "fio");
        gtk_window_set_default_size(GTK_WINDOW(ui->window), 700, 500);
 
-       g_signal_connect(ui->window, "delete-event", G_CALLBACK (quit_clicked), NULL);
-       g_signal_connect(ui->window, "destroy", G_CALLBACK (quit_clicked), NULL);
+       g_signal_connect(ui->window, "delete-event", G_CALLBACK(quit_clicked), NULL);
+       g_signal_connect(ui->window, "destroy", G_CALLBACK(quit_clicked), NULL);
 
        ui->vbox = gtk_vbox_new(FALSE, 0);
        gtk_container_add(GTK_CONTAINER (ui->window), ui->vbox);
 
+       uimanager = gtk_ui_manager_new();
+       menu = get_menubar_menu(ui->window, uimanager);
+       gfio_ui_setup(settings, menu, ui->vbox, uimanager);
+
        /*
         * Set up alignments for widgets at the top of ui, 
         * align top left, expand horizontally but not vertically
@@ -283,11 +445,11 @@ static void init_ui(int *argc, char **argv[], struct gui *ui)
         * Set up combo box for address type
         */
        ui->hostname_combo_box = gtk_combo_new();
-       gtk_entry_set_text(GTK_ENTRY (GTK_COMBO(ui->hostname_combo_box)->entry), "IPv4");
+       gtk_entry_set_text(GTK_ENTRY(GTK_COMBO(ui->hostname_combo_box)->entry), "IPv4");
        hostname_type_list = g_list_append(hostname_type_list, (gpointer) "IPv4"); 
        hostname_type_list = g_list_append(hostname_type_list, (gpointer) "local socket"); 
        hostname_type_list = g_list_append(hostname_type_list, (gpointer) "IPv6"); 
-       gtk_combo_set_popdown_strings (GTK_COMBO (ui->hostname_combo_box), hostname_type_list);
+       gtk_combo_set_popdown_strings(GTK_COMBO(ui->hostname_combo_box), hostname_type_list);
        g_list_free(hostname_type_list);
 
        gtk_container_add(GTK_CONTAINER (ui->hostname_hbox), ui->hostname_label);
@@ -297,16 +459,6 @@ static void init_ui(int *argc, char **argv[], struct gui *ui)
        gtk_container_add(GTK_CONTAINER (ui->hostname_hbox), ui->hostname_combo_box);
        gtk_container_add(GTK_CONTAINER (ui->topvbox), ui->hostname_hbox);
 
-       /*
-        * Set up jobfile text entry (temporary until gui really works)
-        */
-       ui->jobfile_hbox = gtk_hbox_new(FALSE, 0);
-       ui->jobfile_label = gtk_label_new("Job file:");
-       ui->jobfile_entry = gtk_entry_new();
-       gtk_container_add(GTK_CONTAINER (ui->jobfile_hbox), ui->jobfile_label);
-       gtk_container_add(GTK_CONTAINER (ui->jobfile_hbox), ui->jobfile_entry);
-       gtk_container_add(GTK_CONTAINER (ui->topvbox), ui->jobfile_hbox);
-
        /*
         * Set up thread status progress bar
         */
@@ -350,6 +502,8 @@ int main(int argc, char *argv[], char *envp[])
 {
        if (initialize_fio(envp))
                return 1;
+       if (fio_init_options())
+               return 1;
 
        init_ui(&argc, &argv, &ui);
 
diff --git a/init.c b/init.c
index 710e86730b7d82aa085eff13433dcc536df1f7f0..2bdc71f19871023da67037a44ad17916fdd0f34e 100644 (file)
--- a/init.c
+++ b/init.c
@@ -1571,10 +1571,8 @@ int parse_cmd_line(int argc, char *argv[])
        return ini_idx;
 }
 
-int parse_options(int argc, char *argv[])
+int fio_init_options(void)
 {
-       int job_files, i;
-
        f_out = stdout;
        f_err = stderr;
 
@@ -1586,6 +1584,16 @@ int parse_options(int argc, char *argv[])
        if (fill_def_thread())
                return 1;
 
+       return 0;
+}
+
+int parse_options(int argc, char *argv[])
+{
+       int job_files, i;
+
+       if (fio_init_options())
+               return 1;
+
        job_files = parse_cmd_line(argc, argv);
 
        if (job_files > 0) {