summaryrefslogtreecommitdiff
path: root/command/command.c
diff options
context:
space:
mode:
Diffstat (limited to 'command/command.c')
-rw-r--r--command/command.c282
1 files changed, 133 insertions, 149 deletions
diff --git a/command/command.c b/command/command.c
index f62f2325..2cd3c00c 100644
--- a/command/command.c
+++ b/command/command.c
@@ -32,7 +32,6 @@
#include <mate-panel-applet.h>
#include <mate-panel-applet-gsettings.h>
-#include "ma-command.h"
/* Applet constants */
#define APPLET_ICON "utilities-terminal"
@@ -55,15 +54,14 @@ typedef struct
MatePanelApplet *applet;
GSettings *settings;
+ GPid child_pid;
+ gchar *buffer;
GtkLabel *label;
GtkImage *image;
GtkBox *box;
- MaCommand *command;
- GCancellable *cancellable;
- gboolean running;
- gchar *cmdline;
+ gchar *command;
gint interval;
gint width;
@@ -73,11 +71,6 @@ typedef struct
static void command_about_callback (GtkAction *action, CommandApplet *command_applet);
static void command_settings_callback (GtkAction *action, CommandApplet *command_applet);
static gboolean command_execute (CommandApplet *command_applet);
-static gboolean command_text_changed (GtkWidget *widget, GdkEvent *event, gpointer user_data);
-static void interval_value_changed (GtkSpinButton *spin_button, gpointer user_data);
-static void width_value_changed (GtkSpinButton *spin_button, gpointer user_data);
-static void command_async_ready_callback (GObject *source_object, GAsyncResult *res, gpointer user_data);
-static gboolean timeout_callback (CommandApplet *command_applet);
static const GtkActionEntry applet_menu_actions [] = {
{ "Preferences", "document-properties", N_("_Preferences"), NULL, NULL, G_CALLBACK (command_settings_callback) },
@@ -98,15 +91,22 @@ command_applet_destroy (MatePanelApplet *applet_widget, CommandApplet *command_a
command_applet->timeout_id = 0;
}
- if (command_applet->cmdline != NULL)
+ if (command_applet->command != NULL)
{
- g_free (command_applet->cmdline);
- command_applet->cmdline = NULL;
+ g_free (command_applet->command);
+ command_applet->command = NULL;
}
- if (command_applet->command != NULL)
+ if (command_applet->child_pid != 0)
{
- g_object_unref (command_applet->command);
+ g_spawn_close_pid (command_applet->child_pid);
+ command_applet->child_pid = 0;
+ }
+
+ if (command_applet->buffer != NULL)
+ {
+ g_free (command_applet->buffer);
+ command_applet->buffer = NULL;
}
g_object_unref (command_applet->settings);
@@ -127,56 +127,7 @@ command_about_callback (GtkAction *action, CommandApplet *command_applet)
"comments", _("Shows the output of a command"),
"translator-credits", _("translator-credits"),
"logo-icon-name", APPLET_ICON,
- NULL );
-}
-
-static gboolean
-command_text_changed (GtkWidget *widget, GdkEvent *event, gpointer user_data)
-{
- const gchar *text;
- CommandApplet *command_applet;
-
- command_applet = (CommandApplet*) user_data;
- text = gtk_entry_get_text (GTK_ENTRY(widget));
- if (g_strcmp0(command_applet->cmdline, text) == 0) {
- return TRUE;
- }
-
- if (strlen (text) == 0) {
- gtk_label_set_text (command_applet->label, ERROR_OUTPUT);
- return TRUE;
- }
-
- g_settings_set_string (command_applet->settings, COMMAND_KEY, text);
- return TRUE;
-}
-
-static void interval_value_changed (GtkSpinButton *spin_button, gpointer user_data)
-{
- gint value;
- CommandApplet *command_applet;
-
- command_applet = (CommandApplet*) user_data;
- value = gtk_spin_button_get_value_as_int (spin_button);
- if (command_applet->interval == value) {
- return;
- }
-
- g_settings_set_int (command_applet->settings, INTERVAL_KEY, value);
-}
-
-static void width_value_changed (GtkSpinButton *spin_button, gpointer user_data)
-{
- gint value;
- CommandApplet *command_applet;
-
- command_applet = (CommandApplet*) user_data;
- value = gtk_spin_button_get_value_as_int (spin_button);
- if (command_applet->width == value) {
- return;
- }
-
- g_settings_set_int (command_applet->settings, WIDTH_KEY, value);
+ NULL );
}
/* Show the preferences dialog */
@@ -235,13 +186,10 @@ command_settings_callback (GtkAction *action, CommandApplet *command_applet)
g_signal_connect (dialog, "response", G_CALLBACK (gtk_widget_destroy), dialog);
- g_signal_connect(command, "focus-out-event", G_CALLBACK (command_text_changed), command_applet);
- g_signal_connect(interval, "value-changed", G_CALLBACK (interval_value_changed), command_applet);
- g_signal_connect(width, "value-changed", G_CALLBACK (width_value_changed), command_applet);
/* use g_settings_bind to manage settings */
- g_settings_bind (command_applet->settings, COMMAND_KEY, command, "text", G_SETTINGS_BIND_GET_NO_CHANGES);
- g_settings_bind (command_applet->settings, INTERVAL_KEY, interval, "value", G_SETTINGS_BIND_GET_NO_CHANGES);
- g_settings_bind (command_applet->settings, WIDTH_KEY, width, "value", G_SETTINGS_BIND_GET_NO_CHANGES);
+ g_settings_bind (command_applet->settings, COMMAND_KEY, command, "text", G_SETTINGS_BIND_DEFAULT);
+ g_settings_bind (command_applet->settings, INTERVAL_KEY, interval, "value", G_SETTINGS_BIND_DEFAULT);
+ g_settings_bind (command_applet->settings, WIDTH_KEY, width, "value", G_SETTINGS_BIND_DEFAULT);
g_settings_bind (command_applet->settings, SHOW_ICON_KEY, showicon, "active", G_SETTINGS_BIND_DEFAULT);
gtk_widget_show_all (GTK_WIDGET (dialog));
@@ -251,27 +199,17 @@ command_settings_callback (GtkAction *action, CommandApplet *command_applet)
static void
settings_command_changed (GSettings *settings, gchar *key, CommandApplet *command_applet)
{
- GError *error = NULL;
- gchar *cmdline;
- gchar **argv;
+ gchar *command;
- cmdline = g_settings_get_string (command_applet->settings, COMMAND_KEY);
- if (strlen (cmdline) == 0 || g_strcmp0(command_applet->cmdline, cmdline) == 0)
- return;
-
- if (!g_shell_parse_argv (cmdline, NULL, &argv, &error))
- {
- gtk_label_set_text (command_applet->label, ERROR_OUTPUT);
- g_clear_error (&error);
- return;
- }
- g_strfreev(argv);
+ command = g_settings_get_string (command_applet->settings, COMMAND_KEY);
- if (command_applet->cmdline)
- g_free (command_applet->cmdline);
- command_applet->cmdline = cmdline;
+ if (command_applet->command)
+ g_free (command_applet->command);
- command_execute (command_applet);
+ if (command != NULL && command[0] != 0)
+ command_applet->command = command;
+ else
+ command_applet->command = g_strdup ("");
}
static void
@@ -281,9 +219,10 @@ settings_width_changed (GSettings *settings, gchar *key, CommandApplet *command_
width = g_settings_get_int (command_applet->settings, WIDTH_KEY);
- if (command_applet->width != width) {
- command_applet->width = width;
- }
+ command_applet->width = width;
+
+ /* execute command to start new timer */
+ command_execute (command_applet);
}
static void
@@ -297,18 +236,23 @@ settings_interval_changed (GSettings *settings, gchar *key, CommandApplet *comma
if (interval < 1)
interval = 1;
- if (command_applet->interval == interval) {
- return;
- }
command_applet->interval = interval;
+ /* stop current timer */
+ if (command_applet->timeout_id != 0)
+ {
+ g_source_remove (command_applet->timeout_id);
+ command_applet->timeout_id = 0;
+ }
+
+ /* execute command to start new timer */
command_execute (command_applet);
}
static void
process_command_output (CommandApplet *command_applet, gchar *output)
{
- gtk_widget_set_tooltip_text (GTK_WIDGET (command_applet->label), command_applet->cmdline);
+ gtk_widget_set_tooltip_text (GTK_WIDGET (command_applet->label), command_applet->command);
if ((output == NULL) || (output[0] == '\0'))
{
@@ -357,82 +301,121 @@ process_command_output (CommandApplet *command_applet, gchar *output)
}
}
-static void command_async_ready_callback (GObject *source_object, GAsyncResult *res, gpointer user_data)
+static gboolean
+stdout_io_func (GIOChannel *ioc, GIOCondition cond, gpointer data)
{
- gchar *output;
- GError *error = NULL;
CommandApplet *command_applet;
- command_applet = (CommandApplet*) user_data;
+ command_applet = data;
+ if (cond & (G_IO_IN | G_IO_PRI))
+ {
+ if (strlen (command_applet->buffer) == 0)
+ {
+ GError *error = NULL;
+ GIOStatus ret;
+ gsize len = 0;
- output = ma_command_run_finish (command_applet->command, res, &error);
- if (error == NULL) {
- process_command_output (command_applet, output);
- } else {
- if (!g_error_matches (error, G_IO_ERROR, G_IO_ERROR_FAILED)) {
- gtk_label_set_text (command_applet->label, ERROR_OUTPUT);
+ ret = g_io_channel_read_chars (ioc, command_applet->buffer, command_applet->width, &len, &error);
+ if (len <= 0 || ret != G_IO_STATUS_NORMAL)
+ {
+ g_clear_error (&error);
+ return FALSE;
+ }
+ process_command_output (command_applet, command_applet->buffer);
}
- g_error_free (error);
+ return FALSE;
}
- g_free (output);
- command_applet->running = FALSE;
+
+ if (cond & (G_IO_ERR | G_IO_HUP | G_IO_NVAL))
+ return FALSE;
+
+ return TRUE;
}
-static gboolean timeout_callback (CommandApplet *command_applet)
+static void
+on_child_exit (GPid child_pid, gint status, gpointer data)
{
- /* command is empty, wait for next timer execution */
- if (strlen (command_applet->cmdline) == 0) {
- return G_SOURCE_CONTINUE;
- }
+ CommandApplet *command_applet;
- /* command running, wait for next timer execution */
- if (command_applet->running) {
- return G_SOURCE_CONTINUE;
- } else {
- gchar **argv;
- GError *error = NULL;
- if (!g_shell_parse_argv (command_applet->cmdline, NULL, &argv, &error)) {
- gtk_label_set_text (command_applet->label, ERROR_OUTPUT);
- g_clear_error (&error);
- return G_SOURCE_CONTINUE;
- }
- g_strfreev(argv);
- command_execute (command_applet);
- return G_SOURCE_REMOVE;
- }
+ command_applet = data;
+ g_spawn_close_pid (child_pid);
+ command_applet->child_pid = 0;
+}
+
+static void
+set_up_io_channel (gint fd, GIOCondition cond, GIOFunc func, gpointer data)
+{
+ GIOChannel *ioc;
+
+ ioc = g_io_channel_unix_new (fd);
+
+ g_io_channel_set_encoding (ioc, NULL, NULL);
+ g_io_channel_set_buffered (ioc, FALSE);
+
+ g_io_channel_set_close_on_unref (ioc, TRUE);
+
+ g_io_add_watch (ioc, cond, func, data);
+ g_io_channel_unref (ioc);
}
static gboolean
command_execute (CommandApplet *command_applet)
{
- /* stop current timer */
- if (command_applet->timeout_id != 0)
+ GError *error = NULL;
+ gchar **argv;
+ gint stdout_fd;
+
+ /* command is empty, wait for next timer execution */
+ if (strlen (command_applet->command) == 0)
{
- g_source_remove (command_applet->timeout_id);
- command_applet->timeout_id = 0;
+ return TRUE;
}
- if (command_applet->running) {
- g_cancellable_cancel (command_applet->cancellable);
+ /* command running, wait for next timer execution */
+ if (command_applet->child_pid != 0)
+ {
+ return TRUE;
}
- g_object_set (G_OBJECT(command_applet->command), "command", command_applet->cmdline, NULL);
- ma_command_run_async (command_applet->command,
- command_applet->cancellable,
- command_async_ready_callback,
- command_applet);
- if (!command_applet->running) {
- command_applet->running = TRUE;
+ if (!g_shell_parse_argv (command_applet->command, NULL, &argv, &error))
+ {
+ gtk_label_set_text (command_applet->label, ERROR_OUTPUT);
+ g_clear_error (&error);
+ return FALSE;
}
- if (g_cancellable_is_cancelled (command_applet->cancellable)) {
- g_cancellable_reset (command_applet->cancellable);
+ if (!g_spawn_async_with_pipes (NULL,
+ argv,
+ NULL,
+ G_SPAWN_SEARCH_PATH | G_SPAWN_DO_NOT_REAP_CHILD,
+ NULL,
+ NULL,
+ &command_applet->child_pid,
+ NULL,
+ &stdout_fd,
+ NULL,
+ &error))
+ {
+ g_clear_error (&error);
+ g_strfreev (argv);
+ return TRUE;
}
+ g_free(command_applet->buffer);
+ command_applet->buffer = g_new0(gchar, command_applet->width+1);
+
+ set_up_io_channel (stdout_fd, G_IO_IN|G_IO_PRI|G_IO_ERR|G_IO_HUP|G_IO_NVAL,
+ stdout_io_func, command_applet);
+
+ if (command_applet->child_pid != (GPid) 0)
+ g_child_watch_add (command_applet->child_pid, on_child_exit, command_applet);
+
+ /* start timer for next execution */
command_applet->timeout_id = g_timeout_add_seconds (command_applet->interval,
- (GSourceFunc) timeout_callback,
+ (GSourceFunc) command_execute,
command_applet);
- return G_SOURCE_CONTINUE;
+ g_strfreev (argv);
+ return FALSE;
}
static gboolean
@@ -451,15 +434,15 @@ command_applet_fill (MatePanelApplet* applet)
command_applet->settings = mate_panel_applet_settings_new (applet, COMMAND_SCHEMA);
command_applet->interval = g_settings_get_int (command_applet->settings, INTERVAL_KEY);
- command_applet->cmdline = g_settings_get_string (command_applet->settings, COMMAND_KEY);
+ command_applet->command = g_settings_get_string (command_applet->settings, COMMAND_KEY);
command_applet->width = g_settings_get_int (command_applet->settings, WIDTH_KEY);
- command_applet->command = ma_command_new(command_applet->cmdline, NULL);
- command_applet->cancellable = g_cancellable_new ();
command_applet->box = GTK_BOX (gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0));
command_applet->image = GTK_IMAGE (gtk_image_new_from_icon_name (APPLET_ICON, 24));
command_applet->label = GTK_LABEL (gtk_label_new (ERROR_OUTPUT));
command_applet->timeout_id = 0;
+ command_applet->child_pid = 0;
+ command_applet->buffer = NULL;
/* we add the Gtk label into the applet */
gtk_box_pack_start (command_applet->box,
@@ -506,6 +489,7 @@ command_applet_fill (MatePanelApplet* applet)
/* first command execution */
command_execute (command_applet);
+
return TRUE;
}