[Xfce4-commits] <xfce4-taskmanager:master> Initial TaskManager class and make room for Swap

Mike Massonnet noreply at xfce.org
Wed May 5 09:08:05 CEST 2010


Updating branch refs/heads/master
         to 9a49bf015a418bc8b4447293b37b4742f615992e (commit)
       from fdaf8b1b49c43c1c2edd815ea936790bef986214 (commit)

commit 9a49bf015a418bc8b4447293b37b4742f615992e
Author: Mike Massonnet <mmassonnet at xfce.org>
Date:   Fri Apr 30 15:28:57 2010 +0200

    Initial TaskManager class and make room for Swap
    
    Added the TaskManager class to hold system information and tasks.
    Currently it provides username and hostname based on POSIX functions.
    The Swap is now part of the system information and is being displayed in
    the statusbar.

 src/Makefile.am         |    1 +
 src/main.c              |    5 ++
 src/process-statusbar.c |   23 ++++++-
 src/process-window.c    |   50 ++-------------
 src/process-window.h    |    2 +-
 src/task-manager.c      |  150 +++++++++++++++++++++++++++++++++++++++++++++++
 src/task-manager.h      |   49 +++++++++++++++
 7 files changed, 232 insertions(+), 48 deletions(-)

diff --git a/src/Makefile.am b/src/Makefile.am
index bcf3db5..5d8a03a 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -19,6 +19,7 @@ xfce4_taskmanager_SOURCES =						\
 	process-window.c		process-window.h		\
 	process-tree-view.c		process-tree-view.h		\
 	process-statusbar.c		process-statusbar.h		\
+	task-manager.c			task-manager.h			\
 	settings.c			settings.h			\
 	$(NULL)
 
diff --git a/src/main.c b/src/main.c
index 7f86fc6..36b1403 100644
--- a/src/main.c
+++ b/src/main.c
@@ -15,10 +15,12 @@
 #include <gtk/gtk.h>
 
 #include "process-window.h"
+#include "task-manager.h"
 
 int main (int argc, char *argv[])
 {
 	GtkWidget *window;
+	XtmTaskManager *task_manager;
 
 #ifdef ENABLE_NLS
 	bindtextdomain (GETTEXT_PACKAGE, PACKAGE_LOCALE_DIR);
@@ -31,6 +33,9 @@ int main (int argc, char *argv[])
 	window = xtm_process_window_new ();
 	gtk_widget_show (window);
 
+	task_manager = xtm_task_manager_new ();
+	g_message ("Running as %s on %s", xtm_task_manager_get_username (task_manager), xtm_task_manager_get_hostname (task_manager));
+
 	g_signal_connect (window, "destroy", G_CALLBACK (gtk_main_quit), NULL);
 
 	gtk_main ();
diff --git a/src/process-statusbar.c b/src/process-statusbar.c
index 6175607..6c14232 100644
--- a/src/process-statusbar.c
+++ b/src/process-statusbar.c
@@ -23,6 +23,7 @@ enum
 {
 	PROP_CPU = 1,
 	PROP_MEMORY,
+	PROP_SWAP,
 	PROP_NUM_PROCESSES,
 };
 typedef struct _XtmProcessStatusbarClass XtmProcessStatusbarClass;
@@ -37,9 +38,11 @@ struct _XtmProcessStatusbar
 	GtkWidget *		label_num_processes;
 	GtkWidget *		label_cpu;
 	GtkWidget *		label_memory;
+	GtkWidget *		label_swap;
 
 	gushort			cpu;
-	guint64			memory;
+	gushort			memory;
+	gushort			swap;
 	guint			num_processes;
 };
 G_DEFINE_TYPE (XtmProcessStatusbar, xtm_process_statusbar, GTK_TYPE_STATUSBAR)
