From f5e7344a8b152ab4f8762fbc8cf2979b44d5c757 Mon Sep 17 00:00:00 2001 From: Mike Massonnet Date: Sun, 23 May 2010 00:47:02 +0200 Subject: [PATCH] =?UTF-8?q?Implement=20option=20=E2=80=9CMore=20precision?= =?UTF-8?q?=E2=80=9D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main.c | 7 +++++++ src/process-statusbar.c | 26 +++++++++++++++++++++++--- src/task-manager.c | 30 +++++++++++++++++++++++++++--- 3 files changed, 57 insertions(+), 6 deletions(-) diff --git a/src/main.c b/src/main.c index 6d891c3..3efe9ad 100644 --- a/src/main.c +++ b/src/main.c @@ -45,6 +45,12 @@ init_timeout (void) return TRUE; } +static void +force_timeout_update (void) +{ + init_timeout (); +} + static void refresh_rate_changed (XtmSettings *settings) { @@ -77,6 +83,7 @@ int main (int argc, char *argv[]) init_timeout (); g_signal_connect (settings, "notify::refresh-rate", G_CALLBACK (refresh_rate_changed), NULL); + g_signal_connect_after (settings, "notify::more-precision", G_CALLBACK (force_timeout_update), NULL); g_signal_connect (window, "destroy", G_CALLBACK (gtk_main_quit), NULL); diff --git a/src/process-statusbar.c b/src/process-statusbar.c index aecac8f..60a64b8 100644 --- a/src/process-statusbar.c +++ b/src/process-statusbar.c @@ -16,6 +16,7 @@ #include #include "process-statusbar.h" +#include "settings.h" @@ -35,6 +36,8 @@ struct _XtmProcessStatusbar { GtkStatusbar parent; /**/ + XtmSettings * settings; + GtkWidget * label_num_processes; GtkWidget * label_cpu; GtkWidget * label_memory; @@ -72,6 +75,8 @@ xtm_process_statusbar_init (XtmProcessStatusbar *statusbar) { GtkWidget *area, *hbox; + statusbar->settings = xtm_settings_get_default (); + #if GTK_CHECK_VERSION(2,20,0) area = gtk_statusbar_get_message_area (GTK_STATUSBAR (statusbar)); #else @@ -108,32 +113,47 @@ xtm_process_statusbar_init (XtmProcessStatusbar *statusbar) gtk_widget_show_all (hbox); } +static gchar * +rounded_float_value (gfloat value, XtmSettings *settings) +{ + gboolean precision; + g_object_get (settings, "more-precision", &precision, NULL); + return g_strdup_printf ((precision) ? "%.2f" : "%.0f", value); +} + static void xtm_process_statusbar_set_property (GObject *object, guint property_id, const GValue *value, GParamSpec *pspec) { XtmProcessStatusbar *statusbar = XTM_PROCESS_STATUSBAR (object); gchar *text; + gchar *float_value; switch (property_id) { case PROP_CPU: statusbar->cpu = g_value_get_float (value); - text = g_strdup_printf (_("CPU: %.2f%%"), statusbar->cpu); + float_value = rounded_float_value (statusbar->cpu, statusbar->settings); + text = g_strdup_printf (_("CPU: %s%%"), float_value); gtk_label_set_text (GTK_LABEL (statusbar->label_cpu), text); + g_free (float_value); g_free (text); break; case PROP_MEMORY: statusbar->memory = g_value_get_float (value); - text = g_strdup_printf (_("Memory: %.2f%%"), statusbar->memory); + float_value = rounded_float_value (statusbar->memory, statusbar->settings); + text = g_strdup_printf (_("Memory: %s%%"), float_value); gtk_label_set_text (GTK_LABEL (statusbar->label_memory), text); + g_free (float_value); g_free (text); break; case PROP_SWAP: statusbar->swap = g_value_get_float (value); - text = g_strdup_printf (_("Swap: %.2f%%"), statusbar->swap); + float_value = rounded_float_value (statusbar->swap, statusbar->settings); + text = g_strdup_printf (_("Swap: %s%%"), float_value); gtk_label_set_text (GTK_LABEL (statusbar->label_swap), text); + g_free (float_value); g_free (text); break; diff --git a/src/task-manager.c b/src/task-manager.c index 2f3ca5e..5c1fad3 100644 --- a/src/task-manager.c +++ b/src/task-manager.c @@ -23,6 +23,12 @@ #include "task-manager.h" #include "process-tree-view.h" /* for the columns of the model */ +#include "settings.h" /* for the more-precision setting */ + + + +static XtmSettings *settings = NULL; +static gboolean more_precision = FALSE; @@ -36,6 +42,7 @@ struct _XtmTaskManager GObject parent; /**/ GtkTreeModel * model; + gboolean model_update_forced; GArray * tasks; guint owner_uid; gchar * owner_uid_name; @@ -54,6 +61,7 @@ G_DEFINE_TYPE (XtmTaskManager, xtm_task_manager, G_TYPE_OBJECT) static void xtm_task_manager_finalize (GObject *object); +static void setting_more_precision_changed (GObject *object, GParamSpec *pspec, XtmTaskManager *manager); static void model_add_task (GtkTreeModel *model, Task *task); static void model_update_tree_iter (GtkTreeModel *model, GtkTreeIter *iter, Task *task); static void model_update_task (GtkTreeModel *model, Task *task); @@ -75,6 +83,11 @@ xtm_task_manager_init (XtmTaskManager *manager) manager->tasks = g_array_new (FALSE, FALSE, sizeof (Task)); get_owner_uid (&(manager->owner_uid), &(manager->owner_uid_name)); manager->hostname = get_hostname (); + + /* Listen to changes on more-preicision and force an update on the whole model */ + settings = xtm_settings_get_default (); + g_object_get (settings, "more-precision", &more_precision, NULL); + g_signal_connect (settings, "notify::more-precision", G_CALLBACK (setting_more_precision_changed), manager); } static void @@ -86,6 +99,13 @@ xtm_task_manager_finalize (GObject *object) g_free (manager->hostname); } +static void +setting_more_precision_changed (GObject *object, GParamSpec *pspec, XtmTaskManager *manager) +{ + g_object_get (object, "more-precision", &more_precision, NULL); + manager->model_update_forced = TRUE; +} + static void _xtm_task_manager_set_model (XtmTaskManager *manager, GtkTreeModel *model) { @@ -135,11 +155,13 @@ static void model_update_tree_iter (GtkTreeModel *model, GtkTreeIter *iter, Task *task) { gchar vsz[64], rss[64], cpu[16]; + gchar value[14]; memory_human_size (task->vsz, vsz); memory_human_size (task->rss, rss); - // TODO make precision optional - g_snprintf (cpu, 16, _("%.2f%%"), task->cpu_user + task->cpu_system); + + g_snprintf (value, 14, (more_precision) ? "%.2f" : "%.0f", task->cpu_user + task->cpu_system); + g_snprintf (cpu, 16, _("%s%%"), value); gtk_list_store_set (GTK_LIST_STORE (model), iter, XTM_PTV_COLUMN_PPID, task->ppid, @@ -308,7 +330,8 @@ xtm_task_manager_update_model (XtmTaskManager *manager) found = TRUE; /* Update the model (with the rest) only if needed, this keeps the CPU cool */ - if (task->ppid != tasktmp->ppid + if (manager->model_update_forced + || task->ppid != tasktmp->ppid || g_strcmp0 (task->state, tasktmp->state) || task->cpu_user != tasktmp->cpu_user || task->cpu_system != tasktmp->cpu_system @@ -340,6 +363,7 @@ xtm_task_manager_update_model (XtmTaskManager *manager) } g_array_free (array, TRUE); + manager->model_update_forced = FALSE; return; }