Implement option “More precision”

This commit is contained in:
Mike Massonnet
2010-05-23 00:47:02 +02:00
parent 286f561088
commit f5e7344a8b
3 changed files with 57 additions and 6 deletions

View File

@@ -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);

View File

@@ -16,6 +16,7 @@
#include <gtk/gtk.h>
#include "process-statusbar.h"
#include "settings.h"
@@ -35,6 +36,8 @@ struct _XtmProcessStatusbar
{
GtkStatusbar parent;
/*<private>*/
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;

View File

@@ -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;
/*<private>*/
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;
}