From 9d841f6a8ce9a9268edd4777ae76e815a7d8c7b4 Mon Sep 17 00:00:00 2001 From: James Le Cuirot Date: Sun, 11 Jan 2015 21:43:06 +0000 Subject: [PATCH] Process output of click command It is useful to get instant feedback following a click but immediately restarting the timer might not give the desired results due to race conditions. Allowing the click command's output to be processed is more efficient and more flexible. Some thread locks are required for safety. These probably should have been added in the first place. --- panel-plugin/main.c | 64 ++++++++++++++++++++++++++--------------------------- 1 file changed, 31 insertions(+), 33 deletions(-) diff --git a/panel-plugin/main.c b/panel-plugin/main.c index d026a53..fb542cc 100644 --- a/panel-plugin/main.c +++ b/panel-plugin/main.c @@ -81,28 +81,8 @@ typedef struct genmon_t { } genmon_t; /**************************************************************/ -static void ExecOnClickCmd (GtkWidget *p_wSc, void *p_pvPlugin) -/* Execute the onClick Command */ -{ - struct genmon_t *poPlugin = (genmon_t *) p_pvPlugin; - struct monitor_t *poMonitor = &(poPlugin->oMonitor); - GError *error = NULL; - - xfce_spawn_command_line_on_screen( gdk_screen_get_default(), poMonitor->onClickCmd, 0, 0, &error ); - if (error) { - char *first = g_strdup_printf (_("Could not run \"%s\""), poMonitor->onClickCmd); - xfce_message_dialog (NULL, _("Xfce Panel"), - GTK_STOCK_DIALOG_ERROR, first, error->message, - GTK_STOCK_CLOSE, GTK_RESPONSE_OK, NULL); - g_error_free (error); - g_free (first); - } - -} -/**************************************************************/ - -static int DisplayCmdOutput (struct genmon_t *p_poPlugin) +static int DisplayCmdOutput (struct genmon_t *p_poPlugin, const char *cmd) /* Launch the command, get its output and display it in the panel-docked text field */ { @@ -115,12 +95,14 @@ static int DisplayCmdOutput (struct genmon_t *p_poPlugin) char *end; int newVersion=0; + gdk_threads_enter (); + if (!s_poToolTips) s_poToolTips = gtk_tooltips_new (); g_free (p_poPlugin->acValue); - if (poConf->acCmd[0]) - p_poPlugin->acValue = genmon_SpawnCmd (poConf->acCmd, 1); + if (cmd[0]) + p_poPlugin->acValue = genmon_SpawnCmd (cmd, 1); else p_poPlugin->acValue = NULL; @@ -233,6 +215,7 @@ static int DisplayCmdOutput (struct genmon_t *p_poPlugin) acToolTips, 0); g_free (acToolTips); + gdk_threads_leave (); return (0); }/* DisplayCmdOutput() */ @@ -247,11 +230,11 @@ static gboolean SetTimer (void *p_pvPlugin) struct genmon_t *poPlugin = (genmon_t *) p_pvPlugin; struct param_t *poConf = &(poPlugin->oConf.oParam); - DisplayCmdOutput (poPlugin); + DisplayCmdOutput (poPlugin, poConf->acCmd); if (poPlugin->iTimerId == 0) { - poPlugin->iTimerId = g_timeout_add (poConf->iPeriod_ms, + poPlugin->iTimerId = gdk_threads_add_timeout (poConf->iPeriod_ms, (GSourceFunc) SetTimer, poPlugin); return FALSE; } @@ -261,6 +244,27 @@ static gboolean SetTimer (void *p_pvPlugin) /**************************************************************/ +static gboolean RestartTimer (genmon_t *poPlugin) +{ + if (poPlugin->iTimerId) { + g_source_remove (poPlugin->iTimerId); + poPlugin->iTimerId = 0; + } + SetTimer (poPlugin); +}/* RestartTimer() */ + +/**************************************************************/ + +static void ExecOnClickCmd (GtkWidget *p_wSc, void *p_pvPlugin) +/* Execute the onClick Command */ +{ + struct genmon_t *poPlugin = (genmon_t *) p_pvPlugin; + struct monitor_t *poMonitor = &(poPlugin->oMonitor); + DisplayCmdOutput (poPlugin, poMonitor->onClickCmd); +} + +/**************************************************************/ + static genmon_t *genmon_create_control (XfcePanelPlugin *plugin) /* Plugin API */ /* Create the plugin */ @@ -557,12 +561,7 @@ static void UpdateConf (void *p_pvPlugin) SetCmd (poGUI->wTF_Cmd, poPlugin); SetLabel (poGUI->wTF_Title, poPlugin); SetMonitorFont (poPlugin); - /* Restart timer */ - if (poPlugin->iTimerId) { - g_source_remove (poPlugin->iTimerId); - poPlugin->iTimerId = 0; - } - SetTimer(p_pvPlugin); + RestartTimer (poPlugin); }/* UpdateConf() */ /**************************************************************/ @@ -617,8 +616,7 @@ static void genmon_dialog_response (GtkWidget *dlg, int response, gtk_widget_destroy (dlg); xfce_panel_plugin_unblock_menu (genmon->plugin); genmon_write_config (genmon->plugin, genmon); - /* Do not wait the next timer to update display */ - DisplayCmdOutput (genmon); + RestartTimer (genmon); } static void genmon_create_options (XfcePanelPlugin *plugin, -- 2.0.4