@@ -57,7 +60,9 @@ xtm_process_statusbar_class_init (XtmProcessStatusbarClass *klass)
 	g_object_class_install_property (class, PROP_CPU,
 		g_param_spec_uint ("cpu", "CPU", "CPU usage", 0, 100, 0, G_PARAM_CONSTRUCT|G_PARAM_WRITABLE));
 	g_object_class_install_property (class, PROP_MEMORY,
-		g_param_spec_uint64 ("memory", "Memory", "Memory usage", 0, G_MAXUINT64, 0, G_PARAM_CONSTRUCT|G_PARAM_WRITABLE));
+		g_param_spec_uint ("memory", "Memory", "Memory usage", 0, 100, 0, G_PARAM_CONSTRUCT|G_PARAM_WRITABLE));
+	g_object_class_install_property (class, PROP_SWAP,
+		g_param_spec_uint ("swap", "Swap", "Swap usage", 0, 100, 0, G_PARAM_CONSTRUCT|G_PARAM_WRITABLE));
 	g_object_class_install_property (class, PROP_NUM_PROCESSES,
 		g_param_spec_uint ("num-processes", "NumProcesses", "Number of processes", 0, G_MAXUINT, 0, G_PARAM_CONSTRUCT|G_PARAM_WRITABLE));
 }
@@ -85,7 +90,7 @@ xtm_process_statusbar_init (XtmProcessStatusbar *statusbar)
 	}
 #endif
 
-	hbox = gtk_hbox_new (FALSE, 24);
+	hbox = gtk_hbox_new (FALSE, 16);
 	gtk_box_pack_start (GTK_BOX (area), hbox, TRUE, TRUE, 0);
 
 	statusbar->label_num_processes = gtk_label_new (NULL);
@@ -97,6 +102,9 @@ xtm_process_statusbar_init (XtmProcessStatusbar *statusbar)
 	statusbar->label_memory = gtk_label_new (NULL);
 	gtk_box_pack_start (GTK_BOX (hbox), statusbar->label_memory, FALSE, FALSE, 0);
 
+	statusbar->label_swap = gtk_label_new (NULL);
+	gtk_box_pack_start (GTK_BOX (hbox), statusbar->label_swap, FALSE, FALSE, 0);
+
 	gtk_widget_show_all (hbox);
 }
 
@@ -116,12 +124,19 @@ xtm_process_statusbar_set_property (GObject *object, guint property_id, const GV
 		break;
 
 		case PROP_MEMORY:
-		statusbar->memory = g_value_get_uint64 (value);
+		statusbar->memory = g_value_get_uint (value);
 		text = g_strdup_printf (_("Memory: %d%%"), statusbar->memory);
 		gtk_label_set_text (GTK_LABEL (statusbar->label_memory), text);
 		g_free (text);
 		break;
 
+		case PROP_SWAP:
+		statusbar->swap = g_value_get_uint (value);
+		text = g_strdup_printf (_("Swap: %d%%"), statusbar->swap);
+		gtk_label_set_text (GTK_LABEL (statusbar->label_swap), text);
+		g_free (text);
+		break;
+
 		case PROP_NUM_PROCESSES:
 		statusbar->num_processes = g_value_get_uint (value);
 		text = g_strdup_printf (_("Processes: %d"), statusbar->num_processes);
diff --git a/src/process-window.c b/src/process-window.c
index 57155fd..9ffd722 100644
--- a/src/process-window.c
+++ b/src/process-window.c
@@ -75,12 +75,6 @@ xtm_process_window_class_init (XtmProcessWindowClass *klass)
 
 	class->get_property = xtm_process_window_get_property;
 	class->set_property = xtm_process_window_set_property;
-	g_object_class_install_property (class, PROP_CPU,
-		g_param_spec_uint ("cpu", "CPU", "CPU usage", 0, 100, 0, G_PARAM_CONSTRUCT|G_PARAM_WRITABLE));
-	g_object_class_install_property (class, PROP_MEMORY,
-		g_param_spec_uint64 ("memory", "Memory", "Memory usage", 0, G_MAXUINT64, 0, G_PARAM_CONSTRUCT|G_PARAM_WRITABLE));
-	g_object_class_install_property (class, PROP_NUM_PROCESSES,
-		g_param_spec_uint ("num-processes", "NumProcesses", "Number of processes", 0, G_MAXUINT, 0, G_PARAM_CONSTRUCT|G_PARAM_WRITABLE));
 }
 
 static void
@@ -171,23 +165,6 @@ xtm_process_window_set_property (GObject *object, guint property_id, const GValu
 
 	switch (property_id)
 	{
-		case PROP_CPU:
-		priv->cpu = g_value_get_uint (value);
-		// TODO update_cpu_monitor ();
-		g_object_set (priv->statusbar, "cpu", priv->cpu, NULL);
-		break;
-
-		case PROP_MEMORY:
-		priv->memory = g_value_get_uint64 (value);
-		// TODO update_memory_monitor ();
-		g_object_set (priv->statusbar, "memory", priv->memory, NULL);
-		break;
-
-		case PROP_NUM_PROCESSES:
-		priv->num_processes = g_value_get_uint (value);
-		g_object_set (priv->statusbar, "num_processes", priv->num_processes, NULL);
-		break;
-
 		default:
 		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
 		break;
@@ -225,11 +202,8 @@ execute_command (const gchar *command)
 static void
 menu_execute_append_item (GtkMenu *menu, gchar *title, gchar *command, gchar *icon_name)
 {
-	GtkWidget *mi;
-	GtkWidget *image;
-
-	image = gtk_image_new_from_icon_name (icon_name, GTK_ICON_SIZE_MENU);
-	mi = gtk_image_menu_item_new_with_label (title);
+	GtkWidget *image = gtk_image_new_from_icon_name (icon_name, GTK_ICON_SIZE_MENU);
+	GtkWidget *mi = gtk_image_menu_item_new_with_label (title);
 	gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (mi), image);
 	gtk_menu_shell_append (GTK_MENU_SHELL (menu), mi);
 	g_signal_connect_swapped (mi, "activate", G_CALLBACK (execute_command), command);
@@ -258,11 +232,8 @@ show_menu_execute_task (XtmProcessWindow *window)
 static void
 preferences_toggled (GtkCheckMenuItem *mi, XtmSettings *settings)
 {
-	gchar *setting_name;
-	gboolean active;
-
-	active = gtk_check_menu_item_get_active (mi);
-	setting_name = g_object_get_data (G_OBJECT (mi), "setting-name");
+	gboolean active = gtk_check_menu_item_get_active (mi);
+	gchar *setting_name = g_object_get_data (G_OBJECT (mi), "setting-name");
 	g_object_set (settings, setting_name, active, NULL);
 }
 
@@ -385,14 +356,6 @@ xtm_process_window_hide (GtkWidget *widget)
 	gtk_widget_hide (XTM_PROCESS_WINDOW (widget)->priv->window);
 }
 
-void
-xtm_process_window_set_model (XtmProcessWindow *window, GtkTreeModel *model)
-{
-	g_return_if_fail (G_LIKELY (XTM_IS_PROCESS_WINDOW (window)));
-	g_return_if_fail (G_LIKELY (GTK_IS_TREE_VIEW (window->priv->treeview)));
-	gtk_tree_view_set_model (GTK_TREE_VIEW (window->priv->treeview), model);
-}
-
 GtkTreeModel *
 xtm_process_window_get_model (XtmProcessWindow *window)
 {
@@ -402,10 +365,11 @@ xtm_process_window_get_model (XtmProcessWindow *window)
 }
 
 void
-xtm_process_window_set_system_info (XtmProcessWindow *window, guint num_processes, gushort cpu, guint64 memory)
+xtm_process_window_set_system_info (XtmProcessWindow *window, guint num_processes, gushort cpu, gushort memory, gushort swap)
 {
 	g_return_if_fail (G_LIKELY (XTM_IS_PROCESS_WINDOW (window)));
 	g_return_if_fail (G_LIKELY (GTK_IS_STATUSBAR (window->priv->statusbar)));
-	g_object_set (window, "num-processes", num_processes, "cpu", cpu, "memory", memory, NULL);
+	g_object_set (window->priv->statusbar, "num-processes", num_processes, "cpu", cpu, "memory", memory, "swap", swap, NULL);
+	// TODO update cpu/memory monitors
 }
 
diff --git a/src/process-window.h b/src/process-window.h
index 8555d92..4e3cee2 100644
--- a/src/process-window.h
+++ b/src/process-window.h
@@ -29,6 +29,6 @@ typedef struct _XtmProcessWindow XtmProcessWindow;
 GType		xtm_process_window_get_type			(void);
 GtkWidget *	xtm_process_window_new				();
 GtkTreeModel *	xtm_process_window_get_model			(XtmProcessWindow *window);
-void		xtm_process_window_set_system_info		(XtmProcessWindow *window, guint num_processes, gushort cpu, guint64 mem);
+void		xtm_process_window_set_system_info		(XtmProcessWindow *window, guint num_processes, gushort cpu, gushort memory, gushort swap);
 
 #endif /* !PROCESS_WINDOW_H */
diff --git a/src/task-manager.c b/src/task-manager.c
new file mode 100644
index 0000000..36e5474
--- /dev/null
+++ b/src/task-manager.c
@@ -0,0 +1,150 @@
+/*
+ * Copyright (c) 2010 Mike Massonnet, <mmassonnet at xfce.org>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or (at
+ * your option) any later version.
+ */
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <unistd.h>
+#include <sys/types.h>
+#include <pwd.h>
+
+#include <glib-object.h>
+#include <glib/gi18n.h>
+#include <gtk/gtk.h>
+
+#include "task-manager.h"
+
+
+
+typedef struct _XtmTaskManagerClass XtmTaskManagerClass;
+struct _XtmTaskManagerClass
+{
+	GObjectClass		parent_class;
+};
+struct _XtmTaskManager
+{
+	GObject			parent;
+	/*<private>*/
+	GArray *		tasks;
+	guint			owner_uid;
+	gchar *			owner_uid_name;
+	gchar *			hostname;
+	gushort			cpu_count;
+	gushort			cpu_user;
+	gushort			cpu_system;
+	guint64			memory_total;
+	guint64			memory_free;
+	guint64			memory_cache;
+	guint64			memory_buffers;
+	guint64			swap_total;
+	guint64			swap_free;
+};
+G_DEFINE_TYPE (XtmTaskManager, xtm_task_manager, G_TYPE_OBJECT)
+
+static void	xtm_task_manager_finalize			(GObject *object);
+
+static void	get_owner_uid					(guint *owner_uid, gchar **owner_uid_name);
+static gchar *	get_hostname					();
+
+
+
+static void
+xtm_task_manager_class_init (XtmTaskManagerClass *klass)
+{
+	GObjectClass *class = G_OBJECT_CLASS (klass);
+	xtm_task_manager_parent_class = g_type_class_peek_parent (klass);
+	class->finalize = xtm_task_manager_finalize;
+}
+
+static void
+xtm_task_manager_init (XtmTaskManager *manager)
+{
+	get_owner_uid (&(manager->owner_uid), &(manager->owner_uid_name));
+	manager->hostname = get_hostname ();
+}
+
+static void
+xtm_task_manager_finalize (GObject *object)
+{
+	XtmTaskManager *manager = XTM_TASK_MANAGER (object);
+	g_free (manager->owner_uid_name);
+	g_free (manager->hostname);
+}
+
+static void
+get_owner_uid (guint *owner_uid, gchar **owner_uid_name)
+{
+	uid_t uid;
+	struct passwd *pw;
+	gchar *username = NULL;
+
+	uid = getuid ();
+	pw = getpwuid (uid);
+
+	username = g_strdup ((pw != NULL) ? pw->pw_name : "nobody");
+
+	*owner_uid = (guint) uid;
+	*owner_uid_name = username;
+}
+
+static gchar *
+get_hostname ()
+{
+#ifndef HOST_NAME_MAX
+#define HOST_NAME_MAX 255
+#endif
+	char hostname[HOST_NAME_MAX];
+	if (gethostname (hostname, HOST_NAME_MAX))
+		return g_strdup ("(unknown)");
+	return g_strdup_printf ("%s", hostname);
+}
+
+
+
+XtmTaskManager *
+xtm_task_manager_new ()
+{
+	return g_object_new (XTM_TYPE_TASK_MANAGER, NULL);
+}
+
+const gchar *
+xtm_task_manager_get_username (XtmTaskManager *manager)
+{
+	g_return_val_if_fail (G_LIKELY (XTM_IS_TASK_MANAGER (manager)), NULL);
+	return manager->owner_uid_name;
+}
+
+const gchar *
+xtm_task_manager_get_hostname (XtmTaskManager *manager)
+{
+	g_return_val_if_fail (G_LIKELY (XTM_IS_TASK_MANAGER (manager)), NULL);
+	return manager->hostname;
+}
+
+GArray *
+xtm_task_manager_get_tasklist (XtmTaskManager *manager)
+{
+}
+
+void
+xtm_task_manager_get_system_info (XtmTaskManager *manager, guint *num_processes, gushort *cpu, gushort *memory, gushort *swap)
+{
+}
+
+void
+xtm_task_manager_send_signal_to_pid (XtmTaskManager *manager)
+{
+}
+
+void
+xtm_task_manager_set_priority_to_pid (XtmTaskManager *manager)
+{
+}
+
diff --git a/src/task-manager.h b/src/task-manager.h
new file mode 100644
index 0000000..df3a14f
--- /dev/null
+++ b/src/task-manager.h
@@ -0,0 +1,49 @@
+/*
+ * Copyright (c) 2010 Mike Massonnet, <mmassonnet at xfce.org>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ */
+
+#ifndef TASK_MANAGER_H
+#define TASK_MANAGER_H
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <glib-object.h>
+
+typedef struct _Task Task;
+struct _Task
+{
+	guint uid;
+	gchar uid_name[64];
+	guint pid;
+	guint ppid;
+	gchar program_name[64];
+	gchar full_cmdline[255];
+	gchar state[16];
+	gushort cpu;
+	guint64 memory_vsz;
+	guint64 memory_rss;
+	gushort priority;
+};
+
+#define XTM_TYPE_TASK_MANAGER			(xtm_task_manager_get_type ())
+#define XTM_TASK_MANAGER(obj)			(G_TYPE_CHECK_INSTANCE_CAST ((obj), XTM_TYPE_TASK_MANAGER, XtmTaskManager))
+#define XTM_TASK_MANAGER_CLASS(klass)		(G_TYPE_CHECK_CLASS_CAST ((klass), XTM_TYPE_TASK_MANAGER, XtmTaskManagerClass))
+#define XTM_IS_TASK_MANAGER(obj)		(G_TYPE_CHECK_INSTANCE_TYPE ((obj), XTM_TYPE_TASK_MANAGER))
+#define XTM_IS_TASK_MANAGER_CLASS(klass)	(G_TYPE_CHECK_CLASS_TYPE ((klass), XTM_TYPE_TASK_MANAGER))
+#define XTM_TASK_MANAGER_GET_CLASS(obj)		(G_TYPE_INSTANCE_GET_CLASS ((obj), XTM_TYPE_TASK_MANAGER, XtmTaskManagerClass))
+
+typedef struct _XtmTaskManager XtmTaskManager;
+
+GType			xtm_task_manager_get_type			(void);
+XtmTaskManager *	xtm_task_manager_new				();
+const gchar *		xtm_task_manager_get_username			(XtmTaskManager *manager);
+const gchar *		xtm_task_manager_get_hostname			(XtmTaskManager *manager);
+
+#endif /* !TASK_MANAGER_H */



More information about the Xfce4-commits mailing list